You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.5 KiB

  1. #!/usr/bin/env python3
  2. #
  3. # This file is part of usb-protocol.
  4. #
  5. """ Example that builds a device-worth of descriptors using a collection object. """
  6. from usb_protocol.emitters.descriptors import DeviceDescriptorCollection
  7. collection = DeviceDescriptorCollection()
  8. # Create our device descriptor, and populate it with some fields.
  9. # Many fields have sane/common defaults; and thus can be omitted.
  10. with collection.DeviceDescriptor() as d:
  11. d.idVendor = 0xc001
  12. d.idProduct = 0xc0de
  13. d.bNumConfigurations = 1
  14. d.iManufacturer = "usb-tools"
  15. d.iProduct = "Illegitimate USB Device"
  16. d.iSerialNumber = "123456"
  17. # Create our configuration descriptor, and its subordinates.
  18. with collection.ConfigurationDescriptor() as c:
  19. # Here, this is our first configuration, and is automatically assigned number '1'.
  20. # We'll create a simple interface with a couple of endpoints.
  21. with c.InterfaceDescriptor() as i:
  22. i.bInterfaceNumber = 0
  23. # Our endpoints default to bulk; with mostly-sane defaults.
  24. with i.EndpointDescriptor() as e:
  25. e.bEndpointAddress = 0x01
  26. e.wMaxPacketSize = 512
  27. with i.EndpointDescriptor() as e:
  28. e.bEndpointAddress = 0x81
  29. e.wMaxPacketSize = 512
  30. print("This device's descriptors would look like:")
  31. # Iterate over all of our descriptors.
  32. for value, index, raw in collection:
  33. print(f" type {value} (index {index}) = {raw}")