forked from esp8266/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.cpp
309 lines (251 loc) · 7.63 KB
/
time.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
/*
* time.c - ESP8266-specific functions for SNTP
* Copyright (c) 2015 Peter Dobler. All rights reserved.
* This file is part of the esp8266 core for Arduino environment.
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* reworked for newlib and lwIP-v2:
* time source is SNTP/settimeofday()
* system time is micros64() / NONOS-SDK's system_get_time()
* synchronisation of the two through timeshift64
*/
#include <Arduino.h>
// https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-time.c
bool getLocalTime(struct tm * info, uint32_t ms)
{
uint32_t start = millis();
time_t now;
while((millis()-start) <= ms) {
time(&now);
localtime_r(&now, info);
if(info->tm_year > (2016 - 1900)){
return true;
}
delay(10);
}
return false;
}
#if !defined(CORE_MOCK)
#include <stdlib.h>
#include <../include/time.h> // See issue #6714
#include <sys/time.h>
extern "C" {
#include <sys/_tz_structs.h>
};
#include <sys/reent.h>
#include <errno.h>
#include <sntp.h> // nonos-sdk
#include <coredecls.h>
#include <Schedule.h>
extern "C" {
#ifndef _TIMEVAL_DEFINED
#define _TIMEVAL_DEFINED
struct timeval {
time_t tv_sec;
suseconds_t tv_usec;
};
#endif
extern char* sntp_asctime(const struct tm *t);
extern struct tm* sntp_localtime(const time_t *clock);
extern uint64_t micros64();
extern void sntp_set_daylight(int daylight);
static uint64_t timeshift64 = 0;
void tune_timeshift64 (uint64_t now_us)
{
timeshift64 = now_us - micros64();
}
static void setServer(int id, const char* name_or_ip)
{
if (name_or_ip)
{
// per current configuration,
// lwIP can receive an IP address or a fqdn
sntp_setservername(id, (char*) name_or_ip);
}
}
int clock_gettime(clockid_t unused, struct timespec *tp)
{
(void) unused;
uint64_t m = micros64();
tp->tv_sec = m / 1000000;
tp->tv_nsec = (m % 1000000) * 1000;
return 0;
}
///////////////////////////////////////////
// backport legacy nonos-sdk Espressif api
bool sntp_set_timezone_in_seconds (int32_t timezone_sec)
{
configTime(timezone_sec, 0, sntp_getservername(0), sntp_getservername(1), sntp_getservername(2));
return true;
}
bool sntp_set_timezone(sint8 timezone_in_hours)
{
return sntp_set_timezone_in_seconds(3600 * ((int)timezone_in_hours));
}
char* sntp_get_real_time(time_t t)
{
return ctime(&t);
}
uint32 sntp_get_current_timestamp()
{
return time(nullptr);
}
// backport legacy nonos-sdk Espressif api
///////////////////////////////////////////
time_t time(time_t * t)
{
time_t currentTime_s = (micros64() + timeshift64) / 1000000ULL;
if (t)
{
*t = currentTime_s;
}
return currentTime_s;
}
int _gettimeofday_r(struct _reent* unused, struct timeval *tp, void *tzp)
{
(void) unused;
(void) tzp;
if (tp)
{
uint64_t currentTime_us = timeshift64 + micros64();
tp->tv_sec = currentTime_us / 1000000ULL;
tp->tv_usec = currentTime_us % 1000000ULL;
}
return 0;
}
}; // extern "C"
void configTime(int timezone_sec, int daylightOffset_sec, const char* server1, const char* server2, const char* server3)
{
sntp_stop();
// There is no way to tell when DST starts or stop with this API
// So DST is always integrated in TZ
// The other API should be preferred
/*** portable version using posix API
(calls sprintf here, then sscanf internally)
int tzs = daylightOffset_sec + timezone_sec;
int tzh = tzs / 3600;
tzs -= tzh * 3600;
int tzm = tzs / 60;
tzs -= tzm * 60;
// man tzset:
char tzstr [64];
snprintf(tzstr, sizeof tzstr, "ESPUSER<%+d:%02d:%02d>", tzh, tzm, tzs);
return configTime(tzstr, server1, server2, server3);
Replaced by light code found from
newlib inspection and internal structure hacking
(no sprintf, no sscanf, -7584 flash bytes):
*** hack starts here: ***/
static char gmt[] = "GMT";
_timezone = timezone_sec + daylightOffset_sec;
_daylight = 0;
_tzname[0] = gmt;
_tzname[1] = gmt;
auto tz = __gettzinfo();
tz->__tznorth = 1;
tz->__tzyear = 0;
for (int i = 0; i < 2; i++)
{
auto tzr = &tz->__tzrule[i];
tzr->ch = 74;
tzr->m = 0;
tzr->n = 0;
tzr->d = 0;
tzr->s = 0;
tzr->change = 0;
tzr->offset = -_timezone;
}
/*** end of hack ***/
// sntp servers
setServer(0, server1);
setServer(1, server2);
setServer(2, server3);
sntp_init();
}
void configTime(int timezone_sec, int daylightOffset_sec, String server1, String server2, String server3)
{
static String servers[3];
servers[0] = std::move(server1);
servers[1] = std::move(server2);
servers[2] = std::move(server3);
configTime(timezone_sec, daylightOffset_sec,
servers[0].length() ? servers[0].c_str() : nullptr,
servers[1].length() ? servers[1].c_str() : nullptr,
servers[2].length() ? servers[2].c_str() : nullptr);
}
void setTZ(const char* tz){
char tzram[strlen_P(tz) + 1];
memcpy_P(tzram, tz, sizeof(tzram));
setenv("TZ", tzram, 1/*overwrite*/);
tzset();
}
void configTime(const char* tz, const char* server1, const char* server2, const char* server3)
{
sntp_stop();
setServer(0, server1);
setServer(1, server2);
setServer(2, server3);
setTZ(tz);
sntp_init();
}
void configTime(const char* tz, String server1, String server2, String server3)
{
static String servers[3];
servers[0] = std::move(server1);
servers[1] = std::move(server2);
servers[2] = std::move(server3);
configTime(tz,
servers[0].length() ? servers[0].c_str() : nullptr,
servers[1].length() ? servers[1].c_str() : nullptr,
servers[2].length() ? servers[2].c_str() : nullptr);
}
static BoolCB _settimeofday_cb;
void settimeofday_cb (const TrivialCB& cb)
{
_settimeofday_cb = [cb](bool sntp) { (void)sntp; cb(); };
}
void settimeofday_cb (BoolCB&& cb)
{
_settimeofday_cb = std::move(cb);
}
void settimeofday_cb (const BoolCB& cb)
{
_settimeofday_cb = cb;
}
extern "C" {
#include <lwip/apps/sntp.h>
int settimeofday(const struct timeval* tv, const struct timezone* tz)
{
bool from_sntp;
if (tz == (struct timezone*)0xFeedC0de)
{
// This special constant is used by lwip2/SNTP calling
// settimeofday(sntp-time, 0xfeedc0de), secretly using the
// obsolete-but-yet-still-there `tz` field.
// It allows to avoid duplicating this function and inform user
// about the source time change.
tz = nullptr;
from_sntp = true;
}
else
from_sntp = false;
if (tz || !tv)
// tz is obsolete (cf. man settimeofday)
return EINVAL;
// reset time subsystem
tune_timeshift64(tv->tv_sec * 1000000ULL + tv->tv_usec);
if (_settimeofday_cb)
schedule_recurrent_function_us([from_sntp](){ _settimeofday_cb(from_sntp); return false; }, 0);
return 0;
}
}; // extern "C"
#endif // !defined(CORE_MOCK)