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