network abstraction, time handling
This commit is contained in:
@ -1,8 +1,56 @@
|
||||
#include <networkAbstractionLayer.h>
|
||||
#include <PontCoopScheduler.h>
|
||||
#include <iwdg.h>
|
||||
#include <logger.h>
|
||||
|
||||
#include <wizHelper.h>
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
void networkInit() {
|
||||
wizInit();
|
||||
}
|
||||
schAdd(networkSecondsHandler, NULL, 0, 1000);
|
||||
}
|
||||
|
Reference in New Issue
Block a user