correctly return tags for inventory
[Biblio-RFID.git] / lib / Biblio / RFID / Reader / librfid.pm
index ebb45b9..9fa3387 100644 (file)
@@ -22,6 +22,10 @@ Due to limitation of L<librfid-tool> only
 L<Biblio::RFID::Reader::API/inventory> and
 L<Biblio::RFID::Reader::API/read_blocks> is supported.
 
+This version uses modidified C<librfid-tool> which supports
+new C<-s> flag which reads sectors 0-3 with C<-k> key and
+sectors 4-7 with key from C<-s> option.
+
 However, this code might provide template for integration
 with any command-line utilities for different RFID readers.
 
@@ -41,7 +45,7 @@ sub _grep_tool {
        my ( $bin, $param, $coderef, $path ) = @_;
 
        warn "# _grep_tool $bin $param\n";
-       open(my $s, '-|', "$bin $param") || die $!;
+       open(my $s, '-|', "$bin $param 2>/dev/null") || die $!;
 
        my $sid;
        my $iso;
@@ -72,7 +76,7 @@ sub inventory {
        _grep_tool 'librfid-tool', '--scan' => sub {
                my ( $sid, $iso ) = @_;
                if ( $sid ) {
-                       push @tags, $sid unless defined $sid_iso->{$sid};
+                       push @tags, $sid;
                        $sid_iso->{$sid} = $iso;
                }
        };
@@ -80,6 +84,11 @@ sub inventory {
        return @tags;
 }
 
+sub tag_type {
+       my ( $self, $tag ) = @_;
+       return $sid_iso->{$tag} =~ m/15693/ ? 'RFID501' : 'SmartX';
+}
+
 our $mifare_keys;
 sub read_mifare_keys {
        my $key_path = $0;
@@ -108,6 +117,7 @@ sub read_blocks {
        } else {
                read_mifare_keys unless $mifare_keys;
 
+=for unmodified mifate-tool
                foreach my $sector ( keys %$mifare_keys ) {
                        my $key = lc $mifare_keys->{$sector};
                        _grep_tool 'mifare-tool', "-k $key -r $sector" => sub {
@@ -115,6 +125,11 @@ sub read_blocks {
                                if m/data=\s*(.+)/;
                        };
                }
+=cut
+               _grep_tool 'mifare-tool', "-k " . $mifare_keys->{0} . " -s " . $mifare_keys->{4} => sub {
+                       $blocks->{$sid}->[$1] = hex2bytes($2)
+                       if m/page=(\d+).*data=\s*(.+)/;
+               };
        }
        warn "# read_blocks ",dump($blocks);
        return $blocks;