feat(Telemetry): expose SD boot history via telemetry.get_boot_history, fix counter drift

The SD-based boot history (logBootEvent() -> /telemetry_boot_log.json)
was write-only: nothing in the firmware ever read it back, and its
"boot" field was a locally-tallied counter (only advanced on the
NTP-gated write path) that could silently drift from the new
lifetime_boot_count added in the previous commit.

- logBootEvent() now stamps each entry with getLifetimeBootCount()
  directly instead of its own total_boots tally, so the SD history's
  "boot" numbers always agree with what MQTT boot_report and
  telemetry.get_metrics/system.get_telemetry report. The old
  total_boots JSON key is no longer written (old files may still have
  it; current firmware ignores it).
- Each entry now also carries crash detail (task/pc/exc_cause/
  exc_vaddr) when a coredump was captured for that boot, same shape
  as the MQTT boot_report's crash object.
- New Telemetry::getBootHistory() + telemetry.get_boot_history command
  actually exposes the file: optional {"limit": N} (clamped 1-200),
  returns {"count", "boots": [...]}. Returns an error response (not a
  crash or empty success) when there's no SD/FileManager or the file
  doesn't exist yet — expected on agnus (no-SD) variants or before a
  device's first NTP sync.

Full request/response contract documented in Telemetry's README and
feature-catalog.md F-053, written for direct consumption by whoever
builds the console-side retrieval UI.

Verified: pio run on 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:41:17 +03:00
co-authored by Claude Sonnet 5
parent 829ae23f09
commit 957c283788
6 changed files with 156 additions and 6 deletions
+59 -1
View File
@@ -100,12 +100,35 @@ There are three separate, intentionally-distinct boot/crash mechanisms in the fi
|---|---|---|---|---|
| `Telemetry::getLifetimeBootCount()` | NVS, namespace `vesper_boot` | Whole device lifetime | Yes | **The authoritative answer to "how many times has this device rebooted, ever."** Used by `CommunicationRouter::reportBootOnce()` and exposed via `telemetry.get_metrics` / `system.get_telemetry` / `system.get_all`. |
| `FirmwareValidator::getBootCount()` | NVS, namespace `fw_validator` | Boots since this firmware image was flashed (feeds rollback-safety logic) | Yes | Firmware-validation decisions only (`firmware.status`'s `boot_count`). Do not use this as a lifetime counter — it's scoped to the current OTA slot's validation window, not the device's history. |
| `Telemetry::logBootEvent()` → `/telemetry_boot_log.json` | SD card, rotating array (last 200 entries) | Timestamped history of recent boots | **No** — silently produces no entry without an SD card or before NTP sync | Browsing recent boot history with timestamps (e.g. "show me the last 10 reboots and when"). Its own `total_boots` field is **not** a reliable counter — use `getLifetimeBootCount()` instead. |
| `Telemetry::logBootEvent()` → `/telemetry_boot_log.json` | SD card, rotating array (last 200 entries) | Timestamped history of recent boots, retrievable via `telemetry.get_boot_history` | **No** — silently produces no entry without an SD card or before NTP sync | Browsing recent boot history with timestamps and (when available) crash detail, e.g. "show me the last 10 reboots, when, and why." Each entry's `"boot"` field is `getLifetimeBootCount()`'s value at the time — **not** a separately-tallied counter (see below). |
**Coredump-based crash detail.** `captureCrashReport()` runs once at `begin()`, right after `initBootTracking()`. It calls `esp_core_dump_image_check()` — if the last reset left a coredump in the `coredump` flash partition (written automatically by the ESP-IDF panic handler; this is stock ESP-IDF behavior already enabled in this project, not something this firmware added), it reads the summary via `esp_core_dump_get_summary()` into `CrashReport` (faulting task name, program counter, Xtensa exception cause and faulting virtual address — the same information the serial monitor prints as a register dump at the moment of the crash, but retrievable in code after reboot). The dump is erased from flash immediately after reading (`esp_core_dump_image_erase()`) so it's reported exactly once, not re-sent on every subsequent boot.
This required no custom panic hook — `esp_core_dump_get_summary()` is a documented, "user mode" API safe to call from ordinary application code after boot; nothing runs inside the panic handler itself.
**Boot log entry shape.** Each entry `logBootEvent()` appends to `/telemetry_boot_log.json`'s `"boots"` array:
```json
{
"ts": 1752600000,
"boot": 47,
"reason": "PANIC",
"crash": {
"task": "TelemetryTask",
"pc": 1074111832,
"exc_cause": 1,
"exc_vaddr": 0
}
}
```
- `ts` — Unix timestamp (seconds), from `Timekeeper::getTime()` at the point `logBootEvent()` ran (after NTP sync completed for this boot — see the gotcha below).
- `boot` — `getLifetimeBootCount()`'s value for this boot. Fixed as of this firmware version (F-053) — previously this field was a locally-tallied counter scoped to this file alone (only incremented on the NTP-gated write path), which could silently drift from the NVS-backed `lifetime_boot_count` reported elsewhere (MQTT `boot_report`, `telemetry.get_metrics`). Files written by firmware predating F-053 may contain a `"total_boots"` top-level field alongside `"boots"` — that field is no longer written or read by current firmware and can be ignored/removed by consumers.
- `reason` — same reset-reason string as `getResetReasonString()` / the MQTT `boot_report`'s `reset_reason`.
- `crash` — present only when a coredump was found for the crash that produced this boot (same shape as `boot_report`'s `crash` object over MQTT — see [CommunicationRouter](../Communication/CommunicationRouter/README.md)). Omitted on a clean/expected reset or a fault reset with no coredump (e.g. brownout).
**Gotcha: entries are NTP-gated.** `logBootEvent()` is only called from `ntpSyncTask` in `main.cpp`, after a successful NTP sync for that boot (see `main.cpp`'s `ntpSyncTask`). A boot that never gets network/NTP (offline device, WiFi failure, AP-mode fallback) produces **no entry at all** for that boot — not an entry with a zeroed timestamp. This means the history can have gaps, and the entry count in the file will be less than `lifetime_boot_count` on a device with spotty connectivity. `lifetime_boot_count` (NVS) is unaffected by this — it always increments, network or not.
**Remote commands (via TelemetryHandler).** The following v2 commands are handled by `TelemetryHandler` and routed through `CommunicationRouter`:
| Command | Description |
@@ -113,10 +136,45 @@ This required no custom panic hook — `esp_core_dump_get_summary()` is a docume
| `telemetry.get_strikes` | Returns strike counts for all 16 bells |
| `telemetry.reset_strikes` | Resets all counters to 0 (all-at-once; per-bell reset not yet implemented) |
| `telemetry.get_loads` | Returns current load values, `cooling_active` flag, and list of overloaded bell indices |
| `telemetry.get_boot_history` | Returns recent entries from `/telemetry_boot_log.json` — see below |
**`telemetry.get_boot_history` in detail:**
Request contents (all optional):
```json
{ "limit": 20 }
```
- `limit` — how many of the most recent entries to return, oldest-of-the-selection-first (i.e. still chronological order, just truncated from the front). Omit for the full stored history (up to 200 entries). Clamped server-side to `[1, 200]` — a `0` or missing value is treated as "no limit" (200), and anything above 200 is capped at 200.
Success response:
```json
{
"status": "SUCCESS",
"type": "telemetry.get_boot_history",
"data": {
"count": 3,
"boots": [
{ "ts": 1752599000, "boot": 45, "reason": "POWERON" },
{ "ts": 1752599800, "boot": 46, "reason": "SOFTWARE" },
{ "ts": 1752600000, "boot": 47, "reason": "PANIC", "crash": { "task": "TelemetryTask", "pc": 1074111832, "exc_cause": 1, "exc_vaddr": 0 } }
]
}
}
```
- `count` — number of entries in this response (i.e. `boots.length`), not the total ever recorded.
- `boots` — array of entries in the shape documented above, oldest first, most recent last.
Error response — returned when there's no FileManager reference, no SD card present/initialized, or the file has never been written (e.g. this device has never completed an NTP sync):
```json
{ "status": "ERROR", "type": "telemetry.get_boot_history", "message": "No boot history available (no SD card, or no boot has completed NTP sync yet)" }
```
A console should treat this error as "no history yet," not as a fault — it's an expected, normal response for an `agnus` (no-SD) device, or any device on its very first boot before NTP has synced.
Example requests:
```json
{ "v": 2, "cmd": "telemetry.get_strikes", "contents": {} }
{ "v": 2, "cmd": "telemetry.get_loads", "contents": {} }
{ "v": 2, "cmd": "telemetry.reset_strikes", "contents": {} }
{ "v": 2, "cmd": "telemetry.get_boot_history", "contents": {} }
{ "v": 2, "cmd": "telemetry.get_boot_history", "contents": { "limit": 20 } }
```