Skip to main content

Simple serial connectivity to CDC (serial) USB devices exposed by a USBIPD service.

Project description

USBIP Serial client workflow Python PyPI - Implementation coverage


Overview


This package supports connecting to a CDC (serial) USB device exposed by a USBIPD server. USBIP is a protocol that allows sharing USB devices over a TCP/IP connection. The protocol specification can be found here. This package should be capable of running on any system that supports Python.

There are some issues sharing USB devices to docker containers, a major one being if the USB connection is lost it is difficult to recover the connection between the docker container and the hosting server.

Here's a link that discusses this issue and another solution.

The USBIP client implementation will only address USB devices that implemented the CDC protocol, basically simple serial devices. This allows for a direct connection to the USBIP server without the need for mapping USB devices into the container.

Installation


pip install serial-usbipclient should work for most users.

Usage


from serial_usbipclient.usbip_client import USBIPClient, HardwareID, USBIP_Connection

host: str = 'localhost'
port: int = 3240  # commonly used port for USBIPD servers
target: HardwareID = HardwareID(vid=1234, pid=5678)  # USB devices are identified by VID/PID
client: USBIPClient = USBIPClient(remote=(host, port))
client.connect_server()
client.attach(devices=[target])
connections: list[USBIP_Connection] = client.get_connection(device=target)

# using the established connection, data can be written to the USB device
# using the sendall() method
connections[0].sendall(data=b'\01\02\03\04')

# response data can be read either explicitly by specifying the size of the expected
# response, or if 0 size is specified, up to a delimiter. The delimiter is a property of
# the connection and can be set, default=b'\r\n'
connections[0].delimiter = b'\n'
response: bytes = connections[0].response_data(size=0)  # reads until delimiter

SOUP


Module Version comments
Python 3.11, 3.12 Python interpreter
py-datastruct 1.0.0 Serialization of binary to/from dataclasses
type-extensions 4.12.2 extensions for typing (SocketWrapper typing)

Useful Resources


For a Windows version of the usbipd server, look here. You can run this to share USB devices across a network, there are usbipd-clients for Linux & Windows.

Here's a USBIP server for testing written in RUST, https://github.com/jiegec/usbip.

Testing


A MockUSBIP service reads configuration information from the output of lsusb (e.g lsusb -d 1f46:1b01 -v). MockUSBIP will then play back this configuration. Just capture the output of the lsusb command and save with the .lsusb suffix in the test folder. The file name should be the busnum/devnum number.

1-1.lsusb
1-2.lsusb
1-3.lsusb
99-99.lsusb

Would result in 4 devices with busid values of 1-1, 1-2, 1-3 and 99-99. Please note the file 99-99.lsusb is reserved to provide a device to generate failing USBIPD attachments, and 1-2 & 1-3 have the same VID/PID (for testing multiple identical device connections).

During testing, the MockUSBIP service acts as a stand-in for an actual USBIP server. Since the tests are run in parallel, the port on which the service listens must be unique for each unit test. This is accomplished by the conftest.py::pytest_sessionstart which is run when the pytest session is started and collects all tests being run into a file called lists_of_tests.json. The unit test's index into this array is used to determine the offset to be added to the port base (typically 3240). The SocketWrapper class is overridden in unit tests to inject low-level socket failures.

Tooling


This package was created using JetBrains PyCharm Professional IDE, the repository does not contain any IDE specific files. Development work was done using Windows 11 & Python 3.12.5, with final verification on an Ubuntu 20.04 system.

Static code analysis performed using radon and xenon.

Packages required to run tests


Module Version comments
pytest 8.3.2 unit testing framework
pytest-xdist 3.6.1 distributes testing across multiple cpu/cores
coverage 7.6.1 coverage of unit tests
pylint 3.2.6 linter, ensures adherence to PEP-8 standards
pytest-cov 5.0.0 integrates coverage with pytest
pytest-timeout 2.3.1 provides ability to timeout pytest unit tests
mypy 1.11.2 type checking
radon 6.0.1 static code analysis
xenon 0.9.1 static code analysis with thresholds

Packages required publish to PyPi


Module Version comments
poetry-core 1.9.0 build system

All tooling is defined in the pyproject.toml and managed using poetry as follows:

poetry install --with tests

Tooling is not needed to run the package but is required for testing & packaging.

Build Process


Store the PyPi API token

keyring set https://upload.pypi.org/legacy/ __token__

when prompted enter the API token you created using PyPi.

To build the distribution and upload to PyPi

poetry build
poetry publish

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

serial_usbipclient-1.1.0.tar.gz (28.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

serial_usbipclient-1.1.0-py3-none-any.whl (29.8 kB view details)

Uploaded Python 3

File details

Details for the file serial_usbipclient-1.1.0.tar.gz.

File metadata

  • Download URL: serial_usbipclient-1.1.0.tar.gz
  • Upload date:
  • Size: 28.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.12.10 Windows/11

File hashes

Hashes for serial_usbipclient-1.1.0.tar.gz
Algorithm Hash digest
SHA256 d455a8f22b58e84b4a3ff37a2894dcf87a70af81f1b13452ce40aefe04e6ab1b
MD5 37736de7c3586b97d3c4a4665ec5c942
BLAKE2b-256 1b9fefe5db3c99f9813d7f9f752e251e8f32118b2d849510e7fa93e0e2616006

See more details on using hashes here.

File details

Details for the file serial_usbipclient-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for serial_usbipclient-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 73da1d3617cc506fc31282082f2ef405e8d6e2849a7ffd493651d381027928df
MD5 70491c3969ae9d47bd8ccbde4bf50f84
BLAKE2b-256 ce211b1eaac7e07f010c2efb7767cdcef119cadea6a06595514590f0ef0e9a96

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page