File Coverage

blib/lib/USB/HID.pm
Criterion Covered Total %
statement 12 33 36.3
branch 0 16 0.0
condition 0 2 0.0
subroutine 4 5 80.0
pod 1 1 100.0
total 17 57 29.8


line stmt bran cond sub pod time code
1             package USB::HID;
2              
3 1     1   4025 use strict;
  1         3  
  1         47  
4 1     1   7 use warnings;
  1         3  
  1         37  
5 1     1   6 use USB::Descriptor;
  1         2  
  1         27  
6 1     1   666 use USB::HID::Descriptor::Interface;
  1         4  
  1         346  
7              
8             our $VERSION = '1';
9              
10             =head1 NAME
11              
12             USB::HID - USB HID Descriptor generation tools
13              
14             =head1 SYNOPSIS
15              
16             A set of classes and methods for generating USB HID descriptor sets.
17              
18             use USB::HID;
19              
20             my $device = USB::HID::Descriptor( product => 'My First Device' );
21             $device->vendorID(0x1234);
22             $device->productID(0x5678);
23             $device->configurations( [ USB::Descriptor::Configuration->new() ] );
24             ...
25              
26             =head1 DESCRIPTION
27              
28             L provides a means of specifying a USB Human Interface Device's
29             descriptors and then generating descriptor structures suitable for use in the
30             device's firmware. However, L only generates the bytes
31             that comprise the structures, it does not handle generation of valid source code.
32              
33             The easiest way to create a new HID descriptor set is to use the
34             C factory method. It accepts a hash of arguments that
35             happens to be the same hash expected by L and returns
36             a reference to a new L object. Any interface
37             specifications provided to the method will be automatically converted into
38             L objects
39              
40             use USB::HID;
41              
42             my $device = USB::HID::Descriptor(
43             'usb_version' => '2.0.0', # Default
44             'max_packet_size' => 64, # Full speed device
45             'vendorID' => 0x1234,
46             'productID' => 0x5678,
47             'manufacturer' => 'Acme, Inc.',
48             'product' => 'Giant Catapult',
49             'serial_number' => '007',
50             'configurations' => [{
51             'description' => 'Configuration 0',
52             'remote_wakeup' => 1,
53             'max_current' => 100, # mA
54             'interfaces' => [{
55             'description' => 'Interface 0',
56             'endpoints' => [{
57             'direction' => 'in',
58             'number' => 1,
59             'max_packet_size' => 42,
60             }],
61             }],
62             },
63             );
64              
65              
66             =head1 CLASS METHODS
67              
68             =over
69              
70             =item $device = USB::HID::Descriptor(vendorID=>$vendorID, ...);
71              
72             Constructs and returns a new L object using the
73             passed options. Each option key is the name of an accessor method of
74             L. Interface specifications are automatically converted
75             to L.
76              
77             =back
78              
79             =cut
80              
81             sub Descriptor
82             {
83 0     0 1   my %options = @_;
84              
85             # Convert all Interfaces into HID::Interfaces
86 0 0 0       if(exists $options{'configurations'} and scalar $options{'configurations'})
87             {
88             # Find the interfaces in each configuration
89 0           foreach my $configuration ( @{$options{'configurations'}} )
  0            
90             {
91 0           my $interfaces;
92 0 0         if( ref($configuration) eq 'HASH' ) # Hash reference?
    0          
93             {
94 0           $interfaces = $configuration->{'interfaces'};
95             }
96             elsif( ref($configuration) ) # Reference to something else?
97             {
98 0           $interfaces = $configuration->interfaces;
99             }
100              
101             # Now $interfaces is a reference to an array. But, an array of what?
102             # If an array element is a hash reference, use it to create a new
103             # HID interface object. If it's already an object, find out what
104             # kind of object it is and try to convert it to a HID interface
105             # object.
106             my @interfaces = map
107             {
108 0 0         if( ref($_) eq 'HASH' ) # Hash reference?
  0 0          
109             {
110 0           USB::HID::Descriptor::Interface->new(%{$_});
  0            
111             }
112             elsif( ref($_) ) # Reference to something else?
113             {
114 0 0         if( $_->isa('USB::Descriptor::Interface') )
115             {
116 0           USB::HID::Descriptor::Interface->convert($_); # Convert it to a HID interface
117             }
118             else
119             {
120 0           $_; # Use it, whatever it is
121             }
122             }
123 0           else { undef; }
124 0           } @{$interfaces};
125              
126             # Now insert the new interfaces array into the original location
127             # and no one will be the wiser
128 0 0         if( ref($configuration) eq 'HASH' ) # Hash reference?
    0          
129             {
130 0           $configuration->{'interfaces'} = \@interfaces;
131             }
132             elsif( ref($_) ) # Reference to something else?
133             {
134 0           $configuration->interfaces = \@interfaces;
135             }
136             }
137             }
138              
139             # Create and return a Composite Device descriptor
140 0           USB::Descriptor::composite(%options);
141             }
142              
143             1;
144              
145             =head1 AUTHOR
146              
147             Brandon Fosdick, C<< >>
148              
149              
150             =head1 BUGS
151              
152             Please report any bugs or feature requests to C, or through
153             the web interface at L. I will be notified, and then you'll
154             automatically be notified of progress on your bug as I make changes.
155              
156              
157             =head1 SUPPORT
158              
159             You can find documentation for this module with the perldoc command.
160              
161             perldoc USB::HID
162              
163              
164             You can also look for information at:
165              
166             =over 4
167              
168             =item * RT: CPAN's request tracker (report bugs here)
169              
170             L
171              
172             =item * AnnoCPAN: Annotated CPAN documentation
173              
174             L
175              
176             =item * CPAN Ratings
177              
178             L
179              
180             =item * Search CPAN
181              
182             L
183              
184             =back
185              
186              
187             =head1 ACKNOWLEDGEMENTS
188              
189              
190             =head1 LICENSE AND COPYRIGHT
191              
192             Copyright 2011 Brandon Fosdick.
193              
194             This program is released under the terms of the BSD License.
195              
196             =cut