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