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_bytes_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 
execute(ActionEnvironment & environment)29 bool I2CCompareBytesAction::execute(ActionEnvironment& environment)
30 {
31     bool isEqual{true};
32     try
33     {
34         // Read actual device register values.  Use I2C mode where the number of
35         // bytes to read is explicitly specified.
36         i2c::I2CInterface& interface = getI2CInterface(environment);
37         uint8_t size = values.size();
38         uint8_t actualValues[UINT8_MAX];
39         interface.read(reg, size, actualValues, i2c::I2CInterface::Mode::I2C);
40 
41         // Compare actual byte values to expected byte values
42         for (unsigned int i = 0; i < values.size(); ++i)
43         {
44             if ((actualValues[i] & masks[i]) != values[i])
45             {
46                 isEqual = false;
47                 break;
48             }
49         }
50     }
51     catch (const i2c::I2CException& e)
52     {
53         // Nest I2CException within an ActionError so caller will have both the
54         // low level I2C error information and the action information
55         std::throw_with_nested(ActionError(*this));
56     }
57     return isEqual;
58 }
59 
toString() const60 std::string I2CCompareBytesAction::toString() const
61 {
62     std::ostringstream ss;
63     ss << "i2c_compare_bytes: { register: 0x" << std::hex << std::uppercase
64        << static_cast<uint16_t>(reg) << ", values: [ ";
65     for (unsigned int i = 0; i < values.size(); ++i)
66     {
67         ss << ((i > 0) ? ", " : "") << "0x" << static_cast<uint16_t>(values[i]);
68     }
69     ss << " ], masks: [ ";
70     for (unsigned int i = 0; i < masks.size(); ++i)
71     {
72         ss << ((i > 0) ? ", " : "") << "0x" << static_cast<uint16_t>(masks[i]);
73     }
74     ss << " ] }";
75     return ss.str();
76 }
77 
78 } // namespace phosphor::power::regulators
79