2021-05-27 10:59:22 -04:00
|
|
|
/*
|
|
|
|
pdu.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 "pdu.h"
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
namespace ACN {
|
|
|
|
namespace PDU {
|
|
|
|
|
2021-07-26 17:22:24 -04:00
|
|
|
/**
|
|
|
|
* @brief Pdu::Pdu
|
|
|
|
* @param stream
|
|
|
|
* @param vector_size
|
|
|
|
*/
|
2021-07-29 19:26:49 -04:00
|
|
|
Pdu::Pdu(size_t vector_size)
|
|
|
|
: vector_size_(vector_size)
|
2021-05-27 10:59:22 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-26 17:22:24 -04:00
|
|
|
/**
|
|
|
|
* @brief Pdu::~Pdu
|
|
|
|
*/
|
2021-07-26 21:15:20 -04:00
|
|
|
Pdu::~Pdu()
|
|
|
|
{
|
2021-05-27 10:59:22 -04:00
|
|
|
if (header_) delete header_;
|
|
|
|
if (data_) delete data_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-26 17:22:24 -04:00
|
|
|
/**
|
|
|
|
* @brief Pdu::vector
|
|
|
|
* @return
|
|
|
|
*/
|
2021-08-02 10:10:55 -04:00
|
|
|
uint32_t Pdu::vector()
|
2021-07-26 21:15:20 -04:00
|
|
|
{
|
2021-05-27 10:59:22 -04:00
|
|
|
if (flags_.hasVector)
|
|
|
|
return vector_;
|
2021-07-28 10:44:11 -04:00
|
|
|
if (inherit_)
|
2021-05-27 10:59:22 -04:00
|
|
|
return inherit_->vector();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-26 17:22:24 -04:00
|
|
|
/**
|
|
|
|
* @brief Pdu::header
|
|
|
|
* @return
|
|
|
|
*/
|
2021-07-26 21:15:20 -04:00
|
|
|
pdu_header * Pdu::header()
|
|
|
|
{
|
2021-05-27 10:59:22 -04:00
|
|
|
if (flags_.hasHeader)
|
|
|
|
return header_;
|
2021-07-28 10:44:11 -04:00
|
|
|
if (inherit_)
|
2021-05-27 10:59:22 -04:00
|
|
|
return inherit_->header();
|
2021-07-28 10:44:11 -04:00
|
|
|
return nullptr;
|
2021-05-27 10:59:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-26 17:22:24 -04:00
|
|
|
/**
|
|
|
|
* @brief Pdu::data
|
|
|
|
* @return
|
|
|
|
*/
|
2021-07-26 21:15:20 -04:00
|
|
|
pdu_data * Pdu::data()
|
|
|
|
{
|
2021-05-27 10:59:22 -04:00
|
|
|
if (flags_.hasData)
|
|
|
|
return data_;
|
2021-07-28 10:44:11 -04:00
|
|
|
if (inherit_)
|
2021-05-27 10:59:22 -04:00
|
|
|
return inherit_->data();
|
2021-07-28 10:44:11 -04:00
|
|
|
return nullptr;
|
2021-05-27 10:59:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-26 17:22:24 -04:00
|
|
|
/**
|
2021-07-29 19:26:49 -04:00
|
|
|
* @brief Pdu::streamSize
|
|
|
|
* @return
|
2021-07-29 23:40:46 -04:00
|
|
|
*
|
|
|
|
* The maximum buffer size that could be required to buffer this PDU
|
2021-07-29 19:26:49 -04:00
|
|
|
*/
|
2021-07-29 23:40:46 -04:00
|
|
|
size_t Pdu::streamSize() const
|
2021-07-29 19:26:49 -04:00
|
|
|
{
|
2021-07-29 23:40:46 -04:00
|
|
|
size_t s = 2; // flength is at least 2 octets
|
|
|
|
|
|
|
|
// plus the length of the vector
|
|
|
|
if (flags_.hasVector) {
|
|
|
|
if (inherit_) {
|
|
|
|
if (vector_ != inherit_->vector())
|
|
|
|
s += vector_size_;
|
|
|
|
} else {
|
|
|
|
s += vector_size_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// plus the length of the header
|
|
|
|
if (header_) {
|
|
|
|
if (inherit_) {
|
|
|
|
if (header_ != inherit_->header())
|
|
|
|
s += header_->streamSize();
|
|
|
|
} else {
|
|
|
|
s += header_->streamSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// plus the lenth of the data
|
|
|
|
if (data_) {
|
|
|
|
if (inherit_) {
|
|
|
|
if (data_ != inherit_->data())
|
|
|
|
s += data_->streamSize();
|
|
|
|
} else {
|
|
|
|
s += data_->streamSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// and another bit if the length needs it
|
|
|
|
if (s > 0x0fff)
|
|
|
|
s++;
|
|
|
|
return s;
|
2021-07-29 19:26:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Pdu::iStream
|
2021-07-26 17:22:24 -04:00
|
|
|
* @param stream
|
|
|
|
*/
|
2021-07-29 19:26:49 -04:00
|
|
|
void Pdu::iStream(Stream stream)
|
2021-07-26 21:15:20 -04:00
|
|
|
{
|
2021-07-29 19:26:49 -04:00
|
|
|
// get the flags
|
2021-07-29 23:40:46 -04:00
|
|
|
flags_.set(stream->peek());
|
2021-07-29 19:26:49 -04:00
|
|
|
|
|
|
|
// get the length
|
2021-07-29 23:40:46 -04:00
|
|
|
size_t length;
|
|
|
|
length = stream->readType<uint16_t>() & 0x0fff; // high 4 bits are flags
|
2021-07-29 19:26:49 -04:00
|
|
|
if (flags_.hasLength)
|
2021-07-29 23:40:46 -04:00
|
|
|
length = (length << 8 ) | stream->readType<uint8_t>();
|
2021-07-29 19:26:49 -04:00
|
|
|
|
|
|
|
// get the vector
|
|
|
|
if (flags_.hasVector)
|
|
|
|
switch (vector_size_) {
|
|
|
|
case 1:
|
|
|
|
vector_ = stream->readType<uint8_t>();
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
vector_ = stream->readType<uint16_t>();
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
vector_ = stream->readType<uint32_t>();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
stream->setstate(std::ios_base::failbit);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// length includes the flags, length, and vector.
|
|
|
|
// the remainder of the length of header and data
|
2021-08-02 10:10:55 -04:00
|
|
|
uint hd_length = length - (flags_.hasLength ? 3 : 2) - vector_size_;
|
2021-07-29 19:26:49 -04:00
|
|
|
|
|
|
|
// abort if the remaining PDU length isn't available
|
|
|
|
if (!stream->good() || stream->available() < hd_length) {
|
|
|
|
stream->setstate(std::ios_base::failbit);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a stream buffer for the header and data
|
|
|
|
stream_ = Stream(new pdu_stream(stream->data(), hd_length));
|
|
|
|
if (stream_->available() != hd_length) {
|
|
|
|
stream->setstate(std::ios_base::failbit);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fast-forward the input stream
|
2021-08-02 10:10:55 -04:00
|
|
|
for (uint i = 0; i < hd_length; i++)
|
2021-07-29 19:26:49 -04:00
|
|
|
stream->get();
|
|
|
|
if (!stream->available())
|
|
|
|
stream->setstate(std::ios_base::eofbit);
|
2021-05-27 10:59:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-26 17:22:24 -04:00
|
|
|
/**
|
2021-07-29 19:26:49 -04:00
|
|
|
* @brief Pdu::oStream
|
|
|
|
* @param stream
|
2021-07-26 17:22:24 -04:00
|
|
|
*/
|
2021-07-29 19:26:49 -04:00
|
|
|
void Pdu::oStream(Stream stream) const
|
2021-07-26 21:15:20 -04:00
|
|
|
{
|
2021-07-29 23:40:46 -04:00
|
|
|
pdu_flags flags = flags_;
|
|
|
|
size_t length = streamSize();
|
|
|
|
|
|
|
|
// check if length flag need to be set
|
|
|
|
if (length > 0x0fff)
|
|
|
|
flags.hasLength = true;
|
|
|
|
|
|
|
|
// see if we can inherit members
|
|
|
|
if (inherit_) {
|
|
|
|
if (flags.hasVector)
|
|
|
|
if (vector_ == inherit_->vector())
|
|
|
|
flags.hasVector = false;
|
|
|
|
if (flags.hasHeader)
|
|
|
|
if (header_ == inherit_->header())
|
|
|
|
flags.hasHeader = false;
|
|
|
|
if (flags.hasData)
|
|
|
|
if (data_ == inherit_->data())
|
|
|
|
flags.hasData = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// inject flags onto the high 4 bits of the length
|
|
|
|
auto flength = reinterpret_cast<uint8_t*>(length);
|
|
|
|
if (flags.hasLength)
|
|
|
|
flength[2] = (flength[2] & 0x0f) | (uint8_t)flags;
|
|
|
|
else
|
|
|
|
flength[1] = (flength[1] & 0x0f) | (uint8_t)flags;
|
|
|
|
|
|
|
|
// write the flength to stream
|
|
|
|
stream->write(flength, (flags.hasLength ? 3 : 2));
|
|
|
|
|
|
|
|
// write the vector to stream
|
|
|
|
if (flags.hasVector)
|
|
|
|
switch (vector_size_) {
|
|
|
|
case 1:
|
|
|
|
stream->writeType<uint8_t>(vector_);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
stream->writeType<uint16_t>(vector_);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
stream->writeType<uint32_t>(vector_);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
stream->setstate(std::ios_base::failbit);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// write the header to stream
|
|
|
|
if (flags.hasHeader)
|
|
|
|
header_->oStream(stream);
|
|
|
|
|
|
|
|
// write the data to steam
|
|
|
|
if (flags.hasData)
|
|
|
|
data_->oStream(stream);
|
2021-05-27 10:59:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-26 17:22:24 -04:00
|
|
|
/**
|
2021-07-29 23:40:46 -04:00
|
|
|
* @brief pdu_flags::set
|
2021-07-26 17:22:24 -04:00
|
|
|
* @param val
|
|
|
|
*/
|
2021-07-29 23:40:46 -04:00
|
|
|
void pdu_flags::set(const uint8_t val)
|
2021-07-26 21:15:20 -04:00
|
|
|
{
|
2021-05-27 10:59:22 -04:00
|
|
|
hasLength = (val >> 7) & 0b1;
|
|
|
|
hasVector = (val >> 6) & 0b1;
|
|
|
|
hasHeader = (val >> 5) & 0b1;
|
|
|
|
hasData = (val >> 4) & 0b1;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // PDU
|
|
|
|
} // ACN
|