blob: 57fc6c8ff09556a1eae73af9d99d448c1f45084e [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 =
Matt Spinlerfec4b0b2024-01-04 15:10:37 -060019 "org.open_power.OCC.Firmware.Error.PresenceMismatch";
20constexpr auto SAFE_ERROR_PATH = "org.open_power.OCC.Device.Error.SafeState";
Chris Cain4b82f3e2024-04-22 14:44:29 -050021constexpr auto MISSING_OCC_SENSORS_PATH =
22 "org.open_power.OCC.Firmware.Error.MissingOCCSensors";
Chris Cainf0295f52024-09-12 15:41:14 -050023constexpr auto OCC_COMM_ERROR_PATH =
24 "org.open_power.OCC.Device.Error.OpenFailure";
Eddie James9789e712022-05-25 15:43:40 -050025
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053026/** @class Error
27 * @brief Monitors for OCC device error condition
28 */
29class Error
30{
Gunnar Mills94df8c92018-09-14 14:50:03 -050031 public:
32 Error() = delete;
33 Error(const Error&) = delete;
34 Error& operator=(const Error&) = delete;
35 Error(Error&&) = default;
36 Error& operator=(Error&&) = default;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053037
Gunnar Mills94df8c92018-09-14 14:50:03 -050038 /** @brief Constructs the Error object
39 *
40 * @param[in] event - reference to sd_event unique_ptr
41 * @param[in] file - File used by driver to communicate errors
42 * @param[in] callBack - Optional function callback on error condition
43 */
44 Error(EventPtr& event, const fs::path& file,
Eddie James9789e712022-05-25 15:43:40 -050045 std::function<void(int)> callBack = nullptr) :
Patrick Williamsd7542c82024-08-16 15:20:28 -040046 event(event), file(file), callBack(callBack)
Gunnar Mills94df8c92018-09-14 14:50:03 -050047 {
48 // Nothing to do here.
49 }
50
George Liubddcf852021-09-08 08:46:22 +080051 virtual ~Error()
Gunnar Mills94df8c92018-09-14 14:50:03 -050052 {
53 if (fd >= 0)
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053054 {
Gunnar Mills94df8c92018-09-14 14:50:03 -050055 close(fd);
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053056 }
Gunnar Mills94df8c92018-09-14 14:50:03 -050057 }
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053058
Eddie James9789e712022-05-25 15:43:40 -050059 /** @class Descriptor
60 * @brief Contains data relevant to an error that occurred.
61 */
62 class Descriptor
63 {
64 public:
65 Descriptor(const Descriptor&) = default;
66 Descriptor& operator=(const Descriptor&) = default;
67 Descriptor(Descriptor&&) = default;
68 Descriptor& operator=(Descriptor&&) = default;
69
Patrick Williamsa49c9872023-05-10 07:50:35 -050070 Descriptor() : log(false), err(0), callout(nullptr), path(nullptr) {}
Eddie James9789e712022-05-25 15:43:40 -050071
72 /** @brief Constructs the Descriptor object
73 *
74 * @param[in] path - the DBus error path
75 * @param[in] err - Optional error return code
76 * @param[in] callout - Optional PEL callout path
Chris Cain3ece5b92025-01-10 16:06:31 -060077 * @param[in] isInventory - true if the callout path is an
78 * inventory path, false if it is a device path
Eddie James9789e712022-05-25 15:43:40 -050079 */
Chris Cain3ece5b92025-01-10 16:06:31 -060080 Descriptor(const char* path, int err = 0, const char* callout = nullptr,
81 const bool isInventory = false) :
82 log(true), err(err), callout(callout), path(path),
83 isInventoryCallout(isInventory)
Eddie James9789e712022-05-25 15:43:40 -050084 {}
85
86 bool log;
87 int err;
88 const char* callout;
89 const char* path;
Chris Cain3ece5b92025-01-10 16:06:31 -060090 bool isInventoryCallout;
Eddie James9789e712022-05-25 15:43:40 -050091 };
92
Eddie James774f9af2019-03-19 20:58:53 +000093 /** @brief Starts to monitor for error conditions
94 *
95 * @param[in] poll - Indicates whether or not the error file should
96 * actually be polled for changes. Disabling polling is
97 * necessary for error files that don't support the poll
98 * file operation.
99 */
100 void addWatch(bool poll = true);
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530101
Gunnar Mills94df8c92018-09-14 14:50:03 -0500102 /** @brief Removes error watch */
103 void removeWatch();
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530104
Eddie James774f9af2019-03-19 20:58:53 +0000105 inline void setFile(const fs::path& f)
106 {
107 file = f;
108 }
109
Gunnar Mills94df8c92018-09-14 14:50:03 -0500110 private:
111 /** @brief sd_event wrapped in unique_ptr */
112 EventPtr& event;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530113
Gunnar Mills94df8c92018-09-14 14:50:03 -0500114 /** @brief event source wrapped in unique_ptr */
115 EventSourcePtr eventSource;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530116
Gunnar Mills94df8c92018-09-14 14:50:03 -0500117 /** @brief Current state of error watching */
118 bool watching = false;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530119
Gunnar Mills94df8c92018-09-14 14:50:03 -0500120 /** @brief attaches FD to events and sets up callback handler */
121 void registerCallBack();
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530122
Gunnar Mills94df8c92018-09-14 14:50:03 -0500123 /** @brief Opens the file and populates fd */
124 void openFile();
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530125
Gunnar Mills94df8c92018-09-14 14:50:03 -0500126 /** @brief Callback handler when the FD has some activity on it
127 *
128 * @param[in] es - Populated event source
129 * @param[in] fd - Associated File descriptor
130 * @param[in] revents - Type of event
131 * @param[in] userData - User data that was passed during registration
132 *
133 * @return - 0 or positive number on success and negative
134 * errno otherwise
135 */
136 static int processEvents(sd_event_source* es, int fd, uint32_t revents,
137 void* userData);
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530138
Gunnar Mills94df8c92018-09-14 14:50:03 -0500139 /** @brief When the error event is received, analyzes it
140 * and makes a callback to error handler if the
141 * content denotes an error condition
142 */
143 virtual void analyzeEvent();
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530144
Gunnar Mills94df8c92018-09-14 14:50:03 -0500145 protected:
146 /** @brief File descriptor to watch for errors */
147 int fd = -1;
Edward A. James636577f2017-10-06 10:53:55 -0500148
Gunnar Mills94df8c92018-09-14 14:50:03 -0500149 /** Error file */
Eddie James774f9af2019-03-19 20:58:53 +0000150 fs::path file;
Edward A. James636577f2017-10-06 10:53:55 -0500151
Gunnar Mills94df8c92018-09-14 14:50:03 -0500152 /** @brief Optional function to call on error scenario */
Eddie James9789e712022-05-25 15:43:40 -0500153 std::function<void(int)> callBack;
Edward A. James636577f2017-10-06 10:53:55 -0500154
Gunnar Mills94df8c92018-09-14 14:50:03 -0500155 /** @brief Reads file data
156 *
157 * @return data read. Since its a /sysfs entry,
158 * it would be a string
159 */
160 std::string readFile(int) const;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530161};
162
163} // namespace occ
164} // namespace open_power