File Coverage

blib/lib/Device/USB.pm
Criterion Covered Total %
statement 54 105 51.4
branch 2 28 7.1
condition n/a
subroutine 18 31 58.0
pod n/a
total 74 164 45.1


line stmt bran cond sub pod time code
1             package Device::USB;
2              
3             require 5.006;
4 19     19   661252 use warnings;
  19         54  
  19         844  
5 19     19   121 use strict;
  19         40  
  19         990  
6 19     19   116 use Carp;
  19         65  
  19         3912  
7              
8             use Inline (
9 19 0       326 C => "DATA",
    50          
    50          
10             ($ENV{LIBUSB_LIBDIR}
11             ? ( LIBS => "-L\"$ENV{LIBUSB_LIBDIR}\" " .
12             ($^O eq 'MSWin32' ? ' -llibusb -L\"$ENV{WINDDK}\\lib\\crt\\i386\" -lmsvcrt ' : '-lusb') )
13             : ( LIBS => '-lusb', )
14             ),
15             ($ENV{LIBUSB_INCDIR} ? ( INC => "-I\"$ENV{LIBUSB_INCDIR}\"" ) : () ),
16             NAME => 'Device::USB',
17             VERSION => '0.36',
18 19     19   25936 );
  19         606545  
19              
20             Inline->init();
21              
22             #
23             # Now the Perl code.
24             #
25              
26 19     19   22982 use Device::USB::Device;
  19         61  
  19         683  
27 19     19   14111 use Device::USB::DevConfig;
  19         53  
  19         555  
28 19     19   14565 use Device::USB::DevInterface;
  19         69  
  19         1346  
29 19     19   11299 use Device::USB::DevEndpoint;
  19         52  
  19         659  
30 19     19   13475 use Device::USB::Bus;
  19         53  
  19         702  
31              
32 19     19   121 use constant CLASS_PER_INSTANCE => 0;
  19         42  
  19         1336  
33 19     19   112 use constant CLASS_AUDIO => 1;
  19         31  
  19         1100  
34 19     19   139 use constant CLASS_COMM => 2;
  19         37  
  19         775  
35 19     19   102 use constant CLASS_HID => 3;
  19         36  
  19         844  
36 19     19   98 use constant CLASS_PRINTER => 7;
  19         37  
  19         12953  
37 19     19   117 use constant CLASS_MASS_STORAGE => 8;
  19         181  
  19         872  
38 19     19   109 use constant CLASS_HUB => 9;
  19         34  
  19         829  
39 19     19   103 use constant CLASS_DATA => 10;
  19         32  
  19         795  
40 19     19   109 use constant CLASS_VENDOR_SPEC => 0xff;
  19         33  
  19         17515  
41              
42             =encoding utf8
43              
44             =head1 NAME
45              
46             Device::USB - Use libusb to access USB devices.
47              
48             =head1 VERSION
49              
50             Version 0.36
51              
52             =cut
53              
54             our $VERSION=0.36;
55              
56              
57             =head1 SYNOPSIS
58              
59             Device::USB provides a Perl wrapper around the libusb library. This
60             supports Perl code controlling and accessing USB devices.
61              
62             use Device::USB;
63              
64             my $usb = Device::USB->new();
65             my $dev = $usb->find_device( $VENDOR, $PRODUCT );
66              
67             printf "Device: %04X:%04X\n", $dev->idVendor(), $dev->idProduct();
68             $dev->open();
69             print "Manufactured by ", $dev->manufacturer(), "\n",
70             " Product: ", $dev->product(), "\n";
71              
72             $dev->set_configuration( $CFG );
73             $dev->control_msg( @params );
74             ...
75              
76             See the libusb manual for more information about most of the methods. The
77             functionality is generally the same as the libusb function whose name is
78             the method name prepended with "usb_".
79              
80             =head1 DESCRIPTION
81              
82             This module provides a Perl interface to the C library libusb. This library
83             supports a relatively full set of functionality to access a USB device. In
84             addition to the libusb, functioality, Device::USB provides a few
85             convenience features that are intended to produce a more Perl-ish interface.
86              
87             These features include:
88              
89             =over 4
90              
91             =item *
92              
93             Using the library initializes it, no need to call the underlying usb_init
94             function.
95              
96             =item *
97              
98             Object interface reduces namespace pollution and provides a better interface
99             to the library.
100              
101             =item *
102              
103             The find_device method finds the device associated with a vendor id and
104             product id and creates an appropriate Device::USB::Device object to
105             manipulate the USB device.
106              
107             =item *
108              
109             Object interfaces to the bus and device data structures allowing read access
110             to information about each.
111              
112             =back
113              
114             =head1 Device::USB
115              
116             This class provides an interface to the non-bus and non-device specific
117             functions of the libusb library. In particular, it provides interfaces to
118             find busses and devices. It also provides convenience methods that simplify
119             some of the tasks above.
120              
121             =head2 CONSTANTS
122              
123             This class provides a set of constants for the defined device classes. The
124             constants defined at this time are:
125              
126             =over 4
127              
128             =item *
129              
130             CLASS_PER_INSTANCE
131              
132             =item *
133              
134             CLASS_AUDIO
135              
136             =item *
137              
138             CLASS_COMM
139              
140             =item *
141              
142             CLASS_HID
143              
144             =item *
145              
146             CLASS_PRINTER
147              
148             =item *
149              
150             CLASS_MASS_STORAGE
151              
152             =item *
153              
154             CLASS_HUB
155              
156             =item *
157              
158             CLASS_DATA
159              
160             =item *
161              
162             CLASS_VENDOR_SPEC
163              
164             =back
165              
166             =head2 FUNCTIONS
167              
168             =over 4
169              
170             =cut
171              
172             #
173             # Internal-only, one-time init function.
174             my $init_ref;
175             $init_ref = sub
176             {
177             libusb_init();
178             $init_ref = sub {};
179             };
180              
181             =item new
182              
183             Create a new Device::USB object for accessing the library.
184              
185             =cut
186              
187             sub new
188             {
189 0     0     my $class = shift;
190              
191 0           $init_ref->();
192              
193 0           return bless {}, $class;
194             }
195              
196             =item debug_mode
197              
198             This class method enables low-level debugging messages from the library
199             interface code.
200              
201             =over 4
202              
203             =item level
204              
205             0 disables debugging, 1 enables some debug messages, and 2 enables verbose
206             debug messages
207              
208             Any other values are forced to the nearest endpoint.
209              
210             =back
211              
212             =cut
213              
214             sub debug_mode
215             {
216 0     0     my ($class, $level) = @_;
217              
218 0           lib_debug_mode( $level );
219 0           return;
220             }
221              
222              
223             =item find_busses
224              
225             Returns the number of changes since previous call to the function: the
226             number of busses added or removed.
227              
228             =cut
229              
230             sub find_busses
231             {
232 0     0     my $self = shift;
233 0           return libusb_find_busses();
234             }
235              
236             =item find_devices
237              
238             Returns the number of changes since previous call to the function: the
239             number of devices added or removed. Should be called after find_busses.
240              
241             =cut
242              
243             sub find_devices
244             {
245 0     0     my $self = shift;
246 0           return libusb_find_devices();
247             }
248              
249             =item find_device
250              
251             Find a particular USB device based on the vendor and product ids. If more
252             than one device has the same product id from the same vendor, the first one
253             found is returned.
254              
255             =over 4
256              
257             =item vendor
258              
259             the vendor id
260              
261             =item product
262              
263             product id for that vendor
264              
265             =back
266              
267             returns a device reference or undef if none was found.
268              
269             =cut
270              
271             sub find_device
272             {
273 0     0     my $self = shift;
274 0           my $vendor = shift;
275 0           my $product = shift;
276              
277 0           return lib_find_usb_device( $vendor, $product );
278             }
279              
280             =item find_device_if
281              
282             Find a particular USB device based on the supplied predicate coderef. If
283             more than one device would satisfy the predicate, the first one found is
284             returned.
285              
286             =over 4
287              
288             =item pred
289              
290             the predicate used to select a device
291              
292             =back
293              
294             returns a device reference or undef if none was found.
295              
296             =cut
297              
298             sub find_device_if
299             {
300 0     0     my $self = shift;
301 0           my $pred = shift;
302              
303 0 0         croak( "Missing predicate for choosing a device.\n" )
304             unless defined $pred;
305              
306 0 0         croak( "Predicate must be a code reference.\n" )
307             unless 'CODE' eq ref $pred;
308              
309 0           foreach my $bus ($self->list_busses())
310             {
311 0           my $dev = $bus->find_device_if( $pred );
312 0 0         return $dev if defined $dev;
313             }
314              
315 0           return;
316             }
317              
318             =item list_devices
319              
320             Find all devices matching a vendor id and optional product id. If called
321             with no parameters, returns a list of all devices. If no product id is
322             given, returns all devices found with the supplied vendor id. If a product
323             id is given, returns all devices matching both the vendor id and product id.
324              
325             =over 4
326              
327             =item vendor
328              
329             the optional vendor id
330              
331             =item product
332              
333             optional product id for that vendor
334              
335             =back
336              
337             returns a list of devices matching the supplied criteria or a reference
338             to that array in scalar context
339              
340             =cut
341              
342             sub list_devices
343             {
344 0     0     my $self = shift;
345 0           my $vendor = shift;
346 0           my $product = shift;
347 0           my $pred = undef;
348              
349 0 0         if(!defined $vendor)
    0          
350             {
351 0     0     $pred = sub { defined };
  0            
352             }
353             elsif(!defined $product)
354             {
355 0     0     $pred = sub { $vendor == $_->idVendor() };
  0            
356             }
357             else
358             {
359             $pred =
360 0 0   0     sub { $vendor == $_->idVendor() && $product == $_->idProduct() };
  0            
361             }
362              
363 0           return $self->list_devices_if( $pred );
364             }
365              
366             =item list_devices_if
367              
368             This method provides a more flexible interface for finding devices. It
369             takes a single coderef parameter that is used to test each discovered
370             device. If the coderef returns a true value, the device is returned in the
371             list of matching devices, otherwise it is not.
372              
373             =over 4
374              
375             =item pred
376              
377             coderef to test devices.
378              
379             =back
380              
381             For example,
382              
383             my @devices = $usb->list_devices_if(
384             sub { Device::USB::CLASS_HUB == $_->bDeviceClass() }
385             );
386              
387             Returns all USB hubs found. The device to test is available to the coderef
388             in the C<$_> variable for simplicity.
389              
390             =cut
391              
392             sub list_devices_if
393             {
394 0     0     my $self = shift;
395 0           my $pred = shift;
396              
397 0 0         croak( "Missing predicate for choosing devices.\n" )
398             unless defined $pred;
399              
400 0 0         croak( "Predicate must be a code reference.\n" )
401             unless 'CODE' eq ref $pred;
402              
403 0           my @devices = ();
404 0           local $_ = undef;
405              
406 0           foreach my $bus ($self->list_busses())
407             {
408             # Push all matching devices for this bus on list.
409 0           push @devices, $bus->list_devices_if( $pred );
410             }
411              
412 0 0         return wantarray ? @devices : \@devices;
413             }
414              
415             =item list_busses
416              
417             Return the complete list of information after finding busses and devices.
418              
419             By using this function, you do not need to do the find_* calls yourself.
420              
421             returns a reference to an array of busses.
422              
423             =cut
424              
425             sub list_busses
426             {
427 0     0     my $self = shift;
428 0           my $busses = lib_list_busses();
429              
430 0 0         return wantarray ? @{$busses} : $busses;
  0            
431             }
432              
433             =item get_busses
434              
435             Return the complete list of information after finding busses and devices.
436              
437             Before calling this function, remember to call find_busses and find_devices.
438              
439             returns a reference to an array of busses.
440              
441             =cut
442              
443             sub get_busses
444             {
445 0     0     my $self = shift;
446 0           my $busses = lib_get_usb_busses();
447              
448 0 0         return wantarray ? @{$busses} : $busses;
  0            
449             }
450              
451             =back
452              
453             =head1 DIAGNOSTICS
454              
455             This is an explanation of the diagnostic and error messages this module
456             can generate.
457              
458             =head1 DEPENDENCIES
459              
460             This module depends on the Carp, Inline and Inline::C modules, as well as
461             the strict and warnings pragmas. Obviously, libusb must be available since
462             that is the entire reason for the module's existence.
463              
464             =head1 AUTHOR
465              
466             G. Wade Johnson (gwadej at cpan dot org)
467             Paul Archer (paul at paularcher dot org)
468              
469             Houston Perl Mongers Group
470              
471             Original author: David Davis
472              
473             =head1 BUGS
474              
475             Please report any bugs or feature requests to
476             C, or through the web interface at
477             L.
478             I will be notified, and then you'll automatically be notified of progress on
479             your bug as I make changes.
480              
481             =head1 FOR MORE INFORMATION
482              
483             The project is hosted at Google Code L.
484             More information on the project, including installation help is avaliable on the
485             Wiki.
486              
487             =head1 LIMITATIONS
488              
489             So far, this module has only been tested on Linux. It should work on any
490             OS that supports the libusb library. Several people have reported problems
491             compiling the module on Windows. In theory, it should be possible to make
492             the library work with LibUsb-Win32 L.
493             Without access to a Windows development system, I can't make those changes.
494              
495             The Interfaces and Endpoints are not yet proper objects. The code to extract
496             this information is not yet written.
497              
498             =head1 ACKNOWLEDGEMENTS
499              
500             Thanks go to various members of the Houston Perl Mongers group for input
501             on the module. But thanks mostly go to Paul Archer who proposed the project
502             and helped with the development.
503              
504             Thanks to Josep MonĂ©s Teixidor for fixing the C bug.
505              
506             Thanks to Mike McCauley for support of C and
507             C.
508              
509             Thanks to Vadim Mikhailov for fixing a compile problem with VC6 on Windows
510             and then chipping in again for VS 2005 on Windows, and yet again to fix
511             warnings on C99-compliant compilers.
512              
513             Thanks to John R. Hogheruis for information about modifying the Inline
514             parameters for compiling with Strawberry Perl on Windows.
515              
516             Thanks to Tony Shadwick for helping me resolve a problem with bulk_read and
517             interrupt_read.
518              
519             =head1 COPYRIGHT & LICENSE
520              
521             Copyright 2006-2013 Houston Perl Mongers
522              
523             This program is free software; you can redistribute it and/or modify it
524             under the same terms as Perl itself.
525              
526             =cut
527              
528             1;
529              
530             __DATA__