EthernetNetIf Compatibility.

Dependents:   XBeeWiFi_SPI_example

Fork of NetServicesSource by Donatien Garnier

Committer:
donatien
Date:
Tue Jul 27 15:59:42 2010 +0000
Revision:
5:dd63a1e02b1b
Parent:
2:a4f97773c90f

        

Who changed what in which revision?

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