149 lines
3.5 KiB
C++
149 lines
3.5 KiB
C++
/*
|
|
dmp.cpp
|
|
|
|
Copyright (c) 2020 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.
|
|
*/
|
|
|
|
#include "dmp.h"
|
|
|
|
namespace ACN {
|
|
namespace DMP {
|
|
|
|
/**
|
|
* @brief address_type::address_type
|
|
* @param val
|
|
*/
|
|
address_type::address_type(const uint8_t val)
|
|
{
|
|
z_reserved = (val >> 7) & 0b1;
|
|
relative = (val >> 6) & 0b1;
|
|
type = (data_type)((val >> 4) & 0b11);
|
|
x_reserved = (val >> 2) & 0b11;
|
|
width = (address_length)(val & 0b11);
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief range::range
|
|
* @param stream
|
|
* @param t
|
|
* @param l
|
|
*/
|
|
range::range(PDU::Stream stream, data_type t, address_length l) :
|
|
incriment(0),
|
|
count(0)
|
|
{
|
|
address = read(stream, l);
|
|
if (t == SINGLE) return;
|
|
incriment = read(stream, l);
|
|
count = read(stream, l);
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief range::read
|
|
* @param stream
|
|
* @param length
|
|
* @return
|
|
*/
|
|
uint32_t range::read(PDU::Stream stream, address_length length)
|
|
{
|
|
switch (length) {
|
|
case ONE:
|
|
return stream->readType<uint8_t>();
|
|
case TWO:
|
|
return stream->readType<uint16_t>();
|
|
case FOUR:
|
|
return stream->readType<uint32_t>();
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Pdu::Pdu
|
|
* @param stream
|
|
*/
|
|
Pdu::Pdu(PDU::Stream stream)
|
|
: PDU::Pdu(stream, 1) // vectors are 1 octet
|
|
{
|
|
if (stream->fail()) return;
|
|
if (!stream_->good()) return;
|
|
|
|
if (flags_.hasHeader)
|
|
setHeader(new address_type(stream_->readType<uint8_t>()));
|
|
|
|
if (flags_.hasData) {
|
|
switch(vector()) {
|
|
case SET_PROPERTY:
|
|
readSetData();
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Pdu::readSetData
|
|
*/
|
|
void Pdu::readSetData()
|
|
{
|
|
// header may be inherited. check that one exists
|
|
if (!header()) {
|
|
stream_->setstate(std::ios_base::failbit);
|
|
return;
|
|
}
|
|
|
|
auto header = (address_type*)this->header();
|
|
auto data = new dmp_set_data();
|
|
|
|
while(stream_->good()) {
|
|
// Property Address
|
|
range pr(stream_, header->type, header->width);
|
|
|
|
if (!stream_->good() || pr.count * pr.incriment > stream_->available()) {
|
|
stream_->setstate(std::ios_base::failbit);
|
|
break;
|
|
}
|
|
|
|
// Property Data
|
|
std::vector<uint8_t> pd;
|
|
pd.resize(pr.address, 0);
|
|
for (uint32_t i = 0; i < pr.count * pr.incriment; i ++)
|
|
pd.push_back(stream_->readType<uint8_t>());
|
|
|
|
// Property Fields
|
|
set_property fields(pr, pd);
|
|
data->properties.push_back(fields);
|
|
|
|
//set EOF if buffer insufficient for another property range
|
|
if (stream_->available() < header->width * (header->type == SINGLE? 1 : 3))
|
|
stream_->setstate(std::ios_base::eofbit);
|
|
}
|
|
setData(data);
|
|
}
|
|
|
|
} // DMP
|
|
} // ACN
|