2021-08-08 17:25:41 -04:00
|
|
|
|
/*
|
|
|
|
|
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"
|
|
|
|
|
|
2021-08-11 09:42:43 -04:00
|
|
|
|
#include <algorithm>
|
2021-08-10 15:35:27 -04:00
|
|
|
|
#include <limits>
|
|
|
|
|
|
2021-08-08 17:25:41 -04:00
|
|
|
|
namespace RDM {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Responder::Responder
|
2021-08-25 17:20:33 -04:00
|
|
|
|
* @param id
|
|
|
|
|
* @param parent
|
2021-08-08 17:25:41 -04:00
|
|
|
|
*/
|
2021-08-13 10:04:30 -04:00
|
|
|
|
Responder::Responder(UID id, Device* parent)
|
|
|
|
|
: Device(id, parent)
|
2021-08-11 01:26:51 -04:00
|
|
|
|
, control_field(0)
|
2021-08-08 17:25:41 -04:00
|
|
|
|
{
|
2021-08-09 15:04:49 -04:00
|
|
|
|
deviceModelID = 1;
|
|
|
|
|
deviceModelDescription = "Basic RDM Responder";
|
2021-08-11 01:26:51 -04:00
|
|
|
|
subdevice_flag = true;
|
|
|
|
|
|
2023-04-28 20:46:13 -04:00
|
|
|
|
Parameter *pid;
|
|
|
|
|
|
2021-08-26 12:30:44 -04:00
|
|
|
|
/// \cite RDM 7.5 Discovery Unique Branch Message (DISC_UNIQUE_BRANCH)
|
2023-04-28 20:46:13 -04:00
|
|
|
|
pid = addParameter(DISC_UNIQUE_BRANCH);
|
|
|
|
|
pid->discAction(std::bind(&Responder::actionDiscoverUniqueBranch, this,
|
|
|
|
|
std::placeholders::_1, std::placeholders::_2));
|
2021-08-26 12:30:44 -04:00
|
|
|
|
/// \cite RDM 7.6.3 Discovery Mute Message (DISC_MUTE)
|
2021-08-11 01:26:51 -04:00
|
|
|
|
/// A responder port shall set its Mute flag when it receives this message
|
|
|
|
|
/// containing its UID, or a broadcast address.
|
2023-04-28 20:46:13 -04:00
|
|
|
|
pid = addParameter(DISC_MUTE);
|
|
|
|
|
pid->discAction(std::bind(&Responder::actionDiscoveryMute, this,
|
|
|
|
|
std::placeholders::_1, std::placeholders::_2));
|
2021-08-26 12:30:44 -04:00
|
|
|
|
/// \cite RDM 7.6.4 Discovery Un-Mute Message (DISC_UN_MUTE)
|
2021-08-11 01:26:51 -04:00
|
|
|
|
/// A responder port shall clear its Mute flag when it receives this message
|
|
|
|
|
/// containing its UID, or a broadcast address.
|
2023-04-28 20:46:13 -04:00
|
|
|
|
pid = addParameter(DISC_UN_MUTE);
|
|
|
|
|
pid->discAction(std::bind(&Responder::actionDiscoveryUnmute, this,
|
|
|
|
|
std::placeholders::_1, std::placeholders::_2));
|
2021-08-26 12:30:44 -04:00
|
|
|
|
/// \cite RDM 10.2.1 Communication Status (COMMS_STATUS)
|
2021-08-11 09:42:55 -04:00
|
|
|
|
/// The COMMS_STATUS parameter is used to collect information that may be
|
|
|
|
|
/// useful in analyzing the integrity of the communication system.
|
2023-04-28 20:46:13 -04:00
|
|
|
|
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));
|
2021-08-26 12:30:44 -04:00
|
|
|
|
/// \cite RDM 10.3.1 Get Queued Message (QUEUED_MESSAGE)
|
2021-08-12 00:31:00 -04:00
|
|
|
|
/// 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.
|
2023-04-28 20:46:13 -04:00
|
|
|
|
pid = addParameter(QUEUED_MESSAGE);
|
|
|
|
|
pid->getAction(std::bind(&Responder::actionGetQueuedMessage, this,
|
|
|
|
|
std::placeholders::_1, std::placeholders::_2));
|
2021-08-26 12:30:44 -04:00
|
|
|
|
/// \cite RDM 10.3.2 Get Status Messages (STATUS_MESSAGES)
|
2021-08-12 00:31:00 -04:00
|
|
|
|
/// This parameter is used to collect Status or Error information
|
|
|
|
|
/// from a device.
|
2023-04-28 20:46:13 -04:00
|
|
|
|
pid = addParameter(STATUS_MESSAGES);
|
|
|
|
|
pid->getAction(std::bind(&Responder::actionGetStatusMessages, this,
|
|
|
|
|
std::placeholders::_1, std::placeholders::_2));
|
2021-08-26 12:30:44 -04:00
|
|
|
|
/// \cite RDM 10.3.3 Get Status ID Description (STATUS_ID_DESCRIPTION)
|
2021-08-12 00:31:00 -04:00
|
|
|
|
/// This parameter is used to request an ASCII text description of a given
|
|
|
|
|
/// Status ID. The description may be up to 32 characters.
|
2023-04-28 20:46:13 -04:00
|
|
|
|
pid = addParameter(STATUS_ID_DESCRIPTION);
|
|
|
|
|
pid->getAction(std::bind(&Responder::actionGetStatusIdDescription, this,
|
|
|
|
|
std::placeholders::_1, std::placeholders::_2));
|
2021-08-08 17:25:41 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Responder::~Responder()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Responder::send
|
|
|
|
|
* @param data
|
|
|
|
|
*/
|
2021-08-25 17:20:33 -04:00
|
|
|
|
void Responder::send(const std::vector<uint8_t> &data)
|
2021-08-08 17:25:41 -04:00
|
|
|
|
{
|
2023-04-26 10:02:20 -04:00
|
|
|
|
if (auto sp = sender_.lock())
|
|
|
|
|
(*sp)(data); // sender still exists
|
2021-08-08 17:25:41 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Responder::send
|
2021-08-25 17:20:33 -04:00
|
|
|
|
* @param response
|
2021-08-08 17:25:41 -04:00
|
|
|
|
*/
|
2023-04-28 20:46:13 -04:00
|
|
|
|
void Responder::send(const MsgPtr response)
|
2021-08-08 17:25:41 -04:00
|
|
|
|
{
|
2021-08-10 13:33:24 -04:00
|
|
|
|
if (!response)
|
|
|
|
|
return;
|
2021-08-10 00:57:09 -04:00
|
|
|
|
|
2021-08-25 17:20:33 -04:00
|
|
|
|
if (response->do_not_send)
|
2023-04-28 20:46:13 -04:00
|
|
|
|
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;
|
2021-08-11 09:42:43 -04:00
|
|
|
|
|
2023-04-28 21:02:35 -04:00
|
|
|
|
response->source = uid;
|
|
|
|
|
|
2021-08-26 12:30:44 -04:00
|
|
|
|
/// \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.
|
2021-08-11 09:42:43 -04:00
|
|
|
|
response->messageCount = std::min(queued_messages_.size(),
|
|
|
|
|
(size_t)std::numeric_limits<uint8_t>::max());
|
2023-04-28 20:46:13 -04:00
|
|
|
|
|
2021-08-08 17:25:41 -04:00
|
|
|
|
std::vector<uint8_t> data;
|
2021-08-10 13:33:24 -04:00
|
|
|
|
response->write(data);
|
2021-08-08 17:25:41 -04:00
|
|
|
|
send(data);
|
2021-08-10 13:33:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-04-26 10:02:20 -04:00
|
|
|
|
/**
|
|
|
|
|
* @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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-08-10 13:33:24 -04:00
|
|
|
|
/**
|
|
|
|
|
* @brief Responder::receive
|
|
|
|
|
* @param data
|
|
|
|
|
*/
|
|
|
|
|
void Responder::receive(const std::vector<uint8_t> &data)
|
|
|
|
|
{
|
2023-04-28 20:46:13 -04:00
|
|
|
|
auto msg = std::make_shared<Message>();
|
|
|
|
|
msg->read(data);
|
|
|
|
|
receive(msg);
|
2021-08-08 17:25:41 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Responder::receive
|
|
|
|
|
* @param message
|
|
|
|
|
*/
|
2021-08-11 10:28:03 -04:00
|
|
|
|
void Responder::receive(const MsgPtr message)
|
2021-08-08 17:25:41 -04:00
|
|
|
|
{
|
2021-08-10 15:35:27 -04:00
|
|
|
|
if (message->short_message)
|
|
|
|
|
{
|
2023-04-28 20:46:13 -04:00
|
|
|
|
short_message_counter_ = short_message_counter_ == std::numeric_limits<uint16_t>::max()
|
|
|
|
|
? short_message_counter_
|
|
|
|
|
: short_message_counter_++;
|
|
|
|
|
return; // ignore unreadable messages
|
2021-08-10 15:35:27 -04:00
|
|
|
|
}
|
2021-08-08 17:25:41 -04:00
|
|
|
|
|
2021-08-10 13:33:24 -04:00
|
|
|
|
// RDM::UID::operator== also returns true for broadcast messages
|
2023-04-28 21:02:35 -04:00
|
|
|
|
if (message->destination != uid // not for me
|
2023-04-28 20:46:13 -04:00
|
|
|
|
|| message->incorrect_sc // not RDM
|
|
|
|
|
|| message->incorrect_sub_sc // not a supported RDM format
|
|
|
|
|
)
|
|
|
|
|
return; // ignore message
|
2021-08-10 15:35:27 -04:00
|
|
|
|
|
|
|
|
|
if (message->length_mismatch)
|
2023-04-28 20:46:13 -04:00
|
|
|
|
length_mismatch_counter_ = length_mismatch_counter_ == std::numeric_limits<uint16_t>::max()
|
|
|
|
|
? length_mismatch_counter_
|
|
|
|
|
: length_mismatch_counter_++;
|
2021-08-10 15:35:27 -04:00
|
|
|
|
|
|
|
|
|
if (message->checksum_fail)
|
2023-04-28 20:46:13 -04:00
|
|
|
|
checksum_fail_counter_ = checksum_fail_counter_ == std::numeric_limits<uint16_t>::max()
|
|
|
|
|
? checksum_fail_counter_
|
|
|
|
|
: checksum_fail_counter_++;
|
2021-08-10 15:35:27 -04:00
|
|
|
|
|
2023-04-28 20:46:13 -04:00
|
|
|
|
if (message->do_not_send) // read error that prevents NAK
|
2021-08-10 15:35:27 -04:00
|
|
|
|
return;
|
|
|
|
|
|
2023-04-28 20:46:13 -04:00
|
|
|
|
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
|
|
|
|
|
}
|
2021-08-10 13:02:58 -04:00
|
|
|
|
|
2023-04-28 20:46:13 -04:00
|
|
|
|
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
|
2021-08-08 17:25:41 -04:00
|
|
|
|
|
2021-08-26 12:30:44 -04:00
|
|
|
|
/// \cite RDM 5.3 Broadcast Message Addressing
|
2021-08-12 00:31:00 -04:00
|
|
|
|
/// When Broadcast Addressing is used for non-Discovery messages, the
|
|
|
|
|
/// responders shall not send a response.
|
|
|
|
|
if (message->destination.isBroadcast() &&
|
2023-04-28 20:46:13 -04:00
|
|
|
|
response->mdb.cc != DISCOVERY_COMMAND_RESPONSE)
|
2021-08-25 17:20:33 -04:00
|
|
|
|
response->do_not_send = true;
|
2021-08-12 00:31:00 -04:00
|
|
|
|
|
2023-04-28 20:46:13 -04:00
|
|
|
|
if (message->failure_mode)
|
|
|
|
|
response->nak(NR_FORMAT_ERROR); // nak on any failure modes
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
switch (message->mdb.cc) { // dispatch valid messages
|
2021-08-08 17:25:41 -04:00
|
|
|
|
case DISCOVERY_COMMAND:
|
|
|
|
|
rxDiscovery(message, response);
|
2021-08-10 13:02:58 -04:00
|
|
|
|
break;
|
2021-08-08 17:25:41 -04:00
|
|
|
|
case GET_COMMAND:
|
|
|
|
|
rxGet(message, response);
|
2021-08-10 13:02:58 -04:00
|
|
|
|
break;
|
2021-08-08 17:25:41 -04:00
|
|
|
|
case SET_COMMAND:
|
|
|
|
|
rxSet(message, response);
|
2021-08-10 13:02:58 -04:00
|
|
|
|
break;
|
2021-08-08 17:25:41 -04:00
|
|
|
|
default:
|
2023-04-28 20:46:13 -04:00
|
|
|
|
response->nak(NR_UNSUPPORTED_COMMAND_CLASS);
|
|
|
|
|
}
|
2021-08-08 17:25:41 -04:00
|
|
|
|
}
|
2021-08-10 15:35:27 -04:00
|
|
|
|
|
2023-04-28 20:46:13 -04:00
|
|
|
|
send(response); // send the reply
|
2021-08-11 00:04:08 -04:00
|
|
|
|
}
|
2021-08-11 00:02:41 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Responder::reset
|
|
|
|
|
* @param hard
|
|
|
|
|
*/
|
|
|
|
|
void Responder::reset(bool hard)
|
|
|
|
|
{
|
|
|
|
|
(void)hard;
|
|
|
|
|
|
2021-08-26 12:30:44 -04:00
|
|
|
|
/// \cite RDM 10.11.2 Reset Device (RESET_DEVICE)
|
2021-08-11 00:02:41 -04:00
|
|
|
|
/// This parameter shall also clear the Discovery Mute flag.
|
|
|
|
|
discovery_mute_flag_ = false;
|
2021-08-08 17:25:41 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Responder::rxDiscovery
|
|
|
|
|
* @param message
|
2021-08-25 17:20:33 -04:00
|
|
|
|
* @param response
|
2021-08-08 17:25:41 -04:00
|
|
|
|
*/
|
2021-08-11 10:28:03 -04:00
|
|
|
|
void Responder::rxDiscovery(const MsgPtr message, MsgPtr response)
|
2021-08-08 17:25:41 -04:00
|
|
|
|
{
|
2021-08-11 01:26:51 -04:00
|
|
|
|
if (message->subDevice != 0)
|
|
|
|
|
{
|
|
|
|
|
response->nak(NR_SUB_DEVICE_OUT_OF_RANGE);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!actionPrep_(message, response))
|
|
|
|
|
return;
|
|
|
|
|
|
2023-04-28 20:46:13 -04:00
|
|
|
|
parameters_.at(message->mdb.pid)->disc(message, response);
|
2021-08-08 17:25:41 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Responder::rxGet
|
|
|
|
|
* @param message
|
2021-08-25 17:20:33 -04:00
|
|
|
|
* @param response
|
2021-08-08 17:25:41 -04:00
|
|
|
|
*/
|
2021-08-11 10:28:03 -04:00
|
|
|
|
void Responder::rxGet(const MsgPtr message, MsgPtr response)
|
2021-08-08 17:25:41 -04:00
|
|
|
|
{
|
2021-08-26 12:30:44 -04:00
|
|
|
|
/// \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.
|
2021-08-08 17:25:41 -04:00
|
|
|
|
if (message->subDevice == SUB_DEVICE_ALL_CALL)
|
|
|
|
|
{
|
2021-08-10 16:08:09 -04:00
|
|
|
|
response->nak(NR_SUB_DEVICE_OUT_OF_RANGE);
|
2021-08-08 17:25:41 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (message->subDevice == 0)
|
|
|
|
|
{
|
|
|
|
|
get(message, response);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!sub_devices_.count(message->subDevice))
|
|
|
|
|
{
|
2021-08-10 16:08:09 -04:00
|
|
|
|
response->nak(NR_SUB_DEVICE_OUT_OF_RANGE);
|
2021-08-08 17:25:41 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub_devices_.at(message->subDevice)->get(message, response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Responder::rxSet
|
|
|
|
|
* @param message
|
2021-08-25 17:20:33 -04:00
|
|
|
|
* @param response
|
2021-08-08 17:25:41 -04:00
|
|
|
|
*/
|
2021-08-11 10:28:03 -04:00
|
|
|
|
void Responder::rxSet(const MsgPtr message, MsgPtr response)
|
2021-08-08 17:25:41 -04:00
|
|
|
|
{
|
|
|
|
|
if (message->subDevice == 0)
|
|
|
|
|
{
|
|
|
|
|
set(message, response);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 00:31:00 -04:00
|
|
|
|
if (sub_devices_.empty())
|
|
|
|
|
{
|
|
|
|
|
response->nak(NR_SUB_DEVICE_OUT_OF_RANGE);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-08 17:25:41 -04:00
|
|
|
|
if (message->subDevice == SUB_DEVICE_ALL_CALL)
|
|
|
|
|
{
|
|
|
|
|
for (auto& [num, dev] : sub_devices_)
|
2021-08-12 00:31:00 -04:00
|
|
|
|
dev->set(message, response);
|
2021-08-08 17:25:41 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!sub_devices_.count(message->subDevice))
|
|
|
|
|
{
|
2021-08-10 16:08:09 -04:00
|
|
|
|
response->nak(NR_SUB_DEVICE_OUT_OF_RANGE);
|
2021-08-08 17:25:41 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub_devices_.at(message->subDevice)->set(message, response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-08-11 01:26:51 -04:00
|
|
|
|
/**
|
|
|
|
|
* @brief Responder::actionDiscoverUniqueBranch
|
|
|
|
|
* @param message
|
|
|
|
|
* @param response
|
|
|
|
|
*/
|
2021-08-11 10:28:03 -04:00
|
|
|
|
void Responder::actionDiscoverUniqueBranch(const MsgPtr message, MsgPtr response)
|
2021-08-11 01:26:51 -04:00
|
|
|
|
{
|
2023-04-28 20:46:13 -04:00
|
|
|
|
if (message->mdb.pdl() != 12)
|
|
|
|
|
return response->nak(NR_FORMAT_ERROR);
|
2021-08-11 01:26:51 -04:00
|
|
|
|
|
|
|
|
|
if (discovery_mute_flag_)
|
|
|
|
|
{
|
2021-08-25 17:20:33 -04:00
|
|
|
|
response->do_not_send = true;
|
2021-08-11 01:26:51 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UID lower, upper;
|
2023-04-28 20:46:13 -04:00
|
|
|
|
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);
|
2021-08-11 01:26:51 -04:00
|
|
|
|
|
2023-04-28 21:02:35 -04:00
|
|
|
|
if (uid.uid() < lower.uid())
|
2021-08-11 01:26:51 -04:00
|
|
|
|
{
|
2021-08-25 17:20:33 -04:00
|
|
|
|
response->do_not_send = true;
|
2021-08-11 01:26:51 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
2023-04-28 21:02:35 -04:00
|
|
|
|
if (uid.uid() > upper.uid())
|
2021-08-11 01:26:51 -04:00
|
|
|
|
{
|
2021-08-25 17:20:33 -04:00
|
|
|
|
response->do_not_send = true;
|
2021-08-11 01:26:51 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-28 21:02:35 -04:00
|
|
|
|
response->appendParameterData(uid.manufacturer);
|
|
|
|
|
response->appendParameterData(uid.device);
|
2021-08-11 01:26:51 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Responder::actionDiscoveryMute
|
|
|
|
|
* @param message
|
|
|
|
|
* @param response
|
|
|
|
|
*/
|
2021-08-11 10:28:03 -04:00
|
|
|
|
void Responder::actionDiscoveryMute(const MsgPtr message, MsgPtr response)
|
2021-08-11 01:26:51 -04:00
|
|
|
|
{
|
2023-04-28 20:46:13 -04:00
|
|
|
|
if (message->mdb.pdl() != 0)
|
|
|
|
|
return response->nak(NR_FORMAT_ERROR);
|
2021-08-11 01:26:51 -04:00
|
|
|
|
|
|
|
|
|
discovery_mute_flag_ = true;
|
2021-08-12 00:31:00 -04:00
|
|
|
|
controller_uid_ = message->source;
|
2023-04-28 20:46:13 -04:00
|
|
|
|
response->appendParameterData(control_field);
|
2021-08-11 01:26:51 -04:00
|
|
|
|
|
|
|
|
|
response->responseType = RESPONSE_TYPE_ACK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Responder::actionDiscoveryUnmute
|
|
|
|
|
* @param message
|
|
|
|
|
* @param response
|
|
|
|
|
*/
|
2021-08-11 10:28:03 -04:00
|
|
|
|
void Responder::actionDiscoveryUnmute(const MsgPtr message, MsgPtr response)
|
2021-08-11 01:26:51 -04:00
|
|
|
|
{
|
2023-04-28 20:46:13 -04:00
|
|
|
|
if (message->mdb.pdl() != 0)
|
|
|
|
|
return response->nak(NR_FORMAT_ERROR);
|
2021-08-11 01:26:51 -04:00
|
|
|
|
|
|
|
|
|
discovery_mute_flag_ = false;
|
2023-04-28 20:46:13 -04:00
|
|
|
|
response->appendParameterData(control_field);
|
2021-08-11 01:26:51 -04:00
|
|
|
|
|
|
|
|
|
response->responseType = RESPONSE_TYPE_ACK;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-11 09:42:55 -04:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Responder::actionGetCommsStatus
|
|
|
|
|
* @param message
|
|
|
|
|
* @param response
|
|
|
|
|
*/
|
2021-08-11 10:28:03 -04:00
|
|
|
|
void Responder::actionGetCommsStatus(const MsgPtr message, MsgPtr response)
|
2021-08-11 09:42:55 -04:00
|
|
|
|
{
|
2023-04-28 20:46:13 -04:00
|
|
|
|
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_);
|
2021-08-11 09:42:55 -04:00
|
|
|
|
|
|
|
|
|
response->responseType = RESPONSE_TYPE_ACK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Responder::actionSetCommsStatus
|
|
|
|
|
* @param message
|
|
|
|
|
* @param response
|
|
|
|
|
*/
|
2021-08-11 10:28:03 -04:00
|
|
|
|
void Responder::actionSetCommsStatus(const MsgPtr message, MsgPtr response)
|
2021-08-11 09:42:55 -04:00
|
|
|
|
{
|
2023-04-28 20:46:13 -04:00
|
|
|
|
if (message->mdb.pdl() != 0)
|
|
|
|
|
return response->nak(NR_FORMAT_ERROR);
|
2021-08-11 09:42:55 -04:00
|
|
|
|
|
|
|
|
|
short_message_counter_ = 0;
|
|
|
|
|
length_mismatch_counter_ = 0;
|
|
|
|
|
checksum_fail_counter_ = 0;
|
|
|
|
|
|
|
|
|
|
response->responseType = RESPONSE_TYPE_ACK;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 00:31:00 -04:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Responder::actionGetQueuedMessage
|
|
|
|
|
* @param message
|
|
|
|
|
* @param response
|
|
|
|
|
*/
|
|
|
|
|
void Responder::actionGetQueuedMessage(const MsgPtr message, MsgPtr response)
|
|
|
|
|
{
|
2023-04-28 20:46:13 -04:00
|
|
|
|
if (message->mdb.pdl() != 1)
|
|
|
|
|
return response->nak(NR_FORMAT_ERROR);
|
2021-08-12 00:31:00 -04:00
|
|
|
|
|
|
|
|
|
if (queued_messages_.empty())
|
|
|
|
|
{
|
2023-04-28 20:46:13 -04:00
|
|
|
|
response->mdb.pid = STATUS_MESSAGES;
|
2021-08-12 00:31:00 -04:00
|
|
|
|
actionGetStatusMessages(message, response);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-25 17:20:33 -04:00
|
|
|
|
response->do_not_send = true;
|
2021-08-12 00:31:00 -04:00
|
|
|
|
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)
|
|
|
|
|
{
|
2023-04-28 20:46:13 -04:00
|
|
|
|
if (message->mdb.pdl() != 1)
|
|
|
|
|
return response->nak(NR_FORMAT_ERROR);
|
2021-08-12 00:31:00 -04:00
|
|
|
|
|
2023-04-28 20:46:13 -04:00
|
|
|
|
uint8_t type = message->mdb.pd.front();
|
2021-08-12 00:31:00 -04:00
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2021-08-25 17:20:33 -04:00
|
|
|
|
response->do_not_send = true;
|
2021-08-12 00:31:00 -04:00
|
|
|
|
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)
|
2023-04-28 20:46:13 -04:00
|
|
|
|
response->appendParameterData(b);
|
2021-08-12 00:31:00 -04:00
|
|
|
|
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)
|
|
|
|
|
{
|
2023-04-28 20:46:13 -04:00
|
|
|
|
if (message->mdb.pdl() != 2)
|
|
|
|
|
return response->nak(NR_FORMAT_ERROR);
|
2021-08-12 00:31:00 -04:00
|
|
|
|
|
2023-04-28 20:46:13 -04:00
|
|
|
|
uint16_t status = message->readType<uint16_t>(message->mdb.pd, 0);
|
2021-08-12 00:31:00 -04:00
|
|
|
|
|
|
|
|
|
std::string label = RDM::StatusMessageDescription(status);
|
|
|
|
|
for (size_t i = 0; i < label.size(); i++)
|
|
|
|
|
{
|
|
|
|
|
if (i > 32)
|
|
|
|
|
break;
|
2023-04-28 20:46:13 -04:00
|
|
|
|
response->appendParameterData(label.at(i));
|
2021-08-12 00:31:00 -04:00
|
|
|
|
}
|
2023-04-28 20:46:13 -04:00
|
|
|
|
|
|
|
|
|
response->responseType = RESPONSE_TYPE_ACK;
|
2021-08-12 00:31:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-08 17:25:41 -04:00
|
|
|
|
} // namespace RDM
|