I used the example on pg 27 of NNDKprogman.pdf as the basis. What happens is, I boot up (NANO54415) in DHCP. All is fine. I have the board setup to send broadcast UDP datagrams twice a second. I send a command to the board to switch to static ip. It will do it, and I see the datagrams are now coming from the static ip address. Where is crashes is trying to get back to DHCP. When I command it to DHCP mode, it hangs forever somewhere. The opposite also works one time. That is, I can boot up into Static IP mode, send a command to switch to DHCP, which works, then send another command to switch back to static, and it hangs again. A reboot will always go back to the last successfully commanded mode.
In the code below, a command to switch modes generates a call to RedoEthernet(), which is supposed to do it all.
I'm using v2.7.1 IDE.
Code: Select all
void RedoEthernet()
{
// close all tcp/ip connections.
close(listen_fd);
for ( int i=FIRST_TCP_LINK; i<TOTAL_PROTOCOL_LINKS; i++ )
{
if (mandc[i].port == -1) continue;
close(mandc[i].port);
mandc[i].port = -1;
}
EnableDHCP(false, 0);
KillStack();
if ( SS->use_dhcp ) {
InitializeStack();
EnableDHCP( true, 5 );
}
else {
InitializeStack( SS->ip_address, SS->ip_mask, SS->ip_gateway );
}
// Listen for new tcp/ip connections.
listen_fd = listen(INADDR_ANY, TCPIP_PORT, 5);
}
void EnableDHCP(BOOL enable, int wait_seconds)
{
static int ethernet = GetFirstInterface();
static DhcpObject *dhcp_client = new DhcpObject(ethernet);
if (enable) dhcp_client->StartDHCP();
else dhcp_client->StopDHCP();
if (enable && wait_seconds)
{
OSSemPend(&dhcp_client->NotifySem, wait_seconds*TICKS_PER_SECOND); // if (return==OS_TIMEOUT) then it failed.
}
}