document public methods
[Biblio-RFID.git] / lib / RFID / Serial.pm
1 package RFID::Serial;
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::Serial - support serial RFID devices
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::Serial;
31
32     my $rfid = RFID::Serial->new(
33                 device => '/dev/ttyUSB0', # with fallback to RFID_DEVICE
34         );
35
36 =head1 SUBROUTINES/METHODS
37
38 =head2 new
39
40 =cut
41
42 sub new {
43         my $class = shift;
44         my $self = {@_};
45         bless $self, $class;
46
47         $self->port;
48
49         $self->init;
50
51         return $self;
52 }
53
54 =head2 port
55
56   my $serial_obj = $self->port;
57
58 =cut
59
60 sub port {
61         my $self = shift;
62
63         return $self->{port} if defined $self->{port};
64
65         my $settings = $self->serial_settings;
66         $settings->{device} ||= $ENV{RFID_DEVICE};
67         warn "# settings ",dump $settings;
68
69         $self->{port} = Device::SerialPort->new( $settings->{device} )
70         || die "can't open serial port: $!\n";
71
72         $self->{port}->$_( $settings->{$_} )
73         foreach ( qw/handshake baudrate databits parity stopbits/ );
74
75 }
76
77 =head1 MANDATORY IMPLEMENTATIONS
78
79 Each reader must implement following hooks as sub-classes.
80
81 =head2 init
82
83   $self->init;
84
85 =head2 inventory
86
87   my @tags = $self->invetory;
88
89 =head2 read_blocks
90
91   my $hash = $self->read_blocks $tag;
92
93 All blocks are under key which is tag UID
94
95   $hash = { 'E000000123456789' => [ undef, 'block1', 'block2', ... ] };
96
97 L<RFID::Serial::3M810> sends tag UID with data payload, so we might expect
98 to receive response from other tags from protocol specification, 
99
100
101 =head1 EXPORT
102
103 Formatting functions are exported
104
105 =head2 hex2bytes
106
107   my $bytes = hex2bytes($hex);
108
109 =cut
110
111 sub hex2bytes {
112         my $str = shift || die "no str?";
113         my $b = $str;
114         $b =~ s/\s+//g;
115         $b =~ s/(..)/\\x$1/g;
116         $b = "\"$b\"";
117         my $bytes = eval $b;
118         die $@ if $@;
119         warn "## str2bytes( $str ) => $b => ",as_hex($bytes) if $debug;
120         return $bytes;
121 }
122
123 =head2 as_hex
124
125   print as_hex( $bytes );
126
127 =cut
128
129 sub as_hex {
130         my @out;
131         foreach my $str ( @_ ) {
132                 my $hex = uc unpack( 'H*', $str );
133                 $hex =~ s/(..)/$1 /g if length( $str ) > 2;
134                 $hex =~ s/\s+$//;
135                 push @out, $hex;
136         }
137         return join(' | ', @out);
138 }
139
140 =head2 hex_tag
141
142   print hex_tag $8bytes;
143
144 =cut
145
146 sub hex_tag { uc(unpack('H16', shift)) }
147
148
149 =head1 AUTHOR
150
151 Dobrica Pavlinusic, C<< <dpavlin at rot13.org> >>
152
153 =head1 BUGS
154
155 Please report any bugs or feature requests to C<bug-rfid-serial at rt.cpan.org>, or through
156 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=RFID-Serial>.  I will be notified, and then you'll
157 automatically be notified of progress on your bug as I make changes.
158
159
160
161
162 =head1 SUPPORT
163
164 You can find documentation for this module with the perldoc command.
165
166     perldoc RFID::Serial
167
168
169 You can also look for information at:
170
171 =over 4
172
173 =item * RT: CPAN's request tracker
174
175 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=RFID-Serial>
176
177 =item * AnnoCPAN: Annotated CPAN documentation
178
179 L<http://annocpan.org/dist/RFID-Serial>
180
181 =item * CPAN Ratings
182
183 L<http://cpanratings.perl.org/d/RFID-Serial>
184
185 =item * Search CPAN
186
187 L<http://search.cpan.org/dist/RFID-Serial/>
188
189 =back
190
191
192 =head1 ACKNOWLEDGEMENTS
193
194
195 =head1 LICENSE AND COPYRIGHT
196
197 Copyright 2010 Dobrica Pavlinusic.
198
199 This program is free software; you can redistribute it and/or modify
200 it under the terms of the GNU General Public License as published by
201 the Free Software Foundation; version 2 dated June, 1991 or at your option
202 any later version.
203
204 This program is distributed in the hope that it will be useful,
205 but WITHOUT ANY WARRANTY; without even the implied warranty of
206 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
207 GNU General Public License for more details.
208
209 A copy of the GNU General Public License is available in the source tree;
210 if not, write to the Free Software Foundation, Inc.,
211 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
212
213
214 =cut
215
216 1; # End of RFID::Serial