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
+32
View File
@@ -62,6 +62,7 @@ Every feature — user-facing or significant internal mechanism — is listed he
| F-050 | SD log lines prefixed with wall-clock date/time + uptime | v2.1.2 | Stable |
| F-051 | Fix: TelemetryTask/HealthMonitor stack overflow — stack bump + deferred MQTT/SD logging queue | v2.0.3 | Stable |
| F-052 | Consolidated lifetime boot counter + coredump crash detail, published automatically over MQTT | v2.1.3 | Stable |
| F-053 | `telemetry.get_boot_history` command + fixed boot-log counter drift + crash detail in SD history | v2.1.3 | Stable |
---
@@ -762,3 +763,34 @@ The `crash` object is present only when `CrashReport::available` is true (a core
**On-demand access:** `lifetime_boot_count` was also added to `telemetry.get_metrics`, `system.get_telemetry`, and `system.get_all` responses, so a console can poll the same counter without waiting for the next reboot.
Subsystem: `src/Telemetry/Telemetry.hpp`, `src/Telemetry/Telemetry.cpp`, `src/Communication/CommunicationRouter/CommunicationRouter.cpp`, `src/Communication/Handlers/TelemetryHandler/TelemetryHandler.cpp`, `src/Communication/Handlers/SystemHandler/SystemHandler.cpp`
---
### F-053 — `telemetry.get_boot_history` command + fixed boot-log counter drift + crash detail in SD history
**Context — direct follow-up to F-052.** F-052 added an NVS-backed lifetime boot counter and coredump crash capture, reported automatically over MQTT. That work surfaced that the pre-existing SD-based boot history (`Telemetry::logBootEvent()` → `/telemetry_boot_log.json`, written once per boot from `ntpSyncTask` in `main.cpp`) had two problems worth fixing rather than leaving alone:
1. **It was write-only.** Nothing in the firmware ever read `/telemetry_boot_log.json` back — not the firmware itself, not any command handler, not `logs.list`/`logs.download` (which only serve the unrelated day-rotated plain-text `/logs/YYYY-MM-DD.log` files). 200 entries of real boot history were accumulating on every SD-equipped device with no way for a console to ever retrieve them.
2. **Its `"boot"` counter had silently drifted from the new authoritative one.** The file kept its own local tally (`total_boots` in the JSON, incremented only inside `logBootEvent()`) instead of using F-052's new `Telemetry::getLifetimeBootCount()`. Because `logBootEvent()` only runs after a successful NTP sync for that boot (see the NTP-gated gotcha below), a device with intermittent connectivity would have this file's counter fall behind the NVS counter's true value — two numbers that look like they should agree, quietly telling different stories.
**Fix 1 — `logBootEvent()` now uses `lifetimeBootCount` directly** ([Telemetry.cpp](../../src/Telemetry/Telemetry.cpp)) instead of computing its own `doc["total_boots"] + 1`. Every entry's `"boot"` field is now guaranteed consistent with the same counter reported via MQTT `boot_report` and `telemetry.get_metrics`/`system.get_telemetry`/`system.get_all`. The old top-level `"total_boots"` key is no longer written; files from older firmware may still have it lying around — current firmware ignores it, consumers can ignore or discard it.
**Fix 2 — crash detail is now included in each SD history entry** when available, using the same `CrashReport` populated by F-052's `captureCrashReport()`:
```json
{ "ts": 1752600000, "boot": 47, "reason": "PANIC", "crash": { "task": "TelemetryTask", "pc": 1074111832, "exc_cause": 1, "exc_vaddr": 0 } }
```
Same shape as the MQTT `boot_report`'s `crash` object (see F-052) — present only when a coredump was found for that specific boot.
**Fix 3 — new command, `telemetry.get_boot_history`** ([TelemetryHandler.cpp](../../src/Communication/Handlers/TelemetryHandler/TelemetryHandler.cpp)), backed by a new `Telemetry::getBootHistory(JsonDocument&, uint16_t maxEntries)` ([Telemetry.hpp](../../src/Telemetry/Telemetry.hpp)) that reads the file and returns up to N most recent entries. Request takes an optional `{ "limit": N }` (clamped to `[1, 200]`, defaults to all 200). Response: `{ "count": <int>, "boots": [ ...entries, oldest of the selection first... ] }`. Returns a `CommandResult::err` — not a crash or empty success — when there's no SD card, no FileManager, or the file has never been written (expected on `agnus` no-SD variants, or any device that hasn't completed its first NTP sync yet). See [Telemetry's README](../../src/Telemetry/README.md#boot-tracking) for the full request/response contract, including the NTP-gating gotcha (a boot that never reaches NTP sync produces **no entry at all**, not a zeroed one — so this file's entry count can run behind `lifetime_boot_count` on flaky-network devices; that's expected, not a bug).
**Three boot mechanisms remain deliberately separate** (see F-052's table, now joined by this file's corrected role):
| Mechanism | Now used for |
|---|---|
| `Telemetry::getLifetimeBootCount()` (NVS) | The authoritative "how many times, ever" — MQTT `boot_report`, `telemetry.get_metrics`, `system.get_telemetry`, `system.get_all`. |
| `FirmwareValidator::getBootCount()` (NVS, separate namespace) | Unchanged — rollback-safety bookkeeping only, scoped to boots since this OTA slot was flashed. Exposed via `firmware.status`. |
| `Telemetry::logBootEvent()` / `getBootHistory()` (SD) | Timestamped, per-boot history with reason + crash detail — retrievable via `telemetry.get_boot_history`. Its `"boot"` field now just mirrors the NVS counter; it is not an independent count. |
Verified: `pio run` on both `vesper-std-v1` and `agnus-v1` (no-SD variant, where `getBootHistory()` correctly returns `false`/the command returns its error response).
Subsystem: `src/Telemetry/Telemetry.hpp`, `src/Telemetry/Telemetry.cpp`, `src/Communication/Handlers/TelemetryHandler/TelemetryHandler.hpp`, `src/Communication/Handlers/TelemetryHandler/TelemetryHandler.cpp`