From 92f99a75ddc39b3e740f1fe76161ddfa476eed21 Mon Sep 17 00:00:00 2001 From: Stempit Date: Wed, 11 Nov 2026 01:39:24 +0300 Subject: [PATCH] Use firmware build time as fallback clock floor Devices without an RTC/GPS used to reset to hardcoded past epochs on reboot or clkreboot (15 May 2024 in VolatileRTCClock and CommonCLI clkreboot; 1 Mar 2026 in ESP32RTCClock), breaking advert delivery until the clock caught up or was set manually. Derive the floor from DateTime(__DATE__, __TIME__).unixtime() - 86400 via the already-bundled RTClib (1-day margin so builds in UTC+ timezones stay behind UTC for forward-only clock sync). --- src/helpers/ArduinoHelpers.h | 3 ++- src/helpers/CommonCLI.cpp | 4 ++-- src/helpers/ESP32Board.h | 15 ++++++++++----- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/helpers/ArduinoHelpers.h b/src/helpers/ArduinoHelpers.h index 97596daa31f..77ddfad6c10 100644 --- a/src/helpers/ArduinoHelpers.h +++ b/src/helpers/ArduinoHelpers.h @@ -2,13 +2,14 @@ #include #include +#include class VolatileRTCClock : public mesh::RTCClock { uint32_t base_time; uint64_t accumulator; unsigned long prev_millis; public: - VolatileRTCClock() { base_time = 1715770351; accumulator = 0; prev_millis = millis(); } // 15 May 2024, 8:50pm + VolatileRTCClock() { base_time = DateTime(__DATE__, __TIME__).unixtime() - 86400; accumulator = 0; prev_millis = millis(); } // fallback floor = build time (-1d margin for local build TZ vs UTC) uint32_t getCurrentTime() override { return base_time + accumulator/1000; } void setCurrentTime(uint32_t time) override { base_time = time; accumulator = 0; prev_millis = millis(); } diff --git a/src/helpers/CommonCLI.cpp b/src/helpers/CommonCLI.cpp index 64fed99b34d..4ed18f38dc9 100644 --- a/src/helpers/CommonCLI.cpp +++ b/src/helpers/CommonCLI.cpp @@ -196,8 +196,8 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re } else if (memcmp(command, "reboot", 6) == 0) { _board->reboot(); // doesn't return } else if (memcmp(command, "clkreboot", 9) == 0) { - // Reset clock - getRTCClock()->setCurrentTime(1715770351); // 15 May 2024, 8:50pm + // Reset clock to build-time floor (-1d margin for local build TZ vs UTC) + getRTCClock()->setCurrentTime(DateTime(__DATE__, __TIME__).unixtime() - 86400); _board->reboot(); // doesn't return } else if (memcmp(command, "advert.zerohop", 14) == 0 && (command[14] == 0 || command[14] == ' ')) { // send zerohop advert diff --git a/src/helpers/ESP32Board.h b/src/helpers/ESP32Board.h index a47981255b2..974abcdb55e 100644 --- a/src/helpers/ESP32Board.h +++ b/src/helpers/ESP32Board.h @@ -16,6 +16,7 @@ #include "esp_system.h" #include #include +#include class ESP32Board : public mesh::MainBoard { protected: @@ -212,9 +213,12 @@ class ESP32Board : public mesh::MainBoard { extern uint32_t _rtc_backup_time; extern uint32_t _rtc_backup_magic; #define RTC_BACKUP_MAGIC 0xAA55CC33 -#define RTC_TIME_MIN 1772323200 // 1 Mar 2026 class ESP32RTCClock : public mesh::RTCClock { + static uint32_t minTime() { + static const uint32_t t = DateTime(__DATE__, __TIME__).unixtime() - 86400; // 1-day margin for local build TZ vs UTC + return t; + } public: ESP32RTCClock() { } void begin() { @@ -223,12 +227,13 @@ class ESP32RTCClock : public mesh::RTCClock { return; // ESP-IDF preserves system time across deep sleep } // All other resets (power-on, crash, WDT, brownout) lose system time. - // Restore from RTC backup if valid, otherwise use hardcoded seed. + // Restore from RTC backup if valid, otherwise use build-time floor. + uint32_t min_time = minTime(); struct timeval tv; - if (_rtc_backup_magic == RTC_BACKUP_MAGIC && _rtc_backup_time > RTC_TIME_MIN) { + if (_rtc_backup_magic == RTC_BACKUP_MAGIC && _rtc_backup_time > min_time) { tv.tv_sec = _rtc_backup_time; } else { - tv.tv_sec = RTC_TIME_MIN; + tv.tv_sec = min_time; } tv.tv_usec = 0; settimeofday(&tv, NULL); @@ -249,7 +254,7 @@ class ESP32RTCClock : public mesh::RTCClock { void tick() override { time_t now; time(&now); - if (now > RTC_TIME_MIN && (uint32_t)now != _rtc_backup_time) { + if (now > (time_t)minTime() && (uint32_t)now != _rtc_backup_time) { _rtc_backup_time = (uint32_t)now; _rtc_backup_magic = RTC_BACKUP_MAGIC; }