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> |
| 11 | |
Lei YU | ab09332 | 2019-10-09 16:43:22 +0800 | [diff] [blame] | 12 | namespace phosphor |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 13 | { |
| 14 | namespace power |
| 15 | { |
| 16 | namespace util |
| 17 | { |
| 18 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 19 | constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; |
| 20 | constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1"; |
Matt Spinler | 882ce95 | 2017-10-05 16:12:41 -0500 | [diff] [blame] | 21 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 22 | constexpr auto POWEROFF_TARGET = "obmc-chassis-hard-poweroff@0.target"; |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 23 | constexpr auto PROPERTY_INTF = "org.freedesktop.DBus.Properties"; |
| 24 | |
Brandon Wyman | c761b5f | 2020-11-05 18:30:41 -0600 | [diff] [blame] | 25 | using DbusPath = std::string; |
Adriana Kobylak | 4e8b335 | 2021-03-16 20:38:50 +0000 | [diff] [blame] | 26 | using DbusProperty = std::string; |
Brandon Wyman | c761b5f | 2020-11-05 18:30:41 -0600 | [diff] [blame] | 27 | using DbusService = std::string; |
| 28 | using DbusInterface = std::string; |
| 29 | using DbusInterfaceList = std::vector<DbusInterface>; |
| 30 | using DbusSubtree = |
| 31 | std::map<DbusPath, std::map<DbusService, DbusInterfaceList>>; |
Adriana Kobylak | 886574c | 2021-11-01 18:22:28 +0000 | [diff] [blame] | 32 | using DbusVariant = |
| 33 | std::variant<bool, uint64_t, std::string, std::vector<uint64_t>>; |
Adriana Kobylak | 4e8b335 | 2021-03-16 20:38:50 +0000 | [diff] [blame] | 34 | using DbusPropertyMap = std::map<DbusProperty, DbusVariant>; |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 35 | /** |
| 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 Barth | d262440 | 2020-02-03 15:35:12 -0600 | [diff] [blame] | 42 | * @param[in] logError - log error when no service found |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 43 | * |
| 44 | * @return The service name |
| 45 | */ |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 46 | std::string getService(const std::string& path, const std::string& interface, |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 47 | sdbusplus::bus_t& bus, bool logError = true); |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 48 | |
| 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 Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 59 | template <typename T> |
| 60 | void getProperty(const std::string& interface, const std::string& propertyName, |
| 61 | const std::string& path, const std::string& service, |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 62 | sdbusplus::bus_t& bus, T& value) |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 63 | { |
Patrick Williams | abe4941 | 2020-05-13 17:59:47 -0500 | [diff] [blame] | 64 | std::variant<T> property; |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 65 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 66 | auto method = bus.new_method_call(service.c_str(), path.c_str(), |
| 67 | PROPERTY_INTF, "Get"); |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 68 | |
| 69 | method.append(interface, propertyName); |
| 70 | |
| 71 | auto reply = bus.call(method); |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 72 | |
| 73 | reply.read(property); |
Patrick Williams | 365d61c | 2020-05-13 12:23:08 -0500 | [diff] [blame] | 74 | value = std::get<T>(property); |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 75 | } |
| 76 | |
Matt Spinler | 48b4a43 | 2017-08-04 11:57:37 -0500 | [diff] [blame] | 77 | /** |
Brandon Wyman | 0a4f519 | 2017-12-06 20:19:08 -0600 | [diff] [blame] | 78 | * @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 Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 87 | template <typename T> |
| 88 | void setProperty(const std::string& interface, const std::string& propertyName, |
| 89 | const std::string& path, const std::string& service, |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 90 | sdbusplus::bus_t& bus, T& value) |
Brandon Wyman | 0a4f519 | 2017-12-06 20:19:08 -0600 | [diff] [blame] | 91 | { |
Patrick Williams | abe4941 | 2020-05-13 17:59:47 -0500 | [diff] [blame] | 92 | std::variant<T> propertyValue(value); |
Brandon Wyman | 0a4f519 | 2017-12-06 20:19:08 -0600 | [diff] [blame] | 93 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 94 | auto method = bus.new_method_call(service.c_str(), path.c_str(), |
| 95 | PROPERTY_INTF, "Set"); |
Brandon Wyman | 0a4f519 | 2017-12-06 20:19:08 -0600 | [diff] [blame] | 96 | |
| 97 | method.append(interface, propertyName, propertyValue); |
| 98 | |
| 99 | auto reply = bus.call(method); |
Brandon Wyman | 0a4f519 | 2017-12-06 20:19:08 -0600 | [diff] [blame] | 100 | } |
| 101 | |
Adriana Kobylak | 4e8b335 | 2021-03-16 20:38:50 +0000 | [diff] [blame] | 102 | /** |
| 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 Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 112 | DbusPropertyMap getAllProperties(sdbusplus::bus_t& bus, const std::string& path, |
Adriana Kobylak | 4e8b335 | 2021-03-16 20:38:50 +0000 | [diff] [blame] | 113 | const std::string& interface, |
| 114 | const std::string& service = std::string()); |
| 115 | |
Brandon Wyman | c761b5f | 2020-11-05 18:30:41 -0600 | [diff] [blame] | 116 | /** @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 Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 130 | DbusSubtree getSubTree(sdbusplus::bus_t& bus, const std::string& path, |
Brandon Wyman | c761b5f | 2020-11-05 18:30:41 -0600 | [diff] [blame] | 131 | const std::string& interface, int32_t depth); |
| 132 | |
Brandon Wyman | 0a4f519 | 2017-12-06 20:19:08 -0600 | [diff] [blame] | 133 | /** |
Matt Spinler | 882ce95 | 2017-10-05 16:12:41 -0500 | [diff] [blame] | 134 | * Logs an error and powers off the system. |
Matt Spinler | 48b4a43 | 2017-08-04 11:57:37 -0500 | [diff] [blame] | 135 | * |
Matt Spinler | 882ce95 | 2017-10-05 16:12:41 -0500 | [diff] [blame] | 136 | * @tparam T - error that will be logged before the power off |
Matt Spinler | 48b4a43 | 2017-08-04 11:57:37 -0500 | [diff] [blame] | 137 | * @param[in] bus - D-Bus object |
| 138 | */ |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 139 | template <typename T> |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 140 | void powerOff(sdbusplus::bus_t& bus) |
Matt Spinler | 882ce95 | 2017-10-05 16:12:41 -0500 | [diff] [blame] | 141 | { |
| 142 | phosphor::logging::report<T>(); |
| 143 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 144 | auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT, |
| 145 | SYSTEMD_INTERFACE, "StartUnit"); |
Matt Spinler | 882ce95 | 2017-10-05 16:12:41 -0500 | [diff] [blame] | 146 | |
| 147 | method.append(POWEROFF_TARGET); |
| 148 | method.append("replace"); |
| 149 | |
| 150 | bus.call_noreply(method); |
| 151 | } |
Matt Spinler | 48b4a43 | 2017-08-04 11:57:37 -0500 | [diff] [blame] | 152 | |
Lei YU | 7dc31bb | 2019-08-30 10:07:08 +0800 | [diff] [blame] | 153 | /** |
| 154 | * Load json from a file |
| 155 | * |
| 156 | * @param[in] path - The path of the json file |
| 157 | * |
| 158 | * @return The nlohmann::json object |
| 159 | */ |
| 160 | nlohmann::json loadJSONFromFile(const char* path); |
| 161 | |
Lei YU | 4070546 | 2019-10-09 17:07:11 +0800 | [diff] [blame] | 162 | /** |
| 163 | * Get PmBus access type from the json config |
| 164 | * |
| 165 | * @param[in] json - The json object |
| 166 | * |
| 167 | * @return The pmbus access type |
| 168 | */ |
| 169 | phosphor::pmbus::Type getPMBusAccessType(const nlohmann::json& json); |
| 170 | |
Lei YU | cfc040c | 2019-10-29 17:10:26 +0800 | [diff] [blame] | 171 | /** |
| 172 | * Check if power is on |
| 173 | * |
Lei YU | e8c9cd6 | 2019-11-04 14:24:41 +0800 | [diff] [blame] | 174 | * @param[in] bus - D-Bus object |
| 175 | * @param[in] defaultState - The default state if the function fails to get |
| 176 | * the power state. |
| 177 | * |
| 178 | * @return true if power is on, otherwise false; |
| 179 | * defaultState if it fails to get the power state. |
Lei YU | cfc040c | 2019-10-29 17:10:26 +0800 | [diff] [blame] | 180 | */ |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 181 | bool isPoweredOn(sdbusplus::bus_t& bus, bool defaultState = false); |
Lei YU | e8c9cd6 | 2019-11-04 14:24:41 +0800 | [diff] [blame] | 182 | |
| 183 | /** |
| 184 | * Get all PSU inventory paths from D-Bus |
| 185 | * |
| 186 | * @param[in] bus - D-Bus object |
| 187 | * |
| 188 | * @return The list of PSU inventory paths |
| 189 | */ |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 190 | std::vector<std::string> getPSUInventoryPaths(sdbusplus::bus_t& bus); |
Lei YU | cfc040c | 2019-10-29 17:10:26 +0800 | [diff] [blame] | 191 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 192 | } // namespace util |
| 193 | } // namespace power |
Lei YU | ab09332 | 2019-10-09 16:43:22 +0800 | [diff] [blame] | 194 | } // namespace phosphor |