Skip to content

feat: connect over the LAN with the secure channel - #138

Merged
Tichael merged 9 commits into
mainfrom
feat/lan-transport
Dec 6, 2026
Merged

Tichael merged 9 commits into
mainfrom
feat/lan-transport

Conversation

@Tichael

@Tichael Tichael commented Dec 5, 2026

Copy link
Copy Markdown
Collaborator

Requested by Michaël · project thread

What and why

Phase 2 of contracts#44: LAN direct in the app. Closes #130. Contract: OpenCarControls/contracts#52; firmware side: OpenCarControls/firmware#135.

When Bluetooth isn't ready and the phone is on a local network, the app looks for the device and connects over TCP, running the secure channel from phase 1 on the socket. Transport selection is now BLE → LAN → MQTT.

  • Transport (lib/transport/lan_transport.dart): LanCarTransport connects, handshakes with the pinned device identity, carries basic and advanced commands, sends keepalives when idle, and refuses system commands (they stay Bluetooth-only).
  • Discovery (lib/transport/lan_discovery.dart): the address entered in settings, then the contract order: mDNS (a legacy-unicast query, so no multicast lock and no new package), the UDP probe naming the key ID, the last known address. An address is only a hint; the handshake proves the device.
  • Link lifecycle (lib/providers/lan_link_provider.dart): searches while Bluetooth isn't ready, the identity is pinned and the client key enrolled; retries with a growing delay; lets go of the link when Bluetooth is ready; searches again when it drops. It owns the link's disposal.
  • UI: the pill says Wi-Fi, the connection log records Wi-Fi connected / lost, the dashboard uses the Bluetooth tier, and the Bluetooth-only settings sections stay hidden (TransportType.carriesSystemCommands). Settings gets a Wi-Fi address row for networks that block discovery.
  • SystemState.lan_address (from Bluetooth or the LAN) is kept as the last known address.
  • lan_constants.g.dart is generated from transport.toml [lan].

How it was tested

  • New tests: test/lan_transport_test.dart (address parsing, the probe and the contract's reply vector, the mDNS parser, discovery order, LanCarTransport over a loopback socket against the fake secure device) and test/lan_link_test.dart (search order, pinned key, BLE taking over, retry after a drop, no search without an enrolled key or a LAN). settings_test.dart covers the Wi-Fi tier and the address row.
  • Not run here: the sandbox has no Flutter SDK, so dart format, flutter analyze and flutter test were not run; CI is the first run. Formatting was matched by hand.
  • Needs Michaël's hardware test with the PC firmware build from firmware#135 ([network.lan] enabled = true) and the phone on the same Wi-Fi.

Checklist

  • PR title uses conventional-commit style (feat:, fix:, docs:, chore:); CI validates it
  • dart format, flutter analyze --fatal-infos and flutter test pass (left to CI, see above)
  • 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)

Vehicle commands can now go over the LAN, only through the secure channel with the device key pinned over Bluetooth, and only for a phone whose client key the device enrolled. The device still decides availability and authorization (firmware#135 holds every command back for a phone that isn't validated). System commands are refused before sending on the LAN. Pairing and bonding are untouched. The contracts submodule is pinned to contracts#52's branch; it moves to contracts main once that merges.


Generated by Claude Code

Adds LAN direct (contracts docs/lan.md) as a transport between Bluetooth
and MQTT. When Bluetooth isn't ready and the phone is on a local network,
the app looks for the device (the address entered in settings, mDNS, the
UDP probe, then the last known address) and connects over TCP with the
secure channel, pinned to the device identity enrolled over Bluetooth.

- LanCarTransport: a socket with the stream framing and a SecureChannel;
  keepalives when idle; refuses system commands.
- lan_discovery: the UDP probe and a legacy-unicast mDNS query, so no
  multicast lock or new package is needed.
- lanLinkProvider owns the link: searches with a growing delay, drops the
  link when Bluetooth is ready, searches again when it drops.
- The pill, settings, connection log and dashboard tier know the Wi-Fi
  link; system commands stay Bluetooth-only (carriesSystemCommands).
- Settings: a Wi-Fi address row for networks that block discovery.
- The device's SystemState.lan_address is kept as the last known address.
- lan_constants.g.dart is generated from transport.toml's [lan].

Closes #130

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XkZEiUcxbhmHS339r4nWo6
@Tichael Tichael self-assigned this Dec 5, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XkZEiUcxbhmHS339r4nWo6
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XkZEiUcxbhmHS339r4nWo6
@Tichael
Tichael marked this pull request as ready for review December 6, 2026 01:06
@Tichael

Tichael commented Dec 6, 2026

Copy link
Copy Markdown
Collaborator Author

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

Tested together with contracts#52, firmware#135 and firmware#136.

Requested changes

  1. Wi-Fi (LAN direct) is the remote tier, like MQTT. Being on the same network as the car is no proof of being at the car, so only Bluetooth gets the proximity (advanced) tier. Over the LAN the device sends the basic state only, and accepts basic vehicle commands only (advanced and system commands are refused), exactly like the relay. Today Wi-Fi shows the full dashboard: TransportType.lan is with ble in dashboard_screen.dart:60-62, and command_availability_provider.dart:223 only holds back advanced commands on mqtt. LAN should behave like MQTT in both.
  2. The pill says "Remote" for any remote transport: Wi-Fi (LAN direct), MQTT over WiFi or LTE, and the relay later. Which transport it actually is goes in the connection log panel only. Bluetooth keeps its own label.
  3. A manual Wi-Fi address is used alone. When the user has entered an address, the app connects to that address only, with no fallback to mDNS, the probe or the last known address. A wrong address stays wrong (no LAN connection). Today a wrong address fails, then discovery finds the board 2.65 s later (test 7).
  4. Notice a dead link sooner. LanCarTransport has no receive timeout: it only finds out the link is dead when its idle keepalive (60 s) gets a reset. After a board reset on the direct path, the app reconnected 43 s later and could take about 60 s plus the retry delay (test 5). The device sends a system state every 5 s, so treating about 15 s of silence as a dropped link would fix it (see the suggestion in contracts#52 for a shared constant).
  5. Suggestions:
    • Search again right away when the app resumes, instead of waiting out a backoff that grew while it was in the background: 9 s of "Offline" after unlocking the phone (test 10).
    • On a cold start the app connects and handshakes over the LAN in the ~1.5 s before Bluetooth is ready, then drops it (seen at both app starts in this test). It's harmless, but costs the device a handshake each time; consider a short delay before the first LAN search when Bluetooth is turning on.

Results

Tests 8 (flash cost) and 9 (PC plan) are in the firmware PR comments.

Setup (2026-12-06)

  • WiCAN (ESP32-C3) on firmware#136 da1e97e (includes firmware#135 3b7bc4a), platform virtual-car, [network.lan] enabled = true, mdns = true, MQTT on (plain). Not erased: the bond, the validated phone, the enrolled client key and the device identity from the phase 1 test were kept.
  • App: mobile-app#138 4652eeb, release APK installed over the previous one (pairing kept), Pixel 9 Pro Fold (Android), on the same WiFi as the WiCAN.
  • Contracts: contracts#52 ac2fcad (pinned by all three PRs).
  • The phone runs Tailscale with a subnet route for the home LAN. Every phone test was run twice: through the VPN (the app reached the board via a subnet router), and direct (the app excluded from Tailscale, connecting from the phone's own WiFi address). The container's own probes went through the laptop's Tailscale route.
  • PC firmware test (firmware#135's plan): PC board, virtual car, debug build, LAN on, in the dev container, with tools/lan_client.py.

1. [Analysis] Boot, and Bluetooth with the new app: pass

The boot log shows LAN: TCP port 4734, up to 2 clients, UDP discovery on 4735, mDNS on, then LAN: address 10.20.0.212:4734 about 5 s later (after WiFi). The bond and identity were kept. The app connects over Bluetooth as before.

2. [PR] Bluetooth off → Wi-Fi, commands, connection log, settings: pass, but see the tier change below

Through the VPN Direct
BLE drops (Bluetooth off) 01:16:25.353 01:29:39.572
LAN connected (slot 0) +0.30 s +0.07 s
handshake answered in 191 ms 190 ms

Lock and unlock work over Wi-Fi. The connection log shows Wi-Fi connected, and Settings hides the Bluetooth-only sections and shows the Wi-Fi address row. The availability over LAN follows the dev lock (dimmed while locked), and the dev lock can only be unlocked over Bluetooth, as expected for a system command.

3. [PR] Discovery, from the container: pass

  • UDP probe with the device's key ID, and with all zero: a 17-byte OCDR reply, version 1, key ID ceade7dc…, port 4734. Another key ID, a 5- or 16-byte probe, or version 2: no reply.
  • Rate limit: a burst of 20 probes gets 4 replies.
  • mDNS, a legacy-unicast PTR query to the board: answered from 5353 to the sender, ID and question echoed, TTL 10, no cache-flush bit, with PTR OpenCar-cead._opencar._tcp.local, SRV port 4734 → opencar-ceade7dcc2033c4cbc38.local, TXT id=ceade7dcc2033c4cbc38, v=1 only, and A 10.20.0.212. Another service: no answer.

4. [Analysis] Strangers on the LAN can't push the phone off: pass (twice)

With the phone's session in slot 0, three raw TCP connections were opened one second apart and never handshaked. Each took slot 1 from the previous one, and the last was closed 10.0 s after connecting. The phone's session never dropped. The free heap was 66380 B with the phone alone and 65892 B with the phone and one stranger.

5. [PR] Board reset while on Wi-Fi: pass, but slow on the direct path

Through the VPN Direct
reset 01:21:15.8 01:31:59.8
LAN: address (WiFi up) +6.3 s +6.4 s
phone reconnects, fresh handshake +13.4 s (187 ms) +42.9 s (187 ms)

Through the VPN, the subnet router reset the dead connection quickly. On the direct path nothing tells the app the board is gone, so it waits for its idle keepalive (see mobile-app#138).

6. [Analysis] Bluetooth back on → LAN released: pass (twice)

Direct: BLE connected at 01:36:45.9, the app subscribed at 01:36:47.46, and the LAN connection was closed by the peer at 01:36:47.50. The free heap after each LAN close over 6 cycles was 66208–66768 B, with no steady fall.

7. [PR] Manual Wi-Fi address: pass as built, but the requested behaviour changes (see mobile-app#138)

With 10.20.0.212:4734, the app connected at once. Changed to 10.20.0.99:4734, the app dropped the link and reconnected 2.65 s later, after the wrong address failed and discovery found the board.

10. [Analysis] Screen lock and unlock on Wi-Fi: pass

Screen off at 01:33:53, and Android cut the app's network about 4 s after it stopped (board: disconnected (read error) at 01:33:58). App resumed at 01:35:56.4, reconnected at 01:36:05.4: 9 s showing "Offline", waiting out the retry backoff.

Not tested: an address change from DHCP or a WiFi reconnection, the heap with MQTT over TLS, two real phones on the LAN at once (the second session was a PC client), iOS, LTE builds (LAN is left out by design), E-GMP in the car, and the 0x310 frame on candump (no vcan0 in the container).

…racts#52)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XkZEiUcxbhmHS339r4nWo6
Per the hardware-test review: the LAN carries basic state and commands
only, like MQTT, and the pill says Remote for any remote transport (only
the connection log names it). A manual Wi-Fi address is used alone. The
link closes after receive_timeout_ms of silence, the app searches again
right away on resume, and the first search waits briefly while Bluetooth
is connecting.

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

Tichael commented Dec 6, 2026

Copy link
Copy Markdown
Collaborator Author

All in d3a9dbd (plus format fixes); tests, analyze and format are green.

  1. Remote tier. dashboardTierFor(lan) is remote, and advanced commands are held back on any remote link (TransportType.isRemote, used in command_availability_provider and the settings hint).
  2. Pill. LinkKind is now just Bluetooth or remote: the pill (and the vehicle list and settings summary) say "Remote" for the LAN and MQTT. Only the connection log names the transport: its panel says "Connected over Wi-Fi", and the entries stay "Wi-Fi connected" / "Wi-Fi link lost".
  3. Manual address alone. With one set, discovery tries only it; a wrong one stays wrong (new test). The settings dialog says "Only this address is used. Leave empty to search."
  4. Receive timeout. LanCarTransport closes the link after kLanReceiveTimeoutMs (15 s, from the contract) with no frame from the device, then the provider searches again (new test).
  5. Suggestions, both done. The app searches right away when it comes back to the foreground with no link (cancelling the pending retry), and while Bluetooth is connecting the first search waits 3 s, so a car in Bluetooth range isn't reached over the LAN first.

Contracts are pinned to contracts#52's b101725 in its own commit. Docs updated (architecture, ui, ui-screens).


Generated by Claude Code

@Tichael

Tichael commented Dec 6, 2026

Copy link
Copy Markdown
Collaborator Author

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

Retest after 555a5d6, together with contracts#52 b101725, firmware#135 a7bb7fe and firmware#136 c5fa22f.

All the requested changes check out. Nothing is left to change from the hardware side.

Setup (2026-12-06, same bench as the first test)

  • WiCAN (ESP32-C3) on firmware#136 c5fa22f (includes firmware#135 a7bb7fe), virtual-car, [network.lan] enabled = true, mdns = true. Not erased (bond, validation, enrolled key and identity kept).
  • App: mobile-app#138 555a5d6, release APK installed over the previous one; Pixel 9 Pro Fold, the app excluded from Tailscale, so it connects directly over WiFi (10.20.0.193).
  • Contracts: contracts#52 b101725 (pinned by the firmware and app PRs).
  • PC checks: PC board, virtual car, debug build, LAN on, in the dev container, with tools/lan_client.py and a small driver for the advanced command.

R1. Wi-Fi is the remote tier, in the app: pass

Bluetooth off at 04:38:29.19 → LAN connected at 29.39, handshake 190 ms. The pill says Remote, and the dashboard is the remote one (car picture, Doors, Driving, Odometer; no Gear, Speed or Custom 1). Doors lock and unlock work. The connection log says it's Wi-Fi, and Settings still hides the Bluetooth-only sections. Bluetooth back on: BLE connected at 04:56:05.30, and the LAN connection was closed by the app at 06.82.

R3. A manual address is used alone: pass

UTC Event
04:48:34.65 right address 10.20.0.212:4734 saved, the old link closed
04:48:34.80 connected to it 156 ms later, handshake 192 ms
04:49:02.98 wrong address 10.20.0.99:4734 saved, link closed
— 143 s with no LAN connection, through every retry step
04:51:26.63 address cleared, discovery connects at once

R4. Receive timeout, after a board reset: pass

UTC Event
04:40:40.0 reset
04:40:46.3 LAN: address 10.20.0.212:4734
04:40:57.3 the phone reconnects, handshake 190 ms

17.2 s from reset to reconnect, against 42.9 s before the change. That fits the 15 s receive timeout followed by an immediate search.

R5. Search right away on resume: pass

Screen off at 04:42:53.8. Android cut the app's network (board: read error at 04:42:59.4). Screen on at 04:44:57.86, LAN connected at 57.91, the activity resumed at 58.03, handshake done at 58.09. Before the change: 9 s of "Offline".

R6. Cold start with Bluetooth on: pass

App started at 04:37:07. BLE connected at 04:37:08.8 and subscribed at 09.8, with no LAN connection. Before the change the app did a LAN handshake at every start.

R2 (device side of the remote tier), R7–R8 and R9 are in the firmware PR comments.

Not retested (code unchanged): the probe byte format and its rate limit, the 10 s handshake timeout. Still not tested: an address change from DHCP or a WiFi reconnection, the heap with MQTT over TLS, two real phones, iOS, LTE builds, E-GMP in the car, candump (no vcan0).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XkZEiUcxbhmHS339r4nWo6
@Tichael
Tichael merged commit 7b2bddf into main Dec 6, 2026
2 checks passed
@Tichael
Tichael deleted the feat/lan-transport branch December 6, 2026 05:29
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.

LAN transport and discovery

2 participants