From e10aa5442f54d39cc4051da8d171540297189266 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Sun, 21 Apr 2024 07:49:01 -0500 Subject: [PATCH 1/3] Also refresh timestamp for "timeonly" fixed position nodes --- src/mesh/NodeDB.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/mesh/NodeDB.h b/src/mesh/NodeDB.h index 57040fbd64..8e3784e582 100644 --- a/src/mesh/NodeDB.h +++ b/src/mesh/NodeDB.h @@ -150,12 +150,13 @@ class NodeDB void setLocalPosition(meshtastic_Position position, bool timeOnly = false) { if (timeOnly) { - LOG_DEBUG("Setting local position time only: time=%i\n", position.time); + LOG_DEBUG("Setting local position time only: time=%i timestamp=%i\n", position.time, position.timestamp); localPosition.time = position.time; + localPosition.timestamp = position.timestamp > 0 ? position.timestamp : position.time; return; } - LOG_DEBUG("Setting local position: latitude=%i, longitude=%i, time=%i\n", position.latitude_i, position.longitude_i, - position.time); + LOG_DEBUG("Setting local position: latitude=%i, longitude=%i, time=%i, timeestamp=%i\n", position.latitude_i, + position.longitude_i, position.time, position.timestamp); localPosition = position; } From 94ebf78a33e800a3a3cc7831172f088d6ee17668 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Sun, 21 Apr 2024 08:47:37 -0500 Subject: [PATCH 2/3] Added more clear RTC quality handling --- src/gps/RTC.cpp | 24 ++++++++++++++++++++++-- src/gps/RTC.h | 3 +++ src/modules/PositionModule.cpp | 18 +++++++++++------- src/modules/PositionModule.h | 1 + 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/gps/RTC.cpp b/src/gps/RTC.cpp index 26af7cac21..b5161c016f 100644 --- a/src/gps/RTC.cpp +++ b/src/gps/RTC.cpp @@ -104,13 +104,15 @@ bool perhapsSetRTC(RTCQuality q, const struct timeval *tv) bool shouldSet; if (q > currentQuality) { shouldSet = true; - LOG_DEBUG("Upgrading time to quality %d\n", q); + LOG_DEBUG("Upgrading time to quality %s\n", RtcName(q)); } else if (q == RTCQualityGPS && (now - lastSetMsec) > (12 * 60 * 60 * 1000UL)) { // Every 12 hrs we will slam in a new GPS time, to correct for local RTC clock drift shouldSet = true; LOG_DEBUG("Reapplying external time to correct clock drift %ld secs\n", tv->tv_sec); - } else + } else { shouldSet = false; + LOG_DEBUG("Current RTC quality: %s. Ignoring time of RTC quality of %s\n", RtcName(currentQuality), RtcName(q)); + } if (shouldSet) { currentQuality = q; @@ -162,6 +164,24 @@ bool perhapsSetRTC(RTCQuality q, const struct timeval *tv) } } +const char *RtcName(RTCQuality quality) +{ + switch (quality) { + case RTCQualityNone: + return "None"; + case RTCQualityDevice: + return "Device"; + case RTCQualityFromNet: + return "Net"; + case RTCQualityNTP: + return "NTP"; + case RTCQualityGPS: + return "GPS"; + default: + return "Unknown"; + } +} + /** * Sets the RTC time if the provided time is of higher quality than the current RTC time. * diff --git a/src/gps/RTC.h b/src/gps/RTC.h index 0561819bd9..1d609f136a 100644 --- a/src/gps/RTC.h +++ b/src/gps/RTC.h @@ -28,6 +28,9 @@ RTCQuality getRTCQuality(); bool perhapsSetRTC(RTCQuality q, const struct timeval *tv); bool perhapsSetRTC(RTCQuality q, struct tm &t); +/// Return a string name for the quality +const char *RtcName(RTCQuality quality); + /// Return time since 1970 in secs. While quality is RTCQualityNone we will be returning time based at zero uint32_t getTime(bool local = false); diff --git a/src/modules/PositionModule.cpp b/src/modules/PositionModule.cpp index 250daec578..658b8b5a76 100644 --- a/src/modules/PositionModule.cpp +++ b/src/modules/PositionModule.cpp @@ -71,14 +71,8 @@ bool PositionModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, mes p.time); if (p.time && channels.getByIndex(mp.channel).role == meshtastic_Channel_Role_PRIMARY) { - struct timeval tv; - uint32_t secs = p.time; - - tv.tv_sec = secs; - tv.tv_usec = 0; - // Set from phone RTC Quality to RTCQualityNTP since it should be approximately so - perhapsSetRTC(isLocal ? RTCQualityNTP : RTCQualityFromNet, &tv); + trySetRtc(p, isLocal); } nodeDB->updatePosition(getFrom(&mp), p); @@ -93,6 +87,16 @@ bool PositionModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, mes return false; // Let others look at this message also if they want } +void PositionModule::trySetRtc(meshtastic_Position p, bool isLocal) +{ + struct timeval tv; + uint32_t secs = p.time; + + tv.tv_sec = secs; + tv.tv_usec = 0; + perhapsSetRTC(isLocal ? RTCQualityNTP : RTCQualityFromNet, &tv); +} + meshtastic_MeshPacket *PositionModule::allocReply() { if (precision == 0) { diff --git a/src/modules/PositionModule.h b/src/modules/PositionModule.h index 0e24e3a9eb..89ff50c644 100644 --- a/src/modules/PositionModule.h +++ b/src/modules/PositionModule.h @@ -52,6 +52,7 @@ class PositionModule : public ProtobufModule, private concu private: struct SmartPosition getDistanceTraveledSinceLastSend(meshtastic_PositionLite currentPosition); meshtastic_MeshPacket *allocAtakPli(); + void trySetRtc(meshtastic_Position p, bool isLocal); uint32_t precision; void sendLostAndFoundText(); From 95f21df49e72f617268568f74d6a164185049ef5 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Sun, 21 Apr 2024 08:55:38 -0500 Subject: [PATCH 3/3] Fix clock drift from Phone GPS / NTP too --- src/gps/RTC.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/gps/RTC.cpp b/src/gps/RTC.cpp index b5161c016f..85931900f6 100644 --- a/src/gps/RTC.cpp +++ b/src/gps/RTC.cpp @@ -105,8 +105,8 @@ bool perhapsSetRTC(RTCQuality q, const struct timeval *tv) if (q > currentQuality) { shouldSet = true; LOG_DEBUG("Upgrading time to quality %s\n", RtcName(q)); - } else if (q == RTCQualityGPS && (now - lastSetMsec) > (12 * 60 * 60 * 1000UL)) { - // Every 12 hrs we will slam in a new GPS time, to correct for local RTC clock drift + } else if (q >= RTCQualityNTP && (now - lastSetMsec) > (12 * 60 * 60 * 1000UL)) { + // Every 12 hrs we will slam in a new GPS or Phone GPS / NTP time, to correct for local RTC clock drift shouldSet = true; LOG_DEBUG("Reapplying external time to correct clock drift %ld secs\n", tv->tv_sec); } else {