document limitations
[Biblio-RFID.git] / lib / RFID / Biblio / librfid.pm
1 package RFID::Biblio::librfid;
2
3 use warnings;
4 use strict;
5
6 use base 'RFID::Biblio';
7 use RFID::Biblio;
8
9 use Data::Dump qw(dump);
10
11 =head1 NAME
12
13 RFID::Biblio::librfid - execute librfid-tool
14
15 =head2 DESCRIPTION
16
17 This is wrapper around C<librfid-tool> from
18
19 L<http://openmrtd.org/projects/librfid/>
20
21 Due to limitation of C<librfid-tool> only C<inventory> and
22 C<read_blocks> is supported.
23
24 However, this code might provide template for integration
25 with any command-line utilities for different RFID readers.
26
27 =cut
28
29 sub serial_settings {} # don't open serial
30
31 our $bin = '/rest/cvs/librfid/utils/librfid-tool';
32
33 sub init {
34         my $self = shift;
35         warn "# no $bin found\n" if ! -e $bin;
36 }
37
38 sub _grep_tool {
39         my ( $param, $coderef ) = @_;
40
41         warn "# _grep_tool $bin $param\n";
42         open(my $s, '-|', "$bin $param") || die $!;
43         while(<$s>) {
44                 chomp;
45                 warn "## $_\n";
46
47                 my $sid;
48                 if ( m/success.+:\s+(.+)/ ) {
49                         $sid = $1;
50                         $sid =~ s/\s*'\s*//g;
51                         $sid = uc join('', reverse split(/\s+/, $sid));
52                 }
53
54                 $coderef->( $sid );
55         }
56
57
58 }
59
60 sub inventory {
61
62         my @tags; 
63         _grep_tool '--scan' => sub {
64                 my $sid = shift;
65                 push @tags, $sid if $sid;
66         };
67         warn "# invetory ",dump(@tags);
68         return @tags;
69 }
70
71 sub read_blocks {
72
73         my $sid;
74         my $blocks;
75         _grep_tool '--read -1' => sub {
76                 $sid ||= shift;
77                 $blocks->{$sid}->[$1] = hex2bytes($2)
78                 if m/block\[\s*(\d+):.+data.+:\s*(.+)/;
79
80         };
81         warn "# read_blocks ",dump($blocks);
82         return $blocks;
83 }
84
85 sub write_blocks {}
86 sub read_afi {}
87 sub write_afi {}
88
89 1