2021-05-27 10:59:22 -04:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2022-12-09 16:04:31 -05:00
|
|
|
#include "config.h"
|
2021-05-27 10:59:22 -04:00
|
|
|
#include "universe.h"
|
|
|
|
|
2021-08-28 09:01:33 -04:00
|
|
|
namespace sACN {
|
2021-05-27 10:59:22 -04:00
|
|
|
|
2021-08-02 10:09:14 -04:00
|
|
|
/**
|
|
|
|
* @brief Universe::Universe
|
2021-08-31 09:27:53 -04:00
|
|
|
* @param src
|
2022-12-12 13:39:10 -05:00
|
|
|
* @param rsv
|
2021-08-02 10:09:14 -04:00
|
|
|
*/
|
2022-12-12 13:39:10 -05:00
|
|
|
Universe::Universe(Source* src, Receiver* rsv)
|
2021-08-06 12:36:31 -04:00
|
|
|
: DMX::Universe(E131_NETWORK_DATA_LOSS_TIMEOUT)
|
2022-12-08 17:50:29 -05:00
|
|
|
, ACN::DMP::Component(UUID::uuid(), "OpenLCP sACN Universe")
|
2022-12-12 13:39:10 -05:00
|
|
|
, arbitrator_(rsv ? new UniverseArbitrator(this) : nullptr)
|
2022-12-09 15:16:03 -05:00
|
|
|
, sender_(src ? new UniverseSender(src, this) : nullptr)
|
2022-12-08 10:12:25 -05:00
|
|
|
, metadata_(std::make_shared<DATA::data_header>())
|
2022-12-10 11:12:44 -05:00
|
|
|
, sync_data_(nullptr)
|
2022-11-23 14:27:40 -05:00
|
|
|
, sync_sequence_(0)
|
2021-08-02 10:09:14 -04:00
|
|
|
{
|
2023-04-16 11:05:04 -04:00
|
|
|
device_class_ = sender_ ? DMX::CONTROLLER : DMX::RECEIVER;
|
2021-08-30 10:00:54 -04:00
|
|
|
destination.type = ACN::SDT::SDT_ADDR_NULL;
|
2021-08-26 16:49:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Universe::~Universe
|
|
|
|
*/
|
|
|
|
Universe::~Universe()
|
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
delete arbitrator_;
|
2022-12-08 19:09:39 -05:00
|
|
|
delete sender_;
|
2021-08-26 16:49:58 -04:00
|
|
|
delete sync_data_;
|
2021-08-15 23:36:29 -04:00
|
|
|
}
|
2021-08-02 10:09:14 -04:00
|
|
|
|
|
|
|
|
2022-12-12 13:39:10 -05:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
2023-04-16 11:03:53 -04:00
|
|
|
void Universe::setValue(const uint16_t start, const uint16_t footprint,
|
2022-12-12 13:39:10 -05:00
|
|
|
const uint8_t* data)
|
|
|
|
{
|
|
|
|
if (!isEditable())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (arbitrator_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// start and footprint valid?
|
|
|
|
if (start < 1 || start + footprint > null_start_data.size())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-27 10:59:22 -04:00
|
|
|
/**
|
2022-12-09 15:16:03 -05:00
|
|
|
* @brief Universe::metadata
|
|
|
|
* @return
|
2021-08-25 17:20:33 -04:00
|
|
|
*/
|
2022-12-10 11:12:44 -05:00
|
|
|
std::shared_ptr<DATA::data_header> Universe::metadata() const
|
2021-08-15 23:36:29 -04:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
if (arbitrator_)
|
|
|
|
return arbitrator_->metadata();
|
|
|
|
|
|
|
|
std::shared_lock lk_ctl(mtx_control);
|
|
|
|
return metadata_;
|
2022-12-08 18:05:33 -05:00
|
|
|
}
|
|
|
|
|
2021-05-27 10:59:22 -04:00
|
|
|
|
2022-12-08 18:05:33 -05:00
|
|
|
/**
|
2022-12-09 15:16:03 -05:00
|
|
|
* @brief Universe::setMetadata
|
|
|
|
* @param metadata
|
2022-12-08 18:05:33 -05:00
|
|
|
*/
|
2022-12-09 15:16:03 -05:00
|
|
|
void Universe::setMetadata(std::shared_ptr<DATA::data_header> metadata)
|
2022-12-08 18:05:33 -05:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
if (arbitrator_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// bool update = false;
|
|
|
|
// if (metadata_->source_name != metadata->source_name ||
|
|
|
|
// metadata_->priority != metadata->priority)
|
|
|
|
// update = true;
|
|
|
|
|
|
|
|
{
|
2022-12-09 11:18:27 -05:00
|
|
|
std::unique_lock lk_ctl(mtx_control);
|
2022-12-09 15:16:03 -05:00
|
|
|
metadata_ = metadata;
|
2022-12-12 13:39:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// if (update)
|
|
|
|
// doStatusCallbacks();
|
2021-06-20 09:09:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-12-09 15:16:03 -05:00
|
|
|
* @brief Universe::isSyncronized
|
|
|
|
* @return
|
2021-06-20 09:09:03 -04:00
|
|
|
*/
|
2022-12-10 11:12:44 -05:00
|
|
|
bool Universe::isSyncronized() const
|
2021-06-20 09:09:03 -04:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
if (arbitrator_)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
std::shared_lock lk_ctl(mtx_control);
|
|
|
|
return !sync_data_;
|
2022-12-09 15:16:03 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Universe::synchronize
|
|
|
|
* @param sequence_number
|
|
|
|
*/
|
|
|
|
void Universe::synchronize(uint8_t sequence_number)
|
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
if (arbitrator_)
|
|
|
|
return;
|
2022-12-09 15:16:03 -05:00
|
|
|
if (!sync_data_)
|
|
|
|
return;
|
2022-12-09 23:09:52 -05:00
|
|
|
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;
|
|
|
|
}
|
2022-12-09 15:16:03 -05:00
|
|
|
if (dif <= 0 && dif > -20)
|
|
|
|
return;
|
|
|
|
{
|
2022-12-09 23:09:52 -05:00
|
|
|
std::unique_lock lk_ctl(mtx_control);
|
2022-12-09 15:16:03 -05:00
|
|
|
sync_sequence_ = sequence_number;
|
|
|
|
}
|
|
|
|
DMX::Universe::setData(*sync_data_);
|
|
|
|
resetSynchronization();
|
2022-12-11 16:45:37 -05:00
|
|
|
doDataCallbacks();
|
2022-12-08 18:05:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-08 17:47:37 -05:00
|
|
|
/**
|
2022-12-09 15:16:03 -05:00
|
|
|
* @brief Universe::resetSynchronization
|
2022-12-08 17:47:37 -05:00
|
|
|
*/
|
2022-12-09 15:16:03 -05:00
|
|
|
void Universe::resetSynchronization()
|
2022-12-08 17:47:37 -05:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
std::unique_lock lk_ctl(mtx_control);
|
|
|
|
delete sync_data_;
|
|
|
|
sync_data_ = nullptr;
|
2021-05-27 10:59:22 -04:00
|
|
|
}
|
|
|
|
|
2021-08-02 10:09:14 -04:00
|
|
|
|
2021-08-25 17:20:33 -04:00
|
|
|
/**
|
2022-12-09 15:16:03 -05:00
|
|
|
* @brief Universe::setSyncData
|
|
|
|
* @param data
|
2021-08-25 17:20:33 -04:00
|
|
|
*/
|
2022-12-09 15:16:03 -05:00
|
|
|
void Universe::setSyncData(const std::vector<uint8_t> & data)
|
2021-08-25 17:20:33 -04:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
if (arbitrator_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
resetSynchronization();
|
|
|
|
std::unique_lock lk_ctl(mtx_control);
|
|
|
|
sync_data_ = new std::vector<uint8_t>(data);
|
2022-12-09 15:16:03 -05:00
|
|
|
}
|
2021-08-25 17:20:33 -04:00
|
|
|
|
|
|
|
|
2021-09-10 12:00:50 -04:00
|
|
|
/**
|
2022-12-09 15:16:03 -05:00
|
|
|
* @brief Universe::activeSlots
|
2021-09-10 12:00:50 -04:00
|
|
|
* @return
|
|
|
|
*/
|
2022-12-10 11:12:44 -05:00
|
|
|
uint16_t Universe::activeSlots() const
|
2021-09-10 12:00:50 -04:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
if (arbitrator_)
|
|
|
|
return arbitrator_->activeSlots();
|
2021-09-10 12:00:50 -04:00
|
|
|
|
2023-04-16 11:03:53 -04:00
|
|
|
return DMX::Universe::activeSlots();
|
2022-12-09 15:16:03 -05:00
|
|
|
}
|
2021-09-10 12:00:50 -04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-12-12 13:39:10 -05:00
|
|
|
* @brief Universe::hasSources
|
2021-09-10 12:00:50 -04:00
|
|
|
* @return
|
|
|
|
*/
|
2022-12-12 13:39:10 -05:00
|
|
|
bool Universe::hasSources() const
|
2021-09-10 12:00:50 -04:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
std::shared_lock lk_ctl(mtx_control);
|
|
|
|
return arbitrator_;
|
2021-09-10 12:00:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-03 17:43:50 -04:00
|
|
|
/**
|
2022-12-12 13:39:10 -05:00
|
|
|
* @brief Universe::sources
|
2022-12-10 13:20:39 -05:00
|
|
|
* @return
|
|
|
|
*/
|
2022-12-12 13:39:10 -05:00
|
|
|
const std::vector<DATA::data_header> Universe::sources() const
|
2022-12-10 13:20:39 -05:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
if (arbitrator_)
|
|
|
|
return arbitrator_->sources();
|
2022-12-10 13:20:39 -05:00
|
|
|
|
2022-12-12 13:39:10 -05:00
|
|
|
return std::vector<DATA::data_header>();
|
2022-12-11 14:55:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-12-12 13:39:10 -05:00
|
|
|
* @brief Universe::arbitrator
|
2022-12-11 14:55:05 -05:00
|
|
|
* @return
|
|
|
|
*/
|
2022-12-12 13:39:10 -05:00
|
|
|
UniverseArbitrator* Universe::arbitrator() const
|
2022-12-11 14:55:05 -05:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
return arbitrator_;
|
2022-11-21 17:41:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-25 17:20:33 -04:00
|
|
|
/**
|
2022-12-09 15:16:03 -05:00
|
|
|
* @brief Universe::rxDmpGetProperty
|
|
|
|
* @param message
|
2021-08-25 17:20:33 -04:00
|
|
|
*/
|
2022-12-09 15:16:03 -05:00
|
|
|
void Universe::rxDmpGetProperty(ACN::PDU::Message<ACN::DMP::Pdu> message)
|
2021-08-06 12:36:04 -04:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
(void)message;
|
2022-12-08 18:02:43 -05:00
|
|
|
}
|
2021-08-29 01:12:11 -04:00
|
|
|
|
2022-12-08 18:02:43 -05:00
|
|
|
|
|
|
|
/**
|
2022-12-09 15:16:03 -05:00
|
|
|
* @brief Universe::rxDmpSetProperty
|
|
|
|
* @param message
|
2022-12-08 18:02:43 -05:00
|
|
|
*/
|
2022-12-09 15:16:03 -05:00
|
|
|
void Universe::rxDmpSetProperty(ACN::PDU::Message<ACN::DMP::Pdu> message)
|
2022-12-08 18:02:43 -05:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
if (arbitrator_)
|
|
|
|
return;
|
|
|
|
|
2022-12-09 15:16:03 -05:00
|
|
|
// only act on the first property pair in the data
|
|
|
|
if (!message->data())
|
|
|
|
return;
|
2022-12-09 16:04:31 -05:00
|
|
|
#ifdef RTTI_ENABLED
|
|
|
|
auto set_data = std::dynamic_pointer_cast<ACN::DMP::address_pair_list>(message->data());
|
|
|
|
#else
|
2022-12-09 15:16:03 -05:00
|
|
|
auto set_data = std::static_pointer_cast<ACN::DMP::address_pair_list>(message->data());
|
2022-12-09 16:04:31 -05:00
|
|
|
#endif
|
2022-12-09 15:16:03 -05:00
|
|
|
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;
|
2023-04-16 11:03:53 -04:00
|
|
|
|
2022-12-09 15:16:03 -05:00
|
|
|
/// > \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);
|
2021-08-06 12:36:04 -04:00
|
|
|
}
|
|
|
|
|
2021-08-31 10:27:30 -04:00
|
|
|
|
|
|
|
/**
|
2022-12-09 15:16:03 -05:00
|
|
|
* @brief Universe::rxDmpGetProperty
|
|
|
|
* @param message
|
2021-08-31 10:27:30 -04:00
|
|
|
*/
|
2022-12-09 15:16:03 -05:00
|
|
|
void Universe::rxDmpSubscribe(ACN::PDU::Message<ACN::DMP::Pdu> message)
|
2021-08-31 10:27:30 -04:00
|
|
|
{
|
2022-12-09 15:16:03 -05:00
|
|
|
(void)message;
|
2022-12-08 18:02:43 -05:00
|
|
|
}
|
2021-08-31 10:27:30 -04:00
|
|
|
|
2021-08-24 18:10:20 -04:00
|
|
|
}; // namespace SACN
|