regulators: Add i2c_capture_bytes to parser
Enhance the JSON configuration file parser to support the new
i2c_capture_bytes action.
Add gtest test cases to exercise new code.
Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: I92f50fe3df9b3469e16e03b04ae5d0df464eb3e2
diff --git a/phosphor-regulators/src/config_file_parser.cpp b/phosphor-regulators/src/config_file_parser.cpp
index 0b4673d..be28fdc 100644
--- a/phosphor-regulators/src/config_file_parser.cpp
+++ b/phosphor-regulators/src/config_file_parser.cpp
@@ -80,6 +80,11 @@
action = parseCompareVPD(element["compare_vpd"]);
++propertyCount;
}
+ else if (element.contains("i2c_capture_bytes"))
+ {
+ action = parseI2CCaptureBytes(element["i2c_capture_bytes"]);
+ ++propertyCount;
+ }
else if (element.contains("i2c_compare_bit"))
{
action = parseI2CCompareBit(element["i2c_compare_bit"]);
@@ -429,6 +434,31 @@
return values;
}
+std::unique_ptr<I2CCaptureBytesAction> parseI2CCaptureBytes(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 count property
+ const json& countElement = getRequiredProperty(element, "count");
+ uint8_t count = parseUint8(countElement);
+ if (count < 1)
+ {
+ throw std::invalid_argument{"Invalid byte count: Must be > 0"};
+ }
+ ++propertyCount;
+
+ // Verify no invalid properties exist
+ verifyPropertyCount(element, propertyCount);
+
+ return std::make_unique<I2CCaptureBytesAction>(reg, count);
+}
+
std::unique_ptr<I2CCompareBitAction> parseI2CCompareBit(const json& element)
{
verifyIsObject(element);
diff --git a/phosphor-regulators/src/config_file_parser.hpp b/phosphor-regulators/src/config_file_parser.hpp
index 63dcfd6..1062ced 100644
--- a/phosphor-regulators/src/config_file_parser.hpp
+++ b/phosphor-regulators/src/config_file_parser.hpp
@@ -22,6 +22,7 @@
#include "compare_vpd_action.hpp"
#include "configuration.hpp"
#include "device.hpp"
+#include "i2c_capture_bytes_action.hpp"
#include "i2c_compare_bit_action.hpp"
#include "i2c_compare_byte_action.hpp"
#include "i2c_compare_bytes_action.hpp"
@@ -358,6 +359,19 @@
std::vector<uint8_t> parseHexByteArray(const nlohmann::json& element);
/**
+ * Parses a JSON element containing an i2c_capture_bytes action.
+ *
+ * Returns the corresponding C++ I2CCaptureBytesAction object.
+ *
+ * Throws an exception if parsing fails.
+ *
+ * @param element JSON element
+ * @return I2CCaptureBytesAction object
+ */
+std::unique_ptr<I2CCaptureBytesAction>
+ parseI2CCaptureBytes(const nlohmann::json& element);
+
+/**
* Parses a JSON element containing an i2c_compare_bit action.
*
* Returns the corresponding C++ I2CCompareBitAction object.
diff --git a/phosphor-regulators/test/config_file_parser_tests.cpp b/phosphor-regulators/test/config_file_parser_tests.cpp
index 3249465..27df51e 100644
--- a/phosphor-regulators/test/config_file_parser_tests.cpp
+++ b/phosphor-regulators/test/config_file_parser_tests.cpp
@@ -22,6 +22,7 @@
#include "config_file_parser_error.hpp"
#include "configuration.hpp"
#include "device.hpp"
+#include "i2c_capture_bytes_action.hpp"
#include "i2c_compare_bit_action.hpp"
#include "i2c_compare_byte_action.hpp"
#include "i2c_compare_bytes_action.hpp"
@@ -292,6 +293,20 @@
EXPECT_NE(action.get(), nullptr);
}
+ // Test where works: i2c_capture_bytes action type specified
+ {
+ const json element = R"(
+ {
+ "i2c_capture_bytes": {
+ "register": "0xA0",
+ "count": 2
+ }
+ }
+ )"_json;
+ std::unique_ptr<Action> action = parseAction(element);
+ EXPECT_NE(action.get(), nullptr);
+ }
+
// Test where works: i2c_compare_bit action type specified
{
const json element = R"(
@@ -1986,6 +2001,119 @@
}
}
+TEST(ConfigFileParserTests, ParseI2CCaptureBytes)
+{
+ // Test where works
+ {
+ const json element = R"(
+ {
+ "register": "0xA0",
+ "count": 2
+ }
+ )"_json;
+ std::unique_ptr<I2CCaptureBytesAction> action =
+ parseI2CCaptureBytes(element);
+ EXPECT_EQ(action->getRegister(), 0xA0);
+ EXPECT_EQ(action->getCount(), 2);
+ }
+
+ // Test where fails: Element is not an object
+ try
+ {
+ const json element = R"( [ "0xFF", "0x01" ] )"_json;
+ parseI2CCaptureBytes(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": "0x0Z",
+ "count": 2
+ }
+ )"_json;
+ parseI2CCaptureBytes(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: count value is invalid
+ try
+ {
+ const json element = R"(
+ {
+ "register": "0xA0",
+ "count": 0
+ }
+ )"_json;
+ parseI2CCaptureBytes(element);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const std::invalid_argument& e)
+ {
+ EXPECT_STREQ(e.what(), "Invalid byte count: Must be > 0");
+ }
+
+ // Test where fails: Required register property not specified
+ try
+ {
+ const json element = R"(
+ {
+ "count": 2
+ }
+ )"_json;
+ parseI2CCaptureBytes(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 count property not specified
+ try
+ {
+ const json element = R"(
+ {
+ "register": "0xA0"
+ }
+ )"_json;
+ parseI2CCaptureBytes(element);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const std::invalid_argument& e)
+ {
+ EXPECT_STREQ(e.what(), "Required property missing: count");
+ }
+
+ // Test where fails: Invalid property specified
+ try
+ {
+ const json element = R"(
+ {
+ "register": "0xA0",
+ "count": 2,
+ "foo": 3
+ }
+ )"_json;
+ parseI2CCaptureBytes(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(ConfigFileParserTests, ParseI2CCompareBit)
{
// Test where works