/[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 1 by dpavlin, Sun Sep 28 12:57:32 2008 UTC revision 6 by dpavlin, Sun Sep 28 18:19:37 2008 UTC
# Line 5  use strict; Line 5  use strict;
5  use warnings;  use warnings;
6    
7  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
8    use Carp qw/confess/;
9    
10    my $debug = 0;
11    
12  my $response = {  my $response = {
13          'd500090400110a0500027250'                              => 'version?',          'd500090400110a0500027250'                              => 'version?',
# Line 74  $port->read_char_time(5); Line 77  $port->read_char_time(5);
77  #$port->stty_inpck(1);  #$port->stty_inpck(1);
78  #$port->stty_istrip(1);  #$port->stty_istrip(1);
79    
80  sub cmd {  # initial hand-shake with device
         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();  
 }  
81    
82  cmd( 'D5 00  05  04   00   11                 8C66', 'hw version?',  cmd( 'D5 00  05   04 00 11                 8C66', 'hw version?',
83       'D5 00  09  04   00   11   0A 05 00 02   7250 -- hw 10.5.0.2' );       'D5 00  09   04 00 11   0A 05 00 02   7250', 'hw 10.5.0.2', sub {
84            print "hardware version ", join('.', unpack('CCCC', skip_assert(3) )), "\n";
85    });
86    
87    cmd( 'D6 00  0C   13  04  01 00  02 00  03 00  04 00   AAF2','stats?',
88         'D6 00  0C   13  00  02 01 01 03 02 02 03  00     E778','FIXME: unimplemented', sub { assert() }  );
89    
90    # start scanning for tags
91    
92    cmd( 'D6 00  05   FE     00  05         FA40', "XXX scan $_",
93         'D6 00  07   FE  00 00  05     00  C97B', 'no tag', sub {
94    dispatch(
95             'D6 00  0F   FE  00 00  05 ',# 01 E00401003123AA26  941A        # seen, serial length: 8
96                    sub {
97                            my $rest = shift || die "no rest?";
98                            my $nr = ord( substr( $rest, 0, 1 ) );
99                            my $tags = substr( $rest, 1 );
100    
101                            my $tl = length( $tags );
102                            die "wrong length $tl for $nr tags: ",dump( $tags ) if $tl =! $nr * 8;
103    
104                            my @tags;
105                            push @tags, substr($tags, $_ * 8, 8) foreach ( 0 .. $nr - 1 );
106                            warn "## tags ",as_hex($tags), " [$tl] = ",dump( $tags );
107                            print "seen $nr tags: ", join(',', map { unpack('H16', $_) } @tags ) , "\n";
108                    }
109    ) }
110    
111  cmd( 'D6 00  0C  13   04   01 00  02 00  03 00  04 00   AAF2','stats?' );  ) foreach ( 1 .. 100 );
 #     D6 00  0C  13   00   02 01 01 03 02 02 03  00   E778  
   
 cmd( 'D6 00  05  FE     00  05  FA40', "XXX scan $_",  
      'D6 00  07  FE  00 00  05  00  C97B -- no tag' ) foreach ( 1 .. 10 );  
   
 #     D6 00  0F  FE  00 00  05  01  E00401003123AA26  941A       # seen  
112    
113  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' );
114    
# Line 140  print "Port closed\n"; Line 152  print "Port closed\n";
152  sub writechunk  sub writechunk
153  {  {
154          my $str=shift;          my $str=shift;
   
155          my $count = $port->write($str);          my $count = $port->write($str);
156          print ">> ", as_hex( $str ), "\t[$count]\n";          print "#> ", as_hex( $str ), "\t[$count]\n";
157  }  }
158    
159  sub as_hex {  sub as_hex {
160          my @out;          my @out;
161          foreach my $str ( @_ ) {          foreach my $str ( @_ ) {
162                  my $hex = unpack( 'H*', $str );                  my $hex = unpack( 'H*', $str );
163                  $hex =~ s/(..)/$1 /g;                  $hex =~ s/(..)/$1 /g if length( $str ) > 2;
164                  push @out, $hex;                  push @out, $hex;
165          }          }
166          return join('  ', @out);          return join('  ', @out);
# Line 164  sub read_bytes { Line 175  sub read_bytes {
175                  $data .= $b;                  $data .= $b;
176          }          }
177          $desc ||= '?';          $desc ||= '?';
178          warn "#< ", as_hex($data), "\t$desc\n";          warn "#< ", as_hex($data), "\t$desc\n" if $debug;
179          return $data;          return $data;
180  }  }
181    
182    our $assert;
183    
184    # my $rest = skip_assert( 3 );
185    sub skip_assert {
186            assert( 0, shift );
187    }
188    
189    sub assert {
190            my ( $from, $to ) = @_;
191    
192            $from ||= 0;
193            $to = length( $assert->{expect} ) if ! defined $to;
194    
195            my $p = substr( $assert->{payload}, $from, $to );
196            my $e = substr( $assert->{expect},  $from, $to );
197            warn "EXPECTED ",as_hex($e), " GOT ", as_hex($p), " [$from-$to] in ",dump( $assert ), "\n" if $e ne $p;
198    
199            # return the rest
200            return substr( $assert->{payload}, $to );
201    }
202    
203    our $dispatch;
204    sub dispatch {
205            my ( $pattern, $coderef ) = @_;
206            my $patt = substr( str2bytes($pattern), 3 ); # just payload
207            my $l = length($patt);
208            my $p = substr( $assert->{payload}, 0, $l );
209            warn "## dispatch pattern $pattern [$l] ",dump( $patt, $p ) if $debug;
210    
211            if ( $assert->{payload} eq $assert->{expect} ) {
212                    warn "## no dispatch, payload expected" if $debug;
213            } elsif ( $p eq $patt ) {
214                    # if matched call with rest of payload
215                    $coderef->( substr( $assert->{payload}, $l ) );
216            } else {
217                    warn "## dispatch ignored" if $debug;
218            }
219    }
220    
221  sub readchunk {  sub readchunk {
222            my ( $parser ) = @_;
223    
224            sleep 1;        # FIXME remove
225    
226          # read header of packet          # read header of packet
227          my $header = read_bytes( 2, 'header' );          my $header = read_bytes( 2, 'header' );
228          my $len = ord( read_bytes( 1, 'length' ) );          my $length = read_bytes( 1, 'length' );
229            my $len = ord($length);
230          my $data = read_bytes( $len, 'data' );          my $data = read_bytes( $len, 'data' );
231            my ( $cmd ) = unpack('C', $data );
232    
233            my $payload  = substr( $data, 0, -2 );
234            my $payload_len = length($data);
235            warn "## payload too short $payload_len != $len\n" if $payload_len != $len;
236            my $checksum = substr( $data, -2, 2 );
237            # FIXME check checksum
238    
239            print "<< ",as_hex( $header ), " [$len] ", as_hex( $payload ), "checksum: ", as_hex( $checksum ),"\n";
240    
241          warn "<< ",as_hex( $header, ), " [$len] ", as_hex( $data ), "\n";          $assert->{len}      = $len;
242            $assert->{payload}  = $payload;
243            $assert->{checksum} = $checksum;
244    
245            $parser->( $len, $payload, $checksum ) if $parser && ref($parser) eq 'CODE';
246    
247            return $data;
248    }
249    
250          sleep 1;  sub str2bytes {
251            my $str = shift || confess "no str?";
252            my $b = $str;
253            $b =~ s/\s+(\S\S)(\S\S)+\s*/ $1 $2/;    # fix checksum
254            $b =~ s/\s+$//;
255            $b =~ s/\s+/\\x/g;
256            $b = '"\x' . $b . '"';
257            my $bytes = eval $b;
258            die $@ if $@;
259            warn "## str2bytes( $str ) => $b => ",as_hex($bytes) if $debug;
260            return $bytes;
261    }
262    
263    sub cmd {
264            my ( $cmd, $cmd_desc, $expect, $expect_desc, $coderef ) = @_;
265            my $bytes = str2bytes( $cmd );
266    
267            warn ">> ", as_hex( $bytes ), "\t## $cmd_desc\n";
268            $assert->{send} = $cmd;
269            writechunk( $bytes );
270    
271            if ( $expect ) {
272                    warn "?? $expect", $expect_desc ? "\t## $expect_desc" : '', "\n";
273                    $assert->{expect} = substr(str2bytes($expect), 3, -2); # just expected payload
274                    readchunk( $coderef );
275            }
276  }  }
277    

Legend:
Removed from v.1  
changed lines
  Added in v.6

  ViewVC Help
Powered by ViewVC 1.1.26