feat(clock): a node with no GPS starts at its build epoch, not in 2024

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5 committed 2026-08-09 22:24:59 +02:00
1 parent df66bc8711
commit bbd4fbc358
8 files changed
+248 -8

No files matched your search

+20
View File
@@ -74,6 +74,26 @@ public:
virtual const char* getShutdownReasonString(uint8_t reason) { return "Not available"; }
};
// CUSTOM (TeTeHacko): the time a clock with no other source comes up at.
//
// tools/build_version.py injects FIRMWARE_BUILD_EPOCH = the moment of compile, so
// a freshly flashed board is right to within "time since build" -- seconds on the
// bench -- with no GPS, no I2C RTC, nothing carrying a correct time on air and no
// host attached. Without it the value is upstream's hardcoded 15 May 2024, which
// is where bench-board sat for 811 days and every bench board for 812: the clock had
// simply never been set, because setting it was a manual step.
//
// That is not cosmetic. Mesh.cpp stamps every advert this node transmits from the
// RTC, and a receiver drops a stale timestamp as a replay (BaseChatMesh.cpp), so
// a wrong clock turns into replies that vanish.
//
// The fallback is NOT optional: build_version.py is wired in through
// ${stamped.extra_scripts}, which is nRF52-only and reaches 7 of the 16 envs in
// platformio.local.ini. Upstream envs never define it and must keep compiling.
#ifndef FIRMWARE_BUILD_EPOCH
#define FIRMWARE_BUILD_EPOCH 1715770351 // 15 May 2024, 8:50pm
#endif
/**
* An abstraction of the device's Realtime Clock.
*/
+3 -1
View File
@@ -8,7 +8,9 @@ class VolatileRTCClock : public mesh::RTCClock {
uint64_t accumulator;
unsigned long prev_millis;
public:
VolatileRTCClock() { base_time = 1715770351; accumulator = 0; prev_millis = millis(); } // 15 May 2024, 8:50pm
// FIRMWARE_BUILD_EPOCH = compile time when the build is stamped, else upstream's
// 15 May 2024 literal. See the define in MeshCore.h for why this matters.
VolatileRTCClock() { base_time = FIRMWARE_BUILD_EPOCH; accumulator = 0; prev_millis = millis(); }
uint32_t getCurrentTime() override { return base_time + accumulator/1000; }
void setCurrentTime(uint32_t time) override { base_time = time; accumulator = 0; prev_millis = millis(); }
+26 -3
View File
@@ -10,6 +10,12 @@
#define BRIDGE_MAX_BAUD 115200
#endif
// CUSTOM (TeTeHacko): defined by apps that keep the wall clock in retained RAM
// across a soft reset (examples/simple_repeater/main.cpp). Weak, so every build
// that does not links fine and the pointer is simply NULL. `clkreboot` has to
// clear that record or it would undo itself -- see the call site below.
extern "C" void retainedClockInvalidate() __attribute__((weak));
// Believe it or not, this std C function is busted on some platforms!
static uint32_t _atoi(const char* sp) {
uint32_t n = 0;
@@ -246,8 +252,18 @@ 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. On a volatile clock this write is redundant -- the reboot
// re-runs the constructor, which seeds the same value -- but on a battery
// backed I2C RTC it really does rewrite the chip, so it must not put the
// year 2024 there. This is also the ONLY way to move a clock BACKWARDS:
// `time` and `clock sync` both refuse to, so an over-set clock can only be
// fixed by resetting it here and setting it again.
//
// The retained-RAM copy has to die with it. Otherwise the reboot restores
// the very value being thrown away here, and the command silently does
// nothing on exactly the nodes that keep their clock across a reset.
if (retainedClockInvalidate) retainedClockInvalidate();
getRTCClock()->setCurrentTime(FIRMWARE_BUILD_EPOCH);
_board->reboot(); // doesn't return
} else if (memcmp(command, "advert.zerohop", 14) == 0 && (command[14] == 0 || command[14] == ' ')) {
// send zerohop advert
@@ -274,7 +290,14 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re
} else if (memcmp(command, "clock", 5) == 0) {
uint32_t now = getRTCClock()->getCurrentTime();
DateTime dt = DateTime(now);
sprintf(reply, "%02d:%02d - %d/%d/%d UTC", dt.hour(), dt.minute(), dt.day(), dt.month(), dt.year());
// CUSTOM (TeTeHacko): the epoch goes on the end. The human part is minutes
// only, so a host comparing its own clock against this one cannot see a
// difference smaller than a minute -- meaning it cannot tell a node that is
// 40 s out from one that is exact, and has to leave a >59 s deadband. The
// epoch makes the comparison exact. Appended, so anything that only reads
// or prints the reply is unaffected.
sprintf(reply, "%02d:%02d - %d/%d/%d UTC (epoch %u)", dt.hour(), dt.minute(),
dt.day(), dt.month(), dt.year(), (unsigned) now);
} else if (memcmp(command, "time ", 5) == 0) { // set time (to epoch seconds)
uint32_t secs = _atoi(&command[5]);
uint32_t curr = getRTCClock()->getCurrentTime();
+1 -1
View File
@@ -165,7 +165,7 @@ public:
if (reason == ESP_RST_POWERON) {
// start with some date/time in the recent past
struct timeval tv;
tv.tv_sec = 1715770351; // 15 May 2024, 8:50pm
tv.tv_sec = FIRMWARE_BUILD_EPOCH; // compile time when stamped, else 15 May 2024
tv.tv_usec = 0;
settimeofday(&tv, NULL);
}