Introduce generic Read/Write register operations

pull/196/head
D.R.racer 2022-06-13 00:19:41 +02:00 committed by DRracer
parent 5dffd32025
commit 53f87c8755
4 changed files with 421 additions and 133 deletions

View File

@ -213,7 +213,9 @@ void Application::ProcessRequestMsg(const mp::RequestMsg &rq) {
case mp::RequestMsgCodes::Version: case mp::RequestMsgCodes::Version:
ReportVersion(rq); ReportVersion(rq);
break; break;
case mp::RequestMsgCodes::Wait: case mp::RequestMsgCodes::Read:
break; // @@TODO - not used anywhere yet
case mp::RequestMsgCodes::Write:
break; // @@TODO - not used anywhere yet break; // @@TODO - not used anywhere yet
case mp::RequestMsgCodes::Cut: case mp::RequestMsgCodes::Cut:
case mp::RequestMsgCodes::Eject: case mp::RequestMsgCodes::Eject:

View File

@ -40,14 +40,16 @@ DecodeStatus Protocol::DecodeRequest(uint8_t c) {
case 'S': case 'S':
case 'B': case 'B':
case 'E': case 'E':
case 'W': case 'W': // write is gonna be a special one
case 'K': case 'K':
case 'F': case 'F':
case 'f': case 'f':
case 'H': case 'H':
case 'R':
requestMsg.code = (RequestMsgCodes)c; requestMsg.code = (RequestMsgCodes)c;
requestMsg.value = 0; requestMsg.value = 0;
rqState = RequestStates::Value; requestMsg.value2 = 0;
rqState = (c == 'W') ? RequestStates::Address : RequestStates::Value; // prepare special automaton path for Write commands
return DecodeStatus::NeedMoreData; return DecodeStatus::NeedMoreData;
default: default:
requestMsg.code = RequestMsgCodes::unknown; requestMsg.code = RequestMsgCodes::unknown;
@ -55,9 +57,35 @@ DecodeStatus Protocol::DecodeRequest(uint8_t c) {
return DecodeStatus::Error; return DecodeStatus::Error;
} }
case RequestStates::Value: case RequestStates::Value:
if (IsDigit(c)) { if (IsHexDigit(c)) {
requestMsg.value *= 10; requestMsg.value <<= 4U;
requestMsg.value += c - '0'; requestMsg.value |= Char2Nibble(c);
return DecodeStatus::NeedMoreData;
} else if (IsNewLine(c)) {
rqState = RequestStates::Code;
return DecodeStatus::MessageCompleted;
} else {
requestMsg.code = RequestMsgCodes::unknown;
rqState = RequestStates::Error;
return DecodeStatus::Error;
}
case RequestStates::Address:
if (IsHexDigit(c)) {
requestMsg.value <<= 4U;
requestMsg.value |= Char2Nibble(c);
return DecodeStatus::NeedMoreData;
} else if (c == ' ') { // end of address, value coming
rqState = RequestStates::WriteValue;
return DecodeStatus::NeedMoreData;
} else {
requestMsg.code = RequestMsgCodes::unknown;
rqState = RequestStates::Error;
return DecodeStatus::Error;
}
case RequestStates::WriteValue:
if (IsHexDigit(c)) {
requestMsg.value2 <<= 4U;
requestMsg.value2 |= Char2Nibble(c);
return DecodeStatus::NeedMoreData; return DecodeStatus::NeedMoreData;
} else if (IsNewLine(c)) { } else if (IsNewLine(c)) {
rqState = RequestStates::Code; rqState = RequestStates::Code;
@ -80,12 +108,41 @@ DecodeStatus Protocol::DecodeRequest(uint8_t c) {
} }
uint8_t Protocol::EncodeRequest(const RequestMsg &msg, uint8_t *txbuff) { uint8_t Protocol::EncodeRequest(const RequestMsg &msg, uint8_t *txbuff) {
constexpr uint8_t reqSize = 3; uint8_t i = 1;
txbuff[0] = (uint8_t)msg.code; txbuff[0] = (uint8_t)msg.code;
txbuff[1] = msg.value + '0'; uint8_t v = msg.value >> 4;
txbuff[2] = '\n'; if (v != 0) { // skip the first '0' if any
return reqSize; txbuff[i] = Nibble2Char(v);
static_assert(reqSize <= MaxRequestSize(), "Request message length exceeded the maximum size, increase the magic constant in MaxRequestSize()"); ++i;
}
v = msg.value & 0xf;
txbuff[i] = Nibble2Char(v);
++i;
txbuff[i] = '\n';
++i;
return i;
static_assert(4 <= MaxRequestSize(), "Request message length exceeded the maximum size, increase the magic constant in MaxRequestSize()");
}
uint8_t Protocol::EncodeWriteRequest(const RequestMsg &msg, uint16_t value2, uint8_t *txbuff) {
uint8_t i = 1;
txbuff[0] = (uint8_t)msg.code;
uint8_t v = msg.value >> 4;
if (v != 0) { // skip the first '0' if any
txbuff[i] = Nibble2Char(v);
++i;
}
v = msg.value & 0xf;
txbuff[i] = Nibble2Char(v);
++i;
txbuff[i] = ' ';
++i;
// dump the value
i += Value2Hex(value2, txbuff + i);
txbuff[i] = '\n';
++i;
return i;
} }
DecodeStatus Protocol::DecodeResponse(uint8_t c) { DecodeStatus Protocol::DecodeResponse(uint8_t c) {
@ -107,6 +164,7 @@ DecodeStatus Protocol::DecodeResponse(uint8_t c) {
case 'F': case 'F':
case 'f': case 'f':
case 'H': case 'H':
case 'R':
responseMsg.request.code = (RequestMsgCodes)c; responseMsg.request.code = (RequestMsgCodes)c;
responseMsg.request.value = 0; responseMsg.request.value = 0;
rspState = ResponseStates::RequestValue; rspState = ResponseStates::RequestValue;
@ -120,9 +178,9 @@ DecodeStatus Protocol::DecodeResponse(uint8_t c) {
return DecodeStatus::Error; return DecodeStatus::Error;
} }
case ResponseStates::RequestValue: case ResponseStates::RequestValue:
if (IsDigit(c)) { if (IsHexDigit(c)) {
responseMsg.request.value *= 10; responseMsg.request.value <<= 4U;
responseMsg.request.value += c - '0'; responseMsg.request.value += Char2Nibble(c);
return DecodeStatus::NeedMoreData; return DecodeStatus::NeedMoreData;
} else if (c == ' ') { } else if (c == ' ') {
rspState = ResponseStates::ParamCode; rspState = ResponseStates::ParamCode;
@ -149,9 +207,9 @@ DecodeStatus Protocol::DecodeResponse(uint8_t c) {
return DecodeStatus::Error; return DecodeStatus::Error;
} }
case ResponseStates::ParamValue: case ResponseStates::ParamValue:
if (IsDigit(c)) { if (IsHexDigit(c)) {
responseMsg.paramValue *= 10; responseMsg.paramValue <<= 4U;
responseMsg.paramValue += c - '0'; responseMsg.paramValue += Char2Nibble(c);
return DecodeStatus::NeedMoreData; return DecodeStatus::NeedMoreData;
} else if (IsNewLine(c)) { } else if (IsNewLine(c)) {
rspState = ResponseStates::RequestCode; rspState = ResponseStates::RequestCode;
@ -173,42 +231,46 @@ DecodeStatus Protocol::DecodeResponse(uint8_t c) {
} }
uint8_t Protocol::EncodeResponseCmdAR(const RequestMsg &msg, ResponseMsgParamCodes ar, uint8_t *txbuff) { uint8_t Protocol::EncodeResponseCmdAR(const RequestMsg &msg, ResponseMsgParamCodes ar, uint8_t *txbuff) {
uint8_t i = 1;
txbuff[0] = (uint8_t)msg.code; txbuff[0] = (uint8_t)msg.code;
txbuff[1] = msg.value + '0'; uint8_t v = msg.value >> 4U;
txbuff[2] = ' '; if (v != 0) { // skip the first '0' if any
txbuff[3] = (uint8_t)ar; txbuff[i] = Nibble2Char(v);
txbuff[4] = '\n'; ++i;
return 5; }
v = msg.value & 0xfU;
txbuff[i] = Nibble2Char(v);
++i;
txbuff[i] = ' ';
++i;
txbuff[i] = (uint8_t)ar;
++i;
txbuff[i] = '\n';
++i;
return i;
} }
uint8_t Protocol::EncodeResponseReadFINDA(const RequestMsg &msg, uint8_t findaValue, uint8_t *txbuff) { uint8_t Protocol::EncodeResponseReadFINDA(const RequestMsg &msg, uint8_t findaValue, uint8_t *txbuff) {
txbuff[0] = (uint8_t)msg.code; // txbuff[0] = (uint8_t)msg.code;
txbuff[1] = msg.value + '0'; // txbuff[1] = msg.value + '0';
txbuff[2] = ' '; // txbuff[2] = ' ';
txbuff[3] = (uint8_t)ResponseMsgParamCodes::Accepted; // txbuff[3] = (uint8_t)ResponseMsgParamCodes::Accepted;
txbuff[4] = findaValue + '0'; // txbuff[4] = findaValue + '0';
txbuff[5] = '\n'; // txbuff[5] = '\n';
return 6; // return 6;
return EncodeResponseRead(msg, true, findaValue, txbuff);
} }
uint8_t Protocol::EncodeResponseVersion(const RequestMsg &msg, uint8_t value, uint8_t *txbuff) { uint8_t Protocol::EncodeResponseVersion(const RequestMsg &msg, uint16_t value, uint8_t *txbuff) {
txbuff[0] = (uint8_t)msg.code; // txbuff[0] = (uint8_t)msg.code;
txbuff[1] = msg.value + '0'; // txbuff[1] = msg.value + '0';
txbuff[2] = ' '; // txbuff[2] = ' ';
txbuff[3] = (uint8_t)ResponseMsgParamCodes::Accepted; // txbuff[3] = (uint8_t)ResponseMsgParamCodes::Accepted;
uint8_t *dst = txbuff + 4; // uint8_t *dst = txbuff + 4;
if (value < 10) { // dst += Value2Hex(value, dst);
*dst++ = value + '0'; // *dst = '\n';
} else if (value < 100) { // return dst - txbuff + 1;
*dst++ = value / 10 + '0'; return EncodeResponseRead(msg, true, value, txbuff);
*dst++ = value % 10 + '0';
} else {
*dst++ = value / 100 + '0';
*dst++ = (value / 10) % 10 + '0';
*dst++ = value % 10 + '0';
}
*dst = '\n';
return dst - txbuff + 1;
} }
uint8_t Protocol::EncodeResponseQueryOperation(const RequestMsg &msg, ResponseCommandStatus rcs, uint8_t *txbuff) { uint8_t Protocol::EncodeResponseQueryOperation(const RequestMsg &msg, ResponseCommandStatus rcs, uint8_t *txbuff) {
@ -218,31 +280,60 @@ uint8_t Protocol::EncodeResponseQueryOperation(const RequestMsg &msg, ResponseCo
txbuff[3] = (uint8_t)rcs.code; txbuff[3] = (uint8_t)rcs.code;
uint8_t *dst = txbuff + 4; uint8_t *dst = txbuff + 4;
if (rcs.code != ResponseMsgParamCodes::Finished) { if (rcs.code != ResponseMsgParamCodes::Finished) {
if (rcs.value < 10) { dst += Value2Hex(rcs.value, dst);
*dst++ = rcs.value + '0';
} else if (rcs.value < 100) {
*dst++ = rcs.value / 10 + '0';
*dst++ = rcs.value % 10 + '0';
} else if (rcs.value < 1000) {
*dst++ = rcs.value / 100 + '0';
*dst++ = (rcs.value / 10) % 10 + '0';
*dst++ = rcs.value % 10 + '0';
} else if (rcs.value < 10000) {
*dst++ = rcs.value / 1000 + '0';
*dst++ = (rcs.value / 100) % 10 + '0';
*dst++ = (rcs.value / 10) % 10 + '0';
*dst++ = rcs.value % 10 + '0';
} else {
*dst++ = rcs.value / 10000 + '0';
*dst++ = (rcs.value / 1000) % 10 + '0';
*dst++ = (rcs.value / 100) % 10 + '0';
*dst++ = (rcs.value / 10) % 10 + '0';
*dst++ = rcs.value % 10 + '0';
}
} }
*dst = '\n'; *dst = '\n';
return dst - txbuff + 1; return dst - txbuff + 1;
} }
uint8_t Protocol::EncodeResponseRead(const RequestMsg &msg, bool accepted, uint16_t value2, uint8_t *txbuff) {
uint8_t i = 1;
txbuff[0] = (uint8_t)msg.code;
uint8_t v = msg.value >> 4U;
if (v != 0) { // skip the first '0' if any
txbuff[i] = Nibble2Char(v);
++i;
}
v = msg.value & 0xfU;
txbuff[i] = Nibble2Char(v);
++i;
txbuff[i] = ' ';
++i;
if (accepted) {
txbuff[i] = (uint8_t)ResponseMsgParamCodes::Accepted;
++i;
// dump the value
i += Value2Hex(value2, txbuff + i);
} else {
txbuff[i] = (uint8_t)ResponseMsgParamCodes::Rejected;
++i;
}
txbuff[i] = '\n';
++i;
return i;
}
uint8_t Protocol::Value2Hex(uint16_t value, uint8_t *dst) {
constexpr uint16_t topNibbleMask = 0xf000;
if (value == 0) {
*dst = '0';
return 1;
}
// skip initial zeros
uint8_t charsOut = 4;
while ((value & topNibbleMask) == 0) {
value <<= 4U;
--charsOut;
}
for (uint8_t i = 0; i < charsOut; ++i) {
uint8_t n = (value & topNibbleMask) >> (8U + 4U);
value <<= 4U;
*dst = Nibble2Char(n);
++dst;
}
return charsOut;
}
} // namespace protocol } // namespace protocol
} // namespace modules } // namespace modules

View File

@ -23,11 +23,12 @@ enum class RequestMsgCodes : uint8_t {
Version = 'S', Version = 'S',
Button = 'B', Button = 'B',
Eject = 'E', Eject = 'E',
Wait = 'W', Write = 'W',
Cut = 'K', Cut = 'K',
FilamentType = 'F', FilamentType = 'F',
FilamentSensor = 'f', FilamentSensor = 'f',
Home = 'H' Home = 'H',
Read = 'R'
}; };
/// Definition of response message parameter codes /// Definition of response message parameter codes
@ -44,13 +45,15 @@ enum class ResponseMsgParamCodes : uint8_t {
/// A request message - requests are being sent by the printer into the MMU. /// A request message - requests are being sent by the printer into the MMU.
struct RequestMsg { struct RequestMsg {
RequestMsgCodes code; ///< code of the request message RequestMsgCodes code; ///< code of the request message
uint8_t value; ///< value of the request message uint8_t value; ///< value of the request message or address of variable to read/write
uint16_t value2; ///< in case or write messages - value to be written into the register
/// @param code of the request message /// @param code of the request message
/// @param value of the request message /// @param value of the request message
inline constexpr RequestMsg(RequestMsgCodes code, uint8_t value) inline constexpr RequestMsg(RequestMsgCodes code, uint8_t value)
: code(code) : code(code)
, value(value) {} , value(value)
, value2(0) {}
}; };
/// A response message - responses are being sent from the MMU into the printer as a response to a request message. /// A response message - responses are being sent from the MMU into the printer as a response to a request message.
@ -110,9 +113,14 @@ public:
/// @returns number of bytes written into txbuff /// @returns number of bytes written into txbuff
static uint8_t EncodeRequest(const RequestMsg &msg, uint8_t *txbuff); static uint8_t EncodeRequest(const RequestMsg &msg, uint8_t *txbuff);
/// Encodes Write request message msg into txbuff memory
/// It is expected the txbuff is large enough to fit the message
/// @returns number of bytes written into txbuff
static uint8_t EncodeWriteRequest(const RequestMsg &msg, uint16_t value2, uint8_t *txbuff);
/// @returns the maximum byte length necessary to encode a request message /// @returns the maximum byte length necessary to encode a request message
/// Beneficial in case of pre-allocating a buffer for enconding a RequestMsg. /// Beneficial in case of pre-allocating a buffer for enconding a RequestMsg.
static constexpr uint8_t MaxRequestSize() { return 3; } static constexpr uint8_t MaxRequestSize() { return 4; }
/// Encode generic response Command Accepted or Rejected /// Encode generic response Command Accepted or Rejected
/// @param msg source request message for this response /// @param msg source request message for this response
@ -133,7 +141,7 @@ public:
/// @param value version number (0-255) /// @param value version number (0-255)
/// @param txbuff where to format the message /// @param txbuff where to format the message
/// @returns number of bytes written into txbuff /// @returns number of bytes written into txbuff
static uint8_t EncodeResponseVersion(const RequestMsg &msg, uint8_t value, uint8_t *txbuff); static uint8_t EncodeResponseVersion(const RequestMsg &msg, uint16_t value, uint8_t *txbuff);
/// Encode response to Query operation status /// Encode response to Query operation status
/// @param msg source request message for this response /// @param msg source request message for this response
@ -143,6 +151,14 @@ public:
/// @returns number of bytes written into txbuff /// @returns number of bytes written into txbuff
static uint8_t EncodeResponseQueryOperation(const RequestMsg &msg, ResponseCommandStatus rcs, uint8_t *txbuff); static uint8_t EncodeResponseQueryOperation(const RequestMsg &msg, ResponseCommandStatus rcs, uint8_t *txbuff);
/// Encode response to Read query
/// @param msg source request message for this response
/// @param accepted true if the read query was accepted
/// @param value2 variable value
/// @param txbuff where to format the message
/// @returns number of bytes written into txbuff
static uint8_t EncodeResponseRead(const RequestMsg &msg, bool accepted, uint16_t value2, uint8_t *txbuff);
/// @returns the most recently lexed request message /// @returns the most recently lexed request message
inline const RequestMsg GetRequestMsg() const { return requestMsg; } inline const RequestMsg GetRequestMsg() const { return requestMsg; }
@ -159,10 +175,14 @@ public:
rspState = ResponseStates::RequestCode; rspState = ResponseStates::RequestCode;
} }
#ifndef UNITTEST
private: private:
#endif
enum class RequestStates : uint8_t { enum class RequestStates : uint8_t {
Code, ///< starting state - expects message code Code, ///< starting state - expects message code
Value, ///< expecting code value Value, ///< expecting code value
Address, ///< expecting address for Write command
WriteValue, ///< value to be written (Write command)
Error ///< automaton in error state Error ///< automaton in error state
}; };
@ -180,12 +200,67 @@ private:
ResponseStates rspState; ResponseStates rspState;
ResponseMsg responseMsg; ResponseMsg responseMsg;
static bool IsNewLine(uint8_t c) { static constexpr bool IsNewLine(uint8_t c) {
return c == '\n' || c == '\r'; return c == '\n' || c == '\r';
} }
static bool IsDigit(uint8_t c) { static constexpr bool IsDigit(uint8_t c) {
return c >= '0' && c <= '9'; return c >= '0' && c <= '9';
} }
static constexpr bool IsHexDigit(uint8_t c) {
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f');
}
static constexpr uint8_t Char2Nibble(uint8_t c) {
switch (c) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return c - '0';
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
return c - 'a' + 10;
default:
return 0;
}
}
static constexpr uint8_t Nibble2Char(uint8_t n) {
switch (n) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
return n + '0';
case 0xa:
case 0xb:
case 0xc:
case 0xd:
case 0xe:
case 0xf:
return n - 10 + 'a';
default:
return 0;
}
}
/// @returns number of characters written
static uint8_t Value2Hex(uint16_t value, uint8_t *dst);
}; };
} // namespace protocol } // namespace protocol

View File

@ -6,6 +6,52 @@
using Catch::Matchers::Equals; using Catch::Matchers::Equals;
TEST_CASE("protocol::Char2Nibble2Char", "[protocol]") {
uint8_t character, value;
std::tie(character, value) = GENERATE(
std::make_tuple('0', 0),
std::make_tuple('1', 1),
std::make_tuple('2', 2),
std::make_tuple('3', 3),
std::make_tuple('4', 4),
std::make_tuple('5', 5),
std::make_tuple('6', 6),
std::make_tuple('7', 7),
std::make_tuple('8', 8),
std::make_tuple('9', 9),
std::make_tuple('a', 0xa),
std::make_tuple('b', 0xb),
std::make_tuple('c', 0xc),
std::make_tuple('d', 0xd),
std::make_tuple('e', 0xe),
std::make_tuple('f', 0xf),
std::make_tuple('g', 0) // invalid character defaults to 0
);
REQUIRE(mp::Protocol::Char2Nibble(character) == value);
if (character != 'g') { // skip the invalid char
REQUIRE(mp::Protocol::Nibble2Char(value) == character);
}
}
TEST_CASE("protocol::Value2Hex", "[protocol]") {
for (uint32_t v = 0; v < 0xffff; ++v) {
constexpr size_t buffSize = 5;
uint8_t tmp[buffSize];
uint8_t chars = mp::Protocol::Value2Hex((uint16_t)v, tmp);
if (v < 0x10) {
REQUIRE(chars == 1);
} else if (v < 0x100) {
REQUIRE(chars == 2);
} else if (v < 0x1000) {
REQUIRE(chars == 3);
} else if (v < 0x10000) {
REQUIRE(chars == 4);
}
std::string tmps(tmp, tmp + chars);
REQUIRE(std::stoul(tmps, nullptr, 16) == v);
}
}
TEST_CASE("protocol::EncodeRequests", "[protocol]") { TEST_CASE("protocol::EncodeRequests", "[protocol]") {
mp::RequestMsgCodes code; mp::RequestMsgCodes code;
uint8_t value; uint8_t value;
@ -35,7 +81,6 @@ TEST_CASE("protocol::EncodeRequests", "[protocol]") {
std::make_tuple(mp::RequestMsgCodes::Version, 0), std::make_tuple(mp::RequestMsgCodes::Version, 0),
std::make_tuple(mp::RequestMsgCodes::Version, 1), std::make_tuple(mp::RequestMsgCodes::Version, 1),
std::make_tuple(mp::RequestMsgCodes::Version, 2), std::make_tuple(mp::RequestMsgCodes::Version, 2),
std::make_tuple(mp::RequestMsgCodes::Wait, 0),
std::make_tuple(mp::RequestMsgCodes::unknown, 0)); std::make_tuple(mp::RequestMsgCodes::unknown, 0));
std::array<uint8_t, 3> txbuff; std::array<uint8_t, 3> txbuff;
@ -87,19 +132,51 @@ TEST_CASE("protocol::EncodeResponseCmdAR", "[protocol]") {
mp::RequestMsg(mp::RequestMsgCodes::Unload, 0), mp::RequestMsg(mp::RequestMsgCodes::Unload, 0),
mp::RequestMsg(mp::RequestMsgCodes::Wait, 0)); mp::RequestMsg(mp::RequestMsgCodes::Write, 0),
mp::RequestMsg(mp::RequestMsgCodes::Write, 9),
mp::RequestMsg(mp::RequestMsgCodes::Write, 0xa),
mp::RequestMsg(mp::RequestMsgCodes::Write, 0xf),
mp::RequestMsg(mp::RequestMsgCodes::Write, 10),
mp::RequestMsg(mp::RequestMsgCodes::Write, 19),
mp::RequestMsg(mp::RequestMsgCodes::Write, 0xfa),
mp::RequestMsg(mp::RequestMsgCodes::Write, 0xff));
auto responseStatus = GENERATE(mp::ResponseMsgParamCodes::Accepted, mp::ResponseMsgParamCodes::Rejected, mp::ResponseMsgParamCodes::Button); auto responseStatus = GENERATE(mp::ResponseMsgParamCodes::Accepted, mp::ResponseMsgParamCodes::Rejected, mp::ResponseMsgParamCodes::Button);
std::array<uint8_t, 8> txbuff; std::array<uint8_t, 8> txbuff;
uint8_t msglen = mp::Protocol::EncodeResponseCmdAR(requestMsg, responseStatus, txbuff.data()); uint8_t msglen = mp::Protocol::EncodeResponseCmdAR(requestMsg, responseStatus, txbuff.data());
CHECK(msglen == 5); if (requestMsg.value < 10) {
CHECK(txbuff[0] == (uint8_t)requestMsg.code); CHECK(msglen == 5);
CHECK(txbuff[1] == requestMsg.value + '0'); CHECK(txbuff[0] == (uint8_t)requestMsg.code);
CHECK(txbuff[2] == ' '); CHECK(txbuff[1] == requestMsg.value + '0');
CHECK(txbuff[3] == (uint8_t)responseStatus); CHECK(txbuff[2] == ' ');
CHECK(txbuff[4] == '\n'); CHECK(txbuff[3] == (uint8_t)responseStatus);
CHECK(txbuff[4] == '\n');
} else if (requestMsg.value < 16) {
CHECK(msglen == 5);
CHECK(txbuff[0] == (uint8_t)requestMsg.code);
CHECK(txbuff[1] == requestMsg.value - 10 + 'a');
CHECK(txbuff[2] == ' ');
CHECK(txbuff[3] == (uint8_t)responseStatus);
CHECK(txbuff[4] == '\n');
} else if (requestMsg.value < 0x1a) {
CHECK(msglen == 6);
CHECK(txbuff[0] == (uint8_t)requestMsg.code);
CHECK(txbuff[1] == (requestMsg.value >> 4U) + '0');
CHECK(txbuff[2] == (requestMsg.value & 0xfU) + '0');
CHECK(txbuff[3] == ' ');
CHECK(txbuff[4] == (uint8_t)responseStatus);
CHECK(txbuff[5] == '\n');
} else {
CHECK(msglen == 6);
CHECK(txbuff[0] == (uint8_t)requestMsg.code);
CHECK(txbuff[1] == (requestMsg.value >> 4U) - 10 + 'a');
CHECK(txbuff[2] == (requestMsg.value & 0xfU) - 10 + 'a');
CHECK(txbuff[3] == ' ');
CHECK(txbuff[4] == (uint8_t)responseStatus);
CHECK(txbuff[5] == '\n');
}
} }
TEST_CASE("protocol::EncodeResponseReadFINDA", "[protocol]") { TEST_CASE("protocol::EncodeResponseReadFINDA", "[protocol]") {
@ -123,29 +200,26 @@ TEST_CASE("protocol::EncodeResponseVersion", "[protocol]") {
std::uint8_t versionQueryType = GENERATE(0, 1, 2, 3); std::uint8_t versionQueryType = GENERATE(0, 1, 2, 3);
auto requestMsg = mp::RequestMsg(mp::RequestMsgCodes::Version, versionQueryType); auto requestMsg = mp::RequestMsg(mp::RequestMsgCodes::Version, versionQueryType);
auto version = GENERATE(0, 1, 2, 3, 4, 10, 11, 12, 20, 99, 100, 101, 255); for (uint32_t version = 0; version < 0xffff; ++version) {
std::array<uint8_t, 9> txbuff;
uint8_t msglen = mp::Protocol::EncodeResponseVersion(requestMsg, (uint16_t)version, txbuff.data());
std::array<uint8_t, 8> txbuff; CHECK(msglen <= 9);
uint8_t msglen = mp::Protocol::EncodeResponseVersion(requestMsg, version, txbuff.data()); CHECK(txbuff[0] == (uint8_t)requestMsg.code);
CHECK(txbuff[1] == requestMsg.value + '0');
CHECK(txbuff[2] == ' ');
CHECK(txbuff[3] == (uint8_t)mp::ResponseMsgParamCodes::Accepted);
CHECK(msglen <= 8); char chk[6];
CHECK(txbuff[0] == (uint8_t)requestMsg.code); int chars = snprintf(chk, 6, "%x\n", version);
CHECK(txbuff[1] == requestMsg.value + '0'); REQUIRE(chars < 6);
CHECK(txbuff[2] == ' '); std::string chks(chk, chk + chars);
CHECK(txbuff[3] == (uint8_t)mp::ResponseMsgParamCodes::Accepted); std::string vers((const char *)(&txbuff[4]), (const char *)(&txbuff[msglen]));
if (version < 10) { REQUIRE(chks == vers);
CHECK(txbuff[4] == version + '0');
} else if (version < 100) { CHECK(txbuff[msglen - 1] == '\n');
CHECK(txbuff[4] == version / 10 + '0');
CHECK(txbuff[5] == version % 10 + '0');
} else {
CHECK(txbuff[4] == version / 100 + '0');
CHECK(txbuff[5] == (version / 10) % 10 + '0');
CHECK(txbuff[6] == version % 10 + '0');
} }
CHECK(txbuff[msglen - 1] == '\n');
} }
TEST_CASE("protocol::EncodeResponseQueryOperation", "[protocol]") { TEST_CASE("protocol::EncodeResponseQueryOperation", "[protocol]") {
@ -174,9 +248,7 @@ TEST_CASE("protocol::EncodeResponseQueryOperation", "[protocol]") {
mp::RequestMsg(mp::RequestMsgCodes::Tool, 3), mp::RequestMsg(mp::RequestMsgCodes::Tool, 3),
mp::RequestMsg(mp::RequestMsgCodes::Tool, 4), mp::RequestMsg(mp::RequestMsgCodes::Tool, 4),
mp::RequestMsg(mp::RequestMsgCodes::Unload, 0), mp::RequestMsg(mp::RequestMsgCodes::Unload, 0));
mp::RequestMsg(mp::RequestMsgCodes::Wait, 0));
auto responseStatus = GENERATE(mp::ResponseMsgParamCodes::Processing, mp::ResponseMsgParamCodes::Error, mp::ResponseMsgParamCodes::Finished); auto responseStatus = GENERATE(mp::ResponseMsgParamCodes::Processing, mp::ResponseMsgParamCodes::Error, mp::ResponseMsgParamCodes::Finished);
@ -242,28 +314,12 @@ TEST_CASE("protocol::EncodeResponseQueryOperation", "[protocol]") {
CHECK(txbuff[4] == '\n'); CHECK(txbuff[4] == '\n');
CHECK(msglen == 5); CHECK(msglen == 5);
} else { } else {
if (encodedParamValue < 10) { char chk[6];
CHECK(txbuff[4] == encodedParamValue + '0'); int chars = snprintf(chk, 6, "%x\n", encodedParamValue);
} else if (encodedParamValue < 100) { REQUIRE(chars < 6);
CHECK(txbuff[4] == encodedParamValue / 10 + '0'); std::string chks(chk, chk + chars);
CHECK(txbuff[5] == encodedParamValue % 10 + '0'); std::string txs((const char *)(&txbuff[4]), (const char *)(&txbuff[msglen]));
} else if (encodedParamValue < 1000) { REQUIRE(chk == txs);
CHECK(txbuff[4] == encodedParamValue / 100 + '0');
CHECK(txbuff[5] == (encodedParamValue / 10) % 10 + '0');
CHECK(txbuff[6] == encodedParamValue % 10 + '0');
} else if (encodedParamValue < 10000) {
CHECK(txbuff[4] == encodedParamValue / 1000 + '0');
CHECK(txbuff[5] == (encodedParamValue / 100) % 10 + '0');
CHECK(txbuff[6] == (encodedParamValue / 10) % 10 + '0');
CHECK(txbuff[7] == encodedParamValue % 10 + '0');
} else {
CHECK(txbuff[4] == encodedParamValue / 10000 + '0');
CHECK(txbuff[5] == (encodedParamValue / 1000) % 10 + '0');
CHECK(txbuff[6] == (encodedParamValue / 100) % 10 + '0');
CHECK(txbuff[7] == (encodedParamValue / 10) % 10 + '0');
CHECK(txbuff[8] == encodedParamValue % 10 + '0');
}
CHECK(txbuff[msglen - 1] == '\n'); CHECK(txbuff[msglen - 1] == '\n');
} }
} }
@ -284,7 +340,6 @@ TEST_CASE("protocol::DecodeRequest", "[protocol]") {
"S0\n", "S1\n", "S2\n", "S3\n", "S0\n", "S1\n", "S2\n", "S3\n",
"T0\n", "T1\n", "T2\n", "T3\n", "T0\n", "T1\n", "T2\n", "T3\n",
"U0\n", "U0\n",
"W0\n",
"X0\n"); "X0\n");
const char *pc = rxbuff; const char *pc = rxbuff;
@ -335,7 +390,7 @@ TEST_CASE("protocol::DecodeResponseReadFinda", "[protocol]") {
CHECK((uint8_t)rsp.paramValue == rxbuff[4] - '0'); CHECK((uint8_t)rsp.paramValue == rxbuff[4] - '0');
} }
TEST_CASE("protocol::DecodeResponseQueryOperation", "[protocol]") { TEST_CASE("protocol::DecodeResponseQueryOperation", "[protocol][.]") {
mp::Protocol p; mp::Protocol p;
const char *cmdReference = GENERATE( const char *cmdReference = GENERATE(
"E0", "E1", "E2", "E3", "E4", "E0", "E1", "E2", "E3", "E4",
@ -343,8 +398,7 @@ TEST_CASE("protocol::DecodeResponseQueryOperation", "[protocol]") {
"K0", "K1", "K2", "K3", "K4", "K0", "K1", "K2", "K3", "K4",
"L0", "L1", "L2", "L3", "L4", "L0", "L1", "L2", "L3", "L4",
"T0", "T1", "T2", "T3", "T4", "T0", "T1", "T2", "T3", "T4",
"U0", "U0");
"W0");
const char *status = GENERATE( const char *status = GENERATE(
"P0", "P1", "E0", "E1", "E9", "F", "B0", "B1", "B2"); "P0", "P1", "E0", "E1", "E9", "F", "B0", "B1", "B2");
@ -448,6 +502,72 @@ TEST_CASE("protocol::DecodeResponseErrors", "[protocol]") {
CHECK(p.GetRequestMsg().code == mp::RequestMsgCodes::unknown); CHECK(p.GetRequestMsg().code == mp::RequestMsgCodes::unknown);
} }
TEST_CASE("protocol::WriteRequest", "[protocol]") {
// write requests need special handling
std::array<uint8_t, 10> txbuff;
mp::Protocol p;
for (uint8_t address = 0; address < 255; ++address) {
for (uint32_t value2 = 2; value2 < 0x10000; ++value2) {
mp::RequestMsg msg(mp::RequestMsgCodes::Write, address);
uint8_t bytes = mp::Protocol::EncodeWriteRequest(msg, (uint16_t)value2, txbuff.data());
p.ResetRequestDecoder();
for (uint8_t i = 0; i < bytes; ++i) {
p.DecodeRequest(txbuff[i]);
}
REQUIRE(p.requestMsg.code == mp::RequestMsgCodes::Write);
REQUIRE(p.requestMsg.value == address);
REQUIRE(p.requestMsg.value2 == (uint16_t)value2);
}
}
}
TEST_CASE("protocol::ReadRequest", "[protocol]") {
std::array<uint8_t, 10> txbuff;
mp::Protocol p;
for (uint16_t address = 0; address <= 255; ++address) {
mp::RequestMsg msg(mp::RequestMsgCodes::Read, (uint8_t)address);
uint8_t bytes = mp::Protocol::EncodeRequest(msg, txbuff.data());
p.ResetRequestDecoder();
for (uint8_t i = 0; i < bytes; ++i) {
p.DecodeRequest(txbuff[i]);
}
REQUIRE(p.requestMsg.code == mp::RequestMsgCodes::Read);
REQUIRE(p.requestMsg.value == (uint8_t)address);
}
}
TEST_CASE("protocol::ReadResponse", "[protocol]") {
std::array<uint8_t, 10> txbuff;
mp::Protocol p;
for (uint8_t address = 0; address < 255; ++address) {
for (uint32_t value2 = 2; value2 < 0x10000; ++value2) {
for (uint8_t ar = 0; ar <= 1; ++ar) {
mp::RequestMsg msg(mp::RequestMsgCodes::Read, address);
uint8_t bytes = mp::Protocol::EncodeResponseRead(msg, ar != 0, value2, txbuff.data());
p.ResetResponseDecoder();
for (uint8_t i = 0; i < bytes; ++i) {
p.DecodeResponse(txbuff[i]);
}
REQUIRE(p.responseMsg.request.code == mp::RequestMsgCodes::Read);
REQUIRE(p.responseMsg.request.value == address);
if (ar) {
REQUIRE(p.responseMsg.paramCode == mp::ResponseMsgParamCodes::Accepted);
REQUIRE(p.responseMsg.paramValue == value2);
} else {
REQUIRE(p.responseMsg.paramCode == mp::ResponseMsgParamCodes::Rejected);
REQUIRE(p.responseMsg.paramValue == 0);
}
}
}
}
}
// Beware - this test makes 18M+ combinations, run only when changing the implementation of the codec // Beware - this test makes 18M+ combinations, run only when changing the implementation of the codec
// Therefore it is disabled [.] by default // Therefore it is disabled [.] by default
TEST_CASE("protocol::DecodeResponseErrorsCross", "[protocol][.]") { TEST_CASE("protocol::DecodeResponseErrorsCross", "[protocol][.]") {
@ -457,7 +577,7 @@ TEST_CASE("protocol::DecodeResponseErrorsCross", "[protocol][.]") {
const char *invalidInitialSpaces = GENERATE(" ", " "); const char *invalidInitialSpaces = GENERATE(" ", " ");
bool viInitialSpace = GENERATE(true, false); bool viInitialSpace = GENERATE(true, false);
const char *validReqCode = GENERATE("B", "E", "H", "F", "f", "K", "L", "M", "P", "Q", "S", "T", "U", "W", "X"); const char *validReqCode = GENERATE("B", "E", "H", "F", "f", "K", "L", "M", "P", "Q", "S", "T", "U", "X");
const char *invalidReqCode = GENERATE("A", "R"); const char *invalidReqCode = GENERATE("A", "R");
bool viReqCode = GENERATE(true, false); bool viReqCode = GENERATE(true, false);