formatting

This commit is contained in:
john30
2014-11-16 11:07:46 +01:00
parent 73534d2b45
commit 264ba1ca51
2 changed files with 155 additions and 121 deletions
+128 -106
View File
@@ -94,6 +94,7 @@ static const dataType_t dataTypes[] = {
{"B17", 7, bt_num, LST, 0, 0, 0x7f, 1, 1}, // seven bits 1-7 {"B17", 7, bt_num, LST, 0, 0, 0x7f, 1, 1}, // seven bits 1-7
}; };
/** the week day names. */ /** the week day names. */
static const char* dayNames[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}; 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 VALUE_SEPARATOR ','
#define NULL_VALUE "-" #define NULL_VALUE "-"
result_t DataField::create(std::vector<std::string>::iterator& it, const std::vector<std::string>::iterator end, result_t DataField::create(std::vector<std::string>::iterator& it,
const std::map<std::string, DataField*> templates, std::vector<DataField*>& fields, const std::vector<std::string>::iterator end,
const bool isSetMessage, const unsigned char dstAddress) { const std::map<std::string, DataField*> templates,
std::vector<DataField*>& fields, const bool isSetMessage,
const unsigned char dstAddress)
{
std::string unit, comment; std::string unit, comment;
PartType partType; PartType partType;
unsigned int divisor = 0; unsigned int divisor = 0;
@@ -123,19 +127,22 @@ result_t DataField::create(std::vector<std::string>::iterator& it, const std::ve
return RESULT_ERR_EOF; return RESULT_ERR_EOF;
if (dstAddress == BROADCAST || isMaster(dstAddress) if (dstAddress == BROADCAST || isMaster(dstAddress)
|| (isTemplate == false && isSetMessage == true && posStr[0] != 0 && posStr[0] <= '9') || (isTemplate == false && isSetMessage == true && posStr[0] != 0 && posStr[0] <= '9')
|| posStr[0] == 'm') { // master data || posStr[0] == 'm') { // master data
partType = pt_masterData; partType = pt_masterData;
if (posStr[0] == 'm') if (posStr[0] == 'm')
posStr++; 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; partType = pt_slaveData;
if (posStr[0] == 's') if (posStr[0] == 's')
posStr++; posStr++;
} else if (isTemplate) { }
else if (isTemplate) {
partType = pt_template; partType = pt_template;
} else }
else
return RESULT_ERR_INVALID_ARG; return RESULT_ERR_INVALID_ARG;
bool hasPrev = fields.empty() == false; bool hasPrev = fields.empty() == false;
@@ -143,7 +150,7 @@ result_t DataField::create(std::vector<std::string>::iterator& it, const std::ve
DataField* previous = fields.back(); DataField* previous = fields.back();
offset = previous->m_offset + previous->m_length; offset = previous->m_offset + previous->m_length;
if ((previous->m_dataType.numBits % 8) != 0 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 offset--; // previous bits not yet fully consumed
} }
else else
@@ -151,7 +158,8 @@ result_t DataField::create(std::vector<std::string>::iterator& it, const std::ve
if (posStr[0] == 0) { if (posStr[0] == 0) {
length = 0; length = 0;
} else { }
else {
offset = 0; offset = 0;
length = 0; length = 0;
std::istringstream stream(posStr); std::istringstream stream(posStr);
@@ -161,19 +169,19 @@ result_t DataField::create(std::vector<std::string>::iterator& it, const std::ve
const char* start = token.c_str(); const char* start = token.c_str();
char* end = NULL; char* end = NULL;
unsigned int pos = strtoul(start, &end, 10)-1; // 1-based unsigned int pos = strtoul(start, &end, 10) - 1; // 1-based
if (end != start+strlen(start)) if (end != start + strlen(start))
return RESULT_ERR_INVALID_ARG; // invalid pos definition return RESULT_ERR_INVALID_ARG; // invalid pos definition
if (pos > maxPos) // TODO check this in real offset as well if (pos > maxPos) // TODO check this in real offset as well
return RESULT_ERR_INVALID_ARG; // invalid pos definition return RESULT_ERR_INVALID_ARG; // invalid pos definition
if (offsetCnt==1) if (offsetCnt == 1)
offset = pos; offset = pos;
else if (pos >= offset) else if (pos >= offset)
length = pos+1-offset; length = pos + 1 - offset;
else { // wrong order e.g. 4-3 else { // wrong order e.g. 4-3
length = offset-(pos+1); length = offset - (pos + 1);
offset = pos; offset = pos;
} }
} }
@@ -191,7 +199,7 @@ result_t DataField::create(std::vector<std::string>::iterator& it, const std::ve
const char* start = divisorStr.c_str(); const char* start = divisorStr.c_str();
char* end = NULL; char* end = NULL;
divisor = strtoul(start, &end, 10); divisor = strtoul(start, &end, 10);
if (end != start+strlen(start)) if (end != start + strlen(start))
return RESULT_ERR_INVALID_ARG; return RESULT_ERR_INVALID_ARG;
} }
else { else {
@@ -203,7 +211,7 @@ result_t DataField::create(std::vector<std::string>::iterator& it, const std::ve
if (end == NULL || end == start || *end != '=') if (end == NULL || end == start || *end != '=')
return RESULT_ERR_INVALID_ARG; 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<std::string>::iterator& it, const std::ve
return RESULT_OK; 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]; dataType_t dataType = dataTypes[i];
if (strcasecmp(typeStr, dataType.name) == 0) { if (strcasecmp(typeStr, dataType.name) == 0) {
unsigned char numBytes = (dataType.numBits+7)/8; unsigned char numBytes = (dataType.numBits + 7) / 8;
unsigned char useLength = length; unsigned char useLength = length;
if ((dataType.flags&ADJ) != 0) { if ((dataType.flags & ADJ) != 0) {
if (useLength == 0) if (useLength == 0)
useLength = 1; // minimum length defaults to 1 useLength = 1; // minimum length defaults to 1
else if (useLength > numBytes) else if (useLength > numBytes)
@@ -269,22 +277,24 @@ result_t DataField::create(std::vector<std::string>::iterator& it, const std::ve
else if (useLength != numBytes) else if (useLength != numBytes)
continue; // check for another one with same name but different length continue; // check for another one with same name but different length
switch (dataType.type) { switch (dataType.type)
{
case bt_str: case bt_str:
case bt_hexstr: case bt_hexstr:
case bt_dat: case bt_dat:
case bt_tim: 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; return RESULT_OK;
case bt_num: case bt_num:
if (values.empty() == true && (dataType.flags&DAY) != 0) { if (values.empty() == true && (dataType.flags & DAY) != 0) {
for (unsigned int i=0; i<sizeof(dayNames)/sizeof(dayNames[0]); i++) for (unsigned int i = 0; i < sizeof(dayNames) / sizeof(dayNames[0]); i++)
values[dataType.minValueOrLength + i] = dayNames[i]; values[dataType.minValueOrLength + i] = dayNames[i];
} }
if (values.empty() == true || (dataType.flags&LST) == 0) { if (values.empty() == true || (dataType.flags & LST) == 0) {
if (divisor == 0) { if (divisor == 0) {
divisor = dataType.divisor; divisor = dataType.divisor;
} else }
else
divisor *= dataType.divisor; divisor *= dataType.divisor;
fields.push_back(new NumberDataField(name, partType, offset, useLength, dataType, unit, comment, divisor)); fields.push_back(new NumberDataField(name, partType, offset, useLength, dataType, unit, comment, divisor));
return RESULT_OK; return RESULT_OK;
@@ -300,11 +310,13 @@ result_t DataField::create(std::vector<std::string>::iterator& it, const std::ve
return RESULT_ERR_INVALID_ARG; 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; SymbolString& input = m_partType == pt_masterData ? masterData : slaveData;
unsigned char baseOffset; unsigned char baseOffset;
switch (m_partType) { switch (m_partType)
{
case pt_masterData: case pt_masterData:
baseOffset = 5; // skip QQ ZZ PB SB NN baseOffset = 5; // skip QQ ZZ PB SB NN
break; break;
@@ -330,11 +342,13 @@ result_t DataField::read(SymbolString& masterData, SymbolString& slaveData, std:
return RESULT_OK; 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; SymbolString& output = m_partType == pt_masterData ? masterData : slaveData;
unsigned char baseOffset; unsigned char baseOffset;
switch (m_partType) { switch (m_partType)
{
case pt_masterData: case pt_masterData:
baseOffset = 5; // skip QQ ZZ PB SB NN baseOffset = 5; // skip QQ ZZ PB SB NN
break; break;
@@ -347,19 +361,21 @@ result_t DataField::write(std::istringstream& input, SymbolString& masterData, S
return writeSymbols(input, baseOffset, output); return writeSymbols(input, baseOffset, output);
} }
DataField* StringDataField::derive(std::string name, PartType partType,
unsigned char offset, std::string unit, std::string comment,
DataField* StringDataField::derive(std::string name, PartType partType, unsigned char offset, std::string unit, unsigned int divisor, std::map<unsigned int, std::string> values)
std::string comment, unsigned int divisor, std::map<unsigned int, std::string> values) { {
if (unit.empty() == true) if (unit.empty() == true)
unit = m_unit; unit = m_unit;
if (comment.empty() == true) if (comment.empty() == true)
comment = m_comment; 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; size_t start = m_offset, end = m_offset + m_length;
int incr = 1; int incr = 1;
@@ -368,7 +384,7 @@ result_t StringDataField::readSymbols(SymbolString& input, unsigned char baseOff
if (baseOffset + end > input.size()) if (baseOffset + end > input.size())
return RESULT_ERR_INVALID_ARG; 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; end = start - 1;
start = m_offset + m_length - 1; start = m_offset + m_length - 1;
incr = -1; incr = -1;
@@ -383,7 +399,8 @@ result_t StringDataField::readSymbols(SymbolString& input, unsigned char baseOff
return RESULT_ERR_INVALID_ARG; // invalid BCD return RESULT_ERR_INVALID_ARG; // invalid BCD
ch = (ch >> 4) * 10 + (ch & 0x0f); ch = (ch >> 4) * 10 + (ch & 0x0f);
} }
switch (m_dataType.type) { switch (m_dataType.type)
{
case bt_hexstr: case bt_hexstr:
if (i > 0) if (i > 0)
output << ' '; output << ' ';
@@ -392,7 +409,7 @@ result_t StringDataField::readSymbols(SymbolString& input, unsigned char baseOff
break; break;
case bt_dat: case bt_dat:
if (i + 1 == m_length) if (i + 1 == m_length)
output << (2000+ch); output << (2000 + ch);
else if (ch < 1 || (i == 0 && ch > 31) || (i == 1 && ch > 12)) else if (ch < 1 || (i == 0 && ch > 31) || (i == 1 && ch > 12))
return RESULT_ERR_INVALID_ARG; // invalid date return RESULT_ERR_INVALID_ARG; // invalid date
else else
@@ -405,7 +422,7 @@ result_t StringDataField::readSymbols(SymbolString& input, unsigned char baseOff
offset -= incr; // repeat for minutes offset -= incr; // repeat for minutes
} }
else else
ch = (ch%6) * 10; // minutes ch = (ch % 6) * 10; // minutes
} }
if ((i == 0 && ch > 23) || (i > 0 && ch > 59)) if ((i == 0 && ch > 23) || (i > 0 && ch > 59))
return RESULT_ERR_INVALID_ARG; // invalid time return RESULT_ERR_INVALID_ARG; // invalid time
@@ -424,7 +441,8 @@ result_t StringDataField::readSymbols(SymbolString& input, unsigned char baseOff
return RESULT_OK; 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; size_t start = m_offset, end = m_offset + m_length;
int incr = 1; int incr = 1;
@@ -433,7 +451,7 @@ result_t StringDataField::writeSymbols(std::istringstream& input, unsigned char
unsigned long int value = 0, hours = 0; unsigned long int value = 0, hours = 0;
std::string token; 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; end = start - 1;
start = m_offset + m_length - 1; start = m_offset + m_length - 1;
incr = -1; incr = -1;
@@ -441,9 +459,10 @@ result_t StringDataField::writeSymbols(std::istringstream& input, unsigned char
size_t i = 0; size_t i = 0;
for (size_t offset = start; offset != end; offset += incr, i++) { for (size_t offset = start; offset != end; offset += incr, i++) {
switch (m_dataType.type) { switch (m_dataType.type)
{
case bt_hexstr: case bt_hexstr:
while (input.peek()==' ') while (input.peek() == ' ')
input.get(); input.get();
if (input.eof() == true) // no more digits if (input.eof() == true) // no more digits
value = m_dataType.replacement; // fill up with replacement 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(); str = token.c_str();
strEnd = NULL; strEnd = NULL;
value = strtoul(str, &strEnd, 16); value = strtoul(str, &strEnd, 16);
if (strEnd != str+strlen(str)) if (strEnd != str + strlen(str))
return RESULT_ERR_INVALID_ARG; // invalid hex value return RESULT_ERR_INVALID_ARG; // invalid hex value
} }
break; break;
@@ -471,7 +490,7 @@ result_t StringDataField::writeSymbols(std::istringstream& input, unsigned char
str = token.c_str(); str = token.c_str();
strEnd = NULL; strEnd = NULL;
value = strtoul(str, &strEnd, 10); value = strtoul(str, &strEnd, 10);
if (strEnd != str+strlen(str)) if (strEnd != str + strlen(str))
return RESULT_ERR_INVALID_ARG; // invalid date part return RESULT_ERR_INVALID_ARG; // invalid date part
if (i + 1 == m_length && value >= 2000) if (i + 1 == m_length && value >= 2000)
value -= 2000; value -= 2000;
@@ -484,7 +503,7 @@ result_t StringDataField::writeSymbols(std::istringstream& input, unsigned char
str = token.c_str(); str = token.c_str();
strEnd = NULL; strEnd = NULL;
value = strtoul(str, &strEnd, 10); value = strtoul(str, &strEnd, 10);
if (strEnd != str+strlen(str)) if (strEnd != str + strlen(str))
return RESULT_ERR_INVALID_ARG; // invalid time part return RESULT_ERR_INVALID_ARG; // invalid time part
if ((i == 0 && value > 23) || (i > 0 && value > 59)) if ((i == 0 && value > 23) || (i > 0 && value > 59))
return RESULT_ERR_INVALID_ARG; // invalid time part 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) if ((value % 10) != 0)
return RESULT_ERR_INVALID_ARG; // invalid truncated time minutes return RESULT_ERR_INVALID_ARG; // invalid truncated time minutes
value = hours*6 + (value / 10); value = hours * 6 + (value / 10);
if (value > 24*6) if (value > 24 * 6)
return RESULT_ERR_INVALID_ARG; // invalid time return RESULT_ERR_INVALID_ARG; // invalid time
} }
break; break;
@@ -510,11 +529,11 @@ result_t StringDataField::writeSymbols(std::istringstream& input, unsigned char
if ((m_dataType.flags & BCD) != 0) { if ((m_dataType.flags & BCD) != 0) {
if (value > 99) if (value > 99)
return RESULT_ERR_INVALID_ARG; // invalid BCD return RESULT_ERR_INVALID_ARG; // invalid BCD
value = (value/10)<<4 | (value%10); value = (value / 10) << 4 | (value % 10);
} }
if (value > 0xff) if (value > 0xff)
return RESULT_ERR_INVALID_ARG; // value out of range 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) if (i < m_length)
@@ -523,8 +542,8 @@ result_t StringDataField::writeSymbols(std::istringstream& input, unsigned char
return RESULT_OK; return RESULT_OK;
} }
result_t NumericDataField::readRawValue(SymbolString& input,
result_t NumericDataField::readRawValue(SymbolString& input, unsigned char baseOffset, unsigned int& value) unsigned char baseOffset, unsigned int& value)
{ {
size_t start = m_offset, end = m_offset + m_length; size_t start = m_offset, end = m_offset + m_length;
int incr = 1; int incr = 1;
@@ -533,7 +552,7 @@ result_t NumericDataField::readRawValue(SymbolString& input, unsigned char baseO
if (baseOffset + end > input.size()) if (baseOffset + end > input.size())
return RESULT_ERR_INVALID_ARG; // not enough data available 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; end = start - 1;
start = m_offset + m_length - 1; start = m_offset + m_length - 1;
incr = -1; incr = -1;
@@ -551,38 +570,39 @@ result_t NumericDataField::readRawValue(SymbolString& input, unsigned char baseO
return RESULT_ERR_INVALID_ARG; // invalid BCD return RESULT_ERR_INVALID_ARG; // invalid BCD
ch = (ch >> 4) * 10 + (ch & 0x0f); ch = (ch >> 4) * 10 + (ch & 0x0f);
value += ch*exp; value += ch * exp;
exp *= 100; exp *= 100;
} }
else { else {
value |= ch*exp; value |= ch * exp;
exp <<= 8; exp <<= 8;
} }
} }
if ((m_dataType.flags & BCD) == 0) { if ((m_dataType.flags & BCD) == 0) {
value >>= m_bitOffset; value >>= m_bitOffset;
if ((m_dataType.numBits%8) != 0) if ((m_dataType.numBits % 8) != 0)
value &= (1<<m_dataType.numBits)-1; value &= (1 << m_dataType.numBits) - 1;
} }
return RESULT_OK; return RESULT_OK;
} }
result_t NumericDataField::writeRawValue(unsigned int value, unsigned char baseOffset, SymbolString& output) result_t NumericDataField::writeRawValue(unsigned int value,
unsigned char baseOffset, SymbolString& output)
{ {
size_t start = m_offset, end = m_offset + m_length; size_t start = m_offset, end = m_offset + m_length;
int incr = 1; int incr = 1;
unsigned char ch; unsigned char ch;
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; end = start - 1;
start = m_offset + m_length - 1; start = m_offset + m_length - 1;
incr = -1; incr = -1;
} }
if ((m_dataType.flags & BCD) == 0) { if ((m_dataType.flags & BCD) == 0) {
if ((m_dataType.numBits%8) != 0) if ((m_dataType.numBits % 8) != 0)
value &= (1<<m_dataType.numBits)-1; value &= (1 << m_dataType.numBits) - 1;
value <<= m_bitOffset; value <<= m_bitOffset;
} }
for (size_t offset = start, exp = 1; offset != end; offset += incr) { for (size_t offset = start, exp = 1; offset != end; offset += incr) {
@@ -590,16 +610,16 @@ result_t NumericDataField::writeRawValue(unsigned int value, unsigned char baseO
if (value == m_dataType.replacement) if (value == m_dataType.replacement)
ch = m_dataType.replacement; ch = m_dataType.replacement;
else { else {
ch = (value/exp)%100; ch = (value / exp) % 100;
ch = ((ch/10)<<4) | (ch%10); ch = ((ch / 10) << 4) | (ch % 10);
} }
exp = exp*100; exp = exp * 100;
} }
else { else {
ch = (value/exp)&0xff; ch = (value / exp) & 0xff;
exp = exp<<8; exp = exp << 8;
} }
if (offset == start && (m_dataType.numBits%8) != 0 && baseOffset + offset < output.size()) if (offset == start && (m_dataType.numBits % 8) != 0 && baseOffset + offset < output.size())
output[baseOffset + offset] |= ch; output[baseOffset + offset] |= ch;
else else
output[baseOffset + offset] = ch; output[baseOffset + offset] = ch;
@@ -608,9 +628,10 @@ result_t NumericDataField::writeRawValue(unsigned int value, unsigned char baseO
return RESULT_OK; return RESULT_OK;
} }
DataField* NumberDataField::derive(std::string name, PartType partType,
DataField* NumberDataField::derive(std::string name, PartType partType, unsigned char offset, std::string unit, unsigned char offset, std::string unit, std::string comment,
std::string comment, unsigned int divisor, std::map<unsigned int, std::string> values) { unsigned int divisor, std::map<unsigned int, std::string> values)
{
if (unit.empty() == true) if (unit.empty() == true)
unit = m_unit; unit = m_unit;
if (comment.empty() == true) 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); 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; unsigned int value = 0;
int signedValue; int signedValue;
@@ -636,34 +658,34 @@ result_t NumberDataField::readSymbols(SymbolString& input, unsigned char baseOff
output << NULL_VALUE; output << NULL_VALUE;
return RESULT_OK; 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 (m_dataType.numBits == 32) {
if (negative == false) { if (negative == false) {
if (m_divisor <= 1) if (m_divisor <= 1)
output << static_cast<unsigned>(value); output << static_cast<unsigned>(value);
else else
output << std::setprecision((m_dataType.numBits%8)==0 ? m_dataType.precisionOrFirstBit : 0) output << std::setprecision((m_dataType.numBits % 8) == 0 ? m_dataType.precisionOrFirstBit : 0)
<< std::fixed << static_cast<float>(value / (float)m_divisor); << std::fixed << static_cast<float>(value / (float) m_divisor);
return RESULT_OK; 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 else
if (negative) // negative signed value signedValue = (int) value;
signedValue = (int)value - (1 << m_dataType.numBits);
else
signedValue = (int)value;
if (m_divisor <= 1) if (m_divisor <= 1)
output << static_cast<int>(signedValue); output << static_cast<int>(signedValue);
else else
output << std::setprecision((m_dataType.numBits%8)==0 ? m_dataType.precisionOrFirstBit : 0) output << std::setprecision((m_dataType.numBits % 8) == 0 ? m_dataType.precisionOrFirstBit : 0)
<< std::fixed << static_cast<float>(signedValue / (float)m_divisor); << std::fixed << static_cast<float>(signedValue / (float) m_divisor);
return RESULT_OK; 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; unsigned int value;
@@ -677,58 +699,58 @@ result_t NumberDataField::writeSymbols(std::istringstream& input, unsigned char
else { else {
char* strEnd = NULL; char* strEnd = NULL;
if (m_divisor <= 1) { if (m_divisor <= 1) {
if ((m_dataType.flags&SIG) != 0) { if ((m_dataType.flags & SIG) != 0) {
int signedValue = strtol(str, &strEnd, 10); int signedValue = strtol(str, &strEnd, 10);
if (signedValue < 0 && m_dataType.numBits != 32) if (signedValue < 0 && m_dataType.numBits != 32)
value = (unsigned int)(signedValue + (1<<m_dataType.numBits)); value = (unsigned int) (signedValue + (1 << m_dataType.numBits));
else else
value = (unsigned int)signedValue; value = (unsigned int) signedValue;
} }
else else
value = strtoul(str, &strEnd, 10); value = strtoul(str, &strEnd, 10);
if (strEnd != str+len) if (strEnd != str + len)
return RESULT_ERR_INVALID_ARG; // invalid value return RESULT_ERR_INVALID_ARG; // invalid value
} }
else { else {
char* strEnd = NULL; char* strEnd = NULL;
double dvalue = strtod(str, &strEnd); double dvalue = strtod(str, &strEnd);
if (strEnd != str+len) if (strEnd != str + len)
return RESULT_ERR_INVALID_ARG; // invalid value return RESULT_ERR_INVALID_ARG; // invalid value
dvalue = round(dvalue * m_divisor); dvalue = round(dvalue * m_divisor);
if ((m_dataType.flags&SIG) != 0) { if ((m_dataType.flags & SIG) != 0) {
if (dvalue < -(1LL<<(8*m_length)) || dvalue >= (1LL<<(8*m_length))) if (dvalue < -(1LL << (8 * m_length)) || dvalue >= (1LL << (8 * m_length)))
return RESULT_ERR_INVALID_ARG; // value out of range return RESULT_ERR_INVALID_ARG; // value out of range
if (dvalue < 0 && m_dataType.numBits != 32) if (dvalue < 0 && m_dataType.numBits != 32)
value = (unsigned int)(dvalue + (1<<m_dataType.numBits)); value = (unsigned int) (dvalue + (1 << m_dataType.numBits));
else else
value = (unsigned int)dvalue; value = (unsigned int) dvalue;
} }
else { else {
if (dvalue < 0.0 || dvalue >= (1LL<<(8*m_length))) if (dvalue < 0.0 || dvalue >= (1LL << (8 * m_length)))
return RESULT_ERR_INVALID_ARG; // value out of range 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 & (1 << (m_dataType.numBits - 1))) != 0) { // negative signed value
if (value < m_dataType.minValueOrLength) 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) 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) else if (value < m_dataType.minValueOrLength || value > m_dataType.maxValueOrLength)
return RESULT_ERR_INVALID_ARG; // value out of range return RESULT_ERR_INVALID_ARG; // value out of range
} }
return writeRawValue(value, baseOffset, output); return writeRawValue(value, baseOffset, output);
} }
DataField* ValueListDataField::derive(std::string name, PartType partType,
DataField* ValueListDataField::derive(std::string name, PartType partType, unsigned char offset, std::string unit, unsigned char offset, std::string unit, std::string comment,
std::string comment, unsigned int divisor, std::map<unsigned int, std::string> values) { unsigned int divisor, std::map<unsigned int, std::string> values)
{
if (unit.empty() == true) if (unit.empty() == true)
unit = m_unit; unit = m_unit;
if (comment.empty() == true) 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); 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; 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 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(); 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 return RESULT_ERR_INVALID_ARG; // value assignment not found
} }
} //namespace } //namespace
+27 -15
View File
@@ -23,20 +23,25 @@
using namespace libebus; 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 (expectFailMatch == true) {
if (match == 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 else
std::cout << " failed " << type << " match >" << input << "< OK" << std::endl; std::cout << " failed " << type << " match >" << input << "< OK"
} else if (match == true) << std::endl;
}
else if (match == true)
std::cout << " " << type << " >" << input << "< OK" << std::endl; std::cout << " " << type << " >" << input << "< OK" << std::endl;
else else
std::cout << " " << type << " >" << input << "< error: got >" << gotStr std::cout << " " << type << " >" << input << "< error: got >" << gotStr
<< "<, expected >" << expectStr << "<" << std::endl; << "<, expected >" << expectStr << "<" << std::endl;
} }
int main () int main()
{ {
std::string checks[][5] = { std::string checks[][5] = {
//name;[pos];type[;[divisor|values][;[unit][;[comment]]]], decoded value, master, slave, flags //name;[pos];type[;[divisor|values][;[unit][;[comment]]]], decoded value, master, slave, flags
@@ -167,7 +172,7 @@ int main ()
}; };
std::map<std::string, DataField*> templates; std::map<std::string, DataField*> templates;
std::vector<DataField*> fields; std::vector<DataField*> 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::string check[5] = checks[i];
std::istringstream isstr(check[0]); std::istringstream isstr(check[0]);
std::string expectStr = check[1]; std::string expectStr = check[1];
@@ -199,7 +204,8 @@ int main ()
} while (result == RESULT_OK && it != entries.end()); } while (result == RESULT_OK && it != entries.end());
if (result != RESULT_OK) { 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; continue;
} }
if (fields.empty() == true) { if (fields.empty() == true) {
@@ -237,11 +243,14 @@ int main ()
result = field->read(mstr, sstr, output, verbose); result = field->read(mstr, sstr, output, verbose);
if (failedRead == true) if (failedRead == true)
if (result == RESULT_OK) 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 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) { 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; failed = true;
} }
else if (combinedValue == false) { else if (combinedValue == false) {
@@ -257,12 +266,15 @@ int main ()
result = field->write(tokeninput, writeMstr, writeSstr); result = field->write(tokeninput, writeMstr, writeSstr);
if (failedWrite == true) { if (failedWrite == true) {
if (result == RESULT_OK) 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 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) { 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; failed = true;
} }
} }
@@ -283,7 +295,7 @@ int main ()
delete fields.back(); delete fields.back();
fields.pop_back(); fields.pop_back();
} }
for (std::map<std::string, DataField*>::iterator it = templates.begin(); it!=templates.end(); it++) for (std::map<std::string, DataField*>::iterator it = templates.begin(); it != templates.end(); it++)
delete it->second; delete it->second;
return 0; return 0;