Shareef Jalloq / ntp-client

Files at this revision

API Documentation at this revision

Comitter:
Alix955
Date:
Tue Dec 04 17:36:03 2018 +0000
Child:
1:099750f42b02
Commit message:
ntp client stuff;

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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NTPClient.cpp	Tue Dec 04 17:36:03 2018 +0000
@@ -0,0 +1,61 @@
+#include "ntp-client/NTPClient.h"
+#include "mbed.h"
+
+NTPClient::NTPClient(NetworkInterface *iface)
+: iface(iface), nist_server_address((char *)NTP_DEFULT_NIST_SERVER_ADDRESS), nist_server_port(NTP_DEFULT_NIST_SERVER_PORT) {
+}
+
+void NTPClient::set_server(char* server, int port){
+    nist_server_address = server;
+    nist_server_port = port;
+}
+
+time_t NTPClient::get_timestamp(int timeout) {
+    const time_t TIME1970 = (time_t)2208988800UL;
+    int ntp_send_values[12] = {0};
+    int ntp_recv_values[12] = {0};
+
+    SocketAddress nist;
+    int ret_gethostbyname = iface->gethostbyname(nist_server_address, &nist);
+
+    if (ret_gethostbyname < 0) {
+        // Network error on DNS lookup
+        return ret_gethostbyname;
+    }
+
+    nist.set_port(nist_server_port);
+
+    memset(ntp_send_values, 0x00, sizeof(ntp_send_values));
+    ntp_send_values[0] = '\x1b';
+
+    memset(ntp_recv_values, 0x00, sizeof(ntp_recv_values));
+
+    UDPSocket sock;
+    sock.open(iface);
+    sock.set_timeout(timeout);
+
+    sock.sendto(nist, (void*)ntp_send_values, sizeof(ntp_send_values));
+
+    SocketAddress source;
+    const int n = sock.recvfrom(&source, (void*)ntp_recv_values, sizeof(ntp_recv_values));
+
+    if (n > 10) {
+        return ntohl(ntp_recv_values[10]) - TIME1970;
+    } else {
+        if (n < 0) {
+            // Network error
+            return n;
+        } else {
+            // No or partial data returned
+            return -1;
+        }
+    }
+}
+
+uint32_t NTPClient::ntohl(uint32_t x) {
+    uint32_t ret = (x & 0xff) << 24;
+    ret |= (x & 0xff00) << 8;
+    ret |= (x & 0xff0000UL) >> 8;
+    ret |= (x & 0xff000000UL) >> 24;
+    return ret;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NTPClient.h	Tue Dec 04 17:36:03 2018 +0000
@@ -0,0 +1,19 @@
+#include "mbed.h"
+
+#define NTP_DEFULT_NIST_SERVER_ADDRESS "2.pool.ntp.org"
+//#define NTP_DEFULT_NIST_SERVER_ADDRESS "0.europe.pool.ntp.org"
+#define NTP_DEFULT_NIST_SERVER_PORT 123
+
+class NTPClient {
+    public:
+        NTPClient(NetworkInterface *iface);
+        void set_server(char* server, int port);
+        time_t get_timestamp(int timeout = 15000);
+
+    private:
+        NetworkInterface *iface;
+        char* nist_server_address;
+        int nist_server_port;
+
+        uint32_t ntohl(uint32_t num);
+};
\ No newline at end of file