From eb2fa727a7c634109e6d7c27f1d71892f562869e Mon Sep 17 00:00:00 2001 From: Jonathan Bennett Date: Thu, 22 Feb 2024 12:32:01 -0600 Subject: [PATCH] Adds support for position_precision --- src/mesh/Channels.cpp | 1 + src/modules/PositionModule.cpp | 23 +++++++++++------------ src/modules/PositionModule.h | 1 + 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/mesh/Channels.cpp b/src/mesh/Channels.cpp index 80bcc10c6e..fe1041d3de 100644 --- a/src/mesh/Channels.cpp +++ b/src/mesh/Channels.cpp @@ -87,6 +87,7 @@ void Channels::initDefaultChannel(ChannelIndex chIndex) channelSettings.psk.bytes[0] = defaultpskIndex; channelSettings.psk.size = 1; strncpy(channelSettings.name, "", sizeof(channelSettings.name)); + channelSettings.module_settings.position_precision = 32; // default to sending location on the primary channel ch.has_settings = true; ch.role = meshtastic_Channel_Role_PRIMARY; diff --git a/src/modules/PositionModule.cpp b/src/modules/PositionModule.cpp index 32c5a0c411..7ef539b1eb 100644 --- a/src/modules/PositionModule.cpp +++ b/src/modules/PositionModule.cpp @@ -23,6 +23,7 @@ PositionModule::PositionModule() : ProtobufModule("position", meshtastic_PortNum_POSITION_APP, &meshtastic_Position_msg), concurrency::OSThread("PositionModule") { + precision = 0; // safe starting value isPromiscuous = true; // We always want to update our nodedb, even if we are sniffing on others if (config.device.role != meshtastic_Config_DeviceConfig_Role_TRACKER && config.device.role != meshtastic_Config_DeviceConfig_Role_TAK_TRACKER) @@ -83,20 +84,13 @@ bool PositionModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, mes nodeDB.updatePosition(getFrom(&mp), p); - // Only respond to location requests on the channel where we broadcast location. - if (channels.getByIndex(mp.channel).role == meshtastic_Channel_Role_PRIMARY) { - ignoreRequest = false; - } else { - ignoreRequest = true; - } - return false; // Let others look at this message also if they want } meshtastic_MeshPacket *PositionModule::allocReply() { - if (ignoreRequest) { - ignoreRequest = false; // Reset for next request + if (precision == 0) { + LOG_DEBUG("Skipping location send because precision is set to 0!\n"); return nullptr; } @@ -116,8 +110,10 @@ meshtastic_MeshPacket *PositionModule::allocReply() localPosition.seq_number++; // lat/lon are unconditionally included - IF AVAILABLE! - p.latitude_i = localPosition.latitude_i; - p.longitude_i = localPosition.longitude_i; + LOG_DEBUG("Sending location with precision %i\n", precision); + p.latitude_i = localPosition.latitude_i & (INT32_MAX << (32 - precision)); + p.longitude_i = localPosition.longitude_i & (INT32_MAX << (32 - precision)); + p.precision_bits = precision; p.time = localPosition.time; if (pos_flags & meshtastic_Config_PositionConfig_PositionFlags_ALTITUDE) { @@ -213,9 +209,12 @@ void PositionModule::sendOurPosition(NodeNum dest, bool wantReplies, uint8_t cha if (prevPacketId) // if we wrap around to zero, we'll simply fail to cancel in that rare case (no big deal) service.cancelSending(prevPacketId); + // Set's the class precision value for this particular packet + precision = channels.getByIndex(channel).settings.module_settings.position_precision; + meshtastic_MeshPacket *p = allocReply(); if (p == nullptr) { - LOG_WARN("allocReply returned a nullptr\n"); + LOG_DEBUG("allocReply returned a nullptr\n"); return; } diff --git a/src/modules/PositionModule.h b/src/modules/PositionModule.h index bd7a9def4f..fddafef6f7 100644 --- a/src/modules/PositionModule.h +++ b/src/modules/PositionModule.h @@ -50,6 +50,7 @@ class PositionModule : public ProtobufModule, private concu private: struct SmartPosition getDistanceTraveledSinceLastSend(meshtastic_PositionLite currentPosition); meshtastic_MeshPacket *allocAtakPli(); + uint32_t precision; /** Only used in power saving trackers for now */ void clearPosition();