1 /** 2 * Copyright © 2021 IBM Corporation 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #pragma once 17 18 #include "../manager.hpp" 19 #include "action.hpp" 20 #include "group.hpp" 21 #include "zone.hpp" 22 23 #include <nlohmann/json.hpp> 24 25 #include <chrono> 26 #include <functional> 27 #include <memory> 28 #include <vector> 29 30 namespace phosphor::fan::control::json 31 { 32 33 using json = nlohmann::json; 34 35 /** 36 * @class TimerBasedActions - Action that wraps a list of actions with a timer 37 * 38 * Sets up a list of actions to be invoked when the defined timer expires. 39 * Once for a `oneshot` timer or at each expiration of a `repeating` timer. 40 */ 41 class TimerBasedActions : 42 public ActionBase, 43 public ActionRegister<TimerBasedActions> 44 { 45 public: 46 /* Name of this action */ 47 static constexpr auto name = "call_actions_based_on_timer"; 48 49 TimerBasedActions() = delete; 50 TimerBasedActions(const TimerBasedActions&) = delete; 51 TimerBasedActions(TimerBasedActions&&) = delete; 52 TimerBasedActions& operator=(const TimerBasedActions&) = delete; 53 TimerBasedActions& operator=(TimerBasedActions&&) = delete; 54 ~TimerBasedActions() = default; 55 56 /** 57 * @brief Call actions when timer expires 58 * 59 * @param[in] jsonObj - JSON configuration of this action 60 * @param[in] groups - Groups of dbus objects the action uses 61 */ 62 TimerBasedActions(const json& jsonObj, const std::vector<Group>& groups); 63 64 /** 65 * @brief Run the action 66 * 67 * Starts or stops a timer that runs a list of actions whenever the 68 * timer expires. The configured timer is set to callback the list of 69 * actions against the given zones and configured groups. 70 * 71 * Where any group does not have a configured value to be compared against, 72 * the groups' service owned state is used to start/stop the timer. When any 73 * service providing a group member is not owned, the timer is started and 74 * if all members' services are owned, the timer is stopped. 75 * 76 * Where all groups have a configured value to compare against, that will be 77 * compared against all members within each group to start/stop the timer. 78 * When all group members have a given value and it matches what's in the 79 * cache, the timer is started and if any do not match, the timer is 80 * stopped. 81 * 82 * @param[in] zone - Zone to run the action on 83 */ 84 void run(Zone& zone) override; 85 86 /** 87 * @brief Start the timer 88 * 89 * Starts the configured timer of this action if not already running 90 */ 91 void startTimer(); 92 93 /** 94 * @brief Stop the timer 95 * 96 * Stops the configured timer of this action if running 97 */ 98 void stopTimer(); 99 100 /** 101 * @brief Timer expire's callback 102 * 103 * Called each time the timer expires, running the configured actions 104 */ 105 void timerExpired(); 106 107 /** 108 * @brief Set the zones on the action and the timer's actions 109 * 110 * @param[in] zones - Zones for the action and timer's actions 111 * 112 * Sets the zones on this action and the timer's actions to run against 113 */ 114 virtual void 115 setZones(std::vector<std::reference_wrapper<Zone>>& zones) override; 116 117 private: 118 /* The timer for this action */ 119 Timer _timer; 120 121 /* Whether timer triggered by groups' owner or property value states */ 122 bool _byOwner; 123 124 /* Timer interval for this action's timer */ 125 std::chrono::microseconds _interval; 126 127 /* Timer type for this action's timer */ 128 TimerType _type; 129 130 /* List of actions to be called when the timer expires */ 131 std::vector<std::unique_ptr<ActionBase>> _actions; 132 133 /** 134 * @brief Parse and set the timer configuration 135 * 136 * @param[in] jsonObj - JSON object for the action 137 * 138 * Sets the timer configuration used to run the list of actions 139 */ 140 void setTimerConf(const json& jsonObj); 141 142 /** 143 * @brief Parse and set the list of actions 144 * 145 * @param[in] jsonObj - JSON object for the action 146 * 147 * Sets the list of actions that is run when the timer expires 148 */ 149 void setActions(const json& jsonObj); 150 }; 151 152 } // namespace phosphor::fan::control::json 153