1 #pragma once 2 3 #include "pmbus.hpp" 4 5 #include <nlohmann/json.hpp> 6 #include <phosphor-logging/elog.hpp> 7 #include <phosphor-logging/log.hpp> 8 #include <sdbusplus/bus.hpp> 9 10 #include <string> 11 12 namespace phosphor 13 { 14 namespace power 15 { 16 namespace util 17 { 18 19 constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; 20 constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1"; 21 constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; 22 constexpr auto POWEROFF_TARGET = "obmc-chassis-hard-poweroff@0.target"; 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 32 * @param[in] logError - log error when no service found 33 * 34 * @return The service name 35 */ 36 std::string getService(const std::string& path, const std::string& interface, 37 sdbusplus::bus::bus& bus, bool logError = true); 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 */ 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) 53 { 54 std::variant<T> property; 55 56 auto method = bus.new_method_call(service.c_str(), path.c_str(), 57 PROPERTY_INTF, "Get"); 58 59 method.append(interface, propertyName); 60 61 auto reply = bus.call(method); 62 63 reply.read(property); 64 value = std::get<T>(property); 65 } 66 67 /** 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 */ 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) 81 { 82 std::variant<T> propertyValue(value); 83 84 auto method = bus.new_method_call(service.c_str(), path.c_str(), 85 PROPERTY_INTF, "Set"); 86 87 method.append(interface, propertyName, propertyValue); 88 89 auto reply = bus.call(method); 90 } 91 92 /** 93 * Logs an error and powers off the system. 94 * 95 * @tparam T - error that will be logged before the power off 96 * @param[in] bus - D-Bus object 97 */ 98 template <typename T> 99 void powerOff(sdbusplus::bus::bus& bus) 100 { 101 phosphor::logging::report<T>(); 102 103 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT, 104 SYSTEMD_INTERFACE, "StartUnit"); 105 106 method.append(POWEROFF_TARGET); 107 method.append("replace"); 108 109 bus.call_noreply(method); 110 } 111 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 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 130 /** 131 * Check if power is on 132 * 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. 139 */ 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); 150 151 } // namespace util 152 } // namespace power 153 } // namespace phosphor 154