* [PATCH BlueZ v3 1/2] device: Run reverse service discovery on the bonded bearer
@ 2026-11-25 16:58 Simon Mikuda
2026-11-25 16:58 ` [PATCH BlueZ v3 2/2] main.conf: Add 'paired' to ReverseServiceDiscovery Simon Mikuda
2026-11-25 23:47 ` [BlueZ,v3,1/2] device: Run reverse service discovery on the bonded bearer bluez.test.bot
0 siblings, 2 replies; 3+ messages in thread
From: Simon Mikuda @ 2026-11-25 16:58 UTC (permalink / raw)
To: linux-bluetooth; +Cc: Simon Mikuda
When a remote bonds with us, reverse service discovery is started after
DISCOVERY_TIMER, but it browses SDP whenever the device supports
BR/EDR, whichever bearer bonded. A dual-mode phone that connects and
bonds over LE is then paged for SDP while only LE is connected. A
browse on one bearer also makes a browse on the other fail with
-EBUSY, as there is a single request per device.
Keep the discovery timer and the browse request per bearer, and
discover SDP on BR/EDR and GATT on LE. Skip a bearer that is not
connected while the other one is: a BR/EDR link key derived from LE
pairing also completes bonding on BR/EDR. Cancelling a request now
stops only its own discovery, and the callbacks waiting for services
run once SDP completes.
Assisted-by: Claude:claude-opus-5-5
---
Changes in v3:
- skip reverse discovery on a bearer that is not connected while the
other one is: a BR/EDR key derived from LE pairing (CTKD) paged the
device for SDP (CI TestFunctional test_bap)
- cancel only the request's own SDP or GATT discovery in
browse_request_cancel()
- run the svc_callbacks only once BR/EDR services are resolved, not on
GATT completion
- main.conf: reword the commit message and the option's comment: the
profiles' accept, not discovery, reads the protected characteristics
src/device.c | 142 +++++++++++++++++++++++++++++----------------------
1 file changed, 81 insertions(+), 61 deletions(-)
diff --git a/src/device.c b/src/device.c
index 36a980d1e..6aefc8a8e 100644
--- a/src/device.c
+++ b/src/device.c
@@ -157,6 +157,8 @@ struct bearer_state {
bool connectable;
time_t last_seen;
time_t last_used;
+ unsigned int discov_timer; /* reverse service discovery timer */
+ struct browse_req *browse; /* service discovery request */
};
struct ltk_info {
@@ -246,9 +248,7 @@ struct btd_device {
bool connectable;
bool cable_pairing;
unsigned int disconn_timer;
- unsigned int discov_timer;
unsigned int temporary_timer; /* Temporary/disappear timer */
- struct browse_req *browse; /* service discover request */
struct bonding_req *bonding;
struct authentication_req *authr; /* authentication request */
uint8_t bonding_status;
@@ -787,8 +787,10 @@ static void browse_request_free(struct browse_req *req)
{
struct btd_device *device = req->device;
- if (device->browse == req)
- device->browse = NULL;
+ if (device->bredr_state.browse == req)
+ device->bredr_state.browse = NULL;
+ if (device->le_state.browse == req)
+ device->le_state.browse = NULL;
if (req->listener_id)
g_dbus_remove_watch(dbus_conn, req->listener_id);
@@ -882,9 +884,11 @@ static void browse_request_cancel(struct browse_req *req)
DBG("");
- bt_cancel_discovery(btd_adapter_get_address(adapter), &device->bdaddr);
-
- attio_cleanup(device);
+ if (req->type == BROWSE_SDP)
+ bt_cancel_discovery(btd_adapter_get_address(adapter),
+ &device->bdaddr);
+ else
+ attio_cleanup(device);
browse_request_free(req);
}
@@ -928,8 +932,10 @@ static void device_free(gpointer user_data)
if (device->disconn_timer)
timeout_remove(device->disconn_timer);
- if (device->discov_timer)
- timeout_remove(device->discov_timer);
+ if (device->bredr_state.discov_timer)
+ timeout_remove(device->bredr_state.discov_timer);
+ if (device->le_state.discov_timer)
+ timeout_remove(device->le_state.discov_timer);
if (device->temporary_timer)
timeout_remove(device->temporary_timer);
@@ -2076,8 +2082,10 @@ void device_request_disconnect(struct btd_device *device, DBusMessage *msg)
if (device->bonding)
bonding_request_cancel(device->bonding);
- if (device->browse)
- browse_request_cancel(device->browse);
+ if (device->bredr_state.browse)
+ browse_request_cancel(device->bredr_state.browse);
+ if (device->le_state.browse)
+ browse_request_cancel(device->le_state.browse);
if (device->att_io) {
g_io_channel_shutdown(device->att_io, FALSE, NULL);
@@ -2660,7 +2668,7 @@ void btd_device_update_allowed_services(struct btd_device *dev)
/* If service discovery is ongoing, let the service discovery complete
* callback call this function.
*/
- if (dev->browse) {
+ if (dev->bredr_state.browse || dev->le_state.browse) {
ba2str(&dev->bdaddr, addr);
DBG("service discovery of %s is ongoing. Skip updating allowed "
"services", addr);
@@ -2801,7 +2809,7 @@ int btd_device_connect_services(struct btd_device *dev, GSList *services)
GSList *l;
uint8_t bdaddr_type;
- if (dev->pending || dev->connect || dev->browse)
+ if (dev->pending || dev->connect || dev->bredr_state.browse)
return -EBUSY;
if (!btd_adapter_get_powered(dev->adapter))
@@ -2852,7 +2860,7 @@ DBusMessage *device_connect_profiles(struct btd_device *dev,
DBG("%s %s, client %s", dev->path, uuid ? uuid : "(all)",
dbus_message_get_sender(msg));
- if (dev->pending || dev->connect || dev->browse)
+ if (dev->pending || dev->connect || dev->bredr_state.browse)
return btd_error_in_progress_str(msg, ERR_BREDR_CONN_BUSY);
if (!btd_adapter_get_powered(dev->adapter)) {
@@ -3206,7 +3214,7 @@ static void browse_request_complete(struct browse_req *req, uint8_t type,
/* if successfully resolved services we need to free browsing request
* before passing message back to connect functions, otherwise
- * device->browse is set and "InProgress" error is returned instead
+ * the bearer's browse is set and "InProgress" error is returned instead
* of actually connecting services
*/
msg = dbus_message_ref(req->msg);
@@ -3251,7 +3259,7 @@ static void device_svc_resolved(struct btd_device *dev, uint8_t browse_type,
uint8_t bdaddr_type, int err)
{
struct bearer_state *state = get_state(dev, bdaddr_type);
- struct browse_req *req = dev->browse;
+ struct browse_req *req = state->browse;
DBG("%s err %d", dev->path, err);
@@ -3288,7 +3296,10 @@ static void device_svc_resolved(struct btd_device *dev, uint8_t browse_type,
if (req)
browse_request_complete(req, browse_type, bdaddr_type, err);
- while (dev->svc_callbacks) {
+ /* The callbacks wait for BR/EDR services, see
+ * device_wait_for_svc_complete().
+ */
+ while (bdaddr_type == BDADDR_BREDR && dev->svc_callbacks) {
struct svc_callback *cb = dev->svc_callbacks->data;
if (cb->idle_id > 0)
@@ -5658,8 +5669,10 @@ void device_remove(struct btd_device *device, gboolean remove_stored)
device_cancel_bonding(device, status);
}
- if (device->browse)
- browse_request_cancel(device->browse);
+ if (device->bredr_state.browse)
+ browse_request_cancel(device->bredr_state.browse);
+ if (device->le_state.browse)
+ browse_request_cancel(device->le_state.browse);
while (device->services != NULL) {
struct btd_service *service = device->services->data;
@@ -6364,7 +6377,7 @@ static void att_disconnected_cb(int err, void *user_data)
DBG("");
- if (device->browse)
+ if (device->le_state.browse)
goto done;
DBG("%s (%d)", strerror(err), err);
@@ -6392,7 +6405,7 @@ done:
static void register_gatt_services(struct btd_device *device)
{
- struct browse_req *req = device->browse;
+ struct browse_req *req = device->le_state.browse;
GSList *services = NULL;
if (!bt_gatt_client_is_ready(device->client))
@@ -6797,8 +6810,8 @@ static void att_connect_cb(GIOChannel *io, GError *gerr, gpointer user_data)
adapter_connect_list_add(device->adapter, device);
}
- if (device->browse)
- browse_request_complete(device->browse,
+ if (device->le_state.browse)
+ browse_request_complete(device->le_state.browse,
BROWSE_GATT,
device->bdaddr_type,
-ECONNABORTED);
@@ -6924,15 +6937,17 @@ static struct browse_req *browse_request_new(struct btd_device *device,
DBusMessage *msg)
{
struct browse_req *req;
+ struct bearer_state *state = type == BROWSE_SDP ?
+ &device->bredr_state : &device->le_state;
- if (device->browse)
+ if (state->browse)
return NULL;
req = g_new0(struct browse_req, 1);
req->device = device;
req->type = type;
- device->browse = req;
+ state->browse = req;
if (!msg)
return req;
@@ -7051,16 +7066,7 @@ static int device_browse_sdp(struct btd_device *device, DBusMessage *msg)
static gboolean device_is_browsing(struct btd_device *device,
uint8_t bdaddr_type)
{
- if (!device->browse)
- return FALSE;
-
- if (bdaddr_type == BDADDR_BREDR && device->browse->type == BROWSE_SDP)
- return TRUE;
-
- if (bdaddr_type != BDADDR_BREDR && device->browse->type == BROWSE_GATT)
- return TRUE;
-
- return FALSE;
+ return get_state(device, bdaddr_type)->browse != NULL;
}
void device_cancel_browse(struct btd_device *device, uint8_t bdaddr_type)
@@ -7078,12 +7084,13 @@ void device_cancel_browse(struct btd_device *device, uint8_t bdaddr_type)
else
attio_cleanup(device);
- browse_request_free(device->browse);
+ browse_request_free(get_state(device, bdaddr_type)->browse);
}
int device_discover_services(struct btd_device *device,
uint8_t bdaddr_type, DBusMessage *msg)
{
+ struct bearer_state *state = get_state(device, bdaddr_type);
int err;
if (bdaddr_type == BDADDR_BREDR)
@@ -7091,9 +7098,9 @@ int device_discover_services(struct btd_device *device,
else
err = device_browse_gatt(device, msg);
- if (err == 0 && device->discov_timer) {
- timeout_remove(device->discov_timer);
- device->discov_timer = 0;
+ if (err == 0 && state->discov_timer) {
+ timeout_remove(state->discov_timer);
+ state->discov_timer = 0;
}
return err;
@@ -7435,16 +7442,22 @@ bool device_is_connectable(struct btd_device *device)
return state->connectable;
}
-static bool start_discovery_cb(gpointer user_data)
+static bool start_sdp_discovery_cb(gpointer user_data)
{
struct btd_device *device = user_data;
- if (device->bredr)
- device_browse_sdp(device, NULL);
- else
- device_browse_gatt(device, NULL);
+ device->bredr_state.discov_timer = 0;
+ device_browse_sdp(device, NULL);
- device->discov_timer = 0;
+ return FALSE;
+}
+
+static bool start_gatt_discovery_cb(gpointer user_data)
+{
+ struct btd_device *device = user_data;
+
+ device->le_state.discov_timer = 0;
+ device_browse_gatt(device, NULL);
return FALSE;
}
@@ -7544,6 +7557,8 @@ void device_bonding_complete(struct btd_device *device, uint8_t bdaddr_type,
struct bonding_req *bonding = device->bonding;
struct authentication_req *auth = device->authr;
struct bearer_state *state = get_state(device, bdaddr_type);
+ struct bearer_state *other = bdaddr_type == BDADDR_BREDR ?
+ &device->le_state : &device->bredr_state;
int err;
DBG("bonding %p status 0x%02x", bonding, status);
@@ -7618,18 +7633,23 @@ void device_bonding_complete(struct btd_device *device, uint8_t bdaddr_type,
DBUS_TYPE_INVALID);
}
bonding_request_free(bonding);
- } else if (!state->svc_resolved) {
- if (!device->browse && !device->discov_timer &&
- btd_opts.reverse_discovery) {
- /* If we are not initiators and there is no currently
- * active discovery or discovery timer, set discovery
- * timer */
- DBG("setting timer for reverse service discovery");
- device->discov_timer = timeout_add_seconds(
- DISCOVERY_TIMER,
- start_discovery_cb,
- device, NULL);
- }
+ } else if (!state->svc_resolved && !state->browse &&
+ !state->discov_timer &&
+ (state->connected || !other->connected) &&
+ btd_opts.reverse_discovery) {
+ /* If we are not initiators and there is no currently
+ * active discovery or discovery timer, set discovery
+ * timer on the bearer that was bonded. A bearer bonded
+ * with a key derived over the other one (CTKD) is not
+ * connected while the other one is, so it is not
+ * discovered.
+ */
+ DBG("setting timer for reverse service discovery");
+ state->discov_timer = timeout_add_seconds(DISCOVERY_TIMER,
+ bdaddr_type == BDADDR_BREDR ?
+ start_sdp_discovery_cb :
+ start_gatt_discovery_cb,
+ device, NULL);
}
}
@@ -7667,12 +7687,12 @@ unsigned int device_wait_for_svc_complete(struct btd_device *dev,
if (state->svc_resolved || !btd_opts.reverse_discovery)
cb->idle_id = g_idle_add(svc_idle_cb, cb);
else {
- if (dev->discov_timer > 0)
- timeout_remove(dev->discov_timer);
+ if (dev->bredr_state.discov_timer > 0)
+ timeout_remove(dev->bredr_state.discov_timer);
- dev->discov_timer = timeout_add_seconds(
+ dev->bredr_state.discov_timer = timeout_add_seconds(
0,
- start_discovery_cb,
+ start_sdp_discovery_cb,
dev, NULL);
}
base-commit: 1608f836188610aabeff5a3164169ebe0360e443
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* [PATCH BlueZ v3 2/2] main.conf: Add 'paired' to ReverseServiceDiscovery
2026-11-25 16:58 [PATCH BlueZ v3 1/2] device: Run reverse service discovery on the bonded bearer Simon Mikuda
@ 2026-11-25 16:58 ` Simon Mikuda
2026-11-25 23:47 ` [BlueZ,v3,1/2] device: Run reverse service discovery on the bonded bearer bluez.test.bot
1 sibling, 0 replies; 3+ messages in thread
From: Simon Mikuda @ 2026-11-25 16:58 UTC (permalink / raw)
To: linux-bluetooth; +Cc: Simon Mikuda
When a device that is not paired connects to us over LE, its GATT
client is created and the profiles are accepted on its services. Their
attach reads characteristic values right away, e.g. BAP the PAC records
or CSIP the SIRK. When the remote protects them it answers Insufficient
Authentication (0x05) or Insufficient Encryption (0x0f), bt_att raises
the security level to retry the read, and the device is asked to pair
although nobody requested it.
ReverseServiceDiscovery = false avoids this for LE by not creating the
GATT client of such a link at all. Add 'paired' in addition to true
and false: with it, the GATT client of a device that connected to us,
and with it the profiles' accept, is started only once the device is
paired, and reverse SDP of a BR/EDR device likewise waits for pairing.
The default stays true.
Assisted-by: Claude:claude-opus-5-5
---
src/btd.h | 8 +++++++-
src/device.c | 25 ++++++++++++++++++++++---
src/main.c | 29 ++++++++++++++++++++++++++---
src/main.conf | 3 +++
4 files changed, 58 insertions(+), 7 deletions(-)
diff --git a/src/btd.h b/src/btd.h
index 9bf310271..2c897756c 100644
--- a/src/btd.h
+++ b/src/btd.h
@@ -48,6 +48,12 @@ enum bt_gatt_export_t {
BT_GATT_EXPORT_READ_WRITE,
};
+enum reverse_discovery_t {
+ REVERSE_DISCOVERY_NEVER,
+ REVERSE_DISCOVERY_ALWAYS,
+ REVERSE_DISCOVERY_PAIRED,
+};
+
struct btd_br_defaults {
uint16_t page_scan_type;
uint16_t page_scan_interval;
@@ -144,7 +150,7 @@ struct btd_opts {
struct btd_defaults defaults;
- bool reverse_discovery;
+ uint8_t reverse_discovery;
bool name_resolv;
bool debug_keys;
bool fast_conn;
diff --git a/src/device.c b/src/device.c
index 6aefc8a8e..38f2a94d1 100644
--- a/src/device.c
+++ b/src/device.c
@@ -350,6 +350,18 @@ bool btd_device_is_initiator(struct btd_device *dev)
return dev->att_io ? true : false;
}
+static bool reverse_discovery(struct btd_device *dev, uint8_t bdaddr_type)
+{
+ switch (btd_opts.reverse_discovery) {
+ case REVERSE_DISCOVERY_ALWAYS:
+ return true;
+ case REVERSE_DISCOVERY_PAIRED:
+ return device_is_paired(dev, bdaddr_type);
+ default:
+ return false;
+ }
+}
+
static GSList *find_service_with_profile(GSList *list, struct btd_profile *p)
{
GSList *l;
@@ -6476,7 +6488,8 @@ static void gatt_client_init(struct btd_device *device)
gatt_client_cleanup(device);
- if (!btd_device_is_initiator(device) && !btd_opts.reverse_discovery) {
+ if (!btd_device_is_initiator(device) &&
+ !reverse_discovery(device, device->bdaddr_type)) {
DBG("Reverse service discovery disabled: skipping GATT client");
return;
}
@@ -6975,6 +6988,12 @@ static int device_browse_gatt(struct btd_device *device, DBusMessage *msg)
if (!req)
return -EBUSY;
+ /* Connected without a client, as reverse discovery waited for the
+ * device to be paired: start it on the existing link.
+ */
+ if (!device->client && device->att)
+ gatt_client_init(device);
+
if (device->client) {
/*
* If discovery has not yet completed, then wait for gatt-client
@@ -7636,7 +7655,7 @@ void device_bonding_complete(struct btd_device *device, uint8_t bdaddr_type,
} else if (!state->svc_resolved && !state->browse &&
!state->discov_timer &&
(state->connected || !other->connected) &&
- btd_opts.reverse_discovery) {
+ reverse_discovery(device, bdaddr_type)) {
/* If we are not initiators and there is no currently
* active discovery or discovery timer, set discovery
* timer on the bearer that was bonded. A bearer bonded
@@ -7684,7 +7703,7 @@ unsigned int device_wait_for_svc_complete(struct btd_device *dev,
dev->svc_callbacks = g_slist_prepend(dev->svc_callbacks, cb);
- if (state->svc_resolved || !btd_opts.reverse_discovery)
+ if (state->svc_resolved || !reverse_discovery(dev, BDADDR_BREDR))
cb->idle_id = g_idle_add(svc_idle_cb, cb);
else {
if (dev->bredr_state.discov_timer > 0)
diff --git a/src/main.c b/src/main.c
index 3a051c5a4..3672bb707 100644
--- a/src/main.c
+++ b/src/main.c
@@ -924,6 +924,30 @@ static bool parse_config_bool(GKeyFile *config, const char *group,
return true;
}
+static void parse_reverse_discovery(GKeyFile *config)
+{
+ char *str = NULL;
+ bool val;
+
+ if (parse_config_bool(config, "General", "ReverseServiceDiscovery",
+ &val)) {
+ btd_opts.reverse_discovery = val ? REVERSE_DISCOVERY_ALWAYS :
+ REVERSE_DISCOVERY_NEVER;
+ return;
+ }
+
+ if (!parse_config_string(config, "General", "ReverseServiceDiscovery",
+ &str))
+ return;
+
+ if (!strcmp(str, "paired"))
+ btd_opts.reverse_discovery = REVERSE_DISCOVERY_PAIRED;
+ else
+ warn("Invalid value for ReverseServiceDiscovery: %s", str);
+
+ g_free(str);
+}
+
static void parse_privacy(GKeyFile *config)
{
char *str = NULL;
@@ -1096,8 +1120,7 @@ static void parse_general(GKeyFile *config)
&btd_opts.pairto,
0, UINT32_MAX);
parse_device_id(config);
- parse_config_bool(config, "General", "ReverseServiceDiscovery",
- &btd_opts.reverse_discovery);
+ parse_reverse_discovery(config);
parse_config_bool(config, "General", "NameResolving",
&btd_opts.name_resolv);
parse_config_bool(config, "General", "DebugKeys",
@@ -1473,7 +1496,7 @@ static void init_defaults(void)
btd_opts.pairto = DEFAULT_PAIRABLE_TIMEOUT;
btd_opts.discovto = DEFAULT_DISCOVERABLE_TIMEOUT;
btd_opts.tmpto = DEFAULT_TEMPORARY_TIMEOUT;
- btd_opts.reverse_discovery = TRUE;
+ btd_opts.reverse_discovery = REVERSE_DISCOVERY_ALWAYS;
btd_opts.name_resolv = TRUE;
btd_opts.debug_keys = FALSE;
btd_opts.refresh_discovery = TRUE;
diff --git a/src/main.conf b/src/main.conf
index c9906148d..3ee6897c1 100644
--- a/src/main.conf
+++ b/src/main.conf
@@ -34,6 +34,9 @@
# BITE tester doesn't like us doing reverse SDP for some test cases, for LE
# this disables the GATT client functionally so it can be used in system which
# can only operate as peripheral (see also GATT Client option).
+# Possible values: 'true', 'false' or 'paired'. With 'paired' it is done only
+# once the device is paired, so that the profiles do not start pairing with a
+# device that connected to us by reading characteristics that need encryption.
# Defaults to 'true'.
#ReverseServiceDiscovery = true
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* RE: [BlueZ,v3,1/2] device: Run reverse service discovery on the bonded bearer
2026-11-25 16:58 [PATCH BlueZ v3 1/2] device: Run reverse service discovery on the bonded bearer Simon Mikuda
2026-11-25 16:58 ` [PATCH BlueZ v3 2/2] main.conf: Add 'paired' to ReverseServiceDiscovery Simon Mikuda
@ 2026-11-25 23:47 ` bluez.test.bot
1 sibling, 0 replies; 3+ messages in thread
From: bluez.test.bot @ 2026-11-25 23:47 UTC (permalink / raw)
To: linux-bluetooth, simon.mikuda
[-- Attachment #1: Type: text/plain, Size: 1027 bytes --]
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/series/1222362/
---Test result---
Test Summary:
CheckPatch PASS 1.00 seconds
GitLint PASS 0.49 seconds
BuildEll PASS 20.74 seconds
BluezMake PASS 312.85 seconds
CheckSmatch WARNING 318.03 seconds
bluezmakeextell PASS 100.47 seconds
TestFunctional PASS 1231.79 seconds
IncrementalBuild PASS 344.07 seconds
ScanBuild PASS 1212.94 seconds
Details
##############################
Test: CheckSmatch - WARNING
Desc: Run smatch tool with source
Output:
src/main.c: note: in included file (through src/device.h):
https://github.com/bluez/bluez/pull/2922
---
Regards,
Linux Bluetooth
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-25 23:47 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 16:58 [PATCH BlueZ v3 1/2] device: Run reverse service discovery on the bonded bearer Simon Mikuda
2026-11-25 16:58 ` [PATCH BlueZ v3 2/2] main.conf: Add 'paired' to ReverseServiceDiscovery Simon Mikuda
2026-11-25 23:47 ` [BlueZ,v3,1/2] device: Run reverse service discovery on the bonded bearer bluez.test.bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox