refactor dhcp handling

This commit is contained in:
Wolfgang Hottgenroth 2020-11-10 14:26:01 +01:00
parent 60ad31bf5e
commit 6f4cc803be
Signed by: wn
GPG Key ID: 6C1E5E531E0D5D7F
2 changed files with 33 additions and 24 deletions

View File

@ -190,11 +190,11 @@ void my_setup_2() {
wizInit();
frontendInit();
frontendSetThreshold(240);
// frontendInit();
// frontendSetThreshold(240);
schAdd(scheduleMBusRequest, NULL, 0, 1000);
schAdd(triggerMBusRequest, NULL, 0, 100);
// schAdd(scheduleMBusRequest, NULL, 0, 1000);
// schAdd(triggerMBusRequest, NULL, 0, 100);
}
void my_loop() {

View File

@ -78,39 +78,48 @@ static void wizDHCPUpdate() {
}
static void wizDHCPHandler(void *handle) {
static uint8_t lastStablePhyLink = 255;
coloredLog(LOG_RED, "wizdh");
static uint8_t lastDhcpRes = 255;
static bool dhcpInitialized = false;
uint8_t res = DHCP_run();
if (lastDhcpRes != res) {
coloredMsg(LOG_RED, "wizdh, dhcp state has changed: %d", res);
lastDhcpRes = res;
}
}
static void wizPhyLinkHandler(void *handle) {
static uint8_t lastStablePhyLink = 255;
uint8_t phyLink = 0;
int8_t res = ctlwizchip(CW_GET_PHYLINK, (void*) &phyLink);
if (lastStablePhyLink != phyLink) {
coloredMsg(LOG_RED, "wizdh, ctlwizchip returns %d, phy link changed to %d", res, phyLink);
coloredMsg(LOG_RED, "wizplh, ctlwizchip returns %d, phy link changed to %d", res, phyLink);
lastStablePhyLink = phyLink;
wiz_PhyConf wpc;
wizphy_getphyconf(&wpc);
coloredMsg(LOG_RED, "wizdh, wpc: b:%02x, m:%02x, s:%02x, d:%02x",
wpc.by, wpc.mode, wpc.speed, wpc.duplex);
} else if (phyLink == PHY_LINK_ON) {
if (! dhcpInitialized) {
if (phyLink == PHY_LINK_ON) {
// start DHCP handler
memset(dhcpBuffer, 0, DHCP_BUFFER_SIZE);
reg_dhcp_cbfunc(wizDHCPAssign, wizDHCPUpdate, NULL);
DHCP_init(DHCP_SOCK, dhcpBuffer);
coloredMsg(LOG_RED, "wizdh, DHCP initialized");
dhcpInitialized = true;
}
coloredMsg(LOG_RED, "wizplh, DHCP initialized");
uint8_t res = DHCP_run();
// run the dhcp handler the first time after 1s and then every 100ms
schAdd(wizDHCPHandler, NULL, 1000, 1000);
coloredMsg(LOG_RED, "wizplh, DHCP handler scheduled");
} else {
// stop DHCP handler
DHCP_stop();
coloredMsg(LOG_RED, "wizplh, DHCP stopped");
if (lastDhcpRes != res) {
coloredMsg(LOG_RED, "wizdh, dhcp state has changed: %d", res);
lastDhcpRes = res;
schDel(wizDHCPHandler, NULL);
coloredMsg(LOG_RED, "wizplh, DHCP handler unscheduled");
}
}
}
int wizInit() {
coloredMsg(LOG_RED, "wizI, resetting Ethernet module");
wizReset(true);
@ -155,8 +164,8 @@ int wizInit() {
coloredMsg(LOG_RED, "wizI, CW_GET_ID: %d %02x %02x %02x %02x %02x %02x", res, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
schAdd(wizDHCPHandler, NULL, 0, 1000);
coloredMsg(LOG_RED, "wizI, DHCP handler scheduled");
schAdd(wizPhyLinkHandler, NULL, 0, 1000);
coloredMsg(LOG_RED, "wizI, PhyLink handler scheduled");
return 0;
}