/* 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() { deviceModelID = 1; deviceModelDescription = "Basic RDM Responder"; // E1.20 required parameters // DISC_UNIQUE_BRANCH // DISC_MUTE // DISC_UN_MUTE // addt'l parameters // COMMS_STATUS // QUEUED_MESSAGE } /** * @brief Responder::~Responder */ Responder::~Responder() { } /** * @brief Responder::send * @param data */ void Responder::send(__attribute__((unused)) const std::vector &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() > 255) response->messageCount = 255; else response->messageCount = queued_messages_.size(); std::vector data; response->write(data); send(data); delete response; response = nullptr; } /** * @brief Responder::receive * @param data */ void Responder::receive(const std::vector &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(data, data.size() - 2); if (checksum != message->checksum()) { delete message; return; } receive(message); delete message; } /** * @brief Responder::receive * @param message */ void Responder::receive(const Message *message) { if (message->commandClass != DISCOVERY_COMMAND || message->commandClass != GET_COMMAND || message->commandClass != SET_COMMAND) return; // RDM::UID::operator== also returns true for broadcast messages if (message->destination != id) 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: break; } } /** * @brief Responder::rxDiscovery * @param message */ void Responder::rxDiscovery(__attribute__((unused)) const Message *message, Message* response) { response->commandClass = DISCOVERY_COMMAND_RESPONSE; queued_messages_.push(response); } /** * @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(NR_SUB_DEVICE_OUT_OF_RANGE); queued_messages_.push(response); return; } if (message->subDevice == 0) { get(message, response); queued_messages_.push(response); return; } if (!sub_devices_.count(message->subDevice)) { response->responseType = RESPONSE_TYPE_NACK_REASON; response->appendData(NR_SUB_DEVICE_OUT_OF_RANGE); queued_messages_.push(response); return; } sub_devices_.at(message->subDevice)->get(message, response); queued_messages_.push(response); } /** * @brief Responder::rxSet * @param message */ void Responder::rxSet(const Message *message, __attribute__((unused)) Message* response) { if (message->subDevice == 0) { set(message, response); queued_messages_.push(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); queued_messages_.push(rsp); } delete response; return; } if (!sub_devices_.count(message->subDevice)) { response->responseType = RESPONSE_TYPE_NACK_REASON; response->appendData(NR_SUB_DEVICE_OUT_OF_RANGE); queued_messages_.push(response); return; } sub_devices_.at(message->subDevice)->set(message, response); queued_messages_.push(response); } } // namespace RDM