1974c916eSMatt Spinler #pragma once
2974c916eSMatt Spinler 
3882ce956SMatt Spinler #include <phosphor-logging/elog.hpp>
4f0f02b9aSMatt Spinler #include <phosphor-logging/log.hpp>
5974c916eSMatt Spinler #include <sdbusplus/bus.hpp>
62c4fbc4cSPatrick Williams 
7974c916eSMatt Spinler #include <string>
8974c916eSMatt Spinler 
9974c916eSMatt Spinler namespace witherspoon
10974c916eSMatt Spinler {
11974c916eSMatt Spinler namespace power
12974c916eSMatt Spinler {
13974c916eSMatt Spinler namespace util
14974c916eSMatt Spinler {
15974c916eSMatt Spinler 
16882ce956SMatt Spinler constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
17882ce956SMatt Spinler constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
18882ce956SMatt Spinler constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
19882ce956SMatt Spinler constexpr auto POWEROFF_TARGET = "obmc-chassis-hard-poweroff@0.target";
20974c916eSMatt Spinler constexpr auto PROPERTY_INTF = "org.freedesktop.DBus.Properties";
21974c916eSMatt Spinler 
22974c916eSMatt Spinler /**
23974c916eSMatt Spinler  * @brief Get the service name from the mapper for the
24974c916eSMatt Spinler  *        interface and path passed in.
25974c916eSMatt Spinler  *
26974c916eSMatt Spinler  * @param[in] path - the D-Bus path name
27974c916eSMatt Spinler  * @param[in] interface - the D-Bus interface name
28974c916eSMatt Spinler  * @param[in] bus - the D-Bus object
29974c916eSMatt Spinler  *
30974c916eSMatt Spinler  * @return The service name
31974c916eSMatt Spinler  */
32f0f02b9aSMatt Spinler std::string getService(const std::string& path, const std::string& interface,
33*1426a10bSPatrick Williams                        sdbusplus::bus_t& bus);
34974c916eSMatt Spinler 
35974c916eSMatt Spinler /**
36974c916eSMatt Spinler  * @brief Read a D-Bus property
37974c916eSMatt Spinler  *
38974c916eSMatt Spinler  * @param[in] interface - the interface the property is on
39974c916eSMatt Spinler  * @param[in] propertName - the name of the property
40974c916eSMatt Spinler  * @param[in] path - the D-Bus path
41974c916eSMatt Spinler  * @param[in] service - the D-Bus service
42974c916eSMatt Spinler  * @param[in] bus - the D-Bus object
43974c916eSMatt Spinler  * @param[out] value - filled in with the property value
44974c916eSMatt Spinler  */
45974c916eSMatt Spinler template <typename T>
getProperty(const std::string & interface,const std::string & propertyName,const std::string & path,const std::string & service,sdbusplus::bus_t & bus,T & value)46f0f02b9aSMatt Spinler void getProperty(const std::string& interface, const std::string& propertyName,
47f0f02b9aSMatt Spinler                  const std::string& path, const std::string& service,
48*1426a10bSPatrick Williams                  sdbusplus::bus_t& bus, T& value)
49974c916eSMatt Spinler {
50d7778fb0SPatrick Williams     std::variant<T> property;
51974c916eSMatt Spinler 
52f0f02b9aSMatt Spinler     auto method = bus.new_method_call(service.c_str(), path.c_str(),
53f0f02b9aSMatt Spinler                                       PROPERTY_INTF, "Get");
54974c916eSMatt Spinler 
55974c916eSMatt Spinler     method.append(interface, propertyName);
56974c916eSMatt Spinler 
57974c916eSMatt Spinler     auto reply = bus.call(method);
58974c916eSMatt Spinler 
59974c916eSMatt Spinler     reply.read(property);
605124fb35SPatrick Williams     value = std::get<T>(property);
61974c916eSMatt Spinler }
62974c916eSMatt Spinler 
6348b4a430SMatt Spinler /**
640a4f519bSBrandon Wyman  * @brief Write a D-Bus property
650a4f519bSBrandon Wyman  *
660a4f519bSBrandon Wyman  * @param[in] interface - the interface the property is on
670a4f519bSBrandon Wyman  * @param[in] propertName - the name of the property
680a4f519bSBrandon Wyman  * @param[in] path - the D-Bus path
690a4f519bSBrandon Wyman  * @param[in] service - the D-Bus service
700a4f519bSBrandon Wyman  * @param[in] bus - the D-Bus object
710a4f519bSBrandon Wyman  * @param[in] value - the value to set the property to
720a4f519bSBrandon Wyman  */
730a4f519bSBrandon Wyman template <typename T>
setProperty(const std::string & interface,const std::string & propertyName,const std::string & path,const std::string & service,sdbusplus::bus_t & bus,T & value)74f0f02b9aSMatt Spinler void setProperty(const std::string& interface, const std::string& propertyName,
75f0f02b9aSMatt Spinler                  const std::string& path, const std::string& service,
76*1426a10bSPatrick Williams                  sdbusplus::bus_t& bus, T& value)
770a4f519bSBrandon Wyman {
78d7778fb0SPatrick Williams     std::variant<T> propertyValue(value);
790a4f519bSBrandon Wyman 
80f0f02b9aSMatt Spinler     auto method = bus.new_method_call(service.c_str(), path.c_str(),
81f0f02b9aSMatt Spinler                                       PROPERTY_INTF, "Set");
820a4f519bSBrandon Wyman 
830a4f519bSBrandon Wyman     method.append(interface, propertyName, propertyValue);
840a4f519bSBrandon Wyman 
850a4f519bSBrandon Wyman     auto reply = bus.call(method);
860a4f519bSBrandon Wyman }
870a4f519bSBrandon Wyman 
880a4f519bSBrandon Wyman /**
89882ce956SMatt Spinler  * Logs an error and powers off the system.
9048b4a430SMatt Spinler  *
91882ce956SMatt Spinler  * @tparam T - error that will be logged before the power off
9248b4a430SMatt Spinler  * @param[in] bus - D-Bus object
9348b4a430SMatt Spinler  */
94882ce956SMatt Spinler template <typename T>
powerOff(sdbusplus::bus_t & bus)95*1426a10bSPatrick Williams void powerOff(sdbusplus::bus_t& bus)
96882ce956SMatt Spinler {
97882ce956SMatt Spinler     phosphor::logging::report<T>();
98882ce956SMatt Spinler 
99f0f02b9aSMatt Spinler     auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
100f0f02b9aSMatt Spinler                                       SYSTEMD_INTERFACE, "StartUnit");
101882ce956SMatt Spinler 
102882ce956SMatt Spinler     method.append(POWEROFF_TARGET);
103882ce956SMatt Spinler     method.append("replace");
104882ce956SMatt Spinler 
105882ce956SMatt Spinler     bus.call_noreply(method);
106882ce956SMatt Spinler }
10748b4a430SMatt Spinler 
108f0f02b9aSMatt Spinler } // namespace util
109f0f02b9aSMatt Spinler } // namespace power
110f0f02b9aSMatt Spinler } // namespace witherspoon
111