From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH BlueZ v4 15/18] profiles/ranging: Keep CS sessions apart from stale and unrelated events
Date: Fri, 20 Nov 2026 11:55:05 -0500 [thread overview]
Message-ID: <20261120165510.1858959-16-luiz.dentz@gmail.com> (raw)
In-Reply-To: <20261120165510.1858959-1-luiz.dentz@gmail.com>
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
- Filter Procedure Enable Complete, Remote Capabilities, Remote FAE
and Config Complete by the connection of the state machine, and decode
the little-endian event handle before the lookup.
- Use the configuration ID chosen by the peer in the commands that follow
its Config Complete, and clear the role, the pending Security Enable
and the running procedure when the configuration is removed.
- Tie Read Local Capabilities and Set Default Settings responses to the
session that sent them, and do not let the Procedure Enable Complete
of a disable sent before a restart move the new session to STOPPED.
- Ignore Config Complete and Procedure Enable Complete of a stopped
session until StartMeasurement() is called again.
- Defer Set Procedure Parameters until both capability reads are in when
a peer-created configuration finishes Security Enable first.
- Stop a running Reflector procedure by removing the configuration and
report failure if that cannot be done.
- Decide the Reflector session from the negotiated role, ignore the
duration for a Reflector-only request, and run the same cleanup when a
session ends by timeout or by the end of the remote procedure.
Assisted-by: Claude:claude-sonnet-5-5
---
profiles/ranging/rap.c | 61 +++++---
profiles/ranging/rap_hci.c | 276 ++++++++++++++++++++++++++++++-------
src/shared/rap.h | 5 +
3 files changed, 276 insertions(+), 66 deletions(-)
diff --git a/profiles/ranging/rap.c b/profiles/ranging/rap.c
index 8f99572574c0..c4fac3cb1926 100644
--- a/profiles/ranging/rap.c
+++ b/profiles/ranging/rap.c
@@ -55,6 +55,8 @@ struct cs_session {
struct bt_rap_le_cs_frequency freq;
};
+#define CS_ROLE_REFLECTOR 0x02 /* Role_Enable: Reflector only */
+
struct rap_data {
struct btd_device *device;
struct btd_service *service;
@@ -847,6 +849,12 @@ bad_type:
* acts on the remote Initiator's events; rap_def_settings_done_cb()
* leaves Create Config to the Initiator.
*/
+ /* A Reflector only waits for the remote Initiator, so its duration
+ * is ignored rather than ending the wait.
+ */
+ if (settings.role == CS_ROLE_REFLECTOR)
+ duration_secs = 0;
+
if (!bt_rap_start_measurement(data->hci_sm, data->conn_handle,
duration_secs))
return g_dbus_create_error(msg, DBUS_ERROR_FAILED,
@@ -864,6 +872,24 @@ bad_type:
return dbus_message_new_method_return(msg);
}
+/* Common end of a session, however it ends: explicit stop, duration expiry or
+ * the end of the remote procedure a Reflector was armed for.
+ */
+static void rap_end_session(struct rap_data *data)
+{
+ bt_rap_hci_set_procedure_data_cb(data->hci_sm, NULL, NULL, NULL);
+
+ /* The Requester owns the Real-time CCCD subscription. Unregister it
+ * when the procedure ends so the peer receives the required CCCD
+ * disable. The local procedure has already been stopped, so a failure
+ * here (e.g. no remote RAS client) must not fail the caller.
+ */
+ if (!bt_rap_disable_realtime_ranging(data->rap))
+ DBG("Unable to disable Real-time Ranging Data");
+
+ memset(&data->active_session, 0, sizeof(data->active_session));
+}
+
static DBusMessage *stop_measurement(DBusConnection *conn,
DBusMessage *msg, void *user_data)
{
@@ -878,17 +904,7 @@ static DBusMessage *stop_measurement(DBusConnection *conn,
return g_dbus_create_error(msg, DBUS_ERROR_FAILED,
"Stop measurement failed");
- bt_rap_hci_set_procedure_data_cb(data->hci_sm, NULL, NULL, NULL);
-
- /* The Requester owns the Real-time CCCD subscription. Unregister it
- * when the procedure ends so the peer receives the required CCCD
- * disable. The local procedure has already been stopped, so a failure
- * here (e.g. no remote RAS client) must not fail the method.
- */
- if (!bt_rap_disable_realtime_ranging(data->rap))
- DBG("Unable to disable Real-time Ranging Data");
-
- memset(&data->active_session, 0, sizeof(data->active_session));
+ rap_end_session(data);
g_dbus_emit_property_changed(btd_get_dbus_connection(),
device_get_path(data->device),
@@ -937,7 +953,19 @@ static const GDBusMethodTable cs_dbus_methods[] = {
{ }
};
-#define CS_ROLE_INITIATOR 0x01
+/* The role negotiated for the current configuration decides, since the
+ * allowed-role setting can permit both. Until a configuration exists, only a
+ * Reflector-only setting makes this a Reflector session.
+ */
+static bool rap_is_reflector_session(struct rap_data *data)
+{
+ uint8_t role;
+
+ if (bt_rap_get_cs_role(data->hci_sm, &role))
+ return role == CS_REFLECTOR;
+
+ return data->active_session.settings.role == CS_ROLE_REFLECTOR;
+}
static gboolean cs_property_get_active(const GDBusPropertyTable *property,
DBusMessageIter *iter, void *user_data)
@@ -948,8 +976,7 @@ static gboolean cs_property_get_active(const GDBusPropertyTable *property,
/* A Reflector session is only armed while waiting for the peer, so
* only report the procedure state for it.
*/
- if (data->active_session.active &&
- (data->active_session.settings.role & CS_ROLE_INITIATOR))
+ if (data->active_session.active && !rap_is_reflector_session(data))
active = true;
dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &active);
@@ -970,7 +997,7 @@ static void rap_measurement_timeout_cb(void *user_data)
{
struct rap_data *data = user_data;
- memset(&data->active_session, 0, sizeof(data->active_session));
+ rap_end_session(data);
g_dbus_emit_property_changed(btd_get_dbus_connection(),
device_get_path(data->device),
@@ -987,9 +1014,9 @@ static void rap_proc_active_changed(bool active, void *user_data)
* refuse to replace and StopMeasurement() could not be reached for.
*/
if (!active && data->active_session.active &&
- !(data->active_session.settings.role & CS_ROLE_INITIATOR)) {
+ rap_is_reflector_session(data)) {
bt_rap_stop_measurement(data->hci_sm);
- memset(&data->active_session, 0, sizeof(data->active_session));
+ rap_end_session(data);
}
g_dbus_emit_property_changed(btd_get_dbus_connection(),
diff --git a/profiles/ranging/rap_hci.c b/profiles/ranging/rap_hci.c
index 8dbb6b847c11..9b6c48607cc8 100644
--- a/profiles/ranging/rap_hci.c
+++ b/profiles/ranging/rap_hci.c
@@ -74,6 +74,10 @@ struct cs_state_machine {
bool initiator;
bool procedure_active;
bool stop_requested; /* StopMeasurement() since StartMeasurement() */
+ unsigned int session_gen; /* Bumped by every Start/StopMeasurement() */
+ bool disable_pending; /* Procedure Enable (disable) not yet reported */
+ bool stale_disable; /* ... and a new session started meanwhile */
+ bool proc_setup_pending; /* Procedure setup waits for capabilities */
struct bt_rap_hci_cs_options cs_opt; /* Per-instance CS options */
uint8_t role_enable; /* Role value for HCI commands (1, 2, or 3) */
bool sec_enable_pending; /* Security Enable deferred until encrypted */
@@ -390,6 +394,33 @@ static bool cs_local_is_initiator(const struct cs_state_machine *sm)
return sm->role_enable & CS_ROLE_ENABLE_INITIATOR;
}
+struct cs_cmd_ctx {
+ struct cs_state_machine *sm;
+ unsigned int gen;
+};
+
+static void rap_resume_proc_setup(struct cs_state_machine *sm);
+
+/* Tie a command response to the session that sent it, so the response of a
+ * session that was stopped and restarted meanwhile cannot act on the new one.
+ */
+static struct cs_cmd_ctx *cs_cmd_ctx_new(struct cs_state_machine *sm)
+{
+ struct cs_cmd_ctx *ctx = new0(struct cs_cmd_ctx, 1);
+
+ ctx->sm = sm;
+ ctx->gen = sm->session_gen;
+
+ return ctx;
+}
+
+static void cs_set_procedure_active(struct cs_state_machine *sm, bool active)
+{
+ sm->procedure_active = active;
+ if (sm->proc_active_func)
+ sm->proc_active_func(active, sm->proc_active_data);
+}
+
/* Helper function to send read remote capabilities for all connections */
static bool bt_rap_read_remote_supported_capabilities(void *hci_sm,
uint16_t handle)
@@ -426,12 +457,18 @@ static void rap_rd_loc_supp_cap_done_cb(const void *data, uint8_t size,
void *user_data)
{
const struct bt_hci_rsp_le_cs_rd_loc_supp_cap *rsp;
- struct cs_state_machine *sm = (struct cs_state_machine *) user_data;
+ struct cs_cmd_ctx *ctx = user_data;
+ struct cs_state_machine *sm = ctx ? ctx->sm : NULL;
if (!sm || !data ||
size < sizeof(struct bt_hci_rsp_le_cs_rd_loc_supp_cap))
return;
+ if (ctx->gen != sm->session_gen) {
+ DBG("Local capabilities of a previous session, ignoring");
+ return;
+ }
+
DBG("size=0x%02X", size);
rsp = (const struct bt_hci_rsp_le_cs_rd_loc_supp_cap *) data;
@@ -478,6 +515,8 @@ static void rap_rd_loc_supp_cap_done_cb(const void *data, uint8_t size,
DBG("Sending read remote capabilities for handle 0x%04X",
sm->active_conn_handle);
bt_rap_read_remote_supported_capabilities(sm, sm->active_conn_handle);
+
+ rap_resume_proc_setup(sm);
}
static void rap_send_hci_cs_create_config_command(struct cs_state_machine *sm,
@@ -535,11 +574,17 @@ static void rap_def_settings_done_cb(const void *data, uint8_t size,
void *user_data)
{
const struct bt_hci_rsp_le_cs_set_def_settings *rp;
- struct cs_state_machine *sm = user_data;
+ struct cs_cmd_ctx *ctx = user_data;
+ struct cs_state_machine *sm = ctx ? ctx->sm : NULL;
if (!sm || !data || size < sizeof(*rp))
return;
+ if (ctx->gen != sm->session_gen) {
+ DBG("Def settings response of a previous session, ignoring");
+ return;
+ }
+
DBG("size=0x%02X", size);
rp = (const struct bt_hci_rsp_le_cs_set_def_settings *) data;
@@ -580,7 +625,7 @@ static void rap_def_settings_done_cb(const void *data, uint8_t size,
}
}
-static void rap_send_hci_cs_remove_config_command(struct cs_state_machine *sm,
+static bool rap_send_hci_cs_remove_config_command(struct cs_state_machine *sm,
uint16_t handle)
{
struct bt_hci_cmd_le_cs_remove_config cmd;
@@ -588,7 +633,7 @@ static void rap_send_hci_cs_remove_config_command(struct cs_state_machine *sm,
if (!sm || !sm->hci) {
error("CS Remove Config: sm or hci is null");
- return;
+ return false;
}
DBG("Sending CS Remove Config command for handle 0x%04X", handle);
@@ -603,10 +648,11 @@ static void rap_send_hci_cs_remove_config_command(struct cs_state_machine *sm,
if (!status) {
error("Failed to send CS Remove Config command");
cs_set_state(sm, CS_STATE_HOLD);
- return;
+ return false;
}
DBG("CS Remove Config command sent successfully");
+ return true;
}
static void rap_send_hci_cs_security_enable_command(
@@ -838,7 +884,7 @@ static void rap_send_hci_def_settings_command(struct cs_state_machine *sm,
status = bt_hci_send(sm->hci, BT_HCI_CMD_LE_CS_SET_DEF_SETTINGS,
&cp, sizeof(cp), rap_def_settings_done_cb,
- sm, NULL);
+ cs_cmd_ctx_new(sm), free);
DBG("sending set default settings case, status : %d", status);
@@ -869,12 +915,13 @@ static void rap_rd_rem_fae_cmplt_evt(const void *data, uint8_t size,
return;
}
- DBG("status=0x%02X, handle=0x%04X", evt->status, evt->handle);
+ DBG("status=0x%02X, handle=0x%04X", evt->status,
+ le16_to_cpu(evt->handle));
/* Every state machine on this adapter sees every CS event, so ignore
* the ones for other links before acting on the status.
*/
- if (!find_mapping_by_handle(sm, evt->handle)) {
+ if (!find_mapping_by_handle(sm, le16_to_cpu(evt->handle))) {
DBG("Not for this link, ignoring");
return;
}
@@ -964,12 +1011,13 @@ static void rap_rd_rmt_supp_cap_cmplt_evt(const void *data, uint8_t size,
return;
}
- DBG("status=0x%02X, handle=0x%04X", evt->status, evt->handle);
+ DBG("status=0x%02X, handle=0x%04X", evt->status,
+ le16_to_cpu(evt->handle));
/* Every state machine on this adapter sees every CS event, so do not
* let another link's capabilities overwrite this one's.
*/
- if (!find_mapping_by_handle(sm, evt->handle)) {
+ if (!find_mapping_by_handle(sm, le16_to_cpu(evt->handle))) {
DBG("Not for this link, ignoring");
return;
}
@@ -1006,14 +1054,21 @@ static void rap_rd_rmt_supp_cap_cmplt_evt(const void *data, uint8_t size,
bt_rap_set_remote_sw_time(sm->rap, evt->t_sw_time_supported);
+ rap_resume_proc_setup(sm);
+
+ if (sm->stop_requested) {
+ DBG("Measurement stopped, not continuing setup");
+ return;
+ }
+
/* Check Bit 1 of subfeatures_supported (0x0002) */
if (!(subfeatures_supported & 0x0002)) {
DBG("Bit 1 not set, sending Read Remote FAE Table");
- bt_rap_read_remote_fae_table(sm, evt->handle);
+ bt_rap_read_remote_fae_table(sm, le16_to_cpu(evt->handle));
return;
}
- DBG("send def settings for 0x%04X", evt->handle);
+ DBG("send def settings for 0x%04X", le16_to_cpu(evt->handle));
rap_send_hci_def_settings_command(sm, evt);
}
@@ -1042,13 +1097,14 @@ static void rap_cs_config_cmplt_evt(const void *data, uint8_t size,
return;
}
- DBG("status=0x%02X, handle=0x%04X", evt->status, evt->handle);
+ DBG("status=0x%02X, handle=0x%04X", evt->status,
+ le16_to_cpu(evt->handle));
/* Every state machine on this adapter sees every CS event, so ignore
* the ones for other links: acting on them would overwrite this
* link's negotiated role and advance its state.
*/
- mapping = find_mapping_by_handle(sm, evt->handle);
+ mapping = find_mapping_by_handle(sm, le16_to_cpu(evt->handle));
if (!mapping) {
DBG("Not for this link, ignoring");
return;
@@ -1060,6 +1116,24 @@ static void rap_cs_config_cmplt_evt(const void *data, uint8_t size,
*/
if (evt->status == 0 && evt->action == 0x00) {
DBG("Config %u removed", evt->config_id);
+
+ if (!sm->cs_role_known ||
+ evt->config_id == sm->cs_config.config_id) {
+ cs_set_state(sm, CS_STATE_UNSPECIFIED);
+ sm->sec_enable_pending = false;
+ sm->proc_setup_pending = false;
+ if (sm->procedure_active)
+ cs_set_procedure_active(sm, false);
+ sm->cs_role_known = false;
+ }
+ return;
+ }
+
+ /* StartMeasurement() has to be called again after a stop before a
+ * configuration of the peer is acted on.
+ */
+ if (sm->stop_requested) {
+ DBG("Measurement stopped, ignoring Config Complete");
return;
}
@@ -1092,7 +1166,8 @@ static void rap_cs_config_cmplt_evt(const void *data, uint8_t size,
/* Create/update failed — try to remove the config */
error("Configuration failed with status 0x%02X",
evt->status);
- rap_send_hci_cs_remove_config_command(sm, evt->handle);
+ rap_send_hci_cs_remove_config_command(sm,
+ le16_to_cpu(evt->handle));
} else {
error("CS Config Remove failed with status 0x%02X",
evt->status);
@@ -1103,7 +1178,7 @@ static void rap_cs_config_cmplt_evt(const void *data, uint8_t size,
/* Copy fields to rap_ev structure */
rap_ev.status = evt->status;
- rap_ev.conn_hdl = cpu_to_le16(evt->handle);
+ rap_ev.conn_hdl = evt->handle;
rap_ev.config_id = evt->config_id;
rap_ev.action = evt->action;
rap_ev.main_mode_type = evt->main_mode_type;
@@ -1138,6 +1213,10 @@ static void rap_cs_config_cmplt_evt(const void *data, uint8_t size,
return;
}
+ /* The peer picks the configuration ID of the ones it creates, and the
+ * commands that follow have to address that one.
+ */
+ sm->cs_config.config_id = rap_ev.config_id;
sm->cs_role = rap_ev.role;
sm->cs_role_known = true;
sm->cs_opt.rtt_type = rap_ev.rtt_type;
@@ -1176,7 +1255,8 @@ static void rap_cs_config_cmplt_evt(const void *data, uint8_t size,
(mapping->att && bt_att_get_security(mapping->att,
NULL) >= BT_ATT_SECURITY_MEDIUM)) {
DBG("Central,encrypted: Sending CS Security Enable command");
- rap_send_hci_cs_security_enable_command(sm, evt->handle);
+ rap_send_hci_cs_security_enable_command(sm,
+ le16_to_cpu(evt->handle));
} else {
DBG("Central, not yet encrypted: deferring CS Security Enable");
sm->sec_enable_pending = true;
@@ -1219,6 +1299,40 @@ static const uint8_t encrypt_evts[] = {
BT_HCI_EVT_ENCRYPT_CHANGE_V3,
};
+static bool rap_start_procedure(struct cs_state_machine *sm, uint16_t handle)
+{
+ DBG("Initiator role: Sending CS Set Procedure Params");
+ if (!rap_send_hci_cs_set_procedure_parameters(sm, handle)) {
+ error("Failed to send CS Set Procedure Params");
+ return false;
+ }
+
+ DBG("Initiator role: Sending CS Procedure Enable");
+ if (!rap_send_hci_cs_procedure_enable(sm, handle, true)) {
+ error("Failed to send CS Procedure Enable");
+ return false;
+ }
+
+ return true;
+}
+
+static void rap_resume_proc_setup(struct cs_state_machine *sm)
+{
+ if (!sm->proc_setup_pending || !sm->local_caps_known ||
+ !sm->remote_caps_known)
+ return;
+
+ sm->proc_setup_pending = false;
+
+ if (sm->stop_requested || !sm->cs_role_known ||
+ sm->cs_role != CS_INITIATOR ||
+ cs_get_current_state(sm) != CS_STATE_WAIT_PROC_CMPLT)
+ return;
+
+ if (!rap_start_procedure(sm, sm->active_conn_handle))
+ cs_set_state(sm, CS_STATE_HOLD);
+}
+
static void rap_cs_sec_enable_cmplt_evt(const void *data, uint8_t size,
void *user_data)
{
@@ -1283,18 +1397,14 @@ static void rap_cs_sec_enable_cmplt_evt(const void *data, uint8_t size,
return;
}
- DBG("Initiator role: Sending CS Set Procedure Params");
- if (!rap_send_hci_cs_set_procedure_parameters(
- sm, handle)) {
- error("Failed to send CS Set Procedure Params");
- cs_set_state(sm, CS_STATE_HOLD);
- return;
- }
-
- DBG("Initiator role: Sending CS Procedure Enable");
- if (!rap_send_hci_cs_procedure_enable(sm, handle,
- true)) {
- error("Failed to send CS Procedure Enable");
+ /* A peer-created configuration can get here while the
+ * capability reads of StartMeasurement() are still in
+ * flight. They resume the setup when they complete.
+ */
+ if (!sm->local_caps_known || !sm->remote_caps_known) {
+ DBG("Capabilities pending, deferring setup");
+ sm->proc_setup_pending = true;
+ } else if (!rap_start_procedure(sm, handle)) {
cs_set_state(sm, CS_STATE_HOLD);
return;
}
@@ -1346,22 +1456,50 @@ static void rap_cs_proc_enable_cmplt_evt(const void *data, uint8_t size,
return;
}
- DBG("status=0x%02X, handle=0x%04X", evt->status, evt->handle);
+ DBG("status=0x%02X, handle=0x%04X", evt->status,
+ le16_to_cpu(evt->handle));
+
+ /* Every state machine on this adapter sees every CS event, so ignore
+ * the ones for other links.
+ */
+ if (!find_mapping_by_handle(sm, le16_to_cpu(evt->handle))) {
+ DBG("Not for this link, ignoring");
+ return;
+ }
+
+ /* The report of a disable sent by a session that has been restarted
+ * since belongs to that session: it ends the procedure, but must not
+ * move the new session's setup to STOPPED.
+ */
+ if (sm->stale_disable && (evt->status != 0 || evt->state == 0x00)) {
+ DBG("Procedure disabled for a previous session");
+ sm->stale_disable = false;
+ if (sm->procedure_active)
+ cs_set_procedure_active(sm, false);
+ return;
+ }
+
+ if (evt->status != 0 || evt->state == 0x00)
+ sm->disable_pending = false;
+
+ /* A stopped session does not start a procedure again by itself */
+ if (evt->status == 0 && evt->state == 0x01 && sm->stop_requested) {
+ DBG("Measurement stopped, ignoring Procedure Enable Complete");
+ return;
+ }
/* Check status */
if (evt->status != 0) {
error("Procedure enable failed with status 0x%02X",
evt->status);
cs_set_state(sm, CS_STATE_HOLD);
- sm->procedure_active = false;
- if (sm->proc_active_func)
- sm->proc_active_func(false, sm->proc_active_data);
+ cs_set_procedure_active(sm, false);
return;
}
/* Copy fields to rap_ev structure */
rap_ev.status = evt->status;
- rap_ev.conn_hdl = cpu_to_le16(evt->handle);
+ rap_ev.conn_hdl = evt->handle;
rap_ev.config_id = evt->config_id;
rap_ev.state = evt->state;
rap_ev.tone_ant_config_sel = evt->tone_antenna_config_selection;
@@ -1383,14 +1521,10 @@ static void rap_cs_proc_enable_cmplt_evt(const void *data, uint8_t size,
/* Success - procedure started */
if (rap_ev.state == 0x01) {
cs_set_state(sm, CS_STATE_STARTED);
- sm->procedure_active = true;
- if (sm->proc_active_func)
- sm->proc_active_func(true, sm->proc_active_data);
+ cs_set_procedure_active(sm, true);
} else if (rap_ev.state == 0x00) {
cs_set_state(sm, CS_STATE_STOPPED);
- sm->procedure_active = false;
- if (sm->proc_active_func)
- sm->proc_active_func(false, sm->proc_active_data);
+ cs_set_procedure_active(sm, false);
}
/* Send callback to RAP Profile */
@@ -1426,12 +1560,14 @@ static void rap_le_conn_update_complete_evt(const void *data, uint8_t size,
if (evt->status != 0)
return;
- mapping = find_mapping_by_handle(sm, evt->handle);
+ mapping = find_mapping_by_handle(sm, le16_to_cpu(evt->handle));
if (mapping && mapping->rap) {
- DBG("Found handle 0x%04X in mapping cache", evt->handle);
+ DBG("Found handle 0x%04X in mapping cache",
+ le16_to_cpu(evt->handle));
rap = mapping->rap;
} else {
- DBG("No RAP mapping for handle 0x%04X, ignoring", evt->handle);
+ DBG("No RAP mapping for handle 0x%04X, ignoring",
+ le16_to_cpu(evt->handle));
return;
}
@@ -1910,7 +2046,7 @@ static bool bt_rap_read_local_supported_capabilities(
status = bt_hci_send(sm->hci, BT_HCI_CMD_LE_CS_RD_LOC_SUPP_CAP,
NULL, 0, rap_rd_loc_supp_cap_done_cb,
- sm, NULL);
+ cs_cmd_ctx_new(sm), free);
if (!status) {
error("Failed to send Read Local Supported Capabilities");
@@ -2046,8 +2182,20 @@ bool bt_rap_start_measurement(void *hci_sm, uint16_t conn_handle,
return false;
}
+ /* The disable of the previous session is still in flight: let its
+ * report be recognized instead of applying it to this session.
+ */
+ if (sm->disable_pending) {
+ sm->stale_disable = true;
+ sm->disable_pending = false;
+ }
+
+ sm->session_gen++;
sm->active_conn_handle = conn_handle;
sm->stop_requested = false;
+ sm->proc_setup_pending = false;
+ sm->local_caps_known = false;
+ sm->remote_caps_known = false;
if (sm->measurement_timeout_id) {
timeout_remove(sm->measurement_timeout_id);
@@ -2091,6 +2239,8 @@ bool bt_rap_stop_measurement(void *hci_sm)
* keep it from creating a config or enabling a procedure.
*/
sm->stop_requested = true;
+ sm->proc_setup_pending = false;
+ sm->session_gen++;
if (sm->measurement_timeout_id) {
timeout_remove(sm->measurement_timeout_id);
@@ -2111,15 +2261,29 @@ bool bt_rap_stop_measurement(void *hci_sm)
* Only the Initiator enables a procedure, so the same holds for a
* Reflector once the Initiator's procedure is running.
*/
- if ((!sm->procedure_active &&
- cs_get_current_state(sm) != CS_STATE_WAIT_PROC_CMPLT) ||
- !sm->cs_role_known || sm->cs_role != CS_INITIATOR) {
+ if (!sm->procedure_active &&
+ cs_get_current_state(sm) != CS_STATE_WAIT_PROC_CMPLT) {
DBG("No local CS procedure active, nothing to disable");
return true;
}
- return rap_send_hci_cs_procedure_enable(sm, sm->active_conn_handle,
- false);
+ if (!sm->cs_role_known)
+ return true;
+
+ /* Only the Initiator enables and disables a procedure. A Reflector
+ * can only end the remote one by removing the configuration.
+ */
+ if (sm->cs_role != CS_INITIATOR)
+ return rap_send_hci_cs_remove_config_command(sm,
+ sm->active_conn_handle);
+
+ if (!rap_send_hci_cs_procedure_enable(sm, sm->active_conn_handle,
+ false))
+ return false;
+
+ sm->disable_pending = true;
+
+ return true;
}
bool bt_rap_hci_set_procedure_data_cb(void *hci_sm,
@@ -2174,9 +2338,23 @@ void bt_rap_clear_conn_handle(void *hci_sm, uint16_t handle)
sm->sec_enable_pending = false;
sm->remote_caps_known = false;
sm->stop_requested = false;
+ sm->proc_setup_pending = false;
+ sm->disable_pending = false;
+ sm->stale_disable = false;
sm->current_state = CS_STATE_UNSPECIFIED;
}
+bool bt_rap_get_cs_role(void *hci_sm, uint8_t *role)
+{
+ struct cs_state_machine *sm = hci_sm;
+
+ if (!sm || !sm->cs_role_known)
+ return false;
+
+ *role = sm->cs_role;
+ return true;
+}
+
bool bt_rap_is_procedure_active(void *hci_sm)
{
struct cs_state_machine *sm = hci_sm;
diff --git a/src/shared/rap.h b/src/shared/rap.h
index 7ff5de227d70..e7f616c74797 100644
--- a/src/shared/rap.h
+++ b/src/shared/rap.h
@@ -284,6 +284,11 @@ void bt_rap_clear_conn_handle(void *hci_sm, uint16_t handle);
bool bt_rap_is_procedure_active(void *hci_sm);
+/* Negotiated CS role of the current configuration, once Config Complete has
+ * reported it. Returns false until then.
+ */
+bool bt_rap_get_cs_role(void *hci_sm, uint8_t *role);
+
void bt_rap_set_timeout_cb(void *hci_sm, void (*func)(void *),
void *user_data);
--
2.56.0
next prev parent reply other threads:[~2026-11-20 16:55 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 16:54 [PATCH BlueZ v4 00/18] Channel Sounding and RAP fixes Luiz Augusto von Dentz
2026-11-20 16:54 ` [PATCH BlueZ v4 01/18] gatt-database: Drop stale device state for removed bonds Luiz Augusto von Dentz
2026-11-20 22:32 ` Channel Sounding and RAP fixes bluez.test.bot
2026-11-20 16:54 ` [PATCH BlueZ v4 02/18] shared/rap: Reset the readable Ranging Counters per connection Luiz Augusto von Dentz
2026-11-20 16:54 ` [PATCH BlueZ v4 03/18] shared/rap: Subscribe on-demand data with the requested transport Luiz Augusto von Dentz
2026-11-20 16:54 ` [PATCH BlueZ v4 04/18] shared/rap: Subscribe to Ranging Data when a measurement starts Luiz Augusto von Dentz
2026-11-20 16:54 ` [PATCH BlueZ v4 05/18] client/cs: Fix use-after-free in stop_measurement_reply Luiz Augusto von Dentz
2026-11-20 16:54 ` [PATCH BlueZ v4 06/18] shared/hci: Look up the connection handle on the right link Luiz Augusto von Dentz
2026-11-20 16:54 ` [PATCH BlueZ v4 07/18] profiles/ranging: Let the Reflector run the CS setup phase Luiz Augusto von Dentz
2026-11-20 16:54 ` [PATCH BlueZ v4 08/18] profiles/ranging: Let StartMeasurement() pick the Initiator for Both Luiz Augusto von Dentz
2026-11-20 16:54 ` [PATCH BlueZ v4 09/18] profiles/ranging: Take the CS role from Config Complete Luiz Augusto von Dentz
2026-11-20 16:55 ` [PATCH BlueZ v4 10/18] profiles/ranging: Act on a CS configuration created by the peer Luiz Augusto von Dentz
2026-11-20 16:55 ` [PATCH BlueZ v4 11/18] profiles/ranging: Keep a peer-driven setup from being rewound Luiz Augusto von Dentz
2026-11-20 16:55 ` [PATCH BlueZ v4 12/18] profiles/ranging: Defer CS Security Enable until the link is encrypted Luiz Augusto von Dentz
2026-11-20 16:55 ` [PATCH BlueZ v4 13/18] profiles/ranging: Choose the CS antenna configuration from both ends Luiz Augusto von Dentz
2026-11-20 16:55 ` [PATCH BlueZ v4 14/18] profiles/ranging: Do not start a procedure after StopMeasurement() Luiz Augusto von Dentz
2026-11-20 16:55 ` Luiz Augusto von Dentz [this message]
2026-11-20 16:55 ` [PATCH BlueZ v4 16/18] shared/rap: Start the Real-time timeout when the procedure starts Luiz Augusto von Dentz
2026-11-20 16:55 ` [PATCH BlueZ v4 17/18] unit/test-rap: Add tests for Ranging Data subscription and counters Luiz Augusto von Dentz
2026-11-20 16:55 ` [PATCH BlueZ v4 18/18] shared/rap: Defer Ranging Data mode selection until Features is read Luiz Augusto von Dentz
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261120165510.1858959-16-luiz.dentz@gmail.com \
--to=luiz.dentz@gmail.com \
--cc=linux-bluetooth@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox