Skip to content

Commit beba1b4

Browse files
authored
Added map report precision bounds (#6862)
* Added map report precision bounds * Log warning * Precision range should be 12-15 * Missed commit * Update tests * That method was renamed * Removed now-defunct test call * Remove defunct test
1 parent ba1ef45 commit beba1b4

File tree

2 files changed

+18
-40
lines changed

2 files changed

+18
-40
lines changed

src/mqtt/MQTT.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -773,15 +773,20 @@ void MQTT::perhapsReportToMap()
773773
!(moduleConfig.mqtt.proxy_to_client_enabled || isConnectedDirectly()))
774774
return;
775775

776+
// Coerce the map position precision to be within the valid range
777+
// This removes obtusely large radius and privacy problematic ones from the map
778+
if (map_position_precision < 12 || map_position_precision > 15) {
779+
LOG_WARN("MQTT Map report position precision %u is out of range, using default %u", map_position_precision,
780+
default_map_position_precision);
781+
map_position_precision = default_map_position_precision;
782+
}
783+
776784
if (Throttle::isWithinTimespanMs(last_report_to_map, map_publish_interval_msecs))
777785
return;
778786

779-
if (map_position_precision == 0 || (localPosition.latitude_i == 0 && localPosition.longitude_i == 0)) {
787+
if (localPosition.latitude_i == 0 && localPosition.longitude_i == 0) {
780788
last_report_to_map = millis();
781-
if (map_position_precision == 0)
782-
LOG_WARN("MQTT Map report enabled, but precision is 0");
783-
if (localPosition.latitude_i == 0 && localPosition.longitude_i == 0)
784-
LOG_WARN("MQTT Map report enabled, but no position available");
789+
LOG_WARN("MQTT Map report enabled, but no position available");
785790
return;
786791
}
787792

@@ -805,15 +810,11 @@ void MQTT::perhapsReportToMap()
805810
mapReport.has_opted_report_location = true;
806811

807812
// Set position with precision (same as in PositionModule)
808-
if (map_position_precision < 32 && map_position_precision > 0) {
809-
mapReport.latitude_i = localPosition.latitude_i & (UINT32_MAX << (32 - map_position_precision));
810-
mapReport.longitude_i = localPosition.longitude_i & (UINT32_MAX << (32 - map_position_precision));
811-
mapReport.latitude_i += (1 << (31 - map_position_precision));
812-
mapReport.longitude_i += (1 << (31 - map_position_precision));
813-
} else {
814-
mapReport.latitude_i = localPosition.latitude_i;
815-
mapReport.longitude_i = localPosition.longitude_i;
816-
}
813+
mapReport.latitude_i = localPosition.latitude_i & (UINT32_MAX << (32 - map_position_precision));
814+
mapReport.longitude_i = localPosition.longitude_i & (UINT32_MAX << (32 - map_position_precision));
815+
mapReport.latitude_i += (1 << (31 - map_position_precision));
816+
mapReport.longitude_i += (1 << (31 - map_position_precision));
817+
817818
mapReport.altitude = localPosition.altitude;
818819
mapReport.position_precision = map_position_precision;
819820

test/test_mqtt/MQTT.cpp

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -708,42 +708,21 @@ void test_reportToMapDefaultImprecise(void)
708708
TEST_ASSERT_EQUAL(1, pubsub->published_.size());
709709
const auto &[topic, payload] = pubsub->published_.front();
710710
TEST_ASSERT_EQUAL_STRING("msh/2/map/", topic.c_str());
711-
verifyLatLong(std::get<DecodedServiceEnvelope>(payload), 70123520, 30015488);
712-
}
713-
714-
// Precise location is reported when configured.
715-
void test_reportToMapPrecise(void)
716-
{
717-
unitTest->reportToMap(/*precision=*/32);
718-
719-
TEST_ASSERT_EQUAL(1, pubsub->published_.size());
720-
const auto &[topic, payload] = pubsub->published_.front();
721-
TEST_ASSERT_EQUAL_STRING("msh/2/map/", topic.c_str());
722-
verifyLatLong(std::get<DecodedServiceEnvelope>(payload), localPosition.latitude_i, localPosition.longitude_i);
723711
}
724712

725713
// Location is sent over the phone proxy.
726-
void test_reportToMapPreciseProxied(void)
714+
void test_reportToMapImpreciseProxied(void)
727715
{
728716
moduleConfig.mqtt.proxy_to_client_enabled = true;
729717
MQTTUnitTest::restart();
730718

731-
unitTest->reportToMap(/*precision=*/32);
719+
unitTest->reportToMap(/*precision=*/14);
732720

733721
TEST_ASSERT_EQUAL(1, mockMeshService->messages_.size());
734722
const meshtastic_MqttClientProxyMessage &message = mockMeshService->messages_.front();
735723
TEST_ASSERT_EQUAL_STRING("msh/2/map/", message.topic);
736724
TEST_ASSERT_EQUAL(meshtastic_MqttClientProxyMessage_data_tag, message.which_payload_variant);
737725
const DecodedServiceEnvelope env(message.payload_variant.data.bytes, message.payload_variant.data.size);
738-
verifyLatLong(env, localPosition.latitude_i, localPosition.longitude_i);
739-
}
740-
741-
// No location is reported when the precision is invalid.
742-
void test_reportToMapInvalidPrecision(void)
743-
{
744-
unitTest->reportToMap(/*precision=*/0);
745-
746-
TEST_ASSERT_TRUE(pubsub->published_.empty());
747726
}
748727

749728
// isUsingDefaultServer returns true when using the default server.
@@ -920,9 +899,7 @@ void setup()
920899
RUN_TEST(test_publishTextMessageDirect);
921900
RUN_TEST(test_publishTextMessageWithProxy);
922901
RUN_TEST(test_reportToMapDefaultImprecise);
923-
RUN_TEST(test_reportToMapPrecise);
924-
RUN_TEST(test_reportToMapPreciseProxied);
925-
RUN_TEST(test_reportToMapInvalidPrecision);
902+
RUN_TEST(test_reportToMapImpreciseProxied);
926903
RUN_TEST(test_usingDefaultServer);
927904
RUN_TEST(test_usingDefaultServerWithPort);
928905
RUN_TEST(test_usingDefaultServerWithInvalidPort);

0 commit comments

Comments
 (0)