/[RFID]/cpr-m02.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 /cpr-m02.pl

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

3m-810.pl revision 71 by dpavlin, Thu Feb 11 20:57:51 2010 UTC cpr-m02.pl revision 86 by dpavlin, Fri Jul 16 09:31:56 2010 UTC
# Line 10  use Getopt::Long; Line 10  use Getopt::Long;
10  use File::Slurp;  use File::Slurp;
11  use JSON;  use JSON;
12  use POSIX qw(strftime);  use POSIX qw(strftime);
13    use Time::HiRes;
14    
15  use IO::Socket::INET;  use IO::Socket::INET;
16    
# Line 31  sub http_server { Line 32  sub http_server {
32                  Reuse     => 1                  Reuse     => 1
33          );          );
34                                                                                                                                        
35          die "can't setup server" unless $server;          die "can't setup server: $!" unless $server;
36    
37          print "Server $0 ready at $server_url\n";          print "Server $0 ready at $server_url\n";
38    
# Line 155  sub _log { _message('log',@_) }; Line 156  sub _log { _message('log',@_) };
156  sub diag { _message('diag',@_) };  sub diag { _message('diag',@_) };
157    
158  my $device    = "/dev/ttyUSB0";  my $device    = "/dev/ttyUSB0";
159  my $baudrate  = "19200";  my $baudrate  = "38400";
160  my $databits  = "8";  my $databits  = "8";
161  my $parity        = "none";  my $parity        = "even";
162  my $stopbits  = "1";  my $stopbits  = "1";
163  my $handshake = "none";  my $handshake = "none";
164    
# Line 168  my $secure_path = './secure/'; Line 169  my $secure_path = './secure/';
169  my $http_server = 1;  my $http_server = 1;
170    
171  # 3M defaults: 8,4  # 3M defaults: 8,4
172  my $max_rfid_block = 16;  # cards 16, stickers: 8
173    my $max_rfid_block = 8;
174  my $read_blocks = 8;  my $read_blocks = 8;
175    
176  my $response = {  my $response = {
# Line 263  $port->read_char_time(5); Line 265  $port->read_char_time(5);
265  #$port->stty_inpck(1);  #$port->stty_inpck(1);
266  #$port->stty_istrip(1);  #$port->stty_istrip(1);
267    
268    sub cpr_m02_checksum {
269            my $data = shift;
270    
271            my $preset = 0xffff;
272            my $polynom = 0x8408;
273    
274            my $crc = $preset;
275            foreach my $i ( 0 .. length($data) - 1 ) {
276                    $crc ^= ord(substr($data,$i,1));
277                    for my $j ( 0 .. 7 ) {
278                            if ( $crc & 0x0001 ) {
279                                    $crc = ( $crc >> 1 ) ^ $polynom;
280                            } else {
281                                    $crc = $crc >> 1;
282                            }
283                    }
284    #               warn sprintf('%d %04x', $i, $crc & 0xffff);
285            }
286    
287            return pack('v', $crc);
288    }
289    
290    sub cpr {
291            my ( $hex, $description, $coderef ) = @_;
292            my $bytes = str2bytes($hex);
293            my $len = pack( 'c', length( $bytes ) + 3 );
294            my $send = $len . $bytes;
295            my $checksum = cpr_m02_checksum($send);
296            $send .= $checksum;
297    
298            warn ">> ", as_hex( $send ), "\t\t[$description]\n";
299            $port->write( $send );
300    
301            my $r_len = $port->read(1);
302    
303            while ( ! $r_len ) {
304                    warn "# wait for response length 0.050\n";
305                    Time::HiRes::sleep 0.050;
306                    $r_len = $port->read(1);
307            }
308    
309            warn "<< response len: ", as_hex($r_len), "\n";
310            $r_len = ord($r_len) - 1;
311            my $data = $port->read( $r_len );
312            warn "<< ", as_hex( $data );
313            
314            my $t = Time::HiRes::time;
315    
316            $coderef->( $data ) if $coderef;
317    
318            my $dt = Time::HiRes::time - $t;
319            if ( $dt < 0.050 ) {
320                    my $s = 0.050 - $dt;
321                    warn "# sleep for more $s\n";
322                    Time::HiRes::sleep $s;
323            }
324    }
325    
326    # FF = COM-ADDR any
327    
328    cpr( 'FF  52 00',       'Boud Rate Detection' );
329    
330    cpr( 'FF  65',          'Get Software Version' );
331    
332    cpr( 'FF  66 00',       'Get Reader Info - General hard and firware' );
333    
334    cpr( 'FF  69',          'RF Reset' );
335    
336    my $inventory;
337    
338    while(1) {
339    
340    cpr( 'FF  B0  01 00', 'ISO - Inventory', sub {
341            my $data = shift;
342            my $data_sets = ord(substr($data,3,1));
343            $data = substr($data,4);
344            foreach ( 1 .. $data_sets ) {
345                    my $tr_type = substr($data,0,1);
346                    my $dsfid   = substr($data,1,1);
347                    my $uid     = substr($data,2,8);
348                    $inventory->{$uid}++;
349                    $data = substr($data,10);
350                    warn "# TAG $_ ",as_hex( $tr_type, $dsfid, $uid ),$/;
351            }
352            warn "inventory: ",dump($inventory);
353    });
354    
355    }
356    
357    #cpr( '', '?' );
358    
359    exit;
360  # initial hand-shake with device  # initial hand-shake with device
361    
362  cmd( 'D5 00  05   04 00 11                 8C66', 'hw version',  cmd( 'D5 00  05   04 00 11                 8C66', 'hw version',
# Line 392  my $saved_in_log; Line 486  my $saved_in_log;
486  sub decode_tag {  sub decode_tag {
487          my $tag = shift;          my $tag = shift;
488    
489          my $data = $tags_data->{$tag} || die "no data for $tag";          my $data = $tags_data->{$tag};
490            if ( ! $data ) {
491                    warn "no data for $tag\n";
492                    return;
493            }
494    
495          my ( $u1, $set_item, $u2, $type, $content, $br_lib, $custom ) = unpack('C4Z16Nl>',$data);          my ( $u1, $set_item, $u2, $type, $content, $br_lib, $custom ) = unpack('C4Z16Nl>',$data);
496          my $hash = {          my $hash = {
# Line 446  sub read_tag { Line 544  sub read_tag {
544                          "D6 00  0F  FE  00 00  05 01   $tag    BEEF", sub {                          "D6 00  0F  FE  00 00  05 01   $tag    BEEF", sub {
545                                  print "FIXME: tag $tag ready? (expected block read instead)\n";                                  print "FIXME: tag $tag ready? (expected block read instead)\n";
546                          },                          },
547                            "D6 00 0D 02 06 $tag", sub {
548                                    my $rest = shift;
549                                    print "ERROR reading $tag ", as_hex($rest), $/;
550                                    forget_tag $tag;
551                                    $start_block = $max_rfid_block; # XXX break out of while
552                            },
553                  );                  );
554    
555          }          }
# Line 462  sub read_tag { Line 566  sub read_tag {
566                          $security = as_hex( $security );                          $security = as_hex( $security );
567                          $tags_security->{$tag} = $security;                          $tags_security->{$tag} = $security;
568                          warn "# SECURITY $tag = $security\n";                          warn "# SECURITY $tag = $security\n";
569                  }                  },
570                    "D6 00 0C 0A 06", sub {
571                            my $rest = shift;
572                            warn "ERROR reading security from $rest\n";
573                            forget_tag $tag;
574                    },
575          );          );
576    
577          print "TAG $tag ", dump(decode_tag( $tag ));          print "TAG $tag ", dump(decode_tag( $tag ));
# Line 582  sub writechunk Line 691  sub writechunk
691  sub as_hex {  sub as_hex {
692          my @out;          my @out;
693          foreach my $str ( @_ ) {          foreach my $str ( @_ ) {
694                  my $hex = unpack( 'H*', $str );                  my $hex = uc unpack( 'H*', $str );
695                  $hex =~ s/(..)/$1 /g if length( $str ) > 2;                  $hex =~ s/(..)/$1 /g if length( $str ) > 2;
696                  $hex =~ s/\s+$//;                  $hex =~ s/\s+$//;
697                  push @out, $hex;                  push @out, $hex;
# Line 596  sub read_bytes { Line 705  sub read_bytes {
705          while ( length( $data ) < $len ) {          while ( length( $data ) < $len ) {
706                  my ( $c, $b ) = $port->read(1);                  my ( $c, $b ) = $port->read(1);
707                  die "no bytes on port: $!" unless defined $b;                  die "no bytes on port: $!" unless defined $b;
708                  #warn "## got $c bytes: ", as_hex($b), "\n";                  warn "## got $c bytes: ", as_hex($b), "\n";
709                    last if $c == 0;
710                  $data .= $b;                  $data .= $b;
711          }          }
712          $desc ||= '?';          $desc ||= '?';

Legend:
Removed from v.71  
changed lines
  Added in v.86

  ViewVC Help
Powered by ViewVC 1.1.26