regulators: Implements support for i2c_compare actions
Enhance the configuration file parser to support the i2c_compare_bit,
i2c_compare_byte, and i2c_compare_bytes actions.
Signed-off-by: Bob King <Bob_King@wistron.com>
Change-Id: I752afde05f16d82da22d404bf24a8c1ab3b7785c
diff --git a/phosphor-regulators/src/config_file_parser.cpp b/phosphor-regulators/src/config_file_parser.cpp
index cc96545..64b6121 100644
--- a/phosphor-regulators/src/config_file_parser.cpp
+++ b/phosphor-regulators/src/config_file_parser.cpp
@@ -85,21 +85,18 @@
}
else if (element.contains("i2c_compare_bit"))
{
- // TODO: Not implemented yet
- // action = parseI2CCompareBit(element["i2c_compare_bit"]);
- // ++propertyCount;
+ action = parseI2CCompareBit(element["i2c_compare_bit"]);
+ ++propertyCount;
}
else if (element.contains("i2c_compare_byte"))
{
- // TODO: Not implemented yet
- // action = parseI2CCompareByte(element["i2c_compare_byte"]);
- // ++propertyCount;
+ action = parseI2CCompareByte(element["i2c_compare_byte"]);
+ ++propertyCount;
}
else if (element.contains("i2c_compare_bytes"))
{
- // TODO: Not implemented yet
- // action = parseI2CCompareBytes(element["i2c_compare_bytes"]);
- // ++propertyCount;
+ action = parseI2CCompareBytes(element["i2c_compare_bytes"]);
+ ++propertyCount;
}
else if (element.contains("i2c_write_bit"))
{
@@ -354,6 +351,102 @@
return values;
}
+std::unique_ptr<I2CCompareBitAction> parseI2CCompareBit(const json& element)
+{
+ verifyIsObject(element);
+ unsigned int propertyCount{0};
+
+ // Required register property
+ const json& regElement = getRequiredProperty(element, "register");
+ uint8_t reg = parseHexByte(regElement);
+ ++propertyCount;
+
+ // Required position property
+ const json& positionElement = getRequiredProperty(element, "position");
+ uint8_t position = parseBitPosition(positionElement);
+ ++propertyCount;
+
+ // Required value property
+ const json& valueElement = getRequiredProperty(element, "value");
+ uint8_t value = parseBitValue(valueElement);
+ ++propertyCount;
+
+ // Verify no invalid properties exist
+ verifyPropertyCount(element, propertyCount);
+
+ return std::make_unique<I2CCompareBitAction>(reg, position, value);
+}
+
+std::unique_ptr<I2CCompareByteAction> parseI2CCompareByte(const json& element)
+{
+ verifyIsObject(element);
+ unsigned int propertyCount{0};
+
+ // Required register property
+ const json& regElement = getRequiredProperty(element, "register");
+ uint8_t reg = parseHexByte(regElement);
+ ++propertyCount;
+
+ // Required value property
+ const json& valueElement = getRequiredProperty(element, "value");
+ uint8_t value = parseHexByte(valueElement);
+ ++propertyCount;
+
+ // Optional mask property
+ uint8_t mask = 0xff;
+ auto maskIt = element.find("mask");
+ if (maskIt != element.end())
+ {
+ mask = parseHexByte(*maskIt);
+ ++propertyCount;
+ }
+
+ // Verify no invalid properties exist
+ verifyPropertyCount(element, propertyCount);
+
+ return std::make_unique<I2CCompareByteAction>(reg, value, mask);
+}
+
+std::unique_ptr<I2CCompareBytesAction> parseI2CCompareBytes(const json& element)
+{
+ verifyIsObject(element);
+ unsigned int propertyCount{0};
+
+ // Required register property
+ const json& regElement = getRequiredProperty(element, "register");
+ uint8_t reg = parseHexByte(regElement);
+ ++propertyCount;
+
+ // Required values property
+ const json& valueElement = getRequiredProperty(element, "values");
+ std::vector<uint8_t> values = parseHexByteArray(valueElement);
+ ++propertyCount;
+
+ // Optional masks property
+ std::vector<uint8_t> masks{};
+ auto masksIt = element.find("masks");
+ if (masksIt != element.end())
+ {
+ masks = parseHexByteArray(*masksIt);
+ ++propertyCount;
+ }
+
+ // Verify masks array (if specified) was same size as values array
+ if ((!masks.empty()) && (masks.size() != values.size()))
+ {
+ throw std::invalid_argument{"Invalid number of elements in masks"};
+ }
+
+ // Verify no invalid properties exist
+ verifyPropertyCount(element, propertyCount);
+
+ if (masks.empty())
+ {
+ return std::make_unique<I2CCompareBytesAction>(reg, values);
+ }
+ return std::make_unique<I2CCompareBytesAction>(reg, values, masks);
+}
+
std::unique_ptr<i2c::I2CInterface> parseI2CInterface(const json& element)
{
verifyIsObject(element);
diff --git a/phosphor-regulators/src/config_file_parser.hpp b/phosphor-regulators/src/config_file_parser.hpp
index 558debc..b653c00 100644
--- a/phosphor-regulators/src/config_file_parser.hpp
+++ b/phosphor-regulators/src/config_file_parser.hpp
@@ -19,6 +19,9 @@
#include "chassis.hpp"
#include "configuration.hpp"
#include "device.hpp"
+#include "i2c_compare_bit_action.hpp"
+#include "i2c_compare_byte_action.hpp"
+#include "i2c_compare_bytes_action.hpp"
#include "i2c_interface.hpp"
#include "i2c_write_bit_action.hpp"
#include "i2c_write_byte_action.hpp"
@@ -308,6 +311,45 @@
std::vector<uint8_t> parseHexByteArray(const nlohmann::json& element);
/**
+ * Parses a JSON element containing an i2c_compare_bit action.
+ *
+ * Returns the corresponding C++ I2CCompareBitAction object.
+ *
+ * Throws an exception if parsing fails.
+ *
+ * @param element JSON element
+ * @return I2CCompareBitAction object
+ */
+std::unique_ptr<I2CCompareBitAction>
+ parseI2CCompareBit(const nlohmann::json& element);
+
+/**
+ * Parses a JSON element containing an i2c_compare_byte action.
+ *
+ * Returns the corresponding C++ I2CCompareByteAction object.
+ *
+ * Throws an exception if parsing fails.
+ *
+ * @param element JSON element
+ * @return I2CCompareByteAction object
+ */
+std::unique_ptr<I2CCompareByteAction>
+ parseI2CCompareByte(const nlohmann::json& element);
+
+/**
+ * Parses a JSON element containing an i2c_compare_bytes action.
+ *
+ * Returns the corresponding C++ I2CCompareBytesAction object.
+ *
+ * Throws an exception if parsing fails.
+ *
+ * @param element JSON element
+ * @return I2CCompareBytesAction object
+ */
+std::unique_ptr<I2CCompareBytesAction>
+ parseI2CCompareBytes(const nlohmann::json& element);
+
+/**
* Parses a JSON element containing an i2c_interface.
*
* Returns the corresponding C++ i2c::I2CInterface object.
diff --git a/phosphor-regulators/test/config_file_parser_tests.cpp b/phosphor-regulators/test/config_file_parser_tests.cpp
index be8a4c8..6953e07 100644
--- a/phosphor-regulators/test/config_file_parser_tests.cpp
+++ b/phosphor-regulators/test/config_file_parser_tests.cpp
@@ -19,6 +19,9 @@
#include "config_file_parser_error.hpp"
#include "configuration.hpp"
#include "device.hpp"
+#include "i2c_compare_bit_action.hpp"
+#include "i2c_compare_byte_action.hpp"
+#include "i2c_compare_bytes_action.hpp"
#include "i2c_interface.hpp"
#include "i2c_write_bit_action.hpp"
#include "i2c_write_byte_action.hpp"
@@ -246,13 +249,47 @@
// TODO: Not implemented yet
// Test where works: i2c_compare_bit action type specified
- // TODO: Not implemented yet
+ {
+ const json element = R"(
+ {
+ "i2c_compare_bit": {
+ "register": "0xA0",
+ "position": 3,
+ "value": 0
+ }
+ }
+ )"_json;
+ std::unique_ptr<Action> action = parseAction(element);
+ EXPECT_NE(action.get(), nullptr);
+ }
// Test where works: i2c_compare_byte action type specified
- // TODO: Not implemented yet
+ {
+ const json element = R"(
+ {
+ "i2c_compare_byte": {
+ "register": "0x0A",
+ "value": "0xCC"
+ }
+ }
+ )"_json;
+ std::unique_ptr<Action> action = parseAction(element);
+ EXPECT_NE(action.get(), nullptr);
+ }
// Test where works: i2c_compare_bytes action type specified
- // TODO: Not implemented yet
+ {
+ const json element = R"(
+ {
+ "i2c_compare_bytes": {
+ "register": "0x0A",
+ "values": [ "0xCC", "0xFF" ]
+ }
+ }
+ )"_json;
+ std::unique_ptr<Action> action = parseAction(element);
+ EXPECT_NE(action.get(), nullptr);
+ }
// Test where works: i2c_write_bit action type specified
{
@@ -1433,6 +1470,491 @@
}
}
+TEST(ConfigFileParserTests, ParseI2CCompareBit)
+{
+ // Test where works
+ {
+ const json element = R"(
+ {
+ "register": "0xA0",
+ "position": 3,
+ "value": 0
+ }
+ )"_json;
+ std::unique_ptr<I2CCompareBitAction> action =
+ parseI2CCompareBit(element);
+ EXPECT_EQ(action->getRegister(), 0xA0);
+ EXPECT_EQ(action->getPosition(), 3);
+ EXPECT_EQ(action->getValue(), 0);
+ }
+
+ // Test where fails: Invalid property specified
+ try
+ {
+ const json element = R"(
+ {
+ "register": "0xA0",
+ "position": 3,
+ "value": 0,
+ "foo": 3
+ }
+ )"_json;
+ parseI2CCompareBit(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: Element is not an object
+ try
+ {
+ const json element = R"( [ "0xFF", "0x01" ] )"_json;
+ parseI2CCompareBit(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: register value is invalid
+ try
+ {
+ const json element = R"(
+ {
+ "register": "0xAG",
+ "position": 3,
+ "value": 0
+ }
+ )"_json;
+ parseI2CCompareBit(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: position value is invalid
+ try
+ {
+ const json element = R"(
+ {
+ "register": "0xA0",
+ "position": 8,
+ "value": 0
+ }
+ )"_json;
+ parseI2CCompareBit(element);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const std::invalid_argument& e)
+ {
+ EXPECT_STREQ(e.what(), "Element is not a bit position");
+ }
+
+ // Test where fails: value value is invalid
+ try
+ {
+ const json element = R"(
+ {
+ "register": "0xA0",
+ "position": 3,
+ "value": 2
+ }
+ )"_json;
+ parseI2CCompareBit(element);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const std::invalid_argument& e)
+ {
+ EXPECT_STREQ(e.what(), "Element is not a bit value");
+ }
+
+ // Test where fails: Required register property not specified
+ try
+ {
+ const json element = R"(
+ {
+ "position": 3,
+ "value": 0
+ }
+ )"_json;
+ parseI2CCompareBit(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 position property not specified
+ try
+ {
+ const json element = R"(
+ {
+ "register": "0xA0",
+ "value": 0
+ }
+ )"_json;
+ parseI2CCompareBit(element);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const std::invalid_argument& e)
+ {
+ EXPECT_STREQ(e.what(), "Required property missing: position");
+ }
+
+ // Test where fails: Required value property not specified
+ try
+ {
+ const json element = R"(
+ {
+ "register": "0xA0",
+ "position": 3
+ }
+ )"_json;
+ parseI2CCompareBit(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, ParseI2CCompareByte)
+{
+ // Test where works: Only required properties specified
+ {
+ const json element = R"(
+ {
+ "register": "0x0A",
+ "value": "0xCC"
+ }
+ )"_json;
+ std::unique_ptr<I2CCompareByteAction> action =
+ parseI2CCompareByte(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<I2CCompareByteAction> action =
+ parseI2CCompareByte(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;
+ parseI2CCompareByte(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;
+ parseI2CCompareByte(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;
+ parseI2CCompareByte(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;
+ parseI2CCompareByte(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;
+ parseI2CCompareByte(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;
+ parseI2CCompareByte(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;
+ parseI2CCompareByte(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, ParseI2CCompareBytes)
+{
+ // Test where works: Only required properties specified
+ {
+ const json element = R"(
+ {
+ "register": "0x0A",
+ "values": [ "0xCC", "0xFF" ]
+ }
+ )"_json;
+ std::unique_ptr<I2CCompareBytesAction> action =
+ parseI2CCompareBytes(element);
+ EXPECT_EQ(action->getRegister(), 0x0A);
+ EXPECT_EQ(action->getValues().size(), 2);
+ EXPECT_EQ(action->getValues()[0], 0xCC);
+ EXPECT_EQ(action->getValues()[1], 0xFF);
+ EXPECT_EQ(action->getMasks().size(), 2);
+ EXPECT_EQ(action->getMasks()[0], 0xFF);
+ EXPECT_EQ(action->getMasks()[1], 0xFF);
+ }
+
+ // Test where works: All properties specified
+ {
+ const json element = R"(
+ {
+ "register": "0x0A",
+ "values": [ "0xCC", "0xFF" ],
+ "masks": [ "0x7F", "0x77" ]
+ }
+ )"_json;
+ std::unique_ptr<I2CCompareBytesAction> action =
+ parseI2CCompareBytes(element);
+ EXPECT_EQ(action->getRegister(), 0x0A);
+ EXPECT_EQ(action->getValues().size(), 2);
+ EXPECT_EQ(action->getValues()[0], 0xCC);
+ EXPECT_EQ(action->getValues()[1], 0xFF);
+ EXPECT_EQ(action->getMasks().size(), 2);
+ EXPECT_EQ(action->getMasks()[0], 0x7F);
+ EXPECT_EQ(action->getMasks()[1], 0x77);
+ }
+
+ // Test where fails: Element is not an object
+ try
+ {
+ const json element = R"( [ "0xFF", "0x01" ] )"_json;
+ parseI2CCompareBytes(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",
+ "values": [ "0xCC", "0xFF" ],
+ "masks": [ "0x7F", "0x7F" ],
+ "foo": 1
+ }
+ )"_json;
+ parseI2CCompareBytes(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",
+ "values": [ "0xCC", "0xFF" ],
+ "masks": [ "0x7F", "0x7F" ]
+ }
+ )"_json;
+ parseI2CCompareBytes(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: values value is invalid
+ try
+ {
+ const json element = R"(
+ {
+ "register": "0x0A",
+ "values": [ "0xCCC", "0xFF" ],
+ "masks": [ "0x7F", "0x7F" ]
+ }
+ )"_json;
+ parseI2CCompareBytes(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: masks value is invalid
+ try
+ {
+ const json element = R"(
+ {
+ "register": "0x0A",
+ "values": [ "0xCC", "0xFF" ],
+ "masks": [ "F", "0x7F" ]
+ }
+ )"_json;
+ parseI2CCompareBytes(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: number of elements in masks is invalid
+ try
+ {
+ const json element = R"(
+ {
+ "register": "0x0A",
+ "values": [ "0xCC", "0xFF" ],
+ "masks": [ "0x7F" ]
+ }
+ )"_json;
+ parseI2CCompareBytes(element);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const std::invalid_argument& e)
+ {
+ EXPECT_STREQ(e.what(), "Invalid number of elements in masks");
+ }
+
+ // Test where fails: Required register property not specified
+ try
+ {
+ const json element = R"(
+ {
+ "values": [ "0xCC", "0xFF" ]
+ }
+ )"_json;
+ parseI2CCompareBytes(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 values property not specified
+ try
+ {
+ const json element = R"(
+ {
+ "register": "0x0A"
+ }
+ )"_json;
+ parseI2CCompareBytes(element);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const std::invalid_argument& e)
+ {
+ EXPECT_STREQ(e.what(), "Required property missing: values");
+ }
+}
+
TEST(ConfigFileParserTests, ParseI2CWriteBit)
{
// Test where works