Skip to content

Commit 04048b2

Browse files
authored
1 parent 036f44e commit 04048b2

File tree

1 file changed

+52
-30
lines changed

1 file changed

+52
-30
lines changed

cores/esp8266/time.cpp

+52-30
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,23 @@
1414
* License along with this library; if not, write to the Free Software
1515
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1616
*
17+
* reworked for newlib and lwIP-v2:
18+
* time source is SNTP/settimeofday()
19+
* system time is micros64() / NONOS-SDK's system_get_time()
20+
* synchronisation of the two through timeshift64
1721
*/
1822

1923
#include <stdlib.h>
2024
#include <../include/time.h> // See issue #6714
2125
#include <sys/time.h>
2226
#include <sys/reent.h>
23-
#include "sntp.h"
24-
#include "coredecls.h"
27+
#include <errno.h>
2528

26-
#include <Arduino.h> // configTime()
29+
#include <sntp.h> // nonos-sdk
30+
#include <coredecls.h>
31+
#include <Schedule.h>
2732

28-
#include "sntp-lwip2.h"
33+
#include <Arduino.h> // configTime()
2934

3035
extern "C" {
3136

@@ -42,16 +47,11 @@ extern struct tm* sntp_localtime(const time_t *clock);
4247
extern uint64_t micros64();
4348
extern void sntp_set_daylight(int daylight);
4449

45-
// time gap in seconds from 01.01.1900 (NTP time) to 01.01.1970 (UNIX time)
46-
#define DIFF1900TO1970 2208988800UL
47-
48-
bool timeshift64_is_set = false;
4950
static uint64_t timeshift64 = 0;
5051

5152
void tune_timeshift64 (uint64_t now_us)
5253
{
5354
timeshift64 = now_us - micros64();
54-
timeshift64_is_set = true;
5555
}
5656

5757
static void setServer(int id, const char* name_or_ip)
@@ -73,14 +73,8 @@ int clock_gettime(clockid_t unused, struct timespec *tp)
7373
return 0;
7474
}
7575

76-
#if LWIP_VERSION_MAJOR == 1
77-
// hack for espressif time management included in patched lwIP-1.4
78-
#define sntp_real_timestamp sntp_get_current_timestamp()
79-
#endif
80-
81-
#if LWIP_VERSION_MAJOR != 1
82-
83-
// backport Espressif api
76+
///////////////////////////////////////////
77+
// backport legacy nonos-sdk Espressif api
8478

8579
bool sntp_set_timezone_in_seconds (int32_t timezone_sec)
8680
{
@@ -100,18 +94,20 @@ char* sntp_get_real_time(time_t t)
10094

10195
uint32 sntp_get_current_timestamp()
10296
{
103-
return sntp_real_timestamp;
97+
return time(nullptr);
10498
}
10599

106-
#endif
100+
// backport legacy nonos-sdk Espressif api
101+
///////////////////////////////////////////
107102

108103
time_t time(time_t * t)
109104
{
105+
time_t currentTime_s = (micros64() + timeshift64) / 1000000ULL;
110106
if (t)
111107
{
112-
*t = sntp_real_timestamp;
108+
*t = currentTime_s;
113109
}
114-
return sntp_real_timestamp;
110+
return currentTime_s;
115111
}
116112

117113
int _gettimeofday_r(struct _reent* unused, struct timeval *tp, void *tzp)
@@ -120,8 +116,6 @@ int _gettimeofday_r(struct _reent* unused, struct timeval *tp, void *tzp)
120116
(void) tzp;
121117
if (tp)
122118
{
123-
if (!timeshift64_is_set)
124-
tune_timeshift64(sntp_real_timestamp * 1000000ULL);
125119
uint64_t currentTime_us = timeshift64 + micros64();
126120
tp->tv_sec = currentTime_us / 1000000ULL;
127121
tp->tv_usec = currentTime_us % 1000000ULL;
@@ -139,23 +133,19 @@ void configTime(int timezone_sec, int daylightOffset_sec, const char* server1, c
139133

140134
/*** portable version using posix API
141135
(calls sprintf here, then sscanf internally)
142-
143136
int tzs = daylightOffset_sec + timezone_sec;
144137
int tzh = tzs / 3600;
145138
tzs -= tzh * 3600;
146139
int tzm = tzs / 60;
147140
tzs -= tzm * 60;
148-
149141
// man tzset:
150142
char tzstr [64];
151143
snprintf(tzstr, sizeof tzstr, "ESPUSER<%+d:%02d:%02d>", tzh, tzm, tzs);
152144
return configTime(tzstr, server1, server2, server3);
153-
154145
Replaced by light code found from
155146
newlib inspection and internal structure hacking
156147
(no sprintf, no sscanf, -7584 flash bytes):
157-
158-
***/
148+
*** hack starts here: ***/
159149

160150
static char gmt[] = "GMT";
161151

@@ -178,12 +168,12 @@ void configTime(int timezone_sec, int daylightOffset_sec, const char* server1, c
178168
tzr->offset = -_timezone;
179169
}
180170

171+
/*** end of hack ***/
172+
181173
// sntp servers
182174
setServer(0, server1);
183175
setServer(1, server2);
184176
setServer(2, server3);
185-
186-
/*** end of posix replacement ***/
187177
}
188178

189179
void setTZ(const char* tz){
@@ -206,3 +196,35 @@ void configTime(const char* tz, const char* server1, const char* server2, const
206196
sntp_init();
207197
}
208198

199+
static TrivialCB _settimeofday_cb;
200+
201+
void settimeofday_cb (TrivialCB&& cb)
202+
{
203+
_settimeofday_cb = std::move(cb);
204+
}
205+
206+
void settimeofday_cb (const TrivialCB& cb)
207+
{
208+
_settimeofday_cb = cb;
209+
}
210+
211+
extern "C" {
212+
213+
#include <lwip/apps/sntp.h>
214+
215+
int settimeofday(const struct timeval* tv, const struct timezone* tz)
216+
{
217+
if (tz || !tv)
218+
// tz is obsolete (cf. man settimeofday)
219+
return EINVAL;
220+
221+
// reset time subsystem
222+
tune_timeshift64(tv->tv_sec * 1000000ULL + tv->tv_usec);
223+
224+
if (_settimeofday_cb)
225+
schedule_recurrent_function_us([](){ _settimeofday_cb(); return false; }, 0);
226+
227+
return 0;
228+
}
229+
230+
};

0 commit comments

Comments
 (0)