/[RFID]/3m-810.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /3m-810.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2 by dpavlin, Sun Sep 28 14:05:43 2008 UTC revision 15 by dpavlin, Thu Oct 2 21:20:10 2008 UTC
# Line 7  use warnings; Line 7  use warnings;
7  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
8  use Carp qw/confess/;  use Carp qw/confess/;
9    
10    my $debug = 0;
11    
12  my $response = {  my $response = {
13          'd500090400110a0500027250'                              => 'version?',          'd500090400110a0500027250'                              => 'version?',
14          'd60007fe00000500c97b'                                  => 'no tag in range',          'd60007fe00000500c97b'                                  => 'no tag in range',
# Line 37  L<Device::SerialPort(3)> Line 39  L<Device::SerialPort(3)>
39    
40  L<perl(1)>  L<perl(1)>
41    
42    L<http://stackoverflow.com/questions/149617/how-could-i-guess-a-checksum-algorithm>
43    
44  =head1 AUTHOR  =head1 AUTHOR
45    
46  Dobrica Pavlinusic <dpavlin@rot13.org> L<http://www.rot13.org/~dpavlin/>  Dobrica Pavlinusic <dpavlin@rot13.org> L<http://www.rot13.org/~dpavlin/>
# Line 75  $port->read_char_time(5); Line 79  $port->read_char_time(5);
79  #$port->stty_inpck(1);  #$port->stty_inpck(1);
80  #$port->stty_istrip(1);  #$port->stty_istrip(1);
81    
82  cmd( 'D5 00  05  04   00   11                 8C66', 'hw version?',  # initial hand-shake with device
83       'D5 00  09  04   00   11   0A 05 00 02   7250', 'hw 10.5.0.2', sub {  
84          my ( $len, $payload, $checksum ) = @_;  cmd( 'D5 00  05   04 00 11                 8C66', 'hw version?',
85          assert( 0, 3 );       'D5 00  09   04 00 11   0A 05 00 02   7250', 'hw 10.5.0.2', sub {
86          print "hardware version ", join('.', unpack('CCCC', substr($payload,3,4))), "\n";          print "hardware version ", join('.', unpack('CCCC', skip_assert(3) )), "\n";
87  });  });
88    
89  cmd( 'D6 00  0C  13   04   01 00  02 00  03 00  04 00   AAF2','stats?' );  cmd( 'D6 00  0C   13  04  01 00  02 00  03 00  04 00   AAF2','stats?',
90  #     D6 00  0C  13   00   02 01 01 03 02 02 03  00   E778       'D6 00  0C   13  00  02 01 01 03 02 02 03  00     E778','FIXME: unimplemented', sub { assert() }  );
91    
92  cmd( 'D6 00  05  FE     00  05  FA40', "XXX scan $_",  # start scanning for tags
      'D6 00  07  FE  00 00  05  00  C97B -- no tag' ) foreach ( 1 .. 10 );  
93    
94  #     D6 00  0F  FE  00 00  05  01  E00401003123AA26  941A       # seen  cmd( 'D6 00  05   FE     00  05         FA40', "XXX scan $_",
95         'D6 00  07   FE  00 00  05     00  C97B', 'no tag', sub {
96    dispatch(
97             'D6 00  0F   FE  00 00  05 ',# 01 E00401003123AA26  941A        # seen, serial length: 8
98                    sub {
99                            my $rest = shift || die "no rest?";
100                            my $nr = ord( substr( $rest, 0, 1 ) );
101                            my $tags = substr( $rest, 1 );
102    
103                            my $tl = length( $tags );
104                            die "wrong length $tl for $nr tags: ",dump( $tags ) if $tl =! $nr * 8;
105    
106                            my @tags;
107                            push @tags, substr($tags, $_ * 8, 8) foreach ( 0 .. $nr - 1 );
108                            warn "## tags ",as_hex($tags), " [$tl] = ",dump( $tags ) if $debug;
109                            print "seen $nr tags: ", join(',', map { unpack('H16', $_) } @tags ) , "\n";
110                    }
111    ) }
112    
113    ) foreach ( 1 .. 100 );
114    
115  cmd( 'D6 00  0D  02      E00401003123AA26   00   03     1CC4', 'read offset: 0 blocks: 3' );  cmd( 'D6 00  0D  02      E00401003123AA26   00   03     1CC4', 'read offset: 0 blocks: 3' );
116    
# Line 133  sub writechunk Line 155  sub writechunk
155  {  {
156          my $str=shift;          my $str=shift;
157          my $count = $port->write($str);          my $count = $port->write($str);
158          print ">> ", as_hex( $str ), "\t[$count]\n";          print "#> ", as_hex( $str ), "\t[$count]\n";
159  }  }
160    
161  sub as_hex {  sub as_hex {
# Line 141  sub as_hex { Line 163  sub as_hex {
163          foreach my $str ( @_ ) {          foreach my $str ( @_ ) {
164                  my $hex = unpack( 'H*', $str );                  my $hex = unpack( 'H*', $str );
165                  $hex =~ s/(..)/$1 /g if length( $str ) > 2;                  $hex =~ s/(..)/$1 /g if length( $str ) > 2;
166                    $hex =~ s/\s+$//;
167                  push @out, $hex;                  push @out, $hex;
168          }          }
169          return join('  ', @out);          return join(' | ', @out);
170  }  }
171    
172  sub read_bytes {  sub read_bytes {
# Line 155  sub read_bytes { Line 178  sub read_bytes {
178                  $data .= $b;                  $data .= $b;
179          }          }
180          $desc ||= '?';          $desc ||= '?';
181          warn "#< ", as_hex($data), "\t$desc\n";          warn "#< ", as_hex($data), "\t$desc\n" if $debug;
182          return $data;          return $data;
183  }  }
184    
185  my $assert;  our $assert;
186    
187    # my $rest = skip_assert( 3 );
188    sub skip_assert {
189            assert( 0, shift );
190    }
191    
192  sub assert {  sub assert {
193          my ( $from, $to ) = @_;          my ( $from, $to ) = @_;
194    
195          warn "# assert ", dump( $assert );          $from ||= 0;
196            $to = length( $assert->{expect} ) if ! defined $to;
197    
198          my $p = substr( $assert->{payload}, $from, $to );          my $p = substr( $assert->{payload}, $from, $to );
199          my $e = substr( $assert->{expect},  $from, $to );          my $e = substr( $assert->{expect},  $from, $to );
200          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;
201    
202            # return the rest
203            return substr( $assert->{payload}, $to );
204    }
205    
206    our $dispatch;
207    sub dispatch {
208            my ( $pattern, $coderef ) = @_;
209            my $patt = substr( str2bytes($pattern), 3 ); # just payload
210            my $l = length($patt);
211            my $p = substr( $assert->{payload}, 0, $l );
212            warn "## dispatch pattern $pattern [$l] ",dump( $patt, $p ) if $debug;
213    
214            if ( $assert->{payload} eq $assert->{expect} ) {
215                    warn "## no dispatch, payload expected" if $debug;
216            } elsif ( $p eq $patt ) {
217                    # if matched call with rest of payload
218                    $coderef->( substr( $assert->{payload}, $l ) );
219            } else {
220                    warn "## dispatch ignored" if $debug;
221            }
222    }
223    
224    use Digest::CRC;
225    
226    sub crcccitt {
227            my $bytes = shift;
228            my $crc = Digest::CRC->new(
229                    # midified CCITT to xor with 0xffff instead of 0x0000
230                    width => 16, init => 0xffff, xorout => 0xffff, refout => 0, poly => 0x1021, refin => 0,
231            ) or die $!;
232            $crc->add( $bytes );
233            pack('n', $crc->digest);
234    }
235    
236    # my $checksum = checksum( $bytes );
237    # my $checksum = checksum( $bytes, $original_checksum );
238    sub checksum {
239            my ( $bytes, $checksum ) = @_;
240    
241            my $xor = crcccitt( substr($bytes,1) ); # skip D6
242            warn "## checksum ",dump( $bytes, $xor, $checksum ) if $debug;
243    
244            if ( defined $checksum && $xor ne $checksum ) {
245                    print "checksum doesn't match: ", as_hex($xor), " != ", as_hex($checksum), " data: ", as_hex($bytes), "\n";
246            }
247  }  }
248    
249  sub readchunk {  sub readchunk {
# Line 181  sub readchunk { Line 256  sub readchunk {
256          my $length = read_bytes( 1, 'length' );          my $length = read_bytes( 1, 'length' );
257          my $len = ord($length);          my $len = ord($length);
258          my $data = read_bytes( $len, 'data' );          my $data = read_bytes( $len, 'data' );
         my ( $cmd ) = unpack('C', $data );  
259    
260          my $payload  = substr( $data, 0, -2 );          my $payload  = substr( $data, 0, -2 );
261          my $payload_len = length($data);          my $payload_len = length($data);
262          warn "## payload too short $payload_len != $len\n" if $payload_len != $len;          warn "## payload too short $payload_len != $len\n" if $payload_len != $len;
263    
264          my $checksum = substr( $data, -2, 2 );          my $checksum = substr( $data, -2, 2 );
265          # FIXME check checksum          checksum( $header . $length . $payload, $checksum );
266    
267          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";
268    
269          $assert->{len}      = $len;          $assert->{len}      = $len;
270          $assert->{payload}  = $payload;          $assert->{payload}  = $payload;
         $assert->{checksum} = $checksum;  
271    
272          $parser->( $len, $payload, $checksum ) if $parser && ref($parser) eq 'CODE';          $parser->( $len, $payload ) if $parser && ref($parser) eq 'CODE';
273    
274          return $data;          return $data;
275  }  }
276    
277  sub str2bytes {  sub str2bytes {
278          my $str = shift || confess "no str?";          my $str = shift || confess "no str?";
279          $str =~ s/\s+(\S\S)(\S\S)+\s*/ $1 $2/;  # fix checksum          my $b = $str;
280          $str =~ s/\s+/\\x/g;          $b =~ s/\s+(\S\S)(\S\S)+\s*/ $1 $2/;    # fix checksum
281          $str = '"\x' . $str . '"';          $b =~ s/\s+$//;
282          my $bytes = eval $str;          $b =~ s/\s+/\\x/g;
283            $b = '"\x' . $b . '"';
284            my $bytes = eval $b;
285          die $@ if $@;          die $@ if $@;
286            warn "## str2bytes( $str ) => $b => ",as_hex($bytes) if $debug;
287          return $bytes;          return $bytes;
288  }  }
289    

Legend:
Removed from v.2  
changed lines
  Added in v.15

  ViewVC Help
Powered by ViewVC 1.1.26