1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """USB echo gadget module.
7 This gadget has pairs of IN/OUT endpoints that echo packets back to the host.
16 import usb_descriptors
19 class EchoGadget(gadget
.Gadget
):
24 """Create an echo gadget.
26 device_desc
= usb_descriptors
.DeviceDescriptor(
27 idVendor
=usb_constants
.VendorID
.GOOGLE
,
28 idProduct
=usb_constants
.ProductID
.GOOGLE_ECHO_GADGET
,
35 fs_config_desc
= usb_descriptors
.ConfigurationDescriptor(
38 fs_intr_interface_desc
= usb_descriptors
.InterfaceDescriptor(
40 bInterfaceClass
=usb_constants
.DeviceClass
.VENDOR
,
45 fs_intr_interface_desc
.AddEndpoint(usb_descriptors
.EndpointDescriptor(
46 bEndpointAddress
=0x01,
47 bmAttributes
=usb_constants
.TransferType
.INTERRUPT
,
51 fs_intr_interface_desc
.AddEndpoint(usb_descriptors
.EndpointDescriptor(
52 bEndpointAddress
=0x81,
53 bmAttributes
=usb_constants
.TransferType
.INTERRUPT
,
57 fs_config_desc
.AddInterface(fs_intr_interface_desc
)
59 fs_bulk_interface_desc
= usb_descriptors
.InterfaceDescriptor(
61 bInterfaceClass
=usb_constants
.DeviceClass
.VENDOR
,
66 fs_bulk_interface_desc
.AddEndpoint(usb_descriptors
.EndpointDescriptor(
67 bEndpointAddress
=0x02,
68 bmAttributes
=usb_constants
.TransferType
.BULK
,
72 fs_bulk_interface_desc
.AddEndpoint(usb_descriptors
.EndpointDescriptor(
73 bEndpointAddress
=0x82,
74 bmAttributes
=usb_constants
.TransferType
.BULK
,
78 fs_config_desc
.AddInterface(fs_bulk_interface_desc
)
80 fs_config_desc
.AddInterface(usb_descriptors
.InterfaceDescriptor(
82 bInterfaceClass
=usb_constants
.DeviceClass
.VENDOR
,
87 fs_isoc_interface_desc
= usb_descriptors
.InterfaceDescriptor(
90 bInterfaceClass
=usb_constants
.DeviceClass
.VENDOR
,
95 fs_isoc_interface_desc
.AddEndpoint(usb_descriptors
.EndpointDescriptor(
96 bEndpointAddress
=0x03,
97 bmAttributes
=usb_constants
.TransferType
.ISOCHRONOUS
,
101 fs_isoc_interface_desc
.AddEndpoint(usb_descriptors
.EndpointDescriptor(
102 bEndpointAddress
=0x83,
103 bmAttributes
=usb_constants
.TransferType
.ISOCHRONOUS
,
107 fs_config_desc
.AddInterface(fs_isoc_interface_desc
)
109 hs_config_desc
= usb_descriptors
.ConfigurationDescriptor(
113 hs_intr_interface_desc
= usb_descriptors
.InterfaceDescriptor(
115 bInterfaceClass
=usb_constants
.DeviceClass
.VENDOR
,
116 bInterfaceSubClass
=0,
117 bInterfaceProtocol
=0,
120 hs_intr_interface_desc
.AddEndpoint(usb_descriptors
.EndpointDescriptor(
121 bEndpointAddress
=0x01,
122 bmAttributes
=usb_constants
.TransferType
.INTERRUPT
,
126 hs_intr_interface_desc
.AddEndpoint(usb_descriptors
.EndpointDescriptor(
127 bEndpointAddress
=0x81,
128 bmAttributes
=usb_constants
.TransferType
.INTERRUPT
,
132 hs_config_desc
.AddInterface(hs_intr_interface_desc
)
134 hs_bulk_interface_desc
= usb_descriptors
.InterfaceDescriptor(
136 bInterfaceClass
=usb_constants
.DeviceClass
.VENDOR
,
137 bInterfaceSubClass
=0,
138 bInterfaceProtocol
=0,
141 hs_bulk_interface_desc
.AddEndpoint(usb_descriptors
.EndpointDescriptor(
142 bEndpointAddress
=0x02,
143 bmAttributes
=usb_constants
.TransferType
.BULK
,
147 hs_bulk_interface_desc
.AddEndpoint(usb_descriptors
.EndpointDescriptor(
148 bEndpointAddress
=0x82,
149 bmAttributes
=usb_constants
.TransferType
.BULK
,
153 hs_config_desc
.AddInterface(hs_bulk_interface_desc
)
155 hs_config_desc
.AddInterface(usb_descriptors
.InterfaceDescriptor(
157 bInterfaceClass
=usb_constants
.DeviceClass
.VENDOR
,
158 bInterfaceSubClass
=0,
159 bInterfaceProtocol
=0,
162 hs_isoc_interface_desc
= usb_descriptors
.InterfaceDescriptor(
165 bInterfaceClass
=usb_constants
.DeviceClass
.VENDOR
,
166 bInterfaceSubClass
=0,
167 bInterfaceProtocol
=0,
170 hs_isoc_interface_desc
.AddEndpoint(usb_descriptors
.EndpointDescriptor(
171 bEndpointAddress
=0x03,
172 bmAttributes
=usb_constants
.TransferType
.ISOCHRONOUS
,
176 hs_isoc_interface_desc
.AddEndpoint(usb_descriptors
.EndpointDescriptor(
177 bEndpointAddress
=0x83,
178 bmAttributes
=usb_constants
.TransferType
.ISOCHRONOUS
,
182 hs_config_desc
.AddInterface(hs_isoc_interface_desc
)
184 super(EchoGadget
, self
).__init
__(
185 device_desc
, fs_config_desc
, hs_config_desc
)
186 self
.AddStringDescriptor(1, 'Google Inc.')
187 self
.AddStringDescriptor(2, 'Echo Gadget')
188 self
.AddStringDescriptor(3, '{:06X}'.format(uuid
.getnode()))
189 self
.AddStringDescriptor(4, 'Interrupt Echo')
190 self
.AddStringDescriptor(5, 'Bulk Echo')
191 self
.AddStringDescriptor(6, 'Isochronous Echo')
193 def ReceivePacket(self
, endpoint
, data
):
194 """Echo a packet back to the host.
197 endpoint: Incoming endpoint (must be an OUT pipe).
200 assert endpoint
& usb_constants
.Dir
.IN
== 0
202 self
.SendPacket(endpoint | usb_constants
.Dir
.IN
, data
)
204 def RegisterHandlers():
205 """Registers web request handlers with the application server."""
208 from tornado
import web
210 class WebConfigureHandler(web
.RequestHandler
):
213 server
.SwitchGadget(EchoGadget())
215 server
.app
.add_handlers('.*$', [
216 (r
'/echo/configure', WebConfigureHandler
),