2020-10-07 23:28:57 +00:00
|
|
|
#include "RTC.h"
|
2020-10-07 23:46:20 +00:00
|
|
|
#include "configuration.h"
|
2020-10-07 23:28:57 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2020-10-07 23:46:20 +00:00
|
|
|
static RTCQuality currentQuality = RTCQualityNone;
|
|
|
|
|
|
|
|
RTCQuality getRTCQuality()
|
|
|
|
{
|
|
|
|
return currentQuality;
|
|
|
|
}
|
2020-10-07 23:28:57 +00:00
|
|
|
|
|
|
|
// stuff that really should be in in the instance instead...
|
|
|
|
static uint32_t
|
|
|
|
timeStartMsec; // Once we have a GPS lock, this is where we hold the initial msec clock that corresponds to that time
|
|
|
|
static uint64_t zeroOffsetSecs; // GPS based time in secs since 1970 - only updated once on initial lock
|
|
|
|
|
|
|
|
void readFromRTC()
|
|
|
|
{
|
|
|
|
struct timeval tv; /* btw settimeofday() is helpfull here too*/
|
|
|
|
|
|
|
|
if (!gettimeofday(&tv, NULL)) {
|
|
|
|
uint32_t now = millis();
|
|
|
|
|
2020-10-07 23:46:20 +00:00
|
|
|
DEBUG_MSG("Read RTC time as %ld (cur millis %u) quality=%d\n", tv.tv_sec, now, currentQuality);
|
2020-10-07 23:28:57 +00:00
|
|
|
timeStartMsec = now;
|
|
|
|
zeroOffsetSecs = tv.tv_sec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If we haven't yet set our RTC this boot, set it from a GPS derived time
|
2020-10-07 23:46:20 +00:00
|
|
|
bool perhapsSetRTC(RTCQuality q, const struct timeval *tv)
|
2020-10-07 23:28:57 +00:00
|
|
|
{
|
2020-10-07 23:46:20 +00:00
|
|
|
if (q > currentQuality) {
|
|
|
|
currentQuality = q;
|
2020-10-07 23:28:57 +00:00
|
|
|
DEBUG_MSG("Setting RTC %ld secs\n", tv->tv_sec);
|
|
|
|
#ifndef NO_ESP32
|
|
|
|
settimeofday(tv, NULL);
|
|
|
|
#else
|
|
|
|
DEBUG_MSG("ERROR TIME SETTING NOT IMPLEMENTED!\n");
|
|
|
|
#endif
|
|
|
|
readFromRTC();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-07 23:46:20 +00:00
|
|
|
bool perhapsSetRTC(RTCQuality q, struct tm &t)
|
2020-10-07 23:28:57 +00:00
|
|
|
{
|
|
|
|
/* Convert to unix time
|
|
|
|
The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970
|
|
|
|
(midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z).
|
|
|
|
*/
|
|
|
|
time_t res = mktime(&t);
|
|
|
|
struct timeval tv;
|
|
|
|
tv.tv_sec = res;
|
|
|
|
tv.tv_usec = 0; // time.centisecond() * (10 / 1000);
|
|
|
|
|
|
|
|
// DEBUG_MSG("Got time from GPS month=%d, year=%d, unixtime=%ld\n", t.tm_mon, t.tm_year, tv.tv_sec);
|
|
|
|
if (t.tm_year < 0 || t.tm_year >= 300) {
|
|
|
|
// DEBUG_MSG("Ignoring invalid GPS month=%d, year=%d, unixtime=%ld\n", t.tm_mon, t.tm_year, tv.tv_sec);
|
|
|
|
return false;
|
|
|
|
} else {
|
2020-10-07 23:46:20 +00:00
|
|
|
return perhapsSetRTC(q, &tv);
|
2020-10-07 23:28:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t getTime()
|
|
|
|
{
|
|
|
|
return ((millis() - timeStartMsec) / 1000) + zeroOffsetSecs;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t getValidTime()
|
|
|
|
{
|
2020-10-07 23:46:20 +00:00
|
|
|
return (currentQuality >= RTCQualityFromNet) ? getTime() : 0;
|
2020-10-07 23:28:57 +00:00
|
|
|
}
|