forked from arduino-libraries/ArduinoIoTCloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeService.cpp
516 lines (431 loc) · 12.2 KB
/
TimeService.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/*
This file is part of ArduinoIoTCloud.
Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
This software is released under the GNU General Public License version 3,
which covers the main part of arduino-cli.
The terms of this license can be found at:
https://www.gnu.org/licenses/gpl-3.0.en.html
You can be released from the requirements of the above licenses by purchasing
a commercial license. Buying such a license is mandatory if you want to modify or
otherwise use the software for commercial activities involving the Arduino
software without disclosing the source code of your own applications. To purchase
a commercial license, send an email to [email protected].
*/
/**************************************************************************************
* INCLUDE
**************************************************************************************/
#include <AIoTC_Config.h>
#include <time.h>
#include "TimeService.h"
#include "NTPUtils.h"
#include "AIoTC_Const.h"
#ifdef ARDUINO_ARCH_SAMD
#include <RTCZero.h>
#endif
#ifdef ARDUINO_ARCH_MBED
#include <mbed_rtc_time.h>
#endif
#ifdef ARDUINO_ARCH_ESP8266
#include "RTCMillis.h"
#endif
#ifdef ARDUINO_ARCH_RENESAS
#include "RTC.h"
#endif
/**************************************************************************************
* GLOBAL VARIABLES
**************************************************************************************/
#ifdef ARDUINO_ARCH_SAMD
RTCZero rtc;
#endif
#ifdef ARDUINO_ARCH_ESP8266
RTCMillis rtc;
#endif
/**************************************************************************************
* INTERNAL FUNCTION DECLARATION
**************************************************************************************/
time_t cvt_time(char const * time);
#ifdef ARDUINO_ARCH_SAMD
void samd_initRTC();
void samd_setRTC(unsigned long time);
unsigned long samd_getRTC();
#endif
#ifdef ARDUINO_ARCH_MBED
void mbed_initRTC();
void mbed_setRTC(unsigned long time);
unsigned long mbed_getRTC();
#endif
#ifdef ARDUINO_ARCH_ESP32
void esp32_initRTC();
void esp32_setRTC(unsigned long time);
unsigned long esp32_getRTC();
#endif
#ifdef ARDUINO_ARCH_ESP8266
void esp8266_initRTC();
void esp8266_setRTC(unsigned long time);
unsigned long esp8266_getRTC();
#endif
#ifdef ARDUINO_ARCH_RENESAS
void renesas_initRTC();
void renesas_setRTC(unsigned long time);
unsigned long renesas_getRTC();
#endif
/**************************************************************************************
* DEFINES
**************************************************************************************/
#define EPOCH_AT_COMPILE_TIME cvt_time(__DATE__)
/**************************************************************************************
* CONSTANTS
**************************************************************************************/
/* Default NTP synch is scheduled each 24 hours from startup */
static time_t const TIMESERVICE_NTP_SYNC_TIMEOUT_ms = DAYS * 1000;
static time_t const EPOCH = 0;
/**************************************************************************************
* CTOR/DTOR
**************************************************************************************/
TimeServiceClass::TimeServiceClass()
: _con_hdl(nullptr)
, _is_rtc_configured(false)
, _is_tz_configured(false)
, _timezone_offset(24 * 60 * 60)
, _timezone_dst_until(0)
, _last_sync_tick(0)
, _sync_interval_ms(TIMESERVICE_NTP_SYNC_TIMEOUT_ms)
, _sync_func(nullptr)
{
}
/**************************************************************************************
* PUBLIC MEMBER FUNCTIONS
**************************************************************************************/
void TimeServiceClass::begin(ConnectionHandler * con_hdl)
{
_con_hdl = con_hdl;
initRTC();
#ifdef HAS_LORA
setRTC(EPOCH_AT_COMPILE_TIME);
#endif
}
unsigned long TimeServiceClass::getTime()
{
/* Check if it's time to sync */
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) {
sync();
}
/* Read time from RTC */
unsigned long utc = getRTC();
return isTimeValid(utc) ? utc : EPOCH_AT_COMPILE_TIME;
}
void TimeServiceClass::setTime(unsigned long time)
{
setRTC(time);
}
bool TimeServiceClass::sync()
{
_is_rtc_configured = false;
unsigned long utc = EPOCH_AT_COMPILE_TIME;
if(_sync_func) {
utc = _sync_func();
} else {
#ifdef HAS_TCP
utc = getRemoteTime();
#endif
#ifdef HAS_LORA
/* Just keep incrementing stored RTC value*/
utc = getRTC();
#endif
}
if(isTimeValid(utc)) {
DEBUG_DEBUG("TimeServiceClass::%s Drift: %d RTC value: %u", __FUNCTION__, getRTC() - utc, utc);
setRTC(utc);
_last_sync_tick = millis();
_is_rtc_configured = true;
}
return _is_rtc_configured;
}
void TimeServiceClass::setSyncInterval(unsigned long seconds)
{
_sync_interval_ms = seconds * 1000;
}
void TimeServiceClass::setSyncFunction(syncTimeFunctionPtr sync_func)
{
if(sync_func) {
_sync_func = sync_func;
}
}
void TimeServiceClass::setTimeZoneData(long offset, unsigned long dst_until)
{
if(isTimeZoneOffsetValid(offset) && isTimeValid(dst_until)) {
if(_timezone_offset != offset || _timezone_dst_until != dst_until) {
DEBUG_DEBUG("TimeServiceClass::%s offset: %d dst_unitl %u", __FUNCTION__, offset, dst_until);
_timezone_offset = offset;
_timezone_dst_until = dst_until;
_is_tz_configured = true;
}
}
}
unsigned long TimeServiceClass::getLocalTime()
{
unsigned long utc = getTime();
if(_is_tz_configured) {
return utc + _timezone_offset;
} else {
return EPOCH;
}
}
unsigned long TimeServiceClass::getTimeFromString(const String& input)
{
struct tm t =
{
0 /* tm_sec */,
0 /* tm_min */,
0 /* tm_hour */,
0 /* tm_mday */,
0 /* tm_mon */,
0 /* tm_year */,
0 /* tm_wday */,
0 /* tm_yday */,
0 /* tm_isdst */
};
char s_month[16];
int month, day, year, hour, min, sec;
static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
static const int expected_length = 20;
static const int expected_parameters = 6;
if(input == nullptr || input.length() != expected_length) {
DEBUG_ERROR("TimeServiceClass::%s invalid input length", __FUNCTION__);
return 0;
}
int scanned_parameters = sscanf(input.c_str(), "%d %s %d %d:%d:%d", &year, s_month, &day, &hour, &min, &sec);
if(scanned_parameters != expected_parameters) {
DEBUG_ERROR("TimeServiceClass::%s invalid input parameters number", __FUNCTION__);
return 0;
}
char * s_month_position = strstr(month_names, s_month);
if(s_month_position == nullptr || strlen(s_month) != 3) {
DEBUG_ERROR("TimeServiceClass::%s invalid month name, use %s", __FUNCTION__, month_names);
return 0;
}
month = (s_month_position - month_names) / 3;
if(month < 0 || month > 11 || day < 1 || day > 31 || year < 1900 || hour < 0 ||
hour > 24 || min < 0 || min > 60 || sec < 0 || sec > 60) {
DEBUG_ERROR("TimeServiceClass::%s invalid date values", __FUNCTION__);
return 0;
}
t.tm_mon = month;
t.tm_mday = day;
t.tm_year = year - 1900;
t.tm_hour = hour;
t.tm_min = min;
t.tm_sec = sec;
t.tm_isdst = -1;
return mktime(&t);
}
/**************************************************************************************
* PRIVATE MEMBER FUNCTIONS
**************************************************************************************/
#ifdef HAS_TCP
bool TimeServiceClass::connected()
{
if(_con_hdl == nullptr) {
return false;
} else {
return _con_hdl->check() == NetworkConnectionState::CONNECTED;
}
}
unsigned long TimeServiceClass::getRemoteTime()
{
if(connected()) {
/* At first try to obtain a valid time via NTP.
* This is the most reliable time source and it will
* ensure a correct behaviour of the library.
*/
if(_con_hdl->getInterface() != NetworkAdapter::CELL) {
unsigned long const ntp_time = NTPUtils::getTime(_con_hdl->getUDP());
if(isTimeValid(ntp_time)) {
return ntp_time;
}
}
/* As fallback if NTP request fails try to obtain the
* network time using the connection handler.
*/
unsigned long const connection_time = _con_hdl->getTime();
if(isTimeValid(connection_time)) {
return connection_time;
}
}
/* 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;
}
#endif /* HAS_TCP */
bool TimeServiceClass::isTimeValid(unsigned long const time)
{
return (time > EPOCH_AT_COMPILE_TIME);
}
bool TimeServiceClass::isTimeZoneOffsetValid(long const offset)
{
/* UTC offset can go from +14 to -12 hours */
return ((offset < (14 * 60 * 60)) && (offset > (-12 * 60 * 60)));
}
void TimeServiceClass::initRTC()
{
#if defined (ARDUINO_ARCH_SAMD)
samd_initRTC();
#elif defined (ARDUINO_ARCH_MBED)
mbed_initRTC();
#elif defined (ARDUINO_ARCH_ESP32)
esp32_initRTC();
#elif defined (ARDUINO_ARCH_ESP8266)
esp8266_initRTC();
#elif defined (ARDUINO_ARCH_RENESAS)
renesas_initRTC();
#else
#error "RTC not available for this architecture"
#endif
}
void TimeServiceClass::setRTC(unsigned long time)
{
#if defined (ARDUINO_ARCH_SAMD)
samd_setRTC(time);
#elif defined (ARDUINO_ARCH_MBED)
mbed_setRTC(time);
#elif defined (ARDUINO_ARCH_ESP32)
esp32_setRTC(time);
#elif defined (ARDUINO_ARCH_ESP8266)
esp8266_setRTC(time);
#elif defined (ARDUINO_ARCH_RENESAS)
renesas_setRTC(time);
#else
#error "RTC not available for this architecture"
#endif
}
unsigned long TimeServiceClass::getRTC()
{
#if defined (ARDUINO_ARCH_SAMD)
return samd_getRTC();
#elif defined (ARDUINO_ARCH_MBED)
return mbed_getRTC();
#elif defined (ARDUINO_ARCH_ESP32)
return esp32_getRTC();
#elif defined (ARDUINO_ARCH_ESP8266)
return esp8266_getRTC();
#elif defined (ARDUINO_ARCH_RENESAS)
return renesas_getRTC();
#else
#error "RTC not available for this architecture"
#endif
}
/**************************************************************************************
* INTERNAL FUNCTION DEFINITION
**************************************************************************************/
time_t cvt_time(char const * time)
{
static time_t build_time = 0;
if (!build_time) {
char s_month[5];
int month, day, year;
struct tm t =
{
0 /* tm_sec */,
0 /* tm_min */,
0 /* tm_hour */,
0 /* tm_mday */,
0 /* tm_mon */,
0 /* tm_year */,
0 /* tm_wday */,
0 /* tm_yday */,
0 /* tm_isdst */
};
static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
sscanf(time, "%s %d %d", s_month, &day, &year);
month = (strstr(month_names, s_month) - month_names) / 3;
t.tm_mon = month;
t.tm_mday = day;
t.tm_year = year - 1900;
t.tm_isdst = -1;
build_time = mktime(&t);
}
return build_time;
}
#ifdef ARDUINO_ARCH_SAMD
void samd_initRTC()
{
rtc.begin();
}
void samd_setRTC(unsigned long time)
{
rtc.setEpoch(time);
}
unsigned long samd_getRTC()
{
return rtc.getEpoch();
}
#endif
#ifdef ARDUINO_ARCH_MBED
void mbed_initRTC()
{
/* Nothing to do */
}
void mbed_setRTC(unsigned long time)
{
set_time(time);
}
unsigned long mbed_getRTC()
{
return time(NULL);
}
#endif
#ifdef ARDUINO_ARCH_ESP32
void esp32_initRTC()
{
//configTime(0, 0, "time.arduino.cc", "pool.ntp.org", "time.nist.gov");
}
void esp32_setRTC(unsigned long time)
{
const timeval epoch = {(time_t)time, 0};
settimeofday(&epoch, 0);
}
unsigned long esp32_getRTC()
{
return time(NULL);
}
#endif
#ifdef ARDUINO_ARCH_ESP8266
void esp8266_initRTC()
{
rtc.begin();
}
void esp8266_setRTC(unsigned long time)
{
rtc.set(time);
}
unsigned long esp8266_getRTC()
{
return rtc.get();
}
#endif
#ifdef ARDUINO_ARCH_RENESAS
void renesas_initRTC()
{
RTC.begin();
}
void renesas_setRTC(unsigned long time)
{
RTCTime t(time);
RTC.setTime(t);
}
unsigned long renesas_getRTC()
{
RTCTime t;
RTC.getTime(t);
return t.getUnixTime();
}
#endif
/******************************************************************************
* EXTERN DEFINITION
******************************************************************************/
TimeServiceClass TimeService;