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