xref: /openbmc/phosphor-power/utility.hpp (revision 0c9a33d6)
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 using DbusPath = std::string;
26 using DbusProperty = std::string;
27 using DbusService = std::string;
28 using DbusInterface = std::string;
29 using DbusInterfaceList = std::vector<DbusInterface>;
30 using DbusSubtree =
31     std::map<DbusPath, std::map<DbusService, DbusInterfaceList>>;
32 using DbusVariant = std::variant<uint64_t, std::string, std::vector<uint64_t>>;
33 using DbusPropertyMap = std::map<DbusProperty, DbusVariant>;
34 /**
35  * @brief Get the service name from the mapper for the
36  *        interface and path passed in.
37  *
38  * @param[in] path - the D-Bus path name
39  * @param[in] interface - the D-Bus interface name
40  * @param[in] bus - the D-Bus object
41  * @param[in] logError - log error when no service found
42  *
43  * @return The service name
44  */
45 std::string getService(const std::string& path, const std::string& interface,
46                        sdbusplus::bus::bus& bus, bool logError = true);
47 
48 /**
49  * @brief Read a D-Bus property
50  *
51  * @param[in] interface - the interface the property is on
52  * @param[in] propertName - the name of the property
53  * @param[in] path - the D-Bus path
54  * @param[in] service - the D-Bus service
55  * @param[in] bus - the D-Bus object
56  * @param[out] value - filled in with the property value
57  */
58 template <typename T>
59 void getProperty(const std::string& interface, const std::string& propertyName,
60                  const std::string& path, const std::string& service,
61                  sdbusplus::bus::bus& bus, T& value)
62 {
63     std::variant<T> property;
64 
65     auto method = bus.new_method_call(service.c_str(), path.c_str(),
66                                       PROPERTY_INTF, "Get");
67 
68     method.append(interface, propertyName);
69 
70     auto reply = bus.call(method);
71 
72     reply.read(property);
73     value = std::get<T>(property);
74 }
75 
76 /**
77  * @brief Write a D-Bus property
78  *
79  * @param[in] interface - the interface the property is on
80  * @param[in] propertName - the name of the property
81  * @param[in] path - the D-Bus path
82  * @param[in] service - the D-Bus service
83  * @param[in] bus - the D-Bus object
84  * @param[in] value - the value to set the property to
85  */
86 template <typename T>
87 void setProperty(const std::string& interface, const std::string& propertyName,
88                  const std::string& path, const std::string& service,
89                  sdbusplus::bus::bus& bus, T& value)
90 {
91     std::variant<T> propertyValue(value);
92 
93     auto method = bus.new_method_call(service.c_str(), path.c_str(),
94                                       PROPERTY_INTF, "Set");
95 
96     method.append(interface, propertyName, propertyValue);
97 
98     auto reply = bus.call(method);
99 }
100 
101 /**
102  * @brief Get all D-Bus properties
103  *
104  * @param[in] bus - the D-Bus object
105  * @param[in] path - the D-Bus object path
106  * @param[in] interface - the D-Bus interface name
107  * @param[in] service - the D-Bus service name (optional)
108  *
109  * @return DbusPropertyMap - Map of property names and values
110  */
111 DbusPropertyMap getAllProperties(sdbusplus::bus::bus& bus,
112                                  const std::string& path,
113                                  const std::string& interface,
114                                  const std::string& service = std::string());
115 
116 /** @brief Get subtree from the object mapper.
117  *
118  * Helper function to find objects, services, and interfaces.
119  * See:
120  * https://github.com/openbmc/docs/blob/master/architecture/object-mapper.md
121  *
122  * @param[in] bus - The D-Bus object.
123  * @param[in] path - The root of the tree to search.
124  * @param[in] interface - Interface in the subtree to search for
125  * @param[in] depth - The number of path elements to descend.
126  *
127  * @return DbusSubtree - Map of object paths to a map of service names to their
128  *                       interfaces.
129  */
130 DbusSubtree getSubTree(sdbusplus::bus::bus& bus, const std::string& path,
131                        const std::string& interface, int32_t depth);
132 
133 /**
134  * Logs an error and powers off the system.
135  *
136  * @tparam T - error that will be logged before the power off
137  * @param[in] bus - D-Bus object
138  */
139 template <typename T>
140 void powerOff(sdbusplus::bus::bus& bus)
141 {
142     phosphor::logging::report<T>();
143 
144     auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
145                                       SYSTEMD_INTERFACE, "StartUnit");
146 
147     method.append(POWEROFF_TARGET);
148     method.append("replace");
149 
150     bus.call_noreply(method);
151 }
152 
153 /**
154  * Load json from a file
155  *
156  * @param[in] path - The path of the json file
157  *
158  * @return The nlohmann::json object
159  */
160 nlohmann::json loadJSONFromFile(const char* path);
161 
162 /**
163  * Get PmBus access type from the json config
164  *
165  * @param[in] json - The json object
166  *
167  * @return The pmbus access type
168  */
169 phosphor::pmbus::Type getPMBusAccessType(const nlohmann::json& json);
170 
171 /**
172  * Check if power is on
173  *
174  * @param[in] bus - D-Bus object
175  * @param[in] defaultState - The default state if the function fails to get
176  *                           the power state.
177  *
178  * @return true if power is on, otherwise false;
179  *         defaultState if it fails to get the power state.
180  */
181 bool isPoweredOn(sdbusplus::bus::bus& bus, bool defaultState = false);
182 
183 /**
184  * Get all PSU inventory paths from D-Bus
185  *
186  * @param[in] bus - D-Bus object
187  *
188  * @return The list of PSU inventory paths
189  */
190 std::vector<std::string> getPSUInventoryPaths(sdbusplus::bus::bus& bus);
191 
192 } // namespace util
193 } // namespace power
194 } // namespace phosphor
195