Add internal protocol decoding reset

pull/122/head
D.R.racer 2021-09-09 08:29:52 +02:00 committed by DRracer
parent 8a5614844c
commit 89dcafbcef
2 changed files with 17 additions and 1 deletions

View File

@ -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) {

View File

@ -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