unify read_blocks and invetory interface
authorDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 26 Jul 2010 21:34:18 +0000 (23:34 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 26 Jul 2010 21:34:18 +0000 (23:34 +0200)
lib/RFID/Serial.pm
lib/RFID/Serial/3M810.pm
lib/RFID/Serial/CPRM02.pm
t/10-3M-810.t
t/20-CPR-M02.t

index e30f363..9c32a6d 100644 (file)
@@ -3,6 +3,9 @@ package RFID::Serial;
 use warnings;
 use strict;
 
+use base 'Exporter';
+our @EXPORT = qw( hex2bytes as_hex hex_tag );
+
 use Device::SerialPort qw(:STAT);
 use Data::Dump qw(dump);
 
@@ -16,9 +19,6 @@ our $VERSION = '0.01';
 
 my $debug = 0;
 
-use base 'Exporter';
-our @EXPORT = qw( hex2bytes as_hex );
-
 
 =head1 SYNOPSIS
 
@@ -111,6 +111,14 @@ sub as_hex {
        return join(' | ', @out);
 }
 
+=head2 hex_tag
+
+  print hex_tag $bytes;
+
+=cut
+
+sub hex_tag { uc(unpack('H16', shift)) }
+
 
 =head1 AUTHOR
 
index 5aaf78b..41e125c 100644 (file)
@@ -106,11 +106,15 @@ cmd(
 )});
 }
 
-sub tag_hex { uc(unpack('H16', shift)) }
+=head2 inventory
+
+  my @tags = inventory;
+
+=cut
 
 sub inventory {
 
-       my $inventory;
+       my @tags;
 
 cmd( 'FE  00 05', 'scan for tags', sub {
        my $data = shift;
@@ -125,14 +129,14 @@ 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 = tag_hex substr($tags, $_ * 8, 8);
-                       $invetory->{$tag} ||= read_tag($tag);
+                       push @tags, tag_hex substr($tags, $_ * 8, 8);
                }
        }
 
 });
 
-       return $invetory;
+       warn "# tags ",dump @tags;
+       return @tags;
 }
 
 
@@ -151,18 +155,18 @@ sub _matched {
        }
 }
 
-sub read_tag {
+sub read_blocks {
        my $tag = shift || confess "no tag?";
-       warn "# read $tag\n";
+       $tag = shift if ref($tag);
 
        my $tag_blocks;
        my $start = 0;
        cmd(
-                sprintf( "02 $tag %02x %02x", $start, $blocks ) => "read $tag $start/$blocks", sub {
+                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 = tag_hex substr($rest,0,8);
+                               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 ) {
@@ -176,6 +180,8 @@ sub read_tag {
                                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);
                        }
        });
 
index 9613c90..acefc54 100644 (file)
@@ -6,7 +6,7 @@ use RFID::Serial;
 use Time::HiRes;
 use Data::Dump qw(dump);
 
-my $debug = 0;
+my $debug = 1;
 
 sub serial_settings {{
        device    => "/dev/ttyUSB0",
@@ -99,13 +99,13 @@ cpr( 'FF  69',              'RF Reset' );
 
 }
 
-sub cpr_read {
-       my $uid = shift;
-       my $hex_uid = as_hex($uid);
+sub read_blocks {
+       my $tag = shift;
+       $tag = shift if ref $tag;
 
        my $max_block;
 
-       cpr( "FF  B0 2B  01  $hex_uid", "Get System Information $hex_uid", sub {
+       cpr( "FF  B0 2B  01  $tag", "Get System Information $tag", sub {
                my $data = shift;
 
                warn "# data ",as_hex($data);
@@ -122,11 +122,11 @@ sub cpr_read {
                $max_block = ord($SIZE);
        });
 
-       my $transponder_data;
+       my $tag_blocks;
 
        my $block = 0;
        while ( $block < $max_block ) {
-               cpr( sprintf("FF  B0 23  01  $hex_uid %02x 04", $block), "Read Multiple Blocks $block", sub {
+               cpr( sprintf("FF  B0 23  01  $tag %02x 04", $block), "Read Multiple Blocks $block", sub {
                        my $data = shift;
 
                        my $DB_N    = ord substr($data,5-2,1);
@@ -134,26 +134,26 @@ sub cpr_read {
 
                        $data = substr($data,7-2,-2);
 #                      warn "# DB N: $DB_N SIZE: $DB_SIZE ", as_hex( $data ), " transponder_data: [$transponder_data] ",length($transponder_data),"\n";
-                       foreach ( 1 .. $DB_N ) {
-                               my $sec = substr($data,0,1);
+                       foreach my $n ( 1 .. $DB_N ) {
+                               my $sec = ord(substr($data,0,1));
                                my $db  = substr($data,1,$DB_SIZE);
-                               warn "## block $_ ",dump( $sec, $db ) if $debug;
-                               $transponder_data .= reverse split(//,$db);
+                               warn "## block $n ",dump( $sec, $db ) if $debug;
+                               $tag_blocks->{$tag}->[$block+$n] = reverse split(//,$db);
                                $data = substr($data, $DB_SIZE + 1);
                        }
                });
                $block += 4;
        }
 
-       warn "# DATA $hex_uid ", dump($transponder_data);
-       return $transponder_data;
+       warn "# tag_blocks ",dump($tag_blocks),$/;
+       return $tag_blocks;
 }
 
 
 
 sub inventory {
 
-my $inventory;
+       my @tags;
 
 cpr( 'FF  B0  01 00', 'ISO - Inventory', sub {
        my $data = shift;
@@ -161,6 +161,7 @@ cpr( 'FF  B0  01 00', 'ISO - Inventory', sub {
                warn "# no tags in range\n";
                return;
        }
+
        my $data_sets = ord(substr($data,3,1));
        $data = substr($data,4);
        foreach ( 1 .. $data_sets ) {
@@ -170,14 +171,13 @@ cpr( 'FF  B0  01 00', 'ISO - Inventory', sub {
                my $uid     = substr($data,2,8);
                $data = substr($data,10);
                warn "# TAG $_ ",as_hex( $tr_type, $dsfid, $uid ),$/;
-
-               $inventory->{$uid} ||= cpr_read( $uid );
+               push @tags, hex_tag $uid;
                
        }
-       warn "# inventory: ",dump($inventory);
 });
 
-       return $inventory;
+       warn "# tags ",dump(@tags),$/;
+       return @tags;
 }
 
 1
index 66938c1..7a737a7 100755 (executable)
@@ -11,5 +11,7 @@ BEGIN {
 
 ok( my $o = RFID::Serial::3M810->new( device => '/dev/ttyUSB0' ), 'new' );
 
-ok( my $inventory = $o->inventory, 'inventory' );
+ok( my @tags = $o->inventory, 'inventory' );
+diag dump @tags;
 
+ok( my $blocks = $o->read_blocks( $_ ), "read_blocks $_" ) foreach @tags;
index f8b8f4c..54c4a5d 100755 (executable)
@@ -11,6 +11,7 @@ BEGIN {
 
 ok( my $o = RFID::Serial::CPRM02->new( device => '/dev/ttyUSB0' ), 'new' );
 
-ok( $o->inventory, 'inventory' );
+ok( my @tags = $o->inventory, 'inventory' );
 
+ok( my $blocks = $o->read_blocks( $_ ), "read_blocks $_" ) foreach @tags;