1 /*
2  * Copyright (c) 2020 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdint.h>
8 #include <zephyr/kernel.h>
9 #include <soc.h>
10 #include <zephyr/sys/util.h>
11 #include <zephyr/bluetooth/hci_types.h>
12 
13 #include "hal/cpu.h"
14 #include "hal/ccm.h"
15 
16 #include "util/util.h"
17 #include "util/mem.h"
18 #include "util/memq.h"
19 #include "util/mfifo.h"
20 #include "util/dbuf.h"
21 
22 #include "pdu_df.h"
23 #include "lll/pdu_vendor.h"
24 #include "pdu.h"
25 
26 #include "lll.h"
27 #include "lll/lll_adv_types.h"
28 #include "lll_adv.h"
29 #include "lll/lll_adv_pdu.h"
30 #include "lll_scan.h"
31 #include "lll/lll_df_types.h"
32 #include "lll_sync.h"
33 #include "lll_sync_iso.h"
34 #include "lll_conn.h"
35 #include "lll_conn_iso.h"
36 #include "lll_df.h"
37 #include "lll/lll_df_internal.h"
38 
39 #include "isoal.h"
40 #include "ull_scan_types.h"
41 #include "ull_sync_types.h"
42 #include "ull_adv_types.h"
43 #include "ull_tx_queue.h"
44 #include "ull_conn_types.h"
45 #include "ull_iso_types.h"
46 #include "ull_conn_iso_types.h"
47 #include "ull_df_types.h"
48 #include "ull_llcp.h"
49 
50 #include "ull_internal.h"
51 #include "ull_adv_internal.h"
52 #include "ull_sync_internal.h"
53 #include "ull_conn_internal.h"
54 #include "ull_df_internal.h"
55 
56 #include "ll.h"
57 
58 #include "hal/debug.h"
59 
60 #if defined(CONFIG_BT_CTLR_DF_SCAN_CTE_RX) || defined(CONFIG_BT_CTLR_DF_CONN_CTE_RX) || \
61 	defined(CONFIG_BT_CTLR_DTM_HCI_DF_IQ_REPORT)
62 
63 #define CTE_LEN_MAX_US 160U
64 
65 #define IQ_REPORT_HEADER_SIZE      (offsetof(struct node_rx_iq_report, pdu))
66 #define IQ_REPORT_STRUCT_OVERHEAD  (IQ_REPORT_HEADER_SIZE)
67 #define IQ_SAMPLE_SIZE (sizeof(struct iq_sample))
68 
69 #define IQ_REPORT_RX_NODE_POOL_ELEMENT_SIZE              \
70 	MROUND(IQ_REPORT_STRUCT_OVERHEAD + (IQ_SAMPLE_TOTAL_CNT * IQ_SAMPLE_SIZE))
71 #define IQ_REPORT_POOL_SIZE (IQ_REPORT_RX_NODE_POOL_ELEMENT_SIZE * IQ_REPORT_CNT)
72 
73 /* Memory pool to store IQ reports data */
74 static struct {
75 	void *free;
76 	uint8_t pool[IQ_REPORT_POOL_SIZE];
77 } mem_iq_report;
78 
79 /* FIFO to store free IQ report norde_rx objects for LLL to ULL handover. */
80 static MFIFO_DEFINE(iq_report_free, sizeof(void *), IQ_REPORT_CNT);
81 
82 /* Number of available instance of linked list to be used for node_rx_iq_reports. */
83 static uint8_t mem_link_iq_report_quota_pdu;
84 
85 #if defined(CONFIG_BT_CTLR_DF_DEBUG_ENABLE)
86 /* Debug variable to store information about current number of allocated node_rx_iq_report.
87  * It supports verification if there is a resource leak.
88  * The variable may not be used when multiple
89  * advertising syncs are enabled. Checks may fail because CTE reception may be enabled/disabled
90  * in different moments, hence there may be allocated reports when it is expected not to.
91  */
92 COND_CODE_1(CONFIG_BT_PER_ADV_SYNC_MAX, (static uint32_t iq_report_alloc_count;), (EMPTY))
93 #define IF_SINGLE_ADV_SYNC_SET(code) COND_CODE_1(CONFIG_BT_PER_ADV_SYNC_MAX, (code), (EMPTY))
94 #endif /* CONFIG_BT_CTLR_DF_DEBUG_ENABLE */
95 #endif /* CONFIG_BT_CTLR_DF_SCAN_CTE_RX || CONFIG_BT_CTLR_DF_CONN_CTE_RX*/
96 
97 #if defined(CONFIG_BT_CTLR_DF_SCAN_CTE_RX)
98 /* Make sure the configuration follows BT Core 5.3. Vol 4 Part E section 7.8.82 about
99  * max CTE count sampled in periodic advertising chain.
100  */
101 BUILD_ASSERT(CONFIG_BT_CTLR_DF_PER_SCAN_CTE_NUM_MAX <= BT_HCI_LE_SAMPLE_CTE_COUNT_MAX,
102 	     "Max advertising CTE count exceed BT_HCI_LE_SAMPLE_CTE_COUNT_MAX");
103 #endif /* CONFIG_BT_CTLR_DF_SCAN_CTE_RX */
104 
105 /* ToDo:
106  * - Add release of df_adv_cfg when adv_sync is released.
107  *   Open question, should df_adv_cfg be released when Adv. CTE is disabled?
108  *   If yes that would mean, end user must always run ll_df_set_cl_cte_tx_params
109  *   before consecutive Adv CTE enable.
110  */
111 
112 #if defined(CONFIG_BT_CTLR_DF_ADV_CTE_TX)
113 /* Make sure the configuration follows BT Core 5.3. Vol 4 Part E section 7.8.80 about
114  * max CTE count in a periodic advertising chain.
115  */
116 BUILD_ASSERT(CONFIG_BT_CTLR_DF_PER_ADV_CTE_NUM_MAX <= BT_HCI_LE_CTE_COUNT_MAX,
117 	     "Max advertising CTE count exceed BT_HCI_LE_CTE_COUNT_MAX");
118 
119 static struct lll_df_adv_cfg lll_df_adv_cfg_pool[CONFIG_BT_CTLR_ADV_AUX_SET];
120 static void *df_adv_cfg_free;
121 static uint8_t cte_info_clear(struct ll_adv_set *adv, struct lll_df_adv_cfg *df_cfg,
122 			      uint8_t *ter_idx, struct pdu_adv **first_pdu);
123 #endif /* CONFIG_BT_CTLR_DF_ADV_CTE_TX */
124 
125 /* @brief Function performs common steps for initialization and reset
126  * of Direction Finding ULL module.
127  *
128  * @return      Zero in case of success, other value in case of failure.
129  */
130 static int init_reset(void);
131 
132 #if defined(CONFIG_BT_CTLR_DF_ADV_CTE_TX)
133 /* @brief Function acquires memory for DF advertising configuration.
134  *
135  * The memory is acquired from private @ref lll_df_adv_cfg_pool memory store.
136  *
137  * @return Pointer to lll_df_adv_cfg or NULL if there is no more free memory.
138  */
139 static struct lll_df_adv_cfg *df_adv_cfg_acquire(void);
140 
141 static uint8_t cte_info_set(struct ll_adv_set *adv, struct lll_df_adv_cfg *df_cfg, uint8_t *ter_idx,
142 			    struct pdu_adv **first_pdu);
143 #endif /* CONFIG_BT_CTLR_DF_ADV_CTE_TX */
144 
145 /* @brief Function performs ULL Direction Finding initialization
146  *
147  * @return      Zero in case of success, other value in case of failure.
148  */
ull_df_init(void)149 int ull_df_init(void)
150 {
151 	int err;
152 
153 	err = init_reset();
154 	if (err) {
155 		return err;
156 	}
157 
158 	return 0;
159 }
160 
161 /* @brief Function performs ULL Direction Finding reset
162  *
163  * @return      Zero in case of success, other value in case of failure.
164  */
ull_df_reset(void)165 int ull_df_reset(void)
166 {
167 	int err;
168 
169 #if defined(CONFIG_BT_CTLR_DF_ADV_CTE_TX)
170 	struct ll_adv_set *adv;
171 	uint8_t handle;
172 
173 	/* Get the advertising set instance */
174 	for (handle = 0U; handle < BT_CTLR_ADV_SET; handle++) {
175 		adv = ull_adv_is_created_get(handle);
176 		if (!adv) {
177 			continue;
178 		}
179 
180 		adv->df_cfg = NULL;
181 	}
182 #endif /* CONFIG_BT_CTLR_DF_ADV_CTE_TX */
183 
184 	err = init_reset();
185 	if (err) {
186 		return err;
187 	}
188 
189 	return 0;
190 }
191 
init_reset(void)192 static int init_reset(void)
193 {
194 #if defined(CONFIG_BT_CTLR_DF_ADV_CTE_TX)
195 	/* Initialize advertising DF memory configuration pool. */
196 	mem_init(lll_df_adv_cfg_pool, sizeof(struct lll_df_adv_cfg),
197 		 sizeof(lll_df_adv_cfg_pool) / sizeof(struct lll_df_adv_cfg),
198 		 &df_adv_cfg_free);
199 #endif /* CONFIG_BT_CTLR_DF_ADV_CTE_TX */
200 
201 #if defined(CONFIG_BT_CTLR_DF_SCAN_CTE_RX) || defined(CONFIG_BT_CTLR_DF_CONN_CTE_RX) || \
202 	defined(CONFIG_BT_CTLR_DTM_HCI_DF_IQ_REPORT)
203 	/* Re-initialize the free IQ report mfifo */
204 	MFIFO_INIT(iq_report_free);
205 
206 	/* Initialize IQ report memory pool. */
207 	mem_init(mem_iq_report.pool, (IQ_REPORT_RX_NODE_POOL_ELEMENT_SIZE),
208 		 sizeof(mem_iq_report.pool) / (IQ_REPORT_RX_NODE_POOL_ELEMENT_SIZE),
209 		 &mem_iq_report.free);
210 
211 	/* Allocate free IQ report node rx */
212 	mem_link_iq_report_quota_pdu = IQ_REPORT_CNT;
213 	ull_df_rx_iq_report_alloc(UINT8_MAX);
214 #endif /* CONFIG_BT_CTLR_DF_SCAN_CTE_RX || CONFIG_BT_CTLR_DF_CONN_CTE_RX */
215 	return 0;
216 }
217 
218 #if defined(CONFIG_BT_CTLR_DF_ADV_CTE_TX)
219 /* @brief Function sets CTE transmission parameters for periodic advertising.
220  *
221  * @param[in]adv_handle                 Handle of advertising set.
222  * @param[in]cte_len                    Length of CTE in 8us units.
223  * @param[in]cte_type                   Type of CTE to be used for transmission.
224  * @param[in]cte_count                  Number of CTE that should be transmitted
225  *                                      during each periodic advertising
226  *                                      interval.
227  * @param[in]num_ant_ids                Number of antenna IDs in switching
228  *                                      pattern. May be zero if CTE type is
229  *                                      AoA.
230  * @param[in]ant_ids                    Array of antenna IDs in a switching
231  *                                      pattern. May be NULL if CTE type is AoA.
232  *
233  * @return Status of command completion.
234  */
ll_df_set_cl_cte_tx_params(uint8_t adv_handle,uint8_t cte_len,uint8_t cte_type,uint8_t cte_count,uint8_t num_ant_ids,uint8_t * ant_ids)235 uint8_t ll_df_set_cl_cte_tx_params(uint8_t adv_handle, uint8_t cte_len,
236 				   uint8_t cte_type, uint8_t cte_count,
237 				   uint8_t num_ant_ids, uint8_t *ant_ids)
238 {
239 	struct lll_df_adv_cfg *cfg;
240 	struct ll_adv_set *adv;
241 
242 	/* Get the advertising set instance */
243 	adv = ull_adv_is_created_get(adv_handle);
244 	if (!adv) {
245 		return BT_HCI_ERR_UNKNOWN_ADV_IDENTIFIER;
246 	}
247 
248 	if (cte_len < BT_HCI_LE_CTE_LEN_MIN ||
249 	    cte_len > BT_HCI_LE_CTE_LEN_MAX) {
250 		return BT_HCI_ERR_UNSUPP_FEATURE_PARAM_VAL;
251 	}
252 
253 	/* Max number of CTE in a single periodic advertising event is limited
254 	 * by configuration. It shall not be greater than BT_HCI_LE_CTE_COUNT_MAX.
255 	 */
256 	if (cte_count < BT_HCI_LE_CTE_COUNT_MIN ||
257 	    cte_count > CONFIG_BT_CTLR_DF_PER_ADV_CTE_NUM_MAX) {
258 		return BT_HCI_ERR_UNSUPP_FEATURE_PARAM_VAL;
259 	}
260 
261 	if (!(IS_ENABLED(CONFIG_BT_CTLR_DF_ADV_CTE_TX) &&
262 	      ((cte_type == BT_HCI_LE_AOA_CTE) ||
263 		(IS_ENABLED(CONFIG_BT_CTLR_DF_ANT_SWITCH_TX) &&
264 		 ((cte_type == BT_HCI_LE_AOD_CTE_2US) ||
265 		  (IS_ENABLED(CONFIG_BT_CTLR_DF_ANT_SWITCH_1US) &&
266 		   cte_type == BT_HCI_LE_AOD_CTE_1US)))))) {
267 		return BT_HCI_ERR_UNSUPP_FEATURE_PARAM_VAL;
268 	}
269 
270 	if ((cte_type == BT_HCI_LE_AOD_CTE_1US || cte_type == BT_HCI_LE_AOD_CTE_2US) &&
271 	    (num_ant_ids < BT_HCI_LE_CTE_LEN_MIN ||
272 	     num_ant_ids > BT_CTLR_DF_MAX_ANT_SW_PATTERN_LEN || !ant_ids)) {
273 		return BT_HCI_ERR_UNSUPP_FEATURE_PARAM_VAL;
274 	}
275 
276 	if (!adv->df_cfg) {
277 		adv->df_cfg = df_adv_cfg_acquire();
278 	}
279 
280 	cfg = adv->df_cfg;
281 
282 	if (cfg->is_enabled) {
283 		return BT_HCI_ERR_CMD_DISALLOWED;
284 	}
285 
286 	cfg->cte_count = cte_count;
287 	cfg->cte_length = cte_len;
288 	cfg->cte_type = cte_type;
289 
290 	if (cte_type == BT_HCI_LE_AOD_CTE_1US ||
291 	    cte_type == BT_HCI_LE_AOD_CTE_2US) {
292 		/* Note:
293 		 * Are we going to check antenna identifiers if they are valid?
294 		 * BT 5.2 Core spec. Vol. 4 Part E Section 7.8.80 says
295 		 * that not all controller may be able to do that.
296 		 */
297 		memcpy(cfg->ant_ids, ant_ids, num_ant_ids);
298 		cfg->ant_sw_len = num_ant_ids;
299 	} else {
300 		cfg->ant_sw_len = 0;
301 	}
302 
303 	return BT_HCI_ERR_SUCCESS;
304 }
305 
306 /* @brief Function enables or disables CTE TX for periodic advertising.
307  *
308  * @param[in] handle                    Advertising set handle.
309  * @param[in] cte_enable                Enable or disable CTE TX
310  *
311  * @return Status of command completion.
312  */
ll_df_set_cl_cte_tx_enable(uint8_t adv_handle,uint8_t cte_enable)313 uint8_t ll_df_set_cl_cte_tx_enable(uint8_t adv_handle, uint8_t cte_enable)
314 {
315 	struct lll_adv_sync *lll_sync;
316 	struct lll_df_adv_cfg *df_cfg;
317 	struct ll_adv_sync_set *sync;
318 	struct ll_adv_set *adv;
319 	uint8_t err, ter_idx;
320 	struct pdu_adv *pdu;
321 
322 	/* Get the advertising set instance */
323 	adv = ull_adv_is_created_get(adv_handle);
324 	if (!adv) {
325 		return BT_HCI_ERR_UNKNOWN_ADV_IDENTIFIER;
326 	}
327 
328 	lll_sync = adv->lll.sync;
329 	/* If there is no sync in advertising set, then the HCI_LE_Set_-
330 	 * Periodic_Advertising_Parameters command was not issued before.
331 	 */
332 	if (!lll_sync) {
333 		return BT_HCI_ERR_CMD_DISALLOWED;
334 	}
335 
336 	sync = HDR_LLL2ULL(lll_sync);
337 
338 	/* If df_cfg is NULL, then the HCI_LE_Set_Connectionless_CTE_Transmit_-
339 	 * Parameters command was not issued before.
340 	 */
341 	df_cfg = adv->df_cfg;
342 	if (!df_cfg) {
343 		return BT_HCI_ERR_CMD_DISALLOWED;
344 	}
345 
346 	if (adv->lll.phy_s == PHY_CODED) {
347 		return BT_HCI_ERR_CMD_DISALLOWED;
348 	}
349 
350 	if (!cte_enable) {
351 		if (!df_cfg->is_enabled) {
352 			return BT_HCI_ERR_CMD_DISALLOWED;
353 		}
354 
355 		err = cte_info_clear(adv, df_cfg, &ter_idx, &pdu);
356 		if (err) {
357 			return err;
358 		}
359 
360 		df_cfg->is_enabled = 0U;
361 	} else {
362 		if (df_cfg->is_enabled) {
363 			return BT_HCI_ERR_CMD_DISALLOWED;
364 		}
365 
366 		err = cte_info_set(adv, df_cfg, &ter_idx, &pdu);
367 		if (err) {
368 			return err;
369 		}
370 
371 		df_cfg->is_enabled = 1U;
372 	}
373 
374 	if (sync->is_started) {
375 		err = ull_adv_sync_time_update(sync, pdu);
376 		if (err) {
377 			return err;
378 		}
379 	}
380 
381 	lll_adv_sync_data_enqueue(adv->lll.sync, ter_idx);
382 
383 	return BT_HCI_ERR_SUCCESS;
384 }
385 #endif /* CONFIG_BT_CTLR_DF_ADV_CTE_TX */
386 
387 #if defined(CONFIG_BT_CTLR_DF_SCAN_CTE_RX)
388 /* @brief Function sets IQ sampling enabled or disabled.
389  *
390  * Set IQ sampling enable for received PDUs that has attached CTE.
391  *
392  * @param[in]handle                     Connection handle.
393  * @param[in]sampling_enable            Enable or disable CTE RX
394  * @param[in]slot_durations             Switching and sampling slot durations for
395  *                                      AoA mode.
396  * @param[in]max_cte_count              Maximum number of sampled CTEs in single
397  *                                      periodic advertising event.
398  * @param[in]switch_pattern_len         Number of antenna ids in switch pattern.
399  * @param[in]ant_ids                    Array of antenna identifiers.
400  *
401  * @return Status of command completion.
402  *
403  * @Note This function may put TX thread into wait state. This may lead to a
404  *       situation that ll_sync_set instance is released (RX thread has higher
405  *       priority than TX thread). ll_sync_set instance may not be accessed after
406  *       call to ull_sync_slot_update.
407  *       This is related with possible race condition with RX thread handling
408  *       periodic sync lost event.
409  */
ll_df_set_cl_iq_sampling_enable(uint16_t handle,uint8_t sampling_enable,uint8_t slot_durations,uint8_t max_cte_count,uint8_t switch_pattern_len,uint8_t * ant_ids)410 uint8_t ll_df_set_cl_iq_sampling_enable(uint16_t handle,
411 					uint8_t sampling_enable,
412 					uint8_t slot_durations,
413 					uint8_t max_cte_count,
414 					uint8_t switch_pattern_len,
415 					uint8_t *ant_ids)
416 {
417 	struct lll_df_sync_cfg *cfg, *cfg_prev;
418 	uint32_t slot_minus_us = 0;
419 	uint32_t slot_plus_us = 0;
420 	struct ll_sync_set *sync;
421 	struct lll_sync *lll;
422 	uint8_t cfg_idx;
423 
424 	/* After this call and before ull_sync_slot_update the function may not
425 	 * call any kernel API that may put the thread into wait state. It may
426 	 * cause race condition with RX thread and lead to use of released memory.
427 	 */
428 	sync = ull_sync_is_enabled_get(handle);
429 	if (!sync) {
430 		return BT_HCI_ERR_UNKNOWN_ADV_IDENTIFIER;
431 	}
432 
433 	lll = &sync->lll;
434 
435 	/* CTE is not supported for CODED Phy */
436 	if (lll->phy == PHY_CODED) {
437 		return BT_HCI_ERR_CMD_DISALLOWED;
438 	}
439 
440 	cfg_prev = lll_df_sync_cfg_curr_get(&lll->df_cfg);
441 	cfg = lll_df_sync_cfg_alloc(&lll->df_cfg, &cfg_idx);
442 
443 	if (!sampling_enable) {
444 		if (!cfg_prev->is_enabled) {
445 			/* Disable already disabled CTE Rx */
446 			return BT_HCI_ERR_SUCCESS;
447 		}
448 		slot_minus_us = CTE_LEN_MAX_US;
449 		cfg->is_enabled = 0U;
450 	} else {
451 
452 #if defined(CONFIG_BT_CTLR_DF_DEBUG_ENABLE)
453 		/* When CTE is enabled there should be no iq report allocated */
454 		IF_SINGLE_ADV_SYNC_SET(LL_ASSERT(iq_report_alloc_count == 0));
455 #endif /* CONFIG_BT_CTLR_DF_DEBUG_ENABLE */
456 
457 		/* Enable of already enabled CTE updates AoA configuration */
458 
459 		/* According to Core 5.3 Vol 4, Part E, section 7.8.82 slot_durations,
460 		 * switch_pattern_len and ant_ids are used only for AoA and do not affect
461 		 * reception of AoD CTE. If AoA is not supported relax command validation
462 		 * to improve interoperability with different Host implementations.
463 		 */
464 		if (IS_ENABLED(CONFIG_BT_CTLR_DF_ANT_SWITCH_RX)) {
465 			if (!((IS_ENABLED(CONFIG_BT_CTLR_DF_ANT_SWITCH_1US) &&
466 			       slot_durations == BT_HCI_LE_ANTENNA_SWITCHING_SLOT_1US) ||
467 			      slot_durations == BT_HCI_LE_ANTENNA_SWITCHING_SLOT_2US)) {
468 				return BT_HCI_ERR_UNSUPP_FEATURE_PARAM_VAL;
469 			}
470 
471 			if (switch_pattern_len < BT_HCI_LE_SWITCH_PATTERN_LEN_MIN ||
472 			    switch_pattern_len > BT_CTLR_DF_MAX_ANT_SW_PATTERN_LEN || !ant_ids) {
473 				return BT_HCI_ERR_UNSUPP_FEATURE_PARAM_VAL;
474 			}
475 
476 			(void)memcpy(cfg->ant_ids, ant_ids, switch_pattern_len);
477 		}
478 		cfg->slot_durations = slot_durations;
479 		cfg->ant_sw_len = switch_pattern_len;
480 
481 		/* max_cte_count equal to 0x0 has special meaning - sample and
482 		 * report continuously until there are CTEs received.
483 		 */
484 		if (max_cte_count > CONFIG_BT_CTLR_DF_PER_SCAN_CTE_NUM_MAX) {
485 			return BT_HCI_ERR_UNSUPP_FEATURE_PARAM_VAL;
486 		}
487 		cfg->max_cte_count = max_cte_count;
488 
489 		cfg->is_enabled = 1U;
490 
491 		if (!cfg_prev->is_enabled) {
492 			/* Extend sync event by maximum CTE duration.
493 			 * CTE duration depends on transmitter configuration
494 			 * so it is unknown for receiver upfront.
495 			 * BT_HCI_LE_CTE_LEN_MAX is in 8us units.
496 			 */
497 			slot_plus_us = BT_HCI_LE_CTE_LEN_MAX * 8U;
498 		}
499 	}
500 
501 	lll_df_sync_cfg_enqueue(&lll->df_cfg, cfg_idx);
502 
503 	if (slot_plus_us || slot_minus_us) {
504 		int err;
505 		/* Update of sync slot may fail due to race condition.
506 		 * If periodic sync is lost, the ticker event will be stopped.
507 		 * The stop operation may preempt call to this functions.
508 		 * So update may be called after that. Accept this failure
509 		 * (-ENOENT) gracefully.
510 		 * Periodic sync lost event also disables the CTE sampling.
511 		 */
512 		err = ull_sync_slot_update(sync, slot_plus_us, slot_minus_us);
513 		LL_ASSERT(err == 0 || err == -ENOENT);
514 	}
515 
516 	return 0;
517 }
518 
ull_df_sync_cfg_init(struct lll_df_sync * df_cfg)519 void ull_df_sync_cfg_init(struct lll_df_sync *df_cfg)
520 {
521 	(void)memset(&df_cfg->cfg, 0, sizeof(df_cfg->cfg));
522 	df_cfg->first = 0U;
523 	df_cfg->last = 0U;
524 }
525 
ull_df_sync_cfg_is_not_enabled(struct lll_df_sync * df_cfg)526 bool ull_df_sync_cfg_is_not_enabled(struct lll_df_sync *df_cfg)
527 {
528 	struct lll_df_sync_cfg *cfg;
529 
530 	/* If new CTE sampling configuration was enqueued, get reference to
531 	 * latest configuration without swapping buffers. Buffer should be
532 	 * swapped only at the beginning of the radio event.
533 	 *
534 	 * We may not get here if CTE sampling is not enabled in current
535 	 * configuration.
536 	 */
537 	if (lll_df_sync_cfg_is_modified(df_cfg)) {
538 		cfg = lll_df_sync_cfg_peek(df_cfg);
539 	} else {
540 		cfg = lll_df_sync_cfg_curr_get(df_cfg);
541 	}
542 
543 	return !cfg->is_enabled;
544 }
545 #endif /* CONFIG_BT_CTLR_DF_SCAN_CTE_RX */
546 
547 #if defined(CONFIG_BT_CTLR_DF_SCAN_CTE_RX) || defined(CONFIG_BT_CTLR_DF_CONN_CTE_RX) || \
548 	defined(CONFIG_BT_CTLR_DTM_HCI_DF_IQ_REPORT)
ull_df_iq_report_alloc_peek(uint8_t count)549 void *ull_df_iq_report_alloc_peek(uint8_t count)
550 {
551 	if (count > MFIFO_AVAIL_COUNT_GET(iq_report_free)) {
552 		return NULL;
553 	}
554 
555 	return MFIFO_DEQUEUE_PEEK(iq_report_free);
556 }
557 
ull_df_iq_report_alloc_peek_iter(uint8_t * idx)558 void *ull_df_iq_report_alloc_peek_iter(uint8_t *idx)
559 {
560 	return *(void **)MFIFO_DEQUEUE_ITER_GET(iq_report_free, idx);
561 }
562 
ull_df_iq_report_alloc(void)563 void *ull_df_iq_report_alloc(void)
564 {
565 #if defined(CONFIG_BT_CTLR_DF_DEBUG_ENABLE)
566 	IF_SINGLE_ADV_SYNC_SET(iq_report_alloc_count++);
567 #endif /* CONFIG_BT_CTLR_DF_DEBUG_ENABLE */
568 
569 	return MFIFO_DEQUEUE(iq_report_free);
570 }
571 
ull_df_iq_report_mem_release(struct node_rx_pdu * rx)572 void ull_df_iq_report_mem_release(struct node_rx_pdu *rx)
573 {
574 #if defined(CONFIG_BT_CTLR_DF_DEBUG_ENABLE)
575 	IF_SINGLE_ADV_SYNC_SET(iq_report_alloc_count--);
576 #endif /* CONFIG_BT_CTLR_DF_DEBUG_ENABLE */
577 
578 	mem_release(rx, &mem_iq_report.free);
579 }
580 
ull_iq_report_link_inc_quota(int8_t delta)581 void ull_iq_report_link_inc_quota(int8_t delta)
582 {
583 	LL_ASSERT(delta <= 0 || mem_link_iq_report_quota_pdu < (IQ_REPORT_CNT));
584 
585 	mem_link_iq_report_quota_pdu += delta;
586 }
587 
ull_df_rx_iq_report_alloc(uint8_t max)588 void ull_df_rx_iq_report_alloc(uint8_t max)
589 {
590 	uint8_t idx;
591 
592 	if (max > mem_link_iq_report_quota_pdu) {
593 		max = mem_link_iq_report_quota_pdu;
594 	}
595 
596 	while ((max--) && MFIFO_ENQUEUE_IDX_GET(iq_report_free, &idx)) {
597 		memq_link_t *link;
598 		struct node_rx_hdr *rx;
599 
600 		link = ll_rx_link_alloc();
601 		if (!link) {
602 			return;
603 		}
604 
605 		rx = mem_acquire(&mem_iq_report.free);
606 		if (!rx) {
607 			ll_rx_link_release(link);
608 			return;
609 		}
610 
611 		rx->link = link;
612 
613 		MFIFO_BY_IDX_ENQUEUE(iq_report_free, idx, rx);
614 
615 		ull_iq_report_link_inc_quota(-1);
616 	}
617 }
618 #endif /* CONFIG_BT_CTLR_DF_SCAN_CTE_RX || CONFIG_BT_CTLR_DF_CONN_CTE_RX */
619 
620 #if defined(CONFIG_BT_CTLR_DF_CONN_CTE_RX)
ull_df_conn_cfg_is_not_enabled(struct lll_df_conn_rx_cfg * rx_cfg)621 bool ull_df_conn_cfg_is_not_enabled(struct lll_df_conn_rx_cfg *rx_cfg)
622 {
623 	struct lll_df_conn_rx_params *rx_params;
624 
625 	/* If new CTE sampling configuration was enqueued, get reference to
626 	 * latest configuration without swapping buffers. Buffer should be
627 	 * swapped only at the beginning of the radio event.
628 	 *
629 	 * We may not get here if CTE sampling is not enabled in current
630 	 * configuration.
631 	 */
632 	if (dbuf_is_modified(&rx_cfg->hdr)) {
633 		rx_params = dbuf_peek(&rx_cfg->hdr);
634 	} else {
635 		rx_params = dbuf_curr_get(&rx_cfg->hdr);
636 	}
637 
638 	return !rx_params->is_enabled;
639 }
640 #endif /* CONFIG_BT_CTLR_DF_CONN_CTE_RX */
641 
642 #if defined(CONFIG_BT_CTLR_DF_ADV_CTE_TX)
643 /* @brief Function releases unused memory for DF advertising configuration.
644  *
645  * The memory is released to private @ref lll_df_adv_cfg_pool memory store.
646  *
647  * @param[in] df_adv_cfg        Pointer to lll_df_adv_cfg memory to be released.
648  */
ull_df_adv_cfg_release(struct lll_df_adv_cfg * df_adv_cfg)649 void ull_df_adv_cfg_release(struct lll_df_adv_cfg *df_adv_cfg)
650 {
651 	mem_release(df_adv_cfg, &df_adv_cfg_free);
652 }
653 
df_adv_cfg_acquire(void)654 static struct lll_df_adv_cfg *df_adv_cfg_acquire(void)
655 {
656 	struct lll_df_adv_cfg *df_adv_cfg;
657 
658 	df_adv_cfg = mem_acquire(&df_adv_cfg_free);
659 	if (!df_adv_cfg) {
660 		return NULL;
661 	}
662 
663 	df_adv_cfg->is_enabled = 0U;
664 
665 	return df_adv_cfg;
666 }
667 
668 /*
669  * @brief Function sets content of cte_info field for periodic advertising
670  *
671  * @param adv            Pointer to periodic advertising set.
672  * @param df_cfg         Pointer to direction finding configuration
673  * @param[out] ter_idx   Pointer used to return index of allocated or updated PDU.
674  *                       Index is required for scheduling the PDU for transmission in LLL.
675  * @param[out] first_pdu Pointer to return address of first PDU in a periodic advertising chain
676  *
677  * @return Zero in case of success, other value in case of failure.
678  */
cte_info_set(struct ll_adv_set * adv,struct lll_df_adv_cfg * df_cfg,uint8_t * ter_idx,struct pdu_adv ** first_pdu)679 static uint8_t cte_info_set(struct ll_adv_set *adv, struct lll_df_adv_cfg *df_cfg, uint8_t *ter_idx,
680 			    struct pdu_adv **first_pdu)
681 {
682 	struct pdu_adv *pdu_prev, *pdu;
683 	struct lll_adv_sync *lll_sync;
684 	struct pdu_cte_info cte_info;
685 	void *extra_data;
686 	uint8_t err;
687 
688 	lll_sync = adv->lll.sync;
689 
690 	cte_info.type = df_cfg->cte_type;
691 	cte_info.time = df_cfg->cte_length;
692 	cte_info.rfu = 0U;
693 
694 	/* Note: ULL_ADV_PDU_EXTRA_DATA_ALLOC_ALWAYS is just information that extra_data
695 	 * is required in case of this ull_adv_sync_pdu_alloc.
696 	 */
697 	extra_data = NULL;
698 	err = ull_adv_sync_pdu_alloc(adv, ULL_ADV_PDU_EXTRA_DATA_ALLOC_ALWAYS, &pdu_prev, &pdu,
699 				     NULL, &extra_data, ter_idx);
700 	if (err != BT_HCI_ERR_SUCCESS) {
701 		return err;
702 	}
703 
704 	if (extra_data) {
705 		ull_adv_sync_extra_data_set_clear(NULL, extra_data, ULL_ADV_PDU_HDR_FIELD_CTE_INFO,
706 						  0, df_cfg);
707 	}
708 
709 #if (CONFIG_BT_CTLR_DF_PER_ADV_CTE_NUM_MAX > 1)
710 	err = ull_adv_sync_add_cteinfo(lll_sync, pdu_prev, pdu, &cte_info, df_cfg->cte_count);
711 #else
712 	err = ull_adv_sync_add_cteinfo(lll_sync, pdu_prev, pdu, &cte_info, 1U);
713 #endif
714 
715 	if (err != BT_HCI_ERR_SUCCESS) {
716 		return err;
717 	}
718 
719 	*first_pdu = pdu;
720 
721 	return BT_HCI_ERR_SUCCESS;
722 }
723 
724 /*
725  * @brief Function removes content of cte_info field from periodic advertising PDUs.
726  *
727  * @param adv            Pointer to periodic advertising set.
728  * @param df_cfg         Pointer to direction finding configuration
729  * @param[out] ter_idx   Pointer used to return index of allocated or updated PDU.
730  *                       Index is required for scheduling the PDU for transmission in LLL.
731  * @param[out] first_pdu Pointer to return address of first PDU in a chain
732  *
733  * @return Zero in case of success, other value in case of failure.
734  */
cte_info_clear(struct ll_adv_set * adv,struct lll_df_adv_cfg * df_cfg,uint8_t * ter_idx,struct pdu_adv ** first_pdu)735 static uint8_t cte_info_clear(struct ll_adv_set *adv, struct lll_df_adv_cfg *df_cfg,
736 			      uint8_t *ter_idx, struct pdu_adv **first_pdu)
737 {
738 	void *extra_data_prev, *extra_data;
739 	struct pdu_adv *pdu_prev, *pdu;
740 	struct lll_adv_sync *lll_sync;
741 	uint8_t err;
742 
743 	lll_sync = adv->lll.sync;
744 
745 	/* NOTE: ULL_ADV_PDU_EXTRA_DATA_ALLOC_NEVER is just information that extra_data
746 	 * should be removed in case of this call ull_adv_sync_pdu_alloc.
747 	 */
748 	extra_data_prev = NULL;
749 	extra_data = NULL;
750 	err = ull_adv_sync_pdu_alloc(adv, ULL_ADV_PDU_EXTRA_DATA_ALLOC_NEVER, &pdu_prev, &pdu,
751 				     &extra_data_prev, &extra_data, ter_idx);
752 	if (err != BT_HCI_ERR_SUCCESS) {
753 		return err;
754 	}
755 
756 	if (extra_data_prev && extra_data) {
757 		ull_adv_sync_extra_data_set_clear(extra_data_prev, extra_data, 0,
758 						  ULL_ADV_PDU_HDR_FIELD_CTE_INFO, NULL);
759 	}
760 
761 	*first_pdu = pdu;
762 
763 	err = ull_adv_sync_remove_cteinfo(lll_sync, pdu_prev, pdu);
764 
765 	if (err != BT_HCI_ERR_SUCCESS) {
766 		/* TODO: return here leaves periodic advertising chain in an inconsistent state.
767 		 * Add graceful return or assert.
768 		 */
769 		return err;
770 	}
771 
772 	return BT_HCI_ERR_SUCCESS;
773 }
774 #endif /* CONFIG_BT_CTLR_DF_ADV_CTE_TX */
775 
776 #if defined(CONFIG_BT_CTLR_DF_CONN_CTE_TX)
777 /* @brief Function sets CTE transmission parameters for a connection.
778  *
779  * @param handle             Connection handle.
780  * @param cte_types          Bitfield holding information about
781  *                           allowed CTE types.
782  * @param switch_pattern_len Number of antenna ids in switch pattern.
783  * @param ant_id             Array of antenna identifiers.
784  *
785  * @return Status of command completion.
786  */
ll_df_set_conn_cte_tx_params(uint16_t handle,uint8_t cte_types,uint8_t switch_pattern_len,const uint8_t * ant_ids)787 uint8_t ll_df_set_conn_cte_tx_params(uint16_t handle, uint8_t cte_types, uint8_t switch_pattern_len,
788 				     const uint8_t *ant_ids)
789 {
790 	struct lll_df_conn_tx_cfg *df_tx_cfg;
791 	struct ll_conn *conn;
792 
793 	conn = ll_connected_get(handle);
794 	if (!conn) {
795 		return BT_HCI_ERR_UNKNOWN_CONN_ID;
796 	}
797 
798 	df_tx_cfg = &conn->lll.df_tx_cfg;
799 
800 	if (df_tx_cfg->cte_rsp_en) {
801 		return BT_HCI_ERR_CMD_DISALLOWED;
802 	}
803 
804 	/* Bits other than representing AoA, AoD 1us, AoD 2us are RFU */
805 	if (cte_types == 0U ||
806 	    ((cte_types & (~(uint8_t)(BT_HCI_LE_AOA_CTE_RSP | BT_HCI_LE_AOD_CTE_RSP_1US |
807 				      BT_HCI_LE_AOD_CTE_RSP_2US))) != 0U)) {
808 		return BT_HCI_ERR_UNSUPP_FEATURE_PARAM_VAL;
809 	}
810 
811 	if (!IS_ENABLED(CONFIG_BT_CTLR_DF_ANT_SWITCH_TX)) {
812 		if (cte_types & BT_HCI_LE_AOD_CTE_RSP_2US) {
813 			return BT_HCI_ERR_UNSUPP_FEATURE_PARAM_VAL;
814 		}
815 
816 		if ((cte_types & BT_HCI_LE_AOD_CTE_RSP_1US) &&
817 		    !IS_ENABLED(CONFIG_BT_CTLR_DF_ANT_SWITCH_1US)) {
818 			return BT_HCI_ERR_UNSUPP_FEATURE_PARAM_VAL;
819 		}
820 	}
821 
822 	/* Check antenna switching pattern only whether CTE TX in AoD mode is allowed */
823 	if (((cte_types & BT_HCI_LE_AOD_CTE_RSP_1US) || (cte_types & BT_HCI_LE_AOD_CTE_RSP_2US)) &&
824 	    (switch_pattern_len < BT_HCI_LE_SWITCH_PATTERN_LEN_MIN ||
825 	     switch_pattern_len > BT_CTLR_DF_MAX_ANT_SW_PATTERN_LEN || !ant_ids)) {
826 		return BT_HCI_ERR_UNSUPP_FEATURE_PARAM_VAL;
827 	}
828 
829 	(void)memcpy(df_tx_cfg->ant_ids, ant_ids, switch_pattern_len);
830 	df_tx_cfg->ant_sw_len = switch_pattern_len;
831 
832 	df_tx_cfg->cte_types_allowed = cte_types;
833 	df_tx_cfg->is_initialized = 1U;
834 
835 	return BT_HCI_ERR_SUCCESS;
836 }
837 #endif /* CONFIG_BT_CTLR_DF_CONN_CTE_TX */
838 
839 #if defined(CONFIG_BT_CTLR_DF_CONN_CTE_RX)
840 /**
841  * @brief Function sets CTE reception parameters for a connection.
842  *
843  * @note: The CTE may not be send/received with PHY CODED. The BT Core 5.3 specification does not
844  *        mention special handling of CTE receive and sampling while the functionality is enabled
845  *        for a connection that currently uses PHY CODED. Enable of CTE receive for a PHY CODED
846  *        will introduce complications for TISF maintenance by software switch. To avoid that
847  *        the lower link layer will enable the functionality when connection uses PHY UNCODED only.
848  *
849  * @param handle             Connection handle.
850  * @param sampling_enable    Enable or disable CTE RX. When the parameter is set to false,
851  *                           @p slot_durations, @p switch_pattern_len and @ant_ids are ignored.
852  * @param slot_durations     Switching and sampling slot durations for AoA mode.
853  * @param switch_pattern_len Number of antenna ids in switch pattern.
854  * @param ant_ids            Array of antenna identifiers.
855  *
856  * @return HCI status of command completion.
857  */
ll_df_set_conn_cte_rx_params(uint16_t handle,uint8_t sampling_enable,uint8_t slot_durations,uint8_t switch_pattern_len,const uint8_t * ant_ids)858 uint8_t ll_df_set_conn_cte_rx_params(uint16_t handle, uint8_t sampling_enable,
859 				     uint8_t slot_durations, uint8_t switch_pattern_len,
860 				     const uint8_t *ant_ids)
861 {
862 	struct lll_df_conn_rx_params *params_rx;
863 	struct dbuf_hdr *params_buf_hdr;
864 	struct lll_df_conn_rx_cfg *cfg_rx;
865 	struct ll_conn *conn;
866 	uint8_t params_idx;
867 
868 	conn = ll_connected_get(handle);
869 	if (!conn) {
870 		return BT_HCI_ERR_UNKNOWN_CONN_ID;
871 	}
872 
873 	cfg_rx = &conn->lll.df_rx_cfg;
874 	/* This is an information for HCI_LE_Connection_CTE_Request_Enable that
875 	 * HCI_LE_Set_Connection_CTE_Receive_Parameters was called at least once.
876 	 */
877 	cfg_rx->is_initialized = 1U;
878 	params_buf_hdr = &cfg_rx->hdr;
879 
880 	params_rx = dbuf_alloc(params_buf_hdr, &params_idx);
881 
882 	if (!sampling_enable) {
883 		params_rx->is_enabled = false;
884 	} else {
885 		/* According to Core 5.3 Vol 4, Part E, section 7.8.83 slot_durations,
886 		 * switch_pattern_len and ant_ids are used only for AoA and do not affect
887 		 * reception of AoD CTE. If AoA is not supported relax command validation
888 		 * to improve interoperability with different Host implementations.
889 		 */
890 		if (IS_ENABLED(CONFIG_BT_CTLR_DF_ANT_SWITCH_RX)) {
891 			if (!((IS_ENABLED(CONFIG_BT_CTLR_DF_ANT_SWITCH_1US) &&
892 			       slot_durations == BT_HCI_LE_ANTENNA_SWITCHING_SLOT_1US) ||
893 			      slot_durations == BT_HCI_LE_ANTENNA_SWITCHING_SLOT_2US)) {
894 				return BT_HCI_ERR_UNSUPP_FEATURE_PARAM_VAL;
895 			}
896 
897 			if (switch_pattern_len < BT_HCI_LE_SWITCH_PATTERN_LEN_MIN ||
898 			    switch_pattern_len > BT_CTLR_DF_MAX_ANT_SW_PATTERN_LEN || !ant_ids) {
899 				return BT_HCI_ERR_UNSUPP_FEATURE_PARAM_VAL;
900 			}
901 		}
902 
903 		params_rx->is_enabled = true;
904 		params_rx->slot_durations = slot_durations;
905 		(void)memcpy(params_rx->ant_ids, ant_ids, switch_pattern_len);
906 		params_rx->ant_sw_len = switch_pattern_len;
907 	}
908 
909 	dbuf_enqueue(params_buf_hdr, params_idx);
910 
911 	return BT_HCI_ERR_SUCCESS;
912 }
913 #endif /* CONFIG_BT_CTLR_DF_CONN_CTE_RX */
914 
915 #if defined(CONFIG_BT_CTLR_DF_CONN_CTE_RSP)
df_conn_cte_req_disable(void * param)916 static void df_conn_cte_req_disable(void *param)
917 {
918 	k_sem_give(param);
919 }
920 #endif /* CONFIG_BT_CTLR_DF_CONN_CTE_RSP */
921 
922 #if defined(CONFIG_BT_CTLR_DF_CONN_CTE_REQ)
923 /* @brief Function enables or disables CTE request control procedure for a connection.
924  *
925  * The procedure may be enabled in two modes:
926  * - single-shot, it is automatically disabled when the occurrence finishes.
927  * - periodic, it is executed periodically until disabled, connection is lost or PHY is changed
928  *   to the one that does not support CTE.
929  *
930  * @param handle               Connection handle.
931  * @param enable               Enable or disable CTE request. When the parameter is set to false
932  *                             @p cte_request_interval, @requested_cte_length and
933  *                             @p requested_cte_type are ignored.
934  * @param cte_request_interval Value zero enables single-shot mode. Other values enable periodic
935  *                             mode. In periodic mode, the value is a number of connection envets
936  *                             the procedure is executed. The value may not be lower than
937  *                             connection peer latency.
938  * @param requested_cte_length Minimum value of CTE length requested from peer.
939  * @param requested_cte_type   Type of CTE requested from peer.
940  *
941  * @return HCI Status of command completion.
942  */
ll_df_set_conn_cte_req_enable(uint16_t handle,uint8_t enable,uint16_t cte_request_interval,uint8_t requested_cte_length,uint8_t requested_cte_type)943 uint8_t ll_df_set_conn_cte_req_enable(uint16_t handle, uint8_t enable,
944 				      uint16_t cte_request_interval, uint8_t requested_cte_length,
945 				      uint8_t requested_cte_type)
946 {
947 	struct ll_conn *conn;
948 
949 	conn = ll_connected_get(handle);
950 	if (!conn) {
951 		return BT_HCI_ERR_UNKNOWN_CONN_ID;
952 	}
953 
954 	if (!enable) {
955 		ull_cp_cte_req_set_disable(conn);
956 
957 		return BT_HCI_ERR_SUCCESS;
958 	}
959 
960 	if (!conn->lll.df_rx_cfg.is_initialized) {
961 		return BT_HCI_ERR_CMD_DISALLOWED;
962 	}
963 
964 	if (conn->llcp.cte_req.is_enabled) {
965 		return BT_HCI_ERR_CMD_DISALLOWED;
966 	}
967 
968 #if defined(CONFIG_BT_CTLR_PHY)
969 	/* CTE request may be enabled only in case the receiver PHY is not CODED */
970 	if (conn->lll.phy_rx == PHY_CODED) {
971 		return BT_HCI_ERR_CMD_DISALLOWED;
972 	}
973 #endif /* CONFIG_BT_CTLR_PHY */
974 
975 	if (cte_request_interval != 0 && cte_request_interval < conn->lll.latency) {
976 		return BT_HCI_ERR_CMD_DISALLOWED;
977 	}
978 
979 	if (requested_cte_length < BT_HCI_LE_CTE_LEN_MIN ||
980 	    requested_cte_length > BT_HCI_LE_CTE_LEN_MAX) {
981 		return BT_HCI_ERR_UNSUPP_FEATURE_PARAM_VAL;
982 	}
983 
984 	if (requested_cte_type != BT_HCI_LE_AOA_CTE &&
985 	    requested_cte_type != BT_HCI_LE_AOD_CTE_1US &&
986 	    requested_cte_type != BT_HCI_LE_AOD_CTE_2US) {
987 		return BT_HCI_ERR_UNSUPP_FEATURE_PARAM_VAL;
988 	}
989 
990 	conn->llcp.cte_req.is_enabled = 1U;
991 	conn->llcp.cte_req.req_interval = cte_request_interval;
992 	conn->llcp.cte_req.cte_type = requested_cte_type;
993 	conn->llcp.cte_req.min_cte_len = requested_cte_length;
994 
995 	return ull_cp_cte_req(conn, requested_cte_length, requested_cte_type);
996 }
997 #endif /* CONFIG_BT_CTLR_DF_CONN_CTE_REQ */
998 
999 #if defined(CONFIG_BT_CTLR_DF_CONN_CTE_RSP)
1000 /**
1001  * @brief Function enables or disables CTE response control procedure for a connection.
1002  *
1003  * @param handle Connection handle.
1004  * @param enable Enable or disable CTE response.
1005  *
1006  * @return HCI Status of command completion.
1007  */
ll_df_set_conn_cte_rsp_enable(uint16_t handle,uint8_t enable)1008 uint8_t ll_df_set_conn_cte_rsp_enable(uint16_t handle, uint8_t enable)
1009 {
1010 	struct ll_conn *conn;
1011 
1012 	conn = ll_connected_get(handle);
1013 	if (!conn) {
1014 		return BT_HCI_ERR_UNKNOWN_CONN_ID;
1015 	}
1016 
1017 	if (enable) {
1018 		if (!conn->lll.df_tx_cfg.is_initialized) {
1019 			return BT_HCI_ERR_CMD_DISALLOWED;
1020 		}
1021 
1022 #if defined(CONFIG_BT_CTLR_PHY)
1023 		/* CTE may not be send over CODED PHY */
1024 		if (conn->lll.phy_tx == PHY_CODED) {
1025 			return BT_HCI_ERR_CMD_DISALLOWED;
1026 		}
1027 #endif /* CONFIG_BT_CTLR_PHY */
1028 		conn->lll.df_tx_cfg.cte_rsp_en = 1U;
1029 
1030 		ull_cp_cte_rsp_enable(conn, enable, LLL_DF_MAX_CTE_LEN,
1031 				conn->lll.df_tx_cfg.cte_types_allowed);
1032 	} else {
1033 		conn->lll.df_tx_cfg.cte_rsp_en = false;
1034 
1035 		if (conn->llcp.cte_rsp.is_active) {
1036 			struct k_sem sem;
1037 
1038 			k_sem_init(&sem, 0U, 1U);
1039 			conn->llcp.cte_rsp.disable_param = &sem;
1040 			conn->llcp.cte_rsp.disable_cb = df_conn_cte_req_disable;
1041 
1042 			if (!conn->llcp.cte_rsp.is_active) {
1043 				k_sem_take(&sem, K_FOREVER);
1044 			}
1045 		}
1046 	}
1047 
1048 	return BT_HCI_ERR_SUCCESS;
1049 }
1050 #endif /* CONFIG_BT_CTLR_DF_CONN_CTE_RSP */
1051 
1052 /* @brief Function provides information about Direction Finding
1053  *        antennas switching and sampling related settings.
1054  *
1055  * @param[out]switch_sample_rates       Pointer to store available antennas
1056  *                                      switch-sampling configurations.
1057  * @param[out]num_ant                   Pointer to store number of available
1058  *                                      antennas.
1059  * @param[out]max_switch_pattern_len    Pointer to store maximum number of
1060  *                                      antennas ids in switch pattern.
1061  * @param[out]max_cte_len               Pointer to store maximum length of CTE
1062  *                                      in [8us] units.
1063  */
ll_df_read_ant_inf(uint8_t * switch_sample_rates,uint8_t * num_ant,uint8_t * max_switch_pattern_len,uint8_t * max_cte_len)1064 void ll_df_read_ant_inf(uint8_t *switch_sample_rates, uint8_t *num_ant,
1065 			uint8_t *max_switch_pattern_len, uint8_t *max_cte_len)
1066 {
1067 	*switch_sample_rates = 0;
1068 	if (IS_ENABLED(CONFIG_BT_CTLR_DF_ANT_SWITCH_TX) &&
1069 	    IS_ENABLED(CONFIG_BT_CTLR_DF_ANT_SWITCH_1US)) {
1070 		*switch_sample_rates |= DF_AOD_1US_TX;
1071 	}
1072 
1073 	if (IS_ENABLED(CONFIG_BT_CTLR_DF_CTE_RX) &&
1074 	    IS_ENABLED(CONFIG_BT_CTLR_DF_CTE_RX_SAMPLE_1US)) {
1075 		*switch_sample_rates |= DF_AOD_1US_RX;
1076 	}
1077 
1078 	if (IS_ENABLED(CONFIG_BT_CTLR_DF_ANT_SWITCH_RX) &&
1079 	    IS_ENABLED(CONFIG_BT_CTLR_DF_CTE_RX_SAMPLE_1US)) {
1080 		*switch_sample_rates |= DF_AOA_1US;
1081 	}
1082 
1083 	*max_switch_pattern_len = BT_CTLR_DF_MAX_ANT_SW_PATTERN_LEN;
1084 	*num_ant = lll_df_ant_num_get();
1085 	*max_cte_len = LLL_DF_MAX_CTE_LEN;
1086 }
1087