X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=lib%2FBiblio%2FRFID%2FReader%2Flibrfid.pm;h=8f6a96da6e9ab56b8e393049f0b282198b6f2a73;hb=f0440178e57e4a3fc985fc6670bda920600f78c6;hp=40005f7741ea8b037dfa75e725f19282065100ef;hpb=75c0bcf89f52d01dd630a27d30e7a4d84219c2b2;p=Biblio-RFID.git diff --git a/lib/Biblio/RFID/Reader/librfid.pm b/lib/Biblio/RFID/Reader/librfid.pm index 40005f7..8f6a96d 100644 --- a/lib/Biblio/RFID/Reader/librfid.pm +++ b/lib/Biblio/RFID/Reader/librfid.pm @@ -22,6 +22,10 @@ Due to limitation of L only L and L is supported. +This version uses modidified C which supports +new C<-s> flag which reads sectors 0-3 with C<-k> key and +sectors 4-7 with key from C<-s> option. + However, this code might provide template for integration with any command-line utilities for different RFID readers. @@ -40,8 +44,20 @@ sub init { 1 } sub _grep_tool { my ( $bin, $param, $coderef, $path ) = @_; + my $timeout = 3; # s + + eval { + warn "# _grep_tool $bin $param\n"; - open(my $s, '-|', "$bin $param") || die $!; +# $param .= ' 2>/dev/null'; + my $pipe_pid = open(my $s, '-|', "$bin $param") || die $!; + + local $SIG{ALRM} = sub { + warn "TIMEOUT!"; + kill TERM => $pipe_pid; + die "timeout\n" + }; + alarm $timeout; my $sid; my $iso; @@ -61,6 +77,21 @@ sub _grep_tool { $coderef->( $sid, $iso ); } + alarm(0); + close($s); + + }; # eval + + if ( $? >> 8 || $@ ) { + my $lsusb = `lsusb -d 076b:`; + if ( $lsusb =~ m/\S+\s+(\d+)\s+\S+\s+(\d+)/ ) { + my $cmd = "usbreset /dev/bus/usb/$1/$2"; + warn "# $cmd\n"; + system $cmd; + } else { + warn "can't reset device $lsusb"; + } + } } @@ -69,10 +100,11 @@ my $sid_iso; sub inventory { my @tags; + $sid_iso = {}; _grep_tool 'librfid-tool', '--scan' => sub { my ( $sid, $iso ) = @_; - if ( $sid ) { - push @tags, $sid unless defined $sid_iso->{$sid}; + if ( $sid && ! exists $sid_iso->{$sid} ) { + push @tags, $sid; $sid_iso->{$sid} = $iso; } }; @@ -113,6 +145,7 @@ sub read_blocks { } else { read_mifare_keys unless $mifare_keys; +=for unmodified mifate-tool foreach my $sector ( keys %$mifare_keys ) { my $key = lc $mifare_keys->{$sector}; _grep_tool 'mifare-tool', "-k $key -r $sector" => sub { @@ -120,6 +153,11 @@ sub read_blocks { if m/data=\s*(.+)/; }; } +=cut + _grep_tool 'mifare-tool', "-k " . $mifare_keys->{0} . " -s " . $mifare_keys->{4} => sub { + $blocks->{$sid}->[$1] = hex2bytes($2) + if m/page=(\d+).*data=\s*(.+)/; + }; } warn "# read_blocks ",dump($blocks); return $blocks;