Linux bluetooth development
 help / color / mirror / Atom feed
* [RFC PATCH BlueZ] asha: Wait for connection event length requests
@ 2026-12-02 16:04 Kyle Pepper
  0 siblings, 0 replies; only message in thread
From: Kyle Pepper @ 2026-12-02 16:04 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: marcel, luiz.dentz

Request at least 7.5 ms connection event length hints for ASHA and
restore the previous preference on stop if no later writer replaced it.

Wait asynchronously for the kernel request to complete before START.
Bound the wait, propagate errors, cancel pending connection callbacks
during teardown, and continue waiting for AudioStatus after START.
Retain playback on kernels without completion reporting. Propagate socket
request errors before START, including on legacy getters.

Assisted-by: Codex:GPT-6
---
Base: BlueZ eecfd959cf5ae9a24ee007975439747779804be1.
Requires the separate Linux BT_LE_CONN_EVENT_LEN RFC. No verified dependency
submission URL is available at preparation. This is the first public
CE RFC, with no previous CE email available for threading.

PHY and observed-interval notification are already in this base:
https://github.com/bluez/bluez/commit/9659adb6819d7e0284d0d2ba226a3894423d8db4
https://github.com/bluez/bluez/commit/353011d828a7822ef05e5d9939da56a142cb0f61
The older STATUS v6 exports are not a prerequisite for this revision.
Connection interval, latency and supervision defaults are unchanged.

Validation: the preceding source passed 37 native programs. This local
review passes all seven profile cases natively and with ASan/UBSan/leak
checks, including ten completion and five connection-lifetime scenarios.
New regressions reproduce on the preceding source with both legacy and
completion-aware getters; the prior gate-bypass control also failed.
The existing interval fixture also covers the new CE lifecycle and late
connection callbacks; no new test executable or file is added.

The saved stable integration passed a 900-second human trial, but included
other local changes. This exact upstream revision has no hardware or PTS
result. The hint is advisory; restoration is a value comparison, not atomic
arbitration with another application. Human source review is required.

 doc/l2cap-protocol.rst    |  52 ++++++
 lib/bluetooth/bluetooth.h |  11 ++
 profiles/audio/asha.c     | 310 +++++++++++++++++++++++++++++---
 unit/test-asha-conn.c     | 450 ++++++++++++++++++++++++++++++++++++++++++++--
 4 files changed, 792 insertions(+), 31 deletions(-)

diff --git a/doc/l2cap-protocol.rst b/doc/l2cap-protocol.rst
index 8f0af9b8a..a721e0def 100644
--- a/doc/l2cap-protocol.rst
+++ b/doc/l2cap-protocol.rst
@@ -246,6 +246,58 @@ High-speed (AMP) channel policy, possible values:
     **BT_CHANNEL_POLICY_BREDR_PREFERRED**, 1, BR/EDR Preferred
     **BT_CHANNEL_POLICY_BREDR_PREFERRED**, 2, AMP Preferred
 
+BT_LE_CONN_EVENT_LEN
+~~~~~~~~~~~~~~~~~~~~
+
+Request local LE connection event length hints using::
+
+    struct bt_le_conn_event_len {
+        uint16_t min;
+        uint16_t max;
+    };
+
+Both fields use 0.625 ms units; zero means no preference. The minimum must
+not exceed the maximum. These are advisory controller scheduling hints,
+not reserved airtime or measured event lengths.
+
+The socket must be connected to an LE link. Setting the option requires
+the local Central role. A successful setsockopt queues an asynchronous
+LE Connection Update preserving the live interval, latency and supervision
+timeout. It does not confirm controller acceptance. Getsockopt returns
+the latest local request, including requests awaiting controller completion.
+
+The request belongs to the underlying connection, shared by its sockets.
+The latest request replaces earlier values. Closing the socket does not
+reset the request; disconnecting the link does. Applications sharing a
+connection must coordinate requests and restoration. Subsequent local
+connection parameter updates preserve these hints.
+
+Older kernels return ENOPROTOOPT. An unconnected socket returns ENOTCONN;
+a BR/EDR link or a setter in the Peripheral role returns EOPNOTSUPP.
+A setter with a size other than sizeof(struct bt_le_conn_event_len), or
+with min greater than max, returns EINVAL.
+
+An eight-byte getsockopt buffer may use::
+
+    struct bt_le_conn_event_len_state {
+        struct bt_le_conn_event_len request;
+        int32_t status;
+    };
+
+The first four bytes retain the requested min/max values. The optional signed,
+native-endian status is -EINPROGRESS while CE work is queued or executing,
+zero after the latest queued CE command completes successfully, or a negative
+errno after failure. An older completion does not clear a newer pending request.
+Zero on a connection with no CE request means no work is pending, not that a
+nonzero airtime hint was applied. Applications must also check the requested
+values. Successful completion confirms the HCI command, not reserved airtime.
+The setter remains exactly four bytes. Older kernels return only that four-byte
+prefix; callers must inspect the returned length before reading status.
+Some experimental CE v1 kernels leave the supplied length unchanged after
+copying four bytes. Initialize status to INT32_MIN before the call; an unchanged
+sentinel means completion reporting is unavailable. Kernel status values are
+zero or a negated errno, so they cannot equal this sentinel.
+
 BT_PHY (since Linux 5.10)
 -------------------------
 
diff --git a/lib/bluetooth/bluetooth.h b/lib/bluetooth/bluetooth.h
index 58bb65d7b..1e662666a 100644
--- a/lib/bluetooth/bluetooth.h
+++ b/lib/bluetooth/bluetooth.h
@@ -376,6 +376,17 @@ enum {
 
 #define BT_SCM_PKT_SEQNUM	0x05
 
+#define BT_LE_CONN_EVENT_LEN 23
+struct bt_le_conn_event_len {
+	uint16_t min;
+	uint16_t max;
+};
+
+struct bt_le_conn_event_len_state {
+	struct bt_le_conn_event_len request;
+	int32_t status;
+};
+
 /* Byte order conversions */
 #if __BYTE_ORDER == __LITTLE_ENDIAN
 #define htobs(d)  (d)
diff --git a/profiles/audio/asha.c b/profiles/audio/asha.c
index e6a9ded62..f5f391273 100644
--- a/profiles/audio/asha.c
+++ b/profiles/audio/asha.c
@@ -55,6 +55,7 @@
 #define ASHA_MIN_MTU 163
 /* The default of 672 does not work */
 #define ASHA_CONNECTION_MTU 512
+#define ASHA_START_TIMEOUT 10
 
 /* HCI handles include zero; a zero cached interval means unknown. */
 #define ASHA_NUM_HANDLES 0x0f00
@@ -68,14 +69,32 @@ struct asha_adapter {
 static struct queue *asha_adapters;
 static struct queue *asha_streams;
 
+struct asha_start_wait {
+	bt_asha_cb_t callback;
+	void *user_data;
+	unsigned int source_id;
+	unsigned int retry_id;
+	int error;
+	bool connected;
+	bool sent;
+};
+
+struct connect_data {
+	struct bt_asha_device *asha_dev;
+};
+
 struct bt_asha_device {
 	struct bt_asha *asha;
 	struct btd_device *device;
 	struct media_transport *transport;
 
 	GIOChannel *io;
+	struct connect_data *connect_data;
 	uint16_t imtu, omtu;
 
+	struct bt_le_conn_event_len ce_previous, ce_requested;
+	bool ce_changed;
+	struct asha_start_wait *start_wait;
 	unsigned int resume_id;
 };
 
@@ -276,6 +295,239 @@ static void asha_adapter_remove(struct btd_profile *profile,
 	}
 }
 
+static void asha_start_wait_cancel(struct bt_asha_device *asha_dev)
+{
+	struct asha_start_wait *wait = asha_dev->start_wait;
+
+	if (!wait)
+		return;
+
+	asha_dev->start_wait = NULL;
+
+	if (wait->retry_id)
+		g_source_remove(wait->retry_id);
+
+	if (wait->source_id)
+		g_source_remove(wait->source_id);
+
+	g_free(wait);
+}
+
+static void asha_cancel_connect(struct bt_asha_device *asha_dev)
+{
+	if (!asha_dev->connect_data)
+		return;
+
+	asha_dev->connect_data->asha_dev = NULL;
+	asha_dev->connect_data = NULL;
+}
+
+static gboolean asha_start_wait_error(gpointer user_data)
+{
+	struct bt_asha_device *asha_dev = user_data;
+	struct asha_start_wait *wait = asha_dev->start_wait;
+	bt_asha_cb_t callback = wait->callback;
+	void *data = wait->user_data;
+	int err = wait->error;
+
+	wait->source_id = 0;
+	bt_asha_device_stop(asha_dev);
+
+	/* The owner callback may remove the device. */
+	if (callback)
+		callback(err, data);
+
+	return G_SOURCE_REMOVE;
+}
+
+static gboolean asha_start_wait_timeout(gpointer user_data)
+{
+	struct bt_asha_device *asha_dev = user_data;
+
+	asha_dev->start_wait->error = -ETIMEDOUT;
+	return asha_start_wait_error(asha_dev);
+}
+
+static void asha_start_wait_fail(struct bt_asha_device *asha_dev, int err)
+{
+	struct asha_start_wait *wait = asha_dev->start_wait;
+
+	if (!wait || wait->error)
+		return;
+
+	if (wait->source_id)
+		g_source_remove(wait->source_id);
+
+	wait->error = err;
+	/* Do not call the owner from inside the HCI stream iteration. */
+	wait->source_id = g_idle_add(asha_start_wait_error, asha_dev);
+}
+
+static void asha_start_complete(int status, void *user_data)
+{
+	struct bt_asha_device *asha_dev = user_data;
+	struct asha_start_wait *wait = asha_dev->start_wait;
+	bt_asha_cb_t callback;
+	void *data;
+
+	if (!wait || wait->error)
+		return;
+
+	if (status) {
+		asha_start_wait_fail(asha_dev, -EIO);
+		return;
+	}
+
+	callback = wait->callback;
+	data = wait->user_data;
+	asha_start_wait_cancel(asha_dev);
+
+	if (callback)
+		callback(0, data);
+}
+
+static int asha_event_len_ready(struct bt_asha_device *asha_dev)
+{
+	struct bt_le_conn_event_len_state state = { .status = INT32_MIN };
+	socklen_t len = sizeof(state);
+	int fd = g_io_channel_unix_get_fd(asha_dev->io);
+
+	if (getsockopt(fd, SOL_BLUETOOTH, BT_LE_CONN_EVENT_LEN,
+							&state, &len) < 0)
+		return errno == ENOPROTOOPT ? 1 : -errno;
+
+	/* Older CE kernels return only the requested four-byte prefix. */
+	if (len == sizeof(state.request))
+		return 1;
+	if (len != sizeof(state))
+		return -EIO;
+	/* CE v1 did not shorten optlen after copying its four-byte value. */
+	if (state.status == INT32_MIN)
+		return 1;
+	if (state.status == -EINPROGRESS)
+		return 0;
+	if (state.status)
+		return state.status < 0 ? state.status : -EIO;
+	if (state.request.min < 12 || state.request.max < 12)
+		return -ECANCELED;
+
+	return 1;
+}
+
+static gboolean asha_start_wait_retry(gpointer user_data);
+
+static void asha_start_wait_try(struct bt_asha_device *asha_dev)
+{
+	struct asha_start_wait *wait = asha_dev->start_wait;
+	int ready;
+
+	if (!wait || wait->error || !wait->connected || wait->sent)
+		return;
+
+	ready = asha_event_len_ready(asha_dev);
+	if (ready < 0) {
+		asha_start_wait_fail(asha_dev, ready);
+		return;
+	}
+	if (!ready) {
+		/* The kernel publishes completion after the HCI event.
+		 * Query it again without blocking the main loop.
+		 */
+		if (!wait->retry_id)
+			wait->retry_id = g_timeout_add(20,
+					asha_start_wait_retry, asha_dev);
+		return;
+	}
+	if (wait->retry_id) {
+		g_source_remove(wait->retry_id);
+		wait->retry_id = 0;
+	}
+
+	asha_send_interval(asha_dev);
+
+	wait->sent = true;
+
+	if (bt_asha_start(asha_dev->asha, asha_start_complete, asha_dev))
+		asha_start_wait_fail(asha_dev, -EIO);
+}
+
+static gboolean asha_start_wait_retry(gpointer user_data)
+{
+	struct bt_asha_device *asha_dev = user_data;
+
+	asha_dev->start_wait->retry_id = 0;
+	asha_start_wait_try(asha_dev);
+	return G_SOURCE_REMOVE;
+}
+
+static void asha_start_wait_begin(struct bt_asha_device *asha_dev,
+					bt_asha_cb_t callback, void *user_data)
+{
+	struct asha_start_wait *wait;
+
+	wait = g_new0(struct asha_start_wait, 1);
+	wait->callback = callback;
+	wait->user_data = user_data;
+	asha_dev->start_wait = wait;
+	wait->source_id = g_timeout_add_seconds(ASHA_START_TIMEOUT,
+					asha_start_wait_timeout, asha_dev);
+}
+
+static int asha_request_event_len(struct bt_asha_device *asha_dev)
+{
+	struct bt_le_conn_event_len previous, requested;
+	socklen_t len = sizeof(previous);
+	int fd = g_io_channel_unix_get_fd(asha_dev->io);
+
+	if (getsockopt(fd, SOL_BLUETOOTH, BT_LE_CONN_EVENT_LEN,
+						&previous, &len) < 0)
+		return errno == ENOPROTOOPT ? 0 : -errno;
+
+	if (len != sizeof(previous))
+		return -EIO;
+
+	requested.min = MAX(previous.min, 12);
+	requested.max = MAX(previous.max, 12);
+	if (requested.min == previous.min && requested.max == previous.max)
+		return 0;
+
+	if (setsockopt(fd, SOL_BLUETOOTH, BT_LE_CONN_EVENT_LEN,
+					&requested, sizeof(requested)) < 0)
+		return -errno;
+
+	asha_dev->ce_previous = previous;
+	asha_dev->ce_requested = requested;
+	asha_dev->ce_changed = true;
+
+	return 0;
+}
+
+static void asha_release_event_len(struct bt_asha_device *asha_dev)
+{
+	struct bt_le_conn_event_len current;
+	socklen_t len = sizeof(current);
+	int fd;
+
+	if (!asha_dev->ce_changed)
+		return;
+
+	asha_dev->ce_changed = false;
+	fd = g_io_channel_unix_get_fd(asha_dev->io);
+	if (getsockopt(fd, SOL_BLUETOOTH, BT_LE_CONN_EVENT_LEN,
+						&current, &len) < 0 ||
+						len != sizeof(current))
+		return;
+
+	/* Leave a later request from another user of this link untouched. */
+	if (current.min != asha_dev->ce_requested.min ||
+				current.max != asha_dev->ce_requested.max)
+		return;
+
+	if (setsockopt(fd, SOL_BLUETOOTH, BT_LE_CONN_EVENT_LEN,
+			&asha_dev->ce_previous, sizeof(current)) < 0)
+		DBG("Could not restore ASHA event length: %s", strerror(errno));
+}
+
 static char *make_endpoint_path(struct bt_asha_device *asha_dev)
 {
 	char *path;
@@ -291,22 +543,23 @@ static char *make_endpoint_path(struct bt_asha_device *asha_dev)
 	return path;
 }
 
-struct connect_data {
-	struct bt_asha_device *asha_dev;
-	bt_asha_cb_t cb;
-	void *cb_user_data;
-};
-
 static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
 {
 	struct connect_data *conn_data = user_data;
 	struct bt_asha_device *asha_dev = conn_data->asha_dev;
 	GError *gerr = NULL;
 	uint32_t phys;
-	int fd;
+	int fd, ret;
+
+	if (!asha_dev)
+		return;
+
+	asha_dev->connect_data = NULL;
 
 	if (err) {
 		error("%s", err->message);
+
+		asha_start_wait_fail(asha_dev, -EIO);
 		return;
 	}
 
@@ -330,12 +583,14 @@ static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
 	if (setsockopt(fd, SOL_BLUETOOTH, BT_PHY, &phys, sizeof(phys)) < 0)
 		DBG("Could not set LE 2M PHY preference: %s", strerror(errno));
 
-	asha_dev->io = io;
-
-	asha_send_interval(asha_dev);
+	ret = asha_request_event_len(asha_dev);
+	if (ret < 0) {
+		asha_start_wait_fail(asha_dev, ret);
+		return;
+	}
 
-	bt_asha_start(asha_dev->asha, conn_data->cb,
-					conn_data->cb_user_data);
+	asha_dev->start_wait->connected = true;
+	asha_start_wait_try(asha_dev);
 }
 
 static int asha_connect_socket(struct bt_asha_device *asha_dev,
@@ -345,21 +600,22 @@ static int asha_connect_socket(struct bt_asha_device *asha_dev,
 	const bdaddr_t *src_addr;
 	struct connect_data *conn_data;
 
-	if (asha_dev->asha->state != ASHA_STOPPED) {
+	if (asha_dev->io || asha_dev->start_wait ||
+					asha_dev->asha->state != ASHA_STOPPED) {
 		error("ASHA device connect failed. Bad state %d",
 							asha_dev->asha->state);
-		return 0;
+		return -EBUSY;
 	}
 
 	conn_data = g_new0(struct connect_data, 1);
 	conn_data->asha_dev = asha_dev;
-	conn_data->cb = cb;
-	conn_data->cb_user_data = user_data;
+	asha_dev->connect_data = conn_data;
+	asha_start_wait_begin(asha_dev, cb, user_data);
 
 	src_addr = btd_adapter_get_address(
 			device_get_adapter(asha_dev->device));
 
-	if (!bt_io_connect(connect_cb, conn_data,
+	asha_dev->io = bt_io_connect(connect_cb, conn_data,
 				g_free, &gerr,
 				BT_IO_OPT_MODE, BT_IO_MODE_LE_FLOWCTL,
 				BT_IO_OPT_SOURCE_TYPE, BDADDR_LE_PUBLIC,
@@ -370,9 +626,13 @@ static int asha_connect_socket(struct bt_asha_device *asha_dev,
 				BT_IO_OPT_PSM, asha_dev->asha->psm,
 				BT_IO_OPT_OMTU, ASHA_MIN_MTU,
 				BT_IO_OPT_IMTU, ASHA_CONNECTION_MTU,
-				BT_IO_OPT_INVALID)) {
+				BT_IO_OPT_INVALID);
+	if (!asha_dev->io) {
 		error("Could not open L2CAP CoC socket: %s", gerr->message);
 		g_error_free(gerr);
+		asha_dev->connect_data = NULL;
+		g_free(conn_data);
+		asha_start_wait_cancel(asha_dev);
 		goto error;
 	}
 
@@ -404,9 +664,13 @@ unsigned int bt_asha_device_start(struct bt_asha_device *asha_dev,
 
 unsigned int bt_asha_device_stop(struct bt_asha_device *asha_dev)
 {
+	asha_start_wait_cancel(asha_dev);
+	asha_cancel_connect(asha_dev);
 	bt_asha_stop(asha_dev->asha);
+	bt_asha_state_reset(asha_dev->asha);
 
 	if (asha_dev->io) {
+		asha_release_event_len(asha_dev);
 		g_io_channel_shutdown(asha_dev->io, TRUE, NULL);
 		g_io_channel_unref(asha_dev->io);
 		asha_dev->io = NULL;
@@ -417,15 +681,17 @@ unsigned int bt_asha_device_stop(struct bt_asha_device *asha_dev)
 
 void bt_asha_device_state_reset(struct bt_asha_device *asha_dev)
 {
+	asha_start_wait_cancel(asha_dev);
+	asha_cancel_connect(asha_dev);
+
 	if (asha_dev->io) {
+		asha_release_event_len(asha_dev);
 		g_io_channel_shutdown(asha_dev->io, TRUE, NULL);
 		g_io_channel_unref(asha_dev->io);
 		asha_dev->io = NULL;
 	};
 
 	bt_asha_state_reset(asha_dev->asha);
-
-	asha_dev->resume_id = 0;
 }
 
 unsigned int bt_asha_device_device_get_resume_id(
@@ -437,6 +703,9 @@ unsigned int bt_asha_device_device_get_resume_id(
 enum bt_asha_state_t bt_asha_device_get_state(
 					struct bt_asha_device *asha_dev)
 {
+	if (asha_dev->start_wait)
+		return ASHA_STARTING;
+
 	return asha_dev->asha->state;
 }
 
@@ -610,6 +879,7 @@ static void asha_source_device_remove(struct btd_service *service)
 		asha_streams = NULL;
 	}
 
+	bt_asha_device_state_reset(asha_dev);
 	bt_asha_free(asha_dev->asha);
 	g_free(asha_dev);
 }
diff --git a/unit/test-asha-conn.c b/unit/test-asha-conn.c
index efdac837b..4b9e6ed08 100644
--- a/unit/test-asha-conn.c
+++ b/unit/test-asha-conn.c
@@ -10,12 +10,14 @@
 #define bt_att_get_fd test_att_fd
 #define bt_asha_set_connection_interval test_set_interval
 #define bt_asha_start test_start
+#define bt_asha_stop test_stop
 #define bt_io_get test_io_get
 #include "profiles/audio/asha.c"
 #undef bt_gatt_client_get_att
 #undef bt_att_get_fd
 #undef bt_asha_set_connection_interval
 #undef bt_asha_start
+#undef bt_asha_stop
 #undef bt_io_get
 #include "bluetooth/hci.h"
 #include "src/shared/tester.h"
@@ -38,19 +40,25 @@ const char *device_get_path(const struct btd_device *device)
 
 const bdaddr_t *device_get_address(struct btd_device *device)
 {
-	assert(false);
+	static const bdaddr_t address;
+
+	return &address;
 }
 
 const bdaddr_t *btd_adapter_get_address(struct btd_adapter *adapter)
 {
-	assert(false);
+	static const bdaddr_t address;
+
+	return &address;
 }
 
 void btd_device_set_conn_param(struct btd_device *device,
 				uint16_t min_interval, uint16_t max_interval,
 					uint16_t latency, uint16_t timeout)
 {
-	assert(false);
+	assert(min_interval == 16 && max_interval == 16);
+	assert(latency == 10);
+	assert(timeout == 100);
 }
 
 struct gatt_db *btd_device_get_gatt_db(struct btd_device *device)
@@ -132,25 +140,53 @@ const char *media_transport_get_path(struct media_transport *transport)
 	assert(false);
 }
 
+static BtIOConnect pending_connect;
+static GDestroyNotify pending_destroy;
+static void *pending_data;
+static GIOChannel *pending_io;
+static int connect_peer;
+static bool connect_error;
+
 GIOChannel *bt_io_connect(BtIOConnect connect, gpointer user_data,
 				GDestroyNotify destroy, GError **gerr,
 							BtIOOption opt1, ...)
 {
-	assert(false);
+	int fds[2];
+
+	if (connect_error) {
+		g_set_error_literal(gerr, g_quark_from_static_string("test"),
+							EIO, "connect failed");
+		return NULL;
+	}
+
+	assert(!pending_io);
+	assert(!socketpair(AF_UNIX, SOCK_STREAM, 0, fds));
+	pending_io = g_io_channel_unix_new(fds[0]);
+	g_io_channel_set_close_on_unref(pending_io, TRUE);
+	pending_connect = connect;
+	pending_data = user_data;
+	pending_destroy = destroy;
+	connect_peer = fds[1];
+	return g_io_channel_ref(pending_io);
 }
 
 static int peer_fd[2];
 static int observer_fd[2];
 static int pending_peer;
 static unsigned int writes;
+static unsigned int starts;
+static bool start_error;
+static bool interval_error;
+static bt_asha_cb_t start_callback;
+static void *start_data;
 static uint16_t last_interval;
 static struct bt_asha *last_asha;
 static bool socket_ready = true;
 static bool fail_socket;
 static int att_fd = 100;
+static uint16_t att_handle = 16;
 static unsigned int handle_reads;
 static socklen_t conninfo_length = sizeof(struct l2cap_conninfo);
-static uint16_t att_handle = 16;
 
 int __wrap_socket(int domain, int type, int protocol)
 {
@@ -183,9 +219,27 @@ int __wrap_bind(int fd, const struct sockaddr *addr, socklen_t length)
 	return 0;
 }
 
+static struct bt_le_conn_event_len ce_value;
+static bool ce_supported, ce_set_fail, ce_get_fail, ce_short;
+static unsigned int ce_sets;
+static int ce_set_error = ENOPROTOOPT;
+static bool ce_extended, ce_legacy_len;
+static int32_t ce_status;
+
 int __wrap_setsockopt(int fd, int level, int opt, const void *value,
 							socklen_t length)
 {
+	if (level == SOL_BLUETOOTH && opt == BT_LE_CONN_EVENT_LEN) {
+		assert(length == sizeof(ce_value));
+		ce_sets++;
+		if (!ce_supported || ce_set_fail) {
+			errno = ce_set_error;
+			return -1;
+		}
+		memcpy(&ce_value, value, length);
+		return 0;
+	}
+
 	if (level == SOL_HCI)
 		return 0;
 
@@ -197,6 +251,30 @@ int __wrap_getsockopt(int fd, int level, int opt, void *value,
 {
 	struct l2cap_conninfo *info = value;
 
+	if (level == SOL_BLUETOOTH && opt == BT_LE_CONN_EVENT_LEN) {
+		if (!ce_supported || ce_get_fail) {
+			errno = ENOPROTOOPT;
+			return -1;
+		}
+		if (*length == sizeof(struct bt_le_conn_event_len_state) &&
+							ce_extended) {
+			struct bt_le_conn_event_len_state state = {
+				.request = ce_value, .status = ce_status,
+			};
+			memcpy(value, &state, sizeof(state));
+			*length = ce_short ? 1 : sizeof(state);
+		} else {
+			assert(*length >= sizeof(ce_value));
+			memcpy(value, &ce_value, sizeof(ce_value));
+			if (ce_legacy_len)
+				assert(((struct bt_le_conn_event_len_state *)
+						value)->status == INT32_MIN);
+			*length = ce_short ? 1 : ce_legacy_len ?
+						*length : sizeof(ce_value);
+		}
+		return 0;
+	}
+
 	assert(level == SOL_L2CAP && opt == L2CAP_CONNINFO);
 	handle_reads++;
 
@@ -243,14 +321,26 @@ bool test_set_interval(struct bt_asha *asha,
 	writes++;
 	last_asha = asha;
 	last_interval = interval;
-	return true;
+	return !interval_error;
 }
 
 unsigned int test_start(struct bt_asha *asha, bt_asha_cb_t cb,
 							void *user_data)
 {
-	assert(writes == 1 && last_asha == asha && last_interval == 24);
+	starts++;
+
+	if (start_error)
+		return -1;
+
 	asha->state = ASHA_STARTING;
+	start_callback = cb;
+	start_data = user_data;
+	return 0;
+}
+
+unsigned int test_stop(struct bt_asha *asha)
+{
+	bt_asha_state_reset(asha);
 	return 0;
 }
 
@@ -272,11 +362,335 @@ static void event(unsigned int adapter, uint8_t subevent, const void *value,
 		;
 }
 
+struct gate_result {
+	struct bt_asha_device *device;
+	unsigned int calls;
+	int error;
+	bool remove;
+};
+
+static void gate_callback(int status, void *user_data)
+{
+	struct gate_result *result = user_data;
+
+	assert(!result->device->start_wait);
+
+	if (status) {
+		assert(!result->device->io);
+		assert(bt_asha_device_get_state(result->device) ==
+							ASHA_STOPPED);
+	} else {
+		assert(result->device->io);
+		assert(bt_asha_device_get_state(result->device) ==
+							ASHA_STARTED);
+	}
+	result->calls++;
+	result->error = status;
+
+	if (result->remove) {
+		queue_remove(asha_streams, result->device);
+		bt_asha_device_state_reset(result->device);
+		g_free(result->device);
+		result->device = NULL;
+	}
+}
+
+static void finish_connect(bool failed)
+{
+	GError *err = NULL;
+
+	if (failed)
+		g_set_error_literal(&err, g_quark_from_static_string("test"),
+							EIO, "connect failed");
+
+	pending_connect(pending_io, err, pending_data);
+	pending_destroy(pending_data);
+	g_io_channel_unref(pending_io);
+	pending_io = NULL;
+	g_clear_error(&err);
+	close(connect_peer);
+}
+
+static void test_event_len(const void *user_data)
+{
+	struct bt_asha_device dev = {};
+	int fds[2];
+	unsigned int before;
+
+	assert(!pipe(fds));
+	dev.io = g_io_channel_unix_new(fds[0]);
+
+	/* An older kernel must not prevent playback or attempt a restore. */
+	ce_supported = false;
+	ce_sets = 0;
+	asha_request_event_len(&dev);
+	asha_release_event_len(&dev);
+	assert(!dev.ce_changed && !ce_sets);
+
+	ce_supported = true;
+	ce_value = (struct bt_le_conn_event_len) { 0, 0 };
+	asha_request_event_len(&dev);
+	assert(dev.ce_changed && ce_value.min == 12 && ce_value.max == 12);
+	asha_release_event_len(&dev);
+	assert(!dev.ce_changed && !ce_value.min && !ce_value.max);
+	before = ce_sets;
+	asha_release_event_len(&dev);
+	assert(ce_sets == before);
+
+	/* Preserve both stronger and asymmetric requests. */
+	ce_value = (struct bt_le_conn_event_len) { 16, 20 };
+	asha_request_event_len(&dev);
+	assert(!dev.ce_changed && ce_sets == before);
+	ce_value = (struct bt_le_conn_event_len) { 8, 20 };
+	asha_request_event_len(&dev);
+	assert(dev.ce_changed && ce_value.min == 12 && ce_value.max == 20);
+	asha_release_event_len(&dev);
+	assert(ce_value.min == 8 && ce_value.max == 20);
+
+	/* A later writer owns the current connection preference. */
+	ce_value = (struct bt_le_conn_event_len) { 0, 0 };
+	asha_request_event_len(&dev);
+	ce_value.min = ce_value.max = 24;
+	before = ce_sets;
+	asha_release_event_len(&dev);
+	assert(!dev.ce_changed && ce_sets == before && ce_value.min == 24);
+
+	ce_value = (struct bt_le_conn_event_len) { 0, 0 };
+	ce_set_fail = true;
+	asha_request_event_len(&dev);
+	assert(!dev.ce_changed && !ce_value.min);
+	ce_set_fail = false;
+	ce_short = true;
+	before = ce_sets;
+	asha_request_event_len(&dev);
+	assert(!dev.ce_changed && ce_sets == before);
+	ce_short = false;
+
+	asha_request_event_len(&dev);
+	assert(dev.ce_changed);
+	ce_get_fail = true;
+	before = ce_sets;
+	asha_release_event_len(&dev);
+	assert(!dev.ce_changed && ce_sets == before);
+	ce_get_fail = false;
+
+	/* Resume/reconnect must snapshot this connection's preference again. */
+	ce_value = (struct bt_le_conn_event_len) { 4, 8 };
+	asha_request_event_len(&dev);
+	assert(dev.ce_previous.min == 4 && dev.ce_previous.max == 8);
+	asha_release_event_len(&dev);
+	assert(ce_value.min == 4 && ce_value.max == 8);
+	ce_supported = false;
+	g_io_channel_unref(dev.io);
+	close(fds[0]);
+	close(fds[1]);
+	tester_test_passed();
+}
+
+static void test_completion_gate(const void *user_data)
+{
+	for (unsigned int scenario = 0; scenario < 10; scenario++) {
+		struct bt_asha asha = { .client = (void *) 1 };
+		struct bt_asha_device dev = {
+			.asha = &asha, .device = (void *) 1,
+		};
+		struct gate_result result = { .device = &dev };
+		struct bt_hci_evt_le_conn_complete complete = {
+			.handle = cpu_to_le16(16), .interval = cpu_to_le16(16),
+		};
+		struct bt_hci_evt_le_conn_update_complete update = {
+			.handle = cpu_to_le16(16), .interval = cpu_to_le16(16),
+		};
+		unsigned int retry;
+		GSource *timer;
+		int fds[2];
+
+		starts = writes = 0;
+		start_error = interval_error = false;
+		ce_supported = ce_extended = true;
+		ce_status = -EINPROGRESS;
+		ce_value = (struct bt_le_conn_event_len) { 12, 12 };
+		assert(!pipe(fds));
+		assert(!asha_adapter_probe(NULL, (void *) 1));
+		asha_streams = queue_new();
+		queue_push_tail(asha_streams, &dev);
+		event(0, BT_HCI_EVT_LE_CONN_COMPLETE, &complete,
+							sizeof(complete));
+		writes = 0;
+		dev.io = g_io_channel_unix_new(fds[0]);
+		g_io_channel_set_close_on_unref(dev.io, TRUE);
+		asha_start_wait_begin(&dev, gate_callback, &result);
+		dev.start_wait->connected = true;
+		asha_start_wait_try(&dev);
+		assert(!starts && !writes && dev.start_wait->retry_id);
+		retry = dev.start_wait->retry_id;
+
+		/* Cached intervals and older updates cannot release START. */
+		event(0, BT_HCI_EVT_LE_CONN_UPDATE_COMPLETE, &update,
+							sizeof(update));
+		assert(!starts && writes == 1);
+		assert(dev.start_wait->retry_id == retry);
+		writes = 0;
+
+		switch (scenario) {
+		case 0:
+			ce_status = 0;
+			timer = g_main_context_find_source_by_id(NULL, retry);
+			g_source_set_ready_time(timer, 0);
+			while (g_main_context_iteration(NULL, FALSE))
+				;
+			assert(starts == 1 && writes == 1);
+			assert(!dev.start_wait->retry_id);
+			/* Notify while awaiting AudioStatus too. */
+			event(0, BT_HCI_EVT_LE_CONN_UPDATE_COMPLETE, &update,
+								sizeof(update));
+			assert(starts == 1 && writes == 2);
+			asha.state = ASHA_STARTED;
+			start_callback(0, start_data);
+			assert(result.calls == 1 && !result.error);
+			assert(!dev.start_wait);
+			break;
+		case 1:
+			ce_status = -EIO;
+			asha_start_wait_try(&dev);
+			while (g_main_context_iteration(NULL, FALSE))
+				;
+			assert(!starts && result.calls == 1);
+			assert(result.error == -EIO);
+			break;
+		case 2:
+			bt_asha_device_state_reset(&dev);
+			assert(!g_main_context_find_source_by_id(NULL, retry));
+			while (g_main_context_iteration(NULL, FALSE))
+				;
+			assert(!starts && !result.calls);
+			break;
+		case 3:
+			timer = g_main_context_find_source_by_id(NULL,
+						dev.start_wait->source_id);
+			g_source_set_ready_time(timer, 0);
+			while (g_main_context_iteration(NULL, FALSE))
+				;
+			assert(!starts && result.calls == 1);
+			assert(result.error == -ETIMEDOUT);
+			assert(!g_main_context_find_source_by_id(NULL, retry));
+			break;
+		case 4:
+			ce_status = 0;
+			ce_value.min = ce_value.max = 0;
+			asha_start_wait_try(&dev);
+			while (g_main_context_iteration(NULL, FALSE))
+				;
+			assert(!starts && result.calls == 1);
+			assert(result.error == -ECANCELED);
+			break;
+		case 5:
+			ce_legacy_len = true;
+			ce_extended = false;
+			asha_start_wait_try(&dev);
+			assert(starts == 1 && !dev.start_wait->retry_id);
+			break;
+		case 9:
+			ce_extended = false;
+			asha_start_wait_try(&dev);
+			assert(starts == 1 && !dev.start_wait->retry_id);
+			break;
+		case 6:
+			ce_supported = false;
+			asha_start_wait_try(&dev);
+			assert(starts == 1 && !dev.start_wait->retry_id);
+			break;
+		case 7:
+			ce_short = true;
+			asha_start_wait_try(&dev);
+			while (g_main_context_iteration(NULL, FALSE))
+				;
+			assert(!starts && result.calls == 1);
+			assert(result.error == -EIO);
+			ce_short = false;
+			break;
+		case 8:
+			ce_status = 0;
+			ce_value = (struct bt_le_conn_event_len) { 16, 20 };
+			asha_start_wait_try(&dev);
+			assert(starts == 1 && !dev.start_wait->retry_id);
+			break;
+		}
+		bt_asha_device_state_reset(&dev);
+		queue_destroy(asha_streams, NULL);
+		asha_streams = NULL;
+		asha_adapter_remove(NULL, (void *) 1);
+		close(peer_fd[0]);
+		close(fds[1]);
+		ce_supported = ce_extended = false;
+		ce_legacy_len = false;
+	}
+	starts = writes = 0;
+	tester_test_passed();
+}
+
+static void test_connect_lifetime(const void *user_data)
+{
+	for (unsigned int scenario = 0; scenario < 5; scenario++) {
+		struct bt_asha asha = { .state = ASHA_STOPPED };
+		struct bt_asha_device dev = {
+			.asha = &asha, .device = (void *) 1,
+		};
+		struct gate_result result = { .device = &dev };
+		unsigned int timer;
+
+		ce_supported = scenario >= 3;
+		ce_extended = scenario == 4;
+		ce_value = (struct bt_le_conn_event_len) { 0, 0 };
+		ce_set_fail = scenario >= 3;
+		ce_set_error = EIO;
+		ce_status = 0;
+		starts = 0;
+		assert(!asha_connect_socket(&dev, gate_callback, &result));
+		assert(dev.io && dev.start_wait && dev.connect_data);
+		assert(asha_connect_socket(&dev, gate_callback,
+						&result) == -EBUSY);
+		timer = dev.start_wait->source_id;
+		if (scenario == 0) {
+			bt_asha_device_state_reset(&dev);
+			finish_connect(false);
+			assert(!starts && !result.calls);
+			assert(!dev.io && !dev.start_wait);
+		} else if (scenario == 1) {
+			finish_connect(true);
+			while (g_main_context_iteration(NULL, FALSE))
+				;
+			assert(result.calls == 1 && result.error == -EIO);
+		} else if (scenario >= 3) {
+			/* Failed requests must not start on old getters or
+			 * lose the setter error on completion-aware kernels.
+			 */
+			finish_connect(false);
+			while (g_main_context_iteration(NULL, FALSE))
+				;
+			assert(!starts && result.calls == 1);
+			assert(result.error == -EIO);
+		} else {
+			/* Keep old-kernel playback. */
+			finish_connect(false);
+			assert(starts == 1 && dev.start_wait->sent);
+			bt_asha_device_state_reset(&dev);
+		}
+		assert(!g_main_context_find_source_by_id(NULL, timer));
+		assert(!dev.connect_data);
+	}
+	ce_set_fail = ce_extended = ce_supported = false;
+	ce_set_error = ENOPROTOOPT;
+	tester_test_passed();
+}
+
 static void test_connection(const void *user_data)
 {
 	struct bt_asha asha = { .acp_handle = 0x79 };
 	struct bt_asha other = { .acp_handle = 0x79 };
-	struct bt_asha_device dev = { .asha = &asha, .device = (void *) 1 };
+	struct bt_asha_device dev = {
+			.asha = &asha, .device = (void *) 1,
+		};
 	struct bt_asha_device dev2 = { .asha = &other, .device = (void *) 2 };
 	struct connect_data conn = { .asha_dev = &dev };
 	GIOChannel *io;
@@ -313,7 +727,10 @@ static void test_connection(const void *user_data)
 	event(0, BT_HCI_EVT_LE_CONN_COMPLETE, &complete, sizeof(complete));
 	assert(writes == 0);
 	asha.client = (void *) 1;
+	dev.io = io;
+	asha_start_wait_begin(&dev, NULL, NULL);
 	connect_cb(io, NULL, &conn);
+	asha_start_wait_cancel(&dev);
 	assert(writes == 1 && last_interval == 24);
 
 	/* Identical handles on different controllers are separate links. */
@@ -395,7 +812,9 @@ static void test_handle_bounds(const void *user_data)
 	const uint16_t handles[] = { 0, ASHA_NUM_HANDLES - 1,
 					ASHA_NUM_HANDLES, UINT16_MAX };
 	struct bt_asha asha = { .client = (void *) 1, .acp_handle = 0x79 };
-	struct bt_asha_device dev = { .asha = &asha, .device = (void *) 1 };
+	struct bt_asha_device dev = {
+			.asha = &asha, .device = (void *) 1,
+		};
 	struct bt_hci_evt_le_conn_complete complete = {
 		.interval = cpu_to_le16(24),
 	};
@@ -460,7 +879,9 @@ static void test_late_connection(const void *user_data)
 {
 	struct bt_asha asha = { .client = (void *) 1, .acp_handle = 0x79,
 							.state = ASHA_STARTED };
-	struct bt_asha_device dev = { .asha = &asha, .device = (void *) 1 };
+	struct bt_asha_device dev = {
+			.asha = &asha, .device = (void *) 1,
+		};
 	struct bt_hci_evt_le_conn_complete complete = {
 		.handle = cpu_to_le16(16), .interval = cpu_to_le16(24),
 	};
@@ -492,7 +913,9 @@ static void test_late_connection(const void *user_data)
 static void test_observer_lifecycle(const void *user_data)
 {
 	struct bt_asha asha = { .acp_handle = 0x79 };
-	struct bt_asha_device dev = { .asha = &asha, .device = (void *) 1 };
+	struct bt_asha_device dev = {
+			.asha = &asha, .device = (void *) 1,
+		};
 	struct bt_hci_evt_le_conn_complete complete = {
 		.handle = cpu_to_le16(16), .interval = cpu_to_le16(24),
 	};
@@ -568,6 +991,11 @@ static void test_observer_lifecycle(const void *user_data)
 int main(int argc, char *argv[])
 {
 	tester_init(&argc, &argv);
+	tester_add("ASHA/CE/lifecycle", NULL, NULL, test_event_len, NULL);
+	tester_add("ASHA/CE/completion", NULL, NULL,
+						test_completion_gate, NULL);
+	tester_add("ASHA/CE/connect-lifetime", NULL, NULL,
+						test_connect_lifetime, NULL);
 	tester_add("ASHA/interval/HCI", NULL, NULL, test_connection, NULL);
 	tester_add("ASHA/interval/handle-bounds", NULL, NULL,
 						test_handle_bounds, NULL);


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-02 16:05 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 16:04 [RFC PATCH BlueZ] asha: Wait for connection event length requests Kyle Pepper

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox