Shawn McCarney | 5ad5394 | 2020-02-20 09:33:55 -0600 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2020 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #pragma once |
| 17 | |
| 18 | #include "action_environment.hpp" |
| 19 | #include "i2c_action.hpp" |
| 20 | |
| 21 | #include <cstdint> |
| 22 | #include <stdexcept> |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
| 26 | namespace phosphor::power::regulators |
| 27 | { |
| 28 | |
| 29 | /** |
| 30 | * @class I2CCompareBytesAction |
| 31 | * |
| 32 | * Compares device register bytes to a list of expected values. Communicates |
| 33 | * with the device directly using the I2C interface. |
| 34 | * |
| 35 | * Implements the i2c_compare_bytes action in the JSON config file. |
| 36 | */ |
| 37 | class I2CCompareBytesAction : public I2CAction |
| 38 | { |
| 39 | public: |
| 40 | // Specify which compiler-generated methods we want |
| 41 | I2CCompareBytesAction() = delete; |
| 42 | I2CCompareBytesAction(const I2CCompareBytesAction&) = delete; |
| 43 | I2CCompareBytesAction(I2CCompareBytesAction&&) = delete; |
| 44 | I2CCompareBytesAction& operator=(const I2CCompareBytesAction&) = delete; |
| 45 | I2CCompareBytesAction& operator=(I2CCompareBytesAction&&) = delete; |
| 46 | virtual ~I2CCompareBytesAction() = default; |
| 47 | |
| 48 | /** |
| 49 | * Constructor. |
| 50 | * |
| 51 | * Throws an exception if any of the input parameters are invalid. |
| 52 | * |
| 53 | * @param reg Device register address. Note: named 'reg' because 'register' |
| 54 | * is a reserved keyword. |
| 55 | * @param values One or more expected byte values. The bytes must be |
| 56 | * specified in the same order as they will be received from |
| 57 | * the device (e.g. in little-endian order). |
| 58 | */ |
| 59 | explicit I2CCompareBytesAction(uint8_t reg, |
| 60 | const std::vector<uint8_t>& values) : |
| 61 | I2CCompareBytesAction(reg, values, |
| 62 | std::vector<uint8_t>(values.size(), 0xFF)) |
| 63 | { |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Constructor. |
| 68 | * |
| 69 | * Throws an exception if any of the input parameters are invalid. |
| 70 | * |
| 71 | * @param reg Device register address. Note: named 'reg' because 'register' |
| 72 | * is a reserved keyword. |
| 73 | * @param values One or more expected byte values. The bytes must be |
| 74 | * specified in the same order as they will be received from |
| 75 | * the device (e.g. in little-endian order). |
| 76 | * @param masks One or more bit masks. The number of bit masks must match |
| 77 | * the number of expected byte values. Each mask specifies |
| 78 | * which bits should be compared within the corresponding byte |
| 79 | * value. Only the bits with a value of 1 in the mask will be |
| 80 | * compared. |
| 81 | */ |
| 82 | explicit I2CCompareBytesAction(uint8_t reg, |
| 83 | const std::vector<uint8_t>& values, |
| 84 | const std::vector<uint8_t>& masks) : |
| 85 | reg{reg}, |
| 86 | values{values}, masks{masks} |
| 87 | { |
| 88 | // Values vector must not be empty |
| 89 | if (values.size() < 1) |
| 90 | { |
| 91 | throw std::invalid_argument{"Values vector is empty"}; |
| 92 | } |
| 93 | |
| 94 | // Masks vector must have same size as values vector |
| 95 | if (masks.size() != values.size()) |
| 96 | { |
| 97 | throw std::invalid_argument{"Masks vector has invalid size"}; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Executes this action. |
| 103 | * |
| 104 | * Compares device register bytes to a list of expected values using the |
| 105 | * I2C interface. |
| 106 | * |
| 107 | * All of the bytes will be read in a single I2C operation. |
| 108 | * |
| 109 | * The device register, byte values, and bit masks (if any) were specified |
| 110 | * in the constructor. |
| 111 | * |
| 112 | * The device is obtained from the specified action environment. |
| 113 | * |
| 114 | * Throws an exception if an error occurs. |
| 115 | * |
| 116 | * @param environment action execution environment |
| 117 | * @return true if the register bytes contained the expected values, |
| 118 | * otherwise returns false. |
| 119 | */ |
| 120 | virtual bool execute(ActionEnvironment& environment) override; |
| 121 | |
| 122 | /** |
| 123 | * Returns the device register address. |
| 124 | * |
| 125 | * @return register address |
| 126 | */ |
| 127 | uint8_t getRegister() const |
| 128 | { |
| 129 | return reg; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Returns the expected byte values. |
| 134 | * |
| 135 | * @return expected values |
| 136 | */ |
| 137 | const std::vector<uint8_t>& getValues() const |
| 138 | { |
| 139 | return values; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Returns the bit masks. |
| 144 | * |
| 145 | * Each mask specifies which bits should be compared within the |
| 146 | * corresponding byte value. Only the bits with a value of 1 in the mask |
| 147 | * will be compared. |
| 148 | * |
| 149 | * @return bit masks |
| 150 | */ |
| 151 | const std::vector<uint8_t>& getMasks() const |
| 152 | { |
| 153 | return masks; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Returns a string description of this action. |
| 158 | * |
| 159 | * @return description of action |
| 160 | */ |
| 161 | virtual std::string toString() const override; |
| 162 | |
| 163 | private: |
| 164 | /** |
| 165 | * Device register address. Note: named 'reg' because 'register' is a |
| 166 | * reserved keyword. |
| 167 | */ |
| 168 | const uint8_t reg{0x00}; |
| 169 | |
| 170 | /** |
| 171 | * Expected byte values. |
| 172 | */ |
| 173 | const std::vector<uint8_t> values{}; |
| 174 | |
| 175 | /** |
| 176 | * Bit masks. Each mask specifies which bits should be compared within the |
| 177 | * corresponding byte value. Only the bits with a value of 1 in the mask |
| 178 | * will be compared. |
| 179 | */ |
| 180 | const std::vector<uint8_t> masks{}; |
| 181 | }; |
| 182 | |
| 183 | } // namespace phosphor::power::regulators |