blob: 2fdd5ff3e4e6c06de77983ffea317a7de7e447f3 [file] [log] [blame]
Shawn McCarney5ad53942020-02-20 09:33:55 -06001/**
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
26namespace 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 */
37class 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))
Adriana Kobylak0c9a33d2021-09-13 18:05:09 +000063 {}
Shawn McCarney5ad53942020-02-20 09:33:55 -060064
65 /**
66 * Constructor.
67 *
68 * Throws an exception if any of the input parameters are invalid.
69 *
70 * @param reg Device register address. Note: named 'reg' because 'register'
71 * is a reserved keyword.
72 * @param values One or more expected byte values. The bytes must be
73 * specified in the same order as they will be received from
74 * the device (e.g. in little-endian order).
75 * @param masks One or more bit masks. The number of bit masks must match
76 * the number of expected byte values. Each mask specifies
77 * which bits should be compared within the corresponding byte
78 * value. Only the bits with a value of 1 in the mask will be
79 * compared.
80 */
81 explicit I2CCompareBytesAction(uint8_t reg,
82 const std::vector<uint8_t>& values,
83 const std::vector<uint8_t>& masks) :
84 reg{reg},
85 values{values}, masks{masks}
86 {
87 // Values vector must not be empty
88 if (values.size() < 1)
89 {
90 throw std::invalid_argument{"Values vector is empty"};
91 }
92
93 // Masks vector must have same size as values vector
94 if (masks.size() != values.size())
95 {
96 throw std::invalid_argument{"Masks vector has invalid size"};
97 }
98 }
99
100 /**
101 * Executes this action.
102 *
103 * Compares device register bytes to a list of expected values using the
104 * I2C interface.
105 *
106 * All of the bytes will be read in a single I2C operation.
107 *
108 * The device register, byte values, and bit masks (if any) were specified
109 * in the constructor.
110 *
111 * The device is obtained from the specified action environment.
112 *
113 * Throws an exception if an error occurs.
114 *
115 * @param environment action execution environment
116 * @return true if the register bytes contained the expected values,
117 * otherwise returns false.
118 */
119 virtual bool execute(ActionEnvironment& environment) override;
120
121 /**
122 * Returns the device register address.
123 *
124 * @return register address
125 */
126 uint8_t getRegister() const
127 {
128 return reg;
129 }
130
131 /**
132 * Returns the expected byte values.
133 *
134 * @return expected values
135 */
136 const std::vector<uint8_t>& getValues() const
137 {
138 return values;
139 }
140
141 /**
142 * Returns the bit masks.
143 *
144 * Each mask specifies which bits should be compared within the
145 * corresponding byte value. Only the bits with a value of 1 in the mask
146 * will be compared.
147 *
148 * @return bit masks
149 */
150 const std::vector<uint8_t>& getMasks() const
151 {
152 return masks;
153 }
154
155 /**
156 * Returns a string description of this action.
157 *
158 * @return description of action
159 */
160 virtual std::string toString() const override;
161
162 private:
163 /**
164 * Device register address. Note: named 'reg' because 'register' is a
165 * reserved keyword.
166 */
167 const uint8_t reg{0x00};
168
169 /**
170 * Expected byte values.
171 */
172 const std::vector<uint8_t> values{};
173
174 /**
175 * Bit masks. Each mask specifies which bits should be compared within the
176 * corresponding byte value. Only the bits with a value of 1 in the mask
177 * will be compared.
178 */
179 const std::vector<uint8_t> masks{};
180};
181
182} // namespace phosphor::power::regulators