105 lines
2.7 KiB
C
105 lines
2.7 KiB
C
|
/*
|
||
|
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>
|
||
|
#include <vector>
|
||
|
#include "rdm.h"
|
||
|
#include "uid.h"
|
||
|
|
||
|
namespace RDM {
|
||
|
|
||
|
struct Message
|
||
|
{
|
||
|
UID source;
|
||
|
UID destination;
|
||
|
uint8_t transaction;
|
||
|
union {
|
||
|
uint8_t portID;
|
||
|
uint8_t responseType;
|
||
|
};
|
||
|
uint8_t messageCount;
|
||
|
uint16_t subDevice;
|
||
|
uint8_t commandClass;
|
||
|
PID propertyID;
|
||
|
|
||
|
Message();
|
||
|
Message(const Message &obj);
|
||
|
~Message();
|
||
|
|
||
|
const std::vector<uint8_t>* data() const { return &data_; }
|
||
|
uint8_t length() const { return data_.size(); }
|
||
|
uint16_t checksum();
|
||
|
|
||
|
template<typename T>
|
||
|
void appendData(const T & val)
|
||
|
{
|
||
|
Message::writeType<T>(data_, val);
|
||
|
}
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
template<typename T>
|
||
|
static void writeType(std::vector<uint8_t>& data, T val)
|
||
|
{
|
||
|
auto raw = reinterpret_cast<uint8_t*>(val);
|
||
|
for (int i = sizeof(T); --i >= 0; )
|
||
|
data.push_back(raw[i]);
|
||
|
}
|
||
|
|
||
|
private:
|
||
|
std::vector<uint8_t> data_;
|
||
|
|
||
|
template<typename T>
|
||
|
uint16_t addSum_(uint16_t sum, T val) const
|
||
|
{
|
||
|
uint16_t carry;
|
||
|
auto raw = reinterpret_cast<uint8_t*>(val);
|
||
|
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;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
} // namespace RDM
|
||
|
|