regulators: Add more info to PMBusError

Add more information to the PMBusError exception:
* Unique ID of the device where the error occurred
* Inventory path of the device where the error occurred

This information is required in order to create an error log entry based
on the exception.

Modify files that currently use PMBusError to adapt to the new
constructor and get methods.

Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: Iadae82468df3ee46bdd5445f130c43502d454e96
diff --git a/phosphor-regulators/src/actions/pmbus_read_sensor_action.cpp b/phosphor-regulators/src/actions/pmbus_read_sensor_action.cpp
index 4cc6a69..f8a15d7 100644
--- a/phosphor-regulators/src/actions/pmbus_read_sensor_action.cpp
+++ b/phosphor-regulators/src/actions/pmbus_read_sensor_action.cpp
@@ -48,8 +48,7 @@
                 break;
             case pmbus_utils::SensorDataFormat::linear_16:
                 // Get exponent value for converting linear format to volts
-                // value
-                int8_t exponentValue = getExponentValue(interface);
+                int8_t exponentValue = getExponentValue(environment, interface);
 
                 // Convert linear_16 format to a normal decimal number
                 reading.value =
@@ -92,7 +91,8 @@
     return ss.str();
 }
 
-int8_t PMBusReadSensorAction::getExponentValue(i2c::I2CInterface& interface)
+int8_t PMBusReadSensorAction::getExponentValue(ActionEnvironment& environment,
+                                               i2c::I2CInterface& interface)
 {
     // Check if an exponent value is defined for this action
     if (exponent.has_value())
@@ -112,7 +112,9 @@
     // Verify format is linear; other formats not currently supported
     if (format != pmbus_utils::VoutDataFormat::linear)
     {
-        throw PMBusError("VOUT_MODE contains unsupported data format");
+        throw PMBusError("VOUT_MODE contains unsupported data format",
+                         environment.getDeviceID(),
+                         environment.getDevice().getFRU());
     }
 
     // Return parameter value; it contains the exponent when format is linear
diff --git a/phosphor-regulators/src/actions/pmbus_read_sensor_action.hpp b/phosphor-regulators/src/actions/pmbus_read_sensor_action.hpp
index c39fd8a..620a814 100644
--- a/phosphor-regulators/src/actions/pmbus_read_sensor_action.hpp
+++ b/phosphor-regulators/src/actions/pmbus_read_sensor_action.hpp
@@ -159,10 +159,12 @@
      *
      * Throws an exception if an error occurs.
      *
+     * @param environment action execution environment
      * @param interface I2C interface to the current device
      * @return exponent value
      */
-    int8_t getExponentValue(i2c::I2CInterface& interface);
+    int8_t getExponentValue(ActionEnvironment& environment,
+                            i2c::I2CInterface& interface);
 
     /**
      * Sensor value type.
diff --git a/phosphor-regulators/src/actions/pmbus_write_vout_command_action.cpp b/phosphor-regulators/src/actions/pmbus_write_vout_command_action.cpp
index 2a245a9..9e02746 100644
--- a/phosphor-regulators/src/actions/pmbus_write_vout_command_action.cpp
+++ b/phosphor-regulators/src/actions/pmbus_write_vout_command_action.cpp
@@ -38,7 +38,7 @@
         i2c::I2CInterface& interface = getI2CInterface(environment);
 
         // Get exponent value for converting volts value to linear format
-        int8_t exponentValue = getExponentValue(interface);
+        int8_t exponentValue = getExponentValue(environment, interface);
 
         // Convert volts value to linear data format
         uint16_t linearValue =
@@ -92,8 +92,8 @@
     return ss.str();
 }
 
-int8_t
-    PMBusWriteVoutCommandAction::getExponentValue(i2c::I2CInterface& interface)
+int8_t PMBusWriteVoutCommandAction::getExponentValue(
+    ActionEnvironment& environment, i2c::I2CInterface& interface)
 {
     // Check if an exponent value is defined for this action
     if (exponent.has_value())
@@ -113,7 +113,9 @@
     // Verify format is linear; other formats not currently supported
     if (format != pmbus_utils::VoutDataFormat::linear)
     {
-        throw PMBusError("VOUT_MODE contains unsupported data format");
+        throw PMBusError("VOUT_MODE contains unsupported data format",
+                         environment.getDeviceID(),
+                         environment.getDevice().getFRU());
     }
 
     // Return parameter value; it contains the exponent when format is linear
diff --git a/phosphor-regulators/src/actions/pmbus_write_vout_command_action.hpp b/phosphor-regulators/src/actions/pmbus_write_vout_command_action.hpp
index 4ed1e10..e77ced1 100644
--- a/phosphor-regulators/src/actions/pmbus_write_vout_command_action.hpp
+++ b/phosphor-regulators/src/actions/pmbus_write_vout_command_action.hpp
@@ -189,10 +189,12 @@
      *
      * Throws an exception if an error occurs.
      *
+     * @param environment action execution environment
      * @param interface I2C interface to the current device
      * @return exponent value
      */
-    int8_t getExponentValue(i2c::I2CInterface& interface);
+    int8_t getExponentValue(ActionEnvironment& environment,
+                            i2c::I2CInterface& interface);
 
     /**
      * Gets the volts value to write to VOUT_COMMAND.
diff --git a/phosphor-regulators/src/pmbus_error.hpp b/phosphor-regulators/src/pmbus_error.hpp
index b589526..1050af3 100644
--- a/phosphor-regulators/src/pmbus_error.hpp
+++ b/phosphor-regulators/src/pmbus_error.hpp
@@ -42,13 +42,37 @@
      * Constructor.
      *
      * @param error error message
+     * @param deviceID unique ID of the device where error occurred
+     * @param inventoryPath inventory path of the device where error occurred
      */
-    explicit PMBusError(const std::string& error) :
-        error{"PMBusError: " + error}
+    explicit PMBusError(const std::string& error, const std::string& deviceID,
+                        const std::string& inventoryPath) :
+        error{"PMBusError: " + error},
+        deviceID{deviceID}, inventoryPath{inventoryPath}
     {
     }
 
     /**
+     * Returns the unique ID of the device where the error occurred.
+     *
+     * @return device ID
+     */
+    const std::string& getDeviceID() const
+    {
+        return deviceID;
+    }
+
+    /**
+     * Returns the inventory path of the device where the error occurred.
+     *
+     * @return inventory path
+     */
+    const std::string& getInventoryPath() const
+    {
+        return inventoryPath;
+    }
+
+    /**
      * Returns the description of this error.
      *
      * @return error description
@@ -63,6 +87,16 @@
      * Error message.
      */
     const std::string error{};
+
+    /**
+     * Unique ID of the device where the error occurred.
+     */
+    const std::string deviceID{};
+
+    /**
+     * Inventory path of the device where the error occurred.
+     */
+    const std::string inventoryPath{};
 };
 
 } // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/test/actions/pmbus_read_sensor_action_tests.cpp b/phosphor-regulators/test/actions/pmbus_read_sensor_action_tests.cpp
index f907e79..d0f1398 100644
--- a/phosphor-regulators/test/actions/pmbus_read_sensor_action_tests.cpp
+++ b/phosphor-regulators/test/actions/pmbus_read_sensor_action_tests.cpp
@@ -299,6 +299,9 @@
             EXPECT_STREQ(
                 pe.what(),
                 "PMBusError: VOUT_MODE contains unsupported data format");
+            EXPECT_EQ(pe.getDeviceID(), "reg1");
+            EXPECT_EQ(pe.getInventoryPath(), "/xyz/openbmc_project/inventory/"
+                                             "system/chassis/motherboard/reg1");
         }
         catch (...)
         {
diff --git a/phosphor-regulators/test/actions/pmbus_write_vout_command_action_tests.cpp b/phosphor-regulators/test/actions/pmbus_write_vout_command_action_tests.cpp
index d515cad..825b5ec 100644
--- a/phosphor-regulators/test/actions/pmbus_write_vout_command_action_tests.cpp
+++ b/phosphor-regulators/test/actions/pmbus_write_vout_command_action_tests.cpp
@@ -358,6 +358,9 @@
             EXPECT_STREQ(
                 pe.what(),
                 "PMBusError: VOUT_MODE contains unsupported data format");
+            EXPECT_EQ(pe.getDeviceID(), "reg1");
+            EXPECT_EQ(pe.getInventoryPath(), "/xyz/openbmc_project/inventory/"
+                                             "system/chassis/motherboard/reg1");
         }
         catch (...)
         {
diff --git a/phosphor-regulators/test/pmbus_error_tests.cpp b/phosphor-regulators/test/pmbus_error_tests.cpp
index 670df71..3e455ba 100644
--- a/phosphor-regulators/test/pmbus_error_tests.cpp
+++ b/phosphor-regulators/test/pmbus_error_tests.cpp
@@ -21,14 +21,38 @@
 
 TEST(PMBusErrorTests, Constructor)
 {
-    PMBusError error("VOUT_MODE contains unsupported data format");
+    PMBusError error(
+        "VOUT_MODE contains unsupported data format", "vdd_reg",
+        "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2");
+    EXPECT_EQ(error.getDeviceID(), "vdd_reg");
+    EXPECT_EQ(error.getInventoryPath(),
+              "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2");
     EXPECT_STREQ(error.what(),
                  "PMBusError: VOUT_MODE contains unsupported data format");
 }
 
+TEST(PMBusErrorTests, GetDeviceID)
+{
+    PMBusError error(
+        "VOUT_MODE contains unsupported data format", "vdd_reg",
+        "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2");
+    EXPECT_EQ(error.getDeviceID(), "vdd_reg");
+}
+
+TEST(PMBusErrorTests, GetInventoryPath)
+{
+    PMBusError error(
+        "VOUT_MODE contains unsupported data format", "vdd_reg",
+        "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2");
+    EXPECT_EQ(error.getInventoryPath(),
+              "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2");
+}
+
 TEST(PMBusErrorTests, What)
 {
-    PMBusError error("Unable to convert value to linear data format");
+    PMBusError error(
+        "VOUT_MODE contains unsupported data format", "vdd_reg",
+        "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg2");
     EXPECT_STREQ(error.what(),
-                 "PMBusError: Unable to convert value to linear data format");
+                 "PMBusError: VOUT_MODE contains unsupported data format");
 }