1
0
Fork 0
OpenLCP/protocol/esta/sacn/universe.cpp

426 lines
9.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 "config.h"
#include "universe.h"
namespace sACN {
/**
* @brief Universe::Universe
* @param src
* @param rsv
*/
Universe::Universe(Source* src, Receiver* rsv)
: DMX::Universe(E131_NETWORK_DATA_LOSS_TIMEOUT)
, ACN::DMP::Component(UUID::uuid(), "OpenLCP sACN Universe")
, arbitrator_(rsv ? new UniverseArbitrator(this) : nullptr)
, sender_(src ? new UniverseSender(src, this) : nullptr)
, metadata_(std::make_shared<DATA::data_header>())
, sync_data_(nullptr)
, active_data_slots_(1) // start code
, sync_sequence_(0)
{
destination.type = ACN::SDT::SDT_ADDR_NULL;
}
/**
* @brief Universe::~Universe
*/
Universe::~Universe()
{
delete arbitrator_;
delete sender_;
delete sync_data_;
}
/**
* @brief Universe::age
* @return
*/
long Universe::age() const
{
if (arbitrator_)
return arbitrator_->age();
return DMX::Universe::age();
}
/**
* @brief Universe::rxRate
* @return
*/
double Universe::rxRate()
{
if (arbitrator_)
return arbitrator_->rxRate();
return DMX::Universe::rxRate();
}
/**
* @brief Universe::status
* @return
*/
uint8_t Universe::status() const
{
if (arbitrator_)
return arbitrator_->status();
return DMX::Universe::status();
}
/**
* @brief Universe::slot
* @param address
* @return
*/
uint8_t Universe::slot(const uint16_t address) const
{
if (arbitrator_)
return arbitrator_->slot(address);
return DMX::Universe::slot(address);
}
/**
* @brief Universe::setStatus
* @param status
*/
void Universe::setStatus(uint8_t status)
{
if (status == RX_TIMEOUT)
status = sACN_TERMINATED;
DMX::Universe::setStatus(status);
}
/**
* @brief Universe::setValue
* @param start
* @param footprint
* @param data
*/
void Universe::setValue (const uint16_t start, const uint16_t footprint,
const uint8_t* data)
{
if (!isEditable())
return;
if (arbitrator_)
return;
// start and footprint valid?
if (start < 1 || start + footprint > null_start_data.size())
return;
// set active_data_slots to at least end of footprint
if (start + footprint > active_data_slots_)
{
std::unique_lock lk_ctl(mtx_control);
active_data_slots_ = start + footprint;
}
// get a copy of the current values
uint8_t og[footprint];
{
std::unique_lock lk_data(mtx_data);
std::copy_n(std::begin(null_start_data) + start, footprint, og);
}
// data not changed!
if (memcmp(data, og, footprint) == 0)
return;
// set the values
DMX::Universe::setValue(start, footprint, data);
// request sACN message to be sent
sender_->flush();
}
/**
* @brief Universe::metadata
* @return
*/
std::shared_ptr<DATA::data_header> Universe::metadata() const
{
if (arbitrator_)
return arbitrator_->metadata();
std::shared_lock lk_ctl(mtx_control);
return metadata_;
}
/**
* @brief Universe::setMetadata
* @param metadata
*/
void Universe::setMetadata(std::shared_ptr<DATA::data_header> metadata)
{
if (arbitrator_)
return;
// bool update = false;
// if (metadata_->source_name != metadata->source_name ||
// metadata_->priority != metadata->priority)
// update = true;
{
std::unique_lock lk_ctl(mtx_control);
metadata_ = metadata;
}
// if (update)
// doStatusCallbacks();
}
/**
* @brief Universe::isSyncronized
* @return
*/
bool Universe::isSyncronized() const
{
if (arbitrator_)
return true;
std::shared_lock lk_ctl(mtx_control);
return !sync_data_;
};
/**
* @brief Universe::synchronize
* @param sequence_number
*/
void Universe::synchronize(uint8_t sequence_number)
{
if (arbitrator_)
return;
if (!sync_data_)
return;
int8_t dif;
{
std::shared_lock lk_ctl(mtx_control);
/// > \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;
dif = b - a;
}
if (dif <= 0 && dif > -20)
return;
{
std::unique_lock lk_ctl(mtx_control);
sync_sequence_ = sequence_number;
}
DMX::Universe::setData(*sync_data_);
resetSynchronization();
doDataCallbacks();
}
/**
* @brief Universe::resetSynchronization
*/
void Universe::resetSynchronization()
{
std::unique_lock lk_ctl(mtx_control);
delete sync_data_;
sync_data_ = nullptr;
}
/**
* @brief Universe::setSyncData
* @param data
*/
void Universe::setSyncData(const std::vector<uint8_t> & data)
{
if (arbitrator_)
return;
resetSynchronization();
std::unique_lock lk_ctl(mtx_control);
sync_data_ = new std::vector<uint8_t>(data);
}
/**
* @brief Universe::isEditable
* @return
*/
bool Universe::isEditable() const
{
std::shared_lock lk_ctl(mtx_control);
return sender_;
}
/**
* @brief Universe::activeSlots
* @return
*/
uint16_t Universe::activeSlots() const
{
if (arbitrator_)
return arbitrator_->activeSlots();
std::shared_lock lk_ctl(mtx_control);
return active_data_slots_;
}
/**
* @brief Universe::hasSources
* @return
*/
bool Universe::hasSources() const
{
std::shared_lock lk_ctl(mtx_control);
return arbitrator_;
}
/**
* @brief Universe::sources
* @return
*/
const std::vector<DATA::data_header> Universe::sources() const
{
if (arbitrator_)
return arbitrator_->sources();
return std::vector<DATA::data_header>();
}
/**
* @brief Universe::arbitrator
* @return
*/
UniverseArbitrator* Universe::arbitrator() const
{
return arbitrator_;
}
/**
* @brief Universe::rxDmpGetProperty
* @param message
*/
void Universe::rxDmpGetProperty(ACN::PDU::Message<ACN::DMP::Pdu> message)
{
(void)message;
}
/**
* @brief Universe::rxDmpSetProperty
* @param message
*/
void Universe::rxDmpSetProperty(ACN::PDU::Message<ACN::DMP::Pdu> message)
{
if (arbitrator_)
return;
// only act on the first property pair in the data
if (!message->data())
return;
#ifdef RTTI_ENABLED
auto set_data = std::dynamic_pointer_cast<ACN::DMP::address_pair_list>(message->data());
#else
auto set_data = std::static_pointer_cast<ACN::DMP::address_pair_list>(message->data());
#endif
if (set_data->properties.empty())
return;
const auto& [range, data] = set_data->properties.front();
/// > \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 (range.isRelative()) return;
if (range.type() != ACN::DMP::ARRAY) return;
if (range.elementLength() != ACN::DMP::TWO) return;
/// > \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 > null_start_data.size())
return;
{
std::unique_lock lk_ctl(mtx_control);
active_data_slots_ = range.address + range.count;
}
/// > \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);
}
/**
* @brief Universe::rxDmpGetProperty
* @param message
*/
void Universe::rxDmpSubscribe(ACN::PDU::Message<ACN::DMP::Pdu> message)
{
(void)message;
}
}; // namespace SACN