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:
7:170915215fd4
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.cpp */
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 //Debug is disabled by default
donatien 4:881559865a93 21 #if 0
donatien 2:9a64a50df235 22 //Enable debug
donatien 2:9a64a50df235 23 #define __DEBUG__
donatien 2:9a64a50df235 24 #include <cstdio>
WiredHome 8:802277794640 25 #define DBG(x, ...) std::printf("[NTPClient : DBG]"x"\r\n", ##__VA_ARGS__);
WiredHome 8:802277794640 26 #define WARN(x, ...) std::printf("[NTPClient : WARN]"x"\r\n", ##__VA_ARGS__);
WiredHome 8:802277794640 27 #define ERR(x, ...) std::printf("[NTPClient : ERR]"x"\r\n", ##__VA_ARGS__);
donatien 2:9a64a50df235 28 #else
donatien 2:9a64a50df235 29 //Disable debug
WiredHome 8:802277794640 30 #define DBG(x, ...)
donatien 2:9a64a50df235 31 #define WARN(x, ...)
WiredHome 8:802277794640 32 #define ERR(x, ...)
donatien 0:04a82df0f587 33 #endif
donatien 0:04a82df0f587 34
donatien 0:04a82df0f587 35 #include "NTPClient.h"
donatien 0:04a82df0f587 36
donatien 1:b221a8765b3f 37 #include "UDPSocket.h"
donatien 1:b221a8765b3f 38
donatien 0:04a82df0f587 39 #include "mbed.h" //time() and set_time()
donatien 0:04a82df0f587 40
donatien 0:04a82df0f587 41 #define NTP_PORT 123
donatien 0:04a82df0f587 42 #define NTP_CLIENT_PORT 0 //Random port
donatien 0:04a82df0f587 43 #define NTP_TIMESTAMP_DELTA 2208988800ull //Diff btw a UNIX timestamp (Starting Jan, 1st 1970) and a NTP timestamp (Starting Jan, 1st 1900)
donatien 0:04a82df0f587 44
donatien 2:9a64a50df235 45 NTPClient::NTPClient() : m_sock()
donatien 0:04a82df0f587 46 {
donatien 0:04a82df0f587 47
donatien 0:04a82df0f587 48 }
donatien 0:04a82df0f587 49
donatien 2:9a64a50df235 50 NTPResult NTPClient::setTime(const char* host, uint16_t port, uint32_t timeout)
donatien 0:04a82df0f587 51 {
donatien 2:9a64a50df235 52 #ifdef __DEBUG__
WiredHome 8:802277794640 53 time_t ctTime;
WiredHome 8:802277794640 54 ctTime = time(NULL);
WiredHome 8:802277794640 55 DBG("Time is set to (UTC): %s", ctime(&ctTime));
donatien 0:04a82df0f587 56 #endif
donatien 0:04a82df0f587 57
WiredHome 8:802277794640 58 //Create & bind socket
WiredHome 8:802277794640 59 DBG("Binding socket");
WiredHome 8:802277794640 60 m_sock.bind(0); //Bind to a random port
donatien 0:04a82df0f587 61
WiredHome 8:802277794640 62 m_sock.set_blocking(false, timeout); //Set not blocking
WiredHome 8:802277794640 63
WiredHome 8:802277794640 64 struct NTPPacket pkt;
donatien 0:04a82df0f587 65
WiredHome 8:802277794640 66 //Now ping the server and wait for response
WiredHome 8:802277794640 67 DBG("Ping");
WiredHome 8:802277794640 68 //Prepare NTP Packet:
WiredHome 8:802277794640 69 pkt.li = 0; //Leap Indicator : No warning
WiredHome 8:802277794640 70 pkt.vn = 4; //Version Number : 4
WiredHome 8:802277794640 71 pkt.mode = 3; //Client mode
WiredHome 8:802277794640 72 pkt.stratum = 0; //Not relevant here
WiredHome 8:802277794640 73 pkt.poll = 0; //Not significant as well
WiredHome 8:802277794640 74 pkt.precision = 0; //Neither this one is
donatien 0:04a82df0f587 75
WiredHome 8:802277794640 76 pkt.rootDelay = 0; //Or this one
WiredHome 8:802277794640 77 pkt.rootDispersion = 0; //Or that one
WiredHome 8:802277794640 78 pkt.refId = 0; //...
donatien 0:04a82df0f587 79
WiredHome 8:802277794640 80 pkt.refTm_s = 0;
WiredHome 8:802277794640 81 pkt.origTm_s = 0;
WiredHome 8:802277794640 82 pkt.rxTm_s = 0;
WiredHome 8:802277794640 83 pkt.txTm_s = htonl( NTP_TIMESTAMP_DELTA + time(NULL) ); //WARN: We are in LE format, network byte order is BE
donatien 0:04a82df0f587 84
WiredHome 8:802277794640 85 pkt.refTm_f = pkt.origTm_f = pkt.rxTm_f = pkt.txTm_f = 0;
WiredHome 8:802277794640 86
WiredHome 8:802277794640 87 Endpoint outEndpoint;
donatien 0:04a82df0f587 88
WiredHome 8:802277794640 89 if( outEndpoint.set_address(host, port) < 0) {
WiredHome 8:802277794640 90 m_sock.close();
WiredHome 8:802277794640 91 return NTP_DNS;
WiredHome 8:802277794640 92 }
WiredHome 8:802277794640 93
WiredHome 8:802277794640 94 //Set timeout, non-blocking and wait using select
WiredHome 8:802277794640 95 int ret = m_sock.sendTo( outEndpoint, (char*)&pkt, sizeof(NTPPacket) );
WiredHome 8:802277794640 96 if (ret < 0 ) {
WiredHome 8:802277794640 97 ERR("Could not send packet");
WiredHome 8:802277794640 98 m_sock.close();
WiredHome 8:802277794640 99 return NTP_CONN;
WiredHome 8:802277794640 100 }
donatien 0:04a82df0f587 101
WiredHome 8:802277794640 102 //Read response
WiredHome 8:802277794640 103 Endpoint inEndpoint;
WiredHome 8:802277794640 104
WiredHome 8:802277794640 105 // Set the inEndpoint address property
WiredHome 8:802277794640 106 inEndpoint.set_address(outEndpoint.get_address(), 0);
donatien 2:9a64a50df235 107
WiredHome 8:802277794640 108 DBG("Pong");
WiredHome 8:802277794640 109 do {
WiredHome 8:802277794640 110 ret = m_sock.receiveFrom( inEndpoint, (char*)&pkt, sizeof(NTPPacket) ); //FIXME need a DNS Resolver to actually compare the incoming address with the DNS name
WiredHome 8:802277794640 111 if(ret < 0) {
WiredHome 8:802277794640 112 ERR("Could not receive packet");
WiredHome 8:802277794640 113 m_sock.close();
WiredHome 8:802277794640 114 return NTP_CONN;
WiredHome 8:802277794640 115 }
WiredHome 8:802277794640 116 DBG(".");
WiredHome 8:802277794640 117 } while( strcmp(outEndpoint.get_address(), inEndpoint.get_address()) != 0 );
WiredHome 8:802277794640 118
WiredHome 8:802277794640 119 if(ret < (int)sizeof(NTPPacket)) { //TODO: Accept chunks
WiredHome 8:802277794640 120 ERR("Receive packet size does not match");
WiredHome 8:802277794640 121 m_sock.close();
WiredHome 8:802277794640 122 return NTP_PRTCL;
donatien 0:04a82df0f587 123 }
donatien 0:04a82df0f587 124
WiredHome 8:802277794640 125 if( pkt.stratum == 0) { //Kiss of death message : Not good !
WiredHome 8:802277794640 126 ERR("Kissed to death!");
WiredHome 8:802277794640 127 m_sock.close();
WiredHome 8:802277794640 128 return NTP_PRTCL;
WiredHome 8:802277794640 129 }
donatien 0:04a82df0f587 130
WiredHome 8:802277794640 131 //Correct Endianness
WiredHome 8:802277794640 132 pkt.refTm_s = ntohl( pkt.refTm_s );
WiredHome 8:802277794640 133 pkt.refTm_f = ntohl( pkt.refTm_f );
WiredHome 8:802277794640 134 pkt.origTm_s = ntohl( pkt.origTm_s );
WiredHome 8:802277794640 135 pkt.origTm_f = ntohl( pkt.origTm_f );
WiredHome 8:802277794640 136 pkt.rxTm_s = ntohl( pkt.rxTm_s );
WiredHome 8:802277794640 137 pkt.rxTm_f = ntohl( pkt.rxTm_f );
WiredHome 8:802277794640 138 pkt.txTm_s = ntohl( pkt.txTm_s );
WiredHome 8:802277794640 139 pkt.txTm_f = ntohl( pkt.txTm_f );
donatien 0:04a82df0f587 140
WiredHome 8:802277794640 141 //Compute offset, see RFC 4330 p.13
WiredHome 8:802277794640 142 uint32_t destTm_s = (NTP_TIMESTAMP_DELTA + time(NULL));
WiredHome 8:802277794640 143 int64_t offset = ( (int64_t)( pkt.rxTm_s - pkt.origTm_s ) + (int64_t) ( pkt.txTm_s - destTm_s ) ) / 2; //Avoid overflow
WiredHome 8:802277794640 144 DBG("Sent @%ul", pkt.txTm_s);
WiredHome 8:802277794640 145 DBG("Offset: %lld", offset);
WiredHome 8:802277794640 146 //Set time accordingly
WiredHome 8:802277794640 147 set_time( time(NULL) + offset );
donatien 0:04a82df0f587 148
donatien 2:9a64a50df235 149 #ifdef __DEBUG__
WiredHome 8:802277794640 150 ctTime = time(NULL);
WiredHome 8:802277794640 151 DBG("Time is now (UTC): %s", ctime(&ctTime));
donatien 0:04a82df0f587 152 #endif
donatien 0:04a82df0f587 153
WiredHome 8:802277794640 154 m_sock.close();
donatien 0:04a82df0f587 155
WiredHome 8:802277794640 156 return NTP_OK;
donatien 0:04a82df0f587 157 }
donatien 0:04a82df0f587 158