feat(Telemetry,CommunicationRouter): lifetime boot counter + coredump crash detail over MQTT

Investigating the F-051 TelemetryTask crash surfaced two gaps: the
console only ever saw a coarse "Device reset due to fault: PANIC"
alert, none of the serial monitor's task-name/register detail; and
there was no automatic per-boot report a console could use to count
reboots without polling.

Adds Telemetry::getLifetimeBootCount() — a new NVS-backed counter
(namespace "vesper_boot") that increments once per boot, works on
every board variant (SD or not), and is reliable regardless of NTP
sync status. Deliberately kept separate from FirmwareValidator's
existing NVS boot_count (scoped to boots-since-this-OTA-slot-was-
flashed, feeds rollback-safety retry logic — left untouched) and
Telemetry's existing SD-based total_boots (repositioned as history-
only; unreliable as a counter since it's SD/NTP-dependent).

Adds Telemetry::captureCrashReport(), which reads the ESP-IDF
coredump summary (esp_core_dump_get_summary) once at begin(). The
coredump partition and CONFIG_ESP_COREDUMP_ENABLE_TO_FLASH were
already present/enabled in this project's stock min_spiffs.csv and
precompiled Arduino-ESP32 core — no partition table or SDK config
change needed. This is the official "user mode" retrieval API, safe
to call from ordinary application code after boot; no custom panic
hook, nothing runs inside the panic handler itself. Extracts the
faulting task name, PC, and exception cause/vaddr, then erases the
dump so it's reported exactly once.

CommunicationRouter::reportBootReasonOnce() (renamed reportBootOnce())
now always publishes a consolidated "boot_report" event to
status/info on first MQTT connect — boot count, reset reason, free
heap, and crash detail when available — alongside the existing
fault-triggered status/alerts WARNING (unchanged).

lifetime_boot_count also added to telemetry.get_metrics,
system.get_telemetry, and system.get_all for on-demand polling.

Verified: pio run on both vesper-std-v1 and agnus-v1 (no-SD variant).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-16 11:17:41 +03:00
co-authored by Claude Sonnet 5
parent 381465cfe4
commit 829ae23f09
9 changed files with 227 additions and 19 deletions
+45 -1
View File
@@ -55,11 +55,29 @@
#include <Arduino.h>
#include <esp_system.h>
#include <nvs.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "../Logging/Logging.hpp"
#include "../FileManager/FileManager.hpp"
// Coredump summary retrieval (esp_core_dump_get_summary) — populated by the ESP-IDF
// panic handler automatically on every crash (already enabled in this project's SDK
// build; no custom panic hook needed). See CrashReport below.
#include <esp_core_dump.h>
// Most recent crash's coredump summary, captured once at begin() and erased from
// flash immediately after reading so it's reported exactly once. Empty/zeroed
// fields when no coredump was present (e.g. expected reset, or reset predates
// coredump support).
struct CrashReport {
bool available = false;
char taskName[16] = {0}; // esp_core_dump_summary_t::exc_task
uint32_t pc = 0; // Program counter at exception
uint32_t excCause = 0; // Xtensa exception cause code
uint32_t excVaddr = 0; // Faulting virtual address
};
// Maximum number of boot log entries retained on SD (rotating)
static constexpr uint16_t BOOT_LOG_MAX_ENTRIES = 200;
// Runtime flush interval in milliseconds (10 minutes)
@@ -96,6 +114,20 @@ private:
// Reset reason, captured once at begin()
esp_reset_reason_t resetReason = ESP_RST_UNKNOWN;
// Lifetime boot counter — separate from FirmwareValidator's NVS boot_count
// (which tracks boots-since-this-firmware-was-flashed for rollback safety).
// This one is the device's whole-life reboot count, the single source of
// truth for "how many times has this device rebooted, ever." See
// docs/features/feature-catalog.md for why two counters exist.
nvs_handle_t _bootNvsHandle = 0;
uint32_t lifetimeBootCount = 0;
// Most recent crash's coredump summary, if any — read once at begin().
CrashReport crashReport;
void initBootTracking(); // opens NVS namespace, increments+persists lifetimeBootCount
void captureCrashReport(); // reads+erases the coredump summary, if present
public:
// Initialization
void begin();
@@ -136,9 +168,21 @@ public:
uint32_t getCurrentSessionSeconds() const { return (millis() - sessionStartMs) / 1000; }
void flushRuntimeToSD();
// Boot log
// Boot log — rotating SD-side history of recent boots (timestamp + reason).
// NOT the authoritative boot counter (SD-only, silently skipped without NTP
// or on no-SD variants) — use getLifetimeBootCount() for that.
void logBootEvent(uint32_t bootTimestamp);
// Lifetime boot counter — NVS-backed, works on every variant (SD or not),
// reliable regardless of NTP sync status. The single source of truth for
// "how many times has this device rebooted, ever."
uint32_t getLifetimeBootCount() const { return lifetimeBootCount; }
// Most recent crash's coredump summary (task name, PC, exception cause/vaddr),
// if one was captured. available == false when the last reset wasn't a crash,
// or no coredump was found for one that was.
const CrashReport& getCrashReport() const { return crashReport; }
// Reset reason (captured once at begin(), before anything else can crash again)
esp_reset_reason_t getResetReason() const { return resetReason; }
// True if the reset reason indicates a fault (panic, watchdog, brownout) rather