mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2024-11-27 01:56:14 +01:00
Detects when ethernet cable is connected and is disconnected, also correctly handles EthernetServer tead down on such situations
This commit is contained in:
parent
34c3d10767
commit
64b1de08be
|
@ -41,6 +41,7 @@ void EthernetInterface::setup()
|
||||||
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
|
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
|
||||||
DIAG(F("Ethernet shield not found"));
|
DIAG(F("Ethernet shield not found"));
|
||||||
|
|
||||||
|
delete singleton;
|
||||||
singleton=NULL;
|
singleton=NULL;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
@ -71,13 +72,24 @@ EthernetInterface::EthernetInterface()
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Cleanup any resources
|
||||||
|
*
|
||||||
|
* @return none
|
||||||
|
*/
|
||||||
|
EthernetInterface::~EthernetInterface()
|
||||||
|
{
|
||||||
|
delete server;
|
||||||
|
delete outboundRing;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Main loop for the EthernetInterface
|
* @brief Main loop for the EthernetInterface
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
void EthernetInterface::loop()
|
void EthernetInterface::loop()
|
||||||
{
|
{
|
||||||
if(!singleton || ((!singleton->connected) && (!singleton->checkLink())))
|
if(!singleton || (!singleton->checkLink()))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
switch (Ethernet.maintain())
|
switch (Ethernet.maintain())
|
||||||
|
@ -103,11 +115,18 @@ void EthernetInterface::loop()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Checks ethernet link cable status and detects when it connects / disconnects
|
||||||
|
*
|
||||||
|
* @return true when cable is connected, false otherwise
|
||||||
|
*/
|
||||||
bool EthernetInterface::checkLink()
|
bool EthernetInterface::checkLink()
|
||||||
{
|
{
|
||||||
if (Ethernet.linkStatus() != LinkON)
|
if (Ethernet.linkStatus() == LinkON)
|
||||||
return false;
|
{
|
||||||
|
//if we are not connected yet, setup a new server
|
||||||
|
if(!connected)
|
||||||
|
{
|
||||||
DIAG(F("Ethernet cable connected"));
|
DIAG(F("Ethernet cable connected"));
|
||||||
|
|
||||||
connected=true;
|
connected=true;
|
||||||
|
@ -120,9 +139,34 @@ bool EthernetInterface::checkLink()
|
||||||
LCD(4,F("IP: %d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]);
|
LCD(4,F("IP: %d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]);
|
||||||
LCD(5,F("Port:%d"), IP_PORT);
|
LCD(5,F("Port:%d"), IP_PORT);
|
||||||
|
|
||||||
|
//
|
||||||
|
//only create a outboundRing it none exists, this may happen if the cable gets disconnected and connected again
|
||||||
|
if(!outboundRing)
|
||||||
outboundRing=new RingStream(OUTBOUND_RING_SIZE);
|
outboundRing=new RingStream(OUTBOUND_RING_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
else if(connected)
|
||||||
|
{
|
||||||
|
DIAG(F("Ethernet cable disconnected"));
|
||||||
|
connected=false;
|
||||||
|
|
||||||
|
//clean up any client
|
||||||
|
for (byte socket = 0; socket < MAX_SOCK_NUM; socket++)
|
||||||
|
{
|
||||||
|
if(clients[socket].connected())
|
||||||
|
clients[socket].stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* tear down server */
|
||||||
|
delete server;
|
||||||
|
server = nullptr;
|
||||||
|
|
||||||
|
LCD(4,F("IP: None"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void EthernetInterface::loop2()
|
void EthernetInterface::loop2()
|
||||||
|
|
|
@ -59,14 +59,15 @@ class EthernetInterface {
|
||||||
static EthernetInterface * singleton;
|
static EthernetInterface * singleton;
|
||||||
bool connected;
|
bool connected;
|
||||||
EthernetInterface();
|
EthernetInterface();
|
||||||
|
~EthernetInterface();
|
||||||
void loop2();
|
void loop2();
|
||||||
|
|
||||||
bool checkLink();
|
bool checkLink();
|
||||||
|
|
||||||
EthernetServer * server;
|
EthernetServer * server = nullptr;
|
||||||
EthernetClient clients[MAX_SOCK_NUM]; // accept up to MAX_SOCK_NUM client connections at the same time; This depends on the chipset used on the Shield
|
EthernetClient clients[MAX_SOCK_NUM]; // accept up to MAX_SOCK_NUM client connections at the same time; This depends on the chipset used on the Shield
|
||||||
uint8_t buffer[MAX_ETH_BUFFER+1]; // buffer used by TCP for the recv
|
uint8_t buffer[MAX_ETH_BUFFER+1]; // buffer used by TCP for the recv
|
||||||
RingStream * outboundRing;
|
RingStream * outboundRing = nullptr;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user