MeterLogger
sntp.c
Go to the documentation of this file.
1 /**
2  * @file
3  * SNTP client module
4  *
5  * This is simple "SNTP" client for the lwIP raw API.
6  * It is a minimal implementation of SNTPv4 as specified in RFC 4330.
7  *
8  * For a list of some public NTP servers, see this link :
9  * http://support.ntp.org/bin/view/Servers/NTPPoolServers
10  *
11  * @todo:
12  * - set/change servers at runtime
13  * - complete SNTP_CHECK_RESPONSE checks 3 and 4
14  * - support broadcast/multicast mode?
15  */
16 
17 /*
18  * Redistribution and use in source and binary forms, with or without modification,
19  * are permitted provided that the following conditions are met:
20  *
21  * 1. Redistributions of source code must retain the above copyright notice,
22  * this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright notice,
24  * this list of conditions and the following disclaimer in the documentation
25  * and/or other materials provided with the distribution.
26  * 3. The name of the author may not be used to endorse or promote products
27  * derived from this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
30  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
31  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
32  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
34  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
37  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
38  * OF SUCH DAMAGE.
39  *
40  * This file is part of the lwIP TCP/IP stack.
41  *
42  * Author: Simon Goldschmidt (lwIP raw API part)
43  */
44 
45 #include "lwip/sntp.h"
46 #include "osapi.h"
47 #include "os_type.h"
48 #include "lwip/opt.h"
49 #include "lwip/timers.h"
50 #include "lwip/udp.h"
51 #include "lwip/dns.h"
52 #include "lwip/ip_addr.h"
53 #include "lwip/pbuf.h"
54 
55 //#include <string.h>
56 #if LWIP_UDP
57 
58 /**
59  * SNTP_DEBUG: Enable debugging for SNTP.
60  */
61 #ifndef SNTP_DEBUG
62 #define SNTP_DEBUG LWIP_DBG_ON
63 #endif
64 
65 /** SNTP server port */
66 #ifndef SNTP_PORT
67 #define SNTP_PORT 123
68 #endif
69 
70 /** Set this to 1 to allow config of SNTP server(s) by DNS name */
71 #ifndef SNTP_SERVER_DNS
72 #define SNTP_SERVER_DNS 0
73 #endif
74 
75 /** Handle support for more than one server via NTP_MAX_SERVERS,
76  * but catch legacy style of setting SNTP_SUPPORT_MULTIPLE_SERVERS, probably outside of this file
77  */
78 #ifndef SNTP_SUPPORT_MULTIPLE_SERVERS
79 #if SNTP_MAX_SERVERS > 1
80 #define SNTP_SUPPORT_MULTIPLE_SERVERS 1
81 #else /* NTP_MAX_SERVERS > 1 */
82 #define SNTP_SUPPORT_MULTIPLE_SERVERS 0
83 #endif /* NTP_MAX_SERVERS > 1 */
84 #else /* SNTP_SUPPORT_MULTIPLE_SERVERS */
85 /* The developer has defined SNTP_SUPPORT_MULTIPLE_SERVERS, probably from old code */
86 #if SNTP_MAX_SERVERS <= 1
87 #error "SNTP_MAX_SERVERS needs to be defined to the max amount of servers if SNTP_SUPPORT_MULTIPLE_SERVERS is defined"
88 #endif /* SNTP_MAX_SERVERS <= 1 */
89 #endif /* SNTP_SUPPORT_MULTIPLE_SERVERS */
90 
91 
92 /** Sanity check:
93  * Define this to
94  * - 0 to turn off sanity checks (default; smaller code)
95  * - >= 1 to check address and port of the response packet to ensure the
96  * response comes from the server we sent the request to.
97  * - >= 2 to check returned Originate Timestamp against Transmit Timestamp
98  * sent to the server (to ensure response to older request).
99  * - >= 3 @todo: discard reply if any of the LI, Stratum, or Transmit Timestamp
100  * fields is 0 or the Mode field is not 4 (unicast) or 5 (broadcast).
101  * - >= 4 @todo: to check that the Root Delay and Root Dispersion fields are each
102  * greater than or equal to 0 and less than infinity, where infinity is
103  * currently a cozy number like one second. This check avoids using a
104  * server whose synchronization source has expired for a very long time.
105  */
106 #ifndef SNTP_CHECK_RESPONSE
107 #define SNTP_CHECK_RESPONSE 0
108 #endif
109 
110 /** According to the RFC, this shall be a random delay
111  * between 1 and 5 minutes (in milliseconds) to prevent load peaks.
112  * This can be defined to a random generation function,
113  * which must return the delay in milliseconds as u32_t.
114  * Turned off by default.
115  */
116 #ifndef SNTP_STARTUP_DELAY
117 #define SNTP_STARTUP_DELAY 0
118 #endif
119 
120 /** If you want the startup delay to be a function, define this
121  * to a function (including the brackets) and define SNTP_STARTUP_DELAY to 1.
122  */
123 #ifndef SNTP_STARTUP_DELAY_FUNC
124 #define SNTP_STARTUP_DELAY_FUNC SNTP_STARTUP_DELAY
125 #endif
126 
127 /** SNTP receive timeout - in milliseconds
128  * Also used as retry timeout - this shouldn't be too low.
129  * Default is 3 seconds.
130  */
131 #ifndef SNTP_RECV_TIMEOUT
132 #define SNTP_RECV_TIMEOUT 3000
133 #endif
134 
135 /** SNTP update delay - in milliseconds
136  * Default is 1 hour.
137  */
138 #ifndef SNTP_UPDATE_DELAY
139 #define SNTP_UPDATE_DELAY 3600000
140 #endif
141 #if (SNTP_UPDATE_DELAY < 15000) && !SNTP_SUPPRESS_DELAY_CHECK
142 #error "SNTPv4 RFC 4330 enforces a minimum update time of 15 seconds!"
143 #endif
144 
145 /** SNTP macro to change system time and/or the update the RTC clock */
146 #ifndef SNTP_SET_SYSTEM_TIME
147 #define SNTP_SET_SYSTEM_TIME(sec) ((void)sec)
148 #endif
149 
150 /** SNTP macro to change system time including microseconds */
151 #ifdef SNTP_SET_SYSTEM_TIME_US
152 #define SNTP_CALC_TIME_US 1
153 #define SNTP_RECEIVE_TIME_SIZE 2
154 #else
155 #define SNTP_SET_SYSTEM_TIME_US(sec, us)
156 #define SNTP_CALC_TIME_US 0
157 #define SNTP_RECEIVE_TIME_SIZE 1
158 #endif
159 
160 /** SNTP macro to get system time, used with SNTP_CHECK_RESPONSE >= 2
161  * to send in request and compare in response.
162  */
163 #ifndef SNTP_GET_SYSTEM_TIME
164 #define SNTP_GET_SYSTEM_TIME(sec, us) do { (sec) = 0; (us) = 0; } while(0)
165 #endif
166 
167 /** Default retry timeout (in milliseconds) if the response
168  * received is invalid.
169  * This is doubled with each retry until SNTP_RETRY_TIMEOUT_MAX is reached.
170  */
171 #ifndef SNTP_RETRY_TIMEOUT
172 #define SNTP_RETRY_TIMEOUT SNTP_RECV_TIMEOUT
173 #endif
174 
175 /** Maximum retry timeout (in milliseconds). */
176 #ifndef SNTP_RETRY_TIMEOUT_MAX
177 #define SNTP_RETRY_TIMEOUT_MAX (SNTP_RETRY_TIMEOUT * 10)
178 #endif
179 
180 /** Increase retry timeout with every retry sent
181  * Default is on to conform to RFC.
182  */
183 #ifndef SNTP_RETRY_TIMEOUT_EXP
184 #define SNTP_RETRY_TIMEOUT_EXP 1
185 #endif
186 
187 /* the various debug levels for this file */
188 #define SNTP_DEBUG_TRACE (SNTP_DEBUG | LWIP_DBG_TRACE)
189 #define SNTP_DEBUG_STATE (SNTP_DEBUG | LWIP_DBG_STATE)
190 #define SNTP_DEBUG_WARN (SNTP_DEBUG | LWIP_DBG_LEVEL_WARNING)
191 #define SNTP_DEBUG_WARN_STATE (SNTP_DEBUG | LWIP_DBG_LEVEL_WARNING | LWIP_DBG_STATE)
192 #define SNTP_DEBUG_SERIOUS (SNTP_DEBUG | LWIP_DBG_LEVEL_SERIOUS)
193 
194 #define SNTP_ERR_KOD 1
195 
196 /* SNTP protocol defines */
197 #define SNTP_MSG_LEN 48
198 
199 #define SNTP_OFFSET_LI_VN_MODE 0
200 #define SNTP_LI_MASK 0xC0
201 #define SNTP_LI_NO_WARNING 0x00
202 #define SNTP_LI_LAST_MINUTE_61_SEC 0x01
203 #define SNTP_LI_LAST_MINUTE_59_SEC 0x02
204 #define SNTP_LI_ALARM_CONDITION 0x03 /* (clock not synchronized) */
205 
206 #define SNTP_VERSION_MASK 0x38
207 #define SNTP_VERSION (4/* NTP Version 4*/<<3)
208 
209 #define SNTP_MODE_MASK 0x07
210 #define SNTP_MODE_CLIENT 0x03
211 #define SNTP_MODE_SERVER 0x04
212 #define SNTP_MODE_BROADCAST 0x05
213 
214 #define SNTP_OFFSET_STRATUM 1
215 #define SNTP_STRATUM_KOD 0x00
216 
217 #define SNTP_OFFSET_ORIGINATE_TIME 24
218 #define SNTP_OFFSET_RECEIVE_TIME 32
219 #define SNTP_OFFSET_TRANSMIT_TIME 40
220 
221 /* number of seconds between 1900 and 1970 */
222 #define DIFF_SEC_1900_1970 (2208988800UL)
223 
224 /**
225  * SNTP packet format (without optional fields)
226  * Timestamps are coded as 64 bits:
227  * - 32 bits seconds since Jan 01, 1970, 00:00
228  * - 32 bits seconds fraction (0-padded)
229  * For future use, if the MSB in the seconds part is set, seconds are based
230  * on Feb 07, 2036, 06:28:16.
231  */
232 #ifdef PACK_STRUCT_USE_INCLUDES
233 # include "arch/bpstruct.h"
234 #endif
236 #define PACK_STRUCT_FLD_8 PACK_STRUCT_FIELD
237 struct sntp_msg {
238  PACK_STRUCT_FLD_8(u8_t li_vn_mode);
239  PACK_STRUCT_FLD_8(u8_t stratum);
240  PACK_STRUCT_FLD_8(u8_t poll);
241  PACK_STRUCT_FLD_8(u8_t precision);
242  PACK_STRUCT_FIELD(u32_t root_delay);
243  PACK_STRUCT_FIELD(u32_t root_dispersion);
244  PACK_STRUCT_FIELD(u32_t reference_identifier);
245  PACK_STRUCT_FIELD(u32_t reference_timestamp[2]);
246  PACK_STRUCT_FIELD(u32_t originate_timestamp[2]);
247  PACK_STRUCT_FIELD(u32_t receive_timestamp[2]);
248  PACK_STRUCT_FIELD(u32_t transmit_timestamp[2]);
251 #ifdef PACK_STRUCT_USE_INCLUDES
252 # include "arch/epstruct.h"
253 #endif
254 
255 /* function prototypes */
256 static void sntp_request(void *arg);
257 
258 /** The UDP pcb used by the SNTP client */
259 static struct udp_pcb* sntp_pcb;
260 
261 sint8 time_zone = 8;
262 /** Names/Addresses of servers */
263 struct sntp_server {
264 #if SNTP_SERVER_DNS
265  char* name;
266 #endif /* SNTP_SERVER_DNS */
267  ip_addr_t addr;
268 };
269 static struct sntp_server sntp_servers[SNTP_MAX_SERVERS];
270 
271 static u8_t sntp_set_servers_from_dhcp;
272 #if SNTP_SUPPORT_MULTIPLE_SERVERS
273 /** The currently used server (initialized to 0) */
274 static u8_t sntp_current_server;
275 #else /* SNTP_SUPPORT_MULTIPLE_SERVERS */
276 #define sntp_current_server 0
277 #endif /* SNTP_SUPPORT_MULTIPLE_SERVERS */
278 
279 #if SNTP_RETRY_TIMEOUT_EXP
280 #define SNTP_RESET_RETRY_TIMEOUT() sntp_retry_timeout = SNTP_RETRY_TIMEOUT
281 /** Retry time, initialized with SNTP_RETRY_TIMEOUT and doubled with each retry. */
282 static u32_t sntp_retry_timeout;
283 #else /* SNTP_RETRY_TIMEOUT_EXP */
284 #define SNTP_RESET_RETRY_TIMEOUT()
285 #define sntp_retry_timeout SNTP_RETRY_TIMEOUT
286 #endif /* SNTP_RETRY_TIMEOUT_EXP */
287 
288 #if SNTP_CHECK_RESPONSE >= 1
289 /** Saves the last server address to compare with response */
290 static ip_addr_t sntp_last_server_address;
291 #endif /* SNTP_CHECK_RESPONSE >= 1 */
292 
293 #if SNTP_CHECK_RESPONSE >= 2
294 /** Saves the last timestamp sent (which is sent back by the server)
295  * to compare against in response */
296 static u32_t sntp_last_timestamp_sent[2];
297 #endif /* SNTP_CHECK_RESPONSE >= 2 */
298 typedef long time_t;
299 //uint32 current_stamp_1 = 0;
300 //uint32 current_stamp_2 = 0;
301 uint32 realtime_stamp = 0;
302 LOCAL os_timer_t sntp_timer;
303 /*****************************************/
304 #define SECSPERMIN 60L
305 #define MINSPERHOUR 60L
306 #define HOURSPERDAY 24L
307 #define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
308 #define SECSPERDAY (SECSPERHOUR * HOURSPERDAY)
309 #define DAYSPERWEEK 7
310 #define MONSPERYEAR 12
311 
312 #define YEAR_BASE 1900
313 #define EPOCH_YEAR 1970
314 #define EPOCH_WDAY 4
315 #define EPOCH_YEARS_SINCE_LEAP 2
316 #define EPOCH_YEARS_SINCE_CENTURY 70
317 #define EPOCH_YEARS_SINCE_LEAP_CENTURY 370
318 
319 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
320 
321 int __tznorth;
322 int __tzyear;
323 char reult[100];
324 static const int mon_lengths[2][12] = {
325  {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
326  {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
327 } ;
328 
329 static const int year_lengths[2] = {
330  365,
331  366
332 } ;
333 struct tm
334 {
335  int tm_sec;
336  int tm_min;
337  int tm_hour;
338  int tm_mday;
339  int tm_mon;
340  int tm_year;
341  int tm_wday;
342  int tm_yday;
343  int tm_isdst;
344 };
345 
346 struct tm res_buf;
347 typedef struct __tzrule_struct
348 {
349  char ch;
350  int m;
351  int n;
352  int d;
353  int s;
354  time_t change;
355  int offset;
356 } __tzrule_type;
357 
358 __tzrule_type sntp__tzrule[2];
359 struct tm * ICACHE_FLASH_ATTR
360 sntp_mktm_r(const time_t * tim_p ,struct tm *res ,int is_gmtime)
361 {
362  long days, rem;
363  time_t lcltime;
364  int i;
365  int y;
366  int yleap;
367  const int *ip;
368 
369  /* base decision about std/dst time on current time */
370  lcltime = *tim_p;
371 
372  days = ((long)lcltime) / SECSPERDAY;
373  rem = ((long)lcltime) % SECSPERDAY;
374  while (rem < 0)
375  {
376  rem += SECSPERDAY;
377  --days;
378  }
379  while (rem >= SECSPERDAY)
380  {
381  rem -= SECSPERDAY;
382  ++days;
383  }
384 
385  /* compute hour, min, and sec */
386  res->tm_hour = (int) (rem / SECSPERHOUR);
387  rem %= SECSPERHOUR;
388  res->tm_min = (int) (rem / SECSPERMIN);
389  res->tm_sec = (int) (rem % SECSPERMIN);
390 
391  /* compute day of week */
392  if ((res->tm_wday = ((EPOCH_WDAY + days) % DAYSPERWEEK)) < 0)
393  res->tm_wday += DAYSPERWEEK;
394 
395  /* compute year & day of year */
396  y = EPOCH_YEAR;
397  if (days >= 0)
398  {
399  for (;;)
400  {
401  yleap = isleap(y);
402  if (days < year_lengths[yleap])
403  break;
404  y++;
405  days -= year_lengths[yleap];
406  }
407  }
408  else
409  {
410  do
411  {
412  --y;
413  yleap = isleap(y);
414  days += year_lengths[yleap];
415  } while (days < 0);
416  }
417 
418  res->tm_year = y - YEAR_BASE;
419  res->tm_yday = days;
420  ip = mon_lengths[yleap];
421  for (res->tm_mon = 0; days >= ip[res->tm_mon]; ++res->tm_mon)
422  days -= ip[res->tm_mon];
423  res->tm_mday = days + 1;
424 
425  if (!is_gmtime)
426  {
427  int offset;
428  int hours, mins, secs;
429 
430 // TZ_LOCK;
431 // if (_daylight)
432 // {
433 // if (y == __tzyear || __tzcalc_limits (y))
434 // res->tm_isdst = (__tznorth
435 // ? (*tim_p >= __tzrule[0].change && *tim_p < __tzrule[1].change)
436 // : (*tim_p >= __tzrule[0].change || *tim_p < __tzrule[1].change));
437 // else
438 // res->tm_isdst = -1;
439 // }
440 // else
441  res->tm_isdst = 0;
442 
443  offset = (res->tm_isdst == 1 ? sntp__tzrule[1].offset : sntp__tzrule[0].offset);
444 
445  hours = offset / SECSPERHOUR;
446  offset = offset % SECSPERHOUR;
447 
448  mins = offset / SECSPERMIN;
449  secs = offset % SECSPERMIN;
450 
451  res->tm_sec -= secs;
452  res->tm_min -= mins;
453  res->tm_hour -= hours;
454 
455  if (res->tm_sec >= SECSPERMIN)
456  {
457  res->tm_min += 1;
458  res->tm_sec -= SECSPERMIN;
459  }
460  else if (res->tm_sec < 0)
461  {
462  res->tm_min -= 1;
463  res->tm_sec += SECSPERMIN;
464  }
465  if (res->tm_min >= MINSPERHOUR)
466  {
467  res->tm_hour += 1;
468  res->tm_min -= MINSPERHOUR;
469  }
470  else if (res->tm_min < 0)
471  {
472  res->tm_hour -= 1;
473  res->tm_min += MINSPERHOUR;
474  }
475  if (res->tm_hour >= HOURSPERDAY)
476  {
477  ++res->tm_yday;
478  ++res->tm_wday;
479  if (res->tm_wday > 6)
480  res->tm_wday = 0;
481  ++res->tm_mday;
482  res->tm_hour -= HOURSPERDAY;
483  if (res->tm_mday > ip[res->tm_mon])
484  {
485  res->tm_mday -= ip[res->tm_mon];
486  res->tm_mon += 1;
487  if (res->tm_mon == 12)
488  {
489  res->tm_mon = 0;
490  res->tm_year += 1;
491  res->tm_yday = 0;
492  }
493  }
494  }
495  else if (res->tm_hour < 0)
496  {
497  res->tm_yday -= 1;
498  res->tm_wday -= 1;
499  if (res->tm_wday < 0)
500  res->tm_wday = 6;
501  res->tm_mday -= 1;
502  res->tm_hour += 24;
503  if (res->tm_mday == 0)
504  {
505  res->tm_mon -= 1;
506  if (res->tm_mon < 0)
507  {
508  res->tm_mon = 11;
509  res->tm_year -= 1;
510  res->tm_yday = 365 + isleap(res->tm_year);
511  }
512  res->tm_mday = ip[res->tm_mon];
513  }
514  }
515 // TZ_UNLOCK;
516  }
517  else
518  res->tm_isdst = 0;
519 // os_printf("res %d %d %d %d %d\n",res->tm_year,res->tm_mon,res->tm_mday,res->tm_yday,res->tm_hour);
520  return (res);
521 }
522 struct tm * ICACHE_FLASH_ATTR
523 sntp_localtime_r(const time_t * tim_p ,
524  struct tm *res)
525 {
526  return sntp_mktm_r (tim_p, res, 0);
527 }
528 
529 struct tm * ICACHE_FLASH_ATTR
530 sntp_localtime(const time_t * tim_p)
531 {
532  return sntp_localtime_r (tim_p, &res_buf);
533 }
534 
535 
537 sntp__tzcalc_limits(int year)
538 {
539  int days, year_days, years;
540  int i, j;
541 
542  if (year < EPOCH_YEAR)
543  return 0;
544 
545  __tzyear = year;
546 
547  years = (year - EPOCH_YEAR);
548 
549  year_days = years * 365 +
550  (years - 1 + EPOCH_YEARS_SINCE_LEAP) / 4 - (years - 1 + EPOCH_YEARS_SINCE_CENTURY) / 100 +
551  (years - 1 + EPOCH_YEARS_SINCE_LEAP_CENTURY) / 400;
552 
553  for (i = 0; i < 2; ++i)
554  {
555  if (sntp__tzrule[i].ch == 'J')
556  days = year_days + sntp__tzrule[i].d + (isleap(year) && sntp__tzrule[i].d >= 60);
557  else if (sntp__tzrule[i].ch == 'D')
558  days = year_days + sntp__tzrule[i].d;
559  else
560  {
561  int yleap = isleap(year);
562  int m_day, m_wday, wday_diff;
563  const int *ip = mon_lengths[yleap];
564 
565  days = year_days;
566 
567  for (j = 1; j < sntp__tzrule[i].m; ++j)
568  days += ip[j-1];
569 
570  m_wday = (EPOCH_WDAY + days) % DAYSPERWEEK;
571 
572  wday_diff = sntp__tzrule[i].d - m_wday;
573  if (wday_diff < 0)
574  wday_diff += DAYSPERWEEK;
575  m_day = (sntp__tzrule[i].n - 1) * DAYSPERWEEK + wday_diff;
576 
577  while (m_day >= ip[j-1])
578  m_day -= DAYSPERWEEK;
579 
580  days += m_day;
581  }
582 
583  /* store the change-over time in GMT form by adding offset */
584  sntp__tzrule[i].change = days * SECSPERDAY + sntp__tzrule[i].s + sntp__tzrule[i].offset;
585  }
586 
587  __tznorth = (sntp__tzrule[0].change < sntp__tzrule[1].change);
588 
589  return 1;
590 }
591 
592 char * ICACHE_FLASH_ATTR
593 sntp_asctime_r(struct tm *tim_p ,char *result)
594 {
595  static const char day_name[7][4] = {
596  "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
597  };
598  static const char mon_name[12][4] = {
599  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
600  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
601  };
602  os_sprintf (result, "%s %s %02d %02d:%02d:%02d %02d\n",
603  day_name[tim_p->tm_wday],
604  mon_name[tim_p->tm_mon],
605  tim_p->tm_mday, tim_p->tm_hour, tim_p->tm_min,
606  tim_p->tm_sec, 1900 + tim_p->tm_year);
607  return result;
608 }
609 char *ICACHE_FLASH_ATTR
610 sntp_asctime(struct tm *tim_p)
611 {
612 
613  return sntp_asctime_r (tim_p, reult);
614 }
615 
617 {
618  if(realtime_stamp == 0){
619  os_printf("please start sntp first !\n");
620  return 0;
621  } else {
622  return realtime_stamp;
623  }
624 }
625 
626 char* sntp_get_real_time(time_t t)
627 {
628  return sntp_asctime(sntp_localtime (&t));
629 }
630 /**
631  * SNTP get time_zone default GMT + 8
632  */
634 sntp_get_timezone(void)
635 {
636  return time_zone;
637 }
638 /**
639  * SNTP set time_zone default GMT + 8
640  */
641 
643 sntp_set_timezone(sint8 timezone)
644 {
645  if(timezone >= -11 || timezone <= 13) {
646  time_zone = timezone;
647  return true;
648  } else {
649  return false;
650  }
651 
652 }
654 sntp_time_inc(void)
655 {
656  realtime_stamp++;
657 }
658 /**
659  * SNTP processing of received timestamp
660  */
661 static void ICACHE_FLASH_ATTR
662 sntp_process(u32_t *receive_timestamp)
663 {
664  /* convert SNTP time (1900-based) to unix GMT time (1970-based)
665  * @todo: if MSB is 1, SNTP time is 2036-based!
666  */
667  time_t t = (ntohl(receive_timestamp[0]) - DIFF_SEC_1900_1970);
668 
669 #if SNTP_CALC_TIME_US
670  u32_t us = ntohl(receive_timestamp[1]) / 4295;
671  SNTP_SET_SYSTEM_TIME_US(t, us);
672  /* display local time from GMT time */
673  LWIP_DEBUGF(SNTP_DEBUG_TRACE, ("sntp_process: %s, %"U32_F" us", ctime(&t), us));
674 
675 #else /* SNTP_CALC_TIME_US */
676 
677  /* change system time and/or the update the RTC clock */
678  SNTP_SET_SYSTEM_TIME(t);
679  /* display local time from GMT time */
680  t += time_zone * 60 * 60;// format GMT + time_zone TIME ZONE
681  realtime_stamp = t;
682  os_timer_disarm(&sntp_timer);
683  os_timer_setfn(&sntp_timer, (os_timer_func_t *)sntp_time_inc, NULL);
684  os_timer_arm(&sntp_timer, 1000, 1);
685  os_printf("%s\n",sntp_asctime(sntp_localtime (&t)));
686 // os_printf("%s\n",ctime(&t));
687 // LWIP_DEBUGF(SNTP_DEBUG_TRACE, ("sntp_process: %s", ctime(&t)));
688 #endif /* SNTP_CALC_TIME_US */
689 }
690 
691 /**
692  * Initialize request struct to be sent to server.
693  */
694 static void ICACHE_FLASH_ATTR
695 sntp_initialize_request(struct sntp_msg *req)
696 {
697  os_memset(req, 0, SNTP_MSG_LEN);
698  req->li_vn_mode = SNTP_LI_NO_WARNING | SNTP_VERSION | SNTP_MODE_CLIENT;
699 
700 #if SNTP_CHECK_RESPONSE >= 2
701  {
702  u32_t sntp_time_sec, sntp_time_us;
703  /* fill in transmit timestamp and save it in 'sntp_last_timestamp_sent' */
704  SNTP_GET_SYSTEM_TIME(sntp_time_sec, sntp_time_us);
705  sntp_last_timestamp_sent[0] = htonl(sntp_time_sec + DIFF_SEC_1900_1970);
706  req->transmit_timestamp[0] = sntp_last_timestamp_sent[0];
707  /* we send/save us instead of fraction to be faster... */
708  sntp_last_timestamp_sent[1] = htonl(sntp_time_us);
709  req->transmit_timestamp[1] = sntp_last_timestamp_sent[1];
710  }
711 #endif /* SNTP_CHECK_RESPONSE >= 2 */
712 }
713 
714 /**
715  * Retry: send a new request (and increase retry timeout).
716  *
717  * @param arg is unused (only necessary to conform to sys_timeout)
718  */
719 static void ICACHE_FLASH_ATTR
720 sntp_retry(void* arg)
721 {
722  LWIP_UNUSED_ARG(arg);
723 
724  LWIP_DEBUGF(SNTP_DEBUG_STATE, ("sntp_retry: Next request will be sent in %"U32_F" ms\n",
725  sntp_retry_timeout));
726 
727  /* set up a timer to send a retry and increase the retry delay */
728  sys_timeout(sntp_retry_timeout, sntp_request, NULL);
729 
730 #if SNTP_RETRY_TIMEOUT_EXP
731  {
732  u32_t new_retry_timeout;
733  /* increase the timeout for next retry */
734  new_retry_timeout = sntp_retry_timeout << 1;
735  /* limit to maximum timeout and prevent overflow */
736  if ((new_retry_timeout <= SNTP_RETRY_TIMEOUT_MAX) &&
737  (new_retry_timeout > sntp_retry_timeout)) {
738  sntp_retry_timeout = new_retry_timeout;
739  }
740  }
741 #endif /* SNTP_RETRY_TIMEOUT_EXP */
742 }
743 
744 #if SNTP_SUPPORT_MULTIPLE_SERVERS
745 /**
746  * If Kiss-of-Death is received (or another packet parsing error),
747  * try the next server or retry the current server and increase the retry
748  * timeout if only one server is available.
749  * (implicitly, SNTP_MAX_SERVERS > 1)
750  *
751  * @param arg is unused (only necessary to conform to sys_timeout)
752  */
753 static void
754 sntp_try_next_server(void* arg)
755 {
756  u8_t old_server, i;
757  LWIP_UNUSED_ARG(arg);
758 
759  old_server = sntp_current_server;
760  for (i = 0; i < SNTP_MAX_SERVERS - 1; i++) {
761  sntp_current_server++;
762  if (sntp_current_server >= SNTP_MAX_SERVERS) {
763  sntp_current_server = 0;
764  }
765  if (!ip_addr_isany(&sntp_servers[sntp_current_server].addr)
766 #if SNTP_SERVER_DNS
767  || (sntp_servers[sntp_current_server].name != NULL)
768 #endif
769  ) {
770  LWIP_DEBUGF(SNTP_DEBUG_STATE, ("sntp_try_next_server: Sending request to server %"U16_F"\n",
771  (u16_t)sntp_current_server));
772  /* new server: reset retry timeout */
773  SNTP_RESET_RETRY_TIMEOUT();
774  /* instantly send a request to the next server */
775  sntp_request(NULL);
776  return;
777  }
778  }
779  /* no other valid server found */
780  sntp_current_server = old_server;
781  sntp_retry(NULL);
782 }
783 #else /* SNTP_SUPPORT_MULTIPLE_SERVERS */
784 /* Always retry on error if only one server is supported */
785 #define sntp_try_next_server sntp_retry
786 #endif /* SNTP_SUPPORT_MULTIPLE_SERVERS */
787 
788 /** UDP recv callback for the sntp pcb */
789 static void ICACHE_FLASH_ATTR
790 sntp_recv(void *arg, struct udp_pcb* pcb, struct pbuf *p, ip_addr_t *addr, u16_t port)
791 {
792  u8_t mode;
793  u8_t stratum;
794  u32_t receive_timestamp[SNTP_RECEIVE_TIME_SIZE];
795  err_t err;
796 //os_printf("sntp_recv\n");
797  LWIP_UNUSED_ARG(arg);
798  LWIP_UNUSED_ARG(pcb);
799 
800  /* packet received: stop retry timeout */
801  sys_untimeout(sntp_try_next_server, NULL);
802  sys_untimeout(sntp_request, NULL);
803 
804  err = ERR_ARG;
805 #if SNTP_CHECK_RESPONSE >= 1
806  /* check server address and port */
807  if (ip_addr_cmp(addr, &sntp_last_server_address) &&
808  (port == SNTP_PORT))
809 #else /* SNTP_CHECK_RESPONSE >= 1 */
810  LWIP_UNUSED_ARG(addr);
811  LWIP_UNUSED_ARG(port);
812 #endif /* SNTP_CHECK_RESPONSE >= 1 */
813  {
814  /* process the response */
815  if (p->tot_len == SNTP_MSG_LEN) {
816  pbuf_copy_partial(p, &mode, 1, SNTP_OFFSET_LI_VN_MODE);
817  mode &= SNTP_MODE_MASK;
818  /* if this is a SNTP response... */
819  if ((mode == SNTP_MODE_SERVER) ||
820  (mode == SNTP_MODE_BROADCAST)) {
821  pbuf_copy_partial(p, &stratum, 1, SNTP_OFFSET_STRATUM);
822  if (stratum == SNTP_STRATUM_KOD) {
823  /* Kiss-of-death packet. Use another server or increase UPDATE_DELAY. */
824  err = SNTP_ERR_KOD;
825  LWIP_DEBUGF(SNTP_DEBUG_STATE, ("sntp_recv: Received Kiss-of-Death\n"));
826  } else {
827 #if SNTP_CHECK_RESPONSE >= 2
828  /* check originate_timetamp against sntp_last_timestamp_sent */
829  u32_t originate_timestamp[2];
830  pbuf_copy_partial(p, &originate_timestamp, 8, SNTP_OFFSET_ORIGINATE_TIME);
831  if ((originate_timestamp[0] != sntp_last_timestamp_sent[0]) ||
832  (originate_timestamp[1] != sntp_last_timestamp_sent[1]))
833  {
834  LWIP_DEBUGF(SNTP_DEBUG_WARN, ("sntp_recv: Invalid originate timestamp in response\n"));
835  } else
836 #endif /* SNTP_CHECK_RESPONSE >= 2 */
837  /* @todo: add code for SNTP_CHECK_RESPONSE >= 3 and >= 4 here */
838  {
839  /* correct answer */
840  err = ERR_OK;
841  pbuf_copy_partial(p, &receive_timestamp, SNTP_RECEIVE_TIME_SIZE * 4, SNTP_OFFSET_RECEIVE_TIME);
842  }
843  }
844  } else {
845  LWIP_DEBUGF(SNTP_DEBUG_WARN, ("sntp_recv: Invalid mode in response: %"U16_F"\n", (u16_t)mode));
846  }
847  } else {
848  LWIP_DEBUGF(SNTP_DEBUG_WARN, ("sntp_recv: Invalid packet length: %"U16_F"\n", p->tot_len));
849  }
850  }
851  pbuf_free(p);
852  if (err == ERR_OK) {
853  /* Correct response, reset retry timeout */
854  SNTP_RESET_RETRY_TIMEOUT();
855 
856  sntp_process(receive_timestamp);
857 
858  /* Set up timeout for next request */
859  sys_timeout((u32_t)SNTP_UPDATE_DELAY, sntp_request, NULL);
860  LWIP_DEBUGF(SNTP_DEBUG_STATE, ("sntp_recv: Scheduled next time request: %"U32_F" ms\n",
861  (u32_t)SNTP_UPDATE_DELAY));
862  } else if (err == SNTP_ERR_KOD) {
863  /* Kiss-of-death packet. Use another server or increase UPDATE_DELAY. */
864  sntp_try_next_server(NULL);
865  } else {
866  /* another error, try the same server again */
867  sntp_retry(NULL);
868  }
869 }
870 
871 /** Actually send an sntp request to a server.
872  *
873  * @param server_addr resolved IP address of the SNTP server
874  */
875 static void ICACHE_FLASH_ATTR
876 sntp_send_request(ip_addr_t *server_addr)
877 {
878  struct pbuf* p;
879 // os_printf("sntp_send_request\n");
880  p = pbuf_alloc(PBUF_TRANSPORT, SNTP_MSG_LEN, PBUF_RAM);
881  if (p != NULL) {
882  struct sntp_msg *sntpmsg = (struct sntp_msg *)p->payload;
883  LWIP_DEBUGF(SNTP_DEBUG_STATE, ("sntp_send_request: Sending request to server\n"));
884  /* initialize request message */
885  sntp_initialize_request(sntpmsg);
886  /* send request */
887  udp_sendto(sntp_pcb, p, server_addr, SNTP_PORT);
888  /* free the pbuf after sending it */
889  pbuf_free(p);
890  /* set up receive timeout: try next server or retry on timeout */
891  sys_timeout((u32_t)SNTP_RECV_TIMEOUT, sntp_try_next_server, NULL);
892 #if SNTP_CHECK_RESPONSE >= 1
893  /* save server address to verify it in sntp_recv */
894  ip_addr_set(&sntp_last_server_address, server_addr);
895 #endif /* SNTP_CHECK_RESPONSE >= 1 */
896  } else {
897  LWIP_DEBUGF(SNTP_DEBUG_SERIOUS, ("sntp_send_request: Out of memory, trying again in %"U32_F" ms\n",
898  (u32_t)SNTP_RETRY_TIMEOUT));
899  /* out of memory: set up a timer to send a retry */
900  sys_timeout((u32_t)SNTP_RETRY_TIMEOUT, sntp_request, NULL);
901  }
902 }
903 
904 #if SNTP_SERVER_DNS
905 /**
906  * DNS found callback when using DNS names as server address.
907  */
908 static void
909 sntp_dns_found(const char* hostname, ip_addr_t *ipaddr, void *arg)
910 {
911  LWIP_UNUSED_ARG(hostname);
912  LWIP_UNUSED_ARG(arg);
913 
914  if (ipaddr != NULL) {
915  /* Address resolved, send request */
916  LWIP_DEBUGF(SNTP_DEBUG_STATE, ("sntp_dns_found: Server address resolved, sending request\n"));
917  sntp_send_request(ipaddr);
918  } else {
919  /* DNS resolving failed -> try another server */
920  LWIP_DEBUGF(SNTP_DEBUG_WARN_STATE, ("sntp_dns_found: Failed to resolve server address resolved, trying next server\n"));
921  sntp_try_next_server(NULL);
922  }
923 }
924 #endif /* SNTP_SERVER_DNS */
925 
926 /**
927  * Send out an sntp request.
928  *
929  * @param arg is unused (only necessary to conform to sys_timeout)
930  */
931 static void ICACHE_FLASH_ATTR
932 sntp_request(void *arg)
933 {
934  ip_addr_t sntp_server_address;
935  err_t err;
936 
937  LWIP_UNUSED_ARG(arg);
938 
939  /* initialize SNTP server address */
940 #if SNTP_SERVER_DNS
941 
942  if (sntp_servers[sntp_current_server].name) {
943  /* always resolve the name and rely on dns-internal caching & timeout */
944  ip_addr_set_any(&sntp_servers[sntp_current_server].addr);
945  err = dns_gethostbyname(sntp_servers[sntp_current_server].name, &sntp_server_address,
946  sntp_dns_found, NULL);
947  if (err == ERR_INPROGRESS) {
948  /* DNS request sent, wait for sntp_dns_found being called */
949  LWIP_DEBUGF(SNTP_DEBUG_STATE, ("sntp_request: Waiting for server address to be resolved.\n"));
950  return;
951  } else if (err == ERR_OK) {
952  sntp_servers[sntp_current_server].addr = sntp_server_address;
953  }
954  } else
955 #endif /* SNTP_SERVER_DNS */
956  {
957  sntp_server_address = sntp_servers[sntp_current_server].addr;
958 // os_printf("sntp_server_address ip %d\n",sntp_server_address.addr);
959  err = (ip_addr_isany(&sntp_server_address)) ? ERR_ARG : ERR_OK;
960  }
961 
962  if (err == ERR_OK) {
963  LWIP_DEBUGF(SNTP_DEBUG_TRACE, ("sntp_request: current server address is %u.%u.%u.%u\n",
964  ip4_addr1(&sntp_server_address), ip4_addr2(&sntp_server_address), ip4_addr3(&sntp_server_address), ip4_addr4(&sntp_server_address)));
965  sntp_send_request(&sntp_server_address);
966  } else {
967  /* address conversion failed, try another server */
968  LWIP_DEBUGF(SNTP_DEBUG_WARN_STATE, ("sntp_request: Invalid server address, trying next server.\n"));
969  sys_timeout((u32_t)SNTP_RETRY_TIMEOUT, sntp_try_next_server, NULL);
970  }
971 }
972 
973 /**
974  * Initialize this module.
975  * Send out request instantly or after SNTP_STARTUP_DELAY(_FUNC).
976  */
978 sntp_init(void)
979 {
980 #ifdef SNTP_SERVER_ADDRESS
981 #if SNTP_SERVER_DNS
982  sntp_setservername(0, SNTP_SERVER_ADDRESS);
983 #else
984 #error SNTP_SERVER_ADDRESS string not supported SNTP_SERVER_DNS==0
985 #endif
986 #endif /* SNTP_SERVER_ADDRESS */
987 
988  if (sntp_pcb == NULL) {
989  SNTP_RESET_RETRY_TIMEOUT();
990  sntp_pcb = udp_new();
991  LWIP_ASSERT("Failed to allocate udp pcb for sntp client", sntp_pcb != NULL);
992  if (sntp_pcb != NULL) {
993  udp_recv(sntp_pcb, sntp_recv, NULL);
994 #if SNTP_STARTUP_DELAY
995  sys_timeout((u32_t)SNTP_STARTUP_DELAY_FUNC, sntp_request, NULL);
996 #else
997  sntp_request(NULL);
998 #endif
999  }
1000  }
1001 }
1002 
1003 /**
1004  * Stop this module.
1005  */
1006 void ICACHE_FLASH_ATTR
1007 sntp_stop(void)
1008 {
1009  if (sntp_pcb != NULL) {
1010  sys_untimeout(sntp_request, NULL);
1011  udp_remove(sntp_pcb);
1012  sntp_pcb = NULL;
1013  }
1014  os_timer_disarm(&sntp_timer);
1015  realtime_stamp = 0;
1016 }
1017 
1018 #if SNTP_GET_SERVERS_FROM_DHCP
1019 /**
1020  * Config SNTP server handling by IP address, name, or DHCP; clear table
1021  * @param set_servers_from_dhcp enable or disable getting server addresses from dhcp
1022  */
1023 void
1024 sntp_servermode_dhcp(int set_servers_from_dhcp)
1025 {
1026  u8_t new_mode = set_servers_from_dhcp ? 1 : 0;
1027  if (sntp_set_servers_from_dhcp != new_mode) {
1028  sntp_set_servers_from_dhcp = new_mode;
1029  }
1030 }
1031 #endif /* SNTP_GET_SERVERS_FROM_DHCP */
1032 
1033 /**
1034  * Initialize one of the NTP servers by IP address
1035  *
1036  * @param numdns the index of the NTP server to set must be < SNTP_MAX_SERVERS
1037  * @param dnsserver IP address of the NTP server to set
1038  */
1039 void ICACHE_FLASH_ATTR
1040 sntp_setserver(u8_t idx, ip_addr_t *server)
1041 {
1042  if (idx < SNTP_MAX_SERVERS) {
1043  if (server != NULL) {
1044  sntp_servers[idx].addr = (*server);
1045 // os_printf("server ip %d\n",server->addr);
1046  } else {
1047  ip_addr_set_any(&sntp_servers[idx].addr);
1048  }
1049 #if SNTP_SERVER_DNS
1050  sntp_servers[idx].name = NULL;
1051 #endif
1052  }
1053 }
1054 
1055 #if LWIP_DHCP && SNTP_GET_SERVERS_FROM_DHCP
1056 /**
1057  * Initialize one of the NTP servers by IP address, required by DHCP
1058  *
1059  * @param numdns the index of the NTP server to set must be < SNTP_MAX_SERVERS
1060  * @param dnsserver IP address of the NTP server to set
1061  */
1062 void
1063 dhcp_set_ntp_servers(u8_t num, ip_addr_t *server)
1064 {
1065  LWIP_DEBUGF(SNTP_DEBUG_TRACE, ("sntp: %s %u.%u.%u.%u as NTP server #%u via DHCP\n",
1066  (sntp_set_servers_from_dhcp ? "Got" : "Rejected"),
1067  ip4_addr1(server), ip4_addr2(server), ip4_addr3(server), ip4_addr4(server), num));
1068  if (sntp_set_servers_from_dhcp && num) {
1069  u8_t i;
1070  for (i = 0; (i < num) && (i < SNTP_MAX_SERVERS); i++) {
1071  sntp_setserver(i, &server[i]);
1072  }
1073  for (i = num; i < SNTP_MAX_SERVERS; i++) {
1074  sntp_setserver(i, NULL);
1075  }
1076  }
1077 }
1078 #endif /* LWIP_DHCP && SNTP_GET_SERVERS_FROM_DHCP */
1079 
1080 /**
1081  * Obtain one of the currently configured by IP address (or DHCP) NTP servers
1082  *
1083  * @param numdns the index of the NTP server
1084  * @return IP address of the indexed NTP server or "ip_addr_any" if the NTP
1085  * server has not been configured by address (or at all).
1086  */
1088 sntp_getserver(u8_t idx)
1089 {
1090  if (idx < SNTP_MAX_SERVERS) {
1091  return sntp_servers[idx].addr;
1092  }
1093  return *IP_ADDR_ANY;
1094 }
1095 
1096 #if SNTP_SERVER_DNS
1097 /**
1098  * Initialize one of the NTP servers by name
1099  *
1100  * @param numdns the index of the NTP server to set must be < SNTP_MAX_SERVERS
1101  * @param dnsserver DNS name of the NTP server to set, to be resolved at contact time
1102  */
1103 void ICACHE_FLASH_ATTR
1104 sntp_setservername(u8_t idx, char *server)
1105 {
1106  if (idx < SNTP_MAX_SERVERS) {
1107  sntp_servers[idx].name = server;
1108  }
1109 }
1110 
1111 /**
1112  * Obtain one of the currently configured by name NTP servers.
1113  *
1114  * @param numdns the index of the NTP server
1115  * @return IP address of the indexed NTP server or NULL if the NTP
1116  * server has not been configured by name (or at all)
1117  */
1118 char * ICACHE_FLASH_ATTR
1120 {
1121  if (idx < SNTP_MAX_SERVERS) {
1122  return sntp_servers[idx].name;
1123  }
1124  return NULL;
1125 }
1126 #endif /* SNTP_SERVER_DNS */
1127 
1128 #endif /* LWIP_UDP */
#define os_sprintf
Definition: osapi.h:54
u16_t tot_len
Definition: pbuf.h:90
void sntp_init(void)
#define os_timer_t
Definition: os_type.h:34
#define os_timer_disarm
Definition: osapi.h:51
#define ip_addr_set(dest, src)
Definition: ip_addr.h:164
#define U16_F
Definition: cc.h:61
uint32 sntp_get_current_timestamp()
#define NULL
Definition: def.h:47
#define ICACHE_FLASH_ATTR
Definition: c_types.h:99
#define os_timer_func_t
Definition: os_type.h:35
Definition: pbuf.h:58
char * sntp_getservername(u8_t idx)
#define os_printf
Definition: osapi.h:62
#define os_timer_setfn
Definition: osapi.h:52
void sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg) ICACHE_FLASH_ATTR
void sntp_setserver(u8_t idx, ip_addr_t *addr)
#define U32_F
Definition: cc.h:65
#define ntohl(x)
Definition: def.h:84
unsigned long u32_t
Definition: cc.h:56
sint8 sntp_get_timezone(void)
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:94
#define os_memset
Definition: osapi.h:38
#define ip_addr_cmp(addr1, addr2)
Definition: ip_addr.h:198
void sntp_stop(void)
#define ERR_OK
Definition: err.h:52
Definition: pbuf.h:76
#define PACK_STRUCT_STRUCT
Definition: cc.h:72
s8_t err_t
Definition: err.h:47
#define ip_addr_set_any(ipaddr)
Definition: ip_addr.h:170
typedefPACK_STRUCT_END struct ip_addr ip_addr_t
Definition: ip_addr.h:64
#define sntp_servermode_dhcp(x)
Definition: sntp.h:49
void sys_untimeout(sys_timeout_handler handler, void *arg) ICACHE_FLASH_ATTR
#define IP_ADDR_ANY
Definition: ip_addr.h:92
#define SNTP_MAX_SERVERS
Definition: sntp.h:13
#define ip_addr_isany(addr1)
Definition: ip_addr.h:200
#define ip4_addr3(ipaddr)
Definition: ip_addr.h:222
unsigned int uint32
Definition: c_types.h:54
#define ERR_ARG
Definition: err.h:68
u8_t pbuf_free(struct pbuf *p) ICACHE_FLASH_ATTR
Definition: pbuf.c:685
#define PACK_STRUCT_BEGIN
Definition: cc.h:73
void sntp_setservername(u8_t idx, char *server)
#define os_timer_arm(a, b, c)
Definition: osapi.h:50
#define PACK_STRUCT_END
Definition: cc.h:74
unsigned char u8_t
Definition: cc.h:52
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:65
void * payload
Definition: pbuf.h:81
u16_t pbuf_copy_partial(struct pbuf *p, void *dataptr, u16_t len, u16_t offset) ICACHE_FLASH_ATTR
Definition: pbuf.c:996
bool sntp_set_timezone(sint8 timezone)
ip_addr_t sntp_getserver(u8_t idx)
#define ip4_addr2(ipaddr)
Definition: ip_addr.h:221
#define ERR_INPROGRESS
Definition: err.h:57
char * sntp_get_real_time(long t)
signed char sint8
Definition: c_types.h:47
#define ip4_addr4(ipaddr)
Definition: ip_addr.h:223
#define LOCAL
Definition: c_types.h:72
#define SNTP_SERVER_DNS
Definition: sntp.h:24
#define ip4_addr1(ipaddr)
Definition: ip_addr.h:220
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:73
#define htonl(x)
Definition: def.h:83
struct pbuf * pbuf_alloc(pbuf_layer l, u16_t length, pbuf_type type) ICACHE_FLASH_ATTR
Definition: pbuf.c:234
unsigned short u16_t
Definition: cc.h:54
#define PACK_STRUCT_FIELD(x)
Definition: cc.h:71