forked from KirillTregubov/SmartLock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrtc_service.cpp
More file actions
40 lines (34 loc) · 1.01 KB
/
rtc_service.cpp
File metadata and controls
40 lines (34 loc) · 1.01 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
/**
* @file rtc_service.cpp
* @author Kirill Tregubov (KirillTregubov)
* @author Philip Cai (Gadnalf)
* @copyright Copyright (c) 2022 Kirill Tregubov & Philip Cai
*
* @brief This module contains functions for syncing the RTC.
* @bug No known bugs.
*/
#include "rtc_service.hpp"
void sync_rtc_with_factory() {
set_time(1648016868);
time_t factory_time = time(NULL);
printf("> Defaulted RTC to %s", ctime(&factory_time));
}
void sync_rtc_with_ntp(NetworkInterface *wifi) {
NTPClient ntp(wifi);
time_t timestamp = -1;
for (int i = 0; i < 4; i++) {
ntp.set_server("1.pool.ntp.org", 123);
time_t new_timestamp = ntp.get_timestamp();
if ((int)timestamp < 0 || timestamp == new_timestamp ||
timestamp + 1 == new_timestamp) {
timestamp = new_timestamp;
}
}
if ((int)timestamp < 0) {
printf("An error occurred when getting the time. (code %u)\n", timestamp);
sync_rtc_with_factory();
} else {
printf("> Synced RTC to %s", ctime(×tamp));
set_time(timestamp);
}
}