SNMP agent attached to SPI slave

Dependencies:   mbed

Committer:
lorcansmith
Date:
Mon Aug 13 15:07:40 2012 +0000
Revision:
0:2a53a4c3238c
v1.1 release includes ioAlarm traps

Who changed what in which revision?

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