diff --git a/python/__init__.py b/python/__init__.py index 424dd55c..178cb27c 100644 --- a/python/__init__.py +++ b/python/__init__.py @@ -16,7 +16,8 @@ from itertools import accumulate from .constants import McuType from .dfu import PandaDFU from .isotp import isotp_send, isotp_recv -from .spi import SpiHandle, PandaSpiException +from .spi import PandaSpiHandle, PandaSpiException +from .usb import PandaUsbHandle __version__ = '0.0.10' @@ -291,7 +292,7 @@ class Panda: handle = None spi_serial = None try: - handle = SpiHandle() + handle = PandaSpiHandle() dat = handle.controlRead(Panda.REQUEST_IN, 0xc3, 0, 0, 12) spi_serial = binascii.hexlify(dat).decode() except PandaSpiException: @@ -342,7 +343,11 @@ class Panda: break context = usb1.USBContext() # New context needed so new devices show up - return handle, usb_serial, bootstub, bcd + usb_handle = None + if handle is not None: + usb_handle = PandaUsbHandle(handle) + + return usb_handle, usb_serial, bootstub, bcd @staticmethod def list(): diff --git a/python/base.py b/python/base.py new file mode 100644 index 00000000..d051e877 --- /dev/null +++ b/python/base.py @@ -0,0 +1,25 @@ +from abc import ABC, abstractmethod +from typing import List + + +# This mimics the handle given by libusb1 for easy interoperability +class BaseHandle(ABC): + @abstractmethod + def close(self) -> None: + ... + + @abstractmethod + def controlWrite(self, request_type: int, request: int, value: int, index: int, data, timeout: int = 0) -> int: + ... + + @abstractmethod + def controlRead(self, request_type: int, request: int, value: int, index: int, length: int, timeout: int = 0) -> bytes: + ... + + @abstractmethod + def bulkWrite(self, endpoint: int, data: List[int], timeout: int = 0) -> int: + ... + + @abstractmethod + def bulkRead(self, endpoint: int, length: int, timeout: int = 0) -> bytes: + ... diff --git a/python/spi.py b/python/spi.py index 3973e4d4..8b3cab10 100644 --- a/python/spi.py +++ b/python/spi.py @@ -9,6 +9,8 @@ from contextlib import contextmanager from functools import reduce from typing import List +from .base import BaseHandle + try: import spidev except ImportError: @@ -78,7 +80,7 @@ class SpiDevice: self._spidev.close() -class SpiHandle: +class PandaSpiHandle(BaseHandle): """ A class that mimics a libusb1 handle for panda SPI communications. """ diff --git a/python/usb.py b/python/usb.py new file mode 100644 index 00000000..3323ac70 --- /dev/null +++ b/python/usb.py @@ -0,0 +1,23 @@ +from typing import List + +from .base import BaseHandle + +class PandaUsbHandle(BaseHandle): + def __init__(self, libusb_handle): + self._libusb_handle = libusb_handle + + def close(self): + self._libusb_handle.close() + + def controlWrite(self, request_type: int, request: int, value: int, index: int, data, timeout: int = 0): + return self._libusb_handle.controlWrite(request_type, request, value, index, data, timeout) + + def controlRead(self, request_type: int, request: int, value: int, index: int, length: int, timeout: int = 0): + return self._libusb_handle.controlRead(request_type, request, value, index, length, timeout) + + def bulkWrite(self, endpoint: int, data: List[int], timeout: int = 0) -> int: + return self._libusb_handle.bulkWrite(endpoint, data, timeout) + + def bulkRead(self, endpoint: int, length: int, timeout: int = 0) -> bytes: + return self._libusb_handle.bulkRead(endpoint, length, timeout) +