dbus: Ensure the timeout is removed when connected

pull/102/head
Olivier Martin 2019-05-20 16:21:27 +02:00 committed by Olivier Martin
parent d8fb10906b
commit e822d3268a
2 changed files with 13 additions and 8 deletions

View File

@ -203,12 +203,14 @@ gboolean on_handle_device_property_change(
g_variant_get (arg_changed_properties, "a{sv}", &iter);
while (g_variant_iter_loop (iter, "{&sv}", &key, &value)) {
if (strcmp(key, "UUIDs") == 0) {
// When UUIDs properties appear, we are connected to the device
g_main_loop_quit(conn_context->connection_loop);
break;
} else if (strcmp(key, "Connected") == 0) {
if (!g_variant_get_boolean(value)) {
if (strcmp(key, "Connected") == 0) {
if (g_variant_get_boolean(value)) {
// Stop the timeout for connection
g_source_remove(conn_context->connection_timeout);
// Tell we are now connected
g_main_loop_quit(conn_context->connection_loop);
} else {
// Disconnection case
if (connection->disconnection_handler) {
connection->disconnection_handler(connection->disconnection_user_data);
@ -305,7 +307,8 @@ gatt_connection_t *gattlib_connect(const char *src, const char *dst,
G_CALLBACK (on_handle_device_property_change),
connection);
g_timeout_add_seconds (CONNECT_TIMEOUT, stop_scan_func, conn_context->connection_loop);
conn_context->connection_timeout = g_timeout_add_seconds(CONNECT_TIMEOUT, stop_scan_func,
conn_context->connection_loop);
g_main_loop_run(conn_context->connection_loop);
g_main_loop_unref(conn_context->connection_loop);
// Set the attribute to NULL even if not required

View File

@ -2,7 +2,7 @@
*
* GattLib - GATT Library
*
* Copyright (C) 2016-2017 Olivier Martin <olivier@labapart.org>
* Copyright (C) 2016-2019 Olivier Martin <olivier@labapart.org>
*
*
* This program is free software; you can redistribute it and/or modify
@ -48,6 +48,8 @@ typedef struct {
// This attribute is only used during the connection stage. By placing the attribute here, we can pass
// `gatt_connection_t` to
GMainLoop *connection_loop;
// ID of the timeout to know if we managed to connect to the device
guint connection_timeout;
} gattlib_context_t;
#endif