summaryrefslogtreecommitdiff
path: root/tools/gap-tester.c
blob: 942c37d270b8dcccf43a91ff3ae48134e00010db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2012  Intel Corporation. All rights reserved.
 *
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "gdbus/gdbus.h"

#include "src/shared/tester.h"
#include "emulator/hciemu.h"

static DBusConnection *dbus_conn = NULL;
static GDBusClient *dbus_client = NULL;
static GDBusProxy *adapter_proxy = NULL;

static struct hciemu *hciemu_stack = NULL;

static void connect_handler(DBusConnection *connection, void *user_data)
{
	tester_print("Connected to daemon");

	hciemu_stack = hciemu_new(HCIEMU_TYPE_BREDRLE);
}

static void disconnect_handler(DBusConnection *connection, void *user_data)
{
	tester_print("Disconnected from daemon");

	dbus_connection_unref(dbus_conn);
	dbus_conn = NULL;

	tester_teardown_complete();
}

static gboolean compare_string_property(GDBusProxy *proxy, const char *name,
							const char *value)
{
	DBusMessageIter iter;
	const char *str;

	if (g_dbus_proxy_get_property(proxy, name, &iter) == FALSE)
		return FALSE;

	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
		return FALSE;

	dbus_message_iter_get_basic(&iter, &str);

	return g_str_equal(str, value);
}

static void proxy_added(GDBusProxy *proxy, void *user_data)
{
	const char *interface;

	interface = g_dbus_proxy_get_interface(proxy);

	if (g_str_equal(interface, "org.bluez.Adapter1") == TRUE) {
		if (compare_string_property(proxy, "Address",
				hciemu_get_address(hciemu_stack)) == TRUE) {
			adapter_proxy = proxy;
			tester_print("Found adapter");

			tester_setup_complete();
		}
	}
}

static void proxy_removed(GDBusProxy *proxy, void *user_data)
{
	const char *interface;

	interface = g_dbus_proxy_get_interface(proxy);

	if (g_str_equal(interface, "org.bluez.Adapter1") == TRUE) {
		if (adapter_proxy == proxy) {
			adapter_proxy = NULL;
			tester_print("Adapter removed");

			g_dbus_client_unref(dbus_client);
			dbus_client = NULL;
		}
	}
}

static void test_setup(const void *test_data)
{
	dbus_conn = g_dbus_setup_private(DBUS_BUS_SYSTEM, NULL, NULL);

	dbus_client = g_dbus_client_new(dbus_conn, "org.bluez", "/org/bluez");

	g_dbus_client_set_connect_watch(dbus_client, connect_handler, NULL);
	g_dbus_client_set_disconnect_watch(dbus_client,
						disconnect_handler, NULL);

	g_dbus_client_set_proxy_handlers(dbus_client, proxy_added,
						proxy_removed, NULL, NULL);
}

static void test_run(const void *test_data)
{
	tester_test_passed();
}

static void test_teardown(const void *test_data)
{
	hciemu_unref(hciemu_stack);
	hciemu_stack = NULL;
}

int main(int argc, char *argv[])
{
	tester_init(&argc, &argv);

	tester_add("Adapter setup", NULL, test_setup, test_run, test_teardown);

	return tester_run();
}