-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNTPClientLib.cpp
394 lines (333 loc) · 12.7 KB
/
NTPClientLib.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
Copyright 2016 German Martin (gmag11@gmail.com). All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met :
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and / or other materials
provided with the distribution.
THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those of the
authors and should not be interpreted as representing official policies, either expressed
or implied, of German Martin
*/
//
//
//
#include "NtpClientLib.h"
#define DBG_PORT Serial
#ifdef DEBUG_NTPCLIENT
#define DEBUGLOG(...) DBG_PORT.printf(__VA_ARGS__)
#else
#define DEBUGLOG(...)
#endif
NTPClient::NTPClient () {
}
bool NTPClient::setNtpServerName (String ntpServerName) {
char * name = (char *)malloc ((ntpServerName.length () + 1) * sizeof (char));
if (!name)
return false;
ntpServerName.toCharArray (name, ntpServerName.length () + 1);
DEBUGLOG ("NTP server set to %s\n", name);
free (_ntpServerName);
_ntpServerName = name;
return true;
}
bool NTPClient::setNtpServerName (char* ntpServerName) {
char *name = ntpServerName;
if (name == NULL)
return false;
DEBUGLOG ("NTP server set to %s\n", name);
free (_ntpServerName);
_ntpServerName = name;
return true;
}
String NTPClient::getNtpServerName () {
return String (_ntpServerName);
}
char* NTPClient::getNtpServerNamePtr () {
return _ntpServerName;
}
bool NTPClient::setTimeZone (int8_t timeZone, int8_t minutes) {
if ((timeZone >= -12) && (timeZone <= 14) && (minutes >= -59) && (minutes <= 59)) {
// Temporarily set time to new time zone, before trying to synchronize
int8_t timeDiff = timeZone - _timeZone;
_timeZone = timeZone;
_minutesOffset = minutes;
setTime (now () + timeDiff * SECS_PER_HOUR + minutes * SECS_PER_MIN);
if (udp && (timeStatus () != timeNotSet)) {
setTime (getTime ());
}
DEBUGLOG ("NTP time zone set to: %d\r\n", timeZone);
return true;
}
return false;
}
boolean sendNTPpacket (const char* address, UDP *udp) {
uint8_t ntpPacketBuffer[NTP_PACKET_SIZE]; //Buffer to store request message
// set all bytes in the buffer to 0
memset (ntpPacketBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
ntpPacketBuffer[0] = 0b11100011; // LI, Version, Mode
ntpPacketBuffer[1] = 0; // Stratum, or type of clock
ntpPacketBuffer[2] = 6; // Polling Interval
ntpPacketBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
ntpPacketBuffer[12] = 49;
ntpPacketBuffer[13] = 0x4E;
ntpPacketBuffer[14] = 49;
ntpPacketBuffer[15] = 52;
// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
udp->beginPacket (address, DEFAULT_NTP_PORT); //NTP requests are to port 123
udp->write (ntpPacketBuffer, NTP_PACKET_SIZE);
udp->endPacket ();
return true;
}
time_t NTPClient::getTime () {
//DNSClient dns;
//WiFiUDP *udpClient = new WiFiUDP(*udp);
IPAddress timeServerIP; //NTP server IP address
char ntpPacketBuffer[NTP_PACKET_SIZE]; //Buffer to store response message
DEBUGLOG ("Starting UDP\n");
udp->begin (DEFAULT_NTP_PORT);
//DEBUGLOG ("UDP port: %d\n",udp->localPort());
while (udp->parsePacket () > 0); // discard any previously received packets
/*dns.begin(WiFi.dnsServerIP());
uint8_t dnsResult = dns.getHostByName(NTP.getNtpServerName().c_str(), timeServerIP);
DEBUGLOG(F("NTP Server hostname: "));
DEBUGLOGCR(NTP.getNtpServerName());
DEBUGLOG(F("NTP Server IP address: "));
DEBUGLOGCR(timeServerIP);
DEBUGLOG(F("Result code: "));
DEBUGLOG(dnsResult);
DEBUGLOG(" ");
DEBUGLOGCR(F("-- IP Connected. Waiting for sync"));
DEBUGLOGCR(F("-- Transmit NTP Request"));*/
//if (dnsResult == 1) { //If DNS lookup resulted ok
sendNTPpacket (getNtpServerName ().c_str (), udp);
uint32_t beginWait = millis ();
while (millis () - beginWait < NTP_TIMEOUT) {
int size = udp->parsePacket ();
if (size >= NTP_PACKET_SIZE) {
DEBUGLOG ("-- Receive NTP Response\n");
udp->read (ntpPacketBuffer, NTP_PACKET_SIZE); // read packet into the buffer
time_t timeValue = decodeNtpMessage (ntpPacketBuffer);
setSyncInterval (getLongInterval ());
if (!_firstSync) {
// if (timeStatus () == timeSet)
_firstSync = timeValue;
}
//getFirstSync (); // Set firstSync value if not set before
DEBUGLOG ("Sync frequency set low\n");
udp->stop ();
setLastNTPSync (timeValue);
DEBUGLOG ("Successful NTP sync at %s", getTimeDateString (getLastNTPSync ()).c_str ());
if (onSyncEvent)
onSyncEvent (timeSyncd);
return timeValue;
}
#ifdef ARDUINO_ARCH_ESP8266
ESP.wdtFeed ();
#endif
}
DEBUGLOG ("-- No NTP Response :-(\n");
udp->stop ();
setSyncInterval (getShortInterval ()); // Retry connection more often
if (onSyncEvent)
onSyncEvent (noResponse);
return 0; // return 0 if unable to get the time
}
int8_t NTPClient::getTimeZone () {
return _timeZone;
}
int8_t NTPClient::getTimeZoneMinutes () {
return _minutesOffset;
}
/*void NTPClient::setLastNTPSync(time_t moment) {
_lastSyncd = moment;
}*/
time_t NTPClient::s_getTime () {
return NTP.getTime ();
}
#if NETWORK_TYPE == NETWORK_W5100
bool NTPClient::begin (String ntpServerName, int8_t timeZone, bool daylight, int8_t minutes, EthernetUDP* udp_conn) {
#elif NETWORK_TYPE == NETWORK_ESP8266 || NETWORK_TYPE == NETWORK_WIFI101 || NETWORK_TYPE == NETWORK_ESP32
bool NTPClient::begin (String ntpServerName, int8_t timeZone, bool daylight, int8_t minutes, WiFiUDP* udp_conn) {
#endif
if (!setNtpServerName (ntpServerName)) {
DEBUGLOG ("Time sync not started\r\n");
return false;
}
if (!setTimeZone (timeZone, minutes)) {
DEBUGLOG ("Time sync not started\r\n");
return false;
}
if (udp_conn)
udp = udp_conn;
else
#if NETWORK_TYPE == NETWORK_W5100
udp = new EthernetUDP ();
#else
udp = new WiFiUDP ();
#endif
//_timeZone = timeZone;
setDayLight (daylight);
_lastSyncd = 0;
if (!setInterval (DEFAULT_NTP_SHORTINTERVAL, DEFAULT_NTP_INTERVAL)) {
DEBUGLOG ("Time sync not started\r\n");
return false;
}
DEBUGLOG ("Time sync started\r\n");
setSyncInterval (getShortInterval ());
setSyncProvider (s_getTime);
return true;
}
bool NTPClient::stop () {
setSyncProvider (NULL);
DEBUGLOG ("Time sync disabled\n");
return true;
}
bool NTPClient::setInterval (int interval) {
if (interval >= 10) {
if (_longInterval != interval) {
_longInterval = interval;
DEBUGLOG ("Sync interval set to %d\n", interval);
if (timeStatus () == timeSet)
setSyncInterval (interval);
}
return true;
} else
return false;
}
bool NTPClient::setInterval (int shortInterval, int longInterval) {
if (shortInterval >= 10 && longInterval >= 10) {
_shortInterval = shortInterval;
_longInterval = longInterval;
if (timeStatus () != timeSet) {
setSyncInterval (shortInterval);
} else {
setSyncInterval (longInterval);
}
DEBUGLOG ("Short sync interval set to %d\n", shortInterval);
DEBUGLOG ("Long sync interval set to %d\n", longInterval);
return true;
} else
return false;
}
int NTPClient::getInterval () {
return _longInterval;
}
int NTPClient::getShortInterval () {
return _shortInterval;
}
void NTPClient::setDayLight (bool daylight) {
_daylight = daylight;
DEBUGLOG ("--Set daylight saving %s\n", daylight ? "ON" : "OFF");
setTime (getTime ());
}
bool NTPClient::getDayLight () {
return _daylight;
}
String NTPClient::getTimeStr (time_t moment) {
char timeStr[10];
sprintf (timeStr, "%02d:%02d:%02d", hour (moment), minute (moment), second (moment));
return timeStr;
}
String NTPClient::getDateStr (time_t moment) {
char dateStr[12];
sprintf (dateStr, "%02d/%02d/%4d", day (moment), month (moment), year (moment));
return dateStr;
}
String NTPClient::getTimeDateString (time_t moment) {
return getTimeStr (moment) + " " + getDateStr (moment);
}
time_t NTPClient::getLastNTPSync () {
return _lastSyncd;
}
void NTPClient::onNTPSyncEvent (onSyncEvent_t handler) {
onSyncEvent = handler;
}
time_t NTPClient::getUptime () {
_uptime = _uptime + (millis () - _uptime);
return _uptime / 1000;
}
String NTPClient::getUptimeString () {
uint16_t days;
uint8_t hours;
uint8_t minutes;
uint8_t seconds;
time_t uptime = getUptime ();
seconds = uptime % SECS_PER_MIN;
uptime -= seconds;
minutes = (uptime % SECS_PER_HOUR) / SECS_PER_MIN;
uptime -= minutes * SECS_PER_MIN;
hours = (uptime % SECS_PER_DAY) / SECS_PER_HOUR;
uptime -= hours * SECS_PER_HOUR;
days = uptime / SECS_PER_DAY;
char uptimeStr[20];
sprintf (uptimeStr, "%4u days %02d:%02d:%02d", days, hours, minutes, seconds);
return uptimeStr;
}
time_t NTPClient::getLastBootTime () {
if (timeStatus () == timeSet) {
return (now () - getUptime ());
}
return 0;
}
time_t NTPClient::getFirstSync () {
/*if (!_firstSync) {
if (timeStatus () == timeSet) {
_firstSync = now () - getUptime ();
}
}*/
return _firstSync;
}
bool NTPClient::summertime (int year, byte month, byte day, byte hour, byte tzHours)
// input parameters: "normal time" for year, month, day, hour and tzHours (0=UTC, 1=MEZ)
{
if ((month < 3) || (month > 10)) return false; // keine Sommerzeit in Jan, Feb, Nov, Dez
if ((month > 3) && (month < 10)) return true; // Sommerzeit in Apr, Mai, Jun, Jul, Aug, Sep
if ((month == 3 && (hour + 24 * day) >= (1 + tzHours + 24 * (31 - (5 * year / 4 + 4) % 7))) || (month == 10 && (hour + 24 * day) < (1 + tzHours + 24 * (31 - (5 * year / 4 + 1) % 7))))
return true;
else
return false;
}
boolean NTPClient::isSummerTimePeriod (time_t moment) {
return summertime (year (), month (), day (), hour (), getTimeZone ());
}
void NTPClient::setLastNTPSync (time_t moment) {
_lastSyncd = moment;
}
time_t NTPClient::decodeNtpMessage (char *messageBuffer) {
unsigned long secsSince1900;
// convert four bytes starting at location 40 to a long integer
secsSince1900 = (unsigned long)messageBuffer[40] << 24;
secsSince1900 |= (unsigned long)messageBuffer[41] << 16;
secsSince1900 |= (unsigned long)messageBuffer[42] << 8;
secsSince1900 |= (unsigned long)messageBuffer[43];
#define SEVENTY_YEARS 2208988800UL
time_t timeTemp = secsSince1900 - SEVENTY_YEARS + _timeZone * SECS_PER_HOUR + _minutesOffset * SECS_PER_MIN;
if (_daylight) {
if (summertime (year (timeTemp), month (timeTemp), day (timeTemp), hour (timeTemp), _timeZone)) {
timeTemp += SECS_PER_HOUR;
DEBUGLOG ("Summer Time\n");
} else {
DEBUGLOG ("Winter Time\n");
}
} else {
DEBUGLOG ("No daylight\n");
}
return timeTemp;
}
NTPClient NTP;