blob: 6ee1e816bed08bf2f907367baea76f8bac9a8561 [file] [log] [blame]
Brad Bishopb1e329a2017-08-02 01:23:12 -04001#pragma once
2
Patrick Venture3d6d3182018-08-31 09:33:09 -07003#include <phosphor-logging/elog-errors.hpp>
4#include <phosphor-logging/elog.hpp>
George Liu13e3df62022-06-22 15:03:06 +08005#include <phosphor-logging/lg2.hpp>
Brad Bishopb1e329a2017-08-02 01:23:12 -04006#include <sdbusplus/bus.hpp>
7#include <sdbusplus/message.hpp>
Brad Bishopb1e329a2017-08-02 01:23:12 -04008#include <xyz/openbmc_project/Common/error.hpp>
9
George Liu3fe976c2022-06-21 09:37:04 +080010#include <string>
11
Brad Bishopb1e329a2017-08-02 01:23:12 -040012namespace util
13{
14namespace detail
15{
16namespace errors = sdbusplus::xyz::openbmc_project::Common::Error;
17} // namespace detail
18
19/** @brief Alias for PropertiesChanged signal callbacks. */
Brad Bishopd1eac882018-03-29 10:34:05 -040020template <typename... T>
Patrick Williams35b4f332020-05-13 17:53:09 -050021using Properties = std::map<std::string, std::variant<T...>>;
Brad Bishopb1e329a2017-08-02 01:23:12 -040022
23namespace sdbusplus
24{
25
26/** @brief Get the bus connection. */
27static auto& getBus() __attribute__((pure));
28static auto& getBus()
29{
30 static auto bus = ::sdbusplus::bus::new_default();
31 return bus;
32}
33
34/** @brief Invoke a method. */
Brad Bishopd1eac882018-03-29 10:34:05 -040035template <typename... Args>
36static auto callMethod(::sdbusplus::bus::bus& bus, const std::string& busName,
37 const std::string& path, const std::string& interface,
38 const std::string& method, Args&&... args)
Brad Bishopb1e329a2017-08-02 01:23:12 -040039{
Brad Bishopd1eac882018-03-29 10:34:05 -040040 auto reqMsg = bus.new_method_call(busName.c_str(), path.c_str(),
41 interface.c_str(), method.c_str());
Brad Bishopb1e329a2017-08-02 01:23:12 -040042 reqMsg.append(std::forward<Args>(args)...);
43 auto respMsg = bus.call(reqMsg);
44
45 if (respMsg.is_method_error())
46 {
George Liu13e3df62022-06-22 15:03:06 +080047 lg2::error("Failed to invoke DBus method. {PATH}, {INTF}, {METHOD}",
48 "PATH", path, "INTF", interface, "METHOD", method);
Brad Bishopb1e329a2017-08-02 01:23:12 -040049 phosphor::logging::elog<detail::errors::InternalFailure>();
50 }
51
52 return respMsg;
53}
54
55/** @brief Invoke a method. */
Brad Bishopd1eac882018-03-29 10:34:05 -040056template <typename... Args>
57static auto callMethod(const std::string& busName, const std::string& path,
58 const std::string& interface, const std::string& method,
59 Args&&... args)
Brad Bishopb1e329a2017-08-02 01:23:12 -040060{
Brad Bishopd1eac882018-03-29 10:34:05 -040061 return callMethod(getBus(), busName, path, interface, method,
62 std::forward<Args>(args)...);
Brad Bishopb1e329a2017-08-02 01:23:12 -040063}
64
65/** @brief Invoke a method and read the response. */
Brad Bishopd1eac882018-03-29 10:34:05 -040066template <typename Ret, typename... Args>
67static auto
68 callMethodAndRead(::sdbusplus::bus::bus& bus, const std::string& busName,
69 const std::string& path, const std::string& interface,
70 const std::string& method, Args&&... args)
Brad Bishopb1e329a2017-08-02 01:23:12 -040071{
Brad Bishopd1eac882018-03-29 10:34:05 -040072 ::sdbusplus::message::message respMsg = callMethod<Args...>(
73 bus, busName, path, interface, method, std::forward<Args>(args)...);
Brad Bishopb1e329a2017-08-02 01:23:12 -040074 Ret resp;
75 respMsg.read(resp);
76 return resp;
77}
78
79/** @brief Invoke a method and read the response. */
Brad Bishopd1eac882018-03-29 10:34:05 -040080template <typename Ret, typename... Args>
81static auto callMethodAndRead(const std::string& busName,
82 const std::string& path,
83 const std::string& interface,
84 const std::string& method, Args&&... args)
Brad Bishopb1e329a2017-08-02 01:23:12 -040085{
Brad Bishopd1eac882018-03-29 10:34:05 -040086 return callMethodAndRead<Ret>(getBus(), busName, path, interface, method,
87 std::forward<Args>(args)...);
Brad Bishopb1e329a2017-08-02 01:23:12 -040088}
89
Brad Bishopb1e329a2017-08-02 01:23:12 -040090/** @brief Get service from the mapper. */
Brad Bishopd1eac882018-03-29 10:34:05 -040091static auto getService(::sdbusplus::bus::bus& bus, const std::string& path,
92 const std::string& interface)
Brad Bishopb1e329a2017-08-02 01:23:12 -040093{
94 using namespace std::literals::string_literals;
95 using GetObject = std::map<std::string, std::vector<std::string>>;
96
97 auto mapperResp = callMethodAndRead<GetObject>(
Brad Bishopd1eac882018-03-29 10:34:05 -040098 bus, "xyz.openbmc_project.ObjectMapper"s,
99 "/xyz/openbmc_project/object_mapper"s,
100 "xyz.openbmc_project.ObjectMapper"s, "GetObject"s, path,
101 GetObject::mapped_type{interface});
Brad Bishopb1e329a2017-08-02 01:23:12 -0400102
103 if (mapperResp.empty())
104 {
George Liu13e3df62022-06-22 15:03:06 +0800105 lg2::error("Object not found. {PATH}, {INTF}", "PATH", path, "INTF",
106 interface);
Brad Bishopb1e329a2017-08-02 01:23:12 -0400107 phosphor::logging::elog<detail::errors::InternalFailure>();
108 }
109 return mapperResp.begin()->first;
110}
111
112/** @brief Get a property without mapper lookup. */
113template <typename Property>
Brad Bishopd1eac882018-03-29 10:34:05 -0400114static auto getProperty(::sdbusplus::bus::bus& bus, const std::string& busName,
115 const std::string& path, const std::string& interface,
116 const std::string& property)
Brad Bishopb1e329a2017-08-02 01:23:12 -0400117{
118 using namespace std::literals::string_literals;
119
Brad Bishopd1eac882018-03-29 10:34:05 -0400120 auto msg =
121 callMethod(bus, busName, path, "org.freedesktop.DBus.Properties"s,
122 "Get"s, interface, property);
Patrick Williams35b4f332020-05-13 17:53:09 -0500123 ::std::variant<Property> value;
Brad Bishopb1e329a2017-08-02 01:23:12 -0400124 msg.read(value);
Patrick Williams34ef1e52020-05-13 11:07:43 -0500125 return std::get<Property>(value);
Brad Bishopb1e329a2017-08-02 01:23:12 -0400126}
127
128/** @brief Get a property without mapper lookup. */
129template <typename Property>
Brad Bishopd1eac882018-03-29 10:34:05 -0400130static auto getProperty(const std::string& busName, const std::string& path,
131 const std::string& interface,
132 const std::string& property)
Brad Bishopb1e329a2017-08-02 01:23:12 -0400133{
Brad Bishopd1eac882018-03-29 10:34:05 -0400134 return getProperty<Property>(getBus(), busName, path, interface, property);
Brad Bishopb1e329a2017-08-02 01:23:12 -0400135}
136
137/** @brief Get a property with mapper lookup. */
138template <typename Property>
Brad Bishopd1eac882018-03-29 10:34:05 -0400139static auto getProperty(::sdbusplus::bus::bus& bus, const std::string& path,
140 const std::string& interface,
141 const std::string& property)
Brad Bishopb1e329a2017-08-02 01:23:12 -0400142{
Brad Bishopd1eac882018-03-29 10:34:05 -0400143 return getProperty<Property>(bus, getService(bus, path, interface), path,
144 interface, property);
Brad Bishopb1e329a2017-08-02 01:23:12 -0400145}
146
147/** @brief Get a property with mapper lookup. */
148template <typename Property>
Brad Bishopd1eac882018-03-29 10:34:05 -0400149static auto getProperty(const std::string& path, const std::string& interface,
150 const std::string& property)
Brad Bishopb1e329a2017-08-02 01:23:12 -0400151{
Brad Bishopd1eac882018-03-29 10:34:05 -0400152 return getProperty<Property>(getBus(), path, interface, property);
Brad Bishopb1e329a2017-08-02 01:23:12 -0400153}
154
155} // namespace sdbusplus
156} // namespace util