4 /* We use system assert.h here, because we don't want to keep macro.h and log.h C++ compatible */
9 #include "sd-bus-vtable.h"
11 #define DEFAULT_BUS_PATH "unix:path=/run/dbus/system_bus_socket"
16 char *automatic_string_property
;
17 uint32_t automatic_integer_property
;
20 static int handler(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
24 static int value_handler(sd_bus
*bus
, const char *path
, const char *interface
, const char *property
, sd_bus_message
*reply
, void *userdata
, sd_bus_error
*error
) {
28 static int get_handler(sd_bus
*bus
, const char *path
, const char *interface
, const char *property
, sd_bus_message
*reply
, void *userdata
, sd_bus_error
*error
) {
32 static int set_handler(sd_bus
*bus
, const char *path
, const char *interface
, const char *property
, sd_bus_message
*value
, void *userdata
, sd_bus_error
*error
) {
36 static const sd_bus_vtable vtable
[] = {
37 SD_BUS_VTABLE_START(0),
38 SD_BUS_METHOD("AlterSomething", "s", "s", handler
, 0),
39 SD_BUS_METHOD("Exit", "", "", handler
, 0),
40 SD_BUS_METHOD_WITH_OFFSET("AlterSomething2", "s", "s", handler
, 200, 0),
41 SD_BUS_METHOD_WITH_OFFSET("Exit2", "", "", handler
, 200, 0),
42 SD_BUS_METHOD_WITH_NAMES_OFFSET("AlterSomething3", "so", SD_BUS_PARAM(string
) SD_BUS_PARAM(path
),
43 "s", SD_BUS_PARAM(returnstring
), handler
, 200, 0),
44 SD_BUS_METHOD_WITH_NAMES("Exit3", "bx", SD_BUS_PARAM(with_confirmation
) SD_BUS_PARAM(after_msec
),
45 "bb", SD_BUS_PARAM(accepted
) SD_BUS_PARAM(scheduled
), handler
, 0),
46 SD_BUS_PROPERTY("Value", "s", value_handler
, 10, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
47 SD_BUS_PROPERTY("Value2", "s", value_handler
, 10, SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
),
48 SD_BUS_PROPERTY("Value3", "s", value_handler
, 10, SD_BUS_VTABLE_PROPERTY_CONST
),
49 SD_BUS_PROPERTY("Value4", "s", value_handler
, 10, 0),
50 SD_BUS_PROPERTY("AnExplicitProperty", "s", NULL
, offsetof(struct context
, something
),
51 SD_BUS_VTABLE_PROPERTY_EXPLICIT
|SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION
),
52 SD_BUS_WRITABLE_PROPERTY("Something", "s", get_handler
, set_handler
, 0, 0),
53 SD_BUS_WRITABLE_PROPERTY("AutomaticStringProperty", "s", NULL
, NULL
,
54 offsetof(struct context
, automatic_string_property
), 0),
55 SD_BUS_WRITABLE_PROPERTY("AutomaticIntegerProperty", "u", NULL
, NULL
,
56 offsetof(struct context
, automatic_integer_property
), 0),
57 SD_BUS_METHOD("NoOperation", NULL
, NULL
, NULL
, 0),
58 SD_BUS_SIGNAL("DummySignal", "b", 0),
59 SD_BUS_SIGNAL("DummySignal2", "so", 0),
60 SD_BUS_SIGNAL_WITH_NAMES("DummySignal3", "so", SD_BUS_PARAM(string
) SD_BUS_PARAM(path
), 0),
64 struct sd_bus_vtable_original
{
73 const char *signature
;
75 sd_bus_message_handler_t handler
;
80 const char *signature
;
84 const char *signature
;
85 sd_bus_property_get_t get
;
86 sd_bus_property_set_t set
;
92 static const struct sd_bus_vtable_original vtable_format_original
[] = {
94 .type
= _SD_BUS_VTABLE_START
,
98 .element_size
= sizeof(struct sd_bus_vtable_original
)
103 .type
= _SD_BUS_VTABLE_METHOD
,
116 .type
= _SD_BUS_VTABLE_END
,
122 static void test_vtable(void) {
124 struct context c
= {};
127 assert(sd_bus_new(&bus
) >= 0);
129 assert(sd_bus_add_object_vtable(bus
, NULL
, "/foo", "org.freedesktop.systemd.testVtable", vtable
, &c
) >= 0);
130 assert(sd_bus_add_object_vtable(bus
, NULL
, "/foo", "org.freedesktop.systemd.testVtable2", vtable
, &c
) >= 0);
131 /* the cast on the line below is needed to test with the old version of the table */
132 assert(sd_bus_add_object_vtable(bus
, NULL
, "/foo", "org.freedesktop.systemd.testVtableOriginal", (const sd_bus_vtable
*)vtable_format_original
, &c
) >= 0);
134 assert(sd_bus_set_address(bus
, DEFAULT_BUS_PATH
) >= 0);
135 r
= sd_bus_start(bus
);
136 assert(r
== 0 || /* success */
137 r
== -ENOENT
/* dbus is inactive */ );
142 int main(int argc
, char **argv
) {