return $tag_blocks
[Biblio-RFID.git] / lib / RFID / Serial / 3M810.pm
index 67b1168..9b6b618 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,9 +106,15 @@ cmd(
 )});
 }
 
+=head2 inventory
+
+  my @tags = inventory;
+
+=cut
+
 sub inventory {
 
-       my $inventory;
+       my @tags;
 
 cmd( 'FE  00 05', 'scan for tags', sub {
        my $data = shift;
@@ -122,14 +129,64 @@ 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}++;
+                       push @tags, hex_tag substr($tags, $_ * 8, 8);
                }
        }
 
 });
 
-       return $invetory;
+       warn "# tags ",dump @tags;
+       return @tags;
+}
+
+
+# 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_blocks {
+       my $tag = shift || confess "no tag?";
+       $tag = shift if ref($tag);
+
+       my $tag_blocks;
+       my $start = 0;
+       cmd(
+                sprintf( "02 $tag %02x %02x", $start, $blocks ) => "read_blocks $tag $start/$blocks", sub {
+                       my $data = shift;
+                       if ( my $rest = _matched $data => '02 00' ) {
+
+                               my $tag = hex_tag 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);
+                       } else {
+                               die "FIXME unsuported ",as_hex($rest);
+                       }
+       });
+
+       warn "# tag_blocks ",dump($tag_blocks);
+       return $tag_blocks;
 }
 
 1