Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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