Stopping Websocket Update when Ethernet disconnects
Posted: Fri Nov 12, 2021 12:52 pm
So I'm unfortunately still stuck on this problem but I think I've narrowed it down. Here's the code for my websocket update task and the function that handles starting and stopping DHCP:
The issue I'm having is that I'm getting a Debug Interrupt(12) trap about 5 minutes after when the websocket was loaded and then the ethernet cable unplugged. I don't get this trap when I comment out the SendConfigReport() function, which is pretty much exactly what's in the websocket example. So I assume the buffer being over ran (if that is in fact what the trap is pointing towards) is located there. However I've monitored the buffers in that function and didn't notice anything out of the ordinary until SendConfigReport() stops being called since EtherLink() went false and/or DHCP was unassigned.
If I reconnect the ethernet before the trap happens DHCP reconnects and everything continues operating correctly.
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.