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