diff --git a/src/modules/protocol.cpp b/src/modules/protocol.cpp index 3f2ebf6..8a86ef3 100644 --- a/src/modules/protocol.cpp +++ b/src/modules/protocol.cpp @@ -76,10 +76,12 @@ DecodeStatus Protocol::DecodeRequest(uint8_t c) { } uint8_t Protocol::EncodeRequest(const RequestMsg &msg, uint8_t *txbuff) { + constexpr uint8_t reqSize = 3; txbuff[0] = (uint8_t)msg.code; txbuff[1] = msg.value + '0'; txbuff[2] = '\n'; - return 3; + return reqSize; + static_assert(reqSize <= MaxRequestSize(), "Request message length exceeded the maximum size, increase the magic constant in MaxRequestSize()"); } DecodeStatus Protocol::DecodeResponse(uint8_t c) { diff --git a/src/modules/protocol.h b/src/modules/protocol.h index 87f5b95..5c1527c 100644 --- a/src/modules/protocol.h +++ b/src/modules/protocol.h @@ -96,6 +96,10 @@ public: /// @returns number of bytes written into txbuff static uint8_t EncodeRequest(const RequestMsg &msg, uint8_t *txbuff); + /// @returns the maximum byte length necessary to encode a request message + /// Beneficial in case of pre-allocating a buffer for enconding a RequestMsg. + static constexpr uint8_t MaxRequestSize() { return 3; } + /// Encode generic response Command Accepted or Rejected /// @param msg source request message for this response /// @param ar code of response parameter @@ -131,6 +135,16 @@ public: /// @returns the most recently lexed response message inline const ResponseMsg GetResponseMsg() const { return responseMsg; } + /// resets the internal request decoding state (typically after an error) + void ResetRequestDecoder() { + rqState = RequestStates::Code; + } + + /// resets the internal response decoding state (typically after an error) + void ResetResponseDecoder() { + rspState = ResponseStates::RequestCode; + } + private: enum class RequestStates : uint8_t { Code, ///< starting state - expects message code