Skip to content

feat: show each CAN bus's health in the settings - #127

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

Tichael merged 7 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, part of OpenCarControls/firmware#12. Closes #30. The wire format is in OpenCarControls/contracts#43; the firmware side is OpenCarControls/firmware#115.

  • canBusHealthProvider: holds SystemState.can_bus_health from the current connection only, like canAccessStateProvider:
    • null until the device reports it, never cached, and null over MQTT
    • ignores a device that reports another platform
    • replaces the entry for each bus that is reported and keeps the others
  • "CAN buses" settings section (Bluetooth only), one row per bus:
    • the status in words (OK, Silent, Errors, No valid frames, Bus-off)
    • what that status means, e.g. "usually a wrong bitrate, a CAN FD bus or a wiring fault"
    • whether the bus is listen-only
    • its frames, errors, overruns and bus-offs since the device started
  • Docs: docs/architecture.md and docs/ui-screens.md updated. The HTML mockup in docs/ui/mockups/ is not updated.

The dev read-only toggle that #30 also mentions is already there: it's the developer write lock switch in CanAccessSection.

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

  • New tests in test/can_bus_health_test.dart: the provider (live only, per-bus merge, other platform, MQTT), the wording, and a widget test of the section.
  • CI passes: code generation, dart format, flutter analyze --fatal-infos, flutter test and the Android APK build. Flutter isn't installed where I worked, so these were only run in CI.
  • Not run: on a device or against a real controller; no screenshots.

Checklist

  • PR title uses conventional-commit style
  • dart format, flutter analyze --fatal-infos and flutter test pass (in CI)
  • Docs updated
  • No config.toml or credentials committed; generated files not hand-edited
  • The wire format changed: the contracts PR is linked and the submodule bump is its own commit

Safety

  • This change does not touch BLE pairing/bonding or how commands are sent to the vehicle. It only displays state.

claude and others added 4 commits November 24, 2026 16:03
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XkZEiX4By1AMegtzFhVwNo
A new "CAN buses" settings section, Bluetooth only, lists every bus the
device reports in SystemState.can_bus_health: its status (OK, Silent,
Errors, No valid frames, Bus-off) with what it means, whether it is
listen-only, and its counters since the device started.
canBusHealthProvider follows the current connection only, like the CAN
access state, and replaces each reported bus's entry.

Closes #30.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XkZEiX4By1AMegtzFhVwNo
The provider only started listening when the settings first showed the
CAN buses section, so it missed the report sent when the app subscribes
and showed "Waiting for the device" for up to 5 s. Seen on the WiCAN.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@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/firmware#115. Test 2 found a gap in this PR; the fix is pushed here as 58edf2b.

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/contracts#43 and OpenCarControls/firmware#115.

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:31
A frame total only grows, and would saturate after a few weeks of a
busy bus being awake, so the row shows the device's rate over the last
few seconds instead. The No valid frames hint no longer points at
configuration: firmwares are built per platform.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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/firmware#115.

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):
  • 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 077d2eb into main Nov 24, 2026
4 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.

Show CAN bus health

2 participants