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