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:
Thu Nov 26 18:20:56 2015 +0000
Revision:
8:802277794640
Parent:
6:bef14adc58c4
Child:
9:2f607bafc29e
Update to fix a hang. Applied fix from Koen Kempeneers.

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.
donatien 2:9a64a50df235 18 */
donatien 0:04a82df0f587 19
donatien 2:9a64a50df235 20 /** \file
donatien 2:9a64a50df235 21 NTP Client header file
donatien 0:04a82df0f587 22 */
donatien 0:04a82df0f587 23
donatien 0:04a82df0f587 24 #ifndef NTPCLIENT_H_
donatien 0:04a82df0f587 25 #define NTPCLIENT_H_
donatien 0:04a82df0f587 26
donatien 2:9a64a50df235 27 #include <cstdint>
donatien 2:9a64a50df235 28
donatien 2:9a64a50df235 29 using std::uint8_t;
donatien 2:9a64a50df235 30 using std::uint16_t;
donatien 2:9a64a50df235 31 using std::uint32_t;
donatien 2:9a64a50df235 32
donatien 2:9a64a50df235 33 #include "UDPSocket.h"
donatien 2:9a64a50df235 34
donatien 0:04a82df0f587 35 #define NTP_DEFAULT_PORT 123
donatien 0:04a82df0f587 36 #define NTP_DEFAULT_TIMEOUT 4000
donatien 0:04a82df0f587 37
donatien 2:9a64a50df235 38 ///NTP client results
WiredHome 8:802277794640 39 enum NTPResult {
WiredHome 8:802277794640 40 NTP_OK = 0, ///<Success
WiredHome 8:802277794640 41 NTP_DNS, ///<Could not resolve name
WiredHome 8:802277794640 42 NTP_PRTCL, ///<Protocol error
WiredHome 8:802277794640 43 NTP_TIMEOUT, ///<Connection timeout
WiredHome 8:802277794640 44 NTP_CONN, ///<Connection error
donatien 2:9a64a50df235 45 };
donatien 2:9a64a50df235 46
donatien 0:04a82df0f587 47 /** NTP Client to update the mbed's RTC using a remote time server
donatien 0:04a82df0f587 48 *
donatien 0:04a82df0f587 49 */
donatien 0:04a82df0f587 50 class NTPClient
donatien 0:04a82df0f587 51 {
donatien 0:04a82df0f587 52 public:
WiredHome 8:802277794640 53 /**
WiredHome 8:802277794640 54 Instantiate the NTP client
WiredHome 8:802277794640 55 */
WiredHome 8:802277794640 56 NTPClient();
donatien 0:04a82df0f587 57
WiredHome 8:802277794640 58 /**Get current time (blocking)
WiredHome 8:802277794640 59 Update the time using the server host
WiredHome 8:802277794640 60 Blocks until completion
WiredHome 8:802277794640 61 @param[in] host NTP server IPv4 address or hostname (will be resolved via DNS)
WiredHome 8:802277794640 62 @param[in] port port to use; defaults to 123
WiredHome 8:802277794640 63 @param[in] timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
WiredHome 8:802277794640 64 @return 0 on success, NTP error code (<0) on failure
WiredHome 8:802277794640 65 */
WiredHome 8:802277794640 66 NTPResult setTime(const char* host, uint16_t port = NTP_DEFAULT_PORT, uint32_t timeout = NTP_DEFAULT_TIMEOUT); //Blocking
donatien 0:04a82df0f587 67
donatien 0:04a82df0f587 68 private:
WiredHome 8:802277794640 69 struct NTPPacket { //See RFC 4330 for Simple NTP
WiredHome 8:802277794640 70 //WARN: We are in LE! Network is BE!
WiredHome 8:802277794640 71 //LSb first
WiredHome 8:802277794640 72 unsigned mode : 3;
WiredHome 8:802277794640 73 unsigned vn : 3;
WiredHome 8:802277794640 74 unsigned li : 2;
donatien 0:04a82df0f587 75
WiredHome 8:802277794640 76 uint8_t stratum;
WiredHome 8:802277794640 77 uint8_t poll;
WiredHome 8:802277794640 78 uint8_t precision;
WiredHome 8:802277794640 79 //32 bits header
donatien 0:04a82df0f587 80
WiredHome 8:802277794640 81 uint32_t rootDelay;
WiredHome 8:802277794640 82 uint32_t rootDispersion;
WiredHome 8:802277794640 83 uint32_t refId;
donatien 0:04a82df0f587 84
WiredHome 8:802277794640 85 uint32_t refTm_s;
WiredHome 8:802277794640 86 uint32_t refTm_f;
WiredHome 8:802277794640 87 uint32_t origTm_s;
WiredHome 8:802277794640 88 uint32_t origTm_f;
WiredHome 8:802277794640 89 uint32_t rxTm_s;
WiredHome 8:802277794640 90 uint32_t rxTm_f;
WiredHome 8:802277794640 91 uint32_t txTm_s;
WiredHome 8:802277794640 92 uint32_t txTm_f;
WiredHome 8:802277794640 93 } __attribute__ ((packed));
donatien 0:04a82df0f587 94
WiredHome 8:802277794640 95 UDPSocket m_sock;
donatien 0:04a82df0f587 96 };
donatien 0:04a82df0f587 97
donatien 0:04a82df0f587 98
donatien 0:04a82df0f587 99 #endif /* NTPCLIENT_H_ */