You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

82 lines
3.2 KiB

  1. #include <stdbool.h>
  2. #include <stddef.h>
  3. /* We use system assert.h here, because we don't want to keep macro.h and log.h C++ compatible */
  4. #undef NDEBUG
  5. #include <assert.h>
  6. #include <errno.h>
  7. #include "sd-bus-vtable.h"
  8. #define DEFAULT_BUS_PATH "unix:path=/run/dbus/system_bus_socket"
  9. struct context {
  10. bool quit;
  11. char *something;
  12. char *automatic_string_property;
  13. uint32_t automatic_integer_property;
  14. };
  15. static int handler(sd_bus_message *m, void *userdata, sd_bus_error *error) {
  16. return 1;
  17. }
  18. 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) {
  19. return 1;
  20. }
  21. 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) {
  22. return 1;
  23. }
  24. 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) {
  25. return 1;
  26. }
  27. static const sd_bus_vtable vtable[] = {
  28. SD_BUS_VTABLE_START(0),
  29. SD_BUS_METHOD("AlterSomething", "s", "s", handler, 0),
  30. SD_BUS_METHOD("Exit", "", "", handler, 0),
  31. SD_BUS_METHOD_WITH_OFFSET("AlterSomething2", "s", "s", handler, 200, 0),
  32. SD_BUS_METHOD_WITH_OFFSET("Exit2", "", "", handler, 200, 0),
  33. SD_BUS_PROPERTY("Value", "s", value_handler, 10, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
  34. SD_BUS_PROPERTY("Value2", "s", value_handler, 10, SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION),
  35. SD_BUS_PROPERTY("Value3", "s", value_handler, 10, SD_BUS_VTABLE_PROPERTY_CONST),
  36. SD_BUS_PROPERTY("Value4", "s", value_handler, 10, 0),
  37. SD_BUS_PROPERTY("AnExplicitProperty", "s", NULL, offsetof(struct context, something),
  38. SD_BUS_VTABLE_PROPERTY_EXPLICIT|SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION),
  39. SD_BUS_WRITABLE_PROPERTY("Something", "s", get_handler, set_handler, 0, 0),
  40. SD_BUS_WRITABLE_PROPERTY("AutomaticStringProperty", "s", NULL, NULL,
  41. offsetof(struct context, automatic_string_property), 0),
  42. SD_BUS_WRITABLE_PROPERTY("AutomaticIntegerProperty", "u", NULL, NULL,
  43. offsetof(struct context, automatic_integer_property), 0),
  44. SD_BUS_METHOD("NoOperation", NULL, NULL, NULL, 0),
  45. SD_BUS_SIGNAL("DummySignal", "b", 0),
  46. SD_BUS_SIGNAL("DummySignal2", "so", 0),
  47. SD_BUS_VTABLE_END
  48. };
  49. static void test_vtable(void) {
  50. sd_bus *bus = NULL;
  51. struct context c = {};
  52. int r;
  53. assert(sd_bus_new(&bus) >= 0);
  54. assert(sd_bus_add_object_vtable(bus, NULL, "/foo", "org.freedesktop.systemd.testVtable", vtable, &c) >= 0);
  55. assert(sd_bus_add_object_vtable(bus, NULL, "/foo", "org.freedesktop.systemd.testVtable2", vtable, &c) >= 0);
  56. assert(sd_bus_set_address(bus, DEFAULT_BUS_PATH) >= 0);
  57. r = sd_bus_start(bus);
  58. assert(r == 0 || /* success */
  59. r == -ENOENT /* dbus is inactive */ );
  60. sd_bus_unref(bus);
  61. }
  62. int main(int argc, char **argv) {
  63. test_vtable();
  64. return 0;
  65. }