From 8d96e75998f1638d734ca2191cb776bb43ddc7c2 Mon Sep 17 00:00:00 2001 From: danielhrisca Date: Wed, 13 Dec 2023 10:10:34 +0200 Subject: [PATCH] add the USB library path as optional configuration parameter for the usb_transport --- .bumpversion.cfg | 2 +- pyxcp/__init__.py | 2 +- pyxcp/transport/usb_transport.py | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/.bumpversion.cfg b/.bumpversion.cfg index 4b37d9e..f7e1536 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.21.8 +current_version = 0.21.9 commit = True tag = False diff --git a/pyxcp/__init__.py b/pyxcp/__init__.py index acf32b0..8dfd730 100644 --- a/pyxcp/__init__.py +++ b/pyxcp/__init__.py @@ -25,4 +25,4 @@ from .transport import Usb # if you update this manually, do not forget to update .bumpversion.cfg -__version__ = "0.21.8" +__version__ = "0.21.9" diff --git a/pyxcp/transport/usb_transport.py b/pyxcp/transport/usb_transport.py index e69c2bb..e4fa25f 100644 --- a/pyxcp/transport/usb_transport.py +++ b/pyxcp/transport/usb_transport.py @@ -8,6 +8,9 @@ from time import sleep from time import time +import usb.backend.libusb1 as libusb1 +import usb.backend.libusb0 as libusb0 +import usb.backend.openusb as openusb import usb.core import usb.util @@ -29,6 +32,7 @@ class Usb(BaseTransport): "reply_endpoint_number": (int, True, 1), "vendor_id": (int, False, 0), "product_id": (int, False, 0), + "library": (str, False, "") # absolute path to USB shared library } HEADER = struct.Struct("<2H") HEADER_SIZE = HEADER.size @@ -43,6 +47,7 @@ def __init__(self, config=None, policy=None): self.interface_number = self.config.get("interface_number") self.command_endpoint_number = self.config.get("command_endpoint_number") self.reply_endpoint_number = self.config.get("reply_endpoint_number") + self.library = self.config.get("library") self.device = None self.status = 0 @@ -55,15 +60,25 @@ def __init__(self, config=None, policy=None): self._packets = deque() def connect(self): + if self.library: + for backend_provider in (libusb1, libusb0, openusb): + backend = backend_provider.get_backend(find_library=lambda x: self.library) + if backend: + break + else: + backend = None + if self.vendor_id and self.product_id: kwargs = { "find_all": True, "idVendor": self.vendor_id, "idProduct": self.product_id, + "backend": backend, } else: kwargs = { "find_all": True, + "backend": backend, } for device in usb.core.find(**kwargs):