time
This commit is contained in:
@ -30,7 +30,7 @@ void my_errorHandler() {
|
|||||||
|
|
||||||
void second_tick(void *handle) {
|
void second_tick(void *handle) {
|
||||||
t_seconds *seconds = wizGetSeconds();
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -29,6 +29,8 @@ static uint8_t dnsBuffer[DNS_BUFFER_SIZE];
|
|||||||
|
|
||||||
static uint8_t sntpBuffer[MAX_SNTP_BUF_SIZE];
|
static uint8_t sntpBuffer[MAX_SNTP_BUF_SIZE];
|
||||||
static t_seconds seconds = { .seconds = 0, .missedUpdates = 0, .valid = false };
|
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 DHCP_SOCK;
|
||||||
extern const uint8_t DNS_SOCK;
|
extern const uint8_t DNS_SOCK;
|
||||||
@ -140,21 +142,32 @@ static void wizSNTPHandler(void *handle) {
|
|||||||
uint8_t ntpServer[4];
|
uint8_t ntpServer[4];
|
||||||
if (wizDnsQuery(NTP_SERVER, ntpServer)) {
|
if (wizDnsQuery(NTP_SERVER, ntpServer)) {
|
||||||
SNTP_init(SNTP_SOCK, ntpServer, 0, sntpBuffer);
|
SNTP_init(SNTP_SOCK, ntpServer, 0, sntpBuffer);
|
||||||
|
bool updated = false;
|
||||||
for (uint8_t i = 0; i < 16; i++) {
|
for (uint8_t i = 0; i < 16; i++) {
|
||||||
datetime curTime;
|
datetime curTime;
|
||||||
int8_t res = SNTP_run(&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 (res == 1) {
|
||||||
if (seconds.seconds != curTime.seconds) {
|
if (seconds.seconds != receivedSeconds) {
|
||||||
coloredMsg(LOG_BLUE, "wizsh, time deviation: %lld %lld",
|
coloredMsg(LOG_BLUE, "wizsh, time deviation: %lu %lu",
|
||||||
seconds.seconds, curTime.seconds);
|
seconds.seconds, receivedSeconds);
|
||||||
|
} else {
|
||||||
|
coloredMsg(LOG_BLUE, "wizsh, time still matching");
|
||||||
}
|
}
|
||||||
seconds.seconds = curTime.seconds;
|
|
||||||
|
seconds.seconds = receivedSeconds;
|
||||||
seconds.valid = true;
|
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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (! updated) {
|
||||||
|
coloredMsg(LOG_BLUE, "wizsh, time update failed");
|
||||||
|
seconds.missedUpdates += 1;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
seconds.missedUpdates += 1;
|
seconds.missedUpdates += 1;
|
||||||
coloredMsg(LOG_BLUE, "wizsh, error when querying ntp server name");
|
coloredMsg(LOG_BLUE, "wizsh, error when querying ntp server name");
|
||||||
|
Reference in New Issue
Block a user