blob: ad6be99de5e1d0091613c8d4de4ce2dab126fe31 [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 =
34 std::variant<bool, uint64_t, std::string, std::vector<uint64_t>>;
Adriana Kobylak4e8b3352021-03-16 20:38:50 +000035using DbusPropertyMap = std::map<DbusProperty, DbusVariant>;
Matt Spinler974c9162017-08-04 08:36:37 -050036/**
37 * @brief Get the service name from the mapper for the
38 * interface and path passed in.
39 *
40 * @param[in] path - the D-Bus path name
41 * @param[in] interface - the D-Bus interface name
42 * @param[in] bus - the D-Bus object
Matthew Barthd2624402020-02-03 15:35:12 -060043 * @param[in] logError - log error when no service found
Matt Spinler974c9162017-08-04 08:36:37 -050044 *
45 * @return The service name
46 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050047std::string getService(const std::string& path, const std::string& interface,
Patrick Williams7354ce62022-07-22 19:26:56 -050048 sdbusplus::bus_t& bus, bool logError = true);
Matt Spinler974c9162017-08-04 08:36:37 -050049
50/**
51 * @brief Read a D-Bus property
52 *
53 * @param[in] interface - the interface the property is on
54 * @param[in] propertName - the name of the property
55 * @param[in] path - the D-Bus path
56 * @param[in] service - the D-Bus service
57 * @param[in] bus - the D-Bus object
58 * @param[out] value - filled in with the property value
59 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050060template <typename T>
61void getProperty(const std::string& interface, const std::string& propertyName,
62 const std::string& path, const std::string& service,
Patrick Williams7354ce62022-07-22 19:26:56 -050063 sdbusplus::bus_t& bus, T& value)
Matt Spinler974c9162017-08-04 08:36:37 -050064{
Patrick Williamsabe49412020-05-13 17:59:47 -050065 std::variant<T> property;
Matt Spinler974c9162017-08-04 08:36:37 -050066
Matt Spinlerf0f02b92018-10-25 16:12:43 -050067 auto method = bus.new_method_call(service.c_str(), path.c_str(),
68 PROPERTY_INTF, "Get");
Matt Spinler974c9162017-08-04 08:36:37 -050069
70 method.append(interface, propertyName);
71
72 auto reply = bus.call(method);
Matt Spinler974c9162017-08-04 08:36:37 -050073
74 reply.read(property);
Patrick Williams365d61c2020-05-13 12:23:08 -050075 value = std::get<T>(property);
Matt Spinler974c9162017-08-04 08:36:37 -050076}
77
Matt Spinler48b4a432017-08-04 11:57:37 -050078/**
Brandon Wyman0a4f5192017-12-06 20:19:08 -060079 * @brief Write a D-Bus property
80 *
81 * @param[in] interface - the interface the property is on
82 * @param[in] propertName - the name of the property
83 * @param[in] path - the D-Bus path
84 * @param[in] service - the D-Bus service
85 * @param[in] bus - the D-Bus object
86 * @param[in] value - the value to set the property to
87 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050088template <typename T>
89void setProperty(const std::string& interface, const std::string& propertyName,
90 const std::string& path, const std::string& service,
Patrick Williams7354ce62022-07-22 19:26:56 -050091 sdbusplus::bus_t& bus, T& value)
Brandon Wyman0a4f5192017-12-06 20:19:08 -060092{
Patrick Williamsabe49412020-05-13 17:59:47 -050093 std::variant<T> propertyValue(value);
Brandon Wyman0a4f5192017-12-06 20:19:08 -060094
Matt Spinlerf0f02b92018-10-25 16:12:43 -050095 auto method = bus.new_method_call(service.c_str(), path.c_str(),
96 PROPERTY_INTF, "Set");
Brandon Wyman0a4f5192017-12-06 20:19:08 -060097
98 method.append(interface, propertyName, propertyValue);
99
100 auto reply = bus.call(method);
Brandon Wyman0a4f5192017-12-06 20:19:08 -0600101}
102
Adriana Kobylak4e8b3352021-03-16 20:38:50 +0000103/**
104 * @brief Get all D-Bus properties
105 *
106 * @param[in] bus - the D-Bus object
107 * @param[in] path - the D-Bus object path
108 * @param[in] interface - the D-Bus interface name
109 * @param[in] service - the D-Bus service name (optional)
110 *
111 * @return DbusPropertyMap - Map of property names and values
112 */
Patrick Williams7354ce62022-07-22 19:26:56 -0500113DbusPropertyMap getAllProperties(sdbusplus::bus_t& bus, const std::string& path,
Adriana Kobylak4e8b3352021-03-16 20:38:50 +0000114 const std::string& interface,
115 const std::string& service = std::string());
116
Brandon Wymanc761b5f2020-11-05 18:30:41 -0600117/** @brief Get subtree from the object mapper.
118 *
119 * Helper function to find objects, services, and interfaces.
120 * See:
121 * https://github.com/openbmc/docs/blob/master/architecture/object-mapper.md
122 *
123 * @param[in] bus - The D-Bus object.
124 * @param[in] path - The root of the tree to search.
125 * @param[in] interface - Interface in the subtree to search for
126 * @param[in] depth - The number of path elements to descend.
127 *
128 * @return DbusSubtree - Map of object paths to a map of service names to their
129 * interfaces.
130 */
Patrick Williams7354ce62022-07-22 19:26:56 -0500131DbusSubtree getSubTree(sdbusplus::bus_t& bus, const std::string& path,
Brandon Wymanc761b5f2020-11-05 18:30:41 -0600132 const std::string& interface, int32_t depth);
133
Shawn McCarney8270b7d2023-06-26 11:35:51 -0500134/** @brief Get subtree from the object mapper.
135 *
136 * Helper function to find objects, services, and interfaces.
137 * See:
138 * https://github.com/openbmc/docs/blob/master/architecture/object-mapper.md
139 *
140 * @param[in] bus - The D-Bus object.
141 * @param[in] path - The root of the tree to search.
142 * @param[in] interfaces - Interfaces in the subtree to search for.
143 * @param[in] depth - The number of path elements to descend.
144 *
145 * @return DbusSubtree - Map of object paths to a map of service names to their
146 * interfaces.
147 */
148DbusSubtree getSubTree(sdbusplus::bus_t& bus, const std::string& path,
149 const std::vector<std::string>& interfaces,
150 int32_t depth);
151
Matt Spinler06594222023-05-01 10:44:43 -0500152/** @brief GetAssociatedSubTreePaths wrapper from the object mapper.
153 *
154 * Helper function to find object paths that implement a certain
155 * interface and are also an association endpoint.
156 * See:
157 * https://github.com/openbmc/docs/blob/master/architecture/object-mapper.md
158 *
159 * @param[in] bus - The D-Bus object.
160 * @param[in] associationPath - The association it must be an endpoint of.
161 * @param[in] path - The root of the tree to search.
162 * @param[in] interfaces - The interfaces in the subtree to search for
163 * @param[in] depth - The number of path elements to descend.
164 *
165 * @return std::vector<DbusPath> - The object paths.
166 */
167std::vector<DbusPath> getAssociatedSubTreePaths(
168 sdbusplus::bus_t& bus,
169 const sdbusplus::message::object_path& associationPath,
170 const sdbusplus::message::object_path& path,
171 const std::vector<std::string>& interfaces, int32_t depth);
172
Brandon Wyman0a4f5192017-12-06 20:19:08 -0600173/**
Matt Spinler882ce952017-10-05 16:12:41 -0500174 * Logs an error and powers off the system.
Matt Spinler48b4a432017-08-04 11:57:37 -0500175 *
Matt Spinler882ce952017-10-05 16:12:41 -0500176 * @tparam T - error that will be logged before the power off
Matt Spinler48b4a432017-08-04 11:57:37 -0500177 * @param[in] bus - D-Bus object
178 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500179template <typename T>
Patrick Williams7354ce62022-07-22 19:26:56 -0500180void powerOff(sdbusplus::bus_t& bus)
Matt Spinler882ce952017-10-05 16:12:41 -0500181{
182 phosphor::logging::report<T>();
183
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500184 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
185 SYSTEMD_INTERFACE, "StartUnit");
Matt Spinler882ce952017-10-05 16:12:41 -0500186
187 method.append(POWEROFF_TARGET);
188 method.append("replace");
189
190 bus.call_noreply(method);
191}
Matt Spinler48b4a432017-08-04 11:57:37 -0500192
Lei YU7dc31bb2019-08-30 10:07:08 +0800193/**
194 * Load json from a file
195 *
196 * @param[in] path - The path of the json file
197 *
198 * @return The nlohmann::json object
199 */
200nlohmann::json loadJSONFromFile(const char* path);
201
Lei YU40705462019-10-09 17:07:11 +0800202/**
203 * Get PmBus access type from the json config
204 *
205 * @param[in] json - The json object
206 *
207 * @return The pmbus access type
208 */
209phosphor::pmbus::Type getPMBusAccessType(const nlohmann::json& json);
210
Lei YUcfc040c2019-10-29 17:10:26 +0800211/**
212 * Check if power is on
213 *
Lei YUe8c9cd62019-11-04 14:24:41 +0800214 * @param[in] bus - D-Bus object
215 * @param[in] defaultState - The default state if the function fails to get
216 * the power state.
217 *
218 * @return true if power is on, otherwise false;
219 * defaultState if it fails to get the power state.
Lei YUcfc040c2019-10-29 17:10:26 +0800220 */
Patrick Williams7354ce62022-07-22 19:26:56 -0500221bool isPoweredOn(sdbusplus::bus_t& bus, bool defaultState = false);
Lei YUe8c9cd62019-11-04 14:24:41 +0800222
223/**
224 * Get all PSU inventory paths from D-Bus
225 *
226 * @param[in] bus - D-Bus object
227 *
228 * @return The list of PSU inventory paths
229 */
Patrick Williams7354ce62022-07-22 19:26:56 -0500230std::vector<std::string> getPSUInventoryPaths(sdbusplus::bus_t& bus);
Lei YUcfc040c2019-10-29 17:10:26 +0800231
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500232} // namespace util
233} // namespace power
Lei YUab093322019-10-09 16:43:22 +0800234} // namespace phosphor