network abstraction, time handling
This commit is contained in:
parent
bfbcc309df
commit
dfce60e01f
@ -1,9 +1,15 @@
|
||||
#ifndef _NETWORK_ABSTRACTION_LAYER_H_
|
||||
#define _NETWORK_ABSTRACTION_LAYER_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct {
|
||||
uint64_t seconds;
|
||||
bool valid;
|
||||
} t_seconds;
|
||||
|
||||
void networkInit();
|
||||
|
||||
t_seconds* networkGetSeconds();
|
||||
|
||||
#endif /* _NETWORK_ABSTRACTION_LAYER_H_ */
|
||||
|
@ -5,18 +5,12 @@
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
typedef struct {
|
||||
uint64_t seconds;
|
||||
uint32_t missedUpdates;
|
||||
bool valid;
|
||||
} t_seconds;
|
||||
|
||||
int wizInit();
|
||||
bool isNetworkAvailable();
|
||||
uint8_t* wizGetIPAddress();
|
||||
bool wizDnsQuery(char *name, uint8_t *ip);
|
||||
|
||||
t_seconds* wizGetSeconds();
|
||||
uint64_t wizSntpQuery();
|
||||
|
||||
|
||||
#endif // _WIZHELPER_H_
|
||||
|
@ -13,6 +13,8 @@
|
||||
#include <sinkStruct.h>
|
||||
#include <sha256.h>
|
||||
#include <eeprom.h>
|
||||
#include <networkAbstractionLayer.h>
|
||||
|
||||
|
||||
|
||||
const uint32_t COUNTER_FREQUENCY = 1.0e6;
|
||||
@ -198,7 +200,7 @@ void mainsCntsInputCaptureCallback(TIM_HandleTypeDef *htim) {
|
||||
void counterInit() {
|
||||
deviceStats = getGlobalDeviceStats();
|
||||
|
||||
seconds = wizGetSeconds();
|
||||
seconds = networkGetSeconds();
|
||||
for (uint8_t i = 0; i < NUM_OF_MINUTE_BUFFERS; i++) {
|
||||
minuteBufferReady[i] = false;
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -13,7 +13,6 @@
|
||||
#include <dns.h>
|
||||
#include <sntp.h>
|
||||
#include <config.h>
|
||||
#include <iwdg.h>
|
||||
|
||||
|
||||
static t_configBlock *config;
|
||||
@ -28,7 +27,6 @@ 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;
|
||||
|
||||
|
||||
@ -135,11 +133,13 @@ bool wizDnsQuery(char *name, uint8_t *ip) {
|
||||
}
|
||||
|
||||
|
||||
static void wizSNTPHandler(void *handle) {
|
||||
uint64_t wizSntpQuery() {
|
||||
bool success = false;
|
||||
uint64_t seconds = 0;
|
||||
static uint32_t missedUpdates = 0;
|
||||
|
||||
if (networkAvailable) {
|
||||
coloredMsg(LOG_BLUE, "wizsh, about to call SNTP");
|
||||
coloredMsg(LOG_BLUE, "wizsq, about to call SNTP");
|
||||
|
||||
uint8_t ntpServer[4];
|
||||
if (wizDnsQuery(config->ntpServer, ntpServer)) {
|
||||
@ -148,48 +148,27 @@ static void wizSNTPHandler(void *handle) {
|
||||
for (uint8_t i = 0; i < 16; i++) {
|
||||
datetime curTime;
|
||||
int8_t res = SNTP_run(&curTime);
|
||||
uint64_t receivedSeconds = curTime.seconds - UNIX_NTP_EPOCH_DIFF;
|
||||
if (res == 1) {
|
||||
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 = receivedSeconds;
|
||||
seconds.valid = true;
|
||||
seconds.missedUpdates = 0;
|
||||
seconds = curTime.seconds - UNIX_NTP_EPOCH_DIFF;
|
||||
missedUpdates = 0;
|
||||
updated = true;
|
||||
success = true;
|
||||
coloredMsg(LOG_BLUE, "wizsh, curTime: %lu", seconds.seconds);
|
||||
|
||||
coloredMsg(LOG_BLUE, "wizsq, curTime: %lu", seconds);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (! updated) {
|
||||
coloredMsg(LOG_BLUE, "wizsh, time update failed");
|
||||
seconds.missedUpdates += 1;
|
||||
coloredMsg(LOG_BLUE, "wizsq, time update failed");
|
||||
missedUpdates += 1;
|
||||
}
|
||||
} else {
|
||||
seconds.missedUpdates += 1;
|
||||
coloredMsg(LOG_BLUE, "wizsh, error when querying ntp server name");
|
||||
coloredMsg(LOG_BLUE, "wizsq, error when querying ntp server name");
|
||||
missedUpdates += 1;
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t tryAgainIn = (success) ? 3600 : 1;
|
||||
schAdd(wizSNTPHandler, NULL, tryAgainIn * 1000, 0);
|
||||
coloredMsg(LOG_BLUE, "wizsh, next sntp request in %d seconds", tryAgainIn);
|
||||
}
|
||||
|
||||
static void wizSecondsHandler(void *handle) {
|
||||
seconds.seconds += 1;
|
||||
HAL_IWDG_Refresh(&hiwdg);
|
||||
}
|
||||
|
||||
t_seconds* wizGetSeconds() {
|
||||
return &seconds;
|
||||
|
||||
return seconds;
|
||||
}
|
||||
|
||||
static void wizPhyLinkHandler(void *handle) {
|
||||
@ -198,7 +177,6 @@ static void wizPhyLinkHandler(void *handle) {
|
||||
|
||||
static uint8_t lastStablePhyLink = 255;
|
||||
static bool dhcpInitialized = false;
|
||||
static bool sntpInitialized = false;
|
||||
|
||||
uint8_t phyLink = 0;
|
||||
int8_t res = ctlwizchip(CW_GET_PHYLINK, (void*) &phyLink);
|
||||
@ -218,12 +196,6 @@ static void wizPhyLinkHandler(void *handle) {
|
||||
coloredMsg(LOG_BLUE, "wizplh, DHCP handler scheduled");
|
||||
|
||||
dhcpInitialized = true;
|
||||
|
||||
|
||||
schAdd(wizSNTPHandler, NULL, 15, 0);
|
||||
coloredMsg(LOG_BLUE, "wizplh, SNTP handler scheduled");
|
||||
|
||||
sntpInitialized = true;
|
||||
} else {
|
||||
networkAvailable = false;
|
||||
show(LED_GREEN, BLINK);
|
||||
@ -239,14 +211,6 @@ static void wizPhyLinkHandler(void *handle) {
|
||||
|
||||
dhcpInitialized = false;
|
||||
}
|
||||
|
||||
if (sntpInitialized) {
|
||||
schDel(wizSNTPHandler, NULL);
|
||||
coloredMsg(LOG_BLUE, "wizplh, SNTP handler unscheduled");
|
||||
seconds.valid = false;
|
||||
|
||||
sntpInitialized = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -304,7 +268,5 @@ int wizInit() {
|
||||
schAdd(wizPhyLinkHandler, NULL, 0, 1000);
|
||||
coloredMsg(LOG_BLUE, "wizI, PhyLink handler scheduled");
|
||||
|
||||
schAdd(wizSecondsHandler, NULL, 0, 1000);
|
||||
coloredMsg(LOG_BLUE, "wizI, Seconds handler scheduled");
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user