This is a fork of the NTPClient Library to work with the mbed-os 5 way of doing networking

Fork of NTPClient by Dieter Graef

Committer:
jhd25
Date:
Sun Feb 25 11:13:37 2018 +0000
Revision:
2:eda6db3ada54
Parent:
1:52e0f030d00e
Changed to stdint.h from cstdint

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 #include "NTPClient.h"
jhd25 1:52e0f030d00e 21 #include "EthernetInterface.h"
DieterGraef 0:584a18640e84 22 #include "UDPSocket.h"
DieterGraef 0:584a18640e84 23 #include "mbed.h" //time() and set_time()
jhd25 1:52e0f030d00e 24 #include "lwip/opt.h"
jhd25 1:52e0f030d00e 25 #include "lwip/def.h"
DieterGraef 0:584a18640e84 26 #define NTP_PORT 123
DieterGraef 0:584a18640e84 27 #define NTP_CLIENT_PORT 0 //Random port
DieterGraef 0:584a18640e84 28 #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 29
jhd25 1:52e0f030d00e 30 NTPClient::NTPClient(EthernetInterface& net_stack) : m_sock(&net_stack)
DieterGraef 0:584a18640e84 31 {
jhd25 1:52e0f030d00e 32 EthernetInterface nst = net_stack;
DieterGraef 0:584a18640e84 33
DieterGraef 0:584a18640e84 34 }
DieterGraef 0:584a18640e84 35
DieterGraef 0:584a18640e84 36 NTPResult NTPClient::setTime(const char* host, uint16_t port, uint32_t timeout)
DieterGraef 0:584a18640e84 37 {
DieterGraef 0:584a18640e84 38
DieterGraef 0:584a18640e84 39 //Create & bind socket
jhd25 1:52e0f030d00e 40 SocketAddress inEndpoint("0.0.0.0", 0);
jhd25 1:52e0f030d00e 41 m_sock.bind(inEndpoint); //Bind to a random port
jhd25 1:52e0f030d00e 42 m_sock.set_blocking(false); //Set not blocking
jhd25 1:52e0f030d00e 43 SocketAddress outEndpoint(&nst,host,port);
DieterGraef 0:584a18640e84 44 struct NTPPacket pkt;
DieterGraef 0:584a18640e84 45
DieterGraef 0:584a18640e84 46 //Now ping the server and wait for response
DieterGraef 0:584a18640e84 47 //Prepare NTP Packet:
DieterGraef 0:584a18640e84 48 pkt.li = 0; //Leap Indicator : No warning
DieterGraef 0:584a18640e84 49 pkt.vn = 4; //Version Number : 4
DieterGraef 0:584a18640e84 50 pkt.mode = 3; //Client mode
DieterGraef 0:584a18640e84 51 pkt.stratum = 0; //Not relevant here
DieterGraef 0:584a18640e84 52 pkt.poll = 0; //Not significant as well
DieterGraef 0:584a18640e84 53 pkt.precision = 0; //Neither this one is
DieterGraef 0:584a18640e84 54
DieterGraef 0:584a18640e84 55 pkt.rootDelay = 0; //Or this one
DieterGraef 0:584a18640e84 56 pkt.rootDispersion = 0; //Or that one
DieterGraef 0:584a18640e84 57 pkt.refId = 0; //...
DieterGraef 0:584a18640e84 58
DieterGraef 0:584a18640e84 59 pkt.refTm_s = 0;
DieterGraef 0:584a18640e84 60 pkt.origTm_s = 0;
DieterGraef 0:584a18640e84 61 pkt.rxTm_s = 0;
DieterGraef 0:584a18640e84 62 pkt.txTm_s = htonl( NTP_TIMESTAMP_DELTA + time(NULL) ); //WARN: We are in LE format, network byte order is BE
DieterGraef 0:584a18640e84 63
DieterGraef 0:584a18640e84 64 pkt.refTm_f = pkt.origTm_f = pkt.rxTm_f = pkt.txTm_f = 0;
DieterGraef 0:584a18640e84 65
DieterGraef 0:584a18640e84 66 //Set timeout, non-blocking and wait using select
jhd25 1:52e0f030d00e 67 int ret = m_sock.sendto(outEndpoint, &pkt, sizeof(NTPPacket));
jhd25 1:52e0f030d00e 68
DieterGraef 0:584a18640e84 69 if (ret < 0 )
DieterGraef 0:584a18640e84 70 {
DieterGraef 0:584a18640e84 71 m_sock.close();
DieterGraef 0:584a18640e84 72 return NTP_CONN;
DieterGraef 0:584a18640e84 73 }
DieterGraef 0:584a18640e84 74
DieterGraef 0:584a18640e84 75 //Read response
DieterGraef 0:584a18640e84 76 // Set the inEndpoint address property
jhd25 2:eda6db3ada54 77 int tries =0;
jhd25 2:eda6db3ada54 78 while (true) {
jhd25 1:52e0f030d00e 79 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 80 wait(0.01);
jhd25 1:52e0f030d00e 81 if((ret < 0 && ret != -3001) || tries >100)
DieterGraef 0:584a18640e84 82 {
DieterGraef 0:584a18640e84 83 m_sock.close();
DieterGraef 0:584a18640e84 84 return NTP_CONN;
DieterGraef 0:584a18640e84 85 }
jhd25 1:52e0f030d00e 86 if (strcmp(outEndpoint.get_ip_address(), inEndpoint.get_ip_address()) == 0 ) break;
jhd25 1:52e0f030d00e 87 tries++;
jhd25 1:52e0f030d00e 88 }
jhd25 1:52e0f030d00e 89 printf("NTP Server Address: %s \r\n",outEndpoint.get_ip_address());
DieterGraef 0:584a18640e84 90 if(ret < sizeof(NTPPacket)) //TODO: Accept chunks
DieterGraef 0:584a18640e84 91 {
DieterGraef 0:584a18640e84 92 m_sock.close();
DieterGraef 0:584a18640e84 93 return NTP_PRTCL;
DieterGraef 0:584a18640e84 94 }
DieterGraef 0:584a18640e84 95
DieterGraef 0:584a18640e84 96 if( pkt.stratum == 0) //Kiss of death message : Not good !
DieterGraef 0:584a18640e84 97 {
DieterGraef 0:584a18640e84 98 m_sock.close();
DieterGraef 0:584a18640e84 99 return NTP_PRTCL;
DieterGraef 0:584a18640e84 100 }
DieterGraef 0:584a18640e84 101
DieterGraef 0:584a18640e84 102 //Correct Endianness
DieterGraef 0:584a18640e84 103 pkt.refTm_s = ntohl( pkt.refTm_s );
DieterGraef 0:584a18640e84 104 pkt.refTm_f = ntohl( pkt.refTm_f );
DieterGraef 0:584a18640e84 105 pkt.origTm_s = ntohl( pkt.origTm_s );
DieterGraef 0:584a18640e84 106 pkt.origTm_f = ntohl( pkt.origTm_f );
DieterGraef 0:584a18640e84 107 pkt.rxTm_s = ntohl( pkt.rxTm_s );
DieterGraef 0:584a18640e84 108 pkt.rxTm_f = ntohl( pkt.rxTm_f );
DieterGraef 0:584a18640e84 109 pkt.txTm_s = ntohl( pkt.txTm_s );
DieterGraef 0:584a18640e84 110 pkt.txTm_f = ntohl( pkt.txTm_f );
DieterGraef 0:584a18640e84 111
DieterGraef 0:584a18640e84 112 //Compute offset, see RFC 4330 p.13
DieterGraef 0:584a18640e84 113 uint32_t destTm_s = (NTP_TIMESTAMP_DELTA + time(NULL));
DieterGraef 0:584a18640e84 114 int64_t offset = ( (int64_t)( pkt.rxTm_s - pkt.origTm_s ) + (int64_t) ( pkt.txTm_s - destTm_s ) ) / 2; //Avoid overflow
DieterGraef 0:584a18640e84 115 //Set time accordingly
DieterGraef 0:584a18640e84 116 set_time( time(NULL) + offset );
DieterGraef 0:584a18640e84 117
DieterGraef 0:584a18640e84 118
DieterGraef 0:584a18640e84 119 m_sock.close();
DieterGraef 0:584a18640e84 120
DieterGraef 0:584a18640e84 121 return NTP_OK;
DieterGraef 0:584a18640e84 122 }
DieterGraef 0:584a18640e84 123