blob: 1a7a410c590bd3039dc7a7c730cf5bf566adcd22 [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 *
32 * @param[in] bus - DBus bus to attach to
33 * @param[in] objPath - Object path to attach to
34 * @param[in] event - reference to sd_event unique pointer
35 */
36 Watchdog(sdbusplus::bus::bus& bus,
37 const char* objPath,
38 EventPtr& event) :
39 WatchdogInherits(bus, objPath),
40 bus(bus),
Vishwanatha Subbanna8c5a2292017-05-30 15:34:23 +053041 timer(event, std::bind(&Watchdog::timeOutHandler, this))
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053042 {
43 // Nothing
44 }
45
46 /** @brief Enable or disable watchdog
47 * If a watchdog state is changed from disable to enable,
48 * the watchdog timer is set with the default expiration
49 * interval and it starts counting down.
50 * If a watchdog is already enabled, setting @value to true
51 * has no effect.
52 *
53 * @param[in] value - 'true' to enable. 'false' to disable
54 *
55 * @return : applied value if success, previous value otherwise
56 */
57 bool enabled(bool value) override;
58
59 /** @brief Gets the remaining time before watchdog expires.
60 *
61 * @return 0 if watchdog is disabled or expired.
62 * Remaining time in milliseconds otherwise.
63 */
64 uint64_t timeRemaining() const override;
65
66 /** @brief Reset timer to expire after new timeout in milliseconds.
67 *
68 * @param[in] value - the time in miliseconds after which
69 * the watchdog will expire
70 *
71 * @return: updated timeout value if watchdog is enabled.
72 * 0 otherwise.
73 */
74 uint64_t timeRemaining(uint64_t value) override;
75
76 /** @brief Tells if the referenced timer is expired or not */
77 inline auto timerExpired() const
78 {
79 return timer.expired();
80 }
81
82 private:
83 /** @brief sdbusplus handle */
84 sdbusplus::bus::bus& bus;
85
86 /** @brief Contained timer object */
87 Timer timer;
Vishwanatha Subbanna8c5a2292017-05-30 15:34:23 +053088
89 /** @brief Optional Callback handler on timer expirartion */
90 void timeOutHandler();
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053091};
92
93} // namespace watchdog
94} // namespace phosphor