From fac98d7e59d5db2a9447f6eb8eb0724e858bf602 Mon Sep 17 00:00:00 2001 From: Kaya <95276965+kytpbs@users.noreply.github.com> Date: Fri, 23 Aug 2024 18:56:17 +0300 Subject: [PATCH 1/4] fix time validity check --- src/utility/time/TimeService.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/utility/time/TimeService.cpp b/src/utility/time/TimeService.cpp index 6cae34b2..922f1b39 100644 --- a/src/utility/time/TimeService.cpp +++ b/src/utility/time/TimeService.cpp @@ -322,7 +322,9 @@ unsigned long TimeServiceClass::getRemoteTime() bool TimeServiceClass::isTimeValid(unsigned long const time) { - return (time > EPOCH_AT_COMPILE_TIME); + // EPOCH_AT_COMPILE_TIME is in local time, + // so we need to subtract the maximum possible timezone offset to make sure we are less then utc time + return (time > (EPOCH_AT_COMPILE_TIME - (/*UTC+14*/ 14 * 60 * 60))); } bool TimeServiceClass::isTimeZoneOffsetValid(long const offset) From 8d4203293e004cd006a0e6372e069192a4d4c653 Mon Sep 17 00:00:00 2001 From: Kaya <95276965+kytpbs@users.noreply.github.com> Date: Mon, 9 Sep 2024 18:19:51 +0300 Subject: [PATCH 2/4] Improve comment Co-authored-by: Mattia Pennasilico --- src/utility/time/TimeService.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/utility/time/TimeService.cpp b/src/utility/time/TimeService.cpp index 922f1b39..687d958a 100644 --- a/src/utility/time/TimeService.cpp +++ b/src/utility/time/TimeService.cpp @@ -322,9 +322,10 @@ unsigned long TimeServiceClass::getRemoteTime() bool TimeServiceClass::isTimeValid(unsigned long const time) { - // EPOCH_AT_COMPILE_TIME is in local time, - // so we need to subtract the maximum possible timezone offset to make sure we are less then utc time - return (time > (EPOCH_AT_COMPILE_TIME - (/*UTC+14*/ 14 * 60 * 60))); + /* EPOCH_AT_COMPILE_TIME is in local time, so we need to subtract the maximum + * possible timezone offset UTC+14 to make sure we are less then UTC time + */ + return (time > (EPOCH_AT_COMPILE_TIME - (14 * 60 * 60))); } bool TimeServiceClass::isTimeZoneOffsetValid(long const offset) From cf78d4eb10b7c0d64867e0e67703b62b1f512c21 Mon Sep 17 00:00:00 2001 From: pennam Date: Wed, 11 Sep 2024 10:22:36 +0200 Subject: [PATCH 3/4] TimeService: make sure EPOCH_AT_COMPILE_TIME is not stored inside RTC --- src/utility/time/TimeService.cpp | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/utility/time/TimeService.cpp b/src/utility/time/TimeService.cpp index 687d958a..021232ba 100644 --- a/src/utility/time/TimeService.cpp +++ b/src/utility/time/TimeService.cpp @@ -144,9 +144,17 @@ unsigned long TimeServiceClass::getTime() sync(); } - /* Read time from RTC */ - unsigned long utc = getRTC(); - return isTimeValid(utc) ? utc : EPOCH_AT_COMPILE_TIME; + /* Use RTC time if has been configured at least once */ + if(_last_sync_tick) { + return getRTC(); + } + + /* Return the epoch timestamp at compile time as a last line of defense + * trying to connect to the server. Otherwise the certificate expiration + * date is wrong and we'll be unable to establish a connection. Schedulers + * won't work correctly using this value. + */ + return EPOCH_AT_COMPILE_TIME; } void TimeServiceClass::setTime(unsigned long time) @@ -158,7 +166,7 @@ bool TimeServiceClass::sync() { _is_rtc_configured = false; - unsigned long utc = EPOCH_AT_COMPILE_TIME; + unsigned long utc = EPOCH; if(_sync_func) { utc = _sync_func(); } else { @@ -310,12 +318,8 @@ unsigned long TimeServiceClass::getRemoteTime() } } - /* Return the epoch timestamp at compile time as a last - * line of defense. Otherwise the certificate expiration - * date is wrong and we'll be unable to establish a connection - * to the server. - */ - return EPOCH_AT_COMPILE_TIME; + /* Return known invalid value because we are not connected */ + return EPOCH; } #endif /* HAS_NOTECARD || HAS_TCP */ From 278f653bafe1e6d387ba22e636729461bb9c5522 Mon Sep 17 00:00:00 2001 From: pennam Date: Wed, 11 Sep 2024 10:22:59 +0200 Subject: [PATCH 4/4] TimeService: improve debug prints and comments --- src/utility/time/TimeService.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/utility/time/TimeService.cpp b/src/utility/time/TimeService.cpp index 021232ba..5dd35e5d 100644 --- a/src/utility/time/TimeService.cpp +++ b/src/utility/time/TimeService.cpp @@ -141,6 +141,7 @@ unsigned long TimeServiceClass::getTime() unsigned long const current_tick = millis(); bool const is_ntp_sync_timeout = (current_tick - _last_sync_tick) > _sync_interval_ms; if(!_is_rtc_configured || is_ntp_sync_timeout) { + /* Try to sync time from NTP or connection handler */ sync(); } @@ -173,13 +174,13 @@ bool TimeServiceClass::sync() #if defined(HAS_NOTECARD) || defined(HAS_TCP) utc = getRemoteTime(); #elif defined(HAS_LORA) - /* Just keep incrementing stored RTC value*/ + /* Just keep incrementing stored RTC value starting from EPOCH_AT_COMPILE_TIME */ utc = getRTC(); #endif } if(isTimeValid(utc)) { - DEBUG_DEBUG("TimeServiceClass::%s Drift: %d RTC value: %u", __FUNCTION__, getRTC() - utc, utc); + DEBUG_DEBUG("TimeServiceClass::%s done. Drift: %d RTC value: %u", __FUNCTION__, getRTC() - utc, utc); setRTC(utc); _last_sync_tick = millis(); _is_rtc_configured = true; @@ -307,6 +308,7 @@ unsigned long TimeServiceClass::getRemoteTime() return ntp_time; } } + DEBUG_WARNING("TimeServiceClass::%s cannot get time from NTP, fallback on connection handler", __FUNCTION__); #endif /* HAS_TCP */ /* As fallback if NTP request fails try to obtain the @@ -316,6 +318,7 @@ unsigned long TimeServiceClass::getRemoteTime() if(isTimeValid(connection_time)) { return connection_time; } + DEBUG_WARNING("TimeServiceClass::%s cannot get time from connection handler", __FUNCTION__); } /* Return known invalid value because we are not connected */