diff --git a/NTPClient.cpp b/NTPClient.cpp index 1f62f7f..127d412 100644 --- a/NTPClient.cpp +++ b/NTPClient.cpp @@ -26,7 +26,7 @@ void NTPClient::set_server(const char* server, int port) { nist_server_port = port; } -time_t NTPClient::get_timestamp(int timeout) { +int NTPClient::get_timestamp(time_t ×tamp, int timeout) { const time_t TIME1970 = (time_t)2208988800UL; int ntp_send_values[12] = {0}; int ntp_recv_values[12] = {0}; @@ -58,8 +58,8 @@ time_t NTPClient::get_timestamp(int timeout) { const int n = sock.recvfrom(&source, (void*)ntp_recv_values, sizeof(ntp_recv_values)); if (n > 10) { - return ntohl(ntp_recv_values[10]) - TIME1970; - + timestamp = ntohl(ntp_recv_values[10]) - TIME1970; + return 0; } else { if (n < 0) { // Network error @@ -77,6 +77,18 @@ time_t NTPClient::get_timestamp(int timeout) { } } +time_t NTPClient::get_timestamp(int timeout) { + time_t timestamp; + int ret = get_timestamp(timestamp, timeout); + if (ret < 0) { + // This doesn't work with the ARM toolchain whose time_t is unsigned + // Please use the new API get_timestamp(time_t *timestamp, int timeout) + return ret; + } else { + return timestamp; + } +} + void NTPClient::network(NetworkInterface *interface) { iface = interface; } diff --git a/NTPClient.h b/NTPClient.h index 8f755f5..84741ca 100644 --- a/NTPClient.h +++ b/NTPClient.h @@ -15,6 +15,7 @@ */ #include "mbed.h" +#include "platform/mbed_toolchain.h" #define NTP_DEFULT_NIST_SERVER_ADDRESS "2.pool.ntp.org" #define NTP_DEFULT_NIST_SERVER_PORT 123 @@ -22,8 +23,16 @@ class NTPClient { public: explicit NTPClient(NetworkInterface *interface = NULL); + void set_server(const char* server, int port); + + int get_timestamp(time_t ×tamp, int timeout = 15000); + + MBED_DEPRECATED( + "This cannot return negative error codes with ARM toolchain's unsigned time_t. " + "Please use int get_timestamp(time_t ×tamp, int timeout) instead") time_t get_timestamp(int timeout = 15000); + void network(NetworkInterface *interface); private: