use warnings;
use strict;
+use Data::Dump qw(dump);
+
=head1 NAME
RFID::Biblio::RFID501 - RFID Standard for Libraries
$data = join('', @$data) if ref $data eq 'ARRAY';
- warn "## to_hash $data\n";
+ warn "## to_hash ",dump($data);
- my ( $u1, $set_item, $u2, $type, $content, $br_lib, $custom ) = unpack('C4Z16Nl>',$data);
+ my ( $u1, $set_item, $u2, $type, $content, $br_lib, $custom, $zero ) = unpack('C4Z16Nl>l',$data);
my $hash = {
- u1 => $u1, # FIXME
+ u1 => $u1, # FIXME 0x04
set => ( $set_item & 0xf0 ) >> 4,
total => ( $set_item & 0x0f ),
- u2 => $u2, # FIXME
+ u2 => $u2, # FIXME 0x00
type => $type,
type_label => $item_type->{$type},
+
content => $content,
branch => $br_lib >> 20,
custom => $custom,
};
+ warn "expected first byte to be 0x04, not $u1\n" if $u1 != 4;
+ warn "expected third byte to be 0x00, not $u2\n" if $u2 != 0;
+ warn "expected last block to be zero, not $zero\n" if $zero != 0;
+
return $hash;
}
+sub from_hash {
+ my ( $self, $hash ) = @_;
+
+ return pack('C4Z16Nl>l',
+ 0x04,
+ ( $hash->{set} << 4 ) | ( $hash->{total} & 0x0f ),
+ 0x00,
+ $hash->{type},
+
+ $hash->{content},
+
+ ( $hash->{branch} << 20 ) | ( $hash->{library} & 0x000fffff ),
+
+ $hash->{custom},
+ 0x00,
+ );
+}
+
1;
#!/usr/bin/perl
-use Test::More tests => 4;
+use Test::More tests => 7;
use Data::Dump qw(dump);
use lib 'lib';
use_ok( 'RFID::Biblio::RFID501' );
}
-ok( my $hash = RFID::Biblio::RFID501->to_hash( "\x04\x11\x00\x00200912310123\x00\x00\x00\x00" ), 'decode_tag' );
-diag dump $hash;
-
-ok( $hash = RFID::Biblio::RFID501->to_hash( "\x04\x11\x00\x011301234567\x00\x00\x00\x00\x00\x00" ), 'decode_tag' );
-diag dump $hash;
-
-my $tag = [
+my $tags =
+[ [
"\4\21\0\0",
2009,
"0101",
"\xFF\xFF\xFF\xFF",
"\x7F\xFF\xFF\xFF",
"\0\0\0\0",
-];
+],[
+ "\4\21\0\1",
+ 1302,
+ "0037",
+ "67\0\0",
+ "\0\0\0\0",
+ "\0\0\0\0",
+ "\0\0\0\0",
+ "\0\0\0\0",
+] ];
-ok( $hash = RFID::Biblio::RFID501->to_hash( $tag ), 'decode_tag' );
-diag dump $hash;
+foreach my $tag ( @$tags ) {
+
+ ok( $hash = RFID::Biblio::RFID501->to_hash( $tag ), 'to_hash' );
+ diag dump $hash;
+
+ ok( $bytes = RFID::Biblio::RFID501->from_hash( $hash ), 'from_hash' );
+ my $orig = join('', @$tag);
+ cmp_ok( $bytes, 'eq', $orig, 'roundtrip' );
+
+ diag dump( $orig, $bytes );
+
+}