blob: 80847822aa885d3d500be4d0d0a9c8872579be74 [file] [log] [blame]
Matt Spinler974c9162017-08-04 08:36:37 -05001#pragma once
2
Lei YU40705462019-10-09 17:07:11 +08003#include "pmbus.hpp"
4
Lei YU7dc31bb2019-08-30 10:07:08 +08005#include <nlohmann/json.hpp>
Matt Spinler882ce952017-10-05 16:12:41 -05006#include <phosphor-logging/elog.hpp>
Matt Spinlerf0f02b92018-10-25 16:12:43 -05007#include <phosphor-logging/log.hpp>
Matt Spinler974c9162017-08-04 08:36:37 -05008#include <sdbusplus/bus.hpp>
Brandon Wymand1bc4ce2019-12-13 14:20:34 -06009
Matt Spinler974c9162017-08-04 08:36:37 -050010#include <string>
11
Lei YUab093322019-10-09 16:43:22 +080012namespace phosphor
Matt Spinler974c9162017-08-04 08:36:37 -050013{
14namespace power
15{
16namespace util
17{
18
Matt Spinlerf0f02b92018-10-25 16:12:43 -050019constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
20constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
Matt Spinler882ce952017-10-05 16:12:41 -050021constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
Matt Spinlerf0f02b92018-10-25 16:12:43 -050022constexpr auto POWEROFF_TARGET = "obmc-chassis-hard-poweroff@0.target";
Matt Spinler974c9162017-08-04 08:36:37 -050023constexpr 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 *
33 * @return The service name
34 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050035std::string getService(const std::string& path, const std::string& interface,
Matt Spinler974c9162017-08-04 08:36:37 -050036 sdbusplus::bus::bus& bus);
37
38/**
39 * @brief Read a D-Bus property
40 *
41 * @param[in] interface - the interface the property is on
42 * @param[in] propertName - the name of the property
43 * @param[in] path - the D-Bus path
44 * @param[in] service - the D-Bus service
45 * @param[in] bus - the D-Bus object
46 * @param[out] value - filled in with the property value
47 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050048template <typename T>
49void getProperty(const std::string& interface, const std::string& propertyName,
50 const std::string& path, const std::string& service,
51 sdbusplus::bus::bus& bus, T& value)
Matt Spinler974c9162017-08-04 08:36:37 -050052{
53 sdbusplus::message::variant<T> property;
54
Matt Spinlerf0f02b92018-10-25 16:12:43 -050055 auto method = bus.new_method_call(service.c_str(), path.c_str(),
56 PROPERTY_INTF, "Get");
Matt Spinler974c9162017-08-04 08:36:37 -050057
58 method.append(interface, propertyName);
59
60 auto reply = bus.call(method);
Matt Spinler974c9162017-08-04 08:36:37 -050061
62 reply.read(property);
63 value = sdbusplus::message::variant_ns::get<T>(property);
64}
65
Matt Spinler48b4a432017-08-04 11:57:37 -050066/**
Brandon Wyman0a4f5192017-12-06 20:19:08 -060067 * @brief Write a D-Bus property
68 *
69 * @param[in] interface - the interface the property is on
70 * @param[in] propertName - the name of the property
71 * @param[in] path - the D-Bus path
72 * @param[in] service - the D-Bus service
73 * @param[in] bus - the D-Bus object
74 * @param[in] value - the value to set the property to
75 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050076template <typename T>
77void setProperty(const std::string& interface, const std::string& propertyName,
78 const std::string& path, const std::string& service,
79 sdbusplus::bus::bus& bus, T& value)
Brandon Wyman0a4f5192017-12-06 20:19:08 -060080{
81 sdbusplus::message::variant<T> propertyValue(value);
82
Matt Spinlerf0f02b92018-10-25 16:12:43 -050083 auto method = bus.new_method_call(service.c_str(), path.c_str(),
84 PROPERTY_INTF, "Set");
Brandon Wyman0a4f5192017-12-06 20:19:08 -060085
86 method.append(interface, propertyName, propertyValue);
87
88 auto reply = bus.call(method);
Brandon Wyman0a4f5192017-12-06 20:19:08 -060089}
90
91/**
Matt Spinler882ce952017-10-05 16:12:41 -050092 * Logs an error and powers off the system.
Matt Spinler48b4a432017-08-04 11:57:37 -050093 *
Matt Spinler882ce952017-10-05 16:12:41 -050094 * @tparam T - error that will be logged before the power off
Matt Spinler48b4a432017-08-04 11:57:37 -050095 * @param[in] bus - D-Bus object
96 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050097template <typename T>
Matt Spinler882ce952017-10-05 16:12:41 -050098void powerOff(sdbusplus::bus::bus& bus)
99{
100 phosphor::logging::report<T>();
101
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500102 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
103 SYSTEMD_INTERFACE, "StartUnit");
Matt Spinler882ce952017-10-05 16:12:41 -0500104
105 method.append(POWEROFF_TARGET);
106 method.append("replace");
107
108 bus.call_noreply(method);
109}
Matt Spinler48b4a432017-08-04 11:57:37 -0500110
Lei YU7dc31bb2019-08-30 10:07:08 +0800111/**
112 * Load json from a file
113 *
114 * @param[in] path - The path of the json file
115 *
116 * @return The nlohmann::json object
117 */
118nlohmann::json loadJSONFromFile(const char* path);
119
Lei YU40705462019-10-09 17:07:11 +0800120/**
121 * Get PmBus access type from the json config
122 *
123 * @param[in] json - The json object
124 *
125 * @return The pmbus access type
126 */
127phosphor::pmbus::Type getPMBusAccessType(const nlohmann::json& json);
128
Lei YUcfc040c2019-10-29 17:10:26 +0800129/**
130 * Check if power is on
131 *
Lei YUe8c9cd62019-11-04 14:24:41 +0800132 * @param[in] bus - D-Bus object
133 * @param[in] defaultState - The default state if the function fails to get
134 * the power state.
135 *
136 * @return true if power is on, otherwise false;
137 * defaultState if it fails to get the power state.
Lei YUcfc040c2019-10-29 17:10:26 +0800138 */
Lei YUe8c9cd62019-11-04 14:24:41 +0800139bool isPoweredOn(sdbusplus::bus::bus& bus, bool defaultState = false);
140
141/**
142 * Get all PSU inventory paths from D-Bus
143 *
144 * @param[in] bus - D-Bus object
145 *
146 * @return The list of PSU inventory paths
147 */
148std::vector<std::string> getPSUInventoryPaths(sdbusplus::bus::bus& bus);
Lei YUcfc040c2019-10-29 17:10:26 +0800149
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500150} // namespace util
151} // namespace power
Lei YUab093322019-10-09 16:43:22 +0800152} // namespace phosphor