Skip to contentSkip to conversation

org.bluez.Device: Introduced PreferredBearer

PATCH In review Luiz Augusto von Dentz 3 participants 6 messages Feb 26, 2025 – Mar 6, 2025 Bluetooth drivers

lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH v3 1/4 org.bluez.Device: Introduced PreferredBearer

From: Luiz Augusto von Dentz <[email protected]>

This introduces PreferredBearer property which can be used to indicate what bearer shall be connected first rather than just using last seen bearer which may not work always since it seems some devices sometimes advertises on LE bearer but expects connections over BR/EDR e.g:

https://github.com/bluez/bluez/issues/1092

Also with the introduction of LE Audio this might become even more of a problem since most likely users would like to select which bearer to use rather than using the last-seen policy.

---
 doc/org.bluez.Device.rst | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)
doc/org.bluez.Device.rst +28 -1
index f8d4a68a6b41..13328249b0b0 100644
@@ -40,7 +40,8 @@ void Connect()
 	are skip and check latest seen bearer.
 
 	3. Connect last seen bearer, in case the timestamps are the same BR/EDR
-	takes precedence.
+	takes precedence, or in case **PreferredBearer** has been set to a
+	specific bearer then that is used instead.
 
 	Possible errors:
 
@@ -346,3 +347,29 @@ array{object, dict} Sets [readonly, experimental]
 	:byte Rank:
 
 		Rank of the device in the Set.
+
+string PreferredBearer [readwrite, optional, experimental]
+``````````````````````````````````````````````````````````
+
+	Indicate the preferred bearer when initiating a connection, only
+	available for dual-mode devices.
+
+	When changing from "bredr" to "le" the device will be removed from the
+	'auto-connect' list so it won't automatically be connected when
+	adverting.
+
+	Note: Changes only take effect when the device is disconnected.
+
+	Possible values:
+
+	:"last-seen":
+
+		Connect to last seen bearer first. Default.
+
+	:"bredr":
+
+		Connect to BR/EDR first.
+
+	:"le":
+
+		Connect to LE first.
Signature · 1 line
2.48.1
author maintainer
#1 lore raw

PATCH v3 2/4 device: Add implementation of PreferredBearer

From: Luiz Augusto von Dentz <[email protected]>

This adds initial implementation of PreferredBearer.

---
 src/device.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 96 insertions(+), 3 deletions(-)
src/device.c +96 -3
index ec97fc889056..de92e68d2780 100644
@@ -152,6 +152,7 @@ struct svc_callback {
 
 /* Per-bearer (LE or BR/EDR) device state */
 struct bearer_state {
+	bool prefer;
 	bool paired;
 	bool bonded;
 	bool connected;
@@ -2543,10 +2544,12 @@ static uint8_t select_conn_bearer(struct btd_device *dev)
 	time_t bredr_last = NVAL_TIME, le_last = NVAL_TIME;
 	time_t current = time(NULL);
 
-	/* Prefer bonded bearer in case only one is bonded */
-	if (dev->bredr_state.bonded && !dev->le_state.bonded )
+	/* Use preferred bearer or bonded bearer in case only one is bonded */
+	if (dev->bredr_state.prefer ||
+			(dev->bredr_state.bonded && !dev->le_state.bonded))
 		return BDADDR_BREDR;
-	else if (!dev->bredr_state.bonded && dev->le_state.bonded)
+	else if (dev->le_state.prefer ||
+			(!dev->bredr_state.bonded && dev->le_state.bonded))
 		return dev->bdaddr_type;
 
 	/* If the address is random it can only be connected over LE */
@@ -3340,6 +3343,92 @@ static const GDBusMethodTable device_methods[] = {
 	{ }
 };
 
+static const char *device_prefer_bearer_str(struct btd_device *device)
+{
+	if (device->bredr_state.prefer)
+		return "bredr";
+	else if (device->le_state.prefer)
+		return "le";
+	else
+		return "last-seen";
+}
+
+static gboolean
+dev_property_get_prefer_bearer(const GDBusPropertyTable *property,
+				DBusMessageIter *iter, void *data)
+{
+	struct btd_device *device = data;
+	const char *str = device_prefer_bearer_str(device);
+
+	dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &str);
+
+	return TRUE;
+}
+
+static void
+dev_property_set_prefer_bearer(const GDBusPropertyTable *property,
+					 DBusMessageIter *value,
+					 GDBusPendingPropertySet id, void *data)
+{
+	struct btd_device *device = data;
+	const char *str;
+
+	if (dbus_message_iter_get_arg_type(value) != DBUS_TYPE_STRING) {
+		g_dbus_pending_property_error(id,
+					ERROR_INTERFACE ".InvalidArguments",
+					"Invalid arguments in method call");
+		return;
+	}
+
+	dbus_message_iter_get_basic(value, &str);
+
+	if (!strcasecmp(device_prefer_bearer_str(device), str))
+		goto done;
+
+	if (!strcasecmp(str, "last-seen")) {
+		device->bredr_state.prefer = false;
+		device->le_state.prefer = false;
+	} else if (!strcasecmp(str, "bredr")) {
+		device->bredr_state.prefer = true;
+		device->le_state.prefer = false;
+		/* Remove device from auto-connect list so the kernel does not
+		 * attempt to auto-connect to it in case it starts advertising.
+		 */
+		device_set_auto_connect(device, FALSE);
+	} else if (!strcasecmp(str, "le")) {
+		device->le_state.prefer = true;
+		device->bredr_state.prefer = false;
+		/* Add device to auto-connect list */
+		device_set_auto_connect(device, TRUE);
+	} else {
+		g_dbus_pending_property_error(id,
+					ERROR_INTERFACE ".InvalidArguments",
+					"Invalid arguments in method call");
+		return;
+	}
+
+	g_dbus_emit_property_changed(dbus_conn, device->path,
+					DEVICE_INTERFACE, "PreferredBearer");
+
+done:
+	g_dbus_pending_property_success(id);
+}
+
+static gboolean
+dev_property_prefer_bearer_exists(const GDBusPropertyTable *property,
+						void *data)
+{
+	struct btd_device *device = data;
+
+	/* Check if both BR/EDR and LE bearer are connected/bonded */
+	if ((device->bredr_state.connected || device->bredr_state.bonded) &&
+			(device->le_state.connected || device->le_state.bonded))
+		return TRUE;
+
+	/* Check if both BR/EDR and LE are connectable */
+	return device->bredr_state.connectable && device->le_state.connectable;
+}
+
 static const GDBusPropertyTable device_properties[] = {
 	{ "Address", "s", dev_property_get_address },
 	{ "AddressType", "s", property_get_address_type },
@@ -3378,6 +3467,10 @@ static const GDBusPropertyTable device_properties[] = {
 				dev_property_wake_allowed_exist },
 	{ "Sets", "a{oa{sv}}", dev_property_get_set, NULL,
 				dev_property_set_exists },
+	{ "PreferredBearer", "s", dev_property_get_prefer_bearer,
+				dev_property_set_prefer_bearer,
+				dev_property_prefer_bearer_exists,
+				G_DBUS_PROPERTY_FLAG_EXPERIMENTAL },
 	{ }
 };
 
Signature · 1 line
2.48.1
author maintainer
#2 lore raw

PATCH v3 3/4 client: Add support get/set PreferredBearer

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This adds support for PreferredBearer which is printed with the likes of info command:

bluetoothctl> info <addr>
...
PreferredBearer: last-seen

It also introduces a new command to get/set the PreferredBearer:

[bluetoothctl]> bearer --help
Get/Set preferred bearer
Usage:
bearer <dev> [last-seen/bredr/le]

[bluetoothctl]> bearer <addr>
PreferredBearer: last-seen
[bluetoothctl]> bearer <addr> le
bluetoothd: @ MGMT Command: Add Device (0x0033) plen 8
LE Address: <addr>
Action: Auto-connect remote device (0x02)
[CHG] Device <addr> PreferredBearer: le
Changing le succeeded
[bluetoothctl]> bearer <addr>
PreferredBearer: le
[bluetoothctl]> bearer <addr> bredr
bluetoothd: @ MGMT Command: Remove Device (0x0034) plen 7
LE Address: <addr>
[CHG] Device <addr> PreferredBearer: bredr Changing bredr succeeded

---
 client/bluetoothctl.rst | 17 +++++++++++++++++
 client/main.c           | 27 +++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
client/bluetoothctl.rst +17 -0
index b6c2efa351d8..106ed971a59f 100644
@@ -288,6 +288,23 @@ Device information.
 
 :Usage: **> info <dev>**
 
+bearer
+------
+
+Get/Set preferred bearer.
+
+:Usage: **> bearer <dev> [last-seen/bredr/le]**
+:Example get preferred bearer:
+	| > bearer <addr>
+        |    PreferredBearer: last-seen
+:Example set preferred bearer to LE:
+	| > bearer <addr> le
+	| [CHG] Device <addr> PreferredBearer: le
+	| Changing le succeeded
+:Example set preferred bearer to BREDR:
+	| > bearer <addr> bredr
+	| [CHG] Device <addr> PreferredBearer: bredr
+	| Changing bredr succeeded
 
 Advertise Submenu
 =================
client/main.c +27 -0
index feb21a1163d2..76c9bc329c96 100644
@@ -1714,6 +1714,7 @@ static void cmd_info(int argc, char *argv[])
 	print_property(proxy, "AdvertisingFlags");
 	print_property(proxy, "AdvertisingData");
 	print_property(proxy, "Sets");
+	print_property(proxy, "PreferredBearer");
 
 	battery_proxy = find_proxies_by_path(battery_proxies,
 					g_dbus_proxy_get_path(proxy));
@@ -2086,6 +2087,30 @@ static void cmd_wake(int argc, char *argv[])
 	return bt_shell_noninteractive_quit(EXIT_FAILURE);
 }
 
+static void cmd_bearer(int argc, char *argv[])
+{
+	GDBusProxy *proxy;
+	char *str;
+
+	proxy = find_device(argc, argv);
+	if (!proxy)
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+
+	if (argc <= 2) {
+		print_property(proxy, "PreferredBearer");
+		return;
+	}
+
+	str = strdup(argv[2]);
+
+	if (g_dbus_proxy_set_property_basic(proxy, "PreferredBearer",
+					DBUS_TYPE_STRING, &str,
+					generic_callback, str, free))
+		return;
+
+	return bt_shell_noninteractive_quit(EXIT_FAILURE);
+}
+
 static void cmd_list_attributes(int argc, char *argv[])
 {
 	GDBusProxy *proxy;
@@ -3247,6 +3272,8 @@ static const struct bt_shell_menu main_menu = {
 							dev_generator },
 	{ "wake",         "[dev] [on/off]",    cmd_wake, "Get/Set wake support",
 							dev_generator },
+	{ "bearer",       "<dev> [last-seen/bredr/le]", cmd_bearer,
+				"Get/Set preferred bearer", dev_generator },
 	{ } },
 };
 
Signature · 1 line
2.48.1
author maintainer
#3 lore raw

PATCH v3 4/4 storage: Add support for storing PreferredBearer

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This adds support for storing PreferredBearer into device info file.

---
 doc/settings-storage.txt |  2 ++
 src/device.c             | 66 +++++++++++++++++++++++++++++-----------
 2 files changed, 51 insertions(+), 17 deletions(-)
doc/settings-storage.txt +2 -0
index 178e7d07bf11..4e9881eedf02 100644
@@ -293,6 +293,8 @@ Long term key) related to a remote device.
 			strings		remote in 128-bits UUID format,
 					separated by ";"
 
+  PreferredBearer	String		Preferred bearer for remote device
+
 
 [DeviceID] group contains:
 
src/device.c +49 -17

124 lines not on this page · raw message

Signature · 1 line
2.48.1
#4 lore raw
In reply to Luiz Augusto von Dentz

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=938178

---Test result---

Test Summary:

CheckPatch                    PENDING   0.23 seconds
GitLint                       PENDING   0.19 seconds
BuildEll                      PASS      20.53 seconds
BluezMake                     PASS      1523.85 seconds
MakeCheck                     PASS      13.59 seconds
MakeDistcheck                 PASS      159.75 seconds
CheckValgrind                 PASS      216.30 seconds
CheckSmatch                   PASS      285.52 seconds
bluezmakeextell               PASS      98.01 seconds
IncrementalBuild              PENDING   0.30 seconds
ScanBuild                     PASS      866.45 seconds

Details
##############################
Test: CheckPatch - PENDING
Desc: Run checkpatch.pl script
Output:

##############################
Test: GitLint - PENDING
Desc: Run gitlint
Output:

##############################
Test: IncrementalBuild - PENDING
Desc: Incremental build with the patches in the series Output:

---
Regards,
Linux Bluetooth

#5 lore raw
In reply to Luiz Augusto von Dentz
This series was applied to bluetooth/bluez.git (master) by Luiz Augusto von Dentz <[email protected]>:

Hello:

This series was applied to bluetooth/bluez.git (master) by Luiz Augusto von Dentz <[email protected]>:

Quoted 8 lines · On Wed, 26 Feb 2025 13:11:26 -0500 you wrote:

From: Luiz Augusto von Dentz <[email protected]>

This introduces PreferredBearer property which can be used to indicate what bearer shall be connected first rather than just using last seen bearer which may not work always since it seems some devices sometimes advertises on LE bearer but expects connections over BR/EDR e.g:

[...]

Here is the summary with links:
- [BlueZ,v3,1/4] org.bluez.Device: Introduced PreferredBearer

    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=a22935f9baf5
  - [BlueZ,v3,2/4] device: Add implementation of PreferredBearer
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=03240630b39b
  - [BlueZ,v3,3/4] client: Add support get/set PreferredBearer
    (no matching commit)
  - [BlueZ,v3,4/4] storage: Add support for storing PreferredBearer
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=4d3c721ee037

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html