blob: a32bbdfb552135c18c885e4fdeb4d69ca15e92eb [file] [log] [blame]
Shawn McCarneyb38da992020-02-13 08:55:46 -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 <string>
23
24namespace phosphor::power::regulators
25{
26
27/**
28 * @class I2CCompareByteAction
29 *
30 * Compares a device register to a byte value. Communicates with the device
31 * directly using the I2C interface.
32 *
33 * Implements the i2c_compare_byte action in the JSON config file.
34 */
35class I2CCompareByteAction : public I2CAction
36{
37 public:
38 // Specify which compiler-generated methods we want
39 I2CCompareByteAction() = delete;
40 I2CCompareByteAction(const I2CCompareByteAction&) = delete;
41 I2CCompareByteAction(I2CCompareByteAction&&) = delete;
42 I2CCompareByteAction& operator=(const I2CCompareByteAction&) = delete;
43 I2CCompareByteAction& operator=(I2CCompareByteAction&&) = delete;
44 virtual ~I2CCompareByteAction() = default;
45
46 /**
47 * Constructor.
48 *
49 * @param reg Device register address. Note: named 'reg' because 'register'
50 * is a reserved keyword.
51 * @param value Expected byte value.
52 * @param mask Bit mask. Specifies which bits should be compared within the
53 * byte value. Only the bits with a value of 1 in the mask will
54 * be compared. If not specified, defaults to 0xFF which means
55 * that all bits will be compared.
56 */
57 explicit I2CCompareByteAction(uint8_t reg, uint8_t value,
58 uint8_t mask = 0xFF) :
59 reg{reg},
60 value{value}, mask{mask}
61 {
62 }
63
64 /**
65 * Executes this action.
66 *
67 * Compares a device register to a byte value using the I2C interface.
68 *
69 * The device register, byte value, and mask (if any) were specified in the
70 * constructor.
71 *
72 * The device is obtained from the specified action environment.
73 *
74 * Throws an exception if an error occurs.
75 *
76 * @param environment action execution environment
77 * @return true if the register contained the expected value, otherwise
78 * returns false.
79 */
80 virtual bool execute(ActionEnvironment& environment) override;
81
82 /**
83 * Returns the device register address.
84 *
85 * @return register address
86 */
87 uint8_t getRegister() const
88 {
89 return reg;
90 }
91
92 /**
93 * Returns the expected byte value.
94 *
95 * @return expected value
96 */
97 uint8_t getValue() const
98 {
99 return value;
100 }
101
102 /**
103 * Returns the bit mask.
104 *
105 * Specifies which bits should be compared within the byte value. Only the
106 * bits with a value of 1 in the mask will be compared.
107 *
108 * @return bit mask
109 */
110 uint8_t getMask() const
111 {
112 return mask;
113 }
114
115 /**
116 * Returns a string description of this action.
117 *
118 * @return description of action
119 */
120 virtual std::string toString() const override;
121
122 private:
123 /**
124 * Device register address. Note: named 'reg' because 'register' is a
125 * reserved keyword.
126 */
127 const uint8_t reg{0x00};
128
129 /**
130 * Expected byte value.
131 */
132 const uint8_t value{0x00};
133
134 /**
135 * Bit mask. Specifies which bits should be compared within the byte value.
136 * Only the bits with a value of 1 in the mask will be compared.
137 */
138 const uint8_t mask{0xFF};
139};
140
141} // namespace phosphor::power::regulators