From 23d015880443db9dc776d1172ee461bcc2850814 Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Mon, 5 Sep 2022 22:19:18 +0200 Subject: [PATCH] simplify EthernetInterface::setup, make code shorter and format according to our overall style --- EthernetInterface.cpp | 149 +++++++++++++++++------------------------- EthernetInterface.h | 34 +++++----- 2 files changed, 77 insertions(+), 106 deletions(-) diff --git a/EthernetInterface.cpp b/EthernetInterface.cpp index ddd762c..7d8fed8 100644 --- a/EthernetInterface.cpp +++ b/EthernetInterface.cpp @@ -1,6 +1,7 @@ /* + * © 2022 Bruno Sanches * © 2021 Fred Decker - * © 2020-2021 Harald Barth + * © 2020-2022 Harald Barth * © 2020-2021 Chris Harlow * © 2020 Gregor Baues * All rights reserved. @@ -35,17 +36,13 @@ EthernetInterface * EthernetInterface::singleton=NULL; */ void EthernetInterface::setup() { + if (Ethernet.hardwareStatus() == EthernetNoHardware) { + if (singleton!=NULL) + DIAG(F("Prog Error!")); + } else { singleton=new EthernetInterface(); - - DIAG(F("Ethernet begin OK.")); - if (Ethernet.hardwareStatus() == EthernetNoHardware) { - DIAG(F("Ethernet shield not found")); - - delete singleton; - singleton=NULL; - - return; - } + } + DIAG(F("Ethernet shield %sfound"), singleton==NULL ? "not " : ""); }; @@ -77,42 +74,35 @@ EthernetInterface::EthernetInterface() * * @return none */ -EthernetInterface::~EthernetInterface() -{ - delete server; - delete outboundRing; +EthernetInterface::~EthernetInterface() { + delete server; + delete outboundRing; } /** * @brief Main loop for the EthernetInterface * */ -void EthernetInterface::loop() -{ - if(!singleton || (!singleton->checkLink())) - return; - - switch (Ethernet.maintain()) - { - case 1: - //renewed fail - DIAG(F("Ethernet Error: renewed fail")); - singleton=NULL; - return; - - case 3: - //rebind fail - DIAG(F("Ethernet Error: rebind fail")); - singleton=NULL; - return; - - default: - //nothing happened - break; - } - - singleton->loop2(); +void EthernetInterface::loop() { + if(!singleton || (!singleton->checkLink())) + return; + switch (Ethernet.maintain()) { + case 1: + //renewed fail + DIAG(F("Ethernet Error: renewed fail")); + singleton=NULL; + return; + case 3: + //rebind fail + DIAG(F("Ethernet Error: rebind fail")); + singleton=NULL; + return; + default: + //nothing happened + break; + } + singleton->loop2(); } /** @@ -120,57 +110,40 @@ void EthernetInterface::loop() * * @return true when cable is connected, false otherwise */ -bool EthernetInterface::checkLink() -{ - if (Ethernet.linkStatus() == LinkON) - { - //if we are not connected yet, setup a new server - 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); - - // - //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); - } - - 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")); +bool EthernetInterface::checkLink() { + if (Ethernet.linkStatus() == LinkON) { + //if we are not connected yet, setup a new server + 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); + // 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); } - - return false; + return true; + } else { // 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() { // get client from the server EthernetClient client = server->accept(); diff --git a/EthernetInterface.h b/EthernetInterface.h index ce5bbd9..a4544c6 100644 --- a/EthernetInterface.h +++ b/EthernetInterface.h @@ -50,25 +50,23 @@ class EthernetInterface { - public: - - static void setup(); - static void loop(); - - private: - static EthernetInterface * singleton; - bool connected; - EthernetInterface(); - ~EthernetInterface(); - void loop2(); - - bool checkLink(); - - 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 - uint8_t buffer[MAX_ETH_BUFFER+1]; // buffer used by TCP for the recv - RingStream * outboundRing = nullptr; +public: + static void setup(); + static void loop(); +private: + static EthernetInterface * singleton; + bool connected; + EthernetInterface(); + ~EthernetInterface(); + void loop2(); + bool checkLink(); + + 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 + uint8_t buffer[MAX_ETH_BUFFER+1]; // buffer used by TCP for the recv + RingStream * outboundRing = nullptr; }; #endif