diff --git a/cube/User/Inc/wizHelper.h b/cube/User/Inc/wizHelper.h index 80abe4f..bcd7a4b 100644 --- a/cube/User/Inc/wizHelper.h +++ b/cube/User/Inc/wizHelper.h @@ -10,7 +10,6 @@ int wizInit(); bool isNetworkAvailable(); uint8_t* wizGetIPAddress(); bool wizDnsQuery(char *name, uint8_t *ip); -uint64_t wizSntpQuery(); #endif // _WIZHELPER_H_ diff --git a/cube/User/Src/networkAbstractionLayer_lan.c b/cube/User/Src/networkAbstractionLayer_lan.c index c1908b4..fe971c7 100644 --- a/cube/User/Src/networkAbstractionLayer_lan.c +++ b/cube/User/Src/networkAbstractionLayer_lan.c @@ -1,14 +1,54 @@ #include #include +#include #include #include #include +#include +static t_configBlock *config; + + + +static uint8_t sntpBuffer[MAX_SNTP_BUF_SIZE]; +const uint64_t UNIX_NTP_EPOCH_DIFF = 2208988800; +extern const uint8_t SNTP_SOCK; + uint64_t networkSntpQuery() { - return wizSntpQuery(); + uint64_t seconds = 0; + + if (isNetworkAvailable()) { + coloredMsg(LOG_BLUE, "wizsq, about to call SNTP"); + + uint8_t ntpServer[4]; + if (wizDnsQuery(config->ntpServer, ntpServer)) { + SNTP_init(SNTP_SOCK, ntpServer, 0, sntpBuffer); + uint16_t cycles = 0; + uint32_t startTime = HAL_GetTick(); + while (1) { + cycles += 1; + datetime curTime; + if (1 == SNTP_run(&curTime)) { + seconds = curTime.seconds - UNIX_NTP_EPOCH_DIFF; + coloredMsg(LOG_BLUE, "wizsq, cycles: %u, curTime: %lu", cycles, seconds); + uint32_t stopTime = HAL_GetTick(); + coloredMsg(LOG_BLUE, "wizsq, duration: %u ms", stopTime - startTime); + break; + } + } + + if (seconds == 0) { + coloredMsg(LOG_BLUE, "wizsq, time update failed"); + } + } else { + coloredMsg(LOG_BLUE, "wizsq, error when querying ntp server name"); + } + } + + return seconds; } @@ -39,5 +79,6 @@ int8_t networkUdpSend(char *hostname, uint16_t port, uint8_t *buf, uint16_t bufL } void networkImplInit() { + config = getConfig(); wizInit(); } diff --git a/cube/User/Src/wizHelper.c b/cube/User/Src/wizHelper.c index cee477e..1b9292a 100644 --- a/cube/User/Src/wizHelper.c +++ b/cube/User/Src/wizHelper.c @@ -26,13 +26,9 @@ static uint8_t dhcpBuffer[DHCP_BUFFER_SIZE]; static uint8_t dnsBuffer[DNS_BUFFER_SIZE]; -static uint8_t sntpBuffer[MAX_SNTP_BUF_SIZE]; -const uint64_t UNIX_NTP_EPOCH_DIFF = 2208988800; - extern const uint8_t DHCP_SOCK; extern const uint8_t DNS_SOCK; -extern const uint8_t SNTP_SOCK; static bool networkAvailable = false; @@ -133,40 +129,6 @@ bool wizDnsQuery(char *name, uint8_t *ip) { } -uint64_t wizSntpQuery() { - uint64_t seconds = 0; - - if (networkAvailable) { - coloredMsg(LOG_BLUE, "wizsq, about to call SNTP"); - - uint8_t ntpServer[4]; - if (wizDnsQuery(config->ntpServer, ntpServer)) { - SNTP_init(SNTP_SOCK, ntpServer, 0, sntpBuffer); - uint16_t cycles = 0; - uint32_t startTime = HAL_GetTick(); - while (1) { - cycles += 1; - datetime curTime; - if (1 == SNTP_run(&curTime)) { - seconds = curTime.seconds - UNIX_NTP_EPOCH_DIFF; - coloredMsg(LOG_BLUE, "wizsq, cycles: %u, curTime: %lu", cycles, seconds); - uint32_t stopTime = HAL_GetTick(); - coloredMsg(LOG_BLUE, "wizsq, duration: %u ms", stopTime - startTime); - break; - } - } - - if (seconds == 0) { - coloredMsg(LOG_BLUE, "wizsq, time update failed"); - } - } else { - coloredMsg(LOG_BLUE, "wizsq, error when querying ntp server name"); - } - } - - return seconds; -} - static void wizPhyLinkHandler(void *handle) { // this handler is anyhow called with a 1s period, so we reuse it for the DNS timer DNS_time_handler();