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