Путеводитель по Руководству Linux

  User  |  Syst  |  Libr  |  Device  |  Files  |  Other  |  Admin  |  Head  |



   sd_bus_add_fallback    ( 3 )

объявление свойств и методов для пути D-Bus (declare properties and methods for a D-Bus path)

  Name  |  Synopsis  |  Description  |    Examples    |  Return value  |  Note  |  See also  |

Примеры (Examples)

Example 1. Create a simple listener on the bus

#include <errno.h> #include <stdbool.h> #include <stddef.h> #include <stdlib.h> #include <stdio.h> #include <systemd/sd-bus.h>

#define _cleanup_(f) __attribute__((cleanup(f)))

typedef struct object { char *name; uint32_t number; } object;

static int method(sd_bus_message *m, void *userdata, sd_bus_error *error) { printf("Got called with userdata=%p\n", userdata); return 1; }

static const sd_bus_vtable vtable[] = { SD_BUS_VTABLE_START(0), SD_BUS_METHOD( "Method1", "s", "s", method, 0), SD_BUS_METHOD_WITH_NAMES_OFFSET( "Method2", "so", SD_BUS_PARAM(string) SD_BUS_PARAM(path), "s", SD_BUS_PARAM(returnstring), method, offsetof(object, number), SD_BUS_VTABLE_DEPRECATED), SD_BUS_METHOD_WITH_ARGS_OFFSET( "Method3", SD_BUS_ARGS("s", string, "o", path), SD_BUS_RESULT("s", returnstring), method, offsetof(object, number), SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD_WITH_ARGS( "Method4", SD_BUS_NO_ARGS, SD_BUS_NO_RESULT, method, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_SIGNAL( "Signal1", "so", 0), SD_BUS_SIGNAL_WITH_NAMES( "Signal2", "so", SD_BUS_PARAM(string) SD_BUS_PARAM(path), 0), SD_BUS_SIGNAL_WITH_ARGS( "Signal3", SD_BUS_ARGS("s", string, "o", path), 0), SD_BUS_WRITABLE_PROPERTY( "AutomaticStringProperty", "s", NULL, NULL, offsetof(object, name), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), SD_BUS_WRITABLE_PROPERTY( "AutomaticIntegerProperty", "u", NULL, NULL, offsetof(object, number), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION), SD_BUS_VTABLE_END };

#define check(x) ({ \ int r = x; \ errno = r < 0 ? -r : 0; \ printf(#x ": %m\n"); \ if (r < 0) \ return EXIT_FAILURE; \ })

int main(int argc, char **argv) { _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;

sd_bus_default(&bus);

object object = { .number = 666 }; check((object.name = strdup("name")) != NULL);

check(sd_bus_add_object_vtable(bus, NULL, "/object", "org.freedesktop.systemd.VtableExample", vtable, &object));

for (;;) { check(sd_bus_wait(bus, UINT64_MAX)); check(sd_bus_process(bus, NULL)); }

free(object.name);

return 0; }

This creates a simple client on the bus (the user bus, when run as normal user). We may use the D-Bus org.freedesktop.DBus.Introspectable.Introspect call to acquire the XML description of the interface:

<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd"> <node> <interface name="org.freedesktop.DBus.Peer"> <method name="Ping"/> <method name="GetMachineId"> <arg type="s" name="machine_uuid" direction="out"/> </method> </interface> <interface name="org.freedesktop.DBus.Introspectable"> <method name="Introspect"> <arg name="data" type="s" direction="out"/> </method> </interface> <interface name="org.freedesktop.DBus.Properties"> <method name="Get"> <arg name="interface" direction="in" type="s"/> <arg name="property" direction="in" type="s"/> <arg name="value" direction="out" type="v"/> </method> <method name="GetAll"> <arg name="interface" direction="in" type="s"/> <arg name="properties" direction="out" type="a{sv}"/> </method> <method name="Set"> <arg name="interface" direction="in" type="s"/> <arg name="property" direction="in" type="s"/> <arg name="value" direction="in" type="v"/> </method> <signal name="PropertiesChanged"> <arg type="s" name="interface"/> <arg type="a{sv}" name="changed_properties"/> <arg type="as" name="invalidated_properties"/> </signal> </interface> <interface name="org.freedesktop.systemd.VtableExample"> <method name="Method1"> <arg type="s" direction="in"/> <arg type="s" direction="out"/> </method> <method name="Method2"> <arg type="s" name="string" direction="in"/> <arg type="o" name="path" direction="in"/> <arg type="s" name="returnstring" direction="out"/> <annotation name="org.freedesktop.DBus.Deprecated" value="true"/> </method> <property name="AutomaticStringProperty" type="s" access="readwrite"> </property> <property name="AutomaticIntegerProperty" type="u" access="readwrite"> <annotation name="org.freedesktop.DBus.Property.EmitsChangedSignal" value="invalidates"/> </property> </interface> </node>