/* 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 #include namespace ACN { namespace PDU { using std::uint8_t; using std::uint16_t; using std::uint32_t; using std::vector; using std::shared_ptr; // 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; bool hasVector : 1; bool hasHeader : 1; bool hasData : 1; pdu_flags(uint8_t); }; // MAYBE: remove virtuals? // Arduino doen't enable RTTI for run-time polymorphism. struct pdu_header { virtual ~pdu_header() {} }; struct pdu_data { virtual ~pdu_data() {} }; /** Memory buffer of uint8_t data. */ class pdu_buffer : public std::basic_streambuf { public: pdu_buffer(uint8_t * p, size_t l) { setg(p, p, p + l); }; uint8_t * in_ptr() { return gptr(); }; }; /** Input/Output stream of nested PDU */ class pdu_stream : public std::basic_istream { public: pdu_stream(uint8_t * p, size_t l) : std::basic_istream(&_buffer) , _buffer(p, l) { rdbuf(&_buffer); } uint32_t available() { return _buffer.in_avail(); } uint8_t * data() { return _buffer.in_ptr(); }; template T readType() { if (available() < sizeof(T)) { setstate(std::ios_base::failbit); return 0; } T ret = 0; auto data = reinterpret_cast(&ret); for (int i = sizeof(T); --i >= 0; ) data[i] = get(); if (!available()) setstate(std::ios_base::eofbit); return ret; } private: pdu_buffer _buffer; }; using Stream = shared_ptr; /** Base class PDU 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: Pdu(Stream, size_t vector_size); ~Pdu(); // getters const pdu_flags flags() {return flags_;} const uint32_t length() {return length_;} const uint32_t vector(); // may inherit pdu_header * header(); // may inherit pdu_data * data(); // may inherit shared_ptr parent() {return parent_;} Stream stream() {return stream_;} // setters void setParent (shared_ptr pdu) {parent_ = pdu;} void setInherit(shared_ptr pdu) {inherit_ = pdu;} protected: pdu_flags flags_; uint32_t length_; uint32_t vector_; shared_ptr parent_; shared_ptr inherit_; Stream stream_; // private setters void setHeader (pdu_header * h) {header_ = h;} void setData (pdu_data * d) {data_ = d;} private: pdu_header * header_; pdu_data * data_; void readLength(Stream); void readVector(uint8_t); }; template using Handler = std::function)>; template using Block = std::shared_ptr>>; /** Template creator of a PDU Block. @param std::shared_ptr The stream to read from. @return std::shared_ptr>> A block of PDU */ template Block readBlock(Stream buffer, shared_ptr parent = 0) { auto block = Block(new vector>); while(buffer->good()) { shared_ptr pdu(new T(buffer)); if (buffer->fail()) // stream failed during pdu constructor break; if (pdu->stream()->fail()) // pdu buffer errors continue; if (parent != 0) // set parent pdu->setParent(parent); if (!block->empty()) // set inheritee pdu->setInherit(block->back()); block->push_back(pdu); // add to block } return block; } } // PDU } // ACN