blob: 12ae9255c771a7d5da2b49f3f4cc202888c1cf75 [file] [log] [blame]
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +05301#pragma once
2
3#include <unistd.h>
4#include <functional>
5#include <experimental/filesystem>
6#include "occ_events.hpp"
7#include "config.h"
8namespace open_power
9{
10namespace occ
11{
12
13namespace fs = std::experimental::filesystem;
14
15/** @class Error
16 * @brief Monitors for OCC device error condition
17 */
18class 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 James482e31f2017-09-14 13:17:17 -050035 std::function<void(bool)> callBack = nullptr) :
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053036 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 Subbanna2dc9b1a2017-08-18 18:29:41 +053064 /** @brief Current state of error watching */
65 bool watching = false;
66
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053067 /** @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 Subbannaee4d83d2017-06-29 18:35:00 +053073 /** @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. James636577f2017-10-06 10:53:55 -050090 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 James482e31f2017-09-14 13:17:17 -0500100 std::function<void(bool)> callBack;
Edward A. James636577f2017-10-06 10:53:55 -0500101
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 Subbannaee4d83d2017-06-29 18:35:00 +0530108};
109
110} // namespace occ
111} // namespace open_power