Skip to content

Commit 8ccaa9e

Browse files
committed
Check if board is connected before trying to get time from network/NTP
1 parent 38e56a6 commit 8ccaa9e

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

src/utility/time/TimeService.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -206,31 +206,28 @@ unsigned long TimeService::getRemoteTime()
206206
#include "../../AIoTC_Config.h"
207207
#ifndef HAS_LORA
208208

209-
if(_con_hdl == nullptr)
210-
return EPOCH_AT_COMPILE_TIME;
211-
212-
/* At first try to see if a valid time can be obtained
213-
* using the network time available via the connection
214-
* handler.
215-
*/
216-
unsigned long const connection_time = _con_hdl->getTime();
217-
if(isTimeValid(connection_time)) {
218-
return connection_time;
219-
}
209+
if(isConnected()) {
210+
/* At first try to see if a valid time can be obtained
211+
* using the network time available via the connection
212+
* handler.
213+
*/
214+
unsigned long const connection_time = _con_hdl->getTime();
215+
if(isTimeValid(connection_time)) {
216+
return connection_time;
217+
}
220218

221219
#ifndef __AVR__
222-
/* If no valid network time is available try to obtain the
223-
* time via NTP next.
224-
*/
225-
if(_con_hdl->getStatus() == NetworkConnectionState::CONNECTED) {
220+
/* If no valid network time is available try to obtain the
221+
* time via NTP next.
222+
*/
226223
unsigned long const ntp_time = NTPUtils::getTime(_con_hdl->getUDP());
227224
if(isTimeValid(ntp_time)) {
228225
return ntp_time;
229226
}
230-
}
231227
#endif
232228

233229
#endif /* ifndef HAS_LORA */
230+
}
234231

235232
/* Return the epoch timestamp at compile time as a last
236233
* line of defense. Otherwise the certificate expiration

0 commit comments

Comments
 (0)