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