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