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