regulators: Add pmbus_write_vout_command action
Implement the pmbus_write_vout_command action in the JSON config file.
See pmbus_write_vout_command.md for more information about this action.
Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: Idef8da56d19df8a416efec068ffa9fb9493100cc
diff --git a/phosphor-regulators/src/actions/pmbus_write_vout_command_action.cpp b/phosphor-regulators/src/actions/pmbus_write_vout_command_action.cpp
new file mode 100644
index 0000000..ae5ac4d
--- /dev/null
+++ b/phosphor-regulators/src/actions/pmbus_write_vout_command_action.cpp
@@ -0,0 +1,167 @@
+/**
+ * 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_write_vout_command_action.hpp"
+
+#include "action_error.hpp"
+#include "pmbus_error.hpp"
+#include "write_verification_error.hpp"
+
+#include <exception>
+#include <ios>
+#include <sstream>
+
+namespace phosphor::power::regulators
+{
+
+bool PMBusWriteVoutCommandAction::execute(ActionEnvironment& environment)
+{
+ try
+ {
+ // Get volts value
+ double voltsValue = getVoltsValue(environment);
+
+ // Get I2C interface to current device
+ i2c::I2CInterface& interface = getI2CInterface(environment);
+
+ // Get exponent value for converting volts value to linear format
+ int8_t exponentValue = getExponentValue(interface);
+
+ // Convert volts value to linear data format
+ uint16_t linearValue =
+ pmbus_utils::convertToVoutLinear(voltsValue, exponentValue);
+
+ // Write linear format value to VOUT_COMMAND. I2CInterface method
+ // writes low-order byte first as required by PMBus.
+ interface.write(pmbus_utils::VOUT_COMMAND, linearValue);
+
+ // Verify write if necessary
+ if (isWriteVerified)
+ {
+ verifyWrite(environment, interface, linearValue);
+ }
+ }
+ // 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));
+ }
+ catch (const WriteVerificationError& verifyError)
+ {
+ std::throw_with_nested(ActionError(*this));
+ }
+ return true;
+}
+
+std::string PMBusWriteVoutCommandAction::toString() const
+{
+ std::ostringstream ss;
+ ss << "pmbus_write_vout_command: { ";
+
+ if (volts.has_value())
+ {
+ ss << "volts: " << volts.value() << ", ";
+ }
+
+ // Only linear format is currently supported
+ ss << "format: linear";
+
+ if (exponent.has_value())
+ {
+ ss << ", exponent: " << static_cast<int16_t>(exponent.value());
+ }
+
+ ss << ", is_verified: " << std::boolalpha << isWriteVerified << " }";
+ return ss.str();
+}
+
+int8_t
+ PMBusWriteVoutCommandAction::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;
+}
+
+double
+ PMBusWriteVoutCommandAction::getVoltsValue(ActionEnvironment& environment)
+{
+ double voltsValue;
+
+ if (volts.has_value())
+ {
+ // A volts value is defined for this action
+ voltsValue = volts.value();
+ }
+ else if (environment.hasVolts())
+ {
+ // A volts value is defined in the ActionEnvironment
+ voltsValue = environment.getVolts();
+ }
+ else
+ {
+ throw ActionError(*this, "No volts value defined");
+ }
+
+ return voltsValue;
+}
+
+void PMBusWriteVoutCommandAction::verifyWrite(ActionEnvironment& environment,
+ i2c::I2CInterface& interface,
+ uint16_t valueWritten)
+{
+ // Read current value of VOUT_COMMAND. I2CInterface method reads low byte
+ // first as required by PMBus.
+ uint16_t valueRead{0x00};
+ interface.read(pmbus_utils::VOUT_COMMAND, valueRead);
+
+ // Verify value read equals value written
+ if (valueRead != valueWritten)
+ {
+ std::ostringstream ss;
+ ss << "device: " << environment.getDeviceID()
+ << ", register: VOUT_COMMAND, value_written: 0x" << std::hex
+ << std::uppercase << valueWritten << ", value_read: 0x" << valueRead;
+ throw WriteVerificationError(ss.str());
+ }
+}
+
+} // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/src/actions/pmbus_write_vout_command_action.hpp b/phosphor-regulators/src/actions/pmbus_write_vout_command_action.hpp
new file mode 100644
index 0000000..4ed1e10
--- /dev/null
+++ b/phosphor-regulators/src/actions/pmbus_write_vout_command_action.hpp
@@ -0,0 +1,248 @@
+/**
+ * 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.
+ */
+#pragma once
+
+#include "action_environment.hpp"
+#include "i2c_action.hpp"
+#include "i2c_interface.hpp"
+#include "pmbus_utils.hpp"
+
+#include <cstdint>
+#include <optional>
+#include <stdexcept>
+#include <string>
+
+namespace phosphor::power::regulators
+{
+
+/**
+ * @class PMBusWriteVoutCommandAction
+ *
+ * Writes the value of VOUT_COMMAND to set the output voltage of a PMBus
+ * regulator rail. Communicates with the device directly using the I2C
+ * interface.
+ *
+ * Implements the pmbus_write_vout_command action in the JSON config file.
+ *
+ * The volts value to write can be specified in the constructor. Otherwise, the
+ * volts value will be obtained from the ActionEnvironment.
+ *
+ * The PMBus specification defines four data formats for the value of
+ * VOUT_COMMAND:
+ * - Linear
+ * - VID
+ * - Direct
+ * - IEEE Half-Precision Floating Point
+ * Currently only the linear data format is supported. The volts value is
+ * converted into linear format before being written.
+ *
+ * The linear data format requires an exponent value. The exponent value can be
+ * specified in the constructor. Otherwise the exponent value will be obtained
+ * from the PMBus VOUT_MODE command. Note that some PMBus devices do not
+ * support the VOUT_MODE command. The exponent value for a device is often
+ * found in the device documentation (data sheet).
+ *
+ * If desired, write verification can be performed. The value of VOUT_COMMAND
+ * will be read from the device after it is written to ensure that it contains
+ * the expected value. If VOUT_COMMAND contains an unexpected value, a
+ * WriteVerificationError is thrown. To perform verification, the device must
+ * return all 16 bits of voltage data that were written to VOUT_COMMAND.
+ */
+class PMBusWriteVoutCommandAction : public I2CAction
+{
+ public:
+ // Specify which compiler-generated methods we want
+ PMBusWriteVoutCommandAction() = delete;
+ PMBusWriteVoutCommandAction(const PMBusWriteVoutCommandAction&) = delete;
+ PMBusWriteVoutCommandAction(PMBusWriteVoutCommandAction&&) = delete;
+ PMBusWriteVoutCommandAction&
+ operator=(const PMBusWriteVoutCommandAction&) = delete;
+ PMBusWriteVoutCommandAction&
+ operator=(PMBusWriteVoutCommandAction&&) = delete;
+ virtual ~PMBusWriteVoutCommandAction() = default;
+
+ /**
+ * Constructor.
+ *
+ * Throws an exception if any of the input parameters are invalid.
+ *
+ * @param volts Optional volts value to write to VOUT_COMMAND.
+ * @param format Data format of the volts value written to VOUT_COMMAND.
+ * Currently the only supported format is linear.
+ * @param exponent Optional exponent to use to convert the volts value to
+ * linear data format.
+ * @param isVerified Specifies whether the updated value of VOUT_COMMAND is
+ * verified by reading it from the device.
+ */
+ explicit PMBusWriteVoutCommandAction(std::optional<double> volts,
+ pmbus_utils::VoutDataFormat format,
+ std::optional<int8_t> exponent,
+ bool isVerified) :
+ volts{volts},
+ format{format}, exponent{exponent}, isWriteVerified{isVerified}
+ {
+ // Currently only linear format is supported
+ if (format != pmbus_utils::VoutDataFormat::linear)
+ {
+ throw std::invalid_argument{"Unsupported data format specified"};
+ }
+ }
+
+ /**
+ * Executes this action.
+ *
+ * Writes a volts value to VOUT_COMMAND using the I2C interface.
+ *
+ * If a volts value was specified in the constructor, that value will be
+ * used. Otherwise the volts value will be obtained from the
+ * ActionEnvironment.
+ *
+ * The data format is specified in the constructor. Currently only the
+ * linear format is supported.
+ *
+ * An exponent value is required to convert the volts value to linear
+ * format. 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.
+ *
+ * Write verification will be performed if specified in the constructor.
+ *
+ * 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;
+
+ /**
+ * Returns the optional exponent value used to convert the volts value to
+ * linear data format.
+ *
+ * @return optional exponent value
+ */
+ std::optional<int8_t> getExponent() const
+ {
+ return exponent;
+ }
+
+ /**
+ * Returns the data format of the value written to VOUT_COMMAND.
+ *
+ * @return data format
+ */
+ pmbus_utils::VoutDataFormat getFormat() const
+ {
+ return format;
+ }
+
+ /**
+ * Returns the optional volts value to write to VOUT_COMMAND.
+ *
+ * @return optional volts value
+ */
+ std::optional<double> getVolts() const
+ {
+ return volts;
+ }
+
+ /**
+ * Returns whether write verification will be performed when writing to
+ * VOUT_COMMAND.
+ *
+ * @return true if write verification will be performed, false otherwise
+ */
+ bool isVerified() const
+ {
+ return isWriteVerified;
+ }
+
+ /**
+ * Returns a string description of this action.
+ *
+ * @return description of action
+ */
+ virtual std::string toString() const override;
+
+ private:
+ /**
+ * Gets the exponent value to use to convert the volts value to linear data
+ * format.
+ *
+ * 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);
+
+ /**
+ * Gets the volts value to write to VOUT_COMMAND.
+ *
+ * If a volts value is defined for this action, that value is returned.
+ * Otherwise the volts value is obtained from the specified
+ * ActionEnvironment.
+ *
+ * Throws an exception if no volts value is defined.
+ *
+ * @param environment action execution environment
+ * @return volts value
+ */
+ double getVoltsValue(ActionEnvironment& environment);
+
+ /**
+ * Verifies the value written to VOUT_COMMAND. Reads the current value of
+ * VOUT_COMMAND and ensures that it matches the value written.
+ *
+ * Throws an exception if the values do not match or a communication error
+ * occurs.
+ *
+ * @param environment action execution environment
+ * @param interface I2C interface to the current device
+ * @param valueWritten linear format volts value written to VOUT_COMMAND
+ */
+ void verifyWrite(ActionEnvironment& environment,
+ i2c::I2CInterface& interface, uint16_t valueWritten);
+
+ /**
+ * Optional volts value to write.
+ */
+ const std::optional<double> volts{};
+
+ /**
+ * Data format of the volts value written to VOUT_COMMAND.
+ */
+ const pmbus_utils::VoutDataFormat format{};
+
+ /**
+ * Optional exponent value to use to convert the volts value to linear data
+ * format.
+ */
+ const std::optional<int8_t> exponent{};
+
+ /**
+ * Indicates whether write verification will be performed when writing to
+ * VOUT_COMMAND.
+ */
+ const bool isWriteVerified{false};
+};
+
+} // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/src/meson.build b/phosphor-regulators/src/meson.build
index 816e74c..b652f08 100644
--- a/phosphor-regulators/src/meson.build
+++ b/phosphor-regulators/src/meson.build
@@ -13,7 +13,8 @@
'actions/i2c_compare_bytes_action.cpp',
'actions/i2c_write_bit_action.cpp',
'actions/i2c_write_byte_action.cpp',
- 'actions/i2c_write_bytes_action.cpp'
+ 'actions/i2c_write_bytes_action.cpp',
+ 'actions/pmbus_write_vout_command_action.cpp'
]
phosphor_regulators_library = static_library(
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
new file mode 100644
index 0000000..f2a5d9a
--- /dev/null
+++ b/phosphor-regulators/test/actions/pmbus_write_vout_command_action_tests.cpp
@@ -0,0 +1,631 @@
+/**
+ * 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 "action_environment.hpp"
+#include "action_error.hpp"
+#include "device.hpp"
+#include "i2c_interface.hpp"
+#include "id_map.hpp"
+#include "mocked_i2c_interface.hpp"
+#include "pmbus_error.hpp"
+#include "pmbus_utils.hpp"
+#include "pmbus_write_vout_command_action.hpp"
+#include "write_verification_error.hpp"
+
+#include <cstdint>
+#include <memory>
+#include <optional>
+#include <stdexcept>
+#include <string>
+#include <utility>
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+using namespace phosphor::power::regulators;
+
+using ::testing::A;
+using ::testing::Return;
+using ::testing::SetArgReferee;
+using ::testing::Throw;
+using ::testing::TypedEq;
+
+TEST(PMBusWriteVoutCommandActionTests, Constructor)
+{
+ // Test where works: Volts value and exponent value are specified
+ try
+ {
+ std::optional<double> volts{1.3};
+ pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear};
+ std::optional<int8_t> exponent{-8};
+ bool isVerified{true};
+ PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified};
+ EXPECT_EQ(action.getVolts().has_value(), true);
+ EXPECT_EQ(action.getVolts().value(), 1.3);
+ EXPECT_EQ(action.getFormat(), pmbus_utils::VoutDataFormat::linear);
+ EXPECT_EQ(action.getExponent().has_value(), true);
+ EXPECT_EQ(action.getExponent().value(), -8);
+ EXPECT_EQ(action.isVerified(), true);
+ }
+ catch (...)
+ {
+ ADD_FAILURE() << "Should not have caught exception.";
+ }
+
+ // Test where works: Volts value and exponent value are not specified
+ try
+ {
+ std::optional<double> volts{};
+ pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear};
+ std::optional<int8_t> exponent{};
+ bool isVerified{false};
+ PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified};
+ EXPECT_EQ(action.getVolts().has_value(), false);
+ EXPECT_EQ(action.getFormat(), pmbus_utils::VoutDataFormat::linear);
+ EXPECT_EQ(action.getExponent().has_value(), false);
+ EXPECT_EQ(action.isVerified(), false);
+ }
+ catch (...)
+ {
+ ADD_FAILURE() << "Should not have caught exception.";
+ }
+
+ // Test where fails: Data format is not linear
+ try
+ {
+ std::optional<double> volts{};
+ pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::direct};
+ std::optional<int8_t> exponent{};
+ bool isVerified{false};
+ PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified};
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const std::invalid_argument& e)
+ {
+ EXPECT_STREQ(e.what(), "Unsupported data format specified");
+ }
+ catch (...)
+ {
+ ADD_FAILURE() << "Should not have caught exception.";
+ }
+}
+
+TEST(PMBusWriteVoutCommandActionTests, Execute)
+{
+ // Test where works: Volts value and exponent value defined in action;
+ // write is verified.
+ try
+ {
+ // Create mock I2CInterface. Expect action to do the following:
+ // * will not read from VOUT_MODE (command/register 0x20)
+ // * will write 0x014D to VOUT_COMMAND (command/register 0x21)
+ // * will read 0x014D from VOUT_COMMAND
+ std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
+ std::make_unique<i2c::MockedI2CInterface>();
+ EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
+ EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint8_t&>())).Times(0);
+ EXPECT_CALL(*i2cInterface,
+ write(TypedEq<uint8_t>(0x21), TypedEq<uint16_t>(0x014D)))
+ .Times(1);
+ EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x21), A<uint16_t&>()))
+ .Times(1)
+ .WillOnce(SetArgReferee<1>(0x014D));
+
+ // Create Device, IDMap, and ActionEnvironment
+ Device device{"reg1", true, "/system/chassis/motherboard/reg1",
+ std::move(i2cInterface)};
+ IDMap idMap{};
+ idMap.addDevice(device);
+ ActionEnvironment env{idMap, "reg1"};
+
+ // Create and execute action
+ // Linear format volts value = (1.3 / 2^(-8)) = 332.8 = 333 = 0x014D
+ std::optional<double> volts{1.3};
+ pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear};
+ std::optional<int8_t> exponent{-8};
+ bool isVerified{true};
+ PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified};
+ EXPECT_EQ(action.execute(env), true);
+ }
+ catch (...)
+ {
+ ADD_FAILURE() << "Should not have caught exception.";
+ }
+
+ // Test where works: Volts value defined in ActionEnvironment; exponent
+ // value defined in VOUT_MODE; write is not verified.
+ try
+ {
+ // Create mock I2CInterface. Expect action to do the following:
+ // * will read 0b0001'0111 (linear format, -9 exponent) from VOUT_MODE
+ // * will write 0x069A to VOUT_COMMAND
+ // * will not read from VOUT_COMMAND
+ 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>(0x20), A<uint8_t&>()))
+ .Times(1)
+ .WillOnce(SetArgReferee<1>(0b0001'0111));
+ EXPECT_CALL(*i2cInterface,
+ write(TypedEq<uint8_t>(0x21), TypedEq<uint16_t>(0x069A)))
+ .Times(1);
+ EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint16_t&>())).Times(0);
+
+ // Create Device, IDMap, and ActionEnvironment. Set volts value to 3.3
+ // in ActionEnvironment.
+ Device device{"reg1", true, "/system/chassis/motherboard/reg1",
+ std::move(i2cInterface)};
+ IDMap idMap{};
+ idMap.addDevice(device);
+ ActionEnvironment env{idMap, "reg1"};
+ env.setVolts(3.3);
+
+ // Create and execute action
+ // Linear format volts value = (3.3 / 2^(-9)) = 1689.6 = 1690 = 0x069A
+ std::optional<double> volts{};
+ pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear};
+ std::optional<int8_t> exponent{};
+ bool isVerified{false};
+ PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified};
+ EXPECT_EQ(action.execute(env), true);
+ }
+ catch (...)
+ {
+ ADD_FAILURE() << "Should not have caught exception.";
+ }
+
+ // Test where fails: No volts value defined
+ try
+ {
+ // Create IDMap and ActionEnvironment
+ IDMap idMap{};
+ ActionEnvironment env{idMap, "reg1"};
+
+ // Create and execute action
+ std::optional<double> volts{};
+ pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear};
+ std::optional<int8_t> exponent{-8};
+ bool isVerified{false};
+ PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified};
+ action.execute(env);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const ActionError& e)
+ {
+ EXPECT_STREQ(
+ e.what(),
+ "ActionError: pmbus_write_vout_command: { format: linear, "
+ "exponent: -8, is_verified: false }: No volts value defined");
+ }
+ catch (...)
+ {
+ ADD_FAILURE() << "Should not have caught exception.";
+ }
+
+ // Test where fails: Unable to get I2C interface to current device
+ try
+ {
+ // Create IDMap and ActionEnvironment
+ IDMap idMap{};
+ ActionEnvironment env{idMap, "reg1"};
+
+ // Create and execute action
+ std::optional<double> volts{1.3};
+ pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear};
+ std::optional<int8_t> exponent{-8};
+ bool isVerified{false};
+ PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified};
+ action.execute(env);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const std::invalid_argument& e)
+ {
+ EXPECT_STREQ(e.what(), "Unable to find device with ID \"reg1\"");
+ }
+ catch (...)
+ {
+ ADD_FAILURE() << "Should not have caught exception.";
+ }
+
+ // Test where fails: Unable to read VOUT_MODE to get exponent
+ try
+ {
+ // Create mock I2CInterface. Expect action to do the following:
+ // * will try to read VOUT_MODE; exception will be thrown
+ // * will not write to VOUT_COMMAND due to exception
+ 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>(0x20), A<uint8_t&>()))
+ .Times(1)
+ .WillOnce(Throw(
+ i2c::I2CException{"Failed to read byte", "/dev/i2c-1", 0x70}));
+ EXPECT_CALL(*i2cInterface, write(A<uint8_t>(), A<uint16_t>())).Times(0);
+
+ // Create Device, IDMap, and ActionEnvironment
+ Device device{"reg1", true, "/system/chassis/motherboard/reg1",
+ std::move(i2cInterface)};
+ IDMap idMap{};
+ idMap.addDevice(device);
+ ActionEnvironment env{idMap, "reg1"};
+
+ // Create and execute action
+ std::optional<double> volts{3.3};
+ pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear};
+ std::optional<int8_t> exponent{};
+ bool isVerified{false};
+ PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified};
+ action.execute(env);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const ActionError& e)
+ {
+ EXPECT_STREQ(e.what(),
+ "ActionError: pmbus_write_vout_command: { volts: 3.3, "
+ "format: linear, is_verified: false }");
+ try
+ {
+ // Re-throw inner I2CException
+ std::rethrow_if_nested(e);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const i2c::I2CException& ie)
+ {
+ EXPECT_STREQ(
+ ie.what(),
+ "I2CException: Failed to read byte: bus /dev/i2c-1, addr 0x70");
+ }
+ catch (...)
+ {
+ ADD_FAILURE() << "Should not have caught exception.";
+ }
+ }
+ catch (...)
+ {
+ ADD_FAILURE() << "Should not have caught exception.";
+ }
+
+ // Test where fails: VOUT_MODE data format is not linear
+ try
+ {
+ // Create mock I2CInterface. Expect action to do the following:
+ // * will read 0b0010'0000 (vid data format) from VOUT_MODE
+ // * will not write to VOUT_COMMAND due to data format error
+ 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>(0x20), A<uint8_t&>()))
+ .Times(1)
+ .WillOnce(SetArgReferee<1>(0b0010'0000));
+ EXPECT_CALL(*i2cInterface, write(A<uint8_t>(), A<uint16_t>())).Times(0);
+
+ // Create Device, IDMap, and ActionEnvironment
+ Device device{"reg1", true, "/system/chassis/motherboard/reg1",
+ std::move(i2cInterface)};
+ IDMap idMap{};
+ idMap.addDevice(device);
+ ActionEnvironment env{idMap, "reg1"};
+
+ // Create and execute action
+ std::optional<double> volts{3.3};
+ pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear};
+ std::optional<int8_t> exponent{};
+ bool isVerified{false};
+ PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified};
+ action.execute(env);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const ActionError& e)
+ {
+ EXPECT_STREQ(e.what(),
+ "ActionError: pmbus_write_vout_command: { volts: 3.3, "
+ "format: linear, is_verified: false }");
+ try
+ {
+ // Re-throw inner PMBusError
+ std::rethrow_if_nested(e);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const PMBusError& pe)
+ {
+ EXPECT_STREQ(
+ pe.what(),
+ "PMBusError: VOUT_MODE contains unsupported data format");
+ }
+ catch (...)
+ {
+ ADD_FAILURE() << "Should not have caught exception.";
+ }
+ }
+ catch (...)
+ {
+ ADD_FAILURE() << "Should not have caught exception.";
+ }
+
+ // Test where fails: Unable to write VOUT_COMMAND
+ try
+ {
+ // Create mock I2CInterface. Expect action to do the following:
+ // * will not read from VOUT_MODE
+ // * will try to write 0x014D to VOUT_COMMAND; exception will be thrown
+ std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
+ std::make_unique<i2c::MockedI2CInterface>();
+ EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
+ EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint8_t&>())).Times(0);
+ EXPECT_CALL(*i2cInterface,
+ write(TypedEq<uint8_t>(0x21), TypedEq<uint16_t>(0x014D)))
+ .Times(1)
+ .WillOnce(Throw(i2c::I2CException{"Failed to write word data",
+ "/dev/i2c-1", 0x70}));
+
+ // Create Device, IDMap, and ActionEnvironment
+ Device device{"reg1", true, "/system/chassis/motherboard/reg1",
+ std::move(i2cInterface)};
+ IDMap idMap{};
+ idMap.addDevice(device);
+ ActionEnvironment env{idMap, "reg1"};
+
+ // Create and execute action
+ // Linear format volts value = (1.3 / 2^(-8)) = 332.8 = 333 = 0x014D
+ std::optional<double> volts{1.3};
+ pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear};
+ std::optional<int8_t> exponent{-8};
+ bool isVerified{false};
+ PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified};
+ action.execute(env);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const ActionError& e)
+ {
+ EXPECT_STREQ(e.what(),
+ "ActionError: pmbus_write_vout_command: { volts: 1.3, "
+ "format: linear, exponent: -8, is_verified: false }");
+ try
+ {
+ // Re-throw inner I2CException
+ std::rethrow_if_nested(e);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const i2c::I2CException& ie)
+ {
+ EXPECT_STREQ(ie.what(), "I2CException: Failed to write word data: "
+ "bus /dev/i2c-1, addr 0x70");
+ }
+ catch (...)
+ {
+ ADD_FAILURE() << "Should not have caught exception.";
+ }
+ }
+ catch (...)
+ {
+ ADD_FAILURE() << "Should not have caught exception.";
+ }
+
+ // Test where fails: Unable to read VOUT_COMMAND
+ try
+ {
+ // Create mock I2CInterface. Expect action to do the following:
+ // * will not read from VOUT_MODE
+ // * will write 0x014D to VOUT_COMMAND
+ // * will try to read from VOUT_COMMAND; exception will be thrown
+ std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
+ std::make_unique<i2c::MockedI2CInterface>();
+ EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
+ EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint8_t&>())).Times(0);
+ EXPECT_CALL(*i2cInterface,
+ write(TypedEq<uint8_t>(0x21), TypedEq<uint16_t>(0x014D)))
+ .Times(1);
+ EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x21), A<uint16_t&>()))
+ .Times(1)
+ .WillOnce(Throw(i2c::I2CException{"Failed to read word data",
+ "/dev/i2c-1", 0x70}));
+
+ // Create Device, IDMap, and ActionEnvironment
+ Device device{"reg1", true, "/system/chassis/motherboard/reg1",
+ std::move(i2cInterface)};
+ IDMap idMap{};
+ idMap.addDevice(device);
+ ActionEnvironment env{idMap, "reg1"};
+
+ // Create and execute action
+ // Linear format volts value = (1.3 / 2^(-8)) = 332.8 = 333 = 0x014D
+ std::optional<double> volts{1.3};
+ pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear};
+ std::optional<int8_t> exponent{-8};
+ bool isVerified{true};
+ PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified};
+ action.execute(env);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const ActionError& e)
+ {
+ EXPECT_STREQ(e.what(),
+ "ActionError: pmbus_write_vout_command: { volts: 1.3, "
+ "format: linear, exponent: -8, is_verified: true }");
+ try
+ {
+ // Re-throw inner I2CException
+ std::rethrow_if_nested(e);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const i2c::I2CException& ie)
+ {
+ EXPECT_STREQ(ie.what(), "I2CException: Failed to read word data: "
+ "bus /dev/i2c-1, addr 0x70");
+ }
+ catch (...)
+ {
+ ADD_FAILURE() << "Should not have caught exception.";
+ }
+ }
+ catch (...)
+ {
+ ADD_FAILURE() << "Should not have caught exception.";
+ }
+
+ // Test where fails: Write verification error
+ try
+ {
+ // Create mock I2CInterface. Expect action to do the following:
+ // * will not read from VOUT_MODE
+ // * will write 0x014D to VOUT_COMMAND
+ // * will read 0x014C from VOUT_COMMAND (not equal to 0x014D)
+ std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
+ std::make_unique<i2c::MockedI2CInterface>();
+ EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
+ EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint8_t&>())).Times(0);
+ EXPECT_CALL(*i2cInterface,
+ write(TypedEq<uint8_t>(0x21), TypedEq<uint16_t>(0x014D)))
+ .Times(1);
+ EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x21), A<uint16_t&>()))
+ .Times(1)
+ .WillOnce(SetArgReferee<1>(0x014C));
+
+ // Create Device, IDMap, and ActionEnvironment
+ Device device{"reg1", true, "/system/chassis/motherboard/reg1",
+ std::move(i2cInterface)};
+ IDMap idMap{};
+ idMap.addDevice(device);
+ ActionEnvironment env{idMap, "reg1"};
+
+ // Create and execute action
+ // Linear format volts value = (1.3 / 2^(-8)) = 332.8 = 333 = 0x014D
+ std::optional<double> volts{1.3};
+ pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear};
+ std::optional<int8_t> exponent{-8};
+ bool isVerified{true};
+ PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified};
+ action.execute(env);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const ActionError& e)
+ {
+ EXPECT_STREQ(e.what(),
+ "ActionError: pmbus_write_vout_command: { volts: 1.3, "
+ "format: linear, exponent: -8, is_verified: true }");
+ try
+ {
+ // Re-throw inner WriteVerificationError
+ std::rethrow_if_nested(e);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const WriteVerificationError& we)
+ {
+ EXPECT_STREQ(
+ we.what(),
+ "WriteVerificationError: device: reg1, register: VOUT_COMMAND, "
+ "value_written: 0x14D, value_read: 0x14C");
+ }
+ catch (...)
+ {
+ ADD_FAILURE() << "Should not have caught exception.";
+ }
+ }
+ catch (...)
+ {
+ ADD_FAILURE() << "Should not have caught exception.";
+ }
+}
+
+TEST(PMBusWriteVoutCommandActionTests, GetExponent)
+{
+ std::optional<double> volts{1.3};
+ pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear};
+ bool isVerified{true};
+
+ // Exponent value was specified
+ {
+ std::optional<int8_t> exponent{-9};
+ PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified};
+ EXPECT_EQ(action.getExponent().has_value(), true);
+ EXPECT_EQ(action.getExponent().value(), -9);
+ }
+
+ // Exponent value was not specified
+ {
+ std::optional<int8_t> exponent{};
+ PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified};
+ EXPECT_EQ(action.getExponent().has_value(), false);
+ }
+}
+
+TEST(PMBusWriteVoutCommandActionTests, GetFormat)
+{
+ std::optional<double> volts{};
+ pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear};
+ std::optional<int8_t> exponent{};
+ bool isVerified{false};
+ PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified};
+ EXPECT_EQ(action.getFormat(), pmbus_utils::VoutDataFormat::linear);
+}
+
+TEST(PMBusWriteVoutCommandActionTests, GetVolts)
+{
+ pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear};
+ std::optional<int8_t> exponent{-8};
+ bool isVerified{true};
+
+ // Volts value was specified
+ {
+ std::optional<double> volts{1.3};
+ PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified};
+ EXPECT_EQ(action.getVolts().has_value(), true);
+ EXPECT_EQ(action.getVolts().value(), 1.3);
+ }
+
+ // Volts value was not specified
+ {
+ std::optional<double> volts{};
+ PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified};
+ EXPECT_EQ(action.getVolts().has_value(), false);
+ }
+}
+
+TEST(PMBusWriteVoutCommandActionTests, IsVerified)
+{
+ std::optional<double> volts{1.3};
+ pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear};
+ std::optional<int8_t> exponent{-8};
+ bool isVerified{true};
+ PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified};
+ EXPECT_EQ(action.isVerified(), true);
+}
+
+TEST(PMBusWriteVoutCommandActionTests, ToString)
+{
+ // Test where volts value and exponent value are specified
+ {
+ std::optional<double> volts{1.3};
+ pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear};
+ std::optional<int8_t> exponent{-8};
+ bool isVerified{true};
+ PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified};
+ EXPECT_EQ(action.toString(),
+ "pmbus_write_vout_command: { volts: 1.3, format: linear, "
+ "exponent: -8, is_verified: true }");
+ }
+
+ // Test where volts value and exponent value are not specified
+ {
+ std::optional<double> volts{};
+ pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear};
+ std::optional<int8_t> exponent{};
+ bool isVerified{false};
+ PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified};
+ EXPECT_EQ(
+ action.toString(),
+ "pmbus_write_vout_command: { format: linear, is_verified: false }");
+ }
+}
diff --git a/phosphor-regulators/test/meson.build b/phosphor-regulators/test/meson.build
index 140094b..8cee4e7 100644
--- a/phosphor-regulators/test/meson.build
+++ b/phosphor-regulators/test/meson.build
@@ -26,6 +26,7 @@
'actions/if_action_tests.cpp',
'actions/not_action_tests.cpp',
'actions/or_action_tests.cpp',
+ 'actions/pmbus_write_vout_command_action_tests.cpp',
'actions/run_rule_action_tests.cpp',
'actions/set_device_action_tests.cpp'
]