blob: f815a3b0e9985ef3e1cd2b942806b4bb6798e936 [file] [log] [blame]
Matt Spinler69b0cf02020-10-14 10:59:03 -05001#pragma once
2
3#include "logging.hpp"
4#include "power_interface.hpp"
5
6#include <fmt/format.h>
7
8#include <sdeventplus/clock.hpp>
9#include <sdeventplus/event.hpp>
10#include <sdeventplus/utility/timer.hpp>
11
12#include <chrono>
13
14namespace phosphor::fan::monitor
15{
16
17/**
18 * @class PowerOffAction
19 *
20 * This is the base class for a power off action, which is
21 * used by the PowerOffRule class to do different types of
22 * power offs based on fan failures.
23 *
24 * The power off is started with the start() method, and the
25 * derived class may or may not allow it to be stopped with
26 * the cancel() method, which is really only useful when
27 * there is a delay before the power off.
28 *
29 * It uses the PowerInterfaceBase object pointer to perform
30 * the D-Bus call to do the power off, so it can be mocked
31 * for testing.
32 */
33class PowerOffAction
34{
35 public:
Matt Spinlerac1efc12020-10-27 10:20:11 -050036 using PrePowerOffFunc = std::function<void()>;
37
Matt Spinler69b0cf02020-10-14 10:59:03 -050038 PowerOffAction() = delete;
39 virtual ~PowerOffAction() = default;
40 PowerOffAction(const PowerOffAction&) = delete;
41 PowerOffAction& operator=(const PowerOffAction&) = delete;
42 PowerOffAction(PowerOffAction&&) = delete;
43 PowerOffAction& operator=(PowerOffAction&&) = delete;
44
45 /**
46 * @brief Constructor
47 *
48 * @param[in] name - The action name. Used for tracing.
Matt Spinlerac1efc12020-10-27 10:20:11 -050049 * @param[in] powerInterface - The object used to invoke the power off.
50 * @param[in] powerOffFunc - A function to call right before the power
51 * off occurs (after any delays). May be
52 * empty if no function is necessary.
Matt Spinler69b0cf02020-10-14 10:59:03 -050053 */
54 PowerOffAction(const std::string& name,
Matt Spinlerac1efc12020-10-27 10:20:11 -050055 std::shared_ptr<PowerInterfaceBase> powerInterface,
56 PrePowerOffFunc& powerOffFunc) :
Matt Spinler69b0cf02020-10-14 10:59:03 -050057 _name(name),
58 _powerIface(std::move(powerInterface)),
Matt Spinlerac1efc12020-10-27 10:20:11 -050059 _event(sdeventplus::Event::get_default()),
60 _prePowerOffFunc(powerOffFunc)
Matt Spinler69b0cf02020-10-14 10:59:03 -050061 {}
62
63 /**
64 * @brief Starts the power off.
65 *
66 * Though this occurs in the child class, usually this
67 * involves starting a timer and then powering off when it
68 * times out.
69 */
70 virtual void start() = 0;
71
72 /**
73 * @brief Attempts to cancel the power off, if the derived
74 * class allows it, and assuming the power off hasn't
75 * already happened.
76 *
77 * The 'force' parameter is mainly for use when something else
78 * powered off the system so this action doesn't need to run
79 * anymore even if it isn't usually cancelable.
80 *
81 * @param[in] force - If the cancel should be forced
82 *
83 * @return bool - If the cancel was allowed/successful
84 */
85 virtual bool cancel(bool force) = 0;
86
87 /**
Matt Spinler69b0cf02020-10-14 10:59:03 -050088 * @brief Returns the name of the action
89 *
90 * @return const std::string& - The name
91 */
92 const std::string& name() const
93 {
94 return _name;
95 }
96
97 protected:
98 /**
99 * @brief The name of the action, which is set by the
100 * derived class.
101 */
102 const std::string _name;
103
104 /**
Matt Spinler69b0cf02020-10-14 10:59:03 -0500105 * @brief The object used to invoke the power off with.
106 */
107 std::shared_ptr<PowerInterfaceBase> _powerIface;
108
109 /**
110 * @brief The event loop object. Needed by timers.
111 */
112 sdeventplus::Event _event;
Matt Spinlerac1efc12020-10-27 10:20:11 -0500113
114 /**
115 * @brief A function that will be called right before
116 * the power off.
117 */
118 PrePowerOffFunc _prePowerOffFunc;
Matt Spinler69b0cf02020-10-14 10:59:03 -0500119};
120
121/**
122 * @class HardPowerOff
123 *
124 * This class is derived from the PowerOffAction class
125 * and will execute a hard power off after some delay.
126 */
127class HardPowerOff : public PowerOffAction
128{
129 public:
130 HardPowerOff() = delete;
131 ~HardPowerOff() = default;
132 HardPowerOff(const HardPowerOff&) = delete;
133 HardPowerOff& operator=(const HardPowerOff&) = delete;
134 HardPowerOff(HardPowerOff&&) = delete;
135 HardPowerOff& operator=(HardPowerOff&&) = delete;
136
137 /**
138 * @brief Constructor
139 *
140 * @param[in] delay - The amount of time in seconds to wait before
141 * doing the power off
142 * @param[in] powerInterface - The object to use to do the power off
Matt Spinlerac1efc12020-10-27 10:20:11 -0500143 * @param[in] func - A function to call right before the power
144 * off occurs (after the delay). May be
145 * empty if no function is necessary.
Matt Spinler69b0cf02020-10-14 10:59:03 -0500146 */
147 HardPowerOff(uint32_t delay,
Matt Spinlerac1efc12020-10-27 10:20:11 -0500148 std::shared_ptr<PowerInterfaceBase> powerInterface,
149 PrePowerOffFunc func) :
Matt Spinler69b0cf02020-10-14 10:59:03 -0500150 PowerOffAction("Hard Power Off: " + std::to_string(delay) + "s",
Matt Spinlerac1efc12020-10-27 10:20:11 -0500151 powerInterface, func),
Matt Spinler69b0cf02020-10-14 10:59:03 -0500152 _delay(delay),
153 _timer(_event, std::bind(std::mem_fn(&HardPowerOff::powerOff), this))
154 {}
155
156 /**
157 * @brief Starts a timer upon the expiration of which the
158 * hard power off will be done.
159 */
160 void start() override
161 {
162 _timer.restartOnce(_delay);
163 }
164
165 /**
166 * @brief Cancels the timer. This is always allowed.
167 *
168 * @param[in] force - If the cancel should be forced or not
169 * (not checked in this case)
170 * @return bool - Always returns true
171 */
172 bool cancel(bool) override
173 {
174 if (_timer.isEnabled())
175 {
176 _timer.setEnabled(false);
177 }
178
179 // Can always be canceled
180 return true;
181 }
182
183 /**
184 * @brief Performs the hard power off.
185 */
186 void powerOff()
187 {
Matt Spinlerac1efc12020-10-27 10:20:11 -0500188
189 if (_prePowerOffFunc)
190 {
191 _prePowerOffFunc();
192 }
193
Matt Spinler69b0cf02020-10-14 10:59:03 -0500194 getLogger().log(
195 fmt::format("Action '{}' executing hard power off", name()));
196 _powerIface->hardPowerOff();
197 }
198
199 private:
200 /**
201 * @brief The number of seconds to wait between starting the
202 * action and doing the power off.
203 */
204 std::chrono::seconds _delay;
205
206 /**
207 * @brief The Timer object used to handle the delay.
208 */
209 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> _timer;
210};
211
212/**
213 * @class SoftPowerOff
214 *
215 * This class is derived from the PowerOffAction class
216 * and will execute a soft power off after some delay.
217 */
218class SoftPowerOff : public PowerOffAction
219{
220 public:
221 SoftPowerOff() = delete;
222 ~SoftPowerOff() = default;
223 SoftPowerOff(const SoftPowerOff&) = delete;
224 SoftPowerOff& operator=(const SoftPowerOff&) = delete;
225 SoftPowerOff(SoftPowerOff&&) = delete;
226 SoftPowerOff& operator=(SoftPowerOff&&) = delete;
227
228 /**
229 * @brief Constructor
230 *
231 * @param[in] delay - The amount of time in seconds to wait before
232 * doing the power off
233 * @param[in] powerInterface - The object to use to do the power off
Matt Spinlerac1efc12020-10-27 10:20:11 -0500234 * @param[in] func - A function to call right before the power
235 * off occurs (after the delay). May be
236 * empty if no function is necessary.
Matt Spinler69b0cf02020-10-14 10:59:03 -0500237 */
238 SoftPowerOff(uint32_t delay,
Matt Spinlerac1efc12020-10-27 10:20:11 -0500239 std::shared_ptr<PowerInterfaceBase> powerInterface,
240 PrePowerOffFunc func) :
Matt Spinler69b0cf02020-10-14 10:59:03 -0500241 PowerOffAction("Soft Power Off: " + std::to_string(delay) + "s",
Matt Spinlerac1efc12020-10-27 10:20:11 -0500242 powerInterface, func),
Matt Spinler69b0cf02020-10-14 10:59:03 -0500243 _delay(delay),
244 _timer(_event, std::bind(std::mem_fn(&SoftPowerOff::powerOff), this))
245 {}
246
247 /**
248 * @brief Starts a timer upon the expiration of which the
249 * soft power off will be done.
250 */
251 void start() override
252 {
253 _timer.restartOnce(_delay);
254 }
255
256 /**
257 * @brief Cancels the timer. This is always allowed.
258 *
259 * @param[in] force - If the cancel should be forced or not
260 * (not checked in this case)
261 * @return bool - Always returns true
262 */
263 bool cancel(bool) override
264 {
265 if (_timer.isEnabled())
266 {
267 _timer.setEnabled(false);
268 }
269
270 // Can always be canceled
271 return true;
272 }
273
274 /**
275 * @brief Performs the soft power off.
276 */
277 void powerOff()
278 {
Matt Spinlerac1efc12020-10-27 10:20:11 -0500279 if (_prePowerOffFunc)
280 {
281 _prePowerOffFunc();
282 }
283
Matt Spinler69b0cf02020-10-14 10:59:03 -0500284 getLogger().log(
285 fmt::format("Action '{}' executing soft power off", name()));
286 _powerIface->softPowerOff();
287 }
288
289 private:
290 /**
291 * @brief The number of seconds to wait between starting the
292 * action and doing the power off.
293 */
294 std::chrono::seconds _delay;
295
296 /**
297 * @brief The Timer object used to handle the delay.
298 */
299 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> _timer;
300};
301
302/**
303 * @class EpowPowerOff
304 *
305 * Still TODO, but has a cancelable service mode delay followed
306 * by an uncancelable meltdown delay followed by a hard power off, with
307 * some sort of EPOW alert in there as well.
308 */
309class EpowPowerOff : public PowerOffAction
310{
311 public:
312 EpowPowerOff() = delete;
313 ~EpowPowerOff() = default;
314 EpowPowerOff(const EpowPowerOff&) = delete;
315 EpowPowerOff& operator=(const EpowPowerOff&) = delete;
316 EpowPowerOff(EpowPowerOff&&) = delete;
317 EpowPowerOff& operator=(EpowPowerOff&&) = delete;
318
319 EpowPowerOff(uint32_t serviceModeDelay, uint32_t meltdownDelay,
Matt Spinlerac1efc12020-10-27 10:20:11 -0500320 std::shared_ptr<PowerInterfaceBase> powerInterface,
321 PrePowerOffFunc func) :
Matt Spinler69b0cf02020-10-14 10:59:03 -0500322 PowerOffAction("EPOW Power Off: " + std::to_string(serviceModeDelay) +
323 "s/" + std::to_string(meltdownDelay) + "s",
Matt Spinlerac1efc12020-10-27 10:20:11 -0500324 powerInterface, func),
Matt Spinler69b0cf02020-10-14 10:59:03 -0500325 _serviceModeDelay(serviceModeDelay), _meltdownDelay(meltdownDelay)
326 {}
327
328 void start() override
329 {
330 // TODO
331 }
332
333 bool cancel(bool) override
334 {
335 // TODO
336 return true;
337 }
338
339 private:
340 std::chrono::seconds _serviceModeDelay;
341 std::chrono::seconds _meltdownDelay;
342};
343} // namespace phosphor::fan::monitor