regulators: Add monitorSensors support to Rail

Implemented the monitorSensors() method in the Rail class.  This method
reads the sensors for a voltage rail.

Signed-off-by: Bob King <Bob_King@wistron.com>
Change-Id: Ifbe6461349d20fee1eba233844c95c1046e1a54a
diff --git a/phosphor-regulators/src/rail.cpp b/phosphor-regulators/src/rail.cpp
index e95b396..7d90e77 100644
--- a/phosphor-regulators/src/rail.cpp
+++ b/phosphor-regulators/src/rail.cpp
@@ -32,4 +32,13 @@
     }
 }
 
+void Rail::monitorSensors(System& system, Chassis& chassis, Device& device)
+{
+    // If sensor monitoring is defined for this rail, read the sensors.
+    if (sensorMonitoring)
+    {
+        sensorMonitoring->execute(system, chassis, device, *this);
+    }
+}
+
 } // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/src/rail.hpp b/phosphor-regulators/src/rail.hpp
index 9e0ce3d..8340d07 100644
--- a/phosphor-regulators/src/rail.hpp
+++ b/phosphor-regulators/src/rail.hpp
@@ -102,6 +102,20 @@
     }
 
     /**
+     * Monitor the sensors for this rail.
+     *
+     * Sensor monitoring is optional.  If sensor monitoring is defined for this
+     * rail, the sensor values are read.
+     *
+     * This method should be called once per second.
+     *
+     * @param system system that contains the chassis
+     * @param chassis chassis that contains the device
+     * @param device device that contains this rail
+     */
+    void monitorSensors(System& system, Chassis& chassis, Device& device);
+
+    /**
      * Returns the sensor monitoring for this rail, if any.
      *
      * @return Pointer to SensorMonitoring object.  Will equal nullptr if no
diff --git a/phosphor-regulators/test/rail_tests.cpp b/phosphor-regulators/test/rail_tests.cpp
index b0a3466..e3c1c6a 100644
--- a/phosphor-regulators/test/rail_tests.cpp
+++ b/phosphor-regulators/test/rail_tests.cpp
@@ -22,6 +22,7 @@
 #include "mock_action.hpp"
 #include "mock_journal.hpp"
 #include "mocked_i2c_interface.hpp"
+#include "pmbus_read_sensor_action.hpp"
 #include "presence_detection.hpp"
 #include "rail.hpp"
 #include "rule.hpp"
@@ -38,7 +39,9 @@
 
 using namespace phosphor::power::regulators;
 
+using ::testing::A;
 using ::testing::Return;
+using ::testing::TypedEq;
 
 TEST(RailTests, Constructor)
 {
@@ -205,6 +208,113 @@
     EXPECT_EQ(rail.getID(), "vio2");
 }
 
+TEST(RailTests, MonitorSensors)
+{
+    // Test where SensorMonitoring was not specified in constructor
+    {
+        // Create mock I2CInterface.  A two-byte read should NOT occur.
+        std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
+            std::make_unique<i2c::MockedI2CInterface>();
+        EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint16_t&>())).Times(0);
+
+        // Create Rail
+        std::unique_ptr<Rail> rail = std::make_unique<Rail>("vdd0");
+        Rail* railPtr = rail.get();
+
+        // Create Device that contains Rail
+        std::unique_ptr<PresenceDetection> presenceDetection{};
+        std::unique_ptr<Configuration> deviceConfiguration{};
+        std::vector<std::unique_ptr<Rail>> rails{};
+        rails.emplace_back(std::move(rail));
+        std::unique_ptr<Device> device = std::make_unique<Device>(
+            "reg1", true, "/system/chassis/motherboard/reg1",
+            std::move(i2cInterface), std::move(presenceDetection),
+            std::move(deviceConfiguration), std::move(rails));
+        Device* devicePtr = device.get();
+
+        // Create Chassis that contains Device
+        std::vector<std::unique_ptr<Device>> devices{};
+        devices.emplace_back(std::move(device));
+        std::unique_ptr<Chassis> chassis =
+            std::make_unique<Chassis>(1, std::move(devices));
+        Chassis* chassisPtr = chassis.get();
+
+        // Create System that contains Chassis
+        std::vector<std::unique_ptr<Rule>> rules{};
+        std::vector<std::unique_ptr<Chassis>> chassisVec{};
+        chassisVec.emplace_back(std::move(chassis));
+        System system{std::move(rules), std::move(chassisVec)};
+
+        // Call monitorSensors().  Should do nothing.
+        journal::clear();
+        railPtr->monitorSensors(system, *chassisPtr, *devicePtr);
+        EXPECT_EQ(journal::getDebugMessages().size(), 0);
+        EXPECT_EQ(journal::getErrMessages().size(), 0);
+    }
+
+    // Test where SensorMonitoring was specified in constructor
+    {
+        // Create PMBusReadSensorAction
+        pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
+        uint8_t command = 0x8C;
+        pmbus_utils::SensorDataFormat format{
+            pmbus_utils::SensorDataFormat::linear_11};
+        std::optional<int8_t> exponent{};
+        std::unique_ptr<PMBusReadSensorAction> action =
+            std::make_unique<PMBusReadSensorAction>(type, command, format,
+                                                    exponent);
+
+        // Create mock I2CInterface.  A two-byte read should occur.
+        std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
+            std::make_unique<i2c::MockedI2CInterface>();
+        EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
+        EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x8C), A<uint16_t&>()))
+            .Times(1);
+
+        // Create SensorMonitoring
+        std::vector<std::unique_ptr<Action>> actions{};
+        actions.emplace_back(std::move(action));
+        std::unique_ptr<SensorMonitoring> sensorMonitoring =
+            std::make_unique<SensorMonitoring>(std::move(actions));
+
+        // Create Rail
+        std::unique_ptr<Configuration> configuration{};
+        std::unique_ptr<Rail> rail = std::make_unique<Rail>(
+            "vddr1", std::move(configuration), std::move(sensorMonitoring));
+        Rail* railPtr = rail.get();
+
+        // Create Device that contains Rail
+        std::unique_ptr<PresenceDetection> presenceDetection{};
+        std::unique_ptr<Configuration> deviceConfiguration{};
+        std::vector<std::unique_ptr<Rail>> rails{};
+        rails.emplace_back(std::move(rail));
+        std::unique_ptr<Device> device = std::make_unique<Device>(
+            "reg1", true, "/system/chassis/motherboard/reg1",
+            std::move(i2cInterface), std::move(presenceDetection),
+            std::move(deviceConfiguration), std::move(rails));
+        Device* devicePtr = device.get();
+
+        // Create Chassis that contains Device
+        std::vector<std::unique_ptr<Device>> devices{};
+        devices.emplace_back(std::move(device));
+        std::unique_ptr<Chassis> chassis =
+            std::make_unique<Chassis>(1, std::move(devices));
+        Chassis* chassisPtr = chassis.get();
+
+        // Create System that contains Chassis
+        std::vector<std::unique_ptr<Rule>> rules{};
+        std::vector<std::unique_ptr<Chassis>> chassisVec{};
+        chassisVec.emplace_back(std::move(chassis));
+        System system{std::move(rules), std::move(chassisVec)};
+
+        // Call monitorSensors().
+        journal::clear();
+        railPtr->monitorSensors(system, *chassisPtr, *devicePtr);
+        EXPECT_EQ(journal::getDebugMessages().size(), 0);
+        EXPECT_EQ(journal::getErrMessages().size(), 0);
+    }
+}
+
 TEST(RailTests, GetSensorMonitoring)
 {
     // Test where SensorMonitoring was not specified in constructor