Code: Select all
void ToggleDHCP(bool enable)
{
int interface = GetFirstInterface();
InterfaceBlock *dhcpIntf = GetInterFaceBlock(interface);
DhcpObject *dhcpObj = dhcpIntf->dhcpClient;
if(dhcpObj == NULL)
{
iprintf("Create a dhcp object since one doesn't exist\n");
dhcpObj = new DhcpObject(interface);
}
if(enable)
{
iprintf("enabling DHCP\n");
dhcpObj->StartDHCP();
AssignedDHCP = true;
iprintf("dhcpstate: %d\n", dhcpObj->GetDHCPState());
}
else
{
iprintf("disabling DHCP\n");
dhcpObj->StopDHCP();
AssignedDHCP = false;
iprintf("dhcpstate: %d\n", dhcpObj->GetDHCPState());
}
if ( OSSemPend( &( dhcpObj->NotifySem ), 10 * TICKS_PER_SECOND ) == OS_TIMEOUT ) //Wait 10 seconds
{
iprintf("\r\n\r\n*** WARNING ***\r\n");
iprintf("IP Address was set to 0.0.0.0, and a DHCP server could not be found.\r\n");
iprintf("Device does not have a valid IP address.\r\n\r\n");
}
else
{
ShowDhcpSettings(interface);
}
}
void WSTask(void *pd)
{
InterfaceBlock *dhcpIntf = GetInterFaceBlock(GetFirstInterface());
DhcpObject *dhcpObj = dhcpIntf->dhcpClient;
while(1)
{
OSTimeDly(6);
// Send data up to the web interface via the websockets
for(int i = 0; i < WS_MAX_SOCKS; i++)
{
if(EtherLink())
{
if(!AssignedDHCP)
{
// iprintf("Ethernet connected and no DHCP assigned\n");
iprintf("Start DHCP\n");
ToggleDHCP(true);
}
else if(AssignedDHCP)
{
if(ws_fd[i] > 0) // We only need to update the websocket if the website is loaded
{
// iprintf("Update the Websocket\n");
/* This is the websocket update function */
if(dhcpObj->GetDHCPState() == SDHCP_CMPL && dhcpObj->GetDHCPState() != SDHCP_NOTSTARTED)
{
// check ethernet connection again
if(EtherLink()) SendConfigReport(ws_fd[i]);
iprintf("SendConfigReport\n");
}
}
}
}
else
{
if(AssignedDHCP)
{
iprintf("Ethernet disconnected and DHCP is assigned\n");
iprintf("stop dhcp\n");
ToggleDHCP(false);
}
}
}
}
}
If I reconnect the ethernet before the trap happens DHCP reconnects and everything continues operating correctly.