]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-bus: add helper struct for interface definitions
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 15 Apr 2020 15:21:23 +0000 (17:21 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 5 May 2020 20:27:03 +0000 (22:27 +0200)
The idea is to have a static table that defines the dbus API. The vtable is
defined right next to the interface name and path because they are logically
connected.

src/shared/bus-log-control-api.c
src/shared/bus-log-control-api.h
src/shared/bus-util.c
src/shared/bus-util.h

index ffc52c77745e6b434be8df55776cd423f3959abe..a1d0d1729151d8a9ef72330dc8d049014f0c93c4 100644 (file)
@@ -107,17 +107,8 @@ static const sd_bus_vtable log_control_vtable[] = {
         SD_BUS_VTABLE_END,
 };
 
-int bus_log_control_api_register(sd_bus *bus) {
-        int r;
-
-        r = sd_bus_add_object_vtable(
-                        bus,
-                        NULL,
-                        "/org/freedesktop/LogControl1",
-                        "org.freedesktop.LogControl1",
-                        log_control_vtable, NULL);
-        if (r < 0)
-                return log_error_errno(r, "Failed to register service API object: %m");
-
-        return 0;
-}
+const BusObjectImplementation log_control_object = {
+        "/org/freedesktop/LogControl1",
+        "org.freedesktop.LogControl1",
+        .vtables = BUS_VTABLES(log_control_vtable),
+};
index a6fb2757c5f89d4b76a5b9159601eda1c958928d..b1d00b38bcfcdd1d08e7b1cde7cd83200bca2f67 100644 (file)
@@ -2,7 +2,12 @@
 
 #include "sd-bus.h"
 
-int bus_log_control_api_register(sd_bus *bus);
+#include "bus-util.h"
+
+extern const BusObjectImplementation log_control_object;
+static inline int bus_log_control_api_register(sd_bus *bus) {
+        return bus_add_implementation(bus, &log_control_object, NULL);
+}
 
 int bus_property_get_log_level(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error);
 int bus_property_set_log_level(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *value, void *userdata, sd_bus_error *error);
index 81683e5afed1eabbf2fb142fb25321c858e665e7..30d62d5fc7780cb94435b07ab90c7839607a1e30 100644 (file)
@@ -1549,3 +1549,52 @@ int bus_message_new_method_call(
 
         return sd_bus_message_new_method_call(bus, m, locator->destination, locator->path, locator->interface, member);
 }
+
+int bus_add_implementation(sd_bus *bus, const BusObjectImplementation *impl, void *userdata) {
+        int r;
+
+        log_debug("Registering bus object implementation for path=%s iface=%s", impl->path, impl->interface);
+
+        for (const sd_bus_vtable **p = impl->vtables; p && *p; p++) {
+                r = sd_bus_add_object_vtable(bus, NULL,
+                                             impl->path,
+                                             impl->interface,
+                                             *p,
+                                             userdata);
+                if (r < 0)
+                        return log_error_errno(r, "Failed to register bus path %s with interface %s: %m",
+                                               impl->path,
+                                               impl->interface);
+        }
+
+        for (const BusObjectVtablePair *p = impl->fallback_vtables; p && p->vtable; p++) {
+                r = sd_bus_add_fallback_vtable(bus, NULL,
+                                               impl->path,
+                                               impl->interface,
+                                               p->vtable,
+                                               p->object_find,
+                                               userdata);
+                if (r < 0)
+                        return log_error_errno(r, "Failed to register bus path %s with interface %s: %m",
+                                               impl->path,
+                                               impl->interface);
+        }
+
+        if (impl->node_enumerator) {
+                r = sd_bus_add_node_enumerator(bus, NULL,
+                                               impl->path,
+                                               impl->node_enumerator,
+                                               userdata);
+                if (r < 0)
+                        return log_error_errno(r, "Failed to add node enumerator for %s: %m",
+                                               impl->path);
+        }
+
+        for (size_t i = 0; impl->children && impl->children[i]; i++) {
+                r = bus_add_implementation(bus, impl->children[i], userdata);
+                if (r < 0)
+                        return r;
+        }
+
+        return 0;
+}
index fd1820ca7e60fceb992b15a1ae3e3df0f5b64221..a985523cd01711fb550496d23e69b07cde3b14e7 100644 (file)
@@ -23,11 +23,32 @@ typedef enum BusTransport {
 } BusTransport;
 
 typedef struct BusLocator {
-        const char    *destination;
-        const char    *path;
-        const char    *interface;
+        const char *destination;
+        const char *path;
+        const char *interface;
 } BusLocator;
 
+typedef struct BusObjectImplementation BusObjectImplementation;
+
+typedef struct BusObjectVtablePair {
+        const sd_bus_vtable *vtable;
+        sd_bus_object_find_t object_find;
+} BusObjectVtablePair;
+
+struct BusObjectImplementation {
+        const char *path;
+        const char *interface;
+        const sd_bus_vtable **vtables;
+        const BusObjectVtablePair *fallback_vtables;
+        sd_bus_node_enumerator_t node_enumerator;
+        bool manager;
+        const BusObjectImplementation **children;
+};
+
+#define BUS_VTABLES(...) ((const sd_bus_vtable* []){ __VA_ARGS__, NULL })
+#define BUS_FALLBACK_VTABLES(...) ((const BusObjectVtablePair[]) { __VA_ARGS__, {} })
+#define BUS_IMPLEMENTATIONS(...) ((const BusObjectImplementation* []) { __VA_ARGS__, NULL })
+
 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 {
@@ -199,3 +220,5 @@ int bus_set_property(sd_bus *bus, const BusLocator *locator, const char *member,
 int bus_match_signal(sd_bus *bus, sd_bus_slot **ret, const BusLocator *locator, const char *member, sd_bus_message_handler_t callback, void *userdata);
 int bus_match_signal_async(sd_bus *bus, sd_bus_slot **ret, const BusLocator *locator, const char *member, sd_bus_message_handler_t callback, sd_bus_message_handler_t install_callback, void *userdata);
 int bus_message_new_method_call(sd_bus *bus, sd_bus_message **m, const BusLocator *locator, const char *member);
+
+int bus_add_implementation(sd_bus *bus, const BusObjectImplementation *impl, void *userdata);