Skip to content

feat(can): report each bus's health to the app over BLE - #115

Merged
Tichael merged 9 commits into
mainfrom
claude/sweet-albattani-i9lueo
Nov 24, 2026
Merged

Tichael merged 9 commits into
mainfrom
claude/sweet-albattani-i9lueo

Conversation

@Tichael

@Tichael Tichael commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

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:
    • Board drivers hand each RX window (CanRxCounts) to report_can_bus_window(bus_id, &counts, listen_only).
    • It keeps totals since boot for each bus and works out the window's status (can_bus_status).
    • publish_can_bus_health_task sends SystemState.can_bus_health over 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 use try_send, so a full queue or a link that is down never blocks.
  • ESP TWAI: the existing RxStats now 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.
  • PC (SocketCAN): now counts frames and error frames. Bus-off and RX overflow are told apart by the error frame's cause. listen_only is left unset here, because SocketCAN drops TX in software rather than in hardware.
  • MCP2515: not done yet. There's no hardware to test it on, so it doesn't report, and its buses don't appear in the map.
  • Docs: a new "Bus health" section in docs/can.md, plus updates to docs/ble.md and docs/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 test passes. New tests:
    • unit tests for the status rules and the saturating counters
    • integration tests for totals, the status, publish timing (rate-limited), resend, BLE link down, and the worst-case BLE size
    • a test that SocketCAN error frames are counted by cause
  • cargo fmt --all -- --check passes.
  • cargo xtask clippy and cargo xtask build pass for --board pc and for --board esp (ESP32-C3, example config), with both egmp and virtual-car.
  • Not run: the ESP32-S3 (Xtensa) build, which needs the +esp toolchain; anything on real hardware or a car.

Checklist

  • PR title uses conventional-commit style
  • cargo fmt --all -- --check, cargo xtask clippy and cargo xtask test pass
  • Docs updated
  • No config.toml, credentials, build artifacts or .bin files committed
  • The wire format changed: the contracts PR is linked and the submodule bump is its own commit

Safety

  • This change does not touch CAN TX, BLE pairing/encryption, or OTA. It only reads RX counters. The TWAI bus-off path still relocks every bus exactly as before; the only addition is that it reports the window too.

Generated by Claude Code

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
@Tichael

Tichael commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

Hardware test: WiCAN (ESP32-C3) + virtual-car

Tested together with OpenCarControls/contracts#43 and OpenCarControls/mobile-app#127.

Setup

  • Firmware: firmware#115 a756753, --board esp --platform virtual-car, on a WiCAN (ESP32-C3). No flash erase; the phone's existing bond was kept.
  • App: mobile-app#127 release APK. Tests 1–2a ran on 5a67021; test 2a was repeated on, and tests 3–5 ran on, 5a67021 plus the fix now pushed as 58edf2b.
  • Contracts: contracts#43 91554fd (both PRs pin it).
  • Phone: Pixel 9 Pro Fold (Android), connected over BLE.
  • Bus: the WiCAN on the desk with no vehicle bus attached, so bus 0 is idle (no frames, no errors).

The PRs list no manual checks, so all tests come from reading the diffs.

1. [Analysis] The CAN buses row appears: pass

Settings → CAN buses shows "Bus 0 (listen-only) · Silent", "0 frames · 0 errors · 0 overruns · 0 bus-offs". This matches the serial log.

Time (UTC) Event
18:27:41.8 Boot: 1 bond, CAN bus 0: TWAI started in listen-only mode
18:28:03.9 Phone connected (is_known=true)
18:28:04.1 Link encrypted
18:28:04.2 MTU 251
18:28:05.2 app subscribed to TX — resending latest state
every 5 s CAN bus 0 (listen-only), last 5s: 0 frames, … 0 bus-offs

2. [Analysis] Bus health shown when Settings first opens: fail as submitted, pass with 58edf2b

  • 2a, at 5a67021: on opening Settings right after connecting, the section showed "Waiting for the device to report its buses" for up to 5 s. canBusHealthProvider only started listening when CanBusHealthSection was first built, so it missed the report sent on subscribe and waited for the next 5 s repeat. Car access has no such gap, because the dashboard banner watches its provider from the start.
  • Fix (58edf2b): AppEntryRouter now listens to canBusHealthProvider, next to connectionActivityRecorderProvider.
  • 2a again, with the fix: the app re-subscribed at 18:38:44.3, and the row was already there when Settings opened. "Waiting" no longer shows.
  • 2b: after leaving Settings and coming back, the row shows at once.

3. [Analysis] Listen-only ↔ normal mode change (developer write lock): pass, with a lag of up to 5 s

The row dropped "(listen-only)" when the lock was turned off and showed it again when the lock was turned back on.

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_session returns 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
@Tichael

Tichael commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

Hardware test in the car: WiCAN (ESP32-C3) + egmp

Tested together with OpenCarControls/contracts#43 and OpenCarControls/mobile-app#127.

Setup

  • Firmware: firmware#115 a756753, --platform egmp. The flash was erased and the phone paired fresh.
  • Device: WiCAN (ESP32-C3) on the car's M-CAN through a custom harness (not the OBD-II port), at 500 kbit/s.
  • App: mobile-app#127 at 58edf2b.
  • Contracts: contracts#43 91554fd.
  • Phone: Pixel 9 Pro Fold. Read-write was not approved, so bus 0 stayed listen-only throughout, apart from the existing identity probe (about 120 ms in normal mode at boot, already on main).

A. [Analysis] Car awake → OK: pass

The 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.

Window ends (UTC) Frames in window Running total
20:06:33.2 10,828 140,582
20:06:38.2 10,827 151,409 (the app's value)
20:06:43.25 10,788 162,197

About 2,160 frames/s, with 0 RX errors and 0 overruns. There were no BLE GATT TX dropped or queue-full lines on the busy bus.

B. [Analysis] Car going to sleep → Silent: skipped

Skipped 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: fail

The car was awake: about 10,800 frames per 5 s just before and just after the test. Bus 0 still reported 0 frames, 0 RX errors in every window, and the app showed Silent ("the car is asleep, or the bus is not connected").

Cause: the ESP TWAI driver never counts bus errors (bit, stuff, form, CRC, ACK). esp-hal 1.1.2's receive_async only returns a frame, BusOff or Overrun; its interrupt handler reads the error-code capture and discards it. The error counter doesn't help either: in listen-only mode esp-hal pins REC at 128 (an errata workaround) and it stays frozen. On TWAI, NO_VALID_FRAMES, and DEGRADED caused by RX errors, can't happen. The existing serial line's "RX errors" count on main has the same blind spot.

Follow-ups, in progress on this PR set:

  • docs: say that TWAI doesn't count bus errors yet;
  • the app's No valid frames hint no longer points at configuration (firmwares are built per platform);
  • a frames-per-second value replaces the ever-growing frame total in the app;
  • a separate firmware issue to count TWAI bus errors.

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.

Tichael and others added 5 commits November 24, 2026 20:23
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>
@Tichael

Tichael commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

Frames per second: changes and in-car retest

Together with OpenCarControls/contracts#43 and OpenCarControls/mobile-app#127.

Changes after the in-car test

  • contracts#43 (68ea89d, ed013d7): CanBusHealth sends frames as a rate only.
    • frames_per_second (field 3) is the rate over the last report window, rounded down.
    • frames_received and frames_matched are removed. At about 2,160 frames/s on E-GMP M-CAN, a uint32 total saturates after roughly 23 days of the bus being awake, and the app doesn't need it.
    • rx_errors, overruns and bus_offs move to fields 4–6. CanBusHealth isn't on main yet, so the numbers are reused rather than deprecated. buf breaking against main passes.
  • firmware#115 (ce3e556, 3517d4b, d579d34, and the pin bumps):
    • Drivers pass each window's real length to report_can_bus_window, so the rate is right for windows ended early by a bus-off or, with d5b1d6b, a mode switch.
    • The TWAI serial line now prints the real length too (last 0.2s around the identity probe, last 5.0s otherwise).
    • docs/can.md documents that TWAI doesn't count RX errors yet (ESP TWAI never counts RX bus errors, so a wrong bitrate looks silent #117).
  • mobile-app#127 (0bd7a36, 0e3c027):
    • The row reads "2160 frames/s · 0 errors · 0 overruns · 0 bus-offs".
    • The No valid frames hint no longer points at configuration ("Only errors: the device can't read this bus. Check its connection."), since firmwares are built per platform.

Checks: buf format, lint and breaking; cargo xtask test; cargo fmt; cargo xtask clippy (PC and ESP, egmp and virtual-car); flutter analyze --fatal-infos; and flutter test all pass.

Retest in the car: pass

  • Setup: firmware a3257c5 (egmp, no erase, bond kept) and the mobile-app 0e3c027 release APK, on M-CAN with the car awake.
  • App: "Bus 0 (listen-only) · OK", about 2,160 frames/s, 0 errors, 0 overruns and 0 bus-offs. The rate stays steady instead of climbing.
Window ends (UTC) Frames in window Rate
20:35:11.3 10,832 2,166/s
20:35:16.3 10,805 2,161/s
20:35:21.3 10,834 2,166/s
  • Short windows: at boot, the identity probe's mode switches now end windows of about 0.2 s and 0.1 s (511 and 329 frames, firmware d579d34). With the real length these still come out at about 2,200–2,500 frames/s; a fixed 5 s would have shown about 100 frames/s.
  • BLE link: no BLE GATT TX dropped, queue-full or panic lines.

Before merging: the firmware and app pins point at the contracts PR branch (ed013d7) and need re-pinning once contracts#43 is merged.

@Tichael
Tichael merged commit c6e4286 into main Nov 24, 2026
10 checks passed
@Tichael
Tichael deleted the claude/sweet-albattani-i9lueo branch November 24, 2026 20:57
@Tichael Tichael mentioned this pull request Nov 24, 2026
4 tasks done
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.

Report CAN bus health and add the dev read-only toggle

2 participants