| 1 | package Biblio::RFID::Reader::CPRM02; | ||||||||||||
| 2 | |||||||||||||
| 3 | =head1 NAME | ||||||||||||
| 4 | |||||||||||||
| 5 | Biblio::RFID::Reader::CPRM02 - support for CPR-M02 RFID reader | ||||||||||||
| 6 | |||||||||||||
| 7 | =head1 DESCRIPTION | ||||||||||||
| 8 | |||||||||||||
| 9 | This module implements serial protocol over usb/serial adapter with CPR-M02 | ||||||||||||
| 10 | reader as described in document C<H20800-16e-ID-B.pdf> | ||||||||||||
| 11 | |||||||||||||
| 12 | =cut | ||||||||||||
| 13 | |||||||||||||
| 14 | 3 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | 3 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | |||||||||||||
| 17 | 3 | ▶ | use base 'Biblio::RFID::Reader::Serial'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | 3 | ▶ | use Biblio::RFID; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | |||||||||||||
| 20 | 3 | ▶ | use Time::HiRes; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 21 | 3 | ▶ | use Data::Dump qw(dump); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | |||||||||||||
| 23 | 3 | my $debug = 1; | |||||||||||
| 24 | |||||||||||||
| 25 | sub serial_settings {{ | ||||||||||||
| 26 | 1 | ▶ | device => "/dev/ttyUSB0", | ||||||||||
|
Subroutine
serial_settings: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 27 | baudrate => "38400", | ||||||||||||
| 28 | databits => "8", | ||||||||||||
| 29 | parity => "even", | ||||||||||||
| 30 | stopbits => "1", | ||||||||||||
| 31 | handshake => "none", | ||||||||||||
| 32 | }} | ||||||||||||
| 33 | |||||||||||||
| 34 | sub cpr_m02_checksum { | ||||||||||||
| 35 | 0 | ▶ | my $data = shift; | ||||||||||
|
Subroutine
cpr_m02_checksum: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||
| 36 | |||||||||||||
| 37 | 0 | my $preset = 0xffff; | |||||||||||
| 38 | 0 | my $polynom = 0x8408; | |||||||||||
| 39 | |||||||||||||
| 40 | 0 | my $crc = $preset; | |||||||||||
| 41 | 0 | foreach my $i ( 0 .. length($data) - 1 ) { | |||||||||||
| 42 | 0 | $crc ^= ord(substr($data,$i,1)); | |||||||||||
| 43 | 0 | for my $j ( 0 .. 7 ) { | |||||||||||
| 44 | 0 | ▶ | if ( $crc & 0x0001 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 45 | 0 | $crc = ( $crc >> 1 ) ^ $polynom; | |||||||||||
| 46 | } else { | ||||||||||||
| 47 | 0 | $crc = $crc >> 1; | |||||||||||
| 48 | } | ||||||||||||
| 49 | } | ||||||||||||
| 50 | # warn sprintf('%d %04x', $i, $crc & 0xffff); | ||||||||||||
| 51 | } | ||||||||||||
| 52 | |||||||||||||
| 53 | 0 | return pack('v', $crc); | |||||||||||
| 54 | } | ||||||||||||
| 55 | |||||||||||||
| 56 | sub wait_device { | ||||||||||||
| 57 | 0 | ▶ | Time::HiRes::sleep 0.010; | ||||||||||
|
Subroutine
wait_device: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 58 | } | ||||||||||||
| 59 | |||||||||||||
| 60 | 3 | our $port; | |||||||||||
| 61 | |||||||||||||
| 62 | sub cpr { | ||||||||||||
| 63 | 0 | ▶ | my ( $hex, $description, $coderef ) = @_; | ||||||||||
|
Subroutine
cpr: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||
| 64 | 0 | my $bytes = hex2bytes($hex); | |||||||||||
| 65 | 0 | my $len = pack( 'c', length( $bytes ) + 3 ); | |||||||||||
| 66 | 0 | my $send = $len . $bytes; | |||||||||||
| 67 | 0 | my $checksum = cpr_m02_checksum($send); | |||||||||||
| 68 | 0 | $send .= $checksum; | |||||||||||
| 69 | |||||||||||||
| 70 | 0 | warn "##>> ", as_hex( $send ), "\t\t[$description]\n"; | |||||||||||
| 71 | 0 | $port->write( $send ); | |||||||||||
| 72 | |||||||||||||
| 73 | 0 | wait_device; | |||||||||||
| 74 | |||||||||||||
| 75 | 0 | my $r_len = $port->read(1); | |||||||||||
| 76 | |||||||||||||
| 77 | 0 | my $count = 100; | |||||||||||
| 78 | 0 | ▶ | while ( ! $r_len ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 79 | 0 | ▶ | if ( $count-- == 0 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 80 | 0 | warn "no response from device"; | |||||||||||
| 81 | 0 | return; | |||||||||||
| 82 | } | ||||||||||||
| 83 | 0 | wait_device; | |||||||||||
| 84 | 0 | $r_len = $port->read(1); | |||||||||||
| 85 | } | ||||||||||||
| 86 | |||||||||||||
| 87 | 0 | wait_device; | |||||||||||
| 88 | |||||||||||||
| 89 | 0 | my $data_len = ord($r_len) - 1; | |||||||||||
| 90 | 0 | my $data = $port->read( $data_len ); | |||||||||||
| 91 | 0 | warn "##<< ", as_hex( $r_len . $data ),"\n"; | |||||||||||
| 92 | |||||||||||||
| 93 | 0 | wait_device; | |||||||||||
| 94 | |||||||||||||
| 95 | 0 | ▶ | $coderef->( $data ) if $coderef; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 96 | |||||||||||||
| 97 | } | ||||||||||||
| 98 | |||||||||||||
| 99 | # FF = COM-ADDR any | ||||||||||||
| 100 | |||||||||||||
| 101 | sub init { | ||||||||||||
| 102 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
init: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 103 | |||||||||||||
| 104 | 0 | $port = $self->port; | |||||||||||
| 105 | |||||||||||||
| 106 | 0 | cpr( 'FF 52 00', 'Boud Rate Detection' ); | |||||||||||
| 107 | |||||||||||||
| 108 | 0 | cpr( 'FF 65', 'Get Software Version' ); | |||||||||||
| 109 | |||||||||||||
| 110 | 0 | cpr( 'FF 66 00', 'Get Reader Info - General hard and firware' ); | |||||||||||
| 111 | |||||||||||||
| 112 | 0 | cpr( 'FF 69', 'RF Reset' ); | |||||||||||
| 113 | |||||||||||||
| 114 | 0 | return 1; | |||||||||||
| 115 | } | ||||||||||||
| 116 | |||||||||||||
| 117 | |||||||||||||
| 118 | sub inventory { | ||||||||||||
| 119 | |||||||||||||
| 120 | 0 | ▶ | my @tags; | ||||||||||
|
Subroutine
inventory: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 121 | |||||||||||||
| 122 | cpr( 'FF B0 01 00', 'ISO - Inventory', sub { | ||||||||||||
| 123 | 0 | ▶ | my $data = shift; | ||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||
| 124 | 0 | ▶ | if (length($data) < 5 + 2 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 125 | 0 | warn "# no tags in range\n"; | |||||||||||
| 126 | 0 | return; | |||||||||||
| 127 | } | ||||||||||||
| 128 | |||||||||||||
| 129 | 0 | my $data_sets = ord(substr($data,3,1)); | |||||||||||
| 130 | 0 | $data = substr($data,4); | |||||||||||
| 131 | 0 | foreach ( 1 .. $data_sets ) { | |||||||||||
| 132 | 0 | my $tr_type = substr($data,0,1); | |||||||||||
| 133 | 0 | ▶ | die "FIXME only TR-TYPE=3 ISO 15693 supported" unless $tr_type eq "\x03"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 134 | 0 | my $dsfid = substr($data,1,1); | |||||||||||
| 135 | 0 | my $uid = substr($data,2,8); | |||||||||||
| 136 | 0 | $data = substr($data,10); | |||||||||||
| 137 | 0 | warn "# TAG $_ ",as_hex( $tr_type, $dsfid, $uid ),$/; | |||||||||||
| 138 | 0 | push @tags, hex_tag $uid; | |||||||||||
| 139 | |||||||||||||
| 140 | } | ||||||||||||
| 141 | 0 | }); | |||||||||||
| 142 | |||||||||||||
| 143 | 0 | warn "# tags ",dump(@tags),$/; | |||||||||||
| 144 | 0 | return @tags; | |||||||||||
| 145 | } | ||||||||||||
| 146 | |||||||||||||
| 147 | |||||||||||||
| 148 | sub _get_system_info { | ||||||||||||
| 149 | 0 | ▶ | my $tag = shift; | ||||||||||
|
Subroutine
_get_system_info: not called CC=1 SCAR=6.9
| |||||||||||||
| 150 | |||||||||||||
| 151 | 0 | my $info; | |||||||||||
| 152 | |||||||||||||
| 153 | cpr( "FF B0 2B 01 $tag", "Get System Information $tag", sub { | ||||||||||||
| 154 | 0 | ▶ | my $data = shift; | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 155 | |||||||||||||
| 156 | 0 | warn "# data ",as_hex($data); | |||||||||||
| 157 | |||||||||||||
| 158 | 0 | ▶ | return if length($data) < 17; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 159 | |||||||||||||
| 160 | 0 | $info = { | |||||||||||
| 161 | DSFID => substr($data,5-2,1), | ||||||||||||
| 162 | UID => substr($data,6-2,8), | ||||||||||||
| 163 | AFI => substr($data,14-2,1), | ||||||||||||
| 164 | MEM => substr($data,15-2,1), | ||||||||||||
| 165 | SIZE => substr($data,16-2,1), | ||||||||||||
| 166 | IC_REF => substr($data,17-2,1), | ||||||||||||
| 167 | }; | ||||||||||||
| 168 | |||||||||||||
| 169 | 0 | }); | |||||||||||
| 170 | |||||||||||||
| 171 | 0 | warn "# _get_system_info $tag ",dump( $info ); | |||||||||||
| 172 | |||||||||||||
| 173 | 0 | return $info; | |||||||||||
| 174 | } | ||||||||||||
| 175 | |||||||||||||
| 176 | |||||||||||||
| 177 | sub read_blocks { | ||||||||||||
| 178 | 0 | ▶ | my $tag = shift; | ||||||||||
|
Subroutine
read_blocks: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||
| 179 | 0 | ▶ | $tag = shift if ref $tag; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 180 | |||||||||||||
| 181 | 0 | my $info = _get_system_info $tag; | |||||||||||
| 182 | |||||||||||||
| 183 | 0 | ▶ | return unless $info->{SIZE}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 184 | |||||||||||||
| 185 | 0 | my $max_block = ord($info->{SIZE}); | |||||||||||
| 186 | |||||||||||||
| 187 | 0 | my $tag_blocks; | |||||||||||
| 188 | |||||||||||||
| 189 | 0 | my $block = 0; | |||||||||||
| 190 | 0 | ▶ | while ( $block < $max_block ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 191 | cpr( sprintf("FF B0 23 01 $tag %02x 04", $block), "Read Multiple Blocks $block", sub { | ||||||||||||
| 192 | 0 | ▶ | my $data = shift; | ||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||
| 193 | |||||||||||||
| 194 | 0 | my $DB_N = ord substr($data,5-2,1); | |||||||||||
| 195 | 0 | my $DB_SIZE = ord substr($data,6-2,1); | |||||||||||
| 196 | |||||||||||||
| 197 | 0 | $data = substr($data,7-2,-2); | |||||||||||
| 198 | # warn "# DB N: $DB_N SIZE: $DB_SIZE ", as_hex( $data ), " transponder_data: [$transponder_data] ",length($transponder_data),"\n"; | ||||||||||||
| 199 | 0 | foreach my $n ( 1 .. $DB_N ) { | |||||||||||
| 200 | 0 | my $sec = ord(substr($data,0,1)); | |||||||||||
| 201 | 0 | my $db = substr($data,1,$DB_SIZE); | |||||||||||
| 202 | 0 | ▶ | warn "## block $n ",dump( $sec, $db ) if $debug; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 203 | 0 | $tag_blocks->{$tag}->[$block+$n-1] = reverse split(//,$db); | |||||||||||
| 204 | 0 | $data = substr($data, $DB_SIZE + 1); | |||||||||||
| 205 | } | ||||||||||||
| 206 | 0 | }); | |||||||||||
| 207 | 0 | $block += 4; | |||||||||||
| 208 | } | ||||||||||||
| 209 | |||||||||||||
| 210 | 0 | warn "# tag_blocks ",dump($tag_blocks),$/; | |||||||||||
| 211 | 0 | return $tag_blocks; | |||||||||||
| 212 | } | ||||||||||||
| 213 | |||||||||||||
| 214 | |||||||||||||
| 215 | sub write_blocks { | ||||||||||||
| 216 | 0 | ▶ | my $tag = shift; | ||||||||||
|
Subroutine
write_blocks: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||
| 217 | 0 | ▶ | $tag = shift if ref $tag; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 218 | |||||||||||||
| 219 | 0 | my $data = shift; | |||||||||||
| 220 | 0 | ▶ | $data = join('', @$data) if ref $data eq 'ARRAY'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 221 | |||||||||||||
| 222 | 0 | my $DB_ADR = 0; # start at first block | |||||||||||
| 223 | 0 | my $DB_SIZE = 4; # bytes in one block FIXME this should be read from transponder and not hard-coded | |||||||||||
| 224 | 0 | ▶ | if ( my $padding = length($data) % $DB_SIZE ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 225 | 0 | warn "WARNING: data block not padded to $DB_SIZE bytes"; | |||||||||||
| 226 | 0 | $data .= "\x00" x $padding; | |||||||||||
| 227 | } | ||||||||||||
| 228 | 0 | my $DB_N = length($data) / $DB_SIZE; | |||||||||||
| 229 | |||||||||||||
| 230 | 0 | my $send_data; | |||||||||||
| 231 | 0 | foreach my $block ( 0 .. $DB_N ) { | |||||||||||
| 232 | 0 | $send_data .= reverse split(//, substr( $data, $block * $DB_SIZE, $DB_SIZE ) ); | |||||||||||
| 233 | } | ||||||||||||
| 234 | |||||||||||||
| 235 | cpr( sprintf("FF B0 24 01 $tag %02x %02x %02x %s", $DB_ADR, $DB_N, $DB_SIZE, as_hex($send_data)), "Write Multiple Blocks $tag", sub { | ||||||||||||
| 236 | 0 | ▶ | my $data = shift; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 237 | 0 | warn dump( $data ); | |||||||||||
| 238 | 0 | }); | |||||||||||
| 239 | |||||||||||||
| 240 | } | ||||||||||||
| 241 | |||||||||||||
| 242 | sub read_afi { | ||||||||||||
| 243 | 0 | ▶ | my $tag = shift; | ||||||||||
|
Subroutine
read_afi: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 244 | 0 | ▶ | $tag = shift if ref $tag; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 245 | |||||||||||||
| 246 | 0 | my $info = _get_system_info $tag; | |||||||||||
| 247 | 0 | ▶ | return $info->{AFI} || warn "no AFI for $tag in ",dump($info); | ||||||||||
|
Condition0%0/2
$info->{'AFI'} || warn("no AFI for $tag in ", Biblio::RFID::Reader::CPRM02::dump($info))
MC/DC0%0/1
$info->{'AFI'} || warn("no AFI for $tag in ", Biblio::RFID::Reader::CPRM02::dump($info))
$info->{'AFI'}
Truth table0%0/2
A
A= $info->{'AFI'}
| |||||||||||||
| 248 | |||||||||||||
| 249 | } | ||||||||||||
| 250 | |||||||||||||
| 251 | sub write_afi { | ||||||||||||
| 252 | 0 | ▶ | my $tag = shift; | ||||||||||
|
Subroutine
write_afi: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 253 | 0 | ▶ | $tag = shift if ref $tag; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 254 | |||||||||||||
| 255 | 0 | ▶ | my $afi = shift || die "no afi?"; | ||||||||||
|
Condition0%0/2
shift() || die('no afi?')
MC/DC0%0/1
shift() || die('no afi?')
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||
| 256 | 0 | $afi = as_hex $afi; | |||||||||||
| 257 | |||||||||||||
| 258 | cpr( "FF B0 27 01 $tag $afi", "Write AFI $tag $afi", sub { | ||||||||||||
| 259 | 0 | ▶ | my $data = shift; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 260 | 0 | warn "## write_afi $tag got ",as_hex($data); | |||||||||||
| 261 | 0 | }); | |||||||||||
| 262 | |||||||||||||
| 263 | } | ||||||||||||
| 264 | |||||||||||||
| 265 | 3 | 1 | |||||||||||