Skip to content

Commit

Permalink
added SymbolString::adjustHeader(), avoid strcasecmp, code style
Browse files Browse the repository at this point in the history
  • Loading branch information
john30 committed Apr 17, 2017
1 parent aaea8f7 commit 47ed6dd
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/ebusd/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
12 changes: 6 additions & 6 deletions src/ebusd/mainloop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1328,16 +1328,16 @@ string MainLoop::executeGrab(vector<string> &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();
Expand All @@ -1361,15 +1361,15 @@ string MainLoop::executeScan(vector<string> &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));
}
return getResultCode(result);
}

if (strcasecmp(args[1].c_str(), "RESULT") == 0) {
if (args[1] == "result") {
ostringstream ret;
m_busHandler->formatScanResult(ret);
return ret.str();
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ebus/contrib/tem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, '-')) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ebus/datatype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ebus/filereader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 4 additions & 7 deletions src/lib/ebus/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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;
}

Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -2449,7 +2446,7 @@ deque<Message*> 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,
Expand Down
3 changes: 2 additions & 1 deletion src/lib/ebus/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
17 changes: 16 additions & 1 deletion src/lib/ebus/symbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 47ed6dd

Please sign in to comment.