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