/* pdu.h 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. */ #pragma once #include #include #include #include "pdu-stream.h" namespace ACN { namespace PDU { class Pdu; // forward declare /** * @brief 2.4.1. Flags * * Flags is a 4-bit field containing flags L, V, H and D which declare how * the PDU is packed. */ struct pdu_flags { bool hasLength : 1; //!< true if pdu length is > 0x0fff bool hasVector : 1; //!< false if Pdu inherits it's vector bool hasHeader : 1; //!< false if Pdu inherits it's header bool hasData : 1; //!< false if Pdu inherits it's data pdu_flags() { set(0); } void set(const uint8_t); operator uint8_t() const { uint8_t ret = 0; if (hasLength) ret |= 0b10000000; if (hasVector) ret |= 0b01000000; if (hasHeader) ret |= 0b00100000; if (hasData) ret |= 0b00010000; return ret; }; }; /** * @brief The pdu_header struct */ struct pdu_header : public pdu_stream_object {}; /** * @brief The pdu_data struct */ struct pdu_data : public pdu_stream_object {}; /** * @brief PDU::pdu_data subclass that encapsulates other PDU. * @tparam T PDU decendant subclass */ template struct Block : public pdu_data { std::shared_ptr>> pdu = std::shared_ptr>> (new std::vector>); void setParent(std::shared_ptr parent) { for (auto p : *pdu) p->setParent(parent); } size_t streamSize() const override { size_t s = 0; for (auto &child : *pdu) s += child->streamSize(); return s; } void iStream(Stream s) override { while(s->good()) { std::shared_ptr p(new T()); p->iStream(s); if (s->fail()) // stream failed during pdu read break; if (p->stream()->fail()) // pdu buffer failed continue; if (!pdu->empty()) // set inheritee p->setInherit(pdu->back()); pdu->push_back(p); // add to block } } void oStream(Stream s) const override { for ( const auto & child : *pdu ) child->oStream(s); }; }; /** * @brief The Pdu class * * All PDU share common structure of: * flags, length, vector, * and protocol specific header/data. * * Flag values indicate if lenght, vector, header or data * are present in the PDU, or if they should be inherited from the * preceding PDU. */ class Pdu : public std::enable_shared_from_this , public pdu_data { public: Pdu(size_t vector_size); ~Pdu(); // getters uint32_t vector(); // may inherit pdu_header * header(); // may inherit pdu_data * data(); // may inherit std::shared_ptr parent() {return parent_;} Stream stream() {return stream_;} virtual size_t streamSize() const override; virtual void iStream(Stream) override; virtual void oStream(Stream) const override; // setters void setVector (const uint32_t v) { vector_ = v; flags_.hasVector = true; } void setHeader (pdu_header * h) { header_ = h; flags_.hasHeader = true; } void setData (pdu_data * d) { data_ = d; flags_.hasData = true; } void setParent (std::shared_ptr pdu) {parent_ = pdu;} void setInherit(std::shared_ptr pdu) {inherit_ = pdu;} // protocol payloads template void createHeader() { if (flags_.hasHeader) { header_ = new T(); if (stream_->good()) header_->iStream(stream_); } } template void createData() { if (flags_.hasData) { data_ = new T(); if (stream_->good()) data_->iStream(stream_); } } template void createDataBlock() { auto block = new PDU::Block(); block->iStream(stream_); block->setParent(shared_from_this()); data_ = block; } protected: pdu_flags flags_; uint32_t vector_ = 0; size_t vector_size_; std::shared_ptr parent_; std::shared_ptr inherit_; pdu_header * header_ = nullptr; pdu_data * data_ = nullptr; Stream stream_; }; /** * @brief Callback that understands how to proccess a PDU type. * @tparam T PDU decendant subclass */ template using Handler = std::function)>; } // PDU } // ACN