1974c916eSMatt Spinler #pragma once 2974c916eSMatt Spinler 34070546eSLei YU #include "pmbus.hpp" 44070546eSLei YU 57dc31bb1SLei YU #include <nlohmann/json.hpp> 6882ce956SMatt Spinler #include <phosphor-logging/elog.hpp> 7f0f02b9aSMatt Spinler #include <phosphor-logging/log.hpp> 8974c916eSMatt Spinler #include <sdbusplus/bus.hpp> 9974c916eSMatt Spinler #include <string> 10974c916eSMatt Spinler 11ab093328SLei YU namespace phosphor 12974c916eSMatt Spinler { 13974c916eSMatt Spinler namespace power 14974c916eSMatt Spinler { 15974c916eSMatt Spinler namespace util 16974c916eSMatt Spinler { 17974c916eSMatt Spinler 18882ce956SMatt Spinler constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; 19882ce956SMatt Spinler constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1"; 20882ce956SMatt Spinler constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; 21882ce956SMatt Spinler constexpr auto POWEROFF_TARGET = "obmc-chassis-hard-poweroff@0.target"; 22974c916eSMatt Spinler constexpr auto PROPERTY_INTF = "org.freedesktop.DBus.Properties"; 23974c916eSMatt Spinler 24974c916eSMatt Spinler /** 25974c916eSMatt Spinler * @brief Get the service name from the mapper for the 26974c916eSMatt Spinler * interface and path passed in. 27974c916eSMatt Spinler * 28974c916eSMatt Spinler * @param[in] path - the D-Bus path name 29974c916eSMatt Spinler * @param[in] interface - the D-Bus interface name 30974c916eSMatt Spinler * @param[in] bus - the D-Bus object 31974c916eSMatt Spinler * 32974c916eSMatt Spinler * @return The service name 33974c916eSMatt Spinler */ 34f0f02b9aSMatt Spinler std::string getService(const std::string& path, const std::string& interface, 35974c916eSMatt Spinler sdbusplus::bus::bus& bus); 36974c916eSMatt Spinler 37974c916eSMatt Spinler /** 38974c916eSMatt Spinler * @brief Read a D-Bus property 39974c916eSMatt Spinler * 40974c916eSMatt Spinler * @param[in] interface - the interface the property is on 41974c916eSMatt Spinler * @param[in] propertName - the name of the property 42974c916eSMatt Spinler * @param[in] path - the D-Bus path 43974c916eSMatt Spinler * @param[in] service - the D-Bus service 44974c916eSMatt Spinler * @param[in] bus - the D-Bus object 45974c916eSMatt Spinler * @param[out] value - filled in with the property value 46974c916eSMatt Spinler */ 47974c916eSMatt Spinler template <typename T> 48f0f02b9aSMatt Spinler void getProperty(const std::string& interface, const std::string& propertyName, 49f0f02b9aSMatt Spinler const std::string& path, const std::string& service, 50f0f02b9aSMatt Spinler sdbusplus::bus::bus& bus, T& value) 51974c916eSMatt Spinler { 52974c916eSMatt Spinler sdbusplus::message::variant<T> property; 53974c916eSMatt Spinler 54f0f02b9aSMatt Spinler auto method = bus.new_method_call(service.c_str(), path.c_str(), 55f0f02b9aSMatt Spinler PROPERTY_INTF, "Get"); 56974c916eSMatt Spinler 57974c916eSMatt Spinler method.append(interface, propertyName); 58974c916eSMatt Spinler 59974c916eSMatt Spinler auto reply = bus.call(method); 60974c916eSMatt Spinler 61974c916eSMatt Spinler reply.read(property); 62974c916eSMatt Spinler value = sdbusplus::message::variant_ns::get<T>(property); 63974c916eSMatt Spinler } 64974c916eSMatt Spinler 6548b4a430SMatt Spinler /** 660a4f519bSBrandon Wyman * @brief Write a D-Bus property 670a4f519bSBrandon Wyman * 680a4f519bSBrandon Wyman * @param[in] interface - the interface the property is on 690a4f519bSBrandon Wyman * @param[in] propertName - the name of the property 700a4f519bSBrandon Wyman * @param[in] path - the D-Bus path 710a4f519bSBrandon Wyman * @param[in] service - the D-Bus service 720a4f519bSBrandon Wyman * @param[in] bus - the D-Bus object 730a4f519bSBrandon Wyman * @param[in] value - the value to set the property to 740a4f519bSBrandon Wyman */ 750a4f519bSBrandon Wyman template <typename T> 76f0f02b9aSMatt Spinler void setProperty(const std::string& interface, const std::string& propertyName, 77f0f02b9aSMatt Spinler const std::string& path, const std::string& service, 78f0f02b9aSMatt Spinler sdbusplus::bus::bus& bus, T& value) 790a4f519bSBrandon Wyman { 800a4f519bSBrandon Wyman sdbusplus::message::variant<T> propertyValue(value); 810a4f519bSBrandon Wyman 82f0f02b9aSMatt Spinler auto method = bus.new_method_call(service.c_str(), path.c_str(), 83f0f02b9aSMatt Spinler PROPERTY_INTF, "Set"); 840a4f519bSBrandon Wyman 850a4f519bSBrandon Wyman method.append(interface, propertyName, propertyValue); 860a4f519bSBrandon Wyman 870a4f519bSBrandon Wyman auto reply = bus.call(method); 880a4f519bSBrandon Wyman } 890a4f519bSBrandon Wyman 900a4f519bSBrandon Wyman /** 91882ce956SMatt Spinler * Logs an error and powers off the system. 9248b4a430SMatt Spinler * 93882ce956SMatt Spinler * @tparam T - error that will be logged before the power off 9448b4a430SMatt Spinler * @param[in] bus - D-Bus object 9548b4a430SMatt Spinler */ 96882ce956SMatt Spinler template <typename T> 97882ce956SMatt Spinler void powerOff(sdbusplus::bus::bus& bus) 98882ce956SMatt Spinler { 99882ce956SMatt Spinler phosphor::logging::report<T>(); 100882ce956SMatt Spinler 101f0f02b9aSMatt Spinler auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT, 102f0f02b9aSMatt Spinler SYSTEMD_INTERFACE, "StartUnit"); 103882ce956SMatt Spinler 104882ce956SMatt Spinler method.append(POWEROFF_TARGET); 105882ce956SMatt Spinler method.append("replace"); 106882ce956SMatt Spinler 107882ce956SMatt Spinler bus.call_noreply(method); 108882ce956SMatt Spinler } 10948b4a430SMatt Spinler 1107dc31bb1SLei YU /** 1117dc31bb1SLei YU * Load json from a file 1127dc31bb1SLei YU * 1137dc31bb1SLei YU * @param[in] path - The path of the json file 1147dc31bb1SLei YU * 1157dc31bb1SLei YU * @return The nlohmann::json object 1167dc31bb1SLei YU */ 1177dc31bb1SLei YU nlohmann::json loadJSONFromFile(const char* path); 1187dc31bb1SLei YU 1194070546eSLei YU /** 1204070546eSLei YU * Get PmBus access type from the json config 1214070546eSLei YU * 1224070546eSLei YU * @param[in] json - The json object 1234070546eSLei YU * 1244070546eSLei YU * @return The pmbus access type 1254070546eSLei YU */ 1264070546eSLei YU phosphor::pmbus::Type getPMBusAccessType(const nlohmann::json& json); 1274070546eSLei YU 128cfc040c7SLei YU /** 129cfc040c7SLei YU * Check if power is on 130cfc040c7SLei YU * 131*e8c9cd64SLei YU * @param[in] bus - D-Bus object 132*e8c9cd64SLei YU * @param[in] defaultState - The default state if the function fails to get 133*e8c9cd64SLei YU * the power state. 134*e8c9cd64SLei YU * 135*e8c9cd64SLei YU * @return true if power is on, otherwise false; 136*e8c9cd64SLei YU * defaultState if it fails to get the power state. 137cfc040c7SLei YU */ 138*e8c9cd64SLei YU bool isPoweredOn(sdbusplus::bus::bus& bus, bool defaultState = false); 139*e8c9cd64SLei YU 140*e8c9cd64SLei YU /** 141*e8c9cd64SLei YU * Get all PSU inventory paths from D-Bus 142*e8c9cd64SLei YU * 143*e8c9cd64SLei YU * @param[in] bus - D-Bus object 144*e8c9cd64SLei YU * 145*e8c9cd64SLei YU * @return The list of PSU inventory paths 146*e8c9cd64SLei YU */ 147*e8c9cd64SLei YU std::vector<std::string> getPSUInventoryPaths(sdbusplus::bus::bus& bus); 148cfc040c7SLei YU 149f0f02b9aSMatt Spinler } // namespace util 150f0f02b9aSMatt Spinler } // namespace power 151ab093328SLei YU } // namespace phosphor 152