From 3da13dd91af9a90a524f9beee71b898cbaf10045 Mon Sep 17 00:00:00 2001 From: Olexandr Tyshchenko Date: Mon, 9 Nov 2026 23:50:48 +0200 Subject: [PATCH] GPS guard: gps guard on/off, saved in the GPS prefs --- docs/cli_commands.md | 1 + examples/companion_radio/NodePrefs.h | 7 +++++++ src/helpers/CommonCLI.h | 7 +++++++ src/helpers/CommonGpsPrefs.cpp | 7 +++++++ src/helpers/CommonGpsPrefs.h | 3 +++ src/helpers/sensors/GpsGuard.h | 20 ++++++++++++++++++++ 6 files changed, 45 insertions(+) diff --git a/docs/cli_commands.md b/docs/cli_commands.md index 74db9e5c..754f5120 100644 --- a/docs/cli_commands.md +++ b/docs/cli_commands.md @@ -1086,6 +1086,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 on`, `gps guard off`: turn the guard on or off (saved, default on). While off, every fix and GPS time is accepted and `gps guard` shows `off`. - `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); 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 7758354a..6aa3d47c 100644 --- a/examples/companion_radio/NodePrefs.h +++ b/examples/companion_radio/NodePrefs.h @@ -38,6 +38,7 @@ public: 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 gps_guard = 1; // GPS_GUARD on/off 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) @@ -169,6 +170,7 @@ private: def("adv_loc", _parent->advert_loc_policy); #ifdef GPS_GUARD def("fence", _parent->gps_fence); + def("guard", _parent->gps_guard); #endif } public: @@ -183,6 +185,11 @@ private: return true; } void setGuardFence(uint32_t metres) override { _parent->gps_fence = (int32_t)metres; markDirty(); } + bool getGuardEnabled(bool& enabled) const override { + enabled = _parent->gps_guard != 0; + return true; + } + void setGuardEnabled(bool enabled) override { _parent->gps_guard = enabled ? 1 : 0; markDirty(); } }; GPSPrefs gps; diff --git a/src/helpers/CommonCLI.h b/src/helpers/CommonCLI.h index fa39d587..cb85f6f9 100644 --- a/src/helpers/CommonCLI.h +++ b/src/helpers/CommonCLI.h @@ -64,6 +64,7 @@ public: 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 gps_guard = 1; // GPS_GUARD on/off uint8_t advert_loc_policy = 0; uint32_t discovery_mod_timestamp = 0; float adc_multiplier = 0; @@ -165,6 +166,7 @@ private: def("adv_loc", _parent->advert_loc_policy); #ifdef GPS_GUARD def("fence", _parent->gps_fence); + def("guard", _parent->gps_guard); #endif } public: @@ -179,6 +181,11 @@ private: return true; } void setGuardFence(uint32_t metres) override { _parent->gps_fence = (int32_t)metres; markDirty(); } + bool getGuardEnabled(bool& enabled) const override { + enabled = _parent->gps_guard != 0; + return true; + } + void setGuardEnabled(bool enabled) override { _parent->gps_guard = enabled ? 1 : 0; 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 16177559..cdc889d0 100644 --- a/src/helpers/CommonGpsPrefs.cpp +++ b/src/helpers/CommonGpsPrefs.cpp @@ -28,6 +28,8 @@ void CommonGpsPrefs::applyGuardReference(SensorManager& sensors) const { if (guard != nullptr && getGuardReference(lat, lon)) guard->setReference(lat, lon); uint32_t fence; if (guard != nullptr && getGuardFence(fence)) guard->setFence(fence); + bool enabled; + if (guard != nullptr && getGuardEnabled(enabled)) guard->setEnabled(enabled); #endif } @@ -232,6 +234,11 @@ bool CommonGpsPrefs::handleCommand(const char* command, SensorManager& sensors, } else if (strcmp(command, "gps guard") == 0) { reply[0] = '>'; reply[1] = ' '; guard->formatStatus(reply + 2, 158); + } else if (strcmp(command, "gps guard on") == 0 || strcmp(command, "gps guard off") == 0) { + const bool enabled = command[11] == 'n'; + guard->setEnabled(enabled); + setGuardEnabled(enabled); // saved by the caller (isDirty) + strcpy(reply, "OK"); } else if (strcmp(command, "gps guard accept") == 0) { // operator confirms that the alerted GPS time is right: apply it to the clock long offset; diff --git a/src/helpers/CommonGpsPrefs.h b/src/helpers/CommonGpsPrefs.h index 3fa1c9b2..c1e30b8e 100644 --- a/src/helpers/CommonGpsPrefs.h +++ b/src/helpers/CommonGpsPrefs.h @@ -24,6 +24,9 @@ public: // 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) { } + // Saved on/off state of the guard; false = not set, guard on. + virtual bool getGuardEnabled(bool& enabled) const { return false; } + virtual void setGuardEnabled(bool enabled) { } bool setGpsEnabled(SensorManager& sensors, bool enabled); void applyGuardReference(SensorManager& sensors) const; diff --git a/src/helpers/sensors/GpsGuard.h b/src/helpers/sensors/GpsGuard.h index bd26aee0..cf713033 100644 --- a/src/helpers/sensors/GpsGuard.h +++ b/src/helpers/sensors/GpsGuard.h @@ -57,6 +57,7 @@ #endif class GpsGuard { + bool _enabled = true; // off: every fix and GPS time passes, as without GPS_GUARD double _ref_lat = 0, _ref_lon = 0; // 0,0 = no reference position uint32_t _fence_m = GPS_GUARD_FENCE_M; @@ -118,6 +119,19 @@ public: void setFence(uint32_t metres) { _fence_m = metres; } uint32_t getFence() const { return _fence_m; } bool hasTrustedFix() const { return _have_trusted; } + bool isEnabled() const { return _enabled; } + void setEnabled(bool enabled) { + if (enabled == _enabled) return; + _enabled = enabled; + // start over: a re-enabled guard needs fresh stable fixes, and no alert survives + _have_trusted = false; + _stable_count = 0; + _time_accepted = false; + _time_disagree = false; + _time_alert = false; + strcpy(_fix_status, "no fix"); + strcpy(_time_status, "-"); + } bool isTimeAlert() const { return _time_alert; } long getAlertOffset() const { return _time_disagree_offset; } @@ -127,6 +141,7 @@ public: const double lat = lat_e6 / 1000000.0; const double lon = lon_e6 / 1000000.0; const unsigned long now = millis(); + if (!_enabled) return true; const char* why = NULL; double dist = 0; @@ -202,6 +217,7 @@ public: // GPS time is only written to the clock while a trusted fix is current, it is not // older than this firmware, and (once the clock is plausible) it only nudges the clock. bool acceptTime(uint32_t gps_time, uint32_t clock_time) { + if (!_enabled) return true; const char* why = NULL; long step = 0; if (!trustedFixIsCurrent()) { @@ -267,6 +283,10 @@ public: // e.g. "ok 7sat time ok +1s", "rej region time rej nofix", // "ok 9sat time rej step 3600s ALERT gps-clk +3600s 75m" void formatStatus(char* out, size_t out_size) const { + if (!_enabled) { + snprintf(out, out_size, "off"); + return; + } // The time status is only refreshed when a sync is due: without a current trusted fix, // do not keep showing an old "ok" as if GPS time were usable now. const char* time_status = (_time_status[0] == 'o' && !trustedFixIsCurrent()) ? "rej nofix" : _time_status;