blob: efa413ad17c3345bac09cb0af631647e02e831fd [file] [log] [blame]
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +05301#pragma once
2
Patrick Venture8f6c5152018-09-11 17:45:33 -07003#include "timer.hpp"
4
William A. Kennington III73c2cfb2018-09-12 18:01:37 -07005#include <functional>
6#include <map>
7#include <optional>
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +05308#include <sdbusplus/bus.hpp>
9#include <sdbusplus/server/object.hpp>
William A. Kennington III73c2cfb2018-09-12 18:01:37 -070010#include <utility>
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053011#include <xyz/openbmc_project/State/Watchdog/server.hpp>
William A. Kennington III73c2cfb2018-09-12 18:01:37 -070012
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053013namespace phosphor
14{
15namespace watchdog
16{
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053017namespace Base = sdbusplus::xyz::openbmc_project::State::server;
18using WatchdogInherits = sdbusplus::server::object::object<Base::Watchdog>;
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053019
20/** @class Watchdog
21 * @brief OpenBMC watchdog implementation.
22 * @details A concrete implementation for the
23 * xyz.openbmc_project.State.Watchdog DBus API.
24 */
25class Watchdog : public WatchdogInherits
26{
Patrick Venture8f6c5152018-09-11 17:45:33 -070027 public:
28 Watchdog() = delete;
29 ~Watchdog() = default;
30 Watchdog(const Watchdog&) = delete;
31 Watchdog& operator=(const Watchdog&) = delete;
32 Watchdog(Watchdog&&) = delete;
33 Watchdog& operator=(Watchdog&&) = delete;
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053034
Patrick Venture8f6c5152018-09-11 17:45:33 -070035 /** @brief Type used to hold the name of a systemd target.
36 */
37 using TargetName = std::string;
William A. Kennington III1232a152018-02-02 15:57:34 -080038
Patrick Venture8f6c5152018-09-11 17:45:33 -070039 /** @brief Type used to specify the parameters of a fallback watchdog
40 */
41 struct Fallback
42 {
43 Action action;
44 uint64_t interval;
45 bool always;
46 };
William A. Kennington IIId1331082018-02-27 18:47:05 -080047
Patrick Venture8f6c5152018-09-11 17:45:33 -070048 /** @brief Constructs the Watchdog object
49 *
50 * @param[in] bus - DBus bus to attach to.
51 * @param[in] objPath - Object path to attach to.
52 * @param[in] event - reference to sd_event unique pointer
53 * @param[in] actionTargets - map of systemd targets called on timeout
54 * @param[in] fallback
55 */
56 Watchdog(sdbusplus::bus::bus& bus, const char* objPath, EventPtr& event,
57 std::map<Action, TargetName>&& actionTargets =
58 std::map<Action, TargetName>(),
William A. Kennington III73c2cfb2018-09-12 18:01:37 -070059 std::optional<Fallback>&& fallback = std::nullopt) :
Patrick Venture8f6c5152018-09-11 17:45:33 -070060 WatchdogInherits(bus, objPath),
61 bus(bus), actionTargets(std::move(actionTargets)),
62 fallback(std::move(fallback)),
63 timer(event, std::bind(&Watchdog::timeOutHandler, this))
64 {
65 // We need to poke the enable mechanism to make sure that the timer
66 // enters the fallback state if the fallback is always enabled.
67 tryFallbackOrDisable();
68 }
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053069
Patrick Venture8f6c5152018-09-11 17:45:33 -070070 /** @brief Resets the TimeRemaining to the configured Interval
71 * Optionally enables the watchdog.
72 *
73 * @param[in] enableWatchdog - Should the call enable the watchdog
74 */
75 void resetTimeRemaining(bool enableWatchdog) override;
William A. Kennington III1726df62018-04-23 10:28:28 -070076
Patrick Venture8f6c5152018-09-11 17:45:33 -070077 /** @brief Since we are overriding the setter-enabled but not the
78 * getter-enabled, we need to have this using in order to
79 * allow passthrough usage of the getter-enabled.
80 */
81 using Base::Watchdog::enabled;
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053082
Patrick Venture8f6c5152018-09-11 17:45:33 -070083 /** @brief Enable or disable watchdog
84 * If a watchdog state is changed from disable to enable,
85 * the watchdog timer is set with the default expiration
86 * interval and it starts counting down.
87 * If a watchdog is already enabled, setting @value to true
88 * has no effect.
89 *
90 * @param[in] value - 'true' to enable. 'false' to disable
91 *
92 * @return : applied value if success, previous value otherwise
93 */
94 bool enabled(bool value) override;
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053095
Patrick Venture8f6c5152018-09-11 17:45:33 -070096 /** @brief Gets the remaining time before watchdog expires.
97 *
98 * @return 0 if watchdog is expired.
99 * Remaining time in milliseconds otherwise.
100 */
101 uint64_t timeRemaining() const override;
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +0530102
Patrick Venture8f6c5152018-09-11 17:45:33 -0700103 /** @brief Reset timer to expire after new timeout in milliseconds.
104 *
105 * @param[in] value - the time in milliseconds after which
106 * the watchdog will expire
107 *
108 * @return: updated timeout value if watchdog is enabled.
109 * 0 otherwise.
110 */
111 uint64_t timeRemaining(uint64_t value) override;
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +0530112
Patrick Venture8f6c5152018-09-11 17:45:33 -0700113 /** @brief Tells if the referenced timer is expired or not */
114 inline auto timerExpired() const
115 {
116 return timer.expired();
117 }
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +0530118
Patrick Venture8f6c5152018-09-11 17:45:33 -0700119 /** @brief Tells if the timer is running or not */
120 inline bool timerEnabled() const
121 {
122 return timer.getEnabled() != SD_EVENT_OFF;
123 }
William A. Kennington III0650a3f2018-03-01 10:53:25 -0800124
Patrick Venture8f6c5152018-09-11 17:45:33 -0700125 private:
126 /** @brief sdbusplus handle */
127 sdbusplus::bus::bus& bus;
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +0530128
Patrick Venture8f6c5152018-09-11 17:45:33 -0700129 /** @brief Map of systemd units to be started when the timer expires */
130 std::map<Action, TargetName> actionTargets;
Vishwanatha Subbanna3473d702017-05-30 16:38:50 +0530131
Patrick Venture8f6c5152018-09-11 17:45:33 -0700132 /** @brief Fallback timer options */
William A. Kennington III73c2cfb2018-09-12 18:01:37 -0700133 std::optional<Fallback> fallback;
William A. Kennington IIId1331082018-02-27 18:47:05 -0800134
Patrick Venture8f6c5152018-09-11 17:45:33 -0700135 /** @brief Contained timer object */
136 Timer timer;
Vishwanatha Subbanna8c5a2292017-05-30 15:34:23 +0530137
Patrick Venture8f6c5152018-09-11 17:45:33 -0700138 /** @brief Optional Callback handler on timer expirartion */
139 void timeOutHandler();
William A. Kennington III825f4982018-02-27 19:10:56 -0800140
Patrick Venture8f6c5152018-09-11 17:45:33 -0700141 /** @brief Attempt to enter the fallback watchdog or disables it */
142 void tryFallbackOrDisable();
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +0530143};
144
145} // namespace watchdog
146} // namespace phosphor