Files

86 lines
11 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
type: concept
tags: [ota, wifi, esp-now, security, verified]
updated: 2026-09-27
---
# Over-the-air updates for Inst. Cluster
**Status 2026-09-27: implemented on both controllers and VERIFIED END-TO-END on the bench - B went from `dev` to `v0.1.0` on a command typed on A, rebooted and confirmed itself.** 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 Inst. Cluster 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 | `(verified 2026-09-27 21:2x)` wire `485_A → IO1`, ground shared through the USB hub only. A: `PAIR sent over the wire 3 time(s); new key fingerprint A9B7`; B: `PAIRED over the wire ... A = 30:ED:A0:17:B2:4C` (+ one "re-confirmed" for the repeats); A: `Inst. Cluster confirmed pairing (fingerprint A9B7)` within 5 s |
| Encrypted credential handoff | `(verified)` A pushed `ArtServis` on its own right after pairing; B: `Wi-Fi credentials for 'ArtServis' stored`; `wifi status` on B lists the network |
| `link ota check` from A | `(verified)` B joined the AP (IP 10.0.10.101, channel 1) in ~2 s, before the release existed: `no release published yet (push a v* tag)` reported back to A as phase *failed* + message; after the release: `latest release v0.1.0 (1248432 bytes, signature OK)`, A: `update available, latest v0.1.0` |
| `link ota install` from A, first signed release `v0.1.0` | `(verified)` download + flash of 1,248,432 B into `app1` in ~50 s, `verified and installed v0.1.0`, reboot, banner `Firmware v0.1.0`, `NEW IMAGE, unconfirmed`, STATUS to A resumed within seconds (`firmware v0.1.0`), `firmware v0.1.0 confirmed - rollback cancelled` at 30 s; `ota status`: slot app1 state valid. Loop stack headroom after a job: 3.4 KB; heap 230 KB before/after |
| Screen path (Inst. Cluster card on A's Updates page, B's on-screen notice line) | same request functions as the console commands, but the buttons have **not been tapped yet** and the "Updating v0.1.0 N%" line on B's round screen was not watched - user to confirm on the next update |
## 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 `Inst. Cluster 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, **Main CTRL** and **Inst. Cluster**, 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 Inst. Cluster (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 → Inst. Cluster → Check → "Version v0.1.0 is available for Inst. Cluster" → 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**: `(verified)` A on `ArtServis` channel 1, B joined the same AP on channel 1, STATUS kept flowing during the download (A saw phase *installing*), B re-locked on channel 1 after `WiFi.disconnect(false)`. Untested: A **not** on Wi-Fi or on a different AP - by design A refuses B commands unless it is on Wi-Fi itself.
- **Internal RAM on B during TLS**: `(verified)` 230.7 KB free before the job, 230.4 KB after; largest block 164 KB. No PSRAM tricks needed.
- **UART wire quality on the bench**: with ground shared only through the USB hub, B logs bursts of `Checksum error on uart` (7 in a row at times) and an occasional `Framing error` / `Version mismatch got v0`; 2026-09-21 with a direct GND wire it was 0 errors. Harmless for the data (ESP-NOW copy wins, dedupe) and PAIR got through (sent 3×), but run a real GND wire next time → [[open-questions]] #1 / [[link-redundancy]].
- **`link_status_t::message` is 32 bytes**: B's failure reason `no release published yet (push a v* tag)` arrived truncated at "(push". Cosmetic; lengthen if it ever matters.
- **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).
## Related
[[controller-link]] · [[link-redundancy]] · [[esp32-s3-lcd-1-3]] · [[open-questions]] · (sibling: 5B wiki → ota-updates)