Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.43 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796154814; b=ZUfxYO6oyHns/fFXB5pwRkYagK2jHpXNLaiUaCOX3OroQRtwZ6n/p14nE74p3mPkq3Qy vuzXnEq2hs36KNJZ+SH6pTmU9MQ3QCCLozFgG3Urji5weqnwVqDnP9WZBd13IbA2DcWpY GoPcqjUJM1Ie8gITuKS1N9w5/9eOE4NPI9AaJBc0yeer92yyVAalaQWJDiV28TW9ki4R5 VBTpYuCzCEBI09NbBClq7Iof7Q+ba+Vn0GoCdOEhQ505V6uuGF1UXqsBgGx0OAjo/dXkI teY1xqJhF0eaQJtDO2jldrTG59ddLnOyhYPq+MWg3npzeE8OMKpEulOkryJD3sDhVFg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796154814; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:MIME-Version:Content-Transfer-Encoding; bh=CEaZxEaFxch5MOu01uLM4S+Yjc0xO+DQK8lLaRxQoAM=; b=gbnOm6ueL3e6i7oFTMPbuPogTElTjFVWctMFZz8Qdz/0zI5JKhWBGXbxiUMA+YEJC/By Hb7SN4PLxR2+aeDGS3J+3VHkYQWFwv1O+FLe5ZWWZj10YrMRH2O/mFimdRAamyj/CyZut RqMXBZkEUp2Ikhg4B6EE+0w0Whh7gX08aj6fvAbqqRhOC2gdwNmgIE48zPqL/zsbmsqnM l5zARetfcoTL4ELTTAvveDUTalfDYPWPrwVtWWILyvq89pCPuRjmHdYsn7LHZ/NggrNsI NLMOPjc36ayzsv4cblCvPC0xTH7WAAcQohB0j2dFXsY7qRz+tdZWAU1soB5TH0un5BQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=streamunlimited.com; spf=pass smtp.mailfrom=streamunlimited.com; dkim=pass header.d=streamunlimited.com header.i=@streamunlimited.com header.a=rsa-sha256 header.s=google header.b=KTAF69dn; arc=none smtp.remote-ip=209.85.128.43 Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 78C07606A31A for ; Tue, 1 Dec 2026 19:53:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 78C07606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=streamunlimited.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=streamunlimited.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=streamunlimited.com header.i=@streamunlimited.com header.a=rsa-sha256 header.s=google header.b=KTAF69dn Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-4bbd2c5931bso7264675e9.2 for ; Tue, 01 Dec 2026 11:53:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=streamunlimited.com; s=google; t=1796154813; x=1796759613; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=CEaZxEaFxch5MOu01uLM4S+Yjc0xO+DQK8lLaRxQoAM=; b=KTAF69dnhNwiocT/l04rZd2ooW8fQf3H+qiPZsz+x/aAFJLIv4sXJnJua1dtroH/TD NLGdCTWE14/00FyPbTaCeXyIPjA/IWCbF4SIzfklm3hIzY/4gSVSL7w3QPC50a+JCVZp /4QFob9Vsxnuye9nefUDjIZWcSVmkgbsuz5ag= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796154813; x=1796759613; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=CEaZxEaFxch5MOu01uLM4S+Yjc0xO+DQK8lLaRxQoAM=; b=LsvubPLK5mNlH2Gd2U/krsuLrE8HvZa7fWXRrea3sYrycSWqeb6tFz4ePdW7dP8LGu yJ88bXHQoefFt2tn8N33BmobH/tlUo6h9f23gi9S5W/j5Igd24yQpQ70tX4PDv1zT7kg vVlTyQQ7x0RAaWn7iqgOTjq+qvfrS13BOIVSqVUkFSeVNW7kNxmMZPqpXWhcTxkVWuQ2 4Yxcpc/yj2Qcf2aFOygaKWQCdUg5oZ7eDy5QR7Q0u2QXagFdH1SojeCvJ4OMCJ4D/5hF ufQYl01U1LPpqocJBsDx7nSKe3lXvDg0qm18Gy1rNUl8zZ24momcGe28lbgkqQaw+tz4 Qz3A== X-Gm-Message-State: APuU4Z+p0Q+xFDvcfDWAGxBg35+nP9+Pitp2vivEK16qadkAfvGUsC1V koNr0yWk+6y/jAhZmcmxHM5S3AhKWyyNxuqsIB8AOarmRxxfBHpVJPZ5Rk00Ne42K8pKbzAHlpN UVgHz X-Gm-Gg: AZFdTZebMzv1zrmIPRnYk+K8ufEYrxUhLAyn+0cqPnm+iOSrwh02I8Edv/bjLVSxAnh s7YvX/+OVpZoBL3iR8Wp2WQtoq+49lL4eh/4ah1pYZHPnqbqbGjdJW5upZCDTI6SCY04IRPO5N/ IkVdNhulglIZTvtu0mNGkv5DC42WHZqFF2VExHFUCyuW6/89oNZdQKnZzhCa7DO4OgZ8cWbRo2k dHmRew1MqrlmyUbrl7JhUBvdun1Z1iqpJqwSpSDRs+PPTkSjkb/nTVxFIdGGfg2fiw4wTo7GPcO mYtlIfKXVk3D/r/lEEXOEjvFZC6kRoSCejNeTf6mjpTZNW9n5TnrgERQatcd8BEN1jqmeR0jTmq FGxsfWKf64TkUGvWcoc6sEu384qtofconLUNoHMYBqCe4qDYOXHOgKbFNhgDBVlBFqOS46++yIZ 5PPHj2T/SFMGAmNsOT6pyTC2V6hIiOgxkYJ85PeH+kRxgy+zURSAOodLhk2sOshDszLU4mL07FK R3pm+blKOzJqeT0xfDnAIlqbkZnwBJlEC9HbcwkZbzJwHpN/YwVnuNGtm0i4nesOflVvTsNGf9w vm51mxmwKhNxg0FpEWC3QIE9acjmLEXuhBNh9l9LuKJKjp56TEnKIOSaRjrmjTCFqQ== X-Received: by 2002:a05:600c:3b0e:b0:4a1:efba:511 with SMTP id 5b1f17b1804b1-4bb8882508emr801993855e9.35.1796154812753; Tue, 01 Dec 2026 11:53:32 -0800 (PST) Received: from SMI-ubuntu.tail1789c7.ts.net ([2a01:390:0:101:ee93:d613:741b:a9ba]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bbb39dd4e7sm345417185e9.0.2026.12.01.11.53.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 11:53:32 -0800 (PST) From: Simon Mikuda To: linux-bluetooth@vger.kernel.org Cc: Simon Mikuda Subject: [PATCH BlueZ v4 1/2] device: Run reverse service discovery on the bonded bearer Date: Tue, 1 Dec 2026 20:53:29 +0100 Message-ID: <20261201195330.3564672-1-simon.mikuda@streamunlimited.com> X-Mailer: git-send-email 2.43.0 X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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, and it doesn't complete a Pair request made over LE either. 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 v4: - device: a key derived for the other bearer (CTKD) no longer completes the Pair request or starts discovery on that bearer - device_wait_for_svc_complete(): an SDP browse in progress is waited for whatever ReverseServiceDiscovery says src/device.c | 149 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 88 insertions(+), 61 deletions(-) diff --git a/src/device.c b/src/device.c index 720b4af69..0e34dd380 100644 --- a/src/device.c +++ b/src/device.c @@ -158,6 +158,8 @@ struct bearer_state { uint8_t disconn_reason; 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 { @@ -247,9 +249,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; @@ -788,8 +788,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); @@ -883,9 +885,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); } @@ -929,8 +933,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); @@ -2077,8 +2083,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); @@ -2661,7 +2669,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); @@ -2802,7 +2810,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)) @@ -2853,7 +2861,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)) { @@ -3207,7 +3215,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); @@ -3252,7 +3260,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); @@ -3289,7 +3297,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) @@ -5665,8 +5676,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; @@ -6371,7 +6384,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); @@ -6399,7 +6412,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)) @@ -6804,8 +6817,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); @@ -6931,15 +6944,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; @@ -7058,16 +7073,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) @@ -7085,12 +7091,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) @@ -7098,9 +7105,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; @@ -7442,16 +7449,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); + + return FALSE; +} + +static bool start_gatt_discovery_cb(gpointer user_data) +{ + struct btd_device *device = user_data; - device->discov_timer = 0; + device->le_state.discov_timer = 0; + device_browse_gatt(device, NULL); return FALSE; } @@ -7551,6 +7564,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); @@ -7589,6 +7604,13 @@ void device_bonding_complete(struct btd_device *device, uint8_t bdaddr_type, device_set_paired(device, bdaddr_type); + /* A key derived for the other bearer (CTKD) doesn't complete the + * pairing request, nor does it start discovery on that bearer. + */ + if (bonding && (bonding->bdaddr_type == BDADDR_BREDR) != + (bdaddr_type == BDADDR_BREDR)) + bonding = NULL; + /* If services are already resolved just reply to the pairing * request */ @@ -7625,18 +7647,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); } } @@ -7674,12 +7701,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: eecfd959cf5ae9a24ee007975439747779804be1 -- 2.43.0