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

84 lines
6.3 KiB
Markdown
Raw 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, design]
updated: 2026-09-27
---
# Over-the-air updates for Controller B
Design agreed 2026-09-27 (user chose this over A streaming the image through ESP-NOW): **B pulls its own signed release from Gitea over Wi-Fi; A only hands B the network credentials and the trigger, over the existing link.** Everything reuses what already works on A ([[ota-updates]] in the sibling wiki).
## Why pull, not push
| | A pushes over ESP-NOW | B pulls itself (chosen) |
|---|---|---|
| Transport | ~4,400 × 250-byte frames, needs 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` (729 lines, proven) |
| RAM | image transits A's scarce internal RAM | B has 235 KB internal free after the 2026-09-27 clean-up `(verified)`; TLS fits without the PSRAM tricks A needs |
| 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 |
## Prerequisites — all in place `(verified 2026-09-27)`
- B's partition table: two 6.5 MB OTA slots (`default_16MB.csv`), NVS untouched.
- B's core (Arduino 2.0.17 / IDF 4.4, `qio_opi` sdkconfig): `CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE=1`, `MBEDTLS_ECDSA_C`, `SECP256R1`, `PEM_PARSE_C` — no core rebuild needed.
- B has 8 MB PSRAM and a Wi-Fi radio already running in STA mode for ESP-NOW.
## The pieces
### 1. Credential handoff (A → B, once)
A knows the remembered network (`WiFiManager`, NVS namespace `wifi`). B must learn it without it being broadcast in the clear — the ESP-NOW data link is unencrypted broadcast and both repos are public, so a compiled-in key would protect nothing.
- **Pairing on the bench, over the wire**: a console command on A (`link pair`) generates a random 16-byte key, sends it to B **only over UART1** (the physical wire, not ESP-NOW), both sides store it in NVS. Done once; survives reflashes (NVS).
- After pairing, the credential frame goes **ESP-NOW unicast, encrypted** with that key (ESP-NOW peer with `encrypt=true`, LMK = the pairing key, PMK fixed). B learns A's MAC from the frames it already receives; A learns B's MAC from B's status frames (below).
- B stores the credentials in its own NVS so a later update does not need A awake — and `wifi forget` on A also tells B to forget.
### 2. Status back-channel (B → A)
New frame type `STATUS` (B → A, ESP-NOW unicast to A's MAC, every 5 s): B's firmware version, OTA phase, uptime, free heap. A shows it on Settings → Updates as a second row, **Controller B: vX.Y.Z**, with its own Check / Install. This also gives A the MAC it needs for the encrypted unicast.
### 3. Trigger (A → B)
New frame type `OTA_CMD` (A → B, unicast): `check` or `install`. A applies its own parking gate first (engine off, shaft still, lever P/N — the same one that guards A's install) and refuses otherwise. B additionally refuses while it sees rpm > 0 in the link data.
### 4. B's OTA module
`src/modules/ota_update.cpp` on B = A's module with these changes:
- gate: link data (rpm == 0, no shaft) instead of the CAN driver; A's gate already ran.
- no `Tunables::flush()`; `GaugeOutputs` are forced low for the duration (parked anyway).
- notice on the 240×240 screen: a small "Updating vX — N %" line under the gear (SPI panel, no flicker problem).
- `OTA_TARGET "patrol-b"`, `OTA_REPO "art/ESP32-S3-LCD-1.3"`, same public key as A (`tools/ota/firmware_signing_pub.pem`), same Let's Encrypt roots.
- `verifyRollbackLater()` override + confirm after 30 s of healthy uptime, else the bootloader rolls back — identical to A.
- Wi-Fi: join with the stored credentials only for the job, then `WiFi.disconnect()` back to a bare STA so ESP-NOW hopping resumes. (During the join B's channel follows the AP; A follows the same AP, so the link keeps working — and the gauges are idle while parked.)
- Console on B (CH343 serial): `ota status | check | install [force] | rollback`, `wifi status | forget`.
### 5. Release pipeline for B
A copy of A's `.gitea/workflows/firmware.yml` in the B repo: `pio run` (no core rebuild — B is on the stock espressif32@6 platform, so upstream PlatformIO is fine there), tag `v*` → signed release with `manifest.txt` (`target=patrol-b`), `main` → unsigned `main-latest`. Needs the repo secret **`BMW_NISSAN_FIRMWARE_SIGNING_KEY`** on the B repo (or as an organisation secret) — *user action*.
### 6. Shared header
`automotive_data.h` gains `PACKET_TYPE_STATUS 0x03`, `PACKET_TYPE_OTA_CMD 0x04`, `PACKET_TYPE_WIFI_CREDS 0x05` (+ `PACKET_TYPE_PAIR 0x06` on the wire only) with their payload structs. DATA/CONFIG unchanged, so `PACKET_VERSION` stays 2 and an older B simply ignores the new types. The header stays byte-identical in both repos.
## Order of work
1. Shared header + B: STATUS frames out, A: receive callback + Updates row (visible progress from day one).
2. Pairing over the wire + encrypted credential handoff; B stores and can join (`wifi status` on B shows it).
3. B OTA module port + on-screen notice + gauge hold; console-driven test with a **dev release** (see below).
4. B CI workflow; first signed `v0.1.0` for B; end-to-end from A's screen.
5. Wiki + CLAUDE.md on both sides.
Test without the secret: nothing can be signed locally (the private key exists only as a Gitea secret), so step 3 is tested against A's signature path by pointing B's `OTA_REPO` at the B repo's first CI release — i.e. step 4 precedes the end-to-end test. Everything up to "manifest signature OK" can be exercised earlier with A's release (target mismatch is the expected, correct refusal).
## Risks
- **Wi-Fi + ESP-NOW coexistence on B** while joined to the AP: same situation as on A (works, channel follows the AP). If B's ESP-NOW receive stalls during the download it does not matter — parked.
- **Internal RAM on B during TLS**: 235 KB free today; A needed ~40 KB internal per TLS session. Fine, but measure once.
- **A's Updates page** grows a second row; A's LVGL pool is in PSRAM, no concern.
- **Pairing key lost** (NVS erase on either side): re-pair on the bench. The car has no wire today (open question in [[controller-link]]) — pairing must be done at home.
## Related
[[controller-link]] · [[link-redundancy]] · [[esp32-s3-lcd-1-3]] · [[open-questions]] · (sibling: 5B wiki → ota-updates)