blob: 265717806db59dc47f343e5df9d9eabc278f7b10 [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
17#include "i2c_write_byte_action.hpp"
18
19#include "action_error.hpp"
20#include "i2c_interface.hpp"
21
22#include <exception>
23#include <ios>
24#include <sstream>
25
26namespace phosphor::power::regulators
27{
28
29bool I2CWriteByteAction::execute(ActionEnvironment& environment)
30{
31 try
32 {
33 i2c::I2CInterface& interface = getI2CInterface(environment);
34 uint8_t valueToWrite;
35 if (mask == 0xFF)
36 {
37 valueToWrite = value;
38 }
39 else
40 {
41 // Read current value of device register
42 uint8_t currentValue{0x00};
43 interface.read(reg, currentValue);
44
45 // Combine value to write with current value
46 valueToWrite = (value & mask) | (currentValue & (~mask));
47 }
48
49 // Write value to device register
50 interface.write(reg, valueToWrite);
51 }
52 catch (const i2c::I2CException& e)
53 {
54 // Nest I2CException within an ActionError so caller will have both the
55 // low level I2C error information and the action information
56 std::throw_with_nested(ActionError(*this));
57 }
58 return true;
59}
60
61std::string I2CWriteByteAction::toString() const
62{
63 std::ostringstream ss;
64 ss << "i2c_write_byte: { register: 0x" << std::hex << std::uppercase
65 << static_cast<uint16_t>(reg) << ", value: 0x"
66 << static_cast<uint16_t>(value) << ", mask: 0x"
67 << static_cast<uint16_t>(mask) << " }";
68 return ss.str();
69}
70
71} // namespace phosphor::power::regulators