GpsGuard: convert the receiver's date and time with range checks
MicroNMEA turns any two characters of a checksum-valid GGA/RMC time or date field into a number, so month, day, hour, minute and second can each be 0..255. getTimestamp() passed them to RTClib's DateTime, whose date2days() walks its 11-entry days-per-month table up to the given month: a read of up to 243 bytes past it. The result was then written to the RTC. GpsGuard only sees the epoch afterwards, and only when GPS_GUARD is enabled. Add GpsGuard::epochFromFields(), which returns 0 for a date or time out of range (year 2020..2099, the day checked against the month). It is static and available with or without GPS_GUARD. The NMEA provider uses it for its timestamp, and doesn't sync the clock, report a clock offset or apply GPS time when the fields are out of range. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bda1c586f1
commit
96db79dbc0
2 files changed
+29
-9
No files matched your search
@@ -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(); }
|
||||
|
||||
Reference in new issue
Block a user