Controller B OTA: link_peer (STATUS to A, PAIR from the wire, AES-GCM SECURE frames), updater ported from A, console, CI workflow, shared header v2 maintenance frames; wiki + docs
Firmware / build (push) Successful in 2m57s

This commit is contained in:
2026-09-27 20:26:40 +02:00
parent 30ab113218
commit e6773c73ba
24 changed files with 1805 additions and 57 deletions
+105 -3
View File
@@ -16,6 +16,13 @@
* - link_config_t: Patrol gauge calibration + alert thresholds, edited on A,
* executed on B (tach + coolant PWM outputs)
* - the same frames go out on UART1 and ESP-NOW; B dedupes by packet_id
*
* v2 additions (2026-09-27, PACKET_VERSION unchanged - DATA/CONFIG are the same
* bytes, an older B counts the new types as FRAME_ERR_TYPE and carries on):
* - STATUS B -> A (ESP-NOW unicast): B's version, OTA phase, uptime
* - PAIR A -> B (UART wire ONLY): the shared secret for SECURE frames
* - SECURE A -> B (ESP-NOW unicast): AES-256-GCM envelope carrying an
* OTA_CMD or WIFI_CREDS payload. See ESP32-S3-LCD-1.3/wiki/pages/controller-b-ota.md
*/
#ifndef _AUTOMOTIVE_DATA_H
@@ -53,12 +60,19 @@
#define PACKET_END_BYTE 0x55
#define PACKET_VERSION 0x02
#define PACKET_TYPE_DATA 0x01 // payload = automotive_data_t
#define PACKET_TYPE_CONFIG 0x02 // payload = link_config_t
#define PACKET_TYPE_DATA 0x01 // A -> B payload = automotive_data_t
#define PACKET_TYPE_CONFIG 0x02 // A -> B payload = link_config_t
#define PACKET_TYPE_STATUS 0x03 // B -> A payload = link_status_t (ESP-NOW unicast)
#define PACKET_TYPE_PAIR 0x04 // A -> B payload = link_pair_t (UART wire only, never radio)
#define PACKET_TYPE_SECURE 0x05 // A -> B payload = secure envelope, see LINK MAINTENANCE below
// Inner types carried inside a SECURE envelope (never on the wire in the clear):
#define LINK_INNER_OTA_CMD 0x11 // link_ota_cmd_t
#define LINK_INNER_WIFI_CREDS 0x12 // link_wifi_creds_t
#define PACKET_HEADER_SIZE 4 // start, version, type, length
#define PACKET_TRAILER_SIZE 2 // checksum, end
#define PACKET_MAX_PAYLOAD 64
#define PACKET_MAX_PAYLOAD 160 // was 64 until 2026-09-27; SECURE(WIFI_CREDS) needs 127.
// Whole frame 166 B < ESP-NOW's 250 B limit.
#define PACKET_MAX_SIZE (PACKET_HEADER_SIZE + PACKET_MAX_PAYLOAD + PACKET_TRAILER_SIZE)
// ============================================================================
@@ -201,6 +215,88 @@ static inline void link_config_defaults(link_config_t* c) {
c->battery_low_x100 = 1100;
}
// ============================================================================
// LINK MAINTENANCE FRAMES (2026-09-27) - Controller B firmware updates
// ============================================================================
//
// B pulls its own signed release over Wi-Fi; A only hands it the credentials
// and the trigger. Both repos are public, so nothing secret is compiled in:
// the shared key is generated on A at pairing time and travels to B over the
// UART wire only. Everything A later sends B by radio that matters (Wi-Fi
// credentials, OTA commands) goes inside a SECURE envelope under that key.
// STATUS frames carry nothing secret and go in the clear.
#define LINK_KEY_LEN 32 // AES-256
#define LINK_NONCE_LEN 12 // GCM IV, random per frame
#define LINK_TAG_LEN 16 // GCM tag
#define LINK_STATUS_PERIOD_MS 5000 // B -> A STATUS cadence
#define LINK_STATUS_TIMEOUT_MS 15000 // A: B "not heard" after this
// --- PAIR (A -> B, wire only) -----------------------------------------------
#define LINK_PAIR_MAGIC 0x52494150UL // "PAIR"
typedef struct __attribute__((packed)) {
uint32_t magic; // LINK_PAIR_MAGIC - guards against a torn frame
uint8_t key[LINK_KEY_LEN]; // the shared secret; both sides keep it in NVS
uint8_t a_mac[6]; // A's STA MAC: the only sender B accepts SECURE frames from
} link_pair_t; // 42 bytes
// --- STATUS (B -> A, ESP-NOW unicast, every LINK_STATUS_PERIOD_MS) ----------
// ota_phase mirrors Controller A's Ota::phase_t so one UI renders both.
#define LINK_OTA_IDLE 0
#define LINK_OTA_CHECKING 1
#define LINK_OTA_UP_TO_DATE 2
#define LINK_OTA_AVAILABLE 3
#define LINK_OTA_INSTALLING 4
#define LINK_OTA_FAILED 5
#define LINK_STATUS_PAIRED (1 << 0) // B holds a pairing key
#define LINK_STATUS_HAVE_WIFI (1 << 1) // B holds Wi-Fi credentials
#define LINK_STATUS_WIFI_JOINED (1 << 2) // B is currently on the network (during a job)
#define LINK_STATUS_PENDING_VERIFY (1 << 3) // B runs a fresh image not yet confirmed
typedef struct __attribute__((packed)) {
char version[16]; // B's firmware version ("dev" for local builds)
uint8_t ota_phase; // LINK_OTA_*
uint8_t ota_progress; // 0..100 while installing
uint8_t flags; // LINK_STATUS_*
uint8_t reserved;
uint16_t key_fp; // first two bytes of SHA-256(pairing key); 0 = unpaired.
// Lets A see "paired with MY key" vs "paired with an old one".
uint32_t uptime_s;
uint32_t free_heap; // internal heap free, bytes
char latest[16]; // latest release tag B knows of ("" = never checked)
char message[32]; // failure reason when ota_phase == LINK_OTA_FAILED
} link_status_t; // 78 bytes
// --- OTA_CMD (inner, A -> B) ------------------------------------------------
#define LINK_OTA_CMD_CHECK 1
#define LINK_OTA_CMD_INSTALL 2
#define LINK_OTA_CMD_ROLLBACK 3
#define LINK_OTA_FORCE (1 << 0) // reinstall even if versions match
typedef struct __attribute__((packed)) {
uint8_t cmd; // LINK_OTA_CMD_*
uint8_t flags; // LINK_OTA_FORCE
uint32_t nonce; // random; B drops a repeat of a recently seen one
} link_ota_cmd_t; // 6 bytes
// --- WIFI_CREDS (inner, A -> B) ---------------------------------------------
#define LINK_WIFI_SET 1
#define LINK_WIFI_FORGET 2
typedef struct __attribute__((packed)) {
uint8_t action; // LINK_WIFI_*
char ssid[33];
char pass[64];
} link_wifi_creds_t; // 98 bytes
// --- SECURE envelope ---------------------------------------------------------
// payload = nonce[12] | tag[16] | ciphertext
// ciphertext = AES-256-GCM(key, nonce, plaintext = inner_type | inner_payload)
// Helpers that do the encryption live in protocol/link_secure.h (mbedtls).
#define LINK_SECURE_OVERHEAD (LINK_NONCE_LEN + LINK_TAG_LEN)
#define LINK_SECURE_MAX_INNER (PACKET_MAX_PAYLOAD - LINK_SECURE_OVERHEAD) // 132: 1 + 98 fits
// ============================================================================
// CAN BUS ID MAPPINGS (BMW-Specific)
// ============================================================================
@@ -413,6 +509,12 @@ static inline frame_status_t frame_parse(const uint8_t* f, size_t n,
if (l != sizeof(automotive_data_t)) return FRAME_ERR_TYPE;
} else if (t == PACKET_TYPE_CONFIG) {
if (l != sizeof(link_config_t)) return FRAME_ERR_TYPE;
} else if (t == PACKET_TYPE_STATUS) {
if (l != sizeof(link_status_t)) return FRAME_ERR_TYPE;
} else if (t == PACKET_TYPE_PAIR) {
if (l != sizeof(link_pair_t)) return FRAME_ERR_TYPE;
} else if (t == PACKET_TYPE_SECURE) {
if (l < LINK_SECURE_OVERHEAD + 1) return FRAME_ERR_TYPE; // at least the inner type byte
} else {
return FRAME_ERR_TYPE;
}