Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 3 | #include "config.h" |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 4 | |
| 5 | #include "occ_events.hpp" |
| 6 | |
| 7 | #include <unistd.h> |
| 8 | |
George Liu | bcef3b4 | 2021-09-10 12:39:02 +0800 | [diff] [blame] | 9 | #include <filesystem> |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 10 | #include <functional> |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 11 | namespace open_power |
| 12 | { |
| 13 | namespace occ |
| 14 | { |
| 15 | |
George Liu | bcef3b4 | 2021-09-10 12:39:02 +0800 | [diff] [blame] | 16 | namespace fs = std::filesystem; |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 17 | |
Eddie James | 9789e71 | 2022-05-25 15:43:40 -0500 | [diff] [blame] | 18 | constexpr auto PRESENCE_ERROR_PATH = |
Matt Spinler | fec4b0b | 2024-01-04 15:10:37 -0600 | [diff] [blame] | 19 | "org.open_power.OCC.Firmware.Error.PresenceMismatch"; |
| 20 | constexpr auto SAFE_ERROR_PATH = "org.open_power.OCC.Device.Error.SafeState"; |
Chris Cain | 4b82f3e | 2024-04-22 14:44:29 -0500 | [diff] [blame] | 21 | constexpr auto MISSING_OCC_SENSORS_PATH = |
| 22 | "org.open_power.OCC.Firmware.Error.MissingOCCSensors"; |
Eddie James | 9789e71 | 2022-05-25 15:43:40 -0500 | [diff] [blame] | 23 | |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 24 | /** @class Error |
| 25 | * @brief Monitors for OCC device error condition |
| 26 | */ |
| 27 | class Error |
| 28 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 29 | public: |
| 30 | Error() = delete; |
| 31 | Error(const Error&) = delete; |
| 32 | Error& operator=(const Error&) = delete; |
| 33 | Error(Error&&) = default; |
| 34 | Error& operator=(Error&&) = default; |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 35 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 36 | /** @brief Constructs the Error object |
| 37 | * |
| 38 | * @param[in] event - reference to sd_event unique_ptr |
| 39 | * @param[in] file - File used by driver to communicate errors |
| 40 | * @param[in] callBack - Optional function callback on error condition |
| 41 | */ |
| 42 | Error(EventPtr& event, const fs::path& file, |
Eddie James | 9789e71 | 2022-05-25 15:43:40 -0500 | [diff] [blame] | 43 | std::function<void(int)> callBack = nullptr) : |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 44 | event(event), |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 45 | file(file), callBack(callBack) |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 46 | { |
| 47 | // Nothing to do here. |
| 48 | } |
| 49 | |
George Liu | bddcf85 | 2021-09-08 08:46:22 +0800 | [diff] [blame] | 50 | virtual ~Error() |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 51 | { |
| 52 | if (fd >= 0) |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 53 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 54 | close(fd); |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 55 | } |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 56 | } |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 57 | |
Eddie James | 9789e71 | 2022-05-25 15:43:40 -0500 | [diff] [blame] | 58 | /** @class Descriptor |
| 59 | * @brief Contains data relevant to an error that occurred. |
| 60 | */ |
| 61 | class Descriptor |
| 62 | { |
| 63 | public: |
| 64 | Descriptor(const Descriptor&) = default; |
| 65 | Descriptor& operator=(const Descriptor&) = default; |
| 66 | Descriptor(Descriptor&&) = default; |
| 67 | Descriptor& operator=(Descriptor&&) = default; |
| 68 | |
Patrick Williams | a49c987 | 2023-05-10 07:50:35 -0500 | [diff] [blame] | 69 | Descriptor() : log(false), err(0), callout(nullptr), path(nullptr) {} |
Eddie James | 9789e71 | 2022-05-25 15:43:40 -0500 | [diff] [blame] | 70 | |
| 71 | /** @brief Constructs the Descriptor object |
| 72 | * |
| 73 | * @param[in] path - the DBus error path |
| 74 | * @param[in] err - Optional error return code |
| 75 | * @param[in] callout - Optional PEL callout path |
| 76 | */ |
| 77 | Descriptor(const char* path, int err = 0, |
| 78 | const char* callout = nullptr) : |
| 79 | log(true), |
| 80 | err(err), callout(callout), path(path) |
| 81 | {} |
| 82 | |
| 83 | bool log; |
| 84 | int err; |
| 85 | const char* callout; |
| 86 | const char* path; |
| 87 | }; |
| 88 | |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 89 | /** @brief Starts to monitor for error conditions |
| 90 | * |
| 91 | * @param[in] poll - Indicates whether or not the error file should |
| 92 | * actually be polled for changes. Disabling polling is |
| 93 | * necessary for error files that don't support the poll |
| 94 | * file operation. |
| 95 | */ |
| 96 | void addWatch(bool poll = true); |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 97 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 98 | /** @brief Removes error watch */ |
| 99 | void removeWatch(); |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 100 | |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 101 | inline void setFile(const fs::path& f) |
| 102 | { |
| 103 | file = f; |
| 104 | } |
| 105 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 106 | private: |
| 107 | /** @brief sd_event wrapped in unique_ptr */ |
| 108 | EventPtr& event; |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 109 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 110 | /** @brief event source wrapped in unique_ptr */ |
| 111 | EventSourcePtr eventSource; |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 112 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 113 | /** @brief Current state of error watching */ |
| 114 | bool watching = false; |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 115 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 116 | /** @brief attaches FD to events and sets up callback handler */ |
| 117 | void registerCallBack(); |
Vishwanatha Subbanna | 2dc9b1a | 2017-08-18 18:29:41 +0530 | [diff] [blame] | 118 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 119 | /** @brief Opens the file and populates fd */ |
| 120 | void openFile(); |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 121 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 122 | /** @brief Callback handler when the FD has some activity on it |
| 123 | * |
| 124 | * @param[in] es - Populated event source |
| 125 | * @param[in] fd - Associated File descriptor |
| 126 | * @param[in] revents - Type of event |
| 127 | * @param[in] userData - User data that was passed during registration |
| 128 | * |
| 129 | * @return - 0 or positive number on success and negative |
| 130 | * errno otherwise |
| 131 | */ |
| 132 | static int processEvents(sd_event_source* es, int fd, uint32_t revents, |
| 133 | void* userData); |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 134 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 135 | /** @brief When the error event is received, analyzes it |
| 136 | * and makes a callback to error handler if the |
| 137 | * content denotes an error condition |
| 138 | */ |
| 139 | virtual void analyzeEvent(); |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 140 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 141 | protected: |
| 142 | /** @brief File descriptor to watch for errors */ |
| 143 | int fd = -1; |
Edward A. James | 636577f | 2017-10-06 10:53:55 -0500 | [diff] [blame] | 144 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 145 | /** Error file */ |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 146 | fs::path file; |
Edward A. James | 636577f | 2017-10-06 10:53:55 -0500 | [diff] [blame] | 147 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 148 | /** @brief Optional function to call on error scenario */ |
Eddie James | 9789e71 | 2022-05-25 15:43:40 -0500 | [diff] [blame] | 149 | std::function<void(int)> callBack; |
Edward A. James | 636577f | 2017-10-06 10:53:55 -0500 | [diff] [blame] | 150 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 151 | /** @brief Reads file data |
| 152 | * |
| 153 | * @return data read. Since its a /sysfs entry, |
| 154 | * it would be a string |
| 155 | */ |
| 156 | std::string readFile(int) const; |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | } // namespace occ |
| 160 | } // namespace open_power |