1
0
Fork 0
OpenLCP/sacn/receiver.cpp

179 lines
6.3 KiB
C++

/*
receiver.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 "receiver.h"
#include "data.h"
namespace SACN {
/**
Constructor. Register RLP vector callbacks.
*/
Receiver::Receiver(UUID::uuid cid)
: Appliance(cid)
{
registerRlpVectorHandler(VECTOR_ROOT_E131_DATA,
std::bind(&Receiver::rootDataHandler, this, std::placeholders::_1));
registerRlpVectorHandler(VECTOR_ROOT_E131_EXTENDED,
std::bind(&Receiver::rootExtendedHandler, this, std::placeholders::_1));
}
void Receiver::subscribe(const uint16_t num) {
if (!universes_.count(num))
universes_.emplace(num, new SACN::Universe());
}
void Receiver::unsubscribe(const uint16_t num) {
if (universes_.count(num))
universes_.erase(num);
}
SACN::Universe * Receiver::universe(uint16_t universe) {
if (universes_.count(universe))
return universes_.at(universe);
return 0;
}
/**
Receive VECTOR_ROOT_E131_DATA vector'd packets.
@param pdu is a shared pointer to the PDU
*/
void Receiver::rootDataHandler(std::shared_ptr<RLP::Pdu> root) {
auto block = PDU::readBlock<DATA::Pdu>(root->buffer(), root);
if (root->buffer()->fail())
return;
for(auto const &frame : *block) {
// 6.2.1 E1.31 Data Packet: Vector
// Sources sending an E1.31 Data Packet shall set the E1.31 Layer's Vector
// to VECTOR_E131_DATA_PACKET. This value indicates that the E1.31 framing
// layer is wrapping a DMP PDU.
switch(frame->vector()) {
case VECTOR_E131_DATA_PACKET:
dataPacketHandler(frame);
break;
default:
break;
}
}
}
/**
Receive `VECTOR_ROOT_E131_DATA -> VECTOR_E131_DATA_PACKET` vector'd packets.
Merging will be based on frame header. PDU data will be read as a block of DMP PDUs and passed to the universe.
@param pdu is a shared pointer to the PDU
*/
void Receiver::dataPacketHandler(std::shared_ptr<DATA::Pdu> frame) {
// header may be inherited. check that one exists
if (!frame->header())
return;
auto source = std::shared_ptr<UniverseSource>(new UniverseSource(frame));
if (universes_.count(source->universe()) == 0)
return;
Universe * universe = universes_.at(source->universe());
// 6.2.3 E1.31 Data Packet: Priority
// No priority outside the range of 0 to 200 shall be transmitted on
// the network.
if (source->priority() > 200)
frame->buffer()->setstate(frame->buffer()->rdstate() |
std::ios_base::failbit);
// 6.2.6 E1.31 Data Packet: Options
// Preview_Data: Bit 7 (most significant bit)
// This bit, when set to 1, indicates that the data in this packet is intended for use in visualization or media server preview applications and shall not be used to generate live output.
if (source->isPreview())
return;
// 6.2.6 E1.31 Data Packet: Options
// Stream_Terminated: Bit 6
// allow E1.31 sources to terminate transmission of a stream or of
// universe synchronization without waiting for a timeout to occur.
// Any property values in an E1.31 Data Packet containing this bit
// shall be ignored.
if (source->isTerminated()) {
unsubscribe(source->universe());
return;
}
// 6.2.6 E1.31 Data Packet: Options
// Force_Synchronization: Bit 5
// 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.
if (!source->isForced() &&
universe->isSyncronized()) {
return; // operate in synchronized state?
}
// 6.2.4.1 Synchronization Address Usage in an E1.31 Data Packet
// a value of 0 in the Synchronization Address indicates that the universe
// data is not synchronized.
if (source->syncAddress() != 0) {
// TODO: do somthing to engage synchronization
}
// 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.
if (source->syncAddress() == 0 && universe->isSyncronized()) {
// TODO:: do something to break synchronization
}
// 6.2.3.1 Multiple Sources at Highest Priority
// It is possible for there to be multiple sources, all transmitting data at
// the highest currently active priority for a given universe. When this
// occurs, receivers must handle these sources in some way.
// TODO: do something with merging and arbitration
// PDU data will be a block of DMP
auto block = PDU::readBlock<DMP::Pdu>(frame->buffer(), frame);
if (frame->buffer()->fail())
return;
for (auto const &dmp : *block) {
// 7.2 DMP Layer: Vector
// The DMP Layer's Vector shall be set to VECTOR_DMP_SET_PROPERTY, which
// indicates a DMP Set Property message by sources. Receivers shall discard
// the packet if the received value is not VECTOR_DMP_SET_PROPERTY.
switch(dmp->vector()) {
case DMP::SET_PROPERTY:
universe->set(dmp, source);
break;
default:
break;
}
}
}
}; // SACN