Skip to content

Commit

Permalink
Adds support for position_precision
Browse files Browse the repository at this point in the history
  • Loading branch information
jp-bennett committed Feb 22, 2024
1 parent 790f100 commit eb2fa72
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/mesh/Channels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 11 additions & 12 deletions src/modules/PositionModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions src/modules/PositionModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class PositionModule : public ProtobufModule<meshtastic_Position>, 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();
Expand Down

0 comments on commit eb2fa72

Please sign in to comment.