usbreq is a WIP simple helper Python module for quick scripts and tests with USB devices. It is not intended for stable programs.
The core of this library is the USBDevice class, which wraps
PyUSB's usb.core.Device class, and adds additional shortcut methods
(currently only two):
USBDevice.control_request(self, *, direction, req_type, recipient, request, value, index, length, data)direction,req_type,recipient, andrequestare accepted as strings (in any case), integers, or instances of usbreq's relevant enums (e.g.req_type=usbreq.USBRequestType.STANDARD).- The fields of
bmRequestType(direction,req_type, andrecipient) when given as integers are accepted either bitshifted or not. - For accepting
requestas a string, currently all standard requests of all recipients are implemented, but class requests are not yet. datacannot be specified for IN requestslengthis optional and inferred from data for OUT requests, or set to maximum (0xFF) for IN requests.- If specified for OUT requests, only the specified amount of
datawill be sent to the device.
- If specified for OUT requests, only the specified amount of
USBDevice.get_descriptor(self, *, type, index, langid=None, length, find_intended=False)typeis accepted as a string (in any case), an int, or an instance ofusbreq.USBDescriptorType.indexis optional and defaults to 0.langidonly makes sense iftype="string", and is optional there as well (defaults to 0)lengthis passed through directly toUSBDevice.control_request()find_intended=Trueasks this method to "cheat" for you and find a descriptor that is not directly requestable in USB, by parsing data for you. For example:get_descriptor(type="endpoint", index=1, find_intended=True)will first request the device's configuration descriptor, and then parse its subordinate descriptors to find the the second (zero-indexed) descriptor where bDescriptorType is 0x05.
Usage examples pending, but API documentation is live on RTD.