blob: 033a47f269f3284a945a4919c4075fccd0df46fa [file] [log] [blame]
Willam A. Kennington IIIfc73b392022-09-07 15:00:21 -07001#include <systemd/sd-bus.h>
2
3#include <sdbusplus/bus/match.hpp>
4#include <sdbusplus/exception.hpp>
5#include <sdbusplus/message.hpp>
6
7namespace sdbusplus::bus::match
8{
9
William A. Kennington III9d8ba942023-07-13 15:13:51 -070010static slot_t makeMatch(SdBusInterface* intf, sd_bus* bus, const char* _match,
11 sd_bus_message_handler_t handler, void* context)
Willam A. Kennington IIIfc73b392022-09-07 15:00:21 -070012{
13 sd_bus_slot* slot;
William A. Kennington III9d8ba942023-07-13 15:13:51 -070014 int r = intf->sd_bus_add_match(bus, &slot, _match, handler, context);
Willam A. Kennington IIIfc73b392022-09-07 15:00:21 -070015 if (r < 0)
16 {
17 throw exception::SdBusError(-r, "sd_bus_match");
18 }
William A. Kennington III9d8ba942023-07-13 15:13:51 -070019 return slot_t{slot, intf};
Willam A. Kennington IIIfc73b392022-09-07 15:00:21 -070020}
21
22match::match(sdbusplus::bus_t& bus, const char* _match,
23 sd_bus_message_handler_t handler, void* context) :
William A. Kennington III9d8ba942023-07-13 15:13:51 -070024 _slot(
25 makeMatch(bus.getInterface(), get_busp(bus), _match, handler, context))
Willam A. Kennington IIIfc73b392022-09-07 15:00:21 -070026{}
27
28// The callback is 'noexcept' because it is called from C code (sd-bus).
29static 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
38match::match(sdbusplus::bus_t& bus, const char* _match, callback_t callback) :
39 _callback(std::make_unique<callback_t>(std::move(callback))),
William A. Kennington III9d8ba942023-07-13 15:13:51 -070040 _slot(makeMatch(bus.getInterface(), get_busp(bus), _match, matchCallback,
41 _callback.get()))
Willam A. Kennington IIIfc73b392022-09-07 15:00:21 -070042{}
43
44} // namespace sdbusplus::bus::match