1 package RFID::Biblio::CPRM02;
5 RFID::Biblio::CPRM02 - support for CPR-M02 RFID reader
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>
17 use base 'RFID::Biblio';
21 use Data::Dump qw(dump);
25 sub serial_settings {{
26 device => "/dev/ttyUSB0",
34 sub cpr_m02_checksum {
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;
50 # warn sprintf('%d %04x', $i, $crc & 0xffff);
53 return pack('v', $crc);
57 Time::HiRes::sleep 0.010;
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);
70 warn "##>> ", as_hex( $send ), "\t\t[$description]\n";
71 $port->write( $send );
75 my $r_len = $port->read(1);
79 if ( $count-- == 0 ) {
80 warn "no response from device";
84 $r_len = $port->read(1);
89 my $data_len = ord($r_len) - 1;
90 my $data = $port->read( $data_len );
91 warn "##<< ", as_hex( $r_len . $data ),"\n";
95 $coderef->( $data ) if $coderef;
106 cpr( 'FF 52 00', 'Boud Rate Detection' );
108 cpr( 'FF 65', 'Get Software Version' );
110 cpr( 'FF 66 00', 'Get Reader Info - General hard and firware' );
112 cpr( 'FF 69', 'RF Reset' );
118 $tag = shift if ref $tag;
122 cpr( "FF B0 2B 01 $tag", "Get System Information $tag", sub {
125 warn "# data ",as_hex($data);
127 my $DSFID = substr($data,5-2,1);
128 my $UID = substr($data,6-2,8);
129 my $AFI = substr($data,14-2,1);
130 my $MEM = substr($data,15-2,1);
131 my $SIZE = substr($data,16-2,1);
132 my $IC_REF = substr($data,17-2,1);
134 warn "# split ",as_hex( $DSFID, $UID, $AFI, $MEM, $SIZE, $IC_REF );
136 $max_block = ord($SIZE);
142 while ( $block < $max_block ) {
143 cpr( sprintf("FF B0 23 01 $tag %02x 04", $block), "Read Multiple Blocks $block", sub {
146 my $DB_N = ord substr($data,5-2,1);
147 my $DB_SIZE = ord substr($data,6-2,1);
149 $data = substr($data,7-2,-2);
150 # warn "# DB N: $DB_N SIZE: $DB_SIZE ", as_hex( $data ), " transponder_data: [$transponder_data] ",length($transponder_data),"\n";
151 foreach my $n ( 1 .. $DB_N ) {
152 my $sec = ord(substr($data,0,1));
153 my $db = substr($data,1,$DB_SIZE);
154 warn "## block $n ",dump( $sec, $db ) if $debug;
155 $tag_blocks->{$tag}->[$block+$n-1] = reverse split(//,$db);
156 $data = substr($data, $DB_SIZE + 1);
162 warn "# tag_blocks ",dump($tag_blocks),$/;
172 cpr( 'FF B0 01 00', 'ISO - Inventory', sub {
174 if (length($data) < 5 + 2 ) {
175 warn "# no tags in range\n";
179 my $data_sets = ord(substr($data,3,1));
180 $data = substr($data,4);
181 foreach ( 1 .. $data_sets ) {
182 my $tr_type = substr($data,0,1);
183 die "FIXME only TR-TYPE=3 ISO 15693 supported" unless $tr_type eq "\x03";
184 my $dsfid = substr($data,1,1);
185 my $uid = substr($data,2,8);
186 $data = substr($data,10);
187 warn "# TAG $_ ",as_hex( $tr_type, $dsfid, $uid ),$/;
188 push @tags, hex_tag $uid;
193 warn "# tags ",dump(@tags),$/;