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 | |
Alexander Soldatov | 12789e6 | 2018-11-23 10:40:40 +0300 | [diff] [blame] | 5 | #include <phosphor-logging/log.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 | |
Alexander Soldatov | 12789e6 | 2018-11-23 10:40:40 +0300 | [diff] [blame] | 20 | using namespace phosphor::logging; |
| 21 | |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 22 | /** @class CallDBusMethod |
| 23 | * @brief Provide explicit call forwarding to |
| 24 | * DBusInterface::callMethodNoReply. |
| 25 | * |
| 26 | * @tparam DBusInterface - The DBus interface to use. |
| 27 | * @tparam MethodArgs - DBus method argument types. |
| 28 | */ |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 29 | template <typename DBusInterface, typename... MethodArgs> |
| 30 | struct CallDBusMethod |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 31 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 32 | static void op(const std::string& bus, const std::string& path, |
| 33 | const std::string& iface, const std::string& method, |
| 34 | MethodArgs&&... args) |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 35 | { |
Alexander Soldatov | 12789e6 | 2018-11-23 10:40:40 +0300 | [diff] [blame] | 36 | try |
| 37 | { |
| 38 | DBusInterface::callMethodNoReply(bus, path, iface, method, |
| 39 | std::forward<MethodArgs>(args)...); |
| 40 | } |
Patrick Williams | 764adb5 | 2021-09-02 09:36:47 -0500 | [diff] [blame] | 41 | catch (const sdbusplus::exception::exception& e) |
Alexander Soldatov | 12789e6 | 2018-11-23 10:40:40 +0300 | [diff] [blame] | 42 | { |
| 43 | // clang-format off |
| 44 | log<level::ERR>("Unable to call DBus method", |
| 45 | entry("BUS=%s", bus.c_str(), |
| 46 | "PATH=%s", path.c_str(), |
| 47 | "IFACE=%s", iface.c_str(), |
| 48 | "METHOD=%s", method.c_str(), |
| 49 | "ERROR=%s", e.what())); |
| 50 | // clang-format on |
| 51 | } |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 52 | } |
| 53 | }; |
| 54 | } // namespace detail |
| 55 | |
| 56 | /** @class MethodBase |
| 57 | * @brief Invoke DBus method callback implementation. |
| 58 | * |
| 59 | * The method callback invokes the client supplied DBus method. |
| 60 | */ |
| 61 | class MethodBase : public Callback |
| 62 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 63 | public: |
| 64 | MethodBase() = delete; |
| 65 | MethodBase(const MethodBase&) = delete; |
| 66 | MethodBase(MethodBase&&) = default; |
| 67 | MethodBase& operator=(const MethodBase&) = delete; |
| 68 | MethodBase& operator=(MethodBase&&) = default; |
| 69 | virtual ~MethodBase() = default; |
| 70 | MethodBase(const std::string& b, const std::string& p, const std::string& i, |
| 71 | const std::string& m) : |
| 72 | Callback(), |
| 73 | bus(b), path(p), interface(i), method(m) |
George Liu | 3fe976c | 2022-06-21 09:37:04 +0800 | [diff] [blame] | 74 | {} |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 75 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 76 | /** @brief Callback interface implementation. */ |
| 77 | void operator()(Context ctx) override = 0; |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 78 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 79 | protected: |
| 80 | const std::string& bus; |
| 81 | const std::string& path; |
| 82 | const std::string& interface; |
| 83 | const std::string& method; |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | /** @class Method |
| 87 | * @brief C++ type specific logic for the method callback. |
| 88 | * |
| 89 | * @tparam DBusInterface - The DBus interface to use to call the method. |
| 90 | * @tparam MethodArgs - DBus method argument types. |
| 91 | */ |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 92 | template <typename DBusInterface, typename... MethodArgs> |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 93 | class Method : public MethodBase |
| 94 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 95 | public: |
| 96 | Method() = delete; |
| 97 | Method(const Method&) = default; |
| 98 | Method(Method&&) = default; |
| 99 | Method& operator=(const Method&) = default; |
| 100 | Method& operator=(Method&&) = default; |
| 101 | ~Method() = default; |
| 102 | Method(const std::string& bus, const std::string& path, |
| 103 | const std::string& iface, const std::string& method, |
| 104 | MethodArgs&&... arguments) : |
| 105 | MethodBase(bus, path, iface, method), |
| 106 | args(std::forward<MethodArgs>(arguments)...) |
George Liu | 3fe976c | 2022-06-21 09:37:04 +0800 | [diff] [blame] | 107 | {} |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 108 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 109 | /** @brief Callback interface implementation. */ |
George Liu | 5e6b51d | 2022-06-21 16:59:39 +0800 | [diff] [blame^] | 110 | void operator()(Context /* ctx */) override |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 111 | { |
Patrick Williams | 26dc0bc | 2022-06-16 17:06:18 -0500 | [diff] [blame] | 112 | std::apply(detail::CallDBusMethod<DBusInterface, MethodArgs...>::op, |
| 113 | std::tuple_cat(std::make_tuple(bus), std::make_tuple(path), |
| 114 | std::make_tuple(interface), |
| 115 | std::make_tuple(method), args)); |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 116 | } |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 117 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 118 | private: |
| 119 | std::tuple<MethodArgs...> args; |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | /** @brief Argument type deduction for constructing Method instances. */ |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 123 | template <typename DBusInterface, typename... MethodArgs> |
| 124 | auto makeMethod(const std::string& bus, const std::string& path, |
| 125 | const std::string& iface, const std::string& method, |
| 126 | MethodArgs&&... arguments) |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 127 | { |
| 128 | return std::make_unique<Method<DBusInterface, MethodArgs...>>( |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 129 | bus, path, iface, method, std::forward<MethodArgs>(arguments)...); |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | } // namespace monitoring |
| 133 | } // namespace dbus |
| 134 | } // namespace phosphor |