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