merge in latest libebus commit 9520d5a3283472c2a53b8396b0796723f660bed7:

started new DataField classes
This commit is contained in:
john30
2014-10-29 23:31:22 +01:00
parent 614af5984b
commit f798e8ee2e
5 changed files with 915 additions and 0 deletions
Regular → Executable
+2
View File
@@ -8,6 +8,8 @@ libebus_a_SOURCES = result.cpp \
result.h \
symbol.cpp \
symbol.h \
data.cpp \
data.h \
port.cpp \
port.h \
buscommand.cpp \
+575
View File
@@ -0,0 +1,575 @@
/*
* Copyright (C) John Baier 2014 <ebusd@johnm.de>
*
* This file is part of ebusd.
*
* ebusd is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ebusd is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ebusd. If not, see http://www.gnu.org/licenses/.
*/
#include "data.h"
#include "decode.h"
#include "encode.h"
#include <iostream>
#include <algorithm>
#include <sstream>
#include <iomanip>
#include <vector>
#include <cstring>
namespace libebus
{
static const char* days[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
DataField* DataField::create(const unsigned char dstAddress, const bool isSetMessage,
std::vector<std::string>::iterator& it, const std::vector<std::string>::iterator end) {
std::string name, unit, comment;
PartType partType;
float factor;
size_t baseOffset = 0, offset = 0, length = 0, maxPos = 16, offsetCnt = 0;
if (it == end)
return NULL;
name = *it++;
if (it == end)
return NULL;
const char* posStr = (*it++).c_str();
if (it == end)
return NULL;
if (posStr[0] >= 'a' && posStr[1] == 0) {
if (posStr[0] == 's') { // slave ACK
// QQ ZZ PB SB NN + Dx + CRC
//offset = 5+command.getMasterDataLength()+1; // skip QQ ZZ PB SB NN Dx CRC
} else if (posStr[0] == 'm') { // master ACK
//offset = 5+command.getMasterDataLength()+3+command.getSlaveDataLength()+1; // skip QQ ZZ PB SB NN Dx CRC ACK NN Dx CRC
} else {
return NULL; // TODO error code: invalid pos definition
}
} else {
if (dstAddress == BROADCAST
|| isMaster(dstAddress)
|| (isSetMessage == true && posStr[0] <= '9')
|| posStr[0] == 'm') { // master data
partType = pt_masterData;
baseOffset = 5; // skip QQ ZZ PB SB NN
//len = command.getMasterDataLength();
if (posStr[0] == 'm')
posStr++;
} else if ((isSetMessage == false && posStr[0] <= '9')
|| posStr[0] == 's') { // slave data
baseOffset = 1;
//offset = 5+command.getMasterDataLength()+3; // skip QQ ZZ PB SB NN Dx CRC ACK NN
//len = command.getSlaveDataLength();
if (posStr[0] == 's')
posStr++;
} else {
return NULL; // TODO error code: invalid pos definition
}
std::string token;
std::istringstream stream(posStr);
while (std::getline(stream, token, '-') != 0) {
if (++offsetCnt > 2)
return NULL; // TODO error code: invalid pos definition
const char* start = token.c_str();
char* end = NULL;
int pos = strtoul(start, &end, 10)-1; // 1-based
if (end != start+strlen(start))
return NULL; // TODO error code: invalid pos definition
if (pos < 0 || baseOffset+pos > maxPos)
return NULL; // TODO error code: invalid pos definition
else if (offsetCnt==1)
offset = baseOffset+pos;
else if (baseOffset+pos >= offset)
length = baseOffset+pos+1-offset;
else { // wrong order e.g. 4-3
length = offset-(baseOffset+pos+1);
offset = baseOffset+pos;
}
}
}
const char* typeStr = (*it++).c_str();
if (it == end)
factor = 1.0;
else {
std::string factorStr = *it++;
if (factorStr.length() > 0 && factorStr.find_first_not_of("0123456789.") == std::string::npos)
factor = static_cast<float>(strtod(factorStr.c_str(), NULL));
else
factor = 1.0;
}
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();
}
if (strcasecmp(typeStr, "STR") == 0) {
if (length == 0)
length = 1;
return new StringDataField(name, partType, offset, length, dt_string, unit, comment);
}
if (strcasecmp(typeStr, "HEX") == 0) {
if (length == 0)
length = 1;
return new StringDataField(name, partType, offset, length, dt_hex, unit, comment);
}
if (strcasecmp(typeStr, "UCH") == 0) {
if (length != 0 && length != 1)
return NULL; // TODO error code: invalid pos definition
return new NumericDataField(name, partType, offset, 1, dt_uchar, unit, comment, factor, 0xff);
}
if (strcasecmp(typeStr, "SCH") == 0) {
if (length != 0 && length != 1)
return NULL; // TODO error code: invalid pos definition
return new NumericDataField(name, partType, offset, 1, dt_schar, unit, comment, factor, 0x80);
}
if (strcasecmp(typeStr, "BCD") == 0) {
if (length != 0 && length != 1)
return NULL; // TODO error code: invalid pos definition
return new NumericDataField(name, partType, offset, 1, dt_bcd, unit, comment, factor, 0xff); // TODO max value 99
}
if (strcasecmp(typeStr, "D1B") == 0) {
if (length != 0 && length != 1)
return NULL; // TODO error code: invalid pos definition
return new NumericDataField(name, partType, offset, 1, dt_d1b, unit, comment, factor, 0x80);
}
if (strcasecmp(typeStr, "D1C") == 0) {
if (length != 0 && length != 1)
return NULL; // TODO error code: invalid pos definition
return new NumericDataField(name, partType, offset, 1, dt_d1c, unit, comment, factor*0.5, 0xff); // TODO max value 100
}
if (strcasecmp(typeStr, "UIN") == 0) {
if (length != 0 && length != 2)
return NULL; // TODO error code: invalid pos definition
return new NumericDataField(name, partType, offset, 2, dt_uint, unit, comment, factor, 0xffff);
}
if (strcasecmp(typeStr, "SIN") == 0) {
if (length != 0 && length != 2)
return NULL; // TODO error code: invalid pos definition
return new NumericDataField(name, partType, offset, 2, dt_sint, unit, comment, factor, 0x8000);
}
if (strcasecmp(typeStr, "D2B") == 0) {
if (length != 0 && length != 2)
return NULL; // TODO error code: invalid pos definition
return new NumericDataField(name, partType, offset, 2, dt_d2b, unit, comment, factor/256.0, 0x8000);
}
if (strcasecmp(typeStr, "D2C") == 0) {
if (length != 0 && length != 1)
return NULL; // TODO error code: invalid pos definition
return new NumericDataField(name, partType, offset, 2, dt_d2c, unit, comment, factor/16.0, 0x8000);
}
if (strcasecmp(typeStr, "ULG") == 0) {
if (length != 0 && length != 4)
return NULL; // TODO error code: invalid pos definition
return new NumericDataField(name, partType, offset, 4, dt_ulong, unit, comment, factor, 0xffffffff);
}
if (strcasecmp(typeStr, "SLG") == 0) {
if (length != 0 && length != 4)
return NULL; // TODO error code: invalid pos definition
return new NumericDataField(name, partType, offset, 4, dt_slong, unit, comment, factor, 0x80000000);
}
if (strcasecmp(typeStr, "FLT") == 0) {
if (length != 0 && length != 2)
return NULL; // TODO error code: invalid pos definition
return new NumericDataField(name, partType, offset, 2, dt_float, unit, comment, factor/1000.0, 0x8000); // TODO replacement
}
if (strcasecmp(typeStr, "BDA") == 0 || strcasecmp(typeStr, "HDA") == 0) {
if (length == 0)
length = 4;
else if (length != 3 && length != 4)
return NULL; // TODO error code: invalid pos definition
return new StringDataField(name, partType, offset, length, dt_date, unit, comment); // TODO better numeric?
}
if (strcasecmp(typeStr, "BDY") == 0) {
if (length != 0 && length != 1)
return NULL; // TODO error code: invalid pos definition
return new StringDataField(name, partType, offset, 1, dt_day, unit, comment); // TODO better numeric?
}
if (strcasecmp(typeStr, "BTI") == 0 || strcasecmp(typeStr, "HTI") == 0) {
if (length != 0 && length != 3)
return NULL; // TODO error code: invalid pos definition
return new StringDataField(name, partType, offset, 3, dt_time, unit, comment); // TODO better numeric?
}
if (strcasecmp(typeStr, "TTM") == 0) {
if (length != 0 && length != 1)
return NULL; // TODO error code: invalid pos definition
return new StringDataField(name, partType, offset, 1, dt_tTime, unit, comment); // TODO better numeric?
}
return NULL; // TODO error code: invalid type definition
}
const std::string DataField::parseSymbols(SymbolString& masterData, SymbolString& slaveData, bool verbose)
{
SymbolString& data = m_partType == pt_masterData ? masterData : slaveData;
switch (m_partType) {
case pt_masterData:
break;
case pt_slaveData:
break;
default:
return "invalid part type";
}
std::ostringstream output;
if (verbose)
output << m_name << "=";
if (parse(data, output) == false)
return "unable to parse";
if (verbose && m_unit.length() > 0)
output << " " << m_unit;
if (verbose && m_comment.length() > 0)
output << " [" << m_comment << "]";
return output.str();
}
bool DataField::formatSymbols(const std::string& value, SymbolString& masterData, SymbolString& slaveData)
{
SymbolString& data = m_partType == pt_masterData ? masterData : slaveData;
switch (m_partType) {
case pt_masterData:
break;
case pt_slaveData:
break;
default:
return false; // TODO error code
}
std::istringstream input(value);
if (format(data, input) == false)
return false; // TODO error code
return true;
}
bool StringDataField::parse(SymbolString& data, std::ostringstream& output)
{
size_t start = m_offset, end = m_offset+m_length;
unsigned char ch;
if (end > data.size()) {
return false; // TODO error not enough data available
}
if (m_dataType == dt_time) { // reverse order
for (size_t i = end-1; i >= start; i--) {
ch = data[i];
if ((ch&0xf0) > 0x90 || (ch&0x0f) > 0x09)
return false; // invalid BCD
if ((i == end-1 && ch > 0x23) || (i != end-1 && ch > 0x59))
return false; // invalid time
if (i != end-1)
output << ":";
output << std::setw(2) << std::setfill('0') << std::hex << static_cast<unsigned>(ch);
}
return true;
}
for (size_t i = start; i < end; i++) {
ch = data[i];
switch (m_dataType) {
case dt_hex:
if (i != start)
output << " ";
output << std::nouppercase << std::setw(2) << std::hex
<< std::setfill('0') << static_cast<unsigned>(ch);
break;
case dt_date:
if (m_length == 4u && i == m_offset+2u)
break; // skip weekday in between
if ((ch&0xf0) > 0x90 || (ch&0x0f) > 0x09)
return false; // invalid BCD
if (i == end-1)
output << std::hex << (0x2000+ch);
else if (ch < 0x01 || (i == start && ch > 0x31) || (i == start+1 && ch > 0x12))
return false; // invalid date
else
output << std::setw(2) << std::setfill('0') << std::hex << static_cast<unsigned>(ch) << ".";
break;
case dt_day:
if (ch < 1 || ch > 7)
return false; // invalid day
output << days[ch-1];
break;
case dt_tTime:
if (ch/6 > 23)
return false; // invalid time
output << std::setw(2) << std::setfill('0') << static_cast<unsigned>(ch/6) << ":"
<< std::setw(2) << std::setfill('0') << static_cast<unsigned>((ch%6)*10);
break;
default:
if (ch < 0x20)
ch = 0x20;
output << static_cast<char>(ch);
break;
}
}
return true;
}
bool StringDataField::format(SymbolString& data, std::istringstream& input)
{
size_t start = m_offset, end = m_offset+m_length;
const char* str;
char* strEnd;
unsigned long int value, hours;
unsigned char ch;
std::string token;
if (m_dataType == dt_time) { // reverse order
for (size_t i = end-1; i >=start; i--) {
if (std::getline(input, token, ':') == 0)
return false;
str = token.c_str();
strEnd = NULL;
value = strtoul(str, &strEnd, 16);
if (strEnd != str+strlen(str))
return false; // TODO error code: invalid value
if ((value&0xf0) > 0x90 || (value&0x0f) > 0x09)
return false; // invalid BCD
if ((i == end-1 && value > 0x23) || (i != end-1 && value > 0x59))
return false; // invalid time
data[i] = (unsigned char)value;
}
return true;
}
for (size_t i = start; i < end; i++) {
switch (m_dataType) {
case dt_hex:
while (input.peek()==' ')
input.get();
token.clear();
token.push_back(input.get());
if (input.eof() == true)
return false; // TODO error code: invalid value
token.push_back(input.get());
if (input.eof() == true)
return false; // TODO error code: invalid value
str = token.c_str();
strEnd = NULL;
value = strtoul(str, &strEnd, 16);
if (strEnd != str+strlen(str))
return false; // TODO error code: invalid value
data[i] = (unsigned char)value;
break;
case dt_date:
if (m_length == 4u && i == m_offset+2u)
break; // skip weekday in between
if (std::getline(input, token, '.') == 0)
return false;
str = token.c_str();
strEnd = NULL;
value = strtoul(str, &strEnd, 16);
if (strEnd != str+strlen(str))
return false; // TODO error code: invalid value
if ((value&0xf0) > 0x90 || (value&0x0f) > 0x09)
return false; // invalid BCD
if (i == end-1) {
if (value>=0x2000)
value -= 0x2000;
if (value>=0x100)
return false; // invalid year
}
else if (value < 1 || (i == start && value > 0x31) || (i == start+1 && value > 0x12))
return false; // invalid date
data[i] = (unsigned char)value;
break;
case dt_day:
str = input.str().c_str();
for (ch = 0; ch < 7; ch++)
if (strcasecmp(days[ch], str) == 0)
break;
if (ch == 7)
return false; // invalid day
data[i] = ch+1;
break;
case dt_tTime:
if (std::getline(input, token, ':') == 0)
return false;
str = token.c_str();
strEnd = NULL;
hours = strtoul(str, &strEnd, 10);
if (strEnd != str+strlen(str))
return false; // TODO error code: invalid value
if (hours > 23)
return false; // invalid time
if (std::getline(input, token, ':') == 0)
return false;
str = token.c_str();
strEnd = NULL;
value = strtoul(str, &strEnd, 10);
if (strEnd != str+strlen(str))
return false; // TODO error code: invalid value
if (value > 59 || (value%10) != 0)
return false; // invalid time
data[i] = (unsigned char)(hours*6 + value/10);
break;
default:
ch = input.get();
if (input.eof() == true || ch < 0x20)
ch = 0x20;
data[i] = ch;
break;
}
}
return true;
}
bool NumericDataField::parse(SymbolString& data, std::ostringstream& output)
{
size_t start = m_offset, end = m_offset+m_length;
unsigned int value = 0;
int signedValue;
unsigned char ch;
if (end > data.size())
return false; // TODO error not enough data available
for (size_t i = start, exp = 1; i < end; i++) {
ch = data[i];
switch (m_dataType) {
case dt_bcd:
if (ch == m_replacement) {
output << "-";
return true;
}
else if ((ch&0xf0) > 0x90 || (ch&0x0f) > 0x09)
return false; // invalid BCD
else
value |= ((ch>>4)*10 + (ch&0x0f))*exp;
exp = exp*100;
break;
default:
value |= ch*exp;
exp = exp<<8;
break;
}
}
if (value == m_replacement) {
output << "-";
return true;
}
switch (m_dataType) {
case dt_schar:
case dt_d1b:
signedValue = (char)value;
break;
case dt_d2b:
case dt_d2c:
case dt_sint:
signedValue = (short)value;
break;
case dt_slong:
signedValue = (int)value;
break;
case dt_ulong:
if (m_factor == 1.0)
output << std::setprecision(0) << std::fixed << static_cast<float>(value);
else
output << std::setprecision(3) << std::fixed << static_cast<float>(value * m_factor);
return true;
default:
signedValue = value;
break;
}
if (m_factor == 1.0)
output << std::setprecision(0) << std::fixed << static_cast<float>(signedValue);
else
output << std::setprecision(3) << std::fixed << static_cast<float>(signedValue * m_factor);
return true;
}
bool NumericDataField::format(SymbolString& data, std::istringstream& input)
{
size_t start = m_offset, end = m_offset+m_length;
unsigned int value;
unsigned char ch;
const char* str = input.str().c_str();
if (strcasecmp(str, "-") == 0)
// replacement value
value = m_replacement;
else {
char* strEnd = NULL;
double dvalue = strtod(str, &strEnd);
if (strEnd != str+strlen(str))
return false; // TODO error code: invalid value
dvalue /= m_factor;
switch (m_dataType) {
case dt_schar:
case dt_sint:
case dt_slong:
case dt_d1b:
case dt_d2b:
case dt_d2c:
if (dvalue < -(1LL<<(8*m_length)) || dvalue >= (1LL<<(8*m_length)))
return false; // TODO error code: invalid value
break;
default:
// no special handling
if (dvalue < 0.0 || dvalue >= (1LL<<(8*m_length)))
return false; // TODO error code: invalid value
break;
}
value = (unsigned int)dvalue;
}
for (size_t i = start, exp = 1; i < end; i++) {
switch (m_dataType) {
case dt_bcd:
if (value == m_replacement)
ch = m_replacement;
else {
ch = (value/exp)%100;
ch = ((ch/10)<<4) | (ch%10);
}
exp = exp*100;
break;
default:
ch = (value/exp)&0xff;
exp = exp<<8;
break;
}
data[i] = ch;
}
return true;
}
} //namespace
+226
View File
@@ -0,0 +1,226 @@
/*
* Copyright (C) John Baier 2014 <ebusd@johnm.de>
*
* This file is part of ebusd.
*
* ebusd is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ebusd is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ebusd. If not, see http://www.gnu.org/licenses/.
*/
#ifndef LIBEBUS_DATA_H_
#define LIBEBUS_DATA_H_
#include "symbol.h"
#include <string>
#include <vector>
namespace libebus
{
/** the message part in which a data field is stored. */
enum PartType {
pt_masterData, // stored in master data
//pt_slaveAck, // stored in slave acknowledge // TODO implement if reasonable
pt_slaveData, // stored in slave data
//pt_masterAck // stored in master acknowledge // TODO implement if reasonable
};
/** the available data types. */
enum DataType {
dt_string, // string of characters with fixed length>0, filled up with space
dt_hex, // string of hex digits with fixed length>0, separated by space
dt_bcd, // length: 1 byte, binary coded number, string format: "%d"
dt_uchar,
dt_schar,
dt_uint,
dt_sint,
dt_ulong,
dt_slong,
dt_float,
dt_d1b,
dt_d1c,
dt_d2b,
dt_d2c,
dt_date, // length: 4 bytes (with skipped weekday) or 3 bytes (without weekday), string format: "dd.mm.yyyy"
dt_day, // length: 1 byte, string format: "www" (3 character weekday name)
dt_time, // length: 3 bytes, string format: "hh:mm"
dt_tTime // length: 1 byte, string format: "hh:mm"
};
/**
* @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 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.
* @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 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) {}
/**
* @brief Destructor.
*/
virtual ~DataField() {}
/**
* @brief Factory method for creating a new instance.
* @param dstAddress the destination bus address.
* @param isSetMessage whther the field is part of a set message.
* @param it the iterator to traverse for the definition parts.
* @param end the iterator pointing to the end of the definition parts.
*/
static DataField* create(const unsigned char dstAddress, const bool isSetMessage,
std::vector<std::string>::iterator& it, const std::vector<std::string>::iterator end);
/**
* @brief Parses the value from the master or slave @a SymbolString.
* @param masterData the unescaped master data @a SymbolString to parse from.
* @param slaveData the unescaped slave data @a SymbolString to parse from.
* @return the parsed value as string.
*/
const std::string parseSymbols(SymbolString& masterData, SymbolString& slaveData, bool verbose=false);
/**
* @brief Formats the value to the master or slave @a SymbolString.
* @param masterData the unescaped master data @a SymbolString to format to.
* @param slaveData the unescaped slave data @a SymbolString to format to.
* @param value the value as string.
*/
bool formatSymbols(const std::string& value, SymbolString& masterData, SymbolString& slaveData);
protected:
/**
* @brief Internal method doing the actual parse for the individual data type.
* @param data the unescaped data SymbolString to parse from.
* @param output the ostringstream to append to.
* @return true if the value was parsed successfully.
*/
virtual bool parse(SymbolString& data, std::ostringstream& output) = 0;
/**
* @brief Internal method doing the actual format for the individual data type.
* @param data the unescaped data SymbolString to format to.
* @param input the istringstream to interpret.
* @return true if the value was formatted successfully.
*/
virtual bool format(SymbolString& data, std::istringstream& input) = 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. */
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. */
const DataType m_dataType;
//std::vector<std::string> m_valid;//TODO add list of possible values
/** 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
{
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 length the number of symbols in the message part in which the field is stored.
* @param dataType the data type.
* @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 dataType, const std::string unit,
const std::string comment)
: DataField(name, partType, offset, length, dataType, unit, comment) {}
/**
* @brief Destructor.
*/
virtual ~StringDataField() {}
protected:
virtual bool parse(SymbolString& data, std::ostringstream& output);
virtual bool format(SymbolString& data, std::istringstream& input);
};
/**
* @brief Base class for all numeric data fields.
*/
class NumericDataField : public 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 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.
* @param comment the field comment.
* @param unit the value unit.
* @param factor the factor to apply on the value.
* @param replacement the (binary) replacement value to use if the value is not set.
*/
NumericDataField(const std::string name, const PartType partType,
const unsigned char offset, const unsigned char length,
const DataType dataType, const std::string unit,
const std::string comment, const float factor,
const unsigned int replacement)
: DataField(name, partType, offset, length, dataType, unit, comment),
m_factor(factor), m_replacement(replacement) {}
/**
* @brief Destructor.
*/
virtual ~NumericDataField() {}
protected:
virtual bool parse(SymbolString& data, std::ostringstream& output);
virtual bool format(SymbolString& data, std::istringstream& input);
/** the factor to apply on the value. */
const float m_factor;
/** the (binary) replacement value to use if the value is not set. */
const unsigned int m_replacement;
};
} //namespace
#endif // LIBEBUS_DATA_H_
Regular → Executable
+4
View File
@@ -5,6 +5,7 @@ AM_CXXFLAGS = -fpic \
noinst_PROGRAMS = test_port \
test_symbol \
test_data \
test_bus \
test_commands \
test_configfile \
@@ -17,6 +18,9 @@ test_port_LDADD = $(top_srcdir)/src/libebus/libebus.a
test_symbol_SOURCES = test_symbol.cpp
test_symbol_LDADD = $(top_srcdir)/src/libebus/libebus.a
test_data_SOURCES = test_data.cpp
test_data_LDADD = $(top_srcdir)/src/libebus/libebus.a
test_bus_SOURCES = test_bus.cpp
test_bus_LDADD = $(top_srcdir)/src/libebus/libebus.a
+108
View File
@@ -0,0 +1,108 @@
/*
* Copyright (C) John Baier 2014 <ebusd@johnm.de>
*
* This file is part of libebus.
*
* libebus is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libebus is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with libebus. If not, see http://www.gnu.org/licenses/.
*/
#include "data.h"
#include <iostream>
#include <iomanip>
using namespace libebus;
int main ()
{
//TODO dt_float,
//TODO dt_d1b,
//TODO dt_d1c,
//TODO dt_d2c,
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"},
{"hex;1-10;hex","53 70 65 69 63 68 65 72 20 20", "10fe07000a53706569636865722020", "00"},
{"zeit;1;bti","21:04:58","10fe070009580421", "00"},
{"datum;1;bda","26.10.2014","10fe07000926100714", "00"},
{"datum;1-3;bda","26.10.2014","10fe070003261014", "00"},
{"tag;1;bdy","Sun","10fe07000307", "00"},
{"temp;1;d2b","18.004","10fe0700090112", "00"},
{"zeit;1;ttm","22:40","10feffff0188", "00"},
{"bcd;1;bcd","26","10feffff0126", "00"},
{"bcd;1;bcd","-","10feffff01ff", "00"},
{"uch;1;uch","38","10feffff0126", "00"},
{"sch;1;sch","-90","10feffff01a6", "00"},
{"uin;1;uin","38","10feffff022600", "00"},
{"sin;1;sin","-90","10feffff02a6ff", "00"},
{"ulg;1;ulg","38","10feffff0426000000", "00"},
{"slg;1;slg","-90","10feffff04a6ffffff", "00"},
{"str;1-9;str","hallo Du!","10feffff0868616c6c6f20447521", "00"},
{"str;1-9;str","hallo Du ","10feffff0868616c6c6f20447500", "00"},
};
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 item;
std::vector<std::string> entries;
while (std::getline(isstr, item, ';') != 0)
entries.push_back(item);
std::vector<std::string>::iterator it = entries.begin();
DataField* field = DataField::create(mstr[1], false, it, entries.end());
if (field == NULL) {
std::cout << "create \"" << checks[i][0] << "\" invalid: null" << std::endl;
return 1;
}
std::cout << "create \"" << checks[i][0] << "\" successful" << std::endl;
std::string gotStr = field->parseSymbols(mstr, sstr);
if (strcasecmp(gotStr.c_str(), expectStr.c_str()) == 0)
std::cout << "parse successful: " << gotStr << std::endl;
else
std::cout << "parse 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 (field->formatSymbols(gotStr, writeMstr, writeSstr) == false)
std::cout << "format failed" << std::endl;
else {
if (mstr == writeMstr && sstr == writeSstr)
std::cout << "format successful" << std::endl;
else {
std::cout << "format 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 field;
}
return 0;
}