William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 1 | #include "watchdog_service.hpp" |
| 2 | |
William A. Kennington III | d541027 | 2018-05-10 11:17:58 -0700 | [diff] [blame^] | 3 | #include <phosphor-logging/elog.hpp> |
| 4 | #include <phosphor-logging/elog-errors.hpp> |
| 5 | #include <phosphor-logging/log.hpp> |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 6 | #include <sdbusplus/bus.hpp> |
| 7 | #include <sdbusplus/message.hpp> |
| 8 | #include <string> |
William A. Kennington III | d541027 | 2018-05-10 11:17:58 -0700 | [diff] [blame^] | 9 | #include <xyz/openbmc_project/Common/error.hpp> |
William A. Kennington III | b638de2 | 2018-02-09 16:12:53 -0800 | [diff] [blame] | 10 | #include <xyz/openbmc_project/State/Watchdog/server.hpp> |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 11 | |
| 12 | #include "host-ipmid/ipmid-api.h" |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 13 | |
William A. Kennington III | d541027 | 2018-05-10 11:17:58 -0700 | [diff] [blame^] | 14 | using phosphor::logging::entry; |
| 15 | using phosphor::logging::elog; |
| 16 | using phosphor::logging::level; |
| 17 | using phosphor::logging::log; |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 18 | using sdbusplus::message::variant_ns::get; |
| 19 | using sdbusplus::message::variant_ns::variant; |
William A. Kennington III | d541027 | 2018-05-10 11:17:58 -0700 | [diff] [blame^] | 20 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
William A. Kennington III | b638de2 | 2018-02-09 16:12:53 -0800 | [diff] [blame] | 21 | using sdbusplus::xyz::openbmc_project::State::server::convertForMessage; |
| 22 | using sdbusplus::xyz::openbmc_project::State::server::Watchdog; |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 23 | |
| 24 | static constexpr char wd_path[] = "/xyz/openbmc_project/watchdog/host0"; |
| 25 | static constexpr char wd_intf[] = "xyz.openbmc_project.State.Watchdog"; |
| 26 | static constexpr char prop_intf[] = "org.freedesktop.DBus.Properties"; |
| 27 | |
William A. Kennington III | 25bc7ac | 2018-03-15 11:48:41 -0700 | [diff] [blame] | 28 | ipmi::ServiceCache WatchdogService::wd_service(wd_intf, wd_path); |
| 29 | |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 30 | WatchdogService::WatchdogService() |
William A. Kennington III | 25bc7ac | 2018-03-15 11:48:41 -0700 | [diff] [blame] | 31 | : bus(ipmid_get_sd_bus_connection()) |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 32 | { |
| 33 | } |
| 34 | |
| 35 | WatchdogService::Properties WatchdogService::getProperties() |
| 36 | { |
William A. Kennington III | e162849 | 2018-05-11 16:17:28 -0700 | [diff] [blame] | 37 | bool wasValid = wd_service.isValid(bus); |
William A. Kennington III | 25bc7ac | 2018-03-15 11:48:41 -0700 | [diff] [blame] | 38 | auto request = wd_service.newMethodCall(bus, prop_intf, "GetAll"); |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 39 | request.append(wd_intf); |
| 40 | auto response = bus.call(request); |
| 41 | if (response.is_method_error()) |
| 42 | { |
William A. Kennington III | 25bc7ac | 2018-03-15 11:48:41 -0700 | [diff] [blame] | 43 | wd_service.invalidate(); |
William A. Kennington III | e162849 | 2018-05-11 16:17:28 -0700 | [diff] [blame] | 44 | if (wasValid) |
| 45 | { |
| 46 | // Retry the request once in case the cached service was stale |
| 47 | return getProperties(); |
| 48 | } |
William A. Kennington III | d541027 | 2018-05-10 11:17:58 -0700 | [diff] [blame^] | 49 | log<level::ERR>("WatchdogService: Method error getting properties"); |
| 50 | elog<InternalFailure>(); |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | std::map<std::string, variant<bool, uint64_t, std::string>> properties; |
| 54 | response.read(properties); |
| 55 | Properties wd_prop; |
William A. Kennington III | de14a02 | 2018-02-09 16:11:18 -0800 | [diff] [blame] | 56 | wd_prop.initialized = get<bool>(properties.at("Initialized")); |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 57 | wd_prop.enabled = get<bool>(properties.at("Enabled")); |
William A. Kennington III | b638de2 | 2018-02-09 16:12:53 -0800 | [diff] [blame] | 58 | wd_prop.expireAction = Watchdog::convertActionFromString( |
| 59 | get<std::string>(properties.at("ExpireAction"))); |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 60 | wd_prop.interval = get<uint64_t>(properties.at("Interval")); |
| 61 | wd_prop.timeRemaining = get<uint64_t>(properties.at("TimeRemaining")); |
| 62 | return wd_prop; |
| 63 | } |
| 64 | |
| 65 | template <typename T> |
| 66 | void WatchdogService::setProperty(const std::string& key, const T& val) |
| 67 | { |
William A. Kennington III | e162849 | 2018-05-11 16:17:28 -0700 | [diff] [blame] | 68 | bool wasValid = wd_service.isValid(bus); |
William A. Kennington III | 25bc7ac | 2018-03-15 11:48:41 -0700 | [diff] [blame] | 69 | auto request = wd_service.newMethodCall(bus, prop_intf, "Set"); |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 70 | request.append(wd_intf, key, variant<T>(val)); |
| 71 | auto response = bus.call(request); |
| 72 | if (response.is_method_error()) |
| 73 | { |
William A. Kennington III | 25bc7ac | 2018-03-15 11:48:41 -0700 | [diff] [blame] | 74 | wd_service.invalidate(); |
William A. Kennington III | e162849 | 2018-05-11 16:17:28 -0700 | [diff] [blame] | 75 | if (wasValid) |
| 76 | { |
| 77 | // Retry the request once in case the cached service was stale |
| 78 | return setProperty(key, val); |
| 79 | } |
William A. Kennington III | d541027 | 2018-05-10 11:17:58 -0700 | [diff] [blame^] | 80 | log<level::ERR>("WatchdogService: Method error setting property", |
| 81 | entry("PROPERTY=%s", key.c_str())); |
| 82 | elog<InternalFailure>(); |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
William A. Kennington III | de14a02 | 2018-02-09 16:11:18 -0800 | [diff] [blame] | 86 | void WatchdogService::setInitialized(bool initialized) |
| 87 | { |
| 88 | setProperty("Initialized", initialized); |
| 89 | } |
| 90 | |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 91 | void WatchdogService::setEnabled(bool enabled) |
| 92 | { |
| 93 | setProperty("Enabled", enabled); |
| 94 | } |
| 95 | |
William A. Kennington III | b638de2 | 2018-02-09 16:12:53 -0800 | [diff] [blame] | 96 | void WatchdogService::setExpireAction(Action expireAction) |
| 97 | { |
| 98 | setProperty("ExpireAction", convertForMessage(expireAction)); |
| 99 | } |
| 100 | |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 101 | void WatchdogService::setInterval(uint64_t interval) |
| 102 | { |
| 103 | setProperty("Interval", interval); |
| 104 | } |
| 105 | |
| 106 | void WatchdogService::setTimeRemaining(uint64_t timeRemaining) |
| 107 | { |
| 108 | setProperty("TimeRemaining", timeRemaining); |
| 109 | } |