forked from wuspy/portal_calendar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime.cpp
More file actions
210 lines (183 loc) · 7.1 KB
/
Copy pathtime.cpp
File metadata and controls
210 lines (183 loc) · 7.1 KB
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
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include "time.h"
#define NTP_PACKET_SIZE 48
const char* NTP_SERVER_LIST[] = {NTP_SERVERS};
const char* TIMEZONED_SERVER_LIST[] = {TIMEZONED_SERVERS};
static_assert(
sizeof(NTP_SERVER_LIST) / sizeof(char*) > 0,
"No NTP servers configured"
);
static_assert(
sizeof(TIMEZONED_SERVER_LIST) / sizeof(char*) > 0,
"No timezoned servers configured"
);
RTC_DATA_ATTR time_t lastNtpSync = 0;
time_t getLastNtpSync()
{
return lastNtpSync;
}
#ifdef ENABLE_RTC_CORRECTION
RTC_DATA_ATTR double rtcCorrectionFactor = 0.0;
double getRtcCorrectionFactor()
{
return rtcCorrectionFactor;
}
#endif // ENABLE_RTC_CORRECTION
int getDaysInMonth(int month, int year)
{
switch (month) {
case 1:
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) ? 29 : 28;
case 3:
case 5:
case 8:
case 10:
return 30;
default:
return 31;
}
}
void advanceDay(int& month, int& mday, int& year)
{
++mday;
if (mday > getDaysInMonth(month, year)) {
mday = 1;
++month;
if (month > 11) {
month = 0;
++year;
}
}
}
#ifdef TIME_ZONE
String getPosixTz(String name)
{
for (int i = 0; i < sizeof(TIMEZONED_SERVER_LIST) / sizeof(char*); ++i) {
const char* server = TIMEZONED_SERVER_LIST[i];
DEBUG_PRINT("Looking up POSIX timezone for %s from %s", name.c_str(), server);
WiFiUDP udp;
udp.flush();
udp.begin(TIMEZONED_LOCAL_PORT_START + i); // Each server must be called on a different port in case a packet comes in late
unsigned long started = millis();
udp.beginPacket(server, 2342);
udp.write((const uint8_t*)name.c_str(), name.length());
udp.endPacket();
// Wait for packet until timeout
bool parsedPacket;
do {
delay(1);
parsedPacket = udp.parsePacket();
} while (!parsedPacket && millis() - started < TZ_LOOKUP_TIMEOUT_MS);
if (!parsedPacket) {
DEBUG_PRINT("Timeout for server %s", server);
udp.stop();
continue;
}
DEBUG_PRINT("Request to %s request took %lums", server, millis() - started);
// Stick result in String recv
String recv;
recv.reserve(60);
while (udp.available()) {
recv += (char)udp.read();
}
udp.stop();
DEBUG_PRINT("Response from %s: %s", server, recv.c_str());
if (recv.substring(0, 3) == "OK ") {
return recv.substring(recv.indexOf(" ", 4) + 1);
}
}
return "";
}
#endif // TIME_ZONE
/**
* Based on the queryNTP function from ezTime
* https://github.com/ropg/ezTime
*
* @return True if the NTP sync was successful
*/
bool syncNtp()
{
for (int i = 0; i < sizeof(NTP_SERVER_LIST) / sizeof(char*); ++i) {
const char *server = NTP_SERVER_LIST[i];
DEBUG_PRINT("Starting NTP request to %s", server);
// Send NTP packet
byte buffer[NTP_PACKET_SIZE];
memset(buffer, 0, NTP_PACKET_SIZE);
buffer[0] = 0b11100011; // LI, Version, Mode
buffer[1] = 0; // Stratum, or type of clock
buffer[2] = 9; // Polling Interval (9 = 2^9 secs = ~9 mins, close to our 10 min default)
buffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
buffer[12] = 'X'; // "kiss code", see RFC5905
buffer[13] = 'E'; // (codes starting with 'X' are not interpreted)
buffer[14] = 'Z';
buffer[15] = 'T';
WiFiUDP udp;
udp.flush();
udp.begin(NTP_LOCAL_PORT_START + i); // Each server must be called on a different port in case a packet comes in late
unsigned long started = millis();
udp.beginPacket(server, 123); //NTP requests are to port 123
udp.write(buffer, NTP_PACKET_SIZE);
udp.endPacket();
// Wait for packet until timeout
bool parsedPacket;
do {
delay(1);
parsedPacket = udp.parsePacket();
} while (!parsedPacket && millis() - started < NTP_TIMEOUT_MS);
if (!parsedPacket) {
DEBUG_PRINT("NTP sync timeout for server %s", server);
udp.stop();
continue;
}
udp.read(buffer, NTP_PACKET_SIZE);
udp.stop();
//prepare timestamps
uint32_t highWord, lowWord;
highWord = ( buffer[16] << 8 | buffer[17] ) & 0x0000FFFF;
lowWord = ( buffer[18] << 8 | buffer[19] ) & 0x0000FFFF;
uint32_t reftsSec = highWord << 16 | lowWord; // reference timestamp seconds
highWord = ( buffer[32] << 8 | buffer[33] ) & 0x0000FFFF;
lowWord = ( buffer[34] << 8 | buffer[35] ) & 0x0000FFFF;
uint32_t rcvtsSec = highWord << 16 | lowWord; // receive timestamp seconds
highWord = ( buffer[40] << 8 | buffer[41] ) & 0x0000FFFF;
lowWord = ( buffer[42] << 8 | buffer[43] ) & 0x0000FFFF;
uint32_t secsSince1900 = highWord << 16 | lowWord; // transmit timestamp seconds
highWord = ( buffer[44] << 8 | buffer[45] ) & 0x0000FFFF;
lowWord = ( buffer[46] << 8 | buffer[47] ) & 0x0000FFFF;
uint32_t fraction = highWord << 16 | lowWord; // transmit timestamp fractions
//check if received data makes sense
//buffer[1] = stratum - should be 1..15 for valid reply
//also checking that all timestamps are non-zero and receive timestamp seconds are <= transmit timestamp seconds
if ((buffer[1] < 1) or (buffer[1] > 15) or (reftsSec == 0) or (rcvtsSec == 0) or (rcvtsSec > secsSince1900)) {
// we got invalid packet
DEBUG_PRINT("NTP sync failed for server %s", server);
continue;
}
// Set the t and measured_at variables that were passed by reference
unsigned long duration = millis() - started;
DEBUG_PRINT("NTP sync took %lums", duration);
suseconds_t us = (fraction / 4294967UL + duration / 2) * 1000; // Assume symmetric network latency
timeval now = {
.tv_sec = secsSince1900 - 2208988800UL + us / uS_PER_S, // Subtract 70 years to get seconds since 1970
.tv_usec = us % uS_PER_S,
};
timeval oldNow;
gettimeofday(&oldNow, nullptr);
settimeofday(&now, nullptr);
if (lastNtpSync && difftime(now.tv_sec, lastNtpSync) > 30) {
long driftMs = (now.tv_sec - oldNow.tv_sec) * 1000 + (now.tv_usec - oldNow.tv_usec) / 1000;
#ifdef ENABLE_RTC_CORRECTION
rtcCorrectionFactor -= (double)driftMs / 1000.0 / difftime(now.tv_sec, lastNtpSync);
rtcCorrectionFactor = clamp(rtcCorrectionFactor, MAX_RTC_CORRECTION_FACTOR);
DEBUG_PRINT("System clock drift was %ldms, new correction factor is %0.4f", driftMs, rtcCorrectionFactor);
#else
DEBUG_PRINT("System clock drift was %ldms", driftMs);
#endif
}
lastNtpSync = now.tv_sec;
return true;
}
return false;
}