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.h@2:a3aec199dc7c, 2019-05-26 (annotated)
- Committer:
- sjalloq
- Date:
- Sun May 26 08:46:35 2019 +0100
- Revision:
- 2:a3aec199dc7c
- Parent:
- 1:099750f42b02
ntp-client : fork from Alix955/code/ntp-client
- sockets should not be setup as blocking if you want to use a timeout. This
resulted in the socket hanging when packets went missing.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Alix955 | 1:099750f42b02 | 1 | #ifndef _NTPCLIENT_ |
Alix955 | 1:099750f42b02 | 2 | #define _NTPCLIENT_ |
Alix955 | 0:3c1170035e2b | 3 | |
Alix955 | 1:099750f42b02 | 4 | #include "mbed.h" |
Alix955 | 1:099750f42b02 | 5 | //use nslookup 2.pool.ntp.org in cmd to find ip address |
Alix955 | 1:099750f42b02 | 6 | |
sjalloq | 2:a3aec199dc7c | 7 | #define NTP_DEFULT_NIST_SERVER_ADDRESS "0.pool.ntp.org" |
Alix955 | 0:3c1170035e2b | 8 | #define NTP_DEFULT_NIST_SERVER_PORT 123 |
Alix955 | 0:3c1170035e2b | 9 | |
Alix955 | 0:3c1170035e2b | 10 | class NTPClient { |
Alix955 | 0:3c1170035e2b | 11 | public: |
Alix955 | 0:3c1170035e2b | 12 | NTPClient(NetworkInterface *iface); |
Alix955 | 0:3c1170035e2b | 13 | void set_server(char* server, int port); |
Alix955 | 1:099750f42b02 | 14 | time_t get_timestamp(int timeout = 10000); |
Alix955 | 0:3c1170035e2b | 15 | |
Alix955 | 0:3c1170035e2b | 16 | private: |
Alix955 | 0:3c1170035e2b | 17 | NetworkInterface *iface; |
Alix955 | 0:3c1170035e2b | 18 | char* nist_server_address; |
Alix955 | 0:3c1170035e2b | 19 | int nist_server_port; |
Alix955 | 0:3c1170035e2b | 20 | |
Alix955 | 0:3c1170035e2b | 21 | uint32_t ntohl(uint32_t num); |
Alix955 | 1:099750f42b02 | 22 | }; |
Alix955 | 1:099750f42b02 | 23 | |
Alix955 | 1:099750f42b02 | 24 | #endif |