forked from greatscottgadgets/facedancer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUSBSerial.py
123 lines (101 loc) · 4.23 KB
/
USBSerial.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# USBFtdi.py
# Contains class definitions to implement a simple USB Serial chip,
# such as the one in the HP48G+ and HP50G graphing calculators. See
# usb-serial.txt in the Linux documentation for more info.
import facedancer
from facedancer.USB import *
from facedancer.USBDevice import *
from facedancer.USBConfiguration import *
from facedancer.USBInterface import *
from facedancer.USBEndpoint import *
from facedancer.USBVendor import *
class USBSerialVendor(USBVendor):
name = "USB Serial vendor"
def setup_request_handlers(self):
self.request_handlers = {
# There are no vendor requests!
# 0 : self.handle_reset_request,
# 1 : self.handle_modem_ctrl_request,
# 2 : self.handle_set_flow_ctrl_request,
# 3 : self.handle_set_baud_rate_request,
# 4 : self.handle_set_data_request,
# 5 : self.handle_get_status_request,
# 6 : self.handle_set_event_char_request,
# 7 : self.handle_set_error_char_request,
# 9 : self.handle_set_latency_timer_request,
# 10 : self.handle_get_latency_timer_request
}
class USBSerialInterface(USBInterface):
name = "USB Serial interface"
def __init__(self, verbose=0):
descriptors = { }
endpoints = [
USBEndpoint(
1, # endpoint number
USBEndpoint.direction_out,
USBEndpoint.transfer_type_bulk,
USBEndpoint.sync_type_none,
USBEndpoint.usage_type_data,
512, # max packet size
0, # polling interval, see USB 2.0 spec Table 9-13
self.handle_data_available # handler function
),
USBEndpoint(
3, # endpoint number
USBEndpoint.direction_in,
USBEndpoint.transfer_type_bulk,
USBEndpoint.sync_type_none,
USBEndpoint.usage_type_data,
512, # max packet size
0, # polling interval, see USB 2.0 spec Table 9-13
None # handler function
)
]
# TODO: un-hardcode string index (last arg before "verbose")
USBInterface.__init__(
self,
0, # interface number
0, # alternate setting
USBClass(), # interface class: vendor-specific
0xff, # subclass: vendor-specific
0xff, # protocol: vendor-specific
0, # string index
verbose,
endpoints,
descriptors
)
def handle_data_available(self, data):
s=data;
if self.verbose > 0:
print(self.name, "received string", s)
s = s.replace(b'\r', b'\r\n')
s = s.upper()
reply = s
self.configuration.device.maxusb_app.send_on_endpoint(3, reply)
class USBSerialDevice(USBDevice):
name = "USB Serial device"
def __init__(self, maxusb_app, verbose=0):
interface = USBSerialInterface(verbose=verbose)
config = USBConfiguration(
1, # index
"Serial config", # string desc
[ interface ] # interfaces
)
USBDevice.__init__(
self,
maxusb_app,
0, # device class
0, # device subclass
0, # protocol release number
64, # max packet size for endpoint 0
0x03f0, # vendor id: HP
0x0121, # product id: HP50G
0x0001, # device revision
"GoodFET", # manufacturer string
"HP4X Emulator", # product string
"12345", # serial number string
[ config ],
verbose=verbose
)
self.device_vendor = USBSerialVendor()
self.device_vendor.set_device(self)