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"
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
Universe::Universe()
|
2021-08-06 12:36:31 -04:00
|
|
|
: DMX::Universe(E131_NETWORK_DATA_LOSS_TIMEOUT)
|
2021-08-29 11:17:53 -04:00
|
|
|
, active_data_slots(0)
|
2021-08-29 01:00:50 -04:00
|
|
|
, provenance_(std::shared_ptr<DATA::data_header>(new DATA::data_header()))
|
2021-08-02 10:09:14 -04:00
|
|
|
{
|
2021-08-26 16:49:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Universe::~Universe
|
|
|
|
*/
|
|
|
|
Universe::~Universe()
|
|
|
|
{
|
|
|
|
delete sync_data_;
|
2021-08-15 23:36:29 -04:00
|
|
|
}
|
2021-08-02 10:09:14 -04:00
|
|
|
|
|
|
|
|
2021-05-27 10:59:22 -04:00
|
|
|
/**
|
2021-08-25 17:20:33 -04:00
|
|
|
* @brief Universe::set
|
|
|
|
* @param dmp
|
|
|
|
* @param source
|
|
|
|
*/
|
2021-08-26 16:49:58 -04:00
|
|
|
void Universe::set(ACN::PDU::Message<ACN::DMP::Pdu> dmp,
|
2021-08-28 14:47:30 -04:00
|
|
|
std::shared_ptr<DATA::data_header> source)
|
2021-08-15 23:36:29 -04:00
|
|
|
{
|
2021-08-28 12:54:26 -04: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.
|
2021-05-27 10:59:22 -04:00
|
|
|
if (!dmp->header())
|
|
|
|
return;
|
2021-08-02 10:09:14 -04:00
|
|
|
auto type = static_cast<ACN::DMP::address_type*>(dmp->header());
|
2021-05-27 10:59:22 -04:00
|
|
|
if (type->relative) return;
|
2021-08-27 09:02:44 -04:00
|
|
|
if (type->data_type != ACN::DMP::ARRAY) return;
|
|
|
|
if (type->address_length != ACN::DMP::TWO) return;
|
2021-05-27 10:59:22 -04:00
|
|
|
|
|
|
|
// only act on the first property pair in the data
|
|
|
|
if (!dmp->data())
|
|
|
|
return;
|
2021-08-15 21:43:36 -04:00
|
|
|
auto set_data = static_cast<ACN::DMP::address_pair_list*>(dmp->data());
|
2021-08-02 10:09:14 -04:00
|
|
|
const auto& [range, data] = set_data->properties.front();
|
2021-05-27 10:59:22 -04:00
|
|
|
|
2021-08-28 12:54:26 -04:00
|
|
|
/// > \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;
|
|
|
|
|
2021-08-28 12:54:26 -04:00
|
|
|
/// > \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;
|
|
|
|
|
2021-08-28 12:54:26 -04:00
|
|
|
/// > \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).
|
2021-08-02 10:09:14 -04:00
|
|
|
if (range.count < 1 || range.count > 513)
|
2021-05-27 10:59:22 -04:00
|
|
|
return;
|
|
|
|
|
2021-08-28 09:04:06 -04:00
|
|
|
active_data_slots = range.address + range.count;
|
2021-08-24 18:10:20 -04:00
|
|
|
setProvenance(source);
|
2021-07-31 10:14:26 -04:00
|
|
|
|
2021-08-28 12:54:26 -04:00
|
|
|
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.
|
2021-08-28 09:01:33 -04:00
|
|
|
(source->sync_address == 0) ||
|
2021-08-28 12:54:26 -04:00
|
|
|
/// > \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.
|
2021-08-28 09:01:33 -04:00
|
|
|
(isSyncronized() && source->options.force_synchronization && rxRate() == 0))
|
2021-08-15 23:36:29 -04:00
|
|
|
{
|
2021-08-26 16:49:58 -04:00
|
|
|
if (sync_data_)
|
|
|
|
{
|
|
|
|
delete sync_data_;
|
|
|
|
sync_data_ = nullptr;
|
|
|
|
}
|
2021-08-28 12:54:26 -04: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.
|
2021-08-06 12:36:04 -04:00
|
|
|
DMX::Universe::setData(data);
|
|
|
|
}
|
|
|
|
else
|
2021-08-26 16:49:58 -04:00
|
|
|
sync_data_ = new std::vector<uint8_t>(data);
|
2021-06-20 09:09:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Universe::setSource
|
|
|
|
* @param source
|
|
|
|
*/
|
2021-08-28 14:47:30 -04:00
|
|
|
void Universe::setProvenance(std::shared_ptr<DATA::data_header> source)
|
2021-06-20 09:09:03 -04:00
|
|
|
{
|
2021-08-24 18:10:20 -04:00
|
|
|
provenance_ = source;
|
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
|
|
|
|
*/
|
|
|
|
bool Universe::isSyncronized() const
|
|
|
|
{
|
2021-08-26 16:49:58 -04:00
|
|
|
return (sync_data_ != nullptr);
|
2021-08-25 17:20:33 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Universe::source
|
|
|
|
* @return
|
|
|
|
*/
|
2021-08-28 14:47:30 -04:00
|
|
|
std::shared_ptr<DATA::data_header> Universe::provenance() const
|
2021-08-25 17:20:33 -04:00
|
|
|
{
|
|
|
|
return provenance_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-08-26 16:49:58 -04:00
|
|
|
* @brief Universe::synchronize
|
2021-08-29 01:12:11 -04:00
|
|
|
* @param sequence_number
|
2021-08-25 17:20:33 -04:00
|
|
|
*/
|
2021-08-29 01:12:11 -04:00
|
|
|
void Universe::synchronize(uint8_t sequence_number)
|
2021-08-06 12:36:04 -04:00
|
|
|
{
|
2021-08-29 01:12:11 -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)
|
|
|
|
/// 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;
|
2021-08-06 12:36:04 -04:00
|
|
|
}
|
|
|
|
|
2021-08-24 18:10:20 -04:00
|
|
|
}; // namespace SACN
|