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

This commit is contained in:
john30
2014-11-16 10:49:47 +01:00
parent 72fcd8623d
commit 63cde34e6a
3 changed files with 580 additions and 282 deletions
+59 -38
View File
@@ -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<std::string>::iterator& it, const std::vector<std::string>::iterator end,
const std::map<std::string, DataField*> predefined, std::vector<DataField*>& fields,
unsigned char& nextPos);
static result_t create(std::vector<std::string>::iterator& it, const std::vector<std::string>::iterator end,
const std::map<std::string, DataField*> templates, std::vector<DataField*>& 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<unsigned int, std::string> 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<unsigned int, std::string> 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<unsigned int, std::string> 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<unsigned int, std::string> 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<unsigned int, std::string> 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<unsigned int, std::string> m_values;