A time interface class. This class replicates the normal time functions, but goes a couple of steps further. mbed library 82 and prior has a defective gmtime function. Also, this class enables access to setting the time, and adjusting the accuracy of the RTC.

Dependencies:   CalendarPage

Dependents:   CI-data-logger-server WattEye X10Svr SSDP_Server

Committer:
WiredHome
Date:
Sun Sep 13 15:06:14 2020 +0000
Revision:
33:e49b25bdbfa5
Parent:
21:f3818e2e0370
Correct gmtime and localtime apis.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 21:f3818e2e0370 1 /* NTPClient.h */
WiredHome 21:f3818e2e0370 2 /* Copyright (C) 2012 mbed.org, MIT License
WiredHome 21:f3818e2e0370 3 *
WiredHome 21:f3818e2e0370 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
WiredHome 21:f3818e2e0370 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
WiredHome 21:f3818e2e0370 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
WiredHome 21:f3818e2e0370 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
WiredHome 21:f3818e2e0370 8 * furnished to do so, subject to the following conditions:
WiredHome 21:f3818e2e0370 9 *
WiredHome 21:f3818e2e0370 10 * The above copyright notice and this permission notice shall be included in all copies or
WiredHome 21:f3818e2e0370 11 * substantial portions of the Software.
WiredHome 21:f3818e2e0370 12 *
WiredHome 21:f3818e2e0370 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
WiredHome 21:f3818e2e0370 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
WiredHome 21:f3818e2e0370 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
WiredHome 21:f3818e2e0370 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
WiredHome 21:f3818e2e0370 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
WiredHome 21:f3818e2e0370 18 *
WiredHome 21:f3818e2e0370 19 */
WiredHome 21:f3818e2e0370 20
WiredHome 21:f3818e2e0370 21 /** \file
WiredHome 21:f3818e2e0370 22 NTP Client header file
WiredHome 21:f3818e2e0370 23 */
WiredHome 21:f3818e2e0370 24
WiredHome 21:f3818e2e0370 25 #ifndef NTPCLIENT_H_
WiredHome 21:f3818e2e0370 26 #define NTPCLIENT_H_
WiredHome 21:f3818e2e0370 27
WiredHome 21:f3818e2e0370 28 #include <cstdint>
WiredHome 21:f3818e2e0370 29
WiredHome 21:f3818e2e0370 30 using std::uint8_t;
WiredHome 21:f3818e2e0370 31 using std::uint16_t;
WiredHome 21:f3818e2e0370 32 using std::uint32_t;
WiredHome 21:f3818e2e0370 33
WiredHome 21:f3818e2e0370 34 #include "EthernetInterface.h"
WiredHome 21:f3818e2e0370 35 #include "UDPSocket.h"
WiredHome 21:f3818e2e0370 36
WiredHome 21:f3818e2e0370 37 #define NTP_DEFAULT_PORT 123
WiredHome 21:f3818e2e0370 38 #define NTP_DEFAULT_TIMEOUT 4000
WiredHome 21:f3818e2e0370 39
WiredHome 21:f3818e2e0370 40 ///NTP client results
WiredHome 21:f3818e2e0370 41 enum NTPResult {
WiredHome 21:f3818e2e0370 42 NTP_OK = 0, ///<Success
WiredHome 21:f3818e2e0370 43 NTP_DNS, ///<Could not resolve name
WiredHome 21:f3818e2e0370 44 NTP_PRTCL, ///<Protocol error
WiredHome 21:f3818e2e0370 45 NTP_TIMEOUT, ///<Connection timeout
WiredHome 21:f3818e2e0370 46 NTP_CONN, ///<Connection error
WiredHome 21:f3818e2e0370 47 };
WiredHome 21:f3818e2e0370 48
WiredHome 21:f3818e2e0370 49 /** NTP Client to update the mbed's RTC using a remote time server
WiredHome 21:f3818e2e0370 50 *
WiredHome 21:f3818e2e0370 51 */
WiredHome 21:f3818e2e0370 52 class NTPClient
WiredHome 21:f3818e2e0370 53 {
WiredHome 21:f3818e2e0370 54 public:
WiredHome 21:f3818e2e0370 55 /**
WiredHome 21:f3818e2e0370 56 Instantiate the NTP client
WiredHome 21:f3818e2e0370 57 * @param[in] net is a pointer to the EthernetInterface
WiredHome 21:f3818e2e0370 58 */
WiredHome 21:f3818e2e0370 59 NTPClient(EthernetInterface * net = NULL);
WiredHome 21:f3818e2e0370 60
WiredHome 21:f3818e2e0370 61 /**Get current time (blocking)
WiredHome 21:f3818e2e0370 62 Update the time using the server host
WiredHome 21:f3818e2e0370 63 Blocks until completion
WiredHome 21:f3818e2e0370 64 @param[in] host NTP server IPv4 address or hostname (will be resolved via DNS)
WiredHome 21:f3818e2e0370 65 @param[in] port port to use; defaults to 123
WiredHome 21:f3818e2e0370 66 @param[in] timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
WiredHome 21:f3818e2e0370 67 @return 0 on success, NTP error code (<0) on failure
WiredHome 21:f3818e2e0370 68 */
WiredHome 21:f3818e2e0370 69 NTPResult setTime(const char* host, uint16_t port = NTP_DEFAULT_PORT, uint32_t timeout = NTP_DEFAULT_TIMEOUT); //Blocking
WiredHome 21:f3818e2e0370 70
WiredHome 21:f3818e2e0370 71 private:
WiredHome 21:f3818e2e0370 72 EthernetInterface * net;
WiredHome 21:f3818e2e0370 73
WiredHome 21:f3818e2e0370 74 /// The NTP Packet, as defined in RFC 4330 for Simple NTP
WiredHome 21:f3818e2e0370 75 ///
WiredHome 21:f3818e2e0370 76 /// @caution We are in Little Endian locally, and Big Endian at the Network
WiredHome 21:f3818e2e0370 77 /// so some transformations are required.
WiredHome 21:f3818e2e0370 78 ///
WiredHome 21:f3818e2e0370 79 struct NTPPacket {
WiredHome 21:f3818e2e0370 80 /// Mode is a 3-bit number indicating the protocol mode
WiredHome 21:f3818e2e0370 81 ///
WiredHome 21:f3818e2e0370 82 /// values
WiredHome 21:f3818e2e0370 83 /// - 0 = reserved,
WiredHome 21:f3818e2e0370 84 /// - 1 = symmetric active
WiredHome 21:f3818e2e0370 85 /// - 2 = symmetric passive
WiredHome 21:f3818e2e0370 86 /// - 3 = client
WiredHome 21:f3818e2e0370 87 /// - 4 = server
WiredHome 21:f3818e2e0370 88 /// - 5 = broadcast
WiredHome 21:f3818e2e0370 89 /// - 6 = reserved for NTP control message
WiredHome 21:f3818e2e0370 90 /// - 7 = reserved for private use
WiredHome 21:f3818e2e0370 91 ///
WiredHome 21:f3818e2e0370 92 unsigned mode : 3;
WiredHome 21:f3818e2e0370 93
WiredHome 21:f3818e2e0370 94 /// Version Number
WiredHome 21:f3818e2e0370 95 ///
WiredHome 21:f3818e2e0370 96 /// The current version number for NTP/SNTP is 4
WiredHome 21:f3818e2e0370 97 ///
WiredHome 21:f3818e2e0370 98 unsigned vn : 3;
WiredHome 21:f3818e2e0370 99
WiredHome 21:f3818e2e0370 100 /// Leap Indicator
WiredHome 21:f3818e2e0370 101 ///
WiredHome 21:f3818e2e0370 102 /// values
WiredHome 21:f3818e2e0370 103 /// - 0 = no warning,
WiredHome 21:f3818e2e0370 104 /// - 1 = last min had 61s,
WiredHome 21:f3818e2e0370 105 /// - 2 = last min had 59s,
WiredHome 21:f3818e2e0370 106 /// - 3 = alarm - clock not in sync
WiredHome 21:f3818e2e0370 107 ///
WiredHome 21:f3818e2e0370 108 unsigned li : 2;
WiredHome 21:f3818e2e0370 109
WiredHome 21:f3818e2e0370 110 /// This is indicates the stratum
WiredHome 21:f3818e2e0370 111 ///
WiredHome 21:f3818e2e0370 112 /// values
WiredHome 21:f3818e2e0370 113 /// - 0 = kiss-o'-death
WiredHome 21:f3818e2e0370 114 /// - 1 = primary reference
WiredHome 21:f3818e2e0370 115 /// - 2-15 = secondary reference
WiredHome 21:f3818e2e0370 116 /// - 16-255 = reserved
WiredHome 21:f3818e2e0370 117 ///
WiredHome 21:f3818e2e0370 118 uint8_t stratum;
WiredHome 21:f3818e2e0370 119
WiredHome 21:f3818e2e0370 120 /// Poll interval
WiredHome 21:f3818e2e0370 121 ///
WiredHome 21:f3818e2e0370 122 /// This is an exponent of two, where the resulting value is the max
WiredHome 21:f3818e2e0370 123 /// interval between successive messages in seconds.
WiredHome 21:f3818e2e0370 124 ///
WiredHome 21:f3818e2e0370 125 uint8_t poll;
WiredHome 21:f3818e2e0370 126
WiredHome 21:f3818e2e0370 127 /// Precision of the clock
WiredHome 21:f3818e2e0370 128 ///
WiredHome 21:f3818e2e0370 129 /// This is an eight-bit signed integer used as an exponent of
WiredHome 21:f3818e2e0370 130 /// two, where the resulting value is the precision of the system clock
WiredHome 21:f3818e2e0370 131 /// in seconds. This field is significant only in server messages, where
WiredHome 21:f3818e2e0370 132 /// the values range from -6 for mains-frequency clocks to -20 for
WiredHome 21:f3818e2e0370 133 /// microsecond clocks found in some workstations.
WiredHome 21:f3818e2e0370 134 ///
WiredHome 21:f3818e2e0370 135 int8_t precision;
WiredHome 21:f3818e2e0370 136 //32 bits header
WiredHome 21:f3818e2e0370 137
WiredHome 21:f3818e2e0370 138 /// Root Delay
WiredHome 21:f3818e2e0370 139 ///
WiredHome 21:f3818e2e0370 140 /// This is a 32-bit signed fixed-point number indicating the
WiredHome 21:f3818e2e0370 141 /// total roundtrip delay to the primary reference source, in seconds
WiredHome 21:f3818e2e0370 142 /// with the fraction point between bits 15 and 16. Note that this
WiredHome 21:f3818e2e0370 143 /// variable can take on both positive and negative values, depending on
WiredHome 21:f3818e2e0370 144 /// the relative time and frequency offsets. This field is significant
WiredHome 21:f3818e2e0370 145 /// only in server messages, where the values range from negative values
WiredHome 21:f3818e2e0370 146 /// of a few milliseconds to positive values of several hundred
WiredHome 21:f3818e2e0370 147 /// milliseconds.
WiredHome 21:f3818e2e0370 148 ///
WiredHome 21:f3818e2e0370 149 int32_t rootDelay;
WiredHome 21:f3818e2e0370 150
WiredHome 21:f3818e2e0370 151 /// Root Dispersion: This is a 32-bit unsigned fixed-point number
WiredHome 21:f3818e2e0370 152 /// indicating the maximum error due to the clock frequency tolerance, in
WiredHome 21:f3818e2e0370 153 /// seconds with the fraction point between bits 15 and 16. This field
WiredHome 21:f3818e2e0370 154 /// is significant only in server messages, where the values range from
WiredHome 21:f3818e2e0370 155 /// zero to several hundred milliseconds.
WiredHome 21:f3818e2e0370 156 ///
WiredHome 21:f3818e2e0370 157 uint32_t rootDispersion;
WiredHome 21:f3818e2e0370 158
WiredHome 21:f3818e2e0370 159 /// This idenfies a particular clock source
WiredHome 21:f3818e2e0370 160 ///
WiredHome 21:f3818e2e0370 161 uint32_t refId;
WiredHome 21:f3818e2e0370 162
WiredHome 21:f3818e2e0370 163 /// Reference Timestamp identified when the clock was last set.
WiredHome 21:f3818e2e0370 164 ///
WiredHome 21:f3818e2e0370 165 /// It is a 64-bit value, this is in whole seconds
WiredHome 21:f3818e2e0370 166 ///
WiredHome 21:f3818e2e0370 167 uint32_t refTm_s;
WiredHome 21:f3818e2e0370 168 /// Reference Timestamp identified when the clock was last set.
WiredHome 21:f3818e2e0370 169 ///
WiredHome 21:f3818e2e0370 170 /// It is a 64-bit value, this is in fractions of a second
WiredHome 21:f3818e2e0370 171 ///
WiredHome 21:f3818e2e0370 172 uint32_t refTm_f;
WiredHome 21:f3818e2e0370 173
WiredHome 21:f3818e2e0370 174 /// Originate Timestamp identified when the request departed the client.
WiredHome 21:f3818e2e0370 175 ///
WiredHome 21:f3818e2e0370 176 /// It is a 64-bit value, this is in whole seconds
WiredHome 21:f3818e2e0370 177 ///
WiredHome 21:f3818e2e0370 178 uint32_t origTm_s;
WiredHome 21:f3818e2e0370 179 /// Originate Timestamp identified when the request departed the client.
WiredHome 21:f3818e2e0370 180 ///
WiredHome 21:f3818e2e0370 181 /// It is a 64-bit value, this is in fractions of a second
WiredHome 21:f3818e2e0370 182 ///
WiredHome 21:f3818e2e0370 183 uint32_t origTm_f;
WiredHome 21:f3818e2e0370 184
WiredHome 21:f3818e2e0370 185 /// Receive Timestamp identified when the request arrived at the server.
WiredHome 21:f3818e2e0370 186 ///
WiredHome 21:f3818e2e0370 187 /// It is a 64-bit value, this is in whole seconds
WiredHome 21:f3818e2e0370 188 ///
WiredHome 21:f3818e2e0370 189 uint32_t rxTm_s;
WiredHome 21:f3818e2e0370 190 /// Receive Timestamp identified when the request arrived at the server.
WiredHome 21:f3818e2e0370 191 ///
WiredHome 21:f3818e2e0370 192 /// It is a 64-bit value, this is in fractions of a second
WiredHome 21:f3818e2e0370 193 ///
WiredHome 21:f3818e2e0370 194 uint32_t rxTm_f;
WiredHome 21:f3818e2e0370 195
WiredHome 21:f3818e2e0370 196 /// Transmit Timestamp identified when the request departed the client/server.
WiredHome 21:f3818e2e0370 197 ///
WiredHome 21:f3818e2e0370 198 /// It is a 64-bit value, this is in whole seconds
WiredHome 21:f3818e2e0370 199 ///
WiredHome 21:f3818e2e0370 200 uint32_t txTm_s;
WiredHome 21:f3818e2e0370 201 /// Transmit Timestamp identified when the request departed the client/server.
WiredHome 21:f3818e2e0370 202 ///
WiredHome 21:f3818e2e0370 203 /// It is a 64-bit value, this is in fractions of a second
WiredHome 21:f3818e2e0370 204 ///
WiredHome 21:f3818e2e0370 205 uint32_t txTm_f;
WiredHome 21:f3818e2e0370 206 } __attribute__ ((packed));
WiredHome 21:f3818e2e0370 207
WiredHome 21:f3818e2e0370 208 UDPSocket m_sock;
WiredHome 21:f3818e2e0370 209 };
WiredHome 21:f3818e2e0370 210
WiredHome 21:f3818e2e0370 211
WiredHome 21:f3818e2e0370 212 #endif /* NTPCLIENT_H_ */