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.
NTPClient.cpp@1:099750f42b02, 2018-12-07 (annotated)
- Committer:
- Alix955
- Date:
- Fri Dec 07 13:07:56 2018 +0000
- Revision:
- 1:099750f42b02
- Parent:
- 0:3c1170035e2b
- Child:
- 2:a3aec199dc7c
ntp client stuff;
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:099750f42b02 | 23 | printf("Error on DNS lookup : %d\n" , ret_gethostbyname); |
Alix955 | 0:3c1170035e2b | 24 | return ret_gethostbyname; |
Alix955 | 0:3c1170035e2b | 25 | } |
Alix955 | 0:3c1170035e2b | 26 | |
Alix955 | 0:3c1170035e2b | 27 | nist.set_port(nist_server_port); |
Alix955 | 0:3c1170035e2b | 28 | |
Alix955 | 0:3c1170035e2b | 29 | memset(ntp_send_values, 0x00, sizeof(ntp_send_values)); |
Alix955 | 0:3c1170035e2b | 30 | ntp_send_values[0] = '\x1b'; |
Alix955 | 0:3c1170035e2b | 31 | |
Alix955 | 0:3c1170035e2b | 32 | memset(ntp_recv_values, 0x00, sizeof(ntp_recv_values)); |
Alix955 | 0:3c1170035e2b | 33 | |
Alix955 | 0:3c1170035e2b | 34 | UDPSocket sock; |
Alix955 | 0:3c1170035e2b | 35 | sock.open(iface); |
Alix955 | 0:3c1170035e2b | 36 | sock.set_timeout(timeout); |
Alix955 | 0:3c1170035e2b | 37 | |
Alix955 | 0:3c1170035e2b | 38 | sock.sendto(nist, (void*)ntp_send_values, sizeof(ntp_send_values)); |
Alix955 | 0:3c1170035e2b | 39 | |
Alix955 | 0:3c1170035e2b | 40 | SocketAddress source; |
Alix955 | 0:3c1170035e2b | 41 | const int n = sock.recvfrom(&source, (void*)ntp_recv_values, sizeof(ntp_recv_values)); |
Alix955 | 0:3c1170035e2b | 42 | |
Alix955 | 0:3c1170035e2b | 43 | if (n > 10) { |
Alix955 | 0:3c1170035e2b | 44 | return ntohl(ntp_recv_values[10]) - TIME1970; |
Alix955 | 0:3c1170035e2b | 45 | } else { |
Alix955 | 0:3c1170035e2b | 46 | if (n < 0) { |
Alix955 | 0:3c1170035e2b | 47 | // Network error |
Alix955 | 1:099750f42b02 | 48 | printf("Network error: %d\n", n); |
Alix955 | 0:3c1170035e2b | 49 | return n; |
Alix955 | 0:3c1170035e2b | 50 | } else { |
Alix955 | 0:3c1170035e2b | 51 | // No or partial data returned |
Alix955 | 1:099750f42b02 | 52 | printf("No or partial data returned: -1"); |
Alix955 | 0:3c1170035e2b | 53 | return -1; |
Alix955 | 0:3c1170035e2b | 54 | } |
Alix955 | 0:3c1170035e2b | 55 | } |
Alix955 | 0:3c1170035e2b | 56 | } |
Alix955 | 0:3c1170035e2b | 57 | |
Alix955 | 0:3c1170035e2b | 58 | uint32_t NTPClient::ntohl(uint32_t x) { |
Alix955 | 0:3c1170035e2b | 59 | uint32_t ret = (x & 0xff) << 24; |
Alix955 | 0:3c1170035e2b | 60 | ret |= (x & 0xff00) << 8; |
Alix955 | 0:3c1170035e2b | 61 | ret |= (x & 0xff0000UL) >> 8; |
Alix955 | 0:3c1170035e2b | 62 | ret |= (x & 0xff000000UL) >> 24; |
Alix955 | 0:3c1170035e2b | 63 | return ret; |
Alix955 | 0:3c1170035e2b | 64 | } |