EthernetNetIf Compatibility.

Dependents:   XBeeWiFi_SPI_example

Fork of NetServicesSource by Donatien Garnier

Committer:
donatien
Date:
Fri Jun 11 16:05:15 2010 +0000
Revision:
0:632c9925f013
Child:
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 0:632c9925f013 32 #define NTP_CLIENT_PORT 50420
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 0:632c9925f013 45 NTPClient::NTPClient() : 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 0:632c9925f013 71 #ifdef __LINKER_BUG_SOLVED__
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 0:632c9925f013 84 m_host = host;
donatien 0:632c9925f013 85 if(m_host.getIp().isNull())
donatien 0:632c9925f013 86 {
donatien 0:632c9925f013 87 //DNS query required
donatien 0:632c9925f013 88 m_pDnsReq = new DNSRequest();
donatien 0:632c9925f013 89 DBG("\r\nNTPClient : DNSRequest %p\r\n", m_pDnsReq);
donatien 0:632c9925f013 90 m_pDnsReq->setOnReply(this, &NTPClient::onDNSReply);
donatien 0:632c9925f013 91 m_pDnsReq->resolve(&m_host);
donatien 0:632c9925f013 92 return;
donatien 0:632c9925f013 93 }
donatien 0:632c9925f013 94 if(!m_host.getPort())
donatien 0:632c9925f013 95 {
donatien 0:632c9925f013 96 m_host.setPort(NTP_PORT);
donatien 0:632c9925f013 97 }
donatien 0:632c9925f013 98
donatien 0:632c9925f013 99 m_state = NTP_PING;
donatien 0:632c9925f013 100 Host localhost(IpAddr(127,0,0,1), NTP_CLIENT_PORT, "localhost");
donatien 0:632c9925f013 101 m_pUDPSocket->bind(localhost);
donatien 0:632c9925f013 102 set_time( 1280000000 ); //End of July 2010... just there to limit offset range
donatien 0:632c9925f013 103 process();
donatien 0:632c9925f013 104 }
donatien 0:632c9925f013 105
donatien 0:632c9925f013 106 void NTPClient::setOnResult( void (*pMethod)(NTPResult) )
donatien 0:632c9925f013 107 {
donatien 0:632c9925f013 108 m_pCb = pMethod;
donatien 0:632c9925f013 109 }
donatien 0:632c9925f013 110
donatien 0:632c9925f013 111 void NTPClient::init() //Create and setup socket if needed
donatien 0:632c9925f013 112 {
donatien 0:632c9925f013 113 if(!m_closed) //Already opened
donatien 0:632c9925f013 114 return;
donatien 0:632c9925f013 115 m_state = NTP_PING;
donatien 0:632c9925f013 116 m_pUDPSocket = new UDPSocket;
donatien 0:632c9925f013 117 m_pUDPSocket->setOnEvent(this, &NTPClient::onUDPSocketEvent);
donatien 0:632c9925f013 118 m_closed = false;
donatien 0:632c9925f013 119 }
donatien 0:632c9925f013 120
donatien 0:632c9925f013 121 void NTPClient::close()
donatien 0:632c9925f013 122 {
donatien 0:632c9925f013 123 if(m_closed)
donatien 0:632c9925f013 124 return;
donatien 0:632c9925f013 125 m_closed = true; //Prevent recursive calling or calling on an object being destructed by someone else
donatien 0:632c9925f013 126 m_watchdog.detach();
donatien 0:632c9925f013 127 m_pUDPSocket->resetOnEvent();
donatien 0:632c9925f013 128 m_pUDPSocket->close();
donatien 0:632c9925f013 129 delete m_pUDPSocket;
donatien 0:632c9925f013 130 if( m_pDnsReq )
donatien 0:632c9925f013 131 {
donatien 0:632c9925f013 132 m_pDnsReq->close();
donatien 0:632c9925f013 133 delete m_pDnsReq;
donatien 0:632c9925f013 134 m_pDnsReq = NULL;
donatien 0:632c9925f013 135 }
donatien 0:632c9925f013 136 }
donatien 0:632c9925f013 137
donatien 0:632c9925f013 138 #define MIN(a,b) ((a)<(b))?(a):(b)
donatien 0:632c9925f013 139 void NTPClient::process() //Main state-machine
donatien 0:632c9925f013 140 {
donatien 0:632c9925f013 141 NTPPacket pkt;
donatien 0:632c9925f013 142 int len;
donatien 0:632c9925f013 143 Host host;
donatien 0:632c9925f013 144
donatien 0:632c9925f013 145 switch(m_state)
donatien 0:632c9925f013 146 {
donatien 0:632c9925f013 147 case NTP_PING:
donatien 0:632c9925f013 148 DBG("\r\nPing\r\n");
donatien 0:632c9925f013 149 //Prepare NTP Packet:
donatien 0:632c9925f013 150 pkt.li = 0; //Leap Indicator : No warning
donatien 0:632c9925f013 151 pkt.vn = 4; //Version Number : 4
donatien 0:632c9925f013 152 pkt.mode = 3; //Client mode
donatien 0:632c9925f013 153 pkt.stratum = 0; //Not relevant here
donatien 0:632c9925f013 154 pkt.poll = 0; //Not significant as well
donatien 0:632c9925f013 155 pkt.precision = 0; //Neither this one is
donatien 0:632c9925f013 156
donatien 0:632c9925f013 157 pkt.rootDelay = 0; //Or this one
donatien 0:632c9925f013 158 pkt.rootDispersion = 0; //Or that one
donatien 0:632c9925f013 159 pkt.refId = 0; //...
donatien 0:632c9925f013 160
donatien 0:632c9925f013 161 pkt.refTm_s = 0;
donatien 0:632c9925f013 162 pkt.origTm_s = 0;
donatien 0:632c9925f013 163 pkt.rxTm_s = 0;
donatien 0:632c9925f013 164 pkt.txTm_s = htonl( NTP_TIMESTAMP_DELTA + time(NULL) ); //WARN: We are in LE format, network byte order is BE
donatien 0:632c9925f013 165
donatien 0:632c9925f013 166 pkt.refTm_f = pkt.origTm_f = pkt.rxTm_f = pkt.txTm_f = 0;
donatien 0:632c9925f013 167
donatien 0:632c9925f013 168 //Hex Dump:
donatien 0:632c9925f013 169 DBG("\r\nDump Tx:\r\n");
donatien 0:632c9925f013 170 for(int i = 0; i< sizeof(NTPPacket); i++)
donatien 0:632c9925f013 171 {
donatien 0:632c9925f013 172 DBG("%02x ", *((char*)&pkt + i));
donatien 0:632c9925f013 173 }
donatien 0:632c9925f013 174 DBG("\r\n\r\n");
donatien 0:632c9925f013 175
donatien 0:632c9925f013 176 len = m_pUDPSocket->sendto( (char*)&pkt, sizeof(NTPPacket), &m_host );
donatien 0:632c9925f013 177 if(len < sizeof(NTPPacket))
donatien 0:632c9925f013 178 { onResult(NTP_PRTCL); close(); return; }
donatien 0:632c9925f013 179
donatien 0:632c9925f013 180 m_state = NTP_PONG;
donatien 0:632c9925f013 181
donatien 0:632c9925f013 182 break;
donatien 0:632c9925f013 183
donatien 0:632c9925f013 184 case NTP_PONG:
donatien 0:632c9925f013 185 DBG("\r\nPong\r\n");
donatien 0:632c9925f013 186 while( len = m_pUDPSocket->recvfrom( (char*)&pkt, sizeof(NTPPacket), &host ) )
donatien 0:632c9925f013 187 {
donatien 0:632c9925f013 188 if( len <= 0 )
donatien 0:632c9925f013 189 break;
donatien 0:632c9925f013 190 if( !host.getIp().isEq(m_host.getIp()) )
donatien 0:632c9925f013 191 //if( !ip_addr_cmp( &host.getIp().getStruct(), &m_host.getIp().getStruct() ) ) //Was working like that, trying nicer impl above
donatien 0:632c9925f013 192 continue; //Not our packet
donatien 0:632c9925f013 193 if( len > 0 )
donatien 0:632c9925f013 194 break;
donatien 0:632c9925f013 195 }
donatien 0:632c9925f013 196
donatien 0:632c9925f013 197 if(len == 0)
donatien 0:632c9925f013 198 return; //Wait for the next packet
donatien 0:632c9925f013 199
donatien 0:632c9925f013 200 if(len < 0)
donatien 0:632c9925f013 201 { onResult(NTP_PRTCL); close(); return; }
donatien 0:632c9925f013 202
donatien 0:632c9925f013 203 if(len < sizeof(NTPPacket)) //TODO: Accept chunks
donatien 0:632c9925f013 204 { onResult(NTP_PRTCL); close(); return; }
donatien 0:632c9925f013 205
donatien 0:632c9925f013 206 //Hex Dump:
donatien 0:632c9925f013 207 DBG("\r\nDump Rx:\r\n");
donatien 0:632c9925f013 208 for(int i = 0; i< sizeof(NTPPacket); i++)
donatien 0:632c9925f013 209 {
donatien 0:632c9925f013 210 DBG("%02x ", *((char*)&pkt + i));
donatien 0:632c9925f013 211 }
donatien 0:632c9925f013 212 DBG("\r\n\r\n");
donatien 0:632c9925f013 213
donatien 0:632c9925f013 214 if( pkt.stratum == 0) //Kiss of death message : Not good !
donatien 0:632c9925f013 215 {
donatien 0:632c9925f013 216 onResult(NTP_PRTCL); close(); return;
donatien 0:632c9925f013 217 }
donatien 0:632c9925f013 218
donatien 0:632c9925f013 219 //Correct Endianness
donatien 0:632c9925f013 220 pkt.refTm_s = ntohl( pkt.refTm_s );
donatien 0:632c9925f013 221 pkt.refTm_f = ntohl( pkt.refTm_f );
donatien 0:632c9925f013 222 pkt.origTm_s = ntohl( pkt.origTm_s );
donatien 0:632c9925f013 223 pkt.origTm_f = ntohl( pkt.origTm_f );
donatien 0:632c9925f013 224 pkt.rxTm_s = ntohl( pkt.rxTm_s );
donatien 0:632c9925f013 225 pkt.rxTm_f = ntohl( pkt.rxTm_f );
donatien 0:632c9925f013 226 pkt.txTm_s = ntohl( pkt.txTm_s );
donatien 0:632c9925f013 227 pkt.txTm_f = ntohl( pkt.txTm_f );
donatien 0:632c9925f013 228
donatien 0:632c9925f013 229 //Compute offset, see RFC 4330 p.13
donatien 0:632c9925f013 230 uint32_t destTm_s = (NTP_TIMESTAMP_DELTA + time(NULL));
donatien 0:632c9925f013 231 //int32_t origTm = (int32_t) ((uint64_t) pkt.origTm - NTP_TIMESTAMP_DELTA); //Convert in local 32 bits timestamps
donatien 0:632c9925f013 232 //int32_t rxTm = (int32_t) ((uint64_t) pkt.rxTm - NTP_TIMESTAMP_DELTA); //Convert in local 32 bits timestamps
donatien 0:632c9925f013 233 //int32_t txTm = (int32_t) ((uint64_t) pkt.txTm - NTP_TIMESTAMP_DELTA); //Convert in local 32 bits timestamps
donatien 0:632c9925f013 234 // int64_t offset = ( ( ( pkt.rxTm_s - pkt.origTm_s ) + ( pkt.txTm_s - destTm_s ) ) << 32 + ( ( pkt.rxTm_f - pkt.origTm_f ) + ( pkt.txTm_f - 0 ) ) ) / 2;
donatien 0:632c9925f013 235 int64_t offset = ( (int64_t)( pkt.rxTm_s - pkt.origTm_s ) + (int64_t) ( pkt.txTm_s - destTm_s ) ) / 2; //Avoid overflow
donatien 0:632c9925f013 236 DBG("\r\nSent @%d\r\n", pkt.txTm_s);
donatien 0:632c9925f013 237 DBG("\r\nOffset: %d\r\n", offset);
donatien 0:632c9925f013 238 //Set time accordingly
donatien 0:632c9925f013 239 set_time( time(NULL) + (offset /*>> 32*/) );
donatien 0:632c9925f013 240
donatien 0:632c9925f013 241 onResult(NTP_OK);
donatien 0:632c9925f013 242 close();
donatien 0:632c9925f013 243
donatien 0:632c9925f013 244 break;
donatien 0:632c9925f013 245 }
donatien 0:632c9925f013 246 }
donatien 0:632c9925f013 247
donatien 0:632c9925f013 248 void NTPClient::setTimeout(int ms)
donatien 0:632c9925f013 249 {
donatien 0:632c9925f013 250 m_timeout = 1000*ms;
donatien 0:632c9925f013 251 resetTimeout();
donatien 0:632c9925f013 252 }
donatien 0:632c9925f013 253
donatien 0:632c9925f013 254 void NTPClient::resetTimeout()
donatien 0:632c9925f013 255 {
donatien 0:632c9925f013 256 m_watchdog.detach();
donatien 0:632c9925f013 257 m_watchdog.attach_us<NTPClient>(this, &NTPClient::onTimeout, m_timeout);
donatien 0:632c9925f013 258 }
donatien 0:632c9925f013 259
donatien 0:632c9925f013 260 void NTPClient::onTimeout() //Connection has timed out
donatien 0:632c9925f013 261 {
donatien 0:632c9925f013 262 close();
donatien 0:632c9925f013 263 onResult(NTP_TIMEOUT);
donatien 0:632c9925f013 264 }
donatien 0:632c9925f013 265
donatien 0:632c9925f013 266 void NTPClient::onDNSReply(DNSReply r)
donatien 0:632c9925f013 267 {
donatien 0:632c9925f013 268 if(m_closed)
donatien 0:632c9925f013 269 {
donatien 0:632c9925f013 270 DBG("\r\nWARN: Discarded\r\n");
donatien 0:632c9925f013 271 return;
donatien 0:632c9925f013 272 }
donatien 0:632c9925f013 273
donatien 0:632c9925f013 274 if( r != DNS_FOUND )
donatien 0:632c9925f013 275 {
donatien 0:632c9925f013 276 DBG("\r\nCould not resolve hostname.\r\n");
donatien 0:632c9925f013 277 onResult(NTP_DNS);
donatien 0:632c9925f013 278 close();
donatien 0:632c9925f013 279 return;
donatien 0:632c9925f013 280 }
donatien 0:632c9925f013 281 m_pDnsReq->close();
donatien 0:632c9925f013 282 delete m_pDnsReq;
donatien 0:632c9925f013 283 m_pDnsReq=NULL;
donatien 0:632c9925f013 284 doSetTime(m_host);
donatien 0:632c9925f013 285 }
donatien 0:632c9925f013 286
donatien 0:632c9925f013 287 void NTPClient::onUDPSocketEvent(UDPSocketEvent e)
donatien 0:632c9925f013 288 {
donatien 0:632c9925f013 289 switch(e)
donatien 0:632c9925f013 290 {
donatien 0:632c9925f013 291 case UDPSOCKET_READABLE: //The only event for now
donatien 0:632c9925f013 292 resetTimeout();
donatien 0:632c9925f013 293 process();
donatien 0:632c9925f013 294 break;
donatien 0:632c9925f013 295 }
donatien 0:632c9925f013 296 }
donatien 0:632c9925f013 297
donatien 0:632c9925f013 298 void NTPClient::onResult(NTPResult r) //Must be called by impl when the request completes
donatien 0:632c9925f013 299 {
donatien 0:632c9925f013 300 if(m_pCbItem && m_pCbMeth)
donatien 0:632c9925f013 301 (m_pCbItem->*m_pCbMeth)(r);
donatien 0:632c9925f013 302 else if(m_pCb)
donatien 0:632c9925f013 303 m_pCb(r);
donatien 0:632c9925f013 304 m_blockingResult = r; //Blocking mode
donatien 0:632c9925f013 305 }
donatien 0:632c9925f013 306
donatien 0:632c9925f013 307 NTPResult NTPClient::blockingProcess() //Called in blocking mode, calls Net::poll() until return code is available
donatien 0:632c9925f013 308 {
donatien 0:632c9925f013 309 m_blockingResult = NTP_PROCESSING;
donatien 0:632c9925f013 310 do
donatien 0:632c9925f013 311 {
donatien 0:632c9925f013 312 Net::poll();
donatien 0:632c9925f013 313 } while(m_blockingResult == NTP_PROCESSING);
donatien 0:632c9925f013 314 Net::poll(); //Necessary for cleanup
donatien 0:632c9925f013 315 return m_blockingResult;
donatien 0:632c9925f013 316 }