Select one of the symbols to view example projects that use it.
 
Outline
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
#include "esp_log.h"
#include "esp_sntp.h"
#include "lwip/apps/sntp.h"
#include "lwip/priv/tcpip_priv.h"
#include "esp_macros.h"
TAG
sntp_sync_mode
sntp_sync_status
time_sync_notification_cb
s_sync_interval
sntp_set_sync_status(sntp_sync_status_t)
sntp_sync_time(struct timeval *)
sntp_set_sync_mode(sntp_sync_mode_t)
sntp_get_sync_mode()
sntp_set_time_sync_notification_cb(sntp_sync_time_cb_t)
sntp_get_sync_status()
sntp_set_sync_interval(uint32_t)
sntp_get_sync_interval()
sntp_do_restart(void *)
sntp_restart()
sntp_set_system_time(uint32_t, uint32_t)
sntp_get_system_time(uint32_t *, uint32_t *)
do_setoperatingmode(void *)
esp_sntp_setoperatingmode(esp_sntp_operatingmode_t)
do_init(void *)
esp_sntp_init()
do_stop(void *)
esp_sntp_stop()
tcpip_setserver
do_setserver(struct tcpip_api_call_data *)
esp_sntp_setserver(u8_t, const ip_addr_t *)
tcpip_setservername
do_setservername(struct tcpip_api_call_data *)
esp_sntp_setservername(u8_t, const char *)
esp_sntp_getservername(u8_t)
esp_sntp_getserver(u8_t)
esp_sntp_getreachability(uint8_t)
esp_sntp_getoperatingmode()
esp_sntp_enabled()
Files
loading...
SourceVuESP-IDF Framework and ExampleslwIPapps/sntp/sntp.c
 
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 *//* ... */ #include <stdlib.h> #include <time.h> #include <unistd.h> #include <sys/time.h> #include "esp_log.h" #include "esp_sntp.h"6 includes // Remove compat macro and include lwip API #undef SNTP_OPMODE_POLL #include "lwip/apps/sntp.h" #include "lwip/priv/tcpip_priv.h" #include "esp_macros.h" static const char *TAG = "sntp"; ESP_STATIC_ASSERT(SNTP_OPMODE_POLL == ESP_SNTP_OPMODE_POLL, "SNTP mode in lwip doesn't match the IDF enum. Please make sure lwIP version is correct"); ESP_STATIC_ASSERT(SNTP_OPMODE_LISTENONLY == ESP_SNTP_OPMODE_LISTENONLY, "SNTP mode in lwip doesn't match the IDF enum. Please make sure lwIP version is correct"); #define SNTP_ERROR(func, message) do { \ err_t err = func; \ LWIP_ERROR(message, err == ERR_OK, ); \ }{...} while (0)... static volatile sntp_sync_mode_t sntp_sync_mode = SNTP_SYNC_MODE_IMMED; static volatile sntp_sync_status_t sntp_sync_status = SNTP_SYNC_STATUS_RESET; static sntp_sync_time_cb_t time_sync_notification_cb = NULL; static uint32_t s_sync_interval = CONFIG_LWIP_SNTP_UPDATE_DELAY; inline void sntp_set_sync_status(sntp_sync_status_t sync_status) { sntp_sync_status = sync_status; }{ ... } void __attribute__((weak)) sntp_sync_time(struct timeval *tv) { if (sntp_sync_mode == SNTP_SYNC_MODE_IMMED) { settimeofday(tv, NULL); sntp_set_sync_status(SNTP_SYNC_STATUS_COMPLETED); }{...} else if (sntp_sync_mode == SNTP_SYNC_MODE_SMOOTH) { struct timeval tv_now; gettimeofday(&tv_now, NULL); int64_t cpu_time = (int64_t)tv_now.tv_sec * 1000000L + (int64_t)tv_now.tv_usec; int64_t sntp_time = (int64_t)tv->tv_sec * 1000000L + (int64_t)tv->tv_usec; int64_t delta = sntp_time - cpu_time; struct timeval tv_delta = { .tv_sec = delta / 1000000L, .tv_usec = delta % 1000000L }; if (adjtime(&tv_delta, NULL) == -1) { ESP_LOGD(TAG, "Function adjtime don't update time because the error is very big"); settimeofday(tv, NULL); ESP_LOGD(TAG, "Time was synchronized through settimeofday"); sntp_set_sync_status(SNTP_SYNC_STATUS_COMPLETED); }{...} else { sntp_set_sync_status(SNTP_SYNC_STATUS_IN_PROGRESS); }{...} }{...} if (time_sync_notification_cb) { time_sync_notification_cb(tv); }{...} }{ ... } void sntp_set_sync_mode(sntp_sync_mode_t sync_mode) { sntp_sync_mode = sync_mode; }{ ... } sntp_sync_mode_t sntp_get_sync_mode(void) { return sntp_sync_mode; }{ ... } // set a callback function for time synchronization notification void sntp_set_time_sync_notification_cb(sntp_sync_time_cb_t callback) { time_sync_notification_cb = callback; }{ ... } sntp_sync_status_t sntp_get_sync_status(void) { sntp_sync_status_t ret_sync_status = SNTP_SYNC_STATUS_RESET; sntp_sync_status_t sync_status = sntp_sync_status; if (sync_status == SNTP_SYNC_STATUS_COMPLETED) { sntp_set_sync_status(SNTP_SYNC_STATUS_RESET); ret_sync_status = SNTP_SYNC_STATUS_COMPLETED; }{...} else if (sync_status == SNTP_SYNC_STATUS_IN_PROGRESS) { struct timeval outdelta; adjtime(NULL, &outdelta); if (outdelta.tv_sec == 0 && outdelta.tv_usec == 0) { sntp_set_sync_status(SNTP_SYNC_STATUS_RESET); ret_sync_status = SNTP_SYNC_STATUS_COMPLETED; }{...} else { ret_sync_status = SNTP_SYNC_STATUS_IN_PROGRESS; }{...} }{...} return ret_sync_status; }{ ... } void sntp_set_sync_interval(uint32_t interval_ms) { if (interval_ms < 15000) { // SNTPv4 RFC 4330 enforces a minimum update time of 15 seconds interval_ms = 15000; }{...} s_sync_interval = interval_ms; }{ ... } uint32_t sntp_get_sync_interval(void) { return s_sync_interval; }{ ... } static void sntp_do_restart(void *ctx) { (void)ctx; sntp_stop(); sntp_init(); }{ ... } bool sntp_restart(void) { if (sntp_enabled()) { SNTP_ERROR(tcpip_callback(sntp_do_restart, NULL), "sntp_restart: tcpip_callback() failed"); return true; }{...} return false; }{ ... } void sntp_set_system_time(uint32_t sec, uint32_t us) { // Note: SNTP/NTP timestamp is defined as 64-bit fixed point int // in seconds from 1900 (integer part is the first 32 bits) // which overflows in 2036. // The lifetime of the NTP timestamps has been extended by convention // of the MSB bit 0 to span between 1968 and 2104. This is implemented // in lwip sntp module, so this API returns number of seconds/milliseconds // representing dates range from 1968 to 2104. // (see: RFC-4330#section-3 and https://github.com/lwip-tcpip/lwip/blob/239918cc/src/apps/sntp/sntp.c#L129-L134) // Warning: Here, we convert the 32 bit NTP timestamp to 64 bit representation // of system time (time_t). This won't work for timestamps in future // after some time in 2104 struct timeval tv = { .tv_sec = sec, .tv_usec = us }; sntp_sync_time(&tv); }{ ... } void sntp_get_system_time(uint32_t *sec, uint32_t *us) { struct timeval tv = { .tv_sec = 0, .tv_usec = 0 }; gettimeofday(&tv, NULL); // Warning: Here, we convert 64 bit representation of system time (time_t) to // 32 bit NTP timestamp. This won't work for future timestamps after some time in 2104 // (see: RFC-4330#section-3) *(sec) = tv.tv_sec; *(us) = tv.tv_usec; sntp_set_sync_status(SNTP_SYNC_STATUS_RESET); }{ ... } static void do_setoperatingmode(void *ctx) { sntp_setoperatingmode((intptr_t)ctx); }{ ... } void esp_sntp_setoperatingmode(esp_sntp_operatingmode_t operating_mode) { SNTP_ERROR(tcpip_callback(do_setoperatingmode, (void*)operating_mode), "esp_sntp_setoperatingmode: tcpip_callback() failed"); }{ ... } static void do_init(void *ctx) { sntp_init(); }{ ... } void esp_sntp_init(void) { SNTP_ERROR(tcpip_callback(do_init, NULL), "esp_sntp_init: tcpip_callback() failed"); }{ ... } static void do_stop(void *ctx) { sntp_stop(); }{ ... } void esp_sntp_stop(void) { SNTP_ERROR(tcpip_callback(do_stop, NULL), "esp_sntp_stop: tcpip_callback() failed"); }{ ... } struct tcpip_setserver { struct tcpip_api_call_data call; u8_t idx; const ip_addr_t *addr; }{ ... }; static err_t do_setserver(struct tcpip_api_call_data *msg) { struct tcpip_setserver *params = __containerof(msg, struct tcpip_setserver, call); sntp_setserver(params->idx, params->addr); return ERR_OK; }{ ... } void esp_sntp_setserver(u8_t idx, const ip_addr_t *addr) { struct tcpip_setserver params = { .idx = idx, .addr = addr }{...}; SNTP_ERROR(tcpip_api_call(do_setserver, &params.call), "esp_sntp_setserver :tcpip_api_call() failed"); }{ ... } struct tcpip_setservername { struct tcpip_api_call_data call; u8_t idx; const char *server; }{ ... }; static err_t do_setservername(struct tcpip_api_call_data *msg) { struct tcpip_setservername *params = __containerof(msg, struct tcpip_setservername, call); sntp_setservername(params->idx, params->server); return ERR_OK; }{ ... } void esp_sntp_setservername(u8_t idx, const char *server) { struct tcpip_setservername params = { .idx = idx, .server = server }{...}; SNTP_ERROR(tcpip_api_call(do_setservername, &params.call), "esp_sntp_setservername :tcpip_api_call() failed"); }{ ... } const char *esp_sntp_getservername(u8_t idx) { return sntp_getservername(idx); }{ ... } const ip_addr_t* esp_sntp_getserver(u8_t idx) { return sntp_getserver(idx); }{ ... } uint8_t esp_sntp_getreachability(uint8_t idx) { #if SNTP_MONITOR_SERVER_REACHABILITY return sntp_getreachability(idx); #endif LWIP_ERROR("sntp_getreachability() in not enabled in lwipopts", false, ); return 0; }{ ... } esp_sntp_operatingmode_t esp_sntp_getoperatingmode(void) { return (esp_sntp_operatingmode_t)sntp_getoperatingmode(); }{ ... } #if LWIP_DHCP_GET_NTP_SRV static void do_servermode_dhcp(void* ctx) { sntp_servermode_dhcp((intptr_t)ctx); }{...} void esp_sntp_servermode_dhcp(bool enable) { SNTP_ERROR(tcpip_callback(do_servermode_dhcp, (void*)enable), "esp_sntp_servermode_dhcp: tcpip_callback() failed"); }{...} /* ... */ #endif /* LWIP_DHCP_GET_NTP_SRV */ bool esp_sntp_enabled(void) { return sntp_enabled(); }{ ... }
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.