Skip to content

About

USB host access and serial port communication for HuxerUI.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Lib-DeviceIO

Lib-DeviceIO is a C++ library for USB host access and asynchronous serial communication in HuxerUI applications. UsbHost discovers USB devices, requests access, and opens connections for interface claims and data transfers. Serial discovers ports and opens configured connections for byte I/O. The services can be used independently. Android CDC ACM serial uses the library's USB implementation. HuxerUI

Integration

The project uses HuxerUI 0.5.5. CMake consumers use CMake 3.20 or later and C++20. The mcpp configuration uses C++23.

CMake

Load the SDK with find_package(HuxerUI CONFIG REQUIRED) and create your application target with huxerui_add_app(). Then add one huxerui_use_library() declaration from the following options. Replace my_app with your target name. For a Git dependency, set DEVICEIO_REPOSITORY_URL to the library repository's HTTPS Git URL without embedded credentials. The DEVICEIO_* variables can be set in CMake or supplied as -D cache options when configuring.

Git tag

Set DEVICEIO_TAG to a tag that exists in that repository. Pass its plain name, such as v0.1.0, without the refs/tags/ prefix.

huxerui_use_library(my_app
    TARGET HuxerUI::DeviceIO
    URL "${DEVICEIO_REPOSITORY_URL}"
    TAG "${DEVICEIO_TAG}"
)

Git commit

Set DEVICEIO_COMMIT to the full 40-character Git SHA of the revision you want to pin. You can obtain it with git rev-parse HEAD from a checkout at that revision. Each Git declaration takes exactly one of TAG and COMMIT.

huxerui_use_library(my_app
    TARGET HuxerUI::DeviceIO
    URL "${DEVICEIO_REPOSITORY_URL}"
    COMMIT "${DEVICEIO_COMMIT}"
)

Local checkout

To use a checkout beside the application, supply PATH pointing at the directory that contains the library's CMakeLists.txt.

huxerui_use_library(my_app
    TARGET HuxerUI::DeviceIO
    PATH "${CMAKE_CURRENT_SOURCE_DIR}/../Lib-DeviceIO"
)

CMake consumers include <huxerui/deviceio.h>. Linkage is private by default. Use PUBLIC if another library's public headers expose DeviceIO types.

mcpp

Add the local library to the application's mcpp.toml. The path is relative to the manifest.

[dependencies]
Lib-DeviceIO = { path = "../Lib-DeviceIO" }

Use import DeviceIO; in a C++ module consumer.

Quick start

Register deviceio::Install once in AppOptions::application_hooks. Installation makes both services available through UseService. Open devices and request permission from a Task launched by an event handler or committed Lifecycle setup on the application's thread.

#include <huxerui/deviceio.h>
#include <huxerui/huxerui.h>

using namespace huxerui;

View App() {
    return Text("DeviceIO");
}

const Application application{
    App,
    {.application_hooks = {deviceio::Install}},
};

USB host

Await UseService<UsbHost>()->EnumerateAsync() and keep the ID of the device the user selects. Request access before opening on Android. For Bulk or Interrupt transfers, inspect UsbConnection::Interfaces(), claim the selected interface and alternate setting, then use the endpoint's address with ReadAsync() or WriteAsync(). Use ControlInAsync() and ControlOutAsync() for EP0 requests. Isochronous endpoints appear in the descriptor snapshot and return Unsupported when used for I/O.

This helper reads the standard device descriptor over EP0. It takes a device ID from enumeration and runs inside a HuxerUI Task.

#include <huxerui/deviceio.h>

#include <utility>

using namespace huxerui;
using namespace deviceio;

Task<ReadResult> ReadDeviceDescriptorAsync(UsbDeviceId id) {
    auto usb = UseService<UsbHost>();
    auto access = co_await usb->RequestAccessAsync(id);
    if (!access.Succeeded()) co_return ReadResult{.error = access.Error()};

    auto opened = co_await usb->OpenAsync(id);
    if (!opened.Succeeded()) co_return ReadResult{.error = opened.Error()};

    auto connection = std::move(opened).Value();
    auto result = co_await connection.ControlInAsync({0x80, 0x06, 0x0100, 0}, 18, {.timeout = 1s});
    co_await connection.CloseAsync();
    co_return result;
}

Serial

Await UseService<Serial>()->EnumerateAsync() and pass the selected port ID to OpenAsync(). The default line settings are 115200 baud, eight data bits, no parity, and one stop bit. To change settings on an open port, call ConfigureAsync() while its I/O is idle. One read and one write may run concurrently. A successful serial read returns at least one byte, up to the requested limit.

This helper opens the selected port at 9600 baud, writes an owned buffer, and waits for the port to close.

#include <huxerui/deviceio.h>

#include <utility>

using namespace huxerui;
using namespace deviceio;

Task<WriteResult> SendSerialAsync(SerialPortId id, Bytes payload) {
    auto serial = UseService<Serial>();
    auto opened = co_await serial->OpenAsync(id, {.baud_rate = 9600});
    if (!opened.Succeeded()) co_return WriteResult{.error = opened.Error()};

    auto port = std::move(opened).Value();
    auto result = co_await port.WriteAsync(std::move(payload), {.timeout = 1s});
    co_await port.CloseAsync();
    co_return result;
}

Android Serial::OpenAsync() requests access when needed. Denial returns PermissionDenied and leaves the port closed. An Android USB host connection and a CDC ACM serial connection to the same physical device are mutually exclusive, including multiple CDC ACM ports on that device.

Connections and results

  • A device or port ID identifies the current attachment. After unplugging and reconnecting, enumerate again and open a new connection.
  • A connection owns its system resources. Await CloseAsync() to wait for their release. A pending operation reports Closed on closure or Disconnected if removal was recorded first. Canceling its owning Task suppresses result delivery and requests cancellation of the operation.
  • A read can report bytes alongside an error. For writes, inspect WriteResult::bytes_transferred and count_exact. A false count_exact means the confirmed count is a lower bound.
  • If changing serial settings fails, the library attempts to keep or restore the previous settings. If recovery fails and the library closes the connection, ConfigureAsync() returns Closed with the original diagnostic. Removal reported before closure returns Disconnected.

See the public header for the complete API and examples.

Platform support

Platform USB host Serial
Windows Direct access to available interfaces, such as those bound to WinUSB System COM ports
macOS User-space access to available USB interfaces. Sandboxed applications need the USB entitlement. System serial ports, such as /dev/cu.*
Linux Access subject to USB device-node permissions and interface availability System tty devices, including CDC ACM ports exposed by the system
Android API 23 or later on devices with USB Host support. Access is granted per attachment. USB CDC ACM
iOS Unsupported Unsupported
Web Unsupported Unsupported

USB Host leaves system-owned interfaces bound. Desktop serial access uses the operating system's serial drivers.

Preview

The preview demonstrates USB enumeration, access requests, descriptor and endpoint reads, and serial read and write operations. Run it from the library checkout with the SDK and the target platform's toolchain installed. Preview source

huxerui run macos
huxerui run android

For a serial round trip, use a loopback setup or a device that echoes the data.

License

Lib-DeviceIO is distributed under the MIT License. The bundled libusb sources retain their upstream license. See the libusb notes for the version and source provenance.

About

USB host access and serial port communication for HuxerUI.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages