221 lines
6.0 KiB
C++
221 lines
6.0 KiB
C++
/*
|
|
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 <functional>
|
|
#include <memory>
|
|
#include <vector>
|
|
#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 {};
|
|
// C++20
|
|
//template <class T>
|
|
//concept header = std::is_base_of<pdu_header, T>::value;
|
|
|
|
/**
|
|
* @brief The pdu_data struct
|
|
*/
|
|
struct pdu_data : public pdu_stream_object {};
|
|
// C++20
|
|
//template <class T>
|
|
//concept data = std::is_base_of<pdu_data, T>::value;
|
|
|
|
// C++20
|
|
//template <class T>
|
|
//concept pdu = std::is_base_of<Pdu, T>::value;
|
|
|
|
/**
|
|
* @brief PDU::pdu_data subclass that encapsulates other PDU.
|
|
* @tparam T PDU decendant subclass
|
|
*/
|
|
//template<pdu T> // C++20
|
|
template<class T>
|
|
struct Block
|
|
: public pdu_data
|
|
{
|
|
Block() {
|
|
static_assert(std::is_base_of<Pdu, T>::value,
|
|
"type parameter of ACN::PDU::Block must derive from ACN::PDU::Pdu");
|
|
}
|
|
std::shared_ptr<std::vector<std::shared_ptr<T>>> pdu
|
|
= std::shared_ptr<std::vector<std::shared_ptr<T>>>
|
|
(new std::vector<std::shared_ptr<T>>);
|
|
void setParent(std::shared_ptr<Pdu> 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<T> 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<Pdu>
|
|
, 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<Pdu> 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> pdu) {parent_ = pdu;}
|
|
void setInherit(std::shared_ptr<Pdu> pdu) {inherit_ = pdu;}
|
|
|
|
// protocol payloads
|
|
//template<header T> // C++20
|
|
template<class T>
|
|
void createHeader()
|
|
{
|
|
if (flags_.hasHeader)
|
|
{
|
|
header_ = new T();
|
|
if (stream_ && stream_->good())
|
|
header_->iStream(stream_);
|
|
}
|
|
}
|
|
|
|
//template<data T> // C++20
|
|
template<class T>
|
|
void createData()
|
|
{
|
|
if (flags_.hasData)
|
|
{
|
|
data_ = new T();
|
|
if (stream_ && stream_->good())
|
|
data_->iStream(stream_);
|
|
}
|
|
}
|
|
//template<pdu T> // C++20
|
|
template<class T>
|
|
void createDataBlock() {
|
|
auto block = new PDU::Block<T>();
|
|
if (stream_)
|
|
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<Pdu> parent_;
|
|
std::shared_ptr<Pdu> 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<pdu T> // C++20
|
|
template <class T>
|
|
using Handler = std::function<void(std::shared_ptr<T>)>;
|
|
|
|
|
|
} // PDU
|
|
} // ACN
|