solved some TODOs, check hex input and message length, simplified SymbolString ctors, fixed extra newline in scan result, no longer include write messages in default find command types, append command type to find result in verbose mode, add source and/or destination to update log when not set in the message definition, documentation

This commit is contained in:
john30
2015-02-15 12:20:06 +01:00
parent 2de793784e
commit 71d8727d2d
9 changed files with 177 additions and 136 deletions
+4 -4
View File
@@ -252,7 +252,7 @@ result_t Message::prepareMaster(const unsigned char srcAddress, SymbolString& ma
if (m_isPassive == true)
return RESULT_ERR_INVALID_ARG; // prepare not possible
SymbolString master;
SymbolString master(false);
result_t result = master.push_back(srcAddress, false, false);
if (result != RESULT_OK)
return result;
@@ -283,7 +283,7 @@ result_t Message::prepareMaster(const unsigned char srcAddress, SymbolString& ma
result = m_data->write(input, pt_masterData, master, m_id.size() - 2, separator);
if (result != RESULT_OK)
return result;
masterData = SymbolString(master, true);
masterData.addAll(master);
return result;
}
@@ -292,7 +292,7 @@ result_t Message::prepareSlave(SymbolString& slaveData)
if (m_isPassive == false || m_isWrite == true)
return RESULT_ERR_INVALID_ARG; // prepare not possible
SymbolString slave;
SymbolString slave(false);
unsigned char addData = m_data->getLength(pt_slaveData);
result_t result = slave.push_back(addData, false, false);
if (result != RESULT_OK)
@@ -301,7 +301,7 @@ result_t Message::prepareSlave(SymbolString& slaveData)
result = m_data->write(input, pt_slaveData, slave, 0);
if (result != RESULT_OK)
return result;
slaveData = SymbolString(slave, true);
slaveData.addAll(slave);
return result;
}
+22 -24
View File
@@ -48,37 +48,35 @@ static const unsigned char CRC_LOOKUP_TABLE[] =
};
SymbolString::SymbolString(const string& str) //TODO use a factory method instead
: m_unescapeState(0), m_crc(0)
void SymbolString::addAll(const SymbolString& str)
{
// parse + escape
for (size_t i = 0; i+1 < str.size(); i += 2) {
unsigned long value = strtoul(str.substr(i, 2).c_str(), NULL, 16); // TODO check
push_back((unsigned char)value, false, true);
bool addCrc = m_unescapeState == 0;
bool isEscaped = str.m_unescapeState == 0;
vector<unsigned char> data = str.m_data;
for (size_t i = 0; i < data.size(); i++) {
push_back(data[i], isEscaped, addCrc);
}
// add CRC + escape
push_back(m_crc, false, false);
if (addCrc)
push_back(m_crc, false, false); // add CRC
}
SymbolString::SymbolString(const SymbolString& str, const bool escape, const bool addCrc)
: m_unescapeState(escape == true ? 0 : 1), m_crc(0)
result_t SymbolString::parseHex(const string& str, const bool isEscaped)
{
for (size_t i = 0; i < str.size(); i++) {
push_back(str[i], str.m_unescapeState == 0, true);
}
if (addCrc == true)
// add CRC
push_back(m_crc, false, false);
}
bool addCrc = m_unescapeState == 0;
for (size_t i = 0; i < str.size(); i += 2) {
char* strEnd = NULL;
const char* strBegin = str.substr(i, 2).c_str();
unsigned int value = strtoul(strBegin, &strEnd, 16);
SymbolString::SymbolString(const string& str, bool isEscaped)
: m_unescapeState(1), m_crc(0)
{
// parse + optionally unescape
for (size_t i = 0; i+1 < str.size(); i += 2) {
unsigned long value = strtoul(str.substr(i, 2).c_str(), NULL, 16); // TODO check
push_back((unsigned char)value, isEscaped, false);
if (strEnd == NULL || *strEnd != 0 || strEnd != strBegin+2 || value > 0xff)
return RESULT_ERR_INVALID_NUM; // invalid value
push_back((unsigned char)value, isEscaped, addCrc);
}
if (addCrc)
push_back(m_crc, false, false); // add CRC
return RESULT_OK;
}
const string SymbolString::getDataStr(const bool unescape)
+22 -46
View File
@@ -45,30 +45,24 @@ class SymbolString
public:
/**
* Creates a new unescaped empty instance.
* Creates a new empty escaped or unescaped instance.
* @param escaped whether to create an escaped instance.
*/
SymbolString() : m_unescapeState(1), m_crc(0) {}
SymbolString(const bool escaped=true) : m_unescapeState(escaped == true ? 0 : 1), m_crc(0) {}
/**
* Creates a new escaped instance from an unescaped hex string and adds the calculated CRC.
* @param str the unescaped hex string.
* Add all symbols from the other @a SymbolString and the calculated CRC if escaped.
* @param str the @a SymbolString to copy from.
*/
SymbolString(const string& str);
void addAll(const SymbolString& str);
/**
* Creates a new escaped or unescaped instance from another @a SymbolString and adds the calculated CRC.
* @param str the @a SymbolString top copy from.
* @param escape true for an escaped instance, false for an unescaped instance.
* @param addCrc whether to add the calculated CRC as last symbol.
* Parse the escaped or unescaped hex @a string, add all symbols, and add the calculated CRC if escaped.
* @param str the hex @a string.
* @param isEscaped whether the hex string is escaped.
* @return @a RESULT_OK on success, or an error code.
*/
SymbolString(const SymbolString& str, const bool escape, const bool addCrc=true);
/**
* Creates a new unescaped instance from a hex string.
* @param isEscaped whether the hex string is escaped and shall be unescaped.
* @param str the hex string.
*/
SymbolString(const string& str, const bool isEscaped);
result_t parseHex(const string& str, const bool isEscaped=false);
/**
* Returns the symbols as hex string.
@@ -84,32 +78,12 @@ public:
*/
unsigned char& operator[](const size_t index) { if (index >= m_data.size()) m_data.resize(index+1, 0); return m_data[index]; }
/**
* Returns the symbol at the specified index.
* @param index the index of the symbol to return.
* @return the symbol at the specified index.
*/
const unsigned char& operator[](const size_t index) const { return m_data[index]; }
/**
* Returns whether this instance is equal to the other instance.
* @param other the other instance.
* @return true if this instance is equal to the other instance (i.e. both escaped or both unescaped and same symbols).
*/
bool operator==(SymbolString& other) {
return m_unescapeState==other.m_unescapeState && m_data==other.m_data;
/*bool ret = m_unescapeState==other.m_unescapeState && m_data==other.m_data;
for (int i=0; i<m_data.size(); i++) {
cout<<setw(2)<<setfill('0')<<hex<<static_cast<unsigned>(m_data[i])<<" ";
}
cout<<"["<<static_cast<unsigned>(m_unescapeState)<<"]";
cout<<(ret?" == ":" != ");
for (int i=0; i<other.m_data.size(); i++) {
cout<<setw(2)<<setfill('0')<<hex<<static_cast<unsigned>(other.m_data[i])<<" ";
}
cout<<"["<<static_cast<unsigned>(other.m_unescapeState)<<"]"<<endl;
return ret;*/
}
bool operator==(SymbolString& other) { return m_unescapeState==other.m_unescapeState && m_data==other.m_data; }
/**
* Appends a the symbol to the end of the symbol string and escapes/unescapes it if necessary.
@@ -135,10 +109,16 @@ public:
unsigned char getCRC() const { return m_crc; }
/**
* Clears the symbols.
* Clear the symbols.
*/
void clear() { m_data.clear(); m_unescapeState = m_unescapeState==0 ? 0 : 1; m_crc = 0; }
/**
* Clear the symbols and adjust the escape mode.
* @param escape true to set to an escaped instance, false to set to an unescaped instance.
*/
void clear(const bool escape) { m_data.clear(); m_unescapeState = escape ? 0 : 1; m_crc = 0; }
private:
/**
@@ -149,14 +129,12 @@ private:
: m_data(str.m_data), m_unescapeState(str.m_unescapeState), m_crc(str.m_crc) {}
/**
* Updates the calculated CRC in @a m_crc by adding a value.
* Update the calculated CRC in @a m_crc by adding a value.
* @param value the (escaped) value to add to the calculated CRC in @a m_crc.
*/
void addCRC(const unsigned char value);
/**
* the string of bus symbols.
*/
/** the string of bus symbols. */
vector<unsigned char> m_data;
/**
@@ -166,9 +144,7 @@ private:
*/
int m_unescapeState;
/**
* the calculated CRC.
*/
/** the calculated CRC. */
unsigned char m_crc;
};
+23 -5
View File
@@ -215,8 +215,18 @@ int main()
string check[5] = checks[i];
istringstream isstr(check[0]);
string expectStr = check[1];
SymbolString mstr(check[2], false);
SymbolString sstr(check[3], false);
SymbolString mstr(false);
result_t result = mstr.parseHex(check[2]);
if (result != RESULT_OK) {
cout << "\"" << check[0] << "\": parse \"" << check[2] << "\" error: " << getResultCode(result) << endl;
continue;
}
SymbolString sstr(false);
result = sstr.parseHex(check[3]);
if (result != RESULT_OK) {
cout << "\"" << check[0] << "\": parse \"" << check[3] << "\" error: " << getResultCode(result) << endl;
continue;
}
string flags = check[4];
bool isSet = flags.find('s') != string::npos;
bool failedCreate = flags.find('c') != string::npos;
@@ -237,7 +247,7 @@ int main()
fields = NULL;
}
vector<string>::iterator it = entries.begin();
result_t result = DataField::create(it, entries.end(), templates, fields, isSet, isTemplate ? SYN : mstr[1]);
result = DataField::create(it, entries.end(), templates, fields, isSet, isTemplate ? SYN : mstr[1]);
if (failedCreate == true) {
if (result == RESULT_OK)
cout << "\"" << check[0] << "\": failed create error: unexpectedly succeeded" << endl;
@@ -272,8 +282,16 @@ int main()
}
ostringstream output;
SymbolString writeMstr(mstr.getDataStr().substr(0, 10), false);
SymbolString writeSstr(sstr.getDataStr().substr(0, 2), false);
SymbolString writeMstr(false);
result = writeMstr.parseHex(mstr.getDataStr().substr(0, 10));
if (result != RESULT_OK) {
cout << " parse \"" << mstr.getDataStr().substr(0, 10) << "\" error: " << getResultCode(result) << endl;
}
SymbolString writeSstr(false);
result = writeSstr.parseHex(sstr.getDataStr().substr(0, 2));
if (result != RESULT_OK) {
cout << " parse \"" << sstr.getDataStr().substr(0, 2) << "\" error: " << getResultCode(result) << endl;
}
result = fields->read(pt_masterData, mstr, 0, output, false, verbose);
if (result >= RESULT_OK) {
result = fields->read(pt_slaveData, sstr, 0, output, output.str().empty() == false, verbose);
+12 -3
View File
@@ -74,8 +74,18 @@ int main()
string check[5] = checks[i];
istringstream isstr(check[0]);
string inputStr = check[1];
SymbolString mstr(check[2]);
SymbolString sstr(check[3]);
SymbolString mstr(true);
result_t result = mstr.parseHex(check[2]);
if (result != RESULT_OK) {
cout << "\"" << check[0] << "\": parse \"" << check[2] << "\" error: " << getResultCode(result) << endl;
continue;
}
SymbolString sstr(true);
result = sstr.parseHex(check[3]);
if (result != RESULT_OK) {
cout << "\"" << check[0] << "\": parse \"" << check[3] << "\" error: " << getResultCode(result) << endl;
continue;
}
string flags = check[4];
bool isTemplate = flags == "t";
bool dontMap = flags.find('m') != string::npos;
@@ -94,7 +104,6 @@ int main()
delete deleteMessage;
deleteMessage = NULL;
}
result_t result;
if (isTemplate == true) {
// store new template
DataField* fields = NULL;
+13 -6
View File
@@ -25,14 +25,18 @@ using namespace std;
int main ()
{
SymbolString sstr("10feb5050427a915aa");
SymbolString sstr(true);
result_t result = sstr.parseHex("10feb5050427a915aa", false);
if (result != RESULT_OK)
std::cout << "parse escaped error: " << getResultCode(result) << std::endl;
std::string gotStr = sstr.getDataStr(false), expectStr = "10feb5050427a90015a90177";
if (strcasecmp(gotStr.c_str(), expectStr.c_str()) == 0)
std::cout << "ctor escaped OK" << std::endl;
std::cout << "parse escaped OK" << std::endl;
else
std::cout << "ctor escaped error: got " << gotStr << ", expected "
std::cout << "parse escaped error: got " << gotStr << ", expected "
<< expectStr << std::endl;
unsigned char gotCrc = sstr.getCRC(), expectCrc = 0x77;
@@ -55,14 +59,17 @@ int main ()
std::cout << "unescape error: got " << gotStr << ", expected "
<< expectStr << std::endl;
sstr = SymbolString("10feb5050427a90015a90177", true);
sstr = SymbolString(false);
result = sstr.parseHex("10feb5050427a90015a90177", true);
if (result != RESULT_OK)
std::cout << "parse unescaped error: " << getResultCode(result) << std::endl;
gotStr = sstr.getDataStr();
if (strcasecmp(gotStr.c_str(), expectStr.c_str()) == 0)
std::cout << "ctor unescaped OK" << std::endl;
std::cout << "parse unescaped OK" << std::endl;
else
std::cout << "ctor unescaped error: got " << gotStr << ", expected "
std::cout << "parse unescaped error: got " << gotStr << ", expected "
<< expectStr << std::endl;
return 0;