blob: e721b9b0e18c10f6c1ab3a35202ca22bd442fa43 [file] [log] [blame]
Matt Spinler974c9162017-08-04 08:36:37 -05001#pragma once
2
Lei YU40705462019-10-09 17:07:11 +08003#include "pmbus.hpp"
4
Lei YU7dc31bb2019-08-30 10:07:08 +08005#include <nlohmann/json.hpp>
Matt Spinler882ce952017-10-05 16:12:41 -05006#include <phosphor-logging/elog.hpp>
Matt Spinlerf0f02b92018-10-25 16:12:43 -05007#include <phosphor-logging/log.hpp>
Matt Spinler974c9162017-08-04 08:36:37 -05008#include <sdbusplus/bus.hpp>
Brandon Wymand1bc4ce2019-12-13 14:20:34 -06009
Matt Spinler974c9162017-08-04 08:36:37 -050010#include <string>
Shawn McCarney8270b7d2023-06-26 11:35:51 -050011#include <vector>
Matt Spinler974c9162017-08-04 08:36:37 -050012
Lei YUab093322019-10-09 16:43:22 +080013namespace phosphor
Matt Spinler974c9162017-08-04 08:36:37 -050014{
15namespace power
16{
17namespace util
18{
19
Matt Spinlerf0f02b92018-10-25 16:12:43 -050020constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
21constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
Matt Spinler882ce952017-10-05 16:12:41 -050022constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
Matt Spinlerf0f02b92018-10-25 16:12:43 -050023constexpr auto POWEROFF_TARGET = "obmc-chassis-hard-poweroff@0.target";
Matt Spinler974c9162017-08-04 08:36:37 -050024constexpr auto PROPERTY_INTF = "org.freedesktop.DBus.Properties";
25
Brandon Wymanc761b5f2020-11-05 18:30:41 -060026using DbusPath = std::string;
Adriana Kobylak4e8b3352021-03-16 20:38:50 +000027using DbusProperty = std::string;
Brandon Wymanc761b5f2020-11-05 18:30:41 -060028using DbusService = std::string;
29using DbusInterface = std::string;
30using DbusInterfaceList = std::vector<DbusInterface>;
31using DbusSubtree =
32 std::map<DbusPath, std::map<DbusService, DbusInterfaceList>>;
Adriana Kobylak886574c2021-11-01 18:22:28 +000033using DbusVariant =
Shawn McCarney3cc348c2024-05-29 15:41:31 -050034 std::variant<bool, uint64_t, std::string, std::vector<uint64_t>,
35 std::vector<std::string>>;
Adriana Kobylak4e8b3352021-03-16 20:38:50 +000036using DbusPropertyMap = std::map<DbusProperty, DbusVariant>;
Matt Spinler974c9162017-08-04 08:36:37 -050037/**
38 * @brief Get the service name from the mapper for the
39 * interface and path passed in.
40 *
41 * @param[in] path - the D-Bus path name
42 * @param[in] interface - the D-Bus interface name
43 * @param[in] bus - the D-Bus object
Matthew Barthd2624402020-02-03 15:35:12 -060044 * @param[in] logError - log error when no service found
Matt Spinler974c9162017-08-04 08:36:37 -050045 *
46 * @return The service name
47 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050048std::string getService(const std::string& path, const std::string& interface,
Patrick Williams7354ce62022-07-22 19:26:56 -050049 sdbusplus::bus_t& bus, bool logError = true);
Matt Spinler974c9162017-08-04 08:36:37 -050050
51/**
52 * @brief Read a D-Bus property
53 *
54 * @param[in] interface - the interface the property is on
55 * @param[in] propertName - the name of the property
56 * @param[in] path - the D-Bus path
57 * @param[in] service - the D-Bus service
58 * @param[in] bus - the D-Bus object
59 * @param[out] value - filled in with the property value
60 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050061template <typename T>
62void getProperty(const std::string& interface, const std::string& propertyName,
63 const std::string& path, const std::string& service,
Patrick Williams7354ce62022-07-22 19:26:56 -050064 sdbusplus::bus_t& bus, T& value)
Matt Spinler974c9162017-08-04 08:36:37 -050065{
Patrick Williamsabe49412020-05-13 17:59:47 -050066 std::variant<T> property;
Matt Spinler974c9162017-08-04 08:36:37 -050067
Matt Spinlerf0f02b92018-10-25 16:12:43 -050068 auto method = bus.new_method_call(service.c_str(), path.c_str(),
69 PROPERTY_INTF, "Get");
Matt Spinler974c9162017-08-04 08:36:37 -050070
71 method.append(interface, propertyName);
72
73 auto reply = bus.call(method);
Matt Spinler974c9162017-08-04 08:36:37 -050074
75 reply.read(property);
Patrick Williams365d61c2020-05-13 12:23:08 -050076 value = std::get<T>(property);
Matt Spinler974c9162017-08-04 08:36:37 -050077}
78
Matt Spinler48b4a432017-08-04 11:57:37 -050079/**
Brandon Wyman0a4f5192017-12-06 20:19:08 -060080 * @brief Write a D-Bus property
81 *
82 * @param[in] interface - the interface the property is on
83 * @param[in] propertName - the name of the property
84 * @param[in] path - the D-Bus path
85 * @param[in] service - the D-Bus service
86 * @param[in] bus - the D-Bus object
87 * @param[in] value - the value to set the property to
88 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050089template <typename T>
90void setProperty(const std::string& interface, const std::string& propertyName,
91 const std::string& path, const std::string& service,
Patrick Williams7354ce62022-07-22 19:26:56 -050092 sdbusplus::bus_t& bus, T& value)
Brandon Wyman0a4f5192017-12-06 20:19:08 -060093{
Patrick Williamsabe49412020-05-13 17:59:47 -050094 std::variant<T> propertyValue(value);
Brandon Wyman0a4f5192017-12-06 20:19:08 -060095
Matt Spinlerf0f02b92018-10-25 16:12:43 -050096 auto method = bus.new_method_call(service.c_str(), path.c_str(),
97 PROPERTY_INTF, "Set");
Brandon Wyman0a4f5192017-12-06 20:19:08 -060098
99 method.append(interface, propertyName, propertyValue);
100
101 auto reply = bus.call(method);
Brandon Wyman0a4f5192017-12-06 20:19:08 -0600102}
103
Adriana Kobylak4e8b3352021-03-16 20:38:50 +0000104/**
105 * @brief Get all D-Bus properties
106 *
107 * @param[in] bus - the D-Bus object
108 * @param[in] path - the D-Bus object path
109 * @param[in] interface - the D-Bus interface name
110 * @param[in] service - the D-Bus service name (optional)
111 *
112 * @return DbusPropertyMap - Map of property names and values
113 */
Patrick Williams7354ce62022-07-22 19:26:56 -0500114DbusPropertyMap getAllProperties(sdbusplus::bus_t& bus, const std::string& path,
Adriana Kobylak4e8b3352021-03-16 20:38:50 +0000115 const std::string& interface,
116 const std::string& service = std::string());
117
Brandon Wymanc761b5f2020-11-05 18:30:41 -0600118/** @brief Get subtree from the object mapper.
119 *
120 * Helper function to find objects, services, and interfaces.
121 * See:
122 * https://github.com/openbmc/docs/blob/master/architecture/object-mapper.md
123 *
124 * @param[in] bus - The D-Bus object.
125 * @param[in] path - The root of the tree to search.
126 * @param[in] interface - Interface in the subtree to search for
127 * @param[in] depth - The number of path elements to descend.
128 *
129 * @return DbusSubtree - Map of object paths to a map of service names to their
130 * interfaces.
131 */
Patrick Williams7354ce62022-07-22 19:26:56 -0500132DbusSubtree getSubTree(sdbusplus::bus_t& bus, const std::string& path,
Brandon Wymanc761b5f2020-11-05 18:30:41 -0600133 const std::string& interface, int32_t depth);
134
Shawn McCarney8270b7d2023-06-26 11:35:51 -0500135/** @brief Get subtree from the object mapper.
136 *
137 * Helper function to find objects, services, and interfaces.
138 * See:
139 * https://github.com/openbmc/docs/blob/master/architecture/object-mapper.md
140 *
141 * @param[in] bus - The D-Bus object.
142 * @param[in] path - The root of the tree to search.
143 * @param[in] interfaces - Interfaces in the subtree to search for.
144 * @param[in] depth - The number of path elements to descend.
145 *
146 * @return DbusSubtree - Map of object paths to a map of service names to their
147 * interfaces.
148 */
149DbusSubtree getSubTree(sdbusplus::bus_t& bus, const std::string& path,
150 const std::vector<std::string>& interfaces,
151 int32_t depth);
152
Matt Spinler06594222023-05-01 10:44:43 -0500153/** @brief GetAssociatedSubTreePaths wrapper from the object mapper.
154 *
155 * Helper function to find object paths that implement a certain
156 * interface and are also an association endpoint.
157 * See:
158 * https://github.com/openbmc/docs/blob/master/architecture/object-mapper.md
159 *
160 * @param[in] bus - The D-Bus object.
161 * @param[in] associationPath - The association it must be an endpoint of.
162 * @param[in] path - The root of the tree to search.
163 * @param[in] interfaces - The interfaces in the subtree to search for
164 * @param[in] depth - The number of path elements to descend.
165 *
166 * @return std::vector<DbusPath> - The object paths.
167 */
168std::vector<DbusPath> getAssociatedSubTreePaths(
169 sdbusplus::bus_t& bus,
170 const sdbusplus::message::object_path& associationPath,
171 const sdbusplus::message::object_path& path,
172 const std::vector<std::string>& interfaces, int32_t depth);
173
Brandon Wyman0a4f5192017-12-06 20:19:08 -0600174/**
Matt Spinler882ce952017-10-05 16:12:41 -0500175 * Logs an error and powers off the system.
Matt Spinler48b4a432017-08-04 11:57:37 -0500176 *
Matt Spinler882ce952017-10-05 16:12:41 -0500177 * @tparam T - error that will be logged before the power off
Matt Spinler48b4a432017-08-04 11:57:37 -0500178 * @param[in] bus - D-Bus object
179 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500180template <typename T>
Patrick Williams7354ce62022-07-22 19:26:56 -0500181void powerOff(sdbusplus::bus_t& bus)
Matt Spinler882ce952017-10-05 16:12:41 -0500182{
183 phosphor::logging::report<T>();
184
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500185 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
186 SYSTEMD_INTERFACE, "StartUnit");
Matt Spinler882ce952017-10-05 16:12:41 -0500187
188 method.append(POWEROFF_TARGET);
189 method.append("replace");
190
191 bus.call_noreply(method);
192}
Matt Spinler48b4a432017-08-04 11:57:37 -0500193
Lei YU7dc31bb2019-08-30 10:07:08 +0800194/**
195 * Load json from a file
196 *
197 * @param[in] path - The path of the json file
198 *
199 * @return The nlohmann::json object
200 */
201nlohmann::json loadJSONFromFile(const char* path);
202
Lei YU40705462019-10-09 17:07:11 +0800203/**
204 * Get PmBus access type from the json config
205 *
206 * @param[in] json - The json object
207 *
208 * @return The pmbus access type
209 */
210phosphor::pmbus::Type getPMBusAccessType(const nlohmann::json& json);
211
Lei YUcfc040c2019-10-29 17:10:26 +0800212/**
213 * Check if power is on
214 *
Lei YUe8c9cd62019-11-04 14:24:41 +0800215 * @param[in] bus - D-Bus object
216 * @param[in] defaultState - The default state if the function fails to get
217 * the power state.
218 *
219 * @return true if power is on, otherwise false;
220 * defaultState if it fails to get the power state.
Lei YUcfc040c2019-10-29 17:10:26 +0800221 */
Patrick Williams7354ce62022-07-22 19:26:56 -0500222bool isPoweredOn(sdbusplus::bus_t& bus, bool defaultState = false);
Lei YUe8c9cd62019-11-04 14:24:41 +0800223
224/**
225 * Get all PSU inventory paths from D-Bus
226 *
227 * @param[in] bus - D-Bus object
228 *
229 * @return The list of PSU inventory paths
230 */
Patrick Williams7354ce62022-07-22 19:26:56 -0500231std::vector<std::string> getPSUInventoryPaths(sdbusplus::bus_t& bus);
Lei YUcfc040c2019-10-29 17:10:26 +0800232
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500233} // namespace util
234} // namespace power
Lei YUab093322019-10-09 16:43:22 +0800235} // namespace phosphor