/[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 65 by dpavlin, Thu Feb 11 13:23:26 2010 UTC cpr-m02.pl revision 91 by dpavlin, Fri Jul 16 16:34:13 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 84  sub http_server { Line 85  sub http_server {
85                                          $d->{security} = $tags_security->{$_};                                          $d->{security} = $tags_security->{$_};
86                                          push @{ $json->{tags} },  $d;                                          push @{ $json->{tags} },  $d;
87                                  } keys %$tags;                                  } keys %$tags;
88                                  print $client "HTTP/1.0 200 OK\r\nContent-Type: application/x-javascript\r\n\r\n",                                  print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
89                                          $param->{callback}, "(", to_json($json), ")\r\n";                                          $param->{callback}, "(", to_json($json), ")\r\n";
90                          } elsif ( $method =~ m{/program} ) {                          } elsif ( $method =~ m{/program} ) {
91    
92                                  my $status = 501; # Not implementd                                  my $status = 501; # Not implementd
93    
94                                  foreach my $p ( keys %$param ) {                                  foreach my $p ( keys %$param ) {
95                                          next unless $p =~ m/^tag_(\S+)/;                                          next unless $p =~ m/^(E[0-9A-F]{15})$/;
96                                          my $tag = $1;                                          my $tag = $1;
97                                          my $content = "\x04\x11\x00\x01" . $param->{$p};                                          my $content = "\x04\x11\x00\x01" . $param->{$p};
98                                          $content = "\x00" if $param->{$p} eq 'blank';                                          $content = "\x00" if $param->{$p} eq 'blank';
# Line 99  sub http_server { Line 100  sub http_server {
100    
101                                          warn "PROGRAM $tag $content\n";                                          warn "PROGRAM $tag $content\n";
102                                          write_tag( $tag, $content );                                          write_tag( $tag, $content );
103                                            secure_tag_with( $tag, $param->{$p} =~ /^130/ ? 'DA' : 'D7' );
104                                  }                                  }
105    
106                                  print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";                                  print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
107    
108                            } elsif ( $method =~ m{/secure(.js)} ) {
109    
110                                    my $json = $1;
111    
112                                    my $status = 501; # Not implementd
113    
114                                    foreach my $p ( keys %$param ) {
115                                            next unless $p =~ m/^(E[0-9A-F]{15})$/;
116                                            my $tag = $1;
117                                            my $data = $param->{$p};
118                                            $status = 302;
119    
120                                            warn "SECURE $tag $data\n";
121                                            secure_tag_with( $tag, $data );
122                                    }
123    
124                                    if ( $json ) {
125                                            print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n",
126                                                    $param->{callback}, "({ ok: 1 })\r\n";
127                                    } else {
128                                            print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n";
129                                    }
130    
131                          } else {                          } else {
132                                  print $client "HTTP/1.0 404 Unkown method\r\n";                                  print $client "HTTP/1.0 404 Unkown method\r\n\r\n";
133                          }                          }
134                  } else {                  } else {
135                          print $client "HTTP/1.0 500 No method\r\n";                          print $client "HTTP/1.0 500 No method\r\n\r\n";
136                  }                  }
137                  close $client;                  close $client;
138          }          }
# Line 131  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 144  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 239  $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_psst_wait {
291            # Protocol Start Synchronization Time (PSST): 5ms < data timeout 12 ms
292            Time::HiRes::sleep 0.005;
293    }
294    
295    sub cpr {
296            my ( $hex, $description, $coderef ) = @_;
297            my $bytes = str2bytes($hex);
298            my $len = pack( 'c', length( $bytes ) + 3 );
299            my $send = $len . $bytes;
300            my $checksum = cpr_m02_checksum($send);
301            $send .= $checksum;
302    
303            warn ">> ", as_hex( $send ), "\t\t[$description]\n";
304            $port->write( $send );
305    
306            cpr_psst_wait;
307    
308            my $r_len = $port->read(1);
309    
310            while ( ! $r_len ) {
311                    warn "# wait for response length 5ms\n";
312                    cpr_psst_wait;
313                    $r_len = $port->read(1);
314            }
315    
316            my $data_len = ord($r_len) - 1;
317            my $data = $port->read( $data_len );
318            warn "<< ", as_hex( $r_len . $data ),"\n";
319    
320            cpr_psst_wait;
321    
322            $coderef->( $data ) if $coderef;
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    
337    sub cpr_read {
338            my $uid = shift;
339            my $hex_uid = as_hex($uid);
340    
341            my $max_block;
342    
343            cpr( "FF  B0 2B  01  $hex_uid", "Get System Information $hex_uid", sub {
344                    my $data = shift;
345    
346                    warn "# data ",as_hex($data);
347    
348                    my $DSFID    = substr($data,5-2,1);
349                    my $UID      = substr($data,6-2,8);
350                    my $AFI      = substr($data,14-2,1);
351                    my $MEM      = substr($data,15-2,1);
352                    my $SIZE     = substr($data,16-2,1);
353                    my $IC_REF   = substr($data,17-2,1);
354    
355                    warn "# split ",as_hex( $DSFID, $UID, $AFI, $MEM, $SIZE, $IC_REF );
356    
357                    $max_block = ord($SIZE);
358            });
359    
360            my $transponder_data;
361    
362            my $block = 0;
363            while ( $block < $max_block ) {
364                    cpr( sprintf("FF  B0 23  01  $hex_uid %02x 04", $block), "Read Multiple Blocks $block", sub {
365                            my $data = shift;
366    
367                            my $DB_N    = ord substr($data,5-2,1);
368                            my $DB_SIZE = ord substr($data,6-2,1);
369    
370                            $data = substr($data,7-2,-2);
371                            warn "# DB N: $DB_N SIZE: $DB_SIZE ", as_hex( $data );
372                            foreach ( 1 .. $DB_N ) {
373                                    my $sec = substr($data,0,1);
374                                    my $db  = substr($data,1,$DB_SIZE);
375                                    warn "block $_ ",dump( $sec, $db );
376                                    $transponder_data .= reverse split(//,$db);
377                                    $data = substr($data, $DB_SIZE + 1);
378                            }
379                    });
380                    $block += 4;
381            }
382    
383            warn "DATA $hex_uid ", dump($transponder_data);
384            exit;
385    }
386    
387    
388    my $inventory;
389    
390    while(1) {
391    
392    cpr( 'FF  B0  01 00', 'ISO - Inventory', sub {
393            my $data = shift;
394            if (length($data) < 5 + 2 ) {
395                    warn "# no tags in range\n";
396                    return;
397            }
398            my $data_sets = ord(substr($data,3,1));
399            $data = substr($data,4);
400            foreach ( 1 .. $data_sets ) {
401                    my $tr_type = substr($data,0,1);
402                    die "FIXME only TR-TYPE=3 ISO 15693 supported" unless $tr_type eq "\x03";
403                    my $dsfid   = substr($data,1,1);
404                    my $uid     = substr($data,2,8);
405                    $inventory->{$uid}++;
406                    $data = substr($data,10);
407                    warn "# TAG $_ ",as_hex( $tr_type, $dsfid, $uid ),$/;
408    
409                    cpr_read( $uid );
410            }
411            warn "inventory: ",dump($inventory);
412    });
413    
414    }
415    
416    #cpr( '', '?' );
417    
418    exit;
419  # initial hand-shake with device  # initial hand-shake with device
420    
421  cmd( 'D5 00  05   04 00 11                 8C66', 'hw version',  cmd( 'D5 00  05   04 00 11                 8C66', 'hw version',
# Line 368  my $saved_in_log; Line 545  my $saved_in_log;
545  sub decode_tag {  sub decode_tag {
546          my $tag = shift;          my $tag = shift;
547    
548          my $data = $tags_data->{$tag} || die "no data for $tag";          my $data = $tags_data->{$tag};
549            if ( ! $data ) {
550                    warn "no data for $tag\n";
551                    return;
552            }
553    
554          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);
555          my $hash = {          my $hash = {
# Line 395  sub decode_tag { Line 576  sub decode_tag {
576          return $hash;          return $hash;
577  }  }
578    
579    sub forget_tag {
580            my $tag = shift;
581            delete $tags_data->{$tag};
582            delete $visible_tags->{$tag};
583    }
584    
585  sub read_tag {  sub read_tag {
586          my ( $tag ) = @_;          my ( $tag ) = @_;
587    
# Line 416  sub read_tag { Line 603  sub read_tag {
603                          "D6 00  0F  FE  00 00  05 01   $tag    BEEF", sub {                          "D6 00  0F  FE  00 00  05 01   $tag    BEEF", sub {
604                                  print "FIXME: tag $tag ready? (expected block read instead)\n";                                  print "FIXME: tag $tag ready? (expected block read instead)\n";
605                          },                          },
606                            "D6 00 0D 02 06 $tag", sub {
607                                    my $rest = shift;
608                                    print "ERROR reading $tag ", as_hex($rest), $/;
609                                    forget_tag $tag;
610                                    $start_block = $max_rfid_block; # XXX break out of while
611                            },
612                  );                  );
613    
614          }          }
# Line 432  sub read_tag { Line 625  sub read_tag {
625                          $security = as_hex( $security );                          $security = as_hex( $security );
626                          $tags_security->{$tag} = $security;                          $tags_security->{$tag} = $security;
627                          warn "# SECURITY $tag = $security\n";                          warn "# SECURITY $tag = $security\n";
628                  }                  },
629                    "D6 00 0C 0A 06", sub {
630                            my $rest = shift;
631                            warn "ERROR reading security from $rest\n";
632                            forget_tag $tag;
633                    },
634          );          );
635    
636          print "TAG $tag ", dump(decode_tag( $tag ));          print "TAG $tag ", dump(decode_tag( $tag ));
# Line 483  sub write_tag { Line 681  sub write_tag {
681          rename $path, $to;          rename $path, $to;
682          print ">> $to\n";          print ">> $to\n";
683    
684          # force re-read of tag          forget_tag $tag;
685          delete $tags_data->{$tag};  }
686          delete $visible_tags->{$tag};  
687    sub secure_tag_with {
688            my ( $tag, $data ) = @_;
689    
690            cmd(
691                    "d6 00  0c  09  $tag $data BEEF", "secure $tag -> $data",
692                    "d6 00  0c  09 00  $tag    BEEF", sub { assert() },
693            );
694    
695            forget_tag $tag;
696  }  }
697    
698  sub secure_tag {  sub secure_tag {
# Line 494  sub secure_tag { Line 701  sub secure_tag {
701          my $path = "$secure_path/$tag";          my $path = "$secure_path/$tag";
702          my $data = substr(read_file( $path ),0,2);          my $data = substr(read_file( $path ),0,2);
703    
704          cmd(          secure_tag_with( $tag, $data );
                 "d6 00  0c  09  $tag $data BEEF", "secure $tag -> $data",  
                 "d6 00  0c  09 00  $tag    BEEF", sub { assert() },  
         );  
705    
706          my $to = $path;          my $to = $path;
707          $to .= '.' . time();          $to .= '.' . time();
# Line 546  sub writechunk Line 750  sub writechunk
750  sub as_hex {  sub as_hex {
751          my @out;          my @out;
752          foreach my $str ( @_ ) {          foreach my $str ( @_ ) {
753                  my $hex = unpack( 'H*', $str );                  my $hex = uc unpack( 'H*', $str );
754                  $hex =~ s/(..)/$1 /g if length( $str ) > 2;                  $hex =~ s/(..)/$1 /g if length( $str ) > 2;
755                  $hex =~ s/\s+$//;                  $hex =~ s/\s+$//;
756                  push @out, $hex;                  push @out, $hex;
# Line 560  sub read_bytes { Line 764  sub read_bytes {
764          while ( length( $data ) < $len ) {          while ( length( $data ) < $len ) {
765                  my ( $c, $b ) = $port->read(1);                  my ( $c, $b ) = $port->read(1);
766                  die "no bytes on port: $!" unless defined $b;                  die "no bytes on port: $!" unless defined $b;
767                  #warn "## got $c bytes: ", as_hex($b), "\n";                  warn "## got $c bytes: ", as_hex($b), "\n";
768                    last if $c == 0;
769                  $data .= $b;                  $data .= $b;
770          }          }
771          $desc ||= '?';          $desc ||= '?';

Legend:
Removed from v.65  
changed lines
  Added in v.91

  ViewVC Help
Powered by ViewVC 1.1.26