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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""USB echo gadget module.
This gadget has pairs of IN/OUT endpoints that echo packets back to the host.
"""
import uuid
import composite_gadget
import usb_constants
import usb_descriptors
class EchoCompositeFeature(composite_gadget.CompositeFeature):
"""Composite device feature that echos data back to the host.
"""
def __init__(self, endpoints):
"""Create an echo gadget.
"""
fs_interfaces = []
hs_interfaces = []
if len(endpoints) >= 1:
iface_num, iface_string, in_endpoint, out_endpoint = endpoints[0]
fs_intr_interface_desc = usb_descriptors.InterfaceDescriptor(
bInterfaceNumber=iface_num,
bInterfaceClass=usb_constants.DeviceClass.VENDOR,
bInterfaceSubClass=0,
bInterfaceProtocol=0,
iInterface=iface_string,
)
fs_intr_interface_desc.AddEndpoint(usb_descriptors.EndpointDescriptor(
bEndpointAddress=out_endpoint,
bmAttributes=usb_constants.TransferType.INTERRUPT,
wMaxPacketSize=64,
bInterval=1 # 1ms
))
fs_intr_interface_desc.AddEndpoint(usb_descriptors.EndpointDescriptor(
bEndpointAddress=in_endpoint,
bmAttributes=usb_constants.TransferType.INTERRUPT,
wMaxPacketSize=64,
bInterval=1 # 1ms
))
fs_interfaces.append(fs_intr_interface_desc)
hs_intr_interface_desc = usb_descriptors.InterfaceDescriptor(
bInterfaceNumber=iface_num,
bInterfaceClass=usb_constants.DeviceClass.VENDOR,
bInterfaceSubClass=0,
bInterfaceProtocol=0,
iInterface=iface_string
)
hs_intr_interface_desc.AddEndpoint(usb_descriptors.EndpointDescriptor(
bEndpointAddress=out_endpoint,
bmAttributes=usb_constants.TransferType.INTERRUPT,
wMaxPacketSize=64,
bInterval=4 # 1ms
))
hs_intr_interface_desc.AddEndpoint(usb_descriptors.EndpointDescriptor(
bEndpointAddress=in_endpoint,
bmAttributes=usb_constants.TransferType.INTERRUPT,
wMaxPacketSize=64,
bInterval=4 # 1ms
))
hs_interfaces.append(hs_intr_interface_desc)
if len(endpoints) >= 2:
iface_num, iface_string, in_endpoint, out_endpoint = endpoints[1]
fs_bulk_interface_desc = usb_descriptors.InterfaceDescriptor(
bInterfaceNumber=iface_num,
bInterfaceClass=usb_constants.DeviceClass.VENDOR,
bInterfaceSubClass=0,
bInterfaceProtocol=0,
iInterface=iface_string
)
fs_bulk_interface_desc.AddEndpoint(usb_descriptors.EndpointDescriptor(
bEndpointAddress=out_endpoint,
bmAttributes=usb_constants.TransferType.BULK,
wMaxPacketSize=64,
bInterval=0
))
fs_bulk_interface_desc.AddEndpoint(usb_descriptors.EndpointDescriptor(
bEndpointAddress=in_endpoint,
bmAttributes=usb_constants.TransferType.BULK,
wMaxPacketSize=64,
bInterval=0
))
fs_interfaces.append(fs_bulk_interface_desc)
hs_bulk_interface_desc = usb_descriptors.InterfaceDescriptor(
bInterfaceNumber=iface_num,
bInterfaceClass=usb_constants.DeviceClass.VENDOR,
bInterfaceSubClass=0,
bInterfaceProtocol=0,
iInterface=iface_string
)
hs_bulk_interface_desc.AddEndpoint(usb_descriptors.EndpointDescriptor(
bEndpointAddress=out_endpoint,
bmAttributes=usb_constants.TransferType.BULK,
wMaxPacketSize=512,
bInterval=0
))
hs_bulk_interface_desc.AddEndpoint(usb_descriptors.EndpointDescriptor(
bEndpointAddress=in_endpoint,
bmAttributes=usb_constants.TransferType.BULK,
wMaxPacketSize=512,
bInterval=0
))
hs_interfaces.append(hs_bulk_interface_desc)
if len(endpoints) >= 3:
iface_num, iface_string, in_endpoint, out_endpoint = endpoints[2]
fs_interfaces.append(usb_descriptors.InterfaceDescriptor(
bInterfaceNumber=iface_num,
bInterfaceClass=usb_constants.DeviceClass.VENDOR,
bInterfaceSubClass=0,
bInterfaceProtocol=0,
iInterface=iface_string
))
fs_isoc_interface_desc = usb_descriptors.InterfaceDescriptor(
bInterfaceNumber=iface_num,
bAlternateSetting=1,
bInterfaceClass=usb_constants.DeviceClass.VENDOR,
bInterfaceSubClass=0,
bInterfaceProtocol=0,
iInterface=iface_string
)
fs_isoc_interface_desc.AddEndpoint(usb_descriptors.EndpointDescriptor(
bEndpointAddress=out_endpoint,
bmAttributes=usb_constants.TransferType.ISOCHRONOUS,
wMaxPacketSize=1023,
bInterval=1 # 1ms
))
fs_isoc_interface_desc.AddEndpoint(usb_descriptors.EndpointDescriptor(
bEndpointAddress=in_endpoint,
bmAttributes=usb_constants.TransferType.ISOCHRONOUS,
wMaxPacketSize=1023,
bInterval=1 # 1ms
))
fs_interfaces.append(fs_isoc_interface_desc)
hs_interfaces.append(usb_descriptors.InterfaceDescriptor(
bInterfaceNumber=iface_num,
bInterfaceClass=usb_constants.DeviceClass.VENDOR,
bInterfaceSubClass=0,
bInterfaceProtocol=0,
iInterface=iface_string
))
hs_isoc_interface_desc = usb_descriptors.InterfaceDescriptor(
bInterfaceNumber=iface_num,
bAlternateSetting=1,
bInterfaceClass=usb_constants.DeviceClass.VENDOR,
bInterfaceSubClass=0,
bInterfaceProtocol=0,
iInterface=iface_string
)
hs_isoc_interface_desc.AddEndpoint(usb_descriptors.EndpointDescriptor(
bEndpointAddress=out_endpoint,
bmAttributes=usb_constants.TransferType.ISOCHRONOUS,
wMaxPacketSize=512,
bInterval=4 # 1ms
))
hs_isoc_interface_desc.AddEndpoint(usb_descriptors.EndpointDescriptor(
bEndpointAddress=in_endpoint,
bmAttributes=usb_constants.TransferType.ISOCHRONOUS,
wMaxPacketSize=512,
bInterval=4 # 1ms
))
hs_interfaces.append(hs_isoc_interface_desc)
super(EchoCompositeFeature, self).__init__(fs_interfaces, hs_interfaces)
def ReceivePacket(self, endpoint, data):
"""Echo a packet back to the host.
Args:
endpoint: Incoming endpoint (must be an OUT pipe).
data: Packet data.
"""
assert endpoint & usb_constants.Dir.IN == 0
self.SendPacket(endpoint | usb_constants.Dir.IN, data)
class EchoGadget(composite_gadget.CompositeGadget):
"""Echo gadget.
"""
def __init__(self):
"""Create an echo gadget.
"""
device_desc = usb_descriptors.DeviceDescriptor(
idVendor=usb_constants.VendorID.GOOGLE,
idProduct=usb_constants.ProductID.GOOGLE_ECHO_GADGET,
bcdUSB=0x0200,
iManufacturer=1,
iProduct=2,
iSerialNumber=3,
bcdDevice=0x0100)
feature = EchoCompositeFeature(
endpoints=[(0, 4, 0x81, 0x01), (1, 5, 0x82, 0x02), (2, 6, 0x83, 0x03)])
super(EchoGadget, self).__init__(device_desc, [feature])
self.AddStringDescriptor(1, 'Google Inc.')
self.AddStringDescriptor(2, 'Echo Gadget')
self.AddStringDescriptor(3, '{:06X}'.format(uuid.getnode()))
self.AddStringDescriptor(4, 'Interrupt Echo')
self.AddStringDescriptor(5, 'Bulk Echo')
self.AddStringDescriptor(6, 'Isochronous Echo')
# Enable Microsoft OS Descriptors for Windows 8 and above.
self.EnableMicrosoftOSDescriptorsV1(vendor_code=0x01)
# These are used to force Windows to load WINUSB.SYS for the echo functions.
self.SetMicrosoftCompatId(0, 'WINUSB')
self.SetMicrosoftCompatId(1, 'WINUSB')
self.SetMicrosoftCompatId(2, 'WINUSB')
self.AddDeviceCapabilityDescriptor(usb_descriptors.ContainerIdDescriptor(
ContainerID=uuid.uuid4().bytes_le))
def RegisterHandlers():
"""Registers web request handlers with the application server."""
import server
from tornado import web
class WebConfigureHandler(web.RequestHandler):
def post(self):
server.SwitchGadget(EchoGadget())
server.app.add_handlers('.*$', [
(r'/echo/configure', WebConfigureHandler),
])
|