sntp_opts.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /**
  2. * @file
  3. * SNTP client options list
  4. */
  5. /*
  6. * Copyright (c) 2007-2009 Frédéric Bernon, Simon Goldschmidt
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * 3. The name of the author may not be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  21. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  22. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  23. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  25. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  28. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  29. * OF SUCH DAMAGE.
  30. *
  31. * This file is part of the lwIP TCP/IP stack.
  32. *
  33. * Author: Frédéric Bernon, Simon Goldschmidt
  34. *
  35. */
  36. #ifndef LWIP_HDR_APPS_SNTP_OPTS_H
  37. #define LWIP_HDR_APPS_SNTP_OPTS_H
  38. #include "lwip/opt.h"
  39. #include "lwip/prot/iana.h"
  40. /**
  41. * @defgroup sntp_opts Options
  42. * @ingroup sntp
  43. * @{
  44. */
  45. /** SNTP macro to change system time in seconds
  46. * Define SNTP_SET_SYSTEM_TIME_US(sec, us) to set the time in microseconds
  47. * instead of this one if you need the additional precision. Alternatively,
  48. * define SNTP_SET_SYSTEM_TIME_NTP(sec, frac) in order to work with native
  49. * NTP timestamps instead.
  50. */
  51. #if !defined SNTP_SET_SYSTEM_TIME || defined __DOXYGEN__
  52. #define SNTP_SET_SYSTEM_TIME(sec) LWIP_UNUSED_ARG(sec)
  53. #endif
  54. /** The maximum number of SNTP servers that can be set */
  55. #if !defined SNTP_MAX_SERVERS || defined __DOXYGEN__
  56. #define SNTP_MAX_SERVERS LWIP_DHCP_MAX_NTP_SERVERS
  57. #endif
  58. /** Set this to 1 to implement the callback function called by dhcp when
  59. * NTP servers are received. */
  60. #if !defined SNTP_GET_SERVERS_FROM_DHCP || defined __DOXYGEN__
  61. #define SNTP_GET_SERVERS_FROM_DHCP LWIP_DHCP_GET_NTP_SRV
  62. #endif
  63. /** Set this to 1 to implement the callback function called by dhcpv6 when
  64. * NTP servers are received. */
  65. #if !defined SNTP_GET_SERVERS_FROM_DHCPV6 || defined __DOXYGEN__
  66. #define SNTP_GET_SERVERS_FROM_DHCPV6 LWIP_DHCP6_GET_NTP_SRV
  67. #endif
  68. /** Set this to 1 to support DNS names (or IP address strings) to set sntp servers
  69. * One server address/name can be defined as default if SNTP_SERVER_DNS == 1:
  70. * \#define SNTP_SERVER_ADDRESS "pool.ntp.org"
  71. */
  72. #if !defined SNTP_SERVER_DNS || defined __DOXYGEN__
  73. #define SNTP_SERVER_DNS 0
  74. #endif
  75. /**
  76. * SNTP_DEBUG: Enable debugging for SNTP.
  77. */
  78. #if !defined SNTP_DEBUG || defined __DOXYGEN__
  79. #define SNTP_DEBUG LWIP_DBG_OFF
  80. #endif
  81. /** SNTP server port */
  82. #if !defined SNTP_PORT || defined __DOXYGEN__
  83. #define SNTP_PORT LWIP_IANA_PORT_SNTP
  84. #endif
  85. /** Sanity check:
  86. * Define this to
  87. * - 0 to turn off sanity checks (default; smaller code)
  88. * - >= 1 to check address and port of the response packet to ensure the
  89. * response comes from the server we sent the request to.
  90. * - >= 2 to check returned Originate Timestamp against Transmit Timestamp
  91. * sent to the server (to ensure response to older request).
  92. * - >= 3 @todo: discard reply if any of the VN, Stratum, or Transmit Timestamp
  93. * fields is 0 or the Mode field is not 4 (unicast) or 5 (broadcast).
  94. * - >= 4 @todo: to check that the Root Delay and Root Dispersion fields are each
  95. * greater than or equal to 0 and less than infinity, where infinity is
  96. * currently a cozy number like one second. This check avoids using a
  97. * server whose synchronization source has expired for a very long time.
  98. */
  99. #if !defined SNTP_CHECK_RESPONSE || defined __DOXYGEN__
  100. #define SNTP_CHECK_RESPONSE 0
  101. #endif
  102. /** Enable round-trip delay compensation.
  103. * Compensate for the round-trip delay by calculating the clock offset from
  104. * the originate, receive, transmit and destination timestamps, as per RFC.
  105. *
  106. * The calculation requires compiler support for 64-bit integers. Also, either
  107. * SNTP_SET_SYSTEM_TIME_US or SNTP_SET_SYSTEM_TIME_NTP has to be implemented
  108. * for setting the system clock with sub-second precision. Likewise, either
  109. * SNTP_GET_SYSTEM_TIME or SNTP_GET_SYSTEM_TIME_NTP needs to be implemented
  110. * with sub-second precision.
  111. *
  112. * Although not strictly required, it makes sense to combine this option with
  113. * SNTP_CHECK_RESPONSE >= 2 for sanity-checking of the received timestamps.
  114. * Also, in order for the round-trip calculation to work, the difference
  115. * between the local clock and the NTP server clock must not be larger than
  116. * about 34 years. If that limit is exceeded, the implementation will fall back
  117. * to setting the clock without compensation. In order to ensure that the local
  118. * clock is always within the permitted range for compensation, even at first
  119. * try, it may be necessary to store at least the current year in non-volatile
  120. * memory.
  121. */
  122. #if !defined SNTP_COMP_ROUNDTRIP || defined __DOXYGEN__
  123. #define SNTP_COMP_ROUNDTRIP 0
  124. #endif
  125. /** According to the RFC, this shall be a random delay
  126. * between 1 and 5 minutes (in milliseconds) to prevent load peaks.
  127. * This can be defined to a random generation function,
  128. * which must return the delay in milliseconds as u32_t.
  129. * Turned off by default.
  130. */
  131. #if !defined SNTP_STARTUP_DELAY || defined __DOXYGEN__
  132. #ifdef LWIP_RAND
  133. #define SNTP_STARTUP_DELAY 1
  134. #else
  135. #define SNTP_STARTUP_DELAY 0
  136. #endif
  137. #endif
  138. /** If you want the startup delay to be a function, define this
  139. * to a function (including the brackets) and define SNTP_STARTUP_DELAY to 1.
  140. */
  141. #if !defined SNTP_STARTUP_DELAY_FUNC || defined __DOXYGEN__
  142. #define SNTP_STARTUP_DELAY_FUNC (LWIP_RAND() % 5000)
  143. #endif
  144. /** SNTP receive timeout - in milliseconds
  145. * Also used as retry timeout - this shouldn't be too low.
  146. * Default is 15 seconds. Must not be below 15 seconds by specification (i.e. 15000)
  147. */
  148. #if !defined SNTP_RECV_TIMEOUT || defined __DOXYGEN__
  149. #define SNTP_RECV_TIMEOUT 15000
  150. #endif
  151. /** SNTP update delay - in milliseconds
  152. * Default is 1 hour. Must not be below 60 seconds by specification (i.e. 60000)
  153. */
  154. #if !defined SNTP_UPDATE_DELAY || defined __DOXYGEN__
  155. #define SNTP_UPDATE_DELAY 3600000
  156. #endif
  157. /** SNTP macro to get system time, used with SNTP_CHECK_RESPONSE >= 2
  158. * to send in request and compare in response. Also used for round-trip
  159. * delay compensation if SNTP_COMP_ROUNDTRIP != 0.
  160. * Alternatively, define SNTP_GET_SYSTEM_TIME_NTP(sec, frac) in order to
  161. * work with native NTP timestamps instead.
  162. */
  163. #if !defined SNTP_GET_SYSTEM_TIME || defined __DOXYGEN__
  164. #define SNTP_GET_SYSTEM_TIME(sec, us) do { (sec) = 0; (us) = 0; } while(0)
  165. #endif
  166. /** Default retry timeout (in milliseconds) if the response
  167. * received is invalid.
  168. * This is doubled with each retry until SNTP_RETRY_TIMEOUT_MAX is reached.
  169. */
  170. #if !defined SNTP_RETRY_TIMEOUT || defined __DOXYGEN__
  171. #define SNTP_RETRY_TIMEOUT SNTP_RECV_TIMEOUT
  172. #endif
  173. /** Maximum retry timeout (in milliseconds). */
  174. #if !defined SNTP_RETRY_TIMEOUT_MAX || defined __DOXYGEN__
  175. #define SNTP_RETRY_TIMEOUT_MAX (SNTP_RETRY_TIMEOUT * 10)
  176. #endif
  177. /** Increase retry timeout with every retry sent
  178. * Default is on to conform to RFC.
  179. */
  180. #if !defined SNTP_RETRY_TIMEOUT_EXP || defined __DOXYGEN__
  181. #define SNTP_RETRY_TIMEOUT_EXP 1
  182. #endif
  183. /** Keep a reachability shift register per server
  184. * Default is on to conform to RFC.
  185. */
  186. #if !defined SNTP_MONITOR_SERVER_REACHABILITY || defined __DOXYGEN__
  187. #define SNTP_MONITOR_SERVER_REACHABILITY 1
  188. #endif
  189. /**
  190. * @}
  191. */
  192. #endif /* LWIP_HDR_APPS_SNTP_OPTS_H */