OpenLCP/rdm/responder.cpp

254 lines
6.2 KiB
C++
Raw Normal View History

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"
namespace RDM {
/**
* @brief Responder::Responder
*/
Responder::Responder()
: Device()
{
2021-08-09 15:04:49 -04:00
deviceModelID = 1;
deviceModelDescription = "Basic RDM Responder";
2021-08-08 17:25:41 -04:00
// E1.20 required parameters
2021-08-09 15:04:49 -04:00
// DISC_UNIQUE_BRANCH
// DISC_MUTE
// DISC_UN_MUTE
// addt'l parameters
// COMMS_STATUS
// QUEUED_MESSAGE
2021-08-08 17:25:41 -04:00
}
/**
* @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)
2021-08-08 17:25:41 -04:00
{
if (!response)
return;
2021-08-10 00:57:09 -04:00
2021-08-08 17:25:41 -04:00
// 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() > 255)
response->messageCount = 255;
2021-08-08 17:25:41 -04:00
else
response->messageCount = queued_messages_.size();
2021-08-08 17:25:41 -04:00
std::vector<uint8_t> data;
response->write(data);
2021-08-08 17:25:41 -04:00
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);
// 6.2.8.1 Message Count field for Controller Generated Messages
// The Message Count shall be set to 0x00 in all controller generated requests.
if (message->messageCount != 0)
{
delete message;
return;
}
// 6.2.11 Checksum
// If the checksum field in the packet does not match the calculated checksum,
// then the packet shall be discarded and no response sent.
auto checksum = Message::readType<uint16_t>(data, data.size() - 2);
if (checksum != message->checksum())
{
delete message;
return;
}
receive(message);
2021-08-08 17:25:41 -04:00
delete message;
}
/**
* @brief Responder::receive
* @param message
*/
void Responder::receive(const Message *message)
{
2021-08-10 13:02:58 -04:00
if (message->commandClass != DISCOVERY_COMMAND ||
message->commandClass != GET_COMMAND ||
message->commandClass != SET_COMMAND)
return;
2021-08-08 17:25:41 -04:00
// RDM::UID::operator== also returns true for broadcast messages
if (message->destination != id)
2021-08-10 13:02:58 -04:00
return;
auto response = new Message();
response->source = id;
response->destination = message->source;
response->subDevice = message->subDevice;
response->propertyID = message->propertyID;
response->transaction = message->transaction;
2021-08-08 17:25:41 -04:00
switch (message->commandClass) {
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:
2021-08-10 13:02:58 -04:00
break;
2021-08-08 17:25:41 -04:00
}
}
/**
* @brief Responder::rxDiscovery
* @param message
*/
void Responder::rxDiscovery(__attribute__((unused)) const Message *message,
Message* response)
{
response->commandClass = DISCOVERY_COMMAND_RESPONSE;
2021-08-10 13:02:58 -04:00
queued_messages_.push(response);
2021-08-08 17:25:41 -04:00
}
/**
* @brief Responder::rxGet
* @param message
*/
void Responder::rxGet(const Message *message,
__attribute__((unused)) 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->responseType = RESPONSE_TYPE_NACK_REASON;
response->appendData<uint16_t>(NR_SUB_DEVICE_OUT_OF_RANGE);
2021-08-10 13:02:58 -04:00
queued_messages_.push(response);
2021-08-08 17:25:41 -04:00
return;
}
if (message->subDevice == 0)
{
get(message, response);
2021-08-10 13:02:58 -04:00
queued_messages_.push(response);
2021-08-08 17:25:41 -04:00
return;
}
if (!sub_devices_.count(message->subDevice))
{
response->responseType = RESPONSE_TYPE_NACK_REASON;
response->appendData<uint16_t>(NR_SUB_DEVICE_OUT_OF_RANGE);
2021-08-10 13:02:58 -04:00
queued_messages_.push(response);
2021-08-08 17:25:41 -04:00
return;
}
sub_devices_.at(message->subDevice)->get(message, response);
2021-08-10 13:02:58 -04:00
queued_messages_.push(response);
2021-08-08 17:25:41 -04:00
}
/**
* @brief Responder::rxSet
* @param message
*/
void Responder::rxSet(const Message *message,
__attribute__((unused)) Message* response)
{
if (message->subDevice == 0)
{
set(message, response);
2021-08-10 13:02:58 -04:00
queued_messages_.push(response);
2021-08-08 17:25:41 -04:00
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);
queued_messages_.push(rsp);
}
delete response;
return;
}
if (!sub_devices_.count(message->subDevice))
{
response->responseType = RESPONSE_TYPE_NACK_REASON;
response->appendData<uint16_t>(NR_SUB_DEVICE_OUT_OF_RANGE);
2021-08-10 13:02:58 -04:00
queued_messages_.push(response);
2021-08-08 17:25:41 -04:00
return;
}
sub_devices_.at(message->subDevice)->set(message, response);
2021-08-10 13:02:58 -04:00
queued_messages_.push(response);
2021-08-08 17:25:41 -04:00
}
} // namespace RDM