blob: 019173b09e9d85dc2d2ac9f48779c6b664b23203 [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;
26using DbusService = std::string;
27using DbusInterface = std::string;
28using DbusInterfaceList = std::vector<DbusInterface>;
29using DbusSubtree =
30 std::map<DbusPath, std::map<DbusService, DbusInterfaceList>>;
Matt Spinler974c9162017-08-04 08:36:37 -050031/**
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
Matthew Barthd2624402020-02-03 15:35:12 -060038 * @param[in] logError - log error when no service found
Matt Spinler974c9162017-08-04 08:36:37 -050039 *
40 * @return The service name
41 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050042std::string getService(const std::string& path, const std::string& interface,
Matthew Barthd2624402020-02-03 15:35:12 -060043 sdbusplus::bus::bus& bus, bool logError = true);
Matt Spinler974c9162017-08-04 08:36:37 -050044
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 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050055template <typename T>
56void 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)
Matt Spinler974c9162017-08-04 08:36:37 -050059{
Patrick Williamsabe49412020-05-13 17:59:47 -050060 std::variant<T> property;
Matt Spinler974c9162017-08-04 08:36:37 -050061
Matt Spinlerf0f02b92018-10-25 16:12:43 -050062 auto method = bus.new_method_call(service.c_str(), path.c_str(),
63 PROPERTY_INTF, "Get");
Matt Spinler974c9162017-08-04 08:36:37 -050064
65 method.append(interface, propertyName);
66
67 auto reply = bus.call(method);
Matt Spinler974c9162017-08-04 08:36:37 -050068
69 reply.read(property);
Patrick Williams365d61c2020-05-13 12:23:08 -050070 value = std::get<T>(property);
Matt Spinler974c9162017-08-04 08:36:37 -050071}
72
Matt Spinler48b4a432017-08-04 11:57:37 -050073/**
Brandon Wyman0a4f5192017-12-06 20:19:08 -060074 * @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 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050083template <typename T>
84void 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)
Brandon Wyman0a4f5192017-12-06 20:19:08 -060087{
Patrick Williamsabe49412020-05-13 17:59:47 -050088 std::variant<T> propertyValue(value);
Brandon Wyman0a4f5192017-12-06 20:19:08 -060089
Matt Spinlerf0f02b92018-10-25 16:12:43 -050090 auto method = bus.new_method_call(service.c_str(), path.c_str(),
91 PROPERTY_INTF, "Set");
Brandon Wyman0a4f5192017-12-06 20:19:08 -060092
93 method.append(interface, propertyName, propertyValue);
94
95 auto reply = bus.call(method);
Brandon Wyman0a4f5192017-12-06 20:19:08 -060096}
97
Brandon Wymanc761b5f2020-11-05 18:30:41 -060098/** @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 */
112DbusSubtree getSubTree(sdbusplus::bus::bus& bus, const std::string& path,
113 const std::string& interface, int32_t depth);
114
Brandon Wyman0a4f5192017-12-06 20:19:08 -0600115/**
Matt Spinler882ce952017-10-05 16:12:41 -0500116 * Logs an error and powers off the system.
Matt Spinler48b4a432017-08-04 11:57:37 -0500117 *
Matt Spinler882ce952017-10-05 16:12:41 -0500118 * @tparam T - error that will be logged before the power off
Matt Spinler48b4a432017-08-04 11:57:37 -0500119 * @param[in] bus - D-Bus object
120 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500121template <typename T>
Matt Spinler882ce952017-10-05 16:12:41 -0500122void powerOff(sdbusplus::bus::bus& bus)
123{
124 phosphor::logging::report<T>();
125
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500126 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
127 SYSTEMD_INTERFACE, "StartUnit");
Matt Spinler882ce952017-10-05 16:12:41 -0500128
129 method.append(POWEROFF_TARGET);
130 method.append("replace");
131
132 bus.call_noreply(method);
133}
Matt Spinler48b4a432017-08-04 11:57:37 -0500134
Lei YU7dc31bb2019-08-30 10:07:08 +0800135/**
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 */
142nlohmann::json loadJSONFromFile(const char* path);
143
Lei YU40705462019-10-09 17:07:11 +0800144/**
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 */
151phosphor::pmbus::Type getPMBusAccessType(const nlohmann::json& json);
152
Lei YUcfc040c2019-10-29 17:10:26 +0800153/**
154 * Check if power is on
155 *
Lei YUe8c9cd62019-11-04 14:24:41 +0800156 * @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.
Lei YUcfc040c2019-10-29 17:10:26 +0800162 */
Lei YUe8c9cd62019-11-04 14:24:41 +0800163bool 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 */
172std::vector<std::string> getPSUInventoryPaths(sdbusplus::bus::bus& bus);
Lei YUcfc040c2019-10-29 17:10:26 +0800173
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500174} // namespace util
175} // namespace power
Lei YUab093322019-10-09 16:43:22 +0800176} // namespace phosphor