blob: f62370473d2feaa1e05b8d04772a12a393bd5d14 [file] [log] [blame]
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +05301#pragma once
2
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +05303#include "config.h"
Gunnar Mills94df8c92018-09-14 14:50:03 -05004
5#include "occ_events.hpp"
6
7#include <unistd.h>
8
9#include <experimental/filesystem>
10#include <functional>
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053011namespace open_power
12{
13namespace occ
14{
15
16namespace fs = std::experimental::filesystem;
17
18/** @class Error
19 * @brief Monitors for OCC device error condition
20 */
21class Error
22{
Gunnar Mills94df8c92018-09-14 14:50:03 -050023 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 Subbannaee4d83d2017-06-29 18:35:00 +053029
Gunnar Mills94df8c92018-09-14 14:50:03 -050030 /** @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 James774f9af2019-03-19 20:58:53 +000039 file(file), callBack(callBack)
Gunnar Mills94df8c92018-09-14 14:50:03 -050040 {
41 // Nothing to do here.
42 }
43
44 ~Error()
45 {
46 if (fd >= 0)
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053047 {
Gunnar Mills94df8c92018-09-14 14:50:03 -050048 close(fd);
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053049 }
Gunnar Mills94df8c92018-09-14 14:50:03 -050050 }
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053051
Eddie James774f9af2019-03-19 20:58:53 +000052 /** @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 Subbannaee4d83d2017-06-29 18:35:00 +053060
Gunnar Mills94df8c92018-09-14 14:50:03 -050061 /** @brief Removes error watch */
62 void removeWatch();
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053063
Eddie James774f9af2019-03-19 20:58:53 +000064 inline void setFile(const fs::path& f)
65 {
66 file = f;
67 }
68
Gunnar Mills94df8c92018-09-14 14:50:03 -050069 private:
70 /** @brief sd_event wrapped in unique_ptr */
71 EventPtr& event;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053072
Gunnar Mills94df8c92018-09-14 14:50:03 -050073 /** @brief event source wrapped in unique_ptr */
74 EventSourcePtr eventSource;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053075
Gunnar Mills94df8c92018-09-14 14:50:03 -050076 /** @brief Current state of error watching */
77 bool watching = false;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053078
Gunnar Mills94df8c92018-09-14 14:50:03 -050079 /** @brief attaches FD to events and sets up callback handler */
80 void registerCallBack();
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +053081
Gunnar Mills94df8c92018-09-14 14:50:03 -050082 /** @brief Opens the file and populates fd */
83 void openFile();
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053084
Gunnar Mills94df8c92018-09-14 14:50:03 -050085 /** @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 Subbannaee4d83d2017-06-29 18:35:00 +053097
Gunnar Mills94df8c92018-09-14 14:50:03 -050098 /** @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 Subbannaee4d83d2017-06-29 18:35:00 +0530103
Gunnar Mills94df8c92018-09-14 14:50:03 -0500104 protected:
105 /** @brief File descriptor to watch for errors */
106 int fd = -1;
Edward A. James636577f2017-10-06 10:53:55 -0500107
Gunnar Mills94df8c92018-09-14 14:50:03 -0500108 /** Error file */
Eddie James774f9af2019-03-19 20:58:53 +0000109 fs::path file;
Edward A. James636577f2017-10-06 10:53:55 -0500110
Gunnar Mills94df8c92018-09-14 14:50:03 -0500111 /** @brief Optional function to call on error scenario */
112 std::function<void(bool)> callBack;
Edward A. James636577f2017-10-06 10:53:55 -0500113
Gunnar Mills94df8c92018-09-14 14:50:03 -0500114 /** @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 Subbannaee4d83d2017-06-29 18:35:00 +0530120};
121
122} // namespace occ
123} // namespace open_power