blob: 7190a88dd958027d3c61072abd9318c6f903c10a [file] [log] [blame]
Brad Bishop0df00be2017-05-25 23:38:37 -04001#pragma once
2
Brad Bishop0df00be2017-05-25 23:38:37 -04003#include "callback.hpp"
4
George Liu13e3df62022-06-22 15:03:06 +08005#include <phosphor-logging/lg2.hpp>
George Liu3fe976c2022-06-21 09:37:04 +08006
7#include <experimental/tuple>
Andrew Geisslerae4c95c2020-05-16 13:58:53 -05008#include <string>
Patrick Williams26dc0bc2022-06-16 17:06:18 -05009#include <tuple>
Patrick Venture3d6d3182018-08-31 09:33:09 -070010
Brad Bishop0df00be2017-05-25 23:38:37 -040011namespace phosphor
12{
13namespace dbus
14{
15namespace monitoring
16{
17namespace detail
18{
19
20/** @class CallDBusMethod
21 * @brief Provide explicit call forwarding to
22 * DBusInterface::callMethodNoReply.
23 *
24 * @tparam DBusInterface - The DBus interface to use.
25 * @tparam MethodArgs - DBus method argument types.
26 */
Patrick Venture3d6d3182018-08-31 09:33:09 -070027template <typename DBusInterface, typename... MethodArgs>
28struct CallDBusMethod
Brad Bishop0df00be2017-05-25 23:38:37 -040029{
Brad Bishopd1eac882018-03-29 10:34:05 -040030 static void op(const std::string& bus, const std::string& path,
31 const std::string& iface, const std::string& method,
32 MethodArgs&&... args)
Brad Bishop0df00be2017-05-25 23:38:37 -040033 {
Alexander Soldatov12789e62018-11-23 10:40:40 +030034 try
35 {
36 DBusInterface::callMethodNoReply(bus, path, iface, method,
37 std::forward<MethodArgs>(args)...);
38 }
Patrick Williams413a4852022-07-22 19:26:52 -050039 catch (const sdbusplus::exception_t& e)
Alexander Soldatov12789e62018-11-23 10:40:40 +030040 {
George Liu13e3df62022-06-22 15:03:06 +080041 lg2::error(
42 "Unable to call DBus method: {ERROR}. {BUS}, {PATH}, {INTF}, {METHOD}",
43 "ERROR", e, "BUS", bus, "PATH", path, "INTF", iface, "METHOD",
44 method);
Alexander Soldatov12789e62018-11-23 10:40:40 +030045 }
Brad Bishop0df00be2017-05-25 23:38:37 -040046 }
47};
48} // namespace detail
49
50/** @class MethodBase
51 * @brief Invoke DBus method callback implementation.
52 *
53 * The method callback invokes the client supplied DBus method.
54 */
55class MethodBase : public Callback
56{
Brad Bishopd1eac882018-03-29 10:34:05 -040057 public:
58 MethodBase() = delete;
59 MethodBase(const MethodBase&) = delete;
60 MethodBase(MethodBase&&) = default;
61 MethodBase& operator=(const MethodBase&) = delete;
62 MethodBase& operator=(MethodBase&&) = default;
63 virtual ~MethodBase() = default;
64 MethodBase(const std::string& b, const std::string& p, const std::string& i,
65 const std::string& m) :
66 Callback(),
67 bus(b), path(p), interface(i), method(m)
George Liu3fe976c2022-06-21 09:37:04 +080068 {}
Brad Bishop0df00be2017-05-25 23:38:37 -040069
Brad Bishopd1eac882018-03-29 10:34:05 -040070 /** @brief Callback interface implementation. */
71 void operator()(Context ctx) override = 0;
Brad Bishop0df00be2017-05-25 23:38:37 -040072
Brad Bishopd1eac882018-03-29 10:34:05 -040073 protected:
74 const std::string& bus;
75 const std::string& path;
76 const std::string& interface;
77 const std::string& method;
Brad Bishop0df00be2017-05-25 23:38:37 -040078};
79
80/** @class Method
81 * @brief C++ type specific logic for the method callback.
82 *
83 * @tparam DBusInterface - The DBus interface to use to call the method.
84 * @tparam MethodArgs - DBus method argument types.
85 */
Brad Bishopd1eac882018-03-29 10:34:05 -040086template <typename DBusInterface, typename... MethodArgs>
Brad Bishop0df00be2017-05-25 23:38:37 -040087class Method : public MethodBase
88{
Brad Bishopd1eac882018-03-29 10:34:05 -040089 public:
90 Method() = delete;
91 Method(const Method&) = default;
92 Method(Method&&) = default;
93 Method& operator=(const Method&) = default;
94 Method& operator=(Method&&) = default;
95 ~Method() = default;
96 Method(const std::string& bus, const std::string& path,
97 const std::string& iface, const std::string& method,
98 MethodArgs&&... arguments) :
99 MethodBase(bus, path, iface, method),
100 args(std::forward<MethodArgs>(arguments)...)
George Liu3fe976c2022-06-21 09:37:04 +0800101 {}
Brad Bishop0df00be2017-05-25 23:38:37 -0400102
Brad Bishopd1eac882018-03-29 10:34:05 -0400103 /** @brief Callback interface implementation. */
George Liu5e6b51d2022-06-21 16:59:39 +0800104 void operator()(Context /* ctx */) override
Brad Bishopd1eac882018-03-29 10:34:05 -0400105 {
Patrick Williams26dc0bc2022-06-16 17:06:18 -0500106 std::apply(detail::CallDBusMethod<DBusInterface, MethodArgs...>::op,
107 std::tuple_cat(std::make_tuple(bus), std::make_tuple(path),
108 std::make_tuple(interface),
109 std::make_tuple(method), args));
Brad Bishopd1eac882018-03-29 10:34:05 -0400110 }
Brad Bishop0df00be2017-05-25 23:38:37 -0400111
Brad Bishopd1eac882018-03-29 10:34:05 -0400112 private:
113 std::tuple<MethodArgs...> args;
Brad Bishop0df00be2017-05-25 23:38:37 -0400114};
115
116/** @brief Argument type deduction for constructing Method instances. */
Brad Bishopd1eac882018-03-29 10:34:05 -0400117template <typename DBusInterface, typename... MethodArgs>
118auto makeMethod(const std::string& bus, const std::string& path,
119 const std::string& iface, const std::string& method,
120 MethodArgs&&... arguments)
Brad Bishop0df00be2017-05-25 23:38:37 -0400121{
122 return std::make_unique<Method<DBusInterface, MethodArgs...>>(
Brad Bishopd1eac882018-03-29 10:34:05 -0400123 bus, path, iface, method, std::forward<MethodArgs>(arguments)...);
Brad Bishop0df00be2017-05-25 23:38:37 -0400124}
125
126} // namespace monitoring
127} // namespace dbus
128} // namespace phosphor