/* 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 { /** * @brief Universe::Universe * @param src */ Universe::Universe(Source* src) : DMX::Universe(E131_NETWORK_DATA_LOSS_TIMEOUT) , ACN::DMP::Component(UUID::uuid(), "OpenLCP sACN Universe") , metadata_(std::make_shared()) , active_data_slots_(1) // start code , sync_sequence_(0) , sender_(src ? new UniverseSender(src, this) : nullptr) { destination.type = ACN::SDT::SDT_ADDR_NULL; } /** * @brief Universe::~Universe */ Universe::~Universe() { // shut down tx worker thread delete sender_; // delete sync data pointer delete sync_data_; } /** * @brief Universe::rxDmpGetProperty * @param message */ void Universe::rxDmpGetProperty(ACN::PDU::Message message) { (void)message; } /** * @brief Universe::rxDmpSetProperty * @param message */ void Universe::rxDmpSetProperty(ACN::PDU::Message message) { // only act on the first property pair in the data if (!message->data()) return; auto set_data = std::static_pointer_cast(message->data()); 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; 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 message) { (void)message; } /** * @brief Universe::setMetadata * @param metadata */ void Universe::setMetadata(std::shared_ptr metadata) { metadata_ = metadata; } /** * @brief Universe::isSyncronized * @return */ bool Universe::isSyncronized() { return !sync_data_; }; /** * @brief Universe::isEditable * @return */ bool Universe::isEditable() const { return sender_; } /** * @brief Universe::sources * @return */ const std::vector Universe::sources() const { std::vector keys = {*metadata_}; return keys; } /** * @brief Add a callback to be notified when the source list changes. * @param callback * @return */ std::shared_ptr Universe::onSourceListChange(std::function callback) { (void)callback; return nullptr; }; /** * @brief sourceUniverse * @param src * @return */ std::shared_ptr Universe::sourceUniverse(const DATA::data_header& src) { if (src == *metadata_) return shared_from_this(); return nullptr; } /** * @brief Universe::addNewSource * @return */ std::shared_ptr Universe::addNewSource(const DATA::data_header&) { return nullptr; } /** * @brief Universe::activeSlots * @return */ uint16_t Universe::activeSlots() { return active_data_slots_; } void Universe::setValue (const uint16_t start, const uint16_t footprint, const uint8_t* data) { if (!isEditable()) 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_) active_data_slots_ = start + footprint; // 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); } // 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(); } void Universe::setStatus(uint8_t status) { if (status == RX_TIMEOUT) status = sACN_TERMINATED; DMX::Universe::setStatus(status); } /** * @brief Universe::metadata * @return */ std::shared_ptr Universe::metadata() { return metadata_; } /** * @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) return; sync_sequence_ = sequence_number; DMX::Universe::setData(*sync_data_); resetSynchronization(); } /** * @brief Universe::resetSynchronization */ void Universe::resetSynchronization() { delete sync_data_; sync_data_ = nullptr; } /** * @brief Universe::setSyncData * @param data */ void Universe::setSyncData(const std::vector & data) { resetSynchronization(); sync_data_ = new std::vector(data); } }; // namespace SACN