This commit is contained in:
2021-02-07 18:35:30 +01:00
parent 6f4c8b5385
commit 198edaba6d
4 changed files with 37 additions and 12 deletions

View File

@ -7,9 +7,18 @@
#define NTP_SERVER "0.de.pool.ntp.org"
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();
#endif // _WIZHELPER_H_

View File

@ -29,7 +29,8 @@ void my_errorHandler() {
}
void second_tick(void *handle) {
coloredMsg(LOG_GREEN, "Tick");
t_seconds *seconds = wizGetSeconds();
coloredMsg(LOG_GREEN, "Tick %d %ld %lld", seconds->valid, seconds->missedUpdates, seconds->seconds);
}

View File

@ -28,7 +28,7 @@ static uint8_t dnsBuffer[DNS_BUFFER_SIZE];
static uint8_t sntpBuffer[MAX_SNTP_BUF_SIZE];
static datetime curTime;
static t_seconds seconds = { .seconds = 0, .missedUpdates = 0, .valid = false };
extern const uint8_t DHCP_SOCK;
extern const uint8_t DNS_SOCK;
@ -134,7 +134,6 @@ bool wizDnsQuery(char *name, uint8_t *ip) {
static void wizSNTPHandler(void *handle) {
if (networkAvailable) {
coloredMsg(LOG_BLUE, "wizsh, about to call SNTP");
@ -142,22 +141,36 @@ static void wizSNTPHandler(void *handle) {
if (wizDnsQuery(NTP_SERVER, ntpServer)) {
SNTP_init(SNTP_SOCK, ntpServer, 0, sntpBuffer);
for (uint8_t i = 0; i < 16; i++) {
int8_t res = SNTP_run(&curTime);
coloredMsg(LOG_BLUE, "wizsh, res: %d", res);
if (res == 1) {
coloredMsg(LOG_BLUE, "wizsh, curTime: %04d-%02d-%02d %02d:%02d:%02d",
curTime.yy, curTime.mo, curTime.dd,
curTime.hh, curTime.mm, curTime.ss);
break;
}
datetime curTime;
int8_t res = SNTP_run(&curTime);
coloredMsg(LOG_BLUE, "wizsh, res: %d", res);
if (res == 1) {
if (seconds.seconds != curTime.seconds) {
coloredMsg(LOG_BLUE, "wizsh, time deviation: %lld %lld",
seconds.seconds, curTime.seconds);
}
seconds.seconds = curTime.seconds;
seconds.valid = true;
coloredMsg(LOG_BLUE, "wizsh, curTime: %lld", seconds.seconds);
break;
}
}
} else {
seconds.missedUpdates += 1;
coloredMsg(LOG_BLUE, "wizsh, error when querying ntp server name");
}
}
}
static void wizSecondsHandler(void *handle) {
seconds.seconds += 1;
}
t_seconds* wizGetSeconds() {
return &seconds;
}
static void wizPhyLinkHandler(void *handle) {
// this handler is anyhow called with a 1s period, so we reuse it for the DNS timer
@ -270,5 +283,7 @@ 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;
}