593 lines
17 KiB
C++
593 lines
17 KiB
C++
/*
|
||
responder.cpp
|
||
|
||
Copyright (c) 2021 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 "responder.h"
|
||
|
||
#include <algorithm>
|
||
#include <limits>
|
||
|
||
namespace RDM {
|
||
|
||
/**
|
||
* @brief Responder::Responder
|
||
* @param id
|
||
* @param parent
|
||
*/
|
||
Responder::Responder(UID id, Device* parent)
|
||
: Device(id, parent)
|
||
, control_field(0)
|
||
{
|
||
deviceModelID = 1;
|
||
deviceModelDescription = "Basic RDM Responder";
|
||
subdevice_flag = true;
|
||
|
||
Parameter *pid;
|
||
|
||
/// \cite RDM 7.5 Discovery Unique Branch Message (DISC_UNIQUE_BRANCH)
|
||
pid = addParameter(DISC_UNIQUE_BRANCH);
|
||
pid->discAction(std::bind(&Responder::actionDiscoverUniqueBranch, this,
|
||
std::placeholders::_1, std::placeholders::_2));
|
||
/// \cite RDM 7.6.3 Discovery Mute Message (DISC_MUTE)
|
||
/// A responder port shall set its Mute flag when it receives this message
|
||
/// containing its UID, or a broadcast address.
|
||
pid = addParameter(DISC_MUTE);
|
||
pid->discAction(std::bind(&Responder::actionDiscoveryMute, this,
|
||
std::placeholders::_1, std::placeholders::_2));
|
||
/// \cite RDM 7.6.4 Discovery Un-Mute Message (DISC_UN_MUTE)
|
||
/// A responder port shall clear its Mute flag when it receives this message
|
||
/// containing its UID, or a broadcast address.
|
||
pid = addParameter(DISC_UN_MUTE);
|
||
pid->discAction(std::bind(&Responder::actionDiscoveryUnmute, this,
|
||
std::placeholders::_1, std::placeholders::_2));
|
||
/// \cite RDM 10.2.1 Communication Status (COMMS_STATUS)
|
||
/// The COMMS_STATUS parameter is used to collect information that may be
|
||
/// useful in analyzing the integrity of the communication system.
|
||
pid = addParameter(COMMS_STATUS);
|
||
pid->getAction(std::bind(&Responder::actionGetCommsStatus, this,
|
||
std::placeholders::_1, std::placeholders::_2));
|
||
pid->setAction(std::bind(&Responder::actionSetCommsStatus, this,
|
||
std::placeholders::_1, std::placeholders::_2));
|
||
/// \cite RDM 10.3.1 Get Queued Message (QUEUED_MESSAGE)
|
||
/// The QUEUED_MESSAGE parameter shall be used to retrieve a message from the
|
||
/// responder’s message queue. The Message Count field of all response
|
||
/// messages defines the number of messages that are queued in the responder.
|
||
/// Each QUEUED_MESSAGE response shall be composed of a single message response.
|
||
pid = addParameter(QUEUED_MESSAGE);
|
||
pid->getAction(std::bind(&Responder::actionGetQueuedMessage, this,
|
||
std::placeholders::_1, std::placeholders::_2));
|
||
/// \cite RDM 10.3.2 Get Status Messages (STATUS_MESSAGES)
|
||
/// This parameter is used to collect Status or Error information
|
||
/// from a device.
|
||
pid = addParameter(STATUS_MESSAGES);
|
||
pid->getAction(std::bind(&Responder::actionGetStatusMessages, this,
|
||
std::placeholders::_1, std::placeholders::_2));
|
||
/// \cite RDM 10.3.3 Get Status ID Description (STATUS_ID_DESCRIPTION)
|
||
/// This parameter is used to request an ASCII text description of a given
|
||
/// Status ID. The description may be up to 32 characters.
|
||
pid = addParameter(STATUS_ID_DESCRIPTION);
|
||
pid->getAction(std::bind(&Responder::actionGetStatusIdDescription, this,
|
||
std::placeholders::_1, std::placeholders::_2));
|
||
}
|
||
|
||
|
||
Responder::~Responder()
|
||
{
|
||
}
|
||
|
||
|
||
/**
|
||
* @brief Responder::send
|
||
* @param data
|
||
*/
|
||
void Responder::send(const std::vector<uint8_t> &data)
|
||
{
|
||
if (auto sp = sender_.lock())
|
||
(*sp)(data); // sender still exists
|
||
}
|
||
|
||
|
||
/**
|
||
* @brief Responder::send
|
||
* @param response
|
||
*/
|
||
void Responder::send(const MsgPtr response)
|
||
{
|
||
if (!response)
|
||
return;
|
||
|
||
if (response->do_not_send)
|
||
return;
|
||
|
||
/// \cite RDM 6.3.4 Negative Acknowledge (RESPONSE_TYPE_NACK_REASON)
|
||
/// The response RESPONSE_TYPE_NACK_REASON shall only be used in conjunction with the
|
||
/// Command Classes GET_COMMAND_RESPONSE & SET_COMMAND_RESPONSE.
|
||
if (response->responseType == RESPONSE_TYPE_NACK_REASON
|
||
&& !(response->mdb.cc == GET_COMMAND_RESPONSE
|
||
|| response->mdb.cc == SET_COMMAND_RESPONSE))
|
||
return;
|
||
|
||
response->source = uid;
|
||
|
||
/// \cite RDM 6.2.8.2 Message Count field for Responder Generated Messages
|
||
/// If a responder has more than 255 messages queued, then the Message Count
|
||
/// field shall remain at 255 until the number of queued messages is reduced
|
||
/// below that number.
|
||
response->messageCount = std::min(queued_messages_.size(),
|
||
(size_t)std::numeric_limits<uint8_t>::max());
|
||
|
||
std::vector<uint8_t> data;
|
||
response->write(data);
|
||
send(data);
|
||
}
|
||
|
||
|
||
/**
|
||
* @brief Set the message sending callback function.
|
||
* @param cb Callback function.
|
||
* @return Token that the owner of the callback must keep for it's lifetime.
|
||
*/
|
||
std::shared_ptr<void> Responder::setSender(std::function<void(const std::vector<uint8_t>&)> cb)
|
||
{
|
||
// wrap the callback with a shared pointer
|
||
auto sp = std::make_shared<std::function<void(const std::vector<uint8_t>&)>>(std::move(cb));
|
||
// store callback (as a weak pointer)
|
||
sender_ = sp;
|
||
// return token that caller must keep throughout it's scope
|
||
return sp;
|
||
}
|
||
|
||
|
||
/**
|
||
* @brief Responder::receive
|
||
* @param data
|
||
*/
|
||
void Responder::receive(const std::vector<uint8_t> &data)
|
||
{
|
||
auto msg = std::make_shared<Message>();
|
||
msg->read(data);
|
||
receive(msg);
|
||
}
|
||
|
||
|
||
/**
|
||
* @brief Responder::receive
|
||
* @param message
|
||
*/
|
||
void Responder::receive(const MsgPtr message)
|
||
{
|
||
if (message->short_message)
|
||
{
|
||
short_message_counter_ = short_message_counter_ == std::numeric_limits<uint16_t>::max()
|
||
? short_message_counter_
|
||
: short_message_counter_++;
|
||
return; // ignore unreadable messages
|
||
}
|
||
|
||
// RDM::UID::operator== also returns true for broadcast messages
|
||
if (message->destination != uid // not for me
|
||
|| message->incorrect_sc // not RDM
|
||
|| message->incorrect_sub_sc // not a supported RDM format
|
||
)
|
||
return; // ignore message
|
||
|
||
if (message->length_mismatch)
|
||
length_mismatch_counter_ = length_mismatch_counter_ == std::numeric_limits<uint16_t>::max()
|
||
? length_mismatch_counter_
|
||
: length_mismatch_counter_++;
|
||
|
||
if (message->checksum_fail)
|
||
checksum_fail_counter_ = checksum_fail_counter_ == std::numeric_limits<uint16_t>::max()
|
||
? checksum_fail_counter_
|
||
: checksum_fail_counter_++;
|
||
|
||
if (message->do_not_send) // read error that prevents NAK
|
||
return;
|
||
|
||
uint8_t response_cc;
|
||
switch (message->mdb.cc) {
|
||
case DISCOVERY_COMMAND:
|
||
response_cc = DISCOVERY_COMMAND_RESPONSE;
|
||
break;
|
||
case GET_COMMAND:
|
||
response_cc = GET_COMMAND_RESPONSE;
|
||
break;
|
||
case SET_COMMAND:
|
||
response_cc = SET_COMMAND_RESPONSE;
|
||
break;
|
||
default:
|
||
return; // responder can ignore _RESPONSE class messages
|
||
}
|
||
|
||
auto response = std::make_shared<Message>();
|
||
response->destination = message->source; // send response to sender of message
|
||
response->subDevice = message->subDevice; // copy sub-device
|
||
response->mdb.pid = message->mdb.pid; // copy PID
|
||
response->tn = message->tn; // this reply is a response to the received message
|
||
response->mdb.cc = response_cc; // appropriate command class
|
||
|
||
/// \cite RDM 5.3 Broadcast Message Addressing
|
||
/// When Broadcast Addressing is used for non-Discovery messages, the
|
||
/// responders shall not send a response.
|
||
if (message->destination.isBroadcast() &&
|
||
response->mdb.cc != DISCOVERY_COMMAND_RESPONSE)
|
||
response->do_not_send = true;
|
||
|
||
if (message->failure_mode)
|
||
response->nak(NR_FORMAT_ERROR); // nak on any failure modes
|
||
else
|
||
{
|
||
switch (message->mdb.cc) { // dispatch valid messages
|
||
case DISCOVERY_COMMAND:
|
||
rxDiscovery(message, response);
|
||
break;
|
||
case GET_COMMAND:
|
||
rxGet(message, response);
|
||
break;
|
||
case SET_COMMAND:
|
||
rxSet(message, response);
|
||
break;
|
||
default:
|
||
response->nak(NR_UNSUPPORTED_COMMAND_CLASS);
|
||
}
|
||
}
|
||
|
||
send(response); // send the reply
|
||
}
|
||
|
||
|
||
/**
|
||
* @brief Responder::reset
|
||
* @param hard
|
||
*/
|
||
void Responder::reset(bool hard)
|
||
{
|
||
(void)hard;
|
||
|
||
/// \cite RDM 10.11.2 Reset Device (RESET_DEVICE)
|
||
/// This parameter shall also clear the Discovery Mute flag.
|
||
discovery_mute_flag_ = false;
|
||
}
|
||
|
||
|
||
/**
|
||
* @brief Responder::rxDiscovery
|
||
* @param message
|
||
* @param response
|
||
*/
|
||
void Responder::rxDiscovery(const MsgPtr message, MsgPtr response)
|
||
{
|
||
if (message->subDevice != 0)
|
||
{
|
||
response->nak(NR_SUB_DEVICE_OUT_OF_RANGE);
|
||
return;
|
||
}
|
||
|
||
if (!actionPrep_(message, response))
|
||
return;
|
||
|
||
parameters_.at(message->mdb.pid)->disc(message, response);
|
||
}
|
||
|
||
|
||
/**
|
||
* @brief Responder::rxGet
|
||
* @param message
|
||
* @param response
|
||
*/
|
||
void Responder::rxGet(const MsgPtr message, MsgPtr response)
|
||
{
|
||
/// \cite RDM 9.2.2 Using Sub-Devices
|
||
/// Broadcast GET commands sent to the SUB_DEVICE_ALL_CALL Sub-Device ID are
|
||
/// not allowed. Any responder receiving a GET command sent to this Sub-Device
|
||
/// ID shall respond with a NACK with a NACK Reason Code of
|
||
/// NR_SUB_DEVICE_OUT_OF_RANGE.
|
||
if (message->subDevice == SUB_DEVICE_ALL_CALL)
|
||
{
|
||
response->nak(NR_SUB_DEVICE_OUT_OF_RANGE);
|
||
return;
|
||
}
|
||
|
||
if (message->subDevice == 0)
|
||
{
|
||
get(message, response);
|
||
return;
|
||
}
|
||
|
||
if (!sub_devices_.count(message->subDevice))
|
||
{
|
||
response->nak(NR_SUB_DEVICE_OUT_OF_RANGE);
|
||
return;
|
||
}
|
||
|
||
sub_devices_.at(message->subDevice)->get(message, response);
|
||
}
|
||
|
||
|
||
/**
|
||
* @brief Responder::rxSet
|
||
* @param message
|
||
* @param response
|
||
*/
|
||
void Responder::rxSet(const MsgPtr message, MsgPtr response)
|
||
{
|
||
if (message->subDevice == 0)
|
||
{
|
||
set(message, response);
|
||
return;
|
||
}
|
||
|
||
if (sub_devices_.empty())
|
||
{
|
||
response->nak(NR_SUB_DEVICE_OUT_OF_RANGE);
|
||
return;
|
||
}
|
||
|
||
if (message->subDevice == SUB_DEVICE_ALL_CALL)
|
||
{
|
||
for (auto& [num, dev] : sub_devices_)
|
||
dev->set(message, response);
|
||
return;
|
||
}
|
||
|
||
if (!sub_devices_.count(message->subDevice))
|
||
{
|
||
response->nak(NR_SUB_DEVICE_OUT_OF_RANGE);
|
||
return;
|
||
}
|
||
|
||
sub_devices_.at(message->subDevice)->set(message, response);
|
||
}
|
||
|
||
|
||
/**
|
||
* @brief Responder::actionDiscoverUniqueBranch
|
||
* @param message
|
||
* @param response
|
||
*/
|
||
void Responder::actionDiscoverUniqueBranch(const MsgPtr message, MsgPtr response)
|
||
{
|
||
if (message->mdb.pdl() != 12)
|
||
return response->nak(NR_FORMAT_ERROR);
|
||
|
||
if (discovery_mute_flag_)
|
||
{
|
||
response->do_not_send = true;
|
||
return;
|
||
}
|
||
|
||
UID lower, upper;
|
||
lower.manufacturer = Message::readType<uint16_t>(message->mdb.pd, 0);
|
||
lower.device = Message::readType<uint32_t>(message->mdb.pd, 2);
|
||
upper.manufacturer = Message::readType<uint16_t>(message->mdb.pd, 6);
|
||
upper.device = Message::readType<uint32_t>(message->mdb.pd, 8);
|
||
|
||
if (uid.uid() < lower.uid())
|
||
{
|
||
response->do_not_send = true;
|
||
return;
|
||
}
|
||
if (uid.uid() > upper.uid())
|
||
{
|
||
response->do_not_send = true;
|
||
return;
|
||
}
|
||
|
||
response->appendParameterData(uid.manufacturer);
|
||
response->appendParameterData(uid.device);
|
||
}
|
||
|
||
|
||
/**
|
||
* @brief Responder::actionDiscoveryMute
|
||
* @param message
|
||
* @param response
|
||
*/
|
||
void Responder::actionDiscoveryMute(const MsgPtr message, MsgPtr response)
|
||
{
|
||
if (message->mdb.pdl() != 0)
|
||
return response->nak(NR_FORMAT_ERROR);
|
||
|
||
discovery_mute_flag_ = true;
|
||
controller_uid_ = message->source;
|
||
response->appendParameterData(control_field);
|
||
|
||
response->responseType = RESPONSE_TYPE_ACK;
|
||
}
|
||
|
||
|
||
/**
|
||
* @brief Responder::actionDiscoveryUnmute
|
||
* @param message
|
||
* @param response
|
||
*/
|
||
void Responder::actionDiscoveryUnmute(const MsgPtr message, MsgPtr response)
|
||
{
|
||
if (message->mdb.pdl() != 0)
|
||
return response->nak(NR_FORMAT_ERROR);
|
||
|
||
discovery_mute_flag_ = false;
|
||
response->appendParameterData(control_field);
|
||
|
||
response->responseType = RESPONSE_TYPE_ACK;
|
||
}
|
||
|
||
|
||
/**
|
||
* @brief Responder::actionGetCommsStatus
|
||
* @param message
|
||
* @param response
|
||
*/
|
||
void Responder::actionGetCommsStatus(const MsgPtr message, MsgPtr response)
|
||
{
|
||
if (message->mdb.pdl() != 0)
|
||
return response->nak(NR_FORMAT_ERROR);
|
||
|
||
response->appendParameterData(short_message_counter_);
|
||
response->appendParameterData(length_mismatch_counter_);
|
||
response->appendParameterData(checksum_fail_counter_);
|
||
|
||
response->responseType = RESPONSE_TYPE_ACK;
|
||
}
|
||
|
||
|
||
/**
|
||
* @brief Responder::actionSetCommsStatus
|
||
* @param message
|
||
* @param response
|
||
*/
|
||
void Responder::actionSetCommsStatus(const MsgPtr message, MsgPtr response)
|
||
{
|
||
if (message->mdb.pdl() != 0)
|
||
return response->nak(NR_FORMAT_ERROR);
|
||
|
||
short_message_counter_ = 0;
|
||
length_mismatch_counter_ = 0;
|
||
checksum_fail_counter_ = 0;
|
||
|
||
response->responseType = RESPONSE_TYPE_ACK;
|
||
}
|
||
|
||
|
||
/**
|
||
* @brief Responder::actionGetQueuedMessage
|
||
* @param message
|
||
* @param response
|
||
*/
|
||
void Responder::actionGetQueuedMessage(const MsgPtr message, MsgPtr response)
|
||
{
|
||
if (message->mdb.pdl() != 1)
|
||
return response->nak(NR_FORMAT_ERROR);
|
||
|
||
if (queued_messages_.empty())
|
||
{
|
||
response->mdb.pid = STATUS_MESSAGES;
|
||
actionGetStatusMessages(message, response);
|
||
return;
|
||
}
|
||
|
||
response->do_not_send = true;
|
||
auto msg = queued_messages_.front();
|
||
queued_messages_.pop_front();
|
||
last_status_message_ = msg;
|
||
send(msg);
|
||
}
|
||
|
||
|
||
/**
|
||
* @brief Responder::actionGetStatusMessages
|
||
* @param message
|
||
* @param response
|
||
*/
|
||
void Responder::actionGetStatusMessages(const MsgPtr message, MsgPtr response)
|
||
{
|
||
if (message->mdb.pdl() != 1)
|
||
return response->nak(NR_FORMAT_ERROR);
|
||
|
||
uint8_t type = message->mdb.pd.front();
|
||
|
||
if (type != STATUS_GET_LAST_MESSAGE &&
|
||
type != STATUS_ERROR &&
|
||
type != STATUS_WARNING &&
|
||
type != STATUS_ADVISORY)
|
||
{
|
||
response->nak(NR_DATA_OUT_OF_RANGE);
|
||
return;
|
||
}
|
||
|
||
if (type == STATUS_GET_LAST_MESSAGE)
|
||
{
|
||
response->do_not_send = true;
|
||
send(last_status_message_);
|
||
return;
|
||
}
|
||
|
||
int counter = 0;
|
||
auto reportStatusQueue = [response, counter] (std::queue<StatusPtr> q) mutable
|
||
{
|
||
while(!q.empty() && counter < 25)
|
||
{
|
||
for (uint8_t& b : q.front()->bytes)
|
||
response->appendParameterData(b);
|
||
counter++;
|
||
q.pop();
|
||
}
|
||
};
|
||
|
||
if (type == STATUS_ERROR ||
|
||
type == STATUS_WARNING ||
|
||
type == STATUS_ADVISORY)
|
||
{
|
||
reportStatusQueue(queued_statuses_.at(STATUS_ERROR));
|
||
for (auto& [_, dev] : sub_devices_)
|
||
reportStatusQueue(dev->queued_statuses_.at(STATUS_ERROR));
|
||
}
|
||
|
||
if (type == STATUS_WARNING ||
|
||
type == STATUS_ADVISORY)
|
||
{
|
||
reportStatusQueue(queued_statuses_.at(STATUS_WARNING));
|
||
for (auto& [_, dev] : sub_devices_)
|
||
reportStatusQueue(dev->queued_statuses_.at(STATUS_WARNING));
|
||
}
|
||
|
||
if (type == STATUS_ADVISORY)
|
||
{
|
||
reportStatusQueue(queued_statuses_.at(STATUS_ADVISORY));
|
||
for (auto& [_, dev] : sub_devices_)
|
||
reportStatusQueue(dev->queued_statuses_.at(STATUS_ADVISORY));
|
||
}
|
||
|
||
if (counter == 25)
|
||
response->responseType = RESPONSE_TYPE_ACK_OVERFLOW;
|
||
else
|
||
response->responseType = RESPONSE_TYPE_ACK;
|
||
|
||
last_status_message_ = response;
|
||
}
|
||
|
||
|
||
/**
|
||
* @brief Responder::actionGetStatusIdDescription
|
||
* @param message
|
||
* @param response
|
||
*/
|
||
void Responder::actionGetStatusIdDescription(const MsgPtr message, MsgPtr response)
|
||
{
|
||
if (message->mdb.pdl() != 2)
|
||
return response->nak(NR_FORMAT_ERROR);
|
||
|
||
uint16_t status = message->readType<uint16_t>(message->mdb.pd, 0);
|
||
|
||
std::string label = RDM::StatusMessageDescription(status);
|
||
for (size_t i = 0; i < label.size(); i++)
|
||
{
|
||
if (i > 32)
|
||
break;
|
||
response->appendParameterData(label.at(i));
|
||
}
|
||
|
||
response->responseType = RESPONSE_TYPE_ACK;
|
||
}
|
||
|
||
} // namespace RDM
|