Skip to content

Commit

Permalink
Replace libusbsio with hidapi
Browse files Browse the repository at this point in the history
This commit is extracted from: Nitrokey/pynitrokey#523

If the hid module cannot be loaded, a warning is raised so that the SDK
can still be used for other devices if hidapi is not working correctly.

Co-authored-by: Sosthène Guédon <sosthene@nitrokey.com>
  • Loading branch information
robin-nitrokey and sosthene-nitrokey committed Aug 12, 2024
1 parent 7d60efe commit 20df11f
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 307 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

- Update `protobuf` dependency to 5.26
- Vendor `spsdk` dependency to reduce the total number of dependencies
- Replace `libusbsio` dependency with `hidapi`

## [v0.1.0](https://github.com/Nitrokey/nitrokey-sdk-py/releases/tag/v0.1.0) (2024-07-29)

Expand Down
110 changes: 98 additions & 12 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ tlv8 = "^0.10"
# lpc55
crcmod = "^1.7"
cryptography = ">=42"
libusbsio = "^2.1"
hidapi = "^0.14"

# nrf52
ecdsa = "^0.19"
Expand Down Expand Up @@ -59,8 +59,3 @@ mypy_path = "stubs"
show_error_codes = true
python_version = "3.9"
strict = true

# libusbsio is used by lpc55_upload, will be replaced eventually
[[tool.mypy.overrides]]
module = ["libusbsio.*"]
ignore_missing_imports = true
7 changes: 2 additions & 5 deletions src/nitrokey/trussed/_bootloader/lpc55.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from .lpc55_upload.mboot.properties import PropertyTag
from .lpc55_upload.sbfile.sb2.images import BootImageV21
from .lpc55_upload.utils.interfaces.device.usb_device import UsbDevice
from .lpc55_upload.utils.usbfilter import USBDeviceFilter

RKTH = bytes.fromhex("050aad3e77791a81e59c5b2ba5a158937e9460ee325d8ccba09734b8fdebb171")
KEK = bytes([0xAA] * 32)
Expand Down Expand Up @@ -101,9 +100,8 @@ def update(

@classmethod
def _list_vid_pid(cls: type[T], vid: int, pid: int) -> list[T]:
device_filter = USBDeviceFilter(f"0x{vid:x}:0x{pid:x}")
devices = []
for device in UsbDevice.enumerate(device_filter):
for device in UsbDevice.enumerate(vid=vid, pid=pid):
try:
devices.append(cls(device))
except ValueError:
Expand All @@ -114,8 +112,7 @@ def _list_vid_pid(cls: type[T], vid: int, pid: int) -> list[T]:

@classmethod
def _open(cls: type[T], path: str) -> Optional[T]:
device_filter = USBDeviceFilter(path)
devices = UsbDevice.enumerate(device_filter)
devices = UsbDevice.enumerate(path=path)
if len(devices) == 0:
logger.warn(f"No HID device at {path}")
return None
Expand Down
Loading

0 comments on commit 20df11f

Please sign in to comment.