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 | 25bc7ac | 2018-03-15 11:48:41 -0700 | [diff] [blame] | 28 | auto request = wd_service.newMethodCall(bus, prop_intf, "GetAll"); |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 29 | request.append(wd_intf); |
| 30 | auto response = bus.call(request); |
| 31 | if (response.is_method_error()) |
| 32 | { |
William A. Kennington III | 25bc7ac | 2018-03-15 11:48:41 -0700 | [diff] [blame] | 33 | wd_service.invalidate(); |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 34 | 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 III | de14a02 | 2018-02-09 16:11:18 -0800 | [diff] [blame] | 40 | wd_prop.initialized = get<bool>(properties.at("Initialized")); |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 41 | wd_prop.enabled = get<bool>(properties.at("Enabled")); |
William A. Kennington III | b638de2 | 2018-02-09 16:12:53 -0800 | [diff] [blame] | 42 | wd_prop.expireAction = Watchdog::convertActionFromString( |
| 43 | get<std::string>(properties.at("ExpireAction"))); |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 44 | 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 | |
| 49 | template <typename T> |
| 50 | void WatchdogService::setProperty(const std::string& key, const T& val) |
| 51 | { |
William A. Kennington III | 25bc7ac | 2018-03-15 11:48:41 -0700 | [diff] [blame] | 52 | auto request = wd_service.newMethodCall(bus, prop_intf, "Set"); |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 53 | request.append(wd_intf, key, variant<T>(val)); |
| 54 | auto response = bus.call(request); |
| 55 | if (response.is_method_error()) |
| 56 | { |
William A. Kennington III | 25bc7ac | 2018-03-15 11:48:41 -0700 | [diff] [blame] | 57 | wd_service.invalidate(); |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 58 | throw std::runtime_error(std::string("Failed to set property: ") + key); |
| 59 | } |
| 60 | } |
| 61 | |
William A. Kennington III | de14a02 | 2018-02-09 16:11:18 -0800 | [diff] [blame] | 62 | void WatchdogService::setInitialized(bool initialized) |
| 63 | { |
| 64 | setProperty("Initialized", initialized); |
| 65 | } |
| 66 | |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 67 | void WatchdogService::setEnabled(bool enabled) |
| 68 | { |
| 69 | setProperty("Enabled", enabled); |
| 70 | } |
| 71 | |
William A. Kennington III | b638de2 | 2018-02-09 16:12:53 -0800 | [diff] [blame] | 72 | void WatchdogService::setExpireAction(Action expireAction) |
| 73 | { |
| 74 | setProperty("ExpireAction", convertForMessage(expireAction)); |
| 75 | } |
| 76 | |
William A. Kennington III | 5257525 | 2018-02-09 15:54:56 -0800 | [diff] [blame] | 77 | void WatchdogService::setInterval(uint64_t interval) |
| 78 | { |
| 79 | setProperty("Interval", interval); |
| 80 | } |
| 81 | |
| 82 | void WatchdogService::setTimeRemaining(uint64_t timeRemaining) |
| 83 | { |
| 84 | setProperty("TimeRemaining", timeRemaining); |
| 85 | } |