tests: libusb_wrap_sys_device hotplug smoke test#44
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
tests/in_process_hotplug_wrap.cpp— an in-process hotplug binary that constructs the libusb handle vialibusb_wrap_sys_device(fd)instead oflibusb_open_device_with_vid_pid. Mirrors the construction path Android takes when an app obtains a fd fromUsbDeviceConnection.getFileDescriptor().Follow-up to the kaeru episode
8821au-inprocess-hotplug-cant-repro-2026-05-25which noted we lacked a Linux test exercising the wrap_sys_device codepath.How it works
/sys/bus/usb/devices/(NO libusb enumeration) to find a device matching VID:PID — default2357:0120/ T2U Plusbusnum + devnum, opens/dev/bus/usb/BBB/DDDdirectly viaopen(2)libusb_wrap_sys_device(ctx, fd, &handle)→ that's the construction path Android usesCreateRtlDevice + Init, count RX hits forRX_SECONDSg++ -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 chipsetHonest scope note
This test does NOT reproduce the original Android hotplug bug on Linux. Validated 2026-05-26:
01cad75(post-#42)8c87cd5(pre-#42)Same outcome on both. So the Android gap evidently involves more than just
libusb_wrap_sys_deviceconstruction — 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_deviceconstruction path, not a regression test for the specific hotplug bug.Why land it anyway
wrap_sys_deviceconstruction path itself (e.g. if some HAL change broke fd-wrap init, this would catch it).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.Build is opt-in (not added to CMakeLists). Header comment carries the compile + run instructions; no CI integration.
Test plan
8c87cd5) and post-RTL8821AU: 8821-specific init flow (replaces trace-derived parity) #42 (01cad75) — confirms the test does not depend on PR RTL8821AU: 8821-specific init flow (replaces trace-derived parity) #42's changes🤖 Generated with Claude Code