Michael Fischler / Mbed 2 deprecated Contest_Winner

Dependencies:   mbed

Committer:
mafischl
Date:
Thu Oct 13 18:01:31 2011 +0000
Revision:
1:5a7cce9994a3
Parent:
0:d9266031f832

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mafischl 0:d9266031f832 1 #pragma diag_remark 1293
mafischl 0:d9266031f832 2 /*
mafischl 0:d9266031f832 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
mafischl 0:d9266031f832 4
mafischl 0:d9266031f832 5 Permission is hereby granted, free of charge, to any person obtaining a copy
mafischl 0:d9266031f832 6 of this software and associated documentation files (the "Software"), to deal
mafischl 0:d9266031f832 7 in the Software without restriction, including without limitation the rights
mafischl 0:d9266031f832 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
mafischl 0:d9266031f832 9 copies of the Software, and to permit persons to whom the Software is
mafischl 0:d9266031f832 10 furnished to do so, subject to the following conditions:
mafischl 0:d9266031f832 11
mafischl 0:d9266031f832 12 The above copyright notice and this permission notice shall be included in
mafischl 0:d9266031f832 13 all copies or substantial portions of the Software.
mafischl 0:d9266031f832 14
mafischl 0:d9266031f832 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
mafischl 0:d9266031f832 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
mafischl 0:d9266031f832 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
mafischl 0:d9266031f832 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
mafischl 0:d9266031f832 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mafischl 0:d9266031f832 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
mafischl 0:d9266031f832 21 THE SOFTWARE.
mafischl 0:d9266031f832 22 */
mafischl 0:d9266031f832 23
mafischl 0:d9266031f832 24 #include "NTPClient.h"
mafischl 0:d9266031f832 25
mafischl 0:d9266031f832 26 #include <stdio.h>
mafischl 0:d9266031f832 27
mafischl 0:d9266031f832 28 //#define __DEBUG
mafischl 0:d9266031f832 29 #include "dbg/dbg.h"
mafischl 0:d9266031f832 30
mafischl 0:d9266031f832 31 #define NTP_PORT 123
mafischl 0:d9266031f832 32 #define NTP_CLIENT_PORT 0//50420 //Random port
mafischl 0:d9266031f832 33 #define NTP_REQUEST_TIMEOUT 15000
mafischl 0:d9266031f832 34 #define NTP_TIMESTAMP_DELTA 2208988800ull //Diff btw a UNIX timestamp (Starting Jan, 1st 1970) and a NTP timestamp (Starting Jan, 1st 1900)
mafischl 0:d9266031f832 35
mafischl 0:d9266031f832 36 #define htons( x ) ( (( x << 8 ) & 0xFF00) | (( x >> 8 ) & 0x00FF) )
mafischl 0:d9266031f832 37 #define ntohs( x ) (htons(x))
mafischl 0:d9266031f832 38
mafischl 0:d9266031f832 39 #define htonl( x ) ( (( x << 24 ) & 0xFF000000) \
mafischl 0:d9266031f832 40 | (( x << 8 ) & 0x00FF0000) \
mafischl 0:d9266031f832 41 | (( x >> 8 ) & 0x0000FF00) \
mafischl 0:d9266031f832 42 | (( x >> 24 ) & 0x000000FF) )
mafischl 0:d9266031f832 43 #define ntohl( x ) (htonl(x))
mafischl 0:d9266031f832 44
mafischl 0:d9266031f832 45 NTPClient::NTPClient() : NetService(false), m_state(NTP_PING), m_pCbItem(NULL), m_pCbMeth(NULL), m_pCb(NULL),
mafischl 0:d9266031f832 46 m_watchdog(), m_timeout(0), m_closed(true), m_host(), m_pDnsReq(NULL), m_blockingResult(NTP_PROCESSING)
mafischl 0:d9266031f832 47 {
mafischl 0:d9266031f832 48 setTimeout(NTP_REQUEST_TIMEOUT);
mafischl 0:d9266031f832 49 DBG("\r\nNew NTPClient %p\r\n",this);
mafischl 0:d9266031f832 50 }
mafischl 0:d9266031f832 51
mafischl 0:d9266031f832 52 NTPClient::~NTPClient()
mafischl 0:d9266031f832 53 {
mafischl 0:d9266031f832 54 close();
mafischl 0:d9266031f832 55 }
mafischl 0:d9266031f832 56
mafischl 0:d9266031f832 57 //High level setup functions
mafischl 0:d9266031f832 58 NTPResult NTPClient::setTime(const Host& host) //Blocking
mafischl 0:d9266031f832 59 {
mafischl 0:d9266031f832 60 doSetTime(host);
mafischl 0:d9266031f832 61 return blockingProcess();
mafischl 0:d9266031f832 62 }
mafischl 0:d9266031f832 63
mafischl 0:d9266031f832 64 NTPResult NTPClient::setTime(const Host& host, void (*pMethod)(NTPResult)) //Non blocking
mafischl 0:d9266031f832 65 {
mafischl 0:d9266031f832 66 setOnResult(pMethod);
mafischl 0:d9266031f832 67 doSetTime(host);
mafischl 0:d9266031f832 68 return NTP_PROCESSING;
mafischl 0:d9266031f832 69 }
mafischl 0:d9266031f832 70
mafischl 0:d9266031f832 71 #if 0 //For doc only
mafischl 0:d9266031f832 72 template<class T>
mafischl 0:d9266031f832 73 NTPResult NTPClient::setTime(const Host& host, T* pItem, void (T::*pMethod)(NTPResult)) //Non blocking
mafischl 0:d9266031f832 74 {
mafischl 0:d9266031f832 75 setOnResult(pItem, pMethod);
mafischl 0:d9266031f832 76 doSetTime(host);
mafischl 0:d9266031f832 77 return NTP_PROCESSING;
mafischl 0:d9266031f832 78 }
mafischl 0:d9266031f832 79 #endif
mafischl 0:d9266031f832 80
mafischl 0:d9266031f832 81 void NTPClient::doSetTime(const Host& host)
mafischl 0:d9266031f832 82 {
mafischl 0:d9266031f832 83 init();
mafischl 0:d9266031f832 84 resetTimeout();
mafischl 0:d9266031f832 85 m_host = host;
mafischl 0:d9266031f832 86 if(!m_host.getPort())
mafischl 0:d9266031f832 87 {
mafischl 0:d9266031f832 88 m_host.setPort(NTP_PORT);
mafischl 0:d9266031f832 89 }
mafischl 0:d9266031f832 90 if(m_host.getIp().isNull())
mafischl 0:d9266031f832 91 {
mafischl 0:d9266031f832 92 //DNS query required
mafischl 0:d9266031f832 93 m_pDnsReq = new DNSRequest();
mafischl 0:d9266031f832 94 DBG("\r\nNTPClient : DNSRequest %p\r\n", m_pDnsReq);
mafischl 0:d9266031f832 95 m_pDnsReq->setOnReply(this, &NTPClient::onDNSReply);
mafischl 0:d9266031f832 96 m_pDnsReq->resolve(&m_host);
mafischl 0:d9266031f832 97 return;
mafischl 0:d9266031f832 98 }
mafischl 0:d9266031f832 99 open();
mafischl 0:d9266031f832 100 }
mafischl 0:d9266031f832 101
mafischl 0:d9266031f832 102 void NTPClient::setOnResult( void (*pMethod)(NTPResult) )
mafischl 0:d9266031f832 103 {
mafischl 0:d9266031f832 104 m_pCb = pMethod;
mafischl 0:d9266031f832 105 }
mafischl 0:d9266031f832 106
mafischl 0:d9266031f832 107 void NTPClient::close()
mafischl 0:d9266031f832 108 {
mafischl 0:d9266031f832 109 if(m_closed)
mafischl 0:d9266031f832 110 return;
mafischl 0:d9266031f832 111 m_closed = true; //Prevent recursive calling or calling on an object being destructed by someone else
mafischl 0:d9266031f832 112 m_watchdog.stop();
mafischl 0:d9266031f832 113 m_pUDPSocket->resetOnEvent();
mafischl 0:d9266031f832 114 m_pUDPSocket->close();
mafischl 0:d9266031f832 115 delete m_pUDPSocket;
mafischl 0:d9266031f832 116 if( m_pDnsReq )
mafischl 0:d9266031f832 117 {
mafischl 0:d9266031f832 118 m_pDnsReq->close();
mafischl 0:d9266031f832 119 delete m_pDnsReq;
mafischl 0:d9266031f832 120 m_pDnsReq = NULL;
mafischl 0:d9266031f832 121 }
mafischl 0:d9266031f832 122 }
mafischl 0:d9266031f832 123
mafischl 0:d9266031f832 124 void NTPClient::poll() //Called by NetServices
mafischl 0:d9266031f832 125 {
mafischl 0:d9266031f832 126 if( (!m_closed) && (m_watchdog.read_ms() >= m_timeout) )
mafischl 0:d9266031f832 127 {
mafischl 0:d9266031f832 128 onTimeout();
mafischl 0:d9266031f832 129 }
mafischl 0:d9266031f832 130 }
mafischl 0:d9266031f832 131
mafischl 0:d9266031f832 132 void NTPClient::init() //Create and setup socket if needed
mafischl 0:d9266031f832 133 {
mafischl 0:d9266031f832 134 if(!m_closed) //Already opened
mafischl 0:d9266031f832 135 return;
mafischl 0:d9266031f832 136 m_state = NTP_PING;
mafischl 0:d9266031f832 137 m_pUDPSocket = new UDPSocket;
mafischl 0:d9266031f832 138 m_pUDPSocket->setOnEvent(this, &NTPClient::onUDPSocketEvent);
mafischl 0:d9266031f832 139 m_closed = false;
mafischl 0:d9266031f832 140 DBG("NTPClient: Init OK\n");
mafischl 0:d9266031f832 141 }
mafischl 0:d9266031f832 142
mafischl 0:d9266031f832 143 void NTPClient::open()
mafischl 0:d9266031f832 144 {
mafischl 0:d9266031f832 145 resetTimeout();
mafischl 0:d9266031f832 146 DBG("Opening connection\n");
mafischl 0:d9266031f832 147 m_state = NTP_PING;
mafischl 0:d9266031f832 148 Host localhost(IpAddr(), NTP_CLIENT_PORT, "localhost"); //Any local address
mafischl 0:d9266031f832 149 m_pUDPSocket->bind(localhost);
mafischl 0:d9266031f832 150 if ((int)time(NULL) < 1280000000) set_time( 1280000000 ); //End of July 2010... just there to limit offset range
mafischl 0:d9266031f832 151 process();
mafischl 0:d9266031f832 152 }
mafischl 0:d9266031f832 153
mafischl 0:d9266031f832 154 #define MIN(a,b) ((a)<(b))?(a):(b)
mafischl 0:d9266031f832 155 void NTPClient::process() //Main state-machine
mafischl 0:d9266031f832 156 {
mafischl 0:d9266031f832 157 int len;
mafischl 0:d9266031f832 158 Host host;
mafischl 0:d9266031f832 159
mafischl 0:d9266031f832 160 switch(m_state)
mafischl 0:d9266031f832 161 {
mafischl 0:d9266031f832 162 case NTP_PING:
mafischl 0:d9266031f832 163 DBG("\r\nPing\r\n");
mafischl 0:d9266031f832 164 //Prepare NTP Packet:
mafischl 0:d9266031f832 165 m_pkt.li = 0; //Leap Indicator : No warning
mafischl 0:d9266031f832 166 m_pkt.vn = 4; //Version Number : 4
mafischl 0:d9266031f832 167 m_pkt.mode = 3; //Client mode
mafischl 0:d9266031f832 168 m_pkt.stratum = 0; //Not relevant here
mafischl 0:d9266031f832 169 m_pkt.poll = 0; //Not significant as well
mafischl 0:d9266031f832 170 m_pkt.precision = 0; //Neither this one is
mafischl 0:d9266031f832 171
mafischl 0:d9266031f832 172 m_pkt.rootDelay = 0; //Or this one
mafischl 0:d9266031f832 173 m_pkt.rootDispersion = 0; //Or that one
mafischl 0:d9266031f832 174 m_pkt.refId = 0; //...
mafischl 0:d9266031f832 175
mafischl 0:d9266031f832 176 m_pkt.refTm_s = 0;
mafischl 0:d9266031f832 177 m_pkt.origTm_s = 0;
mafischl 0:d9266031f832 178 m_pkt.rxTm_s = 0;
mafischl 0:d9266031f832 179 m_pkt.txTm_s = htonl( NTP_TIMESTAMP_DELTA + time(NULL) ); //WARN: We are in LE format, network byte order is BE
mafischl 0:d9266031f832 180
mafischl 0:d9266031f832 181 m_pkt.refTm_f = m_pkt.origTm_f = m_pkt.rxTm_f = m_pkt.txTm_f = 0;
mafischl 0:d9266031f832 182
mafischl 0:d9266031f832 183 #ifdef __DEBUG
mafischl 0:d9266031f832 184 //Hex Dump:
mafischl 0:d9266031f832 185 DBG("\r\nDump Tx:\r\n");
mafischl 0:d9266031f832 186 for(int i = 0; i< sizeof(NTPPacket); i++)
mafischl 0:d9266031f832 187 {
mafischl 0:d9266031f832 188 DBG("%02x ", *((char*)&m_pkt + i));
mafischl 0:d9266031f832 189 }
mafischl 0:d9266031f832 190 DBG("\r\n\r\n");
mafischl 0:d9266031f832 191 #endif
mafischl 0:d9266031f832 192
mafischl 0:d9266031f832 193 len = m_pUDPSocket->sendto( (char*)&m_pkt, sizeof(NTPPacket), &m_host );
mafischl 0:d9266031f832 194 if(len < sizeof(NTPPacket))
mafischl 0:d9266031f832 195 { onResult(NTP_PRTCL); close(); return; }
mafischl 0:d9266031f832 196
mafischl 0:d9266031f832 197 m_state = NTP_PONG;
mafischl 0:d9266031f832 198
mafischl 0:d9266031f832 199 break;
mafischl 0:d9266031f832 200
mafischl 0:d9266031f832 201 case NTP_PONG:
mafischl 0:d9266031f832 202 DBG("\r\nPong\r\n");
mafischl 0:d9266031f832 203 while( len = m_pUDPSocket->recvfrom( (char*)&m_pkt, sizeof(NTPPacket), &host ) )
mafischl 0:d9266031f832 204 {
mafischl 0:d9266031f832 205 if( len <= 0 )
mafischl 0:d9266031f832 206 break;
mafischl 0:d9266031f832 207 if( !host.getIp().isEq(m_host.getIp()) )
mafischl 0:d9266031f832 208 continue; //Not our packet
mafischl 0:d9266031f832 209 if( len > 0 )
mafischl 0:d9266031f832 210 break;
mafischl 0:d9266031f832 211 }
mafischl 0:d9266031f832 212
mafischl 0:d9266031f832 213 if(len == 0)
mafischl 0:d9266031f832 214 return; //Wait for the next packet
mafischl 0:d9266031f832 215
mafischl 0:d9266031f832 216 if(len < 0)
mafischl 0:d9266031f832 217 { onResult(NTP_PRTCL); close(); return; }
mafischl 0:d9266031f832 218
mafischl 0:d9266031f832 219 if(len < sizeof(NTPPacket)) //TODO: Accept chunks
mafischl 0:d9266031f832 220 { onResult(NTP_PRTCL); close(); return; }
mafischl 0:d9266031f832 221
mafischl 0:d9266031f832 222 #ifdef __DEBUG
mafischl 0:d9266031f832 223 //Hex Dump:
mafischl 0:d9266031f832 224 DBG("\r\nDump Rx:\r\n");
mafischl 0:d9266031f832 225 for(int i = 0; i< sizeof(NTPPacket); i++)
mafischl 0:d9266031f832 226 {
mafischl 0:d9266031f832 227 DBG("%02x ", *((char*)&m_pkt + i));
mafischl 0:d9266031f832 228 }
mafischl 0:d9266031f832 229 DBG("\r\n\r\n");
mafischl 0:d9266031f832 230 #endif
mafischl 0:d9266031f832 231
mafischl 0:d9266031f832 232 if( m_pkt.stratum == 0) //Kiss of death message : Not good !
mafischl 0:d9266031f832 233 {
mafischl 0:d9266031f832 234 onResult(NTP_PRTCL); close(); return;
mafischl 0:d9266031f832 235 }
mafischl 0:d9266031f832 236
mafischl 0:d9266031f832 237 //Correct Endianness
mafischl 0:d9266031f832 238 m_pkt.refTm_s = ntohl( m_pkt.refTm_s );
mafischl 0:d9266031f832 239 m_pkt.refTm_f = ntohl( m_pkt.refTm_f );
mafischl 0:d9266031f832 240 m_pkt.origTm_s = ntohl( m_pkt.origTm_s );
mafischl 0:d9266031f832 241 m_pkt.origTm_f = ntohl( m_pkt.origTm_f );
mafischl 0:d9266031f832 242 m_pkt.rxTm_s = ntohl( m_pkt.rxTm_s );
mafischl 0:d9266031f832 243 m_pkt.rxTm_f = ntohl( m_pkt.rxTm_f );
mafischl 0:d9266031f832 244 m_pkt.txTm_s = ntohl( m_pkt.txTm_s );
mafischl 0:d9266031f832 245 m_pkt.txTm_f = ntohl( m_pkt.txTm_f );
mafischl 0:d9266031f832 246
mafischl 0:d9266031f832 247 //Compute offset, see RFC 4330 p.13
mafischl 0:d9266031f832 248 uint32_t destTm_s = (NTP_TIMESTAMP_DELTA + time(NULL));
mafischl 0:d9266031f832 249 //int32_t origTm = (int32_t) ((uint64_t) m_pkt.origTm - NTP_TIMESTAMP_DELTA); //Convert in local 32 bits timestamps
mafischl 0:d9266031f832 250 //int32_t rxTm = (int32_t) ((uint64_t) m_pkt.rxTm - NTP_TIMESTAMP_DELTA); //Convert in local 32 bits timestamps
mafischl 0:d9266031f832 251 //int32_t txTm = (int32_t) ((uint64_t) m_pkt.txTm - NTP_TIMESTAMP_DELTA); //Convert in local 32 bits timestamps
mafischl 0:d9266031f832 252 // int64_t offset = ( ( ( m_pkt.rxTm_s - m_pkt.origTm_s ) + ( m_pkt.txTm_s - destTm_s ) ) << 32 + ( ( m_pkt.rxTm_f - m_pkt.origTm_f ) + ( m_pkt.txTm_f - 0 ) ) ) / 2;
mafischl 0:d9266031f832 253 int64_t offset = ( (int64_t)( m_pkt.rxTm_s - m_pkt.origTm_s ) + (int64_t) ( m_pkt.txTm_s - destTm_s ) ) / 2; //Avoid overflow
mafischl 0:d9266031f832 254 DBG("\r\nSent @%d\r\n", m_pkt.txTm_s);
mafischl 0:d9266031f832 255 DBG("\r\nOffset: %d\r\n", offset);
mafischl 0:d9266031f832 256 //Set time accordingly
mafischl 0:d9266031f832 257 set_time( time(NULL) + (offset /*>> 32*/) );
mafischl 0:d9266031f832 258
mafischl 0:d9266031f832 259 onResult(NTP_OK);
mafischl 0:d9266031f832 260 close();
mafischl 0:d9266031f832 261
mafischl 0:d9266031f832 262 break;
mafischl 0:d9266031f832 263 }
mafischl 0:d9266031f832 264 }
mafischl 0:d9266031f832 265
mafischl 0:d9266031f832 266 void NTPClient::setTimeout(int ms)
mafischl 0:d9266031f832 267 {
mafischl 0:d9266031f832 268 m_timeout = ms;
mafischl 0:d9266031f832 269 }
mafischl 0:d9266031f832 270
mafischl 0:d9266031f832 271 void NTPClient::resetTimeout()
mafischl 0:d9266031f832 272 {
mafischl 0:d9266031f832 273 m_watchdog.reset();
mafischl 0:d9266031f832 274 m_watchdog.start();
mafischl 0:d9266031f832 275 }
mafischl 0:d9266031f832 276
mafischl 0:d9266031f832 277 void NTPClient::onTimeout() //Connection has timed out
mafischl 0:d9266031f832 278 {
mafischl 0:d9266031f832 279 close();
mafischl 0:d9266031f832 280 onResult(NTP_TIMEOUT);
mafischl 0:d9266031f832 281 }
mafischl 0:d9266031f832 282
mafischl 0:d9266031f832 283 void NTPClient::onDNSReply(DNSReply r)
mafischl 0:d9266031f832 284 {
mafischl 0:d9266031f832 285 if(m_closed)
mafischl 0:d9266031f832 286 {
mafischl 0:d9266031f832 287 DBG("\r\nWARN: Discarded\r\n");
mafischl 0:d9266031f832 288 return;
mafischl 0:d9266031f832 289 }
mafischl 0:d9266031f832 290
mafischl 0:d9266031f832 291 if( r != DNS_FOUND )
mafischl 0:d9266031f832 292 {
mafischl 0:d9266031f832 293 DBG("\r\nCould not resolve hostname.\r\n");
mafischl 0:d9266031f832 294 onResult(NTP_DNS);
mafischl 0:d9266031f832 295 close();
mafischl 0:d9266031f832 296 return;
mafischl 0:d9266031f832 297 }
mafischl 0:d9266031f832 298 DBG("\r\nDNS resolved.\r\n");
mafischl 0:d9266031f832 299 m_pDnsReq->close();
mafischl 0:d9266031f832 300 delete m_pDnsReq;
mafischl 0:d9266031f832 301 m_pDnsReq=NULL;
mafischl 0:d9266031f832 302 open();
mafischl 0:d9266031f832 303 }
mafischl 0:d9266031f832 304
mafischl 0:d9266031f832 305 void NTPClient::onUDPSocketEvent(UDPSocketEvent e)
mafischl 0:d9266031f832 306 {
mafischl 0:d9266031f832 307 resetTimeout();
mafischl 0:d9266031f832 308 switch(e)
mafischl 0:d9266031f832 309 {
mafischl 0:d9266031f832 310 case UDPSOCKET_READABLE: //The only event for now
mafischl 0:d9266031f832 311 resetTimeout();
mafischl 0:d9266031f832 312 process();
mafischl 0:d9266031f832 313 break;
mafischl 0:d9266031f832 314 }
mafischl 0:d9266031f832 315 }
mafischl 0:d9266031f832 316
mafischl 0:d9266031f832 317 void NTPClient::onResult(NTPResult r) //Must be called by impl when the request completes
mafischl 0:d9266031f832 318 {
mafischl 0:d9266031f832 319 if(m_pCbItem && m_pCbMeth)
mafischl 0:d9266031f832 320 (m_pCbItem->*m_pCbMeth)(r);
mafischl 0:d9266031f832 321 else if(m_pCb)
mafischl 0:d9266031f832 322 m_pCb(r);
mafischl 0:d9266031f832 323 m_blockingResult = r; //Blocking mode
mafischl 0:d9266031f832 324 }
mafischl 0:d9266031f832 325
mafischl 0:d9266031f832 326 NTPResult NTPClient::blockingProcess() //Called in blocking mode, calls Net::poll() until return code is available
mafischl 0:d9266031f832 327 {
mafischl 0:d9266031f832 328 m_blockingResult = NTP_PROCESSING;
mafischl 0:d9266031f832 329 do
mafischl 0:d9266031f832 330 {
mafischl 0:d9266031f832 331 Net::poll();
mafischl 0:d9266031f832 332 } while(m_blockingResult == NTP_PROCESSING);
mafischl 0:d9266031f832 333 Net::poll(); //Necessary for cleanup
mafischl 0:d9266031f832 334 return m_blockingResult;
mafischl 0:d9266031f832 335 }