This commit is contained in:
2021-02-28 12:35:43 +01:00
parent 3f024510ae
commit 1b1180a72f
3 changed files with 42 additions and 40 deletions

View File

@ -10,7 +10,6 @@ int wizInit();
bool isNetworkAvailable();
uint8_t* wizGetIPAddress();
bool wizDnsQuery(char *name, uint8_t *ip);
uint64_t wizSntpQuery();
#endif // _WIZHELPER_H_

View File

@ -1,14 +1,54 @@
#include <networkAbstractionLayer_impl.h>
#include <logger.h>
#include <PontCoopScheduler.h>
#include <wizHelper.h>
#include <wizchip_conf.h>
#include <socket.h>
#include <config.h>
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();
}

View File

@ -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();