Controller B updates from A: link_peer (pairing over the wire, encrypted credential handoff, B status + commands), ESP-NOW receive/unicast, Updates page with two cards; shared header maintenance frames; docs + wiki
Firmware / build (push) Successful in 7m1s

This commit is contained in:
2026-09-27 20:26:44 +02:00
parent 8338e4e300
commit 3e86ca3733
20 changed files with 884 additions and 50 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;
}
+93
View File
@@ -0,0 +1,93 @@
/**
* link_secure.h - the SECURE envelope of the A <-> B link (see automotive_data.h).
*
* THIS FILE MUST BE BYTE-IDENTICAL IN BOTH REPOS, like automotive_data.h.
*
* payload = nonce[12] | tag[16] | AES-256-GCM(key, nonce, inner_type | inner)
*
* Both cores ship mbedtls (2.28 on Controller B's IDF 4.4, 3.x on Controller
* A's IDF 5.5); the GCM calls are the same, only SHA-256's name differs.
*/
#ifndef LINK_SECURE_H
#define LINK_SECURE_H
#include "automotive_data.h"
#include <mbedtls/gcm.h>
#include <mbedtls/sha256.h>
#include <mbedtls/version.h>
#if __has_include(<esp_random.h>)
#include <esp_random.h>
#else
#include <esp_system.h> // IDF 4.4: esp_random / esp_fill_random live here
#endif
/** First two bytes of SHA-256(key), never 0 (0 means "unpaired" on the wire). */
static inline uint16_t link_key_fingerprint(const uint8_t key[LINK_KEY_LEN]) {
uint8_t h[32];
#if MBEDTLS_VERSION_MAJOR >= 3
mbedtls_sha256(key, LINK_KEY_LEN, h, 0);
#else
mbedtls_sha256_ret(key, LINK_KEY_LEN, h, 0);
#endif
uint16_t fp = (uint16_t)((h[0] << 8) | h[1]);
return fp ? fp : 1;
}
/**
* Seal inner_type + inner into a SECURE payload.
* @param out buffer of PACKET_MAX_PAYLOAD bytes
* @return payload length, or 0 on error / oversize
*/
static inline size_t link_secure_seal(const uint8_t key[LINK_KEY_LEN], uint8_t inner_type,
const void* inner, size_t inner_len, uint8_t* out) {
if (inner_len + 1 > LINK_SECURE_MAX_INNER) return 0;
uint8_t plain[LINK_SECURE_MAX_INNER];
plain[0] = inner_type;
memcpy(&plain[1], inner, inner_len);
size_t n = inner_len + 1;
uint8_t* nonce = out;
uint8_t* tag = out + LINK_NONCE_LEN;
uint8_t* ct = out + LINK_SECURE_OVERHEAD;
esp_fill_random(nonce, LINK_NONCE_LEN);
mbedtls_gcm_context g;
mbedtls_gcm_init(&g);
int rc = mbedtls_gcm_setkey(&g, MBEDTLS_CIPHER_ID_AES, key, LINK_KEY_LEN * 8);
if (rc == 0) {
rc = mbedtls_gcm_crypt_and_tag(&g, MBEDTLS_GCM_ENCRYPT, n, nonce, LINK_NONCE_LEN,
NULL, 0, plain, ct, LINK_TAG_LEN, tag);
}
mbedtls_gcm_free(&g);
return rc == 0 ? LINK_SECURE_OVERHEAD + n : 0;
}
/**
* Open a SECURE payload. On success *inner_type and inner (LINK_SECURE_MAX_INNER
* bytes) are filled; *inner_len excludes the type byte.
*/
static inline bool link_secure_open(const uint8_t key[LINK_KEY_LEN], const uint8_t* payload, size_t len,
uint8_t* inner_type, uint8_t* inner, size_t* inner_len) {
if (len < LINK_SECURE_OVERHEAD + 1 || len > PACKET_MAX_PAYLOAD) return false;
size_t n = len - LINK_SECURE_OVERHEAD;
uint8_t plain[LINK_SECURE_MAX_INNER];
mbedtls_gcm_context g;
mbedtls_gcm_init(&g);
int rc = mbedtls_gcm_setkey(&g, MBEDTLS_CIPHER_ID_AES, key, LINK_KEY_LEN * 8);
if (rc == 0) {
rc = mbedtls_gcm_auth_decrypt(&g, n, payload, LINK_NONCE_LEN, NULL, 0,
payload + LINK_NONCE_LEN, LINK_TAG_LEN,
payload + LINK_SECURE_OVERHEAD, plain);
}
mbedtls_gcm_free(&g);
if (rc != 0) return false;
*inner_type = plain[0];
*inner_len = n - 1;
memcpy(inner, &plain[1], n - 1);
return true;
}
#endif // LINK_SECURE_H