| Jagpal Singh Gill | a32d241 | 2025-10-01 14:55:05 -0700 | [diff] [blame] | 1 | #include "modbus/modbus.hpp" |
| 2 | #include "modbus_server_tester.hpp" |
| Jagpal Singh Gill | 2fa10f4 | 2025-12-08 10:35:14 -0800 | [diff] [blame] | 3 | #include "test_base.hpp" |
| Jagpal Singh Gill | a32d241 | 2025-10-01 14:55:05 -0700 | [diff] [blame] | 4 | |
| Jagpal Singh Gill | a32d241 | 2025-10-01 14:55:05 -0700 | [diff] [blame] | 5 | #include <gtest/gtest.h> |
| 6 | |
| 7 | using namespace std::literals; |
| 8 | |
| 9 | namespace RTUIntf = phosphor::modbus::rtu; |
| 10 | using ModbusIntf = RTUIntf::Modbus; |
| Jagpal Singh Gill | a32d241 | 2025-10-01 14:55:05 -0700 | [diff] [blame] | 11 | |
| Jagpal Singh Gill | 2fa10f4 | 2025-12-08 10:35:14 -0800 | [diff] [blame] | 12 | class ModbusTest : public BaseTest |
| Jagpal Singh Gill | a32d241 | 2025-10-01 14:55:05 -0700 | [diff] [blame] | 13 | { |
| 14 | public: |
| Jagpal Singh Gill | 2fa10f4 | 2025-12-08 10:35:14 -0800 | [diff] [blame] | 15 | static constexpr auto clientDevicePath = "/tmp/ttyV0"; |
| 16 | static constexpr auto serverDevicePath = "/tmp/ttyV1"; |
| 17 | static constexpr auto serviceName = "xyz.openbmc_project.TestModbus"; |
| Jagpal Singh Gill | a32d241 | 2025-10-01 14:55:05 -0700 | [diff] [blame] | 18 | std::unique_ptr<ModbusIntf> modbus; |
| Jagpal Singh Gill | a32d241 | 2025-10-01 14:55:05 -0700 | [diff] [blame] | 19 | |
| Jagpal Singh Gill | 2fa10f4 | 2025-12-08 10:35:14 -0800 | [diff] [blame] | 20 | ModbusTest() : BaseTest(clientDevicePath, serverDevicePath, serviceName) |
| Jagpal Singh Gill | a32d241 | 2025-10-01 14:55:05 -0700 | [diff] [blame] | 21 | { |
| Jagpal Singh Gill | a32d241 | 2025-10-01 14:55:05 -0700 | [diff] [blame] | 22 | modbus = std::make_unique<ModbusIntf>(ctx, fdClient, 115200, 0); |
| Jagpal Singh Gill | a32d241 | 2025-10-01 14:55:05 -0700 | [diff] [blame] | 23 | } |
| 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 | |
| 53 | TEST_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 | |
| 64 | TEST_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 | |
| 75 | TEST_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 | } |