Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <unistd.h> |
| 4 | #include <functional> |
| 5 | #include <experimental/filesystem> |
| 6 | #include "occ_events.hpp" |
| 7 | #include "config.h" |
| 8 | namespace open_power |
| 9 | { |
| 10 | namespace occ |
| 11 | { |
| 12 | |
| 13 | namespace fs = std::experimental::filesystem; |
| 14 | |
| 15 | /** @class Error |
| 16 | * @brief Monitors for OCC device error condition |
| 17 | */ |
| 18 | class Error |
| 19 | { |
| 20 | public: |
| 21 | Error() = delete; |
| 22 | Error(const Error&) = delete; |
| 23 | Error& operator=(const Error&) = delete; |
| 24 | Error(Error&&) = default; |
| 25 | Error& operator=(Error&&) = default; |
| 26 | |
| 27 | /** @brief Constructs the Error object |
| 28 | * |
| 29 | * @param[in] event - reference to sd_event unique_ptr |
| 30 | * @param[in] file - File used by driver to communicate errors |
| 31 | * @param[in] callBack - Optional function callback on error condition |
| 32 | */ |
| 33 | Error(EventPtr& event, |
| 34 | const fs::path& file, |
Eddie James | 482e31f | 2017-09-14 13:17:17 -0500 | [diff] [blame] | 35 | std::function<void(bool)> callBack = nullptr) : |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 36 | event(event), |
| 37 | file(fs::path(DEV_PATH) / file), |
| 38 | callBack(callBack) |
| 39 | { |
| 40 | // Nothing to do here. |
| 41 | } |
| 42 | |
| 43 | ~Error() |
| 44 | { |
| 45 | if (fd>= 0) |
| 46 | { |
| 47 | close(fd); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** @brief Starts to monitor for error conditions */ |
| 52 | void addWatch(); |
| 53 | |
| 54 | /** @brief Removes error watch */ |
| 55 | void removeWatch(); |
| 56 | |
| 57 | private: |
| 58 | /** @brief sd_event wrapped in unique_ptr */ |
| 59 | EventPtr& event; |
| 60 | |
| 61 | /** @brief event source wrapped in unique_ptr */ |
| 62 | EventSourcePtr eventSource; |
| 63 | |
Vishwanatha Subbanna | 2dc9b1a | 2017-08-18 18:29:41 +0530 | [diff] [blame] | 64 | /** @brief Current state of error watching */ |
| 65 | bool watching = false; |
| 66 | |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 67 | /** @brief attaches FD to events and sets up callback handler */ |
| 68 | void registerCallBack(); |
| 69 | |
| 70 | /** @brief Opens the file and populates fd */ |
| 71 | void openFile(); |
| 72 | |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 73 | /** @brief Callback handler when the FD has some activity on it |
| 74 | * |
| 75 | * @param[in] es - Populated event source |
| 76 | * @param[in] fd - Associated File descriptor |
| 77 | * @param[in] revents - Type of event |
| 78 | * @param[in] userData - User data that was passed during registration |
| 79 | * |
| 80 | * @return - 0 or positive number on success and negative |
| 81 | * errno otherwise |
| 82 | */ |
| 83 | static int processEvents(sd_event_source* es, int fd, |
| 84 | uint32_t revents, void* userData); |
| 85 | |
| 86 | /** @brief When the error event is received, analyzes it |
| 87 | * and makes a callback to error handler if the |
| 88 | * content denotes an error condition |
| 89 | */ |
Edward A. James | 636577f | 2017-10-06 10:53:55 -0500 | [diff] [blame] | 90 | virtual void analyzeEvent(); |
| 91 | |
| 92 | protected: |
| 93 | /** @brief File descriptor to watch for errors */ |
| 94 | int fd = -1; |
| 95 | |
| 96 | /** Error file */ |
| 97 | const fs::path file; |
| 98 | |
| 99 | /** @brief Optional function to call on error scenario */ |
Eddie James | 482e31f | 2017-09-14 13:17:17 -0500 | [diff] [blame] | 100 | std::function<void(bool)> callBack; |
Edward A. James | 636577f | 2017-10-06 10:53:55 -0500 | [diff] [blame] | 101 | |
| 102 | /** @brief Reads file data |
| 103 | * |
| 104 | * @return data read. Since its a /sysfs entry, |
| 105 | * it would be a string |
| 106 | */ |
| 107 | std::string readFile(int) const; |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | } // namespace occ |
| 111 | } // namespace open_power |