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