46 lines
1.3 KiB

  1. #!/usr/bin/env python3
  2. #
  3. # This file is part of usb-protocol.
  4. #
  5. """ Examples for using the simple descriptor data structures. """
  6. from usb_protocol.types.descriptors import StringDescriptor
  7. from usb_protocol.emitters.descriptors import DeviceDescriptorEmitter
  8. string_descriptor = bytes([
  9. 40, # Length
  10. 3, # Type
  11. ord('G'), 0x00,
  12. ord('r'), 0x00,
  13. ord('e'), 0x00,
  14. ord('a'), 0x00,
  15. ord('t'), 0x00,
  16. ord(' '), 0x00,
  17. ord('S'), 0x00,
  18. ord('c'), 0x00,
  19. ord('o'), 0x00,
  20. ord('t'), 0x00,
  21. ord('t'), 0x00,
  22. ord(' '), 0x00,
  23. ord('G'), 0x00,
  24. ord('a'), 0x00,
  25. ord('d'), 0x00,
  26. ord('g'), 0x00,
  27. ord('e'), 0x00,
  28. ord('t'), 0x00,
  29. ord('s'), 0x00,
  30. ])
  31. # Use our simple StringDescriptor object to parse a binary blob string descriptor.
  32. print(f"Parsing: {string_descriptor}")
  33. parsed = StringDescriptor.parse(string_descriptor)
  34. print(parsed)
  35. # Create a simple Device Descriptor via an emitter object.
  36. # Our object has sane defaults, so we can skip some fields if we want.
  37. builder = DeviceDescriptorEmitter()
  38. builder.idVendor = 0x1234
  39. builder.idProduct = 0xabcd
  40. builder.bNumConfigurations = 3
  41. print(f"Generated device descriptor: {builder.emit()}")