Counter

Dependencies:   EthernetInterface NTPClient SDFileSystem TextLCD WebSocketClient mbed-rtos mbed Socket lwip-eth lwip-sys lwip FATFileSystem

Committer:
Tuxitheone
Date:
Mon Feb 29 18:59:15 2016 +0000
Revision:
0:ecaf3e593122
TankCounter

Who changed what in which revision?

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