blob: f5afdae309a251fa9c05b6024621a3919c24db1d [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,
Shawn McCarney3f7da6e2021-08-27 14:15:11 -050039 phaseFaultN = 6,
40 phaseFaultNPlus1 = 7,
41 numTypes = 8
Shawn McCarney2f1b7ba2021-03-17 10:21:14 -050042};
43
44/**
Shawn McCarney66332cd2020-07-13 19:14:18 -050045 * @class ErrorHistory
46 *
Shawn McCarney2f1b7ba2021-03-17 10:21:14 -050047 * History of which error types have been logged.
Shawn McCarney66332cd2020-07-13 19:14:18 -050048 *
Shawn McCarney2f1b7ba2021-03-17 10:21:14 -050049 * The class is used to avoid creating duplicate error log entries.
Shawn McCarney66332cd2020-07-13 19:14:18 -050050 */
51class 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.
Shawn McCarney2f1b7ba2021-03-17 10:21:14 -050064 *
65 * Sets all error types to a 'not logged' state.
Shawn McCarney66332cd2020-07-13 19:14:18 -050066 */
67 void clear()
68 {
Shawn McCarney2f1b7ba2021-03-17 10:21:14 -050069 // Set all bits to false
70 history.reset();
Shawn McCarney66332cd2020-07-13 19:14:18 -050071 }
72
73 /**
Shawn McCarney2f1b7ba2021-03-17 10:21:14 -050074 * Sets whether the specified error type has been logged.
Shawn McCarney66332cd2020-07-13 19:14:18 -050075 *
Shawn McCarney2f1b7ba2021-03-17 10:21:14 -050076 * @param errorType error type
77 * @param wasLogged indicates whether an error was logged
Shawn McCarney66332cd2020-07-13 19:14:18 -050078 */
Shawn McCarney2f1b7ba2021-03-17 10:21:14 -050079 void setWasLogged(ErrorType errorType, bool wasLogged)
Shawn McCarney66332cd2020-07-13 19:14:18 -050080 {
Shawn McCarney2f1b7ba2021-03-17 10:21:14 -050081 // Set bit value for the specified error type
82 history[static_cast<int>(errorType)] = wasLogged;
Shawn McCarney66332cd2020-07-13 19:14:18 -050083 }
84
85 /**
Shawn McCarney2f1b7ba2021-03-17 10:21:14 -050086 * Returns whether the specified error type has been logged.
87 *
88 * @param errorType error type
89 * @return whether specified error type was logged
Shawn McCarney66332cd2020-07-13 19:14:18 -050090 */
Shawn McCarney2f1b7ba2021-03-17 10:21:14 -050091 bool wasLogged(ErrorType errorType) const
92 {
93 // Return bit value for the specified error type
94 return history[static_cast<int>(errorType)];
95 }
Shawn McCarney66332cd2020-07-13 19:14:18 -050096
Shawn McCarney2f1b7ba2021-03-17 10:21:14 -050097 private:
Shawn McCarney66332cd2020-07-13 19:14:18 -050098 /**
Shawn McCarney2f1b7ba2021-03-17 10:21:14 -050099 * 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.
Shawn McCarney66332cd2020-07-13 19:14:18 -0500107 */
Shawn McCarney2f1b7ba2021-03-17 10:21:14 -0500108 std::bitset<static_cast<int>(ErrorType::numTypes)> history{};
Shawn McCarney66332cd2020-07-13 19:14:18 -0500109};
110
111} // namespace phosphor::power::regulators