Repository navigation
feat(can): report each bus's health to the app over BLE - #115
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XkZEiX4By1AMegtzFhVwNo
Board CAN drivers hand each RX window (frames, matched, RX errors, overruns, bus-offs) to core_interface::report_can_bus_window. The new can_health module keeps totals since boot per bus, judges the last window (OK, silent, degraded, no valid frames, bus-off) and publishes SystemState.can_bus_health over BLE: at once on a status or mode change, every 5 s otherwise, and again when the app subscribes. ESP TWAI reports the counters behind its 5 s RX log, and a bus-off at once. The PC board now counts SocketCAN frames and error frames. MCP2515 buses don't report yet. Part of #12, closes #27. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XkZEiX4By1AMegtzFhVwNo
Hardware test: WiCAN (ESP32-C3) + virtual-carTested together with OpenCarControls/contracts#43 and OpenCarControls/mobile-app#127. Setup
The PRs list no manual checks, so all tests come from reading the diffs. 1. [Analysis] The CAN buses row appears: passSettings → CAN buses shows "Bus 0 (listen-only) · Silent", "0 frames · 0 errors · 0 overruns · 0 bus-offs". This matches the serial log.
2. [Analysis] Bus health shown when Settings first opens: fail as submitted, pass with
|
| Time (UTC) | Event |
|---|---|
| 18:41:12.3 | Window report: listen-only |
| 18:41:14.8 | Write lock off: TWAI started in normal mode |
| 18:41:17.3 | Window report: (normal); the app updates, 2.5 s after the switch |
| 18:41:23.4 | Write lock on: TWAI started in listen-only mode |
| 18:41:27.3 | Window report: (listen-only); the app updates, 3.9 s after the switch |
Note on firmware#115:
- When the mode changes,
run_twai_sessionreturns without ending the RX window. The new mode reaches the app only when that window ends, up to 5 s later, rather than "right away". - The window that spans the switch is reported under the new mode.
- Neither blocks the PR. Ending the window on a mode change, as is done on bus-off, would make the update immediate.
4. [Analysis] No regression on the BLE link: pass
- About 20 minutes of serial log, including the extra bus health message every 5 s: no
BLE GATT TX dropped, queue-full or panic lines. - Car access and the dashboard kept updating.
5. [Analysis] Board reset with the app connected: pass
A watchdog reset while the app was on Settings. The row came back as soon as the app reconnected.
| Time (UTC) | Event |
|---|---|
| 18:43:21.8 | Boot: 1 bond, bus 0 in listen-only mode |
| 18:43:26.9 → | Bus 0 window reports every 5 s while the app is away |
| 18:43:56.3 | Phone reconnected (the app's 30 s retry gap) |
| 18:43:56.6 | Link encrypted |
| 18:43:57.6 | Subscribed; latest state resent and the row shows at once |
Unrelated observation
After adb install -r of the new APK, the BLE link seems to have stayed up. The log shows only a new subscribe at 18:38:44, with no new connect or encryption line.
Not tested
- The OK, Errors (degraded), No valid frames and Bus-off statuses, overruns, and the early report on bus-off. These need a live CAN bus; an in-car test with the E-GMP firmware is planned next.
- The PC SocketCAN path (no vcan here).
- MCP2515 (not implemented in this PR).
- The section being hidden over MQTT.
- iOS, a second phone, and ESP32-S3 (Xtensa) builds.
A switch between listen-only and normal mode now ends the RX window, like a bus-off, so the app sees the new mode at once instead of up to 5 s later, and the window is reported under the mode it ran in. Found in the hardware test on #115. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XkZEiX4By1AMegtzFhVwNo
Hardware test in the car: WiCAN (ESP32-C3) + egmpTested together with OpenCarControls/contracts#43 and OpenCarControls/mobile-app#127. Setup
A. [Analysis] Car awake → OK: passThe app showed "Bus 0 (listen-only) · OK", "151409 frames · 0 errors · 0 overruns · 0 bus-offs" at 20:06:43 UTC. That matches the serial log's window sum exactly, up to the window that ended at 20:06:38.
About 2,160 frames/s, with 0 RX errors and 0 overruns. There were no B. [Analysis] Car going to sleep → Silent: skippedSkipped because the phone had to stay on USB in the car. The desk tests already showed Silent rendering correctly. C. [Analysis] Wrong bitrate (250 kbit/s, still listen-only) → No valid frames: failThe car was awake: about 10,800 frames per 5 s just before and just after the test. Bus 0 still reported Cause: the ESP TWAI driver never counts bus errors (bit, stuff, form, CRC, ACK). esp-hal 1.1.2's Follow-ups, in progress on this PR set:
Not tested in the car: bus-off (shouldn't be provoked on a car, and a listen-only controller can't go bus-off) and overruns (none seen). The WiCAN was flashed back to the 500 kbit/s build afterwards. |
Drivers pass each window's length, so the rate is right for a window cut short by a bus-off. Also documents that TWAI doesn't count RX errors yet (#117), so a wrong bitrate reads as silent. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Drops the frame totals from the bus health: on a busy bus they would saturate after a few weeks of the bus being awake. The serial log keeps each window's frame counts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A mode switch or a bus-off ends the window early, but the line still said "last 5s". It now gives the window's length to a tenth of a second, e.g. "last 0.2s" around the identity probe. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Frames per second: changes and in-car retestTogether with OpenCarControls/contracts#43 and OpenCarControls/mobile-app#127. Changes after the in-car test
Checks: Retest in the car: pass
Before merging: the firmware and app pins point at the contracts PR branch ( |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XkZEiX4By1AMegtzFhVwNo
What and why
Shows CAN bus health to the app, the last open item of #12. Closes #27. The wire format is in OpenCarControls/contracts#43.
core-interface/src/can_health.rs:CanRxCounts) toreport_can_bus_window(bus_id, &counts, listen_only).can_bus_status).publish_can_bus_health_tasksendsSystemState.can_bus_healthover BLE only: right away when a bus changes status or mode, or reports for the first time; otherwise every 5 s; and again when the app subscribes. Sends usetry_send, so a full queue or a link that is down never blocks.RxStatsnow uses the shared counters. The 5 s window that feeds the serial log is also reported, and a bus-off ends the window early so it is reported at once.listen_onlyis left unset here, because SocketCAN drops TX in software rather than in hardware.docs/can.md, plus updates todocs/ble.mdanddocs/architecture.md.The contracts submodule bump is its own commit. It points at the contracts PR's branch commit, so it needs re-pinning once that PR merges.
How it was tested
cargo xtask testpasses. New tests:cargo fmt --all -- --checkpasses.cargo xtask clippyandcargo xtask buildpass for--board pcand for--board esp(ESP32-C3, example config), with bothegmpandvirtual-car.+esptoolchain; anything on real hardware or a car.Checklist
cargo fmt --all -- --check,cargo xtask clippyandcargo xtask testpassconfig.toml, credentials, build artifacts or.binfiles committedSafety
Generated by Claude Code