Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:48:09 2011 +0000
Revision:
0:850eacf3e945
revised fixed length to 178 bytes

Who changed what in which revision?

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