xref: /openbmc/sdbusplus/src/bus/match.cpp (revision 9d8ba947)
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 
makeMatch(SdBusInterface * intf,sd_bus * bus,const char * _match,sd_bus_message_handler_t handler,void * context)10 static slot_t makeMatch(SdBusInterface* intf, sd_bus* bus, const char* _match,
11                         sd_bus_message_handler_t handler, void* context)
12 {
13     sd_bus_slot* slot;
14     int r = intf->sd_bus_add_match(bus, &slot, _match, handler, context);
15     if (r < 0)
16     {
17         throw exception::SdBusError(-r, "sd_bus_match");
18     }
19     return slot_t{slot, intf};
20 }
21 
match(sdbusplus::bus_t & bus,const char * _match,sd_bus_message_handler_t handler,void * context)22 match::match(sdbusplus::bus_t& bus, const char* _match,
23              sd_bus_message_handler_t handler, void* context) :
24     _slot(
25         makeMatch(bus.getInterface(), get_busp(bus), _match, handler, context))
26 {}
27 
28 // The callback is 'noexcept' because it is called from C code (sd-bus).
matchCallback(sd_bus_message * m,void * context,sd_bus_error *)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 
match(sdbusplus::bus_t & bus,const char * _match,callback_t callback)38 match::match(sdbusplus::bus_t& bus, const char* _match, callback_t callback) :
39     _callback(std::make_unique<callback_t>(std::move(callback))),
40     _slot(makeMatch(bus.getInterface(), get_busp(bus), _match, matchCallback,
41                     _callback.get()))
42 {}
43 
44 } // namespace sdbusplus::bus::match
45