Files
frame/poc/apps/m1_hardware/main/status_log.cc
T
Lainandlain f5f944bef0 feat(m1): local time display and three-button monitoring (#6)
## Summary

Two M1 firmware enhancements on the Waveshare ESP32-S3-RLCD-4.2:

**Local-time display (UTC+8, fixed offset — China has no DST).**
Internal time stays strictly
UTC (RTC chip, JSONL `utc`, console `--json` keep the `Z`-suffixed
machine format). Only
human-facing surfaces convert via the new
`board::kLocalUtcOffsetSeconds`:

- display RTC line renders `unix + 8*3600` labeled `UTC+8` (split after
the date: measured
montserrat16 widths 157px + 119px fit the 179px status column; the old
one-line layout was
  already 223px and overflowed)
- text `status` and `rtc status` show the labeled UTC+8 line and keep
both the raw unix and a
  UTC line
- `rtc set` now takes UTC+8 local time (validates the local calendar
round-trip, converts
  -8h before `set_rtc_time`); help/usage/README updated

**Three buttons.**

- KEY (GPIO18) unchanged (short <1s / long 1-3s / >=3s portal).
- BOOT (GPIO0, strapping pin — input + pull-up, runtime read only):
mirrors the KEY 30ms
debounce and release bands (short <1s, long >=1s), new
`boot_short_presses` /
  `boot_long_presses`, no portal binding.
- RST is hardwired to chip reset, so it is tracked as a persistent boot
counter: NVS
namespace `frame`, key `rst_cnt`, incremented once per boot after
`nvs_flash_init`, exposed
  as `reset_count`.
- Surfaces: display `KEY:a/b BOOT:c/d` + `RST:e` lines, console text +
`--json`
(`boot_short`, `boot_long`, `reset_count`), JSONL
`boot_short`/`boot_long` only — measured
594B worst observed line vs the 640B buffer (46B headroom, gate was
>=40B); `reset_count`
  is static per boot so it is `--json`-only.

## On-device verification (34/34 deterministic asserts PASS + user
assist)

Evidence:
`.omo/evidence/m1wp/tz-buttons-finish-m1-wifi-provisioning.log`

- Build 0 warnings; clang-format 18.1.8 clean on whole `main/`.
- `rtc status`/text status UTC+8 lines match host `date` +8h (<=3s
skew); `rtc set` with a
  UTC+8 local time round-trips (`1788157541 = 14:25:41 local - 8h`).
- JSONL `utc` unchanged UTC-with-Z, crc32c intact, max line 594B < 640.
- KEY short press: +1 with BOOT unchanged (user-assisted). BOOT short:
+2 (user-assisted).
BOOT long: not physically exercised this session — code path is the
device-proven KEY band
logic (same constants); noted in `.omo/notepads` for the next bench
session.
- RST/reset_count: host RTS-pulse reboot 3->4 with boot banner; `--json`
agrees.
- Regressions: wifi auto-connect CONNECTED, tf_logging true, selftest
`wifi_config: ok` +
  `bad ignored`, `prov status` three-state.

Co-authored-by: ania-lain <[email protected]>
2026-08-31 14:39:04 +08:00

86 lines
3.8 KiB
C++

#include "status_log.hh"
#include <cinttypes>
#include <cstdio>
#include <ctime>
namespace frame::m1 {
namespace {
uint32_t crc32c(const uint8_t* data, std::size_t size) {
uint32_t crc = UINT32_MAX;
for (std::size_t index = 0; index < size; ++index) {
crc ^= data[index];
for (int bit = 0; bit < 8; ++bit) {
const uint32_t mask = 0U - (crc & 1U);
crc = (crc >> 1U) ^ (UINT32_C(0x82F63B78) & mask);
}
}
return ~crc;
}
} // namespace
std::size_t format_status_log(const status_snapshot& status, uint64_t sequence, int64_t uptime_us,
char* output, std::size_t capacity, const char* event,
const char* mode) {
if (output == nullptr || capacity == 0 || event == nullptr || mode == nullptr) {
return 0;
}
char utc[32]{};
if (status.rtc.valid) {
const time_t seconds = static_cast<time_t>(status.rtc.unix_seconds);
struct tm utc_time {};
if (gmtime_r(&seconds, &utc_time) != nullptr) {
const unsigned year = static_cast<unsigned>(utc_time.tm_year + 1900);
std::snprintf(
utc, sizeof(utc), "%04u-%02u-%02uT%02u:%02u:%02uZ", year,
static_cast<unsigned>(utc_time.tm_mon + 1), static_cast<unsigned>(utc_time.tm_mday),
static_cast<unsigned>(utc_time.tm_hour), static_cast<unsigned>(utc_time.tm_min),
static_cast<unsigned>(utc_time.tm_sec));
}
}
const int payload_length = std::snprintf(
output, capacity,
"{\"schema_version\":1,\"sequence\":%" PRIu64 ",\"uptime_us\":%" PRId64
",\"utc_valid\":%s,\"utc\":\"%s\",\"source\":\"m1\",\"event\":\"%s\","
"\"mode\":\"%s\",\"status\":\"%s\",\"device_id\":\"%s\","
"\"app\":\"%s\",\"idf\":\"%s\",\"toolchain\":\"%s\",\"image_sha256\":\"%s\","
"\"partitions\":%s,\"reset_reason\":%" PRIu32 ",\"tf\":\"%s\",\"wifi\":\"%s\","
"\"ap_setup\":%s,"
"\"rtc\":%s,\"shtc3\":%s,\"sensor_valid\":%s,"
"\"temperature_tenths_c\":%d,\"humidity_tenths_percent\":%u,"
"\"key_short\":%" PRIu64 ",\"key_long\":%" PRIu64 ",\"boot_short\":%" PRIu64
",\"boot_long\":%" PRIu64,
sequence, uptime_us, status.rtc.valid ? "true" : "false", utc, event, mode,
hardware_status::health_name(hardware_status::overall_health(status)), status.device_id,
status.app_version, status.idf_version, status.toolchain_version, status.image_sha256,
status.partitions_ok ? "true" : "false", status.reset_reason,
hardware_status::sd_state_name(status.storage),
hardware_status::wifi_state_name(status.wifi), status.ap_active ? "true" : "false",
status.rtc_present ? "true" : "false", status.sensor_present ? "true" : "false",
status.sensor.valid ? "true" : "false",
status.sensor.valid ? static_cast<int>(status.sensor.temperature_tenths_celsius) : 0,
status.sensor.valid ? static_cast<unsigned>(status.sensor.humidity_tenths_percent) : 0U,
status.key_short_presses, status.key_long_presses, status.boot_short_presses,
status.boot_long_presses);
if (payload_length < 0 || static_cast<std::size_t>(payload_length) >= capacity) {
output[0] = '\0';
return 0;
}
const std::size_t payload_size = static_cast<std::size_t>(payload_length);
const uint32_t checksum = crc32c(reinterpret_cast<const uint8_t*>(output), payload_size);
const int suffix_length = std::snprintf(output + payload_size, capacity - payload_size,
",\"crc32c\":\"%08" PRIx32 "\"}\n", checksum);
if (suffix_length < 0 || static_cast<std::size_t>(suffix_length) >= capacity - payload_size) {
output[0] = '\0';
return 0;
}
return payload_size + static_cast<std::size_t>(suffix_length);
}
} // namespace frame::m1