Skip to content

tests: libusb_wrap_sys_device hotplug smoke test#44

Merged
josephnef merged 1 commit into
masterfrom
tests/wrap-sys-device-hotplug-harness
May 26, 2026
Merged

tests: libusb_wrap_sys_device hotplug smoke test#44
josephnef merged 1 commit into
masterfrom
tests/wrap-sys-device-hotplug-harness

Conversation

@josephnef
Copy link
Copy Markdown
Collaborator

Adds tests/in_process_hotplug_wrap.cpp — an in-process hotplug binary that constructs the libusb handle via libusb_wrap_sys_device(fd) instead of libusb_open_device_with_vid_pid. Mirrors the construction path Android takes when an app obtains a fd from UsbDeviceConnection.getFileDescriptor().

Follow-up to the kaeru episode 8821au-inprocess-hotplug-cant-repro-2026-05-25 which noted we lacked a Linux test exercising the wrap_sys_device codepath.

How it works

  1. Walks /sys/bus/usb/devices/ (NO libusb enumeration) to find a device matching VID:PID — default 2357:0120 / T2U Plus
  2. Reads busnum + devnum, opens /dev/bus/usb/BBB/DDD directly via open(2)
  3. libusb_wrap_sys_device(ctx, fd, &handle) → that's the construction path Android uses
  4. CreateRtlDevice + Init, count RX hits for RX_SECONDS
  5. Trigger sysfs unbind+rebind of the same port (full USB reset cycle)
  6. Repeats steps 1-4 — new fd, new wrap_sys_device handle in the same libusb context as session 1
  7. Prints per-session hit counts + verdict
g++ -std=c++20 -O2 tests/in_process_hotplug_wrap.cpp \
    -o /tmp/in_process_hotplug_wrap \
    -I src -I hal build/libWiFiDriver.a \
    $(pkg-config --cflags --libs libusb-1.0) -pthread

sudo /tmp/in_process_hotplug_wrap            # default 2357:0120
sudo /tmp/in_process_hotplug_wrap 0bda 8812  # explicit chipset

Honest scope note

This test does NOT reproduce the original Android hotplug bug on Linux. Validated 2026-05-26:

Built against iter1 hits iter2 hits Verdict
master 01cad75 (post-#42) 246 192 both RX ✓
master 8c87cd5 (pre-#42) 243 241 both RX ✓

Same outcome on both. So the Android gap evidently involves more than just libusb_wrap_sys_device construction — likely fd lifetime, USB reset semantics, or Android-stack-specific teardown behavior we don't replicate with sysfs unbind/rebind alone.

Treat this as a smoke check for the wrap_sys_device construction path, not a regression test for the specific hotplug bug.

Why land it anyway

  • Catches future breakage of the wrap_sys_device construction path itself (e.g. if some HAL change broke fd-wrap init, this would catch it).
  • Scaffolding for future experiments — find_usb_device, open_usbfs, and wrap helpers are reusable. Anyone trying to narrow the Android gap can iterate on closure timing, USBFS reset ioctls, etc., from this base.
  • Documents that "we tested wrap_sys_device on Linux master and it works" — useful when triaging future hotplug reports that may or may not be the same bug.

Build is opt-in (not added to CMakeLists). Header comment carries the compile + run instructions; no CI integration.

Test plan

🤖 Generated with Claude Code

Adds tests/in_process_hotplug_wrap.cpp — an in-process hotplug binary
that constructs the libusb handle via `libusb_wrap_sys_device(fd)`
instead of the traditional `libusb_open_device_with_vid_pid`. Mirrors
the construction path Android takes when an app obtains a fd from
`UsbDeviceConnection.getFileDescriptor()`.

## Why

Earlier hotplug investigation (kaeru episode
`8821au-inprocess-hotplug-cant-repro-2026-05-25`) tested several Linux
hotplug forms via `libusb_open_device_with_vid_pid`, none reproduced
the Android-side bug @RomanLut reported. The hypothesis at the time
was that the Android-specific gap was the `libusb_wrap_sys_device`
construction path. This binary lets us exercise that path directly
from Linux.

## How it works

  1. Walks /sys/bus/usb/devices/ (NO libusb enumeration) to find the
     device matching VID:PID — default 2357:0120 / T2U Plus.
  2. Reads busnum + devnum, opens /dev/bus/usb/BBB/DDD directly via
     open(2).
  3. `libusb_wrap_sys_device(ctx, fd, &handle)`.
  4. CreateRtlDevice + Init, count RX hits for RX_SECONDS.
  5. Triggers sysfs unbind+rebind of the same port (full USB reset
     cycle).
  6. Repeats steps 1-4 — new fd, new wrap_sys_device handle in the
     SAME libusb context as session 1.
  7. Prints per-session hit counts + a verdict.

Build:

    g++ -std=c++20 -O2 tests/in_process_hotplug_wrap.cpp \
        -o /tmp/in_process_hotplug_wrap \
        -I src -I hal build/libWiFiDriver.a \
        $(pkg-config --cflags --libs libusb-1.0) -pthread

Run (sudo for sysfs unbind + /dev/bus/usb open):

    sudo /tmp/in_process_hotplug_wrap            # default 2357:0120
    sudo /tmp/in_process_hotplug_wrap 0bda 8812  # explicit chipset

## Validation 2026-05-26

Built against current master (01cad75 / post-#42):
  iter1 hits=246 iter2 hits=192 → both sessions RX

Built against pre-#42 master (8c87cd5):
  iter1 hits=243 iter2 hits=241 → both sessions RX

Same outcome on both — pre-#42 master also passes the test. So this
binary does NOT reproduce the original Android hotplug bug on Linux.
The Android gap evidently involves more than just the
`libusb_wrap_sys_device` construction path — likely fd lifetime / USB
reset semantics / Android-stack-specific teardown that we don't
replicate with sysfs unbind/rebind alone.

## Scope

Treat this as a smoke check for the `libusb_wrap_sys_device` path,
not a regression test for the specific hotplug bug fixed by #42.
Useful for:

  - Catching future breakage of the wrap_sys_device construction path
    itself (if some HAL change broke fd-wrap init, this would catch it)
  - Scaffolding for future experiments that might reproduce the
    Android bug (e.g. with different fd close timing, USB-FS reset
    ioctls, etc.) — the find_usb_device + open_usbfs + wrap helpers
    are reusable
  - Documentation: "wrap_sys_device works on Linux master, the bug is
    elsewhere"

Build is opt-in (not added to CMakeLists). Header comment carries the
compile + run instructions; no CI integration.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@josephnef josephnef merged commit a13fb94 into master May 26, 2026
5 checks passed
@josephnef josephnef deleted the tests/wrap-sys-device-hotplug-harness branch May 26, 2026 04:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant