blob: 8a2a01134c46b80f3bbe306dfc7620e9cddbaf7f [file] [log] [blame]
Shawn McCarney66332cd2020-07-13 19:14:18 -05001/**
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
Shawn McCarney2f1b7ba2021-03-17 10:21:14 -050018#include <bitset>
Shawn McCarney66332cd2020-07-13 19:14:18 -050019
20namespace phosphor::power::regulators
21{
22
23/**
Shawn McCarney2f1b7ba2021-03-17 10:21:14 -050024 * @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 */
31enum 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/**
Shawn McCarney66332cd2020-07-13 19:14:18 -050043 * @class ErrorHistory
44 *
Shawn McCarney2f1b7ba2021-03-17 10:21:14 -050045 * History of which error types have been logged.
Shawn McCarney66332cd2020-07-13 19:14:18 -050046 *
Shawn McCarney2f1b7ba2021-03-17 10:21:14 -050047 * The class is used to avoid creating duplicate error log entries.
Shawn McCarney66332cd2020-07-13 19:14:18 -050048 */
49class 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.
Shawn McCarney2f1b7ba2021-03-17 10:21:14 -050062 *
63 * Sets all error types to a 'not logged' state.
Shawn McCarney66332cd2020-07-13 19:14:18 -050064 */
65 void clear()
66 {
Shawn McCarney2f1b7ba2021-03-17 10:21:14 -050067 // Set all bits to false
68 history.reset();
Shawn McCarney66332cd2020-07-13 19:14:18 -050069 }
70
71 /**
Shawn McCarney2f1b7ba2021-03-17 10:21:14 -050072 * Sets whether the specified error type has been logged.
Shawn McCarney66332cd2020-07-13 19:14:18 -050073 *
Shawn McCarney2f1b7ba2021-03-17 10:21:14 -050074 * @param errorType error type
75 * @param wasLogged indicates whether an error was logged
Shawn McCarney66332cd2020-07-13 19:14:18 -050076 */
Shawn McCarney2f1b7ba2021-03-17 10:21:14 -050077 void setWasLogged(ErrorType errorType, bool wasLogged)
Shawn McCarney66332cd2020-07-13 19:14:18 -050078 {
Shawn McCarney2f1b7ba2021-03-17 10:21:14 -050079 // Set bit value for the specified error type
80 history[static_cast<int>(errorType)] = wasLogged;
Shawn McCarney66332cd2020-07-13 19:14:18 -050081 }
82
83 /**
Shawn McCarney2f1b7ba2021-03-17 10:21:14 -050084 * Returns whether the specified error type has been logged.
85 *
86 * @param errorType error type
87 * @return whether specified error type was logged
Shawn McCarney66332cd2020-07-13 19:14:18 -050088 */
Shawn McCarney2f1b7ba2021-03-17 10:21:14 -050089 bool wasLogged(ErrorType errorType) const
90 {
91 // Return bit value for the specified error type
92 return history[static_cast<int>(errorType)];
93 }
Shawn McCarney66332cd2020-07-13 19:14:18 -050094
Shawn McCarney2f1b7ba2021-03-17 10:21:14 -050095 private:
Shawn McCarney66332cd2020-07-13 19:14:18 -050096 /**
Shawn McCarney2f1b7ba2021-03-17 10:21:14 -050097 * 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.
Shawn McCarney66332cd2020-07-13 19:14:18 -0500105 */
Shawn McCarney2f1b7ba2021-03-17 10:21:14 -0500106 std::bitset<static_cast<int>(ErrorType::numTypes)> history{};
Shawn McCarney66332cd2020-07-13 19:14:18 -0500107};
108
109} // namespace phosphor::power::regulators