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)
 {