diff --git a/.github/workflows/arduino-lint.yml b/.github/workflows/arduino-lint.yml index a85a4b5..a897cdb 100644 --- a/.github/workflows/arduino-lint.yml +++ b/.github/workflows/arduino-lint.yml @@ -24,6 +24,6 @@ jobs: uses: arduino/arduino-lint-action@v2 with: compliance: strict - library-manager: submit # remember to change to 'update' after the library is published on the libraries index + library-manager: update # remember to change to 'update' after the library is published on the libraries index # Always use this setting for official repositories. Remove for 3rd party projects. official: true \ No newline at end of file diff --git a/.github/workflows/compile-examples.yml b/.github/workflows/compile-examples.yml index da9e4fe..b0443ba 100644 --- a/.github/workflows/compile-examples.yml +++ b/.github/workflows/compile-examples.yml @@ -61,6 +61,8 @@ jobs: artifact-name-suffix: arduino-renesas_portenta-portenta_c33 - fqbn: arduino:renesas_uno:unor4wifi artifact-name-suffix: arduino-renesas_uno-unor4wifi + - fqbn: arduino:zephyr:unoq + artifact-name-suffix: arduino-zephyr-unoq steps: - name: Checkout diff --git a/library.json b/library.json index 94d97ec..9666cda 100644 --- a/library.json +++ b/library.json @@ -4,14 +4,14 @@ "description": "A MessagePack RPC library for Arduino", "repository": { "type": "git", - "url": "https://github.com/bcmi-labs/Arduino_RPClite" + "url": "https://github.com/arduino-libraries/Arduino_RPCLite" }, "authors": { "name": "Lucio Rossi", "url": "https://github.com/eigen-value", "maintainer": true }, - "version": "0.2.0", + "version": "0.2.1", "license": "MPL2.0", "frameworks": "arduino", "platforms": "*", diff --git a/library.properties b/library.properties index 1a36aeb..de0055a 100644 --- a/library.properties +++ b/library.properties @@ -1,10 +1,10 @@ name=Arduino_RPClite -version=0.2.0 +version=0.2.1 author=Arduino, Lucio Rossi (eigen-value) maintainer=Arduino, Lucio Rossi (eigen-value) sentence=A MessagePack RPC library for Arduino paragraph=allows to create a client/server architecture using MessagePack as the serialization format. It follows the MessagePack-RPC protocol specification. It is designed to be lightweight and easy to use, making it suitable for embedded systems and IoT applications. category=Communication -url=https://www.arduino.cc/ +url=https://github.com/arduino-libraries/Arduino_RPCLite architectures=* depends=MsgPack (>=0.4.2) diff --git a/src/decoder.h b/src/decoder.h index 3636646..d0716ca 100644 --- a/src/decoder.h +++ b/src/decoder.h @@ -6,20 +6,22 @@ This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ #ifndef RPCLITE_DECODER_H #define RPCLITE_DECODER_H +// MsgPack log level +#define DEBUGLOG_DEFAULT_LOG_LEVEL_WARN + #include "MsgPack.h" #include "transport.h" #include "rpclite_utils.h" +#include "error.h" using namespace RpcUtils::detail; #define MIN_RPC_BYTES 4 -#define CHUNK_SIZE 32 template class RpcDecoder { @@ -78,7 +80,9 @@ class RpcDecoder { // This should never happen error.code = PARSING_ERR; error.traceback = "Unexpected response type"; - crop_response(true); + consume(_response_size, _response_offset); + if (_response_offset == 0) reset_packet(); + _discarded_packets++; return true; } @@ -86,7 +90,9 @@ class RpcDecoder { // This should never happen error.code = PARSING_ERR; error.traceback = "Unexpected RPC response size"; - crop_response(true); + consume(_response_size, _response_offset); + if (_response_offset == 0) reset_packet(); + _discarded_packets++; return true; } @@ -95,19 +101,25 @@ class RpcDecoder { if (!unpacker.deserialize(nil, result)) { error.code = PARSING_ERR; error.traceback = "Result not parsable (check type)"; - crop_response(true); + consume(_response_size, _response_offset); + if (_response_offset == 0) reset_packet(); + _discarded_packets++; return true; } } else { // RPC returned an error if (!unpacker.deserialize(error, nil)) { error.code = PARSING_ERR; error.traceback = "RPC Error not parsable (check type)"; - crop_response(true); + consume(_response_size, _response_offset); + if (_response_offset == 0) reset_packet(); + _discarded_packets++; return true; } } - crop_response(false); + if (_response_offset == 0) reset_packet(); + consume(_response_size, _response_offset); + return true; } @@ -190,15 +202,13 @@ class RpcDecoder { size_t offset = 0; if (packet_incoming()) { - if (response_queued()) { - return; - } - offset = _response_offset; + if (response_queued()) return; // parsing complete + offset = _response_offset; // looking for a RESP } size_t bytes_checked = 0; size_t container_size; - int type; + int type = NO_MSG; MsgPack::Unpacker unpacker; while (bytes_checked + offset < _bytes_stored){ @@ -220,24 +230,16 @@ class RpcDecoder { break; // Not a valid RPC format } - if (offset == 0) { // that's the first packet + if (offset == 0) { _packet_type = type; _packet_size = bytes_checked; - if (type == RESP_MSG) { // and it is for a client - _response_offset = 0; - _response_size = bytes_checked; - } else if (!response_queued()) { - _response_offset = bytes_checked; - _response_size = 0; - } + } + + if (type == RESP_MSG) { + _response_offset = offset; + _response_size = bytes_checked; // response queued } else { - if (type == RESP_MSG) { // we have a response packet in the queue - _response_offset = offset; - _response_size = bytes_checked; - } else { // look further - _response_offset = offset + bytes_checked; - _response_size = 0; - } + _response_offset = offset + bytes_checked; } break; @@ -250,7 +252,7 @@ class RpcDecoder { bool packet_incoming() const { return _packet_size >= MIN_RPC_BYTES; } bool response_queued() const { - return (_response_offset < _bytes_stored) && (_response_size > 0); + return _response_size > 0; } int packet_type() const { return _packet_type; } @@ -303,23 +305,9 @@ class RpcDecoder { } reset_packet(); - if (_response_offset >= packet_size) { - _response_offset -= packet_size; - } return consume(packet_size); } - void crop_response(bool discard) { - consume(_response_size, _response_offset); - if (_response_offset==0) { // the response was in the first position - reset_packet(); - } - reset_response(); - if (discard) { - _discarded_packets++; - } - } - void discard() { consume(_packet_size); reset_packet(); @@ -332,7 +320,7 @@ class RpcDecoder { } void reset_response() { - _response_offset = _bytes_stored; + _response_offset = 0; _response_size = 0; } @@ -345,10 +333,16 @@ class RpcDecoder { _raw_buffer[i] = _raw_buffer[i+size]; } + if (_response_offset >= offset + size) { + _response_offset -= size; + } else { + reset_response(); + } + _bytes_stored = remaining_bytes; return size; } }; -#endif \ No newline at end of file +#endif diff --git a/src/error.h b/src/error.h index c98cb7d..d177c52 100644 --- a/src/error.h +++ b/src/error.h @@ -14,6 +14,9 @@ #include +// MsgPack log level +#define DEBUGLOG_DEFAULT_LOG_LEVEL_WARN + #include "MsgPack.h" #define NO_ERR 0x00 @@ -43,4 +46,4 @@ struct RpcError { MSGPACK_DEFINE(code, traceback); // -> [code, traceback] }; -#endif \ No newline at end of file +#endif