1
package NetPacket::USBMon;
3
$NetPacket::USBMon::AUTHORITY = 'cpan:YANICK';
6
$NetPacket::USBMon::VERSION = '1.4.2';
10
use vars qw(@ISA @EXPORT_OK %EXPORT_TAGS);
14
@ISA = qw(Exporter NetPacket);
17
USB_TYPE_SUBMISSION USB_TYPE_CALLBACK USB_TYPE_ERROR
18
USB_XFER_TYPE_ISO USB_XFER_TYPE_INTR
19
USB_XFER_TYPE_CONTROL USB_XFER_TYPE_BULK
20
USB_FLAG_SETUP_IRRELEVANT USB_FLAG_SETUP_RELEVANT
21
USB_FLAG_DATA_ERROR USB_FLAG_DATA_INCOMING
22
USB_FLAG_DATA_OUTGOING USB_FLAG_DATA_PRESENT
28
types => [qw(USB_TYPE_SUBMISSION USB_TYPE_CALLBACK
30
xfer_types => [qw(USB_XFER_TYPE_ISO USB_XFER_TYPE_INTR
31
USB_XFER_TYPE_CONTROL USB_XFER_TYPE_BULK)],
32
setup_flags => [qw(USB_FLAG_SETUP_IRRELEVANT USB_FLAG_SETUP_RELEVANT)],
33
data_flags => [qw(USB_FLAG_DATA_ERROR USB_FLAG_DATA_INCOMING
34
USB_FLAG_DATA_OUTGOING USB_FLAG_DATA_PRESENT)],
35
setup_types => [qw(USB_TYPE_VENDOR)],
40
use constant USB_TYPE_SUBMISSION => 'S';
41
use constant USB_TYPE_CALLBACK => 'C';
42
use constant USB_TYPE_ERROR => 'E';
44
use constant USB_XFER_TYPE_ISO => 0;
45
use constant USB_XFER_TYPE_INTR => 1;
46
use constant USB_XFER_TYPE_CONTROL => 2;
47
use constant USB_XFER_TYPE_BULK => 3;
49
use constant USB_FLAG_SETUP_IRRELEVANT => '-';
50
use constant USB_FLAG_SETUP_RELEVANT => chr(0);
52
use constant USB_FLAG_DATA_ERROR => 'E';
53
use constant USB_FLAG_DATA_INCOMING => '<';
54
use constant USB_FLAG_DATA_OUTGOING => '>';
55
use constant USB_FLAG_DATA_PRESENT => chr(0);
57
use constant USB_TYPE_VENDOR => 0x40;
65
my($id, $type, $xfer_type, $epnum, $devnum, $busnum, $flag_setup,
66
$flag_data, $ts_sec, $ts_usec, $status, $length, $len_cap,
67
$s, $interval, $start_frame, $xfer_flags, $ndesc, $rest) =
68
unpack('a8CCCCSCCa8liIIa8llLLa*', $packet);
70
# Try to grok quads. We may loose some address information with 32-bit
71
# Perl parsing 64-bit captures, or timestamp after 2038. Still the best
74
$id = unpack ('Q', $id);
75
$ts_sec = unpack ('Q', $ts_sec);
78
($id) = unpack ('LL', $id);
79
($ts_sec) = unpack ('LL', $ts_sec);
88
xfer_type => $xfer_type,
90
num => ($epnum & 0x7f),
91
dir => ($epnum & 0x80 ? 'IN' : 'OUT'),
95
flag_setup => chr($flag_setup),
96
flag_data => chr($flag_data),
102
interval => $interval,
103
start_frame => $start_frame,
104
xfer_flags => $xfer_flags,
109
if ($self->{flag_setup} ne USB_FLAG_SETUP_IRRELEVANT) {
113
($setup->{bmRequestType}, $setup->{bRequest}, $rest)
114
= unpack('CCa*', $s);
116
if ($setup->{bmRequestType} & USB_TYPE_VENDOR) {
117
($setup->{wValue}, $setup->{wIndex},
118
$setup->{wLength}) = unpack('S3', $rest);
120
# Unknown setup request;
121
$setup->{data} = $rest;
124
$self->{setup} = $setup;
127
# Isochronous descriptors
128
if ($self->{xfer_type} == USB_XFER_TYPE_ISO) {
130
($iso->{error_count}, $iso->{numdesc}) = unpack('ii', $s);
135
warn 'Payload length mismatch'
136
if length($rest) ne $self->{len_cap};
137
$self->{data} = $rest;
139
return bless $self, $class;
158
use NetPacket::USBMon;
160
$usb = NetPacket::USBMon->decode($raw_pkt);
164
C<NetPacket::USBMon> is a L<NetPacket> decoder of USB packets captured via
165
Linux USBMon interface.
171
=item C<NetPacket::USBMon-E<gt>decode([RAW PACKET])>
179
The instance data for the C<NetPacket::UDP> object consists of
180
the following fields.
186
An in-kernel address of the USB Request Block (URB). Stays the same for the
187
transaction submission and completion.
189
Might be truncatted when reading a 64-bit capture with 32-bit file.
193
URB type. Character 'S', 'C' or 'E', for constants USB_TYPE_SUBMISSION,
194
USB_TYPE_CALLBACK or USB_TYPE_ERROR.
198
Transfer type. USB_XFER_TYPE_ISO, USB_XFER_TYPE_INTR, USB_XFER_TYPE_CONTROL
199
or USB_XFER_TYPE_BULK.
203
Endpoint identification.
213
Transfer direction. "IN" or "OUT".
227
Indicates whether setup is present and makes sense.
231
Indicates whether data is present and makes sense.
235
Timestamp seconds since epoch. Subject to truncation with 32-bit Perl,
236
which should be fine until 2038.
240
Timestamp microseconds.
244
URB status. Negative errno.
248
Length of data (submitted or actual).
256
Only present for packets with setup_flag turned on.
257
Some contents are dependent on actual request type.
275
Only present for isochronous transfers.
287
Isochronous packet response rate.
291
Only applicable to isochronous transfers.
295
A copy of URB's transfer_flags.
299
Actual number of isochronous descriptors.
317
USB_TYPE_SUBMISSION, USB_TYPE_CALLBACK, USB_TYPE_ERROR, USB_XFER_TYPE_ISO,
318
USB_XFER_TYPE_INTR, USB_XFER_TYPE_CONTROL, USB_XFER_TYPE_BULK,
319
USB_FLAG_SETUP_IRRELEVANT, USB_FLAG_SETUP_RELEVANT, USB_FLAG_DATA_ERROR,
320
USB_FLAG_DATA_INCOMING, USB_FLAG_DATA_OUTGOING, USB_FLAG_DATA_PRESENT,
325
The following tags group together related exportable items.
331
USB_TYPE_SUBMISSION, USB_TYPE_CALLBACK, USB_TYPE_ERROR
335
USB_XFER_TYPE_ISO, USB_XFER_TYPE_INTR, USB_XFER_TYPE_CONTROL, USB_XFER_TYPE_BULK
337
=item C<:setup_flags>
339
USB_FLAG_SETUP_IRRELEVANT, USB_FLAG_SETUP_RELEVANT
343
USB_FLAG_DATA_ERROR, USB_FLAG_DATA_INCOMING, USB_FLAG_DATA_OUTGOING,
344
USB_FLAG_DATA_PRESENT
346
=item C<:setup_types>
352
All the above exportable items.
360
Copyright (c) 2013 Lubomir Rintel.
362
This module is free software. You can redistribute it and/or
363
modify it under the same terms as Perl itself.
367
Lubomir Rintel E<lt>lkundrak@v3.skE<gt>