Rename Controller A/B -> Main CTRL / Inst. Cluster (user); Sleep when the car is off: Settings switch, deep sleep on quiet CAN + idle screen, CAN RX wake, switch mirrored to the cluster; docs
Firmware / build (push) Successful in 7m22s

This commit is contained in:
2026-09-28 16:38:13 +02:00
parent 7a74c02b20
commit 803af7568f
48 changed files with 400 additions and 156 deletions
+21 -21
View File
@@ -1,5 +1,5 @@
/*
* Link Peer Module Implementation (Controller A)
* Link Peer Module Implementation (Main CTRL)
*
* Threading: onFrame() runs in the Wi-Fi task and only copies the STATUS into
* g_peer under a mutex. Everything else (NVS, sending, console) is the loop task.
@@ -129,9 +129,9 @@ peer_status_t getStatus() {
// ---- outbound ---------------------------------------------------------------
static bool sendSecure(uint8_t inner_type, const void* inner, size_t inner_len, char* why, size_t n) {
peer_status_t p = getStatus();
if (!p.heard) { snprintf(why, n, "Controller B not heard"); return false; }
if (!p.heard) { snprintf(why, n, "Inst. Cluster not heard"); return false; }
if (!p.paired) { snprintf(why, n, "not paired - run `link pair` with the wire connected"); return false; }
if (!p.key_match) { snprintf(why, n, "Controller B holds a different key - run `link pair` again"); return false; }
if (!p.key_match) { snprintf(why, n, "Inst. Cluster holds a different key - run `link pair` again"); return false; }
uint8_t payload[PACKET_MAX_PAYLOAD], frame[PACKET_MAX_SIZE];
size_t plen = link_secure_seal(g_key, inner_type, inner, inner_len, payload);
@@ -175,8 +175,8 @@ static bool pushCredentials(char* why, size_t n, bool log) {
if (ok) {
g_pushed_hash = have ? hashStr(hashStr(2166136261u, c.ssid), c.pass) : 1;
g_last_push_ms = millis();
if (log) print(have ? "[link] Wi-Fi credentials for '%s' sent to Controller B (encrypted)"
: "[link] told Controller B to forget its Wi-Fi credentials", c.ssid);
if (log) print(have ? "[link] Wi-Fi credentials for '%s' sent to Inst. Cluster (encrypted)"
: "[link] told Inst. Cluster to forget its Wi-Fi credentials", c.ssid);
}
memset(&c, 0, sizeof(c));
return ok;
@@ -204,7 +204,7 @@ static void syncCredentials() {
// ---- pairing ----------------------------------------------------------------
static bool pair(char* why, size_t n) {
if (UARTManager::getState() == UARTManager::UART_STATE_OFF) {
snprintf(why, n, "link is off - enable 'Send to Controller B' in Settings first");
snprintf(why, n, "link is off - enable 'Send to Inst. Cluster' in Settings first");
return false;
}
link_pair_t pk = {};
@@ -229,31 +229,31 @@ static bool pair(char* why, size_t n) {
g_pushed_hash = 0; // re-send the credentials under the new key
g_last_push_ms = 0;
print("[link] PAIR sent over the wire %d time(s); new key fingerprint %04X. "
"Controller B confirms in its next STATUS (`link status`).", sent, g_key_fp);
"Inst. Cluster confirms in its next STATUS (`link status`).", sent, g_key_fp);
return true;
}
// ---- public: requests (UI) --------------------------------------------------
static bool bReady(char* why, size_t n, bool need_parked) {
peer_status_t p = getStatus();
if (!p.heard) { snprintf(why, n, "Controller B not heard - is it powered?"); return false; }
if (!p.heard) { snprintf(why, n, "Inst. Cluster not heard - is it powered?"); return false; }
if (!p.paired || !p.key_match) {
snprintf(why, n, "Controller B is not paired with this controller (console: link pair, wire connected)");
snprintf(why, n, "Inst. Cluster is not paired with this controller (console: link pair, wire connected)");
return false;
}
if (!(p.st.flags & LINK_STATUS_HAVE_WIFI)) {
snprintf(why, n, "Controller B has no Wi-Fi credentials yet - join a network on this screen first");
snprintf(why, n, "Inst. Cluster has no Wi-Fi credentials yet - join a network on this screen first");
return false;
}
if (p.st.ota_phase == LINK_OTA_CHECKING || p.st.ota_phase == LINK_OTA_INSTALLING) {
snprintf(why, n, "Controller B is busy (%s)", phaseName(p.st.ota_phase));
snprintf(why, n, "Inst. Cluster is busy (%s)", phaseName(p.st.ota_phase));
return false;
}
if (need_parked && !Ota::isParked(why, n)) return false;
// Same network as B => same radio channel, so B's STATUS frames (progress)
// keep reaching this screen while B is joined.
if (WiFiManager::getStatus().link != WiFiManager::LINK_CONNECTED) {
snprintf(why, n, "join Wi-Fi on this controller first, so Controller B's progress can be followed");
snprintf(why, n, "join Wi-Fi on this controller first, so Inst. Cluster's progress can be followed");
return false;
}
return true;
@@ -274,7 +274,7 @@ void init() {
if (g_mutex == NULL) g_mutex = xSemaphoreCreateMutex();
loadKey();
EspNowLink::setReceiveHandler(onFrame);
print("[link] Controller B: %s", g_paired ? "pairing key loaded" : "NOT paired - `link pair` with the wire connected");
print("[link] Inst. Cluster: %s", g_paired ? "pairing key loaded" : "NOT paired - `link pair` with the wire connected");
}
void tick() {
@@ -288,18 +288,18 @@ void tick() {
peer_status_t p = getStatus();
if (p.heard != was_heard) {
was_heard = p.heard;
if (p.heard) print("[link] Controller B heard: %s, %s, %lu s up", p.st.version,
if (p.heard) print("[link] Inst. Cluster heard: %s, %s, %lu s up", p.st.version,
p.key_match ? "paired" : (p.st.key_fp ? "paired with ANOTHER key" : "unpaired"),
(unsigned long)p.st.uptime_s);
else print("[link] Controller B silent for %lu s", (unsigned long)(p.age_ms / 1000));
else print("[link] Inst. Cluster silent for %lu s", (unsigned long)(p.age_ms / 1000));
}
if (p.heard && p.key_match && !was_match) {
print("[link] Controller B confirmed pairing (fingerprint %04X)", p.st.key_fp);
print("[link] Inst. Cluster confirmed pairing (fingerprint %04X)", p.st.key_fp);
}
was_match = p.heard && p.key_match;
if (p.heard && p.st.ota_phase != last_phase) {
if (last_phase != 0xFF || p.st.ota_phase != LINK_OTA_IDLE)
print("[link] Controller B update: %s%s%s%s%s", phaseName(p.st.ota_phase),
print("[link] Inst. Cluster update: %s%s%s%s%s", phaseName(p.st.ota_phase),
p.st.latest[0] ? ", latest " : "", p.st.latest,
p.st.ota_phase == LINK_OTA_FAILED ? " - " : "",
p.st.ota_phase == LINK_OTA_FAILED ? p.st.message : "");
@@ -310,7 +310,7 @@ void tick() {
// ---- console ----------------------------------------------------------------
static void help() {
print("link status Controller B: version, pairing, update phase");
print("link status Inst. Cluster: version, pairing, update phase");
print("link pair new shared key -> B over the UART wire (bench, wire connected)");
print("link unpair forget the key here (B keeps its own until re-paired)");
print("link creds send the remembered Wi-Fi network to B now");
@@ -329,8 +329,8 @@ bool handleCommand(const char* line) {
print("[link] this side: %s (fingerprint %04X) | STATUS frames %lu | SECURE sent %lu, failed %lu",
p.paired ? "paired" : "NOT paired", p.key_fp, (unsigned long)g_status_count,
(unsigned long)g_secure_sent, (unsigned long)g_secure_fail);
if (!g_ever_heard) { print("[link] Controller B never heard"); return true; }
print("[link] Controller B %02X:%02X:%02X:%02X:%02X:%02X, %s (%lu s ago)",
if (!g_ever_heard) { print("[link] Inst. Cluster never heard"); return true; }
print("[link] Inst. Cluster %02X:%02X:%02X:%02X:%02X:%02X, %s (%lu s ago)",
p.mac[0], p.mac[1], p.mac[2], p.mac[3], p.mac[4], p.mac[5],
p.heard ? "alive" : "SILENT", (unsigned long)(p.age_ms / 1000));
print("[link] firmware %s | key %04X (%s) | Wi-Fi creds %s%s%s",
@@ -360,7 +360,7 @@ bool handleCommand(const char* line) {
else if (!strcmp(b, "install force")) ok = bReady(why, sizeof(why), true) && sendOtaCmd(LINK_OTA_CMD_INSTALL, LINK_OTA_FORCE, why, sizeof(why));
else if (!strcmp(b, "rollback")) ok = bReady(why, sizeof(why), true) && sendOtaCmd(LINK_OTA_CMD_ROLLBACK, 0, why, sizeof(why));
else { help(); return true; }
print(ok ? "[link] command sent to Controller B - watch `link status`" : "[link] refused: %s", why);
print(ok ? "[link] command sent to Inst. Cluster - watch `link status`" : "[link] refused: %s", why);
return true;
}
help();