John Durrell / NTPClient

Fork of NTPClient by Dieter Graef

Committer:
jhd25
Date:
Fri Apr 28 14:17:00 2017 +0000
Revision:
1:52e0f030d00e
Parent:
0:584a18640e84
Child:
2:eda6db3ada54
Fixed to work with mbed-os 5

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DieterGraef 0:584a18640e84 1 /* NTPClient.cpp */
DieterGraef 0:584a18640e84 2 /* Copyright (C) 2012 mbed.org, MIT License
DieterGraef 0:584a18640e84 3 *
DieterGraef 0:584a18640e84 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
DieterGraef 0:584a18640e84 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
DieterGraef 0:584a18640e84 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
DieterGraef 0:584a18640e84 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
DieterGraef 0:584a18640e84 8 * furnished to do so, subject to the following conditions:
DieterGraef 0:584a18640e84 9 *
DieterGraef 0:584a18640e84 10 * The above copyright notice and this permission notice shall be included in all copies or
DieterGraef 0:584a18640e84 11 * substantial portions of the Software.
DieterGraef 0:584a18640e84 12 *
DieterGraef 0:584a18640e84 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
DieterGraef 0:584a18640e84 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
DieterGraef 0:584a18640e84 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DieterGraef 0:584a18640e84 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
DieterGraef 0:584a18640e84 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
DieterGraef 0:584a18640e84 18 */
DieterGraef 0:584a18640e84 19
DieterGraef 0:584a18640e84 20 //Debug is disabled by default
DieterGraef 0:584a18640e84 21 #if 0
DieterGraef 0:584a18640e84 22 //Enable debug
DieterGraef 0:584a18640e84 23 #define __DEBUG__
DieterGraef 0:584a18640e84 24 #include <cstdio>
DieterGraef 0:584a18640e84 25 #define DBG(x, ...) std::printf("[NTPClient : DBG]"x"\r\n", ##__VA_ARGS__);
DieterGraef 0:584a18640e84 26 #define WARN(x, ...) std::printf("[NTPClient : WARN]"x"\r\n", ##__VA_ARGS__);
DieterGraef 0:584a18640e84 27 #define ERR(x, ...) std::printf("[NTPClient : ERR]"x"\r\n", ##__VA_ARGS__);
DieterGraef 0:584a18640e84 28
DieterGraef 0:584a18640e84 29 #else
DieterGraef 0:584a18640e84 30 //Disable debug
DieterGraef 0:584a18640e84 31 #define DBG(x, ...)
DieterGraef 0:584a18640e84 32 #define WARN(x, ...)
DieterGraef 0:584a18640e84 33 #define ERR(x, ...)
DieterGraef 0:584a18640e84 34
DieterGraef 0:584a18640e84 35 #endif
DieterGraef 0:584a18640e84 36
DieterGraef 0:584a18640e84 37 #include "NTPClient.h"
jhd25 1:52e0f030d00e 38 #include "EthernetInterface.h"
DieterGraef 0:584a18640e84 39 #include "UDPSocket.h"
DieterGraef 0:584a18640e84 40
DieterGraef 0:584a18640e84 41 #include "mbed.h" //time() and set_time()
jhd25 1:52e0f030d00e 42 #include "lwip/opt.h"
jhd25 1:52e0f030d00e 43 #include "lwip/def.h"
DieterGraef 0:584a18640e84 44 #define NTP_PORT 123
DieterGraef 0:584a18640e84 45 #define NTP_CLIENT_PORT 0 //Random port
DieterGraef 0:584a18640e84 46 #define NTP_TIMESTAMP_DELTA 2208988800ull //Diff btw a UNIX timestamp (Starting Jan, 1st 1970) and a NTP timestamp (Starting Jan, 1st 1900)
DieterGraef 0:584a18640e84 47
jhd25 1:52e0f030d00e 48 NTPClient::NTPClient(EthernetInterface& net_stack) : m_sock(&net_stack)
DieterGraef 0:584a18640e84 49 {
jhd25 1:52e0f030d00e 50 EthernetInterface nst = net_stack;
DieterGraef 0:584a18640e84 51
DieterGraef 0:584a18640e84 52 }
DieterGraef 0:584a18640e84 53
DieterGraef 0:584a18640e84 54 NTPResult NTPClient::setTime(const char* host, uint16_t port, uint32_t timeout)
DieterGraef 0:584a18640e84 55 {
DieterGraef 0:584a18640e84 56 #ifdef __DEBUG__
DieterGraef 0:584a18640e84 57 time_t ctTime;
DieterGraef 0:584a18640e84 58 ctTime = time(NULL);
DieterGraef 0:584a18640e84 59 DBG("Time is set to (UTC): %s", ctime(&ctTime));
DieterGraef 0:584a18640e84 60 #endif
DieterGraef 0:584a18640e84 61
DieterGraef 0:584a18640e84 62 //Create & bind socket
DieterGraef 0:584a18640e84 63 DBG("Binding socket");
jhd25 1:52e0f030d00e 64 SocketAddress inEndpoint("0.0.0.0", 0);
jhd25 1:52e0f030d00e 65 m_sock.bind(inEndpoint); //Bind to a random port
jhd25 1:52e0f030d00e 66 m_sock.set_blocking(false); //Set not blocking
jhd25 1:52e0f030d00e 67 SocketAddress outEndpoint(&nst,host,port);
DieterGraef 0:584a18640e84 68 struct NTPPacket pkt;
DieterGraef 0:584a18640e84 69
DieterGraef 0:584a18640e84 70 //Now ping the server and wait for response
DieterGraef 0:584a18640e84 71 DBG("Ping");
DieterGraef 0:584a18640e84 72 //Prepare NTP Packet:
DieterGraef 0:584a18640e84 73 pkt.li = 0; //Leap Indicator : No warning
DieterGraef 0:584a18640e84 74 pkt.vn = 4; //Version Number : 4
DieterGraef 0:584a18640e84 75 pkt.mode = 3; //Client mode
DieterGraef 0:584a18640e84 76 pkt.stratum = 0; //Not relevant here
DieterGraef 0:584a18640e84 77 pkt.poll = 0; //Not significant as well
DieterGraef 0:584a18640e84 78 pkt.precision = 0; //Neither this one is
DieterGraef 0:584a18640e84 79
DieterGraef 0:584a18640e84 80 pkt.rootDelay = 0; //Or this one
DieterGraef 0:584a18640e84 81 pkt.rootDispersion = 0; //Or that one
DieterGraef 0:584a18640e84 82 pkt.refId = 0; //...
DieterGraef 0:584a18640e84 83
DieterGraef 0:584a18640e84 84 pkt.refTm_s = 0;
DieterGraef 0:584a18640e84 85 pkt.origTm_s = 0;
DieterGraef 0:584a18640e84 86 pkt.rxTm_s = 0;
DieterGraef 0:584a18640e84 87 pkt.txTm_s = htonl( NTP_TIMESTAMP_DELTA + time(NULL) ); //WARN: We are in LE format, network byte order is BE
DieterGraef 0:584a18640e84 88
DieterGraef 0:584a18640e84 89 pkt.refTm_f = pkt.origTm_f = pkt.rxTm_f = pkt.txTm_f = 0;
DieterGraef 0:584a18640e84 90
jhd25 1:52e0f030d00e 91
jhd25 1:52e0f030d00e 92 DBG("Ping2");
jhd25 1:52e0f030d00e 93
jhd25 1:52e0f030d00e 94
DieterGraef 0:584a18640e84 95 //Set timeout, non-blocking and wait using select
jhd25 1:52e0f030d00e 96 int ret = m_sock.sendto(outEndpoint, &pkt, sizeof(NTPPacket));
jhd25 1:52e0f030d00e 97 DBG("Ping4");
jhd25 1:52e0f030d00e 98
DieterGraef 0:584a18640e84 99 if (ret < 0 )
DieterGraef 0:584a18640e84 100 {
DieterGraef 0:584a18640e84 101 ERR("Could not send packet");
DieterGraef 0:584a18640e84 102 m_sock.close();
DieterGraef 0:584a18640e84 103 return NTP_CONN;
DieterGraef 0:584a18640e84 104 }
DieterGraef 0:584a18640e84 105
DieterGraef 0:584a18640e84 106 //Read response
DieterGraef 0:584a18640e84 107 // Set the inEndpoint address property
DieterGraef 0:584a18640e84 108 DBG("Pong");
jhd25 1:52e0f030d00e 109 int tries =0;
jhd25 1:52e0f030d00e 110 while (true) {
jhd25 1:52e0f030d00e 111 ret = m_sock.recvfrom( &inEndpoint, (char*)&pkt, sizeof(NTPPacket) ); //FIXME need a DNS Resolver to actually compare the incoming address with the DNS name
jhd25 1:52e0f030d00e 112 wait(0.01);
jhd25 1:52e0f030d00e 113 if((ret < 0 && ret != -3001) || tries >100)
DieterGraef 0:584a18640e84 114 {
DieterGraef 0:584a18640e84 115 ERR("Could not receive packet");
DieterGraef 0:584a18640e84 116 m_sock.close();
DieterGraef 0:584a18640e84 117 return NTP_CONN;
DieterGraef 0:584a18640e84 118 }
jhd25 1:52e0f030d00e 119 if (strcmp(outEndpoint.get_ip_address(), inEndpoint.get_ip_address()) == 0 ) break;
jhd25 1:52e0f030d00e 120 tries++;
jhd25 1:52e0f030d00e 121 }
jhd25 1:52e0f030d00e 122 printf("NTP Server Address: %s \r\n",outEndpoint.get_ip_address());
DieterGraef 0:584a18640e84 123 if(ret < sizeof(NTPPacket)) //TODO: Accept chunks
DieterGraef 0:584a18640e84 124 {
DieterGraef 0:584a18640e84 125 ERR("Receive packet size does not match");
DieterGraef 0:584a18640e84 126 m_sock.close();
DieterGraef 0:584a18640e84 127 return NTP_PRTCL;
DieterGraef 0:584a18640e84 128 }
DieterGraef 0:584a18640e84 129
DieterGraef 0:584a18640e84 130 if( pkt.stratum == 0) //Kiss of death message : Not good !
DieterGraef 0:584a18640e84 131 {
DieterGraef 0:584a18640e84 132 ERR("Kissed to death!");
DieterGraef 0:584a18640e84 133 m_sock.close();
DieterGraef 0:584a18640e84 134 return NTP_PRTCL;
DieterGraef 0:584a18640e84 135 }
DieterGraef 0:584a18640e84 136
DieterGraef 0:584a18640e84 137 //Correct Endianness
DieterGraef 0:584a18640e84 138 pkt.refTm_s = ntohl( pkt.refTm_s );
DieterGraef 0:584a18640e84 139 pkt.refTm_f = ntohl( pkt.refTm_f );
DieterGraef 0:584a18640e84 140 pkt.origTm_s = ntohl( pkt.origTm_s );
DieterGraef 0:584a18640e84 141 pkt.origTm_f = ntohl( pkt.origTm_f );
DieterGraef 0:584a18640e84 142 pkt.rxTm_s = ntohl( pkt.rxTm_s );
DieterGraef 0:584a18640e84 143 pkt.rxTm_f = ntohl( pkt.rxTm_f );
DieterGraef 0:584a18640e84 144 pkt.txTm_s = ntohl( pkt.txTm_s );
DieterGraef 0:584a18640e84 145 pkt.txTm_f = ntohl( pkt.txTm_f );
DieterGraef 0:584a18640e84 146
DieterGraef 0:584a18640e84 147 //Compute offset, see RFC 4330 p.13
DieterGraef 0:584a18640e84 148 uint32_t destTm_s = (NTP_TIMESTAMP_DELTA + time(NULL));
DieterGraef 0:584a18640e84 149 int64_t offset = ( (int64_t)( pkt.rxTm_s - pkt.origTm_s ) + (int64_t) ( pkt.txTm_s - destTm_s ) ) / 2; //Avoid overflow
DieterGraef 0:584a18640e84 150 DBG("Sent @%ul", pkt.txTm_s);
DieterGraef 0:584a18640e84 151 DBG("Offset: %lld", offset);
DieterGraef 0:584a18640e84 152 //Set time accordingly
DieterGraef 0:584a18640e84 153 set_time( time(NULL) + offset );
DieterGraef 0:584a18640e84 154
DieterGraef 0:584a18640e84 155 #ifdef __DEBUG__
DieterGraef 0:584a18640e84 156 ctTime = time(NULL);
DieterGraef 0:584a18640e84 157 DBG("Time is now (UTC): %s", ctime(&ctTime));
DieterGraef 0:584a18640e84 158 #endif
DieterGraef 0:584a18640e84 159
DieterGraef 0:584a18640e84 160 m_sock.close();
DieterGraef 0:584a18640e84 161
DieterGraef 0:584a18640e84 162 return NTP_OK;
DieterGraef 0:584a18640e84 163 }
DieterGraef 0:584a18640e84 164