2021-08-08 17:25:41 -04:00
|
|
|
/*
|
|
|
|
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 <cstdint>
|
2021-08-11 10:28:03 -04:00
|
|
|
#include <memory>
|
2021-08-08 17:25:41 -04:00
|
|
|
#include <vector>
|
2021-08-11 10:28:03 -04:00
|
|
|
|
2021-08-08 17:25:41 -04:00
|
|
|
#include "rdm.h"
|
|
|
|
#include "uid.h"
|
|
|
|
|
|
|
|
namespace RDM {
|
|
|
|
|
2021-08-11 10:28:03 -04:00
|
|
|
struct Message;
|
|
|
|
|
2021-08-25 17:20:33 -04:00
|
|
|
using MsgPtr = std::shared_ptr<Message>; //!< valid RDM Message
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The Message struct
|
|
|
|
*/
|
2021-08-08 17:25:41 -04:00
|
|
|
struct Message
|
|
|
|
{
|
2022-12-10 11:07:13 -05:00
|
|
|
explicit Message();
|
|
|
|
explicit Message(const Message &obj);
|
|
|
|
explicit Message(const std::vector<uint8_t> &data);
|
2021-08-10 13:33:24 -04:00
|
|
|
|
|
|
|
void read(const std::vector<uint8_t> &data);
|
|
|
|
void write(std::vector<uint8_t> &data) const;
|
2021-08-10 16:08:09 -04:00
|
|
|
void nak(uint16_t reason);
|
2021-08-10 13:33:24 -04:00
|
|
|
|
2021-08-25 17:20:33 -04:00
|
|
|
UID source; //!< source UID
|
|
|
|
UID destination; //!< destination UID
|
|
|
|
uint8_t transaction; //!< transaction number
|
2021-08-08 17:25:41 -04:00
|
|
|
union {
|
2021-08-25 17:20:33 -04:00
|
|
|
uint8_t portID; //!< destination port
|
|
|
|
uint8_t responseType; //!< type of response
|
2021-08-08 17:25:41 -04:00
|
|
|
};
|
2021-08-25 17:20:33 -04:00
|
|
|
uint8_t messageCount; //!< message count number
|
|
|
|
uint16_t subDevice; //!< destination subdevice
|
|
|
|
uint8_t commandClass; //!< message command class
|
|
|
|
PID parameterId; //!< PID
|
2021-08-08 17:25:41 -04:00
|
|
|
|
2021-08-10 15:35:27 -04:00
|
|
|
union {
|
|
|
|
uint8_t failure_mode;
|
|
|
|
struct {
|
2021-08-25 17:20:33 -04:00
|
|
|
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
|
2021-08-10 15:35:27 -04:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-08-25 17:20:33 -04:00
|
|
|
const std::vector<uint8_t>* data() const;
|
|
|
|
uint8_t length() const;
|
2021-08-10 13:33:24 -04:00
|
|
|
uint16_t checksum() const;
|
2021-08-11 10:28:03 -04:00
|
|
|
bool requiredLength(const size_t length, MsgPtr response) const;
|
2021-08-25 17:20:33 -04:00
|
|
|
/**
|
|
|
|
* @brief appendData
|
|
|
|
* @param val
|
|
|
|
*/
|
2021-08-08 17:25:41 -04:00
|
|
|
template<typename T>
|
|
|
|
void appendData(const T & val)
|
|
|
|
{
|
|
|
|
Message::writeType<T>(data_, val);
|
|
|
|
}
|
2021-08-25 17:20:33 -04:00
|
|
|
/**
|
|
|
|
* @brief readType
|
|
|
|
* @param vect
|
|
|
|
* @param start
|
|
|
|
* @return
|
|
|
|
*/
|
2021-08-08 17:25:41 -04:00
|
|
|
template<typename T>
|
|
|
|
static T readType(const std::vector<uint8_t>& vect, size_t start)
|
|
|
|
{
|
|
|
|
if (vect.size() < sizeof(T))
|
|
|
|
return 0;
|
|
|
|
T ret = 0;
|
|
|
|
auto data = reinterpret_cast<uint8_t*>(&ret);
|
|
|
|
for (int i = sizeof(T); --i >= 0; )
|
|
|
|
data[i] = vect[start + i];
|
|
|
|
return ret;
|
|
|
|
}
|
2021-08-25 17:20:33 -04:00
|
|
|
/**
|
|
|
|
* @brief writeType
|
|
|
|
* @param data
|
|
|
|
* @param val
|
|
|
|
*/
|
2021-08-08 17:25:41 -04:00
|
|
|
template<typename T>
|
2023-04-26 14:34:45 -04:00
|
|
|
static void writeType(std::vector<uint8_t> &data, const T val)
|
2021-08-08 17:25:41 -04:00
|
|
|
{
|
2023-04-26 14:34:45 -04:00
|
|
|
auto raw = reinterpret_cast<const uint8_t*>(&val);
|
2021-08-08 17:25:41 -04:00
|
|
|
for (int i = sizeof(T); --i >= 0; )
|
|
|
|
data.push_back(raw[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<uint8_t> data_;
|
2021-08-11 01:26:51 -04:00
|
|
|
void writeDiscBranch(std::vector<uint8_t> &data) const;
|
2021-08-08 17:25:41 -04:00
|
|
|
|
|
|
|
template<typename T>
|
2023-04-26 14:34:45 -04:00
|
|
|
uint16_t addSum_(uint16_t sum, const T val) const
|
2021-08-08 17:25:41 -04:00
|
|
|
{
|
|
|
|
uint16_t carry;
|
2023-04-26 14:34:45 -04:00
|
|
|
auto raw = reinterpret_cast<const uint8_t*>(&val);
|
2021-08-08 17:25:41 -04:00
|
|
|
for (int i = sizeof(T); --i >= 0; )
|
|
|
|
{
|
|
|
|
uint8_t num = raw[i];
|
|
|
|
while (num != 0)
|
|
|
|
{
|
|
|
|
carry = sum & num;
|
|
|
|
sum = sum ^ num;
|
|
|
|
num = carry << 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sum;
|
|
|
|
}
|
2021-08-11 10:28:03 -04:00
|
|
|
}; // struct Message
|
|
|
|
|
2021-08-08 17:25:41 -04:00
|
|
|
|
|
|
|
} // namespace RDM
|
|
|
|
|