--- 3m-810.pl 2008/09/28 14:05:43 2 +++ 3m-810.pl 2009/06/04 13:52:10 42 @@ -6,6 +6,53 @@ use Data::Dump qw/dump/; use Carp qw/confess/; +use Getopt::Long; +use File::Slurp; + +use IO::Socket::INET; + +my $meteor_server = '192.168.1.13:4671'; +my $meteor_fh; + +sub meteor { + my @a = @_; + push @a, scalar localtime() if $a[0] =~ m{^info}; + + if ( ! defined $meteor_fh ) { + if ( $meteor_fh = + IO::Socket::INET->new( + PeerAddr => $meteor_server, + Timeout => 1, + ) + ) { + warn "# meteor connected to $meteor_server"; + } else { + warn "can't connect to meteor $meteor_server: $!"; + $meteor_fh = 0; + } + } + + if ( $meteor_fh ) { + warn ">> meteor ",dump( @a ); + print $meteor_fh "ADDMESSAGE test ",join('|',@a),"\n" + } +} + +my $debug = 0; + +my $device = "/dev/ttyUSB0"; +my $baudrate = "19200"; +my $databits = "8"; +my $parity = "none"; +my $stopbits = "1"; +my $handshake = "none"; + +my $program_path = './program/'; +my $secure_path = './secure/'; + +# 3M defaults: 8,4 +my $max_rfid_block = 16; +my $read_blocks = 8; my $response = { 'd500090400110a0500027250' => 'version?', @@ -19,13 +66,26 @@ 'd60017fe00000502e00401003123aa26e00401003123aa283124' => 'tag #1 + tag red-stripe', }; +GetOptions( + 'd|debug+' => \$debug, + 'device=s' => \$device, + 'baudrate=i' => \$baudrate, + 'databits=i' => \$databits, + 'parity=s' => \$parity, + 'stopbits=i' => \$stopbits, + 'handshake=s' => \$handshake, + 'meteor=s' => \$meteor_server, +) or die $!; + +my $verbose = $debug > 0 ? $debug-- : 0; + =head1 NAME 3m-810 - support for 3M 810 RFID reader =head1 SYNOPSIS -3m-810.pl [DEVICE [BAUD [DATA [PARITY [STOP [FLOW]]]]]] +3m-810.pl --device /dev/ttyUSB0 =head1 DESCRIPTION @@ -37,6 +97,8 @@ L +L + =head1 AUTHOR Dobrica Pavlinusic L @@ -48,23 +110,35 @@ =cut -# your serial port. -my ($device,$baudrate,$databits,$parity,$stopbits,$handshake)=@ARGV; -$device ||= "/dev/ttyUSB0"; -$baudrate ||= "19200"; -$databits ||= "8"; -$parity ||= "none"; -$stopbits ||= "1"; -$handshake ||= "none"; +my $tags_data; +my $visible_tags; -my $port=new Device::SerialPort($device) || die "new($device): $!\n"; +my $item_type = { + 1 => 'Book', + 6 => 'CD/CD ROM', + 2 => 'Magazine', + 13 => 'Book with Audio Tape', + 9 => 'Book with CD/CD ROM', + 0 => 'Other', + + 5 => 'Video', + 4 => 'Audio Tape', + 3 => 'Bound Journal', + 8 => 'Book with Diskette', + 7 => 'Diskette', +}; + +warn "## known item type: ",dump( $item_type ) if $debug; + +my $port=new Device::SerialPort($device) || die "can't open serial port $device: $!\n"; +warn "using $device $handshake $baudrate $databits $parity $stopbits" if $debug; $handshake=$port->handshake($handshake); $baudrate=$port->baudrate($baudrate); $databits=$port->databits($databits); $parity=$port->parity($parity); $stopbits=$port->stopbits($stopbits); -print "## using $device $baudrate $databits $parity $stopbits\n"; +print "## using $device $baudrate $databits $parity $stopbits debug: $debug verbose: $verbose\n"; # Just in case: reset our timing and buffers $port->lookclear(); @@ -75,33 +149,237 @@ #$port->stty_inpck(1); #$port->stty_istrip(1); -cmd( 'D5 00 05 04 00 11 8C66', 'hw version?', - 'D5 00 09 04 00 11 0A 05 00 02 7250', 'hw 10.5.0.2', sub { - my ( $len, $payload, $checksum ) = @_; - assert( 0, 3 ); - print "hardware version ", join('.', unpack('CCCC', substr($payload,3,4))), "\n"; +# initial hand-shake with device + +cmd( 'D5 00 05 04 00 11 8C66', 'hw version', + 'D5 00 09 04 00 11 0A 05 00 02 7250', sub { + my $hw_ver = join('.', unpack('CCCC', skip_assert(3) )); + print "hardware version $hw_ver\n"; + meteor( 'info', "Found reader hardware $hw_ver" ); }); -cmd( 'D6 00 0C 13 04 01 00 02 00 03 00 04 00 AAF2','stats?' ); -# D6 00 0C 13 00 02 01 01 03 02 02 03 00 E778 +cmd( 'D6 00 0C 13 04 01 00 02 00 03 00 04 00 AAF2','FIXME: stats?', + 'D6 00 0C 13 00 02 01 01 03 02 02 03 00 E778', sub { assert() } ); + +# start scanning for tags + +cmd( 'D6 00 05 FE 00 05 FA40', "scan for tags, retry $_", + 'D6 00 0F FE 00 00 05 ', sub { # 01 E00401003123AA26 941A # seen, serial length: 8 + my $rest = shift || die "no rest?"; + my $nr = ord( substr( $rest, 0, 1 ) ); -cmd( 'D6 00 05 FE 00 05 FA40', "XXX scan $_", - 'D6 00 07 FE 00 00 05 00 C97B -- no tag' ) foreach ( 1 .. 10 ); + if ( ! $nr ) { + print "no tags in range\n"; + update_visible_tags(); + meteor( 'info-none-in-range' ); + $tags_data = {}; + } else { -# D6 00 0F FE 00 00 05 01 E00401003123AA26 941A # seen + my $tags = substr( $rest, 1 ); + + my $tl = length( $tags ); + die "wrong length $tl for $nr tags: ",dump( $tags ) if $tl =! $nr * 8; + + my @tags; + push @tags, uc(unpack('H16', substr($tags, $_ * 8, 8))) foreach ( 0 .. $nr - 1 ); + warn "## tags ",as_hex($tags), " [$tl] = ",dump( $tags ) if $debug; + print "$nr tags in range: ", join(',', @tags ) , "\n"; + + meteor( 'info-in-range', join(' ',@tags)); + + update_visible_tags( @tags ); + } + } +) while(1); +#) foreach ( 1 .. 100 ); -cmd( 'D6 00 0D 02 E00401003123AA26 00 03 1CC4', 'read offset: 0 blocks: 3' ); -# D6 00 1F 02 00 E00401003123AA26 03 00 00 04 11 00 01 01 00 30 30 30 30 02 00 30 30 30 30 E5F4 -warn "D6 00 1F 02 00 E00401003123AA26 03 00 00 04 11 00 01 01 00 31 32 33 34 02 00 35 36 37 38 531F\n"; -cmd( 'D6 00 0D 02 E00401003123AA26 03 04 3970', 'read offset: 3 blocks: 4' ); +sub update_visible_tags { + my @tags = @_; -# D6 00 25 02 00 E00401003123AA26 04 03 00 30 30 00 00 04 00 00 00 00 00 -# 05 00 00 00 00 00 06 00 00 00 00 00 B9BA -warn "D6 00 25 02 00 E00401003123AA26 04 03 00 39 30 31 32 04 00 33 34 35 36 - 05 00 00 00 00 00 06 00 00 00 00 00 524B\n"; -warn "D6 00 0F FE 00 00 05 01 E00401003123AA26 941A ##### ready?\n"; + my $last_visible_tags = $visible_tags; + $visible_tags = {}; + + foreach my $tag ( @tags ) { + if ( ! defined $last_visible_tags->{$tag} ) { + if ( defined $tags_data->{$tag} ) { +# meteor( 'in-range', $tag ); + } else { + meteor( 'read', $tag ); + read_tag( $tag ); + } + $visible_tags->{$tag}++; + } else { + warn "## using cached data for $tag" if $debug; + } + delete $last_visible_tags->{$tag}; # leave just missing tags + + if ( -e "$program_path/$tag" ) { + meteor( 'write', $tag ); + write_tag( $tag ); + } + if ( -e "$secure_path/$tag" ) { + meteor( 'secure', $tag ); + secure_tag( $tag ); + } + } + + foreach my $tag ( keys %$last_visible_tags ) { + my $data = delete $tags_data->{$tag}; + print "removed tag $tag with data ",dump( $data ),"\n"; + meteor( 'removed', $tag ); + } + + warn "## update_visible_tags(",dump( @tags ),") = ",dump( $visible_tags )," removed: ",dump( $last_visible_tags ), " data: ",dump( $tags_data ) if $debug; +} + +my $tag_data_block; + +sub read_tag_data { + my ($start_block,$rest) = @_; + die "no rest?" unless $rest; + + my $last_block = 0; + + warn "## DATA [$start_block] ", dump( $rest ) if $debug; + my $tag = uc(unpack('H16',substr( $rest, 0, 8 ))); + my $blocks = ord(substr($rest,8,1)); + $rest = substr($rest,9); # leave just data blocks + foreach my $nr ( 0 .. $blocks - 1 ) { + my $block = substr( $rest, $nr * 6, 6 ); + warn "## block ",as_hex( $block ) if $debug; + my $ord = unpack('v',substr( $block, 0, 2 )); + my $expected_ord = $nr + $start_block; + warn "got block $ord, expected block $expected_ord from ",dump( $block ) if $ord != $expected_ord; + my $data = substr( $block, 2 ); + die "data payload should be 4 bytes" if length($data) != 4; + warn sprintf "## tag %9s %02d: %s |%-4s|\n", $tag, $ord, as_hex( $data ), $data; + $tag_data_block->{$tag}->[ $ord ] = $data; + $last_block = $ord; + } + $tags_data->{ $tag } = join('', @{ $tag_data_block->{$tag} }); + + my $item_type_nr = ord(substr( $tags_data->{$tag}, 3, 1 )); + print "DATA $tag ",dump( $tags_data ), " item type: ", ( $item_type->{ $item_type_nr } || "UNKWOWN '$item_type_nr'" ), "\n"; + + return $last_block + 1; +} + +sub read_tag { + my ( $tag ) = @_; + + confess "no tag?" unless $tag; + + print "read_tag $tag\n"; + + my $start_block = 0; + + while ( $start_block < $max_rfid_block ) { + + cmd( + sprintf( "D6 00 0D 02 $tag %02x %02x ffff", $start_block, $read_blocks ), + "read $tag offset: $start_block blocks: $read_blocks", + "D6 00 1F 02 00", sub { # $tag 03 00 00 04 11 00 01 01 00 31 32 33 34 02 00 35 36 37 38 531F\n"; + $start_block = read_tag_data( $start_block, @_ ); + warn "# read tag upto $start_block\n"; + }, + "D6 00 0F FE 00 00 05 01 $tag 941A", sub { + print "FIXME: tag $tag ready? (expected block read instead)\n"; + }, + ); + + } + + my $security; + + cmd( + "D6 00 0B 0A $tag 1234", "check security $tag", + "D6 00 0D 0A 00", sub { + my $rest = shift; + my $from_tag; + ( $from_tag, $security ) = ( substr($rest,0,8), substr($rest,8,1) ); + die "security from other tag: ",as_hex( $from_tag ) if $from_tag ne str2bytes( $tag ); + $security = as_hex( $security ); + warn "# SECURITY $tag = $security\n"; + } + ); + + my $data = $tags_data->{$tag} || die "no data for $tag"; + my ( $u1, $set_item, $u2, $type, $content, $br_lib, $custom ) = unpack('C4Z16Nl>',$data); + my $set = ( $set_item & 0xf0 ) >> 4; + my $total = ( $set_item & 0x0f ); + my $branch = $br_lib >> 20; + my $library = $br_lib & 0x000fffff; + print "TAG $tag [$u1] set: $set/$total [$u2] type: $type '$content' library: $library branch: $branch custom: $custom security: $security\n"; + +} + +sub write_tag { + my ($tag) = @_; + + my $path = "$program_path/$tag"; + + my $data = read_file( $path ); + my $hex_data; + + if ( $data =~ s{^hex\s+}{} ) { + $hex_data = $data; + $hex_data =~ s{\s+}{}g; + } else { + + $data .= "\0" x ( 4 - ( length($data) % 4 ) ); + + my $max_len = $max_rfid_block * 4; + + if ( length($data) > $max_len ) { + $data = substr($data,0,$max_len); + warn "strip content to $max_len bytes\n"; + } + + $hex_data = unpack('H*', $data); + } + + my $len = length($hex_data) / 2; + # pad to block size + $hex_data .= '00' x ( 4 - $len % 4 ); + my $blocks = sprintf('%02x', length($hex_data) / 4); + + print "write_tag $tag = ",dump( $data ), " [$len/$blocks] == $hex_data\n"; + + cmd( + "d6 00 ff 04 $tag 00 $blocks 00 $hex_data ffff", "write $tag", + "d6 00 0d 04 00 $tag $blocks afb1", sub { assert() }, + ); # foreach ( 1 .. 3 ); # XXX 3m software does this three times! + + my $to = $path; + $to .= '.' . time(); + + rename $path, $to; + print ">> $to\n"; + + delete $tags_data->{$tag}; # force re-read of tag +} + +sub secure_tag { + my ($tag) = @_; + + my $path = "$secure_path/$tag"; + my $data = substr(read_file( $path ),0,2); + + cmd( + "d6 00 0c 09 $tag $data 1234", "secure $tag -> $data", + "d6 00 0c 09 00 $tag 1234", sub { assert() }, + ); + + my $to = $path; + $to .= '.' . time(); + + rename $path, $to; + print ">> $to\n"; +} + +exit; for ( 1 .. 3 ) { @@ -133,7 +411,9 @@ { my $str=shift; my $count = $port->write($str); - print ">> ", as_hex( $str ), "\t[$count]\n"; + my $len = length($str); + die "wrong write length $count != $len in ",as_hex( $str ) if $count != $len; + print "#> ", as_hex( $str ), "\t[$count]\n" if $debug; } sub as_hex { @@ -141,9 +421,10 @@ foreach my $str ( @_ ) { my $hex = unpack( 'H*', $str ); $hex =~ s/(..)/$1 /g if length( $str ) > 2; + $hex =~ s/\s+$//; push @out, $hex; } - return join(' ', @out); + return join(' | ', @out); } sub read_bytes { @@ -151,29 +432,74 @@ my $data = ''; while ( length( $data ) < $len ) { my ( $c, $b ) = $port->read(1); + die "no bytes on port: $!" unless defined $b; #warn "## got $c bytes: ", as_hex($b), "\n"; $data .= $b; } $desc ||= '?'; - warn "#< ", as_hex($data), "\t$desc\n"; + warn "#< ", as_hex($data), "\t$desc\n" if $debug; return $data; } -my $assert; +our $assert; + +# my $rest = skip_assert( 3 ); +sub skip_assert { + assert( 0, shift ); +} sub assert { my ( $from, $to ) = @_; - warn "# assert ", dump( $assert ); + $from ||= 0; + $to = length( $assert->{expect} ) if ! defined $to; my $p = substr( $assert->{payload}, $from, $to ); my $e = substr( $assert->{expect}, $from, $to ); - warn "EXPECTED ",as_hex($e), " GOT ", as_hex($p), "\t[$from-$to]\n" if $e ne $p; + warn "EXPECTED ",as_hex($e), " GOT ", as_hex($p), " [$from-$to] in ",dump( $assert ), "\n" if $e ne $p; + + # return the rest + return substr( $assert->{payload}, $to ); } -sub readchunk { - my ( $parser ) = @_; +use Digest::CRC; + +sub crcccitt { + my $bytes = shift; + my $crc = Digest::CRC->new( + # midified CCITT to xor with 0xffff instead of 0x0000 + width => 16, init => 0xffff, xorout => 0xffff, refout => 0, poly => 0x1021, refin => 0, + ) or die $!; + $crc->add( $bytes ); + pack('n', $crc->digest); +} + +# my $checksum = checksum( $bytes ); +# my $checksum = checksum( $bytes, $original_checksum ); +sub checksum { + my ( $bytes, $checksum ) = @_; + + my $len = ord(substr($bytes,2,1)); + my $len_real = length($bytes) - 1; + + if ( $len_real != $len ) { + print "length wrong: $len_real != $len\n"; + $bytes = substr($bytes,0,2) . chr($len_real) . substr($bytes,3); + } + + my $xor = crcccitt( substr($bytes,1) ); # skip D6 + warn "## checksum ",dump( $bytes, $xor, $checksum ) if $debug; + + if ( defined $checksum && $xor ne $checksum ) { + print "checksum doesn't match: ", as_hex($xor), " != ", as_hex($checksum), " data: ", as_hex($bytes), "\n"; + return $bytes . $xor; + } + return $bytes . $checksum; +} + +our $dispatch; +sub readchunk { sleep 1; # FIXME remove # read header of packet @@ -181,47 +507,75 @@ my $length = read_bytes( 1, 'length' ); my $len = ord($length); my $data = read_bytes( $len, 'data' ); - my ( $cmd ) = unpack('C', $data ); my $payload = substr( $data, 0, -2 ); my $payload_len = length($data); warn "## payload too short $payload_len != $len\n" if $payload_len != $len; + my $checksum = substr( $data, -2, 2 ); - # FIXME check checksum + checksum( $header . $length . $payload , $checksum ); - print "<< ",as_hex( $header ), " [$len] ", as_hex( $payload ), "checksum: ", as_hex( $checksum ),"\n"; + print "<< ",as_hex( $header ), " [$len] ", as_hex( $payload ), " | sum: ",as_hex($checksum),"\n" if $verbose; $assert->{len} = $len; $assert->{payload} = $payload; - $assert->{checksum} = $checksum; - $parser->( $len, $payload, $checksum ) if $parser && ref($parser) eq 'CODE'; + my $full = $header . $length . $data; # full + # find longest match for incomming data + my ($to) = grep { + my $match = substr($payload,0,length($_)); + m/^\Q$match\E/ + } sort { length($a) <=> length($b) } keys %$dispatch; + warn "?? payload dispatch to ",dump( $payload, $dispatch, $to ) if $debug; + + if ( defined $to ) { + my $rest = substr( $payload, length($to) ) if length($to) < length($payload); + warn "## DISPATCH payload to with rest", dump( $payload, $to, $rest ) if $debug; + $dispatch->{ $to }->( $rest ); + } else { + print "NO DISPATCH for ",dump( $full ),"\n"; + } return $data; } sub str2bytes { my $str = shift || confess "no str?"; - $str =~ s/\s+(\S\S)(\S\S)+\s*/ $1 $2/; # fix checksum - $str =~ s/\s+/\\x/g; - $str = '"\x' . $str . '"'; - my $bytes = eval $str; + my $b = $str; + $b =~ s/\s+//g; + $b =~ s/(..)/\\x$1/g; + $b = "\"$b\""; + my $bytes = eval $b; die $@ if $@; + warn "## str2bytes( $str ) => $b => ",as_hex($bytes) if $debug; return $bytes; } sub cmd { - my ( $cmd, $cmd_desc, $expect, $expect_desc, $coderef ) = @_; + my $cmd = shift || confess "no cmd?"; + my $cmd_desc = shift || confess "no description?"; + my @expect = @_; + my $bytes = str2bytes( $cmd ); - warn ">> ", as_hex( $bytes ), "\t## $cmd_desc\n"; + # fix checksum if needed + $bytes = checksum( substr( $bytes, 0, -2 ), substr( $bytes, -2, 2 ) ); + + warn ">> ", as_hex( $bytes ), "\t## $cmd_desc\n" if $verbose; $assert->{send} = $cmd; writechunk( $bytes ); - if ( $expect ) { - warn "?? $expect", $expect_desc ? "\t## $expect_desc" : '', "\n"; - $assert->{expect} = substr(str2bytes($expect), 3, -2); # just expected payload - readchunk( $coderef ); + while ( @expect ) { + my $pattern = str2bytes( shift @expect ) || confess "no pattern?"; + my $coderef = shift @expect || confess "no coderef?"; + confess "not coderef" unless ref $coderef eq 'CODE'; + + next if defined $dispatch->{ $pattern }; + + $dispatch->{ substr($pattern,3) } = $coderef; + warn "++ dispatch ", as_hex($pattern) ,dump( $dispatch ) if $debug; } + + readchunk; }