Shawn McCarney | 66332cd | 2020-07-13 19:14:18 -0500 | [diff] [blame] | 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 | |
Shawn McCarney | 2f1b7ba | 2021-03-17 10:21:14 -0500 | [diff] [blame] | 18 | #include <bitset> |
Shawn McCarney | 66332cd | 2020-07-13 19:14:18 -0500 | [diff] [blame] | 19 | |
| 20 | namespace phosphor::power::regulators |
| 21 | { |
| 22 | |
| 23 | /** |
Shawn McCarney | 2f1b7ba | 2021-03-17 10:21:14 -0500 | [diff] [blame] | 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, |
Shawn McCarney | 3f7da6e | 2021-08-27 14:15:11 -0500 | [diff] [blame] | 39 | phaseFaultN = 6, |
| 40 | phaseFaultNPlus1 = 7, |
| 41 | numTypes = 8 |
Shawn McCarney | 2f1b7ba | 2021-03-17 10:21:14 -0500 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | /** |
Shawn McCarney | 66332cd | 2020-07-13 19:14:18 -0500 | [diff] [blame] | 45 | * @class ErrorHistory |
| 46 | * |
Shawn McCarney | 2f1b7ba | 2021-03-17 10:21:14 -0500 | [diff] [blame] | 47 | * History of which error types have been logged. |
Shawn McCarney | 66332cd | 2020-07-13 19:14:18 -0500 | [diff] [blame] | 48 | * |
Shawn McCarney | 2f1b7ba | 2021-03-17 10:21:14 -0500 | [diff] [blame] | 49 | * The class is used to avoid creating duplicate error log entries. |
Shawn McCarney | 66332cd | 2020-07-13 19:14:18 -0500 | [diff] [blame] | 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. |
Shawn McCarney | 2f1b7ba | 2021-03-17 10:21:14 -0500 | [diff] [blame] | 64 | * |
| 65 | * Sets all error types to a 'not logged' state. |
Shawn McCarney | 66332cd | 2020-07-13 19:14:18 -0500 | [diff] [blame] | 66 | */ |
| 67 | void clear() |
| 68 | { |
Shawn McCarney | 2f1b7ba | 2021-03-17 10:21:14 -0500 | [diff] [blame] | 69 | // Set all bits to false |
| 70 | history.reset(); |
Shawn McCarney | 66332cd | 2020-07-13 19:14:18 -0500 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
Shawn McCarney | 2f1b7ba | 2021-03-17 10:21:14 -0500 | [diff] [blame] | 74 | * Sets whether the specified error type has been logged. |
Shawn McCarney | 66332cd | 2020-07-13 19:14:18 -0500 | [diff] [blame] | 75 | * |
Shawn McCarney | 2f1b7ba | 2021-03-17 10:21:14 -0500 | [diff] [blame] | 76 | * @param errorType error type |
| 77 | * @param wasLogged indicates whether an error was logged |
Shawn McCarney | 66332cd | 2020-07-13 19:14:18 -0500 | [diff] [blame] | 78 | */ |
Shawn McCarney | 2f1b7ba | 2021-03-17 10:21:14 -0500 | [diff] [blame] | 79 | void setWasLogged(ErrorType errorType, bool wasLogged) |
Shawn McCarney | 66332cd | 2020-07-13 19:14:18 -0500 | [diff] [blame] | 80 | { |
Shawn McCarney | 2f1b7ba | 2021-03-17 10:21:14 -0500 | [diff] [blame] | 81 | // Set bit value for the specified error type |
| 82 | history[static_cast<int>(errorType)] = wasLogged; |
Shawn McCarney | 66332cd | 2020-07-13 19:14:18 -0500 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /** |
Shawn McCarney | 2f1b7ba | 2021-03-17 10:21:14 -0500 | [diff] [blame] | 86 | * Returns whether the specified error type has been logged. |
| 87 | * |
| 88 | * @param errorType error type |
| 89 | * @return whether specified error type was logged |
Shawn McCarney | 66332cd | 2020-07-13 19:14:18 -0500 | [diff] [blame] | 90 | */ |
Shawn McCarney | 2f1b7ba | 2021-03-17 10:21:14 -0500 | [diff] [blame] | 91 | bool wasLogged(ErrorType errorType) const |
| 92 | { |
| 93 | // Return bit value for the specified error type |
| 94 | return history[static_cast<int>(errorType)]; |
| 95 | } |
Shawn McCarney | 66332cd | 2020-07-13 19:14:18 -0500 | [diff] [blame] | 96 | |
Shawn McCarney | 2f1b7ba | 2021-03-17 10:21:14 -0500 | [diff] [blame] | 97 | private: |
Shawn McCarney | 66332cd | 2020-07-13 19:14:18 -0500 | [diff] [blame] | 98 | /** |
Shawn McCarney | 2f1b7ba | 2021-03-17 10:21:14 -0500 | [diff] [blame] | 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. |
Shawn McCarney | 66332cd | 2020-07-13 19:14:18 -0500 | [diff] [blame] | 107 | */ |
Shawn McCarney | 2f1b7ba | 2021-03-17 10:21:14 -0500 | [diff] [blame] | 108 | std::bitset<static_cast<int>(ErrorType::numTypes)> history{}; |
Shawn McCarney | 66332cd | 2020-07-13 19:14:18 -0500 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | } // namespace phosphor::power::regulators |