rename module to Biblio::RFID
[Biblio-RFID.git] / lib / RFID / Biblio / Reader / librfid.pm
1 package Biblio::RFID::Reader::librfid;
2
3 use warnings;
4 use strict;
5
6 use base 'Biblio::RFID::Reader::API';
7 use Biblio::RFID;
8
9 use Data::Dump qw(dump);
10
11 =head1 NAME
12
13 Biblio::RFID::Reader::librfid - execute librfid-tool
14
15 =head1 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 L<librfid-tool> only
22 L<Biblio::RFID::Reader::API/inventory> and
23 L<Biblio::RFID::Reader::API/read_blocks> is supported.
24
25 However, this code might provide template for integration
26 with any command-line utilities for different RFID readers.
27
28 Currently tested with only with Omnikey CardMan 5321 which
29 has problems. After a while it stops responding to commands
30 by C<librfid-tool> so I provided small C program to reset it:
31
32 C<examples/usbreset.c>
33
34 =cut
35
36 sub serial_settings {} # don't open serial
37
38 our $bin = '/rest/cvs/librfid/utils/librfid-tool';
39
40 sub init {
41         my $self = shift;
42         if ( -e $bin ) {
43                 warn "# using $bin";
44                 return 1;
45         } else {
46                 warn "# no $bin found\n";
47                 return 0;
48         }
49 }
50
51 sub _grep_tool {
52         my ( $param, $coderef ) = @_;
53
54         warn "# _grep_tool $bin $param\n";
55         open(my $s, '-|', "$bin $param") || die $!;
56         while(<$s>) {
57                 chomp;
58                 warn "## $_\n";
59
60                 my $sid;
61                 if ( m/success.+:\s+(.+)/ ) {
62                         $sid = $1;
63                         $sid =~ s/\s*'\s*//g;
64                         $sid = uc join('', reverse split(/\s+/, $sid));
65                 }
66
67                 $coderef->( $sid );
68         }
69
70
71 }
72
73 sub inventory {
74
75         my @tags; 
76         _grep_tool '--scan' => sub {
77                 my $sid = shift;
78                 push @tags, $sid if $sid;
79         };
80         warn "# invetory ",dump(@tags);
81         return @tags;
82 }
83
84 sub read_blocks {
85
86         my $sid;
87         my $blocks;
88         _grep_tool '--read -1' => sub {
89                 $sid ||= shift;
90                 $blocks->{$sid}->[$1] = hex2bytes($2)
91                 if m/block\[\s*(\d+):.+data.+:\s*(.+)/;
92
93         };
94         warn "# read_blocks ",dump($blocks);
95         return $blocks;
96 }
97
98 sub write_blocks {}
99 sub read_afi { -1 }
100 sub write_afi {}
101
102 1