From dc3796959eb548d50195b42539f764e13d2ea7f5 Mon Sep 17 00:00:00 2001 From: john30 Date: Sun, 8 Nov 2015 14:00:03 +0100 Subject: [PATCH] use default destination and circuit in condition, check destination address for scan conditions and improved error message --- src/lib/ebus/message.cpp | 47 +++++++++++++++++++++------------------- src/lib/ebus/message.h | 8 ++++--- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/lib/ebus/message.cpp b/src/lib/ebus/message.cpp index 32dbfb8b..192287e6 100644 --- a/src/lib/ebus/message.cpp +++ b/src/lib/ebus/message.cpp @@ -651,20 +651,19 @@ Message* getFirstAvailable(vector &messages) { } -result_t Condition::create(vector::iterator& it, const vector::iterator end, SimpleCondition*& returnValue) +result_t Condition::create(vector::iterator& it, const vector::iterator end, string defaultDest, string defaultCircuit, SimpleCondition*& returnValue) { // name,circuit,messagename,[comment],[fieldname],[ZZ],values (name already skipped by caller) - if (it+2>end) { // at least everything including messagename - it = end; // for error reporting - return RESULT_ERR_EOF; - } - string circuit = *(it++); // circuit + string circuit = it==end ? "" : *(it++); // circuit string name = it==end ? "" : *(it++); // messagename - it++; // comment + if (it0) { dstAddress = (unsigned char)parseInt(zz.c_str(), 16, 0, 0xff, result); if (result != RESULT_OK) @@ -672,6 +671,12 @@ result_t Condition::create(vector::iterator& it, const vector::i if (dstAddress!=SYN && !isValidAddress(dstAddress, false)) return RESULT_ERR_INVALID_ADDR; } + if (name.length()==0) { + if (!isValidAddress(dstAddress, false) || isMaster(dstAddress)) + return RESULT_ERR_INVALID_ADDR; + } else if (circuit.length()==0) { + circuit = defaultCircuit; + } istringstream stream(it==end ? "" : *(it++)); string str; vector valueRanges; @@ -730,22 +735,24 @@ result_t SimpleCondition::resolve(MessageMap* messages, ostringstream& errorMess Message* message; if (m_name.length()==0) { message = messages->getScanMessage(m_dstAddress); + errorMessage << "scan condition " << nouppercase << setw(2) << hex << setfill('0') << static_cast(m_dstAddress); } else { message = messages->find(m_circuit, m_name, false); if (!message) message = messages->find(m_circuit, m_name, false, true); + errorMessage << "condition " << m_circuit << " " << m_name; } if (!message) { - errorMessage << "condition " << m_circuit << " " << m_name << ": message not found"; + errorMessage << ": message not found"; return RESULT_ERR_NOTFOUND; } if (message->getDstAddress()==SYN) { if (message->isPassive()) { - errorMessage << "condition " << m_circuit << " " << m_name << ": invalid passive message"; + errorMessage << ": invalid passive message"; return RESULT_ERR_INVALID_ARG; } if (m_dstAddress==SYN) { - errorMessage << "condition " << m_circuit << " " << m_name << ": destination address missing"; + errorMessage << ": destination address missing"; return RESULT_ERR_INVALID_ADDR; } // clone the message with dedicated dstAddress if necessary @@ -757,7 +764,7 @@ result_t SimpleCondition::resolve(MessageMap* messages, ostringstream& errorMess } else { message = getFirstAvailable(*derived); if (message==NULL) { - errorMessage << "condition " << m_circuit << " " << m_name << ": conditional derived message"; + errorMessage << ": conditional derived message"; return RESULT_ERR_INVALID_ARG; } } @@ -765,7 +772,7 @@ result_t SimpleCondition::resolve(MessageMap* messages, ostringstream& errorMess if (!m_valueRanges.empty()) { if (!message->hasField(m_field.length()>0 ? m_field.c_str() : NULL, true)) { - errorMessage << "condition " << m_circuit << " " << m_name << ": numeric field " << m_field << " not found"; + errorMessage << ": numeric field " << m_field << " not found"; return RESULT_ERR_NOTFOUND; } } @@ -803,12 +810,14 @@ bool SimpleCondition::isTrue() result_t CombinedCondition::resolve(MessageMap* messages, ostringstream& errorMessage) { - ostringstream dummy; for (vector::iterator it = m_conditions.begin(); it!=m_conditions.end(); it++) { Condition* condition = *it; + ostringstream dummy; result_t ret = condition->resolve(messages, dummy); - if (ret!=RESULT_OK) + if (ret!=RESULT_OK) { + errorMessage << dummy.str(); return ret; + } } return RESULT_OK; } @@ -896,18 +905,12 @@ result_t MessageMap::addDefaultFromFile(vector< vector >& defaults, vect m_lastError = "condition "+type+" already defined"; return RESULT_ERR_DUPLICATE_NAME; } - if (row.size()>1 && defaultCircuit.length()>0 && row[1].length()==0) - row[1] = defaultCircuit; // set default circuit SimpleCondition* condition = NULL; - result_t result = Condition::create(++begin, row.end(), condition); - if (result!=RESULT_OK) { - if (condition) - delete condition; + result_t result = Condition::create(++begin, row.end(), defaultDest, defaultCircuit, condition); + if (condition==NULL || result!=RESULT_OK) { m_lastError = "invalid condition"; return result; } - if (!condition) - return RESULT_ERR_INVALID_ARG; m_conditions[key] = condition; return RESULT_OK; } diff --git a/src/lib/ebus/message.h b/src/lib/ebus/message.h index 1d69a9c9..f7df3791 100644 --- a/src/lib/ebus/message.h +++ b/src/lib/ebus/message.h @@ -455,10 +455,12 @@ public: * Factory method for creating a new instance. * @param it the iterator to traverse for the definition parts. * @param end the iterator pointing to the end of the definition parts. + * @param defaultDest the valid destination address extracted from the file name (from ZZ part), or empty. + * @param defaultCircuit the valid circuit name extracted from the file name (from IDENT part), or empty. * @param returnValue the variable in which to store the created instance. * @return @a RESULT_OK on success, or an error code. */ - static result_t create(vector::iterator& it, const vector::iterator end, SimpleCondition*& returnValue); + static result_t create(vector::iterator& it, const vector::iterator end, string defaultDest, string defaultCircuit, SimpleCondition*& returnValue); /** * Combine this condition with another instance using a logical and. @@ -509,8 +511,8 @@ public: */ SimpleCondition(const string circuit, const string name, const unsigned char dstAddress, const string field, const vector valueRanges) : Condition(), - m_circuit(circuit), m_name(name), m_dstAddress(dstAddress), m_field(field), m_valueRanges(valueRanges), - m_message(NULL) { } + m_circuit(circuit), m_name(name), m_dstAddress(dstAddress), m_field(field), + m_valueRanges(valueRanges), m_message(NULL) { } /** * Destructor.