1
0
Fork 0

OTP Layer PDU i/o

This commit is contained in:
Kevin Matz 2021-08-19 16:27:20 -04:00
parent 6c12f7d8f0
commit d77cf14dae
4 changed files with 269 additions and 2 deletions

View File

@ -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

179
otp/base.cpp Normal file
View File

@ -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<base_data>(); // data
auto d = static_cast<base_data*>(data);
switch (vector)
{
case VECTOR_OTP_TRANSFORM_MESSAGE:
d->payload = std::shared_ptr<Transform::pdu>(new Transform::pdu());
d->payload->iStream(stream_);
break;
case VECTOR_OTP_ADVERTISEMENT_MESSAGE:
d->payload = std::shared_ptr<Advertisement::pdu>(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<base_data*>(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

86
otp/base.h Normal file
View File

@ -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<uint8_t> 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

View File

@ -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_;
};