186 lines
4.9 KiB
C++
186 lines
4.9 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(uint8_t);
|
|
};
|
|
|
|
|
|
/**
|
|
* @brief The pdu_header struct
|
|
*/
|
|
struct pdu_header
|
|
{
|
|
virtual ~pdu_header() {}
|
|
friend PDU::Stream& operator>> (PDU::Stream &stream, pdu_header &h)
|
|
{ h.iStream(stream); return stream; }
|
|
friend PDU::Stream& operator<< (PDU::Stream &stream, const pdu_header &h)
|
|
{ h.oStream(stream); return stream; }
|
|
virtual size_t streamSize() = 0;
|
|
protected:
|
|
virtual void iStream(Stream) = 0;
|
|
virtual void oStream(Stream) const = 0;
|
|
};
|
|
|
|
|
|
/**
|
|
* @brief The pdu_data struct
|
|
*/
|
|
struct pdu_data
|
|
{
|
|
virtual ~pdu_data() {}
|
|
friend PDU::Stream& operator>> (PDU::Stream &stream, pdu_data &d)
|
|
{ d.iStream(stream); return stream; }
|
|
friend PDU::Stream& operator<< (PDU::Stream &stream, const pdu_data &d)
|
|
{ d.oStream(stream); return stream; }
|
|
virtual size_t streamSize() = 0;
|
|
protected:
|
|
virtual void iStream(Stream) = 0;
|
|
virtual void oStream(Stream) const = 0;
|
|
};
|
|
|
|
|
|
/**
|
|
* @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(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
|
|
std::shared_ptr<Pdu> parent() {return parent_;}
|
|
Stream stream() {return stream_;}
|
|
|
|
// 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_;
|
|
std::shared_ptr<Pdu> parent_;
|
|
std::shared_ptr<Pdu> 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);
|
|
};
|
|
|
|
|
|
/**
|
|
* @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;
|
|
size_t streamSize() { return 0; }
|
|
protected:
|
|
void iStream(Stream s) {};
|
|
void oStream(Stream s) const {};
|
|
};
|
|
|
|
|
|
/**
|
|
* @brief PDU::readBlock
|
|
* reads a PDU::Block from a PDU::Stream
|
|
* @param std::shared_ptr<PDU::pdu_stream> The stream to read from.
|
|
* @return std::shared_ptr<std::vector<std::shared_ptr<T>>> A block of PDU
|
|
* @tparam T PDU decendant subclass
|
|
*/
|
|
template<typename T>
|
|
Block<T> readBlock(Stream buffer, std::shared_ptr<PDU::Pdu> parent = nullptr) {
|
|
auto block = Block<T>();
|
|
while(buffer->good()) {
|
|
std::shared_ptr<T> pdu(new T(buffer));
|
|
if (buffer->fail()) // stream failed during pdu constructor
|
|
break;
|
|
if (pdu->stream()->fail()) // pdu buffer errors
|
|
continue;
|
|
if (parent) // set parent
|
|
pdu->setParent(parent);
|
|
if (!block.pdu->empty()) // set inheritee
|
|
pdu->setInherit(block.pdu->back());
|
|
block.pdu->push_back(pdu); // add to block
|
|
}
|
|
return block;
|
|
}
|
|
|
|
|
|
|
|
} // PDU
|
|
} // ACN
|