implemented read_tag with _matched helper
authorDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 26 Jul 2010 20:35:51 +0000 (22:35 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 26 Jul 2010 20:35:51 +0000 (22:35 +0200)
This allow easy matching on beginning of some results in if..elsif
constructs

lib/RFID/Serial/3M810.pm

index 67b1168..5aaf78b 100644 (file)
@@ -3,6 +3,7 @@ package RFID::Serial::3M810;
 use base 'RFID::Serial';
 use RFID::Serial;
 
+use Data::Dump qw(dump);
 use Carp qw(confess);
 use Time::HiRes;
 use Digest::CRC;
@@ -105,6 +106,8 @@ cmd(
 )});
 }
 
+sub tag_hex { uc(unpack('H16', shift)) }
+
 sub inventory {
 
        my $inventory;
@@ -122,8 +125,8 @@ cmd( 'FE  00 05', 'scan for tags', sub {
                die "wrong length $tl for $nr tags: ",dump( $tags ) if $tl =! $nr * 8;
 
                foreach ( 0 .. $nr - 1 ) {
-                       my $tag = uc(unpack('H16', substr($tags, $_ * 8, 8)));
-                       $invetory->{$tag}++;
+                       my $tag = tag_hex substr($tags, $_ * 8, 8);
+                       $invetory->{$tag} ||= read_tag($tag);
                }
        }
 
@@ -132,4 +135,51 @@ cmd( 'FE  00 05', 'scan for tags', sub {
        return $invetory;
 }
 
+
+# 3M defaults: 8,4
+# cards 16, stickers: 8
+my $max_rfid_block = 8;
+my $blocks = 8;
+
+sub _matched {
+       my ( $data, $hex ) = @_;
+       my $b = hex2bytes $hex;
+       my $l = length($b);
+       if ( substr($data,0,$l) eq $b ) {
+               warn "_matched $hex [$l] in ",as_hex($data);
+               return substr($data,$l);
+       }
+}
+
+sub read_tag {
+       my $tag = shift || confess "no tag?";
+       warn "# read $tag\n";
+
+       my $tag_blocks;
+       my $start = 0;
+       cmd(
+                sprintf( "02 $tag %02x %02x", $start, $blocks ) => "read $tag $start/$blocks", sub {
+                       my $data = shift;
+                       if ( my $rest = _matched $data => '02 00' ) {
+
+                               my $tag = tag_hex substr($rest,0,8);
+                               my $blocks = ord(substr($rest,8,1));
+                               warn "# response from $tag $blocks blocks ",as_hex substr($rest,9);
+                               foreach ( 1 .. $blocks ) {
+                                       my $pos = ( $_ - 1 ) * 6 + 9;
+                                       my $nr = unpack('v', substr($rest,$pos,2));
+                                       my $payload = substr($rest,$pos+2,4);
+                                       warn "## pos $pos block $nr ",as_hex($payload), $/;
+                                       $tag_blocks->{$tag}->[$nr] = $payload;
+                               }
+                       } elsif ( my $rest = _matched $data => 'FE 00 00 05 01' ) {
+                               warn "FIXME ready? ",as_hex $test;
+                       } elsif ( my $rest = _matched $data => '02 06' ) {
+                               warn "ERROR ",as_hex($rest);
+                       }
+       });
+
+       warn "# tag_blocks ",dump($tag_blocks);
+}
+
 1