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     numTypes = 6
40 };
41 
42 /**
43  * @class ErrorHistory
44  *
45  * History of which error types have been logged.
46  *
47  * The class is used to avoid creating duplicate error log entries.
48  */
49 class ErrorHistory
50 {
51   public:
52     // Specify which compiler-generated methods we want
53     ErrorHistory() = default;
54     ErrorHistory(const ErrorHistory&) = default;
55     ErrorHistory(ErrorHistory&&) = default;
56     ErrorHistory& operator=(const ErrorHistory&) = default;
57     ErrorHistory& operator=(ErrorHistory&&) = default;
58     ~ErrorHistory() = default;
59 
60     /**
61      * Clears the error history.
62      *
63      * Sets all error types to a 'not logged' state.
64      */
65     void clear()
66     {
67         // Set all bits to false
68         history.reset();
69     }
70 
71     /**
72      * Sets whether the specified error type has been logged.
73      *
74      * @param errorType error type
75      * @param wasLogged indicates whether an error was logged
76      */
77     void setWasLogged(ErrorType errorType, bool wasLogged)
78     {
79         // Set bit value for the specified error type
80         history[static_cast<int>(errorType)] = wasLogged;
81     }
82 
83     /**
84      * Returns whether the specified error type has been logged.
85      *
86      * @param errorType error type
87      * @return whether specified error type was logged
88      */
89     bool wasLogged(ErrorType errorType) const
90     {
91         // Return bit value for the specified error type
92         return history[static_cast<int>(errorType)];
93     }
94 
95   private:
96     /**
97      * Bitset used to track which error types have been logged.
98      *
99      * Each bit indicates whether one error type was logged.
100      *
101      * Each ErrorType enum value is the position of the corresponding bit in the
102      * bitset.
103      *
104      * The numTypes enum value is the number of bits needed in the bitset.
105      */
106     std::bitset<static_cast<int>(ErrorType::numTypes)> history{};
107 };
108 
109 } // namespace phosphor::power::regulators
110