forked from thoralt/esp8266wordclock
-
Notifications
You must be signed in to change notification settings - Fork 6
/
ntp.cpp
403 lines (358 loc) · 12.3 KB
/
ntp.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
395
396
397
398
399
400
401
402
403
// ESP8266 Wordclock
// Copyright (C) 2016 Thoralt Franz, https://github.com/thoralt
//
// This module contains a simple NTP client. NTP packets are sent using UDP to a
// configurable NTP server. The server reply is processed and a callback is
// executed to notifiy the calling application of the current time. The internal
// state machine is called from an internal timer to take care of timeouts, the
// NTP request is retried automatically if no reply is being received. The request
// is being repeated every 59 minutes, so the calling module is updated regularly.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// This code is based on (heavily modified):
// https://github.com/sandeepmistry/esp8266-Arduino/blob/master/esp8266com/esp8266/libraries/ESP8266WiFi/examples/NTPClient
#include <Arduino.h>
#include <limits.h>
#include "ntp.h"
//---------------------------------------------------------------------------------------
// CONSTANTS
//---------------------------------------------------------------------------------------
#define NTP_PACKET_SIZE 48
#define LOCAL_PORT 2390
#define NTP_TIMEOUT 5000
#define TIMER_RESOLUTION 10
#define NTP_RELOAD_INTERVAL (59*60*1000)
//---------------------------------------------------------------------------------------
// global instance
//---------------------------------------------------------------------------------------
NtpClass NTP = NtpClass();
//---------------------------------------------------------------------------------------
// tickerFunctionWrapper
//
// Static wrapper which is used as callback function for Ticker and takes supplied
// argument to call the handler tickerFunction() of instance method
//
// -> obj: Instance of class to call the method tickerFunctio() on
// <- --
//---------------------------------------------------------------------------------------
void NtpClass::tickerFunctionWrapper(NtpClass *obj)
{
obj->tickerFunction();
}
//---------------------------------------------------------------------------------------
// NtpClass
//
// Constructor, sets up a timer to trigger the internal state machine
//
// -> --
// <- --
//---------------------------------------------------------------------------------------
NtpClass::NtpClass()
{
this->ticker.attach_ms(TIMER_RESOLUTION, NtpClass::tickerFunctionWrapper,
this);
}
//---------------------------------------------------------------------------------------
// setServer
//
// Sets the current time server and schedules a new NTP request
//
// -> address: IP address of the new time server
// <- --
//---------------------------------------------------------------------------------------
void NtpClass::setServer(IPAddress address)
{
this->timeServer = address;
this->state = NtpState::waitingForReload;
this->timer = NTP_RELOAD_INTERVAL - 2000;
}
//---------------------------------------------------------------------------------------
// getServer
//
// Gets the current time server
//
// -> --
// <- IPAddress current time server
//---------------------------------------------------------------------------------------
IPAddress NtpClass::getServer()
{
return this->timeServer;
}
//---------------------------------------------------------------------------------------
// begin
//
// Initializes the class and starts the first NTP request, automatically fires callback
// upon success, repeats every 59 minutes
//
// -> ip: Address of an NTP server
// callback: Function to receive the current time (hours, minutes, seconds, ms)
// timezone: Hours difference from UTC (will be added to the received time, can be
// negative)
// DST: if true, european daylight savings time is enabled and will be automatically
// adjusted depending on current date
// <- --
//---------------------------------------------------------------------------------------
void NtpClass::begin(IPAddress ip, TNtpCallback callback, int timezone, bool DST)
{
this->_callback = callback;
this->timeServer = ip;
this->tz = timezone * 3600;
this->useDST = DST;
this->udp.begin(LOCAL_PORT);
// wait 2 seconds before starting first request
Serial.println("NtpClass::begin() Waiting 2 seconds");
this->state = NtpState::waitingForReload;
this->timer = NTP_RELOAD_INTERVAL - 2000;
}
//---------------------------------------------------------------------------------------
// tickerFunction
//
// Function to be called by the timer, drives the internal state machine
//
// -> --
// <- --
//---------------------------------------------------------------------------------------
void NtpClass::tickerFunction()
{
// increment timer variable
this->timer += TIMER_RESOLUTION;
switch (this->state)
{
case NtpState::startRequest:
this->udp.flush(); // clear previously received data
this->sendPacket(); // request new time data
this->timer = 0;
this->state = NtpState::waitingForReply;
this->syncInProgress = true;
break;
case NtpState::waitingForReply:
if (this->timer >= NTP_TIMEOUT)
{
Serial.println("NtpClass: NTP request timeout");
this->state = NtpState::startRequest;
}
else if (udp.parsePacket() > 0)
{
Serial.println("NtpClass: Received NTP packet");
this->parse();
if (this->_callback)
this->_callback(this->h, this->m, this->s, this->ms);
this->timer = 0;
this->state = NtpState::waitingForReload;
this->syncInProgress = false;
}
break;
case NtpState::waitingForReload:
if (this->timer >= NTP_RELOAD_INTERVAL)
{
Serial.println("NtpClass: NTP reload timer expired.");
this->state = NtpState::startRequest;
}
break;
case NtpState::idle:
default:
break;
}
}
//---------------------------------------------------------------------------------------
// dayOfWeek
//
// Calculates the weekday for a given date.
//
// -> y, m, d: year, month, day to calculate the weekday for
// <- weekday (0=Sunday, 1=Monday, ...)
//---------------------------------------------------------------------------------------
int NtpClass::dayOfWeek(int y, int m, int d)
{
static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
y -= m < 3;
return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}
//---------------------------------------------------------------------------------------
// lastSunday
//
// Calculates the last Sunday for a given year and month.
//
// -> year, month: ...
// lastDayInMonth: number of days in given month (out of laziness, should be done
// inside the method)
// <- weekday (0=Sunday, 1=Monday, ...)
//---------------------------------------------------------------------------------------
int NtpClass::lastSunday(int year, int month, int lastDayInMonth)
{
for(int day = lastDayInMonth; day > 0; day--)
{
if(this->dayOfWeek(year, month, day) == 0) return day;
}
return 0;
}
//---------------------------------------------------------------------------------------
// isDSTactive
//
// Checks if daylight saving time is currently in effect using the internal member
// variables for year, month, day and hour.
//
// -> --
// <- true if DST is active
//---------------------------------------------------------------------------------------
bool NtpClass::isDSTactive()
{
// active in April ... September
if(this->month > 3 && this->month < 10) return true;
int lastSundayInMarch = this->lastSunday(this->year, 3, 31);
int lastSundayInOctober = this->lastSunday(this->year, 10, 31);
if(this->month == 3)
{
// active after last Sunday in March
if(this->day > lastSundayInMarch) return true;
// active on last Sunday in March after 02:00
if(this->day == lastSundayInMarch && this->h >= 2) return true;
}
if(this->month == 10)
{
// active before last Sunday in October
if(this->day < lastSundayInOctober) return true;
// active on last Sunday in October before 03:00
if(this->day == lastSundayInMarch && this->h < 3) return true;
}
return false;
}
//---------------------------------------------------------------------------------------
// parse
//
// Reads the received UDP packet and decodes the current time, stores result in (this)
//
// -> --
// <- --
//---------------------------------------------------------------------------------------
void NtpClass::parse()
{
byte buf[NTP_PACKET_SIZE];
bool DST = false;
Serial.print("NtpClass::parse() (");
Serial.print(this->timer);
this->udp.read(buf, NTP_PACKET_SIZE);
this->udp.flush(); // discard additional data
unsigned long highWord = word(buf[40], buf[41]);
unsigned long lowWord = word(buf[42], buf[43]);
unsigned long secsSince1970 = (highWord << 16 | lowWord) - 2208988800ULL;
// calculate date and time from timestamp
this->decodeTime(secsSince1970 + this->tz);
randomSeed(secsSince1970);
// check if we need to adjust for daylight savings time
if(this->useDST)
{
// check if we are inside DST window
DST = this->isDSTactive();
if(DST)
{
// decode date/time again using DST offset
this->decodeTime(secsSince1970 + this->tz + 3600);
}
}
Serial.printf("ms), local time: %02i:%02i:%02i, date: %i-%02i-%02i, "
"weekday=%i, DST=%i\r\n", h, m, s, year, month, day, weekday, DST);
}
//---------------------------------------------------------------------------------------
// sendNTPpacket
//
// Requests time from an NTP server
//
// -> --
// <- --
//---------------------------------------------------------------------------------------
void NtpClass::sendPacket()
{
uint8_t buf[NTP_PACKET_SIZE];
Serial.println("NtpClass::sendPacket()");
memset(buf, 0, NTP_PACKET_SIZE);
buf[0] = 0b11100011;
buf[1] = 0;
buf[2] = 6;
buf[3] = 0xEC;
buf[12] = 49;
buf[13] = 0x4E;
buf[14] = 49;
buf[15] = 52;
this->udp.beginPacket(this->timeServer, 123);
this->udp.write(buf, NTP_PACKET_SIZE);
this->udp.endPacket();
this->timer = 0;
}
void NtpClass::setTimeZone(int timeZone)
{
this->tz = timeZone * 3600;
this->timer = NTP_RELOAD_INTERVAL - 1000;
}
// modified/borrowed from http://git.musl-libc.org/cgit/musl/tree/src/time/__secs_to_tm.c?h=v0.9.15
/* 2000-03-01 (mod 400 year, immediately after feb29 */
#define LEAPOCH (946684800LL + 86400*(31+29))
#define DAYS_PER_400Y (365*400 + 97)
#define DAYS_PER_100Y (365*100 + 24)
#define DAYS_PER_4Y (365*4 + 1)
void NtpClass::decodeTime(long long t)
{
long long days, secs;
int remdays, remsecs, remyears;
int qc_cycles, c_cycles, q_cycles;
int years, months;
int wday, yday, leap;
static const char days_in_month[] = {31,30,31,30,31,31,30,31,30,31,31,29};
secs = t - LEAPOCH;
days = secs / 86400;
remsecs = secs % 86400;
if(remsecs < 0)
{
remsecs += 86400;
days--;
}
wday = (3+days)%7;
if (wday < 0) wday += 7;
qc_cycles = days / DAYS_PER_400Y;
remdays = days % DAYS_PER_400Y;
if(remdays < 0)
{
remdays += DAYS_PER_400Y;
qc_cycles--;
}
c_cycles = remdays / DAYS_PER_100Y;
if(c_cycles == 4) c_cycles--;
remdays -= c_cycles * DAYS_PER_100Y;
q_cycles = remdays / DAYS_PER_4Y;
if(q_cycles == 25) q_cycles--;
remdays -= q_cycles * DAYS_PER_4Y;
remyears = remdays / 365;
if(remyears == 4) remyears--;
remdays -= remyears * 365;
leap = !remyears && (q_cycles || !c_cycles);
yday = remdays + 31 + 28 + leap;
if(yday >= 365+leap) yday -= 365+leap;
years = remyears + 4*q_cycles + 100*c_cycles + 400*qc_cycles;
for(months=0; days_in_month[months] <= remdays; months++)
remdays -= days_in_month[months];
this->year = years + 2000;
this->month = months + 3;
if(this->month >= 12)
{
this->month -=12;
this->year++;
}
this->day = remdays + 1;
this->weekday = wday;
this->yearday = yday;
this->h = remsecs / 3600;
this->m = remsecs / 60 % 60;
this->s = remsecs % 60;
}