diff --git a/src/mesh/MeshService.cpp b/src/mesh/MeshService.cpp index b1d4e687..075c0007 100644 --- a/src/mesh/MeshService.cpp +++ b/src/mesh/MeshService.cpp @@ -199,11 +199,33 @@ void MeshService::sendNetworkPing(NodeNum dest, bool wantReplies) nodeInfoPlugin.sendOurNodeInfo(dest, wantReplies); } + +NodeInfo *MeshService::refreshMyNodeInfo() { + NodeInfo *node = nodeDB.getNode(nodeDB.getNodeNum()); + assert(node); + + // We might not have a position yet for our local node, in that case, at least try to send the time + if(!node->has_position) { + memset(&node->position, 0, sizeof(node->position)); + node->has_position = true; + } + + Position &position = node->position; + + // Update our local node info with our position (even if we don't decide to update anyone else) + position.time = getValidTime(RTCQualityGPS); // This nodedb timestamp might be stale, so update it if our clock is valid. + + position.battery_level = powerStatus->getBatteryChargePercent(); + updateBatteryLevel(position.battery_level); + + return node; +} + int MeshService::onGPSChanged(const meshtastic::GPSStatus *unused) { // Update our local node info with our position (even if we don't decide to update anyone else) - - Position pos = Position_init_default; + NodeInfo *node = refreshMyNodeInfo(); + Position pos = node->position; if (gps->hasLock()) { if (gps->altitude != 0) @@ -214,21 +236,16 @@ int MeshService::onGPSChanged(const meshtastic::GPSStatus *unused) else { // The GPS has lost lock, if we are fixed position we should just keep using // the old position - if(radioConfig.preferences.fixed_position) { - NodeInfo *node = nodeDB.getNode(nodeDB.getNodeNum()); - assert(node); - assert(node->has_position); - pos = node->position; + if(!radioConfig.preferences.fixed_position) { DEBUG_MSG("WARNING: Using fixed position\n"); + } else { + // throw away old position + pos.latitude_i = 0; + pos.longitude_i = 0; + pos.altitude = 0; } } - pos.time = getValidTime(RTCQualityGPS); - - // Include our current battery voltage in our position announcement - pos.battery_level = powerStatus->getBatteryChargePercent(); - updateBatteryLevel(pos.battery_level); - DEBUG_MSG("got gps notify time=%u, lat=%d, bat=%d\n", pos.latitude_i, pos.time, pos.battery_level); // Update our current position in the local DB diff --git a/src/mesh/MeshService.h b/src/mesh/MeshService.h index 9e0048eb..5c30339a 100644 --- a/src/mesh/MeshService.h +++ b/src/mesh/MeshService.h @@ -79,6 +79,9 @@ class MeshService /// cache void sendToMesh(MeshPacket *p); + /// Pull the latest power and time info into my nodeinfo + NodeInfo *refreshMyNodeInfo(); + private: /// Called when our gps position has changed - updates nodedb and sends Location message out into the mesh diff --git a/src/mesh/PhoneAPI.cpp b/src/mesh/PhoneAPI.cpp index 545ef2b9..48898f16 100644 --- a/src/mesh/PhoneAPI.cpp +++ b/src/mesh/PhoneAPI.cpp @@ -134,6 +134,8 @@ size_t PhoneAPI::getFromRadio(uint8_t *buf) fromRadioScratch.which_variant = FromRadio_my_info_tag; fromRadioScratch.variant.my_info = myNodeInfo; state = STATE_SEND_RADIO; + + service.refreshMyNodeInfo(); // Update my NodeInfo because the client will be asking for it soon. break; case STATE_SEND_RADIO: diff --git a/src/plugins/PositionPlugin.cpp b/src/plugins/PositionPlugin.cpp index e0c77895..fe4f5773 100644 --- a/src/plugins/PositionPlugin.cpp +++ b/src/plugins/PositionPlugin.cpp @@ -29,21 +29,10 @@ bool PositionPlugin::handleReceivedProtobuf(const MeshPacket &mp, const Position MeshPacket *PositionPlugin::allocReply() { - NodeInfo *node = nodeDB.getNode(nodeDB.getNodeNum()); - assert(node); - - // We might not have a position yet for our local node, in that case, at least try to send the time - if(!node->has_position) { - memset(&node->position, 0, sizeof(node->position)); - node->has_position = true; - } + NodeInfo *node = service.refreshMyNodeInfo(); // should guarantee there is now a position + assert(node->has_position); - Position &position = node->position; - - // Update our local node info with our position (even if we don't decide to update anyone else) - position.time = getValidTime(RTCQualityGPS); // This nodedb timestamp might be stale, so update it if our clock is valid. - - return allocDataProtobuf(position); + return allocDataProtobuf(node->position); } void PositionPlugin::sendOurPosition(NodeNum dest, bool wantReplies)