Sming Framework API
Sming - Open Source framework for high efficiency WiFi SoC ESP8266 native development with C++ language.
NtpClient.h
1 
6 #ifndef APP_NTPCLIENT_H_
7 #define APP_NTPCLIENT_H_
8 
9 #include "UdpConnection.h"
10 #include "../Platform/System.h"
11 #include "../Timer.h"
12 #include "../SystemClock.h"
13 #include "../Platform/Station.h"
14 #include "../Delegate.h"
15 
16 #define NTP_PORT 123
17 #define NTP_PACKET_SIZE 48
18 #define NTP_VERSION 4
19 #define NTP_MODE_CLIENT 3
20 #define NTP_MODE_SERVER 4
21 
22 #define NTP_DEFAULT_SERVER "pool.ntp.org"
23 #define NTP_DEFAULT_QUERY_INTERVAL_SECONDS 600 // 10 minutes
24 #define NTP_RESPONSE_TIMEOUT_MS 20000 // 20 seconds
25 
26 class NtpClient;
27 
28 // Delegate constructor usage: (&YourClass::method, this)
30 
31 
33 class NtpClient : protected UdpConnection
34 {
35 public:
38  NtpClient();
39 
43  NtpClient(NtpTimeResultDelegate onTimeReceivedCb);
44 
50  NtpClient(String reqServer, int reqIntervalSeconds, NtpTimeResultDelegate onTimeReceivedCb = nullptr);
51 
52  virtual ~NtpClient();
53 
57  void requestTime();
58 
63 
67  void setAutoQuery(bool autoQuery);
68 
72  void setAutoQueryInterval(int seconds);
73 
77  void setAutoUpdateSystemClock(bool autoUpdateClock);
78 
79 protected:
85  void onReceive(pbuf *buf, IPAddress remoteIP, uint16_t remotePort);
86 
90  void internalRequestTime(IPAddress serverIp);
91 
92 protected:
93  String server = NTP_DEFAULT_SERVER;
94 
96  bool autoUpdateSystemClock = false;
97 
101 
108  static void staticDnsResponse(const char *name, struct ip_addr *ip, void *arg);
109 };
110 
112 #endif /* APP_NTPCLIENT_H_ */
static void staticDnsResponse(const char *name, struct ip_addr *ip, void *arg)
Handle DNS response.
void setAutoUpdateSystemClock(bool autoUpdateClock)
Enable / disable update of system clock.
Definition: UdpConnection.h:20
Timer connectionTimer
Wait for WiFi connection timer.
Definition: NtpClient.h:100
void onReceive(pbuf *buf, IPAddress remoteIP, uint16_t remotePort)
Handle UDP message reception.
String server
IP address or Hostname of NTP server.
Definition: NtpClient.h:93
Timer autoUpdateTimer
Periodic query timer.
Definition: NtpClient.h:98
NtpClient()
Instantiates NTP client object.
Definition: WString.h:42
void setAutoQueryInterval(int seconds)
Set query period.
void setAutoQuery(bool autoQuery)
Enable / disable periodic query.
Timer timeoutTimer
NTP message timeout timer.
Definition: NtpClient.h:99
void internalRequestTime(IPAddress serverIp)
Send time request to NTP server.
void setNtpServer(String server)
Set the NTP server.
bool autoUpdateSystemClock
True to update system clock with NTP time.
Definition: NtpClient.h:96
NTP client class.
Definition: NtpClient.h:33
Definition: Timer.h:29
Definition: IPAddress.h:30
void requestTime()
Request time from NTP server.
NtpTimeResultDelegate delegateCompleted
NTP result handler delegate.
Definition: NtpClient.h:95