From c6b89faf8a3a28c9f294eabc4514bf01d14bdb09 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Thu, 5 Jun 2025 15:11:12 +0200 Subject: [PATCH 1/2] bugfix: don't overwrite random memory on flush_buffer() --- src/decoder.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/decoder.h b/src/decoder.h index 5e73afe..296b006 100644 --- a/src/decoder.h +++ b/src/decoder.h @@ -263,7 +263,7 @@ class RpcDecoder { inline bool buffer_full() const { return _bytes_stored == BufferSize; } inline bool buffer_empty() const { return _bytes_stored == 0;} inline void flush_buffer() { - uint8_t* discard_buf; + uint8_t discard_buf[CHUNK_SIZE]; while (_transport.read(discard_buf, CHUNK_SIZE) > 0); _bytes_stored = 0; } From 351ca5e8eb008c25d8469cfcfbbcc0e5fda71874 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Thu, 5 Jun 2025 15:11:45 +0200 Subject: [PATCH 2/2] speedup decoding considerably --- src/SerialTransport.h | 17 ++--------------- src/client.h | 5 +++-- src/decoder.h | 11 ++++++++--- 3 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/SerialTransport.h b/src/SerialTransport.h index 53c07d3..4cb9f14 100644 --- a/src/SerialTransport.h +++ b/src/SerialTransport.h @@ -30,21 +30,8 @@ class SerialTransport: public ITransport { } size_t read(uint8_t* buffer, size_t size) override { - - size_t r_size = 0; - - while (_stream->available()){ - if (r_size == size){ - return r_size; - } - buffer[r_size] = _stream->read(); - r_size++; - // TODO the following delay is essential for GIGA to work. Is it worth making giga-specific? - delay(1); - } - - return r_size; - + _stream->setTimeout(0); + return _stream->readBytes(buffer, size); } size_t read_byte(uint8_t& r) override { diff --git a/src/client.h b/src/client.h index 5041edb..860f238 100644 --- a/src/client.h +++ b/src/client.h @@ -32,8 +32,9 @@ class RPCClient { RpcError error; // blocking call while (!decoder.get_response(msg_id, result, error)){ - decoder.process(); - delay(1); + if (!decoder.process()) { + delay(1); + } } lastError.code = error.code; diff --git a/src/decoder.h b/src/decoder.h index 296b006..b8c52d0 100644 --- a/src/decoder.h +++ b/src/decoder.h @@ -168,8 +168,12 @@ class RpcDecoder { } - void process(){ - if (advance()) parse_packet(); + bool process(){ + if (advance()) { + parse_packet(); + return true; + } + return false; } // Fill the raw buffer to its capacity @@ -188,8 +192,9 @@ class RpcDecoder { delay(1); } } + return true; } - return true; + return false; } void parse_packet(){