blob: 7603689590a020c74907b756be1d661186ab03a2 [file] [log] [blame]
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +05301#pragma once
2
3#include <systemd/sd-event.h>
William A. Kennington IIId1331082018-02-27 18:47:05 -08004#include <experimental/optional>
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +05305#include <sdbusplus/bus.hpp>
6#include <sdbusplus/server/object.hpp>
7#include <xyz/openbmc_project/State/Watchdog/server.hpp>
8#include "timer.hpp"
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +05309namespace phosphor
10{
11namespace watchdog
12{
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053013namespace Base = sdbusplus::xyz::openbmc_project::State::server;
14using WatchdogInherits = sdbusplus::server::object::object<Base::Watchdog>;
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053015
16/** @class Watchdog
17 * @brief OpenBMC watchdog implementation.
18 * @details A concrete implementation for the
19 * xyz.openbmc_project.State.Watchdog DBus API.
20 */
21class Watchdog : public WatchdogInherits
22{
23 public:
24 Watchdog() = delete;
25 ~Watchdog() = default;
26 Watchdog(const Watchdog&) = delete;
27 Watchdog& operator=(const Watchdog&) = delete;
28 Watchdog(Watchdog&&) = delete;
29 Watchdog& operator=(Watchdog &&) = delete;
30
William A. Kennington III1232a152018-02-02 15:57:34 -080031 /** @brief Type used to hold the name of a systemd target.
32 */
33 using TargetName = std::string;
34
William A. Kennington IIId1331082018-02-27 18:47:05 -080035 /** @brief Type used to specify the parameters of a fallback watchdog
36 */
37 struct Fallback {
38 Action action;
39 uint64_t interval;
William A. Kennington III22352192018-02-27 18:51:44 -080040 bool always;
William A. Kennington IIId1331082018-02-27 18:47:05 -080041 };
42
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053043 /** @brief Constructs the Watchdog object
44 *
William A. Kennington III1232a152018-02-02 15:57:34 -080045 * @param[in] bus - DBus bus to attach to.
46 * @param[in] objPath - Object path to attach to.
47 * @param[in] event - reference to sd_event unique pointer
48 * @param[in] actionTargets - map of systemd targets called on timeout
William A. Kennington IIId1331082018-02-27 18:47:05 -080049 * @param[in] fallback
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053050 */
51 Watchdog(sdbusplus::bus::bus& bus,
52 const char* objPath,
Vishwanatha Subbanna3473d702017-05-30 16:38:50 +053053 EventPtr& event,
William A. Kennington III1232a152018-02-02 15:57:34 -080054 std::map<Action, TargetName>&& actionTargets =
William A. Kennington IIId1331082018-02-27 18:47:05 -080055 std::map<Action, TargetName>(),
56 std::experimental::optional<Fallback>&&
57 fallback = std::experimental::nullopt) :
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053058 WatchdogInherits(bus, objPath),
59 bus(bus),
William A. Kennington III1232a152018-02-02 15:57:34 -080060 actionTargets(std::move(actionTargets)),
William A. Kennington IIId1331082018-02-27 18:47:05 -080061 fallback(std::move(fallback)),
Vishwanatha Subbanna8c5a2292017-05-30 15:34:23 +053062 timer(event, std::bind(&Watchdog::timeOutHandler, this))
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053063 {
William A. Kennington III22352192018-02-27 18:51:44 -080064 // We need to poke the enable mechanism to make sure that the timer
65 // enters the fallback state if the fallback is always enabled.
66 tryFallbackOrDisable();
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053067 }
68
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053069 /** @brief Since we are overriding the setter-enabled but not the
70 * getter-enabled, we need to have this using in order to
71 * allow passthrough usage of the getter-enabled.
72 */
73 using Base::Watchdog::enabled;
74
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053075 /** @brief Enable or disable watchdog
76 * If a watchdog state is changed from disable to enable,
77 * the watchdog timer is set with the default expiration
78 * interval and it starts counting down.
79 * If a watchdog is already enabled, setting @value to true
80 * has no effect.
81 *
82 * @param[in] value - 'true' to enable. 'false' to disable
83 *
84 * @return : applied value if success, previous value otherwise
85 */
86 bool enabled(bool value) override;
87
88 /** @brief Gets the remaining time before watchdog expires.
89 *
William A. Kennington III825f4982018-02-27 19:10:56 -080090 * @return 0 if watchdog is expired.
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053091 * Remaining time in milliseconds otherwise.
92 */
93 uint64_t timeRemaining() const override;
94
95 /** @brief Reset timer to expire after new timeout in milliseconds.
96 *
Gunnar Millsbfe5cb82017-10-25 20:48:50 -050097 * @param[in] value - the time in milliseconds after which
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053098 * the watchdog will expire
99 *
100 * @return: updated timeout value if watchdog is enabled.
101 * 0 otherwise.
102 */
103 uint64_t timeRemaining(uint64_t value) override;
104
105 /** @brief Tells if the referenced timer is expired or not */
106 inline auto timerExpired() const
107 {
108 return timer.expired();
109 }
110
William A. Kennington III0650a3f2018-03-01 10:53:25 -0800111 /** @brief Tells if the timer is running or not */
112 inline bool timerEnabled() const
113 {
114 return timer.getEnabled() != SD_EVENT_OFF;
115 }
116
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +0530117 private:
118 /** @brief sdbusplus handle */
119 sdbusplus::bus::bus& bus;
120
William A. Kennington III1232a152018-02-02 15:57:34 -0800121 /** @brief Map of systemd units to be started when the timer expires */
122 std::map<Action, TargetName> actionTargets;
Vishwanatha Subbanna3473d702017-05-30 16:38:50 +0530123
William A. Kennington IIId1331082018-02-27 18:47:05 -0800124 /** @brief Fallback timer options */
125 std::experimental::optional<Fallback> fallback;
126
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +0530127 /** @brief Contained timer object */
128 Timer timer;
Vishwanatha Subbanna8c5a2292017-05-30 15:34:23 +0530129
130 /** @brief Optional Callback handler on timer expirartion */
131 void timeOutHandler();
William A. Kennington III825f4982018-02-27 19:10:56 -0800132
William A. Kennington IIId1331082018-02-27 18:47:05 -0800133 /** @brief Attempt to enter the fallback watchdog or disables it */
134 void tryFallbackOrDisable();
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +0530135};
136
137} // namespace watchdog
138} // namespace phosphor