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