From 178e8009699fedad85fe88ca4305df3375487a88 Mon Sep 17 00:00:00 2001 From: geeksville Date: Mon, 27 Apr 2020 08:10:17 -0700 Subject: [PATCH] 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