Skip to content

feat(lan): serve LAN direct on ESP WiFi builds - #136

Merged
Tichael merged 10 commits into
mainfrom
feat/lan-esp
Dec 6, 2026
Merged

Tichael merged 10 commits into
mainfrom
feat/lan-esp

Conversation

@Tichael

@Tichael Tichael commented Dec 6, 2026 •

Copy link
Copy Markdown
Collaborator

Requested by Michaël · project thread

What and why

LAN direct on ESP WiFi builds. Closes #121, part of OpenCarControls/contracts#44. Stacked on #135 (the core LAN part, PC board): it targets main so that CI runs (CI only runs on PRs to main), so the diff also shows #135's commits until that merges. The last commit is the ESP work.

  • boards/esp/src/lan.rs (lan feature): one task per connection plus a spare, so lan_accept can replace a connection without a session or refuse a new one. Each connection runs its reader, writer and a once-a-second lan_should_close check side by side. TCP keepalive 20 s, timeout 60 s.
  • UDP discovery responder on 4735, through lan_discovery_reply.
  • An address task that reports SystemState.lan_address when DHCP changes the address, and runs the mDNS responder: it joins 224.0.0.251, announces twice on each new address, and answers queries.
  • core-interface/src/lan_mdns.rs: a minimal mDNS responder (PTR, SRV, TXT, A for _opencar._tcp). It handles legacy unicast queries, which is what the app sends: answered by unicast with the ID and question echoed, a 10 s TTL and no cache-flush bit. Host-tested.
  • xtask enables lan and spawns start_lan on WiFi builds. An LTE build leaves LAN out with a warning. The stack gets LAN_MAX_CLIENTS + 3 more sockets.
  • Measurements: logs the free heap when a connection opens and closes, and each handshake's time (LAN: handshake answered in … ms).
  • CI now lints and builds the example config (WiFi, ESP32-C3) with LAN on, so the lan feature is compiled.
  • Docs: docs/lan.md (ESP board, the mDNS responder), docs/architecture.md, config.toml.example.

How it was tested

  • New host tests in core-interface/tests/lan.rs: the mDNS answer to a legacy unicast and a multicast query, record by record, the announcement, and silence for other names, responses, truncated or looping names, and no identity. xtask tests cover lan_on_uplink. cargo test -p xtask and cargo fmt --check pass locally.
  • I checked that xtask generates the lan feature and the start_lan spawn for a WiFi config with LAN on.
  • Not run here: core-interface tests and the ESP build. There's no protoc or ESP toolchain in my sandbox, so CI is the first compile.
  • Needs hardware: on the WiCAN (C3), discovery by mDNS and by the probe, a connection, the state, a command, reconnecting after a restart and after a DHCP change, the heap with one and two clients (with and without MQTT TLS), and the flash size against a build without lan.

Checklist

  • PR title uses conventional-commit style (feat(scope):, fix(scope):, docs:, chore:)
  • cargo fmt --all -- --check, cargo xtask clippy and cargo xtask test pass (fmt passes; clippy and tests left to CI)
  • Docs (docs/ or README) updated if behaviour changed
  • No config.toml, credentials, build artifacts or .bin files committed
  • If the wire format changed: the contracts PR is linked and the submodule bump is its own commit (no wire change beyond contracts#52)

Safety

  • This change does not touch CAN TX, BLE pairing/encryption, or OTA

The board only moves bytes. Authorization and the secure channel are in core-interface, unchanged here.

https://claude.ai/code/session_01XkZEiUcxbhmHS339r4nWo6


Generated by Claude Code

…covery

core-interface gets lan.rs: connection slots, frames in and out per
connection (command responses, a system state with the client's command
availability and the LAN address, the full vehicle state), the UDP
discovery reply and the mDNS TXT records. Sessions now know whether their
link is LAN or relay, and handshake inits are rate-limited before any
X25519 work. SystemState.lan_address goes out over BLE too.

The PC board serves it with [network.lan] (non-blocking sockets polled
from one task, mDNS through mdns-sd), and tools/lan_client.py plays the
app's part for an end-to-end test.

Closes #120

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XkZEiUcxbhmHS339r4nWo6
Enrolling a client key needs a validated phone, so the LAN tests
validate both phones, enroll them, then end the validations by
approving another car, like the secure channel tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XkZEiUcxbhmHS339r4nWo6
With [network.lan] enabled on a WiFi build, xtask enables board-esp's
`lan` feature and spawns start_lan on the WiFi stack:
- a task per connection plus a spare, each with its own TcpSocket, so
  lan_accept can replace a connection without a session;
- the UDP discovery responder;
- an address task that reports SystemState.lan_address on DHCP changes
  and runs a minimal mDNS responder (core-interface lan_mdns.rs: PTR,
  SRV, TXT and A, legacy unicast for the app's one-shot query).

Logs the free heap on each connection and the handshake time, for the
C3 measurements. LTE builds leave LAN out with a warning. CI lints and
builds the example config with LAN on.

Closes #121

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XkZEiUcxbhmHS339r4nWo6
@Tichael Tichael self-assigned this Dec 6, 2026
@Tichael
Tichael changed the base branch from feat/lan-transport to main December 6, 2026 00:20
@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 (included in this build) and mobile-app#138.

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. The change belongs in firmware#135's core-interface (see there). Nothing ESP-specific should be needed, but please re-run the ESP checks after it.
  2. Question about the flash cost: LAN adds about 126 KB of .text (test 8), and the secure channel was already in the image. Do you know what that is (embassy-net UDP/multicast features, the per-connection tasks, the mDNS responder)? It would be worth trimming if it's mostly something we don't need.
  3. Nit: once, a connection replaced by a newer one was logged as disconnected (no session) instead of (slot taken back) (01:20:38.6, test 4, through the VPN). The second run logged slot taken back both times, so it looks like a race between the once-a-second lan_should_close check and the replacement. Only the log reason is affected.

Results

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.

8. [PR] Flash and RAM cost on the C3: measured

Same config, [network.lan] enabled off and on (virtual-car, WiFi, MQTT, CAN logger):

LAN off LAN on Difference
Flash (loaded sections) 1,351,326 B 1,482,426 B +131,100 B (+9.7%)
.text 1,117,606 1,243,842 +126,236
.rodata 122,060 126,844 +4,784
Static RAM (.bss + .data) 183,932 214,060 +30,128
Left for the stack 74,648 44,520 −30,128

At runtime the free heap stays around 66 KB with one or two LAN connections open, and a handshake takes 183–205 ms (11 handshakes). The heap with MQTT over TLS wasn't measured.

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
- A secure channel session gets the basic vehicle state only, and its
  client's advanced commands are reported unavailable and refused with
  REJECTED_UNAVAILABLE: reaching the device over a network is no proof of
  being at the car.
- The system state repeats every LAN_SYSTEM_STATE_REPEAT_MS (from the
  contract), which the app's receive timeout relies on.
- lan_mdns_instance_name: the BLE name and the key ID's first four
  characters, so the PC board announces the same name as the ESP.
- lan_is_open, for the boards' logs.

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

Merged #135 in (c5fa22f). CI is green, including both ESP builds and the "ESP, LAN direct" clippy and build.

  1. ESP checks re-run after the core change: green.

  2. The ~126 KB of .text. I can't measure it here (no ESP toolchain in my sandbox), but my best reading of what got linked in:

    • Most of it is likely the secure channel itself, not the LAN code. Before this PR no ESP transport called process_secure_frame, so the Noise IK handshake, ChaCha20-Poly1305, BLAKE2s/HKDF, X25519 and the session/dispatch code were dead-stripped. LAN is the first caller. That part is needed (and the relay will reuse it).
    • Then smoltcp's UDP and IGMP/multicast paths, the 3 per-connection TCP tasks plus the discovery and address tasks, and the hand-written mDNS responder (small, a few KB).
    • Some core::fmt from the {peer:?}/{e:?} Debug logs.

    To see the real split, could you run on your machine: cargo bloat --release --crates -n 30 (or nm --size-sort -S on the ELF) for a WiFi build with and without [network.lan] enabled? If crypto dominates, there's not much to trim. If fmt or embassy-net is big, the cheap wins are Display instead of Debug in the LAN logs and dropping UDP discovery on the ESP (mDNS alone finds it). I'll do whichever the numbers point to.

  3. "no session" vs "slot taken back". Fixed: when the once-a-second check finds the slot was given to a newer connection, the log now says "slot taken back".

The mDNS responder now uses the shared lan_mdns_instance_name() from #135, so the ESP and PC names match.


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 c5fa22f, together with contracts#52 b101725, firmware#135 a7bb7fe and mobile-app#138 555a5d6.

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.

R7. mDNS instance name: pass

ESP: OpenCar-cead._opencar._tcp.local (legacy-unicast answer, TXT id, v=1 only, A 10.20.0.212). PC: announced over mDNS as 'OpenCar-8509'. The probe reply is unchanged (17 bytes, port 4734).

R8. Log reason for a replaced connection: pass

Three raw TCP connections from the container while the phone held slot 0: both replacements logged as slot taken back, the last connection closed 10.0 s after connecting (no session), and the phone's session untouched.

R9. Where the ~128 KB of flash goes

Image with LAN on: 1,483,480 B (about 1 KB more than the first round). By symbol, LAN off → on, with llvm-nm -S -C (approximate, since inlined code is counted in its caller):

Part Approx. Of what
Secure channel, now linked in (LAN is its first caller on the ESP) ~70 KB protobuf ~42 KB (core_interface::proto +23 KB, prost +19 KB), noise + secure_channel ~18 KB, crypto ~10 KB (chacha20poly1305 + chacha20 + poly1305 +6 KB, curve25519_dalek +4 KB)
LAN code itself ~25 KB board_esp::lan 14 KB, core_interface::lan 6 KB, lan_mdns 4 KB
Network stack and async runtime ~38 KB smoltcp +12 KB, embassy-net +5 KB, embassy executor/sync/futures +21 KB (task storage, select)
core::fmt / Debug +2.8 KB

Your reading was right: most of it is the secure channel, which the relay will reuse. Display instead of Debug would save under 3 KB, and dropping UDP discovery only a few KB, so no trimming is needed for now.

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
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XkZEiUcxbhmHS339r4nWo6
@Tichael
Tichael merged commit 1263368 into main Dec 6, 2026
9 checks passed
@Tichael
Tichael deleted the feat/lan-esp 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 on ESP WiFi builds (TCP, mDNS, UDP discovery)

2 participants