Skip to content

feat: reach the car through any MQTT broker over the secure channel - #140

Draft
Tichael wants to merge 3 commits into
mainfrom
feat/mqtt-relay
Draft

Tichael wants to merge 3 commits into
mainfrom
feat/mqtt-relay

Conversation

@Tichael

@Tichael Tichael commented Dec 6, 2026

Copy link
Copy Markdown
Collaborator

Requested by Michaël · project thread

Closes #131. Part of OpenCarControls/contracts#44, phase 3. Ships with OpenCarControls/firmware#138 (relay) and needs OpenCarControls/contracts#53 (pinned to its branch until it merges).

What and why

The app reaches the car through any MQTT broker, with the broker only carrying secure channel frames, so it doesn't need to be trusted and no certificates are required.

  • MqttCarTransport rewritten on the secure channel: subscribes to opencar/{device_key_id}/c/{client_key_id}, publishes to .../d/{client_key_id} (QoS 1, no retain), client ID {client_key_id}-{8 hex} new per connection. Heartbeat (AppToDevice.heartbeat) through the session every 15 s in the foreground (stops 30 s after backgrounding). New handshake after a broker reconnect, and when nothing arrives for 90 s. Reconnects with a growing delay. The broker client is behind MqttRelayClient, so tests use a fake broker in front of the existing fake secure device.
  • Remote tier like the LAN: basic state and basic commands; the device refuses advanced commands with REJECTED_UNAVAILABLE (battery preconditioning stays Bluetooth-only, as decided), the app refuses system commands.
  • Broker settings per vehicle in Settings > MQTT broker: address (mqtt:// or mqtts://, optional port), optional username/password, optional CA and client certificate/key (mTLS). Stored in secure storage (MqttBrokerStore), deleted on unpair. With mqtts:// and no CA, the phone's usual authorities are trusted (hosted brokers). The screen shows the device and phone key IDs for the broker's ACL. config.toml's broker is now only a debug default.
  • Errors the user can act on: a dashboard banner "Can't reach the car remotely" with the reason (broker unreachable, credentials refused, TLS/certificate failure, certificates unreadable, device not answering) and a Broker settings button. A device that no longer knows the phone stays silent, so "not answering" also says to reconnect over Bluetooth.
  • Removed: plaintext topics, SystemState.mqtt_client_id handling, the unused broker fields in PairedVehicleConfig, and the per-vehicle MQTT topic constants. New MqttRelayConstantsBuilder generates mqtt_relay_constants.g.dart from [mqtt].
  • SecretStore gets delete.

Docs: docs/mqtt-tls.md rewritten as docs/mqtt-relay.md; architecture, code generation, UI (banner, settings screen), README and config.toml.example updated.

Not done here: the phone's trust level over remote links (#92 follow-up). The device doesn't send it over the LAN or the relay yet, so the access providers still ignore remote links.

How it was tested

  • New test/mqtt_relay_test.dart: topics and client IDs, URL parsing, settings validation, secure storage round trip, and the transport against a fake broker and fake device (handshake via heartbeat, commands, system commands refused, broker refusal, bad certificates stop retrying, device silent, re-handshake after silence, reconnect with a new client ID, dispose).
  • Existing tests updated for the removed MQTT client ID.
  • Not run here: Flutter isn't available in this environment, so formatting, analyze and tests are left to CI, and nothing ran on a phone. To test by hand: set a broker in Settings (e.g. mqtt://<pc-ip> with a local Mosquitto), turn Bluetooth and Wi-Fi off, and check state and basic commands; then mqtts:// with your CA.

Checklist

  • PR title uses conventional-commit style (feat:, fix:, docs:, chore:); CI validates it
  • dart format, flutter analyze --fatal-infos and flutter test pass (CI)
  • Docs (docs/ or README) updated if behaviour changed
  • No config.toml or credentials committed; generated files not hand-edited
  • If 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 does (explain below)

Remote commands now reach the vehicle over MQTT, but only through the authenticated secure channel: the device decides (validated phone, CAN approval, basic commands only). BLE pairing and bonding are unchanged.


Generated by Claude Code

The MQTT transport now carries secure channel frames only (contracts
docs/mqtt-relay.md): per-client topics named by the device and client
key IDs, a new client ID per connection, heartbeats through the
session, and a new handshake after a broker reconnect or when the
device goes quiet. The plaintext topics and mqtt_client_id are gone.

The broker is set per vehicle in Settings > MQTT broker (address,
optional username and password, optional CA and client certificate),
kept in secure storage. config.toml's broker is only a debug default.
Relay problems show as a dashboard banner the user can act on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XkZEiWoJmJsqnYdysAR9wp
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.

MQTT relay on the secure channel, with a user-set broker

2 participants