blob: f9657ac4169b7425e1f821c6056c11b541f295cc [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
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
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053049 /** @brief Since we are overriding the setter-enabled but not the
50 * getter-enabled, we need to have this using in order to
51 * allow passthrough usage of the getter-enabled.
52 */
53 using Base::Watchdog::enabled;
54
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053055 /** @brief Enable or disable watchdog
56 * If a watchdog state is changed from disable to enable,
57 * the watchdog timer is set with the default expiration
58 * interval and it starts counting down.
59 * If a watchdog is already enabled, setting @value to true
60 * has no effect.
61 *
62 * @param[in] value - 'true' to enable. 'false' to disable
63 *
64 * @return : applied value if success, previous value otherwise
65 */
66 bool enabled(bool value) override;
67
68 /** @brief Gets the remaining time before watchdog expires.
69 *
70 * @return 0 if watchdog is disabled or expired.
71 * Remaining time in milliseconds otherwise.
72 */
73 uint64_t timeRemaining() const override;
74
75 /** @brief Reset timer to expire after new timeout in milliseconds.
76 *
Gunnar Millsbfe5cb82017-10-25 20:48:50 -050077 * @param[in] value - the time in milliseconds after which
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053078 * the watchdog will expire
79 *
80 * @return: updated timeout value if watchdog is enabled.
81 * 0 otherwise.
82 */
83 uint64_t timeRemaining(uint64_t value) override;
84
85 /** @brief Tells if the referenced timer is expired or not */
86 inline auto timerExpired() const
87 {
88 return timer.expired();
89 }
90
91 private:
92 /** @brief sdbusplus handle */
93 sdbusplus::bus::bus& bus;
94
Vishwanatha Subbanna3473d702017-05-30 16:38:50 +053095 /** @brief Systemd unit to be started when the timer expires */
96 std::string target;
97
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +053098 /** @brief Contained timer object */
99 Timer timer;
Vishwanatha Subbanna8c5a2292017-05-30 15:34:23 +0530100
101 /** @brief Optional Callback handler on timer expirartion */
102 void timeOutHandler();
Vishwanatha Subbannad7a3f132017-05-29 19:39:08 +0530103};
104
105} // namespace watchdog
106} // namespace phosphor