blib/lib/Biblio/RFID/Reader/CPRM02.pm untested

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