sntp.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include "sntp.h"
  2. #include "rtc.h"
  3. #include "settings_api.h"
  4. #include <string.h>
  5. #include <time.h>
  6. #ifdef PRINTF_STDLIB
  7. #include <stdio.h>
  8. #endif
  9. #ifdef PRINTF_CUSTOM
  10. #include "tinystdio.h"
  11. #endif
  12. #define SENDFAIL_TIMEOUT 5000 /* 5 seconds */
  13. #define SENT_TIMEOUT 60000 /* 1 minute */
  14. #define BADREPLY_TIMEOUT 60000 /* 1 minute */
  15. #define VALID_TIMEOUT (8 * 3600000) /* 8 hours */
  16. /* number of seconds between 1900 and 1970 */
  17. #define DIFF_SEC_1900_1970 (2208988800UL)
  18. struct sntp_packet
  19. {
  20. uint8_t status;
  21. uint8_t stratum;
  22. uint8_t ppoll;
  23. uint8_t precision;
  24. uint32_t distance;
  25. uint32_t dispersion;
  26. uint32_t refid;
  27. uint64_t reftime;
  28. uint64_t org;
  29. uint64_t rec;
  30. uint64_t xmt;
  31. };
  32. static unsigned int timeout;
  33. static struct udp_pcb* upcb;
  34. static struct ip_addr server;
  35. static int port = 123;
  36. extern bool ntpResult;
  37. extern uint32_t SNTP_Time;
  38. /**
  39. * @brief Отладочный таск. Выводим время в консоль.
  40. * @retval
  41. */
  42. void vTaskSntp(void *arg)
  43. {
  44. TM_RTC_t data;
  45. for (;;)
  46. {
  47. vTaskDelay(1000);
  48. TM_RTC_GetDateTime(&data, TM_RTC_Format_BIN);
  49. printf("%d.%d.%d %d:%d:%d \n\r", data.date,data.month,data.year,data.hours,data.minutes,data.seconds);
  50. }
  51. }
  52. static void recv(void *arg, struct udp_pcb *upcb, struct pbuf *p,
  53. struct ip_addr *addr, u16_t port)
  54. {
  55. time_t t;
  56. //struct tm *time;
  57. //struct tm *ptrTime = &time;
  58. if (p->len == sizeof(struct sntp_packet))
  59. {
  60. int i;
  61. struct sntp_packet aligned;
  62. //myassert(p->len == p->tot_len); /* don't accept chained pbuf */
  63. memcpy(&aligned, p->payload, sizeof(aligned));
  64. i = (aligned.status >> 3) & 7;
  65. if ((i < 1) || (i > 4)) /* SNTP version 1..4 */
  66. goto out;
  67. i = aligned.status & 7;
  68. if ((i != 4) && (i != 5)) /* mode 4 or 5: server or broadcast */
  69. goto out;
  70. if (aligned.xmt == 0)
  71. goto out;
  72. t = (ntohl(aligned.xmt) - 2208988800);
  73. TM_RTC_SetDataTimeUnix((uint32_t)t);
  74. //time = __localtime32(&t);
  75. SNTP_Time = t;
  76. ntpResult = true;
  77. //printf("%s\r\n", asctime(time));
  78. timeout = VALID_TIMEOUT;
  79. }
  80. out:
  81. pbuf_free(p);
  82. }
  83. void SNTP_Enable(bool enable)
  84. {
  85. if (enable)
  86. {
  87. if (upcb == 0)
  88. {
  89. err_t ret;
  90. upcb = udp_new();
  91. if (upcb != 0)
  92. {
  93. ret = udp_bind(upcb, IP_ADDR_ANY, port);
  94. if (ret != ERR_OK)
  95. {
  96. udp_remove(upcb);
  97. upcb = 0;
  98. }
  99. else
  100. {
  101. udp_recv(upcb, recv, 0);
  102. }
  103. timeout = 0;
  104. }
  105. }
  106. }
  107. else if (upcb != 0)
  108. {
  109. udp_remove(upcb);
  110. upcb = 0;
  111. }
  112. }
  113. bool SNTP_IsEnabled(void)
  114. {
  115. return upcb != 0;
  116. }
  117. void SNTP_SetServerAddr(char *addr)
  118. {
  119. server.addr = ipaddr_addr(addr);
  120. }
  121. int sntp_getserverport(void)
  122. {
  123. return port;
  124. }
  125. static void send_request(void)
  126. {
  127. struct sntp_packet packet;
  128. struct pbuf* psend;
  129. memset(&packet, 0, sizeof(packet));
  130. packet.status = (3 << 3) /* SNTP vesion 3 */ | (3 << 0); /* Mode: client */
  131. psend = pbuf_alloc(PBUF_RAW, sizeof(packet), PBUF_REF);
  132. if (psend != 0)
  133. {
  134. psend->payload = &packet;
  135. timeout = (udp_sendto(upcb, psend, &server, port) == ERR_OK) ? SENT_TIMEOUT : SENDFAIL_TIMEOUT;
  136. pbuf_free(psend);
  137. }
  138. }
  139. void SNTP_Poll(void)
  140. {
  141. if (upcb)
  142. send_request();
  143. }