regulators: Implements support for i2c_write_bytes
Implements support for parsing the i2c_write_bytes JSON elements in the
configuration file parser.
Signed-off-by: Bob King <Bob_King@wistron.com>
Change-Id: I6233361a6cc5a243b563e59e1aec70eec7a5b7f5
diff --git a/phosphor-regulators/src/config_file_parser.cpp b/phosphor-regulators/src/config_file_parser.cpp
index 791f3f7..3959978 100644
--- a/phosphor-regulators/src/config_file_parser.cpp
+++ b/phosphor-regulators/src/config_file_parser.cpp
@@ -113,9 +113,8 @@
}
else if (element.contains("i2c_write_bytes"))
{
- // TODO: Not implemented yet
- // action = parseI2CWriteBytes(element["i2c_write_bytes"]);
- // ++propertyCount;
+ action = parseI2CWriteBytes(element["i2c_write_bytes"]);
+ ++propertyCount;
}
else if (element.contains("if"))
{
@@ -193,6 +192,17 @@
return chassis;
}
+std::vector<uint8_t> parseHexByteArray(const json& element)
+{
+ verifyIsArray(element);
+ std::vector<uint8_t> values;
+ for (auto& valueElement : element)
+ {
+ values.emplace_back(parseHexByte(valueElement));
+ }
+ return values;
+}
+
std::unique_ptr<I2CWriteBitAction> parseI2CWriteBit(const json& element)
{
verifyIsObject(element);
@@ -200,7 +210,7 @@
// Required register property
const json& regElement = getRequiredProperty(element, "register");
- uint8_t reg = parseStringToUint8(regElement);
+ uint8_t reg = parseHexByte(regElement);
++propertyCount;
// Required position property
@@ -226,12 +236,12 @@
// Required register property
const json& regElement = getRequiredProperty(element, "register");
- uint8_t reg = parseStringToUint8(regElement);
+ uint8_t reg = parseHexByte(regElement);
++propertyCount;
// Required value property
const json& valueElement = getRequiredProperty(element, "value");
- uint8_t value = parseStringToUint8(valueElement);
+ uint8_t value = parseHexByte(valueElement);
++propertyCount;
// Optional mask property
@@ -239,7 +249,7 @@
auto maskIt = element.find("mask");
if (maskIt != element.end())
{
- mask = parseStringToUint8(*maskIt);
+ mask = parseHexByte(*maskIt);
++propertyCount;
}
@@ -249,6 +259,46 @@
return std::make_unique<I2CWriteByteAction>(reg, value, mask);
}
+std::unique_ptr<I2CWriteBytesAction> parseI2CWriteBytes(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<I2CWriteBytesAction>(reg, values);
+ }
+ return std::make_unique<I2CWriteBytesAction>(reg, values, masks);
+}
+
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 abfa4f5..a8286f1 100644
--- a/phosphor-regulators/src/config_file_parser.hpp
+++ b/phosphor-regulators/src/config_file_parser.hpp
@@ -19,6 +19,7 @@
#include "chassis.hpp"
#include "i2c_write_bit_action.hpp"
#include "i2c_write_byte_action.hpp"
+#include "i2c_write_bytes_action.hpp"
#include "pmbus_write_vout_command_action.hpp"
#include "rule.hpp"
@@ -203,6 +204,53 @@
}
/**
+ * Parses a JSON element containing a byte value expressed as a hexadecimal
+ * string.
+ *
+ * The JSON number data type does not support the hexadecimal format. For this
+ * reason, hexadecimal byte values are stored as strings in the configuration
+ * file.
+ *
+ * Returns the corresponding C++ uint8_t value.
+ *
+ * Throws an exception if parsing fails.
+ *
+ * @param element JSON element
+ * @return uint8_t value
+ */
+inline uint8_t parseHexByte(const nlohmann::json& element)
+{
+ if (!element.is_string())
+ {
+ throw std::invalid_argument{"Element is not a string"};
+ }
+ std::string value = element;
+
+ bool isHex = (value.compare(0, 2, "0x") == 0) && (value.size() > 2) &&
+ (value.size() < 5) &&
+ (value.find_first_not_of("0123456789abcdefABCDEF", 2) ==
+ std::string::npos);
+ if (!isHex)
+ {
+ throw std::invalid_argument{"Element is not hexadecimal string"};
+ }
+ return static_cast<uint8_t>(std::stoul(value, 0, 0));
+}
+
+/**
+ * Parses a JSON element containing an array of byte values expressed as a
+ * hexadecimal strings.
+ *
+ * Returns the corresponding C++ uint8_t values.
+ *
+ * Throws an exception if parsing fails.
+ *
+ * @param element JSON element
+ * @return vector of uint8_t
+ */
+std::vector<uint8_t> parseHexByteArray(const nlohmann::json& element);
+
+/**
* Parses a JSON element containing an i2c_write_bit action.
*
* Returns the corresponding C++ I2CWriteBitAction object.
@@ -229,6 +277,19 @@
parseI2CWriteByte(const nlohmann::json& element);
/**
+ * Parses a JSON element containing an i2c_write_bytes action.
+ *
+ * Returns the corresponding C++ I2CWriteBytesAction object.
+ *
+ * Throws an exception if parsing fails.
+ *
+ * @param element JSON element
+ * @return I2CWriteBytesAction object
+ */
+std::unique_ptr<I2CWriteBytesAction>
+ parseI2CWriteBytes(const nlohmann::json& element);
+
+/**
* Parses a JSON element containing an 8-bit signed integer.
*
* Returns the corresponding C++ int8_t value.
@@ -332,31 +393,6 @@
}
/**
- * Parses a JSON element containing a hexadecimal string.
- *
- * Returns the corresponding C++ uint8_t value.
- *
- * Throws an exception if parsing fails.
- *
- * @param element JSON element
- * @return uint8_t value
- */
-inline uint8_t parseStringToUint8(const nlohmann::json& element)
-{
- std::string value = parseString(element);
-
- bool isHex = (value.compare(0, 2, "0x") == 0) && (value.size() > 2) &&
- (value.size() < 5) &&
- (value.find_first_not_of("0123456789abcdefABCDEF", 2) ==
- std::string::npos);
- if (!isHex)
- {
- throw std::invalid_argument{"Element is not hexadecimal string"};
- }
- return static_cast<uint8_t>(std::stoul(value, 0, 0));
-}
-
-/**
* Parses a JSON element containing an 8-bit unsigned integer.
*
* Returns the corresponding C++ uint8_t value.
diff --git a/phosphor-regulators/test/config_file_parser_tests.cpp b/phosphor-regulators/test/config_file_parser_tests.cpp
index 7d36653..df2d87e 100644
--- a/phosphor-regulators/test/config_file_parser_tests.cpp
+++ b/phosphor-regulators/test/config_file_parser_tests.cpp
@@ -20,6 +20,7 @@
#include "i2c_interface.hpp"
#include "i2c_write_bit_action.hpp"
#include "i2c_write_byte_action.hpp"
+#include "i2c_write_bytes_action.hpp"
#include "pmbus_utils.hpp"
#include "pmbus_write_vout_command_action.hpp"
#include "rule.hpp"
@@ -278,7 +279,18 @@
}
// Test where works: i2c_write_bytes action type specified
- // TODO: Not implemented yet
+ {
+ const json element = R"(
+ {
+ "i2c_write_bytes": {
+ "register": "0x0A",
+ "values": [ "0xCC", "0xFF" ]
+ }
+ }
+ )"_json;
+ std::unique_ptr<Action> action = parseAction(element);
+ EXPECT_NE(action.get(), nullptr);
+ }
// Test where works: if action type specified
// TODO: Not implemented yet
@@ -563,6 +575,137 @@
}
}
+TEST(ConfigFileParserTests, ParseHexByte)
+{
+ // Test where works: "0xFF"
+ {
+ const json element = R"( "0xFF" )"_json;
+ uint8_t value = parseHexByte(element);
+ EXPECT_EQ(value, 0xFF);
+ }
+
+ // Test where works: "0xff"
+ {
+ const json element = R"( "0xff" )"_json;
+ uint8_t value = parseHexByte(element);
+ EXPECT_EQ(value, 0xff);
+ }
+
+ // Test where works: "0xf"
+ {
+ const json element = R"( "0xf" )"_json;
+ uint8_t value = parseHexByte(element);
+ EXPECT_EQ(value, 0xf);
+ }
+
+ // Test where fails: "0xfff"
+ try
+ {
+ const json element = R"( "0xfff" )"_json;
+ parseHexByte(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: "0xAG"
+ try
+ {
+ const json element = R"( "0xAG" )"_json;
+ parseHexByte(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: "ff"
+ try
+ {
+ const json element = R"( "ff" )"_json;
+ parseHexByte(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: ""
+ try
+ {
+ const json element = "";
+ parseHexByte(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: "f"
+ try
+ {
+ const json element = R"( "f" )"_json;
+ parseHexByte(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: "0x"
+ try
+ {
+ const json element = R"( "0x" )"_json;
+ parseHexByte(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: "0Xff"
+ try
+ {
+ const json element = R"( "0XFF" )"_json;
+ parseHexByte(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(ConfigFileParserTests, ParseHexByteArray)
+{
+ // Test where works
+ {
+ const json element = R"( [ "0xCC", "0xFF" ] )"_json;
+ std::vector<uint8_t> hexBytes = parseHexByteArray(element);
+ std::vector<uint8_t> expected = {0xcc, 0xff};
+ EXPECT_EQ(hexBytes, expected);
+ }
+
+ // Test where fails: Element is not an array
+ try
+ {
+ const json element = 0;
+ parseHexByteArray(element);
+ ADD_FAILURE() << "Should not have reached this line.";
+ }
+ catch (const std::invalid_argument& e)
+ {
+ EXPECT_STREQ(e.what(), "Element is not an array");
+ }
+}
+
TEST(ConfigFileParserTests, ParseI2CWriteBit)
{
// Test where works
@@ -868,6 +1011,181 @@
}
}
+TEST(ConfigFileParserTests, ParseI2CWriteBytes)
+{
+ // Test where works: Only required properties specified
+ {
+ const json element = R"(
+ {
+ "register": "0x0A",
+ "values": [ "0xCC", "0xFF" ]
+ }
+ )"_json;
+ std::unique_ptr<I2CWriteBytesAction> action =
+ parseI2CWriteBytes(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(), 0);
+ }
+
+ // Test where works: All properties specified
+ {
+ const json element = R"(
+ {
+ "register": "0x0A",
+ "values": [ "0xCC", "0xFF" ],
+ "masks": [ "0x7F", "0x77" ]
+ }
+ )"_json;
+ std::unique_ptr<I2CWriteBytesAction> action =
+ parseI2CWriteBytes(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;
+ parseI2CWriteBytes(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;
+ parseI2CWriteBytes(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;
+ parseI2CWriteBytes(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;
+ parseI2CWriteBytes(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;
+ parseI2CWriteBytes(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;
+ parseI2CWriteBytes(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;
+ parseI2CWriteBytes(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;
+ parseI2CWriteBytes(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, ParseInt8)
{
// Test where works: INT8_MIN
@@ -1396,114 +1714,6 @@
}
}
-TEST(ConfigFileParserTests, ParseStringToUint8)
-{
- // Test where works: "0xFF"
- {
- const json element = R"( "0xFF" )"_json;
- uint8_t value = parseStringToUint8(element);
- EXPECT_EQ(value, 0xFF);
- }
-
- // Test where works: "0xff"
- {
- const json element = R"( "0xff" )"_json;
- uint8_t value = parseStringToUint8(element);
- EXPECT_EQ(value, 0xff);
- }
-
- // Test where works: "0xf"
- {
- const json element = R"( "0xf" )"_json;
- uint8_t value = parseStringToUint8(element);
- EXPECT_EQ(value, 0xf);
- }
-
- // Test where fails: "0xfff"
- try
- {
- const json element = R"( "0xfff" )"_json;
- parseStringToUint8(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: "0xAG"
- try
- {
- const json element = R"( "0xAG" )"_json;
- parseStringToUint8(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: "ff"
- try
- {
- const json element = R"( "ff" )"_json;
- parseStringToUint8(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: ""
- try
- {
- const json element = "";
- parseStringToUint8(element);
- ADD_FAILURE() << "Should not have reached this line.";
- }
- catch (const std::invalid_argument& e)
- {
- EXPECT_STREQ(e.what(), "Element contains an empty string");
- }
-
- // Test where fails: "f"
- try
- {
- const json element = R"( "f" )"_json;
- parseStringToUint8(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: "0x"
- try
- {
- const json element = R"( "0x" )"_json;
- parseStringToUint8(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: "0Xff"
- try
- {
- const json element = R"( "0XFF" )"_json;
- parseStringToUint8(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(ConfigFileParserTests, ParseUint8)
{
// Test where works: 0
diff --git a/phosphor-regulators/test/meson.build b/phosphor-regulators/test/meson.build
index 5f68fba..d23bdf1 100644
--- a/phosphor-regulators/test/meson.build
+++ b/phosphor-regulators/test/meson.build
@@ -66,5 +66,5 @@
libi2c_dev_mock_inc
]
),
- timeout : 150
+ timeout : 180
)