didactek / deft-simple-usb

Usermode support of custom USB device drivers in Swift on macOS and Linux, using either the libusb system library or the IOUSBHost framework.
Apache License 2.0
18 stars 1 forks source link

Deft Simple USB

Usermode support for writing custom USB device drivers in Swift on macOS and Linux.

API

Usage

Deft Simple USB provides a synchronous API.

Supports

Logging

Loggers are instantiated using the deft-log library using the label prefix com.didactek.deft-simple-usb.

Requirements

Mac requirements

SPM Dependencies

macOS C library dependencies

Linux C library dependencies

Support for older environments

Older environments are relatively easily supported by replacing the .target(name:condition:(.when)) dependencies in the Package.swift with appropriate hardcoded descriptions. The code was largely developed with Swift 5.2. The IOUSBHost framework appeared in macOS 10.15 (Catalina), and the libusb library should be available on most platforms.

Platform Notes

macOS

IOUSBHost implementation

The default provider of the USBBus protocol on macOS is the HostFWUSB module, which is built on the IOUSBHost usermode framework. PortableUSB.platformBus() will vend one of these, one can be instantiated directly.

There are no external dependencies, certifications, or special permissions required to build and use this implementation.

libusb.info implementation

The LibUSB module (and its bridge module CLibUSB) that provides services on Linux will also work on macOS.

The Swift Package Manager will install libraries according to the Package.swift manifest. On macOS, this requires two additional tools be installed:

A quick homebrew test should show

% pkg-config --libs libusb-1.0 -L/usr/local/Cellar/libusb/1.0.23/lib -lusb-1.0

If brew is installed with a nonstandard prefix (e.g., somewhere under a user's home directory), you may need a symlink for pkg-config so it can be found by Xcode. (Notably, Xcode will search /usr/local/bin.)

Linux device permissions

On Linux, users will not have access to a hot-plugged USB device by default. The cleanest way to systematically grant permissions to the device is to set up a udev rule that adjusts permissions whenever the device is connected.

The paths and group in the template below assume:

Under /etc/udev/rules.d/, create a file (suggested name: "70-gpio-ftdi-ft232h.rules") with the contents:

# FTDI FT232H USB -> GPIO + serial adapter
# 2020-09-07 support working with the FT232H using Swift ftdi-synchronous-serial library
ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6014", MODE="660", GROUP="plugdev"

eLinux.org has a useful wiki entry on accessing devices without sudo.

History

Extracted from the ftdi-synchronous-serial project.

To overlay the full history of these files: