Descriptor/Endpoint.pm

filter: Statementstmt 13.3% 16 / 120 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 27.7% 5 / 18 Podpod 100% 11 / 11 totaltotal 13.2% 32 / 241 CC 40 SCAR 37.1
CC
40
Cov
8%
type
110.0
usage_type
72.0
synchronization_type
42.0
CRAP 40.9
« HID/Descriptor/Report.pm ↑ Summary Descriptor/Configuration.pm »
1 package USB::Descriptor::Endpoint;
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 5.010; # Require perl 5.10 to enable the switch feature
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '2'; # Bump this when the interface changes
8  
9 2 ▶use overload '@{}' => \&bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use constant fields => qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
12 bLength bDescriptorType bEndpointAddress bmAttributes wMaxPacketSize
13 bInterval
14 2 );
15  
16 =head1 NAME
17  
18 USB::Descriptor::Endpoint - USB Endpoint Descriptor
19  
20 =head1 SYNOPSIS
21  
22 An object representation of a USB endpoint descriptor.
23  
24 use USB::Descriptor::Endpoint;
25  
26 my $endpoint = USB::Descriptor::Endpoint->new( address => 1 );
27 $endpoint->type('interrupt');
28 $endpoint->direction('in');
29 ...
30  
31 =head1 DESCRIPTION
32  
33 L<USB::Descriptor::Endpoint> represents a USB interface descriptor. When added
34 to the descriptor tree of a L<USB::Descriptor::Device> object it can be used to
35 generate the data structures needed to compile the firmware for a USB device.
36  
37 =head1 CONSTRUCTOR
38  
39 =over
40  
41 =item $endpoint = USB::Descriptor::Endpoint->new(interval=>$interval, ...);
42  
43 Constructs and returns a new L<USB::Descriptor::Endpoint> object using the
44 passed options. Each option key is the name of an accessor method.
45  
46 When constructing endpoint objects, the endpoint direction and endpoint
47 number can be specified with a single key/value pair. For example, you can use
48 C<< new('in' => 3) >> instead of C<< new('direction' => 'in', 'number' => 3) >>.
49  
50 =back
51  
52 =cut
53  
54 sub new
55 0 ▶{
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
56 0 my ($this, %options) = @_;
57 0 ▶ my $class = ref($this) || $this;
Condition0%0/3
ref $this || $this
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table0%0/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
58 0 my $self =
59 {
60 'address' => 0, # Endpoint 0 OUT is invalid
61 'attributes' => 0, # Control
62 'bMaxPacketSize'=> 0,
63 'bInterval' => 10, # 10ms at low/full speed, 125ms at high speed
64 };
65 0 bless $self, $class;
66  
67 0 ▶ while( my ($key, $value) = each %options )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %options) 0 0 0
68 {
69 # Handle the 'direction => number' shortcut for specifying endpoints
70 0 ▶ if( ($key eq 'in') or ($key eq 'out') )
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq 'in' or $key eq 'out') { } 0 0 0
Condition0%0/3
$key eq "in" or $key eq "out"
l!l&&r!l&&!r
000
MC/DC0%0/2
$key eq "in" or $key eq "out"
$key eq "in" $key eq "out"
Truth table0%0/3
A or B
A= $key eq "in"B= $key eq "out"
ABresult
1X1
011
000
71 {
72 0 $self->direction($key);
73 0 $self->number($value);
74 }
75 else
76 {
77 0 $self->$key($value);
78 }
79 }
80  
81 0 return $self;
82 }
83  
84 =head1 Arrayification
85  
86 =over
87  
88 =item $endpoint->bytes (or @{$interface} )
89  
90 Returns an array of bytes containing all of the fields in the endpoint
91 descriptor.
92  
93 =back
94  
95 =cut
96  
97 sub bytes
98 0 ▶{
Subroutine bytes: not called CC=3 SCAR=24.8
Pod documented
99 0 my $s = shift;
100  
101 0 my @bytes;
102  
103 0 push @bytes, 7; # Endpoint descriptors are 7 bytes long
104 0 push @bytes, 0x05; # bDescriptorType
105 0 push @bytes, $s->address; # bEndpointAddress
106 0 push @bytes, $s->attributes; # bmAttributes
107 0 push @bytes, $s->max_packet_size & 0xFF; # wMaxPacketSize low
108 0 push @bytes, ($s->max_packet_size >> 8) & 0xFF; # wMaxPacketSize high
109  
110 # Force interval=1 for isochronous endpoints
111 0 ▶ $s->interval(1) if( 1 == ($s->{'attributes'} & 0x03) );
Branch: 1 branch
BranchTrueFalseTotal
if 1 == ($s->{"attributes"} & 3) 0 0 0
112  
113 0 push @bytes, $s->interval; # bInterval
114  
115 0 ▶ warn "Endpoint descriptor length is wrong" unless $bytes[0] == scalar @bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless $bytes[0] == scalar @bytes 0 0 0
116  
117 0 return \@bytes;
118 }
119  
120 =head1 ATTRIBUTES
121  
122 =over
123  
124 =item $interface->address
125  
126 Direct access to the bEndpointAddress value. Don't use this unless you know what
127 you're doing.
128  
129 =item $interface->attributes
130  
131 Direct access to the bmAttributes value. Don't use this unless you know what
132 you're doing.
133  
134 =item $interface->direction
135  
136 Get/Set the endpoint's direction (bEndpointAddress). Pass 'in' for an IN
137 endpoint or 'out' for an OUT endpoint.
138  
139 =item $interface->interval
140  
141 Get/Set the endpoint's polling interval in frame counts (bInterval). Forced to
142 1 for isochronous endpoints as required by the USB specification.
143  
144 =item $interface->max_packet_size
145  
146 Get/Set the endpoint's maximum packet size (wMaxPacketSize).
147  
148 =item $interface->number
149  
150 Get/Set the endpoint number (bEndpointAddress).
151  
152 =item $interface->synchronization_type
153  
154 Get/Set the endpoint's synchronization type (bmAttributes). Only used by
155 isochronous endpoints.
156  
157 =item $interface->type
158  
159 Get/Set the endpoint's type (bmAttributes). Valid values are 'control',
160 'isochronous', 'bulk', and 'interrupt'.
161  
162 =item $interface->usage_type
163  
164 Get/Set the endpoint's usage type (bmAttributes). Only used by isochronous
165 endpoints.
166  
167 =back
168  
169 =cut
170  
171 sub address
172 0 ▶{
Subroutine address: not called CC=2 SCAR=17.9
Pod documented
173 0 my $s = shift;
174 0 ▶ $s->{'address'} = int(shift) & 0xFF if scalar @_;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ 0 0 0
175 0 $s->{'address'};
176 }
177  
178 sub attributes
179 0 ▶{
Subroutine attributes: not called CC=2 SCAR=17.9
Pod documented
180 0 my $s = shift;
181 0 ▶ $s->{'attributes'} = int(shift) & 0xFF if scalar @_;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ 0 0 0
182 0 $s->{'attributes'};
183 }
184  
185 sub direction
186 0 ▶{
Subroutine direction: not called CC=3 SCAR=24.8
Pod documented
187 0 my $s = shift;
188 0 ▶ if( scalar @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 0 0 0
189 {
190 0 my $d = shift;
191 0 given($d)
192 {
193 0 when('in') { $s->{'address'} |= 0x80; } # Set the direction bit for IN
194 0 when('out') { $s->{'address'} &= ~0x80; } # Clear the direction bit for OUT
195 }
196 }
197 0 ▶ ($s->{'address'} & 0x80) ? 'in' : 'out';
Branch: 1 branch
BranchTrueFalseTotal
$s->{'address'} & 128 ? : 0 0 0
198 }
199  
200 sub interval
201 0 ▶{
Subroutine interval: not called CC=2 SCAR=17.9
Pod documented
202 0 my $s = shift;
203 0 ▶ $s->{'bInterval'} = int(shift) & 0xFF if scalar @_;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ 0 0 0
204 0 $s->{'bInterval'};
205 }
206  
207 sub max_packet_size
208 0 ▶{
Subroutine max_packet_size: not called CC=2 SCAR=17.9
Pod documented
209 0 my $s = shift;
210 0 ▶ $s->{'bMaxPacketSize'} = int(shift) & 0xFF if scalar @_;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ 0 0 0
211 0 $s->{'bMaxPacketSize'};
212 }
213  
214 sub number
215 0 ▶{
Subroutine number: not called CC=2 SCAR=17.9
Pod documented
216 0 my $s = shift;
217 0 ▶ $s->{'address'} = ($s->{'address'} & ~0x0F) | (int(shift) & 0x0F) if scalar @_;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ 0 0 0
218 0 $s->{'address'} & 0x0F;
219 }
220  
221 sub synchronization_type
222 0 ▶{
Subroutine synchronization_type: not called CC=6 SCAR=37.4
Pod documented
223 0 my $s = shift;
224 0 ▶ if( scalar @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 0 0 0
225 {
226 0 my $a = shift;
227 0 my $masked = $s->{'attributes'} & ~0x0C;
228 0 given($a)
229 {
230 0 when('none') { $s->{'attributes'} = $masked; }
231 0 when('asynchronous'){ $s->{'attributes'} = $masked | (0x01 << 2); }
232 0 when('adaptive' ) { $s->{'attributes'} = $masked | (0x02 << 2); }
233 0 when('synchronous') { $s->{'attributes'} = $masked | (0x03 << 2); }
234 }
235 }
236  
237 0 my $t = ($s->{'attributes'} & 0x0C) >> 2;
238  
239 0 ▶ if( 0 == $t ) { 'none'; }
Branch: 4 branches
BranchTrueFalseTotal
if (0 == $t) { } 0 0 0
elsif (1 == $t) { } 0 0 0
elsif (2 == $t) { } 0 0 0
elsif (3 == $t) { } 0 0 0
240 0 elsif( 1 == $t) { 'asynchronous'; }
241 0 elsif( 2 == $t) { 'adaptive'; }
242 0 elsif( 3 == $t) { 'synchronous'; }
243 0 else { undef; }
244 }
245  
246 sub type
247 0 ▶{
Subroutine type: not called CC=10 SCAR=47.0
Pod documented
248 0 my $s = shift;
249 0 ▶ if( scalar @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 0 0 0
250 {
251 0 my $d = shift;
252 0 my $masked = $s->{'attributes'} & ~0x03;
253 0 ▶ if( $d eq 'control' )
Branch: 4 branches
BranchTrueFalseTotal
if ($d eq 'control') { } 0 0 0
elsif ($d eq 'isochronous') { } 0 0 0
elsif ($d eq 'bulk') { } 0 0 0
elsif ($d eq 'interrupt') { } 0 0 0
254 {
255 0 $s->{'attributes'} = $masked;
256 }
257 elsif( $d eq 'isochronous' )
258 {
259 0 $s->{'attributes'} = $masked | 0x01;
260 0 $s->interval(1); # Isochronous endpoints must have interval == 1
261 }
262 elsif( $d eq 'bulk' )
263 {
264 0 $s->{'attributes'} = $masked | 0x02;
265 }
266 elsif( $d eq 'interrupt' )
267 {
268 0 $s->{'attributes'} = $masked | 0x03;
269 }
270 }
271  
272 0 my $t = $s->{'attributes'} & 0x03;
273  
274 0 ▶ if( 0 == $t ) { 'control'; }
Branch: 4 branches
BranchTrueFalseTotal
if (0 == $t) { } 0 0 0
elsif (1 == $t) { } 0 0 0
elsif (2 == $t) { } 0 0 0
elsif (3 == $t) { } 0 0 0
275 0 elsif( 1 == $t) { 'isochronous'; }
276 0 elsif( 2 == $t) { 'bulk'; }
277 0 elsif( 3 == $t) { 'interrupt'; }
278 0 else { undef; }
279 }
280  
281 sub usage_type
282 0 ▶{
Subroutine usage_type: not called CC=8 SCAR=42.8
Pod documented
283 0 my $s = shift;
284 0 ▶ if( scalar @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 0 0 0
285 {
286 0 my $a = shift;
287 0 my $masked = $s->{'attributes'} & ~0x0C;
288 0 ▶ if( $a eq 'data' )
Branch: 3 branches
BranchTrueFalseTotal
if ($a eq 'data') { } 0 0 0
elsif ($a eq 'feedback') { } 0 0 0
elsif ($a eq 'explicit') { } 0 0 0
289 {
290 0 $s->{'attributes'} = $masked;
291 }
292 elsif( $a eq 'feedback' )
293 {
294 0 $s->{'attributes'} = $masked | (0x01 << 2);
295 }
296 elsif( $a eq 'explicit' )
297 {
298 0 $s->{'attributes'} = $masked | (0x02 << 2);
299 }
300 }
301  
302 0 my $t = ($s->{'attributes'} & 0x0C) >> 2;
303  
304 0 ▶ if( 0 == $t ) { 'data'; }
Branch: 3 branches
BranchTrueFalseTotal
if (0 == $t) { } 0 0 0
elsif (1 == $t) { } 0 0 0
elsif (2 == $t) { } 0 0 0
305 0 elsif( 1 == $t) { 'feedback'; }
306 0 elsif( 2 == $t) { 'explicit'; }
307 0 else { undef; }
308 }
309  
310  
311 # --- String Descriptor support ---
312  
313 sub _index_for_string
314 0 ▶{
Subroutine _index_for_string: not called CC=4 SCAR=30.0
315 0 my ($s, $string) = @_;
316 0 ▶ if( defined($string) and length($string) and defined($s->_parent) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $string and length $string and defined $s->_parent) 0 0 0
Condition0%0/6
defined $string and length $string and defined $s->_parent
!ll&&!rl&&r
000
defined $string and length $string
!ll&&!rl&&r
000
MC/DC0%0/3
defined $string and length $string and defined $s->_parent
defined $string length $string defined $s->_parent
Truth table0%0/4
A and B and C
A= defined $stringB= length $stringC= defined $s->_parent
ABCresult
0XX0
10X0
1100
1111
317 {
318 0 return $s->_parent->_index_for_string($string);
319 }
320 0 return 0;
321 }
322  
323 sub _parent
324 0 ▶{
Subroutine _parent: not called CC=3 SCAR=24.8
325 0 my $s = shift;
326 0 ▶ $s->{'parent'} = shift if scalar(@_) && $_[0]->can('_index_for_string');
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ and $_[0]->can("_index_for_string") 0 0 0
Condition0%0/3
scalar @_ and $_[0]->can("_index_for_string")
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @_ and $_[0]->can("_index_for_string")
scalar @_ $_[0]->can("_index_for_string")
Truth table0%0/3
A and B
A= scalar @_B= $_[0]->can("_index_for_string")
ABresult
0X0
100
111
327 0 $s->{'parent'};
328 }
329  
330 2 1;
331  
332 =head1 AUTHOR
333  
334 Brandon Fosdick, C<< <bfoz at bfoz.net> >>
335  
336  
337 =head1 BUGS
338  
339 Please report any bugs or feature requests to C<bug-usb-descriptor-endpoint at rt.cpan.org>, or through
340 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=USB-Descriptor-Endpoint>. I will be notified, and then you'll
341 automatically be notified of progress on your bug as I make changes.
342  
343  
344 =head1 SUPPORT
345  
346 You can find documentation for this module with the perldoc command.
347  
348 perldoc USB::Descriptor::Endpoint
349  
350  
351 You can also look for information at:
352  
353 =over 4
354  
355 =item * RT: CPAN's request tracker (report bugs here)
356  
357 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=USB-Descriptor-Endpoint>
358  
359 =item * AnnoCPAN: Annotated CPAN documentation
360  
361 L<http://annocpan.org/dist/USB-Descriptor-Endpoint>
362  
363 =item * CPAN Ratings
364  
365 L<http://cpanratings.perl.org/d/USB-Descriptor-Endpoint>
366  
367 =item * Search CPAN
368  
369 L<http://search.cpan.org/dist/USB-Descriptor-Endpoint/>
370  
371 =back
372  
373  
374 =head1 ACKNOWLEDGEMENTS
375  
376  
377 =head1 LICENSE AND COPYRIGHT
378  
379 Copyright 2011 Brandon Fosdick.
380  
381 This program is released under the terms of the BSD License.
382  
383 =cut
« HID/Descriptor/Report.pm ↑ Summary Descriptor/Configuration.pm »