1
0
Fork 0
OpenLCP/test/test_rdm.cpp

81 lines
4.0 KiB
C++

/*
test_rdm.cpp
Copyright (c) 2023 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 <gtest/gtest.h>
#include <responder.h>
TEST(RDM, device_info)
{
RDM::Responder responder(RDM::UID(0x12345678, 0x1234));
std::vector<uint8_t> rdm_data;
responder.deviceModelID = 0x4242;
responder.deviceProductCategory = RDM::PRODUCT_CATEGORY_TEST;
auto token = responder.setSender([&rdm_data](const std::vector<uint8_t> &r){rdm_data = r;});
rdm_data.push_back(RDM::SC_RDM); // START code
rdm_data.push_back(RDM::SC_SUB_MESSAGE); // sub-START code
rdm_data.push_back(24); // message length
RDM::Message::writeType<uint16_t>(rdm_data, responder.uid.manufacturer); // dest manufacturer
RDM::Message::writeType<uint32_t>(rdm_data, responder.uid.device); // dest device
RDM::Message::writeType<uint16_t>(rdm_data, 0x5678); // source manufacturer
RDM::Message::writeType<uint32_t>(rdm_data, 0x87654321); // source device
rdm_data.push_back(42); // transaction number
rdm_data.push_back(1); // port ID
rdm_data.push_back(0); // message count
RDM::Message::writeType<uint16_t>(rdm_data, 0); // sub device
rdm_data.push_back(RDM::GET_COMMAND); // command class
RDM::Message::writeType<uint16_t>(rdm_data, RDM::DEVICE_INFO.pid); // parameter ID
rdm_data.push_back(0); // Parameter Data Length
uint16_t csum = 0;
for (const auto byte : rdm_data)
csum += byte;
RDM::Message::writeType<uint16_t>(rdm_data, csum);
EXPECT_EQ(rdm_data.size(), 26) << "mallformed DEVICE_INFO GET_COMMAND";
responder.receive(rdm_data);
EXPECT_EQ(rdm_data.size(), 26+0x13) << "mallformed DEVICE_INFO GET_RESPONSE";
RDM::Message reply;
reply.read(rdm_data);
EXPECT_EQ(reply.failure_mode, 0) << "Reply Message failed";
EXPECT_EQ(reply.source.manufacturer, responder.uid.manufacturer) << "Responder manufacturer mismatch";
EXPECT_EQ(reply.source.device, responder.uid.device) << "Responder device mismatch";
EXPECT_EQ(reply.tn, 42) << "Failed to track command conversation";
EXPECT_EQ(reply.responseType, RDM::RESPONSE_TYPE_ACK) << "Command not acknowledged.";
EXPECT_EQ(reply.messageCount, 0) << "Unexpected queued messages";
EXPECT_EQ(reply.mdb.cc, RDM::GET_COMMAND_RESPONSE) << "Unexpected command class";
EXPECT_EQ(reply.mdb.pid, RDM::DEVICE_INFO.pid) << "Unexpected PID";
EXPECT_EQ(reply.mdb.pdl(), 0x13) << "PDL length mismatch";
auto proto_version = RDM::Message::readType<uint16_t>(reply.mdb.pd, 0);
EXPECT_EQ(proto_version, RDM::RDM_PROTOCOL_VERSION) << "Unknown RDM version.";
auto device_model = RDM::Message::readType<uint16_t>(reply.mdb.pd, 2);
EXPECT_EQ(device_model, 0x4242) << "Unexpected Device Model";
auto product_category = RDM::Message::readType<uint16_t>(reply.mdb.pd, 4);
EXPECT_EQ(product_category, RDM::PRODUCT_CATEGORY_TEST) << "Unexpected Product Category";
}