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
|
2021-08-08 17:25:41 -04:00
|
|
|
|
*/
|
2023-05-02 13:00:19 -04:00
|
|
|
|
Responder::Responder(UID id)
|
|
|
|
|
: Device(id)
|
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-05-02 13:00:19 -04:00
|
|
|
|
Parameter *parameter;
|
2021-08-26 12:30:44 -04:00
|
|
|
|
/// \cite RDM 7.5 Discovery Unique Branch Message (DISC_UNIQUE_BRANCH)
|
2023-05-02 13:00:19 -04:00
|
|
|
|
parameter = addParameter(DISC_UNIQUE_BRANCH);
|
|
|
|
|
parameter->onDisc([this](MsgPair msg){actionDiscoverUniqueBranch(msg);});
|
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-05-02 13:00:19 -04:00
|
|
|
|
parameter = addParameter(DISC_MUTE);
|
|
|
|
|
parameter->onDisc([this](MsgPair msg){actionDiscoveryMute(msg);});
|
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-05-02 13:00:19 -04:00
|
|
|
|
parameter = addParameter(DISC_UN_MUTE);
|
|
|
|
|
parameter->onDisc([this](MsgPair msg){actionDiscoveryUnmute(msg);});
|
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-05-02 13:00:19 -04:00
|
|
|
|
parameter = addParameter(COMMS_STATUS);
|
|
|
|
|
parameter->onGet([this](MsgPair msg){actionGetCommsStatus(msg);});
|
|
|
|
|
parameter->onSet([this](MsgPair msg){actionSetCommsStatus(msg);});
|
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-05-02 13:00:19 -04:00
|
|
|
|
parameter = addParameter(QUEUED_MESSAGE);
|
|
|
|
|
parameter->onGet([this](MsgPair msg){actionGetQueuedMessage(msg);});
|
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-05-02 13:00:19 -04:00
|
|
|
|
parameter = addParameter(STATUS_MESSAGES);
|
|
|
|
|
parameter->onGet([this](MsgPair msg){actionGetStatusMessages(msg);});
|
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-05-02 13:00:19 -04:00
|
|
|
|
parameter = addParameter(STATUS_ID_DESCRIPTION);
|
|
|
|
|
parameter->onGet([this](MsgPair msg){actionGetStatusIdDescription(msg);});
|
2021-08-08 17:25:41 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Responder::~Responder()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Responder::send
|
2023-05-02 13:00:19 -04:00
|
|
|
|
* @param buffer
|
2021-08-08 17:25:41 -04:00
|
|
|
|
*/
|
2023-05-15 11:40:43 -04:00
|
|
|
|
void Responder::send(const std::vector<uint8_t> &buffer) const
|
2021-08-08 17:25:41 -04:00
|
|
|
|
{
|
2023-04-26 10:02:20 -04:00
|
|
|
|
if (auto sp = sender_.lock())
|
2023-05-02 13:00:19 -04:00
|
|
|
|
(*sp)(buffer); // 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-05-15 11:40:43 -04:00
|
|
|
|
void Responder::send(const MsgPtr response) const
|
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-05-15 11:40:43 -04:00
|
|
|
|
/// \cite RDM 6.2.5 Source UID
|
|
|
|
|
/// The Source UID is the UID of the device originating this packet.
|
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(),
|
2023-05-15 11:40:43 -04:00
|
|
|
|
(size_t)std::numeric_limits<uint8_t>::max());
|
2023-04-28 20:46:13 -04:00
|
|
|
|
|
2023-05-15 11:40:43 -04:00
|
|
|
|
std::vector<uint8_t> buffer;
|
|
|
|
|
response->write(buffer);
|
|
|
|
|
send(buffer);
|
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
|
2023-05-02 13:00:19 -04:00
|
|
|
|
* @param buffer
|
2021-08-10 13:33:24 -04:00
|
|
|
|
*/
|
2023-05-02 13:00:19 -04:00
|
|
|
|
void Responder::receive(const std::vector<uint8_t> &buffer)
|
2021-08-10 13:33:24 -04:00
|
|
|
|
{
|
2023-04-28 20:46:13 -04:00
|
|
|
|
auto msg = std::make_shared<Message>();
|
2023-05-02 13:00:19 -04:00
|
|
|
|
msg->read(buffer);
|
2023-04-28 20:46:13 -04:00
|
|
|
|
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
|
2023-05-02 13:00:19 -04:00
|
|
|
|
response->responseType = RESPONSE_TYPE_ACK; // preset type to ack
|
2023-04-28 20:46:13 -04:00
|
|
|
|
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
|
2023-05-02 13:00:19 -04:00
|
|
|
|
dispatch({message, response});
|
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)
|
|
|
|
|
{
|
2023-05-02 13:00:19 -04:00
|
|
|
|
Device::reset(hard);
|
2021-08-11 00:02:41 -04:00
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-08-11 01:26:51 -04:00
|
|
|
|
/**
|
|
|
|
|
* @brief Responder::actionDiscoverUniqueBranch
|
2023-05-02 13:00:19 -04:00
|
|
|
|
* @param msg
|
2021-08-11 01:26:51 -04:00
|
|
|
|
*/
|
2023-05-02 13:00:19 -04:00
|
|
|
|
void Responder::actionDiscoverUniqueBranch(MsgPair msg)
|
2021-08-11 01:26:51 -04:00
|
|
|
|
{
|
2023-05-02 13:00:19 -04:00
|
|
|
|
auto [command, response] = msg;
|
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-05-02 13:00:19 -04:00
|
|
|
|
lower.manufacturer = Message::readType<uint16_t>(command->mdb.pd, 0);
|
|
|
|
|
lower.device = Message::readType<uint32_t>(command->mdb.pd, 2);
|
|
|
|
|
upper.manufacturer = Message::readType<uint16_t>(command->mdb.pd, 6);
|
|
|
|
|
upper.device = Message::readType<uint32_t>(command->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
|
2023-05-02 13:00:19 -04:00
|
|
|
|
* @param msg
|
2021-08-11 01:26:51 -04:00
|
|
|
|
*/
|
2023-05-02 13:00:19 -04:00
|
|
|
|
void Responder::actionDiscoveryMute(MsgPair msg)
|
2021-08-11 01:26:51 -04:00
|
|
|
|
{
|
2023-05-02 13:00:19 -04:00
|
|
|
|
auto [command, response] = msg;
|
2021-08-11 01:26:51 -04:00
|
|
|
|
discovery_mute_flag_ = true;
|
2023-05-02 13:00:19 -04:00
|
|
|
|
controller_uid_ = command->source;
|
2023-04-28 20:46:13 -04:00
|
|
|
|
response->appendParameterData(control_field);
|
2021-08-11 01:26:51 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Responder::actionDiscoveryUnmute
|
2023-05-02 13:00:19 -04:00
|
|
|
|
* @param msg
|
2021-08-11 01:26:51 -04:00
|
|
|
|
*/
|
2023-05-02 13:00:19 -04:00
|
|
|
|
void Responder::actionDiscoveryUnmute(MsgPair msg)
|
2021-08-11 01:26:51 -04:00
|
|
|
|
{
|
2023-05-02 13:00:19 -04:00
|
|
|
|
auto [_, response] = msg;
|
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
|
|
|
|
}
|
|
|
|
|
|
2021-08-11 09:42:55 -04:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Responder::actionGetCommsStatus
|
2023-05-02 13:00:19 -04:00
|
|
|
|
* @param msg
|
2021-08-11 09:42:55 -04:00
|
|
|
|
*/
|
2023-05-02 13:00:19 -04:00
|
|
|
|
void Responder::actionGetCommsStatus(MsgPair msg)
|
2021-08-11 09:42:55 -04:00
|
|
|
|
{
|
2023-05-02 13:00:19 -04:00
|
|
|
|
auto [_, response] = msg;
|
2023-04-28 20:46:13 -04:00
|
|
|
|
response->appendParameterData(short_message_counter_);
|
|
|
|
|
response->appendParameterData(length_mismatch_counter_);
|
|
|
|
|
response->appendParameterData(checksum_fail_counter_);
|
2021-08-11 09:42:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Responder::actionSetCommsStatus
|
2023-05-02 13:00:19 -04:00
|
|
|
|
* @param msg
|
2021-08-11 09:42:55 -04:00
|
|
|
|
*/
|
2023-05-02 13:00:19 -04:00
|
|
|
|
void Responder::actionSetCommsStatus(MsgPair msg)
|
2021-08-11 09:42:55 -04:00
|
|
|
|
{
|
2023-05-02 13:00:19 -04:00
|
|
|
|
auto [_, response] = msg;
|
2021-08-11 09:42:55 -04:00
|
|
|
|
short_message_counter_ = 0;
|
|
|
|
|
length_mismatch_counter_ = 0;
|
|
|
|
|
checksum_fail_counter_ = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 00:31:00 -04:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Responder::actionGetQueuedMessage
|
2023-05-02 13:00:19 -04:00
|
|
|
|
* @param msg
|
2021-08-12 00:31:00 -04:00
|
|
|
|
*/
|
2023-05-02 13:00:19 -04:00
|
|
|
|
void Responder::actionGetQueuedMessage(MsgPair msg)
|
2021-08-12 00:31:00 -04:00
|
|
|
|
{
|
2023-05-02 13:00:19 -04:00
|
|
|
|
auto [_, response] = msg;
|
2021-08-12 00:31:00 -04:00
|
|
|
|
if (queued_messages_.empty())
|
|
|
|
|
{
|
2023-05-02 13:00:19 -04:00
|
|
|
|
response->mdb.pid = STATUS_MESSAGES.pid;
|
|
|
|
|
actionGetStatusMessages(msg);
|
2021-08-12 00:31:00 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-02 13:00:19 -04:00
|
|
|
|
response = queued_messages_.front();
|
2021-08-12 00:31:00 -04:00
|
|
|
|
queued_messages_.pop_front();
|
2023-05-02 13:00:19 -04:00
|
|
|
|
last_status_message_ = response;
|
2021-08-12 00:31:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Responder::actionGetStatusMessages
|
2023-05-02 13:00:19 -04:00
|
|
|
|
* @param msg
|
2021-08-12 00:31:00 -04:00
|
|
|
|
*/
|
2023-05-02 13:00:19 -04:00
|
|
|
|
void Responder::actionGetStatusMessages(MsgPair msg)
|
2021-08-12 00:31:00 -04:00
|
|
|
|
{
|
2023-05-02 13:00:19 -04:00
|
|
|
|
auto [command, response] = msg;
|
|
|
|
|
uint8_t queue = command->mdb.pd.front();
|
2021-08-12 00:31:00 -04:00
|
|
|
|
|
2023-05-02 13:00:19 -04:00
|
|
|
|
/// Due to the maximum packet length limitation, the total number of status messages sent
|
|
|
|
|
/// within a single message cannot exceed 25.
|
|
|
|
|
int count = 25;
|
2021-08-12 00:31:00 -04:00
|
|
|
|
|
2023-05-02 13:00:19 -04:00
|
|
|
|
/// \cite RDM 10.3.2 The Status Type STATUS_GET_LAST_MESSAGE shall be used by the Controller
|
|
|
|
|
/// to request the retransmission of the last sent Status Message or Queued Message.
|
|
|
|
|
if (queue == STATUS_GET_LAST_MESSAGE)
|
|
|
|
|
{
|
|
|
|
|
if (last_status_message_)
|
|
|
|
|
response = last_status_message_;
|
|
|
|
|
else
|
2021-08-12 00:31:00 -04:00
|
|
|
|
response->nak(NR_DATA_OUT_OF_RANGE);
|
2023-05-02 13:00:19 -04:00
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
/// Status is considered successfully delivered when the responder receives a
|
|
|
|
|
/// Status Type Requested other than STATUS_GET_LAST_MESSAGE.
|
|
|
|
|
last_status_message_ = nullptr;
|
|
|
|
|
}
|
2021-08-12 00:31:00 -04:00
|
|
|
|
|
2023-05-02 13:00:19 -04:00
|
|
|
|
// _CLEARED responses sit in the same queue as their status setting counterparts
|
|
|
|
|
queue &= 0xf;
|
2021-08-12 00:31:00 -04:00
|
|
|
|
|
2023-05-02 13:00:19 -04:00
|
|
|
|
auto reportStatusQueue = [msg, &count] (std::queue<StatusPtr> &q)
|
2021-08-12 00:31:00 -04:00
|
|
|
|
{
|
2023-05-02 13:00:19 -04:00
|
|
|
|
auto [_, response] = msg;
|
|
|
|
|
while(!q.empty() && count >= 0)
|
2021-08-12 00:31:00 -04:00
|
|
|
|
{
|
2023-05-02 13:00:19 -04:00
|
|
|
|
count--;
|
|
|
|
|
if (count < 0) break;
|
|
|
|
|
|
|
|
|
|
auto status = q.front();
|
2021-08-12 00:31:00 -04:00
|
|
|
|
q.pop();
|
2023-05-02 13:00:19 -04:00
|
|
|
|
|
|
|
|
|
response->mdb.pd.insert(response->mdb.pd.end(),
|
|
|
|
|
std::begin(status->bytes), std::end(status->bytes));
|
2021-08-12 00:31:00 -04:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-05-02 13:00:19 -04:00
|
|
|
|
while (queue > STATUS_NONE && count >= 0)
|
|
|
|
|
{
|
|
|
|
|
if (queued_statuses.count(queue))
|
|
|
|
|
reportStatusQueue(queued_statuses.at(queue));
|
|
|
|
|
for (auto& [_, dev] : sub_devices_)
|
|
|
|
|
if (dev->queued_statuses.count(queue))
|
|
|
|
|
reportStatusQueue(dev->queued_statuses.at(queue));
|
|
|
|
|
queue--;
|
|
|
|
|
}
|
2021-08-12 00:31:00 -04:00
|
|
|
|
|
2023-05-02 13:00:19 -04:00
|
|
|
|
if (count < 0)
|
2021-08-12 00:31:00 -04:00
|
|
|
|
response->responseType = RESPONSE_TYPE_ACK_OVERFLOW;
|
|
|
|
|
|
2023-05-02 13:00:19 -04:00
|
|
|
|
/// The responder shall maintain reported status information until it has been successfully
|
|
|
|
|
/// delivered to the controller.
|
2021-08-12 00:31:00 -04:00
|
|
|
|
last_status_message_ = response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Responder::actionGetStatusIdDescription
|
2023-05-02 13:00:19 -04:00
|
|
|
|
* @param msg
|
2021-08-12 00:31:00 -04:00
|
|
|
|
*/
|
2023-05-02 13:00:19 -04:00
|
|
|
|
void Responder::actionGetStatusIdDescription(MsgPair msg)
|
2021-08-12 00:31:00 -04:00
|
|
|
|
{
|
2023-05-02 13:00:19 -04:00
|
|
|
|
auto [command, response] = msg;
|
|
|
|
|
uint16_t status = Message::readType<uint16_t>(command->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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-08 17:25:41 -04:00
|
|
|
|
} // namespace RDM
|