--- 3m-810.pl 2009/06/01 13:09:41 37 +++ 3m-810.pl 2009/06/04 13:36:20 41 @@ -50,6 +50,10 @@ 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?', 'd60007fe00000500c97b' => 'no tag in range', @@ -235,6 +239,9 @@ 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)); @@ -244,16 +251,19 @@ warn "## block ",as_hex( $block ) if $debug; my $ord = unpack('v',substr( $block, 0, 2 )); my $expected_ord = $nr + $start_block; - die "got block $ord, expected block $expected_ord from ",dump( $block ) if $ord != $expected_ord; + 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; + 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' in " . dump( $item_type ) ), "\n"; + + return $last_block; } sub read_tag { @@ -263,22 +273,23 @@ print "read_tag $tag\n"; - cmd( - "D6 00 0D 02 $tag 00 03 1CC4", "read $tag offset: 0 blocks: 3", - "D6 00 0F FE 00 00 05 01 $tag 941A", sub { - print "FIXME: tag $tag ready?\n"; - }, - "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"; - read_tag_data( 0, @_ ); - }, - ); + my $start_block = 0; - cmd( - "D6 00 0D 02 $tag 03 04 3970", "read $tag offset: 3 blocks: 4", - "D6 00 25 02 00", sub { # $tag 04 03 00 30 30 00 00 04 00 00 00 00 00 - read_tag_data( 3, @_ ); - } - ); + 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; @@ -310,17 +321,36 @@ my $path = "$program_path/$tag"; my $data = read_file( $path ); + my $hex_data; - $data = substr($data,0,16); + if ( $data =~ s{^hex\s+}{} ) { + $hex_data = $data; + $hex_data =~ s{\s+}{}g; + } else { - my $hex_data = unpack('H*', $data) . ' 00' x ( 16 - length($data) ); + $data .= "\0" x ( 4 - ( length($data) % 4 ) ); - print "write_tag $tag = ",dump( $data ), " == $hex_data\n"; + 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 26 04 $tag 00 06 00 04 11 00 01 $hex_data 00 00 00 00 fd3b", "write $tag", - "d6 00 0d 04 00 $tag 06 afb1", sub { assert() }, - ) foreach ( 1 .. 3 ); # xxx 3m software does this three times! + "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(); @@ -381,6 +411,8 @@ { my $str=shift; my $count = $port->write($str); + 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; } @@ -419,6 +451,8 @@ sub assert { my ( $from, $to ) = @_; + return unless $assert->{expect}; + $from ||= 0; $to = length( $assert->{expect} ) if ! defined $to; @@ -447,17 +481,17 @@ sub checksum { my ( $bytes, $checksum ) = @_; - my $xor = crcccitt( substr($bytes,1) ); # skip D6 - warn "## checksum ",dump( $bytes, $xor, $checksum ) if $debug; - 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,4); + $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; @@ -496,7 +530,7 @@ } sort { length($a) <=> length($b) } keys %$dispatch; warn "?? payload dispatch to ",dump( $payload, $dispatch, $to ) if $debug; - if ( defined $to ) { + if ( defined $to && $payload ) { my $rest = substr( $payload, length($to) ); warn "## DISPATCH payload to with rest", dump( $payload, $to, $rest ) if $debug; $dispatch->{ $to }->( $rest );