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 5e73afe..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(){ @@ -263,7 +268,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; }