-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathTimeService.cpp
163 lines (130 loc) · 4.52 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
/*
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 "../../ArduinoIoTCloud_Config.h"
#ifndef HAS_LORA
#include "TimeService.h"
#include <time.h>
#include "NTPUtils.h"
/**************************************************************************************
* GLOBAL VARIABLES
**************************************************************************************/
#ifdef ARDUINO_ARCH_SAMD
RTCZero rtc;
#endif
/**************************************************************************************
* INTERNAL FUNCTION DECLARATION
**************************************************************************************/
time_t cvt_time(char const * time);
/**************************************************************************************
* CONSTANTS
**************************************************************************************/
static time_t const EPOCH_AT_COMPILE_TIME = cvt_time(__DATE__);
/**************************************************************************************
* CTOR/DTOR
**************************************************************************************/
TimeService::TimeService()
: _con_hdl(nullptr)
#ifdef ARDUINO_ARCH_SAMD
, _is_rtc_configured(false)
#endif
{
}
/**************************************************************************************
* PUBLIC MEMBER FUNCTIONS
**************************************************************************************/
void TimeService::begin(ConnectionHandler * con_hdl)
{
_con_hdl = con_hdl;
#ifdef ARDUINO_ARCH_SAMD
rtc.begin();
#endif
}
unsigned long TimeService::getTime()
{
#ifdef ARDUINO_ARCH_SAMD
if(!_is_rtc_configured)
{
rtc.setEpoch(getRemoteTime());
_is_rtc_configured = true;
}
return rtc.getEpoch();
#else
return getRemoteTime();
#endif
}
/**************************************************************************************
* PRIVATE MEMBER FUNCTIONS
**************************************************************************************/
unsigned long TimeService::getRemoteTime()
{
if(_con_hdl == nullptr)
return EPOCH_AT_COMPILE_TIME;
/* At first try to see if a valid time can be obtained
* using the network time available via the connection
* handler.
*/
unsigned long const connection_time = _con_hdl->getTime();
if(isTimeValid(connection_time)) {
return connection_time;
}
/* If no valid network time is available try to obtain the
* time via NTP next.
*/
unsigned long const ntp_time = NTPUtils::getTime(_con_hdl->getUDP());
if(isTimeValid(ntp_time)) {
return ntp_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;
}
bool TimeService::isTimeValid(unsigned long const time)
{
return (time >= EPOCH_AT_COMPILE_TIME);
}
/**************************************************************************************
* INTERNAL FUNCTION DEFINITION
**************************************************************************************/
time_t cvt_time(char const * 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;
return mktime(&t);
}
#endif /* #ifndef HAS_LORA */