Linux bluetooth development
 help / color / mirror / Atom feed
* [PATCH BlueZ v2 0/2] input: Do not re-page HID devices that disconnected on purpose
@ 2026-11-25  4:17 Dylan MacKenzie
  2026-11-25  4:17 ` [PATCH BlueZ v2 1/2] device: Add btd_device_get_disconnect_reason Dylan MacKenzie
  2026-11-25  4:17 ` [PATCH BlueZ v2 2/2] input/device: Only auto-reconnect on link loss Dylan MacKenzie
  0 siblings, 2 replies; 6+ messages in thread
From: Dylan MacKenzie @ 2026-11-25  4:17 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Dylan MacKenzie

The input plugin re-pages a HID device after its connection is closed
even when the link was terminated on purpose, which takes devices that
can be bonded to several hosts, e.g. earbuds, away from the host they
just switched to. Details are in patch 2.

Patch 1 keeps the reason of the last disconnection in the per-bearer
state of the device, patch 2 uses it to skip the reconnection when the
link was terminated by the remote device or by the local host. The
reason is checked both before the reconnection timer is started and
every time it fires, and a timer still pending from an earlier link
loss is cancelled when the connection is then closed on purpose. Link
loss keeps being handled as before.

I tested with Pixel Buds Pro 2 with these changes applied on top of 5.87.
They no longer reconnect to my Linux laptop after I connect them to my
phone.

According to Claude:

    One behaviour change to be aware of: the kernel reports Remote Device
    Terminated Connection due to Power Off (0x15) as
    MGMT_DEV_DISCONN_REMOTE as well, so a device with ReconnectMode "host"
    that announces it is powering off is no longer paged if it comes back
    within 3 minutes.

Hopefully a maintainer can evaluate the correctness/significance of
this.

Changes in v2:
- Check the disconnect reason every time the timer fires as well as
  before starting it, so a device that was lost, came back and then
  left on purpose before the next attempt is not paged by the timer
  started for the earlier loss.
- Cancel a timer still pending from an earlier link loss when the
  connection is then closed on purpose.

The v1 problem was found by the AI review (Copilot) of the GitHub mirror
of this series, bluez/bluez#2864 and seems legit. It suggested
cancelling the pending timer where the reason was already checked. More
robust is to check the condition twice, once before registering a timer
and once when the timer fires.


Dylan MacKenzie (2):
  device: Add btd_device_get_disconnect_reason
  input/device: Only auto-reconnect on link loss

 profiles/input/device.c | 34 +++++++++++++++++++++++++++++++++-
 src/device.c            |  7 +++++++
 src/device.h            |  1 +
 3 files changed, 41 insertions(+), 1 deletion(-)

-- 
2.56.1


^ permalink raw reply	[flat|nested] 6+ messages in thread
* [PATCH BlueZ 1/2] device: Add btd_device_get_disconnect_reason
@ 2026-11-21 20:58 Dylan MacKenzie
  2026-11-21 23:18 ` input: Do not re-page HID devices that disconnected on purpose bluez.test.bot
  0 siblings, 1 reply; 6+ messages in thread
From: Dylan MacKenzie @ 2026-11-21 20:58 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Dylan MacKenzie

Keep the reason of the last disconnection in the bearer state and add
btd_device_get_disconnect_reason so it can be queried per bearer. The
new field takes the place of padding so struct bearer_state does not
grow.

Assisted-by: Claude:claude-fable-5-2
---
 src/device.c | 7 +++++++
 src/device.h | 1 +
 2 files changed, 8 insertions(+)

diff --git a/src/device.c b/src/device.c
index 36a980d1e..720b4af69 100644
--- a/src/device.c
+++ b/src/device.c
@@ -155,6 +155,7 @@ struct bearer_state {
 	bool svc_resolved;
 	bool initiator;
 	bool connectable;
+	uint8_t disconn_reason;
 	time_t last_seen;
 	time_t last_used;
 };
@@ -3864,6 +3865,11 @@ bool btd_device_bdaddr_type_connected(struct btd_device *dev, uint8_t type)
 	return dev->le_state.connected;
 }
 
+uint8_t btd_device_get_disconnect_reason(struct btd_device *dev, uint8_t type)
+{
+	return get_state(dev, type)->disconn_reason;
+}
+
 bool btd_device_bdaddr_type_connectable(struct btd_device *dev, uint8_t type)
 {
 	if (type == BDADDR_BREDR)
@@ -4071,6 +4077,7 @@ void device_remove_connection(struct btd_device *device, uint8_t bdaddr_type,
 
 	state->connected = false;
 	state->initiator = false;
+	state->disconn_reason = reason;
 	device->general_connect = FALSE;
 
 	device_set_svc_refreshed(device, false);
diff --git a/src/device.h b/src/device.h
index 047a894c3..4527c16d1 100644
--- a/src/device.h
+++ b/src/device.h
@@ -127,6 +127,7 @@ void device_set_flags(struct btd_device *device, uint8_t flags);
 bool btd_device_is_connected(struct btd_device *dev);
 bool btd_device_bearer_is_connected(struct btd_device *dev);
 bool btd_device_bdaddr_type_connected(struct btd_device *dev, uint8_t type);
+uint8_t btd_device_get_disconnect_reason(struct btd_device *dev, uint8_t type);
 bool btd_device_bdaddr_type_connectable(struct btd_device *dev, uint8_t type);
 bool btd_device_bdaddr_type_seen(struct btd_device *dev, uint8_t type);
 bool btd_device_bdaddr_type_initiator(struct btd_device *dev, uint8_t type);
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-25 14:45 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25  4:17 [PATCH BlueZ v2 0/2] input: Do not re-page HID devices that disconnected on purpose Dylan MacKenzie
2026-11-25  4:17 ` [PATCH BlueZ v2 1/2] device: Add btd_device_get_disconnect_reason Dylan MacKenzie
2026-11-25 14:45   ` input: Do not re-page HID devices that disconnected on purpose bluez.test.bot
2026-11-25  4:17 ` [PATCH BlueZ v2 2/2] input/device: Only auto-reconnect on link loss Dylan MacKenzie
  -- strict thread matches above, loose matches on Subject: below --
2026-11-21 20:58 [PATCH BlueZ 1/2] device: Add btd_device_get_disconnect_reason Dylan MacKenzie
2026-11-21 23:18 ` input: Do not re-page HID devices that disconnected on purpose bluez.test.bot
2026-11-23 17:37   ` Luiz Augusto von Dentz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox