implement RFID::Serial->scan and test it
[Biblio-RFID.git] / lib / RFID / Serial.pm
index e30f363..e9435f6 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
 
@@ -32,6 +32,7 @@ Perhaps a little code snippet.
     my $rfid = RFID::Serial->new(
                device => '/dev/ttyUSB0', # with fallback to RFID_DEVICE
        );
+       my $visible = $rfid->scan;
 
 =head1 SUBROUTINES/METHODS
 
@@ -51,6 +52,12 @@ sub new {
        return $self;
 }
 
+=head2 port
+
+  my $serial_obj = $self->port;
+
+=cut
+
 sub port {
        my $self = shift;
 
@@ -68,10 +75,52 @@ sub port {
 
 }
 
-sub init {
-       warn "no init";
+=head2 scan
+
+  my $visible = $rfid->scan;
+
+Returns hash with keys which match tag UID and values with blocks
+
+=cut
+
+sub scan {
+       my $self = shift;
+
+       warn "# scan tags in reader range\n";
+       my @tags = $self->inventory;
+
+       my $visible;
+       # FIXME this is naive implementation which just discards other tags
+       $visible->{$_} = $self->read_blocks( $_ )->{$_} foreach @tags;
+
+       return $visible;
 }
 
+
+=head1 MANDATORY IMPLEMENTATIONS
+
+Each reader must implement following hooks as sub-classes.
+
+=head2 init
+
+  $self->init;
+
+=head2 inventory
+
+  my @tags = $self->invetory;
+
+=head2 read_blocks
+
+  my $hash = $self->read_blocks $tag;
+
+All blocks are under key which is tag UID
+
+  $hash = { 'E000000123456789' => [ undef, 'block1', 'block2', ... ] };
+
+L<RFID::Serial::3M810> sends tag UID with data payload, so we might expect
+to receive response from other tags from protocol specification, 
+
+
 =head1 EXPORT
 
 Formatting functions are exported
@@ -111,6 +160,14 @@ sub as_hex {
        return join(' | ', @out);
 }
 
+=head2 hex_tag
+
+  print hex_tag $8bytes;
+
+=cut
+
+sub hex_tag { uc(unpack('H16', shift)) }
+
 
 =head1 AUTHOR