From dfce60e01f86f9be918a786ca729d5fa1d8fd038 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 16 Feb 2021 11:01:01 +0100 Subject: [PATCH] network abstraction, time handling --- cube/User/Inc/networkAbstractionLayer.h | 8 +++- cube/User/Inc/wizHelper.h | 8 +--- cube/User/Src/counter.c | 4 +- cube/User/Src/networkAbstractionLayer.c | 50 ++++++++++++++++++- cube/User/Src/wizHelper.c | 64 +++++-------------------- 5 files changed, 73 insertions(+), 61 deletions(-) diff --git a/cube/User/Inc/networkAbstractionLayer.h b/cube/User/Inc/networkAbstractionLayer.h index e11e821..fc5611b 100644 --- a/cube/User/Inc/networkAbstractionLayer.h +++ b/cube/User/Inc/networkAbstractionLayer.h @@ -1,9 +1,15 @@ #ifndef _NETWORK_ABSTRACTION_LAYER_H_ #define _NETWORK_ABSTRACTION_LAYER_H_ +#include +#include +typedef struct { + uint64_t seconds; + bool valid; +} t_seconds; void networkInit(); - +t_seconds* networkGetSeconds(); #endif /* _NETWORK_ABSTRACTION_LAYER_H_ */ diff --git a/cube/User/Inc/wizHelper.h b/cube/User/Inc/wizHelper.h index 5d3f4f6..80abe4f 100644 --- a/cube/User/Inc/wizHelper.h +++ b/cube/User/Inc/wizHelper.h @@ -5,18 +5,12 @@ #include -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_ diff --git a/cube/User/Src/counter.c b/cube/User/Src/counter.c index c833d40..edcb875 100644 --- a/cube/User/Src/counter.c +++ b/cube/User/Src/counter.c @@ -13,6 +13,8 @@ #include #include #include +#include + 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; } diff --git a/cube/User/Src/networkAbstractionLayer.c b/cube/User/Src/networkAbstractionLayer.c index 42bbe01..0be10f4 100644 --- a/cube/User/Src/networkAbstractionLayer.c +++ b/cube/User/Src/networkAbstractionLayer.c @@ -1,8 +1,56 @@ #include +#include +#include +#include #include +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(); -} \ No newline at end of file + schAdd(networkSecondsHandler, NULL, 0, 1000); +} diff --git a/cube/User/Src/wizHelper.c b/cube/User/Src/wizHelper.c index a43438d..2face62 100644 --- a/cube/User/Src/wizHelper.c +++ b/cube/User/Src/wizHelper.c @@ -13,7 +13,6 @@ #include #include #include -#include 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; }