Linux bluetooth development
 help / color / mirror / Atom feed
* [PATCH BlueZ] audio/hfp-hf: Destroy MediaTransport1 on codec change/disconnect
@ 2026-11-29 18:47 Frédéric Danis
  2026-11-29 19:38 ` [BlueZ] " bluez.test.bot
  0 siblings, 1 reply; 2+ messages in thread
From: Frédéric Danis @ 2026-11-29 18:47 UTC (permalink / raw)
  To: linux-bluetooth

The HF profile created an org.bluez.MediaTransport1 object for the
SCO audio whenever a call started, but never destroyed it when the
codec changed or the device disconnected (e.g. SLC dropped after
hanging up and disconnecting from the phone). The stale transport(s)
kept being listed by "bluetoothctl transport.list" forever.

Since a device only ever has one SCO connection at a time, clear the
previous codec's transport as soon as a new codec is selected, and
clear the current one when the device goes away, mirroring what
a2dp's clear_config() does.

Assisted-by: Copilot:claude-sonnet-5
---
 profiles/audio/hfp-hf.c | 56 ++++++++++++++++++++++++++++++++++-------
 profiles/audio/media.c  | 15 +++++++++++
 profiles/audio/media.h  |  2 ++
 3 files changed, 64 insertions(+), 9 deletions(-)

diff --git a/profiles/audio/hfp-hf.c b/profiles/audio/hfp-hf.c
index bc6d7229f..56c5f649e 100644
--- a/profiles/audio/hfp-hf.c
+++ b/profiles/audio/hfp-hf.c
@@ -138,6 +138,19 @@ static struct hfp_server *find_server(GSList *list, struct btd_adapter *a)
 	return NULL;
 }
 
+static struct media_endpoint *find_server_endpoint(struct hfp_server *server,
+							uint8_t codec)
+{
+	GSList *l;
+
+	for (l = server->endpoints; l; l = l->next) {
+		if (media_endpoint_get_codec(l->data) == codec)
+			return l->data;
+	}
+
+	return NULL;
+}
+
 static void unregister_endpoint(gpointer data, gpointer user_data)
 {
 	struct media_endpoint *ep = data;
@@ -248,9 +261,21 @@ static void device_destroy(struct hfp_device *dev)
 
 	server = find_server(servers,
 		device_get_adapter(telephony_get_device(dev->telephony)));
-	if (server)
+	if (server) {
+		struct media_endpoint *ep;
+
+		/* Destroy the MediaTransport1 created for this device's SCO
+		 * audio, if any, so it does not linger after the device is
+		 * gone.
+		 */
+		ep = find_server_endpoint(server, dev->codec);
+		if (ep)
+			hfp_hf_clear_configuration(ep,
+					telephony_get_device(dev->telephony));
+
 		g_slist_foreach(server->endpoints, unregister_endpoint,
 					dev->telephony);
+	}
 
 	telephony_unregister_interface(dev->telephony);
 }
@@ -422,21 +447,34 @@ static bool hfp_hf_select_codec(uint8_t codec, void *user_data)
 	struct hfp_device *dev = user_data;
 	struct btd_adapter *adapter;
 	struct hfp_server *server;
-	GSList *l;
+	struct media_endpoint *ep;
 
 	DBG("Selecting codec: %u", codec);
 	adapter = device_get_adapter(telephony_get_device(dev->telephony));
 	server = find_server(servers, adapter);
 
-	for (l = server->endpoints; l; l = l->next) {
-		if (media_endpoint_get_codec(l->data) == codec) {
-			dev->codec = codec;
-			return true;
-		}
+	ep = find_server_endpoint(server, codec);
+	if (!ep) {
+		DBG("Unsupported codec: %u", codec);
+		return false;
 	}
 
-	DBG("Unsupported codec: %u", codec);
-	return false;
+	if (dev->codec != codec) {
+		/* A device only ever has one SCO connection at a time, so
+		 * the previous codec's transport, if any, is no longer
+		 * valid.
+		 */
+		struct media_endpoint *prev;
+
+		prev = find_server_endpoint(server, dev->codec);
+		if (prev)
+			hfp_hf_clear_configuration(prev,
+					telephony_get_device(dev->telephony));
+	}
+
+	dev->codec = codec;
+
+	return true;
 }
 
 static struct hfp_hf_callbacks hf_session_callbacks = {
diff --git a/profiles/audio/media.c b/profiles/audio/media.c
index 13fead886..f1c8906e4 100644
--- a/profiles/audio/media.c
+++ b/profiles/audio/media.c
@@ -1501,6 +1501,21 @@ gboolean hfp_hf_set_configuration(struct media_endpoint *endpoint,
 						cb, user_data, destroy, -1);
 }
 
+void hfp_hf_clear_configuration(struct media_endpoint *endpoint,
+					struct btd_device *device)
+{
+	struct media_transport *transport;
+
+	if (!endpoint)
+		return;
+
+	transport = find_device_transport(endpoint, device);
+	if (!transport)
+		return;
+
+	clear_configuration(endpoint, transport);
+}
+
 static bool endpoint_init_sco(struct media_endpoint *endpoint,
 						int *err)
 {
diff --git a/profiles/audio/media.h b/profiles/audio/media.h
index eb95a8b06..c20238f1a 100644
--- a/profiles/audio/media.h
+++ b/profiles/audio/media.h
@@ -89,3 +89,5 @@ gboolean hfp_hf_set_configuration(struct media_endpoint *endpoint,
 					media_endpoint_cb_t cb,
 					void *user_data,
 					GDestroyNotify destroy);
+void hfp_hf_clear_configuration(struct media_endpoint *endpoint,
+					struct btd_device *device);
-- 
2.43.0


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

* RE: [BlueZ] audio/hfp-hf: Destroy MediaTransport1 on codec change/disconnect
  2026-11-29 18:47 [PATCH BlueZ] audio/hfp-hf: Destroy MediaTransport1 on codec change/disconnect Frédéric Danis
@ 2026-11-29 19:38 ` bluez.test.bot
  0 siblings, 0 replies; 2+ messages in thread
From: bluez.test.bot @ 2026-11-29 19:38 UTC (permalink / raw)
  To: linux-bluetooth, frederic.danis.oss

[-- Attachment #1: Type: text/plain, Size: 1259 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/1225941/

---Test result---

Test Summary:
CheckPatch                    PASS      0.26 seconds
GitLint                       PASS      0.15 seconds
BuildEll                      PASS      13.18 seconds
BluezMake                     PASS      202.61 seconds
MakeCheck                     PASS      3.00 seconds
MakeDistcheck                 PASS      101.36 seconds
CheckValgrind                 PASS      106.12 seconds
CheckSmatch                   PASS      182.48 seconds
bluezmakeextell               PASS      65.75 seconds
TestFunctional                FAIL      963.90 seconds
IncrementalBuild              PASS      207.73 seconds
ScanBuild                     PASS      698.05 seconds

Details
##############################
Test: TestFunctional - FAIL
Desc: Run test-functional
Output:
FAIL functional.test_bap::test_bass_past_earbuds_transport_acquire[hosts11-vm3]: AssertionError: Failed to acquire: org.bluez.Error.Failed


https://github.com/bluez/bluez/pull/2946

---
Regards,
Linux Bluetooth


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

end of thread, other threads:[~2026-11-29 19:38 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-29 18:47 [PATCH BlueZ] audio/hfp-hf: Destroy MediaTransport1 on codec change/disconnect Frédéric Danis
2026-11-29 19:38 ` [BlueZ] " 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