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