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; |
| 26 | using DbusService = std::string; |
| 27 | using DbusInterface = std::string; |
| 28 | using DbusInterfaceList = std::vector<DbusInterface>; |
| 29 | using DbusSubtree = |
| 30 | std::map<DbusPath, std::map<DbusService, DbusInterfaceList>>; |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 31 | /** |
| 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 Barth | d262440 | 2020-02-03 15:35:12 -0600 | [diff] [blame] | 38 | * @param[in] logError - log error when no service found |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 39 | * |
| 40 | * @return The service name |
| 41 | */ |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 42 | std::string getService(const std::string& path, const std::string& interface, |
Matthew Barth | d262440 | 2020-02-03 15:35:12 -0600 | [diff] [blame] | 43 | sdbusplus::bus::bus& bus, bool logError = true); |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 44 | |
| 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 Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 55 | template <typename T> |
| 56 | void 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 Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 59 | { |
Patrick Williams | abe4941 | 2020-05-13 17:59:47 -0500 | [diff] [blame] | 60 | std::variant<T> property; |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 61 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 62 | auto method = bus.new_method_call(service.c_str(), path.c_str(), |
| 63 | PROPERTY_INTF, "Get"); |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 64 | |
| 65 | method.append(interface, propertyName); |
| 66 | |
| 67 | auto reply = bus.call(method); |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 68 | |
| 69 | reply.read(property); |
Patrick Williams | 365d61c | 2020-05-13 12:23:08 -0500 | [diff] [blame] | 70 | value = std::get<T>(property); |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 71 | } |
| 72 | |
Matt Spinler | 48b4a43 | 2017-08-04 11:57:37 -0500 | [diff] [blame] | 73 | /** |
Brandon Wyman | 0a4f519 | 2017-12-06 20:19:08 -0600 | [diff] [blame] | 74 | * @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 Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 83 | template <typename T> |
| 84 | void 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 Wyman | 0a4f519 | 2017-12-06 20:19:08 -0600 | [diff] [blame] | 87 | { |
Patrick Williams | abe4941 | 2020-05-13 17:59:47 -0500 | [diff] [blame] | 88 | std::variant<T> propertyValue(value); |
Brandon Wyman | 0a4f519 | 2017-12-06 20:19:08 -0600 | [diff] [blame] | 89 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 90 | auto method = bus.new_method_call(service.c_str(), path.c_str(), |
| 91 | PROPERTY_INTF, "Set"); |
Brandon Wyman | 0a4f519 | 2017-12-06 20:19:08 -0600 | [diff] [blame] | 92 | |
| 93 | method.append(interface, propertyName, propertyValue); |
| 94 | |
| 95 | auto reply = bus.call(method); |
Brandon Wyman | 0a4f519 | 2017-12-06 20:19:08 -0600 | [diff] [blame] | 96 | } |
| 97 | |
Brandon Wyman | c761b5f | 2020-11-05 18:30:41 -0600 | [diff] [blame] | 98 | /** @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 | */ |
| 112 | DbusSubtree getSubTree(sdbusplus::bus::bus& bus, const std::string& path, |
| 113 | const std::string& interface, int32_t depth); |
| 114 | |
Brandon Wyman | 0a4f519 | 2017-12-06 20:19:08 -0600 | [diff] [blame] | 115 | /** |
Matt Spinler | 882ce95 | 2017-10-05 16:12:41 -0500 | [diff] [blame] | 116 | * Logs an error and powers off the system. |
Matt Spinler | 48b4a43 | 2017-08-04 11:57:37 -0500 | [diff] [blame] | 117 | * |
Matt Spinler | 882ce95 | 2017-10-05 16:12:41 -0500 | [diff] [blame] | 118 | * @tparam T - error that will be logged before the power off |
Matt Spinler | 48b4a43 | 2017-08-04 11:57:37 -0500 | [diff] [blame] | 119 | * @param[in] bus - D-Bus object |
| 120 | */ |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 121 | template <typename T> |
Matt Spinler | 882ce95 | 2017-10-05 16:12:41 -0500 | [diff] [blame] | 122 | void powerOff(sdbusplus::bus::bus& bus) |
| 123 | { |
| 124 | phosphor::logging::report<T>(); |
| 125 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 126 | auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT, |
| 127 | SYSTEMD_INTERFACE, "StartUnit"); |
Matt Spinler | 882ce95 | 2017-10-05 16:12:41 -0500 | [diff] [blame] | 128 | |
| 129 | method.append(POWEROFF_TARGET); |
| 130 | method.append("replace"); |
| 131 | |
| 132 | bus.call_noreply(method); |
| 133 | } |
Matt Spinler | 48b4a43 | 2017-08-04 11:57:37 -0500 | [diff] [blame] | 134 | |
Lei YU | 7dc31bb | 2019-08-30 10:07:08 +0800 | [diff] [blame] | 135 | /** |
| 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 | */ |
| 142 | nlohmann::json loadJSONFromFile(const char* path); |
| 143 | |
Lei YU | 4070546 | 2019-10-09 17:07:11 +0800 | [diff] [blame] | 144 | /** |
| 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 | */ |
| 151 | phosphor::pmbus::Type getPMBusAccessType(const nlohmann::json& json); |
| 152 | |
Lei YU | cfc040c | 2019-10-29 17:10:26 +0800 | [diff] [blame] | 153 | /** |
| 154 | * Check if power is on |
| 155 | * |
Lei YU | e8c9cd6 | 2019-11-04 14:24:41 +0800 | [diff] [blame] | 156 | * @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 YU | cfc040c | 2019-10-29 17:10:26 +0800 | [diff] [blame] | 162 | */ |
Lei YU | e8c9cd6 | 2019-11-04 14:24:41 +0800 | [diff] [blame] | 163 | bool 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 | */ |
| 172 | std::vector<std::string> getPSUInventoryPaths(sdbusplus::bus::bus& bus); |
Lei YU | cfc040c | 2019-10-29 17:10:26 +0800 | [diff] [blame] | 173 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 174 | } // namespace util |
| 175 | } // namespace power |
Lei YU | ab09332 | 2019-10-09 16:43:22 +0800 | [diff] [blame] | 176 | } // namespace phosphor |