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