Forked to make it work with mbed-os 5.3

Dependents:   NTPClient_example mbed_vfd_ntp

Fork of NTPClient by Donatien Garnier

Files at this revision

API Documentation at this revision

Comitter:
lmussier
Date:
Fri Jan 06 13:45:37 2017 +0000
Parent:
4:881559865a93
Commit message:
Adapt for mbed-os 5.3

Changed in this revision

NTPClient.cpp Show annotated file Show diff for this revision Revisions of this file
NTPClient.h Show annotated file Show diff for this revision Revisions of this file
--- a/NTPClient.cpp	Sun Aug 05 16:10:57 2012 +0000
+++ b/NTPClient.cpp	Fri Jan 06 13:45:37 2017 +0000
@@ -18,7 +18,7 @@
  */
 
 //Debug is disabled by default
-#if 0
+#if 1
 //Enable debug
 #define __DEBUG__
 #include <cstdio>
@@ -39,15 +39,12 @@
 #include "UDPSocket.h"
 
 #include "mbed.h" //time() and set_time()
-
+#include "def.h"
 #define NTP_PORT 123
 #define NTP_CLIENT_PORT 0 //Random port
 #define NTP_TIMESTAMP_DELTA 2208988800ull //Diff btw a UNIX timestamp (Starting Jan, 1st 1970) and a NTP timestamp (Starting Jan, 1st 1900)
 
-NTPClient::NTPClient() : m_sock()
-{
-
-
+NTPClient::NTPClient(NetworkInterface& p_networkInterface) : m_sock(), m_net(p_networkInterface) {
 }
 
 NTPResult NTPClient::setTime(const char* host, uint16_t port, uint32_t timeout)
@@ -58,11 +55,11 @@
   DBG("Time is set to (UTC): %s", ctime(&ctTime));
 #endif
 
+  if( m_sock.open(&m_net) != 0){ DBG("Open"); return NTP_CONN;}
+
   //Create & bind socket
   DBG("Binding socket");
-  m_sock.bind(0); //Bind to a random port
-  
-  m_sock.set_blocking(false, timeout); //Set not blocking
+  if( m_sock.bind(99) != 0){ DBG("Bind"); return NTP_CONN;}
 
   struct NTPPacket pkt;
 
@@ -87,37 +84,31 @@
 
   pkt.refTm_f = pkt.origTm_f = pkt.rxTm_f = pkt.txTm_f = 0;
 
-  Endpoint outEndpoint;
-  
-  if( outEndpoint.set_address(host, port) < 0)
-  {
-    m_sock.close();
-    return NTP_DNS;
-  }
-  
+  SocketAddress outEndpoint (host, port);
+    
   //Set timeout, non-blocking and wait using select
-  int ret = m_sock.sendTo( outEndpoint, (char*)&pkt, sizeof(NTPPacket) );
+  int ret = m_sock.sendto(host, port, (char*)&pkt, sizeof(NTPPacket) );
   if (ret < 0 )
   {
-    ERR("Could not send packet");
+    ERR("Could not send packet (%d)", ret);
     m_sock.close();
     return NTP_CONN;
   }
 
   //Read response
-  Endpoint inEndpoint;
+  SocketAddress inEndpoint;
 
   DBG("Pong");
-  do
-  {
-    ret = m_sock.receiveFrom( inEndpoint, (char*)&pkt, sizeof(NTPPacket) ); //FIXME need a DNS Resolver to actually compare the incoming address with the DNS name
+  //do
+  //{
+    ret = m_sock.recvfrom( &inEndpoint, (char*)&pkt, sizeof(NTPPacket) ); //FIXME need a DNS Resolver to actually compare the incoming address with the DNS name
     if(ret < 0)
     {
-      ERR("Could not receive packet");
+      ERR("Could not receive packet (%d)", ret);
       m_sock.close();
       return NTP_CONN;
     }
-  } while( strcmp(outEndpoint.get_address(), inEndpoint.get_address()) != 0 );
+  //} while( strcmp(outEndpoint.get_ip_address(), inEndpoint.get_ip_address()) != 0 );
 
   if(ret < sizeof(NTPPacket)) //TODO: Accept chunks
   {
--- a/NTPClient.h	Sun Aug 05 16:10:57 2012 +0000
+++ b/NTPClient.h	Fri Jan 06 13:45:37 2017 +0000
@@ -30,6 +30,7 @@
 using std::uint16_t;
 using std::uint32_t;
 
+#include "NetworkStack.h"
 #include "UDPSocket.h"
 
 #define NTP_DEFAULT_PORT 123
@@ -54,7 +55,7 @@
   /**
   Instantiate the NTP client
   */
-  NTPClient();
+  NTPClient(NetworkInterface& p_networkInterface);
 
   /**Get current time (blocking)
   Update the time using the server host
@@ -95,7 +96,7 @@
   } __attribute__ ((packed));
   
   UDPSocket m_sock;
-
+  NetworkInterface& m_net;
 };