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