EthernetNetIf Compatibility.

Dependents:   XBeeWiFi_SPI_example

Fork of NetServicesSource by Donatien Garnier

Committer:
donatien
Date:
Fri Jun 18 09:22:54 2010 +0000
Revision:
2:a4f97773c90f
Parent:
0:632c9925f013
Child:
5:dd63a1e02b1b

        

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 2:a4f97773c90f 148 Host localhost(IpAddr(127,0,0,1), NTP_CLIENT_PORT, "localhost");
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 0:632c9925f013 183 //Hex Dump:
donatien 0:632c9925f013 184 DBG("\r\nDump Tx:\r\n");
donatien 0:632c9925f013 185 for(int i = 0; i< sizeof(NTPPacket); i++)
donatien 0:632c9925f013 186 {
donatien 2:a4f97773c90f 187 DBG("%02x ", *((char*)&m_pkt + i));
donatien 0:632c9925f013 188 }
donatien 0:632c9925f013 189 DBG("\r\n\r\n");
donatien 0:632c9925f013 190
donatien 2:a4f97773c90f 191 len = m_pUDPSocket->sendto( (char*)&m_pkt, sizeof(NTPPacket), &m_host );
donatien 0:632c9925f013 192 if(len < sizeof(NTPPacket))
donatien 0:632c9925f013 193 { onResult(NTP_PRTCL); close(); return; }
donatien 0:632c9925f013 194
donatien 0:632c9925f013 195 m_state = NTP_PONG;
donatien 0:632c9925f013 196
donatien 0:632c9925f013 197 break;
donatien 0:632c9925f013 198
donatien 0:632c9925f013 199 case NTP_PONG:
donatien 0:632c9925f013 200 DBG("\r\nPong\r\n");
donatien 2:a4f97773c90f 201 while( len = m_pUDPSocket->recvfrom( (char*)&m_pkt, sizeof(NTPPacket), &host ) )
donatien 0:632c9925f013 202 {
donatien 0:632c9925f013 203 if( len <= 0 )
donatien 0:632c9925f013 204 break;
donatien 0:632c9925f013 205 if( !host.getIp().isEq(m_host.getIp()) )
donatien 0:632c9925f013 206 continue; //Not our packet
donatien 0:632c9925f013 207 if( len > 0 )
donatien 0:632c9925f013 208 break;
donatien 0:632c9925f013 209 }
donatien 0:632c9925f013 210
donatien 0:632c9925f013 211 if(len == 0)
donatien 0:632c9925f013 212 return; //Wait for the next packet
donatien 0:632c9925f013 213
donatien 0:632c9925f013 214 if(len < 0)
donatien 0:632c9925f013 215 { onResult(NTP_PRTCL); close(); return; }
donatien 0:632c9925f013 216
donatien 0:632c9925f013 217 if(len < sizeof(NTPPacket)) //TODO: Accept chunks
donatien 0:632c9925f013 218 { onResult(NTP_PRTCL); close(); return; }
donatien 0:632c9925f013 219
donatien 0:632c9925f013 220 //Hex Dump:
donatien 0:632c9925f013 221 DBG("\r\nDump Rx:\r\n");
donatien 0:632c9925f013 222 for(int i = 0; i< sizeof(NTPPacket); i++)
donatien 0:632c9925f013 223 {
donatien 2:a4f97773c90f 224 DBG("%02x ", *((char*)&m_pkt + i));
donatien 0:632c9925f013 225 }
donatien 0:632c9925f013 226 DBG("\r\n\r\n");
donatien 0:632c9925f013 227
donatien 2:a4f97773c90f 228 if( m_pkt.stratum == 0) //Kiss of death message : Not good !
donatien 0:632c9925f013 229 {
donatien 0:632c9925f013 230 onResult(NTP_PRTCL); close(); return;
donatien 0:632c9925f013 231 }
donatien 0:632c9925f013 232
donatien 0:632c9925f013 233 //Correct Endianness
donatien 2:a4f97773c90f 234 m_pkt.refTm_s = ntohl( m_pkt.refTm_s );
donatien 2:a4f97773c90f 235 m_pkt.refTm_f = ntohl( m_pkt.refTm_f );
donatien 2:a4f97773c90f 236 m_pkt.origTm_s = ntohl( m_pkt.origTm_s );
donatien 2:a4f97773c90f 237 m_pkt.origTm_f = ntohl( m_pkt.origTm_f );
donatien 2:a4f97773c90f 238 m_pkt.rxTm_s = ntohl( m_pkt.rxTm_s );
donatien 2:a4f97773c90f 239 m_pkt.rxTm_f = ntohl( m_pkt.rxTm_f );
donatien 2:a4f97773c90f 240 m_pkt.txTm_s = ntohl( m_pkt.txTm_s );
donatien 2:a4f97773c90f 241 m_pkt.txTm_f = ntohl( m_pkt.txTm_f );
donatien 0:632c9925f013 242
donatien 0:632c9925f013 243 //Compute offset, see RFC 4330 p.13
donatien 0:632c9925f013 244 uint32_t destTm_s = (NTP_TIMESTAMP_DELTA + time(NULL));
donatien 2:a4f97773c90f 245 //int32_t origTm = (int32_t) ((uint64_t) m_pkt.origTm - NTP_TIMESTAMP_DELTA); //Convert in local 32 bits timestamps
donatien 2:a4f97773c90f 246 //int32_t rxTm = (int32_t) ((uint64_t) m_pkt.rxTm - NTP_TIMESTAMP_DELTA); //Convert in local 32 bits timestamps
donatien 2:a4f97773c90f 247 //int32_t txTm = (int32_t) ((uint64_t) m_pkt.txTm - NTP_TIMESTAMP_DELTA); //Convert in local 32 bits timestamps
donatien 2:a4f97773c90f 248 // 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 249 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 250 DBG("\r\nSent @%d\r\n", m_pkt.txTm_s);
donatien 0:632c9925f013 251 DBG("\r\nOffset: %d\r\n", offset);
donatien 0:632c9925f013 252 //Set time accordingly
donatien 0:632c9925f013 253 set_time( time(NULL) + (offset /*>> 32*/) );
donatien 0:632c9925f013 254
donatien 0:632c9925f013 255 onResult(NTP_OK);
donatien 0:632c9925f013 256 close();
donatien 0:632c9925f013 257
donatien 0:632c9925f013 258 break;
donatien 0:632c9925f013 259 }
donatien 0:632c9925f013 260 }
donatien 0:632c9925f013 261
donatien 0:632c9925f013 262 void NTPClient::setTimeout(int ms)
donatien 0:632c9925f013 263 {
donatien 2:a4f97773c90f 264 m_timeout = ms;
donatien 0:632c9925f013 265 }
donatien 0:632c9925f013 266
donatien 0:632c9925f013 267 void NTPClient::resetTimeout()
donatien 0:632c9925f013 268 {
donatien 2:a4f97773c90f 269 m_watchdog.reset();
donatien 2:a4f97773c90f 270 m_watchdog.start();
donatien 0:632c9925f013 271 }
donatien 0:632c9925f013 272
donatien 0:632c9925f013 273 void NTPClient::onTimeout() //Connection has timed out
donatien 0:632c9925f013 274 {
donatien 0:632c9925f013 275 close();
donatien 0:632c9925f013 276 onResult(NTP_TIMEOUT);
donatien 0:632c9925f013 277 }
donatien 0:632c9925f013 278
donatien 0:632c9925f013 279 void NTPClient::onDNSReply(DNSReply r)
donatien 0:632c9925f013 280 {
donatien 0:632c9925f013 281 if(m_closed)
donatien 0:632c9925f013 282 {
donatien 0:632c9925f013 283 DBG("\r\nWARN: Discarded\r\n");
donatien 0:632c9925f013 284 return;
donatien 0:632c9925f013 285 }
donatien 0:632c9925f013 286
donatien 0:632c9925f013 287 if( r != DNS_FOUND )
donatien 0:632c9925f013 288 {
donatien 0:632c9925f013 289 DBG("\r\nCould not resolve hostname.\r\n");
donatien 0:632c9925f013 290 onResult(NTP_DNS);
donatien 0:632c9925f013 291 close();
donatien 0:632c9925f013 292 return;
donatien 0:632c9925f013 293 }
donatien 2:a4f97773c90f 294 DBG("\r\nDNS resolved.\r\n");
donatien 0:632c9925f013 295 m_pDnsReq->close();
donatien 0:632c9925f013 296 delete m_pDnsReq;
donatien 0:632c9925f013 297 m_pDnsReq=NULL;
donatien 2:a4f97773c90f 298 open();
donatien 0:632c9925f013 299 }
donatien 0:632c9925f013 300
donatien 0:632c9925f013 301 void NTPClient::onUDPSocketEvent(UDPSocketEvent e)
donatien 0:632c9925f013 302 {
donatien 2:a4f97773c90f 303 resetTimeout();
donatien 0:632c9925f013 304 switch(e)
donatien 0:632c9925f013 305 {
donatien 0:632c9925f013 306 case UDPSOCKET_READABLE: //The only event for now
donatien 0:632c9925f013 307 resetTimeout();
donatien 0:632c9925f013 308 process();
donatien 0:632c9925f013 309 break;
donatien 0:632c9925f013 310 }
donatien 0:632c9925f013 311 }
donatien 0:632c9925f013 312
donatien 0:632c9925f013 313 void NTPClient::onResult(NTPResult r) //Must be called by impl when the request completes
donatien 0:632c9925f013 314 {
donatien 0:632c9925f013 315 if(m_pCbItem && m_pCbMeth)
donatien 0:632c9925f013 316 (m_pCbItem->*m_pCbMeth)(r);
donatien 0:632c9925f013 317 else if(m_pCb)
donatien 0:632c9925f013 318 m_pCb(r);
donatien 0:632c9925f013 319 m_blockingResult = r; //Blocking mode
donatien 0:632c9925f013 320 }
donatien 0:632c9925f013 321
donatien 0:632c9925f013 322 NTPResult NTPClient::blockingProcess() //Called in blocking mode, calls Net::poll() until return code is available
donatien 0:632c9925f013 323 {
donatien 0:632c9925f013 324 m_blockingResult = NTP_PROCESSING;
donatien 0:632c9925f013 325 do
donatien 0:632c9925f013 326 {
donatien 0:632c9925f013 327 Net::poll();
donatien 0:632c9925f013 328 } while(m_blockingResult == NTP_PROCESSING);
donatien 0:632c9925f013 329 Net::poll(); //Necessary for cleanup
donatien 0:632c9925f013 330 return m_blockingResult;
donatien 0:632c9925f013 331 }