Auto-select COM ports on Windows#2
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds Windows auto-discovery for the firmware COM port (--device) and com0com virtual port (--virtual-port), aligning Windows behavior with the existing Unix auto-discovery so hurra-bridge.exe can run with no flags in common setups.
Changes:
- Introduces a new
serial_enummodule: a pure hardware-ID classifier plus a Win32 SetupAPI enumerator (and a non-Windows stub). - Wires Windows auto-selection into
bridge.cand updates help/error messaging accordingly. - Adds classifier unit tests and updates CMake/README (including linking
setupapion Windows).
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/serial_enum_test.c | Adds unit tests for the pure HWID classifier logic. |
| src/serial_enum.h | Declares the serial port candidate model + classifier/enumerator API. |
| src/serial_enum.c | Implements the case-insensitive HWID classifier. |
| src/serial_enum_win32.c | Implements COM port enumeration via SetupAPI and classification. |
| src/serial_enum_stub.c | Provides a non-Windows serial_enum() stub returning 0 candidates. |
| src/bridge.c | Integrates Windows auto-detection for --device and --virtual-port; updates usage and failure paths. |
| README.md | Updates Windows quickstart and flag documentation to reflect auto-detection. |
| CMakeLists.txt | Adds new sources/tests and links setupapi on Windows builds. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| char name[32]; | ||
| if (!read_port_name(set, &dev, name, sizeof name)) continue; | ||
|
|
There was a problem hiding this comment.
Fixed in 1ea3a29 — skip any PortName beginning LPT during enumeration, so parallel ports don't clutter error listings or consume candidate slots.
| /* Enumerate present COM ports, newest API available per platform. Fills up to | ||
| * `max` candidates, returns the count. Returns 0 on failure or non-Windows. */ | ||
| size_t serial_enum(serial_cand_t *out, size_t max); |
There was a problem hiding this comment.
Collapsing failure and empty into a single 0 return is intentional here, not an oversight — it's the documented design (spec: "SetupAPI / registry failures degrade to 'zero candidates' → existing explicit error paths fire").
The two states aren't actionably different at the call sites: in both cases the bridge cannot auto-select a port and must fall back to the explicit-flag error path. Distinguishing them would only change error wording, while complicating the API contract (out-param or sentinel) that every caller has to handle. That's the graceful degradation the summary refers to — a failed SetupAPI call lands on the same clean bridge_fail as 'no device plugged in', never a crash. Keeping the simple size_t count contract on purpose (YAGNI).
GUID_DEVCLASS_PORTS covers both COM and LPT devices; filter out LPT PortNames so parallel ports don't clutter error listings or consume candidate slots.
Summary
On Windows,
--deviceand--virtual-portare now auto-detected, mirroring the existing Unix glob-based auto-discovery. Runninghurra-bridge.exewith no flags now Just Works when a Hurra device and a com0com pair are present.--device): a new SetupAPI enumerator walksGUID_DEVCLASS_PORTSand classifies ports by hardware ID. The WCH CH343 (VID_1A86) is auto-selected when exactly one is present; zero or multiple produce a clear error listing what was found.--virtual-port): auto-selected (lowest-numbered pair end, since the ends are interchangeable) when the VCOM endpoint is chosen. KMBox endpoint users never need com0com. If no pair exists, the existing "install com0com" guidance is shown — no auto-creation.Implementation
serial_enummodule: a pure, host-testable hardware-ID classifier (serial_enum.c) plus a Windows SetupAPI enumerator (serial_enum_win32.c) and a non-Windows stub (serial_enum_stub.c).bridge.cwires both auto-selects intomain(); the--device-required-on-Windows rejection is removed.setupapilinked on Windows; help text and README updated.Test Plan
tests/serial_enum_test.c, via CTest) — firmware/com0com/other, case-insensitivity, priority, empty/NULLsetupapilink verified by thewindows-latestCI job--device/--virtual-portoverride; KMBox endpoint works with no com0com pair