Skip to content

feat(system): report the health of each CAN bus - #43

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

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

Conversation

@Tichael

@Tichael Tichael commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

What and why

Adds SystemState.can_bus_health (field 9, the next free number), the last open item of OpenCarControls/firmware#12. Closes #21.

  • map<uint32, CanBusHealth> keyed by bus id. I used a map rather than a repeated field so the app's mergeFromMessage replaces each bus's entry instead of appending a new copy on every update. CommandAvailability uses a map for the same reason.
  • CanBusHealth:
    • a CanBusStatus the firmware works out from the last report window: OK, SILENT, DEGRADED, NO_VALID_FRAMES or BUS_OFF
    • listen_only
    • totals since boot: frames received and matched, RX errors, overruns, bus-offs. Totals mean a lost update doesn't lose any counts.
  • BLE only. It is sent in its own state update, like can_access.

Matching PRs: the firmware PR on OpenCarControls/firmware and the mobile-app PR on OpenCarControls/mobile-app, both from branch claude/sweet-albattani-i9lueo.

Checklist

  • PR title uses conventional-commit style
  • buf format, buf lint and buf breaking pass: buf isn't installed where I worked, so I couldn't run them; CI runs them. The schema does compile with protoc through the firmware's prost-build.
  • Append-only rules followed: new fields appended; every message field is optional (a map can't be)
  • BLE message size limit still respected: a firmware test checks that the worst case (every bus with u32::MAX counters) fits in ble.payload.max_message_bytes
  • Matching firmware and mobile-app changes exist

Source of the data

The firmware's own TWAI and SocketCAN RX counters, not vehicle signals.


Generated by Claude Code

Add SystemState.can_bus_health (field 9): per bus id, a firmware-judged
CanBusStatus over the last report window plus RX counters since boot
(frames received and matched, RX errors, overruns, bus-offs) and whether
the controller is listen-only. BLE only.

A map keyed by bus id rather than a repeated field, so the app's
mergeFromMessage replaces each bus's entry instead of appending.

Part of OpenCarControls/firmware#12.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XkZEiX4By1AMegtzFhVwNo
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

The latest Buf updates on your PR. Results from workflow Proto Check / check (pull_request).

BuildFormatLintBreakingUpdated (UTC)
✅ passed✅ passed✅ passed✅ passedNov 24, 2026, 8:31 PM

@Tichael

Tichael commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

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

Tested together with OpenCarControls/firmware#115 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.

@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/firmware#115 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 2 commits November 24, 2026 20:23
The frame total only grows, so after a while in a car it no longer
reads at a glance. The rate over the last report window does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On a busy bus (about 2,000 frames/s on E-GMP M-CAN) a uint32 frame
total saturates after a few weeks of the bus being awake, and the app
doesn't need it. Drops frames_received and frames_matched and moves
frames_per_second into field 3. CanBusHealth isn't released yet, so
the numbers are reused rather than deprecated.

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/firmware#115 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 (#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 1faf470 into main Nov 24, 2026
2 checks passed
@Tichael
Tichael deleted the claude/sweet-albattani-i9lueo branch November 24, 2026 20:44
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 0.15.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add CAN bus health and a dev read-only toggle

2 participants