diff options
author | Vito Caputo <vcaputo@pengaru.com> | 2020-04-03 22:35:42 -0700 |
---|---|---|
committer | Vito Caputo <vcaputo@pengaru.com> | 2020-04-04 13:38:58 -0700 |
commit | 219ab1fbd00c7ee8636e4ae17e0996b5078d556d (patch) | |
tree | 982095d175ac079243965ec4121fc5c49f3676f0 /src | |
parent | 3d9489ee457a81c0efe0bac3ea899f7470ef506a (diff) | |
download | systemd-219ab1fbd00c7ee8636e4ae17e0996b5078d556d.tar.gz |
bus: introduce some sd-bus convenience helpers
Many of the convenience functions from sd-bus operate on verbose sets
of discrete strings for destination/path/interface/member.
For most callers, destination/path/interface are uniform, and just the
member is distinct.
This commit introduces a new struct encapsulating the
destination/path/interface pointers called BusAddress, and wrapper
functions which take a BusAddress* instead of three strings, and just
pass the encapsulated strings on to the sd-bus convenience functions.
Future commits will update call sites to use these helpers throwing
out a bunch of repetitious destination/path/interface strings littered
throughout the codebase, replacing them with some appropriately named
static structs passed by pointer to these new helpers.
Diffstat (limited to 'src')
-rw-r--r-- | src/shared/bus-util.c | 139 | ||||
-rw-r--r-- | src/shared/bus-util.h | 19 |
2 files changed, 158 insertions, 0 deletions
diff --git a/src/shared/bus-util.c b/src/shared/bus-util.c index 4b0a3a3e31..dbbd0dcae6 100644 --- a/src/shared/bus-util.c +++ b/src/shared/bus-util.c @@ -1390,3 +1390,142 @@ const struct hash_ops bus_message_hash_ops = { .compare = trivial_compare_func, .free_value = bus_message_unref_wrapper, }; + +/* Shorthand flavors of the sd-bus convenience helpers with destination,path,interface + * strings encapsulated within a single struct. + */ +int bus_call_method_async( + sd_bus *bus, + sd_bus_slot **slot, + const BusAddress *address, + const char *member, + sd_bus_message_handler_t callback, + void *userdata, + const char *types, ...) { + + va_list ap; + int r; + + assert(address); + + va_start(ap, types); + r = sd_bus_call_method_asyncv(bus, slot, address->destination, address->path, address->interface, member, callback, userdata, types, ap); + va_end(ap); + + return r; +} + +int bus_call_method( + sd_bus *bus, + const BusAddress *address, + const char *member, + sd_bus_error *error, + sd_bus_message **reply, + const char *types, ...) { + + va_list ap; + int r; + + assert(address); + + va_start(ap, types); + r = sd_bus_call_methodv(bus, address->destination, address->path, address->interface, member, error, reply, types, ap); + va_end(ap); + + return r; +} + +int bus_get_property( + sd_bus *bus, + const BusAddress *address, + const char *member, + sd_bus_error *error, + sd_bus_message **reply, + const char *type) { + + assert(address); + + return sd_bus_get_property(bus, address->destination, address->path, address->interface, member, error, reply, type); +} + +int bus_get_property_trivial( + sd_bus *bus, + const BusAddress *address, + const char *member, + sd_bus_error *error, + char type, void *ptr) { + + assert(address); + + return sd_bus_get_property_trivial(bus, address->destination, address->path, address->interface, member, error, type, ptr); +} + +int bus_get_property_string( + sd_bus *bus, + const BusAddress *address, + const char *member, + sd_bus_error *error, + char **ret) { + + assert(address); + + return sd_bus_get_property_string(bus, address->destination, address->path, address->interface, member, error, ret); +} + +int bus_get_property_strv( + sd_bus *bus, + const BusAddress *address, + const char *member, + sd_bus_error *error, + char ***ret) { + + assert(address); + + return sd_bus_get_property_strv(bus, address->destination, address->path, address->interface, member, error, ret); +} + +int bus_set_property( + sd_bus *bus, + const BusAddress *address, + const char *member, + sd_bus_error *error, + const char *type, ...) { + + va_list ap; + int r; + + assert(address); + + va_start(ap, type); + r = sd_bus_set_propertyv(bus, address->destination, address->path, address->interface, member, error, type, ap); + va_end(ap); + + return r; +} + +int bus_match_signal( + sd_bus *bus, + sd_bus_slot **ret, + const BusAddress *address, + const char *member, + sd_bus_message_handler_t callback, + void *userdata) { + + assert(address); + + return sd_bus_match_signal(bus, ret, address->destination, address->path, address->interface, member, callback, userdata); +} + +int bus_match_signal_async( + sd_bus *bus, + sd_bus_slot **ret, + const BusAddress *address, + const char *member, + sd_bus_message_handler_t callback, + sd_bus_message_handler_t install_callback, + void *userdata) { + + assert(address); + + return sd_bus_match_signal_async(bus, ret, address->destination, address->path, address->interface, member, callback, install_callback, userdata); +} diff --git a/src/shared/bus-util.h b/src/shared/bus-util.h index db245a791e..f3bdde41ae 100644 --- a/src/shared/bus-util.h +++ b/src/shared/bus-util.h @@ -22,6 +22,12 @@ typedef enum BusTransport { _BUS_TRANSPORT_INVALID = -1 } BusTransport; +typedef struct BusAddress { + const char *destination; + const char *path; + const char *interface; +} BusAddress; + typedef int (*bus_property_set_t) (sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata); struct bus_properties_map { @@ -179,3 +185,16 @@ static inline int bus_open_system_watch_bind(sd_bus **ret) { int bus_reply_pair_array(sd_bus_message *m, char **l); extern const struct hash_ops bus_message_hash_ops; + +/* Shorthand flavors of the sd-bus convenience helpers with destination,path,interface + * strings encapsulated within a single struct. + */ +int bus_call_method_async(sd_bus *bus, sd_bus_slot **slot, const BusAddress *address, const char *member, sd_bus_message_handler_t callback, void *userdata, const char *types, ...); +int bus_call_method(sd_bus *bus, const BusAddress *address, const char *member, sd_bus_error *error, sd_bus_message **reply, const char *types, ...); +int bus_get_property(sd_bus *bus, const BusAddress *address, const char *member, sd_bus_error *error, sd_bus_message **reply, const char *type); +int bus_get_property_trivial(sd_bus *bus, const BusAddress *address, const char *member, sd_bus_error *error, char type, void *ptr); +int bus_get_property_string(sd_bus *bus, const BusAddress *address, const char *member, sd_bus_error *error, char **ret); +int bus_get_property_strv(sd_bus *bus, const BusAddress *address, const char *member, sd_bus_error *error, char ***ret); +int bus_set_property(sd_bus *bus, const BusAddress *address, const char *member, sd_bus_error *error, const char *type, ...); +int bus_match_signal(sd_bus *bus, sd_bus_slot **ret, const BusAddress *address, const char *member, sd_bus_message_handler_t callback, void *userdata); +int bus_match_signal_async(sd_bus *bus, sd_bus_slot **ret, const BusAddress *address, const char *member, sd_bus_message_handler_t callback, sd_bus_message_handler_t install_callback, void *userdata); |