From d72f67d37bf951402ea328baf13219b4712ad6ef Mon Sep 17 00:00:00 2001 From: Olexandr Tyshchenko Date: Mon, 9 Nov 2026 23:16:29 +0200 Subject: [PATCH] GPS guard: save gps fence in the GPS prefs --- docs/cli_commands.md | 2 +- examples/companion_radio/NodePrefs.h | 10 ++++++++++ src/helpers/CommonCLI.h | 10 ++++++++++ src/helpers/CommonGpsPrefs.cpp | 5 ++++- src/helpers/CommonGpsPrefs.h | 3 +++ 5 files changed, 28 insertions(+), 2 deletions(-) diff --git a/docs/cli_commands.md b/docs/cli_commands.md index babd7102b..74db9e5ce 100644 --- a/docs/cli_commands.md +++ b/docs/cli_commands.md @@ -1087,7 +1087,7 @@ Covers serial NMEA receivers (`MicroNMEALocationProvider`). **Usage:** - `gps guard`: status, e.g. `> ok 7sat time ok +1s`, `> rej region time rej nofix`, or `... ALERT gps-clk +3600s 75m` when a trusted fix has disagreed with the clock by the same offset for `GPS_GUARD_ALERT_MIN` minutes. While the alert is active, the offset in hours is also reported in telemetry on the next free channel. - `gps guard accept`: while an alert is active, confirm that the GPS time is right and apply it to the clock. -- `gps fence`, `gps fence `: fence radius around the configured `lat`/`lon` (`0` = off). Default `GPS_GUARD_FENCE_M` (1000), not saved. +- `gps fence`, `gps fence `: fence radius around the configured `lat`/`lon` (`0` = off). Default `GPS_GUARD_FENCE_M` (1000); a value set here is saved. - `gps ref`: the reference position; follows `set lat`, `set lon` and `gps setloc`. --- diff --git a/examples/companion_radio/NodePrefs.h b/examples/companion_radio/NodePrefs.h index db019370b..7758354a6 100644 --- a/examples/companion_radio/NodePrefs.h +++ b/examples/companion_radio/NodePrefs.h @@ -37,6 +37,7 @@ public: uint8_t gps_enabled = 0; // GPS enabled flag (0=disabled, 1=enabled) uint32_t gps_interval = 0; // GPS read interval in seconds uint32_t gps_period = 0; // periodic acquisition interval in seconds + int32_t gps_fence = -1; // GPS_GUARD fence in metres, 0 = off, -1 = build default uint8_t autoadd_config = 0; // bitmask for auto-add contacts config uint8_t rx_boosted_gain = 0; // SX126x RX boosted gain mode (0=power saving, 1=boosted) uint8_t radio_fem_rxgain = 0; // external LoRa FEM RX gain (LNA) @@ -166,6 +167,9 @@ private: def("int", _parent->gps_interval); // interval in seconds def("period", _parent->gps_period); // periodic acquisition interval in seconds def("adv_loc", _parent->advert_loc_policy); +#ifdef GPS_GUARD + def("fence", _parent->gps_fence); +#endif } public: GPSPrefs(NodePrefs* parent) : _parent(parent) { } @@ -173,6 +177,12 @@ private: void setGpsEnabled(bool enabled) override { _parent->gps_enabled = enabled; markDirty(); } uint32_t getGpsPeriod() const override { return _parent->gps_period; } void setGpsPeriod(uint32_t seconds) override { _parent->gps_period = seconds; markDirty(); } + bool getGuardFence(uint32_t& metres) const override { + if (_parent->gps_fence < 0) return false; + metres = (uint32_t)_parent->gps_fence; + return true; + } + void setGuardFence(uint32_t metres) override { _parent->gps_fence = (int32_t)metres; markDirty(); } }; GPSPrefs gps; diff --git a/src/helpers/CommonCLI.h b/src/helpers/CommonCLI.h index 823f6c0fd..fa39d587c 100644 --- a/src/helpers/CommonCLI.h +++ b/src/helpers/CommonCLI.h @@ -63,6 +63,7 @@ public: uint8_t gps_enabled = 0; uint32_t gps_interval = 0; // in seconds uint32_t gps_period = 0; // periodic acquisition interval in seconds + int32_t gps_fence = -1; // GPS_GUARD fence in metres, 0 = off, -1 = build default uint8_t advert_loc_policy = 0; uint32_t discovery_mod_timestamp = 0; float adc_multiplier = 0; @@ -162,6 +163,9 @@ private: def("int", _parent->gps_interval); // interval in seconds def("period", _parent->gps_period); // periodic acquisition interval in seconds def("adv_loc", _parent->advert_loc_policy); +#ifdef GPS_GUARD + def("fence", _parent->gps_fence); +#endif } public: GPSPrefs(NodePrefs* parent) : _parent(parent) { } @@ -169,6 +173,12 @@ private: void setGpsEnabled(bool enabled) override { _parent->gps_enabled = enabled; markDirty(); } uint32_t getGpsPeriod() const override { return _parent->gps_period; } void setGpsPeriod(uint32_t seconds) override { _parent->gps_period = seconds; markDirty(); } + bool getGuardFence(uint32_t& metres) const override { + if (_parent->gps_fence < 0) return false; + metres = (uint32_t)_parent->gps_fence; + return true; + } + void setGuardFence(uint32_t metres) override { _parent->gps_fence = (int32_t)metres; markDirty(); } bool getGuardReference(double& lat, double& lon) const override { lat = _parent->node_lat; lon = _parent->node_lon; diff --git a/src/helpers/CommonGpsPrefs.cpp b/src/helpers/CommonGpsPrefs.cpp index 8faf01a65..161775595 100644 --- a/src/helpers/CommonGpsPrefs.cpp +++ b/src/helpers/CommonGpsPrefs.cpp @@ -26,6 +26,8 @@ void CommonGpsPrefs::applyGuardReference(SensorManager& sensors) const { GpsGuard* guard = location != nullptr ? location->getGuard() : nullptr; double lat, lon; if (guard != nullptr && getGuardReference(lat, lon)) guard->setReference(lat, lon); + uint32_t fence; + if (guard != nullptr && getGuardFence(fence)) guard->setFence(fence); #endif } @@ -247,10 +249,11 @@ bool CommonGpsPrefs::handleCommand(const char* command, SensorManager& sensors, } else if (memcmp(command, "gps fence ", 10) == 0) { char* end; unsigned long metres = strtoul(command + 10, &end, 10); - if (command[10] == '' || *end != '') { + if (command[10] == '\0' || *end != '\0' || metres > 20000000UL) { // at most half the Earth strcpy(reply, "Error, invalid fence"); } else { guard->setFence((uint32_t)metres); + setGuardFence((uint32_t)metres); // saved by the caller (isDirty) strcpy(reply, "OK"); } } else if (strcmp(command, "gps ref") == 0) { diff --git a/src/helpers/CommonGpsPrefs.h b/src/helpers/CommonGpsPrefs.h index 7a7d57447..3fa1c9b2a 100644 --- a/src/helpers/CommonGpsPrefs.h +++ b/src/helpers/CommonGpsPrefs.h @@ -21,6 +21,9 @@ public: // Reference position for the anti-spoofing guard (GPS_GUARD), e.g. the configured node position. virtual bool getGuardReference(double& lat, double& lon) const { return false; } + // Saved fence radius for the guard; false = not set, use the build default. + virtual bool getGuardFence(uint32_t& metres) const { return false; } + virtual void setGuardFence(uint32_t metres) { } bool setGpsEnabled(SensorManager& sensors, bool enabled); void applyGuardReference(SensorManager& sensors) const;