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