NtpClient.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * NtpClient.h
8  *
9  ****/
10 
17 #pragma once
18 
19 #include "UdpConnection.h"
20 #include "Platform/System.h"
21 #include "Timer.h"
22 #include "DateTime.h"
23 
24 #define NTP_PORT 123
25 #define NTP_PACKET_SIZE 48
26 #define NTP_VERSION 4
27 #define NTP_MODE_CLIENT 3
28 #define NTP_MODE_SERVER 4
29 
30 #define NTP_DEFAULT_SERVER F("pool.ntp.org")
31 #define NTP_DEFAULT_AUTOQUERY_SECONDS 30U // (10U * SECS_PER_MIN) ///< Refresh time if autoupdate set
32 #define NTP_MIN_AUTOQUERY_SECONDS 10U
33 #define NTP_CONNECTION_TIMEOUT_MS 1666U
34 #define NTP_RESPONSE_TIMEOUT_MS 20000U
35 
36 class NtpClient;
37 
38 // Delegate constructor usage: (&YourClass::method, this)
39 using NtpTimeResultDelegate = Delegate<void(NtpClient& client, time_t ntpTime)>;
40 
42 class NtpClient : protected UdpConnection
43 {
44 public:
48  {
49  }
50 
54  NtpClient(NtpTimeResultDelegate onTimeReceivedCb)
55  : NtpClient(nullptr, NTP_DEFAULT_AUTOQUERY_SECONDS, onTimeReceivedCb)
56  {
57  }
58 
64  NtpClient(const String& reqServer, unsigned reqIntervalSeconds, NtpTimeResultDelegate onTimeReceivedCb = nullptr);
65 
69  void requestTime();
70 
74  void setNtpServer(const String& server)
75  {
76  this->server = server;
77  }
78 
82  void setAutoQuery(bool autoQuery);
83 
87  void setAutoQueryInterval(unsigned seconds);
88 
92  void setAutoUpdateSystemClock(bool autoUpdateClock)
93  {
94  autoUpdateSystemClock = autoUpdateClock;
95  }
96 
97 protected:
103  void onReceive(pbuf* buf, IpAddress remoteIP, uint16_t remotePort) override;
104 
109 
113  void startTimer(uint32_t milliseconds)
114  {
115  debug_d("NtpClient::startTimer(%u)", milliseconds);
116  timer.setIntervalMs(milliseconds);
117  timer.startOnce();
118  }
119 
120  void stopTimer()
121  {
122  debug_d("NtpClient::stopTimer()");
123  timer.stop();
124  }
125 
126 protected:
128 
130  bool autoUpdateSystemClock = false;
131  bool autoQueryEnabled = false;
134 };
135 
bool setIntervalMs(uint32_t milliseconds)
Set timer interval in milliseconds.
Definition: CallbackTimer.h:378
void stop()
Stops timer.
Definition: CallbackTimer.h:239
bool startOnce()
Start one-shot timer.
Definition: CallbackTimer.h:232
A class to make it easier to handle and pass around IP addresses.
Definition: IpAddress.h:45
NTP client class.
Definition: NtpClient.h:43
void stopTimer()
Definition: NtpClient.h:120
bool autoUpdateSystemClock
True to update system clock with NTP time.
Definition: NtpClient.h:130
String server
IP address or Hostname of NTP server.
Definition: NtpClient.h:127
unsigned autoQuerySeconds
Definition: NtpClient.h:132
bool autoQueryEnabled
Definition: NtpClient.h:131
void setNtpServer(const String &server)
Set the NTP server.
Definition: NtpClient.h:74
void setAutoQueryInterval(unsigned seconds)
Set query period.
void onReceive(pbuf *buf, IpAddress remoteIP, uint16_t remotePort) override
Handle UDP message reception.
NtpClient(const String &reqServer, unsigned reqIntervalSeconds, NtpTimeResultDelegate onTimeReceivedCb=nullptr)
Instantiates NTP client object.
void setAutoQuery(bool autoQuery)
Enable / disable periodic query.
NtpClient(NtpTimeResultDelegate onTimeReceivedCb)
Instantiates NTP client object.
Definition: NtpClient.h:54
void startTimer(uint32_t milliseconds)
Start the timer running.
Definition: NtpClient.h:113
void internalRequestTime(IpAddress serverIp)
Send time request to NTP server.
NtpClient()
Instantiates NTP client object.
Definition: NtpClient.h:47
NtpTimeResultDelegate delegateCompleted
NTP result handler delegate.
Definition: NtpClient.h:129
void setAutoUpdateSystemClock(bool autoUpdateClock)
Enable / disable update of system clock.
Definition: NtpClient.h:92
Timer timer
Deals with timeouts, retries and autoquery updates.
Definition: NtpClient.h:133
void requestTime()
Request time from NTP server.
The String class.
Definition: WString.h:137
Callback timer class.
Definition: Timer.h:263
Definition: UdpConnection.h:28
#define debug_d
Definition: debug_progmem.h:100
#define NTP_DEFAULT_AUTOQUERY_SECONDS
Definition: NtpClient.h:31