Blame tools/gap-tester.c

Packit 34410b
/*
Packit 34410b
 *
Packit 34410b
 *  BlueZ - Bluetooth protocol stack for Linux
Packit 34410b
 *
Packit 34410b
 *  Copyright (C) 2012  Intel Corporation. All rights reserved.
Packit 34410b
 *
Packit 34410b
 *
Packit 34410b
 *  This program is free software; you can redistribute it and/or modify
Packit 34410b
 *  it under the terms of the GNU General Public License as published by
Packit 34410b
 *  the Free Software Foundation; either version 2 of the License, or
Packit 34410b
 *  (at your option) any later version.
Packit 34410b
 *
Packit 34410b
 *  This program is distributed in the hope that it will be useful,
Packit 34410b
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 34410b
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 34410b
 *  GNU General Public License for more details.
Packit 34410b
 *
Packit 34410b
 *  You should have received a copy of the GNU General Public License
Packit 34410b
 *  along with this program; if not, write to the Free Software
Packit 34410b
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
Packit 34410b
 *
Packit 34410b
 */
Packit 34410b
Packit 34410b
#ifdef HAVE_CONFIG_H
Packit 34410b
#include <config.h>
Packit 34410b
#endif
Packit 34410b
Packit 34410b
#include "gdbus/gdbus.h"
Packit 34410b
Packit 34410b
#include "src/shared/tester.h"
Packit 34410b
#include "emulator/hciemu.h"
Packit 34410b
Packit 34410b
static DBusConnection *dbus_conn = NULL;
Packit 34410b
static GDBusClient *dbus_client = NULL;
Packit 34410b
static GDBusProxy *adapter_proxy = NULL;
Packit 34410b
Packit 34410b
static struct hciemu *hciemu_stack = NULL;
Packit 34410b
Packit 34410b
static void connect_handler(DBusConnection *connection, void *user_data)
Packit 34410b
{
Packit 34410b
	tester_print("Connected to daemon");
Packit 34410b
Packit 34410b
	hciemu_stack = hciemu_new(HCIEMU_TYPE_BREDRLE);
Packit 34410b
}
Packit 34410b
Packit 34410b
static void disconnect_handler(DBusConnection *connection, void *user_data)
Packit 34410b
{
Packit 34410b
	tester_print("Disconnected from daemon");
Packit 34410b
Packit 34410b
	dbus_connection_unref(dbus_conn);
Packit 34410b
	dbus_conn = NULL;
Packit 34410b
Packit 34410b
	tester_teardown_complete();
Packit 34410b
}
Packit 34410b
Packit 34410b
static gboolean compare_string_property(GDBusProxy *proxy, const char *name,
Packit 34410b
							const char *value)
Packit 34410b
{
Packit 34410b
	DBusMessageIter iter;
Packit 34410b
	const char *str;
Packit 34410b
Packit 34410b
	if (g_dbus_proxy_get_property(proxy, name, &iter) == FALSE)
Packit 34410b
		return FALSE;
Packit 34410b
Packit 34410b
	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
Packit 34410b
		return FALSE;
Packit 34410b
Packit 34410b
	dbus_message_iter_get_basic(&iter, &str);
Packit 34410b
Packit 34410b
	return g_str_equal(str, value);
Packit 34410b
}
Packit 34410b
Packit 34410b
static void proxy_added(GDBusProxy *proxy, void *user_data)
Packit 34410b
{
Packit 34410b
	const char *interface;
Packit 34410b
Packit 34410b
	interface = g_dbus_proxy_get_interface(proxy);
Packit 34410b
Packit 34410b
	if (g_str_equal(interface, "org.bluez.Adapter1") == TRUE) {
Packit 34410b
		if (compare_string_property(proxy, "Address",
Packit 34410b
				hciemu_get_address(hciemu_stack)) == TRUE) {
Packit 34410b
			adapter_proxy = proxy;
Packit 34410b
			tester_print("Found adapter");
Packit 34410b
Packit 34410b
			tester_setup_complete();
Packit 34410b
		}
Packit 34410b
	}
Packit 34410b
}
Packit 34410b
Packit 34410b
static void proxy_removed(GDBusProxy *proxy, void *user_data)
Packit 34410b
{
Packit 34410b
	const char *interface;
Packit 34410b
Packit 34410b
	interface = g_dbus_proxy_get_interface(proxy);
Packit 34410b
Packit 34410b
	if (g_str_equal(interface, "org.bluez.Adapter1") == TRUE) {
Packit 34410b
		if (adapter_proxy == proxy) {
Packit 34410b
			adapter_proxy = NULL;
Packit 34410b
			tester_print("Adapter removed");
Packit 34410b
Packit 34410b
			g_dbus_client_unref(dbus_client);
Packit 34410b
			dbus_client = NULL;
Packit 34410b
		}
Packit 34410b
	}
Packit 34410b
}
Packit 34410b
Packit 34410b
static void test_setup(const void *test_data)
Packit 34410b
{
Packit 34410b
	dbus_conn = g_dbus_setup_private(DBUS_BUS_SYSTEM, NULL, NULL);
Packit 34410b
Packit 34410b
	dbus_client = g_dbus_client_new(dbus_conn, "org.bluez", "/org/bluez");
Packit 34410b
Packit 34410b
	g_dbus_client_set_connect_watch(dbus_client, connect_handler, NULL);
Packit 34410b
	g_dbus_client_set_disconnect_watch(dbus_client,
Packit 34410b
						disconnect_handler, NULL);
Packit 34410b
Packit 34410b
	g_dbus_client_set_proxy_handlers(dbus_client, proxy_added,
Packit 34410b
						proxy_removed, NULL, NULL);
Packit 34410b
}
Packit 34410b
Packit 34410b
static void test_run(const void *test_data)
Packit 34410b
{
Packit 34410b
	tester_test_passed();
Packit 34410b
}
Packit 34410b
Packit 34410b
static void test_teardown(const void *test_data)
Packit 34410b
{
Packit 34410b
	hciemu_unref(hciemu_stack);
Packit 34410b
	hciemu_stack = NULL;
Packit 34410b
}
Packit 34410b
Packit 34410b
int main(int argc, char *argv[])
Packit 34410b
{
Packit 34410b
	tester_init(&argc, &argv);
Packit 34410b
Packit 34410b
	tester_add("Adapter setup", NULL, test_setup, test_run, test_teardown);
Packit 34410b
Packit 34410b
	return tester_run();
Packit 34410b
}