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

Committer:
RodColeman
Date:
Thu Sep 08 10:41:36 2011 +0000
Revision:
0:8f5825f330b0
setDataLen hacked to 180bytes

Who changed what in which revision?

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