2021-05-27 10:59:22 -04:00
|
|
|
/*
|
|
|
|
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>
|
2021-07-26 21:15:20 -04:00
|
|
|
#include "pdu-stream.h"
|
2021-05-27 10:59:22 -04:00
|
|
|
|
|
|
|
namespace ACN {
|
|
|
|
namespace PDU {
|
|
|
|
|
2021-07-26 22:47:25 -04:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
{
|
2021-07-29 23:40:46 -04:00
|
|
|
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;
|
|
|
|
};
|
2021-05-27 10:59:22 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-07-29 19:26:49 -04:00
|
|
|
/**
|
|
|
|
* @brief The pdu_header struct
|
|
|
|
*/
|
2021-07-30 08:37:16 -04:00
|
|
|
struct pdu_header : public pdu_stream_object {};
|
2021-07-29 19:26:49 -04:00
|
|
|
|
|
|
|
|
2021-05-27 10:59:22 -04:00
|
|
|
/**
|
2021-07-26 22:47:25 -04:00
|
|
|
* @brief The pdu_data struct
|
|
|
|
*/
|
2021-07-30 08:37:16 -04:00
|
|
|
struct pdu_data : public pdu_stream_object {};
|
2021-05-27 10:59:22 -04:00
|
|
|
|
|
|
|
|
2021-07-26 22:47:25 -04:00
|
|
|
/**
|
|
|
|
* @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.
|
|
|
|
*/
|
2021-08-14 09:28:28 -04:00
|
|
|
class Pdu
|
|
|
|
: public std::enable_shared_from_this<Pdu>
|
|
|
|
, public pdu_data
|
|
|
|
{
|
2021-05-27 10:59:22 -04:00
|
|
|
public:
|
2021-07-29 19:26:49 -04:00
|
|
|
Pdu(size_t vector_size);
|
2021-05-27 10:59:22 -04:00
|
|
|
~Pdu();
|
|
|
|
|
|
|
|
// getters
|
2021-08-02 10:10:55 -04:00
|
|
|
uint32_t vector(); // may inherit
|
2021-05-27 10:59:22 -04:00
|
|
|
pdu_header * header(); // may inherit
|
|
|
|
pdu_data * data(); // may inherit
|
2021-07-26 22:47:25 -04:00
|
|
|
std::shared_ptr<Pdu> parent() {return parent_;}
|
2021-06-18 08:46:30 -04:00
|
|
|
Stream stream() {return stream_;}
|
2021-07-29 23:40:46 -04:00
|
|
|
virtual size_t streamSize() const override;
|
2021-07-29 19:26:49 -04:00
|
|
|
virtual void iStream(Stream) override;
|
|
|
|
virtual void oStream(Stream) const override;
|
2021-05-27 10:59:22 -04:00
|
|
|
|
|
|
|
// setters
|
2021-07-29 23:40:46 -04:00
|
|
|
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; }
|
2021-07-26 22:47:25 -04:00
|
|
|
void setParent (std::shared_ptr<Pdu> pdu) {parent_ = pdu;}
|
|
|
|
void setInherit(std::shared_ptr<Pdu> pdu) {inherit_ = pdu;}
|
2021-05-27 10:59:22 -04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
pdu_flags flags_;
|
2021-07-29 23:40:46 -04:00
|
|
|
uint32_t vector_ = 0;
|
2021-07-29 19:26:49 -04:00
|
|
|
size_t vector_size_;
|
2021-08-14 09:28:28 -04:00
|
|
|
std::shared_ptr<Pdu> parent_;
|
|
|
|
std::shared_ptr<Pdu> inherit_;
|
|
|
|
pdu_header * header_ = nullptr;
|
|
|
|
pdu_data * data_ = nullptr;
|
2021-06-18 08:46:30 -04:00
|
|
|
Stream stream_;
|
2021-05-27 10:59:22 -04:00
|
|
|
};
|
|
|
|
|
2021-07-26 22:47:25 -04:00
|
|
|
|
2021-07-29 11:20:03 -04:00
|
|
|
/**
|
|
|
|
* @brief Callback that understands how to proccess a PDU type.
|
|
|
|
* @tparam T PDU decendant subclass
|
|
|
|
*/
|
2021-05-27 10:59:22 -04:00
|
|
|
template <typename T>
|
|
|
|
using Handler = std::function<void(std::shared_ptr<T>)>;
|
|
|
|
|
2021-07-29 11:20:03 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief PDU::pdu_data subclass that encapsulates other PDU.
|
|
|
|
* @tparam T PDU decendant subclass
|
|
|
|
*/
|
2021-05-27 10:59:22 -04:00
|
|
|
template <typename T>
|
2021-08-14 09:28:28 -04:00
|
|
|
struct Block
|
|
|
|
: public pdu_data
|
2021-07-29 11:20:03 -04:00
|
|
|
{
|
2021-07-29 19:26:49 -04:00
|
|
|
std::shared_ptr<std::vector<std::shared_ptr<T>>> pdu
|
2021-08-14 09:28:28 -04:00
|
|
|
= 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);
|
|
|
|
}
|
2021-07-30 08:21:38 -04:00
|
|
|
size_t streamSize() const override {
|
2021-08-14 09:28:28 -04:00
|
|
|
size_t s = 0;
|
|
|
|
for (auto &child : *pdu)
|
|
|
|
s += child->streamSize();
|
|
|
|
return s;
|
2021-07-29 19:26:49 -04:00
|
|
|
}
|
2021-08-14 09:28:28 -04:00
|
|
|
void iStream(Stream s) override {
|
2021-07-29 19:26:49 -04:00
|
|
|
while(s->good()) {
|
2021-08-14 09:28:28 -04:00
|
|
|
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
|
|
|
|
}
|
2021-07-29 19:26:49 -04:00
|
|
|
}
|
2021-08-14 09:28:28 -04:00
|
|
|
void oStream(Stream s) const override {
|
|
|
|
for ( const auto & child : *pdu )
|
|
|
|
child->oStream(s);
|
|
|
|
};
|
2021-07-29 19:26:49 -04:00
|
|
|
};
|
2021-05-27 10:59:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
} // PDU
|
|
|
|
} // ACN
|