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.