8 years, 9 months ago.

Destroy and re-init Ethernet interface

Hi,

how can I close down the EthernetInterface and start it again?

The following example doesn't work. It crashes when it reaches init() for the second time.

Ethernet crash on reinit

#include "mbed.h"
#include "EthernetInterface.h"

void testEthernet()
{
    EthernetInterface eth;
    printf("Initializing...\n");
    eth.init(); //Use DHCP
    printf("Connecting...\n");
    int rc = eth.connect();
    if (rc == 0)
        printf("\nClient IP Address is %s \n", eth.getIPAddress());
    eth.disconnect();
}

int main() {
    while (1)
    {
        testEthernet();
        wait(1);
    }
}

Question relating to:

1 Answer

8 years, 9 months ago.

Hi Tobias,

Here's my standard pattern, which is obviously a different pattern that your test scenario. I have a slightly customized version of the EthernetInterface, but if you just ignore those calls, it should work for you. I guess I haven't explicitly tested this in a while, but when I did, you could start with or without the cable, and also plug/unplug while running. Some of the applications that used Ethernet were not so kind - if they were in a state to expect something. Well behaved apps return after a timeout.

    if (0 == eth.init()) {  //Use DHCP
        pc.printf("Name: %s\r\n", nn);
        eth.setName(nn);
        do {
            pc.printf("Connecting to network...\r\n");
            if (0 == eth.connect()) {
                bool initFlag = true;
                linkup = true;
                ShowIPAddress(true);
                int speed = eth.get_connection_speed();
                pc.printf("Connected at %d Mb/s\r\n", speed);
                while (eth.is_connected()) {
                    if (initFlag) {
                        SyncToNTPServer();
                    }
                    initFlag = false;
                    wd.Service();
                    Thread::wait(50);
                }
                linkup = false;
                pc.printf("lost connection.\r\n");
                ShowIPAddress(false);
                eth.disconnect();
            } else {
                pc.printf("  ... failed to connect.\r\n");
            }
        } while (1);
    } else {
        pc.printf("  ... failed to initialize, rebooting...\r\n");
        mbed_reset();
    }

Note: I'm using this on the LPC1768, and I see you have one of the other devices.

Hi David, thanks for sharing your code!

But if I understand your snippet correctly, you never call eth.init() a second time. So for example changing the IP Address dynamically also isn't possible with your code, right?

posted by Tobias Wellnitz 08 Feb 2016

That is correct, I don't call it twice. Under DHCP I believe it will work the renewal as the lease time expires. I hadn't thought about forcing a change in IP. I thought routers retained the MAC and then generally grant the same IP on renewal - but this is a "learned" understanding, not necessarily the standard behavior.

posted by David Smart 08 Feb 2016