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