Skip to contentSkip to conversation

adapter: Add retry when bonding device returns connection failure

PATCH In review Chengyi Zhao 3 participants 5 messages Jul 1, 2024 – Jul 3, 2024 1 message not in the archive Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 adapter: Add retry when bonding device returns connection failure

When a user initiates pairing with a BLE Bluetooth mouse, MGMT_STATUS_CONNECT_FAILED(0x04) is returned with a low probability, resulting in pairing failure. To improve user experience, retry bonding is performed when MGMT_STATUS_CONNECT_FAILED is returned.

Just retry once when MGMT_STATUS_CONNECT_FAILED occurs because this status may be continuously returned.

Debug log:
bluetoothd[1539]: src/adapter.c:pair_device_complete() Connect Failed (0x04)
bluetoothd[1539]: src/adapter.c:bonding_attempt_complete() hci0 bdaddr DD:EC:0F:57:A9:2E type 2 status 0x4
bluetoothd[1539]: src/device.c:device_bonding_complete() bonding 0x5591f87230 status 0x04
bluetoothd[1539]: src/device.c:btd_device_set_temporary() temporary 1 bluetoothd[1539]: src/device.c:device_bonding_failed() status 4

HCI package:
Frame 2969: 7 bytes on wire (56 bits), 7 bytes captured (56 bits) Bluetooth
Bluetooth HCI H4
Bluetooth HCI Event - Disconnect Complete
Event Code: Disconnect Complete (0x05)
Parameter Total Length: 4
Status: Success (0x00)
Connection Handle: 0x0040
Reason: Connection Failed to be Established (0x3e)

---
 src/adapter.c |  4 ++++
 src/device.c  | 24 ++++++++++++++++++++++++
 src/device.h  |  2 ++
 3 files changed, 30 insertions(+)
src/adapter.c +4 -0
index bb49a1eca..574fa7665 100644
@@ -8371,6 +8371,10 @@ static void bonding_attempt_complete(struct btd_adapter *adapter,
 		}
 	}
 
+	/* Retry once when status is MGMT_STATUS_CONNECT_FAILED */
+	if (device && device_bonding_check_connection(device, status))
+		return;
+
 	/* Ignore disconnects during retry. */
 	if (status == MGMT_STATUS_DISCONNECTED &&
 					device && device_is_retrying(device))
src/device.c +24 -0
index 097b1fbba..12fabbff1 100644
@@ -290,6 +290,8 @@ struct btd_device {
 	time_t		name_resolve_failed_time;
 
 	int8_t		volume;
+
+	uint8_t bonding_status;
 };
 
 static const uint16_t uuid_list[] = {
@@ -6559,6 +6561,28 @@ bool device_remove_svc_complete_callback(struct btd_device *dev,
 	return false;
 }
 
+gboolean device_bonding_check_connection(struct btd_device *device,
+								uint8_t status)
+{
+	if (status == MGMT_STATUS_CONNECT_FAILED) {
+
+		if (device->bonding_status != MGMT_STATUS_CONNECT_FAILED) {
+			device->bonding_status = MGMT_STATUS_CONNECT_FAILED;
+
+			DBG("status is 0x%x, retry once.", status);
+
+			if (device_bonding_attempt_retry(device) == 0)
+				return TRUE;
+		}
+	} else {
+		device->bonding_status = status;
+
+		DBG("device->bonding_status is 0x%x.", device->bonding_status);
+	}
+
+	return FALSE;
+}
+
 gboolean device_is_bonding(struct btd_device *device, const char *sender)
 {
 	struct bonding_req *bonding = device->bonding;
src/device.h +2 -0
index 0794f92d0..7c269cc4d 100644
@@ -111,6 +111,8 @@ uint8_t btd_device_get_bdaddr_type(struct btd_device *dev);
 bool device_is_retrying(struct btd_device *device);
 void device_bonding_complete(struct btd_device *device, uint8_t bdaddr_type,
 							uint8_t status);
+gboolean device_bonding_check_connection(struct btd_device *device,
+							uint8_t status);
 gboolean device_is_bonding(struct btd_device *device, const char *sender);
 void device_bonding_attempt_failed(struct btd_device *device, uint8_t status);
 void device_bonding_failed(struct btd_device *device, uint8_t status);
Signature · 1 line
2.20.1
#1 lore raw

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=867072

---Test result---

Test Summary:

CheckPatch                    PASS      0.34 seconds
GitLint                       PASS      0.23 seconds
BuildEll                      PASS      26.68 seconds
BluezMake                     PASS      1941.59 seconds
MakeCheck                     PASS      15.25 seconds
MakeDistcheck                 PASS      186.76 seconds
CheckValgrind                 PASS      264.28 seconds
CheckSmatch                   PASS      369.14 seconds
bluezmakeextell               PASS      125.03 seconds
IncrementalBuild              PASS      1689.38 seconds
ScanBuild                     PASS      1091.47 seconds

---
Regards,
Linux Bluetooth

#2 lore raw
In reply to Chengyi Zhao

Hi,

Quoted 107 lines · Chengyi Zhao

When a user initiates pairing with a BLE Bluetooth mouse, MGMT_STATUS_CONNECT_FAILED(0x04) is returned with a low probability, resulting in pairing failure. To improve user experience, retry bonding is performed when MGMT_STATUS_CONNECT_FAILED is returned.

Just retry once when MGMT_STATUS_CONNECT_FAILED occurs because this status may be continuously returned.

Debug log:
bluetoothd[1539]: src/adapter.c:pair_device_complete() Connect Failed (0x04)
bluetoothd[1539]: src/adapter.c:bonding_attempt_complete() hci0 bdaddr DD:EC:0F:57:A9:2E type 2 status 0x4
bluetoothd[1539]: src/device.c:device_bonding_complete() bonding 0x5591f87230 status 0x04
bluetoothd[1539]: src/device.c:btd_device_set_temporary() temporary 1 bluetoothd[1539]: src/device.c:device_bonding_failed() status 4

HCI package:
Frame 2969: 7 bytes on wire (56 bits), 7 bytes captured (56 bits) Bluetooth
Bluetooth HCI H4
Bluetooth HCI Event - Disconnect Complete
Event Code: Disconnect Complete (0x05)
Parameter Total Length: 4
Status: Success (0x00)
Connection Handle: 0x0040
Reason: Connection Failed to be Established (0x3e)

---
 src/adapter.c |  4 ++++
 src/device.c  | 24 ++++++++++++++++++++++++
 src/device.h  |  2 ++
 3 files changed, 30 insertions(+)
src/adapter.c +4 -0
index bb49a1eca..574fa7665 100644
@@ -8371,6 +8371,10 @@ static void bonding_attempt_complete(struct btd_adapter *adapter,
                }
        }
 
+       /* Retry once when status is MGMT_STATUS_CONNECT_FAILED */
+       if (device && device_bonding_check_connection(device, status))
+               return;
+
        /* Ignore disconnects during retry. */
        if (status == MGMT_STATUS_DISCONNECTED &&
                                        device && device_is_retrying(device))
src/device.c +24 -0
index 097b1fbba..12fabbff1 100644
@@ -290,6 +290,8 @@ struct btd_device {
        time_t          name_resolve_failed_time;
 
        int8_t          volume;
+
+       uint8_t bonding_status;
 };
 
 static const uint16_t uuid_list[] = {
@@ -6559,6 +6561,28 @@ bool device_remove_svc_complete_callback(struct btd_device *dev,
        return false;
 }
 
+gboolean device_bonding_check_connection(struct btd_device *device,
+                                                               uint8_t status)
+{
+       if (status == MGMT_STATUS_CONNECT_FAILED) {
+
+               if (device->bonding_status != MGMT_STATUS_CONNECT_FAILED) {
+                       device->bonding_status = MGMT_STATUS_CONNECT_FAILED;
+
+                       DBG("status is 0x%x, retry once.", status);
+
+                       if (device_bonding_attempt_retry(device) == 0)
+                               return TRUE;
+               }
+       } else {
+               device->bonding_status = status;
+
+               DBG("device->bonding_status is 0x%x.", device->bonding_status);
+       }
+
+       return FALSE;
+}
+
 gboolean device_is_bonding(struct btd_device *device, const char *sender)
 {
        struct bonding_req *bonding = device->bonding;
src/device.h +2 -1
index 0794f92d0..7c269cc4d 100644
@@ -111,6 +111,8 @@ uint8_t btd_device_get_bdaddr_type(struct btd_device *dev);
 bool device_is_retrying(struct btd_device *device);
 void device_bonding_complete(struct btd_device *device, uint8_t bdaddr_type,
                                                        uint8_t status);
+gboolean device_bonding_check_connection(struct btd_device *device,
+                                                       uint8_t status);
 gboolean device_is_bonding(struct btd_device *device, const char *sender);
 void device_bonding_attempt_failed(struct btd_device *device, uint8_t status);
 void device_bonding_failed(struct btd_device *device, uint8_t status);
--
Quoted 1 line

2.20.1

Here is what Ive actually had in mind:

src/adapter.c +0 -0
index bb49a1ecad23..f1cc4f2ed25a 100644
@@ -8363,12 +8363,17 @@ static void bonding_attempt_complete(struct

btd_adapter *adapter,

        else
                device = btd_adapter_find_device(adapter, bdaddr, addr_type);

- if (status == MGMT_STATUS_AUTH_FAILED && adapter->pincode_requested) {
- /* On faliure, issue a bonding_retry if possible. */
+ switch (status) {
+ case MGMT_STATUS_AUTH_FAILED:
+ if (!adapter->pincode_requested)
+ break;
+ /* fall through */
+ case MGMT_STATUS_CONNECT_FAILED:

                if (device != NULL) {
                        if (device_bonding_attempt_retry(device) == 0)
                                return;
                }
+               break;
        }

        /* Ignore disconnects during retry. */
Signature · 1 line
Luiz Augusto von Dentz

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#4 lore raw

Hi,

Quoted 26 lines

Here is what Ive actually had in mind:

src/adapter.c +7 -2
index bb49a1ecad23..f1cc4f2ed25a 100644
@@ -8363,12 +8363,17 @@ static void bonding_attempt_complete(struct
 btd_adapter *adapter,
        else
                device = btd_adapter_find_device(adapter, bdaddr, addr_type);
 
-       if (status == MGMT_STATUS_AUTH_FAILED && adapter->pincode_requested) {
-               /* On faliure, issue a bonding_retry if possible. */
+       switch (status) {
+       case MGMT_STATUS_AUTH_FAILED:
+               if (!adapter->pincode_requested)
+                       break;
+       /* fall through */
+       case MGMT_STATUS_CONNECT_FAILED:
                if (device != NULL) {
                        if (device_bonding_attempt_retry(device) == 0)
                                return;
                }
+               break;
        }
 
Quoted 1 line
        /* Ignore disconnects during retry. */

Great, I think you are right if kernel does not continue to return MGMT_STATUS_CONNECT_FAILED status.

My idea is to retry only once after returning MGMT_STATUS_CONNECT_FAILED, it can avoid repeated retry when continuing to return MGMT_STATUS_CONNECT_FAILED.

Cheers,
Chengyi

author
#5 lore raw
In reply to Luiz Augusto von Dentz

Hi guys,

I would like to ask if the patch I submitted is valuable. Maybe its effect is weak, but do you agree to merge it? Thanks.

Quoted 98 lines

When a user initiates pairing with a BLE Bluetooth mouse, MGMT_STATUS_CONNECT_FAILED(0x04) is returned with a low probability, resulting in pairing failure. To improve user experience, retry bonding is performed when MGMT_STATUS_CONNECT_FAILED is returned.

Just retry once when MGMT_STATUS_CONNECT_FAILED occurs because this status may be continuously returned.

Debug log:
bluetoothd[1539]: src/adapter.c:pair_device_complete() Connect Failed (0x04)
bluetoothd[1539]: src/adapter.c:bonding_attempt_complete() hci0 bdaddr DD:EC:0F:57:A9:2E type 2 status 0x4
bluetoothd[1539]: src/device.c:device_bonding_complete() bonding 0x5591f87230 status 0x04
bluetoothd[1539]: src/device.c:btd_device_set_temporary() temporary 1 bluetoothd[1539]: src/device.c:device_bonding_failed() status 4

HCI package:
Frame 2969: 7 bytes on wire (56 bits), 7 bytes captured (56 bits) Bluetooth
Bluetooth HCI H4
Bluetooth HCI Event - Disconnect Complete
Event Code: Disconnect Complete (0x05)
Parameter Total Length: 4
Status: Success (0x00)
Connection Handle: 0x0040
Reason: Connection Failed to be Established (0x3e)
---

src/adapter.c |  4 ++++
src/device.c  | 24 ++++++++++++++++++++++++
src/device.h  |  2 ++
3 files changed, 30 insertions(+)
src/adapter.c +4 -0
index bb49a1eca..574fa7665 100644
@@ -8371,6 +8371,10 @@ static void bonding_attempt_complete(struct btd_adapter *adapter,
 	}
 	}
 
+	/* Retry once when status is MGMT_STATUS_CONNECT_FAILED */
+	if (device && device_bonding_check_connection(device, status))
+	return;
+
 	/* Ignore disconnects during retry. */
 	if (status == MGMT_STATUS_DISCONNECTED &&
 	device && device_is_retrying(device))
src/device.c +24 -0
index 097b1fbba..12fabbff1 100644
@@ -290,6 +290,8 @@ struct btd_device {
 	time_t	name_resolve_failed_time;
 
 	int8_t	volume;
+
+	uint8_t bonding_status;
 };
 
 static const uint16_t uuid_list[] = {
@@ -6559,6 +6561,28 @@ bool device_remove_svc_complete_callback(struct btd_device *dev,
 	return false;
 }
 
+gboolean device_bonding_check_connection(struct btd_device *device,
+	uint8_t status)
+{
+	if (status == MGMT_STATUS_CONNECT_FAILED) {
+
+	if (device->bonding_status != MGMT_STATUS_CONNECT_FAILED) {
+	device->bonding_status = MGMT_STATUS_CONNECT_FAILED;
+
+	DBG("status is 0x%x, retry once.", status);
+
+	if (device_bonding_attempt_retry(device) == 0)
+	return TRUE;
+	}
+	} else {
+	device->bonding_status = status;
+
+	DBG("device->bonding_status is 0x%x.", device->bonding_status);
+	}
+
+	return FALSE;
+}
+
 gboolean device_is_bonding(struct btd_device *device, const char *sender)
 {
 	struct bonding_req *bonding = device->bonding;
src/device.h +0 -0
index 0794f92d0..7c269cc4d 100644

src/device.h uint8_t btd_device_get_bdaddr_type(struct btd_device *dev); · line 118

Quoted 8 lines
src/device.h +2 -0
@@ -111,6 +111,8 @@ uint8_t btd_device_get_bdaddr_type(struct btd_device *dev);
 bool device_is_retrying(struct btd_device *device);
 void device_bonding_complete(struct btd_device *device, uint8_t bdaddr_type,
 	uint8_t status);
+gboolean device_bonding_check_connection(struct btd_device *device,
+	uint8_t status);
 gboolean device_is_bonding(struct btd_device *device, const char *sender);
 void device_bonding_attempt_failed(struct btd_device *device, uint8_t status);
 void device_bonding_failed(struct btd_device *device, uint8_t status);

Cheers,
Chengyi