A port and bug fix of Alix955/code/ntp-client. The socket would hang as it was defined both with a timeout and as blocking.

Committer:
Alix955
Date:
Tue Dec 04 17:36:03 2018 +0000
Revision:
0:3c1170035e2b
Child:
1:099750f42b02
ntp client stuff;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Alix955 0:3c1170035e2b 1 #include "ntp-client/NTPClient.h"
Alix955 0:3c1170035e2b 2 #include "mbed.h"
Alix955 0:3c1170035e2b 3
Alix955 0:3c1170035e2b 4 NTPClient::NTPClient(NetworkInterface *iface)
Alix955 0:3c1170035e2b 5 : iface(iface), nist_server_address((char *)NTP_DEFULT_NIST_SERVER_ADDRESS), nist_server_port(NTP_DEFULT_NIST_SERVER_PORT) {
Alix955 0:3c1170035e2b 6 }
Alix955 0:3c1170035e2b 7
Alix955 0:3c1170035e2b 8 void NTPClient::set_server(char* server, int port){
Alix955 0:3c1170035e2b 9 nist_server_address = server;
Alix955 0:3c1170035e2b 10 nist_server_port = port;
Alix955 0:3c1170035e2b 11 }
Alix955 0:3c1170035e2b 12
Alix955 0:3c1170035e2b 13 time_t NTPClient::get_timestamp(int timeout) {
Alix955 0:3c1170035e2b 14 const time_t TIME1970 = (time_t)2208988800UL;
Alix955 0:3c1170035e2b 15 int ntp_send_values[12] = {0};
Alix955 0:3c1170035e2b 16 int ntp_recv_values[12] = {0};
Alix955 0:3c1170035e2b 17
Alix955 0:3c1170035e2b 18 SocketAddress nist;
Alix955 0:3c1170035e2b 19 int ret_gethostbyname = iface->gethostbyname(nist_server_address, &nist);
Alix955 0:3c1170035e2b 20
Alix955 0:3c1170035e2b 21 if (ret_gethostbyname < 0) {
Alix955 0:3c1170035e2b 22 // Network error on DNS lookup
Alix955 0:3c1170035e2b 23 return ret_gethostbyname;
Alix955 0:3c1170035e2b 24 }
Alix955 0:3c1170035e2b 25
Alix955 0:3c1170035e2b 26 nist.set_port(nist_server_port);
Alix955 0:3c1170035e2b 27
Alix955 0:3c1170035e2b 28 memset(ntp_send_values, 0x00, sizeof(ntp_send_values));
Alix955 0:3c1170035e2b 29 ntp_send_values[0] = '\x1b';
Alix955 0:3c1170035e2b 30
Alix955 0:3c1170035e2b 31 memset(ntp_recv_values, 0x00, sizeof(ntp_recv_values));
Alix955 0:3c1170035e2b 32
Alix955 0:3c1170035e2b 33 UDPSocket sock;
Alix955 0:3c1170035e2b 34 sock.open(iface);
Alix955 0:3c1170035e2b 35 sock.set_timeout(timeout);
Alix955 0:3c1170035e2b 36
Alix955 0:3c1170035e2b 37 sock.sendto(nist, (void*)ntp_send_values, sizeof(ntp_send_values));
Alix955 0:3c1170035e2b 38
Alix955 0:3c1170035e2b 39 SocketAddress source;
Alix955 0:3c1170035e2b 40 const int n = sock.recvfrom(&source, (void*)ntp_recv_values, sizeof(ntp_recv_values));
Alix955 0:3c1170035e2b 41
Alix955 0:3c1170035e2b 42 if (n > 10) {
Alix955 0:3c1170035e2b 43 return ntohl(ntp_recv_values[10]) - TIME1970;
Alix955 0:3c1170035e2b 44 } else {
Alix955 0:3c1170035e2b 45 if (n < 0) {
Alix955 0:3c1170035e2b 46 // Network error
Alix955 0:3c1170035e2b 47 return n;
Alix955 0:3c1170035e2b 48 } else {
Alix955 0:3c1170035e2b 49 // No or partial data returned
Alix955 0:3c1170035e2b 50 return -1;
Alix955 0:3c1170035e2b 51 }
Alix955 0:3c1170035e2b 52 }
Alix955 0:3c1170035e2b 53 }
Alix955 0:3c1170035e2b 54
Alix955 0:3c1170035e2b 55 uint32_t NTPClient::ntohl(uint32_t x) {
Alix955 0:3c1170035e2b 56 uint32_t ret = (x & 0xff) << 24;
Alix955 0:3c1170035e2b 57 ret |= (x & 0xff00) << 8;
Alix955 0:3c1170035e2b 58 ret |= (x & 0xff0000UL) >> 8;
Alix955 0:3c1170035e2b 59 ret |= (x & 0xff000000UL) >> 24;
Alix955 0:3c1170035e2b 60 return ret;
Alix955 0:3c1170035e2b 61 }