[gnome-bluetooth/wip/hadess/discovery-state-restart: 3/4] client: Handle failure to start or filter Discovery




commit 235f44dd0fb384f596e8ff0296dae6ae19b0a601
Author: Bastien Nocera <hadess hadess net>
Date:   Thu Jun 23 14:55:34 2022 +0200

    client: Handle failure to start or filter Discovery
    
    Reorganise functions called from
    _bluetooth_client_set_default_adapter_discovering() so that we only call
    StartDiscovery() if SetDiscoveryFilter() is successful, and we disable
    the "in-flight" ->discovery_started variable so the next default-adapter
    changes would try to enable discovery again if needed.
    
    Closes: #100

 lib/bluetooth-client.c | 74 +++++++++++++++++++++++++++++++-------------------
 1 file changed, 46 insertions(+), 28 deletions(-)
---
diff --git a/lib/bluetooth-client.c b/lib/bluetooth-client.c
index f0f6b36f..458fdfe0 100644
--- a/lib/bluetooth-client.c
+++ b/lib/bluetooth-client.c
@@ -1104,42 +1104,68 @@ _bluetooth_client_get_default_adapter(BluetoothClient *client)
 }
 
 static void
-set_discovery_filter_cb (Adapter1     *adapter,
-                        GAsyncResult *res,
-                        gpointer      user_data)
+stop_discovery_cb (Adapter1     *adapter,
+                  GAsyncResult *res,
+                  gpointer      user_data)
 {
        g_autoptr(GError) error = NULL;
        gboolean ret;
 
-       ret = adapter1_call_set_discovery_filter_finish (adapter, res, &error);
+       ret = adapter1_call_stop_discovery_finish (adapter, res, &error);
        if (!ret) {
-               g_debug ("Error calling SetDiscoveryFilter()  on interface org.bluez.Adapter1: %s (%s, %d)",
+               g_debug ("Error calling StopDiscovery() on %s org.bluez.Adapter1: %s (%s, %d)",
+                        g_dbus_proxy_get_object_path (G_DBUS_PROXY (adapter)),
                         error->message, g_quark_to_string (error->domain), error->code);
+       } else {
+               g_debug ("Ran StopDiscovery() successfully on %s org.bluez.Adapter1",
+                        g_dbus_proxy_get_object_path (G_DBUS_PROXY (adapter)));
        }
 }
 
 static void
-discovery_cb (Adapter1     *adapter,
-             GAsyncResult *res,
-             gpointer      user_data)
+start_discovery_cb (Adapter1     *adapter,
+                   GAsyncResult *res,
+                   gpointer      user_data)
 {
        g_autoptr(GError) error = NULL;
        gboolean ret;
-       gboolean start_call = GPOINTER_TO_UINT (user_data);
 
-       if (start_call)
-               ret = adapter1_call_start_discovery_finish (adapter, res, &error);
-       else
-               ret = adapter1_call_stop_discovery_finish (adapter, res, &error);
+       ret = adapter1_call_start_discovery_finish (adapter, res, &error);
        if (!ret) {
-               g_debug ("Error calling %s on %s org.bluez.Adapter1: %s (%s, %d)",
-                        start_call ? "StartDiscovery()" : "StopDiscovery()",
+               g_debug ("Error calling StartDiscovery() on %s org.bluez.Adapter1: %s (%s, %d)",
                         g_dbus_proxy_get_object_path (G_DBUS_PROXY (adapter)),
                         error->message, g_quark_to_string (error->domain), error->code);
+               if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
+                       BluetoothClient *client = user_data;
+                       client->discovery_started = FALSE;
+               }
+       }
+}
+
+static void
+set_discovery_filter_cb (Adapter1     *adapter,
+                        GAsyncResult *res,
+                        gpointer      user_data)
+{
+       g_autoptr(GError) error = NULL;
+       gboolean ret;
+
+       ret = adapter1_call_set_discovery_filter_finish (adapter, res, &error);
+       if (!ret) {
+               g_debug ("Error calling SetDiscoveryFilter() on interface org.bluez.Adapter1: %s (%s, %d)",
+                        error->message, g_quark_to_string (error->domain), error->code);
+               if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
+                       BluetoothClient *client = user_data;
+                       client->discovery_started = FALSE;
+               }
        } else {
-               g_debug ("Ran %s successfully on %s org.bluez.Adapter1",
-                        start_call ? "StartDiscovery()" : "StopDiscovery()",
-                        g_dbus_proxy_get_object_path (G_DBUS_PROXY (adapter)));
+               BluetoothClient *client = user_data;
+
+               g_debug ("Starting discovery on %s", g_dbus_proxy_get_object_path (G_DBUS_PROXY (adapter)));
+               adapter1_call_start_discovery (ADAPTER1 (adapter),
+                                              client->cancellable,
+                                              (GAsyncReadyCallback) start_discovery_cb,
+                                              client);
        }
 }
 
@@ -1172,22 +1198,14 @@ _bluetooth_client_set_default_adapter_discovering (BluetoothClient *client,
                                                    client->cancellable,
                                                    (GAsyncReadyCallback) set_discovery_filter_cb,
                                                    client);
-       }
-
-       if (discovering) {
-               g_debug ("Starting discovery on %s", g_dbus_proxy_get_object_path (adapter));
-               adapter1_call_start_discovery (ADAPTER1 (adapter),
-                                              client->cancellable,
-                                              (GAsyncReadyCallback) discovery_cb,
-                                              GUINT_TO_POINTER (discovering));
        } else {
                /* Don't cancel a discovery stop when the BluetoothClient
                 * is finalised, so don't pass a cancellable */
                g_debug ("Stopping discovery on %s", g_dbus_proxy_get_object_path (adapter));
                adapter1_call_stop_discovery (ADAPTER1 (adapter),
                                              NULL,
-                                             (GAsyncReadyCallback) discovery_cb,
-                                             GUINT_TO_POINTER (discovering));
+                                             (GAsyncReadyCallback) stop_discovery_cb,
+                                             client);
        }
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]