Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 3 | #include "callback.hpp" |
| 4 | |
George Liu | 13e3df6 | 2022-06-22 15:03:06 +0800 | [diff] [blame] | 5 | #include <phosphor-logging/lg2.hpp> |
George Liu | 3fe976c | 2022-06-21 09:37:04 +0800 | [diff] [blame] | 6 | |
| 7 | #include <experimental/tuple> |
Andrew Geissler | ae4c95c | 2020-05-16 13:58:53 -0500 | [diff] [blame] | 8 | #include <string> |
Patrick Williams | 26dc0bc | 2022-06-16 17:06:18 -0500 | [diff] [blame] | 9 | #include <tuple> |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 10 | |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 11 | namespace phosphor |
| 12 | { |
| 13 | namespace dbus |
| 14 | { |
| 15 | namespace monitoring |
| 16 | { |
| 17 | namespace 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 Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 27 | template <typename DBusInterface, typename... MethodArgs> |
| 28 | struct CallDBusMethod |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 29 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 30 | static void op(const std::string& bus, const std::string& path, |
| 31 | const std::string& iface, const std::string& method, |
| 32 | MethodArgs&&... args) |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 33 | { |
Alexander Soldatov | 12789e6 | 2018-11-23 10:40:40 +0300 | [diff] [blame] | 34 | try |
| 35 | { |
| 36 | DBusInterface::callMethodNoReply(bus, path, iface, method, |
| 37 | std::forward<MethodArgs>(args)...); |
| 38 | } |
Patrick Williams | 413a485 | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 39 | catch (const sdbusplus::exception_t& e) |
Alexander Soldatov | 12789e6 | 2018-11-23 10:40:40 +0300 | [diff] [blame] | 40 | { |
George Liu | 13e3df6 | 2022-06-22 15:03:06 +0800 | [diff] [blame] | 41 | 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 Soldatov | 12789e6 | 2018-11-23 10:40:40 +0300 | [diff] [blame] | 45 | } |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 46 | } |
| 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 | */ |
| 55 | class MethodBase : public Callback |
| 56 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 57 | 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 Liu | 3fe976c | 2022-06-21 09:37:04 +0800 | [diff] [blame] | 68 | {} |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 69 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 70 | /** @brief Callback interface implementation. */ |
| 71 | void operator()(Context ctx) override = 0; |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 72 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 73 | protected: |
| 74 | const std::string& bus; |
| 75 | const std::string& path; |
| 76 | const std::string& interface; |
| 77 | const std::string& method; |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 78 | }; |
| 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 Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 86 | template <typename DBusInterface, typename... MethodArgs> |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 87 | class Method : public MethodBase |
| 88 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 89 | 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 Liu | 3fe976c | 2022-06-21 09:37:04 +0800 | [diff] [blame] | 101 | {} |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 102 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 103 | /** @brief Callback interface implementation. */ |
George Liu | 5e6b51d | 2022-06-21 16:59:39 +0800 | [diff] [blame] | 104 | void operator()(Context /* ctx */) override |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 105 | { |
Patrick Williams | 26dc0bc | 2022-06-16 17:06:18 -0500 | [diff] [blame] | 106 | 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 Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 110 | } |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 111 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 112 | private: |
| 113 | std::tuple<MethodArgs...> args; |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | /** @brief Argument type deduction for constructing Method instances. */ |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 117 | template <typename DBusInterface, typename... MethodArgs> |
| 118 | auto makeMethod(const std::string& bus, const std::string& path, |
| 119 | const std::string& iface, const std::string& method, |
| 120 | MethodArgs&&... arguments) |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 121 | { |
| 122 | return std::make_unique<Method<DBusInterface, MethodArgs...>>( |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 123 | bus, path, iface, method, std::forward<MethodArgs>(arguments)...); |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | } // namespace monitoring |
| 127 | } // namespace dbus |
| 128 | } // namespace phosphor |