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