From dec487064939ac2fcf58351356eff58eb0622597 Mon Sep 17 00:00:00 2001 From: geeksville Date: Mon, 27 Apr 2020 07:54:19 -0700 Subject: [PATCH 1/6] begin cleaning up mesh library layer so that it could be split someday --- .vscode/launch.json | 10 ++++------ bin/regen-protos.sh | 2 +- platformio.ini | 2 +- src/{ => mesh}/MeshRadio.cpp | 0 src/{ => mesh}/MeshRadio.h | 0 src/{ => mesh}/MeshService.cpp | 0 src/{ => mesh}/MeshService.h | 0 src/{ => mesh}/MeshTypes.h | 0 src/{ => mesh}/NodeDB.cpp | 0 src/{ => mesh}/NodeDB.h | 0 src/{ => mesh}/PhoneAPI.cpp | 0 src/{ => mesh}/PhoneAPI.h | 0 src/{ => mesh}/mesh-pb-constants.cpp | 0 src/{ => mesh}/mesh-pb-constants.h | 0 src/{ => mesh}/mesh.pb.c | 0 src/{ => mesh}/mesh.pb.h | 0 16 files changed, 6 insertions(+), 8 deletions(-) rename src/{ => mesh}/MeshRadio.cpp (100%) rename src/{ => mesh}/MeshRadio.h (100%) rename src/{ => mesh}/MeshService.cpp (100%) rename src/{ => mesh}/MeshService.h (100%) rename src/{ => mesh}/MeshTypes.h (100%) rename src/{ => mesh}/NodeDB.cpp (100%) rename src/{ => mesh}/NodeDB.h (100%) rename src/{ => mesh}/PhoneAPI.cpp (100%) rename src/{ => mesh}/PhoneAPI.h (100%) rename src/{ => mesh}/mesh-pb-constants.cpp (100%) rename src/{ => mesh}/mesh-pb-constants.h (100%) rename src/{ => mesh}/mesh.pb.c (100%) rename src/{ => mesh}/mesh.pb.h (100%) diff --git a/.vscode/launch.json b/.vscode/launch.json index 68cb8d526..914831d68 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -12,9 +12,8 @@ "type": "platformio-debug", "request": "launch", "name": "PIO Debug", - "executable": "/home/kevinh/development/meshtastic/meshtastic-esp32/.pio/build/nrf52dk/firmware.elf", - "toolchainBinDir": "/home/kevinh/.platformio/packages/toolchain-gccarmnoneeabi/bin", - "svdPath": "/home/kevinh/.platformio/platforms/nordicnrf52/misc/svd/nrf52840.svd", + "executable": "/home/kevinh/development/meshtastic/meshtastic-esp32/.pio/build/tbeam/firmware.elf", + "toolchainBinDir": "/home/kevinh/.platformio/packages/toolchain-xtensa32/bin", "preLaunchTask": { "type": "PlatformIO", "task": "Pre-Debug" @@ -25,9 +24,8 @@ "type": "platformio-debug", "request": "launch", "name": "PIO Debug (skip Pre-Debug)", - "executable": "/home/kevinh/development/meshtastic/meshtastic-esp32/.pio/build/nrf52dk/firmware.elf", - "toolchainBinDir": "/home/kevinh/.platformio/packages/toolchain-gccarmnoneeabi/bin", - "svdPath": "/home/kevinh/.platformio/platforms/nordicnrf52/misc/svd/nrf52840.svd", + "executable": "/home/kevinh/development/meshtastic/meshtastic-esp32/.pio/build/tbeam/firmware.elf", + "toolchainBinDir": "/home/kevinh/.platformio/packages/toolchain-xtensa32/bin", "internalConsoleOptions": "openOnSessionStart" } ] diff --git a/bin/regen-protos.sh b/bin/regen-protos.sh index bc6bc9b72..0f1ae43c6 100755 --- a/bin/regen-protos.sh +++ b/bin/regen-protos.sh @@ -3,4 +3,4 @@ echo "This script requires https://jpa.kapsi.fi/nanopb/download/ version 0.4.1" # the nanopb tool seems to require that the .options file be in the current directory! cd proto -../../nanopb-0.4.1-linux-x86/generator-bin/protoc --nanopb_out=-v:../src -I=../proto mesh.proto +../../nanopb-0.4.1-linux-x86/generator-bin/protoc --nanopb_out=-v:../src/mesh -I=../proto mesh.proto diff --git a/platformio.ini b/platformio.ini index afba2b65f..46c72b145 100644 --- a/platformio.ini +++ b/platformio.ini @@ -31,7 +31,7 @@ board_build.partitions = partition-table.csv ; note: we add src to our include search path so that lmic_project_config can override ; FIXME: fix lib/BluetoothOTA dependency back on src/ so we can remove -Isrc -build_flags = -Wno-missing-field-initializers -Isrc -Isrc/rf95 -Os -Wl,-Map,.pio/build/output.map +build_flags = -Wno-missing-field-initializers -Isrc -Isrc/rf95 -Isrc/mesh -Os -Wl,-Map,.pio/build/output.map -DAXP_DEBUG_PORT=Serial -DHW_VERSION_${sysenv.COUNTRY} -DAPP_VERSION=${sysenv.APP_VERSION} diff --git a/src/MeshRadio.cpp b/src/mesh/MeshRadio.cpp similarity index 100% rename from src/MeshRadio.cpp rename to src/mesh/MeshRadio.cpp diff --git a/src/MeshRadio.h b/src/mesh/MeshRadio.h similarity index 100% rename from src/MeshRadio.h rename to src/mesh/MeshRadio.h diff --git a/src/MeshService.cpp b/src/mesh/MeshService.cpp similarity index 100% rename from src/MeshService.cpp rename to src/mesh/MeshService.cpp diff --git a/src/MeshService.h b/src/mesh/MeshService.h similarity index 100% rename from src/MeshService.h rename to src/mesh/MeshService.h diff --git a/src/MeshTypes.h b/src/mesh/MeshTypes.h similarity index 100% rename from src/MeshTypes.h rename to src/mesh/MeshTypes.h diff --git a/src/NodeDB.cpp b/src/mesh/NodeDB.cpp similarity index 100% rename from src/NodeDB.cpp rename to src/mesh/NodeDB.cpp diff --git a/src/NodeDB.h b/src/mesh/NodeDB.h similarity index 100% rename from src/NodeDB.h rename to src/mesh/NodeDB.h diff --git a/src/PhoneAPI.cpp b/src/mesh/PhoneAPI.cpp similarity index 100% rename from src/PhoneAPI.cpp rename to src/mesh/PhoneAPI.cpp diff --git a/src/PhoneAPI.h b/src/mesh/PhoneAPI.h similarity index 100% rename from src/PhoneAPI.h rename to src/mesh/PhoneAPI.h diff --git a/src/mesh-pb-constants.cpp b/src/mesh/mesh-pb-constants.cpp similarity index 100% rename from src/mesh-pb-constants.cpp rename to src/mesh/mesh-pb-constants.cpp diff --git a/src/mesh-pb-constants.h b/src/mesh/mesh-pb-constants.h similarity index 100% rename from src/mesh-pb-constants.h rename to src/mesh/mesh-pb-constants.h diff --git a/src/mesh.pb.c b/src/mesh/mesh.pb.c similarity index 100% rename from src/mesh.pb.c rename to src/mesh/mesh.pb.c diff --git a/src/mesh.pb.h b/src/mesh/mesh.pb.h similarity index 100% rename from src/mesh.pb.h rename to src/mesh/mesh.pb.h From 178e8009699fedad85fe88ca4305df3375487a88 Mon Sep 17 00:00:00 2001 From: geeksville Date: Mon, 27 Apr 2020 08:10:17 -0700 Subject: [PATCH 2/6] add beginnings of StreamAPI --- src/mesh/PhoneAPI.cpp | 5 ++--- src/mesh/PhoneAPI.h | 3 +++ src/mesh/StreamAPI.cpp | 1 + src/mesh/StreamAPI.h | 51 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 src/mesh/StreamAPI.cpp create mode 100644 src/mesh/StreamAPI.h diff --git a/src/mesh/PhoneAPI.cpp b/src/mesh/PhoneAPI.cpp index 043d1fb36..194a15e68 100644 --- a/src/mesh/PhoneAPI.cpp +++ b/src/mesh/PhoneAPI.cpp @@ -5,9 +5,8 @@ PhoneAPI::PhoneAPI() { - // Make sure that we never let our packets grow too large for one BLE packet - assert(FromRadio_size <= 512); - assert(ToRadio_size <= 512); + assert(FromRadio_size <= MAX_TO_FROM_RADIO_SIZE); + assert(ToRadio_size <= MAX_TO_FROM_RADIO_SIZE); } void PhoneAPI::init() diff --git a/src/mesh/PhoneAPI.h b/src/mesh/PhoneAPI.h index 56eac067d..391644499 100644 --- a/src/mesh/PhoneAPI.h +++ b/src/mesh/PhoneAPI.h @@ -5,6 +5,9 @@ #include "mesh.pb.h" #include +// Make sure that we never let our packets grow too large for one BLE packet +#define MAX_TO_FROM_RADIO_SIZE 512 + /** * Provides our protobuf based API which phone/PC clients can use to talk to our device * over UDP, bluetooth or serial. diff --git a/src/mesh/StreamAPI.cpp b/src/mesh/StreamAPI.cpp new file mode 100644 index 000000000..f363a001b --- /dev/null +++ b/src/mesh/StreamAPI.cpp @@ -0,0 +1 @@ +#include "StreamAPI.h" diff --git a/src/mesh/StreamAPI.h b/src/mesh/StreamAPI.h new file mode 100644 index 000000000..0b5cfe64f --- /dev/null +++ b/src/mesh/StreamAPI.h @@ -0,0 +1,51 @@ +#pragma once + +#include "PhoneAPI.h" +#include "Stream.h" + +// A To/FromRadio packet + our 32 bit header +#define MAX_STREAM_BUF_SIZE (MAX_TO_FROM_RADIO_SIZE + sizeof(uint32_t)) + +/** + * A version of our 'phone' API that talks over a Stream. So therefore well suited to use with serial links + * or TCP connections. + * + * ## Wire encoding + +When sending protobuf packets over serial or TCP each packet is preceded by uint32 sent in network byte order (big endian). +The upper 16 bits must be 0x94C3. The lower 16 bits are packet length (this encoding gives room to eventually allow quite large +packets). + +Implementations validate length against the maximum possible size of a BLE packet (our lowest common denominator) of 512 bytes. If +the length provided is larger than that we assume the packet is corrupted and begin again looking for 0x4403 framing. + +The packets flowing towards the device are ToRadio protobufs, the packets flowing from the device are FromRadio protobufs. +The 0x94C3 marker can be used as framing to (eventually) resync if packets are corrupted over the wire. + +Note: the 0x94C3 framing was chosen to prevent confusion with the 7 bit ascii character set. It also doesn't collide with any +valid utf8 encoding. This makes it a bit easier to start a device outputting regular debug output on its serial port and then only +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 +{ + /** + * The stream we read/write from + */ + Stream *stream; + + uint8_t rxBuf[MAX_STREAM_BUF_SIZE]; + size_t rxPtr = 0; + + public: + StreamAPI(Stream *_stream) : stream(_stream) {} + + /** + * Currently we require frequent invocation from loop() to check for arrived serial packets. + * FIXME, to support better power behavior instead move to a thread and block on serial reads. + */ + void loop(); + + private: + void readStream(); +}; \ No newline at end of file From dda946d9336424bd6ce5a73afa4bfc55415b47d2 Mon Sep 17 00:00:00 2001 From: geeksville Date: Mon, 27 Apr 2020 08:45:39 -0700 Subject: [PATCH 3/6] Stream API coded but not tested --- src/mesh/StreamAPI.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++ src/mesh/StreamAPI.h | 13 +++++++- 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/src/mesh/StreamAPI.cpp b/src/mesh/StreamAPI.cpp index f363a001b..ece4f4f81 100644 --- a/src/mesh/StreamAPI.cpp +++ b/src/mesh/StreamAPI.cpp @@ -1 +1,69 @@ #include "StreamAPI.h" + +#define START1 0x94 +#define START2 0xc3 +#define HEADER_LEN 4 + +void StreamAPI::loop() +{ + writeStream(); + readStream(); +} + +/** + * Read any rx chars from the link and call handleToRadio + */ +void StreamAPI::readStream() +{ + while (stream->available()) { // Currently we never want to block + uint8_t c = stream->read(); + + // Use the read pointer for a little state machine, first look for framing, then length bytes, then payload + size_t ptr = rxPtr++; // assume we will probably advance the rxPtr + + rxBuf[ptr] = c; // store all bytes (including framing) + + if (ptr == 0) { // looking for START1 + if (c != START1) + rxPtr = 0; // failed to find framing + } else if (ptr == 1) { // looking for START2 + if (c != START2) + rxPtr = 0; // failed to find framing + } else if (ptr >= HEADER_LEN) { // we have at least read our 4 byte framing + uint16_t len = (rxBuf[2] << 8) + rxBuf[3]; // big endian 16 bit length follows framing + + if (ptr == HEADER_LEN) { + // we _just_ finished our 4 byte header, validate length now (note: a length of zero is a valid + // protobuf also) + if (len > MAX_TO_FROM_RADIO_SIZE) + rxPtr = 0; // length is bogus, restart search for framing + } + + if (rxPtr != 0 && ptr == len + HEADER_LEN) { + // If we didn't just fail the packet and we now have the right # of bytes, parse it + handleToRadio(rxBuf + HEADER_LEN, len); + } + } + } +} + +/** + * call getFromRadio() and deliver encapsulated packets to the Stream + */ +void StreamAPI::writeStream() +{ + 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; + + 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 0b5cfe64f..4b85ac4ef 100644 --- a/src/mesh/StreamAPI.h +++ b/src/mesh/StreamAPI.h @@ -37,15 +37,26 @@ class StreamAPI : public PhoneAPI uint8_t rxBuf[MAX_STREAM_BUF_SIZE]; size_t rxPtr = 0; + uint8_t txBuf[MAX_STREAM_BUF_SIZE]; + public: StreamAPI(Stream *_stream) : stream(_stream) {} /** - * Currently we require frequent invocation from loop() to check for arrived serial packets. + * Currently we require frequent invocation from loop() to check for arrived serial packets and to send new packets to the + * phone. * FIXME, to support better power behavior instead move to a thread and block on serial reads. */ void loop(); private: + /** + * Read any rx chars from the link and call handleToRadio + */ void readStream(); + + /** + * call getFromRadio() and deliver encapsulated packets to the Stream + */ + void writeStream(); }; \ No newline at end of file From eb40013ddcff35af8d6893f6623674783d723d35 Mon Sep 17 00:00:00 2001 From: geeksville Date: Mon, 27 Apr 2020 09:01:25 -0700 Subject: [PATCH 4/6] Create RedirectablePrint and NoopPrint for serial debug redirection --- src/RedirectablePrint.cpp | 13 +++++++++++++ src/RedirectablePrint.h | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/RedirectablePrint.cpp create mode 100644 src/RedirectablePrint.h diff --git a/src/RedirectablePrint.cpp b/src/RedirectablePrint.cpp new file mode 100644 index 000000000..04205f498 --- /dev/null +++ b/src/RedirectablePrint.cpp @@ -0,0 +1,13 @@ +#include "RedirectablePrint.h" +#include + +/** + * A printer that doesn't go anywhere + */ +NoopPrint noopPrint; + +void RedirectablePrint::setDestination(Print *_dest) +{ + assert(_dest); + dest = _dest; +} \ No newline at end of file diff --git a/src/RedirectablePrint.h b/src/RedirectablePrint.h new file mode 100644 index 000000000..f75aea010 --- /dev/null +++ b/src/RedirectablePrint.h @@ -0,0 +1,34 @@ +#pragma once + +#include + +/** + * A Printable that can be switched to squirt its bytes to a different sink. + * This class is mostly useful to allow debug printing to be redirected away from Serial + * to some other transport if we switch Serial usage (on the fly) to some other purpose. + */ +class RedirectablePrint : public Print +{ + Print *dest; + + public: + RedirectablePrint(Print *_dest) : dest(_dest) {} + + /** + * Set a new destination + */ + void setDestination(Print *dest); + + virtual size_t write(uint8_t c) { return dest->write(c); } +}; + +class NoopPrint : public Print +{ + public: + virtual size_t write(uint8_t c) { return 1; } +}; + +/** + * A printer that doesn't go anywhere + */ +extern NoopPrint noopPrint; \ No newline at end of file From e5d2d24e2c12e8bacf9dad224c69ec4812873186 Mon Sep 17 00:00:00 2001 From: geeksville Date: Mon, 27 Apr 2020 09:27:36 -0700 Subject: [PATCH 5/6] move nanopb includes to correct directory --- lib/nanopb/{src => include}/pb.h | 0 lib/nanopb/{src => include}/pb_common.h | 0 lib/nanopb/{src => include}/pb_decode.h | 0 lib/nanopb/{src => include}/pb_encode.h | 0 platformio.ini | 2 +- 5 files changed, 1 insertion(+), 1 deletion(-) rename lib/nanopb/{src => include}/pb.h (100%) rename lib/nanopb/{src => include}/pb_common.h (100%) rename lib/nanopb/{src => include}/pb_decode.h (100%) rename lib/nanopb/{src => include}/pb_encode.h (100%) diff --git a/lib/nanopb/src/pb.h b/lib/nanopb/include/pb.h similarity index 100% rename from lib/nanopb/src/pb.h rename to lib/nanopb/include/pb.h diff --git a/lib/nanopb/src/pb_common.h b/lib/nanopb/include/pb_common.h similarity index 100% rename from lib/nanopb/src/pb_common.h rename to lib/nanopb/include/pb_common.h diff --git a/lib/nanopb/src/pb_decode.h b/lib/nanopb/include/pb_decode.h similarity index 100% rename from lib/nanopb/src/pb_decode.h rename to lib/nanopb/include/pb_decode.h diff --git a/lib/nanopb/src/pb_encode.h b/lib/nanopb/include/pb_encode.h similarity index 100% rename from lib/nanopb/src/pb_encode.h rename to lib/nanopb/include/pb_encode.h diff --git a/platformio.ini b/platformio.ini index 46c72b145..1db164b19 100644 --- a/platformio.ini +++ b/platformio.ini @@ -31,7 +31,7 @@ board_build.partitions = partition-table.csv ; note: we add src to our include search path so that lmic_project_config can override ; FIXME: fix lib/BluetoothOTA dependency back on src/ so we can remove -Isrc -build_flags = -Wno-missing-field-initializers -Isrc -Isrc/rf95 -Isrc/mesh -Os -Wl,-Map,.pio/build/output.map +build_flags = -Wno-missing-field-initializers -Isrc -Isrc/rf95 -Isrc/mesh -Ilib/nanopb/include -Os -Wl,-Map,.pio/build/output.map -DAXP_DEBUG_PORT=Serial -DHW_VERSION_${sysenv.COUNTRY} -DAPP_VERSION=${sysenv.APP_VERSION} From cceecf5f8e6ef6a4791f74e8bbca291c23e1a81b Mon Sep 17 00:00:00 2001 From: geeksville Date: Mon, 27 Apr 2020 09:36:39 -0700 Subject: [PATCH 6/6] 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