From a2894068632238572c9a6a4ed728f16bc3bc28bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Thu, 22 Dec 2022 18:24:42 +0100 Subject: [PATCH] refactor classes to accomodate SerialModule --- src/SerialConsole.cpp | 6 +++++- src/SerialConsole.h | 4 +++- src/mesh/StreamAPI.cpp | 2 +- src/mesh/StreamAPI.h | 6 +++--- src/mesh/eth/ethServerAPI.cpp | 4 ++-- src/mesh/eth/ethServerAPI.h | 2 +- src/mesh/wifi/WiFiServerAPI.cpp | 4 ++-- src/mesh/wifi/WiFiServerAPI.h | 2 +- src/modules/SerialModule.cpp | 2 +- 9 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/SerialConsole.cpp b/src/SerialConsole.cpp index b7854b2cf..e7355db29 100644 --- a/src/SerialConsole.cpp +++ b/src/SerialConsole.cpp @@ -25,7 +25,7 @@ void consolePrintf(const char *format, ...) #endif } -SerialConsole::SerialConsole() : StreamAPI(&Port), RedirectablePrint(&Port) +SerialConsole::SerialConsole() : StreamAPI(&Port), RedirectablePrint(&Port), concurrency::OSThread("SerialConsole") { assert(!console); console = this; @@ -46,6 +46,10 @@ SerialConsole::SerialConsole() : StreamAPI(&Port), RedirectablePrint(&Port) emitRebooted(); } +int32_t SerialConsole::runOnce() +{ + return runOncePart(); +} // For the serial port we can't really detect if any client is on the other side, so instead just look for recent messages bool SerialConsole::checkIsConnected() diff --git a/src/SerialConsole.h b/src/SerialConsole.h index e7b8af34f..d1f2abac8 100644 --- a/src/SerialConsole.h +++ b/src/SerialConsole.h @@ -6,7 +6,7 @@ * Provides both debug printing and, if the client starts sending protobufs to us, switches to send/receive protobufs * (and starts dropping debug printing - FIXME, eventually those prints should be encapsulated in protobufs). */ -class SerialConsole : public StreamAPI, public RedirectablePrint +class SerialConsole : public StreamAPI, public RedirectablePrint, private concurrency::OSThread { public: SerialConsole(); @@ -24,6 +24,8 @@ class SerialConsole : public StreamAPI, public RedirectablePrint return RedirectablePrint::write(c); } + virtual int32_t runOnce() override; + protected: /// Check the current underlying physical link to see if the client is currently connected diff --git a/src/mesh/StreamAPI.cpp b/src/mesh/StreamAPI.cpp index 0b959ddcb..d1213ac54 100644 --- a/src/mesh/StreamAPI.cpp +++ b/src/mesh/StreamAPI.cpp @@ -6,7 +6,7 @@ #define START2 0xc3 #define HEADER_LEN 4 -int32_t StreamAPI::runOnce() +int32_t StreamAPI::runOncePart() { auto result = readStream(); writeStream(); diff --git a/src/mesh/StreamAPI.h b/src/mesh/StreamAPI.h index 0f36ecb06..3196e96f8 100644 --- a/src/mesh/StreamAPI.h +++ b/src/mesh/StreamAPI.h @@ -28,7 +28,7 @@ valid utf8 encoding. This makes it a bit easier to start a device outputting reg after it has received a valid packet from the PC, turn off unencoded debug printing and switch to this packet encoding. */ -class StreamAPI : public PhoneAPI, protected concurrency::OSThread +class StreamAPI : public PhoneAPI { /** * The stream we read/write from @@ -42,13 +42,13 @@ class StreamAPI : public PhoneAPI, protected concurrency::OSThread uint32_t lastRxMsec = 0; public: - StreamAPI(Stream *_stream) : concurrency::OSThread("StreamAPI"), stream(_stream) {} + StreamAPI(Stream *_stream) : stream(_stream) {} /** * Currently we require frequent invocation from loop() to check for arrived serial packets and to send new packets to the * phone. */ - virtual int32_t runOnce() override; + virtual int32_t runOncePart(); private: /** diff --git a/src/mesh/eth/ethServerAPI.cpp b/src/mesh/eth/ethServerAPI.cpp index bb7dd927d..3b3b6bbc8 100644 --- a/src/mesh/eth/ethServerAPI.cpp +++ b/src/mesh/eth/ethServerAPI.cpp @@ -14,7 +14,7 @@ void initApiServer(int port) } } -ethServerAPI::ethServerAPI(EthernetClient &_client) : StreamAPI(&client), client(_client) +ethServerAPI::ethServerAPI(EthernetClient &_client) : StreamAPI(&client), concurrency::OSThread("ethServerAPI"), client(_client) { DEBUG_MSG("Incoming ethernet connection\n"); } @@ -42,7 +42,7 @@ bool ethServerAPI::checkIsConnected() int32_t ethServerAPI::runOnce() { if (client.connected()) { - return StreamAPI::runOnce(); + return StreamAPI::runOncePart(); } else { DEBUG_MSG("Client dropped connection, suspending API service\n"); enabled = false; // we no longer need to run diff --git a/src/mesh/eth/ethServerAPI.h b/src/mesh/eth/ethServerAPI.h index c92ab2f17..962841c80 100644 --- a/src/mesh/eth/ethServerAPI.h +++ b/src/mesh/eth/ethServerAPI.h @@ -7,7 +7,7 @@ * Provides both debug printing and, if the client starts sending protobufs to us, switches to send/receive protobufs * (and starts dropping debug printing - FIXME, eventually those prints should be encapsulated in protobufs). */ -class ethServerAPI : public StreamAPI +class ethServerAPI : public StreamAPI, private concurrency::OSThread { private: EthernetClient client; diff --git a/src/mesh/wifi/WiFiServerAPI.cpp b/src/mesh/wifi/WiFiServerAPI.cpp index 34a15f71b..78936176b 100644 --- a/src/mesh/wifi/WiFiServerAPI.cpp +++ b/src/mesh/wifi/WiFiServerAPI.cpp @@ -14,7 +14,7 @@ void initApiServer(int port) } } -WiFiServerAPI::WiFiServerAPI(WiFiClient &_client) : StreamAPI(&client), client(_client) +WiFiServerAPI::WiFiServerAPI(WiFiClient &_client) : StreamAPI(&client), concurrency::OSThread("WiFiServerAPI"), client(_client) { DEBUG_MSG("Incoming wifi connection\n"); } @@ -42,7 +42,7 @@ bool WiFiServerAPI::checkIsConnected() int32_t WiFiServerAPI::runOnce() { if (client.connected()) { - return StreamAPI::runOnce(); + return StreamAPI::runOncePart(); } else { DEBUG_MSG("Client dropped connection, suspending API service\n"); enabled = false; // we no longer need to run diff --git a/src/mesh/wifi/WiFiServerAPI.h b/src/mesh/wifi/WiFiServerAPI.h index 84a23be06..812408818 100644 --- a/src/mesh/wifi/WiFiServerAPI.h +++ b/src/mesh/wifi/WiFiServerAPI.h @@ -7,7 +7,7 @@ * Provides both debug printing and, if the client starts sending protobufs to us, switches to send/receive protobufs * (and starts dropping debug printing - FIXME, eventually those prints should be encapsulated in protobufs). */ -class WiFiServerAPI : public StreamAPI +class WiFiServerAPI : public StreamAPI, private concurrency::OSThread { private: WiFiClient client; diff --git a/src/modules/SerialModule.cpp b/src/modules/SerialModule.cpp index 958faeebe..f0374bb41 100644 --- a/src/modules/SerialModule.cpp +++ b/src/modules/SerialModule.cpp @@ -197,7 +197,7 @@ int32_t SerialModule::runOnce() } else { if (moduleConfig.serial.mode == ModuleConfig_SerialConfig_Serial_Mode_PROTO) { - return StreamAPI::runOnce(); + return runOncePart(); } else if (moduleConfig.serial.mode == ModuleConfig_SerialConfig_Serial_Mode_NMEA) { // in NMEA mode send out GGA every 2 seconds, Don't read from Port if (millis() - lastNmeaTime > 2000) {