Zbigniew Jędrzejewski-Szmek | afb9c0c | 2019-04-21 22:39:30 +0200 | [diff] [blame] | 1 | #include <errno.h> |
| 2 | #include <stdbool.h> |
| 3 | #include <stddef.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <stdio.h> |
| 6 | #include <systemd/sd-bus.h> |
| 7 | |
| 8 | #define _cleanup_(f) __attribute__((cleanup(f))) |
| 9 | |
| 10 | typedef struct object { |
| 11 | char *name; |
| 12 | uint32_t number; |
| 13 | } object; |
| 14 | |
| 15 | static int method(sd_bus_message *m, void *userdata, sd_bus_error *error) { |
| 16 | printf("Got called with userdata=%p\n", userdata); |
| 17 | return 1; |
| 18 | } |
| 19 | |
| 20 | static const sd_bus_vtable vtable[] = { |
| 21 | SD_BUS_VTABLE_START(0), |
| 22 | SD_BUS_METHOD( |
| 23 | "Method1", "s", "s", method, 0), |
| 24 | SD_BUS_METHOD_WITH_NAMES_OFFSET( |
| 25 | "Method2", |
| 26 | "so", SD_BUS_PARAM(string) SD_BUS_PARAM(path), |
| 27 | "s", SD_BUS_PARAM(returnstring), |
| 28 | method, offsetof(object, number), |
| 29 | SD_BUS_VTABLE_DEPRECATED), |
| 30 | SD_BUS_WRITABLE_PROPERTY( |
| 31 | "AutomaticStringProperty", "s", NULL, NULL, |
| 32 | offsetof(object, name), |
| 33 | SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 34 | SD_BUS_WRITABLE_PROPERTY( |
| 35 | "AutomaticIntegerProperty", "u", NULL, NULL, |
| 36 | offsetof(object, number), |
| 37 | SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION), |
| 38 | SD_BUS_VTABLE_END |
| 39 | }; |
| 40 | |
| 41 | #define check(x) ({ \ |
| 42 | int r = x; \ |
| 43 | errno = r < 0 ? -r : 0; \ |
| 44 | printf(#x ": %m\n"); \ |
| 45 | if (r < 0) \ |
| 46 | return EXIT_FAILURE; \ |
| 47 | }) |
| 48 | |
| 49 | int main(int argc, char **argv) { |
| 50 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
| 51 | |
| 52 | sd_bus_default(&bus); |
| 53 | |
| 54 | object object = { .number = 666 }; |
| 55 | check((object.name = strdup("name")) != NULL); |
| 56 | |
| 57 | check(sd_bus_add_object_vtable(bus, NULL, "/object", |
| 58 | "org.freedesktop.systemd.VtableExample", |
| 59 | vtable, |
| 60 | &object)); |
| 61 | |
Frantisek Sumsal | ed0cb34 | 2019-04-29 18:22:22 +0200 | [diff] [blame^] | 62 | for (;;) { |
Zbigniew Jędrzejewski-Szmek | afb9c0c | 2019-04-21 22:39:30 +0200 | [diff] [blame] | 63 | check(sd_bus_wait(bus, UINT64_MAX)); |
| 64 | check(sd_bus_process(bus, NULL)); |
| 65 | } |
| 66 | |
| 67 | free(object.name); |
| 68 | |
| 69 | return 0; |
| 70 | } |