Merge pull request #3972 from MarkRose/fix/gps-date-range-checks
GpsGuard: range-check the GPS date and time for both NMEA and RAK12500 🤖🤖
This commit is contained in:
commit
7dafdf239a
3 files changed
+47
-11
No files matched your search
@@ -11,6 +11,9 @@
|
||||
// first accepted sync), so a spoofer cannot walk the clock away.
|
||||
// A trusted fix that keeps disagreeing with the clock by the same offset raises an alert;
|
||||
// the operator can then accept that GPS time (acceptAlertedTime()).
|
||||
//
|
||||
// epochFromFields() is always available, with or without GPS_GUARD: the location providers use it
|
||||
// to turn the receiver's date and time into an epoch.
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <MeshCore.h>
|
||||
@@ -106,6 +109,20 @@ class GpsGuard {
|
||||
}
|
||||
|
||||
public:
|
||||
// UTC date and time fields as reported by a receiver -> Unix epoch, or 0 if any field is out of
|
||||
// range. Receivers (and their libraries) hand these over unchecked: MicroNMEA turns any two
|
||||
// characters into a value 0..255, and both RTClib's DateTime and SparkFun's getUnixEpoch() index
|
||||
// their days-per-month tables with the month without a range check.
|
||||
static uint32_t epochFromFields(uint16_t year, uint8_t month, uint8_t day,
|
||||
uint8_t hour, uint8_t minute, uint8_t second) {
|
||||
static const uint8_t days_in_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
||||
if (year < 2020 || year > 2099 || month < 1 || month > 12) return 0;
|
||||
uint8_t mdays = days_in_month[month - 1];
|
||||
if (month == 2 && year % 4 == 0) mdays = 29; // 2020..2099: every fourth year is a leap year
|
||||
if (day < 1 || day > mdays || hour > 23 || minute > 59 || second > 60) return 0; // 60: leap second
|
||||
return DateTime(year, month, day, hour, minute, second).unixtime();
|
||||
}
|
||||
|
||||
// Plausible GPS time window: not before this firmware was built, at most 10 years after.
|
||||
static uint32_t minEpoch() {
|
||||
static const uint32_t min_epoch = DateTime(__DATE__, __TIME__).unixtime() - 86400;
|
||||
|
||||
@@ -5,9 +5,7 @@
|
||||
#include <MicroNMEA.h>
|
||||
#include <RTClib.h>
|
||||
#include <string.h>
|
||||
#ifdef GPS_GUARD
|
||||
#include "GpsGuard.h"
|
||||
#endif
|
||||
#include "GpsGuard.h" // epochFromFields(); the guard itself only with GPS_GUARD
|
||||
|
||||
#ifndef GPS_PERIODIC_ACQUISITION_TIMEOUT_MS
|
||||
#define GPS_PERIODIC_ACQUISITION_TIMEOUT_MS 300000UL
|
||||
@@ -161,7 +159,7 @@ class MicroNMEALocationProvider : public LocationProvider {
|
||||
(millis() - _last_time_sync) > TIME_SYNC_INTERVAL) {
|
||||
_time_sync_needed = true;
|
||||
}
|
||||
if (_time_sync_needed && time_valid > 2 && _clock != nullptr && timeSyncAllowed()) {
|
||||
if (_time_sync_needed && time_valid > 2 && _clock != nullptr && currentEpoch() != 0 && timeSyncAllowed()) {
|
||||
_clock->setCurrentTime(getTimestamp());
|
||||
_time_sync_needed = false;
|
||||
_last_time_sync = millis();
|
||||
@@ -175,6 +173,12 @@ class MicroNMEALocationProvider : public LocationProvider {
|
||||
return _clock == nullptr || !_time_sync_needed || time_valid > 3;
|
||||
}
|
||||
|
||||
// the receiver's current date and time, or 0 if a field is out of range
|
||||
uint32_t currentEpoch() {
|
||||
return GpsGuard::epochFromFields(nmea.getYear(), nmea.getMonth(), nmea.getDay(),
|
||||
nmea.getHour(), nmea.getMinute(), nmea.getSecond());
|
||||
}
|
||||
|
||||
bool timeSyncAllowed() {
|
||||
#ifdef GPS_GUARD
|
||||
return _guard.acceptTime(getTimestamp(), _clock->getCurrentTime());
|
||||
@@ -299,14 +303,14 @@ public:
|
||||
|
||||
// GPS time minus clock time, from a current fix with a settled time
|
||||
bool getClockOffset(long& offset) override {
|
||||
if (_clock == nullptr || !isValid() || time_valid < 3) return false;
|
||||
if (_clock == nullptr || !isValid() || time_valid < 3 || currentEpoch() == 0) return false;
|
||||
offset = (long)getTimestamp() - (long)_clock->getCurrentTime();
|
||||
return true;
|
||||
}
|
||||
|
||||
// write the current GPS time to the clock now (caller has done its own checks)
|
||||
bool applyTimeNow() override {
|
||||
if (_clock == nullptr || !isValid() || time_valid < 3) return false;
|
||||
if (_clock == nullptr || !isValid() || time_valid < 3 || currentEpoch() == 0) return false;
|
||||
_clock->setCurrentTime(getTimestamp());
|
||||
_time_sync_needed = false;
|
||||
_last_time_sync = millis();
|
||||
@@ -319,9 +323,8 @@ public:
|
||||
|
||||
long getTimestamp() override {
|
||||
if (!nmea.isValid()) return _lastFix.timestamp;
|
||||
DateTime dt(nmea.getYear(), nmea.getMonth(), nmea.getDay(),
|
||||
nmea.getHour(), nmea.getMinute(), nmea.getSecond());
|
||||
return dt.unixtime();
|
||||
uint32_t t = currentEpoch();
|
||||
return t != 0 ? t : _lastFix.timestamp;
|
||||
}
|
||||
|
||||
void drain() override { _driver->drain(); }
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "GpsDriver.h"
|
||||
|
||||
#include <SparkFun_u-blox_GNSS_Arduino_Library.h>
|
||||
#include "GpsGuard.h" // epochFromFields()
|
||||
|
||||
// Driver for the RAK12500 (u-blox, I2C). Serial NMEA modules are handled by NmeaGpsDriver.
|
||||
class RakI2cGpsDriver : public GpsDriver {
|
||||
@@ -11,6 +12,7 @@ class RakI2cGpsDriver : public GpsDriver {
|
||||
uint8_t _powerPin = 0;
|
||||
bool _detected = false;
|
||||
bool _started = false;
|
||||
bool _pvtSeen = false; // a NAV-PVT has been received since boot
|
||||
|
||||
bool probeRak12500(uint8_t powerPin) {
|
||||
#if defined(ETHERNET_ENABLED) && defined(RAK_BOARD)
|
||||
@@ -95,10 +97,24 @@ public:
|
||||
|
||||
bool isDetected() const { return _detected; }
|
||||
|
||||
bool hasFix() { return _started && _gnss.getGnssFixOk(8); }
|
||||
bool hasFix() {
|
||||
if (!_started) return false;
|
||||
// The library allocates its NAV-PVT record without clearing it: until the first reply has
|
||||
// arrived, the fix flag and every other field are whatever was on the heap.
|
||||
if (!_pvtSeen) {
|
||||
if (!_gnss.getPVT(8)) return false;
|
||||
_pvtSeen = true;
|
||||
}
|
||||
return _gnss.getGnssFixOk(8);
|
||||
}
|
||||
long getLatitude() { return _gnss.getLatitude(2) / 10; }
|
||||
long getLongitude() { return _gnss.getLongitude(2) / 10; }
|
||||
long getAltitude() { return _gnss.getAltitude(2); }
|
||||
long getSatellites() { return _gnss.getSIV(2); }
|
||||
long getTimestamp() { return _gnss.getUnixEpoch(2); }
|
||||
// Not getUnixEpoch(): it indexes its day tables with the module's year and month unchecked.
|
||||
// 0 (no time; RakLocationProvider then leaves the clock alone) for a date or time out of range.
|
||||
long getTimestamp() {
|
||||
return GpsGuard::epochFromFields(_gnss.getYear(2), _gnss.getMonth(2), _gnss.getDay(2),
|
||||
_gnss.getHour(2), _gnss.getMinute(2), _gnss.getSecond(2));
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user