/[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 35 by dpavlin, Fri Apr 10 12:16:20 2009 UTC revision 40 by dpavlin, Mon Jun 1 21:17:12 2009 UTC
# Line 247  sub read_tag_data { Line 247  sub read_tag_data {
247                  die "got block $ord, expected block $expected_ord from ",dump( $block ) if $ord != $expected_ord;                  die "got block $ord, expected block $expected_ord from ",dump( $block ) if $ord != $expected_ord;
248                  my $data  = substr( $block, 2 );                  my $data  = substr( $block, 2 );
249                  die "data payload should be 4 bytes" if length($data) != 4;                  die "data payload should be 4 bytes" if length($data) != 4;
250                  warn sprintf "## tag %9s %02d %s |%-4s|\n", $tag, $ord, as_hex( $data ), $data;                  warn sprintf "## tag %9s %02d: %s |%-4s|\n", $tag, $ord, as_hex( $data ), $data;
251                  $tag_data_block->{$tag}->[ $ord ] = $data;                  $tag_data_block->{$tag}->[ $ord ] = $data;
252          }          }
253          $tags_data->{ $tag } = join('', @{ $tag_data_block->{$tag} });          $tags_data->{ $tag } = join('', @{ $tag_data_block->{$tag} });
# Line 300  sub read_tag { Line 300  sub read_tag {
300          my $total = ( $set_item & 0x0f );          my $total = ( $set_item & 0x0f );
301          my $branch  = $br_lib >> 20;          my $branch  = $br_lib >> 20;
302          my $library = $br_lib & 0x000fffff;          my $library = $br_lib & 0x000fffff;
303          print "TAG $tag [$u1] set: $set/$total [$u2] type: $type '$content' branch: $branch library: $library custom: $custom security: $security\n";          print "TAG $tag [$u1] set: $set/$total [$u2] type: $type '$content' library: $library branch: $branch custom: $custom security: $security\n";
304    
305  }  }
306    
# Line 310  sub write_tag { Line 310  sub write_tag {
310          my $path = "$program_path/$tag";          my $path = "$program_path/$tag";
311    
312          my $data = read_file( $path );          my $data = read_file( $path );
313            my $hex_data;
314    
315          $data = substr($data,0,16);          if ( $data =~ s{^hex\s+}{} ) {
316                    $hex_data = $data;
317                    $hex_data =~ s{\s+}{}g;
318            } else {
319    
320          my $hex_data = unpack('h*', $data) . ' 00' x ( 16 - length($data) );                  $data .= "\0" x ( 4 - ( length($data) % 4 ) );
321    
322          print "write_tag $tag = $data ",dump( $hex_data );                  my $max_len = 7 * 4;
323    
324                    if ( length($data) > $max_len ) {
325                            $data = substr($data,0,$max_len);
326                            warn "strip content to $max_len bytes\n";
327                    }
328    
329                    $hex_data = unpack('H*', $data);
330            }
331    
332            my $len = length($hex_data) / 2;
333            # pad to block size
334            $hex_data .= '00' x ( 4 - $len % 4 );
335            my $blocks = sprintf('%02x', length($hex_data) / 4);
336    
337            print "write_tag $tag = ",dump( $data ), " [$len/$blocks] == $hex_data\n";
338    
339          cmd(          cmd(
340                  "d6 00  26  04  $tag  00 06 00  04 11 00 01  $hex_data 00 00 00 00  fd3b", "write $tag",                  "d6 00  ff  04  $tag  00 $blocks 00  $hex_data  ffff", "write $tag",
341                  "d6 00  0d  04 00  $tag  06  afb1", sub { assert() },                  "d6 00  0d  04 00  $tag  $blocks  afb1", sub { assert() },
342          ) foreach ( 1 .. 3 ); # xxx 3m software does this three times!          ); # foreach ( 1 .. 3 ); # XXX 3m software does this three times!
343    
344          my $to = $path;          my $to = $path;
345          $to .= '.' . time();          $to .= '.' . time();
# Line 381  sub writechunk Line 400  sub writechunk
400  {  {
401          my $str=shift;          my $str=shift;
402          my $count = $port->write($str);          my $count = $port->write($str);
403            my $len = length($str);
404            die "wrong write length $count != $len in ",as_hex( $str ) if $count != $len;
405          print "#> ", as_hex( $str ), "\t[$count]\n" if $debug;          print "#> ", as_hex( $str ), "\t[$count]\n" if $debug;
406  }  }
407    
# Line 419  sub skip_assert { Line 440  sub skip_assert {
440  sub assert {  sub assert {
441          my ( $from, $to ) = @_;          my ( $from, $to ) = @_;
442    
443            return unless $assert->{expect};
444    
445          $from ||= 0;          $from ||= 0;
446          $to = length( $assert->{expect} ) if ! defined $to;          $to = length( $assert->{expect} ) if ! defined $to;
447    
# Line 447  sub crcccitt { Line 470  sub crcccitt {
470  sub checksum {  sub checksum {
471          my ( $bytes, $checksum ) = @_;          my ( $bytes, $checksum ) = @_;
472    
         my $xor = crcccitt( substr($bytes,1) ); # skip D6  
         warn "## checksum ",dump( $bytes, $xor, $checksum ) if $debug;  
   
473          my $len = ord(substr($bytes,2,1));          my $len = ord(substr($bytes,2,1));
474          my $len_real = length($bytes) - 1;          my $len_real = length($bytes) - 1;
475    
476          if ( $len_real != $len ) {          if ( $len_real != $len ) {
477                  print "length wrong: $len_real != $len\n";                  print "length wrong: $len_real != $len\n";
478                  $bytes = substr($bytes,0,2) . chr($len_real) . substr($bytes,4);                  $bytes = substr($bytes,0,2) . chr($len_real) . substr($bytes,3);
479          }          }
480    
481            my $xor = crcccitt( substr($bytes,1) ); # skip D6
482            warn "## checksum ",dump( $bytes, $xor, $checksum ) if $debug;
483    
484          if ( defined $checksum && $xor ne $checksum ) {          if ( defined $checksum && $xor ne $checksum ) {
485                  print "checksum doesn't match: ", as_hex($xor), " != ", as_hex($checksum), " data: ", as_hex($bytes), "\n";                  print "checksum doesn't match: ", as_hex($xor), " != ", as_hex($checksum), " data: ", as_hex($bytes), "\n";
486                  return $bytes . $xor;                  return $bytes . $xor;
# Line 496  sub readchunk { Line 519  sub readchunk {
519          } sort { length($a) <=> length($b) } keys %$dispatch;          } sort { length($a) <=> length($b) } keys %$dispatch;
520          warn "?? payload dispatch to ",dump( $payload, $dispatch, $to ) if $debug;          warn "?? payload dispatch to ",dump( $payload, $dispatch, $to ) if $debug;
521    
522          if ( defined $to ) {          if ( defined $to && $payload ) {
523                  my $rest = substr( $payload, length($to) );                  my $rest = substr( $payload, length($to) );
524                  warn "## DISPATCH payload to with rest", dump( $payload, $to, $rest ) if $debug;                  warn "## DISPATCH payload to with rest", dump( $payload, $to, $rest ) if $debug;
525                  $dispatch->{ $to }->( $rest );                  $dispatch->{ $to }->( $rest );

Legend:
Removed from v.35  
changed lines
  Added in v.40

  ViewVC Help
Powered by ViewVC 1.1.26