OpenLCP/acn/pdu.h
2021-07-29 19:27:13 -04:00

172 lines
4.6 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 {
/**
* @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;
bool hasVector : 1;
bool hasHeader : 1;
bool hasData : 1;
pdu_flags() {};
pdu_flags(uint8_t);
};
/**
* @brief The pdu_base_member struct
*/
struct pdu_base_member
{
virtual ~pdu_base_member() {};
virtual size_t streamSize() = 0;
virtual void iStream(Stream) = 0;
virtual void oStream(Stream) const = 0;
};
/**
* @brief The pdu_header struct
*/
struct pdu_header : public pdu_base_member {};
/**
* @brief The pdu_data struct
*/
struct pdu_data : public pdu_base_member {};
/**
* @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 pdu_base_member {
public:
Pdu(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
std::shared_ptr<Pdu> parent() {return parent_;}
Stream stream() {return stream_;}
virtual size_t streamSize() override;
virtual void iStream(Stream) override;
virtual void oStream(Stream) const override;
// setters
void setParent (std::shared_ptr<Pdu> pdu) {parent_ = pdu;}
void setInherit(std::shared_ptr<Pdu> pdu) {inherit_ = pdu;}
protected:
pdu_flags flags_;
uint32_t length_;
uint32_t vector_;
size_t vector_size_;
std::shared_ptr<Pdu> parent_ = nullptr;
std::shared_ptr<Pdu> inherit_ = nullptr;
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 <typename T>
using Handler = std::function<void(std::shared_ptr<T>)>;
/**
* @brief PDU::pdu_data subclass that encapsulates other PDU.
* @tparam T PDU decendant subclass
*/
template <typename T>
struct Block : public pdu_data
{
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>>);
size_t streamSize() {
size_t s = 0;
for (auto &child : *pdu)
s += child->streamSize();
return s;
}
void iStream(Stream s) { //!< you should call readBlock() directly.
readBlock(s, nullptr);
};
void oStream(Stream s) const {
for ( const auto & child : *pdu )
child->oStream(s);
};
void readBlock(Stream s, std::shared_ptr<PDU::Pdu> parent = nullptr) {
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 (parent) // set parent
p->setParent(parent);
if (!pdu->empty()) // set inheritee
p->setInherit(pdu->back());
pdu->push_back(p); // add to block
}
}
};
} // PDU
} // ACN