diff --git a/examples/simple_repeater/MyMesh.h b/examples/simple_repeater/MyMesh.h index 8ed0317e69..9f623337eb 100644 --- a/examples/simple_repeater/MyMesh.h +++ b/examples/simple_repeater/MyMesh.h @@ -157,6 +157,8 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { return _prefs.multi_acks; } + bool shouldSyncClockFromMesh() const override { return true; } + #if ENV_INCLUDE_GPS == 1 void applyGpsPrefs() { sensors.setSettingValue("gps", _prefs.gps_enabled?"1":"0"); @@ -252,4 +254,4 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { #if defined(USE_SX1262) || defined(USE_SX1268) void setRxBoostedGain(bool enable) override; #endif -}; +}; \ No newline at end of file diff --git a/src/Mesh.cpp b/src/Mesh.cpp index 57fee14036..b4acf08dd7 100644 --- a/src/Mesh.cpp +++ b/src/Mesh.cpp @@ -267,6 +267,18 @@ DispatcherAction Mesh::onRecvPacket(Packet* pkt) { } if (is_ok) { MESH_DEBUG_PRINTLN("%s Mesh::onRecvPacket(): valid advertisement received!", getLogDateTime()); + + // sync clock from verified advert if local clock is clearly wrong (for repeaters without hardware RTC) + if (shouldSyncClockFromMesh()) { + uint32_t local_time = _rtc->getCurrentTime(); + if (local_time < 1735689600UL // local clock before Jan 2025 (clearly wrong) + && timestamp > 1735689600UL // advert after Jan 2025 + && timestamp < 2082758400UL) { // advert before Jan 2036 + _rtc->setCurrentTime(timestamp); + MESH_DEBUG_PRINTLN(" clock synced from advert: %lu", (unsigned long)timestamp); + } + } + onAdvertRecv(pkt, id, timestamp, app_data, app_data_len); action = routeRecvPacket(pkt); } else { diff --git a/src/Mesh.h b/src/Mesh.h index f9f8786320..3ac512abe9 100644 --- a/src/Mesh.h +++ b/src/Mesh.h @@ -49,6 +49,9 @@ class Mesh : public Dispatcher { */ virtual bool filterRecvFloodPacket(Packet* packet) { return false; } + // sync clock from verified advert if local clock is clearly wrong + virtual bool shouldSyncClockFromMesh() const { return false; } + /** * \brief Check whether this packet should be forwarded (re-transmitted) or not. * Is sub-classes responsibility to make sure given packet is only transmitted ONCE (by this node) @@ -222,4 +225,4 @@ class Mesh : public Dispatcher { }; -} +} \ No newline at end of file