![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
First step: AutoIP compiled in and working
Diff: services/ntp/NTPClient.cpp
- Revision:
- 1:4218cacaf696
- Parent:
- 0:55a05330f8cc
diff -r 55a05330f8cc -r 4218cacaf696 services/ntp/NTPClient.cpp --- a/services/ntp/NTPClient.cpp Fri Jun 18 09:11:35 2010 +0000 +++ b/services/ntp/NTPClient.cpp Fri Jun 18 15:54:21 2010 +0000 @@ -29,7 +29,7 @@ #include "dbg/dbg.h" #define NTP_PORT 123 -#define NTP_CLIENT_PORT 50420 +#define NTP_CLIENT_PORT 0//50420 //Random port #define NTP_REQUEST_TIMEOUT 15000 #define NTP_TIMESTAMP_DELTA 2208988800ull //Diff btw a UNIX timestamp (Starting Jan, 1st 1970) and a NTP timestamp (Starting Jan, 1st 1900) @@ -42,7 +42,7 @@ | (( x >> 24 ) & 0x000000FF) ) #define ntohl( x ) (htonl(x)) -NTPClient::NTPClient() : m_state(NTP_PING), m_pCbItem(NULL), m_pCbMeth(NULL), m_pCb(NULL), +NTPClient::NTPClient() : NetService(false), m_state(NTP_PING), m_pCbItem(NULL), m_pCbMeth(NULL), m_pCb(NULL), m_watchdog(), m_timeout(0), m_closed(true), m_host(), m_pDnsReq(NULL), m_blockingResult(NTP_PROCESSING) { setTimeout(NTP_REQUEST_TIMEOUT); @@ -68,7 +68,7 @@ return NTP_PROCESSING; } -#ifdef __LINKER_BUG_SOLVED__ +#if 0 //For doc only template<class T> NTPResult NTPClient::setTime(const Host& host, T* pItem, void (T::*pMethod)(NTPResult)) //Non blocking { @@ -81,7 +81,12 @@ void NTPClient::doSetTime(const Host& host) { init(); + resetTimeout(); m_host = host; + if(!m_host.getPort()) + { + m_host.setPort(NTP_PORT); + } if(m_host.getIp().isNull()) { //DNS query required @@ -91,16 +96,7 @@ m_pDnsReq->resolve(&m_host); return; } - if(!m_host.getPort()) - { - m_host.setPort(NTP_PORT); - } - - m_state = NTP_PING; - Host localhost(IpAddr(127,0,0,1), NTP_CLIENT_PORT, "localhost"); - m_pUDPSocket->bind(localhost); - set_time( 1280000000 ); //End of July 2010... just there to limit offset range - process(); + open(); } void NTPClient::setOnResult( void (*pMethod)(NTPResult) ) @@ -108,22 +104,12 @@ m_pCb = pMethod; } -void NTPClient::init() //Create and setup socket if needed -{ - if(!m_closed) //Already opened - return; - m_state = NTP_PING; - m_pUDPSocket = new UDPSocket; - m_pUDPSocket->setOnEvent(this, &NTPClient::onUDPSocketEvent); - m_closed = false; -} - void NTPClient::close() { if(m_closed) return; m_closed = true; //Prevent recursive calling or calling on an object being destructed by someone else - m_watchdog.detach(); + m_watchdog.stop(); m_pUDPSocket->resetOnEvent(); m_pUDPSocket->close(); delete m_pUDPSocket; @@ -135,45 +121,74 @@ } } +void NTPClient::poll() //Called by NetServices +{ + if( (!m_closed) && (m_watchdog.read_ms() >= m_timeout) ) + { + onTimeout(); + } +} + +void NTPClient::init() //Create and setup socket if needed +{ + if(!m_closed) //Already opened + return; + m_state = NTP_PING; + m_pUDPSocket = new UDPSocket; + m_pUDPSocket->setOnEvent(this, &NTPClient::onUDPSocketEvent); + m_closed = false; + DBG("NTPClient: Init OK\n"); +} + +void NTPClient::open() +{ + resetTimeout(); + DBG("Opening connection\n"); + m_state = NTP_PING; + Host localhost(IpAddr(127,0,0,1), NTP_CLIENT_PORT, "localhost"); + m_pUDPSocket->bind(localhost); + set_time( 1280000000 ); //End of July 2010... just there to limit offset range + process(); +} + #define MIN(a,b) ((a)<(b))?(a):(b) void NTPClient::process() //Main state-machine { - NTPPacket pkt; int len; Host host; - + switch(m_state) { case NTP_PING: DBG("\r\nPing\r\n"); //Prepare NTP Packet: - pkt.li = 0; //Leap Indicator : No warning - pkt.vn = 4; //Version Number : 4 - pkt.mode = 3; //Client mode - pkt.stratum = 0; //Not relevant here - pkt.poll = 0; //Not significant as well - pkt.precision = 0; //Neither this one is + m_pkt.li = 0; //Leap Indicator : No warning + m_pkt.vn = 4; //Version Number : 4 + m_pkt.mode = 3; //Client mode + m_pkt.stratum = 0; //Not relevant here + m_pkt.poll = 0; //Not significant as well + m_pkt.precision = 0; //Neither this one is - pkt.rootDelay = 0; //Or this one - pkt.rootDispersion = 0; //Or that one - pkt.refId = 0; //... + m_pkt.rootDelay = 0; //Or this one + m_pkt.rootDispersion = 0; //Or that one + m_pkt.refId = 0; //... - pkt.refTm_s = 0; - pkt.origTm_s = 0; - pkt.rxTm_s = 0; - pkt.txTm_s = htonl( NTP_TIMESTAMP_DELTA + time(NULL) ); //WARN: We are in LE format, network byte order is BE + m_pkt.refTm_s = 0; + m_pkt.origTm_s = 0; + m_pkt.rxTm_s = 0; + m_pkt.txTm_s = htonl( NTP_TIMESTAMP_DELTA + time(NULL) ); //WARN: We are in LE format, network byte order is BE - pkt.refTm_f = pkt.origTm_f = pkt.rxTm_f = pkt.txTm_f = 0; + m_pkt.refTm_f = m_pkt.origTm_f = m_pkt.rxTm_f = m_pkt.txTm_f = 0; //Hex Dump: DBG("\r\nDump Tx:\r\n"); for(int i = 0; i< sizeof(NTPPacket); i++) { - DBG("%02x ", *((char*)&pkt + i)); + DBG("%02x ", *((char*)&m_pkt + i)); } DBG("\r\n\r\n"); - len = m_pUDPSocket->sendto( (char*)&pkt, sizeof(NTPPacket), &m_host ); + len = m_pUDPSocket->sendto( (char*)&m_pkt, sizeof(NTPPacket), &m_host ); if(len < sizeof(NTPPacket)) { onResult(NTP_PRTCL); close(); return; } @@ -183,12 +198,11 @@ case NTP_PONG: DBG("\r\nPong\r\n"); - while( len = m_pUDPSocket->recvfrom( (char*)&pkt, sizeof(NTPPacket), &host ) ) + while( len = m_pUDPSocket->recvfrom( (char*)&m_pkt, sizeof(NTPPacket), &host ) ) { if( len <= 0 ) break; if( !host.getIp().isEq(m_host.getIp()) ) - //if( !ip_addr_cmp( &host.getIp().getStruct(), &m_host.getIp().getStruct() ) ) //Was working like that, trying nicer impl above continue; //Not our packet if( len > 0 ) break; @@ -207,33 +221,33 @@ DBG("\r\nDump Rx:\r\n"); for(int i = 0; i< sizeof(NTPPacket); i++) { - DBG("%02x ", *((char*)&pkt + i)); + DBG("%02x ", *((char*)&m_pkt + i)); } DBG("\r\n\r\n"); - if( pkt.stratum == 0) //Kiss of death message : Not good ! + if( m_pkt.stratum == 0) //Kiss of death message : Not good ! { onResult(NTP_PRTCL); close(); return; } //Correct Endianness - pkt.refTm_s = ntohl( pkt.refTm_s ); - pkt.refTm_f = ntohl( pkt.refTm_f ); - pkt.origTm_s = ntohl( pkt.origTm_s ); - pkt.origTm_f = ntohl( pkt.origTm_f ); - pkt.rxTm_s = ntohl( pkt.rxTm_s ); - pkt.rxTm_f = ntohl( pkt.rxTm_f ); - pkt.txTm_s = ntohl( pkt.txTm_s ); - pkt.txTm_f = ntohl( pkt.txTm_f ); + m_pkt.refTm_s = ntohl( m_pkt.refTm_s ); + m_pkt.refTm_f = ntohl( m_pkt.refTm_f ); + m_pkt.origTm_s = ntohl( m_pkt.origTm_s ); + m_pkt.origTm_f = ntohl( m_pkt.origTm_f ); + m_pkt.rxTm_s = ntohl( m_pkt.rxTm_s ); + m_pkt.rxTm_f = ntohl( m_pkt.rxTm_f ); + m_pkt.txTm_s = ntohl( m_pkt.txTm_s ); + m_pkt.txTm_f = ntohl( m_pkt.txTm_f ); //Compute offset, see RFC 4330 p.13 uint32_t destTm_s = (NTP_TIMESTAMP_DELTA + time(NULL)); - //int32_t origTm = (int32_t) ((uint64_t) pkt.origTm - NTP_TIMESTAMP_DELTA); //Convert in local 32 bits timestamps - //int32_t rxTm = (int32_t) ((uint64_t) pkt.rxTm - NTP_TIMESTAMP_DELTA); //Convert in local 32 bits timestamps - //int32_t txTm = (int32_t) ((uint64_t) pkt.txTm - NTP_TIMESTAMP_DELTA); //Convert in local 32 bits timestamps - // int64_t offset = ( ( ( pkt.rxTm_s - pkt.origTm_s ) + ( pkt.txTm_s - destTm_s ) ) << 32 + ( ( pkt.rxTm_f - pkt.origTm_f ) + ( pkt.txTm_f - 0 ) ) ) / 2; - int64_t offset = ( (int64_t)( pkt.rxTm_s - pkt.origTm_s ) + (int64_t) ( pkt.txTm_s - destTm_s ) ) / 2; //Avoid overflow - DBG("\r\nSent @%d\r\n", pkt.txTm_s); + //int32_t origTm = (int32_t) ((uint64_t) m_pkt.origTm - NTP_TIMESTAMP_DELTA); //Convert in local 32 bits timestamps + //int32_t rxTm = (int32_t) ((uint64_t) m_pkt.rxTm - NTP_TIMESTAMP_DELTA); //Convert in local 32 bits timestamps + //int32_t txTm = (int32_t) ((uint64_t) m_pkt.txTm - NTP_TIMESTAMP_DELTA); //Convert in local 32 bits timestamps + // 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; + int64_t offset = ( (int64_t)( m_pkt.rxTm_s - m_pkt.origTm_s ) + (int64_t) ( m_pkt.txTm_s - destTm_s ) ) / 2; //Avoid overflow + DBG("\r\nSent @%d\r\n", m_pkt.txTm_s); DBG("\r\nOffset: %d\r\n", offset); //Set time accordingly set_time( time(NULL) + (offset /*>> 32*/) ); @@ -247,14 +261,13 @@ void NTPClient::setTimeout(int ms) { - m_timeout = 1000*ms; - resetTimeout(); + m_timeout = ms; } void NTPClient::resetTimeout() { - m_watchdog.detach(); - m_watchdog.attach_us<NTPClient>(this, &NTPClient::onTimeout, m_timeout); + m_watchdog.reset(); + m_watchdog.start(); } void NTPClient::onTimeout() //Connection has timed out @@ -278,14 +291,16 @@ close(); return; } + DBG("\r\nDNS resolved.\r\n"); m_pDnsReq->close(); delete m_pDnsReq; m_pDnsReq=NULL; - doSetTime(m_host); + open(); } void NTPClient::onUDPSocketEvent(UDPSocketEvent e) { + resetTimeout(); switch(e) { case UDPSOCKET_READABLE: //The only event for now