296 lines
7.2 KiB
C++
296 lines
7.2 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 <limits>
|
||
|
||
namespace RDM {
|
||
|
||
/**
|
||
* @brief Responder::Responder
|
||
*/
|
||
Responder::Responder()
|
||
: Device()
|
||
{
|
||
deviceModelID = 1;
|
||
deviceModelDescription = "Basic RDM Responder";
|
||
|
||
// /// Category – Network Management
|
||
// static const PID DISC_UNIQUE_BRANCH = 0x0001;
|
||
// static const PID DISC_MUTE = 0x0002;
|
||
// static const PID DISC_UN_MUTE = 0x0003;
|
||
// static const PID PROXIED_DEVICES = 0x0010;
|
||
// static const PID PROXIED_DEVICE_COUNT = 0x0011;
|
||
// static const PID COMMS_STATUS = 0x0015;
|
||
// /// Category - Status Collection
|
||
// static const PID QUEUED_MESSAGE = 0x0020; // See Table A-4
|
||
// static const PID STATUS_MESSAGES = 0x0030; // See Table A-4
|
||
// static const PID STATUS_ID_DESCRIPTION = 0x0031;
|
||
// static const PID CLEAR_STATUS_ID = 0x0032;
|
||
// static const PID SUB_DEVICE_STATUS_REPORT_THRESHOLD = 0x0033; // See Table A-4
|
||
}
|
||
|
||
|
||
/**
|
||
* @brief Responder::~Responder
|
||
*/
|
||
Responder::~Responder()
|
||
{
|
||
}
|
||
|
||
|
||
/**
|
||
* @brief Responder::send
|
||
* @param data
|
||
*/
|
||
void Responder::send(__attribute__((unused)) const std::vector<uint8_t> &data)
|
||
{
|
||
|
||
}
|
||
|
||
|
||
/**
|
||
* @brief Responder::send
|
||
* @param message
|
||
*/
|
||
void Responder::send(Message *response)
|
||
{
|
||
if (!response)
|
||
return;
|
||
|
||
// 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.
|
||
if (queued_messages_.size() > std::numeric_limits<uint8_t>::max())
|
||
response->messageCount = std::numeric_limits<uint8_t>::max();
|
||
else
|
||
response->messageCount = queued_messages_.size();
|
||
|
||
std::vector<uint8_t> data;
|
||
response->write(data);
|
||
send(data);
|
||
delete response;
|
||
response = nullptr;
|
||
}
|
||
|
||
|
||
/**
|
||
* @brief Responder::receive
|
||
* @param data
|
||
*/
|
||
void Responder::receive(const std::vector<uint8_t> &data)
|
||
{
|
||
auto message = new Message(data);
|
||
receive(message);
|
||
delete message;
|
||
}
|
||
|
||
|
||
/**
|
||
* @brief Responder::receive
|
||
* @param message
|
||
*/
|
||
void Responder::receive(const Message *message)
|
||
{
|
||
if (message->short_message)
|
||
{
|
||
if (short_message_counter_ != std::numeric_limits<uint16_t>::max())
|
||
short_message_counter_++;
|
||
return;
|
||
}
|
||
|
||
// RDM::UID::operator== also returns true for broadcast messages
|
||
if (message->destination != id ||
|
||
message->incorrect_sc ||
|
||
message->incorrect_sub_sc)
|
||
return;
|
||
|
||
if (message->length_mismatch)
|
||
{
|
||
if (length_mismatch_counter_ != std::numeric_limits<uint16_t>::max())
|
||
length_mismatch_counter_++;
|
||
return;
|
||
}
|
||
|
||
if (message->checksum_fail)
|
||
{
|
||
if (checksum_fail_counter_ != std::numeric_limits<uint16_t>::max())
|
||
checksum_fail_counter_++;
|
||
return;
|
||
}
|
||
|
||
// all other uncaught errors
|
||
if (message->failure_mode != 0)
|
||
return;
|
||
|
||
// responder can ignore _COMMAND_RESPONSE class messages
|
||
if (message->commandClass == DISCOVERY_COMMAND_RESPONSE ||
|
||
message->commandClass == GET_COMMAND_RESPONSE ||
|
||
message->commandClass == SET_COMMAND_RESPONSE)
|
||
return;
|
||
|
||
auto response = new Message();
|
||
response->source = id;
|
||
response->destination = message->source;
|
||
response->subDevice = message->subDevice;
|
||
response->propertyID = message->propertyID;
|
||
response->transaction = message->transaction;
|
||
|
||
switch (message->commandClass) {
|
||
case DISCOVERY_COMMAND:
|
||
rxDiscovery(message, response);
|
||
break;
|
||
case GET_COMMAND:
|
||
rxGet(message, response);
|
||
break;
|
||
case SET_COMMAND:
|
||
rxSet(message, response);
|
||
break;
|
||
default:
|
||
delete response;
|
||
return;
|
||
}
|
||
|
||
if (!response)
|
||
return;
|
||
|
||
/// 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() &&
|
||
message->commandClass != DISCOVERY_COMMAND)
|
||
{
|
||
delete response;
|
||
return;
|
||
}
|
||
|
||
if (response->do_no_send)
|
||
{
|
||
delete response;
|
||
return;
|
||
}
|
||
|
||
queued_messages_.push(response);
|
||
}
|
||
|
||
|
||
/**
|
||
* @brief Responder::reset
|
||
* @param hard
|
||
*/
|
||
void Responder::reset(bool hard)
|
||
{
|
||
(void)hard;
|
||
|
||
/// 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
|
||
*/
|
||
void Responder::rxDiscovery(__attribute__((unused)) const Message *message,
|
||
Message* response)
|
||
{
|
||
response->commandClass = DISCOVERY_COMMAND_RESPONSE;
|
||
}
|
||
|
||
|
||
/**
|
||
* @brief Responder::rxGet
|
||
* @param message
|
||
*/
|
||
void Responder::rxGet(const Message *message, Message* response)
|
||
{
|
||
// 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
|
||
*/
|
||
void Responder::rxSet(const Message *message, Message* response)
|
||
{
|
||
if (message->subDevice == 0)
|
||
{
|
||
set(message, response);
|
||
return;
|
||
}
|
||
|
||
if (message->subDevice == SUB_DEVICE_ALL_CALL)
|
||
{
|
||
for (auto& [num, dev] : sub_devices_)
|
||
{
|
||
Message * rsp = new Message(*response);
|
||
rsp->subDevice = num;
|
||
dev->set(message, rsp);
|
||
if (message->destination.isBroadcast())
|
||
delete rsp;
|
||
else
|
||
queued_messages_.push(rsp);
|
||
}
|
||
response->do_no_send = true;
|
||
return;
|
||
}
|
||
|
||
if (!sub_devices_.count(message->subDevice))
|
||
{
|
||
response->nak(NR_SUB_DEVICE_OUT_OF_RANGE);
|
||
return;
|
||
}
|
||
|
||
sub_devices_.at(message->subDevice)->set(message, response);
|
||
}
|
||
|
||
|
||
} // namespace RDM
|