adapter: Remove custom MGMT send/reply timeout
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Mon, 24 Jan 2022 23:41:03 +0000 (15:41 -0800)
committerAyush Garg <ayush.garg@samsung.com>
Mon, 15 May 2023 09:25:53 +0000 (14:55 +0530)
This removes the custom MGMT send/reply timeout since bt_mgmt itself
can handle them itself and it actually start the timer only when the
command is actually sent to the kernel rather then when it is queued.

Fixes: https://github.com/bluez/bluez/issues/275
Signed-off-by: Manika Shrivastava <manika.sh@samsung.com>
Signed-off-by: Ayush Garg <ayush.garg@samsung.com>
src/adapter.c

index 0f1108f..bcbffae 100644 (file)
@@ -389,15 +389,6 @@ struct btd_adapter {
 
        struct oob_handler *oob_handler;
 
-       unsigned int load_ltks_id;
-       unsigned int load_ltks_timeout;
-
-       unsigned int confirm_name_id;
-       unsigned int confirm_name_timeout;
-
-       unsigned int pair_device_id;
-       unsigned int pair_device_timeout;
-
        unsigned int db_id;             /* Service event handler for GATT db */
 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
        uint8_t central_rpa_res_support;
@@ -8916,21 +8907,6 @@ static void load_link_keys(struct btd_adapter *adapter, GSList *keys,
                                                        adapter->dev_id);
 }
 
-static bool load_ltks_timeout(gpointer user_data)
-{
-       struct btd_adapter *adapter = user_data;
-
-       btd_error(adapter->dev_id, "Loading LTKs timed out for hci%u",
-                                                       adapter->dev_id);
-
-       adapter->load_ltks_timeout = 0;
-
-       mgmt_cancel(adapter->mgmt, adapter->load_ltks_id);
-       adapter->load_ltks_id = 0;
-
-       return FALSE;
-}
-
 static void load_ltks_complete(uint8_t status, uint16_t length,
                                        const void *param, void *user_data)
 {
@@ -8942,11 +8918,6 @@ static void load_ltks_complete(uint8_t status, uint16_t length,
                                adapter->dev_id, mgmt_errstr(status), status);
        }
 
-       adapter->load_ltks_id = 0;
-
-       timeout_remove(adapter->load_ltks_timeout);
-       adapter->load_ltks_timeout = 0;
-
        DBG("LTKs loaded for hci%u", adapter->dev_id);
 }
 
@@ -9019,27 +8990,18 @@ static void load_ltks(struct btd_adapter *adapter, GSList *keys)
                }
        }
 
-       adapter->load_ltks_id = mgmt_send(adapter->mgmt,
-                                       MGMT_OP_LOAD_LONG_TERM_KEYS,
-                                       adapter->dev_id, cp_size, cp,
-                                       load_ltks_complete, adapter, NULL);
-
-       g_free(cp);
-
-       if (adapter->load_ltks_id == 0) {
-               btd_error(adapter->dev_id, "Failed to load LTKs for hci%u",
-                                                       adapter->dev_id);
-               return;
-       }
-
        /*
         * This timeout handling is needed since the kernel is stupid
         * and forgets to send a command complete response. However in
         * case of failures it does send a command status.
         */
-       adapter->load_ltks_timeout = timeout_add_seconds(2,
-                                               load_ltks_timeout, adapter,
-                                               NULL);
+       if (!mgmt_send_timeout(adapter->mgmt, MGMT_OP_LOAD_LONG_TERM_KEYS,
+                       adapter->dev_id, cp_size, cp, load_ltks_complete,
+                       adapter, NULL, 2))
+               btd_error(adapter->dev_id, "Failed to load LTKs for hci%u",
+                                                       adapter->dev_id);
+
+       g_free(cp);
 }
 
 static void load_irks_complete(uint8_t status, uint16_t length,
@@ -10591,15 +10553,6 @@ static void adapter_free(gpointer user_data)
                adapter->passive_scan_timeout = 0;
        }
 
-       if (adapter->load_ltks_timeout > 0)
-               timeout_remove(adapter->load_ltks_timeout);
-
-       if (adapter->confirm_name_timeout > 0)
-               timeout_remove(adapter->confirm_name_timeout);
-
-       if (adapter->pair_device_timeout > 0)
-               timeout_remove(adapter->pair_device_timeout);
-
        if (adapter->auth_idle_id)
                timeout_remove(adapter->auth_idle_id);
 
@@ -11899,21 +11852,6 @@ uint8_t btd_adapter_get_le_address_type(struct btd_adapter * adapter)
 }
 #endif
 
-static bool confirm_name_timeout(gpointer user_data)
-{
-       struct btd_adapter *adapter = user_data;
-
-       btd_error(adapter->dev_id, "Confirm name timed out for hci%u",
-                                                       adapter->dev_id);
-
-       adapter->confirm_name_timeout = 0;
-
-       mgmt_cancel(adapter->mgmt, adapter->confirm_name_id);
-       adapter->confirm_name_id = 0;
-
-       return FALSE;
-}
-
 static void confirm_name_complete(uint8_t status, uint16_t length,
                                        const void *param, void *user_data)
 {
@@ -11923,13 +11861,9 @@ static void confirm_name_complete(uint8_t status, uint16_t length,
                btd_error(adapter->dev_id,
                                "Failed to confirm name for hci%u: %s (0x%02x)",
                                adapter->dev_id, mgmt_errstr(status), status);
+               return;
        }
 
-       adapter->confirm_name_id = 0;
-
-       timeout_remove(adapter->confirm_name_timeout);
-       adapter->confirm_name_timeout = 0;
-
        DBG("Confirm name complete for hci%u", adapter->dev_id);
 }
 
@@ -11943,49 +11877,21 @@ static void confirm_name(struct btd_adapter *adapter, const bdaddr_t *bdaddr,
        DBG("hci%d bdaddr %s name_known %u", adapter->dev_id, addr,
                                                                name_known);
 
-       /*
-        * If the kernel does not answer the confirm name command with
-        * a command complete or command status in time, this might
-        * race against another device found event that also requires
-        * to confirm the name. If there is a pending command, just
-        * cancel it to be safe here.
-        */
-       if (adapter->confirm_name_id > 0) {
-               btd_warn(adapter->dev_id,
-                               "Found pending confirm name for hci%u",
-                                                       adapter->dev_id);
-               mgmt_cancel(adapter->mgmt, adapter->confirm_name_id);
-       }
-
-       if (adapter->confirm_name_timeout > 0) {
-               timeout_remove(adapter->confirm_name_timeout);
-               adapter->confirm_name_timeout = 0;
-       }
-
        memset(&cp, 0, sizeof(cp));
        bacpy(&cp.addr.bdaddr, bdaddr);
        cp.addr.type = bdaddr_type;
        cp.name_known = name_known;
 
-       adapter->confirm_name_id = mgmt_reply(adapter->mgmt,
-                                       MGMT_OP_CONFIRM_NAME,
-                                       adapter->dev_id, sizeof(cp), &cp,
-                                       confirm_name_complete, adapter, NULL);
-
-       if (adapter->confirm_name_id == 0) {
-               btd_error(adapter->dev_id, "Failed to confirm name for hci%u",
-                                                       adapter->dev_id);
-               return;
-       }
-
        /*
         * This timeout handling is needed since the kernel is stupid
         * and forgets to send a command complete response. However in
         * case of failures it does send a command status.
         */
-       adapter->confirm_name_timeout = timeout_add_seconds(2,
-                                               confirm_name_timeout, adapter,
-                                               NULL);
+       if (!mgmt_reply_timeout(adapter->mgmt, MGMT_OP_CONFIRM_NAME,
+                               adapter->dev_id, sizeof(cp), &cp,
+                               confirm_name_complete, adapter, NULL, 2))
+               btd_error(adapter->dev_id, "Failed to confirm name for hci%u",
+                                                       adapter->dev_id);
 }
 
 static void adapter_msd_notify(struct btd_adapter *adapter,
@@ -13905,21 +13811,6 @@ static void free_pair_device_data(void *user_data)
        g_free(data);
 }
 
-static bool pair_device_timeout(gpointer user_data)
-{
-       struct pair_device_data *data = user_data;
-       struct btd_adapter *adapter = data->adapter;
-
-       btd_error(adapter->dev_id, "Pair device timed out for hci%u",
-                                                       adapter->dev_id);
-
-       adapter->pair_device_timeout = 0;
-
-       adapter_cancel_bonding(adapter, &data->bdaddr, data->addr_type);
-
-       return FALSE;
-}
-
 static void pair_device_complete(uint8_t status, uint16_t length,
                                        const void *param, void *user_data)
 {
@@ -13929,13 +13820,6 @@ static void pair_device_complete(uint8_t status, uint16_t length,
 
        DBG("%s (0x%02x)", mgmt_errstr(status), status);
 
-       adapter->pair_device_id = 0;
-
-       if (adapter->pair_device_timeout > 0) {
-               timeout_remove(adapter->pair_device_timeout);
-               adapter->pair_device_timeout = 0;
-       }
-
        /* Workaround for a kernel bug
         *
         * Broken kernels may reply to device pairing command with command
@@ -13963,12 +13847,6 @@ static void pair_device_complete(uint8_t status, uint16_t length,
 int adapter_create_bonding(struct btd_adapter *adapter, const bdaddr_t *bdaddr,
                                        uint8_t addr_type, uint8_t io_cap)
 {
-       if (adapter->pair_device_id > 0) {
-               btd_error(adapter->dev_id,
-                       "Unable pair since another pairing is in progress");
-               return -EBUSY;
-       }
-
 #ifndef TIZEN_FEATURE_BLUEZ_MODIFY
        suspend_discovery(adapter);
 #endif
@@ -14001,11 +13879,14 @@ int adapter_bonding_attempt(struct btd_adapter *adapter, const bdaddr_t *bdaddr,
        bacpy(&data->bdaddr, bdaddr);
        data->addr_type = addr_type;
 
-       id = mgmt_send(adapter->mgmt, MGMT_OP_PAIR_DEVICE,
+       /* Due to a bug in the kernel it is possible that a LE pairing
+        * request never times out. Therefore, add a timer to clean up
+        * if no response arrives
+        */
+       id = mgmt_send_timeout(adapter->mgmt, MGMT_OP_PAIR_DEVICE,
                                adapter->dev_id, sizeof(cp), &cp,
                                pair_device_complete, data,
-                               free_pair_device_data);
-
+                               free_pair_device_data, BONDING_TIMEOUT);
        if (id == 0) {
                btd_error(adapter->dev_id, "Failed to pair %s for hci%u",
                                                        addr, adapter->dev_id);
@@ -14013,16 +13894,6 @@ int adapter_bonding_attempt(struct btd_adapter *adapter, const bdaddr_t *bdaddr,
                return -EIO;
        }
 
-       adapter->pair_device_id = id;
-
-       /* Due to a bug in the kernel it is possible that a LE pairing
-        * request never times out. Therefore, add a timer to clean up
-        * if no response arrives
-        */
-       adapter->pair_device_timeout = timeout_add_seconds(BONDING_TIMEOUT,
-                                               pair_device_timeout, data,
-                                               NULL);
-
        return 0;
 }