Merge origin/main (27 commits from the other machine) with the Engine data screen
Firmware / build (push) Successful in 7m2s

All incoming changes kept as they were. Conflicts resolved by adding the
Engine-data pieces next to them:
- main.cpp: StatusOverlay::tick() and EngineScreen::tick()
- driveline_screen.cpp: both include sets; the old mismatch label stays
  removed (overlay owns it); the Engine data button moves to y 60, left of
  the diff-lock drawing, clear of the overlay band
- CLAUDE.md: Engine data bullet added; DiagProbe paragraph keeps its text
  plus the live-mode sentence
- wiki: all incoming log entries kept, the 2026-10-07 entry appended;
  open-questions #12 keeps the renamed line plus the 2026-10-07 update

Built clean locally (custom_sdkconfig core rebuild).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-08 11:51:53 +02:00
69 changed files with 3445 additions and 230 deletions
+2 -2
View File
@@ -1,5 +1,5 @@
/**
* ota_config.h - where Controller A looks for firmware updates, and what it trusts.
* ota_config.h - where Main CTRL looks for firmware updates, and what it trusts.
*
* Updates are pulled over HTTPS from this repo's Gitea releases (public repo,
* no token). CI (.gitea/workflows/firmware.yml) attaches firmware.bin +
@@ -19,7 +19,7 @@
#define FW_VERSION "dev"
#endif
// Identity inside the signed statement, so a Controller B image can never be
// Identity inside the signed statement, so a Inst. Cluster image can never be
// accepted by A (and vice versa) even though both are signed by the same key.
#define OTA_TARGET "patrol-a"
+40 -1
View File
@@ -9,6 +9,8 @@
#define ESP_UTILS_LOG_TAG "LvPort"
#include "esp_lib_utils.h"
#include "lvgl_v8_port.h"
#include "modules/perf_monitor.h"
#include "esp_timer.h"
using namespace esp_panel::drivers;
@@ -478,14 +480,35 @@ void flush_callback(lv_disp_drv_t *drv, const lv_area_t *area, lv_color_t *color
const int offsetx2 = area->x2;
const int offsety1 = area->y1;
const int offsety2 = area->y2;
const int w = offsetx2 - offsetx1 + 1;
const int h = offsety2 - offsety1 + 1;
lcd->drawBitmap(offsetx1, offsety1, offsetx2 - offsetx1 + 1, offsety2 - offsety1 + 1, (const uint8_t *)color_map);
// Time the pixel copy into the (PSRAM) framebuffer - this is the PSRAM
// write cost the display experiments (open question #13) change.
const int64_t t0 = esp_timer_get_time();
lcd->drawBitmap(offsetx1, offsety1, w, h, (const uint8_t *)color_map);
PerfMon::onFlush((uint32_t)(esp_timer_get_time() - t0), (uint32_t)(w * h));
// For RGB LCD, directly notify LVGL that the buffer is ready
if (lcd->getBus()->getBasicAttributes().type == ESP_PANEL_BUS_TYPE_RGB) {
lv_disp_flush_ready(drv);
}
}
// LVGL calls this once per rendered frame (render + flush time, pixels drawn).
static void monitor_callback(lv_disp_drv_t *drv, uint32_t time_ms, uint32_t px)
{
(void)drv;
PerfMon::onFrame(time_ms, px);
}
// RGB panel "refresh finished" (vsync) interrupt - counted for the perf monitor.
static IRAM_ATTR bool onPerfVsyncCallback(void *user_data)
{
(void)user_data;
PerfMon::onVsync();
return false; // no context switch needed
}
static void update_callback(lv_disp_drv_t *drv)
{
LCD *lcd = (LCD *)drv->user_data;
@@ -605,6 +628,9 @@ static lv_disp_t *display_init(LCD *lcd)
ESP_UTILS_LOGD("Register display driver to LVGL");
lv_disp_drv_init(&disp_drv);
disp_drv.flush_cb = flush_callback;
#if LVGL_PORT_AVOID_TEARING_MODE == 0
disp_drv.monitor_cb = monitor_callback;
#endif
#if (LVGL_PORT_ROTATION_DEGREE == 90) || (LVGL_PORT_ROTATION_DEGREE == 270)
disp_drv.hor_res = lcd_height;
disp_drv.ver_res = lcd_width;
@@ -802,6 +828,10 @@ bool lvgl_port_init(LCD *lcd, Touch *tp)
#if LVGL_PORT_AVOID_TEAR
lcd->attachRefreshFinishCallback(onLcdVsyncCallback, (void *)lvgl_task_handle);
#else
if (bus_type == ESP_PANEL_BUS_TYPE_RGB) {
lcd->attachRefreshFinishCallback(onPerfVsyncCallback, nullptr);
}
#endif
return true;
@@ -815,6 +845,15 @@ bool lvgl_port_lock(int timeout_ms)
return (xSemaphoreTakeRecursive(lvgl_mux, timeout_ticks) == pdTRUE);
}
bool lvgl_port_lock_available(void)
{
if (!lvgl_port_lock(0)) {
return false;
}
lvgl_port_unlock();
return true;
}
bool lvgl_port_unlock(void)
{
ESP_UTILS_CHECK_NULL_RETURN(lvgl_mux, false, "LVGL mutex is not initialized");
+8
View File
@@ -171,6 +171,14 @@ bool lvgl_port_lock(int timeout_ms);
*/
bool lvgl_port_unlock(void);
/**
* @brief Try the LVGL lock without waiting and release it at once.
* Used by the perf monitor to read lv_timer_get_idle() only when the
* UI task is not mid-render (the value is a plain int, so a stale read
* is harmless; a blocked console is not).
*/
bool lvgl_port_lock_available(void);
#ifdef __cplusplus
}
#endif
+1 -1
View File
@@ -69,7 +69,7 @@ typedef struct {
/**
* Decoded driveline speeds. Deliberately NOT part of automotive_data_t: that
* struct is the wire format for the UART link to Controller B, and widening it
* struct is the wire format for the UART link to Inst. Cluster, and widening it
* would break the receiver. These live here until there's a reason to spend a
* protocol version bump on them.
*
+114 -20
View File
@@ -1,8 +1,8 @@
/**
* automotive_data.h
*
* Shared wire contract between Controller A (ESP32-S3-Touch-LCD-5B) and
* Controller B (ESP32-S3-LCD-1.3).
* Shared wire contract between Main CTRL (ESP32-S3-Touch-LCD-5B) and
* Inst. Cluster (ESP32-S3-LCD-1.3).
*
* THIS FILE MUST BE BYTE-IDENTICAL IN BOTH REPOS. Copy it across whenever it
* changes and bump PACKET_VERSION; B rejects frames with any other version.
@@ -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
@@ -38,8 +45,8 @@
// (B hops until it hears A, then locks)
// NOTE: UART pin assignments are controller-specific:
// - Controller A (5B): uart_manager.cpp (TX GPIO 44 -> SP3485 -> 485_A/B)
// - Controller B (1.3): platformio.ini (RX IO1)
// - Main CTRL (5B): uart_manager.cpp (TX GPIO 44 -> SP3485 -> 485_A/B)
// - Inst. Cluster (1.3): platformio.ini (RX IO1)
// ============================================================================
// FRAME FORMAT (identical on UART and ESP-NOW)
@@ -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)
// ============================================================================
@@ -76,7 +90,7 @@ typedef enum {
LEVER_UNKNOWN = 0xFF
} gear_lever_t;
// Controller A's internal gear enum (CAN decode helpers below use it). Not on
// Main CTRL's internal gear enum (CAN decode helpers below use it). Not on
// the wire any more - B never sees it.
typedef enum {
GEAR_PARK = 0,
@@ -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) - Inst. Cluster 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 Main CTRL'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;
}
@@ -654,7 +756,7 @@ static inline const char* gear_to_string(gear_position_t gear) {
* @return Pointer to buffer
*/
static inline char* format_temperature(int16_t temp_x10, char* buffer) {
// Plain "C": Controller B's UI font has no degree-sign glyph.
// Plain "C": Inst. Cluster's UI font has no degree-sign glyph.
sprintf(buffer, "%d.%dC", temp_x10 / 10, abs(temp_x10 % 10));
return buffer;
}
@@ -670,16 +772,8 @@ static inline char* format_voltage(uint16_t voltage_x100, char* buffer) {
return buffer;
}
// ============================================================================
// DEFAULT/TEST VALUES
// ============================================================================
// Default values for testing (before CAN data is available)
#define DEFAULT_GEAR_LEVER LEVER_PARK
#define DEFAULT_GEARBOX_TEMP 850 // 85.0°C
#define DEFAULT_ENGINE_OIL_TEMP 920 // 92.0°C
#define DEFAULT_COOLANT_TEMP 890 // 89.0°C
#define DEFAULT_BATTERY_VOLTAGE 1245 // 12.45V
#define DEFAULT_ALERTS 0x0000 // No alerts
// No default / test values: a field is only ever filled from a CAN frame, and
// its VALID_FLAG_* bit says so. Removed 2026-09-28 - the bench defaults
// (89 / 85 degC, 12.45 V) looked like real readings on Inst. Cluster.
#endif // _AUTOMOTIVE_DATA_H
+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 Inst. Cluster'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