From 63cde34e6a32de2f6e68ffb3b99caff560fa6cfa Mon Sep 17 00:00:00 2001 From: john30 Date: Sun, 16 Nov 2014 10:49:47 +0100 Subject: [PATCH 1/6] introduced type templates, added bit base types, added two-byte time, added precision, added test for any/min/max/replacement/signed min/signed max for all base types --- src/lib/ebus/data.cpp | 444 ++++++++++++++++++++------------ src/lib/ebus/data.h | 97 ++++--- src/lib/ebus/test/test_data.cpp | 321 +++++++++++++++++------ 3 files changed, 580 insertions(+), 282 deletions(-) diff --git a/src/lib/ebus/data.cpp b/src/lib/ebus/data.cpp index ac409c23..b63245b0 100644 --- a/src/lib/ebus/data.cpp +++ b/src/lib/ebus/data.cpp @@ -26,34 +26,72 @@ #include #include #include +#include +#include namespace libebus { /** the known data field types. */ static const dataType_t dataTypes[] = { - {"STR", 16, bt_str, ADJ, ' ', 1, 16, 0}, // >= 1 byte character string filled up with space - {"HEX", 16, bt_hexstr, ADJ, 0, 2, 47, 0}, // >= 1 byte hex digit string, usually separated by space, e.g. 0a 1b 2c 3d - {"BDA", 4, bt_dat, BCD, 0, 10, 10, 0}, // date in BCD, 01.01.2000 - 31.12.2099 (0x01,0x01,WW,0x00 - 0x31,0x12,WW,0x99, WW is ignored weekday) - {"BDA", 3, bt_dat, BCD, 0, 10, 10, 0}, // date in BCD, 01.01.2000 - 31.12.2099 (0x01,0x01,0x00 - 0x31,0x12,0x99) - {"HDA", 4, bt_dat, 0, 0, 10, 10, 0}, // date, 01.01.2000 - 31.12.2099 (0x01,0x01,WW,0x00 - 0x31,0x12,WW,0x99, WW is ignored weekday) // TODO remove duplicate of BDA - {"HDA", 3, bt_dat, 0, 0, 10, 10, 0}, // date, 01.01.2000 - 31.12.2099 (0x01,0x01,0x00 - 0x31,0x12,0x99) // TODO remove duplicate of BDA - {"BTI", 3, bt_tim, BCD|REV, 0, 8, 8, 0}, // time in BCD, 00:00:00 - 23:59:59 (0x00,0x00,0x00 - 0x59,0x59,0x23) - {"TTM", 1, bt_tim, 0, 0, 5, 5, 0}, // truncated time (only multiple of 10 minutes), 00:00 - 24:00 (minutes div 10 + hour * 6 as integer) - {"BDY", 1, bt_num, DAY|LST, 0, 0, 6, 1}, // weekday, "Mon" - "Sun" - {"HDY", 1, bt_num, DAY|LST, 0, 1, 7, 1}, // weekday, "Mon" - "Sun" - {"BCD", 1, bt_num, BCD|LST, 0xff, 0, 0x99, 1}, // unsigned decimal in BCD, 0 - 99 - {"UCH", 1, bt_num, LST, 0xff, 0, 0xff, 1}, // unsigned integer, 0 - 255 - {"SCH", 1, bt_num, SIG, 0x80, 0x80, 0x7f, 1}, // signed integer, -128 - +127 - {"D1B", 1, bt_num, SIG, 0x80, 0x81, 0x7f, 1}, // signed integer, -127 - +127 - {"D1C", 1, bt_num, 0, 0xff, 0x00, 0xc8, 2}, // unsigned number (fraction 1/2), 0 - 100 (0x00 - 0xc8, replacement 0xff) - {"UIN", 2, bt_num, LST, 0xffff, 0, 0xffff, 1}, // unsigned integer, 0 - 65535 - {"SIN", 2, bt_num, SIG, 0x8000, 0x8000, 0x7fff, 1}, // signed integer, -32768 - +32767 - {"FLT", 2, bt_num, SIG, 0x8000, 0x8000, 0x7fff, 1000}, // signed number (fraction 1/1000), -32.768 - +32.767 - {"D2B", 2, bt_num, SIG, 0x8000, 0x8001, 0x7fff, 256}, // signed number (fraction 1/256), -127.99 - +127.99 - {"D2C", 2, bt_num, SIG, 0x8000, 0x8001, 0x7fff, 16}, // signed number (fraction 1/16), -2047.9 - +2047.9 - {"ULG", 4, bt_num, LST, 0xffffffff, 0, 0xffffffff, 1}, // unsigned integer, 0 - 4294967295 - {"SLG", 4, bt_num, SIG, 0x80000000, 0x80000000, 0xffffffff, 1}, // signed integer, -2147483648 - +2147483647 + {"STR",16*8,bt_str, ADJ, ' ', 1, 16, 0, 0}, // >= 1 byte character string filled up with space + {"HEX",16*8,bt_hexstr, ADJ, 0, 2, 47, 0, 0}, // >= 1 byte hex digit string, usually separated by space, e.g. 0a 1b 2c 3d + {"BDA", 32, bt_dat, BCD, 0, 10, 10, 0, 0}, // date in BCD, 01.01.2000 - 31.12.2099 (0x01,0x01,WW,0x00 - 0x31,0x12,WW,0x99, WW is ignored weekday) + {"BDA", 24, bt_dat, BCD, 0, 10, 10, 0, 0}, // date in BCD, 01.01.2000 - 31.12.2099 (0x01,0x01,0x00 - 0x31,0x12,0x99) + {"HDA", 32, bt_dat, 0, 0, 10, 10, 0, 0}, // date, 01.01.2000 - 31.12.2099 (0x01,0x01,WW,0x00 - 0x31,0x12,WW,0x99, WW is ignored weekday) // TODO remove duplicate of BDA + {"HDA", 24, bt_dat, 0, 0, 10, 10, 0, 0}, // date, 01.01.2000 - 31.12.2099 (0x01,0x01,0x00 - 0x31,0x12,0x99) // TODO remove duplicate of BDA + {"BTI", 24, bt_tim, BCD|REV, 0, 8, 8, 0, 0}, // time in BCD, 00:00:00 - 23:59:59 (0x00,0x00,0x00 - 0x59,0x59,0x23) + {"HTM", 16, bt_tim, 0, 0, 5, 5, 0, 0}, // time as hh:mm, 00:00 - 23:59 (0x00,0x00 - 0x17,0x3b) + {"TTM", 8, bt_tim, 0, 0, 5, 5, 0, 0}, // truncated time (only multiple of 10 minutes), 00:00 - 24:00 (minutes div 10 + hour * 6 as integer) + {"BDY", 8, bt_num, DAY|LST, 0x07, 0, 6, 1, 0}, // weekday, "Mon" - "Sun" + {"HDY", 8, bt_num, DAY|LST, 0x00, 1, 7, 1, 0}, // weekday, "Mon" - "Sun" + {"BCD", 8, bt_num, BCD|LST, 0xff, 0, 0x99, 1, 0}, // unsigned decimal in BCD, 0 - 99 + {"UCH", 8, bt_num, LST, 0xff, 0, 0xfe, 1, 0}, // unsigned integer, 0 - 254 + {"SCH", 8, bt_num, SIG, 0x80, 0x81, 0x7f, 1, 0}, // signed integer, -127 - +127 + {"D1B", 8, bt_num, SIG, 0x80, 0x81, 0x7f, 1, 0}, // signed integer, -127 - +127 + {"D1C", 8, bt_num, 0, 0xff, 0x00, 0xc8, 2, 1}, // unsigned number (fraction 1/2), 0 - 100 (0x00 - 0xc8, replacement 0xff) + {"UIN", 16, bt_num, LST, 0xffff, 0, 0xfffe, 1, 0}, // unsigned integer, 0 - 65534 + {"SIN", 16, bt_num, SIG, 0x8000, 0x8001, 0x7fff, 1, 0}, // signed integer, -32767 - +32767 + {"FLT", 16, bt_num, SIG, 0x8000, 0x8001, 0x7fff, 1000, 3}, // signed number (fraction 1/1000), -32.767 - +32.767 + {"D2B", 16, bt_num, SIG, 0x8000, 0x8001, 0x7fff, 256, 3}, // signed number (fraction 1/256), -127.99 - +127.99 + {"D2C", 16, bt_num, SIG, 0x8000, 0x8001, 0x7fff, 16, 2}, // signed number (fraction 1/16), -2047.9 - +2047.9 + {"ULG", 32, bt_num, LST, 0xffffffff, 0, 0xfffffffe, 1, 0}, // unsigned integer, 0 - 4294967294 + {"SLG", 32, bt_num, SIG, 0x80000000, 0x80000001, 0xffffffff, 1, 0}, // signed integer, -2147483647 - +2147483647 + {"BI0", 1, bt_num, LST, 0, 0, 0x1, 1, 0}, // single bit 0 + {"BI1", 1, bt_num, LST, 0, 0, 0x1, 1, 1}, // single bit 1 + {"BI2", 1, bt_num, LST, 0, 0, 0x1, 1, 2}, // single bit 2 + {"BI3", 1, bt_num, LST, 0, 0, 0x1, 1, 3}, // single bit 3 + {"BI4", 1, bt_num, LST, 0, 0, 0x1, 1, 4}, // single bit 4 + {"BI5", 1, bt_num, LST, 0, 0, 0x1, 1, 5}, // single bit 5 + {"BI6", 1, bt_num, LST, 0, 0, 0x1, 1, 6}, // single bit 6 + {"BI7", 1, bt_num, LST, 0, 0, 0x1, 1, 7}, // single bit 7 + {"B01", 2, bt_num, LST, 0, 0, 0x3, 1, 0}, // two bits 0-1 + {"B12", 2, bt_num, LST, 0, 0, 0x3, 1, 1}, // two bits 1-2 + {"B23", 2, bt_num, LST, 0, 0, 0x3, 1, 2}, // two bits 2-3 + {"B34", 2, bt_num, LST, 0, 0, 0x3, 1, 3}, // two bits 3-4 + {"B45", 2, bt_num, LST, 0, 0, 0x3, 1, 4}, // two bits 4-5 + {"B56", 2, bt_num, LST, 0, 0, 0x3, 1, 5}, // two bits 5-6 + {"B67", 2, bt_num, LST, 0, 0, 0x3, 1, 6}, // two bits 6-7 + {"B02", 3, bt_num, LST, 0, 0, 0x7, 1, 0}, // three bits 0-2 + {"B13", 3, bt_num, LST, 0, 0, 0x7, 1, 1}, // three bits 1-3 + {"B24", 3, bt_num, LST, 0, 0, 0x7, 1, 2}, // three bits 2-4 + {"B35", 3, bt_num, LST, 0, 0, 0x7, 1, 3}, // three bits 3-5 + {"B46", 3, bt_num, LST, 0, 0, 0x7, 1, 4}, // three bits 4-6 + {"B57", 3, bt_num, LST, 0, 0, 0x7, 1, 5}, // three bits 5-7 + {"B03", 4, bt_num, LST, 0, 0, 0xf, 1, 0}, // four bits 0-3 + {"B14", 4, bt_num, LST, 0, 0, 0xf, 1, 1}, // four bits 1-4 + {"B25", 4, bt_num, LST, 0, 0, 0xf, 1, 2}, // four bits 2-5 + {"B36", 4, bt_num, LST, 0, 0, 0xf, 1, 3}, // four bits 3-6 + {"B47", 4, bt_num, LST, 0, 0, 0xf, 1, 4}, // four bits 4-7 + {"B04", 5, bt_num, LST, 0, 0, 0x1f, 1, 0}, // five bits 0-4 + {"B15", 5, bt_num, LST, 0, 0, 0x1f, 1, 1}, // five bits 1-5 + {"B26", 5, bt_num, LST, 0, 0, 0x1f, 1, 2}, // five bits 2-6 + {"B37", 5, bt_num, LST, 0, 0, 0x1f, 1, 3}, // five bits 3-7 + {"B05", 6, bt_num, LST, 0, 0, 0x3f, 1, 0}, // six bits 0-5 + {"B16", 6, bt_num, LST, 0, 0, 0x3f, 1, 1}, // six bits 1-6 + {"B27", 6, bt_num, LST, 0, 0, 0x3f, 1, 2}, // six bits 2-7 + {"B06", 7, bt_num, LST, 0, 0, 0x7f, 1, 0}, // seven bits 0-6 + {"B17", 7, bt_num, LST, 0, 0, 0x7f, 1, 1}, // seven bits 1-7 }; /** the week day names. */ @@ -63,69 +101,55 @@ static const char* dayNames[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" #define VALUE_SEPARATOR ',' #define NULL_VALUE "-" -result_t DataField::create(const unsigned char dstAddress, const bool isSetMessage, - std::vector::iterator& it, const std::vector::iterator end, - const std::map predefined, std::vector& fields, - unsigned char& nextPos) { - std::string name, unit, comment; +result_t DataField::create(std::vector::iterator& it, const std::vector::iterator end, + const std::map templates, std::vector& fields, + const bool isSetMessage, const unsigned char dstAddress) { + std::string unit, comment; PartType partType; - unsigned int divisor; - unsigned char baseOffset, offset, length, maxPos = 16, offsetCnt = 0; + unsigned int divisor = 0; + unsigned char offset, length, maxPos = 16, offsetCnt = 0; + const bool isTemplate = dstAddress == SYN; std::string token; - if (it == end) return RESULT_ERR_EOF; - name = *it++; + // name;[pos];type[;[divisor|values][;[unit][;[comment]]]] + const std::string name = *it++; if (it == end || name.empty() == true) return RESULT_ERR_EOF; const char* posStr = (*it++).c_str(); - if (it == end) { - // no more input: check for reference to predefined type - if (posStr[0] == 0) - return RESULT_ERR_EOF; + if (it == end) + return RESULT_ERR_EOF; - std::map::const_iterator ref = predefined.find(name); - if (ref != predefined.end()) { - fields.push_back(ref->second); - return RESULT_OK; - } - // check for sequence of reference to predefined type - std::istringstream stream(name); - while (std::getline(stream, token, VALUE_SEPARATOR) != 0) { - ref = predefined.find(token); - if (ref == predefined.end()) - return RESULT_ERR_INVALID_ARG; - - fields.push_back(ref->second); - } - return RESULT_OK; - } - if (dstAddress == BROADCAST - || isMaster(dstAddress) - || (isSetMessage == true && posStr[0] <= '9') + if (dstAddress == BROADCAST || isMaster(dstAddress) + || (isTemplate == false && isSetMessage == true && posStr[0] != 0 && posStr[0] <= '9') || posStr[0] == 'm') { // master data partType = pt_masterData; - baseOffset = 5; // skip QQ ZZ PB SB NN if (posStr[0] == 'm') posStr++; - } else if ((isSetMessage == false && posStr[0] <= '9') + } else if ((isTemplate == false && isSetMessage == false && posStr[0] != 0 && posStr[0] <= '9') || posStr[0] == 's') { // slave data partType = pt_slaveData; - baseOffset = 1; if (posStr[0] == 's') posStr++; + } else if (isTemplate) { + partType = pt_template; } else return RESULT_ERR_INVALID_ARG; + bool hasPrev = fields.empty() == false; + if (hasPrev == true) { + DataField* previous = fields.back(); + offset = previous->m_offset + previous->m_length; + if ((previous->m_dataType.numBits % 8) != 0 + && previous->m_dataType.precisionOrFirstBit + (previous->m_dataType.numBits % 8) < 8) + offset--; // previous bits not yet fully consumed + } + else + offset = 0; + if (posStr[0] == 0) { - if (nextPos == 0) - offset = baseOffset; - else if (nextPos < baseOffset) - return RESULT_ERR_INVALID_ARG; // missing pos definition - else - offset = nextPos; length = 0; } else { offset = 0; @@ -141,49 +165,46 @@ result_t DataField::create(const unsigned char dstAddress, const bool isSetMessa if (end != start+strlen(start)) return RESULT_ERR_INVALID_ARG; // invalid pos definition - if (baseOffset+pos > maxPos) + if (pos > maxPos) // TODO check this in real offset as well return RESULT_ERR_INVALID_ARG; // invalid pos definition if (offsetCnt==1) - offset = baseOffset+pos; - else if (baseOffset+pos >= offset) - length = baseOffset+pos+1-offset; + offset = pos; + else if (pos >= offset) + length = pos+1-offset; else { // wrong order e.g. 4-3 - length = offset-(baseOffset+pos+1); - offset = baseOffset+pos; + length = offset-(pos+1); + offset = pos; } } - if (offset < baseOffset) - return RESULT_ERR_INVALID_ARG; //invalid pos definition } const char* typeStr = (*it++).c_str(); + if (typeStr[0] == 0) + return RESULT_ERR_EOF; std::map values; - if (it == end) - divisor = 1; - else { + if (it != end) { std::string divisorStr = *it++; - if (divisorStr.empty() == true) - divisor = 1; - else if (divisorStr.find_first_not_of("0123456789") == std::string::npos) { - const char* start = divisorStr.c_str(); - char* end = NULL; - divisor = strtoul(start, &end, 10); - if (end != start+strlen(start)) - return RESULT_ERR_INVALID_ARG; - } - else { - divisor = 1; - std::istringstream stream(divisorStr); - while (std::getline(stream, token, VALUE_SEPARATOR) != 0) { - const char* start = token.c_str(); + if (divisorStr.empty() == false) { + if (divisorStr.find_first_not_of("0123456789") == std::string::npos) { + const char* start = divisorStr.c_str(); char* end = NULL; - unsigned int id = strtoul(start, &end, 10); - if (end == NULL || end == start || *end != '=') + divisor = strtoul(start, &end, 10); + if (end != start+strlen(start)) return RESULT_ERR_INVALID_ARG; + } + else { + std::istringstream stream(divisorStr); + while (std::getline(stream, token, VALUE_SEPARATOR) != 0) { + const char* start = token.c_str(); + char* end = NULL; + unsigned int id = strtoul(start, &end, 10); + if (end == NULL || end == start || *end != '=') + return RESULT_ERR_INVALID_ARG; - values[id] = std::string(end+1); + values[id] = std::string(end+1); + } } } } @@ -204,19 +225,50 @@ result_t DataField::create(const unsigned char dstAddress, const bool isSetMessa if (comment.length() == 1 && comment[0] == '-') comment.clear(); } -//TODO derive more specific subtypes + + // check for reference(s) to templates + if (templates.empty() == false) { + std::istringstream stream(typeStr); + bool found = false; + while (std::getline(stream, token, VALUE_SEPARATOR) != 0) { + std::map::const_iterator ref = templates.find(token); + if (ref == templates.end()) { + if (found == false) + break; // fallback to direct definition + return RESULT_ERR_INVALID_ARG; // cannot mix reference and direct definition + } + found = true; + DataField* templ = ref->second; + dataType_t dataType = templ->m_dataType; + unsigned char numBytes = (dataType.numBits+7)/8; + if (length != 0 && length != templ->m_length) + return RESULT_ERR_INVALID_ARG; // different length not possible for derivation + unsigned char deriveOffset = offset == 0 ? templ->m_offset : offset; + if (templ->m_partType != pt_template && partType == pt_template) + return RESULT_ERR_INVALID_ARG; + DataField* field = templ->derive(name, partType, deriveOffset, unit, comment, divisor, values); + if (field == NULL) + return RESULT_ERR_INVALID_ARG; // non-supported type for derivation or invalid argument + fields.push_back(field); + } + if (found == true) + return RESULT_OK; + } + for (size_t i = 0; i < sizeof(dataTypes)/sizeof(dataTypes[0]); i++) { dataType_t dataType = dataTypes[i]; if (strcasecmp(typeStr, dataType.name) == 0) { + unsigned char numBytes = (dataType.numBits+7)/8; + unsigned char useLength = length; if ((dataType.flags&ADJ) != 0) { - if (length == 0) - length = 1; // minimum length defaults to 1 - else if (length > dataType.numBytes) + if (useLength == 0) + useLength = 1; // minimum length defaults to 1 + else if (useLength > numBytes) return RESULT_ERR_INVALID_ARG; // invalid length } - else if (length == 0) - length = dataType.numBytes; - else if (length != dataType.numBytes) + else if (useLength == 0) + useLength = numBytes; + else if (useLength != numBytes) continue; // check for another one with same name but different length switch (dataType.type) { @@ -224,8 +276,7 @@ result_t DataField::create(const unsigned char dstAddress, const bool isSetMessa case bt_hexstr: case bt_dat: case bt_tim: - fields.push_back(new StringDataField(name, partType, offset, length, dataType, unit, comment)); - nextPos = offset+length; + fields.push_back(new StringDataField(name, partType, offset, useLength, dataType, unit, comment)); return RESULT_OK; case bt_num: if (values.empty() == true && (dataType.flags&DAY) != 0) { @@ -233,18 +284,17 @@ result_t DataField::create(const unsigned char dstAddress, const bool isSetMessa values[dataType.minValueOrLength + i] = dayNames[i]; } if (values.empty() == true || (dataType.flags&LST) == 0) { - fields.push_back(new NumberDataField(name, partType, offset, length, dataType, unit, comment, divisor)); - nextPos = offset+length; + if (divisor == 0) { + divisor = dataType.divisor; + } else + divisor *= dataType.divisor; + fields.push_back(new NumberDataField(name, partType, offset, useLength, dataType, unit, comment, divisor)); return RESULT_OK; } - if (values.begin()->first < dataType.minValueOrLength) + if (values.begin()->first < dataType.minValueOrLength + || values.rbegin()->first > dataType.maxValueOrLength) return RESULT_ERR_INVALID_ARG; - std::map::iterator end = values.end(); - end--; - if (end->first > dataType.maxValueOrLength) - return RESULT_ERR_INVALID_ARG; - fields.push_back(new ValueListDataField(name, partType, offset, length, dataType, unit, comment, values)); - nextPos = offset+length; + fields.push_back(new ValueListDataField(name, partType, offset, useLength, dataType, unit, comment, values)); return RESULT_OK; } } @@ -255,18 +305,22 @@ result_t DataField::create(const unsigned char dstAddress, const bool isSetMessa result_t DataField::read(SymbolString& masterData, SymbolString& slaveData, std::ostringstream& output, bool verbose) { SymbolString& input = m_partType == pt_masterData ? masterData : slaveData; + unsigned char baseOffset; switch (m_partType) { case pt_masterData: + baseOffset = 5; // skip QQ ZZ PB SB NN break; case pt_slaveData: + baseOffset = 1; // skip NN break; default: return RESULT_ERR_INVALID_ARG; // invalid part type } + if (verbose) output << m_name << "="; - result_t result = readSymbols(input, output); + result_t result = readSymbols(input, baseOffset, output); if (result != RESULT_OK) return result; @@ -278,29 +332,42 @@ result_t DataField::read(SymbolString& masterData, SymbolString& slaveData, std: return RESULT_OK; } -result_t DataField::write(const std::string& value, SymbolString& masterData, SymbolString& slaveData) +result_t DataField::write(std::istringstream& input, SymbolString& masterData, SymbolString& slaveData) { SymbolString& output = m_partType == pt_masterData ? masterData : slaveData; + unsigned char baseOffset; switch (m_partType) { case pt_masterData: + baseOffset = 5; // skip QQ ZZ PB SB NN + break; case pt_slaveData: + baseOffset = 1; // skip NN break; default: return RESULT_ERR_INVALID_ARG; } - std::istringstream input(value); - return writeSymbols(input, output); + return writeSymbols(input, baseOffset, output); } -result_t StringDataField::readSymbols(SymbolString& input, std::ostringstream& output) +DataField* StringDataField::derive(std::string name, PartType partType, unsigned char offset, std::string unit, + std::string comment, unsigned int divisor, std::map values) { + if (unit.empty() == true) + unit = m_unit; + if (comment.empty() == true) + comment = m_comment; + + return new StringDataField(name, partType, offset, m_length, m_dataType, unit, comment); +} + +result_t StringDataField::readSymbols(SymbolString& input, unsigned char baseOffset, std::ostringstream& output) { size_t start = m_offset, end = m_offset + m_length; int incr = 1; unsigned char ch; - if (end > input.size()) + if (baseOffset + end > input.size()) return RESULT_ERR_INVALID_ARG; if ((m_dataType.flags&REV) != 0) { // reverted binary representation (most significant byte first) @@ -309,10 +376,10 @@ result_t StringDataField::readSymbols(SymbolString& input, std::ostringstream& o incr = -1; } - for (size_t pos = start, i = 0; pos != end; pos += incr, i++) { + for (size_t offset = start, i = 0; offset != end; offset += incr, i++) { if (m_length == 4 && i == 2 && m_dataType.type == bt_dat) continue; // skip weekday in between - ch = input[pos]; + ch = input[baseOffset + offset]; if ((m_dataType.flags & BCD) != 0) { if ((ch & 0xf0) > 0x90 || (ch & 0x0f) > 0x09) return RESULT_ERR_INVALID_ARG; // invalid BCD @@ -337,7 +404,7 @@ result_t StringDataField::readSymbols(SymbolString& input, std::ostringstream& o if (m_length == 1) { // truncated time if (i == 0) { ch /= 6; // hours - pos -= incr; // repeat for minutes + offset -= incr; // repeat for minutes } else ch = (ch%6) * 10; // minutes @@ -359,7 +426,7 @@ result_t StringDataField::readSymbols(SymbolString& input, std::ostringstream& o return RESULT_OK; } -result_t StringDataField::writeSymbols(std::istringstream& input, SymbolString& output) +result_t StringDataField::writeSymbols(std::istringstream& input, unsigned char baseOffset, SymbolString& output) { size_t start = m_offset, end = m_offset + m_length; int incr = 1; @@ -374,7 +441,8 @@ result_t StringDataField::writeSymbols(std::istringstream& input, SymbolString& incr = -1; } - for (size_t pos = start, i = 0; pos != end; pos += incr, i++) { + size_t i = 0; + for (size_t offset = start; offset != end; offset += incr, i++) { switch (m_dataType.type) { case bt_hexstr: while (input.peek()==' ') @@ -424,7 +492,7 @@ result_t StringDataField::writeSymbols(std::istringstream& input, SymbolString& return RESULT_ERR_INVALID_ARG; // invalid time part if (m_length == 1) { // truncated time if (i == 0) { - pos -= incr; // repeat for minutes + offset -= incr; // repeat for minutes hours = value; continue; } @@ -448,20 +516,23 @@ result_t StringDataField::writeSymbols(std::istringstream& input, SymbolString& } if (value > 0xff) return RESULT_ERR_INVALID_ARG; // value out of range - output[pos] = (unsigned char)value; + output[baseOffset + offset] = (unsigned char)value; } + if (i < m_length) + return RESULT_ERR_INVALID_ARG; // input too short + return RESULT_OK; } -result_t NumericDataField::readRawValue(SymbolString& input, unsigned int& value) +result_t NumericDataField::readRawValue(SymbolString& input, unsigned char baseOffset, unsigned int& value) { size_t start = m_offset, end = m_offset + m_length; int incr = 1; unsigned char ch; - if (end > input.size()) + if (baseOffset + end > input.size()) return RESULT_ERR_INVALID_ARG; // not enough data available if ((m_dataType.flags&REV) != 0) { // reverted binary representation (most significant byte first) @@ -471,8 +542,8 @@ result_t NumericDataField::readRawValue(SymbolString& input, unsigned int& value } value = 0; - for (size_t pos = start, exp = 1; pos != end; pos += incr) { - ch = input[pos]; + for (size_t offset = start, exp = 1; offset != end; offset += incr) { + ch = input[baseOffset + offset]; if ((m_dataType.flags & BCD) != 0) { if (ch == m_dataType.replacement) { value = m_dataType.replacement; @@ -483,17 +554,23 @@ result_t NumericDataField::readRawValue(SymbolString& input, unsigned int& value ch = (ch >> 4) * 10 + (ch & 0x0f); value += ch*exp; - exp = exp*100; + exp *= 100; } else { value |= ch*exp; - exp = exp<<8; + exp <<= 8; } } + + if ((m_dataType.flags & BCD) == 0) { + value >>= m_bitOffset; + if ((m_dataType.numBits%8) != 0) + value &= (1< values) { + if (unit.empty() == true) + unit = m_unit; + if (comment.empty() == true) + comment = m_comment; + if (divisor == 0) + divisor = m_divisor; + else + divisor *= m_dataType.divisor; + + return new NumberDataField(name, partType, offset, m_length, m_dataType, unit, comment, divisor); +} + +result_t NumberDataField::readSymbols(SymbolString& input, unsigned char baseOffset, std::ostringstream& output) { unsigned int value = 0; int signedValue; - result_t result = readRawValue(input, value); + result_t result = readRawValue(input, baseOffset, value); if (result != RESULT_OK) return result; @@ -539,65 +638,70 @@ result_t NumberDataField::readSymbols(SymbolString& input, std::ostringstream& o output << NULL_VALUE; return RESULT_OK; } - bool negative = (m_dataType.flags&SIG) != 0 && (value & (1 << (m_dataType.numBytes*8 - 1))) != 0; - if (m_dataType.numBytes == 4) { + bool negative = (m_dataType.flags&SIG) != 0 && (value & (1 << (m_dataType.numBits - 1))) != 0; + if (m_dataType.numBits == 32) { if (negative == false) { if (m_divisor <= 1) output << static_cast(value); else - output << std::setprecision(3) << std::fixed << static_cast(value / (float)m_divisor); + output << std::setprecision((m_dataType.numBits%8)==0 ? m_dataType.precisionOrFirstBit : 0) + << std::fixed << static_cast(value / (float)m_divisor); return RESULT_OK; } signedValue = (int)value; // negative signed value } else if (negative) // negative signed value - signedValue = (int)value - (1 << (m_dataType.numBytes*8)); + signedValue = (int)value - (1 << m_dataType.numBits); else signedValue = (int)value; if (m_divisor <= 1) output << static_cast(signedValue); else - output << std::setprecision(3) << std::fixed << static_cast(signedValue / (float)m_divisor); + output << std::setprecision((m_dataType.numBits%8)==0 ? m_dataType.precisionOrFirstBit : 0) + << std::fixed << static_cast(signedValue / (float)m_divisor); return RESULT_OK; } -result_t NumberDataField::writeSymbols(std::istringstream& input, SymbolString& output) +result_t NumberDataField::writeSymbols(std::istringstream& input, unsigned char baseOffset, SymbolString& output) { unsigned int value; const char* str = input.str().c_str(); + size_t len = strlen(str); if (strcasecmp(str, NULL_VALUE) == 0) // replacement value value = m_dataType.replacement; + else if (len == 0) + return RESULT_ERR_INVALID_ARG; // input too short else { char* strEnd = NULL; if (m_divisor <= 1) { if ((m_dataType.flags&SIG) != 0) { int signedValue = strtol(str, &strEnd, 10); - if (signedValue < 0 && m_dataType.numBytes != 4) - value = (unsigned int)(signedValue + (1<<(m_dataType.numBytes*8))); + if (signedValue < 0 && m_dataType.numBits != 32) + value = (unsigned int)(signedValue + (1<= (1LL<<(8*m_length))) return RESULT_ERR_INVALID_ARG; // value out of range - if (dvalue < 0 && m_dataType.numBytes != 4) - value = (unsigned int)(dvalue + (1<<(m_dataType.numBytes*8))); + if (dvalue < 0 && m_dataType.numBits != 32) + value = (unsigned int)(dvalue + (1< values) { + if (unit.empty() == true) + unit = m_unit; + if (comment.empty() == true) + comment = m_comment; + + if (values.empty() == true) { + values = m_values; + } + else if (values.begin()->first < m_dataType.minValueOrLength + || values.rbegin()->first > m_dataType.maxValueOrLength) + return NULL; + + return new ValueListDataField(name, partType, offset, m_length, m_dataType, unit, comment, values); +} + +result_t ValueListDataField::readSymbols(SymbolString& input, unsigned char baseOffset, std::ostringstream& output) { unsigned int value = 0; - result_t result = readRawValue(input, value); + result_t result = readRawValue(input, baseOffset, value); if (result != RESULT_OK) return result; + std::map::iterator it = m_values.find(value); + if (it != m_values.end()) { + output << it->second; + return RESULT_OK; + } + if (value == m_dataType.replacement) { output << NULL_VALUE; return RESULT_OK; } - std::map::iterator it = m_values.find(value); - if (it == m_values.end()) - return RESULT_ERR_INVALID_ARG; // value assignment not found - - output << it->second; - return RESULT_OK; + return RESULT_ERR_INVALID_ARG; // value assignment not found } -result_t ValueListDataField::writeSymbols(std::istringstream& input, SymbolString& output) +result_t ValueListDataField::writeSymbols(std::istringstream& input, unsigned char baseOffset, SymbolString& output) { - std::string str; - input >> str; + const char* str = input.str().c_str(); for (std::map::iterator it = m_values.begin(); it != m_values.end(); it++) if (it->second.compare(str) == 0) - return writeRawValue(it->first, output); + return writeRawValue(it->first, baseOffset, output); + + if (strcasecmp(str, NULL_VALUE) == 0) // replacement value + return writeRawValue(m_dataType.replacement, baseOffset, output); return RESULT_ERR_INVALID_ARG; // value assignment not found } diff --git a/src/lib/ebus/data.h b/src/lib/ebus/data.h index e0e8dabe..f156fd80 100644 --- a/src/lib/ebus/data.h +++ b/src/lib/ebus/data.h @@ -32,6 +32,7 @@ namespace libebus /** the message part in which a data field is stored. */ enum PartType { + pt_template, // special part type for templates (relative offset) pt_masterData, // stored in master data pt_slaveData, // stored in slave data }; @@ -46,23 +47,24 @@ enum BaseType { }; /** flags for dataType_t. */ -const unsigned int ADJ = 0x01; // adjustable length, numBytes is maximum length +const unsigned int ADJ = 0x01; // adjustable length, numBits is maximum length const unsigned int BCD = 0x02; // binary representation is BCD const unsigned int REV = 0x04; // reverted binary representation (most significant byte first) const unsigned int SIG = 0x08; // signed value const unsigned int LST = 0x10; // value list is possible (without applied divisor) -const unsigned int DAY = 0x20; // default value list is week days +const unsigned int DAY = 0x20; // forced value list defaulting to week days /** the structure for defining field types with their properties. */ typedef struct { - const char* name; // field identifier - const unsigned int numBytes; // number of bytes (maximum length if ADJ flag is set) - const BaseType type; // base data type - const unsigned int flags; // flags (e.g. BCD) - const unsigned int replacement; // replacement value (fill-up value for bt_str/bt_hexstr) - const unsigned int minValueOrLength; // minimum binary value (minimum length of string for StringDataField) - const unsigned int maxValueOrLength; // maximum binary value (maximum length of string for StringDataField) - const unsigned int divisor; // divisor for bt_number values (or 0 for non-numeric) + const char* name; // field identifier + const unsigned int numBits; // number of bits (maximum length if ADJ flag is set, must be multiple of 8 with flag BCD) + const BaseType type; // base data type + const unsigned int flags; // flags (e.g. BCD) + const unsigned int replacement; // replacement value (fill-up value for bt_str/bt_hexstr, no replacement if equal to minValueOrLength for bt_num) + const unsigned int minValueOrLength; // minimum binary value (minimum length of string for StringDataField) + const unsigned int maxValueOrLength; // maximum binary value (maximum length of string for StringDataField) + const unsigned int divisor; // bt_number: divisor + const unsigned char precisionOrFirstBit; // bt_number: precision for formatting or offset to first bit if (numBits%8)!=0 } dataType_t; @@ -76,7 +78,7 @@ public: * @brief Constructs a new instance. * @param name the field name. * @param partType the message part in which the field is stored. - * @param offset the offset to the first symbol in the message part in which the field is stored. + * @param offset the relatvie offset to the first symbol in the message part in which the field is stored. * @param length the number of symbols in the message part in which the field is stored. * @param dataType the data type definition. * @param unit the value unit. @@ -95,21 +97,19 @@ public: virtual ~DataField() {} /** - * @brief Factory method for creating a new instance. - * @param dstAddress the destination bus address. - * @param isSetMessage whether the field is part of a set message. + * @brief Factory method for creating new instances. * @param it the iterator to traverse for the definition parts. * @param end the iterator pointing to the end of the definition parts. - * @param predefined a map of predefined DataFields to be referenced by name. + * @param templates a map of DataField templates to be referenced by name. * @param fields the vector to which created instances are added. - * @param nextPos the variable holding the next subsequent position. + * @param isSetMessage whether the field is part of a set message (default false). + * @param dstAddress the destination bus address (default @a SYN for creating a template DataFields). * @return RESULT_OK on success, RESULT_ERR_EOF if the iterator is empty, or an error code. * Note: the caller needs to cleanup created instances. */ - static result_t create(const unsigned char dstAddress, const bool isSetMessage, - std::vector::iterator& it, const std::vector::iterator end, - const std::map predefined, std::vector& fields, - unsigned char& nextPos); + static result_t create(std::vector::iterator& it, const std::vector::iterator end, + const std::map templates, std::vector& fields, + const bool isSetMessage=false, const unsigned char dstAddress=SYN); /** * @brief Reads the value from the master or slave @a SymbolString. @@ -124,34 +124,42 @@ public: bool verbose=false); /** * @brief Writes the value to the master or slave @a SymbolString. + * @param input the istringstream to parse the formatted value from. * @param masterData the unescaped master data @a SymbolString for writing binary data. * @param slaveData the unescaped slave data @a SymbolString for writing binary data. - * @param value the formatted value as string. * @return RESULT_OK on success, or an error code. */ - result_t write(const std::string& value, SymbolString& masterData, SymbolString& slaveData); + result_t write(std::istringstream& input, SymbolString& masterData, SymbolString& slaveData); + /** + * @brief Get the field name. + * @return the field name. + */ + const std::string getName() { return m_name; } + //TODO add getter for all fields protected: + virtual DataField* derive(std::string name, PartType partType, unsigned char offset, std::string unit, + std::string comment, unsigned int divisor, std::map values) = 0; /** * @brief Internal method for reading the field from a @a SymbolString. * @param input the unescaped @a SymbolString to read the binary value from. * @param output the ostringstream to append the formatted value to. * @return RESULT_OK on success, or an error code. */ - virtual result_t readSymbols(SymbolString& input, std::ostringstream& output) = 0; + virtual result_t readSymbols(SymbolString& input, unsigned char baseOffset, std::ostringstream& output) = 0; /** * @brief Internal method for writing the field to a @a SymbolString. * @param input the istringstream to parse the formatted value from. * @param output the unescaped @a SymbolString to write the binary value to. * @return RESULT_OK on success, or an error code. */ - virtual result_t writeSymbols(std::istringstream& input, SymbolString& output) = 0; + virtual result_t writeSymbols(std::istringstream& input, unsigned char baseOffset, SymbolString& output) = 0; /** the field name. */ const std::string m_name; /** the message part in which the field is stored. */ const PartType m_partType; - /** the offset to the first symbol in the message part in which the field is stored. */ + /** the relative offset to the first symbol in the message part in which the field is stored. */ const unsigned char m_offset; /** the number of symbols in the message part in which the field is stored. */ const unsigned char m_length; @@ -191,8 +199,10 @@ public: virtual ~StringDataField() {} protected: - virtual result_t readSymbols(SymbolString& input, std::ostringstream& output); - virtual result_t writeSymbols(std::istringstream& input, SymbolString& output); + virtual DataField* derive(std::string name, PartType partType, unsigned char offset, std::string unit, + std::string comment, unsigned int divisor, std::map values); + virtual result_t readSymbols(SymbolString& input, unsigned char baseOffset, std::ostringstream& output); + virtual result_t writeSymbols(std::istringstream& input, unsigned char baseOffset, SymbolString& output); }; @@ -209,6 +219,7 @@ public: * @param partType the message part in which the field is stored. * @param offset the offset to the first symbol in the message part in which the field is stored. * @param length the number of symbols in the message part in which the field is stored. + * @param bitOffset the offset to the first bit in the binary value. * @param dataType the data type definition. * @param unit the value unit. * @param comment the field comment. @@ -216,8 +227,9 @@ public: NumericDataField(const std::string name, const PartType partType, const unsigned char offset, const unsigned char length, const dataType_t dataType, const std::string unit, - const std::string comment) - : DataField(name, partType, offset, length, dataType, unit, comment) {} + const std::string comment, const unsigned char bitOffset) + : DataField(name, partType, offset, length, dataType, unit, comment), + m_bitOffset(bitOffset) {} /** * @brief Destructor. */ @@ -230,14 +242,17 @@ protected: * @param value the variable in which to store the raw value. * @return RESULT_OK on success, or an error code. */ - result_t readRawValue(SymbolString& input, unsigned int& value); + result_t readRawValue(SymbolString& input, unsigned char baseOffset, unsigned int& value); /** * @brief Internal method for writing the raw value to a @a SymbolString. * @param value the raw value to write. * @param output the unescaped @a SymbolString to write the binary value to. * @return RESULT_OK on success, or an error code. */ - result_t writeRawValue(unsigned int value, SymbolString& output); + result_t writeRawValue(unsigned int value, unsigned char baseOffset, SymbolString& output); + + /** the offset to the first bit in the binary value. */ + const unsigned char m_bitOffset; }; @@ -262,16 +277,19 @@ public: const unsigned char offset, const unsigned char length, const dataType_t dataType, const std::string unit, const std::string comment, const unsigned int divisor) - : NumericDataField(name, partType, offset, length, dataType, unit, comment), - m_divisor(divisor * dataType.divisor) {} + : NumericDataField(name, partType, offset, length, dataType, unit, comment, + (dataType.numBits%8) != 0 ? dataType.precisionOrFirstBit : 0), + m_divisor(divisor) {} /** * @brief Destructor. */ virtual ~NumberDataField() {} protected: - virtual result_t readSymbols(SymbolString& input, std::ostringstream& output); - virtual result_t writeSymbols(std::istringstream& input, SymbolString& output); + virtual DataField* derive(std::string name, PartType partType, unsigned char offset, std::string unit, + std::string comment, unsigned int divisor, std::map values); + virtual result_t readSymbols(SymbolString& input, unsigned char baseOffset, std::ostringstream& output); + virtual result_t writeSymbols(std::istringstream& input, unsigned char baseOffset, SymbolString& output); /** the combined divisor to apply on the value, or 1 for none. */ const unsigned int m_divisor; @@ -299,7 +317,8 @@ public: const unsigned char offset, const unsigned char length, const dataType_t dataType, const std::string unit, const std::string comment, const std::map values) - : NumericDataField(name, partType, offset, length, dataType, unit, comment), + :NumericDataField(name, partType, offset, length, dataType, unit, comment, + (dataType.numBits%8) != 0 ? dataType.precisionOrFirstBit : 0), m_values(values) {} /** * @brief Destructor. @@ -307,8 +326,10 @@ public: virtual ~ValueListDataField() {} protected: - virtual result_t readSymbols(SymbolString& input, std::ostringstream& output); - virtual result_t writeSymbols(std::istringstream& input, SymbolString& output); + virtual DataField* derive(std::string name, PartType partType, unsigned char offset, std::string unit, + std::string comment, unsigned int divisor, std::map values); + virtual result_t readSymbols(SymbolString& input, unsigned char baseOffset, std::ostringstream& output); + virtual result_t writeSymbols(std::istringstream& input, unsigned char baseOffset, SymbolString& output); /** the value=text assignments. */ std::map m_values; diff --git a/src/lib/ebus/test/test_data.cpp b/src/lib/ebus/test/test_data.cpp index c2ff3e70..e4ebe4a8 100644 --- a/src/lib/ebus/test/test_data.cpp +++ b/src/lib/ebus/test/test_data.cpp @@ -23,116 +23,269 @@ using namespace libebus; +void verify(bool expectFailMatch, std::string type, std::string input, bool match, std::string expectStr, std::string gotStr) { + if (expectFailMatch == true) { + if (match == true) + std::cout << " failed " << type << " match >" << input << "< error: unexpectedly succeeded" << std::endl; + else + std::cout << " failed " << type << " match >" << input << "< OK" << std::endl; + } else if (match == true) + std::cout << " " << type << " >" << input << "< OK" << std::endl; + else + std::cout << " " << type << " >" << input << "< error: got >" << gotStr + << "<, expected >" << expectStr << "<" << std::endl; +} + int main () { - std::string checks[][4] = { - //name;position(s);type;factor;unit;comment -// {"temp;1;d2b;;°C;Aussentemperatur","temp=18.004 °C [Aussentemperatur]","10fe070009019258042126100714cc", "00"}, -// {"zeit;1;ttm;2;Uhr;","zeit=22:40 Uhr","10feffff0188", "00"}, - {"x;1-10;hex","53 70 65 69 63 68 65 72 20 20", "10fe07000a53706569636865722020", "00"}, - {"x;;bti","21:04:58","10fe070009580421", "00"}, - {"x;;bda","26.10.2014","10fe07000926100714", "00"}, - {"x;1-3;bda","26.10.2014","10fe070003261014", "00"}, - {"x;;hdy","Sun","10fe07000307", "00"}, - {"x;;bdy","Sun","10fe07000306", "00"}, - {"x;;d2b","18.004","10fe0700090112", "00"}, - {"x;;d2c","288.062","10fe0700090112", "00"}, - {"x;;ttm","22:40","10feffff0188", "00"}, - {"x;;bcd","26","10feffff0126", "00"}, - {"x;;bcd","-","10feffff01ff", "00"}, - {"x;;uch","38","10feffff0126", "00"}, - {"x;;sch","-90","10feffff01a6", "00"}, - {"x;;d1b","-90","10feffff01a6", "00"}, - {"x;;d1c","19.500","10feffff0127", "00"}, - {"x;;uin","38","10feffff022600", "00"}, - {"x;;sin","-90","10feffff02a6ff", "00"}, - {"x;;ulg","38","10feffff0426000000", "00"}, - {"x;;slg","-90","10feffff04a6ffffff", "00"}, - {"x;;flt","-0.090","10feffff02a6ff", "00"}, - {"x;1-9;str","hallo Du!","10feffff0868616c6c6f20447521", "00"}, - {"x;1-9;str","hallo Du ","10feffff0868616c6c6f20447520", "00"}, - {"new;1;uch;1=test,2=high,3=off,4=on","on","10feffff0104", "00"}, + std::string checks[][5] = { + //name;[pos];type[;[divisor|values][;[unit][;[comment]]]], decoded value, master, slave, flags + {"x;1-10;str", "Hallo, Du!", "10fe07000a48616c6c6f2c20447521", "00", ""}, + {"x;1-10;str", "Hallo, Du ", "10fe07000a48616c6c6f2c20447520", "00", ""}, + {"x;1-10;str", " ", "10fe07000a20202020202020202020", "00", ""}, + {"x;1-11;str", "", "10fe07000a20202020202020202020", "00", "rW"}, + {"x;;hex", "20", "10fe07000120", "00", ""}, + {"x;1-10;hex", "48 61 6c 6c 6f 2c 20 44 75 21", "10fe07000a48616c6c6f2c20447521", "00", ""}, + {"x;1-11;hex", "", "10fe07000a48616c6c6f2c20447521", "00", "rW"}, + {"x;;bda", "26.10.2014","10fe07000426100014", "00", ""}, + {"x;;bda", "01.01.2000","10fe07000401010000", "00", ""}, + {"x;;bda", "31.12.2099","10fe07000431120099", "00", ""}, + {"x;;bda", "", "10fe07000432100014", "00", "rw"}, + {"x;1-3;bda","26.10.2014","10fe070003261014", "00", ""}, + {"x;1-3;bda","01.01.2000","10fe070003010100", "00", ""}, + {"x;1-3;bda","31.12.2099","10fe070003311299", "00", ""}, + {"x;1-3;bda","", "10fe070003321299", "00", "rw"}, + {"x;;bti", "21:04:58", "10fe070003580421", "00", ""}, + {"x;;bti", "00:00:00", "10fe070003000000", "00", ""}, + {"x;;bti", "23:59:59", "10fe070003595923", "00", ""}, + {"x;;bti", "", "10fe070003605923", "00", "rw"}, + {"x;;htm", "21:04", "10fe0700021504", "00", ""}, + {"x;;htm", "00:00", "10fe0700020000", "00", ""}, + {"x;;htm", "23:59", "10fe070002173b", "00", ""}, + {"x;;htm", "", "10fe070002183b", "00", "rw"}, + {"x;;htm", "24:00", "10fe070002173b", "00", "Rw"}, + {"x;;ttm", "22:40", "10fe07000188", "00", ""}, + {"x;;ttm", "00:00", "10fe07000100", "00", ""}, + {"x;;ttm", "23:50", "10fe0700018f", "00", ""}, + {"x;;ttm", "24:00", "10fe07000190", "00", "rw"}, // TODO check range + {"x;;ttm", "", "10fe07000191", "00", "rw"}, // TODO check range + {"x;;bdy", "Mon", "10fe07000300", "00", ""}, + {"x;;bdy", "Sun", "10fe07000306", "00", ""}, + {"x;;bdy", "", "10fe07000308", "00", "rw"}, + {"x;;hdy", "Mon", "10fe07000301", "00", ""}, + {"x;;hdy", "Sun", "10fe07000307", "00", ""}, + {"x;;hdy", "", "10fe07000308", "00", "rw"}, + {"x;;bcd", "26", "10feffff0126", "00", ""}, + {"x;;bcd", "0", "10feffff0100", "00", ""}, + {"x;;bcd", "99", "10feffff0199", "00", ""}, + {"x;;bcd", "-", "10feffff01ff", "00", ""}, + {"x;;bcd", "", "10feffff019a", "00", "rw"}, + {"x;;uch", "38", "10feffff0126", "00", ""}, + {"x;;uch", "0", "10feffff0100", "00", ""}, + {"x;;uch", "254", "10feffff01fe", "00", ""}, + {"x;;uch", "-", "10feffff01ff", "00", ""}, + {"x;;sch", "-90", "10feffff01a6", "00", ""}, + {"x;;sch", "0", "10feffff0100", "00", ""}, + {"x;;sch", "-1", "10feffff01ff", "00", ""}, + {"x;;sch", "-", "10feffff0180", "00", ""}, + {"x;;sch", "-127", "10feffff0181", "00", ""}, + {"x;;sch", "127", "10feffff017f", "00", ""}, + {"x;;d1b", "-90", "10feffff01a6", "00", ""}, + {"x;;d1b", "0", "10feffff0100", "00", ""}, + {"x;;d1b", "-1", "10feffff01ff", "00", ""}, + {"x;;d1b", "-", "10feffff0180", "00", ""}, + {"x;;d1b", "-127", "10feffff0181", "00", ""}, + {"x;;d1b", "127", "10feffff017f", "00", ""}, + {"x;;d1c", "19.5", "10feffff0127", "00", ""}, + {"x;;d1c", "0.0", "10feffff0100", "00", ""}, + {"x;;d1c", "100.0", "10feffff01c8", "00", ""}, + {"x;;d1c", "-", "10feffff01ff", "00", ""}, + {"x;;uin", "38", "10feffff022600", "00", ""}, + {"x;;uin", "0", "10feffff020000", "00", ""}, + {"x;;uin", "65534", "10feffff02feff", "00", ""}, + {"x;;uin", "-", "10feffff02ffff", "00", ""}, + {"x;;sin", "-90", "10feffff02a6ff", "00", ""}, + {"x;;sin", "0", "10feffff020000", "00", ""}, + {"x;;sin", "-1", "10feffff02ffff", "00", ""}, + {"x;;sin", "-", "10feffff020080", "00", ""}, + {"x;;sin", "-32767", "10feffff020180", "00", ""}, + {"x;;sin", "32767", "10feffff02ff7f", "00", ""}, + {"x;;flt", "-0.090", "10feffff02a6ff", "00", ""}, + {"x;;flt", "0.000", "10feffff020000", "00", ""}, + {"x;;flt", "-0.001", "10feffff02ffff", "00", ""}, + {"x;;flt", "-", "10feffff020080", "00", ""}, + {"x;;flt","-32.767", "10feffff020180", "00", ""}, + {"x;;flt", "32.767", "10feffff02ff7f", "00", ""}, + {"x;;d2b", "18.004", "10fe0700090112", "00", ""}, + {"x;;d2b", "0.000", "10feffff020000", "00", ""}, + {"x;;d2b", "-0.004", "10feffff02ffff", "00", ""}, + {"x;;d2b", "-", "10feffff020080", "00", ""}, + {"x;;d2b","-127.996","10feffff020180", "00", ""}, + {"x;;d2b", "127.996","10feffff02ff7f", "00", ""}, + {"x;;d2c", "288.06", "10fe0700090112", "00", ""}, + {"x;;d2c", "0.00", "10feffff020000", "00", ""}, + {"x;;d2c", "-0.06", "10feffff02ffff", "00", ""}, + {"x;;d2c", "-", "10feffff020080", "00", ""}, + {"x;;d2c","-2047.94","10feffff020180", "00", ""}, + {"x;;d2c", "2047.94","10feffff02ff7f", "00", ""}, + {"x;;ulg", "38", "10feffff0426000000", "00", ""}, + {"x;;ulg", "0", "10feffff0400000000", "00", ""}, + {"x;;ulg", "4294967294", "10feffff04feffffff", "00", ""}, + {"x;;ulg", "-", "10feffff04ffffffff", "00", ""}, + {"x;;slg", "-90", "10feffff04a6ffffff", "00", ""}, + {"x;;slg", "0", "10feffff0400000000", "00", ""}, + {"x;;slg", "-1", "10feffff04ffffffff", "00", ""}, + {"x;;bi3", "1", "10feffff0108", "00", ""}, + {"x;;bi3", "-", "10feffff0100", "00", ""}, + {"x;;bi3;0=off,1=on","on", "10feffff0108", "00", ""}, + {"x;;bi3;0=off,1=on","off","10feffff0100", "00", ""}, + {"x;;b34", "1", "10feffff0108", "00", ""}, + {"x;;b34", "-", "10feffff0100", "00", ""}, + {"x;;b34", "3", "10feffff0118", "00", ""}, + {"x;;b34;1=on","on", "10feffff0108", "00", ""}, + {"x;;b34;1=on","-", "10feffff0100", "00", ""}, + {"x;;b34;0=off,1=on,2=auto,3=eco","auto", "10feffff0110", "00", ""}, + {"x;;b34;0=off,1=on","on", "10feffff0108", "00", ""}, + {"x;;b34;0=off,1=on","off","10feffff0100", "00", ""}, + {"x;;uch;1=test,2=high,3=off,4=on","on","10feffff0104", "00", ""}, + {"x;s3;uch","3","1050ffff00", "03000003", ""}, + {"x;s3;uch","3","1050ffff00", "020000", "rW"}, + {"x;;d2b;;°C;Aussentemperatur","x=18.004 °C [Aussentemperatur]","10fe0700090112", "00", "v"}, + {"x;;bti;;;;y;;bda;;;;z;6;bdy", "21:04:58;26.10.2014;Sun","10fe07000758042126100614", "00", "c"}, + //TODO test bit combinations + {"temprel;;d2b;;°C;Aussentemperatur","","", "", "p"}, // predefined type with relative pos + {"tempabs;1;d2b;;°C;Aussentemperatur","","", "", "p"},// predefined type with absolute pos + {"strucrelrel;;d2b;;;;y;;d1c","","", "", "p"}, // predefined combined type with relative pos + {"strucrelabs;;d2b;;;;y;1;d1c","","", "", "p"}, // predefined combined type with relative pos + {"strucabsrel;1;d2b;;;;y;;d1c","","", "", "p"}, // predefined combined type with relative pos + {"strucabsabs;2;d2b;;;;y;1;d1c","","", "", "p"}, // predefined combined type with absolute pos + {"x;;temprel","18.004","10fe0700090112", "00", ""}, // reference predefined type + {"strucrelrel;;temprel,temprel","","", "", "p"}, // predefined combined type with relative pos + {"strucrelabs;;temprel,tempabs","","", "", "p"}, // predefined combined type with relative pos + {"strucabsrel;1;tempabs,temprel","","", "", "p"}, // predefined combined type with relative pos + {"strucabs;2;tempabs","","", "", "p"}, // predefined combined type with absolute pos }; - std::map predefined; + std::map templates; std::vector fields; - unsigned char nextPos; for (size_t i = 0; i < sizeof(checks)/sizeof(checks[0]); i++) { - std::istringstream isstr(checks[i][0]); - std::string expectStr = checks[i][1]; - SymbolString mstr = SymbolString(checks[i][2], false); - SymbolString sstr = SymbolString(checks[i][3], false); + std::string check[5] = checks[i]; + std::istringstream isstr(check[0]); + std::string expectStr = check[1]; + SymbolString mstr = SymbolString(check[2], false); + SymbolString sstr = SymbolString(check[3], false); + std::string flags = check[4]; + bool isSet = flags.find('s') != std::string::npos; + bool failedRead = flags.find('r') != std::string::npos; + bool failedReadMatch = flags.find('R') != std::string::npos; + bool failedWrite = flags.find('w') != std::string::npos; + bool failedWriteMatch = flags.find('W') != std::string::npos; + bool verbose = flags.find('v') != std::string::npos; + bool combinedValue = flags.find('c') != std::string::npos; + bool isPredefine = flags.find('p') != std::string::npos; std::string item; std::vector entries; + while (fields.empty() == false) { + delete fields.back(); + fields.pop_back(); + } while (std::getline(isstr, item, ';') != 0) entries.push_back(item); std::vector::iterator it = entries.begin(); - nextPos = 0; - result_t result = DataField::create(mstr[1], false, it, entries.end(), predefined, fields, nextPos); + result_t result; + do { + result = DataField::create(it, entries.end(), templates, fields, isSet, isPredefine ? SYN : mstr[1]); + } while (result == RESULT_OK && it != entries.end()); if (result != RESULT_OK) { - std::cout << "create \"" << checks[i][0] << "\" failed: " << getResultCodeCStr(result) << std::endl; + std::cout << "\"" << check[0] << "\": create error: " << getResultCodeCStr(result) << std::endl; continue; } if (fields.empty() == true) { - std::cout << "create \"" << checks[i][0] << "\" failed: empty" << std::endl; + std::cout << "\"" << check[0] << "\": create error: empty" << std::endl; continue; } - std::cout << "create \"" << checks[i][0] << "\" successful" << std::endl; - - DataField* field = fields.back(); - fields.pop_back(); - std::ostringstream output; - result = field->read(mstr, sstr, output); - if (result != RESULT_OK) { - std::cout << "read \"" << checks[i][0] << "\" failed: " << getResultCodeCStr(result) << std::endl; - } - else { - std::string gotStr = output.str(); - - if (strcasecmp(gotStr.c_str(), expectStr.c_str()) == 0) - std::cout << "read successful: " << gotStr << std::endl; - else - std::cout << "read invalid: got " << gotStr - << ", expected " << expectStr << std::endl; - - SymbolString writeMstr = SymbolString(mstr.getDataStr().substr(0, 10), false); - SymbolString writeSstr = SymbolString(sstr.getDataStr().substr(0, 2), false); - - if (result != RESULT_OK) { - std::cout << "create \"" << checks[i][0] << "\" failed: " << getResultCodeCStr(result) << std::endl; - return 1; - } - result = field->write(gotStr, writeMstr, writeSstr); - if (result != RESULT_OK) { - std::cout << "write \"" << checks[i][0] << "\" failed: " << getResultCodeCStr(result) << std::endl; - } - else { - if (mstr == writeMstr && sstr == writeSstr) - std::cout << "write successful" << std::endl; + std::cout << "\"" << check[0] << "\": create OK" << std::endl; + if (isPredefine) { + // store new template + while (fields.empty() == false) { + DataField* field = fields.front(); + fields.erase(fields.begin()); + std::map::iterator current = templates.find(field->getName()); + if (current == templates.end()) + templates[field->getName()] = field; else { - std::cout << "write invalid: "; - if (mstr == writeMstr) - std::cout << "master OK"; - else - std::cout << "master got " << writeMstr.getDataStr() << ", expected " << mstr.getDataStr(); - - if (sstr == writeSstr) - std::cout << ", slave OK"; - else - std::cout << ", slave got " << writeSstr.getDataStr() << ", expected " << sstr.getDataStr(); - std::cout << std::endl; + delete current->second; + current->second = field; } } + continue; } - delete field; + + std::ostringstream output; + std::istringstream input(expectStr); + SymbolString writeMstr = SymbolString(mstr.getDataStr().substr(0, 10), false); + SymbolString writeSstr = SymbolString(sstr.getDataStr().substr(0, 2), false); + bool first = true, failed = false; while (fields.empty() == false) { - field = fields.back(); - fields.pop_back(); - delete field; // TODO use me + DataField* field = fields.front(); + fields.erase(fields.begin()); + if (first == false) + output << ";"; + + result = field->read(mstr, sstr, output, verbose); + if (failedRead == true) + if (result == RESULT_OK) + std::cout << " failed read " << field->getName() << " >" << check[2] << "< error: unexpectedly succeeded" << std::endl; + else + std::cout << " failed read " << field->getName() << " >" << check[2] << "< OK" << std::endl; + else if (result != RESULT_OK) { + std::cout << " read " << field->getName() << " >" << check[2] << "< error: " << getResultCodeCStr(result) << std::endl; + failed = true; + } + else if (combinedValue == false) { + bool match = strcasecmp(output.str().c_str(), expectStr.c_str()) == 0; + verify(failedReadMatch, "read", check[2], match, expectStr, output.str()); + } + + if (verbose == false) { + std::string token; + std::getline(input, token, ';'); + std::istringstream tokeninput(token); + + result = field->write(tokeninput, writeMstr, writeSstr); + if (failedWrite == true) { + if (result == RESULT_OK) + std::cout << " failed write " << field->getName() << " >" << expectStr << "< error: unexpectedly succeeded" << std::endl; + else + std::cout << " failed write " << field->getName() << " >" << expectStr << "< OK" << std::endl; + } + else if (result != RESULT_OK) { + std::cout << " write " << field->getName() << " >" << expectStr << "< error: " << getResultCodeCStr(result) << std::endl; + failed = true; + } + } + first = false; + } + + if (combinedValue == true && failedRead == false && failed == false) { + bool match = strcasecmp(output.str().c_str(), expectStr.c_str()) == 0; + verify(failedReadMatch, "read", check[2], match, expectStr, output.str()); + } + if (verbose == false && failedWrite == false && failed == false) { + bool match = mstr == writeMstr && sstr == writeSstr; + verify(failedWriteMatch, "write", expectStr, match, mstr.getDataStr() + " " + sstr.getDataStr(), writeMstr.getDataStr() + " " + writeSstr.getDataStr()); } } + + while (fields.empty() == false) { + delete fields.back(); + fields.pop_back(); + } + for (std::map::iterator it = templates.begin(); it!=templates.end(); it++) + delete it->second; + return 0; } From f074ff35a48a8f9d14076ed8df8100ade9e43b5b Mon Sep 17 00:00:00 2001 From: john30 Date: Sun, 16 Nov 2014 10:52:19 +0100 Subject: [PATCH 2/6] added getters, compiler warnings --- src/lib/ebus/data.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/lib/ebus/data.cpp b/src/lib/ebus/data.cpp index b63245b0..a36ede2a 100644 --- a/src/lib/ebus/data.cpp +++ b/src/lib/ebus/data.cpp @@ -239,8 +239,6 @@ result_t DataField::create(std::vector::iterator& it, const std::ve } found = true; DataField* templ = ref->second; - dataType_t dataType = templ->m_dataType; - unsigned char numBytes = (dataType.numBits+7)/8; if (length != 0 && length != templ->m_length) return RESULT_ERR_INVALID_ARG; // different length not possible for derivation unsigned char deriveOffset = offset == 0 ? templ->m_offset : offset; From 73534d2b451b62d07f4867737fbcd13d97dfa792 Mon Sep 17 00:00:00 2001 From: john30 Date: Sun, 16 Nov 2014 10:52:45 +0100 Subject: [PATCH 3/6] added getters --- src/lib/ebus/data.h | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/lib/ebus/data.h b/src/lib/ebus/data.h index f156fd80..a5c690bd 100644 --- a/src/lib/ebus/data.h +++ b/src/lib/ebus/data.h @@ -136,7 +136,19 @@ public: * @return the field name. */ const std::string getName() { return m_name; } - //TODO add getter for all fields + + /** + * @brief Get the value unit. + * @return the value unit. + */ + const std::string getUnit() { return m_unit; } + + /** + * @brief Get the field comment. + * @return the field comment. + */ + const std::string getComment() { return m_comment; } + protected: virtual DataField* derive(std::string name, PartType partType, unsigned char offset, std::string unit, std::string comment, unsigned int divisor, std::map values) = 0; From 264ba1ca518cde84394a79262db57c86f3de98a6 Mon Sep 17 00:00:00 2001 From: john30 Date: Sun, 16 Nov 2014 11:07:46 +0100 Subject: [PATCH 4/6] formatting --- src/lib/ebus/data.cpp | 234 +++++++++++++++++--------------- src/lib/ebus/test/test_data.cpp | 42 ++++-- 2 files changed, 155 insertions(+), 121 deletions(-) diff --git a/src/lib/ebus/data.cpp b/src/lib/ebus/data.cpp index a36ede2a..3490c9e4 100644 --- a/src/lib/ebus/data.cpp +++ b/src/lib/ebus/data.cpp @@ -94,6 +94,7 @@ static const dataType_t dataTypes[] = { {"B17", 7, bt_num, LST, 0, 0, 0x7f, 1, 1}, // seven bits 1-7 }; + /** the week day names. */ static const char* dayNames[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}; @@ -101,9 +102,12 @@ static const char* dayNames[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" #define VALUE_SEPARATOR ',' #define NULL_VALUE "-" -result_t DataField::create(std::vector::iterator& it, const std::vector::iterator end, - const std::map templates, std::vector& fields, - const bool isSetMessage, const unsigned char dstAddress) { +result_t DataField::create(std::vector::iterator& it, + const std::vector::iterator end, + const std::map templates, + std::vector& fields, const bool isSetMessage, + const unsigned char dstAddress) +{ std::string unit, comment; PartType partType; unsigned int divisor = 0; @@ -123,19 +127,22 @@ result_t DataField::create(std::vector::iterator& it, const std::ve return RESULT_ERR_EOF; if (dstAddress == BROADCAST || isMaster(dstAddress) - || (isTemplate == false && isSetMessage == true && posStr[0] != 0 && posStr[0] <= '9') - || posStr[0] == 'm') { // master data + || (isTemplate == false && isSetMessage == true && posStr[0] != 0 && posStr[0] <= '9') + || posStr[0] == 'm') { // master data partType = pt_masterData; if (posStr[0] == 'm') posStr++; - } else if ((isTemplate == false && isSetMessage == false && posStr[0] != 0 && posStr[0] <= '9') - || posStr[0] == 's') { // slave data + } + else if ((isTemplate == false && isSetMessage == false && posStr[0] != 0 && posStr[0] <= '9') + || posStr[0] == 's') { // slave data partType = pt_slaveData; if (posStr[0] == 's') posStr++; - } else if (isTemplate) { + } + else if (isTemplate) { partType = pt_template; - } else + } + else return RESULT_ERR_INVALID_ARG; bool hasPrev = fields.empty() == false; @@ -143,7 +150,7 @@ result_t DataField::create(std::vector::iterator& it, const std::ve DataField* previous = fields.back(); offset = previous->m_offset + previous->m_length; if ((previous->m_dataType.numBits % 8) != 0 - && previous->m_dataType.precisionOrFirstBit + (previous->m_dataType.numBits % 8) < 8) + && previous->m_dataType.precisionOrFirstBit + (previous->m_dataType.numBits % 8) < 8) offset--; // previous bits not yet fully consumed } else @@ -151,7 +158,8 @@ result_t DataField::create(std::vector::iterator& it, const std::ve if (posStr[0] == 0) { length = 0; - } else { + } + else { offset = 0; length = 0; std::istringstream stream(posStr); @@ -161,19 +169,19 @@ result_t DataField::create(std::vector::iterator& it, const std::ve const char* start = token.c_str(); char* end = NULL; - unsigned int pos = strtoul(start, &end, 10)-1; // 1-based - if (end != start+strlen(start)) + unsigned int pos = strtoul(start, &end, 10) - 1; // 1-based + if (end != start + strlen(start)) return RESULT_ERR_INVALID_ARG; // invalid pos definition if (pos > maxPos) // TODO check this in real offset as well return RESULT_ERR_INVALID_ARG; // invalid pos definition - if (offsetCnt==1) + if (offsetCnt == 1) offset = pos; else if (pos >= offset) - length = pos+1-offset; + length = pos + 1 - offset; else { // wrong order e.g. 4-3 - length = offset-(pos+1); + length = offset - (pos + 1); offset = pos; } } @@ -191,7 +199,7 @@ result_t DataField::create(std::vector::iterator& it, const std::ve const char* start = divisorStr.c_str(); char* end = NULL; divisor = strtoul(start, &end, 10); - if (end != start+strlen(start)) + if (end != start + strlen(start)) return RESULT_ERR_INVALID_ARG; } else { @@ -203,7 +211,7 @@ result_t DataField::create(std::vector::iterator& it, const std::ve if (end == NULL || end == start || *end != '=') return RESULT_ERR_INVALID_ARG; - values[id] = std::string(end+1); + values[id] = std::string(end + 1); } } } @@ -253,12 +261,12 @@ result_t DataField::create(std::vector::iterator& it, const std::ve return RESULT_OK; } - for (size_t i = 0; i < sizeof(dataTypes)/sizeof(dataTypes[0]); i++) { + for (size_t i = 0; i < sizeof(dataTypes) / sizeof(dataTypes[0]); i++) { dataType_t dataType = dataTypes[i]; if (strcasecmp(typeStr, dataType.name) == 0) { - unsigned char numBytes = (dataType.numBits+7)/8; + unsigned char numBytes = (dataType.numBits + 7) / 8; unsigned char useLength = length; - if ((dataType.flags&ADJ) != 0) { + if ((dataType.flags & ADJ) != 0) { if (useLength == 0) useLength = 1; // minimum length defaults to 1 else if (useLength > numBytes) @@ -269,22 +277,24 @@ result_t DataField::create(std::vector::iterator& it, const std::ve else if (useLength != numBytes) continue; // check for another one with same name but different length - switch (dataType.type) { + switch (dataType.type) + { case bt_str: case bt_hexstr: case bt_dat: case bt_tim: - fields.push_back(new StringDataField(name, partType, offset, useLength, dataType, unit, comment)); + fields.push_back(new StringDataField(name, partType, offset, useLength,dataType, unit, comment)); return RESULT_OK; case bt_num: - if (values.empty() == true && (dataType.flags&DAY) != 0) { - for (unsigned int i=0; i::iterator& it, const std::ve return RESULT_ERR_INVALID_ARG; } -result_t DataField::read(SymbolString& masterData, SymbolString& slaveData, std::ostringstream& output, bool verbose) +result_t DataField::read(SymbolString& masterData, SymbolString& slaveData, + std::ostringstream& output, bool verbose) { SymbolString& input = m_partType == pt_masterData ? masterData : slaveData; unsigned char baseOffset; - switch (m_partType) { + switch (m_partType) + { case pt_masterData: baseOffset = 5; // skip QQ ZZ PB SB NN break; @@ -330,11 +342,13 @@ result_t DataField::read(SymbolString& masterData, SymbolString& slaveData, std: return RESULT_OK; } -result_t DataField::write(std::istringstream& input, SymbolString& masterData, SymbolString& slaveData) +result_t DataField::write(std::istringstream& input, SymbolString& masterData, + SymbolString& slaveData) { SymbolString& output = m_partType == pt_masterData ? masterData : slaveData; unsigned char baseOffset; - switch (m_partType) { + switch (m_partType) + { case pt_masterData: baseOffset = 5; // skip QQ ZZ PB SB NN break; @@ -347,19 +361,21 @@ result_t DataField::write(std::istringstream& input, SymbolString& masterData, S return writeSymbols(input, baseOffset, output); } - - -DataField* StringDataField::derive(std::string name, PartType partType, unsigned char offset, std::string unit, - std::string comment, unsigned int divisor, std::map values) { +DataField* StringDataField::derive(std::string name, PartType partType, + unsigned char offset, std::string unit, std::string comment, + unsigned int divisor, std::map values) +{ if (unit.empty() == true) unit = m_unit; if (comment.empty() == true) comment = m_comment; - return new StringDataField(name, partType, offset, m_length, m_dataType, unit, comment); + return new StringDataField(name, partType, offset, m_length, m_dataType, + unit, comment); } -result_t StringDataField::readSymbols(SymbolString& input, unsigned char baseOffset, std::ostringstream& output) +result_t StringDataField::readSymbols(SymbolString& input, + unsigned char baseOffset, std::ostringstream& output) { size_t start = m_offset, end = m_offset + m_length; int incr = 1; @@ -368,7 +384,7 @@ result_t StringDataField::readSymbols(SymbolString& input, unsigned char baseOff if (baseOffset + end > input.size()) return RESULT_ERR_INVALID_ARG; - if ((m_dataType.flags&REV) != 0) { // reverted binary representation (most significant byte first) + if ((m_dataType.flags & REV) != 0) { // reverted binary representation (most significant byte first) end = start - 1; start = m_offset + m_length - 1; incr = -1; @@ -383,7 +399,8 @@ result_t StringDataField::readSymbols(SymbolString& input, unsigned char baseOff return RESULT_ERR_INVALID_ARG; // invalid BCD ch = (ch >> 4) * 10 + (ch & 0x0f); } - switch (m_dataType.type) { + switch (m_dataType.type) + { case bt_hexstr: if (i > 0) output << ' '; @@ -392,7 +409,7 @@ result_t StringDataField::readSymbols(SymbolString& input, unsigned char baseOff break; case bt_dat: if (i + 1 == m_length) - output << (2000+ch); + output << (2000 + ch); else if (ch < 1 || (i == 0 && ch > 31) || (i == 1 && ch > 12)) return RESULT_ERR_INVALID_ARG; // invalid date else @@ -405,7 +422,7 @@ result_t StringDataField::readSymbols(SymbolString& input, unsigned char baseOff offset -= incr; // repeat for minutes } else - ch = (ch%6) * 10; // minutes + ch = (ch % 6) * 10; // minutes } if ((i == 0 && ch > 23) || (i > 0 && ch > 59)) return RESULT_ERR_INVALID_ARG; // invalid time @@ -424,7 +441,8 @@ result_t StringDataField::readSymbols(SymbolString& input, unsigned char baseOff return RESULT_OK; } -result_t StringDataField::writeSymbols(std::istringstream& input, unsigned char baseOffset, SymbolString& output) +result_t StringDataField::writeSymbols(std::istringstream& input, + unsigned char baseOffset, SymbolString& output) { size_t start = m_offset, end = m_offset + m_length; int incr = 1; @@ -433,7 +451,7 @@ result_t StringDataField::writeSymbols(std::istringstream& input, unsigned char unsigned long int value = 0, hours = 0; std::string token; - if ((m_dataType.flags&REV) != 0) { // reverted binary representation (most significant byte first) + if ((m_dataType.flags & REV) != 0) { // reverted binary representation (most significant byte first) end = start - 1; start = m_offset + m_length - 1; incr = -1; @@ -441,9 +459,10 @@ result_t StringDataField::writeSymbols(std::istringstream& input, unsigned char size_t i = 0; for (size_t offset = start; offset != end; offset += incr, i++) { - switch (m_dataType.type) { + switch (m_dataType.type) + { case bt_hexstr: - while (input.peek()==' ') + while (input.peek() == ' ') input.get(); if (input.eof() == true) // no more digits value = m_dataType.replacement; // fill up with replacement @@ -459,7 +478,7 @@ result_t StringDataField::writeSymbols(std::istringstream& input, unsigned char str = token.c_str(); strEnd = NULL; value = strtoul(str, &strEnd, 16); - if (strEnd != str+strlen(str)) + if (strEnd != str + strlen(str)) return RESULT_ERR_INVALID_ARG; // invalid hex value } break; @@ -471,7 +490,7 @@ result_t StringDataField::writeSymbols(std::istringstream& input, unsigned char str = token.c_str(); strEnd = NULL; value = strtoul(str, &strEnd, 10); - if (strEnd != str+strlen(str)) + if (strEnd != str + strlen(str)) return RESULT_ERR_INVALID_ARG; // invalid date part if (i + 1 == m_length && value >= 2000) value -= 2000; @@ -484,7 +503,7 @@ result_t StringDataField::writeSymbols(std::istringstream& input, unsigned char str = token.c_str(); strEnd = NULL; value = strtoul(str, &strEnd, 10); - if (strEnd != str+strlen(str)) + if (strEnd != str + strlen(str)) return RESULT_ERR_INVALID_ARG; // invalid time part if ((i == 0 && value > 23) || (i > 0 && value > 59)) return RESULT_ERR_INVALID_ARG; // invalid time part @@ -496,8 +515,8 @@ result_t StringDataField::writeSymbols(std::istringstream& input, unsigned char } if ((value % 10) != 0) return RESULT_ERR_INVALID_ARG; // invalid truncated time minutes - value = hours*6 + (value / 10); - if (value > 24*6) + value = hours * 6 + (value / 10); + if (value > 24 * 6) return RESULT_ERR_INVALID_ARG; // invalid time } break; @@ -510,11 +529,11 @@ result_t StringDataField::writeSymbols(std::istringstream& input, unsigned char if ((m_dataType.flags & BCD) != 0) { if (value > 99) return RESULT_ERR_INVALID_ARG; // invalid BCD - value = (value/10)<<4 | (value%10); + value = (value / 10) << 4 | (value % 10); } if (value > 0xff) return RESULT_ERR_INVALID_ARG; // value out of range - output[baseOffset + offset] = (unsigned char)value; + output[baseOffset + offset] = (unsigned char) value; } if (i < m_length) @@ -523,8 +542,8 @@ result_t StringDataField::writeSymbols(std::istringstream& input, unsigned char return RESULT_OK; } - -result_t NumericDataField::readRawValue(SymbolString& input, unsigned char baseOffset, unsigned int& value) +result_t NumericDataField::readRawValue(SymbolString& input, + unsigned char baseOffset, unsigned int& value) { size_t start = m_offset, end = m_offset + m_length; int incr = 1; @@ -533,7 +552,7 @@ result_t NumericDataField::readRawValue(SymbolString& input, unsigned char baseO if (baseOffset + end > input.size()) return RESULT_ERR_INVALID_ARG; // not enough data available - if ((m_dataType.flags&REV) != 0) { // reverted binary representation (most significant byte first) + if ((m_dataType.flags & REV) != 0) { // reverted binary representation (most significant byte first) end = start - 1; start = m_offset + m_length - 1; incr = -1; @@ -551,38 +570,39 @@ result_t NumericDataField::readRawValue(SymbolString& input, unsigned char baseO return RESULT_ERR_INVALID_ARG; // invalid BCD ch = (ch >> 4) * 10 + (ch & 0x0f); - value += ch*exp; + value += ch * exp; exp *= 100; } else { - value |= ch*exp; + value |= ch * exp; exp <<= 8; } } if ((m_dataType.flags & BCD) == 0) { value >>= m_bitOffset; - if ((m_dataType.numBits%8) != 0) - value &= (1< values) { +DataField* NumberDataField::derive(std::string name, PartType partType, + unsigned char offset, std::string unit, std::string comment, + unsigned int divisor, std::map values) +{ if (unit.empty() == true) unit = m_unit; if (comment.empty() == true) @@ -623,7 +644,8 @@ DataField* NumberDataField::derive(std::string name, PartType partType, unsigned return new NumberDataField(name, partType, offset, m_length, m_dataType, unit, comment, divisor); } -result_t NumberDataField::readSymbols(SymbolString& input, unsigned char baseOffset, std::ostringstream& output) +result_t NumberDataField::readSymbols(SymbolString& input, + unsigned char baseOffset, std::ostringstream& output) { unsigned int value = 0; int signedValue; @@ -636,34 +658,34 @@ result_t NumberDataField::readSymbols(SymbolString& input, unsigned char baseOff output << NULL_VALUE; return RESULT_OK; } - bool negative = (m_dataType.flags&SIG) != 0 && (value & (1 << (m_dataType.numBits - 1))) != 0; + bool negative = (m_dataType.flags & SIG) != 0 && (value & (1 << (m_dataType.numBits - 1))) != 0; if (m_dataType.numBits == 32) { if (negative == false) { if (m_divisor <= 1) output << static_cast(value); else - output << std::setprecision((m_dataType.numBits%8)==0 ? m_dataType.precisionOrFirstBit : 0) - << std::fixed << static_cast(value / (float)m_divisor); + output << std::setprecision((m_dataType.numBits % 8) == 0 ? m_dataType.precisionOrFirstBit : 0) + << std::fixed << static_cast(value / (float) m_divisor); return RESULT_OK; } - signedValue = (int)value; // negative signed value + signedValue = (int) value; // negative signed value } + else if (negative) // negative signed value + signedValue = (int) value - (1 << m_dataType.numBits); else - if (negative) // negative signed value - signedValue = (int)value - (1 << m_dataType.numBits); - else - signedValue = (int)value; + signedValue = (int) value; if (m_divisor <= 1) output << static_cast(signedValue); else - output << std::setprecision((m_dataType.numBits%8)==0 ? m_dataType.precisionOrFirstBit : 0) - << std::fixed << static_cast(signedValue / (float)m_divisor); + output << std::setprecision((m_dataType.numBits % 8) == 0 ? m_dataType.precisionOrFirstBit : 0) + << std::fixed << static_cast(signedValue / (float) m_divisor); return RESULT_OK; } -result_t NumberDataField::writeSymbols(std::istringstream& input, unsigned char baseOffset, SymbolString& output) +result_t NumberDataField::writeSymbols(std::istringstream& input, + unsigned char baseOffset, SymbolString& output) { unsigned int value; @@ -677,58 +699,58 @@ result_t NumberDataField::writeSymbols(std::istringstream& input, unsigned char else { char* strEnd = NULL; if (m_divisor <= 1) { - if ((m_dataType.flags&SIG) != 0) { + if ((m_dataType.flags & SIG) != 0) { int signedValue = strtol(str, &strEnd, 10); if (signedValue < 0 && m_dataType.numBits != 32) - value = (unsigned int)(signedValue + (1<= (1LL<<(8*m_length))) + if ((m_dataType.flags & SIG) != 0) { + if (dvalue < -(1LL << (8 * m_length)) || dvalue >= (1LL << (8 * m_length))) return RESULT_ERR_INVALID_ARG; // value out of range if (dvalue < 0 && m_dataType.numBits != 32) - value = (unsigned int)(dvalue + (1<= (1LL<<(8*m_length))) + if (dvalue < 0.0 || dvalue >= (1LL << (8 * m_length))) return RESULT_ERR_INVALID_ARG; // value out of range - value = (unsigned int)dvalue; + value = (unsigned int) dvalue; } } - if ((m_dataType.flags&SIG) != 0) { // signed value + if ((m_dataType.flags & SIG) != 0) { // signed value if ((value & (1 << (m_dataType.numBits - 1))) != 0) { // negative signed value if (value < m_dataType.minValueOrLength) - return RESULT_ERR_INVALID_ARG; // value out of range + return RESULT_ERR_INVALID_ARG; // value out of range } else if (value > m_dataType.maxValueOrLength) - return RESULT_ERR_INVALID_ARG; // value out of range + return RESULT_ERR_INVALID_ARG; // value out of range } else if (value < m_dataType.minValueOrLength || value > m_dataType.maxValueOrLength) return RESULT_ERR_INVALID_ARG; // value out of range } - return writeRawValue(value, baseOffset, output); } - -DataField* ValueListDataField::derive(std::string name, PartType partType, unsigned char offset, std::string unit, - std::string comment, unsigned int divisor, std::map values) { +DataField* ValueListDataField::derive(std::string name, PartType partType, + unsigned char offset, std::string unit, std::string comment, + unsigned int divisor, std::map values) +{ if (unit.empty() == true) unit = m_unit; if (comment.empty() == true) @@ -744,7 +766,8 @@ DataField* ValueListDataField::derive(std::string name, PartType partType, unsig return new ValueListDataField(name, partType, offset, m_length, m_dataType, unit, comment, values); } -result_t ValueListDataField::readSymbols(SymbolString& input, unsigned char baseOffset, std::ostringstream& output) +result_t ValueListDataField::readSymbols(SymbolString& input, + unsigned char baseOffset, std::ostringstream& output) { unsigned int value = 0; @@ -766,7 +789,8 @@ result_t ValueListDataField::readSymbols(SymbolString& input, unsigned char base return RESULT_ERR_INVALID_ARG; // value assignment not found } -result_t ValueListDataField::writeSymbols(std::istringstream& input, unsigned char baseOffset, SymbolString& output) +result_t ValueListDataField::writeSymbols(std::istringstream& input, + unsigned char baseOffset, SymbolString& output) { const char* str = input.str().c_str(); @@ -780,6 +804,4 @@ result_t ValueListDataField::writeSymbols(std::istringstream& input, unsigned ch return RESULT_ERR_INVALID_ARG; // value assignment not found } - } //namespace - diff --git a/src/lib/ebus/test/test_data.cpp b/src/lib/ebus/test/test_data.cpp index e4ebe4a8..d96a9562 100644 --- a/src/lib/ebus/test/test_data.cpp +++ b/src/lib/ebus/test/test_data.cpp @@ -23,20 +23,25 @@ using namespace libebus; -void verify(bool expectFailMatch, std::string type, std::string input, bool match, std::string expectStr, std::string gotStr) { +void verify(bool expectFailMatch, std::string type, std::string input, + bool match, std::string expectStr, std::string gotStr) +{ if (expectFailMatch == true) { if (match == true) - std::cout << " failed " << type << " match >" << input << "< error: unexpectedly succeeded" << std::endl; + std::cout << " failed " << type << " match >" << input + << "< error: unexpectedly succeeded" << std::endl; else - std::cout << " failed " << type << " match >" << input << "< OK" << std::endl; - } else if (match == true) + std::cout << " failed " << type << " match >" << input << "< OK" + << std::endl; + } + else if (match == true) std::cout << " " << type << " >" << input << "< OK" << std::endl; else std::cout << " " << type << " >" << input << "< error: got >" << gotStr - << "<, expected >" << expectStr << "<" << std::endl; + << "<, expected >" << expectStr << "<" << std::endl; } -int main () +int main() { std::string checks[][5] = { //name;[pos];type[;[divisor|values][;[unit][;[comment]]]], decoded value, master, slave, flags @@ -167,7 +172,7 @@ int main () }; std::map templates; std::vector fields; - for (size_t i = 0; i < sizeof(checks)/sizeof(checks[0]); i++) { + for (size_t i = 0; i < sizeof(checks) / sizeof(checks[0]); i++) { std::string check[5] = checks[i]; std::istringstream isstr(check[0]); std::string expectStr = check[1]; @@ -199,7 +204,8 @@ int main () } while (result == RESULT_OK && it != entries.end()); if (result != RESULT_OK) { - std::cout << "\"" << check[0] << "\": create error: " << getResultCodeCStr(result) << std::endl; + std::cout << "\"" << check[0] << "\": create error: " + << getResultCodeCStr(result) << std::endl; continue; } if (fields.empty() == true) { @@ -237,11 +243,14 @@ int main () result = field->read(mstr, sstr, output, verbose); if (failedRead == true) if (result == RESULT_OK) - std::cout << " failed read " << field->getName() << " >" << check[2] << "< error: unexpectedly succeeded" << std::endl; + std::cout << " failed read " << field->getName() << " >" + << check[2] << "< error: unexpectedly succeeded" << std::endl; else - std::cout << " failed read " << field->getName() << " >" << check[2] << "< OK" << std::endl; + std::cout << " failed read " << field->getName() << " >" + << check[2] << "< OK" << std::endl; else if (result != RESULT_OK) { - std::cout << " read " << field->getName() << " >" << check[2] << "< error: " << getResultCodeCStr(result) << std::endl; + std::cout << " read " << field->getName() << " >" << check[2] << "< error: " + << getResultCodeCStr(result) << std::endl; failed = true; } else if (combinedValue == false) { @@ -257,12 +266,15 @@ int main () result = field->write(tokeninput, writeMstr, writeSstr); if (failedWrite == true) { if (result == RESULT_OK) - std::cout << " failed write " << field->getName() << " >" << expectStr << "< error: unexpectedly succeeded" << std::endl; + std::cout << " failed write " << field->getName() << " >" + << expectStr << "< error: unexpectedly succeeded" << std::endl; else - std::cout << " failed write " << field->getName() << " >" << expectStr << "< OK" << std::endl; + std::cout << " failed write " << field->getName() << " >" + << expectStr << "< OK" << std::endl; } else if (result != RESULT_OK) { - std::cout << " write " << field->getName() << " >" << expectStr << "< error: " << getResultCodeCStr(result) << std::endl; + std::cout << " write " << field->getName() << " >" + << expectStr << "< error: " << getResultCodeCStr(result) << std::endl; failed = true; } } @@ -283,7 +295,7 @@ int main () delete fields.back(); fields.pop_back(); } - for (std::map::iterator it = templates.begin(); it!=templates.end(); it++) + for (std::map::iterator it = templates.begin(); it != templates.end(); it++) delete it->second; return 0; From 5f922f8970922ba67c6ad7729c39ebe4381f8fdd Mon Sep 17 00:00:00 2001 From: john30 Date: Thu, 20 Nov 2014 09:23:54 +0100 Subject: [PATCH 5/6] added support for templates and sets of DataFields, simplified DataField creation, added missing bounds checks --- src/lib/ebus/data.cpp | 626 ++++++++++++++++++++------------ src/lib/ebus/data.h | 322 +++++++++++----- src/lib/ebus/test/test_data.cpp | 182 +++++----- 3 files changed, 724 insertions(+), 406 deletions(-) diff --git a/src/lib/ebus/data.cpp b/src/lib/ebus/data.cpp index 3490c9e4..6fe7f592 100644 --- a/src/lib/ebus/data.cpp +++ b/src/lib/ebus/data.cpp @@ -42,7 +42,7 @@ static const dataType_t dataTypes[] = { {"HDA", 24, bt_dat, 0, 0, 10, 10, 0, 0}, // date, 01.01.2000 - 31.12.2099 (0x01,0x01,0x00 - 0x31,0x12,0x99) // TODO remove duplicate of BDA {"BTI", 24, bt_tim, BCD|REV, 0, 8, 8, 0, 0}, // time in BCD, 00:00:00 - 23:59:59 (0x00,0x00,0x00 - 0x59,0x59,0x23) {"HTM", 16, bt_tim, 0, 0, 5, 5, 0, 0}, // time as hh:mm, 00:00 - 23:59 (0x00,0x00 - 0x17,0x3b) - {"TTM", 8, bt_tim, 0, 0, 5, 5, 0, 0}, // truncated time (only multiple of 10 minutes), 00:00 - 24:00 (minutes div 10 + hour * 6 as integer) + {"TTM", 8, bt_tim, 0, 0x90, 5, 5, 0, 0}, // truncated time (only multiple of 10 minutes), 00:00 - 24:00 (minutes div 10 + hour * 6 as integer) {"BDY", 8, bt_num, DAY|LST, 0x07, 0, 6, 1, 0}, // weekday, "Mon" - "Sun" {"HDY", 8, bt_num, DAY|LST, 0x00, 1, 7, 1, 0}, // weekday, "Mon" - "Sun" {"BCD", 8, bt_num, BCD|LST, 0xff, 0, 0x99, 1, 0}, // unsigned decimal in BCD, 0 - 99 @@ -104,214 +104,264 @@ static const char* dayNames[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" result_t DataField::create(std::vector::iterator& it, const std::vector::iterator end, - const std::map templates, - std::vector& fields, const bool isSetMessage, + const std::map< std::string, DataField*> templates, + DataField*& returnField, const bool isSetMessage, const unsigned char dstAddress) { - std::string unit, comment; - PartType partType; - unsigned int divisor = 0; - unsigned char offset, length, maxPos = 16, offsetCnt = 0; - const bool isTemplate = dstAddress == SYN; - std::string token; - if (it == end) - return RESULT_ERR_EOF; + std::vector fields; + std::string firstName, firstComment; + result_t result = RESULT_OK; + while (it != end && result == RESULT_OK) { + std::string unit, comment; + PartType partType; + unsigned int divisor = 0; + unsigned char offset, length, maxPos = 16, offsetCnt = 0; + const bool isTemplate = dstAddress == SYN; + std::string token; + if (it == end) + break; - // name;[pos];type[;[divisor|values][;[unit][;[comment]]]] - const std::string name = *it++; - if (it == end || name.empty() == true) - return RESULT_ERR_EOF; + // name;[pos];type[;[divisor|values][;[unit][;[comment]]]] + const std::string name = *it++; + if (it == end) + break; - const char* posStr = (*it++).c_str(); - if (it == end) - return RESULT_ERR_EOF; + const char* posStr = (*it++).c_str(); + if (it == end) + break; - if (dstAddress == BROADCAST || isMaster(dstAddress) - || (isTemplate == false && isSetMessage == true && posStr[0] != 0 && posStr[0] <= '9') - || posStr[0] == 'm') { // master data - partType = pt_masterData; - if (posStr[0] == 'm') - posStr++; - } - else if ((isTemplate == false && isSetMessage == false && posStr[0] != 0 && posStr[0] <= '9') - || posStr[0] == 's') { // slave data - partType = pt_slaveData; - if (posStr[0] == 's') - posStr++; - } - else if (isTemplate) { - partType = pt_template; - } - else - return RESULT_ERR_INVALID_ARG; - - bool hasPrev = fields.empty() == false; - if (hasPrev == true) { - DataField* previous = fields.back(); - offset = previous->m_offset + previous->m_length; - if ((previous->m_dataType.numBits % 8) != 0 - && previous->m_dataType.precisionOrFirstBit + (previous->m_dataType.numBits % 8) < 8) - offset--; // previous bits not yet fully consumed - } - else - offset = 0; - - if (posStr[0] == 0) { - length = 0; - } - else { - offset = 0; - length = 0; - std::istringstream stream(posStr); - while (std::getline(stream, token, '-') != 0) { - if (++offsetCnt > 2) - return RESULT_ERR_INVALID_ARG; //invalid pos definition - - const char* start = token.c_str(); - char* end = NULL; - unsigned int pos = strtoul(start, &end, 10) - 1; // 1-based - if (end != start + strlen(start)) - return RESULT_ERR_INVALID_ARG; // invalid pos definition - - if (pos > maxPos) // TODO check this in real offset as well - return RESULT_ERR_INVALID_ARG; // invalid pos definition - - if (offsetCnt == 1) - offset = pos; - else if (pos >= offset) - length = pos + 1 - offset; - else { // wrong order e.g. 4-3 - length = offset - (pos + 1); - offset = pos; - } + if (fields.empty() == true) { + firstName = name; + firstComment = comment; + } + if (dstAddress == BROADCAST || isMaster(dstAddress) + || (isTemplate == false && isSetMessage == true && posStr[0] != 0 && posStr[0] <= '9') + || posStr[0] == 'm') { // master data + partType = pt_masterData; + if (posStr[0] == 'm') + posStr++; + } + else if ((isTemplate == false && isSetMessage == false && posStr[0] != 0 && posStr[0] <= '9') + || posStr[0] == 's') { // slave data + partType = pt_slaveData; + if (posStr[0] == 's') + posStr++; + } + else if (isTemplate) { + partType = pt_template; + } + else { + result = RESULT_ERR_INVALID_ARG; + break; } - } - const char* typeStr = (*it++).c_str(); - if (typeStr[0] == 0) - return RESULT_ERR_EOF; + if (posStr[0] == 0) { + if (fields.empty() == false) + offset = fields.back()->getNextOffset(); + else + offset = 0; + length = 0; + } + else { + offset = 0; + length = 0; + std::istringstream stream(posStr); + while (std::getline(stream, token, '-') != 0) { + if (++offsetCnt > 2) + return RESULT_ERR_INVALID_ARG; //invalid pos definition - std::map values; - if (it != end) { - std::string divisorStr = *it++; - if (divisorStr.empty() == false) { - if (divisorStr.find_first_not_of("0123456789") == std::string::npos) { - const char* start = divisorStr.c_str(); + const char* start = token.c_str(); char* end = NULL; - divisor = strtoul(start, &end, 10); - if (end != start + strlen(start)) - return RESULT_ERR_INVALID_ARG; + unsigned int pos = strtoul(start, &end, 10) - 1; // 1-based + if (end != start + strlen(start)) { + result = RESULT_ERR_INVALID_ARG; // invalid pos definition + break; + } + + if (pos > maxPos) { // TODO check this in real offset as well + result = RESULT_ERR_INVALID_ARG; // invalid pos definition + break; + } + + if (offsetCnt == 1) + offset = pos; + else if (pos >= offset) + length = pos + 1 - offset; + else { // wrong order e.g. 4-3 + length = offset - (pos + 1); + offset = pos; + } } - else { - std::istringstream stream(divisorStr); - while (std::getline(stream, token, VALUE_SEPARATOR) != 0) { - const char* start = token.c_str(); + if (result != RESULT_OK) + break; + } + + const char* typeStr = (*it++).c_str(); + if (typeStr[0] == 0) { + break; + } + + std::map values; + if (it != end) { + std::string divisorStr = *it++; + if (divisorStr.empty() == false) { + if (divisorStr.find_first_not_of("0123456789") == std::string::npos) { + const char* start = divisorStr.c_str(); char* end = NULL; - unsigned int id = strtoul(start, &end, 10); - if (end == NULL || end == start || *end != '=') - return RESULT_ERR_INVALID_ARG; - - values[id] = std::string(end + 1); - } - } - } - } - - if (it == end) - unit = ""; - else { - unit = *it++; - - if (unit.length() == 1 && unit[0] == '-') - unit.clear(); - } - - if (it == end) - comment = ""; - else { - comment = *it++; - if (comment.length() == 1 && comment[0] == '-') - comment.clear(); - } - - // check for reference(s) to templates - if (templates.empty() == false) { - std::istringstream stream(typeStr); - bool found = false; - while (std::getline(stream, token, VALUE_SEPARATOR) != 0) { - std::map::const_iterator ref = templates.find(token); - if (ref == templates.end()) { - if (found == false) - break; // fallback to direct definition - return RESULT_ERR_INVALID_ARG; // cannot mix reference and direct definition - } - found = true; - DataField* templ = ref->second; - if (length != 0 && length != templ->m_length) - return RESULT_ERR_INVALID_ARG; // different length not possible for derivation - unsigned char deriveOffset = offset == 0 ? templ->m_offset : offset; - if (templ->m_partType != pt_template && partType == pt_template) - return RESULT_ERR_INVALID_ARG; - DataField* field = templ->derive(name, partType, deriveOffset, unit, comment, divisor, values); - if (field == NULL) - return RESULT_ERR_INVALID_ARG; // non-supported type for derivation or invalid argument - fields.push_back(field); - } - if (found == true) - return RESULT_OK; - } - - for (size_t i = 0; i < sizeof(dataTypes) / sizeof(dataTypes[0]); i++) { - dataType_t dataType = dataTypes[i]; - if (strcasecmp(typeStr, dataType.name) == 0) { - unsigned char numBytes = (dataType.numBits + 7) / 8; - unsigned char useLength = length; - if ((dataType.flags & ADJ) != 0) { - if (useLength == 0) - useLength = 1; // minimum length defaults to 1 - else if (useLength > numBytes) - return RESULT_ERR_INVALID_ARG; // invalid length - } - else if (useLength == 0) - useLength = numBytes; - else if (useLength != numBytes) - continue; // check for another one with same name but different length - - switch (dataType.type) - { - case bt_str: - case bt_hexstr: - case bt_dat: - case bt_tim: - fields.push_back(new StringDataField(name, partType, offset, useLength,dataType, unit, comment)); - return RESULT_OK; - case bt_num: - if (values.empty() == true && (dataType.flags & DAY) != 0) { - for (unsigned int i = 0; i < sizeof(dayNames) / sizeof(dayNames[0]); i++) - values[dataType.minValueOrLength + i] = dayNames[i]; - } - if (values.empty() == true || (dataType.flags & LST) == 0) { - if (divisor == 0) { - divisor = dataType.divisor; + divisor = strtoul(start, &end, 10); + if (end != start + strlen(start)) { + result = RESULT_ERR_INVALID_ARG; + break; } - else - divisor *= dataType.divisor; - fields.push_back(new NumberDataField(name, partType, offset, useLength, dataType, unit, comment, divisor)); - return RESULT_OK; } - if (values.begin()->first < dataType.minValueOrLength - || values.rbegin()->first > dataType.maxValueOrLength) - return RESULT_ERR_INVALID_ARG; - fields.push_back(new ValueListDataField(name, partType, offset, useLength, dataType, unit, comment, values)); - return RESULT_OK; + else { + std::istringstream stream(divisorStr); + while (std::getline(stream, token, VALUE_SEPARATOR) != 0) { + const char* start = token.c_str(); + char* end = NULL; + unsigned int id = strtoul(start, &end, 10); + if (end == NULL || end == start || *end != '=') { + result = RESULT_ERR_INVALID_ARG; + break; + } + + values[id] = std::string(end + 1); + } + if (result != RESULT_OK) + break; + } } } + + if (it == end) + unit = ""; + else { + unit = *it++; + + if (unit.length() == 1 && unit[0] == '-') + unit.clear(); + } + + if (it == end) + comment = ""; + else { + comment = *it++; + if (comment.length() == 1 && comment[0] == '-') + comment.clear(); + } + + // check for reference(s) to templates + if (templates.empty() == false) { + std::istringstream stream(typeStr); + bool found = false; + while (std::getline(stream, token, VALUE_SEPARATOR) != 0) { + std::map< std::string, DataField*>::const_iterator ref = templates.find(token); + if (ref == templates.end()) { + if (found == false) + break; // fallback to direct definition + result = RESULT_ERR_INVALID_ARG; // cannot mix reference and direct definition + break; + } + if (length > 1) { + result = RESULT_ERR_INVALID_ARG; // different length not possible for derivation + break; + } + found = true; + result = ref->second->derive(name, comment, unit, partType, offset, divisor, values, fields); + if (result != RESULT_OK) + break; + offset = fields.back()->getNextOffset(); + } + if (found == true || result != RESULT_OK) + break; + } + SingleDataField* add = NULL; + for (size_t i = 0; result == RESULT_OK && add == NULL && i < sizeof(dataTypes) / sizeof(dataTypes[0]); i++) { + dataType_t dataType = dataTypes[i]; + if (strcasecmp(typeStr, dataType.name) == 0) { + unsigned char numBytes = (dataType.numBits + 7) / 8; + unsigned char useLength = length; + if ((dataType.flags & ADJ) != 0) { + if (useLength == 0) + useLength = 1; // minimum length defaults to 1 + else if (useLength > numBytes) { + result = RESULT_ERR_INVALID_ARG; // invalid length + break; + } + } + else if (useLength == 0) + useLength = numBytes; + else if (useLength != numBytes) + continue; // check for another one with same name but different length + + switch (dataType.type) + { + case bt_str: + case bt_hexstr: + case bt_dat: + case bt_tim: + add = new StringDataField(name, comment, unit, dataType, partType, offset, useLength); + break; + case bt_num: + if (values.empty() == true && (dataType.flags & DAY) != 0) { + for (unsigned int i = 0; i < sizeof(dayNames) / sizeof(dayNames[0]); i++) + values[dataType.minValueOrLength + i] = dayNames[i]; + } + if (values.empty() == true || (dataType.flags & LST) == 0) { + if (divisor == 0) { + divisor = dataType.divisor; + } + else + divisor *= dataType.divisor; + + add = new NumberDataField(name, comment, unit, dataType, partType, offset, useLength, divisor); + break; + } + if (values.begin()->first < dataType.minValueOrLength + || values.rbegin()->first > dataType.maxValueOrLength) { + result = RESULT_ERR_INVALID_ARG; + break; + } + + add = new ValueListDataField(name, comment, unit, dataType, partType, offset, useLength, values); + break; + } + } + } + if (add != NULL) + fields.push_back(add); + else if (result == RESULT_OK) + result = RESULT_ERR_INVALID_ARG; // type not found } - return RESULT_ERR_INVALID_ARG; + if (fields.empty() == true || result != RESULT_OK) { + while (fields.empty() == false) { + delete fields.back(); + fields.pop_back(); + } + return result == RESULT_OK ? RESULT_ERR_INVALID_ARG :result; + } + + if (fields.size() == 1) + returnField = fields[0]; + else { + returnField = new DataFieldSet(firstName, firstComment, fields); + } + return RESULT_OK; } -result_t DataField::read(SymbolString& masterData, SymbolString& slaveData, - std::ostringstream& output, bool verbose) + +unsigned char SingleDataField::getNextOffset() +{ + unsigned char offset = m_offset + m_length; + if ((m_dataType.numBits % 8) != 0 + && m_dataType.precisionOrFirstBit + (m_dataType.numBits % 8) < 8) + offset--; // not all bits of last offset fully consumed + + return offset; +} + +result_t SingleDataField::read(SymbolString& masterData, SymbolString& slaveData, + std::ostringstream& output, bool verbose, char separator) { SymbolString& input = m_partType == pt_masterData ? masterData : slaveData; unsigned char baseOffset; @@ -342,8 +392,8 @@ result_t DataField::read(SymbolString& masterData, SymbolString& slaveData, return RESULT_OK; } -result_t DataField::write(std::istringstream& input, SymbolString& masterData, - SymbolString& slaveData) +result_t SingleDataField::write(std::istringstream& input, SymbolString& masterData, + SymbolString& slaveData, char separator) { SymbolString& output = m_partType == pt_masterData ? masterData : slaveData; unsigned char baseOffset; @@ -361,17 +411,27 @@ result_t DataField::write(std::istringstream& input, SymbolString& masterData, return writeSymbols(input, baseOffset, output); } -DataField* StringDataField::derive(std::string name, PartType partType, - unsigned char offset, std::string unit, std::string comment, - unsigned int divisor, std::map values) + +result_t StringDataField::derive(std::string name, std::string comment, + std::string unit, const PartType partType, unsigned char offset, + unsigned int divisor, std::map values, + std::vector& fields) { - if (unit.empty() == true) - unit = m_unit; + if (m_partType != pt_template && partType == pt_template) + return RESULT_ERR_INVALID_ARG; // cannot create a template from a concrete instance + if (values.empty() == false) + return RESULT_ERR_INVALID_ARG; // cannot set values for string field + if (name.empty() == true) + name = m_name; if (comment.empty() == true) comment = m_comment; + if (unit.empty() == true) + unit = m_unit; + offset += m_offset; - return new StringDataField(name, partType, offset, m_length, m_dataType, - unit, comment); + fields.push_back(new StringDataField(name, comment, unit, m_dataType, partType, offset, m_length)); + + return RESULT_OK; } result_t StringDataField::readSymbols(SymbolString& input, @@ -379,11 +439,11 @@ result_t StringDataField::readSymbols(SymbolString& input, { size_t start = m_offset, end = m_offset + m_length; int incr = 1; - unsigned char ch; + unsigned char ch, last = 0; - if (baseOffset + end > input.size()) + if (baseOffset + end > input.size()) { return RESULT_ERR_INVALID_ARG; - + } if ((m_dataType.flags & REV) != 0) { // reverted binary representation (most significant byte first) end = start - 1; start = m_offset + m_length - 1; @@ -424,7 +484,7 @@ result_t StringDataField::readSymbols(SymbolString& input, else ch = (ch % 6) * 10; // minutes } - if ((i == 0 && ch > 23) || (i > 0 && ch > 59)) + if ((i == 0 && ch > 24) || (i > 0 && (ch > 59 || ( last == 24 && ch > 0) ))) return RESULT_ERR_INVALID_ARG; // invalid time if (i > 0) output << ":"; @@ -436,6 +496,7 @@ result_t StringDataField::readSymbols(SymbolString& input, output << static_cast(ch); break; } + last = ch; } return RESULT_OK; @@ -448,7 +509,7 @@ result_t StringDataField::writeSymbols(std::istringstream& input, int incr = 1; const char* str; char* strEnd; - unsigned long int value = 0, hours = 0; + unsigned long int value = 0, last = 0; std::string token; if ((m_dataType.flags & REV) != 0) { // reverted binary representation (most significant byte first) @@ -505,17 +566,17 @@ result_t StringDataField::writeSymbols(std::istringstream& input, value = strtoul(str, &strEnd, 10); if (strEnd != str + strlen(str)) return RESULT_ERR_INVALID_ARG; // invalid time part - if ((i == 0 && value > 23) || (i > 0 && value > 59)) + if ((i == 0 && value > 24) || (i > 0 && (value > 59 || ( last == 24 && value > 0) ))) return RESULT_ERR_INVALID_ARG; // invalid time part if (m_length == 1) { // truncated time if (i == 0) { offset -= incr; // repeat for minutes - hours = value; + last = value; continue; } if ((value % 10) != 0) return RESULT_ERR_INVALID_ARG; // invalid truncated time minutes - value = hours * 6 + (value / 10); + value = last * 6 + (value / 10); if (value > 24 * 6) return RESULT_ERR_INVALID_ARG; // invalid time } @@ -533,7 +594,8 @@ result_t StringDataField::writeSymbols(std::istringstream& input, } if (value > 0xff) return RESULT_ERR_INVALID_ARG; // value out of range - output[baseOffset + offset] = (unsigned char) value; + output[baseOffset + offset] = (unsigned char)value; + last = value; } if (i < m_length) @@ -542,6 +604,7 @@ result_t StringDataField::writeSymbols(std::istringstream& input, return RESULT_OK; } + result_t NumericDataField::readRawValue(SymbolString& input, unsigned char baseOffset, unsigned int& value) { @@ -628,20 +691,35 @@ result_t NumericDataField::writeRawValue(unsigned int value, return RESULT_OK; } -DataField* NumberDataField::derive(std::string name, PartType partType, - unsigned char offset, std::string unit, std::string comment, - unsigned int divisor, std::map values) + +result_t NumberDataField::derive(std::string name, std::string comment, + std::string unit, const PartType partType, unsigned char offset, + unsigned int divisor, std::map values, + std::vector& fields) { - if (unit.empty() == true) - unit = m_unit; + if (m_partType != pt_template && partType == pt_template) + return RESULT_ERR_INVALID_ARG; // cannot create a template from a concrete instance + if (name.empty() == true) + name = m_name; if (comment.empty() == true) comment = m_comment; + if (unit.empty() == true) + unit = m_unit; + offset += m_offset; if (divisor == 0) divisor = m_divisor; else divisor *= m_dataType.divisor; + if (values.empty() == false) { + if (divisor != 1) + return RESULT_ERR_INVALID_ARG; // cannot use divisor != 1 for value list field - return new NumberDataField(name, partType, offset, m_length, m_dataType, unit, comment, divisor); + fields.push_back(new ValueListDataField(name, comment, unit, m_dataType, partType, offset, m_length, values)); + } + else + fields.push_back(new NumberDataField(name, comment, unit, m_dataType, partType, offset, m_length, divisor)); + + return RESULT_OK; } result_t NumberDataField::readSymbols(SymbolString& input, @@ -658,6 +736,7 @@ result_t NumberDataField::readSymbols(SymbolString& input, output << NULL_VALUE; return RESULT_OK; } + bool negative = (m_dataType.flags & SIG) != 0 && (value & (1 << (m_dataType.numBits - 1))) != 0; if (m_dataType.numBits == 32) { if (negative == false) { @@ -747,23 +826,35 @@ result_t NumberDataField::writeSymbols(std::istringstream& input, return writeRawValue(value, baseOffset, output); } -DataField* ValueListDataField::derive(std::string name, PartType partType, - unsigned char offset, std::string unit, std::string comment, - unsigned int divisor, std::map values) + +result_t ValueListDataField::derive(std::string name, std::string comment, + std::string unit, const PartType partType, unsigned char offset, + unsigned int divisor, std::map values, + std::vector& fields) { - if (unit.empty() == true) - unit = m_unit; + if (m_partType != pt_template && partType == pt_template) + return RESULT_ERR_INVALID_ARG; // cannot create a template from a concrete instance + if (name.empty() == true) + name = m_name; if (comment.empty() == true) comment = m_comment; + if (unit.empty() == true) + unit = m_unit; + offset += m_offset; + if (divisor != 0 && divisor != 1) + return RESULT_ERR_INVALID_ARG; // cannot use divisor != 1 for value list field - if (values.empty() == true) { - values = m_values; - } - else if (values.begin()->first < m_dataType.minValueOrLength + if (values.empty() == false) { + if (values.begin()->first < m_dataType.minValueOrLength || values.rbegin()->first > m_dataType.maxValueOrLength) - return NULL; + return RESULT_ERR_INVALID_ARG; // cannot use divisor != 1 for value list field + } + else + values = m_values; - return new ValueListDataField(name, partType, offset, m_length, m_dataType, unit, comment, values); + fields.push_back(new ValueListDataField(name, comment, unit, m_dataType, partType, offset, m_length, values)); + + return RESULT_OK; } result_t ValueListDataField::readSymbols(SymbolString& input, @@ -804,4 +895,87 @@ result_t ValueListDataField::writeSymbols(std::istringstream& input, return RESULT_ERR_INVALID_ARG; // value assignment not found } + +DataFieldSet::~DataFieldSet() +{ + while (m_fields.empty() == false) { + delete m_fields.back(); + m_fields.pop_back(); + } +} + +unsigned char DataFieldSet::getNextOffset() +{ + return m_fields.back()->getNextOffset(); +} + +result_t DataFieldSet::derive(std::string name, std::string comment, + std::string unit, const PartType partType, unsigned char offset, + unsigned int divisor, std::map values, + std::vector& fields) +{ + if (values.empty() == false) + return RESULT_ERR_INVALID_ARG; // value list not allowed in set derive + + for (std::vector::iterator it = m_fields.begin(); it < m_fields.end(); it++) { + result_t result = (*it)->derive("", "", "", partType, offset, divisor, values, fields); + if (result != RESULT_OK) + return result; + } + + return RESULT_OK; +} + +result_t DataFieldSet::read(SymbolString& masterData, SymbolString& slaveData, std::ostringstream& output, + bool verbose, char separator) +{ + if (verbose) + output << m_name << "={ "; + + bool first = true; + for (std::vector::iterator it = m_fields.begin(); it < m_fields.end(); it++) { + if (first) + first = false; + else + output << separator; + + result_t result = (*it)->read(masterData, slaveData, output, verbose); + + if (result != RESULT_OK) + return result; + } + + if (verbose) { + if (m_comment.length() > 0) + output << " [" << m_comment << "]"; + output << "}"; + } + + return RESULT_OK; +} + +result_t DataFieldSet::write(std::istringstream& input, SymbolString& masterData, SymbolString& slaveData, + char separator) +{ + std::string token; + + for (std::vector::iterator it = m_fields.begin(); it < m_fields.end(); it++) { + result_t result; + if (m_fields.size() > 1) { + if (std::getline(input, token, separator) == 0) + return RESULT_ERR_INVALID_ARG; // incomplete + std::istringstream single(token); + result = (*it)->write(single, masterData, slaveData); + } + else + result = (*it)->write(input, masterData, slaveData); + + if (result != RESULT_OK) + return result; + } + + return RESULT_OK; +} + + } //namespace diff --git a/src/lib/ebus/data.h b/src/lib/ebus/data.h index a5c690bd..c5635022 100644 --- a/src/lib/ebus/data.h +++ b/src/lib/ebus/data.h @@ -68,34 +68,26 @@ typedef struct { } dataType_t; +class SingleDataField; + /** * @brief Base class for all kinds of data fields. */ class DataField { public: + /** * @brief Constructs a new instance. * @param name the field name. - * @param partType the message part in which the field is stored. - * @param offset the relatvie offset to the first symbol in the message part in which the field is stored. - * @param length the number of symbols in the message part in which the field is stored. - * @param dataType the data type definition. - * @param unit the value unit. * @param comment the field comment. */ - DataField(const std::string name, const PartType partType, - const unsigned char offset, const unsigned char length, - const dataType_t dataType, const std::string unit, - const std::string comment) - : m_name(name), m_partType(partType), m_offset(offset), - m_length(length), m_dataType(dataType), m_unit(unit), - m_comment(comment) {} + DataField(const std::string name, const std::string comment) + : m_name(name), m_comment(comment) {} /** * @brief Destructor. */ virtual ~DataField() {} - /** * @brief Factory method for creating new instances. * @param it the iterator to traverse for the definition parts. @@ -108,9 +100,103 @@ public: * Note: the caller needs to cleanup created instances. */ static result_t create(std::vector::iterator& it, const std::vector::iterator end, - const std::map templates, std::vector& fields, + const std::map< std::string, DataField*> templates, DataField*& fields, const bool isSetMessage=false, const unsigned char dstAddress=SYN); + /** + * @brief Returns the offset to the first symbol in the message part for a field following this field. + * @return the offset to the first symbol in the message part for a field following this field. + */ + virtual unsigned char getNextOffset() = 0; + /** + * @brief Derives a new DataField from this field. + * @param name the field name. + * @param comment the field comment, or empty to use this fields comment. + * @param unit the value unit, or empty to use this fields unit (if applicable). + * @param partType the message part in which the field is stored. + * @param offset the (additional) offset to the first symbol in the message part in which the field is stored. + * @param divisor the extra divisor to apply on the value, or 1 for none (if applicable). + * @param values the value=text assignments, or empty to use this fields assignments (if applicable). + */ + virtual result_t derive(std::string name, std::string comment, + std::string unit, const PartType partType, unsigned char offset, + unsigned int divisor, std::map values, + std::vector& fields) = 0; + /** + * @brief Get the field name. + * @return the field name. + */ + const std::string getName() { return m_name; } + /** + * @brief Get the field comment. + * @return the field comment. + */ + const std::string getComment() { return m_comment; } + /** + * @brief Reads the value from the master or slave @a SymbolString. + * @param masterData the unescaped master data @a SymbolString for reading binary data. + * @param slaveData the unescaped slave data @a SymbolString for reading binary data. + * @param output the ostringstream to append the formatted value to. + * @param verbose whether to prepend the name, append the unit (if present), and append + * the comment in square brackets (if present). + * @param separator the separator character between multiple fields. + * @return RESULT_OK on success, or an error code. + */ + virtual result_t read(SymbolString& masterData, SymbolString& slaveData, std::ostringstream& output, + bool verbose=false, char separator=';') = 0; + /** + * @brief Writes the value to the master or slave @a SymbolString. + * @param input the istringstream to parse the formatted value from. + * @param masterData the unescaped master data @a SymbolString for writing binary data. + * @param slaveData the unescaped slave data @a SymbolString for writing binary data. + * @param separator the separator character between multiple fields. + * @return RESULT_OK on success, or an error code. + */ + virtual result_t write(std::istringstream& input, SymbolString& masterData, SymbolString& slaveData, + char separator=';') = 0; +protected: + + /** the field name. */ + const std::string m_name; + /** the field comment. */ + const std::string m_comment; + +}; + + +/** + * @brief A single DataField. + */ +class SingleDataField : public DataField +{ +public: + + /** + * @brief Constructs a new instance. + * @param name the field name. + * @param comment the field comment. + * @param unit the value unit. + * @param dataType the data type definition. + * @param partType the message part in which the field is stored. + * @param offset the offset to the first symbol in the message part in which the field is stored. + * @param length the number of symbols in the message part in which the field is stored. + */ + SingleDataField(const std::string name, const std::string comment, + const std::string unit, const dataType_t dataType, const PartType partType, + const unsigned char offset, const unsigned char length) + : DataField(name, comment), + m_unit(unit), m_dataType(dataType), m_partType(partType), + m_offset(offset), m_length(length) {} + /** + * @brief Destructor. + */ + virtual ~SingleDataField() {} + /** + * @brief Get the value unit. + * @return the value unit. + */ + const std::string getUnit() { return m_unit; } + virtual unsigned char getNextOffset(); /** * @brief Reads the value from the master or slave @a SymbolString. * @param masterData the unescaped master data @a SymbolString for reading binary data. @@ -120,8 +206,8 @@ public: * the comment in square brackets (if present). * @return RESULT_OK on success, or an error code. */ - result_t read(SymbolString& masterData, SymbolString& slaveData, std::ostringstream& output, - bool verbose=false); + virtual result_t read(SymbolString& masterData, SymbolString& slaveData, std::ostringstream& output, + bool verbose=false, char separator=';'); /** * @brief Writes the value to the master or slave @a SymbolString. * @param input the istringstream to parse the formatted value from. @@ -129,29 +215,11 @@ public: * @param slaveData the unescaped slave data @a SymbolString for writing binary data. * @return RESULT_OK on success, or an error code. */ - result_t write(std::istringstream& input, SymbolString& masterData, SymbolString& slaveData); - - /** - * @brief Get the field name. - * @return the field name. - */ - const std::string getName() { return m_name; } - - /** - * @brief Get the value unit. - * @return the value unit. - */ - const std::string getUnit() { return m_unit; } - - /** - * @brief Get the field comment. - * @return the field comment. - */ - const std::string getComment() { return m_comment; } + virtual result_t write(std::istringstream& input, SymbolString& masterData, SymbolString& slaveData, + char separator=';'); protected: - virtual DataField* derive(std::string name, PartType partType, unsigned char offset, std::string unit, - std::string comment, unsigned int divisor, std::map values) = 0; + /** * @brief Internal method for reading the field from a @a SymbolString. * @param input the unescaped @a SymbolString to read the binary value from. @@ -167,52 +235,52 @@ protected: */ virtual result_t writeSymbols(std::istringstream& input, unsigned char baseOffset, SymbolString& output) = 0; - /** the field name. */ - const std::string m_name; + /** the value unit. */ + const std::string m_unit; + /** the data type definition. */ + const dataType_t m_dataType; /** the message part in which the field is stored. */ const PartType m_partType; /** the relative offset to the first symbol in the message part in which the field is stored. */ const unsigned char m_offset; /** the number of symbols in the message part in which the field is stored. */ const unsigned char m_length; - /** the data type definition. */ - const dataType_t m_dataType; - /** the value unit. */ - const std::string m_unit; - /** the field comment. */ - const std::string m_comment; + }; /** * @brief Base class for all string based data fields. */ -class StringDataField : public DataField +class StringDataField : public SingleDataField { public: + /** * @brief Constructs a new instance. * @param name the field name. + * @param comment the field comment. + * @param unit the value unit. + * @param dataType the data type definition. * @param partType the message part in which the field is stored. * @param offset the offset to the first symbol in the message part in which the field is stored. * @param length the number of symbols in the message part in which the field is stored. - * @param dataType the data type definition. - * @param unit the value unit. - * @param comment the field comment. */ - StringDataField(const std::string name, const PartType partType, - const unsigned char offset, const unsigned char length, - const dataType_t dataType, const std::string unit, - const std::string comment) - : DataField(name, partType, offset, length, dataType, unit, comment) {} + StringDataField(const std::string name, const std::string comment, + const std::string unit, const dataType_t dataType, const PartType partType, + const unsigned char offset, const unsigned char length) + : SingleDataField(name, comment, unit, dataType, partType, offset, length) {} /** * @brief Destructor. */ virtual ~StringDataField() {} + virtual result_t derive(std::string name, std::string comment, + std::string unit, const PartType partType, unsigned char offset, + unsigned int divisor, std::map values, + std::vector& fields); protected: - virtual DataField* derive(std::string name, PartType partType, unsigned char offset, std::string unit, - std::string comment, unsigned int divisor, std::map values); + virtual result_t readSymbols(SymbolString& input, unsigned char baseOffset, std::ostringstream& output); virtual result_t writeSymbols(std::istringstream& input, unsigned char baseOffset, SymbolString& output); @@ -222,32 +290,34 @@ protected: /** * @brief Base class for all numeric data fields. */ -class NumericDataField : public DataField +class NumericDataField : public SingleDataField { public: + /** * @brief Constructs a new instance. * @param name the field name. + * @param comment the field comment. + * @param unit the value unit. + * @param dataType the data type definition. * @param partType the message part in which the field is stored. * @param offset the offset to the first symbol in the message part in which the field is stored. * @param length the number of symbols in the message part in which the field is stored. * @param bitOffset the offset to the first bit in the binary value. - * @param dataType the data type definition. - * @param unit the value unit. - * @param comment the field comment. */ - NumericDataField(const std::string name, const PartType partType, + NumericDataField(const std::string name, const std::string comment, + const std::string unit, const dataType_t dataType, const PartType partType, const unsigned char offset, const unsigned char length, - const dataType_t dataType, const std::string unit, - const std::string comment, const unsigned char bitOffset) - : DataField(name, partType, offset, length, dataType, unit, comment), - m_bitOffset(bitOffset) {} + const unsigned char bitOffset) + : SingleDataField(name, comment, unit, dataType, partType, offset, length), + m_bitOffset(bitOffset) {} /** * @brief Destructor. */ virtual ~NumericDataField() {} protected: + /** * @brief Internal method for reading the raw value from a @a SymbolString. * @param input the unescaped @a SymbolString to read the binary value from. @@ -268,38 +338,43 @@ protected: }; + /** * @brief Base class for all numeric data fields with a number representation. */ class NumberDataField : public NumericDataField { public: + /** * @brief Constructs a new instance. * @param name the field name. + * @param comment the field comment. + * @param unit the value unit. + * @param dataType the data type definition. * @param partType the message part in which the field is stored. * @param offset the offset to the first symbol in the message part in which the field is stored. * @param length the number of symbols in the message part in which the field is stored. - * @param dataType the data type definition. - * @param unit the value unit. - * @param comment the field comment. * @param divisor the extra divisor to apply on the value, or 1 for none. */ - NumberDataField(const std::string name, const PartType partType, + NumberDataField(const std::string name, const std::string comment, + const std::string unit, const dataType_t dataType, const PartType partType, const unsigned char offset, const unsigned char length, - const dataType_t dataType, const std::string unit, - const std::string comment, const unsigned int divisor) - : NumericDataField(name, partType, offset, length, dataType, unit, comment, + const unsigned int divisor) + : NumericDataField(name, comment, unit, dataType, partType, offset, length, (dataType.numBits%8) != 0 ? dataType.precisionOrFirstBit : 0), - m_divisor(divisor) {} + m_divisor(divisor) {} /** * @brief Destructor. */ virtual ~NumberDataField() {} protected: - virtual DataField* derive(std::string name, PartType partType, unsigned char offset, std::string unit, - std::string comment, unsigned int divisor, std::map values); + + virtual result_t derive(std::string name, std::string comment, + std::string unit, const PartType partType, unsigned char offset, + unsigned int divisor, std::map values, + std::vector& fields); virtual result_t readSymbols(SymbolString& input, unsigned char baseOffset, std::ostringstream& output); virtual result_t writeSymbols(std::istringstream& input, unsigned char baseOffset, SymbolString& output); @@ -308,38 +383,43 @@ protected: }; + /** * @brief A numeric data field with a list of value=text assignments and a string representation. */ class ValueListDataField : public NumericDataField { public: + /** * @brief Constructs a new instance. * @param name the field name. + * @param comment the field comment. + * @param unit the value unit. + * @param dataType the data type definition. * @param partType the message part in which the field is stored. * @param offset the offset to the first symbol in the message part in which the field is stored. * @param length the number of symbols in the message part in which the field is stored. - * @param dataType the data type definition. - * @param unit the value unit. - * @param comment the field comment. * @param values the value=text assignments. */ - ValueListDataField(const std::string name, const PartType partType, + ValueListDataField(const std::string name, const std::string comment, + const std::string unit, const dataType_t dataType, const PartType partType, const unsigned char offset, const unsigned char length, - const dataType_t dataType, const std::string unit, - const std::string comment, const std::map values) - :NumericDataField(name, partType, offset, length, dataType, unit, comment, + const std::map values) + : NumericDataField(name, comment, unit, dataType, partType, offset, length, (dataType.numBits%8) != 0 ? dataType.precisionOrFirstBit : 0), - m_values(values) {} + m_values(values) {} /** * @brief Destructor. */ virtual ~ValueListDataField() {} protected: - virtual DataField* derive(std::string name, PartType partType, unsigned char offset, std::string unit, - std::string comment, unsigned int divisor, std::map values); + + virtual result_t derive(std::string name, std::string comment, + std::string unit, const PartType partType, unsigned char offset, + unsigned int divisor, std::map values, + std::vector& fields); virtual result_t readSymbols(SymbolString& input, unsigned char baseOffset, std::ostringstream& output); virtual result_t writeSymbols(std::istringstream& input, unsigned char baseOffset, SymbolString& output); @@ -349,6 +429,78 @@ protected: }; +/** + * @brief A set of DataFields. + */ +class DataFieldSet : public DataField +{ +public: + + /** + * @brief Constructs a new instance. + * @param name the field name. + * @param comment the field comment. + * @param fields the list of SingleDataFields part of this set. + */ + DataFieldSet(const std::string name, const std::string comment, + const std::vector fields) + : DataField(name, comment), + m_fields(fields) {} + /** + * @brief Destructor. + */ + virtual ~DataFieldSet(); + virtual unsigned char getNextOffset(); + virtual result_t derive(std::string name, std::string comment, + std::string unit, const PartType partType, unsigned char offset, + unsigned int divisor, std::map values, + std::vector& fields); + /** + * @brief Returns the SingleDataField at the specified index. + * @param index the index of the SingleDataField to return. + * @return the SingleDataField at the specified index, or NULL. + */ + SingleDataField* operator[](const size_t index) { if (index >= m_fields.size()) return NULL; return m_fields[index]; } + /** + * @brief Returns the SingleDataField at the specified index. + * @param index the index of the SingleDataField to return. + * @return the SingleDataField at the specified index, or NULL. + */ + const SingleDataField* operator[](const size_t index) const { if (index >= m_fields.size()) return NULL; return m_fields[index]; } + /** + * @brief Returns the number of SingleDataFields in this set. + * @return the number of available SingleDataField. + */ + size_t size() const { return m_fields.size(); } + /** + * @brief Reads the values from the master and/or slave @a SymbolString. + * @param masterData the unescaped master data @a SymbolString for reading binary data. + * @param slaveData the unescaped slave data @a SymbolString for reading binary data. + * @param output the ostringstream to append the formatted value to. + * @param vervose whether to prepend the name, append the unit (if present), and append + * the comment in square brackets (if present). + * @return RESULT_OK on success, or an error code. + */ + virtual result_t read(SymbolString& masterData, SymbolString& slaveData, std::ostringstream& output, + bool verbose=false, char separator=';'); + /** + * @brief Writes the values to the master and/or slave @a SymbolString. + * @param input the istringstream to parse the formatted value from. + * @param masterData the unescaped master data @a SymbolString for writing binary data. + * @param slaveData the unescaped slave data @a SymbolString for writing binary data. + * @return RESULT_OK on success, or an error code. + */ + virtual result_t write(std::istringstream& input, SymbolString& masterData, SymbolString& slaveData, + char separator=';'); + +protected: + + /** the list of SingleDataFields part of this set. */ + std::vector m_fields; + +}; + + } //namespace #endif // LIBEBUS_DATA_H_ diff --git a/src/lib/ebus/test/test_data.cpp b/src/lib/ebus/test/test_data.cpp index d96a9562..2a3c0a96 100644 --- a/src/lib/ebus/test/test_data.cpp +++ b/src/lib/ebus/test/test_data.cpp @@ -67,13 +67,14 @@ int main() {"x;;htm", "21:04", "10fe0700021504", "00", ""}, {"x;;htm", "00:00", "10fe0700020000", "00", ""}, {"x;;htm", "23:59", "10fe070002173b", "00", ""}, + {"x;;htm", "24:00", "10fe0700021800", "00", ""}, {"x;;htm", "", "10fe070002183b", "00", "rw"}, - {"x;;htm", "24:00", "10fe070002173b", "00", "Rw"}, + {"x;;htm", "24:01", "10fe0700021801", "00", "rw"}, {"x;;ttm", "22:40", "10fe07000188", "00", ""}, {"x;;ttm", "00:00", "10fe07000100", "00", ""}, {"x;;ttm", "23:50", "10fe0700018f", "00", ""}, - {"x;;ttm", "24:00", "10fe07000190", "00", "rw"}, // TODO check range - {"x;;ttm", "", "10fe07000191", "00", "rw"}, // TODO check range + {"x;;ttm", "24:00", "10fe07000190", "00", ""}, + {"x;;ttm", "", "10fe07000191", "00", "rw"}, {"x;;bdy", "Mon", "10fe07000300", "00", ""}, {"x;;bdy", "Sun", "10fe07000306", "00", ""}, {"x;;bdy", "", "10fe07000308", "00", "rw"}, @@ -156,22 +157,37 @@ int main() {"x;s3;uch","3","1050ffff00", "03000003", ""}, {"x;s3;uch","3","1050ffff00", "020000", "rW"}, {"x;;d2b;;°C;Aussentemperatur","x=18.004 °C [Aussentemperatur]","10fe0700090112", "00", "v"}, - {"x;;bti;;;;y;;bda;;;;z;6;bdy", "21:04:58;26.10.2014;Sun","10fe07000758042126100614", "00", "c"}, - //TODO test bit combinations - {"temprel;;d2b;;°C;Aussentemperatur","","", "", "p"}, // predefined type with relative pos - {"tempabs;1;d2b;;°C;Aussentemperatur","","", "", "p"},// predefined type with absolute pos - {"strucrelrel;;d2b;;;;y;;d1c","","", "", "p"}, // predefined combined type with relative pos - {"strucrelabs;;d2b;;;;y;1;d1c","","", "", "p"}, // predefined combined type with relative pos - {"strucabsrel;1;d2b;;;;y;;d1c","","", "", "p"}, // predefined combined type with relative pos - {"strucabsabs;2;d2b;;;;y;1;d1c","","", "", "p"}, // predefined combined type with absolute pos - {"x;;temprel","18.004","10fe0700090112", "00", ""}, // reference predefined type - {"strucrelrel;;temprel,temprel","","", "", "p"}, // predefined combined type with relative pos - {"strucrelabs;;temprel,tempabs","","", "", "p"}, // predefined combined type with relative pos - {"strucabsrel;1;tempabs,temprel","","", "", "p"}, // predefined combined type with relative pos - {"strucabs;2;tempabs","","", "", "p"}, // predefined combined type with absolute pos + {"x;;bti;;;;y;;bda;;;;z;6;bdy", "21:04:58;26.10.2014;Sun","10fe07000758042126100614", "00", ""}, // combination + {"x;;bi3;;;;y;;bi5", "1;-", "10feffff0108", "00", ""}, // bit combination + {"x;;bi3;;;;y;;bi5", "1;1", "10feffff0128", "00", ""}, // bit combination + {"x;;bi3;;;;y;;bi5", "-;1", "10feffff0120", "00", ""}, // bit combination + {"x;;bi3;;;;y;;bi5", "-;-", "10feffff0100", "00", ""}, // bit combination + {"x;;bi3;;;;y;;bi7;;;;t;;uch", "-;-;9","10feffff020009", "00", ""}, // bit combination, auto pos incr + {"x;;bi3;;;;y;;bi5;;;;t;;uch", "-;-;9","10feffff020009", "00", "RW"}, // bit combination + {"temp;;d2b;;°C;Aussentemperatur","","", "", "t"}, // template with relative pos + {"x;;temp","18.004","10fe0700020112", "00", ""}, // reference to template + {"tempoff;2;d2b;;°C;Aussentemperatur","","", "", "t"},// template with offset pos + {"x;;tempoff","18.004","10fe070002ff0112", "00", "W"}, // reference to template + {"relrel;;d2b;;;;y;;d1c","","", "", "t"}, // template struct with relative pos + {"x;2;relrel","18.004;9.5","10fe070004ff011213", "00", "W"}, // reference to template struct + {"reloff;;d2b;;;;y;1;d1c","","", "", "t"}, // template struct with relative+offset pos + {"x;2;reloff","18.004;0.5","10fe070003130112", "00", "W"}, // reference to template struct + {"offrel;2;d2b;;;;y;;d1c","","", "", "t"}, // template struct with offset+relative pos + {"x;2;offrel","18.004;9.5","10fe070005fffe011213", "00", "W"}, // reference to template struct + {"offoff;2;d2b;;;;y;1;d1c","","", "", "t"}, // template struct with offset pos + {"x;2;offoff","18.004;9.5","10fe070004ff130112", "00", "W"}, // reference to template struct + {"trelrel;;temp,temp","","", "", "t"}, // template struct with relative pos and ref to templates + {"x;;trelrel","18.004;19.008","10fe07000401120213", "00", ""}, // reference to template struct + {"x;2;trelrel","18.004;19.008","10fe070005ff01120213", "00", "W"}, // reference to template struct + {"treloff;;temp,tempoff","","", "", "t"}, // template struct with relative+offset pos + {"x;2;treloff","18.004;19.008","10fe070006ff0112fe0213", "00", "W"}, // reference to template struct + {"toffrel;1;tempoff,temp","","", "", "t"}, // template struct with offset+relative pos + {"x;2;toffrel","18.004;19.008","10fe070003fffe01120213", "00", "W"}, // reference to template struct + {"toffoff;1;tempoff,tempoff","","", "", "t"}, // template struct with offset pos + {"x;2;toffoff","18.004;19.008","10fe070003fffe0112fd0213", "00", "W"}, // reference to template struct }; std::map templates; - std::vector fields; + DataField* fields = NULL; for (size_t i = 0; i < sizeof(checks) / sizeof(checks[0]); i++) { std::string check[5] = checks[i]; std::istringstream isstr(check[0]); @@ -185,116 +201,92 @@ int main() bool failedWrite = flags.find('w') != std::string::npos; bool failedWriteMatch = flags.find('W') != std::string::npos; bool verbose = flags.find('v') != std::string::npos; - bool combinedValue = flags.find('c') != std::string::npos; - bool isPredefine = flags.find('p') != std::string::npos; + bool isTemplate = flags.find('t') != std::string::npos; std::string item; std::vector entries; - while (fields.empty() == false) { - delete fields.back(); - fields.pop_back(); - } while (std::getline(isstr, item, ';') != 0) entries.push_back(item); + if (fields != NULL) { + delete fields; + fields = NULL; + } std::vector::iterator it = entries.begin(); - result_t result; - do { - result = DataField::create(it, entries.end(), templates, fields, isSet, isPredefine ? SYN : mstr[1]); - } while (result == RESULT_OK && it != entries.end()); + result_t result = DataField::create(it, entries.end(), templates, fields, isSet, isTemplate ? SYN : mstr[1]); if (result != RESULT_OK) { std::cout << "\"" << check[0] << "\": create error: " << getResultCodeCStr(result) << std::endl; continue; } - if (fields.empty() == true) { + if (fields == NULL) { std::cout << "\"" << check[0] << "\": create error: empty" << std::endl; continue; } + if (it != entries.end()) { + std::cout << "\"" << check[0] << "\": create error: non-empty" << std::endl; + continue; + } std::cout << "\"" << check[0] << "\": create OK" << std::endl; - if (isPredefine) { + if (isTemplate) { // store new template - while (fields.empty() == false) { - DataField* field = fields.front(); - fields.erase(fields.begin()); - std::map::iterator current = templates.find(field->getName()); - if (current == templates.end()) - templates[field->getName()] = field; - else { - delete current->second; - current->second = field; - } + std::string name = fields->getName(); + std::map::iterator current = templates.find(name); + if (current == templates.end()) { + templates[name] = fields; + } else { + delete current->second; + current->second = fields; } + fields = NULL; continue; } std::ostringstream output; - std::istringstream input(expectStr); SymbolString writeMstr = SymbolString(mstr.getDataStr().substr(0, 10), false); SymbolString writeSstr = SymbolString(sstr.getDataStr().substr(0, 2), false); - bool first = true, failed = false; - while (fields.empty() == false) { - DataField* field = fields.front(); - fields.erase(fields.begin()); - if (first == false) - output << ";"; - - result = field->read(mstr, sstr, output, verbose); - if (failedRead == true) - if (result == RESULT_OK) - std::cout << " failed read " << field->getName() << " >" - << check[2] << "< error: unexpectedly succeeded" << std::endl; - else - std::cout << " failed read " << field->getName() << " >" - << check[2] << "< OK" << std::endl; - else if (result != RESULT_OK) { - std::cout << " read " << field->getName() << " >" << check[2] << "< error: " - << getResultCodeCStr(result) << std::endl; - failed = true; - } - else if (combinedValue == false) { - bool match = strcasecmp(output.str().c_str(), expectStr.c_str()) == 0; - verify(failedReadMatch, "read", check[2], match, expectStr, output.str()); - } - - if (verbose == false) { - std::string token; - std::getline(input, token, ';'); - std::istringstream tokeninput(token); - - result = field->write(tokeninput, writeMstr, writeSstr); - if (failedWrite == true) { - if (result == RESULT_OK) - std::cout << " failed write " << field->getName() << " >" - << expectStr << "< error: unexpectedly succeeded" << std::endl; - else - std::cout << " failed write " << field->getName() << " >" - << expectStr << "< OK" << std::endl; - } - else if (result != RESULT_OK) { - std::cout << " write " << field->getName() << " >" - << expectStr << "< error: " << getResultCodeCStr(result) << std::endl; - failed = true; - } - } - first = false; + result = fields->read(mstr, sstr, output, verbose); + if (failedRead == true) + if (result == RESULT_OK) + std::cout << " failed read " << fields->getName() << " >" + << check[2] << "< error: unexpectedly succeeded" << std::endl; + else + std::cout << " failed read " << fields->getName() << " >" + << check[2] << "< OK" << std::endl; + else if (result != RESULT_OK) { + std::cout << " read " << fields->getName() << " >" << check[2] << "< error: " + << getResultCodeCStr(result) << std::endl; } - - if (combinedValue == true && failedRead == false && failed == false) { + else { bool match = strcasecmp(output.str().c_str(), expectStr.c_str()) == 0; verify(failedReadMatch, "read", check[2], match, expectStr, output.str()); } - if (verbose == false && failedWrite == false && failed == false) { - bool match = mstr == writeMstr && sstr == writeSstr; - verify(failedWriteMatch, "write", expectStr, match, mstr.getDataStr() + " " + sstr.getDataStr(), writeMstr.getDataStr() + " " + writeSstr.getDataStr()); + + if (verbose == false) { + std::istringstream input(expectStr); + result = fields->write(input, writeMstr, writeSstr); + if (failedWrite == true) { + if (result == RESULT_OK) + std::cout << " failed write " << fields->getName() << " >" + << expectStr << "< error: unexpectedly succeeded" << std::endl; + else + std::cout << " failed write " << fields->getName() << " >" + << expectStr << "< OK" << std::endl; + } + else if (result != RESULT_OK) { + std::cout << " write " << fields->getName() << " >" + << expectStr << "< error: " << getResultCodeCStr(result) << std::endl; + } + else { + bool match = mstr == writeMstr && sstr == writeSstr; + verify(failedWriteMatch, "write", expectStr, match, mstr.getDataStr() + " " + sstr.getDataStr(), writeMstr.getDataStr() + " " + writeSstr.getDataStr()); + } } + delete fields; + fields = NULL; } - while (fields.empty() == false) { - delete fields.back(); - fields.pop_back(); - } for (std::map::iterator it = templates.begin(); it != templates.end(); it++) delete it->second; From 46bbca5d863b8501b432b62d0d14f833785690a1 Mon Sep 17 00:00:00 2001 From: john30 Date: Thu, 20 Nov 2014 09:30:47 +0100 Subject: [PATCH 6/6] file mode --- src/lib/ebus/data.cpp | 0 src/lib/ebus/data.h | 0 src/lib/ebus/test/test_data.cpp | 0 3 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 src/lib/ebus/data.cpp mode change 100755 => 100644 src/lib/ebus/data.h mode change 100755 => 100644 src/lib/ebus/test/test_data.cpp diff --git a/src/lib/ebus/data.cpp b/src/lib/ebus/data.cpp old mode 100755 new mode 100644 diff --git a/src/lib/ebus/data.h b/src/lib/ebus/data.h old mode 100755 new mode 100644 diff --git a/src/lib/ebus/test/test_data.cpp b/src/lib/ebus/test/test_data.cpp old mode 100755 new mode 100644