From ea63e9d461dc37423d3030a45d2a9be77b9321cb Mon Sep 17 00:00:00 2001 From: john30 Date: Sun, 23 Nov 2014 13:32:38 +0100 Subject: [PATCH] use result_t, added getMasterNumber(), documentation --- src/lib/ebus/symbol.cpp | 45 ++++++++++++++++++++++++++++++++++++++++- src/lib/ebus/symbol.h | 17 +++++++++++----- 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/src/lib/ebus/symbol.cpp b/src/lib/ebus/symbol.cpp index 730003f6..0ce1fbcb 100644 --- a/src/lib/ebus/symbol.cpp +++ b/src/lib/ebus/symbol.cpp @@ -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); } diff --git a/src/lib/ebus/symbol.h b/src/lib/ebus/symbol.h index e8b3b136..cd117362 100644 --- a/src/lib/ebus/symbol.h +++ b/src/lib/ebus/symbol.h @@ -20,6 +20,7 @@ #ifndef LIBEBUS_SYMBOL_H_ #define LIBEBUS_SYMBOL_H_ +#include "result.h" #include #include #include @@ -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 true 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 true if the specified address is a valid bus address.