use result_t, added getMasterNumber(), documentation

This commit is contained in:
john30
2014-11-23 13:32:38 +01:00
parent 6e0ddb1e8b
commit ea63e9d461
2 changed files with 56 additions and 6 deletions
+44 -1
View File
@@ -99,7 +99,7 @@ const string SymbolString::getDataStr(const bool unescape)
return sstr.str();
}
int SymbolString::push_back(const unsigned char value, const bool isEscaped, const bool updateCRC)
result_t SymbolString::push_back(const unsigned char value, const bool isEscaped, const bool updateCRC)
{
if (m_unescapeState == 0) { // store escaped data
if (isEscaped == false && value == ESC) {
@@ -189,6 +189,49 @@ bool isMaster(unsigned char addr) {
&& ((addrLo == 0x0) || (addrLo == 0x1) || (addrLo == 0x3) || (addrLo == 0x7) || (addrLo == 0xF));
}
unsigned char getMasterNumber(unsigned char addr) {
unsigned char addrHi = (addr & 0xF0) >> 4;
unsigned char addrLo = (addr & 0x0F);
unsigned char index;
switch (addrHi)
{
case 0x0:
index = 0;
break;
case 0x1:
index = 1;
break;
case 0x3:
index = 2;
break;
case 0x7:
index = 3;
break;
case 0xF:
index = 4;
break;
default:
return 0;
}
switch (addrLo)
{
case 0x0:
return 5*index + 1;
case 0x1:
return 5*index + 2;
case 0x3:
return 5*index + 3;
case 0x7:
return 5*index + 4;
case 0xF:
return 5*index + 5;
default:
return 0;
}
}
bool isValidAddress(unsigned char addr, bool allowBroadcast) {
return addr != SYN && addr != ESC && (allowBroadcast == true || addr != BROADCAST);
}
+12 -5
View File
@@ -20,6 +20,7 @@
#ifndef LIBEBUS_SYMBOL_H_
#define LIBEBUS_SYMBOL_H_
#include "result.h"
#include <cstring>
#include <cstdlib>
#include <sstream>
@@ -43,7 +44,6 @@ class SymbolString
public:
/**
* @brief Creates a new unescaped empty instance.
* @param escaped whether to create an escaped instance.
*/
SymbolString() : m_unescapeState(1), m_crc(0) {}
/**
@@ -90,12 +90,12 @@ public:
* RESULT_IN_ESC if this is an unescaped instance and the symbol is escaped and the start of the escape sequence was received,
* RESULT_ERR_ESC if this is an unescaped instance and an invalid escaped sequence was detected.
*/
int push_back(const unsigned char value, const bool isEscaped, const bool updateCRC=true);
result_t push_back(const unsigned char value, const bool isEscaped, const bool updateCRC=true);
/**
* @brief Returns the number of symbols in this symbol string.
* @return the number of available symbols.
*/
size_t size() const { return m_data.size(); }
unsigned char size() const { return (unsigned char)m_data.size(); }
/**
* @brief Returns the calculated CRC.
* @return the calculated CRC.
@@ -131,14 +131,21 @@ private:
/**
* Returns whether the address is one of the 25 master addresses.
* @brief Returns 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 valid bus address.
* @brief Returns 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);
/**
* @brief Returns 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.