DCC-EX / CommandStation-EX

EX-CommandStation firmware from DCC-EX. Includes support for WiFi and a standalone WiThrottle server. A complete re-write of the original DCC++.
https://dcc-ex.github.io/
GNU General Public License v3.0
154 stars 104 forks source link

Keep Ethernet singleton "alive" until connection is established. #256

Closed bcsanches closed 1 year ago

bcsanches commented 1 year ago

Instead of keeping a limited loop during Ethernet setup, I propose this change to keep checking the link status until it becomes stable and ethernet link is stable and ip is obtained.

This makes life easier for debugging and testing, also if for some reason the link takes longer to becomes stable, the initialization process will not kill the ehternet singleton.

habazut commented 1 year ago

This is a very good idea. I think it should look someting like this with signgleton->connected used like this:

In EthernetInterface::loop():

    if(!singleton || (!singleton->checkLink()))
        return;

and then checkLink like this:

bool EthernetInterface::checkLink() {    
  if (Ethernet.linkStatus() == LinkON) {
    if (!connected) {
      DIAG(F("Ethernet cable connected"));
      connected=true;
      IPAddress ip = Ethernet.localIP(); // reassign the obtained ip address
      server = new EthernetServer(IP_PORT); // Ethernet Server listening on default port IP_PORT
      server->begin();
      LCD(4,F("IP: %d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]);
      LCD(5,F("Port:%d"), IP_PORT);
      outboundRing=new RingStream(OUTBOUND_RING_SIZE);
    }
    return true;
  } else {
    if (connected) {
      DIAG(F("Ethernet cable disconnected"));
      connected=false;
      /* tear down server */
      LCD(4,F("IP: None"));
    }
    return false;
  }
}

The tear down server part has to be written but that would allow to disconnect and reconnect cable. I think. We can try to make it possible ;-)

Cheers, Harald.

bcsanches commented 1 year ago

Thank you! I implemented those proposals. Now trying to figure out how to update the pull request.

habazut commented 1 year ago

Nice. I often find it is easier to make a new pull request and we throw away the old one. But I think we pull this in and if there are any problems let's deal with them later. I can not test with an ethernet shield anyway as I do not have one. Harald.

bcsanches commented 1 year ago

I did several tests here and seems to works ok. Connected and disconnected the cable several times. It appears to be stable. Thank you

habazut commented 1 year ago

I simplified the setup code and did reformat to our mostly used code style. See latest commit. Added your name to the (c) in EthernetInterface.cpp. Thanks again.

bcsanches commented 1 year ago

Looking nice! Thank you!