/[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 2 by dpavlin, Sun Sep 28 14:05:43 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 $response = {  my $response = {
11          'd500090400110a0500027250'                              => 'version?',          'd500090400110a0500027250'                              => 'version?',
# Line 74  $port->read_char_time(5); Line 75  $port->read_char_time(5);
75  #$port->stty_inpck(1);  #$port->stty_inpck(1);
76  #$port->stty_istrip(1);  #$port->stty_istrip(1);
77    
 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();  
 }  
   
78  cmd( 'D5 00  05  04   00   11                 8C66', 'hw version?',  cmd( 'D5 00  05  04   00   11                 8C66', 'hw version?',
79       '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 {
80            my ( $len, $payload, $checksum ) = @_;
81            assert( 0, 3 );
82            print "hardware version ", join('.', unpack('CCCC', substr($payload,3,4))), "\n";
83    });
84    
85  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?' );
86  #     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
# Line 140  print "Port closed\n"; Line 132  print "Port closed\n";
132  sub writechunk  sub writechunk
133  {  {
134          my $str=shift;          my $str=shift;
   
135          my $count = $port->write($str);          my $count = $port->write($str);
136          print ">> ", as_hex( $str ), "\t[$count]\n";          print ">> ", as_hex( $str ), "\t[$count]\n";
137  }  }
# Line 149  sub as_hex { Line 140  sub as_hex {
140          my @out;          my @out;
141          foreach my $str ( @_ ) {          foreach my $str ( @_ ) {
142                  my $hex = unpack( 'H*', $str );                  my $hex = unpack( 'H*', $str );
143                  $hex =~ s/(..)/$1 /g;                  $hex =~ s/(..)/$1 /g if length( $str ) > 2;
144                  push @out, $hex;                  push @out, $hex;
145          }          }
146          return join('  ', @out);          return join('  ', @out);
# Line 168  sub read_bytes { Line 159  sub read_bytes {
159          return $data;          return $data;
160  }  }
161    
162    my $assert;
163    
164    sub assert {
165            my ( $from, $to ) = @_;
166    
167            warn "# assert ", dump( $assert );
168    
169            my $p = substr( $assert->{payload}, $from, $to );
170            my $e = substr( $assert->{expect},  $from, $to );
171            warn "EXPECTED ",as_hex($e), " GOT ", as_hex($p), "\t[$from-$to]\n" if $e ne $p;
172    }
173    
174  sub readchunk {  sub readchunk {
175            my ( $parser ) = @_;
176    
177            sleep 1;        # FIXME remove
178    
179          # read header of packet          # read header of packet
180          my $header = read_bytes( 2, 'header' );          my $header = read_bytes( 2, 'header' );
181          my $len = ord( read_bytes( 1, 'length' ) );          my $length = read_bytes( 1, 'length' );
182            my $len = ord($length);
183          my $data = read_bytes( $len, 'data' );          my $data = read_bytes( $len, 'data' );
184            my ( $cmd ) = unpack('C', $data );
185    
186            my $payload  = substr( $data, 0, -2 );
187            my $payload_len = length($data);
188            warn "## payload too short $payload_len != $len\n" if $payload_len != $len;
189            my $checksum = substr( $data, -2, 2 );
190            # FIXME check checksum
191    
192          warn "<< ",as_hex( $header, ), " [$len] ", as_hex( $data ), "\n";          print "<< ",as_hex( $header ), " [$len] ", as_hex( $payload ), "checksum: ", as_hex( $checksum ),"\n";
193    
194          sleep 1;          $assert->{len}      = $len;
195            $assert->{payload}  = $payload;
196            $assert->{checksum} = $checksum;
197    
198            $parser->( $len, $payload, $checksum ) if $parser && ref($parser) eq 'CODE';
199    
200            return $data;
201    }
202    
203    sub str2bytes {
204            my $str = shift || confess "no str?";
205            $str =~ s/\s+(\S\S)(\S\S)+\s*/ $1 $2/;  # fix checksum
206            $str =~ s/\s+/\\x/g;
207            $str = '"\x' . $str . '"';
208            my $bytes = eval $str;
209            die $@ if $@;
210            return $bytes;
211    }
212    
213    sub cmd {
214            my ( $cmd, $cmd_desc, $expect, $expect_desc, $coderef ) = @_;
215            my $bytes = str2bytes( $cmd );
216    
217            warn ">> ", as_hex( $bytes ), "\t## $cmd_desc\n";
218            $assert->{send} = $cmd;
219            writechunk( $bytes );
220    
221            if ( $expect ) {
222                    warn "?? $expect", $expect_desc ? "\t## $expect_desc" : '', "\n";
223                    $assert->{expect} = substr(str2bytes($expect), 3, -2); # just expected payload
224                    readchunk( $coderef );
225            }
226  }  }
227    

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

  ViewVC Help
Powered by ViewVC 1.1.26