regulators: Implements support for i2c_write_byte
Implements support for parsing the i2c_write_byte JSON elements in the
configuration file parser.
Signed-off-by: Bob King <Bob_King@wistron.com>
Change-Id: I131edcbda0d637f8d75b3f67bbe2d61407a184d6
diff --git a/phosphor-regulators/src/config_file_parser.cpp b/phosphor-regulators/src/config_file_parser.cpp
index cdf758e..791f3f7 100644
--- a/phosphor-regulators/src/config_file_parser.cpp
+++ b/phosphor-regulators/src/config_file_parser.cpp
@@ -108,9 +108,8 @@
}
else if (element.contains("i2c_write_byte"))
{
- // TODO: Not implemented yet
- // action = parseI2CWriteByte(element["i2c_write_byte"]);
- // ++propertyCount;
+ action = parseI2CWriteByte(element["i2c_write_byte"]);
+ ++propertyCount;
}
else if (element.contains("i2c_write_bytes"))
{
@@ -220,6 +219,36 @@
return std::make_unique<I2CWriteBitAction>(reg, position, value);
}
+std::unique_ptr<I2CWriteByteAction> parseI2CWriteByte(const json& element)
+{
+ verifyIsObject(element);
+ unsigned int propertyCount{0};
+
+ // Required register property
+ const json& regElement = getRequiredProperty(element, "register");
+ uint8_t reg = parseStringToUint8(regElement);
+ ++propertyCount;
+
+ // Required value property
+ const json& valueElement = getRequiredProperty(element, "value");
+ uint8_t value = parseStringToUint8(valueElement);
+ ++propertyCount;
+
+ // Optional mask property
+ uint8_t mask = 0xff;
+ auto maskIt = element.find("mask");
+ if (maskIt != element.end())
+ {
+ mask = parseStringToUint8(*maskIt);
+ ++propertyCount;
+ }
+
+ // Verify no invalid properties exist
+ verifyPropertyCount(element, propertyCount);
+
+ return std::make_unique<I2CWriteByteAction>(reg, value, mask);
+}
+
std::unique_ptr<PMBusWriteVoutCommandAction>
parsePMBusWriteVoutCommand(const json& element)
{
diff --git a/phosphor-regulators/src/config_file_parser.hpp b/phosphor-regulators/src/config_file_parser.hpp
index f96e6b4..abfa4f5 100644
--- a/phosphor-regulators/src/config_file_parser.hpp
+++ b/phosphor-regulators/src/config_file_parser.hpp
@@ -18,6 +18,7 @@
#include "action.hpp"
#include "chassis.hpp"
#include "i2c_write_bit_action.hpp"
+#include "i2c_write_byte_action.hpp"
#include "pmbus_write_vout_command_action.hpp"
#include "rule.hpp"
@@ -215,6 +216,19 @@
parseI2CWriteBit(const nlohmann::json& element);
/**
+ * Parses a JSON element containing an i2c_write_byte action.
+ *
+ * Returns the corresponding C++ I2CWriteByteAction object.
+ *
+ * Throws an exception if parsing fails.
+ *
+ * @param element JSON element
+ * @return I2CWriteByteAction object
+ */
+std::unique_ptr<I2CWriteByteAction>
+ parseI2CWriteByte(const nlohmann::json& element);
+
+/**
* Parses a JSON element containing an 8-bit signed integer.
*
* Returns the corresponding C++ int8_t value.
diff --git a/phosphor-regulators/test/config_file_parser_tests.cpp b/phosphor-regulators/test/config_file_parser_tests.cpp
index c26f472..7d36653 100644
--- a/phosphor-regulators/test/config_file_parser_tests.cpp
+++ b/phosphor-regulators/test/config_file_parser_tests.cpp
@@ -19,6 +19,7 @@
#include "config_file_parser_error.hpp"
#include "i2c_interface.hpp"
#include "i2c_write_bit_action.hpp"
+#include "i2c_write_byte_action.hpp"
#include "pmbus_utils.hpp"
#include "pmbus_write_vout_command_action.hpp"
#include "rule.hpp"
@@ -263,7 +264,18 @@
}
// Test where works: i2c_write_byte action type specified
- // TODO: Not implemented yet
+ {
+ const json element = R"(
+ {
+ "i2c_write_byte": {
+ "register": "0x0A",
+ "value": "0xCC"
+ }
+ }
+ )"_json;
+ std::unique_ptr<Action> action = parseAction(element);
+ EXPECT_NE(action.get(), nullptr);
+ }
// Test where works: i2c_write_bytes action type specified
// TODO: Not implemented yet
@@ -551,59 +563,6 @@
}
}
-TEST(ConfigFileParserTests, ParseInt8)
-{
- // Test where works: INT8_MIN
- {
- const json element = R"( -128 )"_json;
- int8_t value = parseInt8(element);
- EXPECT_EQ(value, -128);
- }
-
- // Test where works: INT8_MAX
- {
- const json element = R"( 127 )"_json;
- int8_t value = parseInt8(element);
- EXPECT_EQ(value, 127);
- }
-
- // Test where fails: Element is not an integer
- try
- {
- const json element = R"( 1.03 )"_json;
- parseInt8(element);
- ADD_FAILURE() << "Should not have reached this line.";
- }
- catch (const std::invalid_argument& e)
- {
- EXPECT_STREQ(e.what(), "Element is not an integer");
- }
-
- // Test where fails: Value < INT8_MIN
- try
- {
- const json element = R"( -129 )"_json;
- parseInt8(element);
- ADD_FAILURE() << "Should not have reached this line.";
- }
- catch (const std::invalid_argument& e)
- {
- EXPECT_STREQ(e.what(), "Element is not an 8-bit signed integer");
- }
-
- // Test where fails: Value > INT8_MAX
- try
- {
- const json element = R"( 128 )"_json;
- parseInt8(element);
- ADD_FAILURE() << "Should not have reached this line.";
- }
- catch (const std::invalid_argument& e)
- {
- EXPECT_STREQ(e.what(), "Element is not an 8-bit signed integer");
- }
-}
-
TEST(ConfigFileParserTests, ParseI2CWriteBit)
{
// Test where works
@@ -758,6 +717,210 @@
}
}
+TEST(ConfigFileParserTests, ParseI2CWriteByte)
+{
+ // Test where works: Only required properties specified
+ {
+ const json element = R"(
+ {
+ "register": "0x0A",
+ "value": "0xCC"
+ }
+ )"_json;
+ std::unique_ptr<I2CWriteByteAction> action = parseI2CWriteByte(element);
+ EXPECT_EQ(action->getRegister(), 0x0A);
+ EXPECT_EQ(action->getValue(), 0xCC);
+ EXPECT_EQ(action->getMask(), 0xFF);
+ }
+
+ // Test where works: All properties specified
+ {
+ const json element = R"(
+ {
+ "register": "0x0A",
+ "value": "0xCC",
+ "mask": "0xF7"
+ }
+ )"_json;
+ std::unique_ptr<I2CWriteByteAction> action = parseI2CWriteByte(element);
+ EXPECT_EQ(action->getRegister(), 0x0A);
+ EXPECT_EQ(action->getValue(), 0xCC);
+ EXPECT_EQ(action->getMask(), 0xF7);
+ }
+
+ // Test where fails: Element is not an object
+ try
+ {
+ const json element = R"( [ "0xFF", "0x01" ] )"_json;
+ parseI2CWriteByte(element);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const std::invalid_argument& e)
+ {
+ EXPECT_STREQ(e.what(), "Element is not an object");
+ }
+
+ // Test where fails: Invalid property specified
+ try
+ {
+ const json element = R"(
+ {
+ "register": "0x0A",
+ "value": "0xCC",
+ "mask": "0xF7",
+ "foo": 1
+ }
+ )"_json;
+ parseI2CWriteByte(element);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const std::invalid_argument& e)
+ {
+ EXPECT_STREQ(e.what(), "Element contains an invalid property");
+ }
+
+ // Test where fails: register value is invalid
+ try
+ {
+ const json element = R"(
+ {
+ "register": "0x0Z",
+ "value": "0xCC",
+ "mask": "0xF7"
+ }
+ )"_json;
+ parseI2CWriteByte(element);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const std::invalid_argument& e)
+ {
+ EXPECT_STREQ(e.what(), "Element is not hexadecimal string");
+ }
+
+ // Test where fails: value value is invalid
+ try
+ {
+ const json element = R"(
+ {
+ "register": "0x0A",
+ "value": "0xCCC",
+ "mask": "0xF7"
+ }
+ )"_json;
+ parseI2CWriteByte(element);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const std::invalid_argument& e)
+ {
+ EXPECT_STREQ(e.what(), "Element is not hexadecimal string");
+ }
+
+ // Test where fails: mask value is invalid
+ try
+ {
+ const json element = R"(
+ {
+ "register": "0x0A",
+ "value": "0xCC",
+ "mask": "F7"
+ }
+ )"_json;
+ parseI2CWriteByte(element);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const std::invalid_argument& e)
+ {
+ EXPECT_STREQ(e.what(), "Element is not hexadecimal string");
+ }
+
+ // Test where fails: Required register property not specified
+ try
+ {
+ const json element = R"(
+ {
+ "value": "0xCC",
+ "mask": "0xF7"
+ }
+ )"_json;
+ parseI2CWriteByte(element);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const std::invalid_argument& e)
+ {
+ EXPECT_STREQ(e.what(), "Required property missing: register");
+ }
+
+ // Test where fails: Required value property not specified
+ try
+ {
+ const json element = R"(
+ {
+ "register": "0x0A",
+ "mask": "0xF7"
+ }
+ )"_json;
+ parseI2CWriteByte(element);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const std::invalid_argument& e)
+ {
+ EXPECT_STREQ(e.what(), "Required property missing: value");
+ }
+}
+
+TEST(ConfigFileParserTests, ParseInt8)
+{
+ // Test where works: INT8_MIN
+ {
+ const json element = R"( -128 )"_json;
+ int8_t value = parseInt8(element);
+ EXPECT_EQ(value, -128);
+ }
+
+ // Test where works: INT8_MAX
+ {
+ const json element = R"( 127 )"_json;
+ int8_t value = parseInt8(element);
+ EXPECT_EQ(value, 127);
+ }
+
+ // Test where fails: Element is not an integer
+ try
+ {
+ const json element = R"( 1.03 )"_json;
+ parseInt8(element);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const std::invalid_argument& e)
+ {
+ EXPECT_STREQ(e.what(), "Element is not an integer");
+ }
+
+ // Test where fails: Value < INT8_MIN
+ try
+ {
+ const json element = R"( -129 )"_json;
+ parseInt8(element);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const std::invalid_argument& e)
+ {
+ EXPECT_STREQ(e.what(), "Element is not an 8-bit signed integer");
+ }
+
+ // Test where fails: Value > INT8_MAX
+ try
+ {
+ const json element = R"( 128 )"_json;
+ parseInt8(element);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const std::invalid_argument& e)
+ {
+ EXPECT_STREQ(e.what(), "Element is not an 8-bit signed integer");
+ }
+}
+
TEST(ConfigFileParserTests, ParsePMBusWriteVoutCommand)
{
// Test where works: Only required properties specified