From cceecf5f8e6ef6a4791f74e8bbca291c23e1a81b Mon Sep 17 00:00:00 2001 From: geeksville Date: Mon, 27 Apr 2020 09:36:39 -0700 Subject: [PATCH] New serial protobuf transport approximately works and is backward compatiable with the text debug output. --- src/SerialConsole.cpp | 32 ++++++++++++++++++++++++++++++++ src/SerialConsole.h | 24 ++++++++++++++++++++++++ src/configuration.h | 4 +++- src/main.cpp | 6 +++++- src/mesh/PhoneAPI.h | 4 ++-- src/mesh/StreamAPI.cpp | 27 ++++++++++++++------------- src/mesh/StreamAPI.h | 4 ++++ 7 files changed, 84 insertions(+), 17 deletions(-) create mode 100644 src/SerialConsole.cpp create mode 100644 src/SerialConsole.h diff --git a/src/SerialConsole.cpp b/src/SerialConsole.cpp new file mode 100644 index 000000000..5a86dbc1d --- /dev/null +++ b/src/SerialConsole.cpp @@ -0,0 +1,32 @@ +#include "SerialConsole.h" +#include "configuration.h" +#include + +#define Port Serial + +SerialConsole console; + +SerialConsole::SerialConsole() : StreamAPI(&Port), RedirectablePrint(&Port) +{ + canWrite = false; // We don't send packets to our port until it has talked to us first + // setDestination(&noopPrint); +} + +/// Do late init that can't happen at constructor time +void SerialConsole::init() +{ + Port.begin(SERIAL_BAUD); + StreamAPI::init(); +} + +/** + * we override this to notice when we've received a protobuf over the serial stream. Then we shunt off + * debug serial output. + */ +void SerialConsole::handleToRadio(const uint8_t *buf, size_t len) +{ + setDestination(&noopPrint); + canWrite = true; + + StreamAPI::handleToRadio(buf, len); +} \ No newline at end of file diff --git a/src/SerialConsole.h b/src/SerialConsole.h new file mode 100644 index 000000000..50efb99af --- /dev/null +++ b/src/SerialConsole.h @@ -0,0 +1,24 @@ +#pragma once + +#include "RedirectablePrint.h" +#include "StreamAPI.h" +/** + * 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 +{ + public: + SerialConsole(); + + /// Do late init that can't happen at constructor time + virtual void init(); + + /** + * we override this to notice when we've received a protobuf over the serial stream. Then we shunt off + * debug serial output. + */ + virtual void handleToRadio(const uint8_t *buf, size_t len); +}; + +extern SerialConsole console; diff --git a/src/configuration.h b/src/configuration.h index a47148ce9..8f443904e 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -237,7 +237,9 @@ along with this program. If not, see . #include "SEGGER_RTT.h" #define DEBUG_MSG(...) SEGGER_RTT_printf(0, __VA_ARGS__) #else -#define DEBUG_PORT Serial // Serial debug port +#include "SerialConsole.h" + +#define DEBUG_PORT console // Serial debug port #ifdef DEBUG_PORT #define DEBUG_MSG(...) DEBUG_PORT.printf(__VA_ARGS__) diff --git a/src/main.cpp b/src/main.cpp index ec59f9248..d0e8ddd7f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -126,7 +126,7 @@ void setup() // Debug #ifdef DEBUG_PORT - DEBUG_PORT.begin(SERIAL_BAUD); + DEBUG_PORT.init(); // Set serial baud rate and init our mesh console #endif initDeepSleep(); @@ -234,6 +234,10 @@ void loop() periodicScheduler.loop(); // axpDebugOutput.loop(); +#ifdef DEBUG_PORT + DEBUG_PORT.loop(); // Send/receive protobufs over the serial port +#endif + #ifndef NO_ESP32 esp32Loop(); #endif diff --git a/src/mesh/PhoneAPI.h b/src/mesh/PhoneAPI.h index 391644499..337408246 100644 --- a/src/mesh/PhoneAPI.h +++ b/src/mesh/PhoneAPI.h @@ -57,12 +57,12 @@ class PhoneAPI PhoneAPI(); /// Do late init that can't happen at constructor time - void init(); + virtual void init(); /** * Handle a ToRadio protobuf */ - void handleToRadio(const uint8_t *buf, size_t len); + virtual void handleToRadio(const uint8_t *buf, size_t len); /** * Get the next packet we want to send to the phone diff --git a/src/mesh/StreamAPI.cpp b/src/mesh/StreamAPI.cpp index ece4f4f81..b82054877 100644 --- a/src/mesh/StreamAPI.cpp +++ b/src/mesh/StreamAPI.cpp @@ -52,18 +52,19 @@ void StreamAPI::readStream() */ void StreamAPI::writeStream() { - uint32_t len; + if (canWrite) { + uint32_t len; + do { + // Send every packet we can + len = getFromRadio(txBuf + HEADER_LEN); + if (len != 0) { + txBuf[0] = START1; + txBuf[1] = START2; + txBuf[2] = (len >> 8) & 0xff; + txBuf[3] = len & 0xff; - do { - // Send every packet we can - len = getFromRadio(txBuf + HEADER_LEN); - if (len != 0) { - txBuf[0] = START1; - txBuf[1] = START2; - txBuf[2] = (len >> 8) & 0xff; - txBuf[3] = len & 0xff; - - stream->write(txBuf, len + HEADER_LEN); - } - } while (len); + stream->write(txBuf, len + HEADER_LEN); + } + } while (len); + } } \ No newline at end of file diff --git a/src/mesh/StreamAPI.h b/src/mesh/StreamAPI.h index 4b85ac4ef..ec8e55946 100644 --- a/src/mesh/StreamAPI.h +++ b/src/mesh/StreamAPI.h @@ -59,4 +59,8 @@ class StreamAPI : public PhoneAPI * call getFromRadio() and deliver encapsulated packets to the Stream */ void writeStream(); + + protected: + /// Are we allowed to write packets to our output stream (subclasses can turn this off - i.e. SerialConsole) + bool canWrite = true; }; \ No newline at end of file