This commit is contained in:
2021-02-07 19:04:15 +01:00
parent 198edaba6d
commit a2fe201fb2
2 changed files with 20 additions and 7 deletions

View File

@ -30,7 +30,7 @@ void my_errorHandler() {
void second_tick(void *handle) {
t_seconds *seconds = wizGetSeconds();
coloredMsg(LOG_GREEN, "Tick %d %ld %lld", seconds->valid, seconds->missedUpdates, seconds->seconds);
coloredMsg(LOG_GREEN, "Tick %d %lu %lu", seconds->valid, seconds->missedUpdates, seconds->seconds);
}

View File

@ -29,6 +29,8 @@ static uint8_t dnsBuffer[DNS_BUFFER_SIZE];
static uint8_t sntpBuffer[MAX_SNTP_BUF_SIZE];
static t_seconds seconds = { .seconds = 0, .missedUpdates = 0, .valid = false };
const uint64_t UNIX_NTP_EPOCH_DIFF = 2208988800;
extern const uint8_t DHCP_SOCK;
extern const uint8_t DNS_SOCK;
@ -140,21 +142,32 @@ static void wizSNTPHandler(void *handle) {
uint8_t ntpServer[4];
if (wizDnsQuery(NTP_SERVER, ntpServer)) {
SNTP_init(SNTP_SOCK, ntpServer, 0, sntpBuffer);
bool updated = false;
for (uint8_t i = 0; i < 16; i++) {
datetime curTime;
int8_t res = SNTP_run(&curTime);
coloredMsg(LOG_BLUE, "wizsh, res: %d", res);
uint64_t receivedSeconds = curTime.seconds - UNIX_NTP_EPOCH_DIFF;
if (res == 1) {
if (seconds.seconds != curTime.seconds) {
coloredMsg(LOG_BLUE, "wizsh, time deviation: %lld %lld",
seconds.seconds, curTime.seconds);
if (seconds.seconds != receivedSeconds) {
coloredMsg(LOG_BLUE, "wizsh, time deviation: %lu %lu",
seconds.seconds, receivedSeconds);
} else {
coloredMsg(LOG_BLUE, "wizsh, time still matching");
}
seconds.seconds = curTime.seconds;
seconds.seconds = receivedSeconds;
seconds.valid = true;
coloredMsg(LOG_BLUE, "wizsh, curTime: %lld", seconds.seconds);
seconds.missedUpdates = 0;
updated = true;
coloredMsg(LOG_BLUE, "wizsh, curTime: %lu", seconds.seconds);
break;
}
}
if (! updated) {
coloredMsg(LOG_BLUE, "wizsh, time update failed");
seconds.missedUpdates += 1;
}
} else {
seconds.missedUpdates += 1;
coloredMsg(LOG_BLUE, "wizsh, error when querying ntp server name");