xref: /openbmc/phosphor-power/phosphor-regulators/src/actions/i2c_write_bytes_action.hpp (revision f54021972b91be5058b50e9046bb0dd5a3b22a80)
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 I2CWriteBytesAction
31  *
32  * Writes bytes to a device register.  Communicates with the device directly
33  * using the I2C interface.
34  *
35  * Implements the i2c_write_bytes action in the JSON config file.
36  */
37 class I2CWriteBytesAction : public I2CAction
38 {
39   public:
40     // Specify which compiler-generated methods we want
41     I2CWriteBytesAction() = delete;
42     I2CWriteBytesAction(const I2CWriteBytesAction&) = delete;
43     I2CWriteBytesAction(I2CWriteBytesAction&&) = delete;
44     I2CWriteBytesAction& operator=(const I2CWriteBytesAction&) = delete;
45     I2CWriteBytesAction& operator=(I2CWriteBytesAction&&) = delete;
46     virtual ~I2CWriteBytesAction() = 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 byte values to write.  The bytes must be
56      *               specified in the order required by the device (e.g. in
57      *               little-endian order).
58      */
I2CWriteBytesAction(uint8_t reg,const std::vector<uint8_t> & values)59     explicit I2CWriteBytesAction(uint8_t reg,
60                                  const std::vector<uint8_t>& values) :
61         reg{reg}, values{values}, masks{}
62     {
63         // Values vector must not be empty
64         if (values.size() < 1)
65         {
66             throw std::invalid_argument{"Values vector is empty"};
67         }
68     }
69 
70     /**
71      * Constructor.
72      *
73      * Throws an exception if any of the input parameters are invalid.
74      *
75      * @param reg Device register address.  Note: named 'reg' because 'register'
76      *            is a reserved keyword.
77      * @param values One or more byte values to write.  The bytes must be
78      *               specified in the order required by the device (e.g. in
79      *               little-endian order).
80      * @param masks One or more bit masks.  The number of bit masks must match
81      *              the number of byte values to write.  Each mask specifies
82      *              which bits to write within the corresponding byte value.
83      *              Only the bits with a value of 1 in the mask will be written.
84      */
I2CWriteBytesAction(uint8_t reg,const std::vector<uint8_t> & values,const std::vector<uint8_t> & masks)85     explicit I2CWriteBytesAction(uint8_t reg,
86                                  const std::vector<uint8_t>& values,
87                                  const std::vector<uint8_t>& masks) :
88         reg{reg}, values{values}, masks{masks}
89     {
90         // Values vector must not be empty
91         if (values.size() < 1)
92         {
93             throw std::invalid_argument{"Values vector is empty"};
94         }
95 
96         // Masks vector must have same size as values vector
97         if (masks.size() != values.size())
98         {
99             throw std::invalid_argument{"Masks vector has invalid size"};
100         }
101     }
102 
103     /**
104      * Executes this action.
105      *
106      * Writes bytes to a device register using the I2C interface.
107      *
108      * All of the bytes will be written in a single I2C operation.
109      *
110      * The device register, byte values, and bit masks (if any) were specified
111      * in the constructor.
112      *
113      * The device is obtained from the specified action environment.
114      *
115      * Throws an exception if an error occurs.
116      *
117      * @param environment action execution environment
118      * @return true
119      */
120     virtual bool execute(ActionEnvironment& environment) override;
121 
122     /**
123      * Returns the device register address.
124      *
125      * @return register address
126      */
getRegister() const127     uint8_t getRegister() const
128     {
129         return reg;
130     }
131 
132     /**
133      * Returns the byte values to write.
134      *
135      * @return values to write
136      */
getValues() const137     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 to write within the corresponding byte
146      * value.  Only the bits with a value of 1 in the mask will be written.
147      *
148      * @return bit masks
149      */
getMasks() const150     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      * Byte values to write.
171      */
172     const std::vector<uint8_t> values{};
173 
174     /**
175      * Bit masks.  Each mask specifies which bits to write within the
176      * corresponding byte value.  Only the bits with a value of 1 in the mask
177      * will be written.
178      */
179     const std::vector<uint8_t> masks{};
180 };
181 
182 } // namespace phosphor::power::regulators
183