composite_echo_gadget.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # Copyright 2015 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.
  4. import uuid
  5. import composite_gadget
  6. import echo_gadget
  7. import hid_echo_gadget
  8. import hid_gadget
  9. import usb_constants
  10. import usb_descriptors
  11. class CompositeEchoGadget(composite_gadget.CompositeGadget):
  12. def __init__(self):
  13. device_desc = usb_descriptors.DeviceDescriptor(
  14. idVendor=usb_constants.VendorID.GOOGLE,
  15. idProduct=usb_constants.ProductID.GOOGLE_COMPOSITE_ECHO_GADGET,
  16. bcdUSB=0x0210, # USB 2.1 to indicate support for BOS descriptors.
  17. iManufacturer=1,
  18. iProduct=2,
  19. iSerialNumber=3,
  20. bcdDevice=0x0100)
  21. echo_feature = echo_gadget.EchoCompositeFeature(
  22. endpoints=[(0, 5, 0x81, 0x01), (1, 6, 0x82, 0x02), (2, 7, 0x83, 0x03)])
  23. hid_echo_feature = hid_echo_gadget.EchoFeature()
  24. hid_feature = hid_gadget.HidCompositeFeature(
  25. report_desc=hid_echo_gadget.EchoFeature.REPORT_DESC,
  26. features={0: hid_echo_feature},
  27. interface_number=3,
  28. interface_string=4,
  29. in_endpoint=0x84, out_endpoint=0x04)
  30. super(CompositeEchoGadget, self).__init__(
  31. device_desc, [echo_feature, hid_feature])
  32. self.AddStringDescriptor(1, 'Google Inc.')
  33. self.AddStringDescriptor(2, 'Echo Gadget')
  34. self.AddStringDescriptor(3, '{:06X}'.format(uuid.getnode()))
  35. self.AddStringDescriptor(4, 'HID Echo')
  36. self.AddStringDescriptor(5, 'Interrupt Echo')
  37. self.AddStringDescriptor(6, 'Bulk Echo')
  38. self.AddStringDescriptor(7, 'Isochronous Echo')
  39. # Enable Microsoft OS 2.0 Descriptors for Windows 8.1 and above.
  40. self.EnableMicrosoftOSDescriptorsV2(vendor_code=0x02)
  41. # These are used to force Windows to load WINUSB.SYS for the echo functions.
  42. self.SetMicrosoftCompatId(0, 'WINUSB')
  43. self.SetMicrosoftCompatId(1, 'WINUSB')
  44. self.SetMicrosoftCompatId(2, 'WINUSB')
  45. def RegisterHandlers():
  46. """Registers web request handlers with the application server."""
  47. import server
  48. from tornado import web
  49. class WebConfigureHandler(web.RequestHandler):
  50. def post(self):
  51. server.SwitchGadget(CompositeEchoGadget())
  52. server.app.add_handlers('.*$', [
  53. (r'/composite_echo/configure', WebConfigureHandler),
  54. ])