NTP Client for the mbed networking libraries. The small change to this version is that there can be only one cause for the return value zero.

Dependents:   WattEye

Fork of NTPClient by Donatien Garnier

Committer:
WiredHome
Date:
Sat Jan 16 20:47:33 2016 +0000
Revision:
9:2f607bafc29e
Parent:
8:802277794640
NTPClient was computing the offset from the server to the client incorrectly (when it was promoting 32 to 64bit values. The result was time-jumps (e.g. from 2016 to 2084) and then back. This was most noticeable with the WiFly interface (extra delays)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
donatien 0:04a82df0f587 1 /* NTPClient.h */
donatien 2:9a64a50df235 2 /* Copyright (C) 2012 mbed.org, MIT License
donatien 2:9a64a50df235 3 *
donatien 2:9a64a50df235 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
donatien 2:9a64a50df235 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
donatien 2:9a64a50df235 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
donatien 2:9a64a50df235 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
donatien 2:9a64a50df235 8 * furnished to do so, subject to the following conditions:
donatien 2:9a64a50df235 9 *
donatien 2:9a64a50df235 10 * The above copyright notice and this permission notice shall be included in all copies or
donatien 2:9a64a50df235 11 * substantial portions of the Software.
donatien 2:9a64a50df235 12 *
donatien 2:9a64a50df235 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
donatien 2:9a64a50df235 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
donatien 2:9a64a50df235 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
donatien 2:9a64a50df235 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
donatien 2:9a64a50df235 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
WiredHome 9:2f607bafc29e 18 *
donatien 2:9a64a50df235 19 */
donatien 0:04a82df0f587 20
donatien 2:9a64a50df235 21 /** \file
donatien 2:9a64a50df235 22 NTP Client header file
donatien 0:04a82df0f587 23 */
donatien 0:04a82df0f587 24
donatien 0:04a82df0f587 25 #ifndef NTPCLIENT_H_
donatien 0:04a82df0f587 26 #define NTPCLIENT_H_
donatien 0:04a82df0f587 27
donatien 2:9a64a50df235 28 #include <cstdint>
donatien 2:9a64a50df235 29
donatien 2:9a64a50df235 30 using std::uint8_t;
donatien 2:9a64a50df235 31 using std::uint16_t;
donatien 2:9a64a50df235 32 using std::uint32_t;
donatien 2:9a64a50df235 33
donatien 2:9a64a50df235 34 #include "UDPSocket.h"
donatien 2:9a64a50df235 35
donatien 0:04a82df0f587 36 #define NTP_DEFAULT_PORT 123
donatien 0:04a82df0f587 37 #define NTP_DEFAULT_TIMEOUT 4000
donatien 0:04a82df0f587 38
donatien 2:9a64a50df235 39 ///NTP client results
WiredHome 8:802277794640 40 enum NTPResult {
WiredHome 8:802277794640 41 NTP_OK = 0, ///<Success
WiredHome 8:802277794640 42 NTP_DNS, ///<Could not resolve name
WiredHome 8:802277794640 43 NTP_PRTCL, ///<Protocol error
WiredHome 8:802277794640 44 NTP_TIMEOUT, ///<Connection timeout
WiredHome 8:802277794640 45 NTP_CONN, ///<Connection error
donatien 2:9a64a50df235 46 };
donatien 2:9a64a50df235 47
donatien 0:04a82df0f587 48 /** NTP Client to update the mbed's RTC using a remote time server
donatien 0:04a82df0f587 49 *
donatien 0:04a82df0f587 50 */
donatien 0:04a82df0f587 51 class NTPClient
donatien 0:04a82df0f587 52 {
donatien 0:04a82df0f587 53 public:
WiredHome 8:802277794640 54 /**
WiredHome 8:802277794640 55 Instantiate the NTP client
WiredHome 8:802277794640 56 */
WiredHome 8:802277794640 57 NTPClient();
donatien 0:04a82df0f587 58
WiredHome 8:802277794640 59 /**Get current time (blocking)
WiredHome 8:802277794640 60 Update the time using the server host
WiredHome 8:802277794640 61 Blocks until completion
WiredHome 8:802277794640 62 @param[in] host NTP server IPv4 address or hostname (will be resolved via DNS)
WiredHome 8:802277794640 63 @param[in] port port to use; defaults to 123
WiredHome 8:802277794640 64 @param[in] timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
WiredHome 8:802277794640 65 @return 0 on success, NTP error code (<0) on failure
WiredHome 8:802277794640 66 */
WiredHome 8:802277794640 67 NTPResult setTime(const char* host, uint16_t port = NTP_DEFAULT_PORT, uint32_t timeout = NTP_DEFAULT_TIMEOUT); //Blocking
donatien 0:04a82df0f587 68
donatien 0:04a82df0f587 69 private:
WiredHome 8:802277794640 70 struct NTPPacket { //See RFC 4330 for Simple NTP
WiredHome 8:802277794640 71 //WARN: We are in LE! Network is BE!
WiredHome 8:802277794640 72 //LSb first
WiredHome 8:802277794640 73 unsigned mode : 3;
WiredHome 8:802277794640 74 unsigned vn : 3;
WiredHome 8:802277794640 75 unsigned li : 2;
donatien 0:04a82df0f587 76
WiredHome 8:802277794640 77 uint8_t stratum;
WiredHome 8:802277794640 78 uint8_t poll;
WiredHome 8:802277794640 79 uint8_t precision;
WiredHome 8:802277794640 80 //32 bits header
donatien 0:04a82df0f587 81
WiredHome 8:802277794640 82 uint32_t rootDelay;
WiredHome 8:802277794640 83 uint32_t rootDispersion;
WiredHome 8:802277794640 84 uint32_t refId;
donatien 0:04a82df0f587 85
WiredHome 8:802277794640 86 uint32_t refTm_s;
WiredHome 8:802277794640 87 uint32_t refTm_f;
WiredHome 8:802277794640 88 uint32_t origTm_s;
WiredHome 8:802277794640 89 uint32_t origTm_f;
WiredHome 8:802277794640 90 uint32_t rxTm_s;
WiredHome 8:802277794640 91 uint32_t rxTm_f;
WiredHome 8:802277794640 92 uint32_t txTm_s;
WiredHome 8:802277794640 93 uint32_t txTm_f;
WiredHome 8:802277794640 94 } __attribute__ ((packed));
donatien 0:04a82df0f587 95
WiredHome 8:802277794640 96 UDPSocket m_sock;
donatien 0:04a82df0f587 97 };
donatien 0:04a82df0f587 98
donatien 0:04a82df0f587 99
donatien 0:04a82df0f587 100 #endif /* NTPCLIENT_H_ */