Sming Framework API
Sming - Open Source framework for high efficiency WiFi SoC ESP8266 native development with C++ language.
NtpClient.h
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/anakod/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * NtpClient.h
8  *
9  ****/
10 
17 #ifndef _SMING_CORE_NETWORK_NTP_CLIENT_H_
18 #define _SMING_CORE_NETWORK_NTP_CLIENT_H_
19 
20 #include "UdpConnection.h"
21 #include "Platform/System.h"
22 #include "Timer.h"
23 #include "DateTime.h"
24 #include "Delegate.h"
25 
26 #define NTP_PORT 123
27 #define NTP_PACKET_SIZE 48
28 #define NTP_VERSION 4
29 #define NTP_MODE_CLIENT 3
30 #define NTP_MODE_SERVER 4
31 
32 #define NTP_DEFAULT_SERVER F("pool.ntp.org")
33 #define NTP_DEFAULT_AUTOQUERY_SECONDS 30U // (10U * SECS_PER_MIN)
34 #define NTP_MIN_AUTOQUERY_SECONDS 10U
35 #define NTP_CONNECTION_TIMEOUT_MS 1666U
36 #define NTP_RESPONSE_TIMEOUT_MS 20000U
37 
38 class NtpClient;
39 
40 // Delegate constructor usage: (&YourClass::method, this)
42 
44 class NtpClient : protected UdpConnection
45 {
46 public:
49  NtpClient() : NtpClient(NTP_DEFAULT_SERVER, NTP_DEFAULT_AUTOQUERY_SECONDS, nullptr)
50  {
51  }
52 
56  NtpClient(NtpTimeResultDelegate onTimeReceivedCb)
57  : NtpClient(NTP_DEFAULT_SERVER, NTP_DEFAULT_AUTOQUERY_SECONDS, onTimeReceivedCb)
58  {
59  }
60 
66  NtpClient(const String& reqServer, unsigned reqIntervalSeconds, NtpTimeResultDelegate onTimeReceivedCb = nullptr);
67 
71  void requestTime();
72 
76  void setNtpServer(const String& server)
77  {
78  this->server = server;
79  }
80 
84  void setAutoQuery(bool autoQuery);
85 
89  void setAutoQueryInterval(unsigned seconds);
90 
94  void setAutoUpdateSystemClock(bool autoUpdateClock)
95  {
96  autoUpdateSystemClock = autoUpdateClock;
97  }
98 
99 protected:
105  void onReceive(pbuf* buf, IPAddress remoteIP, uint16_t remotePort) override;
106 
110  void internalRequestTime(IPAddress serverIp);
111 
115  void startTimer(uint32_t milliseconds)
116  {
117  debug_d("NtpClient::startTimer(%u)", milliseconds);
118  timer.setIntervalMs(milliseconds);
119  timer.startOnce();
120  }
121 
122  void stopTimer()
123  {
124  debug_d("NtpClient::stopTimer()");
125  timer.stop();
126  }
127 
128 protected:
130 
132  bool autoUpdateSystemClock = false;
133  bool autoQueryEnabled = false;
134  unsigned autoQuerySeconds = NTP_DEFAULT_AUTOQUERY_SECONDS;
136 };
137 
139 #endif /* _SMING_CORE_NETWORK_NTP_CLIENT_H_ */
void setAutoUpdateSystemClock(bool autoUpdateClock)
Enable / disable update of system clock.
Definition: NtpClient.h:94
Definition: UdpConnection.h:29
NtpClient(NtpTimeResultDelegate onTimeReceivedCb)
Instantiates NTP client object.
Definition: NtpClient.h:56
String server
IP address or Hostname of NTP server.
Definition: NtpClient.h:129
void setNtpServer(const String &server)
Set the NTP server.
Definition: NtpClient.h:76
NtpClient()
Instantiates NTP client object.
Definition: NtpClient.h:49
The string class.
Definition: WString.h:104
void onReceive(pbuf *buf, IPAddress remoteIP, uint16_t remotePort) override
Handle UDP message reception.
void setAutoQuery(bool autoQuery)
Enable / disable periodic query.
void internalRequestTime(IPAddress serverIp)
Send time request to NTP server.
void setAutoQueryInterval(unsigned seconds)
Set query period.
__forceinline void IRAM_ATTR startOnce()
Start one-shot timer running.
Definition: Timer.h:99
bool autoUpdateSystemClock
True to update system clock with NTP time.
Definition: NtpClient.h:132
Timer timer
Deals with timeouts, retries and autoquery updates.
Definition: NtpClient.h:135
NTP client class.
Definition: NtpClient.h:44
void IRAM_ATTR stop()
Stop timer.
void IRAM_ATTR setIntervalMs(uint32_t milliseconds=1000000)
Set timer interval.
Definition: Timer.h:26
Definition: IPAddress.h:37
void requestTime()
Request time from NTP server.
void startTimer(uint32_t milliseconds)
Start the timer running.
Definition: NtpClient.h:115
NtpTimeResultDelegate delegateCompleted
NTP result handler delegate.
Definition: NtpClient.h:131