Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Matt Spinler | 882ce95 | 2017-10-05 16:12:41 -0500 | [diff] [blame] | 3 | #include <phosphor-logging/elog.hpp> |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 4 | #include <phosphor-logging/log.hpp> |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 5 | #include <sdbusplus/bus.hpp> |
Patrick Williams | 2c4fbc4 | 2020-06-26 15:33:11 -0500 | [diff] [blame] | 6 | |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 7 | #include <string> |
| 8 | |
| 9 | namespace witherspoon |
| 10 | { |
| 11 | namespace power |
| 12 | { |
| 13 | namespace util |
| 14 | { |
| 15 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 16 | constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; |
| 17 | constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1"; |
Matt Spinler | 882ce95 | 2017-10-05 16:12:41 -0500 | [diff] [blame] | 18 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 19 | constexpr auto POWEROFF_TARGET = "obmc-chassis-hard-poweroff@0.target"; |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 20 | constexpr auto PROPERTY_INTF = "org.freedesktop.DBus.Properties"; |
| 21 | |
| 22 | /** |
| 23 | * @brief Get the service name from the mapper for the |
| 24 | * interface and path passed in. |
| 25 | * |
| 26 | * @param[in] path - the D-Bus path name |
| 27 | * @param[in] interface - the D-Bus interface name |
| 28 | * @param[in] bus - the D-Bus object |
| 29 | * |
| 30 | * @return The service name |
| 31 | */ |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 32 | std::string getService(const std::string& path, const std::string& interface, |
Patrick Williams | 1426a10 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 33 | sdbusplus::bus_t& bus); |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * @brief Read a D-Bus property |
| 37 | * |
| 38 | * @param[in] interface - the interface the property is on |
| 39 | * @param[in] propertName - the name of the property |
| 40 | * @param[in] path - the D-Bus path |
| 41 | * @param[in] service - the D-Bus service |
| 42 | * @param[in] bus - the D-Bus object |
| 43 | * @param[out] value - filled in with the property value |
| 44 | */ |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 45 | template <typename T> |
| 46 | void getProperty(const std::string& interface, const std::string& propertyName, |
| 47 | const std::string& path, const std::string& service, |
Patrick Williams | 1426a10 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 48 | sdbusplus::bus_t& bus, T& value) |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 49 | { |
Patrick Williams | d7778fb | 2020-05-13 18:02:03 -0500 | [diff] [blame] | 50 | std::variant<T> property; |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 51 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 52 | auto method = bus.new_method_call(service.c_str(), path.c_str(), |
| 53 | PROPERTY_INTF, "Get"); |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 54 | |
| 55 | method.append(interface, propertyName); |
| 56 | |
| 57 | auto reply = bus.call(method); |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 58 | |
| 59 | reply.read(property); |
Patrick Williams | 5124fb3 | 2020-05-13 11:48:23 -0500 | [diff] [blame] | 60 | value = std::get<T>(property); |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 61 | } |
| 62 | |
Matt Spinler | 48b4a43 | 2017-08-04 11:57:37 -0500 | [diff] [blame] | 63 | /** |
Brandon Wyman | 0a4f519 | 2017-12-06 20:19:08 -0600 | [diff] [blame] | 64 | * @brief Write a D-Bus property |
| 65 | * |
| 66 | * @param[in] interface - the interface the property is on |
| 67 | * @param[in] propertName - the name of the property |
| 68 | * @param[in] path - the D-Bus path |
| 69 | * @param[in] service - the D-Bus service |
| 70 | * @param[in] bus - the D-Bus object |
| 71 | * @param[in] value - the value to set the property to |
| 72 | */ |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 73 | template <typename T> |
| 74 | void setProperty(const std::string& interface, const std::string& propertyName, |
| 75 | const std::string& path, const std::string& service, |
Patrick Williams | 1426a10 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 76 | sdbusplus::bus_t& bus, T& value) |
Brandon Wyman | 0a4f519 | 2017-12-06 20:19:08 -0600 | [diff] [blame] | 77 | { |
Patrick Williams | d7778fb | 2020-05-13 18:02:03 -0500 | [diff] [blame] | 78 | std::variant<T> propertyValue(value); |
Brandon Wyman | 0a4f519 | 2017-12-06 20:19:08 -0600 | [diff] [blame] | 79 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 80 | auto method = bus.new_method_call(service.c_str(), path.c_str(), |
| 81 | PROPERTY_INTF, "Set"); |
Brandon Wyman | 0a4f519 | 2017-12-06 20:19:08 -0600 | [diff] [blame] | 82 | |
| 83 | method.append(interface, propertyName, propertyValue); |
| 84 | |
| 85 | auto reply = bus.call(method); |
Brandon Wyman | 0a4f519 | 2017-12-06 20:19:08 -0600 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /** |
Matt Spinler | 882ce95 | 2017-10-05 16:12:41 -0500 | [diff] [blame] | 89 | * Logs an error and powers off the system. |
Matt Spinler | 48b4a43 | 2017-08-04 11:57:37 -0500 | [diff] [blame] | 90 | * |
Matt Spinler | 882ce95 | 2017-10-05 16:12:41 -0500 | [diff] [blame] | 91 | * @tparam T - error that will be logged before the power off |
Matt Spinler | 48b4a43 | 2017-08-04 11:57:37 -0500 | [diff] [blame] | 92 | * @param[in] bus - D-Bus object |
| 93 | */ |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 94 | template <typename T> |
Patrick Williams | 1426a10 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 95 | void powerOff(sdbusplus::bus_t& bus) |
Matt Spinler | 882ce95 | 2017-10-05 16:12:41 -0500 | [diff] [blame] | 96 | { |
| 97 | phosphor::logging::report<T>(); |
| 98 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 99 | auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT, |
| 100 | SYSTEMD_INTERFACE, "StartUnit"); |
Matt Spinler | 882ce95 | 2017-10-05 16:12:41 -0500 | [diff] [blame] | 101 | |
| 102 | method.append(POWEROFF_TARGET); |
| 103 | method.append("replace"); |
| 104 | |
| 105 | bus.call_noreply(method); |
| 106 | } |
Matt Spinler | 48b4a43 | 2017-08-04 11:57:37 -0500 | [diff] [blame] | 107 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 108 | } // namespace util |
| 109 | } // namespace power |
| 110 | } // namespace witherspoon |