Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Lei YU | 4070546 | 2019-10-09 17:07:11 +0800 | [diff] [blame] | 3 | #include "pmbus.hpp" |
| 4 | |
Lei YU | 7dc31bb | 2019-08-30 10:07:08 +0800 | [diff] [blame] | 5 | #include <nlohmann/json.hpp> |
Matt Spinler | 882ce95 | 2017-10-05 16:12:41 -0500 | [diff] [blame] | 6 | #include <phosphor-logging/elog.hpp> |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 7 | #include <phosphor-logging/log.hpp> |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 8 | #include <sdbusplus/bus.hpp> |
Brandon Wyman | d1bc4ce | 2019-12-13 14:20:34 -0600 | [diff] [blame] | 9 | |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 10 | #include <string> |
Shawn McCarney | 8270b7d | 2023-06-26 11:35:51 -0500 | [diff] [blame] | 11 | #include <vector> |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 12 | |
Lei YU | ab09332 | 2019-10-09 16:43:22 +0800 | [diff] [blame] | 13 | namespace phosphor |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 14 | { |
| 15 | namespace power |
| 16 | { |
| 17 | namespace util |
| 18 | { |
| 19 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 20 | constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; |
| 21 | constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1"; |
Matt Spinler | 882ce95 | 2017-10-05 16:12:41 -0500 | [diff] [blame] | 22 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 23 | constexpr auto POWEROFF_TARGET = "obmc-chassis-hard-poweroff@0.target"; |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 24 | constexpr auto PROPERTY_INTF = "org.freedesktop.DBus.Properties"; |
| 25 | |
Brandon Wyman | c761b5f | 2020-11-05 18:30:41 -0600 | [diff] [blame] | 26 | using DbusPath = std::string; |
Adriana Kobylak | 4e8b335 | 2021-03-16 20:38:50 +0000 | [diff] [blame] | 27 | using DbusProperty = std::string; |
Brandon Wyman | c761b5f | 2020-11-05 18:30:41 -0600 | [diff] [blame] | 28 | using DbusService = std::string; |
| 29 | using DbusInterface = std::string; |
| 30 | using DbusInterfaceList = std::vector<DbusInterface>; |
| 31 | using DbusSubtree = |
| 32 | std::map<DbusPath, std::map<DbusService, DbusInterfaceList>>; |
Adriana Kobylak | 886574c | 2021-11-01 18:22:28 +0000 | [diff] [blame] | 33 | using DbusVariant = |
| 34 | std::variant<bool, uint64_t, std::string, std::vector<uint64_t>>; |
Adriana Kobylak | 4e8b335 | 2021-03-16 20:38:50 +0000 | [diff] [blame] | 35 | using DbusPropertyMap = std::map<DbusProperty, DbusVariant>; |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 36 | /** |
| 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 Barth | d262440 | 2020-02-03 15:35:12 -0600 | [diff] [blame] | 43 | * @param[in] logError - log error when no service found |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 44 | * |
| 45 | * @return The service name |
| 46 | */ |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 47 | std::string getService(const std::string& path, const std::string& interface, |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 48 | sdbusplus::bus_t& bus, bool logError = true); |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 49 | |
| 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 Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 60 | template <typename T> |
| 61 | void getProperty(const std::string& interface, const std::string& propertyName, |
| 62 | const std::string& path, const std::string& service, |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 63 | sdbusplus::bus_t& bus, T& value) |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 64 | { |
Patrick Williams | abe4941 | 2020-05-13 17:59:47 -0500 | [diff] [blame] | 65 | std::variant<T> property; |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 66 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 67 | auto method = bus.new_method_call(service.c_str(), path.c_str(), |
| 68 | PROPERTY_INTF, "Get"); |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 69 | |
| 70 | method.append(interface, propertyName); |
| 71 | |
| 72 | auto reply = bus.call(method); |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 73 | |
| 74 | reply.read(property); |
Patrick Williams | 365d61c | 2020-05-13 12:23:08 -0500 | [diff] [blame] | 75 | value = std::get<T>(property); |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 76 | } |
| 77 | |
Matt Spinler | 48b4a43 | 2017-08-04 11:57:37 -0500 | [diff] [blame] | 78 | /** |
Brandon Wyman | 0a4f519 | 2017-12-06 20:19:08 -0600 | [diff] [blame] | 79 | * @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 Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 88 | template <typename T> |
| 89 | void setProperty(const std::string& interface, const std::string& propertyName, |
| 90 | const std::string& path, const std::string& service, |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 91 | sdbusplus::bus_t& bus, T& value) |
Brandon Wyman | 0a4f519 | 2017-12-06 20:19:08 -0600 | [diff] [blame] | 92 | { |
Patrick Williams | abe4941 | 2020-05-13 17:59:47 -0500 | [diff] [blame] | 93 | std::variant<T> propertyValue(value); |
Brandon Wyman | 0a4f519 | 2017-12-06 20:19:08 -0600 | [diff] [blame] | 94 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 95 | auto method = bus.new_method_call(service.c_str(), path.c_str(), |
| 96 | PROPERTY_INTF, "Set"); |
Brandon Wyman | 0a4f519 | 2017-12-06 20:19:08 -0600 | [diff] [blame] | 97 | |
| 98 | method.append(interface, propertyName, propertyValue); |
| 99 | |
| 100 | auto reply = bus.call(method); |
Brandon Wyman | 0a4f519 | 2017-12-06 20:19:08 -0600 | [diff] [blame] | 101 | } |
| 102 | |
Adriana Kobylak | 4e8b335 | 2021-03-16 20:38:50 +0000 | [diff] [blame] | 103 | /** |
| 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 Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 113 | DbusPropertyMap getAllProperties(sdbusplus::bus_t& bus, const std::string& path, |
Adriana Kobylak | 4e8b335 | 2021-03-16 20:38:50 +0000 | [diff] [blame] | 114 | const std::string& interface, |
| 115 | const std::string& service = std::string()); |
| 116 | |
Brandon Wyman | c761b5f | 2020-11-05 18:30:41 -0600 | [diff] [blame] | 117 | /** @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 Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 131 | DbusSubtree getSubTree(sdbusplus::bus_t& bus, const std::string& path, |
Brandon Wyman | c761b5f | 2020-11-05 18:30:41 -0600 | [diff] [blame] | 132 | const std::string& interface, int32_t depth); |
| 133 | |
Shawn McCarney | 8270b7d | 2023-06-26 11:35:51 -0500 | [diff] [blame] | 134 | /** @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 | */ |
| 148 | DbusSubtree getSubTree(sdbusplus::bus_t& bus, const std::string& path, |
| 149 | const std::vector<std::string>& interfaces, |
| 150 | int32_t depth); |
| 151 | |
Matt Spinler | 0659422 | 2023-05-01 10:44:43 -0500 | [diff] [blame] | 152 | /** @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 | */ |
| 167 | std::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 Wyman | 0a4f519 | 2017-12-06 20:19:08 -0600 | [diff] [blame] | 173 | /** |
Matt Spinler | 882ce95 | 2017-10-05 16:12:41 -0500 | [diff] [blame] | 174 | * Logs an error and powers off the system. |
Matt Spinler | 48b4a43 | 2017-08-04 11:57:37 -0500 | [diff] [blame] | 175 | * |
Matt Spinler | 882ce95 | 2017-10-05 16:12:41 -0500 | [diff] [blame] | 176 | * @tparam T - error that will be logged before the power off |
Matt Spinler | 48b4a43 | 2017-08-04 11:57:37 -0500 | [diff] [blame] | 177 | * @param[in] bus - D-Bus object |
| 178 | */ |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 179 | template <typename T> |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 180 | void powerOff(sdbusplus::bus_t& bus) |
Matt Spinler | 882ce95 | 2017-10-05 16:12:41 -0500 | [diff] [blame] | 181 | { |
| 182 | phosphor::logging::report<T>(); |
| 183 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 184 | auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT, |
| 185 | SYSTEMD_INTERFACE, "StartUnit"); |
Matt Spinler | 882ce95 | 2017-10-05 16:12:41 -0500 | [diff] [blame] | 186 | |
| 187 | method.append(POWEROFF_TARGET); |
| 188 | method.append("replace"); |
| 189 | |
| 190 | bus.call_noreply(method); |
| 191 | } |
Matt Spinler | 48b4a43 | 2017-08-04 11:57:37 -0500 | [diff] [blame] | 192 | |
Lei YU | 7dc31bb | 2019-08-30 10:07:08 +0800 | [diff] [blame] | 193 | /** |
| 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 | */ |
| 200 | nlohmann::json loadJSONFromFile(const char* path); |
| 201 | |
Lei YU | 4070546 | 2019-10-09 17:07:11 +0800 | [diff] [blame] | 202 | /** |
| 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 | */ |
| 209 | phosphor::pmbus::Type getPMBusAccessType(const nlohmann::json& json); |
| 210 | |
Lei YU | cfc040c | 2019-10-29 17:10:26 +0800 | [diff] [blame] | 211 | /** |
| 212 | * Check if power is on |
| 213 | * |
Lei YU | e8c9cd6 | 2019-11-04 14:24:41 +0800 | [diff] [blame] | 214 | * @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 YU | cfc040c | 2019-10-29 17:10:26 +0800 | [diff] [blame] | 220 | */ |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 221 | bool isPoweredOn(sdbusplus::bus_t& bus, bool defaultState = false); |
Lei YU | e8c9cd6 | 2019-11-04 14:24:41 +0800 | [diff] [blame] | 222 | |
| 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 Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 230 | std::vector<std::string> getPSUInventoryPaths(sdbusplus::bus_t& bus); |
Lei YU | cfc040c | 2019-10-29 17:10:26 +0800 | [diff] [blame] | 231 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 232 | } // namespace util |
| 233 | } // namespace power |
Lei YU | ab09332 | 2019-10-09 16:43:22 +0800 | [diff] [blame] | 234 | } // namespace phosphor |