-
Notifications
You must be signed in to change notification settings - Fork 10
/
codegen.py
74 lines (66 loc) · 2.76 KB
/
codegen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
#
# Copyright (C) 2011 Austin Leirvik <aua at pdx.edu>
# Copyright (C) 2011 Wil Cooley <wcooley at pdx.edu>
# Copyright (C) 2011 Joanne McBride <[email protected]>
# Copyright (C) 2011 Danny Aley <[email protected]>
# Copyright (C) 2011 Erich Ulmer <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from usbrevue import *
def packet_to_libusb_code(pack):
"""
Convert a captured USB packet into the libusb C code that would
replicate it. Currently assumes:
a) 'handle' is a valid libusb device handle
b) 'data' is a char array large enough for any incoming data
c) 'TIMEOUT' is a constant or int
d) 'err' and 'n_transferred' are ints
e) 'handle_error()' is defined, and takes an int
"""
if pack.event_type != 'S':
return ''
if pack.is_control_xfer:
data = 'data'
if pack.setup.bmRequestTypeDirection == 'host_to_device':
data = '"%s"' % ''.join(map(lambda x: "\\x%02x" % x, pack.data))
return 'if ((err = libusb_control_transfer(handle, 0x%02x, 0x%x, 0x%x, 0x%x, %s, %s, TIMEOUT)) < 0)\n\thandle_error(err);\n' % (
pack.setup.bmRequestType,
pack.setup.bRequest,
pack.setup.wValue,
pack.setup.wIndex,
data,
pack.length)
if pack.is_bulk_xfer or pack.is_interrupt_xfer:
data = 'data'
if pack.epnum & 0x80 == 0:
data = '"%s"' % ''.join(map(lambda x: "\\x%02x" % x, pack.data))
return 'if ((err = libusb_%s_transfer(handle, 0x%02x, %s, %s, &n_transferred, TIMEOUT)) < 0)\n\thandle_error(err);\n' % (
'bulk' if pack.is_bulk_xfer else 'interrupt',
pack.epnum,
data,
pack.length)
if pack.is_isochronous_xfer:
return '/* unsupported isochronous transfer */\n'
return '/* unsupported transfer */\n'
if __name__ == '__main__':
import pcapy
import sys
pcap = pcapy.open_offline(sys.argv[1])
while 1:
h, p = pcap.next();
if h is None: break
pack = Packet(h,p)
sys.stdout.write(packet_to_libusb_code(pack))