using namespace std;

This commit is contained in:
john30
2014-11-22 20:56:53 +01:00
parent d97bc1bc34
commit 3a34267bee
10 changed files with 294 additions and 277 deletions
+68 -67
View File
@@ -24,9 +24,10 @@
#include <iomanip> #include <iomanip>
#include <vector> #include <vector>
#include <cstring> #include <cstring>
//include <typeinfo>
#include <math.h> #include <math.h>
using namespace std;
/** the known data field types. */ /** the known data field types. */
static const dataType_t dataTypes[] = { static const dataType_t dataTypes[] = {
{"IGN",16*8,bt_str, IGN|ADJ, 0, 1, 16, 0, 0}, // >= 1 byte ignored data {"IGN",16*8,bt_str, IGN|ADJ, 0, 1, 16, 0, 0}, // >= 1 byte ignored data
@@ -94,26 +95,26 @@ unsigned int parseInt(const char* str, int base, const unsigned int minValue, co
return ret; return ret;
} }
result_t DataField::create(std::vector<std::string>::iterator& it, result_t DataField::create(vector<string>::iterator& it,
const std::vector<std::string>::iterator end, const vector<string>::iterator end,
const std::map< std::string, DataField*> templates, const map< string, DataField*> templates,
DataField*& returnField, const bool isSetMessage, DataField*& returnField, const bool isSetMessage,
const unsigned char dstAddress) const unsigned char dstAddress)
{ {
std::vector<SingleDataField*> fields; vector<SingleDataField*> fields;
std::string firstName, firstComment; string firstName, firstComment;
result_t result = RESULT_OK; result_t result = RESULT_OK;
while (it != end && result == RESULT_OK) { while (it != end && result == RESULT_OK) {
std::string unit, comment; string unit, comment;
PartType partType; PartType partType;
unsigned int divisor = 0; unsigned int divisor = 0;
const bool isTemplate = dstAddress == SYN; const bool isTemplate = dstAddress == SYN;
std::string token; string token;
if (it == end) if (it == end)
break; break;
// name;part;type[:len][;[divisor|values][;[unit][;[comment]]]] // name;part;type[:len][;[divisor|values][;[unit][;[comment]]]]
const std::string name = *it++; const string name = *it++;
if (it == end) if (it == end)
break; break;
@@ -145,25 +146,25 @@ result_t DataField::create(std::vector<std::string>::iterator& it,
break; break;
} }
std::string typeStr = *it++; string typeStr = *it++;
if (typeStr.empty() == true) { if (typeStr.empty() == true) {
if (name.empty() == false || partStr[0] != 0) if (name.empty() == false || partStr[0] != 0)
result = RESULT_ERR_INVALID_ARG; result = RESULT_ERR_INVALID_ARG;
break; break;
} }
std::map<unsigned int, std::string> values; map<unsigned int, string> values;
if (it != end) { if (it != end) {
std::string divisorStr = *it++; string divisorStr = *it++;
if (divisorStr.empty() == false) { if (divisorStr.empty() == false) {
if (divisorStr.find('=') == std::string::npos) { if (divisorStr.find('=') == string::npos) {
divisor = parseInt(divisorStr.c_str(), 10, 1, 10000, result); divisor = parseInt(divisorStr.c_str(), 10, 1, 10000, result);
if (result != RESULT_OK) if (result != RESULT_OK)
break; break;
} }
else { else {
std::istringstream stream(divisorStr); istringstream stream(divisorStr);
while (std::getline(stream, token, VALUE_SEPARATOR) != 0) { while (getline(stream, token, VALUE_SEPARATOR) != 0) {
const char* str = token.c_str(); const char* str = token.c_str();
char* strEnd = NULL; char* strEnd = NULL;
unsigned int id = strtoul(str, &strEnd, 10); unsigned int id = strtoul(str, &strEnd, 10);
@@ -172,7 +173,7 @@ result_t DataField::create(std::vector<std::string>::iterator& it,
break; break;
} }
values[id] = std::string(strEnd + 1); values[id] = string(strEnd + 1);
} }
if (result != RESULT_OK) if (result != RESULT_OK)
break; break;
@@ -198,15 +199,15 @@ result_t DataField::create(std::vector<std::string>::iterator& it,
size_t pos = typeStr.find(LENGTH_SEPARATOR); size_t pos = typeStr.find(LENGTH_SEPARATOR);
unsigned char length; unsigned char length;
if (pos == std::string::npos) { if (pos == string::npos) {
length = 0; length = 0;
// check for reference(s) to templates // check for reference(s) to templates
if (templates.empty() == false) { if (templates.empty() == false) {
std::istringstream stream(typeStr); istringstream stream(typeStr);
bool found = false; bool found = false;
std::string lengthStr; string lengthStr;
while (std::getline(stream, token, VALUE_SEPARATOR) != 0) { while (getline(stream, token, VALUE_SEPARATOR) != 0) {
std::map<std::string, DataField*>::const_iterator ref = templates.find(token); map<string, DataField*>::const_iterator ref = templates.find(token);
if (ref == templates.end()) { if (ref == templates.end()) {
if (found == false) if (found == false)
break; // fallback to direct definition break; // fallback to direct definition
@@ -325,7 +326,7 @@ result_t DataField::create(std::vector<std::string>::iterator& it,
result_t SingleDataField::read(SymbolString& masterData, unsigned char masterOffset, result_t SingleDataField::read(SymbolString& masterData, unsigned char masterOffset,
SymbolString& slaveData, unsigned char slaveOffset, SymbolString& slaveData, unsigned char slaveOffset,
std::ostringstream& output, ostringstream& output,
bool verbose, char separator) bool verbose, char separator)
{ {
SymbolString& input = m_partType != pt_slaveData ? masterData : slaveData; SymbolString& input = m_partType != pt_slaveData ? masterData : slaveData;
@@ -365,7 +366,7 @@ result_t SingleDataField::read(SymbolString& masterData, unsigned char masterOff
return RESULT_OK; return RESULT_OK;
} }
result_t SingleDataField::write(std::istringstream& input, result_t SingleDataField::write(istringstream& input,
SymbolString& masterData, unsigned char masterOffset, SymbolString& masterData, unsigned char masterOffset,
SymbolString& slaveData, unsigned char slaveOffset, SymbolString& slaveData, unsigned char slaveOffset,
char separator) char separator)
@@ -388,10 +389,10 @@ result_t SingleDataField::write(std::istringstream& input,
} }
result_t StringDataField::derive(std::string name, std::string comment, result_t StringDataField::derive(string name, string comment,
std::string unit, const PartType partType, string unit, const PartType partType,
unsigned int divisor, std::map<unsigned int, std::string> values, unsigned int divisor, map<unsigned int, string> values,
std::vector<SingleDataField*>& fields) vector<SingleDataField*>& fields)
{ {
if (m_partType != pt_any && partType == pt_any) if (m_partType != pt_any && partType == pt_any)
return RESULT_ERR_INVALID_ARG; // cannot create a template from a concrete instance return RESULT_ERR_INVALID_ARG; // cannot create a template from a concrete instance
@@ -410,7 +411,7 @@ result_t StringDataField::derive(std::string name, std::string comment,
} }
result_t StringDataField::readSymbols(SymbolString& input, result_t StringDataField::readSymbols(SymbolString& input,
unsigned char baseOffset, std::ostringstream& output) unsigned char baseOffset, ostringstream& output)
{ {
size_t start = 0, count = m_length; size_t start = 0, count = m_length;
int incr = 1; int incr = 1;
@@ -439,8 +440,8 @@ result_t StringDataField::readSymbols(SymbolString& input,
case bt_hexstr: case bt_hexstr:
if (i > 0) if (i > 0)
output << ' '; output << ' ';
output << std::nouppercase << std::setw(2) << std::hex output << nouppercase << setw(2) << hex << setfill('0')
<< std::setfill('0') << static_cast<unsigned>(ch); << static_cast<unsigned>(ch);
break; break;
case bt_dat: case bt_dat:
if (i + 1 == m_length) if (i + 1 == m_length)
@@ -448,7 +449,7 @@ result_t StringDataField::readSymbols(SymbolString& input,
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
output << std::setw(2) << std::setfill('0') << static_cast<unsigned>(ch) << "."; output << setw(2) << setfill('0') << static_cast<unsigned>(ch) << ".";
break; break;
case bt_tim: case bt_tim:
if (m_length == 1) { // truncated time if (m_length == 1) { // truncated time
@@ -464,7 +465,7 @@ result_t StringDataField::readSymbols(SymbolString& input,
return RESULT_ERR_INVALID_ARG; // invalid time return RESULT_ERR_INVALID_ARG; // invalid time
if (i > 0) if (i > 0)
output << ":"; output << ":";
output << std::setw(2) << std::setfill('0') << static_cast<unsigned>(ch); output << setw(2) << setfill('0') << static_cast<unsigned>(ch);
break; break;
default: default:
if (ch < 0x20) if (ch < 0x20)
@@ -478,13 +479,13 @@ result_t StringDataField::readSymbols(SymbolString& input,
return RESULT_OK; return RESULT_OK;
} }
result_t StringDataField::writeSymbols(std::istringstream& input, result_t StringDataField::writeSymbols(istringstream& input,
unsigned char baseOffset, SymbolString& output) unsigned char baseOffset, SymbolString& output)
{ {
size_t start = 0, count = m_length; size_t start = 0, count = m_length;
int incr = 1; int incr = 1;
unsigned long int value = 0, last = 0, lastLast = 0; unsigned long int value = 0, last = 0, lastLast = 0;
std::string token; 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)
start = m_length - 1; start = m_length - 1;
@@ -524,7 +525,7 @@ result_t StringDataField::writeSymbols(std::istringstream& input,
case bt_dat: case bt_dat:
if (m_length == 4 && i == 2) if (m_length == 4 && i == 2)
continue; // skip weekday in between continue; // skip weekday in between
if (input.eof() == true || std::getline(input, token, '.') == 0) if (input.eof() == true || getline(input, token, '.') == 0)
return RESULT_ERR_INVALID_ARG; // incomplete return RESULT_ERR_INVALID_ARG; // incomplete
value = parseInt(token.c_str(), 10, 0, 2099, result); value = parseInt(token.c_str(), 10, 0, 2099, result);
if (result != RESULT_OK) if (result != RESULT_OK)
@@ -555,7 +556,7 @@ result_t StringDataField::writeSymbols(std::istringstream& input,
return RESULT_ERR_INVALID_ARG; // invalid date part return RESULT_ERR_INVALID_ARG; // invalid date part
break; break;
case bt_tim: case bt_tim:
if (input.eof() == true || std::getline(input, token, LENGTH_SEPARATOR) == 0) if (input.eof() == true || getline(input, token, LENGTH_SEPARATOR) == 0)
return RESULT_ERR_INVALID_ARG; // incomplete return RESULT_ERR_INVALID_ARG; // incomplete
value = parseInt(token.c_str(), 10, 0, 59, result); value = parseInt(token.c_str(), 10, 0, 59, result);
if (result != RESULT_OK) if (result != RESULT_OK)
@@ -698,10 +699,10 @@ result_t NumericDataField::writeRawValue(unsigned int value,
} }
result_t NumberDataField::derive(std::string name, std::string comment, result_t NumberDataField::derive(string name, string comment,
std::string unit, const PartType partType, string unit, const PartType partType,
unsigned int divisor, std::map<unsigned int, std::string> values, unsigned int divisor, map<unsigned int, string> values,
std::vector<SingleDataField*>& fields) vector<SingleDataField*>& fields)
{ {
if (m_partType != pt_any && partType == pt_any) if (m_partType != pt_any && partType == pt_any)
return RESULT_ERR_INVALID_ARG; // cannot create a template from a concrete instance return RESULT_ERR_INVALID_ARG; // cannot create a template from a concrete instance
@@ -728,7 +729,7 @@ result_t NumberDataField::derive(std::string name, std::string comment,
} }
result_t NumberDataField::readSymbols(SymbolString& input, result_t NumberDataField::readSymbols(SymbolString& input,
unsigned char baseOffset, std::ostringstream& output) unsigned char baseOffset, ostringstream& output)
{ {
unsigned int value = 0; unsigned int value = 0;
int signedValue; int signedValue;
@@ -748,8 +749,8 @@ result_t NumberDataField::readSymbols(SymbolString& input,
if (m_divisor <= 1) if (m_divisor <= 1)
output << static_cast<unsigned>(value); output << static_cast<unsigned>(value);
else else
output << std::setprecision((m_bitCount % 8) == 0 ? m_dataType.precisionOrFirstBit : 0) output << setprecision((m_bitCount % 8) == 0 ? m_dataType.precisionOrFirstBit : 0)
<< std::fixed << static_cast<float>(value / (float) m_divisor); << 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
@@ -762,13 +763,13 @@ result_t NumberDataField::readSymbols(SymbolString& input,
if (m_divisor <= 1) if (m_divisor <= 1)
output << static_cast<int>(signedValue); output << static_cast<int>(signedValue);
else else
output << std::setprecision((m_bitCount % 8) == 0 ? m_dataType.precisionOrFirstBit : 0) output << setprecision((m_bitCount % 8) == 0 ? m_dataType.precisionOrFirstBit : 0)
<< std::fixed << static_cast<float>(signedValue / (float) m_divisor); << fixed << static_cast<float>(signedValue / (float) m_divisor);
return RESULT_OK; return RESULT_OK;
} }
result_t NumberDataField::writeSymbols(std::istringstream& input, result_t NumberDataField::writeSymbols(istringstream& input,
unsigned char baseOffset, SymbolString& output) unsigned char baseOffset, SymbolString& output)
{ {
unsigned int value; unsigned int value;
@@ -830,10 +831,10 @@ result_t NumberDataField::writeSymbols(std::istringstream& input,
} }
result_t ValueListDataField::derive(std::string name, std::string comment, result_t ValueListDataField::derive(string name, string comment,
std::string unit, const PartType partType, string unit, const PartType partType,
unsigned int divisor, std::map<unsigned int, std::string> values, unsigned int divisor, map<unsigned int, string> values,
std::vector<SingleDataField*>& fields) vector<SingleDataField*>& fields)
{ {
if (m_partType != pt_any && partType == pt_any) if (m_partType != pt_any && partType == pt_any)
return RESULT_ERR_INVALID_ARG; // cannot create a template from a concrete instance return RESULT_ERR_INVALID_ARG; // cannot create a template from a concrete instance
@@ -860,7 +861,7 @@ result_t ValueListDataField::derive(std::string name, std::string comment,
} }
result_t ValueListDataField::readSymbols(SymbolString& input, result_t ValueListDataField::readSymbols(SymbolString& input,
unsigned char baseOffset, std::ostringstream& output) unsigned char baseOffset, ostringstream& output)
{ {
unsigned int value = 0; unsigned int value = 0;
@@ -868,7 +869,7 @@ result_t ValueListDataField::readSymbols(SymbolString& input,
if (result != RESULT_OK) if (result != RESULT_OK)
return result; return result;
std::map<unsigned int, std::string>::iterator it = m_values.find(value); map<unsigned int, string>::iterator it = m_values.find(value);
if (it != m_values.end()) { if (it != m_values.end()) {
output << it->second; output << it->second;
return RESULT_OK; return RESULT_OK;
@@ -882,7 +883,7 @@ result_t ValueListDataField::readSymbols(SymbolString& input,
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, result_t ValueListDataField::writeSymbols(istringstream& input,
unsigned char baseOffset, SymbolString& output) unsigned char baseOffset, SymbolString& output)
{ {
if (isIgnored() == true) if (isIgnored() == true)
@@ -890,7 +891,7 @@ result_t ValueListDataField::writeSymbols(std::istringstream& input,
const char* str = input.str().c_str(); const char* str = input.str().c_str();
for (std::map<unsigned int, std::string>::iterator it = m_values.begin(); it != m_values.end(); it++) for (map<unsigned int, string>::iterator it = m_values.begin(); it != m_values.end(); it++)
if (it->second.compare(str) == 0) if (it->second.compare(str) == 0)
return writeRawValue(it->first, baseOffset, output); return writeRawValue(it->first, baseOffset, output);
@@ -914,7 +915,7 @@ unsigned char DataFieldSet::getLength(PartType partType)
bool previousFullByteOffset[] = { true, true, true, true }; bool previousFullByteOffset[] = { true, true, true, true };
for (std::vector<SingleDataField*>::iterator it = m_fields.begin(); it < m_fields.end(); it++) { for (vector<SingleDataField*>::iterator it = m_fields.begin(); it < m_fields.end(); it++) {
SingleDataField* field = *it; SingleDataField* field = *it;
if (field->getPartType() == partType) { if (field->getPartType() == partType) {
if (previousFullByteOffset[partType] == false && field->hasFullByteOffset(false) == false) if (previousFullByteOffset[partType] == false && field->hasFullByteOffset(false) == false)
@@ -929,15 +930,15 @@ unsigned char DataFieldSet::getLength(PartType partType)
return length; return length;
} }
result_t DataFieldSet::derive(std::string name, std::string comment, result_t DataFieldSet::derive(string name, string comment,
std::string unit, const PartType partType, string unit, const PartType partType,
unsigned int divisor, std::map<unsigned int, std::string> values, unsigned int divisor, map<unsigned int, string> values,
std::vector<SingleDataField*>& fields) vector<SingleDataField*>& fields)
{ {
if (values.empty() == false) if (values.empty() == false)
return RESULT_ERR_INVALID_ARG; // value list not allowed in set derive return RESULT_ERR_INVALID_ARG; // value list not allowed in set derive
for (std::vector<SingleDataField*>::iterator it = m_fields.begin(); it < m_fields.end(); it++) { for (vector<SingleDataField*>::iterator it = m_fields.begin(); it < m_fields.end(); it++) {
result_t result = (*it)->derive("", "", "", partType, divisor, values, fields); result_t result = (*it)->derive("", "", "", partType, divisor, values, fields);
if (result != RESULT_OK) if (result != RESULT_OK)
return result; return result;
@@ -948,7 +949,7 @@ result_t DataFieldSet::derive(std::string name, std::string comment,
result_t DataFieldSet::read(SymbolString& masterData, unsigned char masterOffset, result_t DataFieldSet::read(SymbolString& masterData, unsigned char masterOffset,
SymbolString& slaveData, unsigned char slaveOffset, SymbolString& slaveData, unsigned char slaveOffset,
std::ostringstream& output, bool verbose, char separator) ostringstream& output, bool verbose, char separator)
{ {
if (verbose) if (verbose)
output << m_name << "={ "; output << m_name << "={ ";
@@ -959,7 +960,7 @@ result_t DataFieldSet::read(SymbolString& masterData, unsigned char masterOffset
offsets[pt_masterData] = masterOffset; offsets[pt_masterData] = masterOffset;
offsets[pt_slaveData] = slaveOffset; offsets[pt_slaveData] = slaveOffset;
bool previousFullByteOffset[] = { true, true, true, true }; bool previousFullByteOffset[] = { true, true, true, true };
for (std::vector<SingleDataField*>::iterator it = m_fields.begin(); it < m_fields.end(); it++) { for (vector<SingleDataField*>::iterator it = m_fields.begin(); it < m_fields.end(); it++) {
SingleDataField* field = *it; SingleDataField* field = *it;
bool ignored = field->isIgnored(); bool ignored = field->isIgnored();
PartType partType = field->getPartType(); PartType partType = field->getPartType();
@@ -994,19 +995,19 @@ result_t DataFieldSet::read(SymbolString& masterData, unsigned char masterOffset
return RESULT_OK; return RESULT_OK;
} }
result_t DataFieldSet::write(std::istringstream& input, result_t DataFieldSet::write(istringstream& input,
SymbolString& masterData, unsigned char masterOffset, SymbolString& masterData, unsigned char masterOffset,
SymbolString& slaveData, unsigned char slaveOffset, SymbolString& slaveData, unsigned char slaveOffset,
char separator) char separator)
{ {
std::string token; string token;
unsigned char offsets[4]; unsigned char offsets[4];
memset(offsets, 0, sizeof(offsets)); memset(offsets, 0, sizeof(offsets));
offsets[pt_masterData] = masterOffset; offsets[pt_masterData] = masterOffset;
offsets[pt_slaveData] = slaveOffset; offsets[pt_slaveData] = slaveOffset;
bool previousFullByteOffset[] = { true, true, true, true }; bool previousFullByteOffset[] = { true, true, true, true };
for (std::vector<SingleDataField*>::iterator it = m_fields.begin(); it < m_fields.end(); it++) { for (vector<SingleDataField*>::iterator it = m_fields.begin(); it < m_fields.end(); it++) {
SingleDataField* field = *it; SingleDataField* field = *it;
bool ignored = field->isIgnored(); bool ignored = field->isIgnored();
PartType partType = field->getPartType(); PartType partType = field->getPartType();
@@ -1020,10 +1021,10 @@ result_t DataFieldSet::write(std::istringstream& input,
if (m_fields.size() > 1) { if (m_fields.size() > 1) {
if (ignored == true) if (ignored == true)
token.clear(); token.clear();
else if (std::getline(input, token, separator) == 0) else if (getline(input, token, separator) == 0)
return RESULT_ERR_INVALID_ARG; // incomplete return RESULT_ERR_INVALID_ARG; // incomplete
std::istringstream single(token); istringstream single(token);
result = (*it)->write(single, masterData, offsets[pt_masterData], slaveData, offsets[pt_slaveData], separator); result = (*it)->write(single, masterData, offsets[pt_masterData], slaveData, offsets[pt_slaveData], separator);
} }
else else
+67 -65
View File
@@ -26,6 +26,8 @@
#include <vector> #include <vector>
#include <map> #include <map>
using namespace std;
/** the message part in which a data field is stored. */ /** the message part in which a data field is stored. */
enum PartType { enum PartType {
pt_any, // stored in any data (master or slave) pt_any, // stored in any data (master or slave)
@@ -92,7 +94,7 @@ public:
* @param name the field name. * @param name the field name.
* @param comment the field comment. * @param comment the field comment.
*/ */
DataField(const std::string name, const std::string comment) DataField(const string name, const string comment)
: m_name(name), m_comment(comment) {} : m_name(name), m_comment(comment) {}
/** /**
* @brief Destructor. * @brief Destructor.
@@ -109,8 +111,8 @@ public:
* @return @a RESULT_OK on success, or an error code. * @return @a RESULT_OK on success, or an error code.
* Note: the caller needs to free the created instance. * Note: the caller needs to free the created instance.
*/ */
static result_t create(std::vector<std::string>::iterator& it, const std::vector<std::string>::iterator end, static result_t create(vector<string>::iterator& it, const vector<string>::iterator end,
const std::map<std::string, DataField*> templates, DataField*& returnField, const map<string, DataField*> templates, DataField*& returnField,
const bool isSetMessage=false, const unsigned char dstAddress=SYN); const bool isSetMessage=false, const unsigned char dstAddress=SYN);
/** /**
* @brief Returns the length of this field (or contained fields) in bytes. * @brief Returns the length of this field (or contained fields) in bytes.
@@ -126,27 +128,27 @@ public:
* @param partType the message part in which the field is stored. * @param partType 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 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). * @param values the value=text assignments, or empty to use this fields assignments (if applicable).
* @param fields the @a std::vector to which created @a SingleDataField instances shall be added. * @param fields the @a vector to which created @a SingleDataField instances shall be added.
*/ */
virtual result_t derive(std::string name, std::string comment, virtual result_t derive(string name, string comment,
std::string unit, const PartType partType, string unit, const PartType partType,
unsigned int divisor, std::map<unsigned int, std::string> values, unsigned int divisor, map<unsigned int, string> values,
std::vector<SingleDataField*>& fields) = 0; vector<SingleDataField*>& fields) = 0;
/** /**
* @brief Get the field name. * @brief Get the field name.
* @return the field name. * @return the field name.
*/ */
std::string getName() const { return m_name; } string getName() const { return m_name; }
/** /**
* @brief Get the field comment. * @brief Get the field comment.
* @return the field comment. * @return the field comment.
*/ */
std::string getComment() const { return m_comment; } string getComment() const { return m_comment; }
/** /**
* @brief Reads the value from the master or slave @a SymbolString. * @brief Reads the value from the master or slave @a SymbolString.
* @param masterData the unescaped master data @a SymbolString for reading binary data. * @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 slaveData the unescaped slave data @a SymbolString for reading binary data.
* @param output the @a std::ostringstream to append the formatted value to. * @param output the @a ostringstream to append the formatted value to.
* @param verbose whether to prepend the name, append the unit (if present), and append * @param verbose whether to prepend the name, append the unit (if present), and append
* the comment in square brackets (if present). * the comment in square brackets (if present).
* @param separator the separator character between multiple fields. * @param separator the separator character between multiple fields.
@@ -154,17 +156,17 @@ public:
*/ */
virtual result_t read(SymbolString& masterData, unsigned char masterOffset, virtual result_t read(SymbolString& masterData, unsigned char masterOffset,
SymbolString& slaveData, unsigned char slaveOffset, SymbolString& slaveData, unsigned char slaveOffset,
std::ostringstream& output, ostringstream& output,
bool verbose=false, char separator=';') = 0; bool verbose=false, char separator=';') = 0;
/** /**
* @brief Writes the value to the master or slave @a SymbolString. * @brief Writes the value to the master or slave @a SymbolString.
* @param input the @a std::istringstream to parse the formatted value from. * @param input the @a istringstream to parse the formatted value from.
* @param masterData the unescaped master data @a SymbolString for writing binary data. * @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 slaveData the unescaped slave data @a SymbolString for writing binary data.
* @param separator the separator character between multiple fields. * @param separator the separator character between multiple fields.
* @return @a RESULT_OK on success, or an error code. * @return @a RESULT_OK on success, or an error code.
*/ */
virtual result_t write(std::istringstream& input, virtual result_t write(istringstream& input,
SymbolString& masterData, unsigned char masterOffset, SymbolString& masterData, unsigned char masterOffset,
SymbolString& slaveData, unsigned char slaveOffset, SymbolString& slaveData, unsigned char slaveOffset,
char separator=';') = 0; char separator=';') = 0;
@@ -172,9 +174,9 @@ public:
protected: protected:
/** the field name. */ /** the field name. */
const std::string m_name; const string m_name;
/** the field comment. */ /** the field comment. */
const std::string m_comment; const string m_comment;
}; };
@@ -195,8 +197,8 @@ public:
* @param partType the message part in which the field is stored. * @param partType 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 length the number of symbols in the message part in which the field is stored.
*/ */
SingleDataField(const std::string name, const std::string comment, SingleDataField(const string name, const string comment,
const std::string unit, const dataType_t dataType, const PartType partType, const string unit, const dataType_t dataType, const PartType partType,
const unsigned char length) const unsigned char length)
: DataField(name, comment), : DataField(name, comment),
m_unit(unit), m_dataType(dataType), m_partType(partType), m_unit(unit), m_dataType(dataType), m_partType(partType),
@@ -209,7 +211,7 @@ public:
* @brief Get the value unit. * @brief Get the value unit.
* @return the value unit. * @return the value unit.
*/ */
std::string getUnit() const { return m_unit; } string getUnit() const { return m_unit; }
/** /**
* @brief Get whether this field is ignored. * @brief Get whether this field is ignored.
* @return whether this field is ignored. * @return whether this field is ignored.
@@ -243,18 +245,18 @@ public:
*/ */
virtual result_t read(SymbolString& masterData, unsigned char masterOffset, virtual result_t read(SymbolString& masterData, unsigned char masterOffset,
SymbolString& slaveData, unsigned char slaveOffset, SymbolString& slaveData, unsigned char slaveOffset,
std::ostringstream& output, ostringstream& output,
bool verbose, char separator); bool verbose, char separator);
/** /**
* @brief Writes the value to the master or slave @a SymbolString. * @brief Writes the value to the master or slave @a SymbolString.
* @param input the @a std::istringstream to parse the formatted value from. * @param input the @a istringstream to parse the formatted value from.
* @param masterData the unescaped master data @a SymbolString for writing binary data. * @param masterData the unescaped master data @a SymbolString for writing binary data.
* @param masterOffset the extra offset for writing master data. * @param masterOffset the extra offset for writing master data.
* @param slaveData the unescaped slave data @a SymbolString for writing binary data. * @param slaveData the unescaped slave data @a SymbolString for writing binary data.
* @param slaveOffset the extra offset for writing slave data. * @param slaveOffset the extra offset for writing slave data.
* @return @a RESULT_OK on success, or an error code. * @return @a RESULT_OK on success, or an error code.
*/ */
virtual result_t write(std::istringstream& input, virtual result_t write(istringstream& input,
SymbolString& masterData, unsigned char masterOffset, SymbolString& masterData, unsigned char masterOffset,
SymbolString& slaveData, unsigned char slaveOffset, SymbolString& slaveData, unsigned char slaveOffset,
char separator); char separator);
@@ -268,18 +270,18 @@ protected:
* @param output the ostringstream to append the formatted value to. * @param output the ostringstream to append the formatted value to.
* @return @a RESULT_OK on success, or an error code. * @return @a RESULT_OK on success, or an error code.
*/ */
virtual result_t readSymbols(SymbolString& input, const unsigned char offset, std::ostringstream& output) = 0; virtual result_t readSymbols(SymbolString& input, const unsigned char offset, ostringstream& output) = 0;
/** /**
* @brief Internal method for writing the field to a @a SymbolString. * @brief Internal method for writing the field to a @a SymbolString.
* @param input the @a std::istringstream to parse the formatted value from. * @param input the @a istringstream to parse the formatted value from.
* @param offset the offset in the @a SymbolString. * @param offset the offset in the @a SymbolString.
* @param output the unescaped @a SymbolString to write the binary value to. * @param output the unescaped @a SymbolString to write the binary value to.
* @return @a RESULT_OK on success, or an error code. * @return @a RESULT_OK on success, or an error code.
*/ */
virtual result_t writeSymbols(std::istringstream& input, const unsigned char offset, SymbolString& output) = 0; virtual result_t writeSymbols(istringstream& input, const unsigned char offset, SymbolString& output) = 0;
/** the value unit. */ /** the value unit. */
const std::string m_unit; const string m_unit;
/** the data type definition. */ /** the data type definition. */
const dataType_t m_dataType; const dataType_t m_dataType;
/** the message part in which the field is stored. */ /** the message part in which the field is stored. */
@@ -306,8 +308,8 @@ public:
* @param partType the message part in which the field is stored. * @param partType 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 length the number of symbols in the message part in which the field is stored.
*/ */
StringDataField(const std::string name, const std::string comment, StringDataField(const string name, const string comment,
const std::string unit, const dataType_t dataType, const PartType partType, const string unit, const dataType_t dataType, const PartType partType,
const unsigned char length) const unsigned char length)
: SingleDataField(name, comment, unit, dataType, partType, length) {} : SingleDataField(name, comment, unit, dataType, partType, length) {}
/** /**
@@ -315,17 +317,17 @@ public:
*/ */
virtual ~StringDataField() {} virtual ~StringDataField() {}
// @copydoc // @copydoc
virtual result_t derive(std::string name, std::string comment, virtual result_t derive(string name, string comment,
std::string unit, const PartType partType, string unit, const PartType partType,
unsigned int divisor, std::map<unsigned int, std::string> values, unsigned int divisor, map<unsigned int, string> values,
std::vector<SingleDataField*>& fields); vector<SingleDataField*>& fields);
protected: protected:
// @copydoc // @copydoc
virtual result_t readSymbols(SymbolString& input, const unsigned char offset, std::ostringstream& output); virtual result_t readSymbols(SymbolString& input, const unsigned char offset, ostringstream& output);
// @copydoc // @copydoc
virtual result_t writeSymbols(std::istringstream& input, const unsigned char offset, SymbolString& output); virtual result_t writeSymbols(istringstream& input, const unsigned char offset, SymbolString& output);
}; };
@@ -348,8 +350,8 @@ public:
* @param bitCount the number of bits in the binary value. * @param bitCount the number of bits in the binary value.
* @param bitOffset the offset to the first bit in the binary value. * @param bitOffset the offset to the first bit in the binary value.
*/ */
NumericDataField(const std::string name, const std::string comment, NumericDataField(const string name, const string comment,
const std::string unit, const dataType_t dataType, const PartType partType, const string unit, const dataType_t dataType, const PartType partType,
const unsigned char length, const unsigned char bitCount, const unsigned char bitOffset) const unsigned char length, const unsigned char bitCount, const unsigned char bitOffset)
: SingleDataField(name, comment, unit, dataType, partType, length), : SingleDataField(name, comment, unit, dataType, partType, length),
m_bitCount(bitCount), m_bitOffset(bitOffset) {} m_bitCount(bitCount), m_bitOffset(bitOffset) {}
@@ -406,8 +408,8 @@ public:
* @param length the number of symbols 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 divisor the extra divisor to apply on the value, or 1 for none. * @param divisor the extra divisor to apply on the value, or 1 for none.
*/ */
NumberDataField(const std::string name, const std::string comment, NumberDataField(const string name, const string comment,
const std::string unit, const dataType_t dataType, const PartType partType, const string unit, const dataType_t dataType, const PartType partType,
const unsigned char length, const unsigned char bitCount, const unsigned char length, const unsigned char bitCount,
const unsigned int divisor) const unsigned int divisor)
: NumericDataField(name, comment, unit, dataType, partType, length, bitCount, : NumericDataField(name, comment, unit, dataType, partType, length, bitCount,
@@ -418,17 +420,17 @@ public:
*/ */
virtual ~NumberDataField() {} virtual ~NumberDataField() {}
// @copydoc // @copydoc
virtual result_t derive(std::string name, std::string comment, virtual result_t derive(string name, string comment,
std::string unit, const PartType partType, string unit, const PartType partType,
unsigned int divisor, std::map<unsigned int, std::string> values, unsigned int divisor, map<unsigned int, string> values,
std::vector<SingleDataField*>& fields); vector<SingleDataField*>& fields);
protected: protected:
// @copydoc // @copydoc
virtual result_t readSymbols(SymbolString& input, const unsigned char offset, std::ostringstream& output); virtual result_t readSymbols(SymbolString& input, const unsigned char offset, ostringstream& output);
// @copydoc // @copydoc
virtual result_t writeSymbols(std::istringstream& input, const unsigned char offset, SymbolString& output); virtual result_t writeSymbols(istringstream& input, const unsigned char offset, SymbolString& output);
/** the combined divisor to apply on the value, or 1 for none. */ /** the combined divisor to apply on the value, or 1 for none. */
const unsigned int m_divisor; const unsigned int m_divisor;
@@ -453,10 +455,10 @@ public:
* @param length the number of symbols 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 values the value=text assignments. * @param values the value=text assignments.
*/ */
ValueListDataField(const std::string name, const std::string comment, ValueListDataField(const string name, const string comment,
const std::string unit, const dataType_t dataType, const PartType partType, const string unit, const dataType_t dataType, const PartType partType,
const unsigned char length, const unsigned char bitCount, const unsigned char length, const unsigned char bitCount,
const std::map<unsigned int, std::string> values) const map<unsigned int, string> values)
: NumericDataField(name, comment, unit, dataType, partType, length, bitCount, : NumericDataField(name, comment, unit, dataType, partType, length, bitCount,
(dataType.maxBits < 8) ? dataType.precisionOrFirstBit : 0), (dataType.maxBits < 8) ? dataType.precisionOrFirstBit : 0),
m_values(values) {} m_values(values) {}
@@ -465,20 +467,20 @@ public:
*/ */
virtual ~ValueListDataField() {} virtual ~ValueListDataField() {}
// @copydoc // @copydoc
virtual result_t derive(std::string name, std::string comment, virtual result_t derive(string name, string comment,
std::string unit, const PartType partType, unsigned int divisor, string unit, const PartType partType, unsigned int divisor,
std::map<unsigned int, std::string> values, map<unsigned int, string> values,
std::vector<SingleDataField*>& fields); vector<SingleDataField*>& fields);
protected: protected:
// @copydoc // @copydoc
virtual result_t readSymbols(SymbolString& input, const unsigned char offset, std::ostringstream& output); virtual result_t readSymbols(SymbolString& input, const unsigned char offset, ostringstream& output);
// @copydoc // @copydoc
virtual result_t writeSymbols(std::istringstream& input, const unsigned char offset, SymbolString& output); virtual result_t writeSymbols(istringstream& input, const unsigned char offset, SymbolString& output);
/** the value=text assignments. */ /** the value=text assignments. */
std::map<unsigned int, std::string> m_values; map<unsigned int, string> m_values;
}; };
@@ -494,10 +496,10 @@ public:
* @brief Constructs a new instance. * @brief Constructs a new instance.
* @param name the field name. * @param name the field name.
* @param comment the field comment. * @param comment the field comment.
* @param fields the @a std::vector of @a SingleDataField instances part of this set. * @param fields the @a vector of @a SingleDataField instances part of this set.
*/ */
DataFieldSet(const std::string name, const std::string comment, DataFieldSet(const string name, const string comment,
const std::vector<SingleDataField*> fields) const vector<SingleDataField*> fields)
: DataField(name, comment), : DataField(name, comment),
m_fields(fields) {} m_fields(fields) {}
/** /**
@@ -507,10 +509,10 @@ public:
// @copydoc // @copydoc
virtual unsigned char getLength(PartType partType); virtual unsigned char getLength(PartType partType);
// @copydoc // @copydoc
virtual result_t derive(std::string name, std::string comment, virtual result_t derive(string name, string comment,
std::string unit, const PartType partType, string unit, const PartType partType,
unsigned int divisor, std::map<unsigned int, std::string> values, unsigned int divisor, map<unsigned int, string> values,
std::vector<SingleDataField*>& fields); vector<SingleDataField*>& fields);
/** /**
* @brief Returns the @a SingleDataField at the specified index. * @brief Returns the @a SingleDataField at the specified index.
* @param index the index of the @a SingleDataField to return. * @param index the index of the @a SingleDataField to return.
@@ -531,18 +533,18 @@ public:
// @copydoc // @copydoc
virtual result_t read(SymbolString& masterData, unsigned char masterOffset, virtual result_t read(SymbolString& masterData, unsigned char masterOffset,
SymbolString& slaveData, unsigned char slaveOffset, SymbolString& slaveData, unsigned char slaveOffset,
std::ostringstream& output, ostringstream& output,
bool verbose, char separator); bool verbose, char separator);
// @copydoc // @copydoc
virtual result_t write(std::istringstream& input, virtual result_t write(istringstream& input,
SymbolString& masterData, unsigned char masterOffset, SymbolString& masterData, unsigned char masterOffset,
SymbolString& slaveData, unsigned char slaveOffset, SymbolString& slaveData, unsigned char slaveOffset,
char separator); char separator);
protected: protected:
/** the @a std::vector of @a SingleDataField instances part of this set. */ /** the @a vector of @a SingleDataField instances part of this set. */
std::vector<SingleDataField*> m_fields; vector<SingleDataField*> m_fields;
}; };
+13 -11
View File
@@ -25,8 +25,10 @@
#include <vector> #include <vector>
#include <cstring> #include <cstring>
result_t Message::create(std::vector<std::string>::iterator& it, const std::vector<std::string>::iterator end, using namespace std;
const std::map<std::string, DataField*> templates, Message*& returnValue)
result_t Message::create(vector<string>::iterator& it, const vector<string>::iterator end,
const map<string, DataField*> templates, Message*& returnValue)
{ {
result_t result; result_t result;
// [type];class;name;[comment];[QQ];ZZ;id;fields... // [type];class;name;[comment];[QQ];ZZ;id;fields...
@@ -60,17 +62,17 @@ result_t Message::create(std::vector<std::string>::iterator& it, const std::vect
isSetMessage = false; isSetMessage = false;
} }
std::string clazz = *it++; string clazz = *it++;
if (it == end) if (it == end)
return RESULT_ERR_EOF; return RESULT_ERR_EOF;
std::string name = *it++; string name = *it++;
if (it == end) if (it == end)
return RESULT_ERR_EOF; return RESULT_ERR_EOF;
if (name.length() == 0) if (name.length() == 0)
return RESULT_ERR_INVALID_ARG; // empty name return RESULT_ERR_INVALID_ARG; // empty name
std::string comment = *it++; string comment = *it++;
if (it == end) if (it == end)
return RESULT_ERR_EOF; return RESULT_ERR_EOF;
@@ -98,9 +100,9 @@ result_t Message::create(std::vector<std::string>::iterator& it, const std::vect
if (isValidAddress(dstAddress) == false) if (isValidAddress(dstAddress) == false)
return RESULT_ERR_INVALID_ARG; return RESULT_ERR_INVALID_ARG;
std::istringstream input(*it++); // message id (PBSB + optional master data) istringstream input(*it++); // message id (PBSB + optional master data)
std::vector<unsigned char> id; vector<unsigned char> id;
std::string token; string token;
if (it == end) if (it == end)
return RESULT_ERR_EOF; return RESULT_ERR_EOF;
while (input.eof() == false) { while (input.eof() == false) {
@@ -131,7 +133,7 @@ result_t Message::create(std::vector<std::string>::iterator& it, const std::vect
return RESULT_OK; return RESULT_OK;
} }
result_t Message::prepare(const unsigned char srcAddress, SymbolString& masterData, std::istringstream& input, char separator) result_t Message::prepare(const unsigned char srcAddress, SymbolString& masterData, istringstream& input, char separator)
{ {
if (m_isActiveMessage == true) { if (m_isActiveMessage == true) {
masterData.clear(); masterData.clear();
@@ -153,7 +155,7 @@ result_t Message::prepare(const unsigned char srcAddress, SymbolString& masterDa
} }
result_t Message::handle(SymbolString& masterData, SymbolString& slaveData, result_t Message::handle(SymbolString& masterData, SymbolString& slaveData,
std::ostringstream& output, char separator, bool answer) ostringstream& output, char separator, bool answer)
{ {
if (m_isActiveMessage == true) { if (m_isActiveMessage == true) {
result_t result = m_data->read(masterData, m_id.size() - 2, slaveData, 0, output, false, separator); result_t result = m_data->read(masterData, m_id.size() - 2, slaveData, 0, output, false, separator);
@@ -161,7 +163,7 @@ result_t Message::handle(SymbolString& masterData, SymbolString& slaveData,
return result; return result;
} }
else if (answer == true) { else if (answer == true) {
std::istringstream input; // TODO create input from database of internal variables istringstream input; // TODO create input from database of internal variables
result_t result = m_data->write(input, masterData, m_id.size() - 2, slaveData, 0, separator); result_t result = m_data->write(input, masterData, m_id.size() - 2, slaveData, 0, separator);
if (result != RESULT_OK) if (result != RESULT_OK)
return result; return result;
+20 -18
View File
@@ -26,6 +26,8 @@
#include <string> #include <string>
#include <vector> #include <vector>
using namespace std;
/** /**
* @brief Base class for all kinds of bus messages. * @brief Base class for all kinds of bus messages.
*/ */
@@ -48,10 +50,10 @@ public:
* @param data the @a DataField for encoding/decoding the message. * @param data the @a DataField for encoding/decoding the message.
* @param pollPriority the priority for polling, or 0 for no polling at all. * @param pollPriority the priority for polling, or 0 for no polling at all.
*/ */
Message(const std::string clazz, const std::string name, const bool isSetMessage, Message(const string clazz, const string name, const bool isSetMessage,
const bool isActiveMessage, const std::string comment, const bool isActiveMessage, const string comment,
const unsigned char srcAddress, const unsigned char dstAddress, const unsigned char srcAddress, const unsigned char dstAddress,
const std::vector<unsigned char> id, DataField* data, const vector<unsigned char> id, DataField* data,
const unsigned int pollPriority) const unsigned int pollPriority)
: m_class(clazz), m_name(name), m_isSetMessage(isSetMessage), : m_class(clazz), m_name(name), m_isSetMessage(isSetMessage),
m_isActiveMessage(isActiveMessage), m_comment(comment), m_isActiveMessage(isActiveMessage), m_comment(comment),
@@ -70,18 +72,18 @@ public:
* @return @a RESULT_OK on success, or an error code. * @return @a RESULT_OK on success, or an error code.
* Note: the caller needs to free the created instance. * Note: the caller needs to free the created instance.
*/ */
static result_t create(std::vector<std::string>::iterator& it, const std::vector<std::string>::iterator end, static result_t create(vector<string>::iterator& it, const vector<string>::iterator end,
const std::map<std::string, DataField*> templates, Message*& returnValue); const map<string, DataField*> templates, Message*& returnValue);
/** /**
* @brief Get the optional device class. * @brief Get the optional device class.
* @return the optional device class. * @return the optional device class.
*/ */
std::string getClass() const { return m_class; } string getClass() const { return m_class; }
/** /**
* @brief Get the message name (unique within the same class and type). * @brief Get the message name (unique within the same class and type).
* @return the message name (unique within the same class and type). * @return the message name (unique within the same class and type).
*/ */
std::string getName() const { return m_name; } string getName() const { return m_name; }
/** /**
* @brief Get whether this is a set message. * @brief Get whether this is a set message.
* @return whether this is a set message. * @return whether this is a set message.
@@ -99,7 +101,7 @@ public:
* @brief Get the comment. * @brief Get the comment.
* @return the comment. * @return the comment.
*/ */
std::string getComment() const { return m_comment; } string getComment() const { return m_comment; }
/** /**
* @brief Get the source address. * @brief Get the source address.
* @return the source address, or @a SYN for any. * @return the source address, or @a SYN for any.
@@ -114,39 +116,39 @@ public:
* @brief Get the command ID bytes. * @brief Get the command ID bytes.
* @return the primary, secondary, and optionally further command ID bytes. * @return the primary, secondary, and optionally further command ID bytes.
*/ */
std::vector<unsigned char> getId() const { return m_id; } vector<unsigned char> getId() const { return m_id; }
/** /**
* @brief Reads the value from the master or slave @a SymbolString. * @brief Reads the value from the master or slave @a SymbolString.
* @param masterData the unescaped master data @a SymbolString for reading binary data. * @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 slaveData the unescaped slave data @a SymbolString for reading binary data.
* @param output the @a std::ostringstream to append the formatted value to. * @param output the @a ostringstream to append the formatted value to.
* @param verbose whether to prepend the name, append the unit (if present), and append * @param verbose whether to prepend the name, append the unit (if present), and append
* the comment in square brackets (if present). * the comment in square brackets (if present).
* @param separator the separator character between multiple fields. * @param separator the separator character between multiple fields.
* @return @a RESULT_OK on success, or an error code. * @return @a RESULT_OK on success, or an error code.
*/ */
//result_t read(SymbolString& masterData, SymbolString& slaveData, std::ostringstream& output, //result_t read(SymbolString& masterData, SymbolString& slaveData, ostringstream& output,
// bool verbose=false, char separator=';') = 0; // bool verbose=false, char separator=';') = 0;
/** /**
* @brief Writes the value to the master or slave @a SymbolString. * @brief Writes the value to the master or slave @a SymbolString.
* @param input the @a std::istringstream to parse the formatted value from. * @param input the @a istringstream to parse the formatted value from.
* @param masterData the unescaped master data @a SymbolString for writing binary data. * @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 slaveData the unescaped slave data @a SymbolString for writing binary data.
* @param separator the separator character between multiple fields. * @param separator the separator character between multiple fields.
* @return @a RESULT_OK on success, or an error code. * @return @a RESULT_OK on success, or an error code.
*/ */
result_t prepare(const unsigned char srcAddress, SymbolString& masterData, result_t prepare(const unsigned char srcAddress, SymbolString& masterData,
std::istringstream& input, char separator=';'); istringstream& input, char separator=';');
result_t handle(SymbolString& masterData, SymbolString& slaveData, result_t handle(SymbolString& masterData, SymbolString& slaveData,
std::ostringstream& output, char separator=';', bool answer=false); ostringstream& output, char separator=';', bool answer=false);
private: private:
/** the optional device class. */ /** the optional device class. */
const std::string m_class; const string m_class;
/** the message name (unique within the same class and type). */ /** the message name (unique within the same class and type). */
const std::string m_name; const string m_name;
/** whether this is a set message. */ /** whether this is a set message. */
const bool m_isSetMessage; const bool m_isSetMessage;
/** true if message can be initiated by the daemon itself and any other /** true if message can be initiated by the daemon itself and any other
@@ -154,13 +156,13 @@ private:
* other than the daemon. */ * other than the daemon. */
const bool m_isActiveMessage; const bool m_isActiveMessage;
/** the comment. */ /** the comment. */
const std::string m_comment; const string m_comment;
/** the source address (optional if passive), or @a SYN for any. */ /** the source address (optional if passive), or @a SYN for any. */
const unsigned char m_srcAddress; const unsigned char m_srcAddress;
/** the destination address. */ /** the destination address. */
const unsigned char m_dstAddress; const unsigned char m_dstAddress;
/** the primary, secondary, and optionally further command ID bytes. */ /** the primary, secondary, and optionally further command ID bytes. */
const std::vector<unsigned char> m_id; const vector<unsigned char> m_id;
/** the @a DataField for encoding/decoding the message. */ /** the @a DataField for encoding/decoding the message. */
DataField* m_data; DataField* m_data;
/** the priority for polling, or 0 for no polling at all. */ /** the priority for polling, or 0 for no polling at all. */
+4
View File
@@ -18,8 +18,12 @@
*/ */
#include "result.h" #include "result.h"
#include <iostream>
using namespace std;
const char* getResultCode(result_t resultCode) { const char* getResultCode(result_t resultCode) {
cout << "DEBUG error code: " << static_cast<signed>(resultCode) << endl;
switch (resultCode) { switch (resultCode) {
case RESULT_ERR_SEND: return "ERR_SEND: send error"; case RESULT_ERR_SEND: return "ERR_SEND: send error";
case RESULT_ERR_EXTRA_DATA: return "ERR_EXTRA_DATA: received bytes > sent bytes"; case RESULT_ERR_EXTRA_DATA: return "ERR_EXTRA_DATA: received bytes > sent bytes";
+8 -6
View File
@@ -22,6 +22,8 @@
#include <iostream> #include <iostream>
#include <iomanip> #include <iomanip>
using namespace std;
/** /**
* @brief CRC8 lookup table for the polynom 0x9b = x^8 + x^7 + x^4 + x^3 + x^1 + 1. * @brief CRC8 lookup table for the polynom 0x9b = x^8 + x^7 + x^4 + x^3 + x^1 + 1.
*/ */
@@ -46,7 +48,7 @@ static const unsigned char CRC_LOOKUP_TABLE[] =
}; };
SymbolString::SymbolString(const std::string str) SymbolString::SymbolString(const string str)
: m_unescapeState(0), m_crc(0) : m_unescapeState(0), m_crc(0)
{ {
// parse + escape // parse + escape
@@ -58,7 +60,7 @@ SymbolString::SymbolString(const std::string str)
push_back(m_crc, false, false); push_back(m_crc, false, false);
} }
SymbolString::SymbolString(const std::string str, bool isEscaped) SymbolString::SymbolString(const string str, bool isEscaped)
: m_unescapeState(1), m_crc(0) : m_unescapeState(1), m_crc(0)
{ {
// parse + optionally unescape // parse + optionally unescape
@@ -68,9 +70,9 @@ SymbolString::SymbolString(const std::string str, bool isEscaped)
} }
} }
const std::string SymbolString::getDataStr(const bool unescape) const string SymbolString::getDataStr(const bool unescape)
{ {
std::stringstream sstr; stringstream sstr;
bool previousEscape = false; bool previousEscape = false;
for (size_t i = 0; i < m_data.size(); i++) { for (size_t i = 0; i < m_data.size(); i++) {
@@ -89,8 +91,8 @@ const std::string SymbolString::getDataStr(const bool unescape)
previousEscape = true; // escape sequence not yet finished previousEscape = true; // escape sequence not yet finished
} }
else { else {
sstr << std::nouppercase << std::setw(2) << std::hex sstr << nouppercase << setw(2) << hex
<< std::setfill('0') << static_cast<unsigned>(value); << setfill('0') << static_cast<unsigned>(value);
} }
} }
+6 -4
View File
@@ -25,6 +25,8 @@
#include <sstream> #include <sstream>
#include <queue> #include <queue>
using namespace std;
static const unsigned char ESC = 0xA9; // escape symbol, either followed by 0x00 for the value 0xA9, or 0x01 for the value 0xAA static const unsigned char ESC = 0xA9; // escape symbol, either followed by 0x00 for the value 0xA9, or 0x01 for the value 0xAA
static const unsigned char SYN = 0xAA; // synchronization symbol static const unsigned char SYN = 0xAA; // synchronization symbol
static const unsigned char ACK = 0x00; // positive acknowledge static const unsigned char ACK = 0x00; // positive acknowledge
@@ -48,19 +50,19 @@ public:
* @brief Creates a new escaped instance from an unescaped hex string and adds the calculated CRC. * @brief Creates a new escaped instance from an unescaped hex string and adds the calculated CRC.
* @param str the unescaped hex string. * @param str the unescaped hex string.
*/ */
SymbolString(const std::string str); SymbolString(const string str);
/** /**
* @brief Creates a new unescaped instance from a hex string. * @brief Creates a new unescaped instance from a hex string.
* @param isEscaped whether the hex string is escaped and shall be unescaped. * @param isEscaped whether the hex string is escaped and shall be unescaped.
* @param str the hex string. * @param str the hex string.
*/ */
SymbolString(const std::string str, const bool isEscaped); SymbolString(const string str, const bool isEscaped);
/** /**
* @brief Returns the symbols as hex string. * @brief Returns the symbols as hex string.
* @param unescape whether to unescape an escaped instance. * @param unescape whether to unescape an escaped instance.
* @return the symbols as hex string. * @return the symbols as hex string.
*/ */
const std::string getDataStr(const bool unescape=true); const string getDataStr(const bool unescape=true);
/** /**
* @brief Returns a reference to the symbol at the specified index. * @brief Returns a reference to the symbol at the specified index.
* @param index the index of the symbol to return. * @param index the index of the symbol to return.
@@ -114,7 +116,7 @@ private:
/** /**
* @brief the string of bus symbols. * @brief the string of bus symbols.
*/ */
std::vector<unsigned char> m_data; vector<unsigned char> m_data;
/** /**
* @brief 0 if the symbols in @a m_data are escaped, * @brief 0 if the symbols in @a m_data are escaped,
* 1 if the symbols in @a m_data are unescaped and the last symbol passed to @a push_back was a normal symbol, * 1 if the symbols in @a m_data are unescaped and the last symbol passed to @a push_back was a normal symbol,
+51 -51
View File
@@ -21,27 +21,28 @@
#include <iostream> #include <iostream>
#include <iomanip> #include <iomanip>
void verify(bool expectFailMatch, std::string type, std::string input, using namespace std;
bool match, std::string expectStr, std::string gotStr)
void verify(bool expectFailMatch, string type, string input,
bool match, string expectStr, string gotStr)
{ {
if (expectFailMatch == true) { if (expectFailMatch == true) {
if (match == true) if (match == true)
std::cout << " failed " << type << " match >" << input cout << " failed " << type << " match >" << input
<< "< error: unexpectedly succeeded" << std::endl; << "< error: unexpectedly succeeded" << endl;
else else
std::cout << " failed " << type << " match >" << input << "< OK" cout << " failed " << type << " match >" << input << "< OK" << endl;
<< std::endl;
} }
else if (match == true) else if (match == true)
std::cout << " " << type << " match >" << input << "< OK" << std::endl; cout << " " << type << " match >" << input << "< OK" << endl;
else else
std::cout << " " << type << " match >" << input << "< error: got >" << gotStr cout << " " << type << " match >" << input << "< error: got >"
<< "<, expected >" << expectStr << "<" << std::endl; << gotStr << "<, expected >" << expectStr << "<" << endl;
} }
int main() int main()
{ {
std::string checks[][5] = { string checks[][5] = {
//name;[len];type[;[divisor|values][;[unit][;[comment]]]], decoded value, master, slave, flags //name;[len];type[;[divisor|values][;[unit][;[comment]]]], decoded value, master, slave, flags
{"x;;ign:10", "", "10fe07000a00000000000000000000", "00", ""}, {"x;;ign:10", "", "10fe07000a00000000000000000000", "00", ""},
{"x;;str:10", "Hallo, Du!", "10fe07000a48616c6c6f2c20447521", "00", ""}, {"x;;str:10", "Hallo, Du!", "10fe07000a48616c6c6f2c20447521", "00", ""},
@@ -180,60 +181,59 @@ int main()
{"x;;trelrel","18.004;19.008","10fe07000401120213", "00", ""}, // reference to template struct {"x;;trelrel","18.004;19.008","10fe07000401120213", "00", ""}, // reference to template struct
{"x;;temp;;;;y;;d1c","18.004;9.5","10fe070003011213", "00", ""}, // reference to template, normal def {"x;;temp;;;;y;;d1c","18.004;9.5","10fe070003011213", "00", ""}, // reference to template, normal def
}; };
std::map<std::string, DataField*> templates; map<string, DataField*> templates;
DataField* fields = NULL; DataField* fields = NULL;
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]; string check[5] = checks[i];
std::istringstream isstr(check[0]); istringstream isstr(check[0]);
std::string expectStr = check[1]; string expectStr = check[1];
SymbolString mstr = SymbolString(check[2], false); SymbolString mstr = SymbolString(check[2], false);
SymbolString sstr = SymbolString(check[3], false); SymbolString sstr = SymbolString(check[3], false);
std::string flags = check[4]; string flags = check[4];
bool isSet = flags.find('s') != std::string::npos; bool isSet = flags.find('s') != string::npos;
bool failedCreate = flags.find('c') != std::string::npos; bool failedCreate = flags.find('c') != string::npos;
bool failedRead = flags.find('r') != std::string::npos; bool failedRead = flags.find('r') != string::npos;
bool failedReadMatch = flags.find('R') != std::string::npos; bool failedReadMatch = flags.find('R') != string::npos;
bool failedWrite = flags.find('w') != std::string::npos; bool failedWrite = flags.find('w') != string::npos;
bool failedWriteMatch = flags.find('W') != std::string::npos; bool failedWriteMatch = flags.find('W') != string::npos;
bool verbose = flags.find('v') != std::string::npos; bool verbose = flags.find('v') != string::npos;
bool isTemplate = flags.find('t') != std::string::npos; bool isTemplate = flags.find('t') != string::npos;
std::string item; string item;
std::vector<std::string> entries; vector<string> entries;
while (std::getline(isstr, item, ';') != 0) while (getline(isstr, item, ';') != 0)
entries.push_back(item); entries.push_back(item);
if (fields != NULL) { if (fields != NULL) {
delete fields; delete fields;
fields = NULL; fields = NULL;
} }
std::vector<std::string>::iterator it = entries.begin(); vector<string>::iterator it = entries.begin();
result_t result = DataField::create(it, entries.end(), templates, fields, isSet, isTemplate ? SYN : mstr[1]); result_t result = DataField::create(it, entries.end(), templates, fields, isSet, isTemplate ? SYN : mstr[1]);
if (failedCreate == true) { if (failedCreate == true) {
if (result == RESULT_OK) if (result == RESULT_OK)
std::cout << "\"" << check[0] << "\": failed create error: unexpectedly succeeded" << std::endl; cout << "\"" << check[0] << "\": failed create error: unexpectedly succeeded" << endl;
else else
std::cout << "\"" << check[0] << "\": failed create OK" << std::endl; cout << "\"" << check[0] << "\": failed create OK" << endl;
continue; continue;
} }
if (result != RESULT_OK) { if (result != RESULT_OK) {
std::cout << "\"" << check[0] << "\": create error: " cout << "\"" << check[0] << "\": create error: " << getResultCode(result) << endl;
<< getResultCode(result) << std::endl;
continue; continue;
} }
if (fields == NULL) { if (fields == NULL) {
std::cout << "\"" << check[0] << "\": create error: NULL" << std::endl; cout << "\"" << check[0] << "\": create error: NULL" << endl;
continue; continue;
} }
if (it != entries.end()) { if (it != entries.end()) {
std::cout << "\"" << check[0] << "\": create error: trailing input" << std::endl; cout << "\"" << check[0] << "\": create error: trailing input" << endl;
continue; continue;
} }
std::cout << "\"" << check[0] << "\": create OK" << std::endl; cout << "\"" << check[0] << "\": create OK" << endl;
if (isTemplate) { if (isTemplate) {
// store new template // store new template
std::string name = fields->getName(); string name = fields->getName();
std::map<std::string, DataField*>::iterator current = templates.find(name); map<string, DataField*>::iterator current = templates.find(name);
if (current == templates.end()) { if (current == templates.end()) {
templates[name] = fields; templates[name] = fields;
} else { } else {
@@ -244,20 +244,20 @@ int main()
continue; continue;
} }
std::ostringstream output; ostringstream output;
SymbolString writeMstr = SymbolString(mstr.getDataStr().substr(0, 10), false); SymbolString writeMstr = SymbolString(mstr.getDataStr().substr(0, 10), false);
SymbolString writeSstr = SymbolString(sstr.getDataStr().substr(0, 2), false); SymbolString writeSstr = SymbolString(sstr.getDataStr().substr(0, 2), false);
result = fields->read(mstr, 0, sstr, 0, output, verbose); result = fields->read(mstr, 0, sstr, 0, output, verbose);
if (failedRead == true) if (failedRead == true)
if (result == RESULT_OK) if (result == RESULT_OK)
std::cout << " failed read " << fields->getName() << " >" cout << " failed read " << fields->getName() << " >"
<< check[2] << "< error: unexpectedly succeeded" << std::endl; << check[2] << "< error: unexpectedly succeeded" << endl;
else else
std::cout << " failed read " << fields->getName() << " >" cout << " failed read " << fields->getName() << " >"
<< check[2] << "< OK" << std::endl; << check[2] << "< OK" << endl;
else if (result != RESULT_OK) { else if (result != RESULT_OK) {
std::cout << " read " << fields->getName() << " >" << check[2] << "< error: " cout << " read " << fields->getName() << " >" << check[2]
<< getResultCode(result) << std::endl; << "< error: " << getResultCode(result) << endl;
} }
else { else {
bool match = strcasecmp(output.str().c_str(), expectStr.c_str()) == 0; bool match = strcasecmp(output.str().c_str(), expectStr.c_str()) == 0;
@@ -265,19 +265,19 @@ int main()
} }
if (verbose == false) { if (verbose == false) {
std::istringstream input(expectStr); istringstream input(expectStr);
result = fields->write(input, writeMstr, 0, writeSstr, 0); result = fields->write(input, writeMstr, 0, writeSstr, 0);
if (failedWrite == true) { if (failedWrite == true) {
if (result == RESULT_OK) if (result == RESULT_OK)
std::cout << " failed write " << fields->getName() << " >" cout << " failed write " << fields->getName() << " >"
<< expectStr << "< error: unexpectedly succeeded" << std::endl; << expectStr << "< error: unexpectedly succeeded" << endl;
else else
std::cout << " failed write " << fields->getName() << " >" cout << " failed write " << fields->getName() << " >"
<< expectStr << "< OK" << std::endl; << expectStr << "< OK" << endl;
} }
else if (result != RESULT_OK) { else if (result != RESULT_OK) {
std::cout << " write " << fields->getName() << " >" cout << " write " << fields->getName() << " >" << expectStr
<< expectStr << "< error: " << getResultCode(result) << std::endl; << "< error: " << getResultCode(result) << endl;
} }
else { else {
bool match = mstr == writeMstr && sstr == writeSstr; bool match = mstr == writeMstr && sstr == writeSstr;
@@ -288,7 +288,7 @@ int main()
fields = NULL; fields = NULL;
} }
for (std::map<std::string, DataField*>::iterator it = templates.begin(); it != templates.end(); it++) for (map<string, DataField*>::iterator it = templates.begin(); it != templates.end(); it++)
delete it->second; delete it->second;
return 0; return 0;
+43 -42
View File
@@ -21,27 +21,28 @@
#include <iostream> #include <iostream>
#include <iomanip> #include <iomanip>
void verify(bool expectFailMatch, std::string type, std::string input, using namespace std;
bool match, std::string expectStr, std::string gotStr)
void verify(bool expectFailMatch, string type, string input,
bool match, string expectStr, string gotStr)
{ {
if (expectFailMatch == true) { if (expectFailMatch == true) {
if (match == true) if (match == true)
std::cout << " failed " << type << " match >" << input cout << " failed " << type << " match >" << input
<< "< error: unexpectedly succeeded" << std::endl; << "< error: unexpectedly succeeded" << endl;
else else
std::cout << " failed " << type << " match >" << input << "< OK" cout << " failed " << type << " match >" << input << "< OK" << endl;
<< std::endl;
} }
else if (match == true) else if (match == true)
std::cout << " " << type << " match >" << input << "< OK" << std::endl; cout << " " << type << " match >" << input << "< OK" << endl;
else else
std::cout << " " << type << " match >" << input << "< error: got >" << gotStr cout << " " << type << " match >" << input << "< error: got >"
<< "<, expected >" << expectStr << "<" << std::endl; << gotStr << "<, expected >" << expectStr << "<" << endl;
} }
void printErrorPos(std::vector<std::string>::iterator it, const std::vector<std::string>::iterator end, std::vector<std::string>::iterator pos) void printErrorPos(vector<string>::iterator it, const vector<string>::iterator end, vector<string>::iterator pos)
{ {
std::cout << "Errroneous item is here:" << std::endl; cout << "Errroneous item is here:" << endl;
bool first = true; bool first = true;
int cnt = 0; int cnt = 0;
if (pos > it) if (pos > it)
@@ -50,7 +51,7 @@ void printErrorPos(std::vector<std::string>::iterator it, const std::vector<std:
if (first == true) if (first == true)
first = false; first = false;
else { else {
std::cout << ';'; cout << ';';
if (it <= pos) { if (it <= pos) {
cnt++; cnt++;
} }
@@ -58,86 +59,86 @@ void printErrorPos(std::vector<std::string>::iterator it, const std::vector<std:
if (it < pos) { if (it < pos) {
cnt += (*it).length(); cnt += (*it).length();
} }
std::cout << (*it++); cout << (*it++);
} }
std::cout << std::endl; cout << endl;
std::cout << std::setw(cnt) << " " << std::setw(0) << "^" << std::endl; cout << setw(cnt) << " " << setw(0) << "^" << endl;
} }
int main() int main()
{ {
// message= [type];class;name;[comment];[QQ];ZZ;PBSB;fields... // message= [type];class;name;[comment];[QQ];ZZ;PBSB;fields...
// field= name;[pos];type[;[divisor|values][;[unit][;[comment]]]] // field= name;[pos];type[;[divisor|values][;[unit][;[comment]]]]
std::string checks[][5] = { string checks[][5] = {
// "message", "flags" // "message", "flags"
{";;first;;;fe;0700;x;;bda", "26.10.2014", "fffe0700042610001451", "00", ""}, {";;first;;;fe;0700;x;;bda", "26.10.2014", "fffe0700042610001451", "00", ""},
{"w;;first;;;15;b5090400;date;;bda", "26.10.2014", "ff15b5090604002610001445", "00", ""}, {"w;;first;;;15;b5090400;date;;bda", "26.10.2014", "ff15b5090604002610001445", "00", ""},
}; };
std::map<std::string, DataField*> templates; map<string, DataField*> templates;
Message* message = NULL; Message* message = NULL;
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]; string check[5] = checks[i];
std::istringstream isstr(check[0]); istringstream isstr(check[0]);
std::string inputStr = check[1]; string inputStr = check[1];
SymbolString mstr = SymbolString(check[2], false); SymbolString mstr = SymbolString(check[2], false);
SymbolString sstr = SymbolString(check[3], false); SymbolString sstr = SymbolString(check[3], false);
std::string flags = check[4]; string flags = check[4];
bool failedCreate = flags.find('c') != std::string::npos; bool failedCreate = flags.find('c') != string::npos;
bool failedPrepare = flags.find('p') != std::string::npos; bool failedPrepare = flags.find('p') != string::npos;
bool failedPrepareMatch = flags.find('P') != std::string::npos; bool failedPrepareMatch = flags.find('P') != string::npos;
std::string item; string item;
std::vector<std::string> entries; vector<string> entries;
while (std::getline(isstr, item, ';') != 0) while (getline(isstr, item, ';') != 0)
entries.push_back(item); entries.push_back(item);
if (message != NULL) { if (message != NULL) {
delete message; delete message;
message = NULL; message = NULL;
} }
std::vector<std::string>::iterator it = entries.begin(); vector<string>::iterator it = entries.begin();
result_t result = Message::create(it, entries.end(), templates, message); result_t result = Message::create(it, entries.end(), templates, message);
if (failedCreate == true) { if (failedCreate == true) {
if (result == RESULT_OK) if (result == RESULT_OK)
std::cout << "\"" << check[0] << "\": failed create error: unexpectedly succeeded" << std::endl; cout << "\"" << check[0] << "\": failed create error: unexpectedly succeeded" << endl;
else else
std::cout << "\"" << check[0] << "\": failed create OK" << std::endl; cout << "\"" << check[0] << "\": failed create OK" << endl;
continue; continue;
} }
if (result != RESULT_OK) { if (result != RESULT_OK) {
std::cout << "\"" << check[0] << "\": create error: " cout << "\"" << check[0] << "\": create error: "
<< getResultCode(result) << std::endl; << getResultCode(result) << endl;
printErrorPos(entries.begin(), entries.end(), it); printErrorPos(entries.begin(), entries.end(), it);
continue; continue;
} }
if (message == NULL) { if (message == NULL) {
std::cout << "\"" << check[0] << "\": create error: NULL" << std::endl; cout << "\"" << check[0] << "\": create error: NULL" << endl;
continue; continue;
} }
if (it != entries.end()) { if (it != entries.end()) {
std::cout << "\"" << check[0] << "\": create error: trailing input" << std::endl; cout << "\"" << check[0] << "\": create error: trailing input" << endl;
continue; continue;
} }
std::cout << "\"" << check[0] << "\": create OK" << std::endl; cout << "\"" << check[0] << "\": create OK" << endl;
std::istringstream input(inputStr); istringstream input(inputStr);
SymbolString writeMstr = SymbolString(); SymbolString writeMstr = SymbolString();
result = message->prepare(0xff, writeMstr, input); result = message->prepare(0xff, writeMstr, input);
if (failedPrepare == true) { if (failedPrepare == true) {
if (result == RESULT_OK) if (result == RESULT_OK)
std::cout << "\"" << check[0] << "\": failed prepare error: unexpectedly succeeded" << std::endl; cout << "\"" << check[0] << "\": failed prepare error: unexpectedly succeeded" << endl;
else else
std::cout << "\"" << check[0] << "\": failed prepare OK" << std::endl; cout << "\"" << check[0] << "\": failed prepare OK" << endl;
continue; continue;
} }
if (result != RESULT_OK) { if (result != RESULT_OK) {
std::cout << " prepare >" << inputStr << "< error: " cout << " prepare >" << inputStr << "< error: "
<< getResultCode(result) << std::endl; << getResultCode(result) << endl;
continue; continue;
} }
std::cout << " prepare >" << inputStr << "< OK" << std::endl; cout << " prepare >" << inputStr << "< OK" << endl;
bool match = writeMstr==mstr; bool match = writeMstr==mstr;
verify(failedPrepareMatch, "prepare", inputStr, match, mstr.getDataStr(), writeMstr.getDataStr()); verify(failedPrepareMatch, "prepare", inputStr, match, mstr.getDataStr(), writeMstr.getDataStr());
@@ -146,7 +147,7 @@ int main()
message = NULL; message = NULL;
} }
for (std::map<std::string, DataField*>::iterator it = templates.begin(); it != templates.end(); it++) for (map<string, DataField*>::iterator it = templates.begin(); it != templates.end(); it++)
delete it->second; delete it->second;
return 0; return 0;
+11 -10
View File
@@ -21,6 +21,8 @@
#include <iostream> #include <iostream>
#include <iomanip> #include <iomanip>
using namespace std;
int main () int main ()
{ {
SymbolString sstr = SymbolString("10feb5050427a915aa"); SymbolString sstr = SymbolString("10feb5050427a915aa");
@@ -30,18 +32,17 @@ int main ()
if (strcasecmp(gotStr.c_str(), expectStr.c_str()) == 0) if (strcasecmp(gotStr.c_str(), expectStr.c_str()) == 0)
std::cout << "ctor escaped successful." << std::endl; std::cout << "ctor escaped successful." << std::endl;
else else
std::cout << "ctor escaped invalid: got " << gotStr std::cout << "ctor escaped invalid: got " << gotStr << ", expected "
<< ", expected " << expectStr << std::endl; << expectStr << std::endl;
unsigned char gotCrc = sstr.getCRC(), expectCrc = 0x77; unsigned char gotCrc = sstr.getCRC(), expectCrc = 0x77;
if (gotCrc == expectCrc) if (gotCrc == expectCrc)
std::cout << "CRC successful." << std::endl; std::cout << "CRC successful." << std::endl;
else else
std::cout << "CRC invalid: got 0x" std::cout << "CRC invalid: got 0x" << std::nouppercase << std::setw(2)
<< std::nouppercase << std::setw(2) << std::hex << std::hex << std::setfill('0')
<< std::setfill('0') << static_cast<unsigned>(gotCrc) << static_cast<unsigned>(gotCrc) << ", expected 0x"
<< ", expected 0x"
<< std::nouppercase << std::setw(2) << std::hex << std::nouppercase << std::setw(2) << std::hex
<< std::setfill('0') << static_cast<unsigned>(expectCrc) << std::setfill('0') << static_cast<unsigned>(expectCrc)
<< std::endl; << std::endl;
@@ -51,8 +52,8 @@ int main ()
if (strcasecmp(gotStr.c_str(), expectStr.c_str()) == 0) if (strcasecmp(gotStr.c_str(), expectStr.c_str()) == 0)
std::cout << "unescape successful." << std::endl; std::cout << "unescape successful." << std::endl;
else else
std::cout << "unescape invalid: got " << gotStr std::cout << "unescape invalid: got " << gotStr << ", expected "
<< ", expected " << expectStr << std::endl; << expectStr << std::endl;
sstr = SymbolString("10feb5050427a90015a90177", true); sstr = SymbolString("10feb5050427a90015a90177", true);
@@ -61,8 +62,8 @@ int main ()
if (strcasecmp(gotStr.c_str(), expectStr.c_str()) == 0) if (strcasecmp(gotStr.c_str(), expectStr.c_str()) == 0)
std::cout << "ctor unescaped successful." << std::endl; std::cout << "ctor unescaped successful." << std::endl;
else else
std::cout << "ctor unescaped invalid: got " << gotStr std::cout << "ctor unescaped invalid: got " << gotStr << ", expected "
<< ", expected " << expectStr << std::endl; << expectStr << std::endl;
return 0; return 0;