HID.pm

filter: Statementstmt 40.0% 14 / 35 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 80.0% 4 / 5 Podpod 100% 1 / 1 totaltotal 31.6% 19 / 60 CC 11 SCAR 45.8
CC
11
Cov
26%
Descriptor
132.0
BEGIN
1.0
BEGIN
1.0
CRAP 97.1
↑ Summary HID/Descriptor/Report.pm »
1 package USB::HID;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use USB::Descriptor;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use USB::HID::Descriptor::Interface;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 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<USB::HID> 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<USB::HID::Descriptor> 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<USB::HID::Descriptor()> factory method. It accepts a hash of arguments that
35 happens to be the same hash expected by L<USB::Descriptor::Device> and returns
36 a reference to a new L<USB::Descriptor::Device> object. Any interface
37 specifications provided to the method will be automatically converted into
38 L<USB::HID::Descriptor::Interface> 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<USB::Descriptor::Device> object using the
73 passed options. Each option key is the name of an accessor method of
74 L<USB::Descriptor::Device>. Interface specifications are automatically converted
75 to L<USB::HID::Descriptor::Interface>.
76  
77 =back
78  
79 =cut
80  
81 sub Descriptor
82 0 ▶{
Subroutine Descriptor: not called CC=11 SCAR=48.8
Pod documented
83 0 my %options = @_;
84  
85 # Convert all Interfaces into HID::Interfaces
86 0 ▶ if(exists $options{'configurations'} and scalar $options{'configurations'})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{"configurations"} and scalar $options{"configurations"}) 0 0 0
Condition0%0/2
exists $options{"configurations"} and scalar $options{"configurations"}
!ll
00
MC/DC0%0/1
exists $options{"configurations"} and scalar $options{"configurations"}
exists $options{"configurations"}
Truth table0%0/2
A
A= exists $options{"configurations"}
Aresult
00
11
87 {
88 # Find the interfaces in each configuration
89 0 foreach my $configuration ( @{$options{'configurations'}} )
90 {
91 0 my $interfaces;
92 0 ▶ if( ref($configuration) eq 'HASH' ) # Hash reference?
Branch: 2 branches
BranchTrueFalseTotal
if (ref $configuration eq 'HASH') { } 0 0 0
elsif (ref $configuration) { } 0 0 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 ▶ if( ref($_) eq 'HASH' ) # Hash reference?
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_ eq 'HASH') { } 0 0 0
elsif (ref $_) { } 0 0 0
109 {
110 0 USB::HID::Descriptor::Interface->new(%{$_});
111 }
112 elsif( ref($_) ) # Reference to something else?
113 {
114 0 ▶ if( $_->isa('USB::Descriptor::Interface') )
Branch: 1 branch
BranchTrueFalseTotal
if ($_->isa('USB::Descriptor::Interface')) { } 0 0 0
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 ▶ if( ref($configuration) eq 'HASH' ) # Hash reference?
Branch: 2 branches
BranchTrueFalseTotal
if (ref $configuration eq 'HASH') { } 0 0 0
elsif (ref $_) { } 0 0 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 2 1;
144  
145 =head1 AUTHOR
146  
147 Brandon Fosdick, C<< <bfoz at bfoz.net> >>
148  
149  
150 =head1 BUGS
151  
152 Please report any bugs or feature requests to C<bug-usb-hid at rt.cpan.org>, or through
153 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=USB-HID>. 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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=USB-HID>
171  
172 =item * AnnoCPAN: Annotated CPAN documentation
173  
174 L<http://annocpan.org/dist/USB-Descriptor>
175  
176 =item * CPAN Ratings
177  
178 L<http://cpanratings.perl.org/d/USB-HID>
179  
180 =item * Search CPAN
181  
182 L<http://search.cpan.org/dist/USB-HID/>
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
↑ Summary HID/Descriptor/Report.pm »