blob: 26962dc1f9cfa51f4a4efeddb5a55eae405cfb54 [file] [log] [blame]
William A. Kennington III52575252018-02-09 15:54:56 -08001#include "watchdog_service.hpp"
2
William A. Kennington IIId5410272018-05-10 11:17:58 -07003#include <phosphor-logging/elog.hpp>
4#include <phosphor-logging/elog-errors.hpp>
5#include <phosphor-logging/log.hpp>
William A. Kennington III52575252018-02-09 15:54:56 -08006#include <sdbusplus/bus.hpp>
7#include <sdbusplus/message.hpp>
8#include <string>
William A. Kennington IIId5410272018-05-10 11:17:58 -07009#include <xyz/openbmc_project/Common/error.hpp>
William A. Kennington IIIb638de22018-02-09 16:12:53 -080010#include <xyz/openbmc_project/State/Watchdog/server.hpp>
William A. Kennington III52575252018-02-09 15:54:56 -080011
12#include "host-ipmid/ipmid-api.h"
William A. Kennington III52575252018-02-09 15:54:56 -080013
William A. Kennington IIId5410272018-05-10 11:17:58 -070014using phosphor::logging::entry;
15using phosphor::logging::elog;
16using phosphor::logging::level;
17using phosphor::logging::log;
William A. Kennington III52575252018-02-09 15:54:56 -080018using sdbusplus::message::variant_ns::get;
19using sdbusplus::message::variant_ns::variant;
William A. Kennington IIId5410272018-05-10 11:17:58 -070020using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
William A. Kennington IIIb638de22018-02-09 16:12:53 -080021using sdbusplus::xyz::openbmc_project::State::server::convertForMessage;
22using sdbusplus::xyz::openbmc_project::State::server::Watchdog;
William A. Kennington III52575252018-02-09 15:54:56 -080023
24static constexpr char wd_path[] = "/xyz/openbmc_project/watchdog/host0";
25static constexpr char wd_intf[] = "xyz.openbmc_project.State.Watchdog";
26static constexpr char prop_intf[] = "org.freedesktop.DBus.Properties";
27
William A. Kennington III25bc7ac2018-03-15 11:48:41 -070028ipmi::ServiceCache WatchdogService::wd_service(wd_intf, wd_path);
29
William A. Kennington III52575252018-02-09 15:54:56 -080030WatchdogService::WatchdogService()
William A. Kennington III25bc7ac2018-03-15 11:48:41 -070031 : bus(ipmid_get_sd_bus_connection())
William A. Kennington III52575252018-02-09 15:54:56 -080032{
33}
34
William A. Kennington III4b017a92018-04-27 14:31:08 -070035void WatchdogService::resetTimeRemaining(bool enableWatchdog)
36{
37 bool wasValid = wd_service.isValid(bus);
38 auto request = wd_service.newMethodCall(
39 bus, wd_intf, "ResetTimeRemaining");
40 request.append(enableWatchdog);
41 auto response = bus.call(request);
42 if (response.is_method_error())
43 {
44 wd_service.invalidate();
45 if (wasValid)
46 {
47 // Retry the request once in case the cached service was stale
48 return resetTimeRemaining(enableWatchdog);
49 }
50 log<level::ERR>(
51 "WatchdogService: Method error resetting time remaining",
52 entry("ENABLE_WATCHDOG=%d", !!enableWatchdog));
53 elog<InternalFailure>();
54 }
55}
56
William A. Kennington III52575252018-02-09 15:54:56 -080057WatchdogService::Properties WatchdogService::getProperties()
58{
William A. Kennington IIIe1628492018-05-11 16:17:28 -070059 bool wasValid = wd_service.isValid(bus);
William A. Kennington III25bc7ac2018-03-15 11:48:41 -070060 auto request = wd_service.newMethodCall(bus, prop_intf, "GetAll");
William A. Kennington III52575252018-02-09 15:54:56 -080061 request.append(wd_intf);
62 auto response = bus.call(request);
63 if (response.is_method_error())
64 {
William A. Kennington III25bc7ac2018-03-15 11:48:41 -070065 wd_service.invalidate();
William A. Kennington IIIe1628492018-05-11 16:17:28 -070066 if (wasValid)
67 {
68 // Retry the request once in case the cached service was stale
69 return getProperties();
70 }
William A. Kennington IIId5410272018-05-10 11:17:58 -070071 log<level::ERR>("WatchdogService: Method error getting properties");
72 elog<InternalFailure>();
William A. Kennington III52575252018-02-09 15:54:56 -080073 }
74
75 std::map<std::string, variant<bool, uint64_t, std::string>> properties;
76 response.read(properties);
77 Properties wd_prop;
William A. Kennington IIIde14a022018-02-09 16:11:18 -080078 wd_prop.initialized = get<bool>(properties.at("Initialized"));
William A. Kennington III52575252018-02-09 15:54:56 -080079 wd_prop.enabled = get<bool>(properties.at("Enabled"));
William A. Kennington IIIb638de22018-02-09 16:12:53 -080080 wd_prop.expireAction = Watchdog::convertActionFromString(
81 get<std::string>(properties.at("ExpireAction")));
William A. Kennington III52575252018-02-09 15:54:56 -080082 wd_prop.interval = get<uint64_t>(properties.at("Interval"));
83 wd_prop.timeRemaining = get<uint64_t>(properties.at("TimeRemaining"));
84 return wd_prop;
85}
86
87template <typename T>
88void WatchdogService::setProperty(const std::string& key, const T& val)
89{
William A. Kennington IIIe1628492018-05-11 16:17:28 -070090 bool wasValid = wd_service.isValid(bus);
William A. Kennington III25bc7ac2018-03-15 11:48:41 -070091 auto request = wd_service.newMethodCall(bus, prop_intf, "Set");
William A. Kennington III52575252018-02-09 15:54:56 -080092 request.append(wd_intf, key, variant<T>(val));
93 auto response = bus.call(request);
94 if (response.is_method_error())
95 {
William A. Kennington III25bc7ac2018-03-15 11:48:41 -070096 wd_service.invalidate();
William A. Kennington IIIe1628492018-05-11 16:17:28 -070097 if (wasValid)
98 {
99 // Retry the request once in case the cached service was stale
100 return setProperty(key, val);
101 }
William A. Kennington IIId5410272018-05-10 11:17:58 -0700102 log<level::ERR>("WatchdogService: Method error setting property",
103 entry("PROPERTY=%s", key.c_str()));
104 elog<InternalFailure>();
William A. Kennington III52575252018-02-09 15:54:56 -0800105 }
106}
107
William A. Kennington IIIde14a022018-02-09 16:11:18 -0800108void WatchdogService::setInitialized(bool initialized)
109{
110 setProperty("Initialized", initialized);
111}
112
William A. Kennington III52575252018-02-09 15:54:56 -0800113void WatchdogService::setEnabled(bool enabled)
114{
115 setProperty("Enabled", enabled);
116}
117
William A. Kennington IIIb638de22018-02-09 16:12:53 -0800118void WatchdogService::setExpireAction(Action expireAction)
119{
120 setProperty("ExpireAction", convertForMessage(expireAction));
121}
122
William A. Kennington III52575252018-02-09 15:54:56 -0800123void WatchdogService::setInterval(uint64_t interval)
124{
125 setProperty("Interval", interval);
126}
127
128void WatchdogService::setTimeRemaining(uint64_t timeRemaining)
129{
130 setProperty("TimeRemaining", timeRemaining);
131}