178 lines
4.4 KiB
C++
178 lines
4.4 KiB
C++
|
/*
|
||
|
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 {
|
||
|
|
||
|
Pdu::Pdu(Stream stream, size_t vector_size)
|
||
|
: flags_(stream->peek())
|
||
|
{
|
||
|
header_ = 0;
|
||
|
data_ = 0;
|
||
|
parent_ = 0;
|
||
|
inherit_ = 0; // pointer to previous PDU in block
|
||
|
|
||
|
// read length and vector off of the stream
|
||
|
// abort if the remaining PDU length isn't available
|
||
|
if (stream->available() < (flags_.hasLength ? 3 : 2)) {
|
||
|
stream->setstate(stream->rdstate() | std::ios_base::failbit);
|
||
|
return;
|
||
|
}
|
||
|
readLength(stream);
|
||
|
if (!length_ || length_ > std::pow(2, (8 * (flags_.hasLength ? 3 : 2)) - 4)) {
|
||
|
stream->setstate(stream->rdstate() | std::ios_base::failbit);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// length includes the flags, length, and vector
|
||
|
// calculate the remaining length of the PDU
|
||
|
int len = length_ - (flags_.hasLength ? 3 : 2);
|
||
|
|
||
|
// abort if the remaining PDU length isn't available
|
||
|
if (!stream->good() || stream->available() < len) {
|
||
|
stream->setstate(stream->rdstate() | std::ios_base::failbit);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// create a stream buffer for the header and data
|
||
|
buffer_ = Stream(new pdu_stream(stream->data(), len));
|
||
|
if (buffer_->available() != len) {
|
||
|
stream->setstate(stream->rdstate() | std::ios_base::failbit);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// fast-forward the input stream
|
||
|
for (int i = 0; i < len; i++)
|
||
|
stream->get();
|
||
|
if (!stream->available())
|
||
|
stream->setstate(stream->rdstate() | std::ios_base::eofbit);
|
||
|
|
||
|
if (flags_.hasVector)
|
||
|
readVector(vector_size);
|
||
|
}
|
||
|
|
||
|
|
||
|
Pdu::~Pdu() {
|
||
|
if (header_) delete header_;
|
||
|
if (data_) delete data_;
|
||
|
}
|
||
|
|
||
|
|
||
|
const uint32_t Pdu::vector() {
|
||
|
if (flags_.hasVector)
|
||
|
return vector_;
|
||
|
if (inherit_ != nullptr)
|
||
|
return inherit_->vector();
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
|
||
|
pdu_header * Pdu::header() {
|
||
|
if (flags_.hasHeader)
|
||
|
return header_;
|
||
|
if (inherit_ != nullptr)
|
||
|
return inherit_->header();
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
|
||
|
pdu_data * Pdu::data() {
|
||
|
if (flags_.hasData)
|
||
|
return data_;
|
||
|
if (inherit_ != nullptr)
|
||
|
return inherit_->data();
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
|
||
|
void Pdu::readLength(Stream stream) {
|
||
|
length_ = stream->read16() & 0x0fff; // high 4 bytes are flags
|
||
|
if (flags_.hasLength)
|
||
|
length_ = (length_ << 8 ) | stream->read8();
|
||
|
}
|
||
|
|
||
|
|
||
|
void Pdu::readVector(uint8_t vector_size) {
|
||
|
vector_ = 0;
|
||
|
for (int o = vector_size - 1; o >= 0; o--)
|
||
|
vector_ |= (buffer_->read8() << (8 * o));
|
||
|
}
|
||
|
|
||
|
|
||
|
pdu_flags::pdu_flags(uint8_t val) {
|
||
|
hasLength = (val >> 7) & 0b1;
|
||
|
hasVector = (val >> 6) & 0b1;
|
||
|
hasHeader = (val >> 5) & 0b1;
|
||
|
hasData = (val >> 4) & 0b1;
|
||
|
};
|
||
|
|
||
|
|
||
|
uint8_t pdu_stream::read8() {
|
||
|
uint8_t ret = 0;
|
||
|
if (available() < (int)sizeof(ret)) {
|
||
|
setstate(rdstate() | std::ios_base::failbit);
|
||
|
return 0;
|
||
|
}
|
||
|
ret = get();
|
||
|
if (!available())
|
||
|
setstate(rdstate() | std::ios_base::eofbit);
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
|
||
|
uint16_t pdu_stream::read16() {
|
||
|
uint16_t ret = 0;
|
||
|
if (available() < (int)sizeof(ret)) {
|
||
|
setstate(rdstate() | std::ios_base::failbit);
|
||
|
return 0;
|
||
|
}
|
||
|
ret |= get() << 8;
|
||
|
ret |= get();
|
||
|
if (!available())
|
||
|
setstate(rdstate() | std::ios_base::eofbit);
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
|
||
|
uint32_t pdu_stream::read32() {
|
||
|
uint32_t ret = 0;
|
||
|
if (available() < (int)sizeof(ret)) {
|
||
|
setstate(rdstate() | std::ios_base::failbit);
|
||
|
return 0;
|
||
|
}
|
||
|
ret |= get() << 24;
|
||
|
ret |= get() << 16;
|
||
|
ret |= get() << 6;
|
||
|
ret |= get();
|
||
|
if (!available())
|
||
|
setstate(rdstate() | std::ios_base::eofbit);
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
} // PDU
|
||
|
} // ACN
|