]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
bus: introduce some sd-bus convenience helpers 15331/head
authorVito Caputo <vcaputo@pengaru.com>
Sat, 4 Apr 2020 05:35:42 +0000 (22:35 -0700)
committerVito Caputo <vcaputo@pengaru.com>
Sat, 4 Apr 2020 20:38:58 +0000 (13:38 -0700)
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.

src/shared/bus-util.c
src/shared/bus-util.h

index 4b0a3a3e317e23863a37dbdcbf67309b8291f96a..dbbd0dcae6a6264b83b9e575958af0accc4f9d76 100644 (file)
@@ -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);
+}
index db245a791ea4fa4c21450e0394f41993e5f38dde..f3bdde41aea9917b3880e72a8ad81d83d20d5f0b 100644 (file)
@@ -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);