| Jagpal Singh Gill | 7184805 | 2025-10-16 00:28:58 -0700 | [diff] [blame] | 1 | #include "common/events.hpp" |
| Jagpal Singh Gill | e92aba4 | 2025-10-16 00:00:13 -0700 | [diff] [blame] | 2 | #include "device/device_factory.hpp" |
| Jagpal Singh Gill | e92aba4 | 2025-10-16 00:00:13 -0700 | [diff] [blame] | 3 | #include "port/base_port.hpp" |
| Jagpal Singh Gill | 2fa10f4 | 2025-12-08 10:35:14 -0800 | [diff] [blame] | 4 | #include "test_base.hpp" |
| Jagpal Singh Gill | e92aba4 | 2025-10-16 00:00:13 -0700 | [diff] [blame] | 5 | |
| Jagpal Singh Gill | 1f1d004 | 2025-11-17 13:15:33 -0800 | [diff] [blame] | 6 | #include <xyz/openbmc_project/Association/Definitions/client.hpp> |
| Jagpal Singh Gill | e92aba4 | 2025-10-16 00:00:13 -0700 | [diff] [blame] | 7 | #include <xyz/openbmc_project/Sensor/Value/client.hpp> |
| Jagpal Singh Gill | 1f1d004 | 2025-11-17 13:15:33 -0800 | [diff] [blame] | 8 | #include <xyz/openbmc_project/State/Decorator/Availability/client.hpp> |
| 9 | #include <xyz/openbmc_project/State/Decorator/OperationalStatus/client.hpp> |
| Jagpal Singh Gill | e92aba4 | 2025-10-16 00:00:13 -0700 | [diff] [blame] | 10 | |
| 11 | #include <cmath> |
| 12 | #include <string> |
| 13 | |
| 14 | #include <gtest/gtest.h> |
| 15 | |
| 16 | using namespace std::literals; |
| 17 | using namespace testing; |
| 18 | using SensorValueIntf = |
| 19 | sdbusplus::client::xyz::openbmc_project::sensor::Value<>; |
| Jagpal Singh Gill | 1f1d004 | 2025-11-17 13:15:33 -0800 | [diff] [blame] | 20 | using OperationalStatusIntf = sdbusplus::client::xyz::openbmc_project::state:: |
| 21 | decorator::OperationalStatus<>; |
| 22 | using AvailabilityIntf = |
| 23 | sdbusplus::client::xyz::openbmc_project::state::decorator::Availability<>; |
| 24 | using AssociationIntf = |
| 25 | sdbusplus::client::xyz::openbmc_project::association::Definitions<>; |
| Jagpal Singh Gill | e92aba4 | 2025-10-16 00:00:13 -0700 | [diff] [blame] | 26 | |
| Jagpal Singh Gill | e92aba4 | 2025-10-16 00:00:13 -0700 | [diff] [blame] | 27 | namespace ModbusIntf = phosphor::modbus::rtu; |
| 28 | namespace PortIntf = phosphor::modbus::rtu::port; |
| 29 | namespace PortConfigIntf = PortIntf::config; |
| 30 | namespace DeviceIntf = phosphor::modbus::rtu::device; |
| 31 | namespace DeviceConfigIntf = DeviceIntf::config; |
| Jagpal Singh Gill | 7184805 | 2025-10-16 00:28:58 -0700 | [diff] [blame] | 32 | namespace EventIntf = phosphor::modbus::events; |
| Jagpal Singh Gill | e92aba4 | 2025-10-16 00:00:13 -0700 | [diff] [blame] | 33 | |
| 34 | class MockPort : public PortIntf::BasePort |
| 35 | { |
| 36 | public: |
| 37 | MockPort(sdbusplus::async::context& ctx, |
| 38 | const PortConfigIntf::Config& config, |
| 39 | const std::string& devicePath) : BasePort(ctx, config, devicePath) |
| 40 | {} |
| 41 | }; |
| 42 | |
| Jagpal Singh Gill | 2fa10f4 | 2025-12-08 10:35:14 -0800 | [diff] [blame] | 43 | class SensorsTest : public BaseTest |
| Jagpal Singh Gill | e92aba4 | 2025-10-16 00:00:13 -0700 | [diff] [blame] | 44 | { |
| 45 | public: |
| Jagpal Singh Gill | 2fa10f4 | 2025-12-08 10:35:14 -0800 | [diff] [blame] | 46 | static constexpr auto clientDevicePath = "/tmp/ttySensorsTestPort0"; |
| 47 | static constexpr auto serverDevicePath = "/tmp/ttySensorsTestPort1"; |
| Jagpal Singh Gill | e92aba4 | 2025-10-16 00:00:13 -0700 | [diff] [blame] | 48 | static constexpr auto portName = "TestPort0"; |
| Jagpal Singh Gill | e92aba4 | 2025-10-16 00:00:13 -0700 | [diff] [blame] | 49 | static constexpr auto serviceName = |
| 50 | "xyz.openbmc_project.TestModbusRTUSensors"; |
| 51 | static constexpr auto sensorName = "OutletTemperature"; |
| Jagpal Singh Gill | e92aba4 | 2025-10-16 00:00:13 -0700 | [diff] [blame] | 52 | |
| Jagpal Singh Gill | 2fa10f4 | 2025-12-08 10:35:14 -0800 | [diff] [blame] | 53 | PortConfigIntf::Config portConfig; |
| 54 | std::string deviceName; |
| 55 | std::string fullSensorName; |
| 56 | std::string objectPath; |
| 57 | |
| 58 | SensorsTest() : BaseTest(clientDevicePath, serverDevicePath, serviceName) |
| Jagpal Singh Gill | e92aba4 | 2025-10-16 00:00:13 -0700 | [diff] [blame] | 59 | { |
| 60 | portConfig.name = portName; |
| 61 | portConfig.portMode = PortConfigIntf::PortMode::rs485; |
| 62 | portConfig.baudRate = baudRate; |
| 63 | portConfig.rtsDelay = 1; |
| 64 | |
| 65 | deviceName = std::format("ResorviorPumpUnit_{}_{}", |
| 66 | TestIntf::testDeviceAddress, portName); |
| 67 | |
| 68 | fullSensorName = std::format("{}_{}", deviceName, sensorName); |
| 69 | |
| 70 | objectPath = std::format( |
| 71 | "{}/{}/{}", SensorValueIntf::namespace_path::value, |
| 72 | SensorValueIntf::namespace_path::temperature, fullSensorName); |
| Jagpal Singh Gill | e92aba4 | 2025-10-16 00:00:13 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| Jagpal Singh Gill | 1f1d004 | 2025-11-17 13:15:33 -0800 | [diff] [blame] | 75 | auto checkInventoryAssociations() -> sdbusplus::async::task<void> |
| 76 | { |
| 77 | constexpr auto numOfInventoryAssociations = 2; |
| 78 | auto associationProperties = |
| 79 | co_await AssociationIntf(ctx) |
| 80 | .service(serviceName) |
| 81 | .path(objectPath) |
| 82 | .properties(); |
| 83 | EXPECT_EQ(associationProperties.associations.size(), |
| 84 | numOfInventoryAssociations); |
| 85 | } |
| 86 | |
| Jagpal Singh Gill | e92aba4 | 2025-10-16 00:00:13 -0700 | [diff] [blame] | 87 | auto testSensorCreation(std::string objectPath, |
| 88 | DeviceConfigIntf::SensorRegister sensorRegister, |
| 89 | double expectedValue) |
| 90 | -> sdbusplus::async::task<void> |
| 91 | { |
| 92 | DeviceConfigIntf::DeviceFactoryConfig deviceFactoryConfig = { |
| 93 | { |
| 94 | .address = TestIntf::testDeviceAddress, |
| 95 | .parity = ModbusIntf::Parity::none, |
| 96 | .baudRate = baudRate, |
| 97 | .name = deviceName, |
| 98 | .portName = portConfig.name, |
| 99 | .inventoryPath = sdbusplus::message::object_path( |
| 100 | "xyz/openbmc_project/Inventory/ResorviorPumpUnit"), |
| 101 | .sensorRegisters = {sensorRegister}, |
| 102 | .statusRegisters = {}, |
| 103 | .firmwareRegisters = {}, |
| 104 | }, |
| 105 | DeviceConfigIntf::DeviceType::reservoirPumpUnit, |
| 106 | DeviceConfigIntf::DeviceModel::RDF040DSS5193E0, |
| 107 | }; |
| Jagpal Singh Gill | 7184805 | 2025-10-16 00:28:58 -0700 | [diff] [blame] | 108 | EventIntf::Events events{ctx}; |
| Jagpal Singh Gill | 7184805 | 2025-10-16 00:28:58 -0700 | [diff] [blame] | 109 | MockPort mockPort(ctx, portConfig, clientDevicePath); |
| Jagpal Singh Gill | e92aba4 | 2025-10-16 00:00:13 -0700 | [diff] [blame] | 110 | auto device = DeviceIntf::DeviceFactory::create( |
| Jagpal Singh Gill | 7184805 | 2025-10-16 00:28:58 -0700 | [diff] [blame] | 111 | ctx, deviceFactoryConfig, mockPort, events); |
| Jagpal Singh Gill | e92aba4 | 2025-10-16 00:00:13 -0700 | [diff] [blame] | 112 | |
| 113 | co_await device->readSensorRegisters(); |
| 114 | |
| 115 | auto properties = co_await SensorValueIntf(ctx) |
| 116 | .service(serviceName) |
| 117 | .path(objectPath) |
| 118 | .properties(); |
| Jagpal Singh Gill | e92aba4 | 2025-10-16 00:00:13 -0700 | [diff] [blame] | 119 | EXPECT_EQ(properties.value, expectedValue) << "Sensor value mismatch"; |
| 120 | EXPECT_EQ(properties.unit, sensorRegister.unit) |
| 121 | << "Sensor unit mismatch"; |
| 122 | EXPECT_TRUE(std::isnan(properties.min_value)) << "Min value mismatch"; |
| 123 | EXPECT_TRUE(std::isnan(properties.max_value)) << "Max value mismatch"; |
| 124 | |
| Jagpal Singh Gill | 1f1d004 | 2025-11-17 13:15:33 -0800 | [diff] [blame] | 125 | auto operationalProperties = |
| 126 | co_await OperationalStatusIntf(ctx) |
| 127 | .service(serviceName) |
| 128 | .path(objectPath) |
| 129 | .properties(); |
| 130 | EXPECT_EQ(operationalProperties.functional, true) |
| 131 | << "Operational status mismatch"; |
| 132 | |
| 133 | auto availabilityProperties = |
| 134 | co_await AvailabilityIntf(ctx) |
| 135 | .service(serviceName) |
| 136 | .path(objectPath) |
| 137 | .properties(); |
| 138 | EXPECT_EQ(availabilityProperties.available, true) |
| 139 | << "Availability mismatch"; |
| 140 | |
| 141 | co_await checkInventoryAssociations(); |
| 142 | |
| Jagpal Singh Gill | e92aba4 | 2025-10-16 00:00:13 -0700 | [diff] [blame] | 143 | co_return; |
| 144 | } |
| Jagpal Singh Gill | e92aba4 | 2025-10-16 00:00:13 -0700 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | TEST_F(SensorsTest, TestSensorValueUnsigned) |
| 148 | { |
| 149 | const DeviceConfigIntf::SensorRegister sensorRegister = { |
| 150 | .name = sensorName, |
| 151 | .pathSuffix = SensorValueIntf::namespace_path::temperature, |
| 152 | .unit = SensorValueIntf::Unit::DegreesC, |
| 153 | .offset = TestIntf::testReadHoldingRegisterTempUnsignedOffset, |
| 154 | .size = TestIntf::testReadHoldingRegisterTempCount, |
| 155 | .format = DeviceConfigIntf::SensorFormat::floatingPoint, |
| 156 | }; |
| 157 | |
| 158 | ctx.spawn( |
| 159 | testSensorCreation(objectPath, sensorRegister, |
| 160 | TestIntf::testReadHoldingRegisterTempUnsigned[0])); |
| 161 | |
| 162 | ctx.spawn(sdbusplus::async::sleep_for(ctx, 1s) | |
| 163 | sdbusplus::async::execution::then([&]() { ctx.request_stop(); })); |
| 164 | |
| 165 | ctx.run(); |
| 166 | } |
| 167 | |
| 168 | TEST_F(SensorsTest, TestSensorValueSigned) |
| 169 | { |
| 170 | const DeviceConfigIntf::SensorRegister sensorRegister = { |
| 171 | .name = sensorName, |
| 172 | .pathSuffix = SensorValueIntf::namespace_path::temperature, |
| 173 | .unit = SensorValueIntf::Unit::DegreesC, |
| 174 | .offset = TestIntf::testReadHoldingRegisterTempSignedOffset, |
| 175 | .size = TestIntf::testReadHoldingRegisterTempCount, |
| 176 | .isSigned = true, |
| 177 | .format = DeviceConfigIntf::SensorFormat::floatingPoint, |
| 178 | }; |
| 179 | |
| 180 | // Convert expected hex value to a signed 16-bit integer for comparison |
| 181 | const int16_t expectedSigned = |
| 182 | static_cast<int16_t>(TestIntf::testReadHoldingRegisterTempSigned[0]); |
| 183 | |
| 184 | ctx.spawn(testSensorCreation(objectPath, sensorRegister, expectedSigned)); |
| 185 | |
| 186 | ctx.spawn(sdbusplus::async::sleep_for(ctx, 1s) | |
| 187 | sdbusplus::async::execution::then([&]() { ctx.request_stop(); })); |
| 188 | |
| 189 | ctx.run(); |
| 190 | } |
| 191 | |
| 192 | static auto applyValueSettings(double value, double shift, double scale, |
| 193 | uint8_t precision) |
| 194 | { |
| 195 | return (shift + (scale * (value / (1ULL << precision)))); |
| 196 | } |
| 197 | |
| 198 | TEST_F(SensorsTest, TestSensorValueWithSettings) |
| 199 | { |
| 200 | const DeviceConfigIntf::SensorRegister sensorRegister = { |
| 201 | .name = sensorName, |
| 202 | .pathSuffix = SensorValueIntf::namespace_path::temperature, |
| 203 | .unit = SensorValueIntf::Unit::DegreesC, |
| 204 | .offset = TestIntf::testReadHoldingRegisterTempUnsignedOffset, |
| 205 | .size = TestIntf::testReadHoldingRegisterTempCount, |
| 206 | .precision = 2, |
| 207 | .scale = 0.1, |
| 208 | .shift = 50, |
| 209 | .format = DeviceConfigIntf::SensorFormat::floatingPoint, |
| 210 | }; |
| 211 | |
| 212 | ctx.spawn(testSensorCreation( |
| 213 | objectPath, sensorRegister, |
| 214 | applyValueSettings(TestIntf::testReadHoldingRegisterTempUnsigned[0], |
| 215 | sensorRegister.shift, sensorRegister.scale, |
| 216 | sensorRegister.precision))); |
| 217 | |
| 218 | ctx.spawn(sdbusplus::async::sleep_for(ctx, 1s) | |
| 219 | sdbusplus::async::execution::then([&]() { ctx.request_stop(); })); |
| 220 | |
| 221 | ctx.run(); |
| 222 | } |