Willam A. Kennington III | fc73b39 | 2022-09-07 15:00:21 -0700 | [diff] [blame] | 1 | #include <systemd/sd-bus.h> |
| 2 | |
| 3 | #include <sdbusplus/bus/match.hpp> |
| 4 | #include <sdbusplus/exception.hpp> |
| 5 | #include <sdbusplus/message.hpp> |
| 6 | |
| 7 | namespace sdbusplus::bus::match |
| 8 | { |
| 9 | |
William A. Kennington III | 9d8ba94 | 2023-07-13 15:13:51 -0700 | [diff] [blame] | 10 | static slot_t makeMatch(SdBusInterface* intf, sd_bus* bus, const char* _match, |
| 11 | sd_bus_message_handler_t handler, void* context) |
Willam A. Kennington III | fc73b39 | 2022-09-07 15:00:21 -0700 | [diff] [blame] | 12 | { |
| 13 | sd_bus_slot* slot; |
William A. Kennington III | 9d8ba94 | 2023-07-13 15:13:51 -0700 | [diff] [blame] | 14 | int r = intf->sd_bus_add_match(bus, &slot, _match, handler, context); |
Willam A. Kennington III | fc73b39 | 2022-09-07 15:00:21 -0700 | [diff] [blame] | 15 | if (r < 0) |
| 16 | { |
| 17 | throw exception::SdBusError(-r, "sd_bus_match"); |
| 18 | } |
William A. Kennington III | 9d8ba94 | 2023-07-13 15:13:51 -0700 | [diff] [blame] | 19 | return slot_t{slot, intf}; |
Willam A. Kennington III | fc73b39 | 2022-09-07 15:00:21 -0700 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | match::match(sdbusplus::bus_t& bus, const char* _match, |
| 23 | sd_bus_message_handler_t handler, void* context) : |
William A. Kennington III | 9d8ba94 | 2023-07-13 15:13:51 -0700 | [diff] [blame] | 24 | _slot( |
| 25 | makeMatch(bus.getInterface(), get_busp(bus), _match, handler, context)) |
Willam A. Kennington III | fc73b39 | 2022-09-07 15:00:21 -0700 | [diff] [blame] | 26 | {} |
| 27 | |
| 28 | // The callback is 'noexcept' because it is called from C code (sd-bus). |
| 29 | static int matchCallback(sd_bus_message* m, void* context, |
| 30 | sd_bus_error* /*e*/) noexcept |
| 31 | { |
| 32 | auto c = static_cast<match::callback_t*>(context); |
| 33 | message_t message{m}; |
| 34 | (*c)(message); |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | match::match(sdbusplus::bus_t& bus, const char* _match, callback_t callback) : |
| 39 | _callback(std::make_unique<callback_t>(std::move(callback))), |
William A. Kennington III | 9d8ba94 | 2023-07-13 15:13:51 -0700 | [diff] [blame] | 40 | _slot(makeMatch(bus.getInterface(), get_busp(bus), _match, matchCallback, |
| 41 | _callback.get())) |
Willam A. Kennington III | fc73b39 | 2022-09-07 15:00:21 -0700 | [diff] [blame] | 42 | {} |
| 43 | |
| 44 | } // namespace sdbusplus::bus::match |