--- 3m-810.pl 2008/09/28 12:57:32 1 +++ 3m-810.pl 2008/10/03 08:53:57 17 @@ -5,6 +5,9 @@ use warnings; use Data::Dump qw/dump/; +use Carp qw/confess/; + +my $debug = 0; my $response = { 'd500090400110a0500027250' => 'version?', @@ -36,6 +39,8 @@ L +L + =head1 AUTHOR Dobrica Pavlinusic L @@ -74,42 +79,65 @@ #$port->stty_inpck(1); #$port->stty_istrip(1); -sub cmd { - my ( $cmd, $desc, $expect ) = @_; - $cmd =~ s/\s+(\S\S)(\S\S)+\s*/ $1 $2/; # fix checksum - $cmd =~ s/\s+/\\x/g; - $cmd = '"\x' . $cmd . '"'; - my $bytes = eval $cmd; - die $@ if $@; - warn ">> ", as_hex( $bytes ), "\t$desc\n"; - writechunk( $bytes ); - warn "?? $expect\n" if $expect; - readchunk(); -} +# 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', 'hw 10.5.0.2', sub { + print "hardware version ", join('.', unpack('CCCC', skip_assert(3) )), "\n"; +}); -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' ); +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','FIXME: unimplemented', sub { assert() } ); -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 +# start scanning for tags -cmd( 'D6 00 05 FE 00 05 FA40', "XXX scan $_", - 'D6 00 07 FE 00 00 05 00 C97B -- no tag' ) foreach ( 1 .. 10 ); +cmd( 'D6 00 05 FE 00 05 FA40', "XXX scan $_", + 'D6 00 07 FE 00 00 05 00 C97B', 'no tag', sub { +dispatch( + 'D6 00 0F FE 00 00 05 ',# 01 E00401003123AA26 941A # seen, serial length: 8 + sub { + my $rest = shift || die "no rest?"; + my $nr = ord( substr( $rest, 0, 1 ) ); + my $tags = substr( $rest, 1 ); -# D6 00 0F FE 00 00 05 01 E00401003123AA26 941A # seen + my $tl = length( $tags ); + die "wrong length $tl for $nr tags: ",dump( $tags ) if $tl =! $nr * 8; -cmd( 'D6 00 0D 02 E00401003123AA26 00 03 1CC4', 'read offset: 0 blocks: 3' ); + 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 "seen $nr tags: ", join(',', @tags ) , "\n"; -# 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"; + # XXX read first tag + read_tag( @tags ); -cmd( 'D6 00 0D 02 E00401003123AA26 03 04 3970', 'read offset: 3 blocks: 4' ); + } +) } -# 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"; +) foreach ( 1 .. 100 ); + +my $read_cached; + +sub read_tag { + my ( $tag ) = @_; + + print "read_tag $tag\n"; + return if $read_cached->{ $tag }++; + + cmd( "D6 00 0D 02 $tag 00 03 1CC4", 'read offset: 0 blocks: 3' ); + + # D6 00 1F 02 00 $tag 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 $tag 03 00 00 04 11 00 01 01 00 31 32 33 34 02 00 35 36 37 38 531F\n"; +if (0) { + cmd( "D6 00 0D 02 $tag 03 04 3970", 'read offset: 3 blocks: 4' ); + + # D6 00 25 02 00 $tag 04 03 00 30 30 00 00 04 00 00 00 00 00 + # $tag 05 00 00 00 00 00 06 00 00 00 00 00 B9BA + warn "?? D6 00 25 02 00 $tag 04 03 00 39 30 31 32 04 00 ....\n"; +} + warn "?? D6 00 0F FE 00 00 05 01 $tag 941A ##### ready?\n"; + +} for ( 1 .. 3 ) { @@ -140,19 +168,19 @@ sub writechunk { my $str=shift; - my $count = $port->write($str); - print ">> ", as_hex( $str ), "\t[$count]\n"; + print "#> ", as_hex( $str ), "\t[$count]\n"; } sub as_hex { my @out; foreach my $str ( @_ ) { my $hex = unpack( 'H*', $str ); - $hex =~ s/(..)/$1 /g; + $hex =~ s/(..)/$1 /g if length( $str ) > 2; + $hex =~ s/\s+$//; push @out, $hex; } - return join(' ', @out); + return join(' | ', @out); } sub read_bytes { @@ -164,19 +192,139 @@ $data .= $b; } $desc ||= '?'; - warn "#< ", as_hex($data), "\t$desc\n"; + warn "#< ", as_hex($data), "\t$desc\n" if $debug; return $data; } +our $assert; + +# my $rest = skip_assert( 3 ); +sub skip_assert { + assert( 0, shift ); +} + +sub assert { + my ( $from, $to ) = @_; + + $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), " [$from-$to] in ",dump( $assert ), "\n" if $e ne $p; + + # return the rest + return substr( $assert->{payload}, $to ); +} + +our $dispatch; +sub dispatch { + my ( $pattern, $coderef ) = @_; + my $patt = substr( str2bytes($pattern), 3 ); # just payload + my $l = length($patt); + my $p = substr( $assert->{payload}, 0, $l ); + warn "## dispatch pattern $pattern [$l] ",dump( $patt, $p ) if $debug; + + if ( $assert->{payload} eq $assert->{expect} ) { + warn "## no dispatch, payload expected" if $debug; + } elsif ( $p eq $patt ) { + # if matched call with rest of payload + $coderef->( substr( $assert->{payload}, $l ) ); + } else { + warn "## dispatch ignored" if $debug; + } +} + +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 $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); + } + + 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; +} + sub readchunk { + my ( $parser ) = @_; + + sleep 1; # FIXME remove # read header of packet my $header = read_bytes( 2, 'header' ); - my $len = ord( read_bytes( 1, 'length' ) ); + my $length = read_bytes( 1, 'length' ); + my $len = ord($length); my $data = read_bytes( $len, 'data' ); - warn "<< ",as_hex( $header, ), " [$len] ", as_hex( $data ), "\n"; + 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 ); + checksum( $header . $length . $payload, $checksum ); + + print "<< ",as_hex( $header ), " [$len] ", as_hex( $payload ), " | sum: ",as_hex($checksum),"\n"; + + $assert->{len} = $len; + $assert->{payload} = $payload; - sleep 1; + $parser->( $len, $payload ) if $parser && ref($parser) eq 'CODE'; + + return $data; +} + +sub str2bytes { + my $str = shift || confess "no 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 $bytes = str2bytes( $cmd ); + + # fix checksum if needed + $bytes = checksum( substr( $bytes, 0, -2 ), substr( $bytes, -2, 2 ) ); + + warn ">> ", as_hex( $bytes ), "\t## $cmd_desc\n"; + $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 ); + } }