blob: b2c0b8d8672254686aaccb1b70f77487b2807d8f [file] [log] [blame]
Jagpal Singh Gillcad9ecf2025-10-22 19:53:16 -07001#include "inventory/modbus_inventory.hpp"
2#include "modbus_server_tester.hpp"
3#include "port/base_port.hpp"
Jagpal Singh Gill2fa10f42025-12-08 10:35:14 -08004#include "test_base.hpp"
Jagpal Singh Gillcad9ecf2025-10-22 19:53:16 -07005
6#include <xyz/openbmc_project/Inventory/Source/Modbus/FRU/client.hpp>
7
8#include <gtest/gtest.h>
9
10using namespace std::literals;
11using namespace testing;
12using InventorySourceIntf =
13 sdbusplus::client::xyz::openbmc_project::inventory::source::modbus::FRU<>;
14
Jagpal Singh Gillcad9ecf2025-10-22 19:53:16 -070015namespace ModbusIntf = phosphor::modbus::rtu;
16namespace PortIntf = phosphor::modbus::rtu::port;
17namespace PortConfigIntf = PortIntf::config;
18namespace InventoryIntf = phosphor::modbus::rtu::inventory;
19namespace InventoryConfigIntf = InventoryIntf::config;
20
21class MockPort : public PortIntf::BasePort
22{
23 public:
24 MockPort(sdbusplus::async::context& ctx,
25 const PortConfigIntf::Config& config,
26 const std::string& devicePath) : BasePort(ctx, config, devicePath)
27 {}
28};
29
Jagpal Singh Gill2fa10f42025-12-08 10:35:14 -080030class InventoryTest : public BaseTest
Jagpal Singh Gillcad9ecf2025-10-22 19:53:16 -070031{
32 public:
Jagpal Singh Gillcad9ecf2025-10-22 19:53:16 -070033 static constexpr const char* clientDevicePath =
34 "/tmp/ttyInventoryTestPort0";
35 static constexpr const char* serverDevicePath =
36 "/tmp/ttyInventoryTestPort1";
Jagpal Singh Gillcad9ecf2025-10-22 19:53:16 -070037 static constexpr const auto deviceName = "Test1";
38 static constexpr auto serviceName = "xyz.openbmc_project.TestModbusRTU";
Jagpal Singh Gill2fa10f42025-12-08 10:35:14 -080039 PortConfigIntf::Config portConfig;
Jagpal Singh Gillcad9ecf2025-10-22 19:53:16 -070040
Jagpal Singh Gill2fa10f42025-12-08 10:35:14 -080041 InventoryTest() : BaseTest(clientDevicePath, serverDevicePath, serviceName)
Jagpal Singh Gillcad9ecf2025-10-22 19:53:16 -070042 {
43 portConfig.name = "TestPort1";
44 portConfig.portMode = PortConfigIntf::PortMode::rs485;
45 portConfig.baudRate = 115200;
46 portConfig.rtsDelay = 1;
Jagpal Singh Gillcad9ecf2025-10-22 19:53:16 -070047 }
48
49 auto testInventorySourceCreation(std::string objPath)
50 -> sdbusplus::async::task<void>
51 {
52 InventoryConfigIntf::Config::port_address_map_t addressMap;
53 addressMap[portConfig.name] = {{.start = TestIntf::testDeviceAddress,
54 .end = TestIntf::testDeviceAddress}};
55 InventoryConfigIntf::Config deviceConfig = {
56 .name = deviceName,
57 .addressMap = addressMap,
58 .registers = {{"Model",
59 TestIntf::testReadHoldingRegisterModelOffset,
60 TestIntf::testReadHoldingRegisterModelCount}},
61 .parity = ModbusIntf::Parity::none,
62 .baudRate = 115200};
63 InventoryIntf::Device::serial_port_map_t ports;
64 ports[portConfig.name] =
65 std::make_unique<MockPort>(ctx, portConfig, clientDevicePath);
66
67 auto inventoryDevice =
68 std::make_unique<InventoryIntf::Device>(ctx, deviceConfig, ports);
69
70 co_await inventoryDevice->probePorts();
71
72 // Create InventorySource client interface to read back D-Bus properties
73 auto properties = co_await InventorySourceIntf(ctx)
74 .service(serviceName)
75 .path(objPath)
76 .properties();
77
78 constexpr auto defaultInventoryValue = "Unknown";
79
80 EXPECT_EQ(properties.name,
81 std::format("{} {} {}", deviceName,
82 TestIntf::testDeviceAddress, portConfig.name))
83 << "Name mismatch";
84 EXPECT_EQ(properties.address, TestIntf::testDeviceAddress)
85 << "Address mismatch";
86 EXPECT_EQ(properties.link_tty, portConfig.name) << "Link TTY mismatch";
87 EXPECT_EQ(properties.model, TestIntf::testReadHoldingRegisterModelStr)
88 << "Model mismatch";
89 EXPECT_EQ(properties.serial_number, defaultInventoryValue)
90 << "Part Number mismatch";
91
92 co_return;
93 }
Jagpal Singh Gillcad9ecf2025-10-22 19:53:16 -070094};
95
96TEST_F(InventoryTest, TestAddInventorySource)
97{
98 auto objPath =
99 std::format("{}/{}_{}_{}", InventorySourceIntf::namespace_path,
100 deviceName, TestIntf::testDeviceAddress, portConfig.name);
101
102 ctx.spawn(testInventorySourceCreation(objPath));
103
104 ctx.spawn(sdbusplus::async::sleep_for(ctx, 1s) |
105 sdbusplus::async::execution::then([&]() { ctx.request_stop(); }));
106
107 ctx.run();
108}