From d77cf14daeff9ad15418655a5c87aeca0415e80d Mon Sep 17 00:00:00 2001 From: Kevin Matz Date: Thu, 19 Aug 2021 16:27:20 -0400 Subject: [PATCH] OTP Layer PDU i/o --- CMakeLists.txt | 2 + otp/base.cpp | 179 +++++++++++++++++++++++++++++++++++++++++++++++++ otp/base.h | 86 ++++++++++++++++++++++++ otp/pdu.h | 4 +- 4 files changed, 269 insertions(+), 2 deletions(-) create mode 100644 otp/base.cpp create mode 100644 otp/base.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 9aa547d..2cf38d3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,6 +57,8 @@ set(SOURCE_FILES dmx/personality.cpp dmx/universe.cpp dmx/universe.h + otp/base.h + otp/base.cpp otp/otp.h otp/pdu.h otp/pdu.cpp diff --git a/otp/base.cpp b/otp/base.cpp new file mode 100644 index 0000000..98ccc35 --- /dev/null +++ b/otp/base.cpp @@ -0,0 +1,179 @@ +/* + otp/base.cpp + + 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. +*/ +#include "base.h" +#include "otp.h" +#include "transform.h" +#include "advertisement.h" + +namespace OTP::Layer { + +/** + * @brief base_data::streamSize + * @return + */ +size_t base_data::streamSize() const +{ + return 15 + + UUID_LENGTH + + 32 // name length + + payload->streamSize(); +} + + +/** + * @brief base_data::iStream + * @param stream + */ +void base_data::iStream(PDU::Stream stream) +{ + *stream >> footer_options; + *stream >> footer_length; + *stream >> cid; + *stream >> folio_number; + *stream >> page; + *stream >> last_page; + *stream >> options; + *stream >> reserved; + stream->readString(name, 32); +} + + +/** + * @brief base_data::oStream + * @param stream + */ +void base_data::oStream(PDU::Stream stream) const +{ + *stream << footer_options; + *stream << footer_length; + *stream << cid; + *stream << folio_number; + *stream << page; + *stream << last_page; + *stream << options; + *stream << reserved; + stream->writeString(name, 32); + payload->oStream(stream); +} + + +/** + * @brief base_footer::iStream + * @param stream + */ +void base_footer::iStream(PDU::Stream stream) +{ + if (stream->available() < length) + return stream->setstate(std::ios_base::failbit); + + for (uint8_t i = 0; i < length; i++) + data.push_back(stream->get()); +} + + +/** + * @brief base_footer::oStream + * @param stream + */ +void base_footer::oStream(PDU::Stream stream) const +{ + for (const auto &d : data) + *stream << d; +} + + +/** + * @brief pdu::streamSize + * @return + */ +size_t pdu::streamSize() const +{ + size_t ret = 0; + ret += sizeof(OTP_PACKET_IDENTIFIER); + ret += footer.streamSize(); + return ret; +} + + +/** + * @brief pdu::iStream + * @param stream + */ +void pdu::iStream(PDU::Stream stream) +{ + // check packet identifier + uint8_t ident[sizeof(OTP_PACKET_IDENTIFIER)]; + stream->read(ident, sizeof(ident)); + if (stream->gcount() != sizeof(ident)) + return stream->setstate(std::ios_base::failbit); + if (memcmp(OTP_PACKET_IDENTIFIER, ident, sizeof(ident)) != 0) + return stream->setstate(std::ios_base::failbit); + + OTP::PDU::pdu::iStream(stream); // vector and length + createData(); // data + + auto d = static_cast(data); + switch (vector) + { + case VECTOR_OTP_TRANSFORM_MESSAGE: + d->payload = std::shared_ptr(new Transform::pdu()); + d->payload->iStream(stream_); + break; + case VECTOR_OTP_ADVERTISEMENT_MESSAGE: + d->payload = std::shared_ptr(new Advertisement::pdu()); + d->payload->iStream(stream_); + break; + } + + footer.options = d->footer_options; + footer.length = d->footer_length; + footer.iStream(stream); // footer +} + + +/** + * @brief pdu::oStream + * @param stream + */ +void pdu::oStream(PDU::Stream stream) const +{ + // packet identifier + stream->write(OTP_PACKET_IDENTIFIER, sizeof (OTP_PACKET_IDENTIFIER)); + + // sync footer parameters + auto d = static_cast(data); + d->footer_options = footer.options; + d->footer_length = footer.streamSize(); + + // PDU body + OTP::PDU::pdu::oStream(stream); + + // footer + footer.oStream(stream); +} + + + +} // namespace OTP::Layer + diff --git a/otp/base.h b/otp/base.h new file mode 100644 index 0000000..cd683a3 --- /dev/null +++ b/otp/base.h @@ -0,0 +1,86 @@ +/* + otp/base.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 + +#include "pdu.h" + +namespace OTP::Layer { + +/** + * @brief The base_data struct + */ +struct base_data + : PDU::pdu_data +{ + uint8_t footer_options; + uint8_t footer_length; + UUID::uuid cid; + uint32_t folio_number; + uint16_t page; + uint16_t last_page; + uint8_t options; + uint32_t reserved; + std::string name; + PDU::Pdu payload; + + virtual size_t streamSize() const override; + virtual void iStream(PDU::Stream) override; + virtual void oStream(PDU::Stream) const override; +}; + + +/** + * @brief The base_footer struct + */ +struct base_footer + : PDU::pdu_stream_object +{ + uint8_t options; + uint8_t length; + std::vector data; + + virtual size_t streamSize() const override { return data.size(); } + virtual void iStream(PDU::Stream) override; + virtual void oStream(PDU::Stream) const override; +}; + + +/** + * @brief The pdu class + */ +class pdu + : public OTP::PDU::pdu +{ +public: + base_footer footer; + + virtual size_t streamSize() const override; + virtual void iStream(PDU::Stream) override; + virtual void oStream(PDU::Stream) const override; +}; + + + + +} // OTP::Layer diff --git a/otp/pdu.h b/otp/pdu.h index 8a42ce1..2d75bc7 100644 --- a/otp/pdu.h +++ b/otp/pdu.h @@ -36,7 +36,7 @@ using ACN::PDU::pdu_stream_object; /** * @brief The pdu_data struct */ -struct pdu_data : public pdu_stream_object {}; +struct pdu_data : pdu_stream_object {}; class pdu; @@ -118,7 +118,7 @@ public: data = block; } -private: +protected: Stream stream_; };