mains-frequency-counter-stm32/cube/User/Src/networkAbstractionLayer.c

57 lines
1.6 KiB
C
Raw Normal View History

2021-02-16 10:37:09 +01:00
#include <networkAbstractionLayer.h>
2021-02-16 11:01:01 +01:00
#include <PontCoopScheduler.h>
#include <iwdg.h>
#include <logger.h>
2021-02-16 10:37:09 +01:00
#include <wizHelper.h>
2021-02-16 11:01:01 +01:00
static t_seconds seconds = { .seconds = 0, .valid = false };
static void networkSecondsHandler(void *handle) {
static bool tryAgain = false;
seconds.seconds += 1;
if (! seconds.valid) {
coloredMsg(LOG_YELLOW, "nsh, initially querying time");
uint64_t tmpSeconds = wizSntpQuery();
if (tmpSeconds != 0) {
coloredMsg(LOG_YELLOW, "nsh, success, time is %lu", tmpSeconds);
seconds.seconds = tmpSeconds;
seconds.valid = true;
} else {
coloredMsg(LOG_YELLOW, "nsh, failed");
}
} else if (tryAgain || ((seconds.seconds % 3600) == 0)) {
coloredMsg(LOG_YELLOW, "nsh, periodically querying time");
uint64_t tmpSeconds = wizSntpQuery();
if (tmpSeconds != 0) {
coloredMsg(LOG_YELLOW, "nsh, success, network time is %lu", tmpSeconds);
tryAgain = false;
if (seconds.seconds != tmpSeconds) {
coloredMsg(LOG_YELLOW, "nsh, local time updated");
seconds.seconds = tmpSeconds;
} else {
coloredMsg(LOG_YELLOW, "nsh, local time still in sync");
}
} else {
coloredMsg(LOG_YELLOW, "nsh, failed, trying again ...");
tryAgain = true;
}
}
HAL_IWDG_Refresh(&hiwdg);
}
t_seconds* networkGetSeconds() {
return &seconds;
}
2021-02-16 10:37:09 +01:00
void networkInit() {
wizInit();
2021-02-16 11:01:01 +01:00
schAdd(networkSecondsHandler, NULL, 0, 1000);
}