blob: ba4610e07e5df6c8e05f834d8c6d5d6dc2168483 [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
George Liubcef3b42021-09-10 12:39:02 +08009#include <filesystem>
Gunnar Mills94df8c92018-09-14 14:50:03 -050010#include <functional>
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053011namespace open_power
12{
13namespace occ
14{
15
George Liubcef3b42021-09-10 12:39:02 +080016namespace fs = std::filesystem;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053017
Eddie James9789e712022-05-25 15:43:40 -050018constexpr auto PRESENCE_ERROR_PATH =
19 "org.open_power.OCC.Firmware.PresenceMismatch";
20constexpr auto SAFE_ERROR_PATH = "org.open_power.OCC.Device.SafeState";
21
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053022/** @class Error
23 * @brief Monitors for OCC device error condition
24 */
25class Error
26{
Gunnar Mills94df8c92018-09-14 14:50:03 -050027 public:
28 Error() = delete;
29 Error(const Error&) = delete;
30 Error& operator=(const Error&) = delete;
31 Error(Error&&) = default;
32 Error& operator=(Error&&) = default;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053033
Gunnar Mills94df8c92018-09-14 14:50:03 -050034 /** @brief Constructs the Error object
35 *
36 * @param[in] event - reference to sd_event unique_ptr
37 * @param[in] file - File used by driver to communicate errors
38 * @param[in] callBack - Optional function callback on error condition
39 */
40 Error(EventPtr& event, const fs::path& file,
Eddie James9789e712022-05-25 15:43:40 -050041 std::function<void(int)> callBack = nullptr) :
Gunnar Mills94df8c92018-09-14 14:50:03 -050042 event(event),
Eddie James774f9af2019-03-19 20:58:53 +000043 file(file), callBack(callBack)
Gunnar Mills94df8c92018-09-14 14:50:03 -050044 {
45 // Nothing to do here.
46 }
47
George Liubddcf852021-09-08 08:46:22 +080048 virtual ~Error()
Gunnar Mills94df8c92018-09-14 14:50:03 -050049 {
50 if (fd >= 0)
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053051 {
Gunnar Mills94df8c92018-09-14 14:50:03 -050052 close(fd);
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053053 }
Gunnar Mills94df8c92018-09-14 14:50:03 -050054 }
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053055
Eddie James9789e712022-05-25 15:43:40 -050056 /** @class Descriptor
57 * @brief Contains data relevant to an error that occurred.
58 */
59 class Descriptor
60 {
61 public:
62 Descriptor(const Descriptor&) = default;
63 Descriptor& operator=(const Descriptor&) = default;
64 Descriptor(Descriptor&&) = default;
65 Descriptor& operator=(Descriptor&&) = default;
66
67 Descriptor() : log(false), err(0), callout(nullptr), path(nullptr)
68 {}
69
70 /** @brief Constructs the Descriptor object
71 *
72 * @param[in] path - the DBus error path
73 * @param[in] err - Optional error return code
74 * @param[in] callout - Optional PEL callout path
75 */
76 Descriptor(const char* path, int err = 0,
77 const char* callout = nullptr) :
78 log(true),
79 err(err), callout(callout), path(path)
80 {}
81
82 bool log;
83 int err;
84 const char* callout;
85 const char* path;
86 };
87
Eddie James774f9af2019-03-19 20:58:53 +000088 /** @brief Starts to monitor for error conditions
89 *
90 * @param[in] poll - Indicates whether or not the error file should
91 * actually be polled for changes. Disabling polling is
92 * necessary for error files that don't support the poll
93 * file operation.
94 */
95 void addWatch(bool poll = true);
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053096
Gunnar Mills94df8c92018-09-14 14:50:03 -050097 /** @brief Removes error watch */
98 void removeWatch();
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053099
Eddie James774f9af2019-03-19 20:58:53 +0000100 inline void setFile(const fs::path& f)
101 {
102 file = f;
103 }
104
Gunnar Mills94df8c92018-09-14 14:50:03 -0500105 private:
106 /** @brief sd_event wrapped in unique_ptr */
107 EventPtr& event;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530108
Gunnar Mills94df8c92018-09-14 14:50:03 -0500109 /** @brief event source wrapped in unique_ptr */
110 EventSourcePtr eventSource;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530111
Gunnar Mills94df8c92018-09-14 14:50:03 -0500112 /** @brief Current state of error watching */
113 bool watching = false;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530114
Gunnar Mills94df8c92018-09-14 14:50:03 -0500115 /** @brief attaches FD to events and sets up callback handler */
116 void registerCallBack();
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530117
Gunnar Mills94df8c92018-09-14 14:50:03 -0500118 /** @brief Opens the file and populates fd */
119 void openFile();
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530120
Gunnar Mills94df8c92018-09-14 14:50:03 -0500121 /** @brief Callback handler when the FD has some activity on it
122 *
123 * @param[in] es - Populated event source
124 * @param[in] fd - Associated File descriptor
125 * @param[in] revents - Type of event
126 * @param[in] userData - User data that was passed during registration
127 *
128 * @return - 0 or positive number on success and negative
129 * errno otherwise
130 */
131 static int processEvents(sd_event_source* es, int fd, uint32_t revents,
132 void* userData);
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530133
Gunnar Mills94df8c92018-09-14 14:50:03 -0500134 /** @brief When the error event is received, analyzes it
135 * and makes a callback to error handler if the
136 * content denotes an error condition
137 */
138 virtual void analyzeEvent();
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530139
Gunnar Mills94df8c92018-09-14 14:50:03 -0500140 protected:
141 /** @brief File descriptor to watch for errors */
142 int fd = -1;
Edward A. James636577f2017-10-06 10:53:55 -0500143
Gunnar Mills94df8c92018-09-14 14:50:03 -0500144 /** Error file */
Eddie James774f9af2019-03-19 20:58:53 +0000145 fs::path file;
Edward A. James636577f2017-10-06 10:53:55 -0500146
Gunnar Mills94df8c92018-09-14 14:50:03 -0500147 /** @brief Optional function to call on error scenario */
Eddie James9789e712022-05-25 15:43:40 -0500148 std::function<void(int)> callBack;
Edward A. James636577f2017-10-06 10:53:55 -0500149
Gunnar Mills94df8c92018-09-14 14:50:03 -0500150 /** @brief Reads file data
151 *
152 * @return data read. Since its a /sysfs entry,
153 * it would be a string
154 */
155 std::string readFile(int) const;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530156};
157
158} // namespace occ
159} // namespace open_power