Skip to content

Rewrite device model auto-detection to use config memory magic#46

Merged
cr merged 4 commits intocr:devfrom
johannessen:autodetect
Jul 7, 2025
Merged

Rewrite device model auto-detection to use config memory magic#46
cr merged 4 commits intocr:devfrom
johannessen:autodetect

Conversation

@johannessen
Copy link
Contributor

I took a swing at the device model auto-detection, as we discussed in #42 (comment).

This proposed change largely replaces the enumerate() function with new logic that’s better suited for adding new radio models, especially those lacking USB. It does so by removing hard-coded lists of model classes, and by reading config memory magic rather than the flash ID (#44).

Reading data from the device memory for identification is potentially problematic: Some devices require different baud rates than others, which can cause delays due to timeouts, and some systems have extra serial ports that raise errors. To avoid these issues, the proposed logic trusts USB metadata whenever possible, which means reading the magic is rarely even necessary. It also adds an exclusion list of OS-specific serial ports known to cause errors or timeouts.

Additionally, this change streamlines the user experience by handling the --tty and --model CLI options more consistently: Both options when given now simply restrict the auto-detection in accordance with the given values.

For example, hxtool --model HX890 will now run the full auto-detection logic, but only consider devices identifying as HX890. This is a new feature, a rather useful one when you work with multiple device models connected at the same time. Previously, --model simply caused an error unless --tty was given, too.

As before, overriding auto-detection is possible by specifying both --tty and --model.

Localising knowledge about the simulators for each device model in the respective model classes avoids the proliferation of simulator generation statements in enumerate().
- Allow device selection with --model by itself (without --tty given). This is very useful when you have two different models connected at the same time (like HX870 and GX1400) because the model names are easy to remember, but the tty names are not.

- Allow --tty to specify an incomplete match (for example, `--tty usbserial` is now enough to select `/dev/cu.usbserial-17522`). An exact match is still preferred if there is one.

- Bring back model auto-detection for GX1400, now using the config memory magic rather than the flash ID. If the GX1400 is the only connected device, it should work without --tty or --model. This change makes provisions for reading the config magic from other non-USB device models that might need several different baudrates, although in practice, all the GX and HX models that are implemented in hxtool at this time seem to work correctly at 38400 baud.

- Auto-detecting the device model by USB meta data is now preferred whenever possible, even with --tty given. This should be fast and reliable, and it makes for a more unified approach to device selection.

- Overriding auto-detection is still possible by specifying both --tty and --model.
@cr
Copy link
Owner

cr commented Jul 7, 2025

Please let me know when it is rtl.

@johannessen
Copy link
Contributor Author

From my perspective, it has been ready since I opened the PR last night (except for a typo that I fixed immediately).

Did I miss something?

@cr
Copy link
Owner

cr commented Jul 7, 2025

I won't get to a full review. The tests with the sims pass fine? Anything more to add to the other tests, perhaps?

@johannessen
Copy link
Contributor Author

Sure, tests are passing. This PR doesn’t touch any of the tests that already existed, they’re all fine as far as I can tell.

As for a review – because the rewrite has left very little of the old enumerate() function, the GitHub diff is struggling to compare the new version with the old one. If you did want to look at the code, the regular source view of device.py in the branch to be merged might be more useful to read through than the diff.

For what it’s worth, I’m quite confident about the design overall. However, the new code surrounding ReadMagicProtocol seems inelegant to me. That’s the part I’m least happy with here.

This PR will merge into the dev branch, so it won’t be “live” right away. But it seems to me that dev is probably ready to be merged into master, too. 🙂

@cr cr merged commit 36cb5a5 into cr:dev Jul 7, 2025
@johannessen johannessen deleted the autodetect branch July 13, 2025 12:27
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.

2 participants