2021-08-19 12:55:25 -04:00
|
|
|
/*
|
|
|
|
otp/pdu.h
|
|
|
|
|
|
|
|
Copyright (c) 2021 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
|
|
|
|
|
2022-11-30 09:52:25 -05:00
|
|
|
#include "pdu-stream.h"
|
2021-08-19 12:55:25 -04:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace OTP {
|
|
|
|
namespace PDU {
|
|
|
|
|
|
|
|
using ACN::PDU::Stream;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The pdu_data struct
|
|
|
|
*/
|
2023-04-02 13:24:55 -04:00
|
|
|
struct pdu_data : streamable {};
|
2021-08-19 12:55:25 -04:00
|
|
|
|
|
|
|
|
|
|
|
class pdu;
|
|
|
|
using Pdu = std::shared_ptr<pdu>;
|
|
|
|
|
|
|
|
|
2021-08-25 17:20:33 -04:00
|
|
|
/**
|
|
|
|
* @brief The PDU::Block struct
|
|
|
|
*/
|
2021-08-19 12:55:25 -04:00
|
|
|
template<class T>
|
|
|
|
struct Block
|
|
|
|
: public pdu_data
|
|
|
|
{
|
|
|
|
Block() {
|
|
|
|
static_assert(std::is_base_of<OTP::PDU::pdu, T>::value,
|
|
|
|
"type parameter of ACN::PDU::Block must derive from OTP::PDU::Pdu");
|
|
|
|
}
|
2021-08-25 17:20:33 -04:00
|
|
|
/**
|
|
|
|
* @brief Messages contained in the block
|
|
|
|
*/
|
2021-08-19 12:55:25 -04:00
|
|
|
std::shared_ptr<std::vector<std::shared_ptr<T>>> members
|
2021-09-02 13:22:50 -04:00
|
|
|
= std::make_shared<std::vector<std::shared_ptr<T>>>();
|
2021-08-25 17:20:33 -04:00
|
|
|
/**
|
|
|
|
* @brief setParent
|
|
|
|
* @param parent
|
|
|
|
*/
|
2021-08-19 12:55:25 -04:00
|
|
|
void setParent(Pdu parent) {
|
|
|
|
for (auto &p : *members)
|
|
|
|
p->parent = parent;
|
|
|
|
}
|
2021-08-25 17:20:33 -04:00
|
|
|
/**
|
|
|
|
* @brief streamSize
|
|
|
|
* @return
|
|
|
|
*/
|
2021-08-19 12:55:25 -04:00
|
|
|
size_t streamSize() const override {
|
|
|
|
size_t s = 0;
|
|
|
|
for (auto &p : *members)
|
|
|
|
s += p->streamSize();
|
|
|
|
return s;
|
|
|
|
}
|
2021-08-25 17:20:33 -04:00
|
|
|
/**
|
|
|
|
* @brief iStream
|
|
|
|
* @param s
|
|
|
|
*/
|
2021-08-19 12:55:25 -04:00
|
|
|
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;
|
|
|
|
members->push_back(p); // add to block
|
|
|
|
}
|
|
|
|
}
|
2021-08-25 17:20:33 -04:00
|
|
|
/**
|
|
|
|
* @brief oStream
|
|
|
|
* @param s
|
|
|
|
*/
|
2021-08-19 12:55:25 -04:00
|
|
|
void oStream(Stream s) const override {
|
|
|
|
for ( const auto& p : *members )
|
|
|
|
p->oStream(s);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The OTP::PDU::Pdu class
|
|
|
|
*/
|
|
|
|
class pdu
|
|
|
|
: public std::enable_shared_from_this<pdu>
|
|
|
|
, public pdu_data
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
pdu();
|
|
|
|
~pdu();
|
|
|
|
|
2021-08-25 17:20:33 -04:00
|
|
|
pdu_data* data = nullptr; //!< data Message
|
|
|
|
uint16_t vector = 0; //!< vector
|
|
|
|
Pdu parent; //!< parent Message
|
2021-08-19 12:55:25 -04:00
|
|
|
|
2021-08-25 17:20:33 -04:00
|
|
|
Stream stream();
|
2021-08-19 12:55:25 -04:00
|
|
|
virtual size_t streamSize() const override;
|
|
|
|
virtual void iStream(Stream) override;
|
|
|
|
virtual void oStream(Stream) const override;
|
|
|
|
|
2021-08-25 17:20:33 -04:00
|
|
|
/**
|
|
|
|
* @brief createData
|
|
|
|
*/
|
2021-08-19 12:55:25 -04:00
|
|
|
template<class T>
|
|
|
|
void createData()
|
|
|
|
{
|
|
|
|
data = new T();
|
|
|
|
if (stream_ && stream_->good())
|
|
|
|
data->iStream(stream_);
|
|
|
|
}
|
|
|
|
|
2021-08-19 16:27:20 -04:00
|
|
|
protected:
|
2021-08-25 17:20:33 -04:00
|
|
|
Stream stream_; //!< buffer
|
2021-08-19 12:55:25 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace PDU
|
|
|
|
} // namespace OTP
|