NTP

Dependents:   DISCO-F746NG_Ethernet gather_sensor_data final DISCO-F746NG_Ethernet

Committer:
DieterGraef
Date:
Sun Jun 19 16:24:06 2016 +0000
Revision:
0:584a18640e84
NTPClient

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"
DieterGraef 0:584a18640e84 38
DieterGraef 0:584a18640e84 39 #include "UDPSocket.h"
DieterGraef 0:584a18640e84 40
DieterGraef 0:584a18640e84 41 #include "mbed.h" //time() and set_time()
DieterGraef 0:584a18640e84 42
DieterGraef 0:584a18640e84 43 #define NTP_PORT 123
DieterGraef 0:584a18640e84 44 #define NTP_CLIENT_PORT 0 //Random port
DieterGraef 0:584a18640e84 45 #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 46
DieterGraef 0:584a18640e84 47 NTPClient::NTPClient() : m_sock()
DieterGraef 0:584a18640e84 48 {
DieterGraef 0:584a18640e84 49
DieterGraef 0:584a18640e84 50
DieterGraef 0:584a18640e84 51 }
DieterGraef 0:584a18640e84 52
DieterGraef 0:584a18640e84 53 NTPResult NTPClient::setTime(const char* host, uint16_t port, uint32_t timeout)
DieterGraef 0:584a18640e84 54 {
DieterGraef 0:584a18640e84 55 #ifdef __DEBUG__
DieterGraef 0:584a18640e84 56 time_t ctTime;
DieterGraef 0:584a18640e84 57 ctTime = time(NULL);
DieterGraef 0:584a18640e84 58 DBG("Time is set to (UTC): %s", ctime(&ctTime));
DieterGraef 0:584a18640e84 59 #endif
DieterGraef 0:584a18640e84 60
DieterGraef 0:584a18640e84 61 //Create & bind socket
DieterGraef 0:584a18640e84 62 DBG("Binding socket");
DieterGraef 0:584a18640e84 63 m_sock.bind(0); //Bind to a random port
DieterGraef 0:584a18640e84 64
DieterGraef 0:584a18640e84 65 m_sock.set_blocking(false, timeout); //Set not blocking
DieterGraef 0:584a18640e84 66
DieterGraef 0:584a18640e84 67 struct NTPPacket pkt;
DieterGraef 0:584a18640e84 68
DieterGraef 0:584a18640e84 69 //Now ping the server and wait for response
DieterGraef 0:584a18640e84 70 DBG("Ping");
DieterGraef 0:584a18640e84 71 //Prepare NTP Packet:
DieterGraef 0:584a18640e84 72 pkt.li = 0; //Leap Indicator : No warning
DieterGraef 0:584a18640e84 73 pkt.vn = 4; //Version Number : 4
DieterGraef 0:584a18640e84 74 pkt.mode = 3; //Client mode
DieterGraef 0:584a18640e84 75 pkt.stratum = 0; //Not relevant here
DieterGraef 0:584a18640e84 76 pkt.poll = 0; //Not significant as well
DieterGraef 0:584a18640e84 77 pkt.precision = 0; //Neither this one is
DieterGraef 0:584a18640e84 78
DieterGraef 0:584a18640e84 79 pkt.rootDelay = 0; //Or this one
DieterGraef 0:584a18640e84 80 pkt.rootDispersion = 0; //Or that one
DieterGraef 0:584a18640e84 81 pkt.refId = 0; //...
DieterGraef 0:584a18640e84 82
DieterGraef 0:584a18640e84 83 pkt.refTm_s = 0;
DieterGraef 0:584a18640e84 84 pkt.origTm_s = 0;
DieterGraef 0:584a18640e84 85 pkt.rxTm_s = 0;
DieterGraef 0:584a18640e84 86 pkt.txTm_s = htonl( NTP_TIMESTAMP_DELTA + time(NULL) ); //WARN: We are in LE format, network byte order is BE
DieterGraef 0:584a18640e84 87
DieterGraef 0:584a18640e84 88 pkt.refTm_f = pkt.origTm_f = pkt.rxTm_f = pkt.txTm_f = 0;
DieterGraef 0:584a18640e84 89
DieterGraef 0:584a18640e84 90 Endpoint outEndpoint;
DieterGraef 0:584a18640e84 91
DieterGraef 0:584a18640e84 92 if( outEndpoint.set_address(host, port) < 0)
DieterGraef 0:584a18640e84 93 {
DieterGraef 0:584a18640e84 94 m_sock.close();
DieterGraef 0:584a18640e84 95 return NTP_DNS;
DieterGraef 0:584a18640e84 96 }
DieterGraef 0:584a18640e84 97
DieterGraef 0:584a18640e84 98 //Set timeout, non-blocking and wait using select
DieterGraef 0:584a18640e84 99 int ret = m_sock.sendTo( outEndpoint, (char*)&pkt, sizeof(NTPPacket) );
DieterGraef 0:584a18640e84 100 if (ret < 0 )
DieterGraef 0:584a18640e84 101 {
DieterGraef 0:584a18640e84 102 ERR("Could not send packet");
DieterGraef 0:584a18640e84 103 m_sock.close();
DieterGraef 0:584a18640e84 104 return NTP_CONN;
DieterGraef 0:584a18640e84 105 }
DieterGraef 0:584a18640e84 106
DieterGraef 0:584a18640e84 107 //Read response
DieterGraef 0:584a18640e84 108 Endpoint inEndpoint;
DieterGraef 0:584a18640e84 109 // Set the inEndpoint address property
DieterGraef 0:584a18640e84 110 inEndpoint.set_address(outEndpoint.get_address(), 0);
DieterGraef 0:584a18640e84 111 DBG("Pong");
DieterGraef 0:584a18640e84 112 do
DieterGraef 0:584a18640e84 113 {
DieterGraef 0:584a18640e84 114 ret = m_sock.receiveFrom( inEndpoint, (char*)&pkt, sizeof(NTPPacket) ); //FIXME need a DNS Resolver to actually compare the incoming address with the DNS name
DieterGraef 0:584a18640e84 115 if(ret < 0)
DieterGraef 0:584a18640e84 116 {
DieterGraef 0:584a18640e84 117 ERR("Could not receive packet");
DieterGraef 0:584a18640e84 118 m_sock.close();
DieterGraef 0:584a18640e84 119 return NTP_CONN;
DieterGraef 0:584a18640e84 120 }
DieterGraef 0:584a18640e84 121 } while( strcmp(outEndpoint.get_address(), inEndpoint.get_address()) != 0 );
DieterGraef 0:584a18640e84 122
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