1
0

sntp.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /**
  2. * @file
  3. * SNTP client module
  4. *
  5. */
  6. /*
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted provided that the following conditions are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * 3. The name of the author may not be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  19. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  20. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  21. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  23. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  26. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  27. * OF SUCH DAMAGE.
  28. *
  29. * This file is part of the lwIP TCP/IP stack.
  30. *
  31. */
  32. #include "lwip/sys.h"
  33. #include "lwip/sockets.h"
  34. #include <string.h>
  35. #include <time.h>
  36. /** This is an example of a "SNTP" client (with socket API).
  37. *
  38. * For a list of some public NTP servers, see this link :
  39. * http://support.ntp.org/bin/view/Servers/NTPPoolServers
  40. *
  41. */
  42. /**
  43. * SNTP_DEBUG: Enable debugging for SNTP.
  44. */
  45. #ifndef SNTP_DEBUG
  46. #define SNTP_DEBUG LWIP_DBG_ON
  47. #endif
  48. /** SNTP server port */
  49. #ifndef SNTP_PORT
  50. #define SNTP_PORT 123
  51. #endif
  52. /** SNTP server address as IPv4 address in "u32_t" format */
  53. #ifndef SNTP_SERVER_ADDRESS
  54. #define SNTP_SERVER_ADDRESS inet_addr("213.161.194.93") /* pool.ntp.org */
  55. #endif
  56. /** SNTP receive timeout - in milliseconds */
  57. #ifndef SNTP_RECV_TIMEOUT
  58. #define SNTP_RECV_TIMEOUT 3000
  59. #endif
  60. /** SNTP update delay - in milliseconds */
  61. #ifndef SNTP_UPDATE_DELAY
  62. #define SNTP_UPDATE_DELAY 60000
  63. #endif
  64. /** SNTP macro to change system time and/or the update the RTC clock */
  65. #ifndef SNTP_SYSTEM_TIME
  66. #define SNTP_SYSTEM_TIME(t)
  67. #endif
  68. /* SNTP protocol defines */
  69. #define SNTP_MAX_DATA_LEN 48
  70. #define SNTP_RCV_TIME_OFS 32
  71. #define SNTP_LI_NO_WARNING 0x00
  72. #define SNTP_VERSION (4/* NTP Version 4*/<<3)
  73. #define SNTP_MODE_CLIENT 0x03
  74. #define SNTP_MODE_SERVER 0x04
  75. #define SNTP_MODE_BROADCAST 0x05
  76. #define SNTP_MODE_MASK 0x07
  77. /* number of seconds between 1900 and 1970 */
  78. #define DIFF_SEC_1900_1970 (2208988800)
  79. /**
  80. * SNTP processing
  81. */
  82. static void sntp_process( time_t t)
  83. {
  84. /* change system time and/or the update the RTC clock */
  85. SNTP_SYSTEM_TIME(t);
  86. /* display local time from GMT time */
  87. LWIP_DEBUGF( SNTP_DEBUG, ("sntp_process: %s", ctime(&t)));
  88. }
  89. /**
  90. * SNTP request
  91. */
  92. static void sntp_request()
  93. {
  94. int sock;
  95. struct sockaddr_in local;
  96. struct sockaddr_in to;
  97. int tolen;
  98. int size;
  99. int timeout;
  100. u8_t sntp_request [SNTP_MAX_DATA_LEN];
  101. u8_t sntp_response[SNTP_MAX_DATA_LEN];
  102. u32_t sntp_server_address;
  103. u32_t timestamp;
  104. time_t t;
  105. /* initialize SNTP server address */
  106. sntp_server_address = SNTP_SERVER_ADDRESS;
  107. /* if we got a valid SNTP server address... */
  108. if (sntp_server_address!=0)
  109. {
  110. /* create new socket */
  111. sock = socket( AF_INET, SOCK_DGRAM, 0);
  112. if (sock>=0)
  113. {
  114. /* prepare local address */
  115. memset(&local, 0, sizeof(local));
  116. local.sin_family = AF_INET;
  117. local.sin_port = htons(INADDR_ANY);
  118. local.sin_addr.s_addr = htonl(INADDR_ANY);
  119. /* bind to local address */
  120. if (bind( sock, (struct sockaddr *)&local, sizeof(local))==0)
  121. {
  122. /* set recv timeout */
  123. timeout = SNTP_RECV_TIMEOUT;
  124. setsockopt( sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
  125. /* prepare SNTP request */
  126. memset( sntp_request, 0, sizeof(sntp_request));
  127. sntp_request[0] = SNTP_LI_NO_WARNING | SNTP_VERSION | SNTP_MODE_CLIENT;
  128. /* prepare SNTP server address */
  129. memset(&to, 0, sizeof(to));
  130. to.sin_family = AF_INET;
  131. to.sin_port = htons(SNTP_PORT);
  132. to.sin_addr.s_addr = sntp_server_address;
  133. /* send SNTP request to server */
  134. if (sendto( sock, sntp_request, sizeof(sntp_request), 0, (struct sockaddr *)&to, sizeof(to))>=0)
  135. {
  136. /* receive SNTP server response */
  137. tolen = sizeof(to);
  138. size = recvfrom( sock, sntp_response, sizeof(sntp_response), 0, (struct sockaddr *)&to, (socklen_t *)&tolen);
  139. /* if the response size is good */
  140. if (size == SNTP_MAX_DATA_LEN)
  141. {
  142. /* if this is a SNTP response... */
  143. if (((sntp_response[0] & SNTP_MODE_MASK) == SNTP_MODE_SERVER) || ((sntp_response[0] & SNTP_MODE_MASK) == SNTP_MODE_BROADCAST))
  144. {
  145. /* extract GMT time from response */
  146. SMEMCPY( &timestamp, (sntp_response+SNTP_RCV_TIME_OFS), sizeof(timestamp));
  147. t = (ntohl(timestamp) - DIFF_SEC_1900_1970);
  148. /* do time processing */
  149. sntp_process(t);
  150. }
  151. else
  152. {
  153. LWIP_DEBUGF( SNTP_DEBUG, ("sntp_request: not response frame code\n"));
  154. }
  155. }
  156. else
  157. {
  158. LWIP_DEBUGF( SNTP_DEBUG, ("sntp_request: not recvfrom==%i\n", errno));
  159. }
  160. }
  161. else
  162. {
  163. LWIP_DEBUGF( SNTP_DEBUG, ("sntp_request: not sendto==%i\n", errno));
  164. }
  165. }
  166. /* close the socket */
  167. closesocket(sock);
  168. }
  169. }
  170. }
  171. /**
  172. * SNTP thread
  173. */
  174. static void
  175. sntp_thread(void *arg)
  176. {
  177. LWIP_UNUSED_ARG(arg);
  178. while(1)
  179. {
  180. sntp_request();
  181. sys_msleep(SNTP_UPDATE_DELAY);
  182. }
  183. }
  184. void sntp_init(void)
  185. {
  186. sys_thread_new("sntp_thread", sntp_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
  187. }