This commit is contained in:
2021-02-07 13:47:34 +01:00
parent c691620ff7
commit a5f20fc073
7 changed files with 204 additions and 46 deletions

View File

@ -5,6 +5,8 @@
#include <stdint.h>
#define NTP_SERVER "0.pool.ntp.org"
int wizInit();
bool isNetworkAvailable();
uint8_t* wizGetIPAddress();

View File

@ -5,7 +5,6 @@
// on the W5500 there are eight ports available
const uint8_t DHCP_SOCK = 0;
const uint8_t MQTT_SOCK = 1;
const uint8_t SNTP_SOCK = 1;
const uint8_t CMD_SOCK = 2;
const uint8_t SYSLOG_SOCK = 3;
const uint8_t DNS_SOCK = 4;
const uint8_t DNS_SOCK = 3;

View File

@ -11,6 +11,7 @@
#include <dhcp.h>
#include <show.h>
#include <dns.h>
#include <sntp.h>
#include <config.h>
@ -25,8 +26,14 @@ static uint8_t dhcpBuffer[DHCP_BUFFER_SIZE];
#define DNS_BUFFER_SIZE MAX_DNS_BUF_SIZE
static uint8_t dnsBuffer[DNS_BUFFER_SIZE];
static uint8_t sntpBuffer[MAX_SNTP_BUF_SIZE];
static datetime curTime;
extern const uint8_t DHCP_SOCK;
extern const uint8_t DNS_SOCK;
extern const uint8_t SNTP_SOCK;
static bool networkAvailable = false;
@ -125,12 +132,32 @@ bool wizDnsQuery(char *name, uint8_t *ip) {
return retCode;
}
static void wizSNTPHandler(void *handle) {
if (networkAvailable) {
coloredMsg(LOG_BLUE, "wizsh, about to call SNTP");
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);
} else {
coloredMsg(LOG_BLUE, "wizsh, error when requesting time");
}
}
}
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();
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);
@ -150,6 +177,13 @@ static void wizPhyLinkHandler(void *handle) {
coloredMsg(LOG_BLUE, "wizplh, DHCP handler scheduled");
dhcpInitialized = true;
SNTP_init(SNTP_SOCK, NTP_SERVER, 0, sntpBuffer);
schAdd(wizSNTPHandler, NULL, 0, 60*1000);
coloredMsg(LOG_BLUE, "wizplh, SNTP handler scheduled");
sntpInitialized = true;
} else {
networkAvailable = false;
show(LED_GREEN, BLINK);
@ -165,6 +199,13 @@ static void wizPhyLinkHandler(void *handle) {
dhcpInitialized = false;
}
if (sntpInitialized) {
schDel(wizSNTPHandler, NULL);
coloredMsg(LOG_BLUE, "wizplh, SNTP handler unscheduled");
sntpInitialized = false;
}
}
}
}