regulators: Add monitorSensors support to Chassis

Implemented the monitorSensors() method in the Chassis class.  This method
reads the sensors for the voltage rails produced by the chassis.

Signed-off-by: Bob King <Bob_King@wistron.com>
Change-Id: Idb283b09c645983190601e030334399ee76c1995
diff --git a/phosphor-regulators/src/chassis.cpp b/phosphor-regulators/src/chassis.cpp
index a0d0c2b..29ebb79 100644
--- a/phosphor-regulators/src/chassis.cpp
+++ b/phosphor-regulators/src/chassis.cpp
@@ -55,4 +55,13 @@
     }
 }
 
+void Chassis::monitorSensors(System& system)
+{
+    // Monitor sensors in each device
+    for (std::unique_ptr<Device>& device : devices)
+    {
+        device->monitorSensors(system, *this);
+    }
+}
+
 } // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/src/chassis.hpp b/phosphor-regulators/src/chassis.hpp
index 3fd3792..4423926 100644
--- a/phosphor-regulators/src/chassis.hpp
+++ b/phosphor-regulators/src/chassis.hpp
@@ -122,6 +122,16 @@
         return number;
     }
 
+    /**
+     * Monitors the sensors for the voltage rails produced by this chassis, if
+     * any.
+     *
+     * This method should be called once per second.
+     *
+     * @param system system that contains the chassis
+     */
+    void monitorSensors(System& system);
+
   private:
     /**
      * Chassis number within the system.
diff --git a/phosphor-regulators/test/chassis_tests.cpp b/phosphor-regulators/test/chassis_tests.cpp
index 5f8266e..eca6d97 100644
--- a/phosphor-regulators/test/chassis_tests.cpp
+++ b/phosphor-regulators/test/chassis_tests.cpp
@@ -22,6 +22,7 @@
 #include "journal.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"
@@ -34,12 +35,15 @@
 #include <utility>
 #include <vector>
 
+#include <gmock/gmock.h>
 #include <gtest/gtest.h>
 
 using namespace phosphor::power::regulators;
 using namespace phosphor::power::regulators::test_utils;
 
+using ::testing::A;
 using ::testing::Return;
+using ::testing::TypedEq;
 
 TEST(ChassisTests, Constructor)
 {
@@ -287,3 +291,88 @@
     Chassis chassis{3};
     EXPECT_EQ(chassis.getNumber(), 3);
 }
+
+TEST(ChassisTests, MonitorSensors)
+{
+    // Test where no devices were specified in constructor
+    {
+        // Create Chassis
+        std::vector<std::unique_ptr<Device>> devices{};
+        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();
+        chassisPtr->monitorSensors(system);
+        EXPECT_EQ(journal::getDebugMessages().size(), 0);
+        EXPECT_EQ(journal::getErrMessages().size(), 0);
+    }
+
+    // Test where devices were specified in constructor
+    {
+        std::vector<std::unique_ptr<Device>> devices{};
+
+        // 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::vector<std::unique_ptr<Rail>> rails{};
+        std::unique_ptr<Configuration> configuration{};
+        std::unique_ptr<Rail> rail = std::make_unique<Rail>(
+            "vdd0", std::move(configuration), std::move(sensorMonitoring));
+        rails.emplace_back(std::move(rail));
+
+        // Create Device
+        std::unique_ptr<PresenceDetection> presenceDetection{};
+        std::unique_ptr<Configuration> deviceConfiguration{};
+        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));
+
+        // Create Chassis
+        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();
+        chassisPtr->monitorSensors(system);
+        EXPECT_EQ(journal::getDebugMessages().size(), 0);
+        EXPECT_EQ(journal::getErrMessages().size(), 0);
+    }
+}