blob: 1311fcbea7fb6e5ace039bbdceda7e91179be524 [file] [log] [blame]
Matt Spinler974c9162017-08-04 08:36:37 -05001#pragma once
2
Matt Spinler882ce952017-10-05 16:12:41 -05003#include <phosphor-logging/elog.hpp>
Matt Spinlerf0f02b92018-10-25 16:12:43 -05004#include <phosphor-logging/log.hpp>
Matt Spinler974c9162017-08-04 08:36:37 -05005#include <sdbusplus/bus.hpp>
Patrick Williams2c4fbc42020-06-26 15:33:11 -05006
Matt Spinler974c9162017-08-04 08:36:37 -05007#include <string>
8
9namespace witherspoon
10{
11namespace power
12{
13namespace util
14{
15
Matt Spinlerf0f02b92018-10-25 16:12:43 -050016constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
17constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
Matt Spinler882ce952017-10-05 16:12:41 -050018constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
Matt Spinlerf0f02b92018-10-25 16:12:43 -050019constexpr auto POWEROFF_TARGET = "obmc-chassis-hard-poweroff@0.target";
Matt Spinler974c9162017-08-04 08:36:37 -050020constexpr 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 Spinlerf0f02b92018-10-25 16:12:43 -050032std::string getService(const std::string& path, const std::string& interface,
Patrick Williams1426a102022-07-22 19:26:55 -050033 sdbusplus::bus_t& bus);
Matt Spinler974c9162017-08-04 08:36:37 -050034
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 Spinlerf0f02b92018-10-25 16:12:43 -050045template <typename T>
46void getProperty(const std::string& interface, const std::string& propertyName,
47 const std::string& path, const std::string& service,
Patrick Williams1426a102022-07-22 19:26:55 -050048 sdbusplus::bus_t& bus, T& value)
Matt Spinler974c9162017-08-04 08:36:37 -050049{
Patrick Williamsd7778fb2020-05-13 18:02:03 -050050 std::variant<T> property;
Matt Spinler974c9162017-08-04 08:36:37 -050051
Matt Spinlerf0f02b92018-10-25 16:12:43 -050052 auto method = bus.new_method_call(service.c_str(), path.c_str(),
53 PROPERTY_INTF, "Get");
Matt Spinler974c9162017-08-04 08:36:37 -050054
55 method.append(interface, propertyName);
56
57 auto reply = bus.call(method);
Matt Spinler974c9162017-08-04 08:36:37 -050058
59 reply.read(property);
Patrick Williams5124fb32020-05-13 11:48:23 -050060 value = std::get<T>(property);
Matt Spinler974c9162017-08-04 08:36:37 -050061}
62
Matt Spinler48b4a432017-08-04 11:57:37 -050063/**
Brandon Wyman0a4f5192017-12-06 20:19:08 -060064 * @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 Spinlerf0f02b92018-10-25 16:12:43 -050073template <typename T>
74void setProperty(const std::string& interface, const std::string& propertyName,
75 const std::string& path, const std::string& service,
Patrick Williams1426a102022-07-22 19:26:55 -050076 sdbusplus::bus_t& bus, T& value)
Brandon Wyman0a4f5192017-12-06 20:19:08 -060077{
Patrick Williamsd7778fb2020-05-13 18:02:03 -050078 std::variant<T> propertyValue(value);
Brandon Wyman0a4f5192017-12-06 20:19:08 -060079
Matt Spinlerf0f02b92018-10-25 16:12:43 -050080 auto method = bus.new_method_call(service.c_str(), path.c_str(),
81 PROPERTY_INTF, "Set");
Brandon Wyman0a4f5192017-12-06 20:19:08 -060082
83 method.append(interface, propertyName, propertyValue);
84
85 auto reply = bus.call(method);
Brandon Wyman0a4f5192017-12-06 20:19:08 -060086}
87
88/**
Matt Spinler882ce952017-10-05 16:12:41 -050089 * Logs an error and powers off the system.
Matt Spinler48b4a432017-08-04 11:57:37 -050090 *
Matt Spinler882ce952017-10-05 16:12:41 -050091 * @tparam T - error that will be logged before the power off
Matt Spinler48b4a432017-08-04 11:57:37 -050092 * @param[in] bus - D-Bus object
93 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050094template <typename T>
Patrick Williams1426a102022-07-22 19:26:55 -050095void powerOff(sdbusplus::bus_t& bus)
Matt Spinler882ce952017-10-05 16:12:41 -050096{
97 phosphor::logging::report<T>();
98
Matt Spinlerf0f02b92018-10-25 16:12:43 -050099 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
100 SYSTEMD_INTERFACE, "StartUnit");
Matt Spinler882ce952017-10-05 16:12:41 -0500101
102 method.append(POWEROFF_TARGET);
103 method.append("replace");
104
105 bus.call_noreply(method);
106}
Matt Spinler48b4a432017-08-04 11:57:37 -0500107
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500108} // namespace util
109} // namespace power
110} // namespace witherspoon