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 | |
| 25 | /** |
| 26 | * @brief Get the service name from the mapper for the |
| 27 | * interface and path passed in. |
| 28 | * |
| 29 | * @param[in] path - the D-Bus path name |
| 30 | * @param[in] interface - the D-Bus interface name |
| 31 | * @param[in] bus - the D-Bus object |
Matthew Barth | d262440 | 2020-02-03 15:35:12 -0600 | [diff] [blame] | 32 | * @param[in] logError - log error when no service found |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 33 | * |
| 34 | * @return The service name |
| 35 | */ |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 36 | std::string getService(const std::string& path, const std::string& interface, |
Matthew Barth | d262440 | 2020-02-03 15:35:12 -0600 | [diff] [blame] | 37 | sdbusplus::bus::bus& bus, bool logError = true); |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * @brief Read a D-Bus property |
| 41 | * |
| 42 | * @param[in] interface - the interface the property is on |
| 43 | * @param[in] propertName - the name of the property |
| 44 | * @param[in] path - the D-Bus path |
| 45 | * @param[in] service - the D-Bus service |
| 46 | * @param[in] bus - the D-Bus object |
| 47 | * @param[out] value - filled in with the property value |
| 48 | */ |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 49 | template <typename T> |
| 50 | void getProperty(const std::string& interface, const std::string& propertyName, |
| 51 | const std::string& path, const std::string& service, |
| 52 | sdbusplus::bus::bus& bus, T& value) |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 53 | { |
Patrick Williams | abe4941 | 2020-05-13 17:59:47 -0500 | [diff] [blame] | 54 | std::variant<T> property; |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 55 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 56 | auto method = bus.new_method_call(service.c_str(), path.c_str(), |
| 57 | PROPERTY_INTF, "Get"); |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 58 | |
| 59 | method.append(interface, propertyName); |
| 60 | |
| 61 | auto reply = bus.call(method); |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 62 | |
| 63 | reply.read(property); |
Patrick Williams | 365d61c | 2020-05-13 12:23:08 -0500 | [diff] [blame] | 64 | value = std::get<T>(property); |
Matt Spinler | 974c916 | 2017-08-04 08:36:37 -0500 | [diff] [blame] | 65 | } |
| 66 | |
Matt Spinler | 48b4a43 | 2017-08-04 11:57:37 -0500 | [diff] [blame] | 67 | /** |
Brandon Wyman | 0a4f519 | 2017-12-06 20:19:08 -0600 | [diff] [blame] | 68 | * @brief Write a D-Bus property |
| 69 | * |
| 70 | * @param[in] interface - the interface the property is on |
| 71 | * @param[in] propertName - the name of the property |
| 72 | * @param[in] path - the D-Bus path |
| 73 | * @param[in] service - the D-Bus service |
| 74 | * @param[in] bus - the D-Bus object |
| 75 | * @param[in] value - the value to set the property to |
| 76 | */ |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 77 | template <typename T> |
| 78 | void setProperty(const std::string& interface, const std::string& propertyName, |
| 79 | const std::string& path, const std::string& service, |
| 80 | sdbusplus::bus::bus& bus, T& value) |
Brandon Wyman | 0a4f519 | 2017-12-06 20:19:08 -0600 | [diff] [blame] | 81 | { |
Patrick Williams | abe4941 | 2020-05-13 17:59:47 -0500 | [diff] [blame] | 82 | std::variant<T> propertyValue(value); |
Brandon Wyman | 0a4f519 | 2017-12-06 20:19:08 -0600 | [diff] [blame] | 83 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 84 | auto method = bus.new_method_call(service.c_str(), path.c_str(), |
| 85 | PROPERTY_INTF, "Set"); |
Brandon Wyman | 0a4f519 | 2017-12-06 20:19:08 -0600 | [diff] [blame] | 86 | |
| 87 | method.append(interface, propertyName, propertyValue); |
| 88 | |
| 89 | auto reply = bus.call(method); |
Brandon Wyman | 0a4f519 | 2017-12-06 20:19:08 -0600 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /** |
Matt Spinler | 882ce95 | 2017-10-05 16:12:41 -0500 | [diff] [blame] | 93 | * Logs an error and powers off the system. |
Matt Spinler | 48b4a43 | 2017-08-04 11:57:37 -0500 | [diff] [blame] | 94 | * |
Matt Spinler | 882ce95 | 2017-10-05 16:12:41 -0500 | [diff] [blame] | 95 | * @tparam T - error that will be logged before the power off |
Matt Spinler | 48b4a43 | 2017-08-04 11:57:37 -0500 | [diff] [blame] | 96 | * @param[in] bus - D-Bus object |
| 97 | */ |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 98 | template <typename T> |
Matt Spinler | 882ce95 | 2017-10-05 16:12:41 -0500 | [diff] [blame] | 99 | void powerOff(sdbusplus::bus::bus& bus) |
| 100 | { |
| 101 | phosphor::logging::report<T>(); |
| 102 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 103 | auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT, |
| 104 | SYSTEMD_INTERFACE, "StartUnit"); |
Matt Spinler | 882ce95 | 2017-10-05 16:12:41 -0500 | [diff] [blame] | 105 | |
| 106 | method.append(POWEROFF_TARGET); |
| 107 | method.append("replace"); |
| 108 | |
| 109 | bus.call_noreply(method); |
| 110 | } |
Matt Spinler | 48b4a43 | 2017-08-04 11:57:37 -0500 | [diff] [blame] | 111 | |
Lei YU | 7dc31bb | 2019-08-30 10:07:08 +0800 | [diff] [blame] | 112 | /** |
| 113 | * Load json from a file |
| 114 | * |
| 115 | * @param[in] path - The path of the json file |
| 116 | * |
| 117 | * @return The nlohmann::json object |
| 118 | */ |
| 119 | nlohmann::json loadJSONFromFile(const char* path); |
| 120 | |
Lei YU | 4070546 | 2019-10-09 17:07:11 +0800 | [diff] [blame] | 121 | /** |
| 122 | * Get PmBus access type from the json config |
| 123 | * |
| 124 | * @param[in] json - The json object |
| 125 | * |
| 126 | * @return The pmbus access type |
| 127 | */ |
| 128 | phosphor::pmbus::Type getPMBusAccessType(const nlohmann::json& json); |
| 129 | |
Lei YU | cfc040c | 2019-10-29 17:10:26 +0800 | [diff] [blame] | 130 | /** |
| 131 | * Check if power is on |
| 132 | * |
Lei YU | e8c9cd6 | 2019-11-04 14:24:41 +0800 | [diff] [blame] | 133 | * @param[in] bus - D-Bus object |
| 134 | * @param[in] defaultState - The default state if the function fails to get |
| 135 | * the power state. |
| 136 | * |
| 137 | * @return true if power is on, otherwise false; |
| 138 | * defaultState if it fails to get the power state. |
Lei YU | cfc040c | 2019-10-29 17:10:26 +0800 | [diff] [blame] | 139 | */ |
Lei YU | e8c9cd6 | 2019-11-04 14:24:41 +0800 | [diff] [blame] | 140 | bool isPoweredOn(sdbusplus::bus::bus& bus, bool defaultState = false); |
| 141 | |
| 142 | /** |
| 143 | * Get all PSU inventory paths from D-Bus |
| 144 | * |
| 145 | * @param[in] bus - D-Bus object |
| 146 | * |
| 147 | * @return The list of PSU inventory paths |
| 148 | */ |
| 149 | std::vector<std::string> getPSUInventoryPaths(sdbusplus::bus::bus& bus); |
Lei YU | cfc040c | 2019-10-29 17:10:26 +0800 | [diff] [blame] | 150 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 151 | } // namespace util |
| 152 | } // namespace power |
Lei YU | ab09332 | 2019-10-09 16:43:22 +0800 | [diff] [blame] | 153 | } // namespace phosphor |