Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
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.
 
 
 
 
 
 

63 lines
1.9 KiB

  1. import codecs
  2. import struct
  3. from usb_protocol.types import USBTransferType
  4. from usb_protocol.emitters.descriptors import DeviceDescriptorCollection, get_string_descriptor
  5. # Imported defines from usbd_def.h
  6. USBD_IDX_LANGID_STR = 0x00
  7. USBD_IDX_MFC_STR = 0x01
  8. USBD_IDX_PRODUCT_STR = 0x02
  9. USBD_IDX_SERIAL_STR = 0x03
  10. USBD_IDX_CONFIG_STR = 0x04
  11. USBD_IDX_INTERFACE_STR = 0x05
  12. USB_CLASS_HID = 0x03
  13. USB_CLASS_HID_NO_BOOT = 0x00
  14. USB_CLASS_HID_BOOT = 0x01
  15. USB_PROTOCOL_HID_NONE = 0x00
  16. USB_PROTOCOL_HID_KEYBOARD = 0x01
  17. USB_PROTOCOL_HID_MOUSE = 0x02
  18. collection = DeviceDescriptorCollection()
  19. with collection.DeviceDescriptor() as d:
  20. # https://pid.codes/1209/
  21. d.idVendor = 0x1209
  22. d.idProduct = 0x001
  23. d.bNumConfigurations = 1
  24. # Hack for ST reference code
  25. d.iProduct = USBD_IDX_PRODUCT_STR
  26. collection.add_descriptor(get_string_descriptor('RS-485 to HID'), d.iProduct)
  27. with collection.ConfigurationDescriptor() as c:
  28. with c.InterfaceDescriptor() as i:
  29. i.bInterfaceNumber = 0
  30. i.bInterfaceClass = USB_CLASS_HID
  31. i.bInterfaceSubclass = USB_CLASS_HID_BOOT
  32. i.bInterfaceProtocol = USB_PROTOCOL_HID_KEYBOARD
  33. report_desc = codecs.decode('05010906a101050719e029e71500250175019508810295017508810195057501050819012905910295017503910195067508150025650507190029658100c0', 'hex')
  34. collection.add_descriptor(report_desc, descriptor_type=0x22)
  35. hid_desc = codecs.decode('09211101000122', 'hex') + struct.pack('<H', len(report_desc))
  36. # add HID Descriptor after interface, but before
  37. # endpoint descriptors per HID v1.11 ยง 7.1
  38. i.add_subordinate_descriptor(hid_desc)
  39. # No OUT endpoint. Use the SET_REPORT via the control EP
  40. with i.EndpointDescriptor() as e:
  41. e.bEndpointAddress = 0x81
  42. e.bmAttributes = USBTransferType.INTERRUPT
  43. # it'd be good to adjust based upon FS vs LS
  44. e.bInterval = 10 # polling interval (LS/FS in ms)
  45. # Full-speed max size
  46. e.wMaxPacketSize = 64