From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH BlueZ v2 13/16] profiles/ranging: Choose the CS antenna configuration from both ends
Date: Wed, 18 Nov 2026 14:58:41 -0500 [thread overview]
Message-ID: <20261118195846.393710-14-luiz.dentz@gmail.com> (raw)
In-Reply-To: <20261118195846.393710-1-luiz.dentz@gmail.com>
From: Cheng Jiang <cheng.jiang@oss.qualcomm.com>
Set Procedure Parameters sent the configured antenna configuration as
is, ACI 7 unless the application gave another. ACI 7 is the 2:2
configuration and needs two antennas and four antenna paths on both
ends. Against a single-antenna peer, or on a single-antenna local
Controller, the command was rejected with Rejected Due to Limited
Resources and no procedure was started.
The capabilities of both ends are read during the setup phase, but the
antenna counts were only logged. Keep the antenna count and antenna path
limit from both, and check the requested configuration against them and
the eight configurations of Core v6.2 Vol 6 Part A, Table 5.3. If it
fits, use it as given; otherwise use the first one that fits, with a
Preferred_Peer_Antenna that has at least as many bits set as the peer has
antenna elements in it.
Table 5.3 is written as "device A:device B" where device A is the
initiator (Vol 6 Part B, 2.4.2.47), so a configuration is asymmetric:
one antenna locally and four on the peer is ACI 6 as the Initiator but
ACI 3 as the Reflector. Use the negotiated CS role to decide which end
contributes which count, and refuse to send the command until both
capability reads and the role are known.
Assisted-by: Claude:claude-sonnet-5-5
---
profiles/ranging/rap_hci.c | 133 +++++++++++++++++++++++++++++++++++++
1 file changed, 133 insertions(+)
diff --git a/profiles/ranging/rap_hci.c b/profiles/ranging/rap_hci.c
index ad9c4c0feb9b..80379598d510 100644
--- a/profiles/ranging/rap_hci.c
+++ b/profiles/ranging/rap_hci.c
@@ -89,6 +89,12 @@ struct cs_state_machine {
struct timespec last_chan_class_time; /* For 1-second rate limit */
struct bt_rap_le_cs_config cs_config;
struct bt_rap_le_cs_frequency cs_frequency;
+ uint8_t remote_num_antennas; /* from Read Remote Supported Caps */
+ uint8_t remote_max_antenna_paths;
+ bool remote_caps_known;
+ uint8_t local_num_antennas; /* from LE CS Read Local Supported Caps */
+ uint8_t local_max_antenna_paths;
+ bool local_caps_known;
cs_timeout_func_t timeout_func;
void *timeout_data;
void (*proc_active_func)(bool active, void *user_data);
@@ -441,6 +447,13 @@ static void rap_rd_loc_supp_cap_done_cb(const void *data, uint8_t size,
rsp->max_consecutive_procedures_supported);
DBG(" Num Antennas: %u", rsp->num_antennas_supported);
DBG(" Max Antenna Paths: %u", rsp->max_antenna_paths_supported);
+
+ /* Needed to pick the antenna configuration, which has to fit both
+ * ends (Core v6.2 Vol 6 Part A, Table 5.3).
+ */
+ sm->local_num_antennas = rsp->num_antennas_supported;
+ sm->local_max_antenna_paths = rsp->max_antenna_paths_supported;
+ sm->local_caps_known = true;
DBG(" Roles Supported: 0x%02X", rsp->roles_supported);
DBG(" Modes Supported: 0x%02X", rsp->modes_supported);
DBG(" RTT Capability: 0x%02X", rsp->rtt_capability);
@@ -621,6 +634,104 @@ static void rap_send_hci_cs_security_enable_command(
DBG("CS Security Enable command sent successfully");
}
+/*
+ * Pick the antenna configuration for Set Procedure Parameters.
+ *
+ * The ACI values of Core v6.2 Vol 6 Part A, Table 5.3 are "device A:device B"
+ * antenna counts, where device A is the initiator (Vol 6 Part B, 2.4.2.47),
+ * so which end contributes which count depends on the local CS role. The
+ * entries are ordered so that the first match uses as many antenna paths as
+ * both ends allow.
+ */
+static const struct {
+ uint8_t aci;
+ uint8_t paths;
+ uint8_t init_ant;
+ uint8_t refl_ant;
+} cs_antenna_configs[] = {
+ { 0x07, 4, 2, 2 }, /* 2:2 */
+ { 0x03, 4, 4, 1 }, /* N_AP:1 */
+ { 0x06, 4, 1, 4 }, /* 1:N_AP */
+ { 0x02, 3, 3, 1 },
+ { 0x05, 3, 1, 3 },
+ { 0x01, 2, 2, 1 },
+ { 0x04, 2, 1, 2 },
+ { 0x00, 1, 1, 1 }, /* 1:1, always usable */
+};
+
+static bool cs_select_antenna_config(struct cs_state_machine *sm)
+{
+ uint8_t local_ant, peer_ant, max_paths, req, sel_peer = 0;
+ bool initiator;
+ size_t i, sel = ARRAY_SIZE(cs_antenna_configs);
+
+ /* Both capability reads have to be in before the configuration can be
+ * known to fit: ACI 7 (2:2), the default, is rejected with Rejected
+ * Due to Limited Resources if either end has a single antenna. The
+ * role decides which end of the configuration is ours, so it has to
+ * be settled as well.
+ */
+ if (!sm->local_caps_known || !sm->remote_caps_known ||
+ !sm->cs_role_known) {
+ error("CS Set Proc Params: capabilities or role unknown");
+ return false;
+ }
+
+ initiator = sm->cs_role == CS_INITIATOR;
+ local_ant = sm->local_num_antennas;
+ peer_ant = sm->remote_num_antennas;
+ max_paths = MIN(sm->local_max_antenna_paths,
+ sm->remote_max_antenna_paths);
+ req = sm->cs_frequency.tone_antenna_config_selection;
+
+ for (i = 0; i < ARRAY_SIZE(cs_antenna_configs); i++) {
+ uint8_t need_local = initiator ?
+ cs_antenna_configs[i].init_ant :
+ cs_antenna_configs[i].refl_ant;
+ uint8_t need_peer = initiator ?
+ cs_antenna_configs[i].refl_ant :
+ cs_antenna_configs[i].init_ant;
+
+ if (cs_antenna_configs[i].paths > max_paths ||
+ need_local > local_ant || need_peer > peer_ant)
+ continue;
+
+ /* The requested configuration fits, so keep it as given */
+ if (cs_antenna_configs[i].aci == req) {
+ DBG("ACI 0x%02X fits both ends", req);
+ return true;
+ }
+
+ if (sel == ARRAY_SIZE(cs_antenna_configs)) {
+ sel = i;
+ sel_peer = need_peer;
+ }
+ }
+
+ /* The 1:1 entry needs one antenna on each side, so this only happens
+ * if a side reported none.
+ */
+ if (sel == ARRAY_SIZE(cs_antenna_configs)) {
+ error("No usable CS antenna configuration: local %u ant, "
+ "peer %u ant", local_ant, peer_ant);
+ return false;
+ }
+
+ sm->cs_frequency.tone_antenna_config_selection =
+ cs_antenna_configs[sel].aci;
+ /* Preferred_Peer_Antenna needs at least as many bits set as the peer
+ * has antenna elements in this configuration.
+ */
+ sm->cs_frequency.preferred_peer_antenna = (1 << sel_peer) - 1;
+
+ DBG("ACI 0x%02X does not fit, using 0x%02X: local %u ant, peer %u ant, "
+ "%u paths, peer mask 0x%02X, initiator=%d", req,
+ cs_antenna_configs[sel].aci, local_ant, peer_ant, max_paths,
+ sm->cs_frequency.preferred_peer_antenna, initiator);
+
+ return true;
+}
+
static bool rap_send_hci_cs_set_procedure_parameters(
struct cs_state_machine *sm, uint16_t handle)
{
@@ -632,6 +743,9 @@ static bool rap_send_hci_cs_set_procedure_parameters(
return false;
}
+ if (!cs_select_antenna_config(sm))
+ return false;
+
DBG("Sending CS Set Procedure Parameters for handle 0x%04X", handle);
memset(&cmd, 0, sizeof(cmd));
@@ -841,6 +955,14 @@ static void rap_rd_rmt_supp_cap_cmplt_evt(const void *data, uint8_t size,
DBG("status=0x%02X, handle=0x%04X", evt->status, 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)) {
+ DBG("Not for this link, ignoring");
+ return;
+ }
+
/* Check status */
if (evt->status != 0) {
error("Remote capabilities failed with status 0x%02X",
@@ -861,6 +983,16 @@ static void rap_rd_rmt_supp_cap_cmplt_evt(const void *data, uint8_t size,
subfeatures_supported = le16_to_cpu(evt->subfeatures_supported);
DBG("subfeatures_supported=0x%04X", subfeatures_supported);
+ /* Remember the peer's antenna count: the antenna configuration used in
+ * Set Procedure Parameters must fit both ends (Core v6.2 Vol 4 Part E
+ * 7.8.140).
+ */
+ sm->remote_num_antennas = evt->num_antennas_supported;
+ sm->remote_max_antenna_paths = evt->max_antenna_paths_supported;
+ sm->remote_caps_known = true;
+ DBG("remote antennas=%u paths=%u", sm->remote_num_antennas,
+ sm->remote_max_antenna_paths);
+
bt_rap_set_remote_sw_time(sm->rap, evt->t_sw_time_supported);
/* Check Bit 1 of subfeatures_supported (0x0002) */
@@ -1992,6 +2124,7 @@ void bt_rap_clear_conn_handle(void *hci_sm, uint16_t handle)
sm->procedure_active = false;
sm->cs_role_known = false;
sm->sec_enable_pending = false;
+ sm->remote_caps_known = false;
sm->current_state = CS_STATE_UNSPECIFIED;
}
--
2.56.0
next prev parent reply other threads:[~2026-11-18 19:58 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 19:58 [PATCH BlueZ v2 00/16] Channel Sounding and RAP fixes Luiz Augusto von Dentz
2026-11-18 19:58 ` [PATCH BlueZ v2 01/16] gatt-database: Drop stale device state for removed bonds Luiz Augusto von Dentz
2026-11-19 1:57 ` Channel Sounding and RAP fixes bluez.test.bot
2026-11-18 19:58 ` [PATCH BlueZ v2 02/16] shared/rap: Reset the readable Ranging Counters per connection Luiz Augusto von Dentz
2026-11-18 19:58 ` [PATCH BlueZ v2 03/16] shared/rap: Subscribe on-demand data with the requested transport Luiz Augusto von Dentz
2026-11-18 19:58 ` [PATCH BlueZ v2 04/16] shared/rap: Subscribe to Ranging Data when a measurement starts Luiz Augusto von Dentz
2026-11-18 19:58 ` [PATCH BlueZ v2 05/16] client/cs: Fix use-after-free in stop_measurement_reply Luiz Augusto von Dentz
2026-11-18 19:58 ` [PATCH BlueZ v2 06/16] shared/hci: Look up the connection handle on the right link Luiz Augusto von Dentz
2026-11-18 19:58 ` [PATCH BlueZ v2 07/16] profiles/ranging: Let the Reflector run the CS setup phase Luiz Augusto von Dentz
2026-11-18 19:58 ` [PATCH BlueZ v2 08/16] profiles/ranging: Let StartMeasurement() pick the Initiator for Both Luiz Augusto von Dentz
2026-11-18 19:58 ` [PATCH BlueZ v2 09/16] profiles/ranging: Take the CS role from Config Complete Luiz Augusto von Dentz
2026-11-18 19:58 ` [PATCH BlueZ v2 10/16] profiles/ranging: Act on a CS configuration created by the peer Luiz Augusto von Dentz
2026-11-18 19:58 ` [PATCH BlueZ v2 11/16] profiles/ranging: Keep a peer-driven setup from being rewound Luiz Augusto von Dentz
2026-11-18 19:58 ` [PATCH BlueZ v2 12/16] profiles/ranging: Defer CS Security Enable until the link is encrypted Luiz Augusto von Dentz
2026-11-18 19:58 ` Luiz Augusto von Dentz [this message]
2026-11-18 19:58 ` [PATCH BlueZ v2 14/16] profiles/ranging: Do not start a procedure after StopMeasurement() Luiz Augusto von Dentz
2026-11-18 19:58 ` [PATCH BlueZ v2 15/16] shared/rap: Start the Real-time timeout when the procedure starts Luiz Augusto von Dentz
2026-11-18 19:58 ` [PATCH BlueZ v2 16/16] unit/test-rap: Add tests for Ranging Data subscription and counters 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=20261118195846.393710-14-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