/* 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 namespace ACN { namespace PDU { /** * @brief Pdu::Pdu * @param stream * @param vector_size */ Pdu::Pdu(size_t vector_size) : vector_size_(vector_size) { } /** * @brief Pdu::~Pdu */ Pdu::~Pdu() { if (header_) delete header_; if (data_) delete data_; } /** * @brief Pdu::vector * @return */ const uint32_t Pdu::vector() { if (flags_.hasVector) return vector_; if (inherit_) return inherit_->vector(); return 0; } /** * @brief Pdu::header * @return */ pdu_header * Pdu::header() { if (flags_.hasHeader) return header_; if (inherit_) return inherit_->header(); return nullptr; } /** * @brief Pdu::data * @return */ pdu_data * Pdu::data() { if (flags_.hasData) return data_; if (inherit_) return inherit_->data(); return nullptr; } /** * @brief Pdu::streamSize * @return */ size_t Pdu::streamSize() { return 1 /// TODO: real size of vector + flags + length + header_->streamSize() + data_->streamSize(); } /** * @brief Pdu::iStream * @param stream */ void Pdu::iStream(Stream stream) { // get the flags flags_ = pdu_flags(stream->peek()); // get the length length_ = stream->readType() & 0x0fff; // high 4 bits are flags if (flags_.hasLength) length_ = (length_ << 8 ) | stream->readType(); // get the vector if (flags_.hasVector) switch (vector_size_) { case 1: vector_ = stream->readType(); break; case 2: vector_ = stream->readType(); break; case 4: vector_ = stream->readType(); 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 int hd_length = length_ - (flags_.hasLength ? 3 : 2) - vector_size_; // 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 for (int i = 0; i < hd_length; i++) stream->get(); if (!stream->available()) stream->setstate(std::ios_base::eofbit); } /** * @brief Pdu::oStream * @param stream */ void Pdu::oStream(Stream stream) const { /// TODO: write to stream } /** * @brief pdu_flags::pdu_flags * @param val */ pdu_flags::pdu_flags(uint8_t val) { hasLength = (val >> 7) & 0b1; hasVector = (val >> 6) & 0b1; hasHeader = (val >> 5) & 0b1; hasData = (val >> 4) & 0b1; }; } // PDU } // ACN