File Coverage

blib/lib/RFID/Matrics/Tag.pm
Criterion Covered Total %
statement 40 45 88.8
branch 3 6 50.0
condition 1 3 33.3
subroutine 10 12 83.3
pod 2 4 50.0
total 56 70 80.0


line stmt bran cond sub pod time code
1             package RFID::Matrics::Tag;
2             @ISA = qw(RFID::Tag Exporter);
3 4     4   27 use RFID::Matrics::Reader; $VERSION=$RFID::Matrics::Reader::VERSION;
  4         12  
  4         210  
4 4     4   26 use RFID::Tag qw(tagcmp);
  4         8  
  4         225  
5 4     4   21 use Exporter;
  4         8  
  4         155  
6            
7             # Written by Scott Gifford
8             # Copyright (C) 2004 The Regents of the University of Michigan.
9             # See the file LICENSE included with the distribution for license
10             # information.
11            
12             =head1 NAME
13            
14             RFID::Matrics::Tag - Object representing a single proprietary Matrics tag.
15            
16             =head1 SYNOPSIS
17            
18             These objects are usually returned by an
19             L object:
20            
21             use RFID::Matrics::Tag qw(tag2txt);
22            
23             my $rff = RFID::Matrics::Reader->new->readfullfield(antenna => MATRICS_ANT_1);
24             foreach my $tag (@{$pp->{utags}})
25             {
26             print "I see tag $tag->{id}\n";
27             }
28            
29             But you can create your own if you want:
30            
31             my $tag = RFID::Matrics::Tag->new(type => MATRICS_TAGTYPE_EPC,
32             id = "c80507a8009609de");
33             print "Tag is $tag->{id}\n";
34            
35             =head1 DESCRIPTION
36            
37             =cut
38            
39 4     4   23 use strict;
  4         7  
  4         340  
40            
41 4     4   23 use vars qw(@EXPORT_OK %EXPORT_TAGS);
  4         7  
  4         319  
42             @EXPORT_OK=(@RFID::Tag::EXPORT_OK);
43            
44             =head2 Constants
45            
46             =head3 Tag Type Constants
47            
48             The constants C, C, and
49             C are recognized tag types. They can be
50             imported into your namespace with the C<:tagtypes> tag.
51            
52             =cut
53            
54 4     4   34 use constant MATRICS_TAGTYPE_EPC => 0;
  4         20  
  4         232  
55 4     4   21 use constant MATRICS_TAGTYPE_MATRICS => 1;
  4         6  
  4         392  
56 4     4   21 use constant MATRICS_TAGTYPE_OLDMATRICS => 2;
  4         7  
  4         2245  
57            
58             =head2 Constructor
59            
60             =head3 new
61            
62             Creates a new I object. Takes a hash containing
63             various settings as its parameters:
64            
65             =over 4
66            
67             =item id_bits
68            
69             A binary string containing the tag's ID. This is the representation
70             used natively by the reader; it will be automatically generated if it
71             is not given but I is.
72            
73             =item id
74            
75             A hex string containing the tag's ID. This is the human-readable
76             representation; it will be automatically generated if it is not given
77             but I is.
78            
79             =item type
80            
81             The type of tag this is. See the I section of this page
82             for recognized tag types.
83            
84             =back
85            
86             =cut
87            
88             sub new
89             {
90 16     16 1 260 my $class = shift;
91 16         72 my(%p)=@_;
92 16         30 my $self = {};
93            
94 16         41 $self->{id_bits} = $p{id_bits};
95 16         110 $self->{id} = $p{id};
96 16 50 33     88 if ($self->{id_bits} && $self->{id})
    50          
    50          
97             {
98             # Do nothing.
99             }
100             elsif ($self->{id_bits})
101             {
102 0         0 $self->{id} = tag2txt($self->{id_bits});
103             }
104             elsif ($self->{id})
105             {
106 16         39 $self->{id_bits} = txt2tag($self->{id});
107             }
108            
109 16         56 $self->{len}=length($self->{id_bits});
110 16         29 $self->{type} = $p{type};
111 16         729 bless $self,$class;
112 16         303 $self->_init(%p);
113 16         401 $self;
114             }
115            
116             sub type
117             {
118             # my $self = shift;
119 0     0 1 0 return 'matrics';
120             }
121            
122             =head2 Utility Functions
123            
124             =head3 tagcmp
125            
126             A comparison function for C. Compares the ID numbers of two
127             tags, and returns -1 if the first ID is lower, 0 if they are the same,
128             or 1 if the first ID is higher.
129            
130             =cut
131            
132             sub tag2txt
133             {
134 0     0 0 0 my @a = split(//,$_[0]);
135 0         0 sprintf "%02x" x scalar(@a), reverse map {ord} @a;
  0         0  
136             }
137            
138             sub txt2tag
139             {
140 16     16 0 24 my $hex = $_[0];
141 16         27 $hex =~ tr/0-9a-fA-F//cd;
142 16         95 pack("C*",map { hex } reverse unpack("a2"x(length($hex)/2),$hex));
  128         317  
143             }
144            
145             =head1 SEE ALSO
146            
147             L, L, L,
148             L.
149            
150             =head1 AUTHOR
151            
152             Scott Gifford ,
153            
154             Copyright (C) 2004 The Regents of the University of Michigan.
155            
156             See the file LICENSE included with the distribution for license
157             information.
158            
159             =cut
160            
161             1;
162