/* message.h Copyright (c) 2021 Kevin Matz (kevin.matz@gmail.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #pragma once #include #include #include #include "rdm.h" #include "uid.h" namespace RDM { struct Message; //!< \cite RDM 6 Message Structure using MsgPtr = std::shared_ptr; //!< Managed RDM Message pointer. /** * @brief \cite RDM 6.2 Packet Format * * \cite RDM All RDM packets shall use the following message structure, with the exception * of the Discovery Unique Branch response message. */ struct Message { explicit Message(); UID destination; //!< \cite RDM 6.2.4 Destination UID UID source; //!< \cite RDM 6.2.5 Source UID uint8_t tn; //!< \cite RDM 6.2.6 Transaction Number (TN) union { uint8_t portID; //!< \cite RDM 6.2.7.1 Port ID for Controller Generated Messages uint8_t responseType; //!< \cite RDM 6.2.7.2 Response Type for Responder Generated Messages }; uint8_t messageCount; //!< \cite RDM 6.2.8 Message Count uint16_t subDevice; //!< \cite RDM 6.2.9 Sub-Device Field /// \cite RDM Table 6-4 shows the format of the Message Data Block portion /// of the data packet from Table 6-2. struct DataBlock { uint8_t cc; //!< \cite RDM 6.2.10.1 Command Class (CC) PID pid; //!< \cite RDM 6.2.10.2 Parameter ID (PID) uint8_t pdl() const {return pd.size();} //!< \cite RDM 6.2.10.3 Parameter Data Length (PDL) @return std::vector pd; //!< \cite RDM 6.2.10.4 Parameter Data (PD) } mdb; //!< \cite RDM 6.2.10 Message Data Block (MDB) uint16_t checksum() const; //!< \cite RDM 6.2.11 Checksum union { uint8_t failure_mode; struct { bool short_message : 1; //!< undersized buffer bool length_mismatch : 1; //!< message longer than adervertised bool checksum_fail : 1; //!< message did not match checksum bool incorrect_sc : 1; //!< invalid StartCode bool incorrect_sub_sc: 1; //!< invalid sub-StartCode bool do_not_send : 1; //!< message does not require transmission }; }; void read(const std::vector &buffer); void write(std::vector &buffer) const; void nak(uint16_t reason); private: void writeDiscBranch(std::vector &buffer) const; public: // templates /** * @brief Big-Endian Read from std::vector * @param buffer * @param index * @return * * > \cite RDM 6.1 Byte Ordering * > All multi-byte data shall be transmitted in Big-Endian order. */ template static T readType(const std::vector &buffer, const size_t index) { if (buffer.size() < sizeof(T)) return 0; T val = 0; auto data = reinterpret_cast(&val); for (int i = 0; i < sizeof(T); i++) data[sizeof(T)-1-i] = buffer[index + i]; return val; } /** * @brief Big-Endian Write to std::vector * @param buffer * @param val * * > \cite RDM 6.1 Byte Ordering * > All multi-byte data shall be transmitted in Big-Endian order. */ template static void writeType(std::vector &buffer, const T &val) { auto data = reinterpret_cast(&val); for (int i = 0; i < sizeof(T); i++) buffer.push_back(data[sizeof(T)-1-i]); } /** * @brief Endian-correct appending of Parameter Data * @param val */ template void appendParameterData(const T &val) { Message::writeType(mdb.pd, val); } private: // templates /** * @brief addSum_ * @param sum * @param val */ template static void addSum_(uint16_t &sum, const T &val) { uint8_t byte; uint16_t carry; auto data = reinterpret_cast(&val); for (int i = sizeof(T); --i >= 0; ) { byte = data[i]; while (byte != 0) { carry = sum & byte; sum = sum ^ byte; byte = carry << 1; } } } }; // struct Message } // namespace RDM