62 #define SNTP_DEBUG LWIP_DBG_ON 71 #ifndef SNTP_SERVER_DNS 72 #define SNTP_SERVER_DNS 0 78 #ifndef SNTP_SUPPORT_MULTIPLE_SERVERS 79 #if SNTP_MAX_SERVERS > 1 80 #define SNTP_SUPPORT_MULTIPLE_SERVERS 1 82 #define SNTP_SUPPORT_MULTIPLE_SERVERS 0 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" 106 #ifndef SNTP_CHECK_RESPONSE 107 #define SNTP_CHECK_RESPONSE 0 116 #ifndef SNTP_STARTUP_DELAY 117 #define SNTP_STARTUP_DELAY 0 123 #ifndef SNTP_STARTUP_DELAY_FUNC 124 #define SNTP_STARTUP_DELAY_FUNC SNTP_STARTUP_DELAY 131 #ifndef SNTP_RECV_TIMEOUT 132 #define SNTP_RECV_TIMEOUT 3000 138 #ifndef SNTP_UPDATE_DELAY 139 #define SNTP_UPDATE_DELAY 3600000 141 #if (SNTP_UPDATE_DELAY < 15000) && !SNTP_SUPPRESS_DELAY_CHECK 142 #error "SNTPv4 RFC 4330 enforces a minimum update time of 15 seconds!" 146 #ifndef SNTP_SET_SYSTEM_TIME 147 #define SNTP_SET_SYSTEM_TIME(sec) ((void)sec) 151 #ifdef SNTP_SET_SYSTEM_TIME_US 152 #define SNTP_CALC_TIME_US 1 153 #define SNTP_RECEIVE_TIME_SIZE 2 155 #define SNTP_SET_SYSTEM_TIME_US(sec, us) 156 #define SNTP_CALC_TIME_US 0 157 #define SNTP_RECEIVE_TIME_SIZE 1 163 #ifndef SNTP_GET_SYSTEM_TIME 164 #define SNTP_GET_SYSTEM_TIME(sec, us) do { (sec) = 0; (us) = 0; } while(0) 171 #ifndef SNTP_RETRY_TIMEOUT 172 #define SNTP_RETRY_TIMEOUT SNTP_RECV_TIMEOUT 176 #ifndef SNTP_RETRY_TIMEOUT_MAX 177 #define SNTP_RETRY_TIMEOUT_MAX (SNTP_RETRY_TIMEOUT * 10) 183 #ifndef SNTP_RETRY_TIMEOUT_EXP 184 #define SNTP_RETRY_TIMEOUT_EXP 1 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) 194 #define SNTP_ERR_KOD 1 197 #define SNTP_MSG_LEN 48 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 206 #define SNTP_VERSION_MASK 0x38 207 #define SNTP_VERSION (4<<3) 209 #define SNTP_MODE_MASK 0x07 210 #define SNTP_MODE_CLIENT 0x03 211 #define SNTP_MODE_SERVER 0x04 212 #define SNTP_MODE_BROADCAST 0x05 214 #define SNTP_OFFSET_STRATUM 1 215 #define SNTP_STRATUM_KOD 0x00 217 #define SNTP_OFFSET_ORIGINATE_TIME 24 218 #define SNTP_OFFSET_RECEIVE_TIME 32 219 #define SNTP_OFFSET_TRANSMIT_TIME 40 222 #define DIFF_SEC_1900_1970 (2208988800UL) 232 #ifdef PACK_STRUCT_USE_INCLUDES 233 # include "arch/bpstruct.h" 236 #define PACK_STRUCT_FLD_8 PACK_STRUCT_FIELD 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);
251 #ifdef PACK_STRUCT_USE_INCLUDES 252 # include "arch/epstruct.h" 256 static void sntp_request(
void *arg);
259 static struct udp_pcb* sntp_pcb;
271 static u8_t sntp_set_servers_from_dhcp;
272 #if SNTP_SUPPORT_MULTIPLE_SERVERS 274 static u8_t sntp_current_server;
276 #define sntp_current_server 0 279 #if SNTP_RETRY_TIMEOUT_EXP 280 #define SNTP_RESET_RETRY_TIMEOUT() sntp_retry_timeout = SNTP_RETRY_TIMEOUT 282 static u32_t sntp_retry_timeout;
284 #define SNTP_RESET_RETRY_TIMEOUT() 285 #define sntp_retry_timeout SNTP_RETRY_TIMEOUT 288 #if SNTP_CHECK_RESPONSE >= 1 290 static ip_addr_t sntp_last_server_address;
293 #if SNTP_CHECK_RESPONSE >= 2 296 static u32_t sntp_last_timestamp_sent[2];
301 uint32 realtime_stamp = 0;
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 312 #define YEAR_BASE 1900 313 #define EPOCH_YEAR 1970 315 #define EPOCH_YEARS_SINCE_LEAP 2 316 #define EPOCH_YEARS_SINCE_CENTURY 70 317 #define EPOCH_YEARS_SINCE_LEAP_CENTURY 370 319 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0) 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}
329 static const int year_lengths[2] = {
347 typedef struct __tzrule_struct
358 __tzrule_type sntp__tzrule[2];
360 sntp_mktm_r(
const time_t * tim_p ,
struct tm *res ,
int is_gmtime)
372 days = ((long)lcltime) / SECSPERDAY;
373 rem = ((long)lcltime) % SECSPERDAY;
379 while (rem >= SECSPERDAY)
386 res->tm_hour = (int) (rem / SECSPERHOUR);
388 res->tm_min = (int) (rem / SECSPERMIN);
389 res->tm_sec = (int) (rem % SECSPERMIN);
392 if ((res->tm_wday = ((EPOCH_WDAY + days) % DAYSPERWEEK)) < 0)
393 res->tm_wday += DAYSPERWEEK;
402 if (days < year_lengths[yleap])
405 days -= year_lengths[yleap];
414 days += year_lengths[yleap];
418 res->tm_year = y - YEAR_BASE;
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;
428 int hours, mins, secs;
443 offset = (res->tm_isdst == 1 ? sntp__tzrule[1].offset : sntp__tzrule[0].offset);
445 hours = offset / SECSPERHOUR;
446 offset = offset % SECSPERHOUR;
448 mins = offset / SECSPERMIN;
449 secs = offset % SECSPERMIN;
453 res->tm_hour -= hours;
455 if (res->tm_sec >= SECSPERMIN)
458 res->tm_sec -= SECSPERMIN;
460 else if (res->tm_sec < 0)
463 res->tm_sec += SECSPERMIN;
465 if (res->tm_min >= MINSPERHOUR)
468 res->tm_min -= MINSPERHOUR;
470 else if (res->tm_min < 0)
473 res->tm_min += MINSPERHOUR;
475 if (res->tm_hour >= HOURSPERDAY)
479 if (res->tm_wday > 6)
482 res->tm_hour -= HOURSPERDAY;
483 if (res->tm_mday > ip[res->tm_mon])
485 res->tm_mday -= ip[res->tm_mon];
487 if (res->tm_mon == 12)
495 else if (res->tm_hour < 0)
499 if (res->tm_wday < 0)
503 if (res->tm_mday == 0)
510 res->tm_yday = 365 + isleap(res->tm_year);
512 res->tm_mday = ip[res->tm_mon];
523 sntp_localtime_r(
const time_t * tim_p ,
526 return sntp_mktm_r (tim_p, res, 0);
530 sntp_localtime(
const time_t * tim_p)
532 return sntp_localtime_r (tim_p, &res_buf);
537 sntp__tzcalc_limits(
int year)
539 int days, year_days, years;
542 if (year < EPOCH_YEAR)
547 years = (year - EPOCH_YEAR);
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;
553 for (i = 0; i < 2; ++i)
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;
561 int yleap = isleap(year);
562 int m_day, m_wday, wday_diff;
563 const int *ip = mon_lengths[yleap];
567 for (j = 1; j < sntp__tzrule[i].m; ++j)
570 m_wday = (EPOCH_WDAY + days) % DAYSPERWEEK;
572 wday_diff = sntp__tzrule[i].d - m_wday;
574 wday_diff += DAYSPERWEEK;
575 m_day = (sntp__tzrule[i].n - 1) * DAYSPERWEEK + wday_diff;
577 while (m_day >= ip[j-1])
578 m_day -= DAYSPERWEEK;
584 sntp__tzrule[i].change = days * SECSPERDAY + sntp__tzrule[i].s + sntp__tzrule[i].offset;
587 __tznorth = (sntp__tzrule[0].change < sntp__tzrule[1].change);
593 sntp_asctime_r(
struct tm *tim_p ,
char *result)
595 static const char day_name[7][4] = {
596 "Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat" 598 static const char mon_name[12][4] = {
599 "Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
600 "Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec" 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);
610 sntp_asctime(
struct tm *tim_p)
613 return sntp_asctime_r (tim_p, reult);
618 if(realtime_stamp == 0){
619 os_printf(
"please start sntp first !\n");
622 return realtime_stamp;
628 return sntp_asctime(sntp_localtime (&t));
645 if(timezone >= -11 || timezone <= 13) {
646 time_zone = timezone;
662 sntp_process(
u32_t *receive_timestamp)
667 time_t t = (
ntohl(receive_timestamp[0]) - DIFF_SEC_1900_1970);
669 #if SNTP_CALC_TIME_US 670 u32_t us =
ntohl(receive_timestamp[1]) / 4295;
671 SNTP_SET_SYSTEM_TIME_US(t, us);
673 LWIP_DEBUGF(SNTP_DEBUG_TRACE, (
"sntp_process: %s, %"U32_F" us", ctime(&t), us));
678 SNTP_SET_SYSTEM_TIME(t);
680 t += time_zone * 60 * 60;
685 os_printf(
"%s\n",sntp_asctime(sntp_localtime (&t)));
695 sntp_initialize_request(
struct sntp_msg *req)
698 req->li_vn_mode = SNTP_LI_NO_WARNING | SNTP_VERSION | SNTP_MODE_CLIENT;
700 #if SNTP_CHECK_RESPONSE >= 2 702 u32_t sntp_time_sec, sntp_time_us;
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];
708 sntp_last_timestamp_sent[1] =
htonl(sntp_time_us);
709 req->transmit_timestamp[1] = sntp_last_timestamp_sent[1];
720 sntp_retry(
void* arg)
724 LWIP_DEBUGF(SNTP_DEBUG_STATE, (
"sntp_retry: Next request will be sent in %"U32_F" ms\n",
725 sntp_retry_timeout));
730 #if SNTP_RETRY_TIMEOUT_EXP 732 u32_t new_retry_timeout;
734 new_retry_timeout = sntp_retry_timeout << 1;
736 if ((new_retry_timeout <= SNTP_RETRY_TIMEOUT_MAX) &&
737 (new_retry_timeout > sntp_retry_timeout)) {
738 sntp_retry_timeout = new_retry_timeout;
744 #if SNTP_SUPPORT_MULTIPLE_SERVERS 754 sntp_try_next_server(
void* arg)
759 old_server = sntp_current_server;
761 sntp_current_server++;
762 if (sntp_current_server >= SNTP_MAX_SERVERS) {
763 sntp_current_server = 0;
767 || (sntp_servers[sntp_current_server].name !=
NULL)
770 LWIP_DEBUGF(SNTP_DEBUG_STATE, (
"sntp_try_next_server: Sending request to server %"U16_F"\n",
771 (
u16_t)sntp_current_server));
773 SNTP_RESET_RETRY_TIMEOUT();
780 sntp_current_server = old_server;
785 #define sntp_try_next_server sntp_retry 790 sntp_recv(
void *arg,
struct udp_pcb* pcb,
struct pbuf *p,
ip_addr_t *addr,
u16_t port)
794 u32_t receive_timestamp[SNTP_RECEIVE_TIME_SIZE];
805 #if SNTP_CHECK_RESPONSE >= 1 807 if (
ip_addr_cmp(addr, &sntp_last_server_address) &&
815 if (p->
tot_len == SNTP_MSG_LEN) {
817 mode &= SNTP_MODE_MASK;
819 if ((mode == SNTP_MODE_SERVER) ||
820 (mode == SNTP_MODE_BROADCAST)) {
822 if (stratum == SNTP_STRATUM_KOD) {
825 LWIP_DEBUGF(SNTP_DEBUG_STATE, (
"sntp_recv: Received Kiss-of-Death\n"));
827 #if SNTP_CHECK_RESPONSE >= 2 829 u32_t originate_timestamp[2];
831 if ((originate_timestamp[0] != sntp_last_timestamp_sent[0]) ||
832 (originate_timestamp[1] != sntp_last_timestamp_sent[1]))
834 LWIP_DEBUGF(SNTP_DEBUG_WARN, (
"sntp_recv: Invalid originate timestamp in response\n"));
841 pbuf_copy_partial(p, &receive_timestamp, SNTP_RECEIVE_TIME_SIZE * 4, SNTP_OFFSET_RECEIVE_TIME);
854 SNTP_RESET_RETRY_TIMEOUT();
856 sntp_process(receive_timestamp);
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) {
864 sntp_try_next_server(
NULL);
876 sntp_send_request(
ip_addr_t *server_addr)
882 struct sntp_msg *sntpmsg = (
struct sntp_msg *)p->
payload;
883 LWIP_DEBUGF(SNTP_DEBUG_STATE, (
"sntp_send_request: Sending request to server\n"));
885 sntp_initialize_request(sntpmsg);
887 udp_sendto(sntp_pcb, p, server_addr, SNTP_PORT);
892 #if SNTP_CHECK_RESPONSE >= 1 894 ip_addr_set(&sntp_last_server_address, server_addr);
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));
909 sntp_dns_found(
const char* hostname,
ip_addr_t *ipaddr,
void *arg)
914 if (ipaddr !=
NULL) {
916 LWIP_DEBUGF(SNTP_DEBUG_STATE, (
"sntp_dns_found: Server address resolved, sending request\n"));
917 sntp_send_request(ipaddr);
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);
932 sntp_request(
void *arg)
942 if (sntp_servers[sntp_current_server].name) {
945 err = dns_gethostbyname(sntp_servers[sntp_current_server].name, &sntp_server_address,
946 sntp_dns_found,
NULL);
949 LWIP_DEBUGF(SNTP_DEBUG_STATE, (
"sntp_request: Waiting for server address to be resolved.\n"));
951 }
else if (err ==
ERR_OK) {
952 sntp_servers[sntp_current_server].addr = sntp_server_address;
957 sntp_server_address = sntp_servers[sntp_current_server].addr;
963 LWIP_DEBUGF(SNTP_DEBUG_TRACE, (
"sntp_request: current server address is %u.%u.%u.%u\n",
965 sntp_send_request(&sntp_server_address);
968 LWIP_DEBUGF(SNTP_DEBUG_WARN_STATE, (
"sntp_request: Invalid server address, trying next server.\n"));
980 #ifdef SNTP_SERVER_ADDRESS 984 #error SNTP_SERVER_ADDRESS string not supported SNTP_SERVER_DNS==0 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 1009 if (sntp_pcb !=
NULL) {
1011 udp_remove(sntp_pcb);
1018 #if SNTP_GET_SERVERS_FROM_DHCP 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;
1042 if (idx < SNTP_MAX_SERVERS) {
1043 if (server !=
NULL) {
1044 sntp_servers[idx].addr = (*server);
1050 sntp_servers[idx].name =
NULL;
1055 #if LWIP_DHCP && SNTP_GET_SERVERS_FROM_DHCP 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"),
1068 if (sntp_set_servers_from_dhcp && num) {
1070 for (i = 0; (i < num) && (i < SNTP_MAX_SERVERS); i++) {
1090 if (idx < SNTP_MAX_SERVERS) {
1091 return sntp_servers[idx].addr;
1106 if (idx < SNTP_MAX_SERVERS) {
1107 sntp_servers[idx].name = server;
1121 if (idx < SNTP_MAX_SERVERS) {
1122 return sntp_servers[idx].name;
#define ip_addr_set(dest, src)
uint32 sntp_get_current_timestamp()
#define ICACHE_FLASH_ATTR
char * sntp_getservername(u8_t idx)
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)
sint8 sntp_get_timezone(void)
#define LWIP_DEBUGF(debug, message)
#define ip_addr_cmp(addr1, addr2)
#define PACK_STRUCT_STRUCT
#define ip_addr_set_any(ipaddr)
typedefPACK_STRUCT_END struct ip_addr ip_addr_t
#define sntp_servermode_dhcp(x)
void sys_untimeout(sys_timeout_handler handler, void *arg) ICACHE_FLASH_ATTR
#define ip_addr_isany(addr1)
#define ip4_addr3(ipaddr)
u8_t pbuf_free(struct pbuf *p) ICACHE_FLASH_ATTR
#define PACK_STRUCT_BEGIN
void sntp_setservername(u8_t idx, char *server)
#define os_timer_arm(a, b, c)
#define LWIP_ASSERT(message, assertion)
u16_t pbuf_copy_partial(struct pbuf *p, void *dataptr, u16_t len, u16_t offset) ICACHE_FLASH_ATTR
bool sntp_set_timezone(sint8 timezone)
ip_addr_t sntp_getserver(u8_t idx)
#define ip4_addr2(ipaddr)
char * sntp_get_real_time(long t)
#define ip4_addr4(ipaddr)
#define ip4_addr1(ipaddr)
#define LWIP_UNUSED_ARG(x)
struct pbuf * pbuf_alloc(pbuf_layer l, u16_t length, pbuf_type type) ICACHE_FLASH_ATTR
#define PACK_STRUCT_FIELD(x)