blob: af50127cae43425c2d61b39660eb1be622b00751 [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
Matthew Barthd2624402020-02-03 15:35:12 -060032 * @param[in] logError - log error when no service found
Matt Spinler974c9162017-08-04 08:36:37 -050033 *
34 * @return The service name
35 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050036std::string getService(const std::string& path, const std::string& interface,
Matthew Barthd2624402020-02-03 15:35:12 -060037 sdbusplus::bus::bus& bus, bool logError = true);
Matt Spinler974c9162017-08-04 08:36:37 -050038
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 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050049template <typename T>
50void 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)
Matt Spinler974c9162017-08-04 08:36:37 -050053{
54 sdbusplus::message::variant<T> property;
55
Matt Spinlerf0f02b92018-10-25 16:12:43 -050056 auto method = bus.new_method_call(service.c_str(), path.c_str(),
57 PROPERTY_INTF, "Get");
Matt Spinler974c9162017-08-04 08:36:37 -050058
59 method.append(interface, propertyName);
60
61 auto reply = bus.call(method);
Matt Spinler974c9162017-08-04 08:36:37 -050062
63 reply.read(property);
64 value = sdbusplus::message::variant_ns::get<T>(property);
65}
66
Matt Spinler48b4a432017-08-04 11:57:37 -050067/**
Brandon Wyman0a4f5192017-12-06 20:19:08 -060068 * @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 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050077template <typename T>
78void 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)
Brandon Wyman0a4f5192017-12-06 20:19:08 -060081{
82 sdbusplus::message::variant<T> propertyValue(value);
83
Matt Spinlerf0f02b92018-10-25 16:12:43 -050084 auto method = bus.new_method_call(service.c_str(), path.c_str(),
85 PROPERTY_INTF, "Set");
Brandon Wyman0a4f5192017-12-06 20:19:08 -060086
87 method.append(interface, propertyName, propertyValue);
88
89 auto reply = bus.call(method);
Brandon Wyman0a4f5192017-12-06 20:19:08 -060090}
91
92/**
Matt Spinler882ce952017-10-05 16:12:41 -050093 * Logs an error and powers off the system.
Matt Spinler48b4a432017-08-04 11:57:37 -050094 *
Matt Spinler882ce952017-10-05 16:12:41 -050095 * @tparam T - error that will be logged before the power off
Matt Spinler48b4a432017-08-04 11:57:37 -050096 * @param[in] bus - D-Bus object
97 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050098template <typename T>
Matt Spinler882ce952017-10-05 16:12:41 -050099void powerOff(sdbusplus::bus::bus& bus)
100{
101 phosphor::logging::report<T>();
102
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500103 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
104 SYSTEMD_INTERFACE, "StartUnit");
Matt Spinler882ce952017-10-05 16:12:41 -0500105
106 method.append(POWEROFF_TARGET);
107 method.append("replace");
108
109 bus.call_noreply(method);
110}
Matt Spinler48b4a432017-08-04 11:57:37 -0500111
Lei YU7dc31bb2019-08-30 10:07:08 +0800112/**
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 */
119nlohmann::json loadJSONFromFile(const char* path);
120
Lei YU40705462019-10-09 17:07:11 +0800121/**
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 */
128phosphor::pmbus::Type getPMBusAccessType(const nlohmann::json& json);
129
Lei YUcfc040c2019-10-29 17:10:26 +0800130/**
131 * Check if power is on
132 *
Lei YUe8c9cd62019-11-04 14:24:41 +0800133 * @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.
Lei YUcfc040c2019-10-29 17:10:26 +0800139 */
Lei YUe8c9cd62019-11-04 14:24:41 +0800140bool 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 */
149std::vector<std::string> getPSUInventoryPaths(sdbusplus::bus::bus& bus);
Lei YUcfc040c2019-10-29 17:10:26 +0800150
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500151} // namespace util
152} // namespace power
Lei YUab093322019-10-09 16:43:22 +0800153} // namespace phosphor