Files
ESP32-S3-LCD-1.3/wiki/pages/controller-b-ota.md
T

9.0 KiB
Raw Permalink Blame History

type, tags, updated
type tags updated
concept
ota
wifi
esp-now
security
design
verified
2026-09-27

Over-the-air updates for Controller B

Status 2026-09-27: implemented on both controllers, bench-tested up to the pairing step. B pulls its own signed release from Gitea over Wi-Fi; A hands B the network credentials and the trigger over the existing link, and shows B's progress on its own Updates page. Design chosen by the user over "A streams the image through ESP-NOW" (comparison below). Everything reuses A's proven pipeline (ota-updates in the sibling wiki).

What is verified, what is not

Step State
B → A STATUS frames (version, phase, heap, uptime) over ESP-NOW unicast (verified 2026-09-27) A's console link status lists B's MAC, dev, heap 234 KB; A's Updates page shows the Controller B card
Gates on both sides (unpaired → refused, no credentials → refused, key mismatch → refused) (verified 2026-09-27) each refusal text seen on the consoles
link pair over the UART wire not yet: the bench had no wire connected when tested (B: uart=0 ... uart silent). Needs 485_A → IO1, GND → GND, 120 Ω switch OFF (controller-link bench wiring)
Encrypted credential handoff, B joins the network not yet (needs pairing)
link ota check / install from A's screen, first signed B release not yet (needs pairing + release v0.1.0 of this repo)

Why pull, not push

A pushes over ESP-NOW B pulls itself (chosen)
Transport ~4,400 × 250-byte frames, acks/resends both ways (new protocol) one HTTPS download, TCP does the rest
Code new chunk protocol on both boards port of A's ota_update.cpp (proven)
RAM image transits A's scarce internal RAM B has 235 KB internal free (verified); TLS fits without A's PSRAM tricks
Integrity same signed manifest same signed manifest, same public key, target=patrol-b
New risk protocol bugs mid-flash Wi-Fi credentials must reach B once

How it works

1. Pairing (once, on the bench, over the wire)

link pair on A's console. A generates a random 32-byte key, sends PAIR (magic, key, A's STA MAC) three times over UART1 only and stores the key in NVS (linkpeer/key). B accepts PAIR only from the UART source (a PAIR heard on the radio is dropped and logged), stores key + A's MAC in NVS (linkpeer/key, linkpeer/amac) and answers with a STATUS carrying key_fp = first two bytes of SHA-256(key). A logs Controller B confirmed pairing (fingerprint XXXX) when it matches. Both repos are public, so this is the only secret and it never travels by radio. Survives reflashes (NVS); an NVS erase on either side means re-pairing at home — the car has no wire today (controller-link).

2. SECURE frames (A → B, ESP-NOW unicast)

Everything A sends B by radio that matters goes inside PACKET_TYPE_SECURE: nonce[12] | tag[16] | AES-256-GCM(key, nonce, inner_type | inner). Helpers in the shared include/protocol/link_secure.h (mbedtls GCM, same calls on IDF 4.4 and 5.5). B additionally requires the sender MAC to equal the paired A MAC. Decision 2026-09-27: application-level GCM instead of ESP-NOW's built-in peer encryption — no PMK/LMK peer bookkeeping, no doubt about whether an encrypted peer still receives A's plain broadcasts, and the same code on both cores. Inner payloads:

  • WIFI_CREDS (action SET/FORGET, ssid, pass): B stores them in NVS namespace wifi (same keys as A's). Pushed automatically: A's LinkPeer::tick() compares a hash of its remembered network against what B reports (LINK_STATUS_HAVE_WIFI) and pushes when B lacks credentials, when A's changed, or a FORGET when A forgot its network and B still has one. At most one push per 15 s. link creds pushes now.
  • OTA_CMD (check / install [force] / rollback, random nonce): B drops a nonce it has seen among the last four (replay).

3. STATUS (B → A, every 5 s and on every phase change)

link_status_t: B's FW_VERSION, OTA phase (same numbering as A's Ota::phase_t), progress %, flags (paired, have Wi-Fi, Wi-Fi joined, pending verify), key_fp, uptime, free heap, latest tag, failure message. Unicast to A's MAC as heard on A's DATA/CONFIG frames (or the MAC from pairing). A keeps the last one (LinkPeer::getStatus()), "not heard" after 15 s. During a job the loop task is blocked, so B's updater pumps a STATUS at most once a second from inside the download loop — that is how the progress percentage reaches A's screen.

4. A's Updates page

Settings → Updates now has two cards, Controller A and Controller B, each with Installed / Latest / a status line / Check / Install. B's buttons are enabled only when B is heard, paired with this key and holds credentials; the status line otherwise says which of those is missing. Install asks for confirmation, then LinkPeer::requestInstall() applies A's parking gate (engine off, shaft still, lever P/N — Ota::isParked()), requires A itself to be on Wi-Fi (same network as B ⇒ same radio channel ⇒ B's STATUS frames keep arriving while B is joined) and sends the OTA_CMD.

5. B's updater (src/modules/ota_update.cpp)

Port of A's module. Differences: gate = link data (rpm == 0, lever P/N/unknown; a silent link counts as parked, A's gate already ran); no Tunables; the gauge outputs are driven to their no-link state before the job; a one-line notice on the round screen (GaugeScreen::setNotice(), on lv_layer_top()), "Updating vX 42%"; OTA_TARGET "patrol-b", OTA_REPO "art/ESP32-S3-LCD-1.3", same public key and Let's Encrypt roots (include/config/ota_config.h); mbedtls 2.28 names. Wi-Fi: WiFi.begin() with the stored credentials only for the job (25 s join timeout), WiFi.disconnect(false) afterwards so the ESP-NOW receiver hops back to A's channel. verifyRollbackLater() override + confirm after 30 s of uptime, else the bootloader rolls back — identical to A. Jobs run in the loop task; the gauges pause (parked anyway). Console (CH343 serial): ota status | check | install [force] | rollback, link status | unpair, wifi status | forget, help.

6. Release pipeline

.gitea/workflows/firmware.yml in this repo (copy of A's): push to master → unsigned pre-release main-latest; tag v* → signed release with manifest.txt (target=patrol-b, ECDSA P-256 over patrol-b|<tag>|<sha256>), self-checked against tools/ota/firmware_signing_pub.pem. Upstream PlatformIO core is fine here (stock espressif32@6 platform; the pioarduino fork is only needed on A). Secret BMW_NISSAN_FIRMWARE_SIGNING_KEY set on the B repo by the user 2026-09-27. Version strings must be ≤ 15 characters (link_status_t::version[16]) — the workflow checks.

7. Shared header

automotive_data.h: PACKET_TYPE_STATUS 0x03, PACKET_TYPE_PAIR 0x04, PACKET_TYPE_SECURE 0x05, inner types LINK_INNER_OTA_CMD 0x11 / LINK_INNER_WIFI_CREDS 0x12, structs link_pair_t (42 B), link_status_t (78 B), link_ota_cmd_t, link_wifi_creds_t (98 B); PACKET_MAX_PAYLOAD 64 → 160 (a SECURE WIFI_CREDS payload is 127 B; the whole frame stays under ESP-NOW's 250 B). DATA/CONFIG bytes unchanged, so PACKET_VERSION stays 2 and an older B logs the new types as type errors and carries on. Header + link_secure.h byte-identical in both repos (cmp-checked).

Procedure: first pairing and first update

  1. Bench: both boards on USB, 485_A → IO1, GND → GND, 5B's 120 Ω switch OFF. B's 5-second stats line must say uart alive.
  2. A console: link pair → A prints the fingerprint; within 5 s link status shows key XXXX (matches) and B's console printed PAIRED over the wire.
  3. A on Wi-Fi with its remembered network → within ~15 s A logs Wi-Fi credentials for '<ssid>' sent to Controller B (encrypted), B logs stored, link status shows Wi-Fi creds yes.
  4. Tag this repo (git tag -a v0.1.0 -m ... && git push --follow-tags origin master), wait for the Gitea release with manifest.txt.
  5. A: Settings → Updates → Controller B → Check → "Version v0.1.0 is available for Controller B" → Install → confirm. B's screen shows "Updating v0.1.0 N%", A's card shows the percentage, B restarts, its STATUS says v0.1.0 with "new firmware, confirming..." for 30 s, then plain. Console alternative: link ota check, link ota install.

Risks / open

  • Wi-Fi + ESP-NOW coexistence on B while joined: A and B on the same AP share the channel; B's receiver follows the AP channel while joined and hops back after WiFi.disconnect(false). Not yet observed live.
  • Internal RAM on B during TLS: 235 KB free today; the job logs heap before/after. Measure once on the first check.
  • Pairing key lost (NVS erase on either side): re-pair at home over the wire.
  • Two boards, one PlatformIO penv: the first pio run after switching projects reinstalls the other Arduino core and can fail once with a TypeError; run it again. Builds must be started from PowerShell, not Git Bash (idf_tools.py refuses MSYS).

controller-link · link-redundancy · esp32-s3-lcd-1-3 · open-questions · (sibling: 5B wiki → ota-updates)