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