blob: e829699ae021c43424af8af1eb8d7f86e0570499 [file] [log] [blame]
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +05301#pragma once
2
3#include <systemd/sd-event.h>
4#include <sdbusplus/bus.hpp>
5#include <sdbusplus/server/object.hpp>
6#include <xyz/openbmc_project/State/Watchdog/server.hpp>
7#include "timer.hpp"
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +05308namespace phosphor
9{
10namespace watchdog
11{
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053012namespace Base = sdbusplus::xyz::openbmc_project::State::server;
13using WatchdogInherits = sdbusplus::server::object::object<Base::Watchdog>;
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053014
15/** @class Watchdog
16 * @brief OpenBMC watchdog implementation.
17 * @details A concrete implementation for the
18 * xyz.openbmc_project.State.Watchdog DBus API.
19 */
20class Watchdog : public WatchdogInherits
21{
22 public:
23 Watchdog() = delete;
24 ~Watchdog() = default;
25 Watchdog(const Watchdog&) = delete;
26 Watchdog& operator=(const Watchdog&) = delete;
27 Watchdog(Watchdog&&) = delete;
28 Watchdog& operator=(Watchdog &&) = delete;
29
William A. Kennington III1232a152018-02-02 15:57:34 -080030 /** @brief Type used to hold the name of a systemd target.
31 */
32 using TargetName = std::string;
33
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053034 /** @brief Constructs the Watchdog object
35 *
William A. Kennington III1232a152018-02-02 15:57:34 -080036 * @param[in] bus - DBus bus to attach to.
37 * @param[in] objPath - Object path to attach to.
38 * @param[in] event - reference to sd_event unique pointer
39 * @param[in] actionTargets - map of systemd targets called on timeout
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053040 */
41 Watchdog(sdbusplus::bus::bus& bus,
42 const char* objPath,
Vishwanatha Subbanna3473d702017-05-30 16:38:50 +053043 EventPtr& event,
William A. Kennington III1232a152018-02-02 15:57:34 -080044 std::map<Action, TargetName>&& actionTargets =
45 std::map<Action, TargetName>()) :
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053046 WatchdogInherits(bus, objPath),
47 bus(bus),
William A. Kennington III1232a152018-02-02 15:57:34 -080048 actionTargets(std::move(actionTargets)),
Vishwanatha Subbanna8c5a2292017-05-30 15:34:23 +053049 timer(event, std::bind(&Watchdog::timeOutHandler, this))
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053050 {
51 // Nothing
52 }
53
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053054 /** @brief Since we are overriding the setter-enabled but not the
55 * getter-enabled, we need to have this using in order to
56 * allow passthrough usage of the getter-enabled.
57 */
58 using Base::Watchdog::enabled;
59
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053060 /** @brief Enable or disable watchdog
61 * If a watchdog state is changed from disable to enable,
62 * the watchdog timer is set with the default expiration
63 * interval and it starts counting down.
64 * If a watchdog is already enabled, setting @value to true
65 * has no effect.
66 *
67 * @param[in] value - 'true' to enable. 'false' to disable
68 *
69 * @return : applied value if success, previous value otherwise
70 */
71 bool enabled(bool value) override;
72
73 /** @brief Gets the remaining time before watchdog expires.
74 *
75 * @return 0 if watchdog is disabled or expired.
76 * Remaining time in milliseconds otherwise.
77 */
78 uint64_t timeRemaining() const override;
79
80 /** @brief Reset timer to expire after new timeout in milliseconds.
81 *
Gunnar Millsbfe5cb82017-10-25 20:48:50 -050082 * @param[in] value - the time in milliseconds after which
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053083 * the watchdog will expire
84 *
85 * @return: updated timeout value if watchdog is enabled.
86 * 0 otherwise.
87 */
88 uint64_t timeRemaining(uint64_t value) override;
89
90 /** @brief Tells if the referenced timer is expired or not */
91 inline auto timerExpired() const
92 {
93 return timer.expired();
94 }
95
William A. Kennington III0650a3f2018-03-01 10:53:25 -080096 /** @brief Tells if the timer is running or not */
97 inline bool timerEnabled() const
98 {
99 return timer.getEnabled() != SD_EVENT_OFF;
100 }
101
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +0530102 private:
103 /** @brief sdbusplus handle */
104 sdbusplus::bus::bus& bus;
105
William A. Kennington III1232a152018-02-02 15:57:34 -0800106 /** @brief Map of systemd units to be started when the timer expires */
107 std::map<Action, TargetName> actionTargets;
Vishwanatha Subbanna3473d702017-05-30 16:38:50 +0530108
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +0530109 /** @brief Contained timer object */
110 Timer timer;
Vishwanatha Subbanna8c5a2292017-05-30 15:34:23 +0530111
112 /** @brief Optional Callback handler on timer expirartion */
113 void timeOutHandler();
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +0530114};
115
116} // namespace watchdog
117} // namespace phosphor