OpenLCP/protocol/rlp/component.cpp

242 lines
6.1 KiB
C++

/*
rlp/component.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.
*/
#include "component.h"
#include "sdt/sdt.h"
#include "tcp.h"
#include "udp.h"
namespace ACN::RLP {
/**
* @brief Component::Component
* @param cid
* @param fctn
* @param ipv4
* @param ipv6
*/
Component::Component(UUID::uuid cid, std::string fctn, bool ipv4, bool ipv6)
: ACN::Component(cid, fctn)
, enable_IPv4(ipv4)
, enable_IPv6(ipv6)
{
/// All RLP PDU generated by this component will referance the same header.
rlp_header_ = std::make_shared<rlp_header>();
rlp_header_->cid = cid;
};
/**
* @brief \cite epi17 EPI 17 payload receiver for UDP
* @param stream
*/
void Component::UdpPayloadReceiver(PDU::Stream stream)
{
/// Read the input stream as EPI 17 transported RLP
auto transport = RLP::UDP::transport();
*stream >> transport;
/// The RLP PDU Block will contain only suscessfully read PDU
for(auto const &root : *transport.root.pdu)
RlpReceiver(root);
}
/**
* @brief \cite epi17 EPI 17 payload sender over UDP
* @param vector
* @param data
* @param ip
*/
void Component::rlpSendUdp (const uint32_t vector,
const PDU::Message<PDU::pdu_data> data,
const SDT::UDP::ipAddress& ip)
{
switch (ip.type) {
case ACN::SDT::SDT_ADDR_IPV4:
if (!enable_IPv4)
return;
break;
case ACN::SDT::SDT_ADDR_IPV6:
if (!enable_IPv6)
return;
break;
default:
return;
}
/// Constructs an RLP PDU.
auto rlp = std::make_shared<RLP::Pdu>();
rlp->setVector(vector);
rlp->setHeader(rlp_header_);
rlp->setData(data);
/// Adds the PDU to the root block of the EPI 17 tranport format.
UDP::transport transport(true);
transport.root.pdu->push_back(rlp);
/// Fills an output stream with the transported PDU
size_t length = transport.streamSize();
uint8_t buffer[length];
PDU::Stream stream(new PDU::pdu_stream(buffer, length));
*stream << transport;
/// Platform devices shall impliment the sending on the network.
sendUDP(stream, ip);
}
/**
* @brief \cite epi33 EPI 33 payload receiver for TCP
* @param stream
*/
void Component::TcpPacketReceiver(PDU::Stream stream)
{
/// Read input stream as EPI 33 transported RLP
auto transport = RLP::TCP::transport();
*stream >> transport;
/// The RLP PDU Block will only contain suscessfully read PDU
for(auto const &root : *transport.root.pdu)
RlpReceiver(root);
}
/**
* @brief \cite epi33 EPI 33 payload sender over TCP
* @param vector
* @param data
* @param ip
*/
void Component::rlpSendTcp (const uint32_t vector,
const PDU::Message<PDU::pdu_data> data,
const SDT::UDP::ipAddress& ip)
{
switch (ip.type) {
case ACN::SDT::SDT_ADDR_IPV4:
if (!enable_IPv4)
return;
break;
case ACN::SDT::SDT_ADDR_IPV6:
if (!enable_IPv6)
return;
break;
default:
return;
}
/// Constructs an RLP PDU.
auto rlp = std::make_shared<RLP::Pdu>();
rlp->setVector(vector);
rlp->setHeader(rlp_header_);
rlp->setData(data);
/// Adds the PDU to the root block of the EPI 33 tranport format.
TCP::transport transport(true);
transport.root.pdu->push_back(rlp);
/// Fills an output stream with the transported PDU
size_t length = transport.streamSize();
uint8_t buffer[length];
PDU::Stream stream(new PDU::pdu_stream(buffer, length));
*stream << transport;
/// Platform devices shall impliment the sending on the network.
sendTCP(stream, ip);
}
/**
* @brief Component::RlpReceiver
* @param root
*
* Dispatch a recieved RLP PDU to the appropriate vector handlers.
*/
void Component::RlpReceiver(PDU::Message<RLP::Pdu> root)
{
/// To this point, the data segment of the RLP PDU will not have been read.
/// The root PDU must have not failed, and still have bytes available to read.
if (!root->stream()->good())
return;
/// PDU with unregistered vectors are ignored.
if (!rlp_vectors_.count(root->vector()))
return;
/// The root PDU is passed to every registered handler for the vector, in the
/// order in which the handlers were registered.
for(const auto &handler : rlp_vectors_[root->vector()])
handler(root);
}
/**
* @brief Add callback handler for an RLP vector.
* @param vect
* @param handle
*/
void Component::RlpRegisterVector(uint32_t vect, PDU::Handler<RLP::Pdu> handle)
{
rlp_vectors_[vect].push_back(handle);
}
/**
* @brief Component::sendUDP
* @param stream
* @param ip
*
* \attention Override this method in the derived platform class that can
* write the datagram to the network.
*
* The base class behavior is to do nothing.
*/
void Component::sendUDP(const PDU::Stream stream,
const SDT::UDP::ipAddress& ip)
{
(void)stream;
(void)ip;
}
/**
* @brief Component::sendTCP
* @param stream
* @param ip
*
* \attention Override this method in the derived platform class that can
* write the packet to the network.
*
* The base class behavior is to do nothing.
*/
void Component::sendTCP(const PDU::Stream stream,
const SDT::UDP::ipAddress& ip)
{
(void)stream;
(void)ip;
}
}; // ACN::RLP