Skip to content

Commit

Permalink
fix dump of divisor
Browse files Browse the repository at this point in the history
  • Loading branch information
john30 committed Dec 12, 2024
1 parent 2790860 commit 85a3eef
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 26 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
## Bug Fixes
* fix for device string symlink with colon
* fix "read" and "write" command response
* fix dump of divisor


# 24.1 (2024-10-27)
Expand Down
2 changes: 1 addition & 1 deletion src/ebusd/bushandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ bool decodeType(const DataType* type, const SymbolString& input, size_t length,
first = false;
*output << endl << " ";
ostringstream::pos_type cnt = output->tellp();
type->dump(OF_NONE, length, false, output);
type->dump(OF_NONE, length, ad_none, output);
cnt = output->tellp() - cnt;
while (cnt < 5) {
*output << " ";
Expand Down
2 changes: 1 addition & 1 deletion src/ebusd/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ int main(int argc, char* argv[], char* envp[]) {
}
if (s_opt.dumpConfig & OF_JSON) {
*out << "{\"datatypes\":[";
DataTypeList::getInstance()->dump(s_opt.dumpConfig, true, out);
DataTypeList::getInstance()->dump(s_opt.dumpConfig, out);
*out << "],\"templates\":[";
const auto tmpl = s_scanHelper->getTemplates("");
tmpl->dump(s_opt.dumpConfig, out);
Expand Down
2 changes: 1 addition & 1 deletion src/ebusd/mainloop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2279,7 +2279,7 @@ result_t MainLoop::executeGet(const vector<string>& args, bool* connected, ostri
if (uri == "/datatypes") {
*ostream << "[";
OutputFormat verbosity = OF_NAMES|OF_JSON|OF_ALL_ATTRS;
DataTypeList::getInstance()->dump(verbosity, true, ostream);
DataTypeList::getInstance()->dump(verbosity, ostream);
*ostream << "\n]";
type = 6;
*connected = false;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ebus/data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ void SingleDataField::dumpPrefix(bool prependFieldSeparator, OutputFormat output
}
*output << FIELD_SEPARATOR;
}
m_dataType->dump(outputFormat, m_length, true, output);
m_dataType->dump(outputFormat, m_length, ad_normal, output);
}

void SingleDataField::dumpSuffix(OutputFormat outputFormat, ostream* output) const {
Expand Down
32 changes: 16 additions & 16 deletions src/lib/ebus/datatype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ uint16_t floatToUint16(float value) {
return static_cast<uint16_t>((shift << 11) | (negative ? 0x8000 | (0x800-sig) : sig));
}

bool DataType::dump(OutputFormat outputFormat, size_t length, bool appendDivisor, ostream* output) const {
bool DataType::dump(OutputFormat outputFormat, size_t length, AppendDivisor appendDivisor, ostream* output) const {
if (outputFormat & OF_JSON) {
*output << "\"type\": \"" << m_id << "\", \"isbits\": "
<< (getBitCount() < 8 ? "true" : "false");
Expand All @@ -149,15 +149,15 @@ bool DataType::dump(OutputFormat outputFormat, size_t length, bool appendDivisor
*output << static_cast<unsigned>(length);
}
}
if (appendDivisor) {
if (appendDivisor != ad_none) {
*output << FIELD_SEPARATOR;
}
}
return false;
}


bool StringDataType::dump(OutputFormat outputFormat, size_t length, bool appendDivisor, ostream* output) const {
bool StringDataType::dump(OutputFormat outputFormat, size_t length, AppendDivisor appendDivisor, ostream* output) const {
DataType::dump(outputFormat, length, appendDivisor, output);
if ((outputFormat & OF_JSON) && (outputFormat & OF_ALL_ATTRS)) {
*output << ", \"result\": \"" << (isIgnored() ? "void" : "string") << "\"";
Expand Down Expand Up @@ -300,7 +300,7 @@ result_t StringDataType::writeSymbols(size_t offset, size_t length, istringstrea
}


bool DateTimeDataType::dump(OutputFormat outputFormat, size_t length, bool appendDivisor, ostream* output) const {
bool DateTimeDataType::dump(OutputFormat outputFormat, size_t length, AppendDivisor appendDivisor, ostream* output) const {
DataType::dump(outputFormat, length, appendDivisor, output);
if ((outputFormat & OF_JSON) && (outputFormat & OF_ALL_ATTRS)) {
*output << ", \"result\": \"" << (hasDate() ? hasTime() ? "datetime" : "date" : "time") << "\"";
Expand Down Expand Up @@ -672,7 +672,7 @@ size_t NumberDataType::calcPrecision(int divisor) {
return precision;
}

bool NumberDataType::dump(OutputFormat outputFormat, size_t length, bool appendDivisor, ostream* output) const {
bool NumberDataType::dump(OutputFormat outputFormat, size_t length, AppendDivisor appendDivisor, ostream* output) const {
if (m_bitCount < 8) {
DataType::dump(outputFormat, m_bitCount, appendDivisor, output);
} else {
Expand All @@ -681,24 +681,24 @@ bool NumberDataType::dump(OutputFormat outputFormat, size_t length, bool appendD
if ((outputFormat & OF_JSON) && (outputFormat & OF_ALL_ATTRS)) {
*output << ", \"result\": \"number\"";
}
if (!appendDivisor) {
if (appendDivisor == ad_none) {
return false;
}
bool ret = false;
if (m_baseType) {
if (appendDivisor == ad_full && m_divisor != 1) {
if (outputFormat & OF_JSON) {
*output << ", \"divisor\": ";
}
*output << m_divisor;
ret = true;
} else if (m_baseType) {
if (m_baseType->m_divisor != m_divisor) {
if (outputFormat & OF_JSON) {
*output << ", \"divisor\": ";
}
*output << (m_divisor / m_baseType->m_divisor);
ret = true;
}
} else if (m_divisor != 1) {
if (outputFormat & OF_JSON) {
*output << ", \"divisor\": ";
}
*output << m_divisor;
ret = true;
}
if (ret && (outputFormat & OF_JSON) && (outputFormat & OF_ALL_ATTRS)) {
*output << ", \"precision\": " << static_cast<unsigned>(getPrecision());
Expand Down Expand Up @@ -1317,7 +1317,7 @@ DataTypeList* DataTypeList::getInstance() {
return &s_instance;
}

void DataTypeList::dump(OutputFormat outputFormat, bool appendDivisor, ostream* output) const {
void DataTypeList::dump(OutputFormat outputFormat, ostream* output) const {
bool json = outputFormat & OF_JSON;
string sep = "\n";
for (const auto &it : m_typesById) {
Expand All @@ -1329,9 +1329,9 @@ void DataTypeList::dump(OutputFormat outputFormat, bool appendDivisor, ostream*
*output << sep << " {";
}
if ((dataType->getBitCount() % 8) != 0) {
dataType->dump(outputFormat, dataType->getBitCount(), appendDivisor, output);
dataType->dump(outputFormat, dataType->getBitCount(), ad_full, output);
} else {
dataType->dump(outputFormat, dataType->getBitCount() / 8, appendDivisor, output);
dataType->dump(outputFormat, dataType->getBitCount() / 8, ad_full, output);
}
if (json) {
*output << "}";
Expand Down
18 changes: 12 additions & 6 deletions src/lib/ebus/datatype.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ constexpr inline enum OutputFormat operator~ (enum OutputFormat self) {
return (enum OutputFormat)(~(OutputFormatBaseType)self);
}

/** whether divisor should be appended to a dump. */
enum AppendDivisor {
ad_none, //!< no dump of divisor
ad_normal, //!< regular dump of divisor (i.e. not for base types)
ad_full, //!< full dump of divisor (i.e. also for base types)
};

/** the message part in which a data field is stored. */
enum PartType {
pt_any, //!< stored in any data (master or slave)
Expand Down Expand Up @@ -286,7 +293,7 @@ class DataType {
* @param output the @a ostream to dump to.
* @return true when a non-default divisor was written to the output.
*/
virtual bool dump(OutputFormat outputFormat, size_t length, bool appendDivisor, ostream* output) const;
virtual bool dump(OutputFormat outputFormat, size_t length, AppendDivisor appendDivisor, ostream* output) const;

/**
* Internal method for reading the numeric raw value from a @a SymbolString.
Expand Down Expand Up @@ -363,7 +370,7 @@ class StringDataType : public DataType {
virtual ~StringDataType() {}

// @copydoc
bool dump(OutputFormat outputFormat, size_t length, bool appendDivisor, ostream* output) const override;
bool dump(OutputFormat outputFormat, size_t length, AppendDivisor appendDivisor, ostream* output) const override;

// @copydoc
result_t readRawValue(size_t offset, size_t length, const SymbolString& input,
Expand Down Expand Up @@ -410,7 +417,7 @@ class DateTimeDataType : public DataType {
virtual ~DateTimeDataType() {}

// @copydoc
bool dump(OutputFormat outputFormat, size_t length, bool appendDivisor, ostream* output) const override;
bool dump(OutputFormat outputFormat, size_t length, AppendDivisor appendDivisor, ostream* output) const override;

/**
* @return true if date part is present.
Expand Down Expand Up @@ -503,7 +510,7 @@ class NumberDataType : public DataType {
static size_t calcPrecision(int divisor);

// @copydoc
bool dump(OutputFormat outputFormat, size_t length, bool appendDivisor, ostream* output) const override;
bool dump(OutputFormat outputFormat, size_t length, AppendDivisor appendDivisor, ostream* output) const override;

/**
* Derive a new @a NumberDataType from this.
Expand Down Expand Up @@ -658,10 +665,9 @@ class DataTypeList {
/**
* Dump the type list optionally including the divisor to the output.
* @param outputFormat the @a OutputFormat options.
* @param appendDivisor whether to append the divisor (if available).
* @param output the @a ostream to dump to.
*/
void dump(OutputFormat outputFormat, bool appendDivisor, ostream* output) const;
void dump(OutputFormat outputFormat, ostream* output) const;

/**
* Removes all @a DataType instances.
Expand Down
2 changes: 2 additions & 0 deletions src/lib/ebus/test/test_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ int main() {
" ]\n"
" }: \n"
" \"field\": {\"value\": 42, \"raw\": [42]}", "ff75b509030d0100", "012a", "jNr"},
{"r,CIRCUIT,NAME,COMMENT,,,,0100,field,,temp", "r,cirCIRCUITcuit,naNAMEme,comCOMMENTment,ff,75,b509,0d0100,field,s,D2C,,°C,Temperatur: field=42.00 °C [Temperatur]", "ff75b509030d0100", "02a002", "DN"},
{"r,CIRCUIT,NAME,COMMENT,,,,0100,field,,D2C,,°C,Temperatur", "r,cirCIRCUITcuit,naNAMEme,comCOMMENTment,ff,75,b509,0d0100,field,s,D2C,,°C,Temperatur: field=42.00 °C [Temperatur]", "ff75b509030d0100", "02a002", "DN"},
};
templates = new DataFieldTemplates();
unsigned int lineNo = 0;
Expand Down

0 comments on commit 85a3eef

Please sign in to comment.