regulators: Implement i2c_compare_bytes action

Implement the i2c_compare_bytes action in the JSON config file.  See
i2c_compare_bytes.md for more information about this action.

Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: I6997f647a2b899f08f06de43aba62d32f88e1911
diff --git a/phosphor-regulators/src/actions/i2c_compare_bytes_action.cpp b/phosphor-regulators/src/actions/i2c_compare_bytes_action.cpp
new file mode 100644
index 0000000..c7f56f0
--- /dev/null
+++ b/phosphor-regulators/src/actions/i2c_compare_bytes_action.cpp
@@ -0,0 +1,78 @@
+/**
+ * Copyright © 2020 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "i2c_compare_bytes_action.hpp"
+
+#include "action_error.hpp"
+#include "i2c_interface.hpp"
+
+#include <exception>
+#include <ios>
+#include <sstream>
+
+namespace phosphor::power::regulators
+{
+
+bool I2CCompareBytesAction::execute(ActionEnvironment& environment)
+{
+    bool isEqual{true};
+    try
+    {
+        // Read actual device register values.  Use I2C mode where the number of
+        // bytes to read is explicitly specified.
+        i2c::I2CInterface& interface = getI2CInterface(environment);
+        uint8_t size = values.size();
+        uint8_t actualValues[UINT8_MAX];
+        interface.read(reg, size, actualValues, i2c::I2CInterface::Mode::I2C);
+
+        // Compare actual byte values to expected byte values
+        for (unsigned int i = 0; i < values.size(); ++i)
+        {
+            if ((actualValues[i] & masks[i]) != values[i])
+            {
+                isEqual = false;
+                break;
+            }
+        }
+    }
+    catch (const i2c::I2CException& e)
+    {
+        // Nest I2CException within an ActionError so caller will have both the
+        // low level I2C error information and the action information
+        std::throw_with_nested(ActionError(*this));
+    }
+    return isEqual;
+}
+
+std::string I2CCompareBytesAction::toString() const
+{
+    std::ostringstream ss;
+    ss << "i2c_compare_bytes: { register: 0x" << std::hex << std::uppercase
+       << static_cast<uint16_t>(reg) << ", values: [ ";
+    for (unsigned int i = 0; i < values.size(); ++i)
+    {
+        ss << ((i > 0) ? ", " : "") << "0x" << static_cast<uint16_t>(values[i]);
+    }
+    ss << " ], masks: [ ";
+    for (unsigned int i = 0; i < masks.size(); ++i)
+    {
+        ss << ((i > 0) ? ", " : "") << "0x" << static_cast<uint16_t>(masks[i]);
+    }
+    ss << " ] }";
+    return ss.str();
+}
+
+} // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/src/actions/i2c_compare_bytes_action.hpp b/phosphor-regulators/src/actions/i2c_compare_bytes_action.hpp
new file mode 100644
index 0000000..3f0ec83
--- /dev/null
+++ b/phosphor-regulators/src/actions/i2c_compare_bytes_action.hpp
@@ -0,0 +1,183 @@
+/**
+ * Copyright © 2020 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include "action_environment.hpp"
+#include "i2c_action.hpp"
+
+#include <cstdint>
+#include <stdexcept>
+#include <string>
+#include <vector>
+
+namespace phosphor::power::regulators
+{
+
+/**
+ * @class I2CCompareBytesAction
+ *
+ * Compares device register bytes to a list of expected values.  Communicates
+ * with the device directly using the I2C interface.
+ *
+ * Implements the i2c_compare_bytes action in the JSON config file.
+ */
+class I2CCompareBytesAction : public I2CAction
+{
+  public:
+    // Specify which compiler-generated methods we want
+    I2CCompareBytesAction() = delete;
+    I2CCompareBytesAction(const I2CCompareBytesAction&) = delete;
+    I2CCompareBytesAction(I2CCompareBytesAction&&) = delete;
+    I2CCompareBytesAction& operator=(const I2CCompareBytesAction&) = delete;
+    I2CCompareBytesAction& operator=(I2CCompareBytesAction&&) = delete;
+    virtual ~I2CCompareBytesAction() = default;
+
+    /**
+     * Constructor.
+     *
+     * Throws an exception if any of the input parameters are invalid.
+     *
+     * @param reg Device register address.  Note: named 'reg' because 'register'
+     *            is a reserved keyword.
+     * @param values One or more expected byte values.  The bytes must be
+     *               specified in the same order as they will be received from
+     *               the device (e.g. in little-endian order).
+     */
+    explicit I2CCompareBytesAction(uint8_t reg,
+                                   const std::vector<uint8_t>& values) :
+        I2CCompareBytesAction(reg, values,
+                              std::vector<uint8_t>(values.size(), 0xFF))
+    {
+    }
+
+    /**
+     * Constructor.
+     *
+     * Throws an exception if any of the input parameters are invalid.
+     *
+     * @param reg Device register address.  Note: named 'reg' because 'register'
+     *            is a reserved keyword.
+     * @param values One or more expected byte values.  The bytes must be
+     *               specified in the same order as they will be received from
+     *               the device (e.g. in little-endian order).
+     * @param masks One or more bit masks.  The number of bit masks must match
+     *              the number of expected byte values.  Each mask specifies
+     *              which bits should be compared within the corresponding byte
+     *              value.  Only the bits with a value of 1 in the mask will be
+     *              compared.
+     */
+    explicit I2CCompareBytesAction(uint8_t reg,
+                                   const std::vector<uint8_t>& values,
+                                   const std::vector<uint8_t>& masks) :
+        reg{reg},
+        values{values}, masks{masks}
+    {
+        // Values vector must not be empty
+        if (values.size() < 1)
+        {
+            throw std::invalid_argument{"Values vector is empty"};
+        }
+
+        // Masks vector must have same size as values vector
+        if (masks.size() != values.size())
+        {
+            throw std::invalid_argument{"Masks vector has invalid size"};
+        }
+    }
+
+    /**
+     * Executes this action.
+     *
+     * Compares device register bytes to a list of expected values using the
+     * I2C interface.
+     *
+     * All of the bytes will be read in a single I2C operation.
+     *
+     * The device register, byte values, and bit masks (if any) were specified
+     * in the constructor.
+     *
+     * The device is obtained from the specified action environment.
+     *
+     * Throws an exception if an error occurs.
+     *
+     * @param environment action execution environment
+     * @return true if the register bytes contained the expected values,
+     *         otherwise returns false.
+     */
+    virtual bool execute(ActionEnvironment& environment) override;
+
+    /**
+     * Returns the device register address.
+     *
+     * @return register address
+     */
+    uint8_t getRegister() const
+    {
+        return reg;
+    }
+
+    /**
+     * Returns the expected byte values.
+     *
+     * @return expected values
+     */
+    const std::vector<uint8_t>& getValues() const
+    {
+        return values;
+    }
+
+    /**
+     * Returns the bit masks.
+     *
+     * Each mask specifies which bits should be compared within the
+     * corresponding byte value.  Only the bits with a value of 1 in the mask
+     * will be compared.
+     *
+     * @return bit masks
+     */
+    const std::vector<uint8_t>& getMasks() const
+    {
+        return masks;
+    }
+
+    /**
+     * Returns a string description of this action.
+     *
+     * @return description of action
+     */
+    virtual std::string toString() const override;
+
+  private:
+    /**
+     * Device register address.  Note: named 'reg' because 'register' is a
+     * reserved keyword.
+     */
+    const uint8_t reg{0x00};
+
+    /**
+     * Expected byte values.
+     */
+    const std::vector<uint8_t> values{};
+
+    /**
+     * Bit masks.  Each mask specifies which bits should be compared within the
+     * corresponding byte value.  Only the bits with a value of 1 in the mask
+     * will be compared.
+     */
+    const std::vector<uint8_t> masks{};
+};
+
+} // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/src/meson.build b/phosphor-regulators/src/meson.build
index f100d77..2cc8347 100644
--- a/phosphor-regulators/src/meson.build
+++ b/phosphor-regulators/src/meson.build
@@ -8,7 +8,8 @@
 
     'actions/if_action.cpp',
     'actions/i2c_compare_bit_action.cpp',
-    'actions/i2c_compare_byte_action.cpp'
+    'actions/i2c_compare_byte_action.cpp',
+    'actions/i2c_compare_bytes_action.cpp'
 ]
 
 phosphor_regulators_library = static_library(