blob: 0f008e3e55003a65ccf2466ce6c6fd4b8a81abbe [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>
6#include <string>
7
8namespace witherspoon
9{
10namespace power
11{
12namespace util
13{
14
Matt Spinlerf0f02b92018-10-25 16:12:43 -050015constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
16constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
Matt Spinler882ce952017-10-05 16:12:41 -050017constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
Matt Spinlerf0f02b92018-10-25 16:12:43 -050018constexpr auto POWEROFF_TARGET = "obmc-chassis-hard-poweroff@0.target";
Matt Spinler974c9162017-08-04 08:36:37 -050019constexpr auto PROPERTY_INTF = "org.freedesktop.DBus.Properties";
20
21/**
22 * @brief Get the service name from the mapper for the
23 * interface and path passed in.
24 *
25 * @param[in] path - the D-Bus path name
26 * @param[in] interface - the D-Bus interface name
27 * @param[in] bus - the D-Bus object
28 *
29 * @return The service name
30 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050031std::string getService(const std::string& path, const std::string& interface,
Matt Spinler974c9162017-08-04 08:36:37 -050032 sdbusplus::bus::bus& bus);
33
34/**
35 * @brief Read a D-Bus property
36 *
37 * @param[in] interface - the interface the property is on
38 * @param[in] propertName - the name of the property
39 * @param[in] path - the D-Bus path
40 * @param[in] service - the D-Bus service
41 * @param[in] bus - the D-Bus object
42 * @param[out] value - filled in with the property value
43 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050044template <typename T>
45void getProperty(const std::string& interface, const std::string& propertyName,
46 const std::string& path, const std::string& service,
47 sdbusplus::bus::bus& bus, T& value)
Matt Spinler974c9162017-08-04 08:36:37 -050048{
49 sdbusplus::message::variant<T> property;
50
Matt Spinlerf0f02b92018-10-25 16:12:43 -050051 auto method = bus.new_method_call(service.c_str(), path.c_str(),
52 PROPERTY_INTF, "Get");
Matt Spinler974c9162017-08-04 08:36:37 -050053
54 method.append(interface, propertyName);
55
56 auto reply = bus.call(method);
Matt Spinler974c9162017-08-04 08:36:37 -050057
58 reply.read(property);
59 value = sdbusplus::message::variant_ns::get<T>(property);
60}
61
Matt Spinler48b4a432017-08-04 11:57:37 -050062/**
Brandon Wyman0a4f5192017-12-06 20:19:08 -060063 * @brief Write a D-Bus property
64 *
65 * @param[in] interface - the interface the property is on
66 * @param[in] propertName - the name of the property
67 * @param[in] path - the D-Bus path
68 * @param[in] service - the D-Bus service
69 * @param[in] bus - the D-Bus object
70 * @param[in] value - the value to set the property to
71 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050072template <typename T>
73void setProperty(const std::string& interface, const std::string& propertyName,
74 const std::string& path, const std::string& service,
75 sdbusplus::bus::bus& bus, T& value)
Brandon Wyman0a4f5192017-12-06 20:19:08 -060076{
77 sdbusplus::message::variant<T> propertyValue(value);
78
Matt Spinlerf0f02b92018-10-25 16:12:43 -050079 auto method = bus.new_method_call(service.c_str(), path.c_str(),
80 PROPERTY_INTF, "Set");
Brandon Wyman0a4f5192017-12-06 20:19:08 -060081
82 method.append(interface, propertyName, propertyValue);
83
84 auto reply = bus.call(method);
Brandon Wyman0a4f5192017-12-06 20:19:08 -060085}
86
87/**
Matt Spinler882ce952017-10-05 16:12:41 -050088 * Logs an error and powers off the system.
Matt Spinler48b4a432017-08-04 11:57:37 -050089 *
Matt Spinler882ce952017-10-05 16:12:41 -050090 * @tparam T - error that will be logged before the power off
Matt Spinler48b4a432017-08-04 11:57:37 -050091 * @param[in] bus - D-Bus object
92 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050093template <typename T>
Matt Spinler882ce952017-10-05 16:12:41 -050094void powerOff(sdbusplus::bus::bus& bus)
95{
96 phosphor::logging::report<T>();
97
Matt Spinlerf0f02b92018-10-25 16:12:43 -050098 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
99 SYSTEMD_INTERFACE, "StartUnit");
Matt Spinler882ce952017-10-05 16:12:41 -0500100
101 method.append(POWEROFF_TARGET);
102 method.append("replace");
103
104 bus.call_noreply(method);
105}
Matt Spinler48b4a432017-08-04 11:57:37 -0500106
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500107} // namespace util
108} // namespace power
109} // namespace witherspoon