blob: 7f5d1799eb385282688cb77510d445ef9d2e80c3 [file] [log] [blame]
William A. Kennington III52575252018-02-09 15:54:56 -08001#include "watchdog_service.hpp"
2
3#include <sdbusplus/bus.hpp>
4#include <sdbusplus/message.hpp>
5#include <string>
6
7#include "host-ipmid/ipmid-api.h"
8#include "utils.hpp"
9
10using sdbusplus::message::variant_ns::get;
11using sdbusplus::message::variant_ns::variant;
12
13static constexpr char wd_path[] = "/xyz/openbmc_project/watchdog/host0";
14static constexpr char wd_intf[] = "xyz.openbmc_project.State.Watchdog";
15static constexpr char prop_intf[] = "org.freedesktop.DBus.Properties";
16
17WatchdogService::WatchdogService()
18 : bus(ipmid_get_sd_bus_connection()),
19 wd_service(ipmi::getService(bus, wd_intf, wd_path))
20{
21}
22
23WatchdogService::Properties WatchdogService::getProperties()
24{
25 auto request = bus.new_method_call(wd_service.c_str(), wd_path,
26 prop_intf, "GetAll");
27 request.append(wd_intf);
28 auto response = bus.call(request);
29 if (response.is_method_error())
30 {
31 throw std::runtime_error("Failed to get watchdog properties");
32 }
33
34 std::map<std::string, variant<bool, uint64_t, std::string>> properties;
35 response.read(properties);
36 Properties wd_prop;
37 wd_prop.enabled = get<bool>(properties.at("Enabled"));
38 wd_prop.interval = get<uint64_t>(properties.at("Interval"));
39 wd_prop.timeRemaining = get<uint64_t>(properties.at("TimeRemaining"));
40 return wd_prop;
41}
42
43template <typename T>
44void WatchdogService::setProperty(const std::string& key, const T& val)
45{
46 auto request = bus.new_method_call(wd_service.c_str(), wd_path,
47 prop_intf, "Set");
48 request.append(wd_intf, key, variant<T>(val));
49 auto response = bus.call(request);
50 if (response.is_method_error())
51 {
52 throw std::runtime_error(std::string("Failed to set property: ") + key);
53 }
54}
55
56void WatchdogService::setEnabled(bool enabled)
57{
58 setProperty("Enabled", enabled);
59}
60
61void WatchdogService::setInterval(uint64_t interval)
62{
63 setProperty("Interval", interval);
64}
65
66void WatchdogService::setTimeRemaining(uint64_t timeRemaining)
67{
68 setProperty("TimeRemaining", timeRemaining);
69}