1974c916eSMatt Spinler #pragma once 2974c916eSMatt Spinler 3*7dc31bb1SLei YU #include <nlohmann/json.hpp> 4882ce956SMatt Spinler #include <phosphor-logging/elog.hpp> 5f0f02b9aSMatt Spinler #include <phosphor-logging/log.hpp> 6974c916eSMatt Spinler #include <sdbusplus/bus.hpp> 7974c916eSMatt Spinler #include <string> 8974c916eSMatt Spinler 9974c916eSMatt Spinler namespace witherspoon 10974c916eSMatt Spinler { 11974c916eSMatt Spinler namespace power 12974c916eSMatt Spinler { 13974c916eSMatt Spinler namespace util 14974c916eSMatt Spinler { 15974c916eSMatt Spinler 16882ce956SMatt Spinler constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; 17882ce956SMatt Spinler constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1"; 18882ce956SMatt Spinler constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; 19882ce956SMatt Spinler constexpr auto POWEROFF_TARGET = "obmc-chassis-hard-poweroff@0.target"; 20974c916eSMatt Spinler constexpr auto PROPERTY_INTF = "org.freedesktop.DBus.Properties"; 21974c916eSMatt Spinler 22974c916eSMatt Spinler /** 23974c916eSMatt Spinler * @brief Get the service name from the mapper for the 24974c916eSMatt Spinler * interface and path passed in. 25974c916eSMatt Spinler * 26974c916eSMatt Spinler * @param[in] path - the D-Bus path name 27974c916eSMatt Spinler * @param[in] interface - the D-Bus interface name 28974c916eSMatt Spinler * @param[in] bus - the D-Bus object 29974c916eSMatt Spinler * 30974c916eSMatt Spinler * @return The service name 31974c916eSMatt Spinler */ 32f0f02b9aSMatt Spinler std::string getService(const std::string& path, const std::string& interface, 33974c916eSMatt Spinler sdbusplus::bus::bus& bus); 34974c916eSMatt Spinler 35974c916eSMatt Spinler /** 36974c916eSMatt Spinler * @brief Read a D-Bus property 37974c916eSMatt Spinler * 38974c916eSMatt Spinler * @param[in] interface - the interface the property is on 39974c916eSMatt Spinler * @param[in] propertName - the name of the property 40974c916eSMatt Spinler * @param[in] path - the D-Bus path 41974c916eSMatt Spinler * @param[in] service - the D-Bus service 42974c916eSMatt Spinler * @param[in] bus - the D-Bus object 43974c916eSMatt Spinler * @param[out] value - filled in with the property value 44974c916eSMatt Spinler */ 45974c916eSMatt Spinler template <typename T> 46f0f02b9aSMatt Spinler void getProperty(const std::string& interface, const std::string& propertyName, 47f0f02b9aSMatt Spinler const std::string& path, const std::string& service, 48f0f02b9aSMatt Spinler sdbusplus::bus::bus& bus, T& value) 49974c916eSMatt Spinler { 50974c916eSMatt Spinler sdbusplus::message::variant<T> property; 51974c916eSMatt Spinler 52f0f02b9aSMatt Spinler auto method = bus.new_method_call(service.c_str(), path.c_str(), 53f0f02b9aSMatt Spinler PROPERTY_INTF, "Get"); 54974c916eSMatt Spinler 55974c916eSMatt Spinler method.append(interface, propertyName); 56974c916eSMatt Spinler 57974c916eSMatt Spinler auto reply = bus.call(method); 58974c916eSMatt Spinler 59974c916eSMatt Spinler reply.read(property); 60974c916eSMatt Spinler value = sdbusplus::message::variant_ns::get<T>(property); 61974c916eSMatt Spinler } 62974c916eSMatt Spinler 6348b4a430SMatt Spinler /** 640a4f519bSBrandon Wyman * @brief Write a D-Bus property 650a4f519bSBrandon Wyman * 660a4f519bSBrandon Wyman * @param[in] interface - the interface the property is on 670a4f519bSBrandon Wyman * @param[in] propertName - the name of the property 680a4f519bSBrandon Wyman * @param[in] path - the D-Bus path 690a4f519bSBrandon Wyman * @param[in] service - the D-Bus service 700a4f519bSBrandon Wyman * @param[in] bus - the D-Bus object 710a4f519bSBrandon Wyman * @param[in] value - the value to set the property to 720a4f519bSBrandon Wyman */ 730a4f519bSBrandon Wyman template <typename T> 74f0f02b9aSMatt Spinler void setProperty(const std::string& interface, const std::string& propertyName, 75f0f02b9aSMatt Spinler const std::string& path, const std::string& service, 76f0f02b9aSMatt Spinler sdbusplus::bus::bus& bus, T& value) 770a4f519bSBrandon Wyman { 780a4f519bSBrandon Wyman sdbusplus::message::variant<T> propertyValue(value); 790a4f519bSBrandon Wyman 80f0f02b9aSMatt Spinler auto method = bus.new_method_call(service.c_str(), path.c_str(), 81f0f02b9aSMatt Spinler PROPERTY_INTF, "Set"); 820a4f519bSBrandon Wyman 830a4f519bSBrandon Wyman method.append(interface, propertyName, propertyValue); 840a4f519bSBrandon Wyman 850a4f519bSBrandon Wyman auto reply = bus.call(method); 860a4f519bSBrandon Wyman } 870a4f519bSBrandon Wyman 880a4f519bSBrandon Wyman /** 89882ce956SMatt Spinler * Logs an error and powers off the system. 9048b4a430SMatt Spinler * 91882ce956SMatt Spinler * @tparam T - error that will be logged before the power off 9248b4a430SMatt Spinler * @param[in] bus - D-Bus object 9348b4a430SMatt Spinler */ 94882ce956SMatt Spinler template <typename T> 95882ce956SMatt Spinler void powerOff(sdbusplus::bus::bus& bus) 96882ce956SMatt Spinler { 97882ce956SMatt Spinler phosphor::logging::report<T>(); 98882ce956SMatt Spinler 99f0f02b9aSMatt Spinler auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT, 100f0f02b9aSMatt Spinler SYSTEMD_INTERFACE, "StartUnit"); 101882ce956SMatt Spinler 102882ce956SMatt Spinler method.append(POWEROFF_TARGET); 103882ce956SMatt Spinler method.append("replace"); 104882ce956SMatt Spinler 105882ce956SMatt Spinler bus.call_noreply(method); 106882ce956SMatt Spinler } 10748b4a430SMatt Spinler 108*7dc31bb1SLei YU /** 109*7dc31bb1SLei YU * Load json from a file 110*7dc31bb1SLei YU * 111*7dc31bb1SLei YU * @param[in] path - The path of the json file 112*7dc31bb1SLei YU * 113*7dc31bb1SLei YU * @return The nlohmann::json object 114*7dc31bb1SLei YU */ 115*7dc31bb1SLei YU nlohmann::json loadJSONFromFile(const char* path); 116*7dc31bb1SLei YU 117f0f02b9aSMatt Spinler } // namespace util 118f0f02b9aSMatt Spinler } // namespace power 119f0f02b9aSMatt Spinler } // namespace witherspoon 120