rename Reader implementation all over [0.02]
[Biblio-RFID.git] / lib / RFID / Biblio / Reader / librfid.pm
1 package RFID::Biblio::Reader::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::Reader::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         if ( -e $bin ) {
36                 warn "# using $bin";
37                 return 1;
38         } else {
39                 warn "# no $bin found\n";
40                 return 0;
41         }
42 }
43
44 sub _grep_tool {
45         my ( $param, $coderef ) = @_;
46
47         warn "# _grep_tool $bin $param\n";
48         open(my $s, '-|', "$bin $param") || die $!;
49         while(<$s>) {
50                 chomp;
51                 warn "## $_\n";
52
53                 my $sid;
54                 if ( m/success.+:\s+(.+)/ ) {
55                         $sid = $1;
56                         $sid =~ s/\s*'\s*//g;
57                         $sid = uc join('', reverse split(/\s+/, $sid));
58                 }
59
60                 $coderef->( $sid );
61         }
62
63
64 }
65
66 sub inventory {
67
68         my @tags; 
69         _grep_tool '--scan' => sub {
70                 my $sid = shift;
71                 push @tags, $sid if $sid;
72         };
73         warn "# invetory ",dump(@tags);
74         return @tags;
75 }
76
77 sub read_blocks {
78
79         my $sid;
80         my $blocks;
81         _grep_tool '--read -1' => sub {
82                 $sid ||= shift;
83                 $blocks->{$sid}->[$1] = hex2bytes($2)
84                 if m/block\[\s*(\d+):.+data.+:\s*(.+)/;
85
86         };
87         warn "# read_blocks ",dump($blocks);
88         return $blocks;
89 }
90
91 sub write_blocks {}
92 sub read_afi { -1 }
93 sub write_afi {}
94
95 1