sntp.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include "sntp.h"
  2. #include "rtc.h"
  3. #include "settings_api.h"
  4. #include <string.h>
  5. #include <time.h>
  6. #include "FreeRTOS.h"
  7. #include "task.h"
  8. #ifdef PRINTF_STDLIB
  9. #include <stdio.h>
  10. #endif
  11. #ifdef PRINTF_CUSTOM
  12. #include "tinystdio.h"
  13. #endif
  14. #define SENDFAIL_TIMEOUT 5000 /* 5 seconds */
  15. #define SENT_TIMEOUT 60000 /* 1 minute */
  16. #define BADREPLY_TIMEOUT 60000 /* 1 minute */
  17. #define VALID_TIMEOUT (8 * 3600000) /* 8 hours */
  18. /* number of seconds between 1900 and 1970 */
  19. #define DIFF_SEC_1900_1970 (2208988800UL)
  20. struct sntp_packet
  21. {
  22. uint8_t status;
  23. uint8_t stratum;
  24. uint8_t ppoll;
  25. uint8_t precision;
  26. uint32_t distance;
  27. uint32_t dispersion;
  28. uint32_t refid;
  29. uint64_t reftime;
  30. uint64_t org;
  31. uint64_t rec;
  32. uint64_t xmt;
  33. };
  34. /*
  35. static unsigned int timeout;
  36. static struct udp_pcb* upcb;
  37. static struct ip_addr server;
  38. static int port = 123;
  39. */
  40. /**
  41. * @brief Общая структура настроек
  42. */
  43. extern SETTINGS_t sSettings;
  44. /**
  45. * @brief Разовая синхронизация времени при старте контроллера
  46. */
  47. //extern TaskHandle_t xHandleSntpOnceSinhro;
  48. /**
  49. * @brief Синхронизация времени единоразово при включении контроллера
  50. * @retval
  51. */
  52. void vTaskOnceSynchro(void *arg)
  53. {
  54. for (;;)
  55. {
  56. if (sSettings.sSNTP.sntpEnable)
  57. {
  58. vTaskDelay(7000);
  59. //SNTP_Poll();
  60. //printf("Once time sinhro\n\r");
  61. //vTaskDelete(xHandleSntpOnceSinhro);
  62. }
  63. else
  64. {
  65. vTaskDelay(7000);
  66. }
  67. //vTaskDelete(xHandleSntpOnceSinhro);
  68. }
  69. }
  70. /**
  71. * @brief Периодическая синхронизация времени.
  72. * Выполняется раз в сутки с 0 часов.
  73. * @retval
  74. */
  75. void vTaskPeriodicSynchro(void *arg)
  76. {
  77. TM_RTC_t data;
  78. static uint8_t fSinhro = 0;
  79. for (;;)
  80. {
  81. vTaskDelay(10000);
  82. #if 0
  83. if (sSettings.sSNTP.sntpEnable)
  84. {
  85. TM_RTC_GetDateTime(&data, TM_RTC_Format_BIN);
  86. /* Если пришло время синхронизации */
  87. if ((data.hours == 0) && (fSinhro == 0))
  88. {
  89. SNTP_Poll();
  90. fSinhro = 1;
  91. //printf("Periodic time sinhro\n\r");
  92. }
  93. if (data.hours > 1)
  94. fSinhro = 0;
  95. }
  96. #endif
  97. }
  98. }
  99. /**
  100. * @brief Отладочный таск. Выводим время в консоль.
  101. * @retval
  102. */
  103. void vTaskSntp(void *arg)
  104. {
  105. TM_RTC_t data;
  106. for (;;)
  107. {
  108. vTaskDelay(1000);
  109. TM_RTC_GetDateTime(&data, TM_RTC_Format_BIN);
  110. printf("%d.%d.%d %d:%d:%d \n\r", data.date,data.month,data.year,data.hours,data.minutes,data.seconds);
  111. }
  112. }
  113. /**
  114. * @brief Инициализация SNTP.
  115. * @retval
  116. */
  117. void SNTP_Init(void)
  118. {
  119. //SNTP_SetServerAddr(sSettings.sSNTP.ip);
  120. //SNTP_Enable(sSettings.sSNTP.sntpEnable);
  121. }
  122. #if 0
  123. static void recv(void *arg, struct udp_pcb *upcb, struct pbuf *p,
  124. struct ip_addr *addr, u16_t port)
  125. {
  126. time_t t;
  127. int utcSec = 0;
  128. TM_RTC_t data;
  129. if (p->len == sizeof(struct sntp_packet))
  130. {
  131. int i;
  132. struct sntp_packet aligned;
  133. //myassert(p->len == p->tot_len); /* don't accept chained pbuf */
  134. memcpy(&aligned, p->payload, sizeof(aligned));
  135. i = (aligned.status >> 3) & 7;
  136. if ((i < 1) || (i > 4)) /* SNTP version 1..4 */
  137. goto out;
  138. i = aligned.status & 7;
  139. if ((i != 4) && (i != 5)) /* mode 4 or 5: server or broadcast */
  140. goto out;
  141. if (aligned.xmt == 0)
  142. goto out;
  143. utcSec = (int)(3600.0*sSettings.sSNTP.timeZone);
  144. t = (ntohl(aligned.xmt) - 2208988800 + utcSec );
  145. TM_RTC_SetDataTimeUnix((uint32_t)t);
  146. /* Сохраним время последней синхронизации */
  147. TM_RTC_GetDateTime(&data, TM_RTC_Format_BIN);
  148. memset(sSettings.sSNTP.data, 0, sizeof(sSettings.sSNTP.data));
  149. sprintf(sSettings.sSNTP.data, "%02d.%02d.%02d %02d:%02d:%02d",
  150. data.date, data.month, data.year,
  151. data.hours, data.minutes, data.seconds);
  152. timeout = VALID_TIMEOUT;
  153. }
  154. out:
  155. pbuf_free(p);
  156. }
  157. #endif
  158. void SNTP_Enable(bool enable)
  159. { /*
  160. if (enable)
  161. {
  162. if (upcb == 0)
  163. {
  164. err_t ret;
  165. upcb = udp_new();
  166. if (upcb != 0)
  167. {
  168. ret = udp_bind(upcb, IP_ADDR_ANY, port);
  169. if (ret != ERR_OK)
  170. {
  171. udp_remove(upcb);
  172. upcb = 0;
  173. }
  174. else
  175. {
  176. udp_recv(upcb, recv, 0);
  177. }
  178. timeout = 0;
  179. }
  180. }
  181. }
  182. else if (upcb != 0)
  183. {
  184. udp_remove(upcb);
  185. upcb = 0;
  186. }
  187. */
  188. }
  189. bool SNTP_IsEnabled(void)
  190. {
  191. //return upcb != 0;
  192. }
  193. void SNTP_SetServerAddr(char *addr)
  194. {
  195. //server.addr = ipaddr_addr(addr);
  196. }
  197. int sntp_getserverport(void)
  198. {
  199. //return port;
  200. }
  201. static void send_request(void)
  202. {
  203. #if 0
  204. struct sntp_packet packet;
  205. struct pbuf* psend;
  206. memset(&packet, 0, sizeof(packet));
  207. packet.status = (3 << 3) /* SNTP vesion 3 */ | (3 << 0); /* Mode: client */
  208. psend = pbuf_alloc(PBUF_RAW, sizeof(packet), PBUF_REF);
  209. if (psend != 0)
  210. {
  211. psend->payload = &packet;
  212. timeout = (udp_sendto(upcb, psend, &server, port) == ERR_OK) ? SENT_TIMEOUT : SENDFAIL_TIMEOUT;
  213. pbuf_free(psend);
  214. }
  215. #endif
  216. }
  217. void SNTP_Poll(void)
  218. { /*
  219. if (upcb)
  220. send_request();*/
  221. }