1 /**
2  * Copyright © 2021 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 "compare_vpd_action.hpp"
18 
19 #include "action_error.hpp"
20 
21 #include <exception>
22 #include <sstream>
23 
24 namespace phosphor::power::regulators
25 {
26 
27 bool CompareVPDAction::execute(ActionEnvironment& environment)
28 {
29     bool isEqual{false};
30     try
31     {
32         // Get actual VPD keyword value
33         std::vector<uint8_t> actualValue =
34             environment.getServices().getVPD().getValue(fru, keyword);
35 
36         // Check if actual value equals expected value
37         isEqual = (actualValue == value);
38     }
39     catch (const std::exception& e)
40     {
41         // Nest exception within an ActionError so the caller will have both the
42         // low level error information and the action information.
43         std::throw_with_nested(ActionError(*this));
44     }
45     return isEqual;
46 }
47 
48 std::string CompareVPDAction::toString() const
49 {
50     std::ostringstream ss;
51     ss << "compare_vpd: { ";
52     ss << "fru: " << fru << ", ";
53     ss << "keyword: " << keyword << ", ";
54     ss << "value: [ ";
55     ss << std::hex << std::uppercase;
56     for (unsigned int i = 0; i < value.size(); ++i)
57     {
58         ss << ((i > 0) ? ", " : "") << "0x" << static_cast<uint16_t>(value[i]);
59     }
60     ss << " ] }";
61     return ss.str();
62 }
63 
64 } // namespace phosphor::power::regulators
65