blob: 787d433d42ab8169bb5d2e05cd91c2cbc7895ac5 [file] [log] [blame]
Jagpal Singh Gilla32d2412025-10-01 14:55:05 -07001#include "modbus/modbus.hpp"
2#include "modbus_server_tester.hpp"
Jagpal Singh Gill2fa10f42025-12-08 10:35:14 -08003#include "test_base.hpp"
Jagpal Singh Gilla32d2412025-10-01 14:55:05 -07004
Jagpal Singh Gilla32d2412025-10-01 14:55:05 -07005#include <gtest/gtest.h>
6
7using namespace std::literals;
8
9namespace RTUIntf = phosphor::modbus::rtu;
10using ModbusIntf = RTUIntf::Modbus;
Jagpal Singh Gilla32d2412025-10-01 14:55:05 -070011
Jagpal Singh Gill2fa10f42025-12-08 10:35:14 -080012class ModbusTest : public BaseTest
Jagpal Singh Gilla32d2412025-10-01 14:55:05 -070013{
14 public:
Jagpal Singh Gill2fa10f42025-12-08 10:35:14 -080015 static constexpr auto clientDevicePath = "/tmp/ttyV0";
16 static constexpr auto serverDevicePath = "/tmp/ttyV1";
17 static constexpr auto serviceName = "xyz.openbmc_project.TestModbus";
Jagpal Singh Gilla32d2412025-10-01 14:55:05 -070018 std::unique_ptr<ModbusIntf> modbus;
Jagpal Singh Gilla32d2412025-10-01 14:55:05 -070019
Jagpal Singh Gill2fa10f42025-12-08 10:35:14 -080020 ModbusTest() : BaseTest(clientDevicePath, serverDevicePath, serviceName)
Jagpal Singh Gilla32d2412025-10-01 14:55:05 -070021 {
Jagpal Singh Gilla32d2412025-10-01 14:55:05 -070022 modbus = std::make_unique<ModbusIntf>(ctx, fdClient, 115200, 0);
Jagpal Singh Gilla32d2412025-10-01 14:55:05 -070023 }
24
25 auto TestHoldingRegisters(uint16_t registerOffset, bool res)
26 -> sdbusplus::async::task<void>
27 {
28 std::cout << "TestHoldingRegisters() start" << std::endl;
29
30 std::vector<uint16_t> registers(
31 TestIntf::testSuccessReadHoldingRegisterCount);
32
33 auto ret = co_await modbus->readHoldingRegisters(
34 TestIntf::testDeviceAddress, registerOffset, registers);
35
36 EXPECT_EQ(ret, res) << "Failed to read holding registers";
37
38 if (!res)
39 {
40 co_return;
41 }
42
43 for (auto i = 0; i < TestIntf::testSuccessReadHoldingRegisterCount; i++)
44 {
45 EXPECT_EQ(registers[i],
46 TestIntf::testSuccessReadHoldingRegisterResponse[i]);
47 }
48
49 co_return;
50 }
51};
52
53TEST_F(ModbusTest, TestReadHoldingRegisterSuccess)
54{
55 ctx.spawn(TestHoldingRegisters(
56 TestIntf::testSuccessReadHoldingRegisterOffset, true));
57
58 ctx.spawn(sdbusplus::async::sleep_for(ctx, 1s) |
59 sdbusplus::async::execution::then([&]() { ctx.request_stop(); }));
60
61 ctx.run();
62}
63
64TEST_F(ModbusTest, TestReadHoldingRegisterSegmentedSuccess)
65{
66 ctx.spawn(TestHoldingRegisters(
67 TestIntf::testSuccessReadHoldingRegisterSegmentedOffset, true));
68
69 ctx.spawn(sdbusplus::async::sleep_for(ctx, 1s) |
70 sdbusplus::async::execution::then([&]() { ctx.request_stop(); }));
71
72 ctx.run();
73}
74
75TEST_F(ModbusTest, TestReadHoldingRegisterFailure)
76{
77 ctx.spawn(
78 TestHoldingRegisters(TestIntf::testFailureReadHoldingRegister, false));
79
80 ctx.spawn(sdbusplus::async::sleep_for(ctx, 1s) |
81 sdbusplus::async::execution::then([&]() { ctx.request_stop(); }));
82
83 ctx.run();
84}