1
0
Fork 0
OpenLCP/protocols/sacn/universe.cpp

253 lines
7.3 KiB
C++

/*
universe.cpp
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.
*/
#include "source.h"
#include "universe.h"
namespace sACN {
/**
* @brief Universe::Universe
* @param src
*/
Universe::Universe(Source* src)
: DMX::Universe(E131_NETWORK_DATA_LOSS_TIMEOUT)
, active_data_slots(0)
, provenance_(std::shared_ptr<DATA::data_header>(new DATA::data_header()))
, source_(src)
{
destination.type = ACN::SDT::SDT_ADDR_NULL;
}
/**
* @brief Universe::~Universe
*/
Universe::~Universe()
{
delete sync_data_;
}
/**
* @brief Universe::set
* @param dmp
* @param source
*/
void Universe::set(ACN::PDU::Message<ACN::DMP::Pdu> dmp,
std::shared_ptr<DATA::data_header> source)
{
/// > \cite sACN 7.3 Address Type and Data Type
/// >
/// > Sources shall set the DMP Layer's Address Type and Data Type to 0xa1.
/// > Receivers shall discard the packet if the received value is not 0xa1.
if (!dmp->header())
return;
auto type = static_cast<ACN::DMP::address_type*>(dmp->header());
if (type->relative) return;
if (type->data_type != ACN::DMP::ARRAY) return;
if (type->address_length != ACN::DMP::TWO) return;
// only act on the first property pair in the data
if (!dmp->data())
return;
auto set_data = static_cast<ACN::DMP::address_pair_list*>(dmp->data());
const auto& [range, data] = set_data->properties.front();
/// > \cite sACN 7.4 First Property Address
/// >
/// > Sources shall set the DMP Layer's First Property Address to 0x0000.
/// > Receivers shall discard the packet if the received value is not 0x0000.
if (range.address != 0)
return;
/// > \cite sACN 7.5 Address Increment
/// >
/// > Sources shall set the DMP Layer's Address Increment to 0x0001.
/// > Receivers shall discard the packet if the received value is not 0x0001.
if (range.incriment != 1)
return;
/// > \cite sACN 7.6 Property Value Count
/// >
/// > The DMP Layer's Property Value Count is used to encode the number of
/// > DMX512-A [DMX] Slots (including the START Code slot).
if (range.count < 1 || range.count > 513)
return;
active_data_slots = range.address + range.count;
setProvenance(source);
if (/// > \cite sACN 6.2.4.1 If a receiver is presented with an E1.31 Data
/// >
/// > Packet containing a Synchronization Address of 0, it shall discard
/// > any data waiting to be processed and immediately act on that Data
/// > Packet.
(source->sync_address == 0) ||
/// > \cite sACN 6.2.6 E1.31 Data Packet: Options Force_Synchronization
/// >
/// > This bit indicates whether to lock or revert to an unsynchronized
/// > state when synchronization is lost. When set to 0, components that
/// > had been operating in a synchronized state shall not update with
/// > any new packets until synchronization resumes. When set to 1, once
/// > synchronization has been lost, components that had been operating
/// > in a synchronized state need not wait for a new E1.31
/// > Synchronization Packet in order to update to the next E1.31 Data
/// > Packet.
(isSyncronized() && source->options.force_synchronization && rxRate() == 0))
{
if (sync_data_)
{
delete sync_data_;
sync_data_ = nullptr;
}
/// > \cite sACN 7.7 Property Values (DMX512-A Data)
/// >
/// > The DMP Layer's Property values field is used to encode the
/// > DMX512-A [DMX] START Code and data.
DMX::Universe::setData(data);
}
else
sync_data_ = new std::vector<uint8_t>(data);
}
/**
* @brief Universe::setSource
* @param source
*/
void Universe::setProvenance(std::shared_ptr<DATA::data_header> source)
{
provenance_ = source;
}
/**
* @brief Universe::isSyncronized
* @return
*/
bool Universe::isSyncronized() const
{
return (sync_data_ != nullptr);
};
/**
* @brief Universe::source
* @return
*/
std::shared_ptr<DATA::data_header> Universe::provenance() const
{
return provenance_;
}
/**
* @brief Universe::synchronize
* @param sequence_number
*/
void Universe::synchronize(uint8_t sequence_number)
{
if (!sync_data_)
return;
/// > \cite sACN 6.7.2 Sequence Numbering
/// >
/// > Having first received a packet with sequence number A,
/// > a second packet with sequence number B arrives.
/// > If, using signed 8-bit binary arithmetic, B - A
/// > is less than or equal to 0, but greater than -20,
/// > then the packet containing sequence number B shall be deemed out of
/// > sequence and discarded.
auto a = sync_sequence_;
auto b = sequence_number;
int8_t dif = b - a;
if (dif <= 0 && dif > -20)
/// Tolerate out-of-spec sources transmitting 0s instead of a sequence number.
if (!(a == 0 && b == 0))
return;
sync_sequence_ = sequence_number;
DMX::Universe::setData(*sync_data_);
delete sync_data_;
sync_data_ = nullptr;
}
/**
* @brief Universe::sACNsend
*/
void Universe::sACNsend() const
{
// header
ACN::DMP::address_type addrtyp;
addrtyp.byte = 0;
addrtyp.address_length = ACN::DMP::TWO;
addrtyp.data_type = ACN::DMP::ARRAY;
addrtyp.relative = false;
// property range
ACN::DMP::range pr(addrtyp);
pr.address = 0;
pr.incriment = 1;
pr.count = (active_data_slots <= 513 ? active_data_slots : 513);
// property data
std::vector<octet> pd;
std::copy(null_start_data.begin(), null_start_data.begin() + pr.count,
std::back_inserter(pd));
// data segment
ACN::DMP::address_pair_list addrlst(addrtyp);
addrlst.properties.push_back(ACN::DMP::address_data_pair(pr, pd));
// DMP layer
auto dmp = std::make_shared<ACN::DMP::Pdu>();
dmp->setVector(ACN::DMP::SET_PROPERTY);
dmp->setHeader(&addrtyp);
dmp->setData(&addrlst);
// sACN Framing Layer
auto frame = std::make_shared<DATA::Pdu>();
frame->setVector(VECTOR_E131_DATA_PACKET);
frame->setHeader(provenance_.get());
frame->setData(dmp.get());
// send
switch (destination.type)
{
case ACN::SDT::SDT_ADDR_NULL:
source_->rlpSendUdp(VECTOR_ROOT_E131_DATA, frame,
IPv4MulticastAddress(provenance_->universe));
source_->rlpSendUdp(VECTOR_ROOT_E131_DATA, frame,
IPv6MulticastAddress(provenance_->universe));
break;
default:
source_->rlpSendUdp(VECTOR_ROOT_E131_DATA, frame, destination);
}
}
}; // namespace SACN