added getSlaveAddress(), formatting

This commit is contained in:
john30
2016-11-19 11:29:58 +01:00
parent 4ac65925b7
commit c8aedf2bf5
2 changed files with 67 additions and 50 deletions
+49 -39
View File
@@ -59,8 +59,9 @@ void SymbolString::addAll(const SymbolString& str, bool skipLastSymbol)
for (size_t i = 0; i < end; i++) {
push_back(data[i], isEscaped, addCrc);
}
if (addCrc)
if (addCrc) {
push_back(m_crc, false, false); // add CRC
}
}
result_t SymbolString::parseHex(const string& str, const bool isEscaped)
@@ -71,14 +72,14 @@ result_t SymbolString::parseHex(const string& str, const bool isEscaped)
const char* strBegin = str.substr(i, 2).c_str();
unsigned long int value = strtoul(strBegin, &strEnd, 16);
if (strEnd == NULL || strEnd != strBegin+2 || value > 0xff)
if (strEnd == NULL || strEnd != strBegin+2 || value > 0xff) {
return RESULT_ERR_INVALID_NUM; // invalid value
}
push_back((unsigned char)value, isEscaped, addCrc);
}
if (addCrc)
if (addCrc) {
push_back(m_crc, false, false); // add CRC
}
return RESULT_OK;
}
@@ -91,24 +92,22 @@ const string SymbolString::getDataStr(const bool unescape, const bool skipLastSy
unsigned char value = m_data[i];
if (m_unescapeState == 0 && unescape && previousEscape) {
if (!skipLastSymbol || i+1 < m_data.size()) {
if (value == 0x00)
if (value == 0x00) {
sstr << "a9"; // ESC
else if (value == 0x01)
} else if (value == 0x01) {
sstr << "aa"; // SYN
else
} else {
sstr << "XX"; // invalid escape sequence
}
}
previousEscape = false;
}
else if (m_unescapeState == 0 && unescape && value == ESC) {
} else if (m_unescapeState == 0 && unescape && value == ESC) {
previousEscape = true; // escape sequence not yet finished
}
else if (!skipLastSymbol || i+1 < m_data.size()) {
} else if (!skipLastSymbol || i+1 < m_data.size()) {
sstr << nouppercase << setw(2) << hex
<< setfill('0') << static_cast<unsigned>(value);
}
}
return sstr.str();
}
@@ -122,46 +121,43 @@ result_t SymbolString::push_back(const unsigned char value, const bool isEscaped
addCRC(ESC);
addCRC(0x00);
}
}
else if (!isEscaped && value == SYN) {
} else if (!isEscaped && value == SYN) {
m_data.push_back(ESC);
m_data.push_back(0x01);
if (updateCRC) {
addCRC(ESC);
addCRC(0x01);
}
}
else {
} else {
m_data.push_back(value);
if (updateCRC)
if (updateCRC) {
addCRC(value);
}
}
return RESULT_OK;
}
else if (!isEscaped) {
if (m_unescapeState != 1)
if (!isEscaped) {
if (m_unescapeState != 1) {
return RESULT_ERR_ESC; // invalid unescape state
}
m_data.push_back(value);
if (updateCRC) {
if (value == ESC) {
addCRC(ESC);
addCRC(0x00);
}
else if (value == SYN) {
} else if (value == SYN) {
addCRC(ESC);
addCRC(0x01);
}
else {
} else {
addCRC(value);
}
}
return RESULT_OK;
}
else if (m_unescapeState != 1) {
if (updateCRC)
if (m_unescapeState != 1) {
if (updateCRC) {
addCRC(value);
}
if (value == 0x00) {
m_data.push_back(ESC);
m_unescapeState = 1;
@@ -174,16 +170,16 @@ result_t SymbolString::push_back(const unsigned char value, const bool isEscaped
}
return RESULT_ERR_ESC; // invalid escape sequence
}
else if (value == ESC) {
if (updateCRC)
if (value == ESC) {
if (updateCRC) {
addCRC(value);
}
m_unescapeState = 2;
return RESULT_CONTINUE;
}
if (updateCRC)
if (updateCRC) {
addCRC(value);
}
m_data.push_back(value);
return RESULT_OK;
}
@@ -206,14 +202,24 @@ bool isSlaveMaster(unsigned char addr) {
return isMaster((unsigned char)(addr+256-5));
}
unsigned char getSlaveAddress(unsigned char addr) {
if (isMaster(addr)) {
return (unsigned char)(addr+5);
}
if (isValidAddress(addr, false)) {
return addr;
}
return SYN;
}
unsigned char getMasterAddress(unsigned char addr) {
if (isMaster(addr))
if (isMaster(addr)) {
return addr;
}
addr = (unsigned char)(addr+256-5);
if (isMaster(addr))
if (isMaster(addr)) {
return addr;
}
return SYN;
}
@@ -241,9 +247,13 @@ unsigned char getMasterPartIndex(unsigned char bits) {
}
unsigned char getMasterNumber(unsigned char addr) {
unsigned char priority = getMasterPartIndex(addr & 0x0F);
if (priority==0) return 0;
if (priority==0) {
return 0;
}
unsigned char index = getMasterPartIndex((addr & 0xF0) >> 4);
if (index==0) return 0;
if (index==0) {
return 0;
}
return (unsigned char)(5*(priority-1) + index);
}
+18 -11
View File
@@ -99,7 +99,7 @@ public:
result_t parseHex(const string& str, const bool isEscaped=false);
/**
* Returns the symbols as hex string.
* Return the symbols as hex string.
* @param unescape whether to unescape an escaped instance.
* @param skipLastSymbol whether to skip the last symbol (probably the CRC).
* @return the symbols as hex string.
@@ -107,21 +107,21 @@ public:
const string getDataStr(const bool unescape=true, const bool skipLastSymbol=true);
/**
* Returns a reference to the symbol at the specified index.
* Return a reference to the symbol at the specified index.
* @param index the index of the symbol to return.
* @return the reference to the symbol at the specified index.
*/
unsigned char& operator[](const size_t index) { if (index >= m_data.size()) m_data.resize(index+1, 0); return m_data[index]; }
/**
* Returns whether this instance is equal to the other instance.
* Return 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; }
/**
* Returns whether this instance is different from the other instance.
* Return whether this instance is different from the other instance.
* @param other the other instance.
* @return true if this instance is different from the other instance.
*/
@@ -154,13 +154,13 @@ public:
result_t push_back(const unsigned char value, const bool isEscaped=true, const bool updateCRC=true);
/**
* Returns the number of symbols in this symbol string.
* Return the number of symbols in this symbol string.
* @return the number of available symbols.
*/
unsigned char size() const { return (unsigned char)m_data.size(); }
/**
* Returns the calculated CRC.
* Return the calculated CRC.
* @return the calculated CRC.
*/
unsigned char getCRC() const { return m_crc; }
@@ -207,35 +207,42 @@ private:
/**
* Returns whether the address is one of the 25 master addresses.
* Return whether the address is one of the 25 master addresses.
* @param addr the address to check.
* @return <code>true</code> if the specified address is a master address.
*/
bool isMaster(unsigned char addr);
/**
* Returns whether the address is a slave address of one of the 25 masters.
* Return whether the address is a slave address of one of the 25 masters.
* @param addr the address to check.
* @return <code>true</code> if the specified address is a slave address of a master.
*/
bool isSlaveMaster(unsigned char addr);
/**
* Returns the master address associated with the specified address (master or slave).
* Return the slave address associated with the specified address (master or slave).
* @param addr the address to check.
* @return the slave address, or SYN if the specified address is neither a master address nor a slave address of a master.
*/
unsigned char getSlaveAddress(unsigned char addr);
/**
* Return the master address associated with the specified address (master or slave).
* @param addr the address to check.
* @return the master address, or SYN if the specified address is neither a master address nor a slave address of a master.
*/
unsigned char getMasterAddress(unsigned char addr);
/**
* Returns the number of the master if the address is a valid bus address.
* Return the number of the master if the address is a valid bus address.
* @param addr the bus address.
* @return the number of the master if the address is a valid bus address (1 to 25), or 0.
*/
unsigned char getMasterNumber(unsigned char addr);
/**
* Returns whether the address is a valid bus address.
* Return whether the address is a valid bus address.
* @param addr the address to check.
* @param allowBroadcast whether to also allow @a addr to be the broadcast address (default true).
* @return <code>true</code> if the specified address is a valid bus address.