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, |
| 35 | std::function<void()> callBack = nullptr) : |
| 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 | |
| 64 | /** Error file */ |
| 65 | const fs::path file; |
| 66 | |
| 67 | /** @brief Optional function to call on error scenario */ |
| 68 | std::function<void()> callBack; |
| 69 | |
| 70 | /** @brief File descriptor to watch for errors */ |
| 71 | int fd = -1; |
| 72 | |
Vishwanatha Subbanna | 2dc9b1a | 2017-08-18 18:29:41 +0530 | [diff] [blame] | 73 | /** @brief Current state of error watching */ |
| 74 | bool watching = false; |
| 75 | |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 76 | /** @brief attaches FD to events and sets up callback handler */ |
| 77 | void registerCallBack(); |
| 78 | |
| 79 | /** @brief Opens the file and populates fd */ |
| 80 | void openFile(); |
| 81 | |
| 82 | /** @brief Reads file data |
| 83 | * |
| 84 | * @return data read. Since its a /sysfs entry, |
| 85 | * it would be a string |
| 86 | */ |
| 87 | std::string readFile(int) const; |
| 88 | |
| 89 | /** @brief Callback handler when the FD has some activity on it |
| 90 | * |
| 91 | * @param[in] es - Populated event source |
| 92 | * @param[in] fd - Associated File descriptor |
| 93 | * @param[in] revents - Type of event |
| 94 | * @param[in] userData - User data that was passed during registration |
| 95 | * |
| 96 | * @return - 0 or positive number on success and negative |
| 97 | * errno otherwise |
| 98 | */ |
| 99 | static int processEvents(sd_event_source* es, int fd, |
| 100 | uint32_t revents, void* userData); |
| 101 | |
| 102 | /** @brief When the error event is received, analyzes it |
| 103 | * and makes a callback to error handler if the |
| 104 | * content denotes an error condition |
| 105 | */ |
| 106 | void analyzeEvent(); |
| 107 | }; |
| 108 | |
| 109 | } // namespace occ |
| 110 | } // namespace open_power |