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
+36 -4
View File
@@ -360,20 +360,52 @@ void Telemetry::logBootEvent(uint32_t bootTimestamp) {
entries = doc["boots"].as<JsonArray>();
}
uint32_t bootNum = (uint32_t)(doc["total_boots"] | 0) + 1;
// "boot" is the same lifetime_boot_count reported over MQTT (boot_report,
// telemetry.get_metrics, etc.) — NOT a locally-tallied total_boots. The two
// used to be separate counters that could silently drift apart (this file
// previously only advanced its own counter here, gated on NTP success,
// while the NVS counter increments on every boot regardless of network).
// See docs/features/feature-catalog.md F-053.
JsonObject entry = entries.add<JsonObject>();
entry["ts"] = bootTimestamp;
entry["boot"] = bootNum;
entry["boot"] = lifetimeBootCount;
entry["reason"] = getResetReasonString();
doc["total_boots"] = bootNum;
if (crashReport.available) {
JsonObject crash = entry["crash"].to<JsonObject>();
crash["task"] = crashReport.taskName;
crash["pc"] = crashReport.pc;
crash["exc_cause"] = crashReport.excCause;
crash["exc_vaddr"] = crashReport.excVaddr;
}
if (!fileManager->writeJsonFile("/telemetry_boot_log.json", doc)) {
LOG_WARNING(TAG, "Failed to write boot log");
} else {
LOG_INFO(TAG, "Boot #%u logged at timestamp %u", bootNum, bootTimestamp);
LOG_INFO(TAG, "Boot #%lu logged at timestamp %u", lifetimeBootCount, bootTimestamp);
}
}
bool Telemetry::getBootHistory(JsonDocument& outEntries, uint16_t maxEntries) {
if (!fileManager) return false;
JsonDocument doc;
if (!fileManager->readJsonFile("/telemetry_boot_log.json", doc)) {
return false; // no SD, or file doesn't exist yet (e.g. NTP never synced)
}
JsonArray entries = doc["boots"].as<JsonArray>();
if (entries.isNull()) return false;
JsonArray out = outEntries["boots"].to<JsonArray>();
size_t total = entries.size();
size_t start = (total > maxEntries) ? (total - maxEntries) : 0;
for (size_t i = start; i < total; i++) {
out.add(entries[i]);
}
outEntries["count"] = out.size();
return true;
}
void Telemetry::loadMetrics() {
if (!fileManager) return;