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 <cstdint>
|
|
|
|
#include <functional>
|
2021-06-21 10:26:46 -04:00
|
|
|
#include <iostream>
|
2021-05-27 10:59:22 -04:00
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace ACN {
|
|
|
|
namespace PDU {
|
|
|
|
|
|
|
|
using std::uint8_t;
|
|
|
|
using std::uint16_t;
|
|
|
|
using std::uint32_t;
|
|
|
|
using std::vector;
|
|
|
|
using std::shared_ptr;
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
};
|
|
|
|
|
|
|
|
// MAYBE: remove virtuals?
|
|
|
|
// Arduino doen't enable RTTI for run-time polymorphism.
|
|
|
|
struct pdu_header { virtual ~pdu_header() {} };
|
|
|
|
struct pdu_data { virtual ~pdu_data() {} };
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-06-18 08:46:30 -04:00
|
|
|
Input/Output stream of nested PDU
|
2021-05-27 10:59:22 -04:00
|
|
|
*/
|
2021-06-21 10:26:46 -04:00
|
|
|
|
2021-05-27 10:59:22 -04:00
|
|
|
class pdu_stream
|
2021-06-21 10:26:46 -04:00
|
|
|
: private std::basic_streambuf<uint8_t>
|
|
|
|
, public std::basic_iostream<uint8_t>
|
2021-05-27 10:59:22 -04:00
|
|
|
{
|
|
|
|
public:
|
2021-06-21 10:26:46 -04:00
|
|
|
pdu_stream(uint8_t * p, std::streamsize l)
|
|
|
|
: std::basic_streambuf<uint8_t>()
|
|
|
|
, std::basic_iostream<uint8_t>(this)
|
|
|
|
{
|
|
|
|
setg(p, p, p + l);
|
|
|
|
setp(p, p + l);
|
|
|
|
}
|
|
|
|
uint32_t available() { return in_avail(); }
|
|
|
|
uint8_t * data() { return gptr(); };
|
2021-07-26 13:33:14 -04:00
|
|
|
pdu_stream& operator>> (uint8_t& val) {
|
|
|
|
val = readType<uint8_t>();
|
|
|
|
return *this;
|
|
|
|
};
|
|
|
|
pdu_stream& operator>> (uint16_t& val) {
|
|
|
|
val = readType<uint16_t>();
|
|
|
|
return *this;
|
|
|
|
};
|
|
|
|
pdu_stream& operator>> (uint32_t& val) {
|
|
|
|
val = readType<uint32_t>();
|
|
|
|
return *this;
|
|
|
|
};
|
2021-06-18 08:46:30 -04:00
|
|
|
template<typename T> T readType() {
|
2021-06-21 10:26:46 -04:00
|
|
|
if (in_avail() < sizeof(T)) {
|
2021-06-19 10:18:09 -04:00
|
|
|
setstate(std::ios_base::failbit);
|
2021-06-18 08:46:30 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
T ret = 0;
|
|
|
|
auto data = reinterpret_cast<uint8_t*>(&ret);
|
|
|
|
for (int i = sizeof(T); --i >= 0; )
|
|
|
|
data[i] = get();
|
2021-06-21 10:26:46 -04:00
|
|
|
if (!in_avail())
|
2021-06-19 10:18:09 -04:00
|
|
|
setstate(std::ios_base::eofbit);
|
2021-06-18 08:46:30 -04:00
|
|
|
return ret;
|
|
|
|
}
|
2021-05-27 10:59:22 -04:00
|
|
|
};
|
|
|
|
using Stream = shared_ptr<pdu_stream>;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Base class PDU
|
|
|
|
|
|
|
|
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
|
|
|
|
shared_ptr<Pdu> parent() {return parent_;}
|
2021-06-18 08:46:30 -04:00
|
|
|
Stream stream() {return stream_;}
|
2021-05-27 10:59:22 -04:00
|
|
|
|
|
|
|
// setters
|
|
|
|
void setParent (shared_ptr<Pdu> pdu) {parent_ = pdu;}
|
|
|
|
void setInherit(shared_ptr<Pdu> pdu) {inherit_ = pdu;}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
pdu_flags flags_;
|
|
|
|
uint32_t length_;
|
|
|
|
uint32_t vector_;
|
|
|
|
shared_ptr<Pdu> parent_;
|
|
|
|
shared_ptr<Pdu> inherit_;
|
2021-06-18 08:46:30 -04:00
|
|
|
Stream stream_;
|
2021-05-27 10:59:22 -04:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
using Handler = std::function<void(std::shared_ptr<T>)>;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
using Block = std::shared_ptr<std::vector<std::shared_ptr<T>>>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Template creator of a PDU Block.
|
|
|
|
|
|
|
|
@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
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
Block<T> readBlock(Stream buffer, shared_ptr<PDU::Pdu> parent = 0) {
|
|
|
|
auto block = Block<T>(new vector<shared_ptr<T>>);
|
|
|
|
while(buffer->good()) {
|
|
|
|
shared_ptr<T> pdu(new T(buffer));
|
|
|
|
if (buffer->fail()) // stream failed during pdu constructor
|
|
|
|
break;
|
2021-06-18 08:46:30 -04:00
|
|
|
if (pdu->stream()->fail()) // pdu buffer errors
|
2021-05-27 10:59:22 -04:00
|
|
|
continue;
|
|
|
|
if (parent != 0) // set parent
|
|
|
|
pdu->setParent(parent);
|
|
|
|
if (!block->empty()) // set inheritee
|
|
|
|
pdu->setInherit(block->back());
|
|
|
|
block->push_back(pdu); // add to block
|
|
|
|
}
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // PDU
|
|
|
|
} // ACN
|