regulators: ActionEnvironment enhancements

Define a new struct named SensorReading.  This will be used the store the
value read when PMBusReadSensorAction::execute() is called.

Add a new data member sensorReadings.
This will hold the sensor readings that are obtained

Add a new method addSensorReading.
It add the specified SensorReading to the sensorReadings vector.

Add a new method getSensorReadings.
This will be called by the SensorMonitoring object.

Signed-off-by: Bob King <Bob_King@wistron.com>
Change-Id: I91076227498712750187d354b76faa2d95e33aac
diff --git a/phosphor-regulators/src/actions/action_environment.hpp b/phosphor-regulators/src/actions/action_environment.hpp
index f94345a..ce06aff 100644
--- a/phosphor-regulators/src/actions/action_environment.hpp
+++ b/phosphor-regulators/src/actions/action_environment.hpp
@@ -16,11 +16,13 @@
 #pragma once
 
 #include "id_map.hpp"
+#include "pmbus_utils.hpp"
 
 #include <cstddef> // for size_t
 #include <optional>
 #include <stdexcept>
 #include <string>
+#include <vector>
 
 namespace phosphor::power::regulators
 {
@@ -39,6 +41,7 @@
  *   - current volts value (if any)
  *   - mapping from device and rule IDs to the corresponding objects
  *   - rule call stack depth (to detect infinite recursion)
+ *   - sensor readings
  */
 class ActionEnvironment
 {
@@ -70,6 +73,16 @@
     }
 
     /**
+     * Adds the specified sensor reading to this action environment.
+     *
+     * @param reading sensor reading from a regulator rail
+     */
+    void addSensorReading(const pmbus_utils::SensorReading& reading)
+    {
+        sensorReadings.emplace_back(reading);
+    }
+
+    /**
      * Decrements the rule call stack depth by one.
      *
      * Should be used when a call to a rule returns.  Does nothing if depth is
@@ -131,6 +144,16 @@
     }
 
     /**
+     * Returns the sensor readings stored in this action environment.
+     *
+     * @return sensor readings
+     */
+    const std::vector<pmbus_utils::SensorReading>& getSensorReadings() const
+    {
+        return sensorReadings;
+    }
+
+    /**
      * Returns the current volts value, if set.
      *
      * @return current volts value
@@ -201,6 +224,11 @@
      * Rule call stack depth.
      */
     size_t ruleDepth{0};
+
+    /**
+     * Sensor readings for a single regulator rail.
+     */
+    std::vector<pmbus_utils::SensorReading> sensorReadings{};
 };
 
 } // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/src/pmbus_utils.hpp b/phosphor-regulators/src/pmbus_utils.hpp
index 438ee46..4d4b047 100644
--- a/phosphor-regulators/src/pmbus_utils.hpp
+++ b/phosphor-regulators/src/pmbus_utils.hpp
@@ -142,6 +142,22 @@
 };
 
 /**
+ * This struct represents one sensor reading from a device.
+ */
+struct SensorReading
+{
+    /**
+     * Sensor value type that was read.
+     */
+    SensorValueType type;
+
+    /**
+     * Sensor value that was read.
+     */
+    double value;
+};
+
+/**
  * Parse the one byte value of the VOUT_MODE command.
  *
  * VOUT_MODE contains a 'mode' field that indicates the data format used for
diff --git a/phosphor-regulators/test/actions/action_environment_tests.cpp b/phosphor-regulators/test/actions/action_environment_tests.cpp
index efed4d3..4100078 100644
--- a/phosphor-regulators/test/actions/action_environment_tests.cpp
+++ b/phosphor-regulators/test/actions/action_environment_tests.cpp
@@ -18,6 +18,7 @@
 #include "i2c_interface.hpp"
 #include "id_map.hpp"
 #include "mocked_i2c_interface.hpp"
+#include "pmbus_utils.hpp"
 #include "rule.hpp"
 
 #include <cstddef> // for size_t
@@ -58,6 +59,28 @@
     }
 }
 
+TEST(ActionEnvironmentTests, AddSensorReading)
+{
+    IDMap idMap{};
+    ActionEnvironment env{idMap, ""};
+    pmbus_utils::SensorReading reading;
+    reading.type = pmbus_utils::SensorValueType::iout;
+    reading.value = 1;
+    EXPECT_EQ(env.getSensorReadings().size(), 0);
+    env.addSensorReading(reading);
+    EXPECT_EQ(env.getSensorReadings().size(), 1);
+    EXPECT_EQ(env.getSensorReadings()[0].type,
+              pmbus_utils::SensorValueType::iout);
+    EXPECT_EQ(env.getSensorReadings()[0].value, 1);
+    reading.type = pmbus_utils::SensorValueType::vout;
+    reading.value = 2;
+    env.addSensorReading(reading);
+    EXPECT_EQ(env.getSensorReadings().size(), 2);
+    EXPECT_EQ(env.getSensorReadings()[1].type,
+              pmbus_utils::SensorValueType::vout);
+    EXPECT_EQ(env.getSensorReadings()[1].value, 2);
+}
+
 TEST(ActionEnvironmentTests, DecrementRuleDepth)
 {
     IDMap idMap{};
@@ -181,6 +204,28 @@
     EXPECT_EQ(env.getRuleDepth(), 0);
 }
 
+TEST(ActionEnvironmentTests, GetSensorReadings)
+{
+    IDMap idMap{};
+    ActionEnvironment env{idMap, ""};
+    pmbus_utils::SensorReading reading;
+    reading.type = pmbus_utils::SensorValueType::pout;
+    reading.value = 1.3;
+    EXPECT_EQ(env.getSensorReadings().size(), 0);
+    env.addSensorReading(reading);
+    EXPECT_EQ(env.getSensorReadings().size(), 1);
+    EXPECT_EQ(env.getSensorReadings()[0].type,
+              pmbus_utils::SensorValueType::pout);
+    EXPECT_EQ(env.getSensorReadings()[0].value, 1.3);
+    reading.type = pmbus_utils::SensorValueType::temperature;
+    reading.value = -1;
+    env.addSensorReading(reading);
+    EXPECT_EQ(env.getSensorReadings().size(), 2);
+    EXPECT_EQ(env.getSensorReadings()[1].type,
+              pmbus_utils::SensorValueType::temperature);
+    EXPECT_EQ(env.getSensorReadings()[1].value, -1);
+}
+
 TEST(ActionEnvironmentTests, GetVolts)
 {
     IDMap idMap{};