Skip to contentSkip to conversation

org.bluez.Adapter: Add AutoConnect to SetDiscoveryFilter

PATCH Applied Luiz Augusto von Dentz 3 participants 6 messages May 23, 2025 Bluetooth drivers

lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH v2 1/4 org.bluez.Adapter: Add AutoConnect to SetDiscoveryFilter

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

This adds AutoConnect filter option to SetDiscoveryFilter which can be used by application to automatically connect devices found matching a pattern.

---
 doc/org.bluez.Adapter.rst | 42 ++++++++++++++++++++++-----------------
 1 file changed, 24 insertions(+), 18 deletions(-)
doc/org.bluez.Adapter.rst +24 -18
index 021e4f8e5a9f..c5b8ed8e9132 100644
@@ -150,29 +150,35 @@ void SetDiscoveryFilter(dict filter)
 		that don't set any pattern as it work as a logical OR, also
 		setting empty string "" pattern will match any device found.
 
-		When discovery filter is set, Device objects will be created as
-		new devices with matching criteria are discovered regardless of
-		they are connectable or discoverable which enables listening to
-		non-connectable and non-discoverable devices.
+	:bool AutoConnect (Default false):
 
-		When multiple clients call SetDiscoveryFilter, their filters are
-		internally merged, and notifications about new devices are sent
-		to all clients. Therefore, each client must check that device
-		updates actually match its filter.
+		Connect to discovered devices automatically if a Pattern has
+		been set and it matches the device address or name and it is
+		connectable.
 
-		When SetDiscoveryFilter is called multiple times by the same
-		client, last filter passed will be active for given client.
+	When discovery filter is set, Device objects will be created as new
+	devices with matching criteria are discovered regardless of they are
+	connectable or discoverable which enables listening to non-connectable
+	and non-discoverable devices.
 
-		SetDiscoveryFilter can be called before StartDiscovery.
-		It is useful when client will create first discovery session,
-		to ensure that proper scan will be started right after call to
-		StartDiscovery.
+	When multiple clients call SetDiscoveryFilter, their filters are
+	internally merged, and notifications about new devices are sent to all
+	clients. Therefore, each client must check that device updates actually
+	match its filter.
 
-		Possible errors:
+	When SetDiscoveryFilter is called multiple times by the same client,
+	last filter passed will be active for given client.
 
-		:org.bluez.Error.NotReady:
-		:org.bluez.Error.NotSupported:
-		:org.bluez.Error.Failed:
+	SetDiscoveryFilter can be called before StartDiscovery.
+	It is useful when client will create first discovery session,
+	to ensure that proper scan will be started right after call to
+	StartDiscovery.
+
+	Possible errors:
+
+	:org.bluez.Error.NotReady:
+	:org.bluez.Error.NotSupported:
+	:org.bluez.Error.Failed:
 
 array{string} GetDiscoveryFilters()
 ```````````````````````````````````
Signature · 1 line
2.49.0
author maintainer
#1 lore raw

PATCH v2 2/4 adapter: Implement SetFilterPolicy AutoConnect filter

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

This implements AutoConnect filter option in SetFilterPolicy method according to its documentation.

---
 src/adapter.c |  55 +++++++++++++++++++++----
 src/device.c  | 111 +++++++++++++++++++++++++++-----------------------
 2 files changed, 107 insertions(+), 59 deletions(-)
src/adapter.c +47 -8
index fd425e6d2fe4..6f0699302257 100644
@@ -214,6 +214,7 @@ struct discovery_filter {
 	GSList *uuids;
 	bool duplicate;
 	bool discoverable;
+	bool auto_connect;
 };
 
 struct discovery_client {
@@ -2697,6 +2698,21 @@ static bool parse_pattern(DBusMessageIter *value,
 	return true;
 }
 
+static bool parse_auto_connect(DBusMessageIter *value,
+					struct discovery_filter *filter)
+{
+	dbus_bool_t connect;
+
+	if (dbus_message_iter_get_arg_type(value) != DBUS_TYPE_BOOLEAN)
+		return false;
+
+	dbus_message_iter_get_basic(value, &connect);
+
+	filter->auto_connect = connect;
+
+	return true;
+}
+
 struct filter_parser {
 	const char *name;
 	bool (*func)(DBusMessageIter *iter, struct discovery_filter *filter);
@@ -2708,6 +2724,7 @@ struct filter_parser {
 	{ "DuplicateData", parse_duplicate_data },
 	{ "Discoverable", parse_discoverable },
 	{ "Pattern", parse_pattern },
+	{ "AutoConnect", parse_auto_connect },
 	{ }
 };
 
@@ -2748,6 +2765,7 @@ static bool parse_discovery_filter_dict(struct btd_adapter *adapter,
 	(*filter)->type = get_scan_type(adapter);
 	(*filter)->duplicate = false;
 	(*filter)->discoverable = false;
+	(*filter)->auto_connect = false;
 	(*filter)->pattern = NULL;
 
 	dbus_message_iter_init(msg, &iter);
@@ -2794,11 +2812,12 @@ static bool parse_discovery_filter_dict(struct btd_adapter *adapter,
 		goto invalid_args;
 
 	DBG("filtered discovery params: transport: %d rssi: %d pathloss: %d "
-		" duplicate data: %s discoverable %s pattern %s",
+		" duplicate data: %s discoverable %s pattern %s auto-connect %s",
 		(*filter)->type, (*filter)->rssi, (*filter)->pathloss,
 		(*filter)->duplicate ? "true" : "false",
 		(*filter)->discoverable ? "true" : "false",
-		(*filter)->pattern);
+		(*filter)->pattern,
+		(*filter)->auto_connect ? "true" : "false");
 
 	return true;
 
@@ -7212,7 +7231,7 @@ static void filter_duplicate_data(void *data, void *user_data)
 
 static bool device_is_discoverable(struct btd_adapter *adapter,
 					struct eir_data *eir, const char *addr,
-					uint8_t bdaddr_type)
+					uint8_t bdaddr_type, bool *auto_connect)
 {
 	GSList *l;
 	bool discoverable;
@@ -7242,15 +7261,21 @@ static bool device_is_discoverable(struct btd_adapter *adapter,
 		discoverable = false;
 
 		pattern_len = strlen(filter->pattern);
-		if (!pattern_len)
+		if (!pattern_len) {
+			*auto_connect = filter->auto_connect;
 			return true;
+		}
 
-		if (!strncmp(filter->pattern, addr, pattern_len))
+		if (!strncmp(filter->pattern, addr, pattern_len)) {
+			*auto_connect = filter->auto_connect;
 			return true;
+		}
 
 		if (eir->name && !strncmp(filter->pattern, eir->name,
-							pattern_len))
+							pattern_len)) {
+			*auto_connect = filter->auto_connect;
 			return true;
+		}
 	}
 
 	return discoverable;
@@ -7274,6 +7299,7 @@ void btd_adapter_device_found(struct btd_adapter *adapter,
 	bool name_resolve_failed;
 	bool scan_rsp;
 	bool duplicate = false;
+	bool auto_connect = false;
 	struct queue *matched_monitors = NULL;
 
 	confirm = (flags & MGMT_DEV_FOUND_CONFIRM_NAME);
@@ -7310,7 +7336,7 @@ void btd_adapter_device_found(struct btd_adapter *adapter,
 	ba2str(bdaddr, addr);
 
 	discoverable = device_is_discoverable(adapter, &eir_data, addr,
-							bdaddr_type);
+						bdaddr_type, &auto_connect);
 
 	dev = btd_adapter_find_device(adapter, bdaddr, bdaddr_type);
 	if (!dev) {
@@ -7330,7 +7356,14 @@ void btd_adapter_device_found(struct btd_adapter *adapter,
 				MGMT_SETTING_ISO_SYNC_RECEIVER))
 			monitoring = true;
 
-		if (!discoverable && !monitoring && not_connectable) {
+		/* Monitor Devices advertising RSI since those can be
+		 * coordinated sets not marked as visible but their object are
+		 * needed.
+		 */
+		if (eir_data.rsi)
+			monitoring = true;
+
+		if (!discoverable && !monitoring) {
 			eir_data_free(&eir_data);
 			return;
 		}
@@ -7468,6 +7501,12 @@ void btd_adapter_device_found(struct btd_adapter *adapter,
 	adapter->discovery_found = g_slist_prepend(adapter->discovery_found,
 									dev);
 
+	/* If device has a pattern match and it also set auto-connect then
+	 * attempt to connect.
+	 */
+	if (!btd_device_is_connected(dev) && auto_connect)
+		btd_device_connect_services(dev, NULL);
+
 	return;
 
 connect_le:
src/device.c +60 -51

132 lines not on this page · raw message

Signature · 1 line
2.49.0
author maintainer
#2 lore raw

PATCH v2 3/4 client: Add scan.auto-connect command

In reply to Luiz Augusto von Dentz

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

[bluetoothctl]> scan.auto-connect --help
Set/Get auto-connect filter
Usage:
auto-connect [on/off]

---
 client/main.c | 40 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 39 insertions(+), 1 deletion(-)
client/main.c +39 -1
index 5d53a7be11e4..46687c6dd944 100644
@@ -161,6 +161,7 @@ static struct set_discovery_filter_args {
 	size_t uuids_len;
 	dbus_bool_t duplicate;
 	dbus_bool_t discoverable;
+	dbus_bool_t auto_connect;
 	bool set;
 	bool active;
 	unsigned int timeout;
@@ -1253,9 +1254,14 @@ static void set_discovery_filter_setup(DBusMessageIter *iter, void *user_data)
 						DBUS_TYPE_BOOLEAN,
 						&args->discoverable);
 
-	if (args->pattern != NULL)
+	if (args->pattern != NULL) {
 		g_dbus_dict_append_entry(&dict, "Pattern", DBUS_TYPE_STRING,
 						&args->pattern);
+		if (args->auto_connect)
+			g_dbus_dict_append_entry(&dict, "AutoConnect",
+						DBUS_TYPE_BOOLEAN,
+						&args->auto_connect);
+	}
 
 	dbus_message_iter_close_container(iter, &dict);
 }
@@ -1492,6 +1498,29 @@ static void cmd_scan_filter_pattern(int argc, char *argv[])
 		set_discovery_filter(false);
 }
 
+static void cmd_scan_filter_auto_connect(int argc, char *argv[])
+{
+	if (argc < 2 || !strlen(argv[1])) {
+		bt_shell_printf("AutoConnect: %s\n",
+				filter.auto_connect ? "on" : "off");
+		return bt_shell_noninteractive_quit(EXIT_SUCCESS);
+	}
+
+	if (!strcmp(argv[1], "on"))
+		filter.auto_connect = true;
+	else if (!strcmp(argv[1], "off"))
+		filter.auto_connect = false;
+	else {
+		bt_shell_printf("Invalid option: %s\n", argv[1]);
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+
+	filter.set = false;
+
+	if (filter.active)
+		set_discovery_filter(false);
+}
+
 static void filter_clear_uuids(void)
 {
 	g_strfreev(filter.uuids);
@@ -1531,6 +1560,11 @@ static void filter_clear_pattern(void)
 	filter.pattern = NULL;
 }
 
+static void filter_auto_connect(void)
+{
+	filter.auto_connect = false;
+}
+
 struct clear_entry {
 	const char *name;
 	void (*clear) (void);
@@ -1544,6 +1578,7 @@ static const struct clear_entry filter_clear[] = {
 	{ "duplicate-data", filter_clear_duplicate },
 	{ "discoverable", filter_clear_discoverable },
 	{ "pattern", filter_clear_pattern },
+	{ "auto-connect", filter_auto_connect },
 	{}
 };
 
@@ -3219,6 +3254,9 @@ static const struct bt_shell_menu scan_menu = {
 	{ "pattern", "[value]", cmd_scan_filter_pattern,
 				"Set/Get pattern filter",
 				NULL },
+	{ "auto-connect", "[on/off]", cmd_scan_filter_auto_connect,
+				"Set/Get auto-connect filter",
+				NULL },
 	{ "clear",
 	"[uuids/rssi/pathloss/transport/duplicate-data/discoverable/pattern]",
 				cmd_scan_filter_clear,
Signature · 1 line
2.49.0
author maintainer
#3 lore raw

PATCH v2 4/4 bluetoothctl-scan: Add documentation for auto-connect command

In reply to Luiz Augusto von Dentz

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

This adds documentation for auto-connect command.

---
 client/bluetoothctl-scan.rst | 10 ++++++++++
 1 file changed, 10 insertions(+)
client/bluetoothctl-scan.rst +10 -0
index b4b8315926a2..cee255131622 100644
@@ -122,6 +122,16 @@ When set, it disregards device discoverable flags.
 :Usage: **> pattern [value]**
 
 
+auto-connect
+------------
+
+Set/Get auto-connect filter.
+
+Connect to discovered devices automatically if pattern filter has been set and
+it matches the device address or name and the device is connectable.
+
+:Usage: **> auto-connect [on/off]**
+
 clear
 -----
 
Signature · 1 line
2.49.0
#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=965873

---Test result---

Test Summary:

CheckPatch                    PENDING   0.31 seconds
GitLint                       PENDING   0.23 seconds
BuildEll                      PASS      20.13 seconds
BluezMake                     PASS      2841.09 seconds
MakeCheck                     PASS      20.62 seconds
MakeDistcheck                 PASS      198.80 seconds
CheckValgrind                 PASS      278.24 seconds
CheckSmatch                   PASS      301.87 seconds
bluezmakeextell               PASS      127.98 seconds
IncrementalBuild              PENDING   0.29 seconds
ScanBuild                     PASS      912.05 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 Fri, 23 May 2025 10:25:02 -0400 you wrote:

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

This adds AutoConnect filter option to SetDiscoveryFilter which can be used by application to automatically connect devices found matching a pattern.

---
 doc/org.bluez.Adapter.rst | 42 ++++++++++++++++++++++-----------------
 1 file changed, 24 insertions(+), 18 deletions(-)

Here is the summary with links:
- [BlueZ,v2,1/4] org.bluez.Adapter: Add AutoConnect to SetDiscoveryFilter

    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=3a8d778b0a9a
  - [BlueZ,v2,2/4] adapter: Implement SetFilterPolicy AutoConnect filter
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=af7d5dc61154
  - [BlueZ,v2,3/4] client: Add scan.auto-connect command
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=aba81b553348
  - [BlueZ,v2,4/4] bluetoothctl-scan: Add documentation for auto-connect command
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=c888c682fbd4

You are awesome, thank you!

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