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 | |
| 9 | #include <experimental/filesystem> |
| 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 | |
| 16 | namespace fs = std::experimental::filesystem; |
| 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), |
| 39 | file(fs::path(DEV_PATH) / file), callBack(callBack) |
| 40 | { |
| 41 | // Nothing to do here. |
| 42 | } |
| 43 | |
| 44 | ~Error() |
| 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 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 52 | /** @brief Starts to monitor for error conditions */ |
| 53 | void addWatch(); |
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 | /** @brief Removes error watch */ |
| 56 | void removeWatch(); |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 57 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 58 | private: |
| 59 | /** @brief sd_event wrapped in unique_ptr */ |
| 60 | EventPtr& event; |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 61 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 62 | /** @brief event source wrapped in unique_ptr */ |
| 63 | EventSourcePtr eventSource; |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 64 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 65 | /** @brief Current state of error watching */ |
| 66 | bool watching = false; |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 67 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 68 | /** @brief attaches FD to events and sets up callback handler */ |
| 69 | void registerCallBack(); |
Vishwanatha Subbanna | 2dc9b1a | 2017-08-18 18:29:41 +0530 | [diff] [blame] | 70 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 71 | /** @brief Opens the file and populates fd */ |
| 72 | void openFile(); |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 73 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 74 | /** @brief Callback handler when the FD has some activity on it |
| 75 | * |
| 76 | * @param[in] es - Populated event source |
| 77 | * @param[in] fd - Associated File descriptor |
| 78 | * @param[in] revents - Type of event |
| 79 | * @param[in] userData - User data that was passed during registration |
| 80 | * |
| 81 | * @return - 0 or positive number on success and negative |
| 82 | * errno otherwise |
| 83 | */ |
| 84 | static int processEvents(sd_event_source* es, int fd, uint32_t revents, |
| 85 | void* userData); |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 86 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 87 | /** @brief When the error event is received, analyzes it |
| 88 | * and makes a callback to error handler if the |
| 89 | * content denotes an error condition |
| 90 | */ |
| 91 | virtual void analyzeEvent(); |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 92 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 93 | protected: |
| 94 | /** @brief File descriptor to watch for errors */ |
| 95 | int fd = -1; |
Edward A. James | 636577f | 2017-10-06 10:53:55 -0500 | [diff] [blame] | 96 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 97 | /** Error file */ |
| 98 | const fs::path file; |
Edward A. James | 636577f | 2017-10-06 10:53:55 -0500 | [diff] [blame] | 99 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 100 | /** @brief Optional function to call on error scenario */ |
| 101 | std::function<void(bool)> callBack; |
Edward A. James | 636577f | 2017-10-06 10:53:55 -0500 | [diff] [blame] | 102 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 103 | /** @brief Reads file data |
| 104 | * |
| 105 | * @return data read. Since its a /sysfs entry, |
| 106 | * it would be a string |
| 107 | */ |
| 108 | std::string readFile(int) const; |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | } // namespace occ |
| 112 | } // namespace open_power |