Fix Companion USB clients and shared-port logging

This commit is contained in:
mikecarper committed 2026-09-14 13:36:17 -07:00
1 parent bd30388436
commit b5440c75c7
14 files changed
+731 -47

No files matched your search

+117
View File
@@ -0,0 +1,117 @@
# USB Companion client compatibility
The RAK4631 report was reproduced on the MercerWoodMesh Pi with the published
`RAK_4631_companion_radio_full-v1.17.1.6-halo-keymind-cascade-dev-d4a641ff`
image. The fixes described here are in the source and test firmware;
the existing GitHub release assets have not yet been replaced with this fix.
Hardware validation was completed on 2026-09-14 with test builds labeled
`v1.17.1.6-usb-client-test-0e5955f8` (base commit plus these USB fixes).
## Port selection
| Hardware | Companion/ASCII port | USB logging |
| --- | --- | --- |
| RAK4631 and other nRF52 Full Companions | Primary CDC, Linux `*-if00` | Optional second CDC, `*-if02` |
| ESP32 Full Companions | Primary USB serial port | Shares that port; must be off for Binary Companion |
`+++MESHCORE-TERM-STOP` changes the primary port to Binary Companion. Its
`OK - Binary mode` reply does not move the client to a second port. Close the
terminal before opening that same port with `meshcli -s PORT infos`. Do not use
MeshCLI's repeater-mode `-r` option for a binary Companion connection.
## Two reproduced failures
1. Stock `meshcore-cli` 1.6.3 was tested with `meshcore` 2.3.10, whose serial client
deliberately deasserts DTR. The nRF52 primary USB gate and response route
both required DTR. A raw battery query returned a valid 14-byte response
with DTR high and no bytes with DTR low.
2. The automatic ASCII banner ended in `> `. MeshCLI recognized that `>` as a
binary frame marker and consumed the real response's leading `>` as a
length byte. Even forcing DTR high in a diagnostic client did not recover
the first `APP_START` response. Stock MeshCLI exited without node information.
The nRF52 transport now accepts real USB input as evidence of a client, tagged
with the current session generation. Close, line-coding, and device-reset
events invalidate that evidence; an old reader racing cleanup cannot grant
access to a newer session. The write path remains bounded and does not wait
for an unread host. CDC1 logging retains its own DTR requirement.
The default ASCII terminal now suppresses its banner and unsolicited terminal
events until ASCII input arrives. Enter or a command reveals the terminal.
A binary probe receives a clean first response. Explicit START/STOP remains
available, and an incomplete binary probe still times out back to ASCII.
On a shared logging port, STOP now reports
`ERROR: set usb.logging off before Binary mode` while logging is enabled.
It no longer acknowledges Binary mode and immediately reclaims the port for
ASCII logging.
## Hardware validation
The RAK4631 Full test firmware passed with an unmodified installed MeshCLI and
its normal DTR-low serial library:
- ASCII `ver` from the default mode, without START;
- explicit STOP acknowledgement followed by 10 binary battery queries;
- 10 immediate close/reopen queries each with DTR low and high;
- 10 stock-library reconnects and three separate `meshcli ... infos` runs;
- all of the above while the second CDC streamed logs: 598 bytes received,
no logging disconnections.
The Heltec V4 Full Companion also passed using its ESP32-S3 USB Serial/JTAG
interface, with USB logging disabled:
- default ASCII `ver`, then STOP and 10 clean binary battery replies;
- 10 stock-library reconnects and three unmodified `meshcli ... infos` runs;
- a stock-client connection directly after reboot, without either mode token.
The shared-port logging policy was exercised on the V4 as well. With logging
on, STOP returned the explicit error, a binary probe could not claim the port,
and stock MeshCLI could not connect. After `set usb.logging off`, STOP worked,
10 binary replies were clean, and three stock-client reconnects succeeded.
After testing, the V4's complete original flash snapshot was restored and
verified by the flasher. Its original `v1.17.1-soak-v4-B` firmware answered
again, and the previously active memory-soak collector was restarted. This
test interrupted that board's uptime; the restored firmware starts a new run.
Fresh successful V4 telemetry was verified at `2026-09-14T19:46:01Z`.
The RAK4631 remains on the patched Full Companion test build with its separate
logging interface enabled. The Pi's previously active services were restored.
The nRF52 DTR stress sequence is deliberately not used on ESP32. Changing those
control lines during immediate close/reopen triggered the V4's hardware
bootloader and produced an `ESP` boot message. Preselecting RTS before opening
did not prevent that transition on this host. ESP32 reconnect results above
use the unmodified client's normal connection sequence instead.
The test issues local information queries only. It does not transmit test chat
messages over LoRa. Firmware installation and logging configuration are
separate from the reusable test runner:
```sh
python3 tools/hil/companion_usb_client.py \
--port /dev/serial/by-id/PRIMARY-if00 \
--logging-port /dev/serial/by-id/LOGGING-if02 \
--test-dtr --cycles 10 --output usb-client-results.json
```
Replace the example paths with the device's actual stable paths. Install
`meshcore-cli` in the Python environment used for the test. Start after a fresh
reboot so the default-ASCII check has a new session. On ESP32, omit both
`--logging-port` and `--test-dtr`, and disable USB logging first.
## Automated checks
- 53 Python USB checks pass, including compiled execution of the real nRF52
callbacks and transport gate with DTR-low clients, cleanup races, early
input, line-coding changes, physical disconnects, full TX FIFO, CDC1 isolation,
and the 1200-baud bootloader entry.
- The real terminal methods are compiled for ordinary and Full modes to verify
that unsolicited text is suppressed before the first binary response.
- The actual STOP branch is exercised for logging off, shared-port logging on,
and dedicated-port logging on.
- 63 native serial-switching, flow-control, and terminal-session tests pass.
- RAK4631 Full and Heltec V4 Full firmware compile and pass their packaged
capability checks.
The USB workflow runs the new compiled regression tests on subsequent changes.
+30 -14
View File
@@ -11,8 +11,14 @@ to ASCII after a USB session reset that the hardware can observe. A Binary
Companion client does not need to send a special mode command: its first valid
frame automatically hands the interface to the binary parser.
This behavior applies to Full and ordinary USB Companion builds in the updated
1.17.1.6 release. The explicit `+++MESHCORE-TERM-START` and
Current source keeps the default terminal quiet until the host sends ASCII
input. Press Enter or type a command to see the banner. This prevents the ASCII
`>` prompt from being mistaken for the beginning of an app's first binary
response. See [USB client compatibility validation](companion_usb_client_validation.md)
for the tested firmware and release status.
These mode controls apply to Full and ordinary USB Companion builds. The
explicit `+++MESHCORE-TERM-START` and
`+++MESHCORE-TERM-STOP` controls remain available, including for older firmware.
See the [Terminal Chat CLI guide](./terminal_chat_cli.md).
@@ -151,6 +157,11 @@ mOTA traffic on the same stream.
| nRF52 USB serial mOTA active | mOTA owns primary USB; ASCII and Binary Companion are unavailable there |
| BLE/WiFi/Ethernet/hardware serial | Always Binary Companion and unaffected by the USB mode |
On RAK4631 and other nRF52 Full Companions, ASCII and Binary Companion both use
the primary interface (`*-if00` on Linux). The optional `*-if02` interface is
for logging. `OK - Binary mode` acknowledges a mode change on the same primary
port; it does not move the connection to the logging port.
On every ESP32 Full Companion, use `set usb.logging off` in its logging terminal
before trying to use USB with an app. That command stops logging and returns to
the ordinary ASCII terminal; it does not select Binary Companion. The app's
@@ -162,6 +173,10 @@ base where supported; RC32 and ESP32-C6 keep their board-required Arduino 3.x
platform but follow the same one-TTY policy. No ESP32 Full build enumerates a
second CDC interface.
With logging enabled on a shared port, the stop token is rejected with
`ERROR: set usb.logging off before Binary mode`. Turn logging off first; a
successful binary-mode acknowledgement must not immediately revert to logging.
On nRF52, serial `motatool` is also text-first. Its exact initial
`ota folder on` line is recognized in either startup ASCII or Binary Companion
mode. From ASCII, the firmware leaves terminal mode and directly enters
@@ -186,6 +201,12 @@ mode for that session. Native USB with DTR/session events restores ASCII when
the host closes or resets the connection. Old input, queued output and mOTA
ownership are cleared before selecting the default for the next session.
Some clients, including `meshcore` 2.3.10 used by stock MeshCLI, deliberately
deassert DTR. The nRF52 primary interface also accepts actual incoming USB data
as evidence of a client in the current session. Closing, changing line coding,
or resetting USB invalidates that evidence. The dedicated logging interface
continues to require its own connected reader.
ESP32 hardware USB Serial/JTAG reports bus reset and physical host loss, but
cannot reliably report a terminal program closing its handle. A USB-to-UART
bridge also usually cannot report that close. On those ports, use the terminal
@@ -216,19 +237,14 @@ unknown or malformed at the application layer. This is safe for stream
separation but means a random complete frame can leave the device in binary
mode until an observable USB session reset or a manual switch back.
### The terminal banner is best effort
### The default terminal waits for input
The firmware enters ASCII mode during boot, often before a host opens the CDC
device. The banner may therefore be absent even though the terminal is ready;
send a newline or a harmless `get` command rather than treating a missing
banner as proof of binary mode.
A host that remains connected across a reboot may receive ASCII banner and
prompt bytes before the first binary response. Binary clients should discard
leading bytes until a plausible `>` frame marker and length are found, reject
implausible lengths, and resynchronize. The ordinary open-after-boot path has
been tested with `meshcli`, but third-party clients that assume byte zero is
always `>` may fail.
The firmware enters ASCII mode during boot but suppresses the banner and
unsolicited terminal events until ASCII input identifies a terminal client.
Send Enter or a harmless `get` command to display the banner. A binary client
instead gets a clean first response. Older builds that immediately printed
`> ` could consume the first binary response's marker as part of a bogus frame
length; scanning past leading text alone did not reliably recover that reply.
### Text and binary output cannot be interleaved
+8
View File
@@ -91,6 +91,10 @@ Companion. You can open a terminal without a start token:
picocom --baud 115200 /dev/ttyACM0
```
Current source waits for input before printing the USB banner. Press Enter or
type a command. This preserves the clean first response needed by binary apps;
see [USB client validation](companion_usb_client_validation.md) for release status.
For older firmware or a port already in Binary mode, use:
```sh
@@ -125,6 +129,10 @@ Send the following exact sequence to return to the binary protocol:
+++MESHCORE-TERM-STOP
```
If logging shares the primary USB port (ESP32), first run `set usb.logging off`.
With logging still on, the stop token reports an error and keeps ASCII logging
active. nRF52 Full's separate logging port can remain enabled.
An observable USB session reset restores ASCII after clearing the previous
client's state. Native USB with DTR can detect a terminal closing; ESP32 hardware
USB Serial/JTAG detects bus resets and physical host loss. USB-to-UART bridges