File Coverage

blib/lib/Device/USB/Bus.pm
Criterion Covered Total %
statement 9 32 28.1
branch 0 14 0.0
condition n/a
subroutine 3 8 37.5
pod 5 5 100.0
total 17 59 28.8


line stmt bran cond sub pod time code
1             package Device::USB::Bus;
2              
3             require 5.006;
4 19     19   111 use warnings;
  19         34  
  19         724  
5 19     19   107 use strict;
  19         33  
  19         680  
6 19     19   114 use Carp;
  19         40  
  19         12980  
7              
8             =encoding utf8
9              
10             =head1 NAME
11              
12             Device::USB::Bus - Use libusb to access USB devices.
13              
14             =head1 VERSION
15              
16             Version 0.36
17              
18             =cut
19              
20             our $VERSION=0.36;
21              
22             =head1 SYNOPSIS
23              
24             This class encapsulates the USB bus structure and provides methods for
25             retrieving data from it. This class is not meant to be used alone, it is
26             part of the Device::USB package.
27              
28             Device:USB:LibUSB provides a Perl wrapper around the libusb library. This
29             supports Perl code controlling and accessing USB devices.
30              
31             use Device::USB;
32              
33             my $usb = Device::USB->new();
34              
35             foreach my $bus ($usb->list_busses())
36             {
37             print $bus->dirname(), ":\n";
38             foreach my $dev ($bus->devices())
39             {
40             print "\t", $dev->filename(), "\n";
41             }
42             }
43              
44              
45             =head1 DESCRIPTION
46              
47             This module provides a Perl interface to the bus structures returned by the
48             libusb library. This library supports a read-only interface to the data libusb
49             returns about a USB bus.
50              
51             =head1 FUNCTIONS
52              
53             =over 4
54              
55             =item dirname
56              
57             Return the directory name associated with this bus.
58              
59             =cut
60              
61             sub dirname
62             {
63 0     0 1   my $self = shift;
64              
65 0           return $self->{dirname};
66             }
67              
68             =item location
69              
70             Return the location value associated with this bus.
71              
72             =cut
73              
74             sub location
75             {
76 0     0 1   my $self = shift;
77              
78 0           return $self->{location};
79             }
80              
81             =item devices
82              
83             In array context, it returns a list of Device::USB::Device objects
84             representing all of the devices on this bus. In scalar context, it returns a
85             reference to that array.
86              
87             =cut
88              
89             sub devices
90             {
91 0     0 1   my $self = shift;
92              
93 0 0         return wantarray ? @{$self->{devices}} : $self->{devices};
  0            
94             }
95              
96             =item find_device_if
97              
98             Find a particular USB device based on the supplied predicate coderef. If
99             more than one device would satisfy the predicate, the first one found is
100             returned.
101              
102             =over 4
103              
104             =item pred
105              
106             the predicate used to select a device
107              
108             =back
109              
110             returns a device reference or undef if none was found.
111              
112             =cut
113              
114             sub find_device_if
115             {
116 0     0 1   my $self = shift;
117 0           my $pred = shift;
118              
119 0 0         croak( "Missing predicate for choosing a device.\n" )
120             unless defined $pred;
121              
122 0 0         croak( "Predicate must be a code reference.\n" )
123             unless 'CODE' eq ref $pred;
124              
125 0           local $_ = undef;
126              
127 0           foreach($self->devices())
128             {
129 0 0         return $_ if $pred->();
130             }
131              
132 0           return;
133             }
134              
135             =item list_devices_if
136             This method provides a flexible interface for finding devices. It
137             takes a single coderef parameter that is used to test each discovered
138             device. If the coderef returns a true value, the device is returned in the
139             list of matching devices, otherwise it is not.
140              
141             =over 4
142              
143             =item pred
144              
145             coderef to test devices.
146              
147             =back
148              
149             For example,
150              
151             my @devices = $bus->list_devices_if(
152             sub { Device::USB::CLASS_HUB == $_->bDeviceClass() }
153             );
154              
155             Returns all USB hubs found on this bus. The device to test is available to
156             the coderef in the C<$_> variable for simplicity.
157              
158             =cut
159              
160             sub list_devices_if
161             {
162 0     0 1   my $self = shift;
163 0           my $pred = shift;
164              
165 0 0         croak( "Missing predicate for choosing devices.\n" )
166             unless defined $pred;
167              
168 0 0         croak( "Predicate must be a code reference.\n" )
169             unless 'CODE' eq ref $pred;
170              
171 0           local $_ = undef;
172              
173 0           my @devices = grep { $pred->() } $self->devices();
  0            
174              
175 0 0         return wantarray ? @devices : \@devices;
176             }
177              
178             =back
179              
180             =head1 DIAGNOSTICS
181              
182             This is an explanation of the diagnostic and error messages this module
183             can generate.
184              
185             =head1 DEPENDENCIES
186              
187             This module depends on the Carp and Device::USB, as well as
188             the strict and warnings pragmas. Obviously, libusb must be available since
189             that is the entire reason for the module's existence.
190              
191             =head1 AUTHOR
192              
193             G. Wade Johnson (gwadej at cpan dot org)
194             Paul Archer (paul at paularcher dot org)
195              
196             Houston Perl Mongers Group
197              
198             =head1 BUGS
199              
200             Please report any bugs or feature requests to
201             C, or through the web interface at
202             L.
203             I will be notified, and then you'll automatically be notified of progress on
204             your bug as I make changes.
205              
206             =head1 ACKNOWLEDGEMENTS
207              
208             Thanks go to various members of the Houston Perl Mongers group for input
209             on the module. But thanks mostly go to Paul Archer who proposed the project
210             and helped with the development.
211              
212             Thanks also go to Josep MonĂ©s Teixidor, Mike McCauley, and Tony Awtrey for
213             spotting, reporting, and (sometimes) fixing bugs.
214              
215             =head1 COPYRIGHT & LICENSE
216              
217             Copyright 2006-2013 Houston Perl Mongers
218              
219             This program is free software; you can redistribute it and/or modify it
220             under the same terms as Perl itself.
221              
222             =cut
223              
224             1;