tizen 2.3 release
[framework/connectivity/bluez.git] / tools / gap-tester.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2012  Intel Corporation. All rights reserved.
6  *
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <gdbus.h>
29
30 #include "src/shared/tester.h"
31 #include "src/shared/hciemu.h"
32
33 static DBusConnection *dbus_conn = NULL;
34 static GDBusClient *dbus_client = NULL;
35 static GDBusProxy *adapter_proxy = NULL;
36
37 static struct hciemu *hciemu_stack = NULL;
38
39 static void connect_handler(DBusConnection *connection, void *user_data)
40 {
41         tester_print("Connected to daemon");
42
43         hciemu_stack = hciemu_new(HCIEMU_TYPE_BREDRLE);
44 }
45
46 static void disconnect_handler(DBusConnection *connection, void *user_data)
47 {
48         tester_print("Disconnected from daemon");
49
50         dbus_connection_unref(dbus_conn);
51         dbus_conn = NULL;
52
53         tester_teardown_complete();
54 }
55
56 static gboolean compare_string_property(GDBusProxy *proxy, const char *name,
57                                                         const char *value)
58 {
59         DBusMessageIter iter;
60         const char *str;
61
62         if (g_dbus_proxy_get_property(proxy, name, &iter) == FALSE)
63                 return FALSE;
64
65         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
66                 return FALSE;
67
68         dbus_message_iter_get_basic(&iter, &str);
69
70         return g_str_equal(str, value);
71 }
72
73 static void proxy_added(GDBusProxy *proxy, void *user_data)
74 {
75         const char *interface;
76
77         interface = g_dbus_proxy_get_interface(proxy);
78
79         if (g_str_equal(interface, "org.bluez.Adapter1") == TRUE) {
80                 if (compare_string_property(proxy, "Address",
81                                 hciemu_get_address(hciemu_stack)) == TRUE) {
82                         adapter_proxy = proxy;
83                         tester_print("Found adapter");
84
85                         tester_setup_complete();
86                 }
87         }
88 }
89
90 static void proxy_removed(GDBusProxy *proxy, void *user_data)
91 {
92         const char *interface;
93
94         interface = g_dbus_proxy_get_interface(proxy);
95
96         if (g_str_equal(interface, "org.bluez.Adapter1") == TRUE) {
97                 if (adapter_proxy == proxy) {
98                         adapter_proxy = NULL;
99                         tester_print("Adapter removed");
100
101                         g_dbus_client_unref(dbus_client);
102                         dbus_client = NULL;
103                 }
104         }
105 }
106
107 static void test_setup(const void *test_data)
108 {
109         dbus_conn = g_dbus_setup_private(DBUS_BUS_SYSTEM, NULL, NULL);
110
111         dbus_client = g_dbus_client_new(dbus_conn, "org.bluez", "/org/bluez");
112
113         g_dbus_client_set_connect_watch(dbus_client, connect_handler, NULL);
114         g_dbus_client_set_disconnect_watch(dbus_client,
115                                                 disconnect_handler, NULL);
116
117         g_dbus_client_set_proxy_handlers(dbus_client, proxy_added,
118                                                 proxy_removed, NULL, NULL);
119 }
120
121 static void test_run(const void *test_data)
122 {
123         tester_test_passed();
124 }
125
126 static void test_teardown(const void *test_data)
127 {
128         hciemu_unref(hciemu_stack);
129         hciemu_stack = NULL;
130 }
131
132 int main(int argc, char *argv[])
133 {
134         tester_init(&argc, &argv);
135
136         tester_add("Adapter setup", NULL, test_setup, test_run, test_teardown);
137
138         return tester_run();
139 }