Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add the USB library path as optional configuration parameter for the usb_transport #152

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.21.8
current_version = 0.21.9
commit = True
tag = False

Expand Down
2 changes: 1 addition & 1 deletion pyxcp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
15 changes: 15 additions & 0 deletions pyxcp/transport/usb_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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):
Expand Down
Loading