blob: ebacc8ab835352b833aba5ad9491559d13229c14 [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),
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 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
Gunnar Mills94df8c92018-09-14 14:50:03 -050052 /** @brief Starts to monitor for error conditions */
53 void addWatch();
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053054
Gunnar Mills94df8c92018-09-14 14:50:03 -050055 /** @brief Removes error watch */
56 void removeWatch();
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053057
Gunnar Mills94df8c92018-09-14 14:50:03 -050058 private:
59 /** @brief sd_event wrapped in unique_ptr */
60 EventPtr& event;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053061
Gunnar Mills94df8c92018-09-14 14:50:03 -050062 /** @brief event source wrapped in unique_ptr */
63 EventSourcePtr eventSource;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053064
Gunnar Mills94df8c92018-09-14 14:50:03 -050065 /** @brief Current state of error watching */
66 bool watching = false;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053067
Gunnar Mills94df8c92018-09-14 14:50:03 -050068 /** @brief attaches FD to events and sets up callback handler */
69 void registerCallBack();
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +053070
Gunnar Mills94df8c92018-09-14 14:50:03 -050071 /** @brief Opens the file and populates fd */
72 void openFile();
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053073
Gunnar Mills94df8c92018-09-14 14:50:03 -050074 /** @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 Subbannaee4d83d2017-06-29 18:35:00 +053086
Gunnar Mills94df8c92018-09-14 14:50:03 -050087 /** @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 Subbannaee4d83d2017-06-29 18:35:00 +053092
Gunnar Mills94df8c92018-09-14 14:50:03 -050093 protected:
94 /** @brief File descriptor to watch for errors */
95 int fd = -1;
Edward A. James636577f2017-10-06 10:53:55 -050096
Gunnar Mills94df8c92018-09-14 14:50:03 -050097 /** Error file */
98 const fs::path file;
Edward A. James636577f2017-10-06 10:53:55 -050099
Gunnar Mills94df8c92018-09-14 14:50:03 -0500100 /** @brief Optional function to call on error scenario */
101 std::function<void(bool)> callBack;
Edward A. James636577f2017-10-06 10:53:55 -0500102
Gunnar Mills94df8c92018-09-14 14:50:03 -0500103 /** @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 Subbannaee4d83d2017-06-29 18:35:00 +0530109};
110
111} // namespace occ
112} // namespace open_power