improve pod
[Biblio-RFID.git] / lib / RFID / Biblio.pm
1 package RFID::Biblio;
2
3 use warnings;
4 use strict;
5
6 use base 'Exporter';
7 our @EXPORT = qw( hex2bytes as_hex hex_tag );
8
9 use Device::SerialPort qw(:STAT);
10 use Data::Dump qw(dump);
11
12 =head1 NAME
13
14 RFID::Biblio - easy to use API for writing RFID enabled appliaction
15
16 =cut
17
18 our $VERSION = '0.01';
19
20 my $debug = 0;
21
22
23 =head1 SYNOPSIS
24
25 This module tries to support USB serial RFID readers wsing simple API
26 which is sutable for direct mapping to REST JSONP service.
27
28 Perhaps a little code snippet.
29
30         use RFID::Biblio;
31
32         my $rfid = RFID::Biblio->new(
33                 device => '/dev/ttyUSB0', # with fallback to RFID_DEVICE
34         );
35
36         # invetory tags in reader range and read data from them
37         my $visible = $rfid->scan;
38
39 =head1 METHODS
40
41 =head2 new
42
43 Open serial port (if needed) and init reader
44
45 =cut
46
47 sub new {
48         my $class = shift;
49         my $self = {@_};
50         bless $self, $class;
51
52         $self->port;
53
54         $self->init;
55
56         return $self;
57 }
58
59 =head2 port
60
61   my $serial_obj = $self->port;
62
63 =cut
64
65 sub port {
66         my $self = shift;
67
68         return $self->{port} if defined $self->{port};
69
70         my $settings = $self->serial_settings;
71         my $device   = $settings->{device} ||= $ENV{RFID_DEVICE};
72         warn "# settings ",dump $settings;
73
74         if ( ! $device ) {
75                 warn "# no device, serial port not opened\n";
76                 return;
77         }
78
79         $self->{port} = Device::SerialPort->new( $settings->{device} )
80         || die "can't open serial port: $!\n";
81
82         $self->{port}->$_( $settings->{$_} )
83         foreach ( qw/handshake baudrate databits parity stopbits/ );
84
85 }
86
87 =head2 scan
88
89   my $visible = $rfid->scan;
90
91 Returns hash with keys which match tag UID and values with blocks
92
93 =cut
94
95 sub scan {
96         my $self = shift;
97
98         warn "# scan tags in reader range\n";
99         my @tags = $self->inventory;
100
101         my $visible;
102         # FIXME this is naive implementation which just discards other tags
103         foreach my $tag ( @tags ) {
104                 my $blocks = $self->read_blocks( $tag );
105                 if ( ! $blocks ) {
106                         warn "ERROR: can't read tag $tag\n";
107                         delete $visible->{$tag};
108                 } else {
109                         $visible->{$tag} = $blocks->{$tag};
110                 }
111         }
112
113         return $visible;
114 }
115
116
117 =head1 READER IMPLEMENTATION
118
119 Each reader must implement following hooks as sub-classes.
120
121 =head2 init
122
123   $self->init;
124
125 =head2 inventory
126
127   my @tags = $self->invetory;
128
129 =head2 read_blocks
130
131   my $hash = $self->read_blocks( $tag );
132
133 All blocks are under key which is tag UID with array of blocks returned from reader
134
135   $hash = { 'E000000123456789' => [ 'blk1', 'blk2', ... ] };
136
137 L<RFID::Biblio::3M810> sends tag UID with data payload, so we might expect
138 to receive response from other tags from protocol specification, 
139
140 =head2 write_blocks
141
142   $self->write_blocks( $tag => $bytes );
143
144   $self->write_blocks( $tag => [ 'blk1', 'blk2', ... ] );
145
146 =head2 read_afi
147
148   my $afi = $self->read_afi( $tag );
149
150 =head2 write_afi
151
152   $self->write_afi( $tag => $afi );
153
154
155
156 =head1 EXPORT
157
158 Formatting functions are exported
159
160 =head2 hex2bytes
161
162   my $bytes = hex2bytes($hex);
163
164 =cut
165
166 sub hex2bytes {
167         my $str = shift || die "no str?";
168         my $b = $str;
169         $b =~ s/\s+//g;
170         $b =~ s/(..)/\\x$1/g;
171         $b = "\"$b\"";
172         my $bytes = eval $b;
173         die $@ if $@;
174         warn "## str2bytes( $str ) => $b => ",as_hex($bytes) if $debug;
175         return $bytes;
176 }
177
178 =head2 as_hex
179
180   print as_hex( $bytes );
181
182 =cut
183
184 sub as_hex {
185         my @out;
186         foreach my $str ( @_ ) {
187                 my $hex = uc unpack( 'H*', $str );
188                 $hex =~ s/(..)/$1 /g if length( $str ) > 2;
189                 $hex =~ s/\s+$//;
190                 push @out, $hex;
191         }
192         return join(' | ', @out);
193 }
194
195 =head2 hex_tag
196
197   print hex_tag $8bytes;
198
199 =cut
200
201 sub hex_tag { uc(unpack('H16', shift)) }
202
203
204 =head1 SUPPORTED READERS
205
206 Support for different RFID readers is implemented in subclasses:
207
208 =head2 3M 810
209
210 L<RFID::Biblio::3M810>
211
212 =head2 CPR-M02
213
214 L<RFID::Biblio::CPRM02>
215
216 =head2 librfid
217
218 L<RFID::Biblio::librfid>
219
220 =head1 AUTHOR
221
222 Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>
223
224 =head1 BUGS
225
226 Please report any bugs or feature requests to C<bug-rfid-serial at rt.cpan.org>, or through
227 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=RFID-Biblio>.  I will be notified, and then you'll
228 automatically be notified of progress on your bug as I make changes.
229
230
231
232
233 =head1 SUPPORT
234
235 You can find documentation for this module with the perldoc command.
236
237     perldoc RFID::Biblio
238
239
240 You can also look for information at:
241
242 =over 4
243
244 =item * RT: CPAN's request tracker
245
246 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=RFID-Biblio>
247
248 =item * AnnoCPAN: Annotated CPAN documentation
249
250 L<http://annocpan.org/dist/RFID-Biblio>
251
252 =item * CPAN Ratings
253
254 L<http://cpanratings.perl.org/d/RFID-Biblio>
255
256 =item * Search CPAN
257
258 L<http://search.cpan.org/dist/RFID-Biblio/>
259
260 =back
261
262
263 =head1 ACKNOWLEDGEMENTS
264
265
266 =head1 LICENSE AND COPYRIGHT
267
268 Copyright 2010 Dobrica Pavlinusic.
269
270 This program is free software; you can redistribute it and/or modify
271 it under the terms of the GNU General Public License as published by
272 the Free Software Foundation; version 2 dated June, 1991 or at your option
273 any later version.
274
275 This program is distributed in the hope that it will be useful,
276 but WITHOUT ANY WARRANTY; without even the implied warranty of
277 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
278 GNU General Public License for more details.
279
280 A copy of the GNU General Public License is available in the source tree;
281 if not, write to the Free Software Foundation, Inc.,
282 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
283
284
285 =cut
286
287 1; # End of RFID::Biblio