1
0
Fork 0
OpenLCP/protocol/artistic/artnet/device.cpp

590 lines
13 KiB
C++

/*
device.cpp
Copyright (c) 2022 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 "device.h"
namespace ARTNET {
/**
* @brief Device::Device
* @param style
*
* \note A ArtPollReply packet is required to be broadcast to the Directed Broadcast address by
* all Art-Net devices on power up. Since information in that packet requres a fully configured
* device, it must be specifically sent later.
*/
Device::Device(Style style)
: styleCode(style)
, diagnostic_reporting_threshold(DpCritial)
, device_status {.rdm_capable=true, .authority=AuthorityUnused}
, _shortName("OpenLCP Device")
, _longName("Generic OpenLCP Art-Net Device")
, _report_code(RcPowerOk)
, _poll_reply_count(0)
, _report_text("initialized")
{
}
/**
* @brief Device::receive
* @param buffer
* @param origin
*/
void Device::receive(ACN::PDU::Stream buffer, ipAddress &origin)
{
if (origin.port != UDP_PORT || origin.type != 1) // ACN::SDT::SDT_ADDR_IPV4
return buffer->setstate(std::ios_base::badbit);
auto packet = std::make_shared<Packet>();
packet->iStream(buffer);
if (buffer->fail() || buffer->bad())
{
_report_code = RcParseFail;
_report_text = "unable to read packet";
return;
}
packet->originAddress = origin.address.ipv4.value;
receive(packet);
}
/**
* @brief Device::receive
* @param packet
*/
void Device::receive(std::shared_ptr<Packet> packet)
{
switch (packet->opcode()) {
case OpPoll:
rxArtPoll(std::static_pointer_cast<ArtPoll>(packet));
break;
case OpPollReply:
rxArtPollReply(std::static_pointer_cast<ArtPollReply>(packet));
break;
case OpDiagData:
rxArtDiagData(std::static_pointer_cast<ArtDiagData>(packet));
break;
case OpCommand:
rxArtCommand(std::static_pointer_cast<ArtCommand>(packet));
break;
case OpDmx:
rxArtDmx(std::static_pointer_cast<ArtDmx>(packet));
break;
case OpNzs:
rxArtNzs(std::static_pointer_cast<ArtNzs>(packet));
break;
case OpSync:
rxArtSync(std::static_pointer_cast<ArtSync>(packet));
break;
case OpAddress:
rxArtAddress(std::static_pointer_cast<ArtAddress>(packet));
break;
case OpInput:
rxArtInput(std::static_pointer_cast<ArtInput>(packet));
break;
case OpTodRequest:
rxArtTodRequest(std::static_pointer_cast<ArtTodRequest>(packet));
break;
case OpTodData:
rxArtTodData(std::static_pointer_cast<ArtTodData>(packet));
break;
case OpTodControl:
rxArtTodControl(std::static_pointer_cast<ArtTodControl>(packet));
break;
case OpRdm:
rxArtRdm(std::static_pointer_cast<ArtRdm>(packet));
break;
case OpRdmSub:
rxArtRdmSub(std::static_pointer_cast<ArtRdmSub>(packet));
break;
case OpFirmwareMaster:
rxArtFirmwareMaster(std::static_pointer_cast<ArtFirmwareMaster>(packet));
break;
case OpFirmwareReply:
rxArtFirmwareReply(std::static_pointer_cast<ArtFirmwareReply>(packet));
break;
case OpIpProg:
rxArtIpProg(std::static_pointer_cast<ArtIpProg>(packet));
break;
case OpIpProgReply:
rxArtIpProgReply(std::static_pointer_cast<ArtIpProgReply>(packet));
break;
case OpTimeCode:
rxArtTimeCode(std::static_pointer_cast<ArtTimeCode>(packet));
break;
case OpTrigger:
rxArtTrigger(std::static_pointer_cast<ArtTrigger>(packet));
break;
default:
break;
}
}
/**
* @brief Device::setSender
* @param sender
* @return
*/
std::shared_ptr<void> Device::setSender(const std::function<void(std::shared_ptr<bufferstream>,
ipAddress)> sender)
{
// wrap the callback with a shared pointer
auto sp = std::make_shared<std::function<void(std::shared_ptr<bufferstream>,
ipAddress)>>(std::move(sender));
// store sender function (as a weak pointer)
_sender = sp;
// return token that caller must keep throughout it's scope
return sp;
}
/**
* @brief Device::send
* @param packet
* @param address
*/
void Device::send(const std::shared_ptr<Packet> packet, const ipAddress &address) const
{
auto buffer = std::vector<uint8_t>(packet->streamSize());
auto stream = std::make_shared<bufferstream>(buffer.data(), buffer.size());
packet->oStream(stream);
if (auto sp = _sender.lock()) // the owner is still holding the token
(*sp)(stream, address);
}
/**
* @brief Device::setStatus
* @param status
* @param text
*/
void Device::setReport(const NodeReport status, const std::string &text)
{
_report_code = status;
_report_text = text.substr(Node_Report_Length-8-1); // less required chars and null terminator
}
/**
* @brief Device::rxArtPoll
* @param packet
*/
void Device::rxArtPoll(std::shared_ptr<ArtPoll> packet)
{
auto data_opt = packet->data<poll_data>();
if (!data_opt.has_value())
return;
auto data = data_opt.value();
/// \bug The specification details resolution of diagostice behavior from multiple simultanious
/// controllers. However, the spec does not provide a mechanism for differentiating between
/// controllers. Until OSI layer 3 data is available, LTP the reporting settings.
diagnostic_reporting_behavior = data->talk_to_me;
diagnostic_reporting_threshold = data->diagnostic_level;
/// All device types send an ArtPollReply in response to receiving an ArtPoll.
txArtPollReply();
}
/**
* @brief Device::rxArtPollReply
* @param packet
*/
void Device::rxArtPollReply(std::shared_ptr<ArtPollReply> packet)
{
(void)packet;
}
/**
* @brief Device::rxArtDiagData
* @param packet
*/
void Device::rxArtDiagData(std::shared_ptr<ArtDiagData> packet)
{
(void)packet;
}
/**
* @brief Device::rxArtCommand
* @param packet
*/
void Device::rxArtCommand(std::shared_ptr<ArtCommand> packet)
{
(void)packet;
}
/**
* @brief Device::rxArtDmx
* @param packet
*/
void Device::rxArtDmx(std::shared_ptr<ArtDmx> packet)
{
(void)packet;
}
/**
* @brief Device::rxArtNzs
* @param packet
*/
void Device::rxArtNzs(std::shared_ptr<ArtNzs> packet)
{
if (packet->isVLC())
rxArtVlc(packet);
}
/**
* @brief Device::rxArtVlc
* @param packet
*/
void Device::rxArtVlc(std::shared_ptr<ArtNzs> packet)
{
auto data_opt = packet->data<nzs_data>();
if (!data_opt.has_value())
return;
auto data = std::make_shared<vlc_data>(data_opt.value().get());
}
/**
* @brief Device::rxArtSync
* @param packet
*/
void Device::rxArtSync(std::shared_ptr<ArtSync> packet)
{
(void)packet;
}
/**
* @brief Device::rxArtAddress
* @param packet
*/
void Device::rxArtAddress(std::shared_ptr<ArtAddress> packet)
{
(void)packet;
}
/**
* @brief Device::rxArtInput
* @param packet
*/
void Device::rxArtInput(std::shared_ptr<ArtInput> packet)
{
(void)packet;
}
/**
* @brief Device::rxArtTodRequest
* @param packet
*/
void Device::rxArtTodRequest(std::shared_ptr<ArtTodRequest> packet)
{
(void)packet;
}
/**
* @brief Device::rxArtTodData
* @param packet
*/
void Device::rxArtTodData(std::shared_ptr<ArtTodData> packet)
{
(void)packet;
}
/**
* @brief Device::rxArtTodControl
* @param packet
*/
void Device::rxArtTodControl(std::shared_ptr<ArtTodControl> packet)
{
(void)packet;
}
/**
* @brief Device::rxArtRdm
* @param packet
*/
void Device::rxArtRdm(std::shared_ptr<ArtRdm> packet)
{
(void)packet;
}
/**
* @brief Device::rxArtRdmSub
* @param packet
*/
void Device::rxArtRdmSub(std::shared_ptr<ArtRdmSub> packet)
{
(void)packet;
}
/**
* @brief Device::rxArtFirmwareMaster
* @param packet
*/
void Device::rxArtFirmwareMaster(std::shared_ptr<ArtFirmwareMaster> packet)
{
(void)packet;
}
/**
* @brief Device::rxArtFirmwareReply
* @param packet
*/
void Device::rxArtFirmwareReply(std::shared_ptr<ArtFirmwareReply> packet)
{
(void)packet;
}
/**
* @brief Device::rxArtIpProg
* @param packet
*/
void Device::rxArtIpProg(std::shared_ptr<ArtIpProg> packet)
{
(void)packet;
}
/**
* @brief Device::rxArtIpProgReply
* @param packet
*/
void Device::rxArtIpProgReply(std::shared_ptr<ArtIpProgReply> packet)
{
(void)packet;
}
/**
* @brief Device::rxArtTimeCode
* @param packet
*/
void Device::rxArtTimeCode(std::shared_ptr<ArtTimeCode> packet)
{
(void)packet;
}
/**
* @brief Device::rxArtTrigger
* @param packet
*/
void Device::rxArtTrigger(std::shared_ptr<ArtTrigger> packet)
{
(void)packet;
}
/**
* @brief Device::txArtPollReply
* @param reply
*/
void Device::txArtPollReply(std::shared_ptr<pollreply_data> reply)
{
if (!reply)
reply = std::make_shared<pollreply_data>();
/// \todo complete data field population of ArtPollReply
reply->my_ip = deviceIp().address.ipv4.value;
reply->fw_version = LIB_VERSION >> 16; // only able to send 16 bits, use the highest
// reply->net_sub_switch = ;
reply->status = device_status;
reply->short_name = _shortName;
reply->long_name = _longName;
reply->set_report(_report_code, _poll_reply_count, _report_text);
// reply->num_ports = ;
// reply->port_types = ;
// reply->good_input = ;
// reply->good_output = ;
// reply->SwIn = ;
// reply->SwOut = ;
// reply->SwMacro = ;
// reply->SwRemote = ;
reply->style = styleCode;
const auto mac = deviceMac();
std::copy(mac.cbegin(), mac.cend(), reply->mac_address);
// reply->bind_ip = reply->my_ip;
// reply->bind_index = ;
auto packet = std::make_shared<ArtPollReply>(reply);
send(packet, broadcastIp());
// reset report components
_report_code = RcPowerOk;
_poll_reply_count++;
_report_text = "nominal";
}
/**
* @brief Device::txArtDiagData
*/
void Device::txArtDiagData()
{
/// \todo impliment txArtDiagData
}
/**
* @brief Device::deviceMac
* @return
*
* The default implimentation is to return an invalid MAC
* \note Platform aware device types should return a valid MAC address.
*/
std::array<uint8_t, 6> Device::deviceMac() const
{
return std::array<uint8_t, 6>({0});
}
/**
* @brief Device::deviceIp
* @return
*
* The default implimentation is to return an invalid IP.
* \note Platform aware device types must return a valid IP address.
*/
ipAddress Device::deviceIp() const
{
ipAddress addr;
addr.type = 1; // ACN::SDT::SDT_ADDR_IPV4
addr.port = UDP_PORT;
addr.address.ipv4.value = 0;
return addr;
}
/**
* @brief Device::broadcastIp
* @return
*
* The default implimentation is to return the limited broadcast address.
* \note Platform aware device types must return their directed broadcast address.
*/
ipAddress Device::broadcastIp() const
{
ipAddress addr;
addr.type = 1; // ACN::SDT::SDT_ADDR_IPV4
addr.port = UDP_PORT;
addr.address.ipv4.value = 0xFFFFFFFF;
return addr;
}
/**
* @brief Device::deviceSubnetMask
* @return
*
* The default implimenation is to return an invalid mask.
* \note Platform aware device types must return their valid subnet mask.
*/
uint32_t Device::deviceSubnetMask() const
{
return -1;
}
/**
* @brief Device::deviceHasDHCP
* @return
*
* The default implimentation is to return false, as the default invalid IP is static.
* \note Platform aware device types must return their DHCP status.
*/
bool Device::deviceHasDHCP() const
{
return false;
}
/**
* @brief Device::shortName
* @return
*/
std::string Device::shortName() const
{
return _shortName;
}
/**
* @brief Device::setShortName
* @param newShortName
*/
void Device::setShortName(const std::string &newShortName)
{
_shortName = newShortName.substr(0, Short_Name_Length-1); // leave room for null terminator
_report_code = RcShNameOk;
if (newShortName.length() == _shortName.length())
_report_text = "short name accepted";
else
_report_text = "short name truncated";
}
/**
* @brief Device::longName
* @return
*/
std::string Device::longName() const
{
return _longName;
}
/**
* @brief Device::setLongName
* @param newLongName
*/
void Device::setLongName(const std::string &newLongName)
{
_longName = newLongName.substr(0, Long_Name_Length-1); // leave room for null terminator
_report_code = RcLoNameOk;
if (newLongName.length() == _longName.length())
_report_text = "long name accepted";
else
_report_text = "long name truncated";
}
} // namespace ARTNET