Patrick Williams | ebd6b9c | 2016-10-18 22:14:12 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Williams | ebd6b9c | 2016-10-18 22:14:12 -0500 | [diff] [blame] | 3 | #include <sdbusplus/bus.hpp> |
Patrick Williams | 2f7c887 | 2017-05-01 16:27:57 -0500 | [diff] [blame] | 4 | #include <sdbusplus/message.hpp> |
Patrick Venture | 95269db | 2018-08-31 09:19:17 -0700 | [diff] [blame] | 5 | #include <sdbusplus/slot.hpp> |
Patrick Williams | 127b8ab | 2020-05-21 15:24:19 -0500 | [diff] [blame] | 6 | |
| 7 | #include <functional> |
| 8 | #include <memory> |
Patrick Venture | 95269db | 2018-08-31 09:19:17 -0700 | [diff] [blame] | 9 | #include <string> |
Patrick Williams | ebd6b9c | 2016-10-18 22:14:12 -0500 | [diff] [blame] | 10 | |
| 11 | namespace sdbusplus |
| 12 | { |
| 13 | |
Christian Andersen | c69def6 | 2016-12-20 13:51:52 +0100 | [diff] [blame] | 14 | namespace bus |
Patrick Williams | ebd6b9c | 2016-10-18 22:14:12 -0500 | [diff] [blame] | 15 | { |
| 16 | |
| 17 | namespace match |
| 18 | { |
| 19 | |
| 20 | struct match |
| 21 | { |
Christian Andersen | c69def6 | 2016-12-20 13:51:52 +0100 | [diff] [blame] | 22 | /* Define all of the basic class operations: |
| 23 | * Not allowed: |
| 24 | * - Default constructor to avoid nullptrs. |
| 25 | * - Copy operations due to internal unique_ptr. |
| 26 | * Allowed: |
| 27 | * - Move operations. |
| 28 | * - Destructor. |
| 29 | */ |
Patrick Williams | ebd6b9c | 2016-10-18 22:14:12 -0500 | [diff] [blame] | 30 | match() = delete; |
| 31 | match(const match&) = delete; |
| 32 | match& operator=(const match&) = delete; |
| 33 | match(match&&) = default; |
| 34 | match& operator=(match&&) = default; |
| 35 | ~match() = default; |
| 36 | |
| 37 | /** @brief Register a signal match. |
| 38 | * |
| 39 | * @param[in] bus - The bus to register on. |
| 40 | * @param[in] match - The match to register. |
| 41 | * @param[in] handler - The callback for matches. |
Patrick Williams | b12ca16 | 2016-10-19 10:38:19 -0500 | [diff] [blame] | 42 | * @param[in] context - An optional context to pass to the handler. |
Patrick Williams | ebd6b9c | 2016-10-18 22:14:12 -0500 | [diff] [blame] | 43 | */ |
| 44 | match(sdbusplus::bus::bus& bus, const char* match, |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 45 | sd_bus_message_handler_t handler, void* context = nullptr) : |
| 46 | _slot(nullptr) |
Patrick Williams | ebd6b9c | 2016-10-18 22:14:12 -0500 | [diff] [blame] | 47 | { |
| 48 | sd_bus_slot* slot = nullptr; |
Patrick Williams | b12ca16 | 2016-10-19 10:38:19 -0500 | [diff] [blame] | 49 | sd_bus_add_match(bus.get(), &slot, match, handler, context); |
Patrick Williams | ebd6b9c | 2016-10-18 22:14:12 -0500 | [diff] [blame] | 50 | |
| 51 | _slot = decltype(_slot){slot}; |
| 52 | } |
Patrick Williams | da25f34 | 2017-05-01 18:08:16 -0500 | [diff] [blame] | 53 | match(sdbusplus::bus::bus& bus, const std::string& _match, |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 54 | sd_bus_message_handler_t handler, void* context = nullptr) : |
| 55 | match(bus, _match.c_str(), handler, context) |
Patrick Williams | 127b8ab | 2020-05-21 15:24:19 -0500 | [diff] [blame] | 56 | {} |
Patrick Williams | ebd6b9c | 2016-10-18 22:14:12 -0500 | [diff] [blame] | 57 | |
Patrick Williams | 2f7c887 | 2017-05-01 16:27:57 -0500 | [diff] [blame] | 58 | using callback_t = std::function<void(sdbusplus::message::message&)>; |
| 59 | |
| 60 | /** @brief Register a signal match. |
| 61 | * |
| 62 | * @param[in] bus - The bus to register on. |
| 63 | * @param[in] match - The match to register. |
| 64 | * @param[in] callback - The callback for matches. |
| 65 | */ |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 66 | match(sdbusplus::bus::bus& bus, const char* match, callback_t callback) : |
| 67 | _slot(nullptr), |
| 68 | _callback(std::make_unique<callback_t>(std::move(callback))) |
Patrick Williams | 2f7c887 | 2017-05-01 16:27:57 -0500 | [diff] [blame] | 69 | { |
| 70 | sd_bus_slot* slot = nullptr; |
| 71 | sd_bus_add_match(bus.get(), &slot, match, callCallback, |
| 72 | _callback.get()); |
| 73 | |
| 74 | _slot = decltype(_slot){slot}; |
| 75 | } |
Patrick Williams | da25f34 | 2017-05-01 18:08:16 -0500 | [diff] [blame] | 76 | match(sdbusplus::bus::bus& bus, const std::string& _match, |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 77 | callback_t callback) : |
| 78 | match(bus, _match.c_str(), callback) |
Patrick Williams | 127b8ab | 2020-05-21 15:24:19 -0500 | [diff] [blame] | 79 | {} |
Patrick Williams | 2f7c887 | 2017-05-01 16:27:57 -0500 | [diff] [blame] | 80 | |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 81 | private: |
| 82 | slot::slot _slot; |
| 83 | std::unique_ptr<callback_t> _callback = nullptr; |
Patrick Williams | 2f7c887 | 2017-05-01 16:27:57 -0500 | [diff] [blame] | 84 | |
Patrick Williams | 78b7803 | 2020-05-20 10:32:05 -0500 | [diff] [blame] | 85 | static int callCallback(sd_bus_message* m, void* context, |
| 86 | sd_bus_error* /*e*/) |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 87 | { |
| 88 | auto c = static_cast<callback_t*>(context); |
| 89 | message::message message{m}; |
Patrick Williams | 2f7c887 | 2017-05-01 16:27:57 -0500 | [diff] [blame] | 90 | |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 91 | (*c)(message); |
Patrick Williams | 2f7c887 | 2017-05-01 16:27:57 -0500 | [diff] [blame] | 92 | |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 93 | return 0; |
| 94 | } |
Patrick Williams | ebd6b9c | 2016-10-18 22:14:12 -0500 | [diff] [blame] | 95 | }; |
| 96 | |
Patrick Williams | a85fdff | 2017-05-01 17:52:15 -0500 | [diff] [blame] | 97 | /** Utilities for defining match rules based on the DBus specification */ |
| 98 | namespace rules |
| 99 | { |
| 100 | |
| 101 | using namespace std::string_literals; |
| 102 | |
| 103 | namespace type |
| 104 | { |
| 105 | |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 106 | inline auto signal() |
| 107 | { |
| 108 | return "type='signal',"s; |
| 109 | } |
| 110 | inline auto method() |
| 111 | { |
| 112 | return "type='method',"s; |
| 113 | } |
| 114 | inline auto method_return() |
| 115 | { |
| 116 | return "type='method_return',"s; |
| 117 | } |
| 118 | inline auto error() |
| 119 | { |
| 120 | return "type='error',"s; |
| 121 | } |
Patrick Williams | a85fdff | 2017-05-01 17:52:15 -0500 | [diff] [blame] | 122 | |
| 123 | } // namespace type |
| 124 | |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 125 | inline auto sender(const std::string& s) |
| 126 | { |
| 127 | return "sender='"s + s + "',"; |
| 128 | } |
Patrick Williams | a85fdff | 2017-05-01 17:52:15 -0500 | [diff] [blame] | 129 | inline auto interface(const std::string& s) |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 130 | { |
| 131 | return "interface='"s + s + "',"; |
| 132 | } |
| 133 | inline auto member(const std::string& s) |
| 134 | { |
| 135 | return "member='"s + s + "',"; |
| 136 | } |
| 137 | inline auto path(const std::string& s) |
| 138 | { |
| 139 | return "path='"s + s + "',"; |
| 140 | } |
Patrick Williams | a85fdff | 2017-05-01 17:52:15 -0500 | [diff] [blame] | 141 | inline auto path_namespace(const std::string& s) |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 142 | { |
| 143 | return "path_namespace='"s + s + "',"; |
| 144 | } |
Patrick Williams | a85fdff | 2017-05-01 17:52:15 -0500 | [diff] [blame] | 145 | inline auto destination(const std::string& s) |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 146 | { |
| 147 | return "destination='"s + s + "',"; |
| 148 | } |
Patrick Williams | a85fdff | 2017-05-01 17:52:15 -0500 | [diff] [blame] | 149 | inline auto argN(size_t n, const std::string& s) |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 150 | { |
| 151 | return "arg"s + std::to_string(n) + "='"s + s + "',"; |
| 152 | } |
Patrick Williams | a85fdff | 2017-05-01 17:52:15 -0500 | [diff] [blame] | 153 | inline auto argNpath(size_t n, const std::string& s) |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 154 | { |
| 155 | return "arg"s + std::to_string(n) + "path='"s + s + "',"; |
| 156 | } |
Patrick Williams | a85fdff | 2017-05-01 17:52:15 -0500 | [diff] [blame] | 157 | inline auto arg0namespace(const std::string& s) |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 158 | { |
| 159 | return "arg0namespace='"s + s + "',"; |
| 160 | } |
| 161 | inline auto eavesdrop() |
| 162 | { |
| 163 | return "eavesdrop='true',"s; |
| 164 | } |
Patrick Williams | a85fdff | 2017-05-01 17:52:15 -0500 | [diff] [blame] | 165 | |
| 166 | inline auto nameOwnerChanged() |
| 167 | { |
| 168 | return "type='signal'," |
| 169 | "sender='org.freedesktop.DBus'," |
| 170 | "member='NameOwnerChanged',"s; |
| 171 | } |
| 172 | |
Patrick Williams | 5c48c8b | 2017-05-30 16:12:05 -0500 | [diff] [blame] | 173 | inline auto interfacesAdded() |
| 174 | { |
| 175 | return "type='signal'," |
| 176 | "interface='org.freedesktop.DBus.ObjectManager'," |
| 177 | "member='InterfacesAdded',"s; |
| 178 | } |
| 179 | |
Brad Bishop | 82f083d | 2017-06-19 16:15:08 -0400 | [diff] [blame] | 180 | inline auto interfacesRemoved() |
| 181 | { |
| 182 | return "type='signal'," |
| 183 | "interface='org.freedesktop.DBus.ObjectManager'," |
| 184 | "member='InterfacesRemoved',"s; |
| 185 | } |
| 186 | |
| 187 | inline auto interfacesAdded(const std::string& p) |
| 188 | { |
| 189 | return interfacesAdded() + path(p); |
| 190 | } |
| 191 | |
| 192 | inline auto interfacesRemoved(const std::string& p) |
| 193 | { |
| 194 | return interfacesRemoved() + path(p); |
| 195 | } |
| 196 | |
| 197 | inline auto propertiesChanged(const std::string& p, const std::string& i) |
| 198 | { |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 199 | return type::signal() + path(p) + member("PropertiesChanged"s) + |
| 200 | interface("org.freedesktop.DBus.Properties"s) + argN(0, i); |
Brad Bishop | 82f083d | 2017-06-19 16:15:08 -0400 | [diff] [blame] | 201 | } |
| 202 | |
Matthew Barth | a3c58a9 | 2017-11-14 13:43:21 -0600 | [diff] [blame] | 203 | /** |
| 204 | * @brief Constructs a NameOwnerChanged match string for a service name |
| 205 | * |
| 206 | * @param[in] s - Service name |
| 207 | * |
| 208 | * @return NameOwnerChanged match string for a service name |
| 209 | */ |
| 210 | inline auto nameOwnerChanged(const std::string& s) |
| 211 | { |
| 212 | return nameOwnerChanged() + argN(0, s); |
| 213 | } |
| 214 | |
Patrick Williams | a85fdff | 2017-05-01 17:52:15 -0500 | [diff] [blame] | 215 | } // namespace rules |
Patrick Williams | ebd6b9c | 2016-10-18 22:14:12 -0500 | [diff] [blame] | 216 | } // namespace match |
| 217 | |
| 218 | using match_t = match::match; |
| 219 | |
Christian Andersen | c69def6 | 2016-12-20 13:51:52 +0100 | [diff] [blame] | 220 | } // namespace bus |
Patrick Williams | ebd6b9c | 2016-10-18 22:14:12 -0500 | [diff] [blame] | 221 | } // namespace sdbusplus |