From 4a70ba1f7a1ac2e1aae74ed28daa3876b5442dba Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Fri, 9 Oct 2020 10:01:13 +0800 Subject: [PATCH] fix nodeinfo stored times (I think) for @lgoix --- src/gps/RTC.cpp | 4 ++-- src/gps/RTC.h | 2 +- src/mesh/MeshService.cpp | 9 +++++---- src/mesh/NodeDB.cpp | 6 +++--- src/mesh/Router.cpp | 5 ++--- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/gps/RTC.cpp b/src/gps/RTC.cpp index ff2e9afa..bc65dea1 100644 --- a/src/gps/RTC.cpp +++ b/src/gps/RTC.cpp @@ -71,7 +71,7 @@ uint32_t getTime() return ((millis() - timeStartMsec) / 1000) + zeroOffsetSecs; } -uint32_t getValidTime() +uint32_t getValidTime(RTCQuality minQuality) { - return (currentQuality >= RTCQualityFromNet) ? getTime() : 0; + return (currentQuality >= minQuality) ? getTime() : 0; } diff --git a/src/gps/RTC.h b/src/gps/RTC.h index e17ca7a9..d6e8703e 100644 --- a/src/gps/RTC.h +++ b/src/gps/RTC.h @@ -25,6 +25,6 @@ bool perhapsSetRTC(RTCQuality q, struct tm &t); uint32_t getTime(); /// Return time since 1970 in secs. If quality is RTCQualityNone return zero -uint32_t getValidTime(); +uint32_t getValidTime(RTCQuality minQuality); void readFromRTC(); \ No newline at end of file diff --git a/src/mesh/MeshService.cpp b/src/mesh/MeshService.cpp index a3242327..399d9690 100644 --- a/src/mesh/MeshService.cpp +++ b/src/mesh/MeshService.cpp @@ -151,7 +151,7 @@ int MeshService::handleFromRadio(const MeshPacket *mp) { powerFSM.trigger(EVENT_RECEIVED_PACKET); // Possibly keep the node from sleeping - // If it is a position packet, perhaps set our clock + // If it is a position packet, perhaps set our clock - this must be before nodeDB.updateFrom handleIncomingPosition(mp); if (mp->which_payload == MeshPacket_decoded_tag && mp->decoded.which_payload == SubPacket_user_tag) { @@ -226,7 +226,7 @@ void MeshService::handleToRadio(MeshPacket &p) if (p.id == 0) p.id = generatePacketId(); // If the phone didn't supply one, then pick one - p.rx_time = getValidTime(); // Record the time the packet arrived from the phone + p.rx_time = getValidTime(RTCQualityFromNet); // Record the time the packet arrived from the phone // (so we update our nodedb for the local node) // Send the packet into the mesh @@ -285,7 +285,7 @@ void MeshService::sendOurPosition(NodeNum dest, bool wantReplies) p->decoded.which_payload = SubPacket_position_tag; p->decoded.position = node->position; p->decoded.want_response = wantReplies; - p->decoded.position.time = getValidTime(); // This nodedb timestamp might be stale, so update it if our clock is valid. + p->decoded.position.time = getValidTime(RTCQualityGPS); // This nodedb timestamp might be stale, so update it if our clock is valid. sendToMesh(p); } @@ -303,9 +303,10 @@ int MeshService::onGPSChanged(const meshtastic::GPSStatus *unused) pos.altitude = gps->altitude; pos.latitude_i = gps->latitude; pos.longitude_i = gps->longitude; - pos.time = getValidTime(); } + pos.time = getValidTime(RTCQualityGPS); + // Include our current battery voltage in our position announcement pos.battery_level = powerStatus->getBatteryChargePercent(); updateBatteryLevel(pos.battery_level); diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 1224ae6d..613665dc 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -415,10 +415,10 @@ void NodeDB::updateFrom(const MeshPacket &mp) switch (p.which_payload) { case SubPacket_position_tag: { - // we carefully preserve the old time, because we always trust our local timestamps more - uint32_t oldtime = info->position.time; + // we always trust our local timestamps more info->position = p.position; - info->position.time = oldtime; + if (mp.rx_time) + info->position.time = mp.rx_time; info->has_position = true; updateGUIforNode = info; notifyObservers(true); // Force an update whether or not our node counts have changed diff --git a/src/mesh/Router.cpp b/src/mesh/Router.cpp index 6a338200..e352a5b2 100644 --- a/src/mesh/Router.cpp +++ b/src/mesh/Router.cpp @@ -89,7 +89,7 @@ MeshPacket *Router::allocForSending() p->to = NODENUM_BROADCAST; p->hop_limit = HOP_RELIABLE; p->id = generatePacketId(); - p->rx_time = getValidTime(); // Just in case we process the packet locally - make sure it has a valid timestamp + p->rx_time = getValidTime(RTCQualityFromNet); // Just in case we process the packet locally - make sure it has a valid timestamp return p; } @@ -198,9 +198,8 @@ NodeNum Router::getNodeNum() */ void Router::handleReceived(MeshPacket *p) { - // FIXME, this class shouldn't EVER need to know about the GPS, move getValidTime() into a non gps dependent function // Also, we should set the time from the ISR and it should have msec level resolution - p->rx_time = getValidTime(); // store the arrival timestamp for the phone + p->rx_time = getValidTime(RTCQualityFromNet); // store the arrival timestamp for the phone // Take those raw bytes and convert them back into a well structured protobuf we can understand if (perhapsDecode(p)) {