xref: /openbmc/phosphor-power/utility.hpp (revision cfc040c7)
1 #pragma once
2 
3 #include "pmbus.hpp"
4 
5 #include <nlohmann/json.hpp>
6 #include <phosphor-logging/elog.hpp>
7 #include <phosphor-logging/log.hpp>
8 #include <sdbusplus/bus.hpp>
9 #include <string>
10 
11 namespace phosphor
12 {
13 namespace power
14 {
15 namespace util
16 {
17 
18 constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
19 constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
20 constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
21 constexpr auto POWEROFF_TARGET = "obmc-chassis-hard-poweroff@0.target";
22 constexpr auto PROPERTY_INTF = "org.freedesktop.DBus.Properties";
23 
24 /**
25  * @brief Get the service name from the mapper for the
26  *        interface and path passed in.
27  *
28  * @param[in] path - the D-Bus path name
29  * @param[in] interface - the D-Bus interface name
30  * @param[in] bus - the D-Bus object
31  *
32  * @return The service name
33  */
34 std::string getService(const std::string& path, const std::string& interface,
35                        sdbusplus::bus::bus& bus);
36 
37 /**
38  * @brief Read a D-Bus property
39  *
40  * @param[in] interface - the interface the property is on
41  * @param[in] propertName - the name of the property
42  * @param[in] path - the D-Bus path
43  * @param[in] service - the D-Bus service
44  * @param[in] bus - the D-Bus object
45  * @param[out] value - filled in with the property value
46  */
47 template <typename T>
48 void getProperty(const std::string& interface, const std::string& propertyName,
49                  const std::string& path, const std::string& service,
50                  sdbusplus::bus::bus& bus, T& value)
51 {
52     sdbusplus::message::variant<T> property;
53 
54     auto method = bus.new_method_call(service.c_str(), path.c_str(),
55                                       PROPERTY_INTF, "Get");
56 
57     method.append(interface, propertyName);
58 
59     auto reply = bus.call(method);
60 
61     reply.read(property);
62     value = sdbusplus::message::variant_ns::get<T>(property);
63 }
64 
65 /**
66  * @brief Write a D-Bus property
67  *
68  * @param[in] interface - the interface the property is on
69  * @param[in] propertName - the name of the property
70  * @param[in] path - the D-Bus path
71  * @param[in] service - the D-Bus service
72  * @param[in] bus - the D-Bus object
73  * @param[in] value - the value to set the property to
74  */
75 template <typename T>
76 void setProperty(const std::string& interface, const std::string& propertyName,
77                  const std::string& path, const std::string& service,
78                  sdbusplus::bus::bus& bus, T& value)
79 {
80     sdbusplus::message::variant<T> propertyValue(value);
81 
82     auto method = bus.new_method_call(service.c_str(), path.c_str(),
83                                       PROPERTY_INTF, "Set");
84 
85     method.append(interface, propertyName, propertyValue);
86 
87     auto reply = bus.call(method);
88 }
89 
90 /**
91  * Logs an error and powers off the system.
92  *
93  * @tparam T - error that will be logged before the power off
94  * @param[in] bus - D-Bus object
95  */
96 template <typename T>
97 void powerOff(sdbusplus::bus::bus& bus)
98 {
99     phosphor::logging::report<T>();
100 
101     auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
102                                       SYSTEMD_INTERFACE, "StartUnit");
103 
104     method.append(POWEROFF_TARGET);
105     method.append("replace");
106 
107     bus.call_noreply(method);
108 }
109 
110 /**
111  * Load json from a file
112  *
113  * @param[in] path - The path of the json file
114  *
115  * @return The nlohmann::json object
116  */
117 nlohmann::json loadJSONFromFile(const char* path);
118 
119 /**
120  * Get PmBus access type from the json config
121  *
122  * @param[in] json - The json object
123  *
124  * @return The pmbus access type
125  */
126 phosphor::pmbus::Type getPMBusAccessType(const nlohmann::json& json);
127 
128 /**
129  * Check if power is on
130  *
131  * @return true if power is on, otherwise false
132  */
133 bool isPoweredOn(sdbusplus::bus::bus& bus);
134 
135 } // namespace util
136 } // namespace power
137 } // namespace phosphor
138