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 
17 #include "i2c_compare_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 
26 namespace phosphor::power::regulators
27 {
28 
29 bool I2CCompareByteAction::execute(ActionEnvironment& environment)
30 {
31     bool isEqual{false};
32     try
33     {
34         // Read actual value of device register
35         uint8_t actualValue{0x00};
36         i2c::I2CInterface& interface = getI2CInterface(environment);
37         interface.read(reg, actualValue);
38 
39         // Modify actual value to only include bits specified in the mask
40         actualValue &= mask;
41 
42         // Check if actual value equals expected value
43         isEqual = (actualValue == value);
44     }
45     catch (const i2c::I2CException& e)
46     {
47         // Nest I2CException within an ActionError so caller will have both the
48         // low level I2C error information and the action information
49         std::throw_with_nested(ActionError(*this));
50     }
51     return isEqual;
52 }
53 
54 std::string I2CCompareByteAction::toString() const
55 {
56     std::ostringstream ss;
57     ss << "i2c_compare_byte: { register: 0x" << std::hex << std::uppercase
58        << static_cast<uint16_t>(reg) << ", value: 0x"
59        << static_cast<uint16_t>(value) << ", mask: 0x"
60        << static_cast<uint16_t>(mask) << " }";
61     return ss.str();
62 }
63 
64 } // namespace phosphor::power::regulators
65