regulators: Add PMBusReadSensorAction::execute()

Implementation and Testing for PMBusReadSensorAction::execute().

Signed-off-by: Bob King <Bob_King@wistron.com>
Change-Id: I350eccfd6daaf957865329756cdfb3be48f49377
diff --git a/phosphor-regulators/src/actions/pmbus_read_sensor_action.cpp b/phosphor-regulators/src/actions/pmbus_read_sensor_action.cpp
new file mode 100644
index 0000000..4cc6a69
--- /dev/null
+++ b/phosphor-regulators/src/actions/pmbus_read_sensor_action.cpp
@@ -0,0 +1,122 @@
+/**
+ * Copyright © 2020 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "pmbus_read_sensor_action.hpp"
+
+#include "action_error.hpp"
+#include "pmbus_error.hpp"
+
+#include <exception>
+#include <ios>
+#include <sstream>
+
+namespace phosphor::power::regulators
+{
+
+bool PMBusReadSensorAction::execute(ActionEnvironment& environment)
+{
+    try
+    {
+        pmbus_utils::SensorReading reading{};
+        reading.type = type;
+
+        // Get I2C interface to current device
+        i2c::I2CInterface& interface = getI2CInterface(environment);
+
+        // Read value of the PMBus command code
+        uint16_t value;
+        interface.read(command, value);
+
+        switch (format)
+        {
+            case pmbus_utils::SensorDataFormat::linear_11:
+                // Convert linear_11 format to a normal decimal number
+                reading.value = pmbus_utils::convertFromLinear(value);
+                break;
+            case pmbus_utils::SensorDataFormat::linear_16:
+                // Get exponent value for converting linear format to volts
+                // value
+                int8_t exponentValue = getExponentValue(interface);
+
+                // Convert linear_16 format to a normal decimal number
+                reading.value =
+                    pmbus_utils::convertFromVoutLinear(value, exponentValue);
+                break;
+        }
+
+        environment.addSensorReading(reading);
+    }
+    // Nest the following exception types within an ActionError so the caller
+    // will have both the low level error information and the action information
+    catch (const i2c::I2CException& i2cError)
+    {
+        std::throw_with_nested(ActionError(*this));
+    }
+    catch (const PMBusError& pmbusError)
+    {
+        std::throw_with_nested(ActionError(*this));
+    }
+    return true;
+}
+
+std::string PMBusReadSensorAction::toString() const
+{
+    std::ostringstream ss;
+    ss << "pmbus_read_sensor: { ";
+    ss << "type: " << pmbus_utils::toString(type) << ", " << std::hex
+       << std::uppercase;
+    ss << "command: 0x" << static_cast<uint16_t>(command) << ", " << std::dec
+       << std::nouppercase;
+    ss << "format: " << pmbus_utils::toString(format);
+
+    if (exponent.has_value())
+    {
+        ss << ", exponent: " << static_cast<int16_t>(exponent.value());
+    }
+
+    ss << " }";
+
+    return ss.str();
+}
+
+int8_t PMBusReadSensorAction::getExponentValue(i2c::I2CInterface& interface)
+{
+    // Check if an exponent value is defined for this action
+    if (exponent.has_value())
+    {
+        return exponent.value();
+    }
+
+    // Read value of the VOUT_MODE command
+    uint8_t voutModeValue;
+    interface.read(pmbus_utils::VOUT_MODE, voutModeValue);
+
+    // Parse VOUT_MODE value to get data format and parameter value
+    pmbus_utils::VoutDataFormat format;
+    int8_t parameter;
+    pmbus_utils::parseVoutMode(voutModeValue, format, parameter);
+
+    // Verify format is linear; other formats not currently supported
+    if (format != pmbus_utils::VoutDataFormat::linear)
+    {
+        throw PMBusError("VOUT_MODE contains unsupported data format");
+    }
+
+    // Return parameter value; it contains the exponent when format is linear
+    return parameter;
+}
+
+} // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/src/actions/pmbus_read_sensor_action.hpp b/phosphor-regulators/src/actions/pmbus_read_sensor_action.hpp
index 8737eba..c39fd8a 100644
--- a/phosphor-regulators/src/actions/pmbus_read_sensor_action.hpp
+++ b/phosphor-regulators/src/actions/pmbus_read_sensor_action.hpp
@@ -22,7 +22,6 @@
 
 #include <cstdint>
 #include <optional>
-#include <stdexcept>
 #include <string>
 
 namespace phosphor::power::regulators
@@ -78,16 +77,29 @@
     /**
      * Executes this action.
      *
-     * TODO: Not implemented yet
+     * Reads one sensor using the I2C interface.
+     *
+     * The sensor value type is specified in the constructor.
+     *
+     * The PMBus command code is specified in the constructor.
+     * It is the register to read on the device.
+     *
+     * The sensor data format is specified in the constructor. Currently
+     * the linear_11 and linear_16 formats are supported.
+     *
+     * The linear_16 data format requires an exponent value.
+     * If an exponent value was specified in the constructor, that
+     * value will be used.  Otherwise the exponent value will be obtained from
+     * the VOUT_MODE command.
+     *
+     * The device is obtained from the ActionEnvironment.
+     *
+     * Throws an exception if an error occurs.
      *
      * @param environment Action execution environment.
      * @return true
      */
-    virtual bool execute(ActionEnvironment& /* environment */) override
-    {
-        // TODO: Not implemented yet
-        return true;
-    }
+    virtual bool execute(ActionEnvironment& environment) override;
 
     /**
      * Returns the PMBus command code.
@@ -134,28 +146,25 @@
      *
      * @return description of action
      */
-    virtual std::string toString() const override
-    {
-        std::ostringstream ss;
-        ss << "pmbus_read_sensor: { ";
-        ss << "type: " << pmbus_utils::toString(type) << ", " << std::hex
-           << std::uppercase;
-        ss << "command: 0x" << static_cast<uint16_t>(command) << ", "
-           << std::dec << std::nouppercase;
-        ss << "format: " << pmbus_utils::toString(format);
-
-        if (exponent.has_value())
-        {
-            ss << ", exponent: " << static_cast<int16_t>(exponent.value());
-        }
-
-        ss << " }";
-
-        return ss.str();
-    }
+    virtual std::string toString() const override;
 
   private:
     /**
+     * Gets the exponent value to use to convert a linear_16 format value to a
+     * decimal volts value.
+     *
+     * If an exponent value is defined for this action, that value is returned.
+     * Otherwise VOUT_MODE is read from the current device to obtain the
+     * exponent value.
+     *
+     * Throws an exception if an error occurs.
+     *
+     * @param interface I2C interface to the current device
+     * @return exponent value
+     */
+    int8_t getExponentValue(i2c::I2CInterface& interface);
+
+    /**
      * Sensor value type.
      */
     const pmbus_utils::SensorValueType type{};
diff --git a/phosphor-regulators/src/meson.build b/phosphor-regulators/src/meson.build
index 1a1602f..4adeb58 100644
--- a/phosphor-regulators/src/meson.build
+++ b/phosphor-regulators/src/meson.build
@@ -22,6 +22,7 @@
     'actions/i2c_write_bit_action.cpp',
     'actions/i2c_write_byte_action.cpp',
     'actions/i2c_write_bytes_action.cpp',
+    'actions/pmbus_read_sensor_action.cpp',
     'actions/pmbus_write_vout_command_action.cpp'
 ]