diff --git a/cube/User/Src/main2.c b/cube/User/Src/main2.c index c33c7fa..ccad62a 100644 --- a/cube/User/Src/main2.c +++ b/cube/User/Src/main2.c @@ -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); } diff --git a/cube/User/Src/wizHelper.c b/cube/User/Src/wizHelper.c index d0c9b94..03fe735 100644 --- a/cube/User/Src/wizHelper.c +++ b/cube/User/Src/wizHelper.c @@ -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");