blob: 79df438e733cafee18b02303e2ac49cc149f3ba3 [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{
12using WatchdogInherits = sdbusplus::server::object::object<
13 sdbusplus::xyz::openbmc_project::State::server::Watchdog>;
14
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
30 /** @brief Constructs the Watchdog object
31 *
Vishwanatha Subbanna3473d702017-05-30 16:38:50 +053032 * @param[in] bus - DBus bus to attach to.
33 * @param[in] objPath - Object path to attach to.
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053034 * @param[in] event - reference to sd_event unique pointer
Vishwanatha Subbanna3473d702017-05-30 16:38:50 +053035 * @param[in] target - systemd target to be called into on timeout
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053036 */
37 Watchdog(sdbusplus::bus::bus& bus,
38 const char* objPath,
Vishwanatha Subbanna3473d702017-05-30 16:38:50 +053039 EventPtr& event,
40 std::string&& target = std::string()) :
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053041 WatchdogInherits(bus, objPath),
42 bus(bus),
Vishwanatha Subbanna3473d702017-05-30 16:38:50 +053043 target(std::move(target)),
Vishwanatha Subbanna8c5a2292017-05-30 15:34:23 +053044 timer(event, std::bind(&Watchdog::timeOutHandler, this))
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053045 {
46 // Nothing
47 }
48
49 /** @brief Enable or disable watchdog
50 * If a watchdog state is changed from disable to enable,
51 * the watchdog timer is set with the default expiration
52 * interval and it starts counting down.
53 * If a watchdog is already enabled, setting @value to true
54 * has no effect.
55 *
56 * @param[in] value - 'true' to enable. 'false' to disable
57 *
58 * @return : applied value if success, previous value otherwise
59 */
60 bool enabled(bool value) override;
61
62 /** @brief Gets the remaining time before watchdog expires.
63 *
64 * @return 0 if watchdog is disabled or expired.
65 * Remaining time in milliseconds otherwise.
66 */
67 uint64_t timeRemaining() const override;
68
69 /** @brief Reset timer to expire after new timeout in milliseconds.
70 *
71 * @param[in] value - the time in miliseconds after which
72 * the watchdog will expire
73 *
74 * @return: updated timeout value if watchdog is enabled.
75 * 0 otherwise.
76 */
77 uint64_t timeRemaining(uint64_t value) override;
78
79 /** @brief Tells if the referenced timer is expired or not */
80 inline auto timerExpired() const
81 {
82 return timer.expired();
83 }
84
85 private:
86 /** @brief sdbusplus handle */
87 sdbusplus::bus::bus& bus;
88
Vishwanatha Subbanna3473d702017-05-30 16:38:50 +053089 /** @brief Systemd unit to be started when the timer expires */
90 std::string target;
91
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053092 /** @brief Contained timer object */
93 Timer timer;
Vishwanatha Subbanna8c5a2292017-05-30 15:34:23 +053094
95 /** @brief Optional Callback handler on timer expirartion */
96 void timeOutHandler();
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053097};
98
99} // namespace watchdog
100} // namespace phosphor