blob: 36a967c5ab7b6150da98b96019bc5b3c76e1aba2 [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;
William A. Kennington IIIde14a022018-02-09 16:11:18 -080037 wd_prop.initialized = get<bool>(properties.at("Initialized"));
William A. Kennington III52575252018-02-09 15:54:56 -080038 wd_prop.enabled = get<bool>(properties.at("Enabled"));
39 wd_prop.interval = get<uint64_t>(properties.at("Interval"));
40 wd_prop.timeRemaining = get<uint64_t>(properties.at("TimeRemaining"));
41 return wd_prop;
42}
43
44template <typename T>
45void WatchdogService::setProperty(const std::string& key, const T& val)
46{
47 auto request = bus.new_method_call(wd_service.c_str(), wd_path,
48 prop_intf, "Set");
49 request.append(wd_intf, key, variant<T>(val));
50 auto response = bus.call(request);
51 if (response.is_method_error())
52 {
53 throw std::runtime_error(std::string("Failed to set property: ") + key);
54 }
55}
56
William A. Kennington IIIde14a022018-02-09 16:11:18 -080057void WatchdogService::setInitialized(bool initialized)
58{
59 setProperty("Initialized", initialized);
60}
61
William A. Kennington III52575252018-02-09 15:54:56 -080062void WatchdogService::setEnabled(bool enabled)
63{
64 setProperty("Enabled", enabled);
65}
66
67void WatchdogService::setInterval(uint64_t interval)
68{
69 setProperty("Interval", interval);
70}
71
72void WatchdogService::setTimeRemaining(uint64_t timeRemaining)
73{
74 setProperty("TimeRemaining", timeRemaining);
75}