blob: d3fa3adb4d985567adb1dedf0814fa51687159ea [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"
William A. Kennington III52575252018-02-09 15:54:56 -08009
10using sdbusplus::message::variant_ns::get;
11using sdbusplus::message::variant_ns::variant;
William A. Kennington IIIb638de22018-02-09 16:12:53 -080012using sdbusplus::xyz::openbmc_project::State::server::convertForMessage;
13using sdbusplus::xyz::openbmc_project::State::server::Watchdog;
William A. Kennington III52575252018-02-09 15:54:56 -080014
15static constexpr char wd_path[] = "/xyz/openbmc_project/watchdog/host0";
16static constexpr char wd_intf[] = "xyz.openbmc_project.State.Watchdog";
17static constexpr char prop_intf[] = "org.freedesktop.DBus.Properties";
18
William A. Kennington III25bc7ac2018-03-15 11:48:41 -070019ipmi::ServiceCache WatchdogService::wd_service(wd_intf, wd_path);
20
William A. Kennington III52575252018-02-09 15:54:56 -080021WatchdogService::WatchdogService()
William A. Kennington III25bc7ac2018-03-15 11:48:41 -070022 : bus(ipmid_get_sd_bus_connection())
William A. Kennington III52575252018-02-09 15:54:56 -080023{
24}
25
26WatchdogService::Properties WatchdogService::getProperties()
27{
William A. Kennington III25bc7ac2018-03-15 11:48:41 -070028 auto request = wd_service.newMethodCall(bus, prop_intf, "GetAll");
William A. Kennington III52575252018-02-09 15:54:56 -080029 request.append(wd_intf);
30 auto response = bus.call(request);
31 if (response.is_method_error())
32 {
William A. Kennington III25bc7ac2018-03-15 11:48:41 -070033 wd_service.invalidate();
William A. Kennington III52575252018-02-09 15:54:56 -080034 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{
William A. Kennington III25bc7ac2018-03-15 11:48:41 -070052 auto request = wd_service.newMethodCall(bus, prop_intf, "Set");
William A. Kennington III52575252018-02-09 15:54:56 -080053 request.append(wd_intf, key, variant<T>(val));
54 auto response = bus.call(request);
55 if (response.is_method_error())
56 {
William A. Kennington III25bc7ac2018-03-15 11:48:41 -070057 wd_service.invalidate();
William A. Kennington III52575252018-02-09 15:54:56 -080058 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}