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