|
| 1 | +from .communicationport import CommunicationPort, CommunicationReadTimeout |
| 2 | + |
| 3 | + |
| 4 | +class HIDPort(CommunicationPort): |
| 5 | + """A CommunicationPort over a USB HID device, backed by hidapi. |
| 6 | +
|
| 7 | + HID is the only way to reach a device the operating system claims as HID. |
| 8 | + On macOS the IOHIDFamily driver owns the interface, so neither SerialPort |
| 9 | + (there is no /dev node) nor USBPort (libusb cannot claim the interface) can |
| 10 | + open it; hidapi goes through the same IOKit HID manager the OS reserves. |
| 11 | +
|
| 12 | + This exposes the same primitives as SerialPort/USBPort (open/close/ |
| 13 | + readData/writeData/flush), so a driver written against CommunicationPort |
| 14 | + works unchanged. It requires the optional 'hidapi' dependency (imported as |
| 15 | + 'hid'); the import is deferred to open() so the rest of the library does not |
| 16 | + depend on it. |
| 17 | +
|
| 18 | + hidapi frames every report with the report ID as byte 0. Devices that use a |
| 19 | + single unnumbered report expect that byte to be 0x00; writeData prepends it, |
| 20 | + and the count it returns excludes it. |
| 21 | + """ |
| 22 | + |
| 23 | + def __init__(self, idVendor, idProduct, serialNumber=None): |
| 24 | + super().__init__() |
| 25 | + self.idVendor = idVendor |
| 26 | + self.idProduct = idProduct |
| 27 | + self.serialNumber = serialNumber |
| 28 | + self.device = None |
| 29 | + self.defaultTimeout = 500 |
| 30 | + self.reportSize = 64 |
| 31 | + # Bounded drain for flush(): a small positive per-read timeout (never 0, |
| 32 | + # which blocks in hidapi rather than returning immediately) and a cap on |
| 33 | + # the number of reports drained, so flush can never hang. |
| 34 | + self.drainTimeout = 2 |
| 35 | + self.maxDrainReports = 16 |
| 36 | + self._internalBuffer = bytearray() |
| 37 | + |
| 38 | + @property |
| 39 | + def isOpen(self) -> bool: |
| 40 | + return self.device is not None |
| 41 | + |
| 42 | + def open(self): |
| 43 | + if self.isOpen: |
| 44 | + raise Exception("Port already open") |
| 45 | + |
| 46 | + import hid |
| 47 | + self.device = hid.device() |
| 48 | + if self.serialNumber in (None, "*", ".*"): |
| 49 | + self.device.open(self.idVendor, self.idProduct) |
| 50 | + else: |
| 51 | + self.device.open(self.idVendor, self.idProduct, self.serialNumber) |
| 52 | + self._internalBuffer = bytearray() |
| 53 | + |
| 54 | + def close(self): |
| 55 | + with self.portLock: |
| 56 | + if self.device is not None: |
| 57 | + self.device.close() |
| 58 | + self.device = None |
| 59 | + self._internalBuffer = bytearray() |
| 60 | + |
| 61 | + def bytesAvailable(self, endPoint=None) -> int: |
| 62 | + with self.portLock: |
| 63 | + return len(self._internalBuffer) |
| 64 | + |
| 65 | + def flush(self, endPoint=None): |
| 66 | + with self.portLock: |
| 67 | + self._internalBuffer = bytearray() |
| 68 | + if self.device is None: |
| 69 | + return |
| 70 | + # timeout_ms must stay positive: hidapi blocks on 0. The cap bounds |
| 71 | + # the wait even if a device were to stream input reports. |
| 72 | + for _ in range(self.maxDrainReports): |
| 73 | + if not self.device.read(self.reportSize, timeout_ms=self.drainTimeout): |
| 74 | + break |
| 75 | + |
| 76 | + def readData(self, length, endPoint=None) -> bytearray: |
| 77 | + with self.portLock: |
| 78 | + while length > len(self._internalBuffer): |
| 79 | + chunk = self.device.read(self.reportSize, timeout_ms=self.defaultTimeout) |
| 80 | + if not chunk: |
| 81 | + raise CommunicationReadTimeout( |
| 82 | + "Read {0} of {1} requested bytes from HID device".format( |
| 83 | + len(self._internalBuffer), length)) |
| 84 | + self._internalBuffer += bytearray(chunk) |
| 85 | + |
| 86 | + data = self._internalBuffer[:length] |
| 87 | + self._internalBuffer = self._internalBuffer[length:] |
| 88 | + |
| 89 | + return data |
| 90 | + |
| 91 | + def writeData(self, data, endPoint=None) -> int: |
| 92 | + with self.portLock: |
| 93 | + # Byte 0 is the HID report ID (0x00 for a single, unnumbered report). |
| 94 | + written = self.device.write(bytes([0x00]) + bytes(data)) |
| 95 | + if written < 0: |
| 96 | + raise IOError("Unable to write to HID device") |
| 97 | + |
| 98 | + return max(0, written - 1) |
0 commit comments