improve documentation
[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::Reader::API';
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 =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 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 Currently tested with only with Omnikey CardMan 5321
28
29 =cut
30
31 sub serial_settings {} # don't open serial
32
33 our $bin = '/rest/cvs/librfid/utils/librfid-tool';
34
35 sub init {
36         my $self = shift;
37         if ( -e $bin ) {
38                 warn "# using $bin";
39                 return 1;
40         } else {
41                 warn "# no $bin found\n";
42                 return 0;
43         }
44 }
45
46 sub _grep_tool {
47         my ( $param, $coderef ) = @_;
48
49         warn "# _grep_tool $bin $param\n";
50         open(my $s, '-|', "$bin $param") || die $!;
51         while(<$s>) {
52                 chomp;
53                 warn "## $_\n";
54
55                 my $sid;
56                 if ( m/success.+:\s+(.+)/ ) {
57                         $sid = $1;
58                         $sid =~ s/\s*'\s*//g;
59                         $sid = uc join('', reverse split(/\s+/, $sid));
60                 }
61
62                 $coderef->( $sid );
63         }
64
65
66 }
67
68 sub inventory {
69
70         my @tags; 
71         _grep_tool '--scan' => sub {
72                 my $sid = shift;
73                 push @tags, $sid if $sid;
74         };
75         warn "# invetory ",dump(@tags);
76         return @tags;
77 }
78
79 sub read_blocks {
80
81         my $sid;
82         my $blocks;
83         _grep_tool '--read -1' => sub {
84                 $sid ||= shift;
85                 $blocks->{$sid}->[$1] = hex2bytes($2)
86                 if m/block\[\s*(\d+):.+data.+:\s*(.+)/;
87
88         };
89         warn "# read_blocks ",dump($blocks);
90         return $blocks;
91 }
92
93 sub write_blocks {}
94 sub read_afi { -1 }
95 sub write_afi {}
96
97 1