OS5.15 complaint NTP library
Dependents: Firebase-Example TCP-NTP-Server TCP-NTP-Server-OS5depreciated
NTPclient.h@1:a97d143dd94e, 2020-06-01 (annotated)
- Committer:
- star297
- Date:
- Mon Jun 01 16:16:42 2020 +0000
- Revision:
- 1:a97d143dd94e
- Parent:
- 0:3854dc7bd4de
added DST
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
star297 | 0:3854dc7bd4de | 1 | |
star297 | 0:3854dc7bd4de | 2 | #include "mbed.h" |
star297 | 0:3854dc7bd4de | 3 | |
star297 | 0:3854dc7bd4de | 4 | #ifndef NTPCLIENT_H |
star297 | 0:3854dc7bd4de | 5 | #define NTPCLIENT_H |
star297 | 0:3854dc7bd4de | 6 | |
star297 | 0:3854dc7bd4de | 7 | #include <stdint.h> |
star297 | 0:3854dc7bd4de | 8 | #include "UDPSocket.h" |
star297 | 0:3854dc7bd4de | 9 | #include "NetworkInterface.h" |
star297 | 0:3854dc7bd4de | 10 | |
star297 | 0:3854dc7bd4de | 11 | #define NTP_DEFAULT_PORT 123 |
star297 | 0:3854dc7bd4de | 12 | #define NTP_DEFAULT_TIMEOUT 4000 |
star297 | 0:3854dc7bd4de | 13 | #define NTP_DEFAULT_TIMEZONE_OFFSET 0 |
star297 | 0:3854dc7bd4de | 14 | |
star297 | 0:3854dc7bd4de | 15 | class NTPclient |
star297 | 0:3854dc7bd4de | 16 | { |
star297 | 0:3854dc7bd4de | 17 | public: |
star297 | 0:3854dc7bd4de | 18 | NTPclient(NetworkInterface & _m_intf); |
star297 | 1:a97d143dd94e | 19 | // Get current time in seconds (blocking) |
star297 | 0:3854dc7bd4de | 20 | // Update the time using the server host |
star297 | 0:3854dc7bd4de | 21 | // Blocks until completion |
star297 | 0:3854dc7bd4de | 22 | // NTPpool, NTP server IPv4 address or hostname (will be resolved via DNS) |
star297 | 0:3854dc7bd4de | 23 | // tzoffset, offset in seconds (3600 add 1 hour, -3600 subtract 1 hour) |
star297 | 0:3854dc7bd4de | 24 | // dst, adjust for DST 1= enabled, 0=dissabled |
star297 | 0:3854dc7bd4de | 25 | // setRTC, set system RTC 1= enabled, 0=dissabled |
star297 | 0:3854dc7bd4de | 26 | |
star297 | 0:3854dc7bd4de | 27 | uint32_t getNTP(const char* NTPpool, uint32_t tzoffset, bool dst, bool setRTC); |
star297 | 0:3854dc7bd4de | 28 | |
star297 | 0:3854dc7bd4de | 29 | private: |
star297 | 0:3854dc7bd4de | 30 | struct NTPPacket |
star297 | 0:3854dc7bd4de | 31 | { |
star297 | 0:3854dc7bd4de | 32 | //We are in LE Network is BE |
star297 | 0:3854dc7bd4de | 33 | //LSb first |
star297 | 0:3854dc7bd4de | 34 | unsigned mode : 3; |
star297 | 0:3854dc7bd4de | 35 | unsigned vn : 3; |
star297 | 0:3854dc7bd4de | 36 | unsigned li : 2; |
star297 | 0:3854dc7bd4de | 37 | |
star297 | 0:3854dc7bd4de | 38 | uint8_t stratum; |
star297 | 0:3854dc7bd4de | 39 | uint8_t poll; |
star297 | 0:3854dc7bd4de | 40 | uint8_t precision; |
star297 | 0:3854dc7bd4de | 41 | |
star297 | 0:3854dc7bd4de | 42 | //32 bits header |
star297 | 0:3854dc7bd4de | 43 | uint32_t rootDelay; |
star297 | 0:3854dc7bd4de | 44 | uint32_t rootDispersion; |
star297 | 0:3854dc7bd4de | 45 | uint32_t refId; |
star297 | 0:3854dc7bd4de | 46 | uint32_t refTm_s; |
star297 | 0:3854dc7bd4de | 47 | uint32_t refTm_f; |
star297 | 0:3854dc7bd4de | 48 | uint32_t origTm_s; |
star297 | 0:3854dc7bd4de | 49 | uint32_t origTm_f; |
star297 | 0:3854dc7bd4de | 50 | uint32_t rxTm_s; |
star297 | 0:3854dc7bd4de | 51 | uint32_t rxTm_f; |
star297 | 0:3854dc7bd4de | 52 | uint32_t txTm_s; |
star297 | 0:3854dc7bd4de | 53 | uint32_t txTm_f; |
star297 | 0:3854dc7bd4de | 54 | } __attribute__ ((packed)); |
star297 | 0:3854dc7bd4de | 55 | |
star297 | 0:3854dc7bd4de | 56 | NetworkInterface & m_intf; // WiFi interface |
star297 | 0:3854dc7bd4de | 57 | UDPSocket m_sock; |
star297 | 0:3854dc7bd4de | 58 | }; |
star297 | 0:3854dc7bd4de | 59 | #endif /* NTPCLIENT_H_ */ |