OpenLCP/protocol/esta/sacn/universe.cpp

318 lines
7.2 KiB
C++
Raw Normal View History

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.
*/
#include "universe.h"
namespace sACN {
2021-05-27 10:59:22 -04:00
2021-08-02 10:09:14 -04:00
/**
* @brief Universe::Universe
* @param src
2021-08-02 10:09:14 -04:00
*/
Universe::Universe(Source* src)
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")
, metadata_(std::make_shared<DATA::data_header>())
, active_data_slots_(1) // start code
, sync_sequence_(0)
2022-12-09 09:25:29 -05:00
, sender_(src ? new UniverseSender(src, this) : nullptr)
2021-08-02 10:09:14 -04:00
{
destination.type = ACN::SDT::SDT_ADDR_NULL;
}
/**
* @brief Universe::~Universe
*/
Universe::~Universe()
{
2021-09-01 12:53:53 -04:00
// shut down tx worker thread
2022-12-08 19:09:39 -05:00
delete sender_;
2021-09-01 12:53:53 -04:00
// delete sync data pointer
delete sync_data_;
}
2021-08-02 10:09:14 -04:00
2021-05-27 10:59:22 -04:00
/**
2022-12-08 18:05:33 -05:00
* @brief Universe::rxDmpGetProperty
* @param message
2021-08-25 17:20:33 -04:00
*/
2022-12-08 18:05:33 -05:00
void Universe::rxDmpGetProperty(ACN::PDU::Message<ACN::DMP::Pdu> message)
{
2022-12-08 18:05:33 -05:00
(void)message;
}
2021-05-27 10:59:22 -04:00
2022-12-08 18:05:33 -05:00
/**
* @brief Universe::rxDmpSetProperty
* @param message
*/
void Universe::rxDmpSetProperty(ACN::PDU::Message<ACN::DMP::Pdu> message)
{
2021-05-27 10:59:22 -04:00
// only act on the first property pair in the data
2022-12-08 18:05:33 -05:00
if (!message->data())
return;
auto set_data = std::static_pointer_cast<ACN::DMP::address_pair_list>(message->data());
if (set_data->properties.empty())
2021-05-27 10:59:22 -04:00
return;
2021-08-02 10:09:14 -04:00
const auto& [range, data] = set_data->properties.front();
2021-05-27 10:59:22 -04:00
2022-12-08 18:05:33 -05:00
/// > \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.
2021-08-02 10:09:14 -04:00
if (range.address != 0)
2021-05-27 10:59:22 -04:00
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.
2021-08-02 10:09:14 -04:00
if (range.incriment != 1)
2021-05-27 10:59:22 -04:00
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).
2022-12-08 18:05:33 -05:00
if (range.count < 1 || range.count > null_start_data.size())
2021-05-27 10:59:22 -04:00
return;
active_data_slots_ = range.address + range.count;
2022-12-08 18:05:33 -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);
}
/**
2022-12-08 18:05:33 -05:00
* @brief Universe::rxDmpGetProperty
* @param message
*/
2022-12-08 18:05:33 -05:00
void Universe::rxDmpSubscribe(ACN::PDU::Message<ACN::DMP::Pdu> message)
{
2022-12-08 18:05:33 -05:00
(void)message;
}
2022-12-08 17:47:37 -05:00
/**
* @brief Universe::setMetadata
* @param metadata
*/
void Universe::setMetadata(std::shared_ptr<DATA::data_header> metadata)
{
metadata_ = metadata;
2021-05-27 10:59:22 -04:00
}
2021-08-02 10:09:14 -04:00
2021-08-25 17:20:33 -04:00
/**
* @brief Universe::isSyncronized
* @return
*/
2021-09-10 12:48:33 -04:00
bool Universe::isSyncronized()
2021-08-25 17:20:33 -04:00
{
2022-12-08 17:47:37 -05:00
return !sync_data_;
2021-08-25 17:20:33 -04:00
};
/**
* @brief Universe::isEditable
* @return
*/
bool Universe::isEditable() const
{
return sender_;
}
2021-09-10 12:00:50 -04:00
/**
* @brief Universe::sources
* @return
*/
const std::vector<DATA::data_header> Universe::sources() const
{
2022-12-08 17:47:37 -05:00
std::vector<DATA::data_header> keys = {*metadata_};
2021-09-10 12:00:50 -04:00
return keys;
}
/**
* @brief Add a callback to be notified when the source list changes.
2022-12-08 17:47:37 -05:00
* @param callback
* @return
2021-09-10 12:00:50 -04:00
*/
2022-12-08 17:47:37 -05:00
std::shared_ptr<void> Universe::onSourceListChange(std::function<void()> callback)
2021-09-10 12:00:50 -04:00
{
2022-12-08 17:47:37 -05:00
(void)callback;
return nullptr;
2021-09-10 12:00:50 -04:00
};
/**
* @brief sourceUniverse
* @param src
* @return
*/
std::shared_ptr<Universe> Universe::sourceUniverse(const DATA::data_header& src)
{
2022-12-08 17:47:37 -05:00
if (src == *metadata_)
2021-09-10 12:00:50 -04:00
return shared_from_this();
return nullptr;
}
/**
* @brief Universe::addNewSource
* @return
*/
std::shared_ptr<Universe> Universe::addNewSource(const DATA::data_header&)
{
return nullptr;
}
/**
* @brief Universe::activeSlots
* @return
*/
2021-09-10 12:48:33 -04:00
uint16_t Universe::activeSlots()
{
return active_data_slots_;
}
2021-09-01 12:37:38 -04:00
void Universe::setValue (const uint16_t start, const uint16_t footprint,
const uint8_t* data)
{
if (!isEditable())
return;
2021-09-01 12:37:38 -04:00
// start and footprint valid?
2022-12-06 13:24:09 -05:00
if (start < 1 || start + footprint > null_start_data.size())
2021-09-01 12:37:38 -04:00
return;
// set active_data_slots to at least end of footprint
2022-12-06 18:04:37 -05:00
if (start + footprint > active_data_slots_)
active_data_slots_ = start + footprint;
2021-09-01 12:37:38 -04:00
// get a copy of the current values
uint8_t og[footprint];
{
std::unique_lock lk_data(null_start_mutex);
std::copy_n(std::begin(null_start_data) + start, footprint, og);
}
2021-09-01 12:37:38 -04:00
// data not changed!
if (memcmp(data, og, footprint) == 0)
return;
// set the values
DMX::Universe::setValue(start, footprint, data);
2021-09-01 12:53:53 -04:00
// request sACN message to be sent
2022-12-08 19:09:39 -05:00
sender_->flush();
2021-09-01 12:37:38 -04:00
}
void Universe::setStatus(uint8_t status)
{
2022-12-02 13:04:06 -05:00
if (status == RX_TIMEOUT)
status = sACN_TERMINATED;
DMX::Universe::setStatus(status);
}
2021-08-25 17:20:33 -04:00
/**
* @brief Universe::metadata
2021-08-25 17:20:33 -04:00
* @return
*/
std::shared_ptr<DATA::data_header> Universe::metadata()
2021-08-25 17:20:33 -04:00
{
return metadata_;
2021-08-25 17:20:33 -04:00
}
/**
* @brief Universe::synchronize
* @param sequence_number
2021-08-25 17:20:33 -04:00
*/
void Universe::synchronize(uint8_t sequence_number)
2021-08-06 12:36:04 -04:00
{
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)
return;
sync_sequence_ = sequence_number;
DMX::Universe::setData(*sync_data_);
2022-12-08 18:02:43 -05:00
resetSynchronization();
}
2022-12-08 18:02:43 -05:00
/**
* @brief Universe::resetSynchronization
*/
void Universe::resetSynchronization()
{
delete sync_data_;
sync_data_ = nullptr;
2021-08-06 12:36:04 -04:00
}
2021-08-31 10:27:30 -04:00
/**
2022-12-08 18:02:43 -05:00
* @brief Universe::setSyncData
* @param data
2021-08-31 10:27:30 -04:00
*/
2022-12-08 18:02:43 -05:00
void Universe::setSyncData(const std::vector<uint8_t> & data)
2021-08-31 10:27:30 -04:00
{
2022-12-08 18:02:43 -05:00
resetSynchronization();
sync_data_ = new std::vector<uint8_t>(data);
}
2021-08-31 10:27:30 -04:00
}; // namespace SACN