Skip to content

Commit

Permalink
Merge pull request #152 from danielhrisca/master
Browse files Browse the repository at this point in the history
add the USB library path as optional configuration parameter for the usb_transport
  • Loading branch information
christoph2 authored Dec 13, 2023
2 parents 3de69c5 + 8d96e75 commit b392c94
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
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

0 comments on commit b392c94

Please sign in to comment.