Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "data_types.hpp" |
| 4 | |
William A. Kennington III | 223c409 | 2018-10-19 15:56:09 -0700 | [diff] [blame] | 5 | #include <sdeventplus/clock.hpp> |
| 6 | #include <sdeventplus/event.hpp> |
| 7 | #include <sdeventplus/utility/timer.hpp> |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 8 | |
George Liu | 3fe976c | 2022-06-21 09:37:04 +0800 | [diff] [blame] | 9 | #include <chrono> |
| 10 | #include <cstddef> |
| 11 | |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 12 | namespace phosphor |
| 13 | { |
| 14 | namespace dbus |
| 15 | { |
| 16 | namespace monitoring |
| 17 | { |
| 18 | |
| 19 | /** @class Callback |
| 20 | * @brief Callback interface. |
| 21 | * |
| 22 | * Callbacks of any type can be run. |
| 23 | */ |
| 24 | class Callback |
| 25 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 26 | public: |
| 27 | Callback() = default; |
| 28 | Callback(const Callback&) = delete; |
| 29 | Callback(Callback&&) = default; |
| 30 | Callback& operator=(const Callback&) = delete; |
| 31 | Callback& operator=(Callback&&) = default; |
| 32 | virtual ~Callback() = default; |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 33 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 34 | /** @brief Run the callback. |
| 35 | * @param[in] ctx - caller context |
| 36 | * Context could be Startup or Signal |
| 37 | * Startup: Callback is called as part of process startup. |
| 38 | * Signal: Callback is called as part of watch condition has been met. |
| 39 | * |
| 40 | */ |
George Liu | 5e6b51d | 2022-06-21 16:59:39 +0800 | [diff] [blame] | 41 | virtual void operator()(Context /* ctx */) = 0; |
Marri Devender Rao | 70aafbb | 2018-04-12 01:11:48 -0500 | [diff] [blame] | 42 | |
| 43 | /** @brief Run the callback. |
| 44 | * @param[in] ctx - caller context |
| 45 | * Context could be Startup or Signal |
| 46 | * Startup: Callback is called as part of process startup. |
| 47 | * Signal: Callback is called as part of watch condition has been met. |
| 48 | * @param[in] msg - The sdbusplus signal message |
| 49 | */ |
George Liu | 5e6b51d | 2022-06-21 16:59:39 +0800 | [diff] [blame] | 50 | virtual void operator()(Context /* ctx */, |
Patrick Williams | eab4f8c | 2024-08-16 15:20:10 -0400 | [diff] [blame^] | 51 | sdbusplus::message_t& /* msg */) {}; |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 52 | }; |
| 53 | |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 54 | /** @class Conditional |
| 55 | * @brief Condition interface. |
| 56 | * |
| 57 | * Conditions of any type can be tested for true or false. |
| 58 | */ |
| 59 | class Conditional |
| 60 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 61 | public: |
| 62 | Conditional() = default; |
| 63 | Conditional(const Conditional&) = delete; |
| 64 | Conditional(Conditional&&) = default; |
| 65 | Conditional& operator=(const Conditional&) = delete; |
| 66 | Conditional& operator=(Conditional&&) = default; |
| 67 | virtual ~Conditional() = default; |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 68 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 69 | /** @brief Test the condition. */ |
| 70 | virtual bool operator()() = 0; |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | /** @class IndexedConditional |
| 74 | * @brief Condition with an index. |
| 75 | */ |
| 76 | class IndexedConditional : public Conditional |
| 77 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 78 | public: |
| 79 | IndexedConditional() = delete; |
| 80 | IndexedConditional(const IndexedConditional&) = delete; |
| 81 | IndexedConditional(IndexedConditional&&) = default; |
| 82 | IndexedConditional& operator=(const IndexedConditional&) = delete; |
| 83 | IndexedConditional& operator=(IndexedConditional&&) = default; |
| 84 | virtual ~IndexedConditional() = default; |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 85 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 86 | explicit IndexedConditional(const PropertyIndex& conditionIndex) : |
| 87 | Conditional(), index(conditionIndex) |
George Liu | 3fe976c | 2022-06-21 09:37:04 +0800 | [diff] [blame] | 88 | {} |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 89 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 90 | /** @brief Test the condition. */ |
| 91 | virtual bool operator()() override = 0; |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 92 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 93 | protected: |
| 94 | /** @brief Property names and their associated storage. */ |
| 95 | const PropertyIndex& index; |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 96 | }; |
| 97 | |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 98 | /** @class IndexedCallback |
| 99 | * @brief Callback with an index. |
| 100 | */ |
| 101 | class IndexedCallback : public Callback |
| 102 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 103 | public: |
| 104 | IndexedCallback() = delete; |
| 105 | IndexedCallback(const IndexedCallback&) = delete; |
| 106 | IndexedCallback(IndexedCallback&&) = default; |
| 107 | IndexedCallback& operator=(const IndexedCallback&) = delete; |
| 108 | IndexedCallback& operator=(IndexedCallback&&) = default; |
| 109 | virtual ~IndexedCallback() = default; |
| 110 | explicit IndexedCallback(const PropertyIndex& callbackIndex) : |
| 111 | Callback(), index(callbackIndex) |
George Liu | 3fe976c | 2022-06-21 09:37:04 +0800 | [diff] [blame] | 112 | {} |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 113 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 114 | /** @brief Run the callback. */ |
| 115 | virtual void operator()(Context ctx) override = 0; |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 116 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 117 | protected: |
| 118 | /** @brief Property names and their associated storage. */ |
| 119 | const PropertyIndex& index; |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 120 | }; |
| 121 | |
Brad Bishop | 49e6617 | 2017-05-23 19:16:21 -0400 | [diff] [blame] | 122 | /** @class GroupOfCallbacks |
| 123 | * @brief Invoke multiple callbacks. |
| 124 | * |
Gunnar Mills | 78199b4 | 2017-10-25 16:30:18 -0500 | [diff] [blame] | 125 | * A group of callbacks is implemented as a vector of array indices |
Brad Bishop | 49e6617 | 2017-05-23 19:16:21 -0400 | [diff] [blame] | 126 | * into an external array of callbacks. The group function call |
Gunnar Mills | 78199b4 | 2017-10-25 16:30:18 -0500 | [diff] [blame] | 127 | * operator traverses the vector of indices, invoking each |
Brad Bishop | 49e6617 | 2017-05-23 19:16:21 -0400 | [diff] [blame] | 128 | * callback. |
| 129 | * |
| 130 | * @tparam CallbackAccess - Access to the array of callbacks. |
| 131 | */ |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 132 | template <typename CallbackAccess> |
| 133 | class GroupOfCallbacks : public Callback |
Brad Bishop | 49e6617 | 2017-05-23 19:16:21 -0400 | [diff] [blame] | 134 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 135 | public: |
| 136 | GroupOfCallbacks() = delete; |
| 137 | GroupOfCallbacks(const GroupOfCallbacks&) = delete; |
| 138 | GroupOfCallbacks(GroupOfCallbacks&&) = default; |
| 139 | GroupOfCallbacks& operator=(const GroupOfCallbacks&) = delete; |
| 140 | GroupOfCallbacks& operator=(GroupOfCallbacks&&) = default; |
| 141 | ~GroupOfCallbacks() = default; |
| 142 | explicit GroupOfCallbacks(const std::vector<size_t>& graphEntry) : |
| 143 | graph(graphEntry) |
George Liu | 3fe976c | 2022-06-21 09:37:04 +0800 | [diff] [blame] | 144 | {} |
Brad Bishop | 49e6617 | 2017-05-23 19:16:21 -0400 | [diff] [blame] | 145 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 146 | /** @brief Run the callbacks. */ |
| 147 | void operator()(Context ctx) override |
| 148 | { |
| 149 | for (auto e : graph) |
Brad Bishop | 49e6617 | 2017-05-23 19:16:21 -0400 | [diff] [blame] | 150 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 151 | (*CallbackAccess::get()[e])(ctx); |
Brad Bishop | 49e6617 | 2017-05-23 19:16:21 -0400 | [diff] [blame] | 152 | } |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 153 | } |
Brad Bishop | 49e6617 | 2017-05-23 19:16:21 -0400 | [diff] [blame] | 154 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 155 | private: |
| 156 | /** @brief The offsets of the callbacks in the group. */ |
| 157 | const std::vector<size_t>& graph; |
Brad Bishop | 49e6617 | 2017-05-23 19:16:21 -0400 | [diff] [blame] | 158 | }; |
| 159 | |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 160 | /** @class ConditionalCallback |
Manojkiran Eda | 0c1e024 | 2024-06-17 12:02:59 +0530 | [diff] [blame] | 161 | * @brief Callback adaptor that associates a condition with a callback. |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 162 | */ |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 163 | template <typename CallbackAccess> |
| 164 | class ConditionalCallback : public Callback |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 165 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 166 | public: |
| 167 | ConditionalCallback() = delete; |
| 168 | ConditionalCallback(const ConditionalCallback&) = delete; |
| 169 | ConditionalCallback(ConditionalCallback&&) = default; |
| 170 | ConditionalCallback& operator=(const ConditionalCallback&) = delete; |
| 171 | ConditionalCallback& operator=(ConditionalCallback&&) = default; |
| 172 | virtual ~ConditionalCallback() = default; |
| 173 | ConditionalCallback(const std::vector<size_t>& graphEntry, |
Patrick Williams | eab4f8c | 2024-08-16 15:20:10 -0400 | [diff] [blame^] | 174 | Conditional& cond) : graph(graphEntry), condition(cond) |
George Liu | 3fe976c | 2022-06-21 09:37:04 +0800 | [diff] [blame] | 175 | {} |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 176 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 177 | /** @brief Run the callback if the condition is satisfied. */ |
| 178 | virtual void operator()(Context ctx) override |
| 179 | { |
| 180 | if (condition()) |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 181 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 182 | (*CallbackAccess::get()[graph[0]])(ctx); |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 183 | } |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 184 | } |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 185 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 186 | protected: |
| 187 | /** @brief The index of the callback to conditionally invoke. */ |
| 188 | const std::vector<size_t>& graph; |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 189 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 190 | /** @brief The condition to test. */ |
| 191 | Conditional& condition; |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 192 | }; |
| 193 | |
Brad Bishop | 3539db6 | 2017-05-30 14:21:12 -0400 | [diff] [blame] | 194 | /** @class DeferrableCallback |
| 195 | * |
| 196 | * Deferrable callbacks wait a configurable period before |
| 197 | * invoking their associated callback. |
| 198 | * |
Gunnar Mills | 78199b4 | 2017-10-25 16:30:18 -0500 | [diff] [blame] | 199 | * When the callback condition is initially met, start a timer. If the |
Brad Bishop | 3539db6 | 2017-05-30 14:21:12 -0400 | [diff] [blame] | 200 | * condition is tested again before the timer expires and it is not |
| 201 | * met cancel the timer. If the timer expires invoke the associated |
| 202 | * callback. |
| 203 | * |
| 204 | * @tparam CallbackAccess - Provide access to callback group instances. |
Brad Bishop | 3539db6 | 2017-05-30 14:21:12 -0400 | [diff] [blame] | 205 | */ |
William A. Kennington III | 223c409 | 2018-10-19 15:56:09 -0700 | [diff] [blame] | 206 | template <typename CallbackAccess> |
Brad Bishop | 3539db6 | 2017-05-30 14:21:12 -0400 | [diff] [blame] | 207 | class DeferrableCallback : public ConditionalCallback<CallbackAccess> |
| 208 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 209 | public: |
William A. Kennington III | 223c409 | 2018-10-19 15:56:09 -0700 | [diff] [blame] | 210 | using TimerType = |
| 211 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>; |
| 212 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 213 | DeferrableCallback() = delete; |
| 214 | DeferrableCallback(const DeferrableCallback&) = delete; |
William A. Kennington III | 223c409 | 2018-10-19 15:56:09 -0700 | [diff] [blame] | 215 | DeferrableCallback(DeferrableCallback&&) = delete; |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 216 | DeferrableCallback& operator=(const DeferrableCallback&) = delete; |
William A. Kennington III | 223c409 | 2018-10-19 15:56:09 -0700 | [diff] [blame] | 217 | DeferrableCallback& operator=(DeferrableCallback&&) = delete; |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 218 | ~DeferrableCallback() = default; |
Brad Bishop | 3539db6 | 2017-05-30 14:21:12 -0400 | [diff] [blame] | 219 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 220 | DeferrableCallback(const std::vector<size_t>& graphEntry, Conditional& cond, |
| 221 | const std::chrono::microseconds& delay) : |
| 222 | ConditionalCallback<CallbackAccess>(graphEntry, cond), |
George Liu | ecef119 | 2022-07-07 08:59:12 +0800 | [diff] [blame] | 223 | delayInterval(delay) |
George Liu | 3fe976c | 2022-06-21 09:37:04 +0800 | [diff] [blame] | 224 | {} |
Brad Bishop | 3539db6 | 2017-05-30 14:21:12 -0400 | [diff] [blame] | 225 | |
Alexander Soldatov | 78a5df9 | 2018-11-23 14:37:50 +0300 | [diff] [blame] | 226 | /** @brief Start internal timer if the condition is satisfied. |
| 227 | * |
| 228 | * When the timer expires, it calls operator() for the |
| 229 | * ConditionalCallback with the context saved in |
| 230 | * DeferrableCallback instance. |
| 231 | */ |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 232 | void operator()(Context ctx) override |
| 233 | { |
| 234 | if (!timer) |
Brad Bishop | 3539db6 | 2017-05-30 14:21:12 -0400 | [diff] [blame] | 235 | { |
Patrick Williams | eab4f8c | 2024-08-16 15:20:10 -0400 | [diff] [blame^] | 236 | timer = std::make_unique<TimerType>( |
| 237 | sdeventplus::Event::get_default(), |
| 238 | // **INDENT-OFF** |
| 239 | [this](auto& /* source */) { |
| 240 | // The timer uses the context saved on timer enable |
| 241 | this->ConditionalCallback<CallbackAccess>::operator()( |
| 242 | this->ctx); |
| 243 | }); |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 244 | // **INDENT-ON** |
Brad Bishop | 3539db6 | 2017-05-30 14:21:12 -0400 | [diff] [blame] | 245 | } |
| 246 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 247 | if (this->condition()) |
| 248 | { |
William A. Kennington III | 223c409 | 2018-10-19 15:56:09 -0700 | [diff] [blame] | 249 | if (!timer->isEnabled()) |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 250 | { |
| 251 | // This is the first time the condition evaluated. |
Alexander Soldatov | 78a5df9 | 2018-11-23 14:37:50 +0300 | [diff] [blame] | 252 | // Save current context for timer use. |
| 253 | this->ctx = ctx; |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 254 | // Start the countdown. |
William A. Kennington III | 223c409 | 2018-10-19 15:56:09 -0700 | [diff] [blame] | 255 | timer->restartOnce(delayInterval); |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | else |
| 259 | { |
| 260 | // The condition did not evaluate. Stop the countdown. |
William A. Kennington III | 223c409 | 2018-10-19 15:56:09 -0700 | [diff] [blame] | 261 | timer->setEnabled(false); |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 262 | } |
| 263 | } |
Brad Bishop | 3539db6 | 2017-05-30 14:21:12 -0400 | [diff] [blame] | 264 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 265 | private: |
| 266 | /** @brief The length to wait for the condition to stop evaluating. */ |
| 267 | std::chrono::microseconds delayInterval; |
| 268 | |
| 269 | /** @brief Delegated timer functions. */ |
George Liu | ecef119 | 2022-07-07 08:59:12 +0800 | [diff] [blame] | 270 | std::unique_ptr<TimerType> timer = nullptr; |
Alexander Soldatov | 78a5df9 | 2018-11-23 14:37:50 +0300 | [diff] [blame] | 271 | |
| 272 | /** @brief Current context for timer. */ |
George Liu | ecef119 | 2022-07-07 08:59:12 +0800 | [diff] [blame] | 273 | Context ctx = Context::START; |
Brad Bishop | 3539db6 | 2017-05-30 14:21:12 -0400 | [diff] [blame] | 274 | }; |
| 275 | |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 276 | } // namespace monitoring |
| 277 | } // namespace dbus |
| 278 | } // namespace phosphor |