blob: ffc489c6d6d91cf880c44155ae5240ee1f97f61c [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>
9#include <string>
10
Lei YUab093322019-10-09 16:43:22 +080011namespace phosphor
Matt Spinler974c9162017-08-04 08:36:37 -050012{
13namespace power
14{
15namespace util
16{
17
Matt Spinlerf0f02b92018-10-25 16:12:43 -050018constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
19constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
Matt Spinler882ce952017-10-05 16:12:41 -050020constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
Matt Spinlerf0f02b92018-10-25 16:12:43 -050021constexpr auto POWEROFF_TARGET = "obmc-chassis-hard-poweroff@0.target";
Matt Spinler974c9162017-08-04 08:36:37 -050022constexpr auto PROPERTY_INTF = "org.freedesktop.DBus.Properties";
23
24/**
25 * @brief Get the service name from the mapper for the
26 * interface and path passed in.
27 *
28 * @param[in] path - the D-Bus path name
29 * @param[in] interface - the D-Bus interface name
30 * @param[in] bus - the D-Bus object
31 *
32 * @return The service name
33 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050034std::string getService(const std::string& path, const std::string& interface,
Matt Spinler974c9162017-08-04 08:36:37 -050035 sdbusplus::bus::bus& bus);
36
37/**
38 * @brief Read a D-Bus property
39 *
40 * @param[in] interface - the interface the property is on
41 * @param[in] propertName - the name of the property
42 * @param[in] path - the D-Bus path
43 * @param[in] service - the D-Bus service
44 * @param[in] bus - the D-Bus object
45 * @param[out] value - filled in with the property value
46 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050047template <typename T>
48void getProperty(const std::string& interface, const std::string& propertyName,
49 const std::string& path, const std::string& service,
50 sdbusplus::bus::bus& bus, T& value)
Matt Spinler974c9162017-08-04 08:36:37 -050051{
52 sdbusplus::message::variant<T> property;
53
Matt Spinlerf0f02b92018-10-25 16:12:43 -050054 auto method = bus.new_method_call(service.c_str(), path.c_str(),
55 PROPERTY_INTF, "Get");
Matt Spinler974c9162017-08-04 08:36:37 -050056
57 method.append(interface, propertyName);
58
59 auto reply = bus.call(method);
Matt Spinler974c9162017-08-04 08:36:37 -050060
61 reply.read(property);
62 value = sdbusplus::message::variant_ns::get<T>(property);
63}
64
Matt Spinler48b4a432017-08-04 11:57:37 -050065/**
Brandon Wyman0a4f5192017-12-06 20:19:08 -060066 * @brief Write a D-Bus property
67 *
68 * @param[in] interface - the interface the property is on
69 * @param[in] propertName - the name of the property
70 * @param[in] path - the D-Bus path
71 * @param[in] service - the D-Bus service
72 * @param[in] bus - the D-Bus object
73 * @param[in] value - the value to set the property to
74 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050075template <typename T>
76void setProperty(const std::string& interface, const std::string& propertyName,
77 const std::string& path, const std::string& service,
78 sdbusplus::bus::bus& bus, T& value)
Brandon Wyman0a4f5192017-12-06 20:19:08 -060079{
80 sdbusplus::message::variant<T> propertyValue(value);
81
Matt Spinlerf0f02b92018-10-25 16:12:43 -050082 auto method = bus.new_method_call(service.c_str(), path.c_str(),
83 PROPERTY_INTF, "Set");
Brandon Wyman0a4f5192017-12-06 20:19:08 -060084
85 method.append(interface, propertyName, propertyValue);
86
87 auto reply = bus.call(method);
Brandon Wyman0a4f5192017-12-06 20:19:08 -060088}
89
90/**
Matt Spinler882ce952017-10-05 16:12:41 -050091 * Logs an error and powers off the system.
Matt Spinler48b4a432017-08-04 11:57:37 -050092 *
Matt Spinler882ce952017-10-05 16:12:41 -050093 * @tparam T - error that will be logged before the power off
Matt Spinler48b4a432017-08-04 11:57:37 -050094 * @param[in] bus - D-Bus object
95 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050096template <typename T>
Matt Spinler882ce952017-10-05 16:12:41 -050097void powerOff(sdbusplus::bus::bus& bus)
98{
99 phosphor::logging::report<T>();
100
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500101 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
102 SYSTEMD_INTERFACE, "StartUnit");
Matt Spinler882ce952017-10-05 16:12:41 -0500103
104 method.append(POWEROFF_TARGET);
105 method.append("replace");
106
107 bus.call_noreply(method);
108}
Matt Spinler48b4a432017-08-04 11:57:37 -0500109
Lei YU7dc31bb2019-08-30 10:07:08 +0800110/**
111 * Load json from a file
112 *
113 * @param[in] path - The path of the json file
114 *
115 * @return The nlohmann::json object
116 */
117nlohmann::json loadJSONFromFile(const char* path);
118
Lei YU40705462019-10-09 17:07:11 +0800119/**
120 * Get PmBus access type from the json config
121 *
122 * @param[in] json - The json object
123 *
124 * @return The pmbus access type
125 */
126phosphor::pmbus::Type getPMBusAccessType(const nlohmann::json& json);
127
Lei YUcfc040c2019-10-29 17:10:26 +0800128/**
129 * Check if power is on
130 *
131 * @return true if power is on, otherwise false
132 */
133bool isPoweredOn(sdbusplus::bus::bus& bus);
134
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500135} // namespace util
136} // namespace power
Lei YUab093322019-10-09 16:43:22 +0800137} // namespace phosphor