blob: cf386b86b53c683917b253f66d2cc0b8311f1273 [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 IIIe1628492018-05-11 16:17:28 -070028 bool wasValid = wd_service.isValid(bus);
William A. Kennington III25bc7ac2018-03-15 11:48:41 -070029 auto request = wd_service.newMethodCall(bus, prop_intf, "GetAll");
William A. Kennington III52575252018-02-09 15:54:56 -080030 request.append(wd_intf);
31 auto response = bus.call(request);
32 if (response.is_method_error())
33 {
William A. Kennington III25bc7ac2018-03-15 11:48:41 -070034 wd_service.invalidate();
William A. Kennington IIIe1628492018-05-11 16:17:28 -070035 if (wasValid)
36 {
37 // Retry the request once in case the cached service was stale
38 return getProperties();
39 }
William A. Kennington III52575252018-02-09 15:54:56 -080040 throw std::runtime_error("Failed to get watchdog properties");
41 }
42
43 std::map<std::string, variant<bool, uint64_t, std::string>> properties;
44 response.read(properties);
45 Properties wd_prop;
William A. Kennington IIIde14a022018-02-09 16:11:18 -080046 wd_prop.initialized = get<bool>(properties.at("Initialized"));
William A. Kennington III52575252018-02-09 15:54:56 -080047 wd_prop.enabled = get<bool>(properties.at("Enabled"));
William A. Kennington IIIb638de22018-02-09 16:12:53 -080048 wd_prop.expireAction = Watchdog::convertActionFromString(
49 get<std::string>(properties.at("ExpireAction")));
William A. Kennington III52575252018-02-09 15:54:56 -080050 wd_prop.interval = get<uint64_t>(properties.at("Interval"));
51 wd_prop.timeRemaining = get<uint64_t>(properties.at("TimeRemaining"));
52 return wd_prop;
53}
54
55template <typename T>
56void WatchdogService::setProperty(const std::string& key, const T& val)
57{
William A. Kennington IIIe1628492018-05-11 16:17:28 -070058 bool wasValid = wd_service.isValid(bus);
William A. Kennington III25bc7ac2018-03-15 11:48:41 -070059 auto request = wd_service.newMethodCall(bus, prop_intf, "Set");
William A. Kennington III52575252018-02-09 15:54:56 -080060 request.append(wd_intf, key, variant<T>(val));
61 auto response = bus.call(request);
62 if (response.is_method_error())
63 {
William A. Kennington III25bc7ac2018-03-15 11:48:41 -070064 wd_service.invalidate();
William A. Kennington IIIe1628492018-05-11 16:17:28 -070065 if (wasValid)
66 {
67 // Retry the request once in case the cached service was stale
68 return setProperty(key, val);
69 }
William A. Kennington III52575252018-02-09 15:54:56 -080070 throw std::runtime_error(std::string("Failed to set property: ") + key);
71 }
72}
73
William A. Kennington IIIde14a022018-02-09 16:11:18 -080074void WatchdogService::setInitialized(bool initialized)
75{
76 setProperty("Initialized", initialized);
77}
78
William A. Kennington III52575252018-02-09 15:54:56 -080079void WatchdogService::setEnabled(bool enabled)
80{
81 setProperty("Enabled", enabled);
82}
83
William A. Kennington IIIb638de22018-02-09 16:12:53 -080084void WatchdogService::setExpireAction(Action expireAction)
85{
86 setProperty("ExpireAction", convertForMessage(expireAction));
87}
88
William A. Kennington III52575252018-02-09 15:54:56 -080089void WatchdogService::setInterval(uint64_t interval)
90{
91 setProperty("Interval", interval);
92}
93
94void WatchdogService::setTimeRemaining(uint64_t timeRemaining)
95{
96 setProperty("TimeRemaining", timeRemaining);
97}