blob: f11c4bff823a81294bfefd6410005f0190c30dde [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>
William A. Kennington IIIb638de22018-02-09 16:12:53 -08006#include <xyz/openbmc_project/State/Watchdog/server.hpp>
William A. Kennington III52575252018-02-09 15:54:56 -08007
8#include "host-ipmid/ipmid-api.h"
9#include "utils.hpp"
10
11using sdbusplus::message::variant_ns::get;
12using sdbusplus::message::variant_ns::variant;
William A. Kennington IIIb638de22018-02-09 16:12:53 -080013using sdbusplus::xyz::openbmc_project::State::server::convertForMessage;
14using sdbusplus::xyz::openbmc_project::State::server::Watchdog;
William A. Kennington III52575252018-02-09 15:54:56 -080015
16static constexpr char wd_path[] = "/xyz/openbmc_project/watchdog/host0";
17static constexpr char wd_intf[] = "xyz.openbmc_project.State.Watchdog";
18static constexpr char prop_intf[] = "org.freedesktop.DBus.Properties";
19
20WatchdogService::WatchdogService()
21 : bus(ipmid_get_sd_bus_connection()),
22 wd_service(ipmi::getService(bus, wd_intf, wd_path))
23{
24}
25
26WatchdogService::Properties WatchdogService::getProperties()
27{
28 auto request = bus.new_method_call(wd_service.c_str(), wd_path,
29 prop_intf, "GetAll");
30 request.append(wd_intf);
31 auto response = bus.call(request);
32 if (response.is_method_error())
33 {
34 throw std::runtime_error("Failed to get watchdog properties");
35 }
36
37 std::map<std::string, variant<bool, uint64_t, std::string>> properties;
38 response.read(properties);
39 Properties wd_prop;
William A. Kennington IIIde14a022018-02-09 16:11:18 -080040 wd_prop.initialized = get<bool>(properties.at("Initialized"));
William A. Kennington III52575252018-02-09 15:54:56 -080041 wd_prop.enabled = get<bool>(properties.at("Enabled"));
William A. Kennington IIIb638de22018-02-09 16:12:53 -080042 wd_prop.expireAction = Watchdog::convertActionFromString(
43 get<std::string>(properties.at("ExpireAction")));
William A. Kennington III52575252018-02-09 15:54:56 -080044 wd_prop.interval = get<uint64_t>(properties.at("Interval"));
45 wd_prop.timeRemaining = get<uint64_t>(properties.at("TimeRemaining"));
46 return wd_prop;
47}
48
49template <typename T>
50void WatchdogService::setProperty(const std::string& key, const T& val)
51{
52 auto request = bus.new_method_call(wd_service.c_str(), wd_path,
53 prop_intf, "Set");
54 request.append(wd_intf, key, variant<T>(val));
55 auto response = bus.call(request);
56 if (response.is_method_error())
57 {
58 throw std::runtime_error(std::string("Failed to set property: ") + key);
59 }
60}
61
William A. Kennington IIIde14a022018-02-09 16:11:18 -080062void WatchdogService::setInitialized(bool initialized)
63{
64 setProperty("Initialized", initialized);
65}
66
William A. Kennington III52575252018-02-09 15:54:56 -080067void WatchdogService::setEnabled(bool enabled)
68{
69 setProperty("Enabled", enabled);
70}
71
William A. Kennington IIIb638de22018-02-09 16:12:53 -080072void WatchdogService::setExpireAction(Action expireAction)
73{
74 setProperty("ExpireAction", convertForMessage(expireAction));
75}
76
William A. Kennington III52575252018-02-09 15:54:56 -080077void WatchdogService::setInterval(uint64_t interval)
78{
79 setProperty("Interval", interval);
80}
81
82void WatchdogService::setTimeRemaining(uint64_t timeRemaining)
83{
84 setProperty("TimeRemaining", timeRemaining);
85}