Skip to content

Commit

Permalink
TimeService: make sure EPOCH_AT_COMPILE_TIME is not stored inside RTC
Browse files Browse the repository at this point in the history
  • Loading branch information
pennam committed Sep 16, 2024
1 parent 8d42032 commit cf78d4e
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/utility/time/TimeService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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 */
Expand Down

0 comments on commit cf78d4e

Please sign in to comment.