gap-tester.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2012 Intel Corporation. All rights reserved.
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include "gdbus/gdbus.h"
  14. #include "src/shared/tester.h"
  15. #include "emulator/hciemu.h"
  16. static DBusConnection *dbus_conn = NULL;
  17. static GDBusClient *dbus_client = NULL;
  18. static GDBusProxy *adapter_proxy = NULL;
  19. static struct hciemu *hciemu_stack = NULL;
  20. static void connect_handler(DBusConnection *connection, void *user_data)
  21. {
  22. tester_print("Connected to daemon");
  23. hciemu_stack = hciemu_new(HCIEMU_TYPE_BREDRLE);
  24. }
  25. static void disconnect_handler(DBusConnection *connection, void *user_data)
  26. {
  27. tester_print("Disconnected from daemon");
  28. dbus_connection_unref(dbus_conn);
  29. dbus_conn = NULL;
  30. tester_teardown_complete();
  31. }
  32. static gboolean compare_string_property(GDBusProxy *proxy, const char *name,
  33. const char *value)
  34. {
  35. DBusMessageIter iter;
  36. const char *str;
  37. if (g_dbus_proxy_get_property(proxy, name, &iter) == FALSE)
  38. return FALSE;
  39. if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
  40. return FALSE;
  41. dbus_message_iter_get_basic(&iter, &str);
  42. return g_str_equal(str, value);
  43. }
  44. static void proxy_added(GDBusProxy *proxy, void *user_data)
  45. {
  46. const char *interface;
  47. interface = g_dbus_proxy_get_interface(proxy);
  48. if (g_str_equal(interface, "org.bluez.Adapter1") == TRUE) {
  49. if (compare_string_property(proxy, "Address",
  50. hciemu_get_address(hciemu_stack)) == TRUE) {
  51. adapter_proxy = proxy;
  52. tester_print("Found adapter");
  53. tester_setup_complete();
  54. }
  55. }
  56. }
  57. static void proxy_removed(GDBusProxy *proxy, void *user_data)
  58. {
  59. const char *interface;
  60. interface = g_dbus_proxy_get_interface(proxy);
  61. if (g_str_equal(interface, "org.bluez.Adapter1") == TRUE) {
  62. if (adapter_proxy == proxy) {
  63. adapter_proxy = NULL;
  64. tester_print("Adapter removed");
  65. g_dbus_client_unref(dbus_client);
  66. dbus_client = NULL;
  67. }
  68. }
  69. }
  70. static void test_setup(const void *test_data)
  71. {
  72. dbus_conn = g_dbus_setup_private(DBUS_BUS_SYSTEM, NULL, NULL);
  73. dbus_client = g_dbus_client_new(dbus_conn, "org.bluez", "/org/bluez");
  74. g_dbus_client_set_connect_watch(dbus_client, connect_handler, NULL);
  75. g_dbus_client_set_disconnect_watch(dbus_client,
  76. disconnect_handler, NULL);
  77. g_dbus_client_set_proxy_handlers(dbus_client, proxy_added,
  78. proxy_removed, NULL, NULL);
  79. }
  80. static void test_run(const void *test_data)
  81. {
  82. tester_test_passed();
  83. }
  84. static void test_teardown(const void *test_data)
  85. {
  86. hciemu_unref(hciemu_stack);
  87. hciemu_stack = NULL;
  88. }
  89. int main(int argc, char *argv[])
  90. {
  91. tester_init(&argc, &argv);
  92. tester_add("Adapter setup", NULL, test_setup, test_run, test_teardown);
  93. return tester_run();
  94. }