14
14
* License along with this library; if not, write to the Free Software
15
15
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16
16
*
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
17
21
*/
18
22
19
23
#include < stdlib.h>
20
24
#include < ../include/time.h> // See issue #6714
21
25
#include < sys/time.h>
22
26
#include < sys/reent.h>
23
- #include " sntp.h"
24
- #include " coredecls.h"
27
+ #include < errno.h>
25
28
26
- #include < Arduino.h> // configTime()
29
+ #include < sntp.h> // nonos-sdk
30
+ #include < coredecls.h>
31
+ #include < Schedule.h>
27
32
28
- #include " sntp-lwip2.h "
33
+ #include < Arduino.h > // configTime()
29
34
30
35
extern " C" {
31
36
@@ -42,16 +47,11 @@ extern struct tm* sntp_localtime(const time_t *clock);
42
47
extern uint64_t micros64 ();
43
48
extern void sntp_set_daylight (int daylight );
44
49
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 ;
49
50
static uint64_t timeshift64 = 0 ;
50
51
51
52
void tune_timeshift64 (uint64_t now_us)
52
53
{
53
54
timeshift64 = now_us - micros64 ();
54
- timeshift64_is_set = true ;
55
55
}
56
56
57
57
static void setServer (int id, const char * name_or_ip)
@@ -73,7 +73,8 @@ int clock_gettime(clockid_t unused, struct timespec *tp)
73
73
return 0 ;
74
74
}
75
75
76
- // backport Espressif api
76
+ // /////////////////////////////////////////
77
+ // backport legacy nonos-sdk Espressif api
77
78
78
79
bool sntp_set_timezone_in_seconds (int32_t timezone_sec)
79
80
{
@@ -93,16 +94,20 @@ char* sntp_get_real_time(time_t t)
93
94
94
95
uint32 sntp_get_current_timestamp ()
95
96
{
96
- return sntp_real_timestamp ;
97
+ return time ( nullptr ) ;
97
98
}
98
99
100
+ // backport legacy nonos-sdk Espressif api
101
+ // /////////////////////////////////////////
102
+
99
103
time_t time (time_t * t)
100
104
{
105
+ time_t currentTime_s = (micros64 () + timeshift64) / 1000000ULL ;
101
106
if (t)
102
107
{
103
- *t = sntp_real_timestamp ;
108
+ *t = currentTime_s ;
104
109
}
105
- return sntp_real_timestamp ;
110
+ return currentTime_s ;
106
111
}
107
112
108
113
int _gettimeofday_r (struct _reent * unused, struct timeval *tp, void *tzp)
@@ -111,8 +116,6 @@ int _gettimeofday_r(struct _reent* unused, struct timeval *tp, void *tzp)
111
116
(void ) tzp;
112
117
if (tp)
113
118
{
114
- if (!timeshift64_is_set)
115
- tune_timeshift64 (sntp_real_timestamp * 1000000ULL );
116
119
uint64_t currentTime_us = timeshift64 + micros64 ();
117
120
tp->tv_sec = currentTime_us / 1000000ULL ;
118
121
tp->tv_usec = currentTime_us % 1000000ULL ;
@@ -146,7 +149,7 @@ void configTime(int timezone_sec, int daylightOffset_sec, const char* server1, c
146
149
newlib inspection and internal structure hacking
147
150
(no sprintf, no sscanf, -7584 flash bytes):
148
151
149
- ***/
152
+ *** hack starts here: ** * /
150
153
151
154
static char gmt[] = " GMT" ;
152
155
@@ -169,12 +172,12 @@ void configTime(int timezone_sec, int daylightOffset_sec, const char* server1, c
169
172
tzr->offset = -_timezone;
170
173
}
171
174
175
+ /* ** end of hack ***/
176
+
172
177
// sntp servers
173
178
setServer (0 , server1);
174
179
setServer (1 , server2);
175
180
setServer (2 , server3);
176
-
177
- /* ** end of posix replacement ***/
178
181
}
179
182
180
183
void setTZ (const char * tz){
@@ -197,3 +200,35 @@ void configTime(const char* tz, const char* server1, const char* server2, const
197
200
sntp_init ();
198
201
}
199
202
203
+ static TrivialCB _settimeofday_cb;
204
+
205
+ void settimeofday_cb (TrivialCB&& cb)
206
+ {
207
+ _settimeofday_cb = std::move (cb);
208
+ }
209
+
210
+ void settimeofday_cb (const TrivialCB& cb)
211
+ {
212
+ _settimeofday_cb = cb;
213
+ }
214
+
215
+ extern " C" {
216
+
217
+ #include < lwip/apps/sntp.h>
218
+
219
+ int settimeofday (const struct timeval * tv, const struct timezone * tz)
220
+ {
221
+ if (tz || !tv)
222
+ // tz is obsolete (cf. man settimeofday)
223
+ return EINVAL;
224
+
225
+ // reset time subsystem
226
+ tune_timeshift64 (tv->tv_sec * 1000000ULL + tv->tv_usec );
227
+
228
+ if (_settimeofday_cb)
229
+ schedule_recurrent_function_us ([](){ _settimeofday_cb (); return false ; }, 0 );
230
+
231
+ return 0 ;
232
+ }
233
+
234
+ };
0 commit comments