From 47ed6dde3f449faecc8a1fa03f841e98396f9859 Mon Sep 17 00:00:00 2001 From: john30 Date: Mon, 17 Apr 2017 17:46:32 +0200 Subject: [PATCH] added SymbolString::adjustHeader(), avoid strcasecmp, code style --- src/ebusd/main.cpp | 3 ++- src/ebusd/mainloop.cpp | 12 ++++++------ src/lib/ebus/contrib/tem.cpp | 2 +- src/lib/ebus/datatype.cpp | 2 +- src/lib/ebus/filereader.h | 2 +- src/lib/ebus/message.cpp | 11 ++++------- src/lib/ebus/message.h | 3 ++- src/lib/ebus/symbol.h | 17 ++++++++++++++++- 8 files changed, 33 insertions(+), 19 deletions(-) diff --git a/src/ebusd/main.cpp b/src/ebusd/main.cpp index d6d14df0..1ce9b0bc 100644 --- a/src/ebusd/main.cpp +++ b/src/ebusd/main.cpp @@ -179,7 +179,8 @@ static const struct argp_option argpoptions[] = { "\"none\" or empty for no initial scan message, \"full\" for full scan, or a single hex address to scan, " "default is broadcast ident message). If combined with --checkconfig, you can add scan message data as " "arguments for checking a particular scan configuration, e.g. \"FF08070400/0AB5454850303003277201\".", 0 }, - {"configlang", O_CFGLNG, "LANG", 0, "Prefer LANG in multilingual configuration files [system default language]", 0 }, + {"configlang", O_CFGLNG, "LANG", 0, + "Prefer LANG in multilingual configuration files [system default language]", 0 }, {"checkconfig", O_CHKCFG, NULL, 0, "Check CSV config files, then stop", 0 }, {"dumpconfig", O_DMPCFG, NULL, 0, "Check and dump CSV config files, then stop", 0 }, {"pollinterval", O_POLINT, "SEC", 0, "Poll for data every SEC seconds (0=disable) [5]", 0 }, diff --git a/src/ebusd/mainloop.cpp b/src/ebusd/mainloop.cpp index 60432308..7bbff82d 100644 --- a/src/ebusd/mainloop.cpp +++ b/src/ebusd/mainloop.cpp @@ -1328,16 +1328,16 @@ string MainLoop::executeGrab(vector &args) { if (args.size() == 1) { return m_busHandler->enableGrab(true) ? "grab started" : "grab continued"; } - if (args.size() == 2 && strcasecmp(args[1].c_str(), "STOP") == 0) { + if (args.size() == 2 && args[1] == "stop") { return m_busHandler->enableGrab(false) ? "grab stopped" : "grab not running"; } - if (args.size() >= 2 && strcasecmp(args[1].c_str(), "RESULT") == 0) { - if (args.size() == 2 || strcasecmp(args[2].c_str(), "ALL") == 0) { + if (args.size() >= 2 && args[1] == "result") { + if (args.size() == 2 || args[2] == "all") { ostringstream result; m_busHandler->formatGrabResult(args.size() == 2, result); return result.str(); } - if (args.size() == 3 || strcasecmp(args[2].c_str(), "decode") == 0) { // TODO remove strcasecmp + if (args.size() == 3 || args[2] == "decode") { ostringstream result; m_busHandler->formatGrabResult(true, result, true); return result.str(); @@ -1361,7 +1361,7 @@ string MainLoop::executeScan(vector &args, string levels) { } if (args.size() == 2) { - if (strcasecmp(args[1].c_str(), "FULL") == 0) { + if (args[1] == "full") { result_t result = m_busHandler->startScan(true, levels); if (result != RESULT_OK) { logError(lf_main, "full scan: %s", getResultCode(result)); @@ -1369,7 +1369,7 @@ string MainLoop::executeScan(vector &args, string levels) { return getResultCode(result); } - if (strcasecmp(args[1].c_str(), "RESULT") == 0) { + if (args[1] == "result") { ostringstream ret; m_busHandler->formatScanResult(ret); return ret.str(); diff --git a/src/lib/ebus/contrib/tem.cpp b/src/lib/ebus/contrib/tem.cpp index 59c0e04f..80cfdec4 100644 --- a/src/lib/ebus/contrib/tem.cpp +++ b/src/lib/ebus/contrib/tem.cpp @@ -98,7 +98,7 @@ result_t TemParamDataType::writeSymbols(istringstream& input, string token; const char* str = input.str().c_str(); - if (strcasecmp(str, NULL_VALUE) == 0) { + if (strcmp(str, NULL_VALUE) == 0) { value = m_replacement; // replacement value } else { if (input.eof() || !getline(input, token, '-')) { diff --git a/src/lib/ebus/datatype.cpp b/src/lib/ebus/datatype.cpp index 63e82967..0902b30b 100644 --- a/src/lib/ebus/datatype.cpp +++ b/src/lib/ebus/datatype.cpp @@ -792,7 +792,7 @@ result_t NumberDataType::writeSymbols(istringstream& input, unsigned int value; const char* str = input.str().c_str(); - if (!hasFlag(REQ) && (isIgnored() || strcasecmp(str, NULL_VALUE) == 0)) { + if (!hasFlag(REQ) && (isIgnored() || strcmp(str, NULL_VALUE) == 0)) { value = m_replacement; // replacement value } else if (str == NULL || *str == 0) { return RESULT_ERR_EOF; // input too short diff --git a/src/lib/ebus/filereader.h b/src/lib/ebus/filereader.h index 9b16e7cd..75e579ac 100644 --- a/src/lib/ebus/filereader.h +++ b/src/lib/ebus/filereader.h @@ -59,7 +59,7 @@ using std::mutex; #define VALUE_SEPARATOR ';' /** special marker string for skipping columns in @a MappedFileReader. */ -static const string SKIP_COLUMN = "\b"; +static const char SKIP_COLUMN[] = "\b"; /** * An abstract class that support reading definitions from a file. diff --git a/src/lib/ebus/message.cpp b/src/lib/ebus/message.cpp index bbd16487..a5391219 100644 --- a/src/lib/ebus/message.cpp +++ b/src/lib/ebus/message.cpp @@ -669,7 +669,6 @@ result_t Message::prepareMasterPart(MasterSymbolString& master, istringstream& i if (index != 0) { return RESULT_ERR_NOTFOUND; } - size_t pos = master.size(); master.push_back(0); // length, will be set later for (size_t i = 2; i < m_id.size(); i++) { master.push_back(m_id[i]); @@ -678,7 +677,7 @@ result_t Message::prepareMasterPart(MasterSymbolString& master, istringstream& i if (result != RESULT_OK) { return result; } - master[pos] = (symbol_t)(master.size()-pos-1); + master.adjustHeader(); return result; } @@ -692,7 +691,7 @@ result_t Message::prepareSlave(istringstream& input, SlaveSymbolString& slave) { if (result != RESULT_OK) { return result; } - slave[0] = (symbol_t)(slave.size()-1); + slave.adjustHeader(); time(&m_lastUpdateTime); if (slave != m_lastSlaveData) { m_lastChangeTime = m_lastUpdateTime; @@ -1201,11 +1200,9 @@ result_t ChainedMessage::combineLastParts() { } } // adjust NN - if (master.size()-5 > 255 || slave.size()-1 > 255) { + if (!master.adjustHeader() || !slave.adjustHeader()) { return RESULT_ERR_INVALID_POS; } - master[4] = (symbol_t)(master.size()-5); - slave[0] = (symbol_t)(slave.size()-1); result_t result = Message::storeLastData(master, 0); if (result == RESULT_OK) { result = Message::storeLastData(slave, 0); @@ -2449,7 +2446,7 @@ deque MessageMap::findAll(const string& circuit, const string& name, c Message* MessageMap::find(MasterSymbolString& master, bool anyDestination, const bool withRead, const bool withWrite, const bool withPassive, const bool onlyAvailable) const { - if (master.size() >= 5 && master[4] == 0 && anyDestination && master[2] == 0x07 && master[3] == 0x04) { + if (anyDestination && master.size() >= 5 && master[4] == 0 && master[2] == 0x07 && master[3] == 0x04) { return m_scanMessage; } uint64_t baseKey = Message::createKey(master, diff --git a/src/lib/ebus/message.h b/src/lib/ebus/message.h index 733ec97d..c2933d03 100644 --- a/src/lib/ebus/message.h +++ b/src/lib/ebus/message.h @@ -1215,7 +1215,8 @@ class MessageMap : public MappedFileReader { * @param addAll whether to add all messages, even if duplicate. * @param preferLanguage the preferred language to use, or empty. */ - explicit MessageMap(const bool addAll = false, const string preferLanguage = "") : MappedFileReader::MappedFileReader(true), + explicit MessageMap(const bool addAll = false, const string preferLanguage = "") + : MappedFileReader::MappedFileReader(true), m_addAll(addAll), m_additionalScanMessages(false), m_maxIdLength(0), m_maxBroadcastIdLength(0), m_messageCount(0), m_conditionalMessageCount(0), m_passiveMessageCount(0) { m_scanMessage = Message::createScanMessage(); diff --git a/src/lib/ebus/symbol.h b/src/lib/ebus/symbol.h index 2ac29313..f414da66 100644 --- a/src/lib/ebus/symbol.h +++ b/src/lib/ebus/symbol.h @@ -130,7 +130,7 @@ class SymbolString { * @param value the escaped value to add to the current CRC. */ static void updateCrc(symbol_t& crc, const symbol_t value); - // TODO add SymbolString::updateHeader() for adjusting length field + /** * Return whether this instance if for the master part. * @return whether this instance if for the master part. @@ -226,6 +226,21 @@ class SymbolString { */ size_t size() const { return m_data.size(); } + /** + * Adjust the header NN field to the number of data bytes DD. + * @return true on success, false if the number of data bytes DD is too big. + */ + bool adjustHeader() { + size_t lengthOffset = (m_isMaster ? 4 : 0); + if (m_data.size() <= lengthOffset) { + m_data.resize(lengthOffset+1); + } else if (m_data.size() >= lengthOffset+255) { + return false; + } + m_data[lengthOffset] = (symbol_t)(m_data.size() - 1 - lengthOffset); + return true; + } + /** * Return the offset to the first data byte DD. * @return the offset to the first data byte DD.