1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2025-02-19 23:46:02 +01:00

simplify EthernetInterface::setup, make code shorter and format according to our overall style

This commit is contained in:
Harald Barth 2022-09-05 22:19:18 +02:00
parent 2e9e614ad5
commit 23d0158804
2 changed files with 77 additions and 106 deletions

View File

@ -1,6 +1,7 @@
/* /*
* © 2022 Bruno Sanches
* © 2021 Fred Decker * © 2021 Fred Decker
* © 2020-2021 Harald Barth * © 2020-2022 Harald Barth
* © 2020-2021 Chris Harlow * © 2020-2021 Chris Harlow
* © 2020 Gregor Baues * © 2020 Gregor Baues
* All rights reserved. * All rights reserved.
@ -35,17 +36,13 @@ EthernetInterface * EthernetInterface::singleton=NULL;
*/ */
void EthernetInterface::setup() void EthernetInterface::setup()
{ {
singleton=new EthernetInterface();
DIAG(F("Ethernet begin OK."));
if (Ethernet.hardwareStatus() == EthernetNoHardware) { if (Ethernet.hardwareStatus() == EthernetNoHardware) {
DIAG(F("Ethernet shield not found")); if (singleton!=NULL)
DIAG(F("Prog Error!"));
delete singleton; } else {
singleton=NULL; singleton=new EthernetInterface();
return;
} }
DIAG(F("Ethernet shield %sfound"), singleton==NULL ? "not " : "");
}; };
@ -77,8 +74,7 @@ EthernetInterface::EthernetInterface()
* *
* @return none * @return none
*/ */
EthernetInterface::~EthernetInterface() EthernetInterface::~EthernetInterface() {
{
delete server; delete server;
delete outboundRing; delete outboundRing;
} }
@ -87,32 +83,26 @@ EthernetInterface::~EthernetInterface()
* @brief Main loop for the EthernetInterface * @brief Main loop for the EthernetInterface
* *
*/ */
void EthernetInterface::loop() void EthernetInterface::loop() {
{
if(!singleton || (!singleton->checkLink())) if(!singleton || (!singleton->checkLink()))
return; return;
switch (Ethernet.maintain()) switch (Ethernet.maintain()) {
{
case 1: case 1:
//renewed fail //renewed fail
DIAG(F("Ethernet Error: renewed fail")); DIAG(F("Ethernet Error: renewed fail"));
singleton=NULL; singleton=NULL;
return; return;
case 3: case 3:
//rebind fail //rebind fail
DIAG(F("Ethernet Error: rebind fail")); DIAG(F("Ethernet Error: rebind fail"));
singleton=NULL; singleton=NULL;
return; return;
default: default:
//nothing happened //nothing happened
break; break;
} }
singleton->loop2(); singleton->loop2();
} }
/** /**
@ -120,57 +110,40 @@ void EthernetInterface::loop()
* *
* @return true when cable is connected, false otherwise * @return true when cable is connected, false otherwise
*/ */
bool EthernetInterface::checkLink() bool EthernetInterface::checkLink() {
{ if (Ethernet.linkStatus() == LinkON) {
if (Ethernet.linkStatus() == LinkON)
{
//if we are not connected yet, setup a new server //if we are not connected yet, setup a new server
if(!connected) if(!connected) {
{
DIAG(F("Ethernet cable connected")); DIAG(F("Ethernet cable connected"));
connected=true; connected=true;
IPAddress ip = Ethernet.localIP(); // reassign the obtained ip address IPAddress ip = Ethernet.localIP(); // reassign the obtained ip address
server = new EthernetServer(IP_PORT); // Ethernet Server listening on default port IP_PORT server = new EthernetServer(IP_PORT); // Ethernet Server listening on default port IP_PORT
server->begin(); server->begin();
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
//only create a outboundRing it none exists, this may happen if the cable gets disconnected and connected again
if(!outboundRing) if(!outboundRing)
outboundRing=new RingStream(OUTBOUND_RING_SIZE); outboundRing=new RingStream(OUTBOUND_RING_SIZE);
} }
return true; return true;
} } else { // connected
else if(connected)
{
DIAG(F("Ethernet cable disconnected")); DIAG(F("Ethernet cable disconnected"));
connected=false; connected=false;
//clean up any client //clean up any client
for (byte socket = 0; socket < MAX_SOCK_NUM; socket++) for (byte socket = 0; socket < MAX_SOCK_NUM; socket++) {
{
if(clients[socket].connected()) if(clients[socket].connected())
clients[socket].stop(); clients[socket].stop();
} }
// tear down server
/* tear down server */
delete server; delete server;
server = nullptr; server = nullptr;
LCD(4,F("IP: None")); LCD(4,F("IP: None"));
} }
return false; return false;
} }
void EthernetInterface::loop2() void EthernetInterface::loop2() {
{
// get client from the server // get client from the server
EthernetClient client = server->accept(); EthernetClient client = server->accept();

View File

@ -51,7 +51,6 @@
class EthernetInterface { class EthernetInterface {
public: public:
static void setup(); static void setup();
static void loop(); static void loop();
@ -61,14 +60,13 @@ class EthernetInterface {
EthernetInterface(); EthernetInterface();
~EthernetInterface(); ~EthernetInterface();
void loop2(); void loop2();
bool checkLink(); bool checkLink();
EthernetServer *server = nullptr; 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 = nullptr; RingStream * outboundRing = nullptr;
}; };
#endif #endif