diff --git a/src/lib/ebus/symbol.cpp b/src/lib/ebus/symbol.cpp index 9e3702da..9075d451 100644 --- a/src/lib/ebus/symbol.cpp +++ b/src/lib/ebus/symbol.cpp @@ -79,7 +79,7 @@ result_t SymbolString::parseHex(const string& str, const bool isEscaped) return RESULT_OK; } -const string SymbolString::getDataStr(const bool unescape) +const string SymbolString::getDataStr(const bool unescape, const bool skipLastSymbol) { stringstream sstr; bool previousEscape = false; @@ -87,19 +87,20 @@ const string SymbolString::getDataStr(const bool unescape) for (size_t i = 0; i < m_data.size(); i++) { unsigned char value = m_data[i]; if (m_unescapeState == 0 && unescape && previousEscape) { - if (value == 0x00) - sstr << "a9"; // ESC - else if (value == 0x01) - sstr << "aa"; // SYN - else - sstr << "XX"; // invalid escape sequence - + if (!skipLastSymbol || i+1 < m_data.size()) { + if (value == 0x00) + sstr << "a9"; // ESC + else if (value == 0x01) + sstr << "aa"; // SYN + else + sstr << "XX"; // invalid escape sequence + } previousEscape = false; } else if (m_unescapeState == 0 && unescape && value == ESC) { previousEscape = true; // escape sequence not yet finished } - else { + else if (!skipLastSymbol || i+1 < m_data.size()) { sstr << nouppercase << setw(2) << hex << setfill('0') << static_cast(value); } diff --git a/src/lib/ebus/symbol.h b/src/lib/ebus/symbol.h index e52b5593..7914934d 100644 --- a/src/lib/ebus/symbol.h +++ b/src/lib/ebus/symbol.h @@ -67,9 +67,10 @@ public: /** * Returns the symbols as hex string. * @param unescape whether to unescape an escaped instance. + * @param skipLastSymbol whether to skip the last symbol (probably the CRC). * @return the symbols as hex string. */ - const string getDataStr(const bool unescape=true); + const string getDataStr(const bool unescape=true, const bool skipLastSymbol=true); /** * Returns a reference to the symbol at the specified index. diff --git a/src/lib/ebus/test/test_data.cpp b/src/lib/ebus/test/test_data.cpp index f3dbca50..1d9f14f1 100644 --- a/src/lib/ebus/test/test_data.cpp +++ b/src/lib/ebus/test/test_data.cpp @@ -26,6 +26,7 @@ using namespace std; void verify(bool expectFailMatch, string type, string input, bool match, string expectStr, string gotStr) { + match = match && expectStr == gotStr; if (expectFailMatch) { if (match) cout << " failed " << type << " match >" << input @@ -301,14 +302,14 @@ int main() ostringstream output; SymbolString writeMstr(false); - result = writeMstr.parseHex(mstr.getDataStr().substr(0, 10)); + result = writeMstr.parseHex(mstr.getDataStr(true, false).substr(0, 10)); if (result != RESULT_OK) { - cout << " parse \"" << mstr.getDataStr().substr(0, 10) << "\" error: " << getResultCode(result) << endl; + cout << " parse \"" << mstr.getDataStr(true, false).substr(0, 10) << "\" error: " << getResultCode(result) << endl; } SymbolString writeSstr(false); - result = writeSstr.parseHex(sstr.getDataStr().substr(0, 2)); + result = writeSstr.parseHex(sstr.getDataStr(true, false).substr(0, 2)); if (result != RESULT_OK) { - cout << " parse \"" << sstr.getDataStr().substr(0, 2) << "\" error: " << getResultCode(result) << endl; + cout << " parse \"" << sstr.getDataStr(true, false).substr(0, 2) << "\" error: " << getResultCode(result) << endl; } result = fields->read(pt_masterData, mstr, 0, output, false, verbose); if (result >= RESULT_OK) { diff --git a/src/lib/ebus/test/test_symbol.cpp b/src/lib/ebus/test/test_symbol.cpp index 8a8b273c..e3376d26 100644 --- a/src/lib/ebus/test/test_symbol.cpp +++ b/src/lib/ebus/test/test_symbol.cpp @@ -31,7 +31,7 @@ int main () if (result != RESULT_OK) std::cout << "parse escaped error: " << getResultCode(result) << std::endl; - std::string gotStr = sstr.getDataStr(false), expectStr = "10feb5050427a90015a90177"; + std::string gotStr = sstr.getDataStr(false, false), expectStr = "10feb5050427a90015a90177"; if (strcasecmp(gotStr.c_str(), expectStr.c_str()) == 0) std::cout << "parse escaped OK" << std::endl; @@ -51,7 +51,7 @@ int main () << std::setfill('0') << static_cast(expectCrc) << std::endl; - gotStr = sstr.getDataStr(), expectStr = "10feb5050427a915aa77"; + gotStr = sstr.getDataStr(true, false), expectStr = "10feb5050427a915aa77"; if (strcasecmp(gotStr.c_str(), expectStr.c_str()) == 0) std::cout << "unescape OK" << std::endl; @@ -64,7 +64,7 @@ int main () if (result != RESULT_OK) std::cout << "parse unescaped error: " << getResultCode(result) << std::endl; - gotStr = sstr.getDataStr(); + gotStr = sstr.getDataStr(true, false); if (strcasecmp(gotStr.c_str(), expectStr.c_str()) == 0) std::cout << "parse unescaped OK" << std::endl;