blob: 043c3c0c9beff728dbdcf34b6470b48d365fdc22 [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>
11
Lei YUab093322019-10-09 16:43:22 +080012namespace phosphor
Matt Spinler974c9162017-08-04 08:36:37 -050013{
14namespace power
15{
16namespace util
17{
18
Matt Spinlerf0f02b92018-10-25 16:12:43 -050019constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
20constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
Matt Spinler882ce952017-10-05 16:12:41 -050021constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
Matt Spinlerf0f02b92018-10-25 16:12:43 -050022constexpr auto POWEROFF_TARGET = "obmc-chassis-hard-poweroff@0.target";
Matt Spinler974c9162017-08-04 08:36:37 -050023constexpr auto PROPERTY_INTF = "org.freedesktop.DBus.Properties";
24
Brandon Wymanc761b5f2020-11-05 18:30:41 -060025using DbusPath = std::string;
Adriana Kobylak4e8b3352021-03-16 20:38:50 +000026using DbusProperty = std::string;
Brandon Wymanc761b5f2020-11-05 18:30:41 -060027using DbusService = std::string;
28using DbusInterface = std::string;
29using DbusInterfaceList = std::vector<DbusInterface>;
30using DbusSubtree =
31 std::map<DbusPath, std::map<DbusService, DbusInterfaceList>>;
Adriana Kobylak886574c2021-11-01 18:22:28 +000032using DbusVariant =
33 std::variant<bool, uint64_t, std::string, std::vector<uint64_t>>;
Adriana Kobylak4e8b3352021-03-16 20:38:50 +000034using DbusPropertyMap = std::map<DbusProperty, DbusVariant>;
Matt Spinler974c9162017-08-04 08:36:37 -050035/**
36 * @brief Get the service name from the mapper for the
37 * interface and path passed in.
38 *
39 * @param[in] path - the D-Bus path name
40 * @param[in] interface - the D-Bus interface name
41 * @param[in] bus - the D-Bus object
Matthew Barthd2624402020-02-03 15:35:12 -060042 * @param[in] logError - log error when no service found
Matt Spinler974c9162017-08-04 08:36:37 -050043 *
44 * @return The service name
45 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050046std::string getService(const std::string& path, const std::string& interface,
Patrick Williams7354ce62022-07-22 19:26:56 -050047 sdbusplus::bus_t& bus, bool logError = true);
Matt Spinler974c9162017-08-04 08:36:37 -050048
49/**
50 * @brief Read a D-Bus property
51 *
52 * @param[in] interface - the interface the property is on
53 * @param[in] propertName - the name of the property
54 * @param[in] path - the D-Bus path
55 * @param[in] service - the D-Bus service
56 * @param[in] bus - the D-Bus object
57 * @param[out] value - filled in with the property value
58 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050059template <typename T>
60void getProperty(const std::string& interface, const std::string& propertyName,
61 const std::string& path, const std::string& service,
Patrick Williams7354ce62022-07-22 19:26:56 -050062 sdbusplus::bus_t& bus, T& value)
Matt Spinler974c9162017-08-04 08:36:37 -050063{
Patrick Williamsabe49412020-05-13 17:59:47 -050064 std::variant<T> property;
Matt Spinler974c9162017-08-04 08:36:37 -050065
Matt Spinlerf0f02b92018-10-25 16:12:43 -050066 auto method = bus.new_method_call(service.c_str(), path.c_str(),
67 PROPERTY_INTF, "Get");
Matt Spinler974c9162017-08-04 08:36:37 -050068
69 method.append(interface, propertyName);
70
71 auto reply = bus.call(method);
Matt Spinler974c9162017-08-04 08:36:37 -050072
73 reply.read(property);
Patrick Williams365d61c2020-05-13 12:23:08 -050074 value = std::get<T>(property);
Matt Spinler974c9162017-08-04 08:36:37 -050075}
76
Matt Spinler48b4a432017-08-04 11:57:37 -050077/**
Brandon Wyman0a4f5192017-12-06 20:19:08 -060078 * @brief Write a D-Bus property
79 *
80 * @param[in] interface - the interface the property is on
81 * @param[in] propertName - the name of the property
82 * @param[in] path - the D-Bus path
83 * @param[in] service - the D-Bus service
84 * @param[in] bus - the D-Bus object
85 * @param[in] value - the value to set the property to
86 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050087template <typename T>
88void setProperty(const std::string& interface, const std::string& propertyName,
89 const std::string& path, const std::string& service,
Patrick Williams7354ce62022-07-22 19:26:56 -050090 sdbusplus::bus_t& bus, T& value)
Brandon Wyman0a4f5192017-12-06 20:19:08 -060091{
Patrick Williamsabe49412020-05-13 17:59:47 -050092 std::variant<T> propertyValue(value);
Brandon Wyman0a4f5192017-12-06 20:19:08 -060093
Matt Spinlerf0f02b92018-10-25 16:12:43 -050094 auto method = bus.new_method_call(service.c_str(), path.c_str(),
95 PROPERTY_INTF, "Set");
Brandon Wyman0a4f5192017-12-06 20:19:08 -060096
97 method.append(interface, propertyName, propertyValue);
98
99 auto reply = bus.call(method);
Brandon Wyman0a4f5192017-12-06 20:19:08 -0600100}
101
Adriana Kobylak4e8b3352021-03-16 20:38:50 +0000102/**
103 * @brief Get all D-Bus properties
104 *
105 * @param[in] bus - the D-Bus object
106 * @param[in] path - the D-Bus object path
107 * @param[in] interface - the D-Bus interface name
108 * @param[in] service - the D-Bus service name (optional)
109 *
110 * @return DbusPropertyMap - Map of property names and values
111 */
Patrick Williams7354ce62022-07-22 19:26:56 -0500112DbusPropertyMap getAllProperties(sdbusplus::bus_t& bus, const std::string& path,
Adriana Kobylak4e8b3352021-03-16 20:38:50 +0000113 const std::string& interface,
114 const std::string& service = std::string());
115
Brandon Wymanc761b5f2020-11-05 18:30:41 -0600116/** @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 */
Patrick Williams7354ce62022-07-22 19:26:56 -0500130DbusSubtree getSubTree(sdbusplus::bus_t& bus, const std::string& path,
Brandon Wymanc761b5f2020-11-05 18:30:41 -0600131 const std::string& interface, int32_t depth);
132
Matt Spinler06594222023-05-01 10:44:43 -0500133/** @brief GetAssociatedSubTreePaths wrapper from the object mapper.
134 *
135 * Helper function to find object paths that implement a certain
136 * interface and are also an association endpoint.
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] associationPath - The association it must be an endpoint of.
142 * @param[in] path - The root of the tree to search.
143 * @param[in] interfaces - The interfaces in the subtree to search for
144 * @param[in] depth - The number of path elements to descend.
145 *
146 * @return std::vector<DbusPath> - The object paths.
147 */
148std::vector<DbusPath> getAssociatedSubTreePaths(
149 sdbusplus::bus_t& bus,
150 const sdbusplus::message::object_path& associationPath,
151 const sdbusplus::message::object_path& path,
152 const std::vector<std::string>& interfaces, int32_t depth);
153
Brandon Wyman0a4f5192017-12-06 20:19:08 -0600154/**
Matt Spinler882ce952017-10-05 16:12:41 -0500155 * Logs an error and powers off the system.
Matt Spinler48b4a432017-08-04 11:57:37 -0500156 *
Matt Spinler882ce952017-10-05 16:12:41 -0500157 * @tparam T - error that will be logged before the power off
Matt Spinler48b4a432017-08-04 11:57:37 -0500158 * @param[in] bus - D-Bus object
159 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500160template <typename T>
Patrick Williams7354ce62022-07-22 19:26:56 -0500161void powerOff(sdbusplus::bus_t& bus)
Matt Spinler882ce952017-10-05 16:12:41 -0500162{
163 phosphor::logging::report<T>();
164
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500165 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
166 SYSTEMD_INTERFACE, "StartUnit");
Matt Spinler882ce952017-10-05 16:12:41 -0500167
168 method.append(POWEROFF_TARGET);
169 method.append("replace");
170
171 bus.call_noreply(method);
172}
Matt Spinler48b4a432017-08-04 11:57:37 -0500173
Lei YU7dc31bb2019-08-30 10:07:08 +0800174/**
175 * Load json from a file
176 *
177 * @param[in] path - The path of the json file
178 *
179 * @return The nlohmann::json object
180 */
181nlohmann::json loadJSONFromFile(const char* path);
182
Lei YU40705462019-10-09 17:07:11 +0800183/**
184 * Get PmBus access type from the json config
185 *
186 * @param[in] json - The json object
187 *
188 * @return The pmbus access type
189 */
190phosphor::pmbus::Type getPMBusAccessType(const nlohmann::json& json);
191
Lei YUcfc040c2019-10-29 17:10:26 +0800192/**
193 * Check if power is on
194 *
Lei YUe8c9cd62019-11-04 14:24:41 +0800195 * @param[in] bus - D-Bus object
196 * @param[in] defaultState - The default state if the function fails to get
197 * the power state.
198 *
199 * @return true if power is on, otherwise false;
200 * defaultState if it fails to get the power state.
Lei YUcfc040c2019-10-29 17:10:26 +0800201 */
Patrick Williams7354ce62022-07-22 19:26:56 -0500202bool isPoweredOn(sdbusplus::bus_t& bus, bool defaultState = false);
Lei YUe8c9cd62019-11-04 14:24:41 +0800203
204/**
205 * Get all PSU inventory paths from D-Bus
206 *
207 * @param[in] bus - D-Bus object
208 *
209 * @return The list of PSU inventory paths
210 */
Patrick Williams7354ce62022-07-22 19:26:56 -0500211std::vector<std::string> getPSUInventoryPaths(sdbusplus::bus_t& bus);
Lei YUcfc040c2019-10-29 17:10:26 +0800212
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500213} // namespace util
214} // namespace power
Lei YUab093322019-10-09 16:43:22 +0800215} // namespace phosphor