Skip to content

feat(transport): add the LAN transport constants, discovery and the device's LAN address - #52

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

Tichael merged 2 commits into
mainfrom
feat/lan-transport-contract

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 the epic #44 (LAN direct), contracts side. Closes #47.

  • [lan] in transport.toml: TCP port 4734, 2 clients, connect timeout, app keepalive every 60 s, and the discovery order (mDNS, UDP probe, last known address).
  • [lan.mdns]: _opencar._tcp, TXT keys id (device key ID) and v (version), browse timeout.
  • [lan.udp_discovery]: port 4735, a fixed-size 15-byte probe (OCDQ, version, key ID or all zero) and a 17-byte reply (OCDR, version, key ID, TCP port), rate-limited to 4 replies per second. Specified as bytes in docs/lan.md, with a test vector.
  • SystemState.lan_address (field 11, ip:port), sent over BLE and through the channel, only a hint.
  • [secure_channel] max_handshake_inits_per_second = 8: a cap on handshake inits across all transports, since each costs X25519 work on the C3 and anyone on the LAN (or the relay's broker, later) can send them. Documented in secure-channel.md.
  • docs/lan.md, linked from the README and secure-channel.md.

Choices the issue left open (please check)

  • Ports 4734 (TCP) and 4735 (UDP): unassigned at IANA (4734–4736 are free), registered range, so not ephemeral ports.
  • No pid TXT key: the issue listed it, but the app matches on id only, and announcing the car platform tells everyone on the WiFi what car this is.
  • The probe is always 15 bytes (all-zero key ID = any device), so it's almost as long as the reply and the responder can't amplify traffic.
  • Connection slots: when both are taken, a new connection replaces one with no session yet, otherwise it's refused, so someone on the LAN can't kick the phone off. Connections without a handshake after 10 s, or with no live session, are closed.
  • Session start = subscribe: when a LAN session starts, the device sends what it sends a BLE client that subscribes.

Checklist

  • PR title uses conventional-commit style
  • buf format, buf lint and buf breaking --against '.git#branch=main' pass
  • Append-only rules followed: lan_address appended as field 11 and optional
  • BLE message size limit still respected (an ip:port string adds at most about 24 bytes to a system state update)
  • Matching firmware and mobile-app changes exist or are linked: firmware#120/#121 and mobile-app#130 follow

Source of the data

Not vehicle data. Port availability from the IANA registry.


Generated by Claude Code

…evice's LAN address

Adds [lan] to transport.toml (TCP port, client limit, mDNS service and
TXT keys, the UDP probe and reply), SystemState.lan_address, a handshake
rate limit for the secure channel, and docs/lan.md.

Closes #47

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
@github-actions

github-actions Bot commented Dec 5, 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✅ passedDec 6, 2026, 1:54 AM

@Tichael

Tichael commented Dec 6, 2026

Copy link
Copy Markdown
Collaborator Author

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

Tested together with firmware#135, firmware#136 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. This PR (and docs/secure-channel.md from feat(system): add the device identity, client key enrollment and the secure channel spec #50) says the opposite:

    • docs/lan.md:17: "a validated phone may send basic and advanced vehicle commands"
    • docs/secure-channel.md:74: "the LAN gets the full state like BLE"

    Please change both, and anything else that ties LAN to the BLE tier. Whether the LAN's basic state is throttled like the relay's is up to you; say which in the docs.

  2. A manual address is used alone. If the app has an address the user entered, it connects to that address only, with no fallback to mDNS, the probe or the last known address. A wrong manual address stays wrong. If the discovery order in docs/lan.md mentions or implies a manual address, make that explicit.

  3. Suggestion: a receive timeout for the app. The device sends a system state on every LAN session every 5 s, but the only way the app notices a dead link today is its 60 s idle keepalive. After a board reset on the direct path, the app took 43 s to reconnect (test 5). A contract constant in [lan] (e.g. "the app treats N s without any frame as a dropped link", with N about 3× the 5 s period) would give the app and firmware a shared value.

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.

9. [PR #135] The PR's PC test plan: pass

Two clients enrolled through the BLE mock, A from 127.0.0.1 and B from 172.17.0.2 (the mock tells phones apart by IP).

  1. discover (broadcast and subnet broadcast) finds 8509306d… at 172.17.0.2:4734. run --lock does the handshake in 46 ms, receives the system state (availability and lan_address) then the full state, and the lock gets command response …: ok. candump vcan0 couldn't be checked: the dev container can't create vcan0.
  2. With A holding a session, the firmware was stopped and started again: same identity, and run did a fresh handshake (45 ms).
  3. With A and B both holding sessions, a third connection was closed at once, logged as LAN: refused 127.0.0.1:49880: every connection has a session. Both sessions kept receiving updates (12 each in 25 s).
  4. mDNS (no avahi in the container; checked with raw queries): mdns-sd answers legacy unicast and multicast, with PTR OpenCar._opencar._tcp.local, SRV 4734, TXT id, v=1 only, and A.

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).

Contract-level checks that held on hardware: the 15-byte probe and 17-byte reply, the 4 replies per second, the TXT keys (id, v, no pid), the 2 slots with the no-session replacement and refusal rule, the 10 s handshake deadline, and SystemState.lan_address (10.20.0.212:4734) over BLE and LAN.

- Through the secure channel (LAN and relay) a phone gets the basic state
  only and may send basic vehicle commands only: reaching the device over
  a network is no proof of being at the car. Advanced commands need
  Bluetooth.
- An address the user entered is tried alone, with no discovery fallback.
- [lan] system_state_repeat_ms (5000) and receive_timeout_ms (15000): the
  app treats a silent connection as dropped.

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

Thanks for the test. All three are in b101725:

  1. Remote tier. docs/lan.md and docs/secure-channel.md now say a LAN session gets the basic state only (no advanced_state_bytes) and basic commands only; advanced commands are refused with REJECTED_UNAVAILABLE, system commands stay Bluetooth-only. The LAN's basic state is not throttled (sent as it changes); the relay throttles it.
  2. Manual address alone. The discovery section and the discovery_order comment say a manual address replaces the whole list, with no fallback.
  3. Receive timeout. Added receive_timeout_ms = 15000 and system_state_repeat_ms = 5000 under [lan]: the device repeats its system state every 5 s, so a client closes after 15 s of silence.

firmware#135/#136 and mobile-app#138 are pinned to this commit.


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 b101725, together with firmware#135 a7bb7fe, firmware#136 c5fa22f 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.

R2. Wi-Fi is the remote tier, on the device (PC): pass

Over the LAN session, decoded:

  • vehicle state: basic=6B advanced=0B (BLE through the mock still gets the 6-byte advanced state);
  • availability: basic {door_lock: AVAILABLE}, advanced {toggle_custom_state1: UNAVAILABLE};
  • AdvancedCommand.toggle_custom_state1 → status_code 4 (REJECTED_UNAVAILABLE), "command rejected: advanced commands need a Bluetooth connection";
  • the basic door lock → ok.

Regression: with two sessions open, a third connection is still refused (LAN: refused 127.0.0.1:50564: every connection has a session).

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.

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).

@Tichael
Tichael merged commit 3998261 into main Dec 6, 2026
2 checks passed
@Tichael
Tichael deleted the feat/lan-transport-contract branch December 6, 2026 04:59
@github-actions

github-actions Bot commented Dec 6, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 0.17.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 LAN transport constants, discovery and the device's LAN address

2 participants