diff --git a/RSMS1AMessageBuilder.cpp b/RSMS1AMessageBuilder.cpp index 8d46f9b..e7d3d34 100644 --- a/RSMS1AMessageBuilder.cpp +++ b/RSMS1AMessageBuilder.cpp @@ -21,7 +21,7 @@ #include "RSMS1AMessageBuilder.h" #include "StringUtils.h" -std::vector CRSMS1AMessageBuilder::m_charsToEscape = {-17, 0, 17, 19, -2, -25, 26, -3, -1, 36, 13, 44}; +std::vector CRSMS1AMessageBuilder::m_charsToEscape = {-17, 0, 17, 19, -2, -25, 26, -3, -1, 36, 13, 44}; void CRSMS1AMessageBuilder::buildMessage(std::string& message, const std::string& sender, const std::string& recipient, const std::string body) { @@ -30,28 +30,28 @@ void CRSMS1AMessageBuilder::buildMessage(std::string& message, const std::string escapeBody(bodyTmp, body + (char)bodyCrc); std::string header = CStringUtils::string_format("%s,%s,0011", sender.c_str(), recipient.c_str()); - char c1, c2; + signed char c1, c2; calcMsgIcomCRC(header, c1, c2); header.push_back(c1); header.push_back(c2); message = "$$Msg," + header + bodyTmp + '\r'; } -char CRSMS1AMessageBuilder::calculateBodyCRC(const std::string& body) +signed char CRSMS1AMessageBuilder::calculateBodyCRC(const std::string& body) { if(body.length() == 1) return body[0]; - unsigned int num = 0; - for(auto c : body) { + signed int num = 0; + for(signed char c : body) { num += c; } - auto res = (num & 255); + signed int res = (num & 255); if(res >= 128) res -= 128; - return (char)res; + return (signed char)res; } void CRSMS1AMessageBuilder::escapeBody(std::string& output, const std::string& body) @@ -65,18 +65,18 @@ void CRSMS1AMessageBuilder::escapeBody(std::string& output, const std::string& b } } -void CRSMS1AMessageBuilder::calcMsgIcomCRC(const std::string& msg, char& c1, char& c2) +void CRSMS1AMessageBuilder::calcMsgIcomCRC(const std::string& msg, signed char& c1, signed char& c2) { int num = 0; for(unsigned int i = 0U; i < msg.length(); i++) { num += msg[i]; } - c1 = doWhatever((char)((num >> 4) & 15)); - c2 = doWhatever((char)(num & 15)); + c1 = doWhatever((signed char)((num >> 4) & 15)); + c2 = doWhatever((signed char)(num & 15)); } -char CRSMS1AMessageBuilder::doWhatever(char b2) { +signed char CRSMS1AMessageBuilder::doWhatever(signed char b2) { int i; int i2 = b2 & 255; if (i2 >= 0 && i2 <= 9) { @@ -86,7 +86,7 @@ char CRSMS1AMessageBuilder::doWhatever(char b2) { } else { i = b2 + 55; } - return (char) i; + return (signed char) i; } RSMS1A_PARSE_STATUS CRSMS1AMessageBuilder::parseMessage(std::string& sender, std::string& recipient, std::string& body, const std::string& message) diff --git a/RSMS1AMessageBuilder.h b/RSMS1AMessageBuilder.h index 2b13d4b..142c0e2 100644 --- a/RSMS1AMessageBuilder.h +++ b/RSMS1AMessageBuilder.h @@ -34,12 +34,12 @@ public: static RSMS1A_PARSE_STATUS parseMessage(std::string& sender, std::string& recipient, std::string& body, const std::string& message); private: - static void calcMsgIcomCRC(const std::string& msg, char& c1, char& c2); + static void calcMsgIcomCRC(const std::string& msg, signed char& c1, signed char& c2); static void escapeBody(std::string& output, const std::string& body); static void unescapeBody(std::string& output, const std::string& body); static void escapeBytes(std::vector output, const std::vector input); - static char calculateBodyCRC(const std::string& body); - static char doWhatever(char b2); + static signed char calculateBodyCRC(const std::string& body); + static signed char doWhatever(signed char b2); - static std::vector m_charsToEscape; + static std::vector m_charsToEscape; }; \ No newline at end of file diff --git a/Tests/Utils/swap_endian_be.cpp b/Tests/Utils/swap_endian_be.cpp index 7099a0e..8e3a817 100644 --- a/Tests/Utils/swap_endian_be.cpp +++ b/Tests/Utils/swap_endian_be.cpp @@ -19,6 +19,8 @@ #include #include "../../Utils.h" +#include "../../StringUtils.h" +#include "../../Log.h" class Utils_swap_endian_be : public ::testing::Test { @@ -31,4 +33,13 @@ TEST_F(Utils_swap_endian_be, SwapUINT32_be) { uint32_t res = CUtils::swap_endian_be(test); EXPECT_EQ(res, 0x12345678U); -} \ No newline at end of file +} + + +TEST_F(Utils_swap_endian_be, blabla) { + std::vector charsToEscape = {-17, 0, 17, 19, 128-2, -25, 26, 128-3, -1, 36, 13, 44}; + + for(unsigned int i = 1U; i < charsToEscape.size(); i++) { + CLog::logDebug("%08x - %08x", charsToEscape[i], (unsigned char)charsToEscape[i]); + } +}