2012-12-17 06:03:16 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* BlueZ - Bluetooth protocol stack for Linux
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Intel Corporation
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
#include <gdbus.h>
|
|
|
|
|
|
|
|
#define SERVICE_NAME "org.bluez.unit.test-gdbus-client"
|
|
|
|
#define SERVICE_PATH "/org/bluez/unit/test_gdbus_client"
|
|
|
|
|
2013-01-03 07:35:37 +08:00
|
|
|
struct context {
|
|
|
|
GMainLoop *main_loop;
|
|
|
|
DBusConnection *dbus_conn;
|
|
|
|
GDBusClient *dbus_client;
|
|
|
|
guint timeout_source;
|
|
|
|
};
|
2012-12-17 06:03:16 +08:00
|
|
|
|
|
|
|
static const GDBusMethodTable methods[] = {
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const GDBusSignalTable signals[] = {
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const GDBusPropertyTable properties[] = {
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2013-01-03 07:35:37 +08:00
|
|
|
static struct context *create_context(void)
|
|
|
|
{
|
|
|
|
struct context *context = g_new0(struct context, 1);
|
2013-01-10 09:03:28 +08:00
|
|
|
DBusError err;
|
2013-01-03 07:35:37 +08:00
|
|
|
|
|
|
|
context->main_loop = g_main_loop_new(NULL, FALSE);
|
|
|
|
if (context->main_loop == NULL) {
|
|
|
|
g_free(context);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-01-10 09:03:28 +08:00
|
|
|
dbus_error_init(&err);
|
|
|
|
|
2013-01-03 07:35:37 +08:00
|
|
|
context->dbus_conn = g_dbus_setup_private(DBUS_BUS_SESSION,
|
2013-01-10 09:03:28 +08:00
|
|
|
SERVICE_NAME, &err);
|
2013-01-03 07:35:37 +08:00
|
|
|
if (context->dbus_conn == NULL) {
|
2013-01-10 09:03:28 +08:00
|
|
|
if (dbus_error_is_set(&err)) {
|
|
|
|
if (g_test_verbose())
|
|
|
|
g_printerr("D-Bus setup failed: %s\n",
|
|
|
|
err.message);
|
|
|
|
dbus_error_free(&err);
|
|
|
|
}
|
|
|
|
|
2013-01-03 07:35:37 +08:00
|
|
|
g_main_loop_unref(context->main_loop);
|
|
|
|
g_free(context);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_context(struct context *context)
|
|
|
|
{
|
|
|
|
if (context == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
dbus_connection_flush(context->dbus_conn);
|
|
|
|
dbus_connection_close(context->dbus_conn);
|
|
|
|
|
|
|
|
g_main_loop_unref(context->main_loop);
|
|
|
|
|
|
|
|
g_free(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean timeout_handler(gpointer user_data)
|
2012-12-17 06:03:16 +08:00
|
|
|
{
|
2013-01-03 07:35:37 +08:00
|
|
|
struct context *context = user_data;
|
|
|
|
|
2012-12-28 16:10:07 +08:00
|
|
|
if (g_test_verbose())
|
|
|
|
g_print("timeout triggered\n");
|
|
|
|
|
2013-01-03 07:35:37 +08:00
|
|
|
context->timeout_source = 0;
|
2012-12-17 06:03:16 +08:00
|
|
|
|
2013-01-03 07:35:37 +08:00
|
|
|
g_dbus_client_unref(context->dbus_client);
|
2012-12-17 06:03:16 +08:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void connect_handler(DBusConnection *connection, void *user_data)
|
|
|
|
{
|
2013-01-03 07:35:37 +08:00
|
|
|
struct context *context = user_data;
|
|
|
|
|
2012-12-28 16:10:07 +08:00
|
|
|
if (g_test_verbose())
|
|
|
|
g_print("service connected\n");
|
|
|
|
|
2013-01-03 07:35:37 +08:00
|
|
|
g_dbus_client_unref(context->dbus_client);
|
2012-12-17 06:03:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void disconnect_handler(DBusConnection *connection, void *user_data)
|
|
|
|
{
|
2013-01-03 07:35:37 +08:00
|
|
|
struct context *context = user_data;
|
|
|
|
|
2012-12-28 16:10:07 +08:00
|
|
|
if (g_test_verbose())
|
|
|
|
g_print("service disconnected\n");
|
|
|
|
|
2013-01-03 07:35:37 +08:00
|
|
|
g_main_loop_quit(context->main_loop);
|
2012-12-17 06:03:16 +08:00
|
|
|
}
|
|
|
|
|
2012-12-28 16:10:07 +08:00
|
|
|
static void simple_client(void)
|
|
|
|
{
|
2013-01-03 07:35:37 +08:00
|
|
|
struct context *context = create_context();
|
2012-12-28 16:10:07 +08:00
|
|
|
|
2013-01-03 07:35:37 +08:00
|
|
|
if (context == NULL)
|
2012-12-28 16:10:07 +08:00
|
|
|
return;
|
|
|
|
|
2013-01-03 07:35:37 +08:00
|
|
|
context->dbus_client = g_dbus_client_new(context->dbus_conn,
|
|
|
|
SERVICE_NAME, SERVICE_PATH);
|
2012-12-28 16:10:07 +08:00
|
|
|
|
2013-01-03 07:35:37 +08:00
|
|
|
g_dbus_client_set_connect_watch(context->dbus_client,
|
|
|
|
connect_handler, context);
|
|
|
|
g_dbus_client_set_disconnect_watch(context->dbus_client,
|
|
|
|
disconnect_handler, context);
|
2012-12-28 16:10:07 +08:00
|
|
|
|
2013-01-03 07:35:37 +08:00
|
|
|
g_main_loop_run(context->main_loop);
|
2012-12-28 16:10:07 +08:00
|
|
|
|
2013-01-03 07:35:37 +08:00
|
|
|
destroy_context(context);
|
2012-12-28 16:10:07 +08:00
|
|
|
}
|
|
|
|
|
2012-12-17 06:03:16 +08:00
|
|
|
static void client_connect_disconnect(void)
|
|
|
|
{
|
2013-01-03 07:35:37 +08:00
|
|
|
struct context *context = create_context();
|
2012-12-17 06:03:16 +08:00
|
|
|
|
2013-01-03 07:35:37 +08:00
|
|
|
if (context == NULL)
|
2012-12-18 07:12:35 +08:00
|
|
|
return;
|
|
|
|
|
2013-01-03 07:35:37 +08:00
|
|
|
g_dbus_register_interface(context->dbus_conn,
|
|
|
|
SERVICE_PATH, SERVICE_NAME,
|
2012-12-17 06:03:16 +08:00
|
|
|
methods, signals, properties, NULL, NULL);
|
|
|
|
|
2013-01-03 07:35:37 +08:00
|
|
|
context->dbus_client = g_dbus_client_new(context->dbus_conn,
|
|
|
|
SERVICE_NAME, SERVICE_PATH);
|
2012-12-17 06:03:16 +08:00
|
|
|
|
2013-01-03 07:35:37 +08:00
|
|
|
g_dbus_client_set_connect_watch(context->dbus_client,
|
|
|
|
connect_handler, context);
|
|
|
|
g_dbus_client_set_disconnect_watch(context->dbus_client,
|
|
|
|
disconnect_handler, context);
|
2012-12-17 06:03:16 +08:00
|
|
|
|
2013-01-03 07:35:37 +08:00
|
|
|
context->timeout_source = g_timeout_add_seconds(10, timeout_handler,
|
|
|
|
context);
|
2012-12-17 06:03:16 +08:00
|
|
|
|
2013-01-03 07:35:37 +08:00
|
|
|
g_main_loop_run(context->main_loop);
|
2012-12-17 06:03:16 +08:00
|
|
|
|
2013-01-03 07:35:37 +08:00
|
|
|
if (context->timeout_source > 0)
|
|
|
|
g_source_remove(context->timeout_source);
|
2012-12-17 06:03:16 +08:00
|
|
|
|
2013-01-03 07:35:37 +08:00
|
|
|
g_dbus_unregister_interface(context->dbus_conn,
|
|
|
|
SERVICE_PATH, SERVICE_NAME);
|
2012-12-17 06:03:16 +08:00
|
|
|
|
2013-01-03 07:35:37 +08:00
|
|
|
destroy_context(context);
|
2012-12-17 06:03:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
g_test_init(&argc, &argv, NULL);
|
|
|
|
|
2012-12-28 16:10:07 +08:00
|
|
|
g_test_add_func("/gdbus/simple_client", simple_client);
|
|
|
|
|
2012-12-17 06:03:16 +08:00
|
|
|
g_test_add_func("/gdbus/client_connect_disconnect",
|
|
|
|
client_connect_disconnect);
|
|
|
|
|
|
|
|
return g_test_run();
|
|
|
|
}
|