/* * Copyright (C) John Baier 2014 * * This file is part of ebusd. * * ebusd is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * ebusd is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with ebusd. If not, see http://www.gnu.org/licenses/. */ #ifndef LIBEBUS_SYMBOL_H_ #define LIBEBUS_SYMBOL_H_ #include "result.h" #include #include #include #include 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 SYN = 0xAA; // synchronization symbol static const unsigned char ACK = 0x00; // positive acknowledge static const unsigned char NAK = 0xFF; // negative acknowledge static const unsigned char BROADCAST = 0xFE; // the broadcast destination address /** * @brief A string of escaped or unescaped bus symbols. */ class SymbolString { public: /** * @brief Creates a new unescaped empty instance. */ SymbolString() : m_unescapeState(1), m_crc(0) {} /** * @brief Creates a new escaped instance from an unescaped hex string and adds the calculated CRC. * @param str the unescaped hex string. */ SymbolString(const string& str); /** * @brief Creates a new escaped or unescaped instance from another @a SymbolString and adds the calculated CRC. * @param str the @a SymbolString top copy from. */ SymbolString(const SymbolString& str, const bool escape, const bool addCrc=true); /** * @brief 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); /** * @brief Returns the symbols as hex string. * @param unescape whether to unescape an escaped instance. * @return the symbols as hex string. */ const string getDataStr(const bool unescape=true); /** * @brief Returns 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]; } /** * @brief 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]; } /** * @brief 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[i])<<" "; } cout<<"["<(m_unescapeState)<<"]"; cout<<(ret?" == ":" != "); for (int i=0; i(other.m_data[i])<<" "; } cout<<"["<(other.m_unescapeState)<<"]"<