From e484bf57b9a7c08cf3b135fa548f7bf187c7fa55 Mon Sep 17 00:00:00 2001 From: john30 Date: Sat, 21 Feb 2015 16:19:02 +0100 Subject: [PATCH] added helper for dumping messages, corrected line number printing --- src/lib/ebus/data.cpp | 42 +++++++++++++++++++++++-------- src/lib/ebus/data.h | 8 ++++++ src/lib/ebus/message.cpp | 53 ++++++++++++++++++++++++++++++++++++++++ src/lib/ebus/message.h | 14 ++++++++++- 4 files changed, 106 insertions(+), 11 deletions(-) diff --git a/src/lib/ebus/data.cpp b/src/lib/ebus/data.cpp index 0fd7322c..b64a4bb8 100644 --- a/src/lib/ebus/data.cpp +++ b/src/lib/ebus/data.cpp @@ -114,7 +114,7 @@ void printErrorPos(vector::iterator begin, const vector::iterato { if (pos > begin) pos--; - cout << "Error reading \"" << filename << "\" line " << static_cast(lineNo) << " field " << static_cast(1+pos.base()-begin.base()) << " value \"" << *pos << "\": " << getResultCode(result) << endl; + cout << "Error reading \"" << filename << "\" line " << setw(0) << dec << static_cast(lineNo) << " field " << static_cast(1+pos.base()-begin.base()) << " value \"" << *pos << "\": " << getResultCode(result) << endl; cout << "Erroneous item is here:" << endl; bool first = true; int cnt = 0; @@ -376,15 +376,28 @@ result_t DataField::create(vector::iterator& it, return RESULT_OK; } +void DataField::dumpString(ostream& output, const string str, const bool prependFieldSeparator) +{ + if (prependFieldSeparator) + output << FIELD_SEPARATOR; + if (str.find_first_of(FIELD_SEPARATOR) == string::npos) { + output << str; + } else { + output << TEXT_SEPARATOR << str << TEXT_SEPARATOR; + } +} + void SingleDataField::dump(ostream& output) { - output << m_name << FIELD_SEPARATOR; + output << setw(0) << dec; // initialize formatting + dumpString(output, m_name, false); + output << FIELD_SEPARATOR; if (m_partType == pt_masterData) output << "m"; else if (m_partType == pt_slaveData) output << "s"; - output << FIELD_SEPARATOR << m_dataType.name; + dumpString(output, m_dataType.name); } result_t SingleDataField::read(const PartType partType, @@ -480,8 +493,9 @@ void StringDataField::dump(ostream& output) SingleDataField::dump(output); if ((m_dataType.flags & ADJ) != 0) output << ":" << static_cast(m_length); - output << FIELD_SEPARATOR << FIELD_SEPARATOR; // no value list, no divisor - output << m_unit << FIELD_SEPARATOR << m_comment << FIELD_SEPARATOR; + output << FIELD_SEPARATOR; // no value list, no divisor + dumpString(output, m_unit); + dumpString(output, m_comment); } result_t StringDataField::readSymbols(SymbolString& input, @@ -873,8 +887,10 @@ result_t NumberDataField::derive(string name, string comment, void NumberDataField::dump(ostream& output) { NumericDataField::dump(output); - output << static_cast(m_divisor) << FIELD_SEPARATOR; - output << m_unit << FIELD_SEPARATOR << m_comment << FIELD_SEPARATOR; + if ((m_dataType.bitCount%8) != 0 && m_dataType.divisorOrFirstBit != m_divisor) + output << static_cast(m_divisor / m_dataType.divisorOrFirstBit) << FIELD_SEPARATOR; + dumpString(output, m_unit); + dumpString(output, m_comment); } result_t NumberDataField::readSymbols(SymbolString& input, @@ -1025,8 +1041,8 @@ void ValueListDataField::dump(ostream& output) output << VALUE_SEPARATOR; output << static_cast(it->first) << "=" << it->second; } - output << FIELD_SEPARATOR; - output << m_unit << FIELD_SEPARATOR << m_comment << FIELD_SEPARATOR; + dumpString(output, m_unit); + dumpString(output, m_comment); } result_t ValueListDataField::readSymbols(SymbolString& input, @@ -1156,8 +1172,14 @@ result_t DataFieldSet::derive(string name, string comment, void DataFieldSet::dump(ostream& output) { - for (vector::iterator it = m_fields.begin(); it < m_fields.end(); it++) + bool first = true; + for (vector::iterator it = m_fields.begin(); it < m_fields.end(); it++) { + if (first) + first = false; + else + output << FIELD_SEPARATOR; (*it)->dump(output); + } } result_t DataFieldSet::read(const PartType partType, diff --git a/src/lib/ebus/data.h b/src/lib/ebus/data.h index 6f45b7a9..b17878b4 100644 --- a/src/lib/ebus/data.h +++ b/src/lib/ebus/data.h @@ -147,6 +147,14 @@ public: DataFieldTemplates* templates, DataField*& returnField, const bool isWriteMessage=false, const unsigned char dstAddress=SYN); + /** + * Dump the @a string optionally embedded in @a TEXT_SEPARATOR to the output. + * @param output the @a ostream to dump to. + * @param str the @a string to dump. + * @param prependFieldSeparator whether to start with a @a FIELD_SEPARATOR. + */ + static void dumpString(ostream& output, const string str, const bool prependFieldSeparator=true); + /** * Returns the length of this field (or contained fields) in bytes. * @param partType the message part of the contained fields to limit the length calculation to. diff --git a/src/lib/ebus/message.cpp b/src/lib/ebus/message.cpp index cba16196..df5d84e9 100644 --- a/src/lib/ebus/message.cpp +++ b/src/lib/ebus/message.cpp @@ -26,6 +26,7 @@ #include #include #include +#include using namespace std; @@ -371,6 +372,41 @@ bool Message::isLessPollWeight(const Message* other) return false; } +void Message::dump(ostream& output) +{ + if (m_isPassive) { + output << "u"; + if (m_isWrite) + output << "w"; + } else if (m_isWrite) + output << "w"; + else { + output << "r"; + if (m_pollPriority>0) + output << static_cast(m_pollPriority); + } + DataField::dumpString(output, m_class); + DataField::dumpString(output, m_name); + DataField::dumpString(output, m_comment); + output << FIELD_SEPARATOR; + if (m_srcAddress != SYN) + output << hex << setw(2) << setfill('0') << static_cast(m_srcAddress); + output << FIELD_SEPARATOR; + if (m_dstAddress != SYN) + output << hex << setw(2) << setfill('0') << static_cast(m_dstAddress); + output << FIELD_SEPARATOR; + unsigned int cnt = 0; + for (vector::const_iterator it=m_id.begin(); it(*it); + } + if (cnt <= 2) + output << FIELD_SEPARATOR; // no further ID bytes besides PBSB + output << FIELD_SEPARATOR; + m_data->dump(output); +} + string strtolower(const string& str) { @@ -587,3 +623,20 @@ Message* MessageMap::getNextPoll() m_pollMessages.push(ret); // re-insert at new position return ret; } + +void MessageMap::dump(ostream& output) +{ + bool first = true; + for (map::iterator it = m_messagesByName.begin(); it != m_messagesByName.end(); it++) { + if (it->first[0] == '-') // skip instances stored multiple times (key starting with "-") + continue; + Message* message = it->second; + if (first) + first = false; + else + cout << endl; + message->dump(cout); + } + if (!first) + cout << endl; +} diff --git a/src/lib/ebus/message.h b/src/lib/ebus/message.h index 983fe9da..0b50339c 100644 --- a/src/lib/ebus/message.h +++ b/src/lib/ebus/message.h @@ -240,6 +240,12 @@ public: */ bool isLessPollWeight(const Message* other); + /** + * Write the message definition to the @a ostream. + * @param output the @a ostream to append the formatted value to. + */ + void dump(ostream& output); + private: /** the optional device class. */ @@ -261,7 +267,7 @@ private: /** the source address, or @a SYN for any (only relevant if passive). */ const unsigned char m_srcAddress; - /** the destination address. */ + /** the destination address, or @a SYN for any (only for temporary scan messages). */ const unsigned char m_dstAddress; /** the primary, secondary, and optionally further command ID bytes. */ @@ -396,6 +402,12 @@ public: */ Message* getNextPoll(); + /** + * Write the message definitions to the @a ostream. + * @param output the @a ostream to append the formatted messages to. + */ + void dump(ostream& output); + private: /** the minimum ID length used by any of the known @a Message instances. */