/* 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" /** * @brief @cite ACN 1.2.8.1 Common Packet Format * * In developing SDT and DMP it was recognized that the messages in both protocols have many * similarities and that by using a common message format across all the ACN protocols, the code to * recognize and decode messages can be shared across different message types. This is why ACN * defines a common message format the Protocol Data Unit or PDU. */ namespace ACN::PDU { class Pdu; // forward declare /** * @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 Definitions: Message - A valid PDU. * @tparam T PDU derived class */ template using Message = std::shared_ptr; /** * @brief Callback that understands how to proccess a PDU type. * @tparam T PDU derived class */ template using Handler = std::function)>; /** * @brief PDU::pdu_data subclass that encapsulates other PDU. * @tparam T PDU decendant subclass */ template struct Block : public pdu_data { Block() { static_assert(std::is_base_of::value, "type parameter of ACN::PDU::Block must derive from ACN::PDU::Pdu"); } /** * @brief member Messages of this block */ std::shared_ptr>> pdu = std::make_shared>>(); /** * @brief setParent * @param parent */ void setParent(Message parent) { for (auto & p : *pdu) p->setParent(parent); } size_t streamSize() const override { size_t s = 0; for (const auto & child : *pdu) s += child->streamSize(); return s; } void iStream(Stream s) override { while(s->good()) { auto p = std::make_shared(); 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 @cite ACN 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 { union { uint8_t _raw = 0; struct { uint8_t lengthH : 4; //!< hightest 4 bytes of the PDU length, unused here bool hasData : 1; //!< false if Pdu inherits it's data bool hasHeader : 1; //!< false if Pdu inherits it's header bool hasVector : 1; //!< false if Pdu inherits it's vector bool hasLength : 1; //!< true if pdu length is > 0x0fff }; }; }; /** * @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); // getters uint32_t vector(); // may inherit std::shared_ptr header(); // may inherit std::shared_ptr data(); // may inherit Message parent(); Stream stream(); virtual size_t streamSize() const override; virtual void iStream(Stream) override; virtual void oStream(Stream) const override; // setters void setVector (const uint32_t); void setHeader (std::shared_ptr); void setData (std::shared_ptr); void setParent (Message pdu); void setInherit(Message pdu); // protocol payloads /** * @brief createHeader */ template void createHeader() { if (!flags_.hasHeader) return; // doesn't get a header segment, inherits if (header_) return; // already has a header segment header_ = std::make_shared(); if (stream_ && stream_->good()) header_->iStream(stream_); } /** * @brief createData */ template void createData() { if (!flags_.hasData) return; // doesn't get a data segment, inherits if (data_) return; // already has a data segment data_ = std::make_shared(); if (stream_ && stream_->good()) data_->iStream(stream_); } /** * @brief createDataBlock */ template void createDataBlock() { if (!data_) { // may already have a data segment createData>(); auto block = std::static_pointer_cast>(data_); block->setParent(shared_from_this()); } } protected: pdu_flags flags_; //!< flags for length, vector, header and data uint32_t vector_ = 0; //!< vector of this PDU size_t vector_size_; //!< width (numbe of octets) of the vector Message parent_; //!< parent PDU Message inherit_; //!< PDU from which to inherit Stream stream_; //!< buffer std::shared_ptr header_ = nullptr; //!< header segment std::shared_ptr data_ = nullptr; //!< data segment }; } // ACN::PDU