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 <bitset>
19 
20 namespace phosphor::power::regulators
21 {
22 
23 /**
24  * @enum ErrorType
25  *
26  * The error types tracked by the ErrorHistory class.
27  *
28  * The enumerators must have consecutive integer values that start at 0.  The
29  * value of the last enumerator must be the number of error types.
30  */
31 enum class ErrorType
32 {
33     configFile = 0,
34     dbus = 1,
35     i2c = 2,
36     internal = 3,
37     pmbus = 4,
38     writeVerification = 5,
39     phaseFaultN = 6,
40     phaseFaultNPlus1 = 7,
41     numTypes = 8
42 };
43 
44 /**
45  * @class ErrorHistory
46  *
47  * History of which error types have been logged.
48  *
49  * The class is used to avoid creating duplicate error log entries.
50  */
51 class ErrorHistory
52 {
53   public:
54     // Specify which compiler-generated methods we want
55     ErrorHistory() = default;
56     ErrorHistory(const ErrorHistory&) = default;
57     ErrorHistory(ErrorHistory&&) = default;
58     ErrorHistory& operator=(const ErrorHistory&) = default;
59     ErrorHistory& operator=(ErrorHistory&&) = default;
60     ~ErrorHistory() = default;
61 
62     /**
63      * Clears the error history.
64      *
65      * Sets all error types to a 'not logged' state.
66      */
67     void clear()
68     {
69         // Set all bits to false
70         history.reset();
71     }
72 
73     /**
74      * Sets whether the specified error type has been logged.
75      *
76      * @param errorType error type
77      * @param wasLogged indicates whether an error was logged
78      */
79     void setWasLogged(ErrorType errorType, bool wasLogged)
80     {
81         // Set bit value for the specified error type
82         history[static_cast<int>(errorType)] = wasLogged;
83     }
84 
85     /**
86      * Returns whether the specified error type has been logged.
87      *
88      * @param errorType error type
89      * @return whether specified error type was logged
90      */
91     bool wasLogged(ErrorType errorType) const
92     {
93         // Return bit value for the specified error type
94         return history[static_cast<int>(errorType)];
95     }
96 
97   private:
98     /**
99      * Bitset used to track which error types have been logged.
100      *
101      * Each bit indicates whether one error type was logged.
102      *
103      * Each ErrorType enum value is the position of the corresponding bit in the
104      * bitset.
105      *
106      * The numTypes enum value is the number of bits needed in the bitset.
107      */
108     std::bitset<static_cast<int>(ErrorType::numTypes)> history{};
109 };
110 
111 } // namespace phosphor::power::regulators
112