1 /*
2  * Copyright (c) 2016-2018 Nordic Semiconductor ASA
3  * Copyright (c) 2016 Vinayak Kariappa Chettimada
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <stddef.h>
9 #include <string.h>
10 
11 #include <version.h>
12 #include <errno.h>
13 
14 #include <sys/util.h>
15 #include <sys/byteorder.h>
16 #include <sys/atomic.h>
17 
18 #include <drivers/bluetooth/hci_driver.h>
19 
20 #include <bluetooth/hci.h>
21 #include <bluetooth/hci_vs.h>
22 #include <bluetooth/buf.h>
23 #include <bluetooth/bluetooth.h>
24 
25 #include "../host/hci_ecc.h"
26 
27 #include "util/util.h"
28 #include "util/memq.h"
29 #include "util/mem.h"
30 
31 #include "hal/ecb.h"
32 #include "hal/ccm.h"
33 
34 #include "ll_sw/pdu.h"
35 
36 #include "ll_sw/lll.h"
37 #include "lll/lll_adv_types.h"
38 #include "ll_sw/lll_adv.h"
39 #include "lll/lll_adv_pdu.h"
40 #include "ll_sw/lll_sync_iso.h"
41 #include "ll_sw/lll_scan.h"
42 #include "lll/lll_df_types.h"
43 #include "ll_sw/lll_sync.h"
44 #include "ll_sw/lll_conn.h"
45 #include "ll_sw/lll_conn_iso.h"
46 
47 #include "ll_sw/ull_adv_types.h"
48 #include "ll_sw/ull_scan_types.h"
49 #include "ll_sw/ull_sync_types.h"
50 #include "ll_sw/ull_sync_internal.h"
51 #include "ll_sw/ull_conn_types.h"
52 #include "ll_sw/ull_conn_internal.h"
53 #include "ll_sw/ull_conn_iso_types.h"
54 #include "ll_sw/ull_df_types.h"
55 #include "ll_sw/ull_df_internal.h"
56 
57 #include "ll.h"
58 #include "ll_feat.h"
59 #include "ll_settings.h"
60 #include "hci_internal.h"
61 #include "hci_vendor.h"
62 
63 #if defined(CONFIG_BT_HCI_MESH_EXT)
64 #include "ll_sw/ll_mesh.h"
65 #endif /* CONFIG_BT_HCI_MESH_EXT */
66 
67 #if defined(CONFIG_BT_CTLR_DTM_HCI)
68 #include "ll_sw/ll_test.h"
69 #endif /* CONFIG_BT_CTLR_DTM_HCI */
70 
71 #if defined(CONFIG_BT_CTLR_USER_EXT)
72 #include "hci_user_ext.h"
73 #endif /* CONFIG_BT_CTLR_USER_EXT */
74 
75 #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_DEBUG_HCI_DRIVER)
76 #define LOG_MODULE_NAME bt_ctlr_hci
77 #include "common/log.h"
78 #include "hal/debug.h"
79 
80 /* opcode of the HCI command currently being processed. The opcode is stored
81  * by hci_cmd_handle() and then used during the creation of cmd complete and
82  * cmd status events to avoid passing it up the call chain.
83  */
84 static uint16_t _opcode;
85 
86 #if CONFIG_BT_CTLR_DUP_FILTER_LEN > 0
87 /* Scan duplicate filter */
88 struct dup {
89 	uint8_t      mask;
90 	bt_addr_le_t addr;
91 
92 #if defined(CONFIG_BT_CTLR_ADV_EXT)
93 	uint8_t            adv_mode:2;
94 	uint8_t            data_cmplt:1;
95 	struct pdu_adv_adi adi;
96 #endif
97 };
98 static struct dup dup_filter[CONFIG_BT_CTLR_DUP_FILTER_LEN];
99 static int32_t dup_count;
100 static uint32_t dup_curr;
101 #endif
102 
103 #if defined(CONFIG_BT_HCI_MESH_EXT)
104 struct scan_filter {
105 	uint8_t count;
106 	uint8_t lengths[CONFIG_BT_CTLR_MESH_SF_PATTERNS];
107 	uint8_t patterns[CONFIG_BT_CTLR_MESH_SF_PATTERNS]
108 		     [BT_HCI_MESH_PATTERN_LEN_MAX];
109 };
110 
111 static struct scan_filter scan_filters[CONFIG_BT_CTLR_MESH_SCAN_FILTERS];
112 static uint8_t sf_curr;
113 #endif
114 
115 #if defined(CONFIG_BT_HCI_ACL_FLOW_CONTROL)
116 int32_t    hci_hbuf_total;
117 uint32_t    hci_hbuf_sent;
118 uint32_t    hci_hbuf_acked;
119 uint16_t    hci_hbuf_pend[CONFIG_BT_MAX_CONN];
120 atomic_t hci_state_mask;
121 static struct k_poll_signal *hbuf_signal;
122 #endif
123 
124 #if defined(CONFIG_BT_CONN)
125 static uint32_t conn_count;
126 #endif
127 
128 #if defined(CONFIG_BT_CTLR_CENTRAL_ISO)
129 static uint32_t cis_pending_count;
130 #endif
131 
132 #if !defined(CONFIG_BT_HCI_RAW) && defined(CONFIG_BT_BUF_EVT_DISCARDABLE_COUNT)
133 #define ADV_REPORT_EVT_MAX_LEN CONFIG_BT_BUF_EVT_DISCARDABLE_SIZE
134 #else
135 #define ADV_REPORT_EVT_MAX_LEN CONFIG_BT_BUF_EVT_RX_SIZE
136 #endif
137 
138 #define DEFAULT_EVENT_MASK           0x1fffffffffff
139 #define DEFAULT_EVENT_MASK_PAGE_2    0x0
140 #define DEFAULT_LE_EVENT_MASK 0x1f
141 
142 static uint64_t event_mask = DEFAULT_EVENT_MASK;
143 static uint64_t event_mask_page_2 = DEFAULT_EVENT_MASK_PAGE_2;
144 static uint64_t le_event_mask = DEFAULT_LE_EVENT_MASK;
145 
146 static struct net_buf *cmd_complete_status(uint8_t status);
147 
148 #if defined(CONFIG_BT_CTLR_ADV_EXT)
adv_cmds_legacy_check(struct net_buf ** cc_evt)149 static int adv_cmds_legacy_check(struct net_buf **cc_evt)
150 {
151 	int err;
152 
153 #if defined(CONFIG_BT_HCI_RAW)
154 	err = ll_adv_cmds_set(LL_ADV_CMDS_LEGACY);
155 	if (err && cc_evt) {
156 		*cc_evt = cmd_complete_status(BT_HCI_ERR_CMD_DISALLOWED);
157 	}
158 #else
159 	if (cc_evt) {
160 		*cc_evt = cmd_complete_status(BT_HCI_ERR_CMD_DISALLOWED);
161 	}
162 
163 	err = -EINVAL;
164 #endif /* CONFIG_BT_HCI_RAW */
165 
166 	return err;
167 }
168 
adv_cmds_ext_check(struct net_buf ** cc_evt)169 static int adv_cmds_ext_check(struct net_buf **cc_evt)
170 {
171 	int err;
172 
173 #if defined(CONFIG_BT_HCI_RAW)
174 	err = ll_adv_cmds_set(LL_ADV_CMDS_EXT);
175 	if (err && cc_evt) {
176 		*cc_evt = cmd_complete_status(BT_HCI_ERR_CMD_DISALLOWED);
177 	}
178 #else
179 	err = 0;
180 #endif /* CONFIG_BT_HCI_RAW */
181 
182 	return err;
183 }
184 #else
adv_cmds_legacy_check(struct net_buf ** cc_evt)185 static inline int adv_cmds_legacy_check(struct net_buf **cc_evt)
186 {
187 	return 0;
188 }
189 #endif /* CONFIG_BT_CTLR_ADV_EXT */
190 
191 #if defined(CONFIG_BT_CONN)
192 static void le_conn_complete(struct pdu_data *pdu_data, uint16_t handle,
193 			     struct net_buf *buf);
194 #endif /* CONFIG_BT_CONN */
195 
hci_evt_create(struct net_buf * buf,uint8_t evt,uint8_t len)196 static void hci_evt_create(struct net_buf *buf, uint8_t evt, uint8_t len)
197 {
198 	struct bt_hci_evt_hdr *hdr;
199 
200 	hdr = net_buf_add(buf, sizeof(*hdr));
201 	hdr->evt = evt;
202 	hdr->len = len;
203 }
204 
hci_cmd_complete(struct net_buf ** buf,uint8_t plen)205 void *hci_cmd_complete(struct net_buf **buf, uint8_t plen)
206 {
207 	*buf = bt_hci_cmd_complete_create(_opcode, plen);
208 
209 	return net_buf_add(*buf, plen);
210 }
211 
cmd_status(uint8_t status)212 static struct net_buf *cmd_status(uint8_t status)
213 {
214 	return bt_hci_cmd_status_create(_opcode, status);
215 }
216 
cmd_complete_status(uint8_t status)217 static struct net_buf *cmd_complete_status(uint8_t status)
218 {
219 	struct net_buf *buf;
220 	struct bt_hci_evt_cc_status *ccst;
221 
222 	buf = bt_hci_cmd_complete_create(_opcode, sizeof(*ccst));
223 	ccst = net_buf_add(buf, sizeof(*ccst));
224 	ccst->status = status;
225 
226 	return buf;
227 }
228 
meta_evt(struct net_buf * buf,uint8_t subevt,uint8_t melen)229 static void *meta_evt(struct net_buf *buf, uint8_t subevt, uint8_t melen)
230 {
231 	struct bt_hci_evt_le_meta_event *me;
232 
233 	hci_evt_create(buf, BT_HCI_EVT_LE_META_EVENT, sizeof(*me) + melen);
234 	me = net_buf_add(buf, sizeof(*me));
235 	me->subevent = subevt;
236 
237 	return net_buf_add(buf, melen);
238 }
239 
240 #if defined(CONFIG_BT_HCI_MESH_EXT)
mesh_evt(struct net_buf * buf,uint8_t subevt,uint8_t melen)241 static void *mesh_evt(struct net_buf *buf, uint8_t subevt, uint8_t melen)
242 {
243 	struct bt_hci_evt_mesh *me;
244 
245 	hci_evt_create(buf, BT_HCI_EVT_VENDOR, sizeof(*me) + melen);
246 	me = net_buf_add(buf, sizeof(*me));
247 	me->prefix = BT_HCI_MESH_EVT_PREFIX;
248 	me->subevent = subevt;
249 
250 	return net_buf_add(buf, melen);
251 }
252 #endif /* CONFIG_BT_HCI_MESH_EXT */
253 
254 #if defined(CONFIG_BT_CONN)
disconnect(struct net_buf * buf,struct net_buf ** evt)255 static void disconnect(struct net_buf *buf, struct net_buf **evt)
256 {
257 	struct bt_hci_cp_disconnect *cmd = (void *)buf->data;
258 	uint16_t handle;
259 	uint8_t status;
260 
261 	handle = sys_le16_to_cpu(cmd->handle);
262 	status = ll_terminate_ind_send(handle, cmd->reason);
263 
264 	*evt = cmd_status(status);
265 }
266 
read_remote_ver_info(struct net_buf * buf,struct net_buf ** evt)267 static void read_remote_ver_info(struct net_buf *buf, struct net_buf **evt)
268 {
269 	struct bt_hci_cp_read_remote_version_info *cmd = (void *)buf->data;
270 	uint16_t handle;
271 	uint8_t status;
272 
273 	handle = sys_le16_to_cpu(cmd->handle);
274 	status = ll_version_ind_send(handle);
275 
276 	*evt = cmd_status(status);
277 }
278 #endif /* CONFIG_BT_CONN */
279 
link_control_cmd_handle(uint16_t ocf,struct net_buf * cmd,struct net_buf ** evt)280 static int link_control_cmd_handle(uint16_t  ocf, struct net_buf *cmd,
281 				   struct net_buf **evt)
282 {
283 	switch (ocf) {
284 #if defined(CONFIG_BT_CONN)
285 	case BT_OCF(BT_HCI_OP_DISCONNECT):
286 		disconnect(cmd, evt);
287 		break;
288 	case BT_OCF(BT_HCI_OP_READ_REMOTE_VERSION_INFO):
289 		read_remote_ver_info(cmd, evt);
290 		break;
291 #endif /* CONFIG_BT_CONN */
292 	default:
293 		return -EINVAL;
294 	}
295 
296 	return 0;
297 }
298 
set_event_mask(struct net_buf * buf,struct net_buf ** evt)299 static void set_event_mask(struct net_buf *buf, struct net_buf **evt)
300 {
301 	struct bt_hci_cp_set_event_mask *cmd = (void *)buf->data;
302 
303 	event_mask = sys_get_le64(cmd->events);
304 
305 	*evt = cmd_complete_status(0x00);
306 }
307 
set_event_mask_page_2(struct net_buf * buf,struct net_buf ** evt)308 static void set_event_mask_page_2(struct net_buf *buf, struct net_buf **evt)
309 {
310 	struct bt_hci_cp_set_event_mask_page_2 *cmd = (void *)buf->data;
311 
312 	event_mask_page_2 = sys_get_le64(cmd->events_page_2);
313 
314 	*evt = cmd_complete_status(0x00);
315 }
316 
reset(struct net_buf * buf,struct net_buf ** evt)317 static void reset(struct net_buf *buf, struct net_buf **evt)
318 {
319 #if defined(CONFIG_BT_HCI_MESH_EXT)
320 	int i;
321 
322 	for (i = 0; i < ARRAY_SIZE(scan_filters); i++) {
323 		scan_filters[i].count = 0U;
324 	}
325 	sf_curr = 0xFF;
326 #endif
327 
328 #if CONFIG_BT_CTLR_DUP_FILTER_LEN > 0
329 	dup_count = -1;
330 #endif
331 
332 	/* reset event masks */
333 	event_mask = DEFAULT_EVENT_MASK;
334 	event_mask_page_2 = DEFAULT_EVENT_MASK_PAGE_2;
335 	le_event_mask = DEFAULT_LE_EVENT_MASK;
336 
337 	if (buf) {
338 		ll_reset();
339 		*evt = cmd_complete_status(0x00);
340 	}
341 
342 #if defined(CONFIG_BT_CONN)
343 	conn_count = 0U;
344 #endif
345 
346 #if defined(CONFIG_BT_CTLR_CENTRAL_ISO)
347 	cis_pending_count = 0U;
348 #endif
349 
350 #if defined(CONFIG_BT_HCI_ACL_FLOW_CONTROL)
351 	hci_hbuf_total = 0;
352 	hci_hbuf_sent = 0U;
353 	hci_hbuf_acked = 0U;
354 	(void)memset(hci_hbuf_pend, 0, sizeof(hci_hbuf_pend));
355 	if (buf) {
356 		atomic_set_bit(&hci_state_mask, HCI_STATE_BIT_RESET);
357 		k_poll_signal_raise(hbuf_signal, 0x0);
358 	}
359 #endif
360 }
361 
362 #if defined(CONFIG_BT_HCI_ACL_FLOW_CONTROL)
set_ctl_to_host_flow(struct net_buf * buf,struct net_buf ** evt)363 static void set_ctl_to_host_flow(struct net_buf *buf, struct net_buf **evt)
364 {
365 	struct bt_hci_cp_set_ctl_to_host_flow *cmd = (void *)buf->data;
366 	uint8_t flow_enable = cmd->flow_enable;
367 	struct bt_hci_evt_cc_status *ccst;
368 
369 	ccst = hci_cmd_complete(evt, sizeof(*ccst));
370 
371 	/* require host buffer size before enabling flow control, and
372 	 * disallow if any connections are up
373 	 */
374 	if (!hci_hbuf_total || conn_count) {
375 		ccst->status = BT_HCI_ERR_CMD_DISALLOWED;
376 		return;
377 	} else {
378 		ccst->status = 0x00;
379 	}
380 
381 	switch (flow_enable) {
382 	case BT_HCI_CTL_TO_HOST_FLOW_DISABLE:
383 		if (hci_hbuf_total < 0) {
384 			/* already disabled */
385 			return;
386 		}
387 		break;
388 	case BT_HCI_CTL_TO_HOST_FLOW_ENABLE:
389 		if (hci_hbuf_total > 0) {
390 			/* already enabled */
391 			return;
392 		}
393 		break;
394 	default:
395 		ccst->status = BT_HCI_ERR_INVALID_PARAM;
396 		return;
397 	}
398 
399 	hci_hbuf_sent = 0U;
400 	hci_hbuf_acked = 0U;
401 	(void)memset(hci_hbuf_pend, 0, sizeof(hci_hbuf_pend));
402 	hci_hbuf_total = -hci_hbuf_total;
403 }
404 
host_buffer_size(struct net_buf * buf,struct net_buf ** evt)405 static void host_buffer_size(struct net_buf *buf, struct net_buf **evt)
406 {
407 	struct bt_hci_cp_host_buffer_size *cmd = (void *)buf->data;
408 	uint16_t acl_pkts = sys_le16_to_cpu(cmd->acl_pkts);
409 	uint16_t acl_mtu = sys_le16_to_cpu(cmd->acl_mtu);
410 	struct bt_hci_evt_cc_status *ccst;
411 
412 	ccst = hci_cmd_complete(evt, sizeof(*ccst));
413 
414 	if (hci_hbuf_total) {
415 		ccst->status = BT_HCI_ERR_CMD_DISALLOWED;
416 		return;
417 	}
418 	/* fragmentation from controller to host not supported, require
419 	 * ACL MTU to be at least the LL MTU
420 	 */
421 	if (acl_mtu < LL_LENGTH_OCTETS_RX_MAX) {
422 		ccst->status = BT_HCI_ERR_INVALID_PARAM;
423 		return;
424 	}
425 
426 	BT_DBG("FC: host buf size: %d", acl_pkts);
427 	hci_hbuf_total = -acl_pkts;
428 }
429 
host_num_completed_packets(struct net_buf * buf,struct net_buf ** evt)430 static void host_num_completed_packets(struct net_buf *buf,
431 				       struct net_buf **evt)
432 {
433 	struct bt_hci_cp_host_num_completed_packets *cmd = (void *)buf->data;
434 	struct bt_hci_evt_cc_status *ccst;
435 	uint32_t count = 0U;
436 	int i;
437 
438 	/* special case, no event returned except for error conditions */
439 	if (hci_hbuf_total <= 0) {
440 		ccst = hci_cmd_complete(evt, sizeof(*ccst));
441 		ccst->status = BT_HCI_ERR_CMD_DISALLOWED;
442 		return;
443 	} else if (!conn_count) {
444 		ccst = hci_cmd_complete(evt, sizeof(*ccst));
445 		ccst->status = BT_HCI_ERR_INVALID_PARAM;
446 		return;
447 	}
448 
449 	/* leave *evt == NULL so no event is generated */
450 	for (i = 0; i < cmd->num_handles; i++) {
451 		uint16_t h = sys_le16_to_cpu(cmd->h[i].handle);
452 		uint16_t c = sys_le16_to_cpu(cmd->h[i].count);
453 
454 		if ((h >= ARRAY_SIZE(hci_hbuf_pend)) ||
455 		    (c > hci_hbuf_pend[h])) {
456 			ccst = hci_cmd_complete(evt, sizeof(*ccst));
457 			ccst->status = BT_HCI_ERR_INVALID_PARAM;
458 			return;
459 		}
460 
461 		hci_hbuf_pend[h] -= c;
462 		count += c;
463 	}
464 
465 	BT_DBG("FC: acked: %d", count);
466 	hci_hbuf_acked += count;
467 	k_poll_signal_raise(hbuf_signal, 0x0);
468 }
469 #endif
470 
471 #if defined(CONFIG_BT_CTLR_LE_PING)
read_auth_payload_timeout(struct net_buf * buf,struct net_buf ** evt)472 static void read_auth_payload_timeout(struct net_buf *buf, struct net_buf **evt)
473 {
474 	struct bt_hci_cp_read_auth_payload_timeout *cmd = (void *)buf->data;
475 	struct bt_hci_rp_read_auth_payload_timeout *rp;
476 	uint16_t auth_payload_timeout;
477 	uint16_t handle;
478 	uint8_t status;
479 
480 	handle = sys_le16_to_cpu(cmd->handle);
481 
482 	status = ll_apto_get(handle, &auth_payload_timeout);
483 
484 	rp = hci_cmd_complete(evt, sizeof(*rp));
485 	rp->status = status;
486 	rp->handle = sys_cpu_to_le16(handle);
487 	rp->auth_payload_timeout = sys_cpu_to_le16(auth_payload_timeout);
488 }
489 
write_auth_payload_timeout(struct net_buf * buf,struct net_buf ** evt)490 static void write_auth_payload_timeout(struct net_buf *buf,
491 				       struct net_buf **evt)
492 {
493 	struct bt_hci_cp_write_auth_payload_timeout *cmd = (void *)buf->data;
494 	struct bt_hci_rp_write_auth_payload_timeout *rp;
495 	uint16_t auth_payload_timeout;
496 	uint16_t handle;
497 	uint8_t status;
498 
499 	handle = sys_le16_to_cpu(cmd->handle);
500 	auth_payload_timeout = sys_le16_to_cpu(cmd->auth_payload_timeout);
501 
502 	status = ll_apto_set(handle, auth_payload_timeout);
503 
504 	rp = hci_cmd_complete(evt, sizeof(*rp));
505 	rp->status = status;
506 	rp->handle = sys_cpu_to_le16(handle);
507 }
508 #endif /* CONFIG_BT_CTLR_LE_PING */
509 
510 #if defined(CONFIG_BT_CTLR_CONN_ISO)
configure_data_path(struct net_buf * buf,struct net_buf ** evt)511 static void configure_data_path(struct net_buf *buf,
512 				struct net_buf **evt)
513 {
514 	struct bt_hci_cp_configure_data_path *cmd = (void *)buf->data;
515 	struct bt_hci_rp_configure_data_path *rp;
516 
517 	uint8_t *vs_config;
518 	uint8_t status;
519 
520 	vs_config = &cmd->vs_config[0];
521 
522 	status = ll_configure_data_path(cmd->data_path_dir,
523 					cmd->data_path_id,
524 					cmd->vs_config_len,
525 					vs_config);
526 
527 	rp = hci_cmd_complete(evt, sizeof(*rp));
528 	rp->status = status;
529 }
530 #endif /* CONFIG_BT_CTLR_CONN_ISO */
531 
532 #if defined(CONFIG_BT_CONN)
read_tx_power_level(struct net_buf * buf,struct net_buf ** evt)533 static void read_tx_power_level(struct net_buf *buf, struct net_buf **evt)
534 {
535 	struct bt_hci_cp_read_tx_power_level *cmd = (void *)buf->data;
536 	struct bt_hci_rp_read_tx_power_level *rp;
537 	uint16_t handle;
538 	uint8_t status;
539 	uint8_t type;
540 
541 	handle = sys_le16_to_cpu(cmd->handle);
542 	type = cmd->type;
543 
544 	rp = hci_cmd_complete(evt, sizeof(*rp));
545 
546 	status = ll_tx_pwr_lvl_get(BT_HCI_VS_LL_HANDLE_TYPE_CONN,
547 				   handle, type, &rp->tx_power_level);
548 
549 	rp->status = status;
550 	rp->handle = sys_cpu_to_le16(handle);
551 }
552 #endif /* CONFIG_BT_CONN */
553 
ctrl_bb_cmd_handle(uint16_t ocf,struct net_buf * cmd,struct net_buf ** evt)554 static int ctrl_bb_cmd_handle(uint16_t  ocf, struct net_buf *cmd,
555 			      struct net_buf **evt)
556 {
557 	switch (ocf) {
558 	case BT_OCF(BT_HCI_OP_SET_EVENT_MASK):
559 		set_event_mask(cmd, evt);
560 		break;
561 
562 	case BT_OCF(BT_HCI_OP_RESET):
563 		reset(cmd, evt);
564 		break;
565 
566 	case BT_OCF(BT_HCI_OP_SET_EVENT_MASK_PAGE_2):
567 		set_event_mask_page_2(cmd, evt);
568 		break;
569 
570 #if defined(CONFIG_BT_CONN)
571 	case BT_OCF(BT_HCI_OP_READ_TX_POWER_LEVEL):
572 		read_tx_power_level(cmd, evt);
573 		break;
574 #endif /* CONFIG_BT_CONN */
575 
576 #if defined(CONFIG_BT_HCI_ACL_FLOW_CONTROL)
577 	case BT_OCF(BT_HCI_OP_SET_CTL_TO_HOST_FLOW):
578 		set_ctl_to_host_flow(cmd, evt);
579 		break;
580 
581 	case BT_OCF(BT_HCI_OP_HOST_BUFFER_SIZE):
582 		host_buffer_size(cmd, evt);
583 		break;
584 
585 	case BT_OCF(BT_HCI_OP_HOST_NUM_COMPLETED_PACKETS):
586 		host_num_completed_packets(cmd, evt);
587 		break;
588 #endif
589 
590 #if defined(CONFIG_BT_CTLR_LE_PING)
591 	case BT_OCF(BT_HCI_OP_READ_AUTH_PAYLOAD_TIMEOUT):
592 		read_auth_payload_timeout(cmd, evt);
593 		break;
594 
595 	case BT_OCF(BT_HCI_OP_WRITE_AUTH_PAYLOAD_TIMEOUT):
596 		write_auth_payload_timeout(cmd, evt);
597 		break;
598 #endif /* CONFIG_BT_CTLR_LE_PING */
599 
600 #if defined(CONFIG_BT_CTLR_CONN_ISO)
601 	case BT_OCF(BT_HCI_OP_CONFIGURE_DATA_PATH):
602 		configure_data_path(cmd, evt);
603 		break;
604 #endif /* CONFIG_BT_CTLR_CONN_ISO */
605 
606 	default:
607 		return -EINVAL;
608 	}
609 
610 	return 0;
611 }
612 
read_local_version_info(struct net_buf * buf,struct net_buf ** evt)613 static void read_local_version_info(struct net_buf *buf, struct net_buf **evt)
614 {
615 	struct bt_hci_rp_read_local_version_info *rp;
616 
617 	rp = hci_cmd_complete(evt, sizeof(*rp));
618 
619 	rp->status = 0x00;
620 	rp->hci_version = LL_VERSION_NUMBER;
621 	rp->hci_revision = sys_cpu_to_le16(0);
622 	rp->lmp_version = LL_VERSION_NUMBER;
623 	rp->manufacturer = sys_cpu_to_le16(ll_settings_company_id());
624 	rp->lmp_subversion = sys_cpu_to_le16(ll_settings_subversion_number());
625 }
626 
read_supported_commands(struct net_buf * buf,struct net_buf ** evt)627 static void read_supported_commands(struct net_buf *buf, struct net_buf **evt)
628 {
629 	struct bt_hci_rp_read_supported_commands *rp;
630 
631 	rp = hci_cmd_complete(evt, sizeof(*rp));
632 
633 	rp->status = 0x00;
634 	(void)memset(&rp->commands[0], 0, sizeof(rp->commands));
635 
636 #if defined(CONFIG_BT_REMOTE_VERSION)
637 	/* Read Remote Version Info. */
638 	rp->commands[2] |= BIT(7);
639 #endif
640 	/* Set Event Mask, and Reset. */
641 	rp->commands[5] |= BIT(6) | BIT(7);
642 	/* Read TX Power Level. */
643 	rp->commands[10] |= BIT(2);
644 
645 #if defined(CONFIG_BT_HCI_ACL_FLOW_CONTROL)
646 	/* Set FC, Host Buffer Size and Host Num Completed */
647 	rp->commands[10] |= BIT(5) | BIT(6) | BIT(7);
648 #endif /* CONFIG_BT_HCI_ACL_FLOW_CONTROL */
649 
650 	/* Read Local Version Info, Read Local Supported Features. */
651 	rp->commands[14] |= BIT(3) | BIT(5);
652 	/* Read BD ADDR. */
653 	rp->commands[15] |= BIT(1);
654 
655 #if defined(CONFIG_BT_CTLR_CONN_RSSI)
656 	/* Read RSSI. */
657 	rp->commands[15] |= BIT(5);
658 #endif /* CONFIG_BT_CTLR_CONN_RSSI */
659 
660 	/* Set Event Mask Page 2 */
661 	rp->commands[22] |= BIT(2);
662 	/* LE Set Event Mask, LE Read Buffer Size, LE Read Local Supp Feats,
663 	 * Set Random Addr
664 	 */
665 	rp->commands[25] |= BIT(0) | BIT(1) | BIT(2) | BIT(4);
666 
667 #if defined(CONFIG_BT_CTLR_FILTER_ACCEPT_LIST)
668 	/* LE Read FAL Size, LE Clear FAL */
669 	rp->commands[26] |= BIT(6) | BIT(7);
670 	/* LE Add Dev to FAL, LE Remove Dev from FAL */
671 	rp->commands[27] |= BIT(0) | BIT(1);
672 #endif /* CONFIG_BT_CTLR_FILTER_ACCEPT_LIST */
673 
674 	/* LE Encrypt, LE Rand */
675 	rp->commands[27] |= BIT(6) | BIT(7);
676 	/* LE Read Supported States */
677 	rp->commands[28] |= BIT(3);
678 
679 #if defined(CONFIG_BT_BROADCASTER)
680 	/* LE Set Adv Params, LE Read Adv Channel TX Power, LE Set Adv Data */
681 	rp->commands[25] |= BIT(5) | BIT(6) | BIT(7);
682 	/* LE Set Scan Response Data, LE Set Adv Enable */
683 	rp->commands[26] |= BIT(0) | BIT(1);
684 
685 #if defined(CONFIG_BT_CTLR_ADV_EXT)
686 	/* LE Set Adv Set Random Addr, LE Set Ext Adv Params, LE Set Ext Adv
687 	 * Data, LE Set Ext Adv Scan Rsp Data, LE Set Ext Adv Enable, LE Read
688 	 * Max Adv Data Len, LE Read Num Supp Adv Sets
689 	 */
690 	rp->commands[36] |= BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(5) |
691 			    BIT(6) | BIT(7);
692 	/* LE Remove Adv Set, LE Clear Adv Sets */
693 	rp->commands[37] |= BIT(0) | BIT(1);
694 #if defined(CONFIG_BT_CTLR_ADV_PERIODIC)
695 	/* LE Set PA Params, LE Set PA Data, LE Set PA Enable */
696 	rp->commands[37] |= BIT(2) | BIT(3) | BIT(4);
697 #endif /* CONFIG_BT_CTLR_ADV_PERIODIC */
698 #endif /* CONFIG_BT_CTLR_ADV_EXT */
699 #endif /* CONFIG_BT_BROADCASTER */
700 
701 #if defined(CONFIG_BT_OBSERVER)
702 	/* LE Set Scan Params, LE Set Scan Enable */
703 	rp->commands[26] |= BIT(2) | BIT(3);
704 
705 #if defined(CONFIG_BT_CTLR_ADV_EXT)
706 	/* LE Set Extended Scan Params, LE Set Extended Scan Enable */
707 	rp->commands[37] |= BIT(5) | BIT(6);
708 #if defined(CONFIG_BT_CTLR_SYNC_PERIODIC)
709 	/* LE PA Create Sync, LE PA Create Sync Cancel, LE PA Terminate Sync */
710 	rp->commands[38] |= BIT(0) | BIT(1) | BIT(2);
711 	/* LE Set PA Receive Enable */
712 	rp->commands[40] |= BIT(5);
713 #endif /* CONFIG_BT_CTLR_SYNC_PERIODIC */
714 #endif /* CONFIG_BT_CTLR_ADV_EXT */
715 
716 #endif /* CONFIG_BT_OBSERVER */
717 
718 #if defined(CONFIG_BT_CONN)
719 #if defined(CONFIG_BT_CENTRAL)
720 	/* LE Create Connection, LE Create Connection Cancel */
721 	rp->commands[26] |= BIT(4) | BIT(5);
722 	/* Set Host Channel Classification */
723 	rp->commands[27] |= BIT(3);
724 
725 #if defined(CONFIG_BT_CTLR_ADV_EXT)
726 	/* LE Extended Create Connection */
727 	rp->commands[37] |= BIT(7);
728 #endif /* CONFIG_BT_CTLR_ADV_EXT */
729 
730 #if defined(CONFIG_BT_CTLR_LE_ENC)
731 	/* LE Start Encryption */
732 	rp->commands[28] |= BIT(0);
733 #endif /* CONFIG_BT_CTLR_LE_ENC */
734 #endif /* CONFIG_BT_CENTRAL */
735 
736 #if defined(CONFIG_BT_PERIPHERAL)
737 #if defined(CONFIG_BT_CTLR_LE_ENC)
738 	/* LE LTK Request Reply, LE LTK Request Negative Reply */
739 	rp->commands[28] |= BIT(1) | BIT(2);
740 #endif /* CONFIG_BT_CTLR_LE_ENC */
741 #endif /* CONFIG_BT_PERIPHERAL */
742 
743 	/* Disconnect. */
744 	rp->commands[0] |= BIT(5);
745 	/* LE Connection Update, LE Read Channel Map, LE Read Remote Features */
746 	rp->commands[27] |= BIT(2) | BIT(4) | BIT(5);
747 
748 #if defined(CONFIG_BT_CTLR_CONN_PARAM_REQ)
749 	/* LE Remote Conn Param Req and Neg Reply */
750 	rp->commands[33] |= BIT(4) | BIT(5);
751 #endif /* CONFIG_BT_CTLR_CONN_PARAM_REQ */
752 
753 #if defined(CONFIG_BT_CTLR_LE_PING)
754 	/* Read and Write authenticated payload timeout */
755 	rp->commands[32] |= BIT(4) | BIT(5);
756 #endif /* CONFIG_BT_CTLR_LE_PING */
757 
758 #if defined(CONFIG_BT_CTLR_DATA_LENGTH)
759 	/* LE Set Data Length, and LE Read Suggested Data Length. */
760 	rp->commands[33] |= BIT(6) | BIT(7);
761 	/* LE Write Suggested Data Length. */
762 	rp->commands[34] |= BIT(0);
763 	/* LE Read Maximum Data Length. */
764 	rp->commands[35] |= BIT(3);
765 #endif /* CONFIG_BT_CTLR_DATA_LENGTH */
766 
767 #if defined(CONFIG_BT_CTLR_PHY)
768 	/* LE Read PHY Command. */
769 	rp->commands[35] |= BIT(4);
770 	/* LE Set Default PHY Command. */
771 	rp->commands[35] |= BIT(5);
772 	/* LE Set PHY Command. */
773 	rp->commands[35] |= BIT(6);
774 #endif /* CONFIG_BT_CTLR_PHY */
775 #endif /* CONFIG_BT_CONN */
776 
777 #if defined(CONFIG_BT_CTLR_DTM_HCI)
778 	/* LE RX Test, LE TX Test, LE Test End */
779 	rp->commands[28] |= BIT(4) | BIT(5) | BIT(6);
780 	/* LE Enhanced RX Test. */
781 	rp->commands[35] |= BIT(7);
782 	/* LE Enhanced TX Test. */
783 	rp->commands[36] |= BIT(0);
784 #endif /* CONFIG_BT_CTLR_DTM_HCI */
785 
786 #if defined(CONFIG_BT_CTLR_PRIVACY)
787 	/* LE resolving list commands, LE Read Peer RPA */
788 	rp->commands[34] |= BIT(3) | BIT(4) | BIT(5) | BIT(6) | BIT(7);
789 	/* LE Read Local RPA, LE Set AR Enable, Set RPA Timeout */
790 	rp->commands[35] |= BIT(0) | BIT(1) | BIT(2);
791 	/* LE Set Privacy Mode */
792 	rp->commands[39] |= BIT(2);
793 #endif /* CONFIG_BT_CTLR_PRIVACY */
794 
795 #if defined(CONFIG_BT_CTLR_DF)
796 #if defined(CONFIG_BT_CTLR_DF_ADV_CTE_TX)
797 	/* LE Set Connectionless CTE Transmit Parameters,
798 	 * LE Set Connectionless CTE Transmit Enable
799 	 */
800 	rp->commands[39] |= BIT(5) | BIT(6);
801 #endif /* CONFIG_BT_CTLR_DF_ADV_CTE_TX */
802 #if defined(CONFIG_BT_CTLR_DF_SCAN_CTE_RX)
803 	/* LE Set Connectionless IQ Sampling Enable */
804 	rp->commands[39] |= BIT(7);
805 #endif /* CONFIG_BT_CTLR_DF_SCAN_CTE_RX */
806 	/* LE Read Antenna Information */
807 	rp->commands[40] |= BIT(4);
808 #endif /* CONFIG_BT_CTLR_DF */
809 
810 #if defined(CONFIG_BT_CTLR_SYNC_PERIODIC)
811 	/* LE Set Periodic Advertising Receive Enable */
812 	rp->commands[40] |= BIT(5);
813 #endif /* CONFIG_BT_CTLR_SYNC_PERIODIC */
814 
815 #if defined(CONFIG_BT_HCI_RAW) && defined(CONFIG_BT_TINYCRYPT_ECC)
816 	bt_hci_ecc_supported_commands(rp->commands);
817 #endif /* CONFIG_BT_HCI_RAW && CONFIG_BT_TINYCRYPT_ECC */
818 
819 	/* LE Read TX Power. */
820 	rp->commands[38] |= BIT(7);
821 
822 #if defined(CONFIG_BT_CTLR_ADV_ISO) || defined(CONFIG_BT_CTLR_CONN_ISO)
823 	/* LE Read Buffer Size v2 */
824 	rp->commands[41] |= BIT(5);
825 
826 #endif /* CONFIG_BT_CTLR_ISO || CONFIG_BT_CTLR_CONN_ISO */
827 
828 #if defined(CONFIG_BT_CTLR_HCI_CODEC_AND_DELAY_INFO)
829 	/* Read Supported Codecs */
830 	rp->commands[29] |= BIT(5);
831 	/* Read Supported Codecs [v2], Codec Capabilities, Controller Delay */
832 	rp->commands[45] |= BIT(2) | BIT(3) | BIT(4);
833 #endif /* CONFIG_BT_CTLR_HCI_CODEC_AND_DELAY_INFO */
834 }
835 
read_local_features(struct net_buf * buf,struct net_buf ** evt)836 static void read_local_features(struct net_buf *buf, struct net_buf **evt)
837 {
838 	struct bt_hci_rp_read_local_features *rp;
839 
840 	rp = hci_cmd_complete(evt, sizeof(*rp));
841 
842 	rp->status = 0x00;
843 	(void)memset(&rp->features[0], 0x00, sizeof(rp->features));
844 	/* BR/EDR not supported and LE supported */
845 	rp->features[4] = (1 << 5) | (1 << 6);
846 }
847 
read_bd_addr(struct net_buf * buf,struct net_buf ** evt)848 static void read_bd_addr(struct net_buf *buf, struct net_buf **evt)
849 {
850 	struct bt_hci_rp_read_bd_addr *rp;
851 
852 	rp = hci_cmd_complete(evt, sizeof(*rp));
853 
854 	rp->status = 0x00;
855 
856 	(void)ll_addr_read(0, &rp->bdaddr.val[0]);
857 }
858 
859 #if defined(CONFIG_BT_CTLR_HCI_CODEC_AND_DELAY_INFO)
hci_vendor_read_std_codecs(const struct bt_hci_std_codec_info_v2 ** codecs)860 uint8_t __weak hci_vendor_read_std_codecs(
861 	const struct bt_hci_std_codec_info_v2 **codecs)
862 {
863 	ARG_UNUSED(codecs);
864 
865 	/* return number of supported codecs */
866 	return 0;
867 }
868 
hci_vendor_read_vs_codecs(const struct bt_hci_vs_codec_info_v2 ** codecs)869 uint8_t __weak hci_vendor_read_vs_codecs(
870 	const struct bt_hci_vs_codec_info_v2 **codecs)
871 {
872 	ARG_UNUSED(codecs);
873 
874 	/* return number of supported codecs */
875 	return 0;
876 }
877 
read_codecs(struct net_buf * buf,struct net_buf ** evt)878 static void read_codecs(struct net_buf *buf, struct net_buf **evt)
879 {
880 	struct bt_hci_rp_read_codecs *rp;
881 	const struct bt_hci_std_codec_info_v2 *std_codec_info;
882 	const struct bt_hci_vs_codec_info_v2 *vs_codec_info;
883 	struct bt_hci_std_codecs *std_codecs;
884 	struct bt_hci_vs_codecs *vs_codecs;
885 	size_t std_codecs_bytes;
886 	size_t vs_codecs_bytes;
887 	uint8_t num_std_codecs;
888 	uint8_t num_vs_codecs;
889 	uint8_t i;
890 
891 	/* read standard codec information */
892 	num_std_codecs = hci_vendor_read_std_codecs(&std_codec_info);
893 	std_codecs_bytes = sizeof(struct bt_hci_std_codecs) +
894 		num_std_codecs * sizeof(struct bt_hci_std_codec_info);
895 	/* read vendor-specific codec information */
896 	num_vs_codecs = hci_vendor_read_vs_codecs(&vs_codec_info);
897 	vs_codecs_bytes = sizeof(struct bt_hci_vs_codecs) +
898 		num_vs_codecs *	sizeof(struct bt_hci_vs_codec_info);
899 
900 	/* allocate response packet */
901 	rp = hci_cmd_complete(evt, sizeof(*rp) +
902 			      std_codecs_bytes +
903 			      vs_codecs_bytes);
904 	rp->status = 0x00;
905 
906 	/* copy standard codec information */
907 	std_codecs = (struct bt_hci_std_codecs *)&rp->codecs[0];
908 	std_codecs->num_codecs = num_std_codecs;
909 	for (i = 0; i < num_std_codecs; i++) {
910 		struct bt_hci_std_codec_info *codec;
911 
912 		codec = &std_codecs->codec_info[i];
913 		codec->codec_id = std_codec_info[i].codec_id;
914 	}
915 
916 	/* copy vendor specific codec information */
917 	vs_codecs = (struct bt_hci_vs_codecs *)&rp->codecs[std_codecs_bytes];
918 	vs_codecs->num_codecs = num_vs_codecs;
919 	for (i = 0; i < num_std_codecs; i++) {
920 		struct bt_hci_vs_codec_info *codec;
921 
922 		codec = &vs_codecs->codec_info[i];
923 		codec->company_id =
924 			sys_cpu_to_le16(vs_codec_info[i].company_id);
925 		codec->codec_id = sys_cpu_to_le16(vs_codec_info[i].codec_id);
926 	}
927 }
928 
read_codecs_v2(struct net_buf * buf,struct net_buf ** evt)929 static void read_codecs_v2(struct net_buf *buf, struct net_buf **evt)
930 {
931 	struct bt_hci_rp_read_codecs_v2 *rp;
932 	const struct bt_hci_std_codec_info_v2 *std_codec_info;
933 	const struct bt_hci_vs_codec_info_v2 *vs_codec_info;
934 	struct bt_hci_std_codecs_v2 *std_codecs;
935 	struct bt_hci_vs_codecs_v2 *vs_codecs;
936 	size_t std_codecs_bytes;
937 	size_t vs_codecs_bytes;
938 	uint8_t num_std_codecs;
939 	uint8_t num_vs_codecs;
940 	uint8_t i;
941 
942 	/* read standard codec information */
943 	num_std_codecs = hci_vendor_read_std_codecs(&std_codec_info);
944 	std_codecs_bytes = sizeof(struct bt_hci_std_codecs_v2) +
945 		num_std_codecs * sizeof(struct bt_hci_std_codec_info_v2);
946 	/* read vendor-specific codec information */
947 	num_vs_codecs = hci_vendor_read_vs_codecs(&vs_codec_info);
948 	vs_codecs_bytes = sizeof(struct bt_hci_vs_codecs_v2) +
949 		num_vs_codecs *	sizeof(struct bt_hci_vs_codec_info_v2);
950 
951 	/* allocate response packet */
952 	rp = hci_cmd_complete(evt, sizeof(*rp) +
953 			      std_codecs_bytes +
954 			      vs_codecs_bytes);
955 	rp->status = 0x00;
956 
957 	/* copy standard codec information */
958 	std_codecs = (struct bt_hci_std_codecs_v2 *)&rp->codecs[0];
959 	std_codecs->num_codecs = num_std_codecs;
960 	for (i = 0; i < num_std_codecs; i++) {
961 		struct bt_hci_std_codec_info_v2 *codec;
962 
963 		codec = &std_codecs->codec_info[i];
964 		codec->codec_id = std_codec_info[i].codec_id;
965 		codec->transports = std_codec_info[i].transports;
966 	}
967 
968 	/* copy vendor specific codec information  */
969 	vs_codecs = (struct bt_hci_vs_codecs_v2 *)&rp->codecs[std_codecs_bytes];
970 	vs_codecs->num_codecs = num_vs_codecs;
971 	for (i = 0; i < num_std_codecs; i++) {
972 		struct bt_hci_vs_codec_info_v2 *codec;
973 
974 		codec = &vs_codecs->codec_info[i];
975 		codec->company_id =
976 			sys_cpu_to_le16(vs_codec_info[i].company_id);
977 		codec->codec_id = sys_cpu_to_le16(vs_codec_info[i].codec_id);
978 		codec->transports = vs_codec_info[i].transports;
979 	}
980 }
981 
hci_vendor_read_codec_capabilities(uint8_t coding_format,uint16_t company_id,uint16_t vs_codec_id,uint8_t transport,uint8_t direction,uint8_t * num_capabilities,size_t * capabilities_bytes,const uint8_t ** capabilities)982 uint8_t __weak hci_vendor_read_codec_capabilities(uint8_t coding_format,
983 						  uint16_t company_id,
984 						  uint16_t vs_codec_id,
985 						  uint8_t transport,
986 						  uint8_t direction,
987 						  uint8_t *num_capabilities,
988 						  size_t *capabilities_bytes,
989 						  const uint8_t **capabilities)
990 {
991 	ARG_UNUSED(coding_format);
992 	ARG_UNUSED(company_id);
993 	ARG_UNUSED(vs_codec_id);
994 	ARG_UNUSED(transport);
995 	ARG_UNUSED(direction);
996 	ARG_UNUSED(capabilities);
997 
998 	*num_capabilities = 0;
999 	*capabilities_bytes = 0;
1000 
1001 	/* return status */
1002 	return 0x00;
1003 }
1004 
read_codec_capabilities(struct net_buf * buf,struct net_buf ** evt)1005 static void read_codec_capabilities(struct net_buf *buf, struct net_buf **evt)
1006 {
1007 	struct bt_hci_cp_read_codec_capabilities *cmd = (void *)buf->data;
1008 	struct bt_hci_rp_read_codec_capabilities *rp;
1009 	const uint8_t *capabilities;
1010 	size_t capabilities_bytes;
1011 	uint8_t num_capabilities;
1012 	uint16_t vs_codec_id;
1013 	uint16_t company_id;
1014 	uint8_t status;
1015 
1016 	company_id = sys_le16_to_cpu(cmd->codec_id.company_id);
1017 	vs_codec_id = sys_le16_to_cpu(cmd->codec_id.vs_codec_id);
1018 
1019 	/* read codec capabilities */
1020 	status = hci_vendor_read_codec_capabilities(cmd->codec_id.coding_format,
1021 						    company_id,
1022 						    vs_codec_id,
1023 						    cmd->transport,
1024 						    cmd->direction,
1025 						    &num_capabilities,
1026 						    &capabilities_bytes,
1027 						    &capabilities);
1028 
1029 	/* allocate response packet */
1030 	rp = hci_cmd_complete(evt, sizeof(*rp) + capabilities_bytes);
1031 	rp->status = status;
1032 
1033 	/* copy codec capabilities information */
1034 	rp->num_capabilities = num_capabilities;
1035 	memcpy(&rp->capabilities, capabilities, capabilities_bytes);
1036 }
1037 
hci_vendor_read_ctlr_delay(uint8_t coding_format,uint16_t company_id,uint16_t vs_codec_id,uint8_t transport,uint8_t direction,uint8_t codec_config_len,const uint8_t * codec_config,uint32_t * min_delay,uint32_t * max_delay)1038 uint8_t __weak hci_vendor_read_ctlr_delay(uint8_t coding_format,
1039 					  uint16_t company_id,
1040 					  uint16_t vs_codec_id,
1041 					  uint8_t transport,
1042 					  uint8_t direction,
1043 					  uint8_t codec_config_len,
1044 					  const uint8_t *codec_config,
1045 					  uint32_t *min_delay,
1046 					  uint32_t *max_delay)
1047 {
1048 	ARG_UNUSED(coding_format);
1049 	ARG_UNUSED(company_id);
1050 	ARG_UNUSED(vs_codec_id);
1051 	ARG_UNUSED(transport);
1052 	ARG_UNUSED(direction);
1053 	ARG_UNUSED(codec_config_len);
1054 	ARG_UNUSED(codec_config);
1055 
1056 	*min_delay = 0;
1057 	*max_delay = 0x3D0900; /* 4 seconds, maximum value allowed by spec */
1058 
1059 	/* return status */
1060 	return 0x00;
1061 }
1062 
read_ctlr_delay(struct net_buf * buf,struct net_buf ** evt)1063 static void read_ctlr_delay(struct net_buf *buf, struct net_buf **evt)
1064 {
1065 	struct bt_hci_cp_read_ctlr_delay *cmd = (void *)buf->data;
1066 	struct bt_hci_rp_read_ctlr_delay *rp;
1067 	uint16_t vs_codec_id;
1068 	uint16_t company_id;
1069 	uint32_t min_delay;
1070 	uint32_t max_delay;
1071 	uint8_t status;
1072 
1073 	company_id = sys_le16_to_cpu(cmd->codec_id.company_id);
1074 	vs_codec_id = sys_le16_to_cpu(cmd->codec_id.vs_codec_id);
1075 
1076 	status = hci_vendor_read_ctlr_delay(cmd->codec_id.coding_format,
1077 					    company_id,
1078 					    vs_codec_id,
1079 					    cmd->transport,
1080 					    cmd->direction,
1081 					    cmd->codec_config_len,
1082 					    cmd->codec_config,
1083 					    &min_delay,
1084 					    &max_delay);
1085 
1086 	rp = hci_cmd_complete(evt, sizeof(*rp));
1087 	rp->status = status;
1088 	sys_put_le24(min_delay, rp->min_ctlr_delay);
1089 	sys_put_le24(max_delay, rp->max_ctlr_delay);
1090 }
1091 #endif /* CONFIG_BT_CTLR_HCI_CODEC_AND_DELAY_INFO */
1092 
info_cmd_handle(uint16_t ocf,struct net_buf * cmd,struct net_buf ** evt)1093 static int info_cmd_handle(uint16_t  ocf, struct net_buf *cmd,
1094 			   struct net_buf **evt)
1095 {
1096 	switch (ocf) {
1097 	case BT_OCF(BT_HCI_OP_READ_LOCAL_VERSION_INFO):
1098 		read_local_version_info(cmd, evt);
1099 		break;
1100 
1101 	case BT_OCF(BT_HCI_OP_READ_SUPPORTED_COMMANDS):
1102 		read_supported_commands(cmd, evt);
1103 		break;
1104 
1105 	case BT_OCF(BT_HCI_OP_READ_LOCAL_FEATURES):
1106 		read_local_features(cmd, evt);
1107 		break;
1108 
1109 	case BT_OCF(BT_HCI_OP_READ_BD_ADDR):
1110 		read_bd_addr(cmd, evt);
1111 		break;
1112 
1113 #if defined(CONFIG_BT_CTLR_HCI_CODEC_AND_DELAY_INFO)
1114 	case BT_OCF(BT_HCI_OP_READ_CODECS):
1115 		read_codecs(cmd, evt);
1116 		break;
1117 
1118 	case BT_OCF(BT_HCI_OP_READ_CODECS_V2):
1119 		read_codecs_v2(cmd, evt);
1120 		break;
1121 
1122 	case BT_OCF(BT_HCI_OP_READ_CODEC_CAPABILITIES):
1123 		read_codec_capabilities(cmd, evt);
1124 		break;
1125 
1126 	case BT_OCF(BT_HCI_OP_READ_CTLR_DELAY):
1127 		read_ctlr_delay(cmd, evt);
1128 		break;
1129 #endif /* CONFIG_BT_CTLR_HCI_CODEC_AND_DELAY_INFO */
1130 
1131 	default:
1132 		return -EINVAL;
1133 	}
1134 
1135 	return 0;
1136 }
1137 
1138 #if defined(CONFIG_BT_CTLR_CONN_RSSI)
read_rssi(struct net_buf * buf,struct net_buf ** evt)1139 static void read_rssi(struct net_buf *buf, struct net_buf **evt)
1140 {
1141 	struct bt_hci_cp_read_rssi *cmd = (void *)buf->data;
1142 	struct bt_hci_rp_read_rssi *rp;
1143 	uint16_t handle;
1144 
1145 	handle = sys_le16_to_cpu(cmd->handle);
1146 
1147 	rp = hci_cmd_complete(evt, sizeof(*rp));
1148 
1149 	rp->status = ll_rssi_get(handle, &rp->rssi);
1150 
1151 	rp->handle = sys_cpu_to_le16(handle);
1152 	/* The Link Layer currently returns RSSI as an absolute value */
1153 	rp->rssi = (!rp->status) ? -rp->rssi : 127;
1154 }
1155 #endif /* CONFIG_BT_CTLR_CONN_RSSI */
1156 
status_cmd_handle(uint16_t ocf,struct net_buf * cmd,struct net_buf ** evt)1157 static int status_cmd_handle(uint16_t  ocf, struct net_buf *cmd,
1158 			     struct net_buf **evt)
1159 {
1160 	switch (ocf) {
1161 #if defined(CONFIG_BT_CTLR_CONN_RSSI)
1162 	case BT_OCF(BT_HCI_OP_READ_RSSI):
1163 		read_rssi(cmd, evt);
1164 		break;
1165 #endif /* CONFIG_BT_CTLR_CONN_RSSI */
1166 
1167 	default:
1168 		return -EINVAL;
1169 	}
1170 
1171 	return 0;
1172 }
1173 
le_set_event_mask(struct net_buf * buf,struct net_buf ** evt)1174 static void le_set_event_mask(struct net_buf *buf, struct net_buf **evt)
1175 {
1176 	struct bt_hci_cp_set_event_mask *cmd = (void *)buf->data;
1177 
1178 	le_event_mask = sys_get_le64(cmd->events);
1179 
1180 	*evt = cmd_complete_status(0x00);
1181 }
1182 
le_read_buffer_size(struct net_buf * buf,struct net_buf ** evt)1183 static void le_read_buffer_size(struct net_buf *buf, struct net_buf **evt)
1184 {
1185 	struct bt_hci_rp_le_read_buffer_size *rp;
1186 
1187 	rp = hci_cmd_complete(evt, sizeof(*rp));
1188 
1189 	rp->status = 0x00;
1190 
1191 	rp->le_max_len = sys_cpu_to_le16(CONFIG_BT_BUF_ACL_TX_SIZE);
1192 	rp->le_max_num = CONFIG_BT_BUF_ACL_TX_COUNT;
1193 }
1194 
1195 #if defined(CONFIG_BT_CTLR_ADV_ISO) || defined(CONFIG_BT_CTLR_CONN_ISO)
le_read_buffer_size_v2(struct net_buf * buf,struct net_buf ** evt)1196 static void le_read_buffer_size_v2(struct net_buf *buf, struct net_buf **evt)
1197 {
1198 	struct bt_hci_rp_le_read_buffer_size_v2 *rp;
1199 
1200 	rp = hci_cmd_complete(evt, sizeof(*rp));
1201 
1202 	rp->status = 0x00;
1203 
1204 	rp->acl_max_len = sys_cpu_to_le16(CONFIG_BT_BUF_ACL_TX_SIZE);
1205 	rp->acl_max_num = CONFIG_BT_BUF_ACL_TX_COUNT;
1206 	rp->iso_max_len = sys_cpu_to_le16(CONFIG_BT_CTLR_ISO_TX_BUFFER_SIZE);
1207 	rp->iso_max_num = CONFIG_BT_CTLR_ISO_TX_BUFFERS;
1208 }
1209 #endif /* CONFIG_BT_CTLR_ADV_ISO || CONFIG_BT_CTLR_CONN_ISO */
1210 
le_read_local_features(struct net_buf * buf,struct net_buf ** evt)1211 static void le_read_local_features(struct net_buf *buf, struct net_buf **evt)
1212 {
1213 	struct bt_hci_rp_le_read_local_features *rp;
1214 
1215 	rp = hci_cmd_complete(evt, sizeof(*rp));
1216 
1217 	rp->status = 0x00;
1218 
1219 	(void)memset(&rp->features[0], 0x00, sizeof(rp->features));
1220 	sys_put_le64(ll_feat_get(), rp->features);
1221 }
1222 
le_set_random_address(struct net_buf * buf,struct net_buf ** evt)1223 static void le_set_random_address(struct net_buf *buf, struct net_buf **evt)
1224 {
1225 	struct bt_hci_cp_le_set_random_address *cmd = (void *)buf->data;
1226 	uint8_t status;
1227 
1228 	status = ll_addr_set(1, &cmd->bdaddr.val[0]);
1229 
1230 	*evt = cmd_complete_status(status);
1231 }
1232 
1233 #if defined(CONFIG_BT_CTLR_FILTER_ACCEPT_LIST)
le_read_fal_size(struct net_buf * buf,struct net_buf ** evt)1234 static void le_read_fal_size(struct net_buf *buf, struct net_buf **evt)
1235 {
1236 	struct bt_hci_rp_le_read_fal_size *rp;
1237 
1238 	rp = hci_cmd_complete(evt, sizeof(*rp));
1239 	rp->status = 0x00;
1240 
1241 	rp->fal_size = ll_fal_size_get();
1242 }
1243 
le_clear_fal(struct net_buf * buf,struct net_buf ** evt)1244 static void le_clear_fal(struct net_buf *buf, struct net_buf **evt)
1245 {
1246 	uint8_t status;
1247 
1248 	status = ll_fal_clear();
1249 
1250 	*evt = cmd_complete_status(status);
1251 }
1252 
le_add_dev_to_fal(struct net_buf * buf,struct net_buf ** evt)1253 static void le_add_dev_to_fal(struct net_buf *buf, struct net_buf **evt)
1254 {
1255 	struct bt_hci_cp_le_add_dev_to_fal *cmd = (void *)buf->data;
1256 	uint8_t status;
1257 
1258 	status = ll_fal_add(&cmd->addr);
1259 
1260 	*evt = cmd_complete_status(status);
1261 }
1262 
le_rem_dev_from_fal(struct net_buf * buf,struct net_buf ** evt)1263 static void le_rem_dev_from_fal(struct net_buf *buf, struct net_buf **evt)
1264 {
1265 	struct bt_hci_cp_le_rem_dev_from_fal *cmd = (void *)buf->data;
1266 	uint8_t status;
1267 
1268 	status = ll_fal_remove(&cmd->addr);
1269 
1270 	*evt = cmd_complete_status(status);
1271 }
1272 #endif /* CONFIG_BT_CTLR_FILTER_ACCEPT_LIST */
1273 
le_encrypt(struct net_buf * buf,struct net_buf ** evt)1274 static void le_encrypt(struct net_buf *buf, struct net_buf **evt)
1275 {
1276 	struct bt_hci_cp_le_encrypt *cmd = (void *)buf->data;
1277 	struct bt_hci_rp_le_encrypt *rp;
1278 	uint8_t enc_data[16];
1279 
1280 	ecb_encrypt(cmd->key, cmd->plaintext, enc_data, NULL);
1281 
1282 	rp = hci_cmd_complete(evt, sizeof(*rp));
1283 
1284 	rp->status = 0x00;
1285 	memcpy(rp->enc_data, enc_data, 16);
1286 }
1287 
le_rand(struct net_buf * buf,struct net_buf ** evt)1288 static void le_rand(struct net_buf *buf, struct net_buf **evt)
1289 {
1290 	struct bt_hci_rp_le_rand *rp;
1291 	uint8_t count = sizeof(rp->rand);
1292 
1293 	rp = hci_cmd_complete(evt, sizeof(*rp));
1294 	rp->status = 0x00;
1295 
1296 	lll_csrand_get(rp->rand, count);
1297 }
1298 
le_read_supp_states(struct net_buf * buf,struct net_buf ** evt)1299 static void le_read_supp_states(struct net_buf *buf, struct net_buf **evt)
1300 {
1301 	struct bt_hci_rp_le_read_supp_states *rp;
1302 	uint64_t states = 0U;
1303 
1304 	rp = hci_cmd_complete(evt, sizeof(*rp));
1305 	rp->status = 0x00;
1306 
1307 #define ST_ADV (BIT64(0)  | BIT64(1)  | BIT64(8)  | BIT64(9)  | BIT64(12) | \
1308 		BIT64(13) | BIT64(16) | BIT64(17) | BIT64(18) | BIT64(19) | \
1309 		BIT64(20) | BIT64(21))
1310 
1311 #define ST_SCA (BIT64(4)  | BIT64(5)  | BIT64(8)  | BIT64(9)  | BIT64(10) | \
1312 		BIT64(11) | BIT64(12) | BIT64(13) | BIT64(14) | BIT64(15) | \
1313 		BIT64(22) | BIT64(23) | BIT64(24) | BIT64(25) | BIT64(26) | \
1314 		BIT64(27) | BIT64(30) | BIT64(31))
1315 
1316 #define ST_PER (BIT64(2)  | BIT64(3)  | BIT64(7)  | BIT64(10) | BIT64(11) | \
1317 		BIT64(14) | BIT64(15) | BIT64(20) | BIT64(21) | BIT64(26) | \
1318 		BIT64(27) | BIT64(29) | BIT64(30) | BIT64(31) | BIT64(32) | \
1319 		BIT64(33) | BIT64(34) | BIT64(35) | BIT64(36) | BIT64(37) | \
1320 		BIT64(38) | BIT64(39) | BIT64(40) | BIT64(41))
1321 
1322 #define ST_CEN (BIT64(6)  | BIT64(16) | BIT64(17) | BIT64(18) | BIT64(19) | \
1323 		BIT64(22) | BIT64(23) | BIT64(24) | BIT64(25) | BIT64(28) | \
1324 		BIT64(32) | BIT64(33) | BIT64(34) | BIT64(35) | BIT64(36) | \
1325 		BIT64(37) | BIT64(41))
1326 
1327 #if defined(CONFIG_BT_BROADCASTER)
1328 	states |= ST_ADV;
1329 #else
1330 	states &= ~ST_ADV;
1331 #endif
1332 #if defined(CONFIG_BT_OBSERVER)
1333 	states |= ST_SCA;
1334 #else
1335 	states &= ~ST_SCA;
1336 #endif
1337 #if defined(CONFIG_BT_PERIPHERAL)
1338 	states |= ST_PER;
1339 #else
1340 	states &= ~ST_PER;
1341 #endif
1342 #if defined(CONFIG_BT_CENTRAL)
1343 	states |= ST_CEN;
1344 #else
1345 	states &= ~ST_CEN;
1346 #endif
1347 	/* All states and combinations supported except:
1348 	 * Initiating State + Passive Scanning
1349 	 * Initiating State + Active Scanning
1350 	 */
1351 	states &= ~(BIT64(22) | BIT64(23));
1352 	BT_DBG("states: 0x%08x%08x", (uint32_t)(states >> 32),
1353 				     (uint32_t)(states & 0xffffffff));
1354 	sys_put_le64(states, rp->le_states);
1355 }
1356 
1357 #if defined(CONFIG_BT_BROADCASTER)
le_set_adv_param(struct net_buf * buf,struct net_buf ** evt)1358 static void le_set_adv_param(struct net_buf *buf, struct net_buf **evt)
1359 {
1360 	struct bt_hci_cp_le_set_adv_param *cmd = (void *)buf->data;
1361 	uint16_t min_interval;
1362 	uint8_t status;
1363 
1364 	if (adv_cmds_legacy_check(evt)) {
1365 		return;
1366 	}
1367 
1368 	min_interval = sys_le16_to_cpu(cmd->min_interval);
1369 
1370 	if (IS_ENABLED(CONFIG_BT_CTLR_PARAM_CHECK) &&
1371 	    (cmd->type != BT_HCI_ADV_DIRECT_IND)) {
1372 		uint16_t max_interval = sys_le16_to_cpu(cmd->max_interval);
1373 
1374 		if ((min_interval > max_interval) ||
1375 		    (min_interval < 0x0020) ||
1376 		    (max_interval > 0x4000)) {
1377 			*evt = cmd_complete_status(BT_HCI_ERR_INVALID_PARAM);
1378 			return;
1379 		}
1380 	}
1381 
1382 #if defined(CONFIG_BT_CTLR_ADV_EXT)
1383 	status = ll_adv_params_set(0, 0, min_interval, cmd->type,
1384 				   cmd->own_addr_type, cmd->direct_addr.type,
1385 				   &cmd->direct_addr.a.val[0], cmd->channel_map,
1386 				   cmd->filter_policy, 0, 0, 0, 0, 0, 0);
1387 #else /* !CONFIG_BT_CTLR_ADV_EXT */
1388 	status = ll_adv_params_set(min_interval, cmd->type,
1389 				   cmd->own_addr_type, cmd->direct_addr.type,
1390 				   &cmd->direct_addr.a.val[0], cmd->channel_map,
1391 				   cmd->filter_policy);
1392 #endif /* !CONFIG_BT_CTLR_ADV_EXT */
1393 
1394 	*evt = cmd_complete_status(status);
1395 }
1396 
le_read_adv_chan_tx_power(struct net_buf * buf,struct net_buf ** evt)1397 static void le_read_adv_chan_tx_power(struct net_buf *buf, struct net_buf **evt)
1398 {
1399 	struct bt_hci_rp_le_read_chan_tx_power *rp;
1400 
1401 	if (adv_cmds_legacy_check(evt)) {
1402 		return;
1403 	}
1404 
1405 	rp = hci_cmd_complete(evt, sizeof(*rp));
1406 
1407 	rp->status = 0x00;
1408 
1409 	rp->tx_power_level = 0;
1410 }
1411 
le_set_adv_data(struct net_buf * buf,struct net_buf ** evt)1412 static void le_set_adv_data(struct net_buf *buf, struct net_buf **evt)
1413 {
1414 	struct bt_hci_cp_le_set_adv_data *cmd = (void *)buf->data;
1415 	uint8_t status;
1416 
1417 	if (adv_cmds_legacy_check(evt)) {
1418 		return;
1419 	}
1420 
1421 #if defined(CONFIG_BT_CTLR_ADV_EXT)
1422 	status = ll_adv_data_set(0, cmd->len, &cmd->data[0]);
1423 #else /* !CONFIG_BT_CTLR_ADV_EXT */
1424 	status = ll_adv_data_set(cmd->len, &cmd->data[0]);
1425 #endif /* !CONFIG_BT_CTLR_ADV_EXT */
1426 
1427 	*evt = cmd_complete_status(status);
1428 }
1429 
le_set_scan_rsp_data(struct net_buf * buf,struct net_buf ** evt)1430 static void le_set_scan_rsp_data(struct net_buf *buf, struct net_buf **evt)
1431 {
1432 	struct bt_hci_cp_le_set_scan_rsp_data *cmd = (void *)buf->data;
1433 	uint8_t status;
1434 
1435 	if (adv_cmds_legacy_check(evt)) {
1436 		return;
1437 	}
1438 
1439 #if defined(CONFIG_BT_CTLR_ADV_EXT)
1440 	status = ll_adv_scan_rsp_set(0, cmd->len, &cmd->data[0]);
1441 #else /* !CONFIG_BT_CTLR_ADV_EXT */
1442 	status = ll_adv_scan_rsp_set(cmd->len, &cmd->data[0]);
1443 #endif /* !CONFIG_BT_CTLR_ADV_EXT */
1444 
1445 	*evt = cmd_complete_status(status);
1446 }
1447 
le_set_adv_enable(struct net_buf * buf,struct net_buf ** evt)1448 static void le_set_adv_enable(struct net_buf *buf, struct net_buf **evt)
1449 {
1450 	struct bt_hci_cp_le_set_adv_enable *cmd = (void *)buf->data;
1451 	uint8_t status;
1452 
1453 	if (adv_cmds_legacy_check(evt)) {
1454 		return;
1455 	}
1456 
1457 #if defined(CONFIG_BT_CTLR_ADV_EXT) || defined(CONFIG_BT_HCI_MESH_EXT)
1458 #if defined(CONFIG_BT_HCI_MESH_EXT)
1459 	status = ll_adv_enable(0, cmd->enable, 0, 0, 0, 0, 0);
1460 #else /* !CONFIG_BT_HCI_MESH_EXT */
1461 	status = ll_adv_enable(0, cmd->enable, 0, 0);
1462 #endif /* !CONFIG_BT_HCI_MESH_EXT */
1463 #else /* !CONFIG_BT_CTLR_ADV_EXT || !CONFIG_BT_HCI_MESH_EXT */
1464 	status = ll_adv_enable(cmd->enable);
1465 #endif /* !CONFIG_BT_CTLR_ADV_EXT || !CONFIG_BT_HCI_MESH_EXT */
1466 
1467 	*evt = cmd_complete_status(status);
1468 }
1469 
1470 #if defined(CONFIG_BT_CTLR_ADV_ISO)
le_create_big(struct net_buf * buf,struct net_buf ** evt)1471 static void le_create_big(struct net_buf *buf, struct net_buf **evt)
1472 {
1473 	struct bt_hci_cp_le_create_big *cmd = (void *)buf->data;
1474 	uint8_t status;
1475 	uint32_t sdu_interval;
1476 	uint16_t max_sdu;
1477 	uint16_t max_latency;
1478 	uint8_t big_handle;
1479 	uint8_t adv_handle;
1480 
1481 	status = ll_adv_iso_by_hci_handle_new(cmd->big_handle, &big_handle);
1482 	if (status) {
1483 		*evt = cmd_status(status);
1484 		return;
1485 	}
1486 
1487 	status = ll_adv_set_by_hci_handle_get(cmd->adv_handle, &adv_handle);
1488 	if (status) {
1489 		*evt = cmd_status(status);
1490 		return;
1491 	}
1492 
1493 	sdu_interval = sys_get_le24(cmd->sdu_interval);
1494 	max_sdu = sys_le16_to_cpu(cmd->max_sdu);
1495 	max_latency = sys_le16_to_cpu(cmd->max_latency);
1496 
1497 	status = ll_big_create(big_handle, adv_handle, cmd->num_bis,
1498 			       sdu_interval, max_sdu, max_latency, cmd->rtn,
1499 			       cmd->phy, cmd->packing, cmd->framing,
1500 			       cmd->encryption, cmd->bcode);
1501 
1502 	*evt = cmd_status(status);
1503 }
1504 
le_create_big_test(struct net_buf * buf,struct net_buf ** evt)1505 static void le_create_big_test(struct net_buf *buf, struct net_buf **evt)
1506 {
1507 	struct bt_hci_cp_le_create_big_test *cmd = (void *)buf->data;
1508 	uint8_t status;
1509 	uint32_t sdu_interval;
1510 	uint16_t iso_interval;
1511 	uint16_t max_sdu;
1512 	uint16_t max_pdu;
1513 
1514 	sdu_interval = sys_get_le24(cmd->sdu_interval);
1515 	iso_interval = sys_le16_to_cpu(cmd->iso_interval);
1516 	max_sdu = sys_le16_to_cpu(cmd->max_sdu);
1517 	max_pdu = sys_le16_to_cpu(cmd->max_pdu);
1518 
1519 	status = ll_big_test_create(cmd->big_handle, cmd->adv_handle,
1520 				    cmd->num_bis, sdu_interval, iso_interval,
1521 				    cmd->nse, max_sdu, max_pdu, cmd->phy,
1522 				    cmd->packing, cmd->framing, cmd->bn,
1523 				    cmd->irc, cmd->pto, cmd->encryption,
1524 				    cmd->bcode);
1525 
1526 	*evt = cmd_status(status);
1527 }
1528 
le_terminate_big(struct net_buf * buf,struct net_buf ** evt)1529 static void le_terminate_big(struct net_buf *buf, struct net_buf **evt)
1530 {
1531 	struct bt_hci_cp_le_terminate_big *cmd = (void *)buf->data;
1532 	uint8_t status;
1533 
1534 	status = ll_big_terminate(cmd->big_handle, cmd->reason);
1535 
1536 	*evt = cmd_status(status);
1537 }
1538 #endif /* CONFIG_BT_CTLR_ADV_ISO */
1539 #endif /* CONFIG_BT_BROADCASTER */
1540 
1541 #if defined(CONFIG_BT_OBSERVER)
le_set_scan_param(struct net_buf * buf,struct net_buf ** evt)1542 static void le_set_scan_param(struct net_buf *buf, struct net_buf **evt)
1543 {
1544 	struct bt_hci_cp_le_set_scan_param *cmd = (void *)buf->data;
1545 	uint16_t interval;
1546 	uint16_t window;
1547 	uint8_t status;
1548 
1549 	if (adv_cmds_legacy_check(evt)) {
1550 		return;
1551 	}
1552 
1553 	interval = sys_le16_to_cpu(cmd->interval);
1554 	window = sys_le16_to_cpu(cmd->window);
1555 
1556 	status = ll_scan_params_set(cmd->scan_type, interval, window,
1557 				    cmd->addr_type, cmd->filter_policy);
1558 
1559 	*evt = cmd_complete_status(status);
1560 }
1561 
le_set_scan_enable(struct net_buf * buf,struct net_buf ** evt)1562 static void le_set_scan_enable(struct net_buf *buf, struct net_buf **evt)
1563 {
1564 	struct bt_hci_cp_le_set_scan_enable *cmd = (void *)buf->data;
1565 	uint8_t status;
1566 
1567 	if (adv_cmds_legacy_check(evt)) {
1568 		return;
1569 	}
1570 
1571 #if CONFIG_BT_CTLR_DUP_FILTER_LEN > 0
1572 	/* initialize duplicate filtering */
1573 	if (cmd->enable && cmd->filter_dup) {
1574 		dup_count = 0;
1575 		dup_curr = 0U;
1576 	} else {
1577 		dup_count = -1;
1578 	}
1579 #endif
1580 
1581 #if defined(CONFIG_BT_CTLR_ADV_EXT)
1582 	status = ll_scan_enable(cmd->enable, 0, 0);
1583 #else /* !CONFIG_BT_CTLR_ADV_EXT */
1584 	status = ll_scan_enable(cmd->enable);
1585 #endif /* !CONFIG_BT_CTLR_ADV_EXT */
1586 
1587 	/* NOTE: As filter duplicates is implemented here in HCI source code,
1588 	 *       enabling of already enabled scanning shall succeed after
1589 	 *       updates to filter duplicates is handled in the above
1590 	 *       statements. Refer to BT Spec v5.0 Vol 2 Part E Section 7.8.11.
1591 	 */
1592 	if (!IS_ENABLED(CONFIG_BT_CTLR_SCAN_ENABLE_STRICT) &&
1593 	    (status == BT_HCI_ERR_CMD_DISALLOWED)) {
1594 		status = BT_HCI_ERR_SUCCESS;
1595 	}
1596 
1597 	*evt = cmd_complete_status(status);
1598 }
1599 
1600 #if defined(CONFIG_BT_CTLR_SYNC_ISO)
le_big_create_sync(struct net_buf * buf,struct net_buf ** evt)1601 static void le_big_create_sync(struct net_buf *buf, struct net_buf **evt)
1602 {
1603 	struct bt_hci_cp_le_big_create_sync *cmd = (void *)buf->data;
1604 	uint8_t status;
1605 	uint16_t sync_handle;
1606 	uint16_t sync_timeout;
1607 
1608 	sync_handle = sys_le16_to_cpu(cmd->sync_handle);
1609 	sync_timeout = sys_le16_to_cpu(cmd->sync_timeout);
1610 
1611 	status = ll_big_sync_create(cmd->big_handle, sync_handle,
1612 				    cmd->encryption, cmd->bcode, cmd->mse,
1613 				    sync_timeout, cmd->num_bis, cmd->bis);
1614 
1615 	*evt = cmd_status(status);
1616 }
1617 
1618 
le_big_terminate_sync(struct net_buf * buf,struct net_buf ** evt,void ** node_rx)1619 static void le_big_terminate_sync(struct net_buf *buf, struct net_buf **evt,
1620 				  void **node_rx)
1621 {
1622 	struct bt_hci_cp_le_big_terminate_sync *cmd = (void *)buf->data;
1623 	uint8_t status;
1624 
1625 	status = ll_big_sync_terminate(cmd->big_handle, node_rx);
1626 
1627 	*evt = cmd_complete_status(status);
1628 }
1629 #endif /* CONFIG_BT_CTLR_SYNC_ISO */
1630 #endif /* CONFIG_BT_OBSERVER */
1631 
1632 #if defined(CONFIG_BT_CONN)
1633 #if defined(CONFIG_BT_CENTRAL)
1634 
check_cconn_params(bool ext,uint16_t scan_interval,uint16_t scan_window,uint16_t conn_interval_max,uint16_t conn_latency,uint16_t supervision_timeout)1635 static uint8_t check_cconn_params(bool ext, uint16_t scan_interval,
1636 				  uint16_t scan_window,
1637 				  uint16_t conn_interval_max,
1638 				  uint16_t conn_latency,
1639 				  uint16_t supervision_timeout)
1640 {
1641 	if (scan_interval < 0x0004 || scan_window < 0x0004 ||
1642 	    (!ext && (scan_interval > 0x4000 || scan_window > 0x4000))) {
1643 		return BT_HCI_ERR_INVALID_PARAM;
1644 	}
1645 
1646 	if (conn_interval_max < 0x0006 || conn_interval_max > 0x0C80) {
1647 		return BT_HCI_ERR_INVALID_PARAM;
1648 	}
1649 
1650 	if (conn_latency > 0x01F3) {
1651 		return BT_HCI_ERR_INVALID_PARAM;
1652 	}
1653 
1654 	if (supervision_timeout < 0x000A || supervision_timeout > 0x0C80) {
1655 		return BT_HCI_ERR_INVALID_PARAM;
1656 	}
1657 
1658 	/* sto * 10ms > (1 + lat) * ci * 1.25ms * 2
1659 	 * sto * 10 > (1 + lat) * ci * 2.5
1660 	 * sto * 2 > (1 + lat) * ci * 0.5
1661 	 * sto * 4 > (1 + lat) * ci
1662 	 */
1663 	if ((supervision_timeout << 2) <= ((1 + conn_latency) *
1664 					   conn_interval_max)) {
1665 		return BT_HCI_ERR_INVALID_PARAM;
1666 	}
1667 
1668 	return 0;
1669 }
1670 
le_create_connection(struct net_buf * buf,struct net_buf ** evt)1671 static void le_create_connection(struct net_buf *buf, struct net_buf **evt)
1672 {
1673 	struct bt_hci_cp_le_create_conn *cmd = (void *)buf->data;
1674 	uint16_t supervision_timeout;
1675 	uint16_t conn_interval_max;
1676 	uint16_t scan_interval;
1677 	uint16_t conn_latency;
1678 	uint16_t scan_window;
1679 	uint8_t status;
1680 
1681 	if (adv_cmds_legacy_check(NULL)) {
1682 		*evt = cmd_status(BT_HCI_ERR_CMD_DISALLOWED);
1683 		return;
1684 	}
1685 
1686 	scan_interval = sys_le16_to_cpu(cmd->scan_interval);
1687 	scan_window = sys_le16_to_cpu(cmd->scan_window);
1688 	conn_interval_max = sys_le16_to_cpu(cmd->conn_interval_max);
1689 	conn_latency = sys_le16_to_cpu(cmd->conn_latency);
1690 	supervision_timeout = sys_le16_to_cpu(cmd->supervision_timeout);
1691 
1692 	if (IS_ENABLED(CONFIG_BT_CTLR_PARAM_CHECK)) {
1693 		status = check_cconn_params(false, scan_interval,
1694 					    scan_window,
1695 					    conn_interval_max,
1696 					    conn_latency,
1697 					    supervision_timeout);
1698 		if (status) {
1699 			*evt = cmd_status(status);
1700 			return;
1701 		}
1702 	}
1703 
1704 #if defined(CONFIG_BT_CTLR_ADV_EXT)
1705 	status = ll_create_connection(scan_interval, scan_window,
1706 				      cmd->filter_policy,
1707 				      cmd->peer_addr.type,
1708 				      &cmd->peer_addr.a.val[0],
1709 				      cmd->own_addr_type, conn_interval_max,
1710 				      conn_latency, supervision_timeout,
1711 				      PHY_LEGACY);
1712 	if (status) {
1713 		*evt = cmd_status(status);
1714 		return;
1715 	}
1716 
1717 	status = ll_connect_enable(0U);
1718 
1719 #else /* !CONFIG_BT_CTLR_ADV_EXT */
1720 	status = ll_create_connection(scan_interval, scan_window,
1721 				      cmd->filter_policy,
1722 				      cmd->peer_addr.type,
1723 				      &cmd->peer_addr.a.val[0],
1724 				      cmd->own_addr_type, conn_interval_max,
1725 				      conn_latency, supervision_timeout);
1726 #endif /* !CONFIG_BT_CTLR_ADV_EXT */
1727 
1728 	*evt = cmd_status(status);
1729 }
1730 
le_create_conn_cancel(struct net_buf * buf,struct net_buf ** evt,void ** node_rx)1731 static void le_create_conn_cancel(struct net_buf *buf, struct net_buf **evt,
1732 				  void **node_rx)
1733 {
1734 	uint8_t status;
1735 
1736 	status = ll_connect_disable(node_rx);
1737 
1738 	*evt = cmd_complete_status(status);
1739 }
1740 
le_set_host_chan_classif(struct net_buf * buf,struct net_buf ** evt)1741 static void le_set_host_chan_classif(struct net_buf *buf, struct net_buf **evt)
1742 {
1743 	struct bt_hci_cp_le_set_host_chan_classif *cmd = (void *)buf->data;
1744 	uint8_t status;
1745 
1746 	status = ll_chm_update(&cmd->ch_map[0]);
1747 
1748 	*evt = cmd_complete_status(status);
1749 }
1750 
1751 #if defined(CONFIG_BT_CTLR_LE_ENC)
le_start_encryption(struct net_buf * buf,struct net_buf ** evt)1752 static void le_start_encryption(struct net_buf *buf, struct net_buf **evt)
1753 {
1754 	struct bt_hci_cp_le_start_encryption *cmd = (void *)buf->data;
1755 	uint16_t handle;
1756 	uint8_t status;
1757 
1758 	handle = sys_le16_to_cpu(cmd->handle);
1759 	status = ll_enc_req_send(handle,
1760 				 (uint8_t *)&cmd->rand,
1761 				 (uint8_t *)&cmd->ediv,
1762 				 &cmd->ltk[0]);
1763 
1764 	*evt = cmd_status(status);
1765 }
1766 #endif /* CONFIG_BT_CTLR_LE_ENC */
1767 
1768 #if defined(CONFIG_BT_CTLR_CENTRAL_ISO)
le_set_cig_parameters(struct net_buf * buf,struct net_buf ** evt)1769 static void le_set_cig_parameters(struct net_buf *buf, struct net_buf **evt)
1770 {
1771 	struct bt_hci_cp_le_set_cig_params *cmd = (void *)buf->data;
1772 	struct bt_hci_rp_le_set_cig_params *rp;
1773 	uint32_t c_interval;
1774 	uint32_t p_interval;
1775 	uint16_t c_latency;
1776 	uint16_t p_latency;
1777 	uint8_t status;
1778 	uint8_t i;
1779 
1780 	c_interval = sys_get_le24(cmd->c_interval);
1781 	p_interval = sys_get_le24(cmd->p_interval);
1782 	c_latency = sys_le16_to_cpu(cmd->c_latency);
1783 	p_latency = sys_le16_to_cpu(cmd->p_latency);
1784 
1785 	/* Create CIG or start modifying existing CIG */
1786 	status = ll_cig_parameters_open(cmd->cig_id, c_interval, p_interval,
1787 					cmd->sca, cmd->packing, cmd->framing,
1788 					c_latency, p_latency, cmd->num_cis);
1789 
1790 	rp = hci_cmd_complete(evt, sizeof(*rp) +
1791 				   cmd->num_cis * sizeof(uint16_t));
1792 	rp->cig_id = cmd->cig_id;
1793 	rp->num_handles = cmd->num_cis;
1794 
1795 	/* Configure individual CISes */
1796 	for (i = 0; !status && i < cmd->num_cis; i++) {
1797 		struct bt_hci_cis_params *params = cmd->cis;
1798 		uint16_t handle;
1799 		uint16_t c_sdu;
1800 		uint16_t p_sdu;
1801 
1802 		c_sdu = sys_le16_to_cpu(params->c_sdu);
1803 		p_sdu = sys_le16_to_cpu(params->p_sdu);
1804 
1805 		status = ll_cis_parameters_set(params->cis_id, c_sdu, p_sdu,
1806 					       params->c_phy, params->p_phy,
1807 					       params->c_rtn, params->p_rtn,
1808 					       &handle);
1809 		rp->handle[i] = sys_cpu_to_le16(handle);
1810 	}
1811 
1812 	/* Only apply parameters if all went well */
1813 	if (!status) {
1814 		status = ll_cig_parameters_commit(cmd->cig_id);
1815 	}
1816 
1817 	rp->status = status;
1818 }
1819 
le_set_cig_params_test(struct net_buf * buf,struct net_buf ** evt)1820 static void le_set_cig_params_test(struct net_buf *buf, struct net_buf **evt)
1821 {
1822 	struct bt_hci_cp_le_set_cig_params_test *cmd = (void *)buf->data;
1823 	struct bt_hci_rp_le_set_cig_params_test *rp;
1824 
1825 	uint32_t c_interval;
1826 	uint32_t p_interval;
1827 	uint16_t iso_interval;
1828 	uint8_t status;
1829 	uint8_t i;
1830 
1831 	c_interval = sys_get_le24(cmd->c_interval);
1832 	p_interval = sys_get_le24(cmd->p_interval);
1833 	iso_interval = sys_le16_to_cpu(cmd->iso_interval);
1834 
1835 	/* Create CIG or start modifying existing CIG */
1836 	status = ll_cig_parameters_test_open(cmd->cig_id, c_interval,
1837 					     p_interval, cmd->c_ft,
1838 					     cmd->p_ft, iso_interval,
1839 					     cmd->sca, cmd->packing,
1840 					     cmd->framing,
1841 					     cmd->num_cis);
1842 
1843 	rp = hci_cmd_complete(evt, sizeof(*rp) +
1844 				   cmd->num_cis * sizeof(uint16_t));
1845 	rp->cig_id = cmd->cig_id;
1846 	rp->num_handles = cmd->num_cis;
1847 
1848 	/* Configure individual CISes */
1849 	for (i = 0; !status && i < cmd->num_cis; i++) {
1850 		struct bt_hci_cis_params_test *params = cmd->cis;
1851 		uint16_t handle;
1852 		uint16_t c_sdu;
1853 		uint16_t p_sdu;
1854 		uint16_t c_pdu;
1855 		uint16_t p_pdu;
1856 
1857 		c_sdu = sys_le16_to_cpu(params->c_sdu);
1858 		p_sdu = sys_le16_to_cpu(params->p_sdu);
1859 		c_pdu = sys_le16_to_cpu(params->c_pdu);
1860 		p_pdu = sys_le16_to_cpu(params->p_pdu);
1861 
1862 		status = ll_cis_parameters_test_set(params->cis_id,
1863 						    c_sdu, p_sdu,
1864 						    c_pdu, p_pdu,
1865 						    params->c_phy,
1866 						    params->p_phy,
1867 						    params->c_bn,
1868 						    params->p_bn,
1869 						    &handle);
1870 		rp->handle[i] = sys_cpu_to_le16(handle);
1871 	}
1872 
1873 	/* Only apply parameters if all went well */
1874 	if (!status) {
1875 		status = ll_cig_parameters_commit(cmd->cig_id);
1876 	}
1877 
1878 	rp->status = status;
1879 }
1880 
le_create_cis(struct net_buf * buf,struct net_buf ** evt)1881 static void le_create_cis(struct net_buf *buf, struct net_buf **evt)
1882 {
1883 	struct bt_hci_cp_le_create_cis *cmd = (void *)buf->data;
1884 	uint8_t status;
1885 	uint8_t i;
1886 
1887 	/*
1888 	 * Creating new CISes is disallowed until all previous CIS
1889 	 * established events have been generated
1890 	 */
1891 	if (cis_pending_count) {
1892 		*evt = cmd_status(BT_HCI_ERR_CMD_DISALLOWED);
1893 		return;
1894 	}
1895 
1896 	/* Check all handles before actually starting to create CISes */
1897 	status = 0x00;
1898 	for (i = 0; !status && i < cmd->num_cis; i++) {
1899 		uint16_t cis_handle;
1900 		uint16_t acl_handle;
1901 
1902 		cis_handle = sys_le16_to_cpu(cmd->cis[i].cis_handle);
1903 		acl_handle = sys_le16_to_cpu(cmd->cis[i].acl_handle);
1904 		status = ll_cis_create_check(cis_handle, acl_handle);
1905 	}
1906 	*evt = cmd_status(status);
1907 
1908 	if (!status) {
1909 		return;
1910 	}
1911 
1912 	/*
1913 	 * Actually create CISes, any errors are to be reported
1914 	 * through CIS established events
1915 	 */
1916 	cis_pending_count = cmd->num_cis;
1917 	for (i = 0; i < cmd->num_cis; i++) {
1918 		uint16_t cis_handle;
1919 		uint16_t acl_handle;
1920 
1921 		cis_handle = sys_le16_to_cpu(cmd->cis[i].cis_handle);
1922 		acl_handle = sys_le16_to_cpu(cmd->cis[i].acl_handle);
1923 		ll_cis_create(cis_handle, acl_handle);
1924 	}
1925 }
1926 
le_remove_cig(struct net_buf * buf,struct net_buf ** evt)1927 static void le_remove_cig(struct net_buf *buf, struct net_buf **evt)
1928 {
1929 	struct bt_hci_cp_le_remove_cig *cmd = (void *)buf->data;
1930 	struct bt_hci_rp_le_remove_cig *rp;
1931 	uint8_t status;
1932 
1933 	status = ll_cig_remove(cmd->cig_id);
1934 
1935 	rp = hci_cmd_complete(evt, sizeof(*rp));
1936 	rp->status = status;
1937 	rp->cig_id = cmd->cig_id;
1938 }
1939 #endif /* CONFIG_BT_CTLR_CENTRAL_ISO */
1940 
1941 #endif /* CONFIG_BT_CENTRAL */
1942 
1943 #if defined(CONFIG_BT_CTLR_CONN_ISO)
le_read_iso_tx_sync(struct net_buf * buf,struct net_buf ** evt)1944 static void le_read_iso_tx_sync(struct net_buf *buf, struct net_buf **evt)
1945 {
1946 	struct bt_hci_cp_le_read_iso_tx_sync *cmd = (void *)buf->data;
1947 	struct bt_hci_rp_le_read_iso_tx_sync *rp;
1948 	uint8_t status;
1949 	uint16_t handle, handle_le16;
1950 
1951 	uint16_t seq;
1952 	uint32_t timestamp;
1953 	uint32_t offset;
1954 
1955 	handle_le16 = cmd->handle;
1956 	handle = sys_le16_to_cpu(handle_le16);
1957 
1958 	status = ll_read_iso_tx_sync(handle, &seq, &timestamp, &offset);
1959 
1960 	rp = hci_cmd_complete(evt, sizeof(*rp));
1961 	rp->status = status;
1962 	rp->handle = handle_le16;
1963 	rp->seq       = sys_cpu_to_le16(seq);
1964 	rp->timestamp = sys_cpu_to_le32(timestamp);
1965 	sys_put_le24(offset, rp->offset);
1966 }
1967 
le_read_iso_link_quality(struct net_buf * buf,struct net_buf ** evt)1968 static void le_read_iso_link_quality(struct net_buf *buf, struct net_buf **evt)
1969 {
1970 	struct bt_hci_cp_le_read_iso_link_quality *cmd = (void *)buf->data;
1971 	struct bt_hci_rp_le_read_iso_link_quality *rp;
1972 	uint8_t status;
1973 	uint16_t handle, handle_le16;
1974 
1975 	uint32_t tx_unacked_packets;
1976 	uint32_t tx_flushed_packets;
1977 	uint32_t tx_last_subevent_packets;
1978 	uint32_t retransmitted_packets;
1979 	uint32_t crc_error_packets;
1980 	uint32_t rx_unreceived_packets;
1981 	uint32_t duplicate_packets;
1982 
1983 	handle_le16 = cmd->handle;
1984 	handle = sys_le16_to_cpu(handle_le16);
1985 	status = ll_read_iso_link_quality(handle, &tx_unacked_packets,
1986 					  &tx_flushed_packets,
1987 					  &tx_last_subevent_packets,
1988 					  &retransmitted_packets,
1989 					  &crc_error_packets,
1990 					  &rx_unreceived_packets,
1991 					  &duplicate_packets);
1992 
1993 	rp = hci_cmd_complete(evt, sizeof(*rp));
1994 	rp->status = status;
1995 	rp->handle = handle_le16;
1996 	rp->tx_unacked_packets = sys_cpu_to_le32(tx_unacked_packets);
1997 	rp->tx_flushed_packets = sys_cpu_to_le32(tx_flushed_packets);
1998 	rp->tx_last_subevent_packets =
1999 		sys_cpu_to_le32(tx_last_subevent_packets);
2000 	rp->retransmitted_packets = sys_cpu_to_le32(retransmitted_packets);
2001 	rp->crc_error_packets     = sys_cpu_to_le32(crc_error_packets);
2002 	rp->rx_unreceived_packets = sys_cpu_to_le32(rx_unreceived_packets);
2003 	rp->duplicate_packets     = sys_cpu_to_le32(duplicate_packets);
2004 }
2005 
le_setup_iso_path(struct net_buf * buf,struct net_buf ** evt)2006 static void le_setup_iso_path(struct net_buf *buf, struct net_buf **evt)
2007 {
2008 	struct bt_hci_cp_le_setup_iso_path *cmd = (void *)buf->data;
2009 	struct bt_hci_rp_le_setup_iso_path *rp;
2010 	uint32_t controller_delay;
2011 	uint8_t *codec_config;
2012 	uint8_t coding_format;
2013 	uint16_t vs_codec_id;
2014 	uint16_t company_id;
2015 	uint16_t handle;
2016 	uint8_t status;
2017 
2018 	handle = sys_le16_to_cpu(cmd->handle);
2019 	coding_format = cmd->codec_id.coding_format;
2020 	company_id = sys_le16_to_cpu(cmd->codec_id.company_id);
2021 	vs_codec_id = sys_le16_to_cpu(cmd->codec_id.vs_codec_id);
2022 	controller_delay = sys_get_le24(cmd->controller_delay);
2023 	codec_config = &cmd->codec_config[0];
2024 
2025 	status = ll_setup_iso_path(handle, cmd->path_dir, cmd->path_id,
2026 				   coding_format, company_id, vs_codec_id,
2027 				   controller_delay, cmd->codec_config_len,
2028 				   codec_config);
2029 
2030 	rp = hci_cmd_complete(evt, sizeof(*rp));
2031 	rp->status = status;
2032 	rp->handle = sys_cpu_to_le16(handle);
2033 }
2034 
le_remove_iso_path(struct net_buf * buf,struct net_buf ** evt)2035 static void le_remove_iso_path(struct net_buf *buf, struct net_buf **evt)
2036 {
2037 	struct bt_hci_cp_le_remove_iso_path *cmd = (void *)buf->data;
2038 	struct bt_hci_rp_le_remove_iso_path *rp;
2039 	uint8_t status;
2040 	uint16_t handle;
2041 
2042 	handle = sys_le16_to_cpu(cmd->handle);
2043 
2044 	status = ll_remove_iso_path(handle, cmd->path_dir);
2045 
2046 	rp = hci_cmd_complete(evt, sizeof(*rp));
2047 	rp->status = status;
2048 	rp->handle = sys_cpu_to_le16(handle);
2049 }
2050 
le_iso_receive_test(struct net_buf * buf,struct net_buf ** evt)2051 static void le_iso_receive_test(struct net_buf *buf, struct net_buf **evt)
2052 {
2053 	struct bt_hci_cp_le_iso_receive_test *cmd = (void *)buf->data;
2054 	struct bt_hci_rp_le_iso_receive_test *rp;
2055 	uint8_t status;
2056 	uint16_t handle;
2057 
2058 	handle = sys_le16_to_cpu(cmd->handle);
2059 
2060 	status = ll_iso_receive_test(handle, cmd->payload_type);
2061 
2062 	rp = hci_cmd_complete(evt, sizeof(*rp));
2063 	rp->status = status;
2064 	rp->handle = cmd->handle;
2065 }
2066 
le_iso_transmit_test(struct net_buf * buf,struct net_buf ** evt)2067 static void le_iso_transmit_test(struct net_buf *buf, struct net_buf **evt)
2068 {
2069 	struct bt_hci_cp_le_iso_transmit_test *cmd = (void *)buf->data;
2070 	struct bt_hci_rp_le_iso_transmit_test *rp;
2071 	uint8_t status;
2072 	uint16_t handle;
2073 
2074 	handle = sys_le16_to_cpu(cmd->handle);
2075 
2076 	status = ll_iso_transmit_test(handle, cmd->payload_type);
2077 
2078 	rp = hci_cmd_complete(evt, sizeof(*rp));
2079 	rp->status = status;
2080 	rp->handle = cmd->handle;
2081 }
2082 
le_iso_test_end(struct net_buf * buf,struct net_buf ** evt)2083 static void le_iso_test_end(struct net_buf *buf, struct net_buf **evt)
2084 {
2085 	struct bt_hci_cp_le_iso_test_end *cmd = (void *)buf->data;
2086 	struct bt_hci_rp_le_iso_test_end *rp;
2087 	uint8_t status;
2088 	uint16_t handle;
2089 	uint32_t received_cnt;
2090 	uint32_t missed_cnt;
2091 	uint32_t failed_cnt;
2092 
2093 	handle = sys_le16_to_cpu(cmd->handle);
2094 	status = ll_iso_test_end(handle, &received_cnt, &missed_cnt,
2095 				 &failed_cnt);
2096 
2097 	rp = hci_cmd_complete(evt, sizeof(*rp));
2098 	rp->status = status;
2099 	rp->handle = cmd->handle;
2100 	rp->received_cnt = sys_cpu_to_le32(received_cnt);
2101 	rp->missed_cnt   = sys_cpu_to_le32(missed_cnt);
2102 	rp->failed_cnt   = sys_cpu_to_le32(failed_cnt);
2103 }
2104 
le_iso_read_test_counters(struct net_buf * buf,struct net_buf ** evt)2105 static void le_iso_read_test_counters(struct net_buf *buf, struct net_buf **evt)
2106 {
2107 	struct bt_hci_cp_le_read_test_counters *cmd = (void *)buf->data;
2108 	struct bt_hci_rp_le_read_test_counters *rp;
2109 	uint8_t status;
2110 	uint16_t handle;
2111 	uint32_t received_cnt;
2112 	uint32_t missed_cnt;
2113 	uint32_t failed_cnt;
2114 
2115 	handle = sys_le16_to_cpu(cmd->handle);
2116 	status = ll_iso_read_test_counters(handle, &received_cnt,
2117 					   &missed_cnt, &failed_cnt);
2118 
2119 	rp = hci_cmd_complete(evt, sizeof(*rp));
2120 	rp->status = status;
2121 	rp->handle = cmd->handle;
2122 	rp->received_cnt = sys_cpu_to_le32(received_cnt);
2123 	rp->missed_cnt   = sys_cpu_to_le32(missed_cnt);
2124 	rp->failed_cnt   = sys_cpu_to_le32(failed_cnt);
2125 }
2126 #endif /* CONFIG_BT_CTLR_CONN_ISO */
2127 
2128 #if defined(CONFIG_BT_CTLR_SET_HOST_FEATURE)
le_set_host_feature(struct net_buf * buf,struct net_buf ** evt)2129 static void le_set_host_feature(struct net_buf *buf, struct net_buf **evt)
2130 {
2131 	struct bt_hci_cp_le_set_host_feature *cmd = (void *)buf->data;
2132 	struct bt_hci_rp_le_set_host_feature *rp;
2133 	uint8_t status;
2134 
2135 	status = ll_set_host_feature(cmd->bit_number, cmd->bit_value);
2136 
2137 	rp = hci_cmd_complete(evt, sizeof(*rp));
2138 	rp->status = status;
2139 }
2140 #endif /* CONFIG_BT_CTLR_SET_HOST_FEATURE */
2141 
2142 #if defined(CONFIG_BT_PERIPHERAL)
2143 #if defined(CONFIG_BT_CTLR_LE_ENC)
le_ltk_req_reply(struct net_buf * buf,struct net_buf ** evt)2144 static void le_ltk_req_reply(struct net_buf *buf, struct net_buf **evt)
2145 {
2146 	struct bt_hci_cp_le_ltk_req_reply *cmd = (void *)buf->data;
2147 	struct bt_hci_rp_le_ltk_req_reply *rp;
2148 	uint16_t handle;
2149 	uint8_t status;
2150 
2151 	handle = sys_le16_to_cpu(cmd->handle);
2152 	status = ll_start_enc_req_send(handle, 0x00, &cmd->ltk[0]);
2153 
2154 	rp = hci_cmd_complete(evt, sizeof(*rp));
2155 	rp->status = status;
2156 	rp->handle = sys_cpu_to_le16(handle);
2157 }
2158 
le_ltk_req_neg_reply(struct net_buf * buf,struct net_buf ** evt)2159 static void le_ltk_req_neg_reply(struct net_buf *buf, struct net_buf **evt)
2160 {
2161 	struct bt_hci_cp_le_ltk_req_neg_reply *cmd = (void *)buf->data;
2162 	struct bt_hci_rp_le_ltk_req_neg_reply *rp;
2163 	uint16_t handle;
2164 	uint8_t status;
2165 
2166 	handle = sys_le16_to_cpu(cmd->handle);
2167 	status = ll_start_enc_req_send(handle, BT_HCI_ERR_PIN_OR_KEY_MISSING,
2168 				       NULL);
2169 
2170 	rp = hci_cmd_complete(evt, sizeof(*rp));
2171 	rp->status = status;
2172 	rp->handle = sys_le16_to_cpu(handle);
2173 }
2174 #endif /* CONFIG_BT_CTLR_LE_ENC */
2175 
2176 #if defined(CONFIG_BT_CTLR_PERIPHERAL_ISO)
le_accept_cis(struct net_buf * buf,struct net_buf ** evt)2177 static void le_accept_cis(struct net_buf *buf, struct net_buf **evt)
2178 {
2179 	struct bt_hci_cp_le_accept_cis *cmd = (void *)buf->data;
2180 	uint16_t handle;
2181 	uint8_t status;
2182 
2183 	handle = sys_le16_to_cpu(cmd->handle);
2184 	status = ll_cis_accept(handle);
2185 	*evt = cmd_status(status);
2186 }
2187 
le_reject_cis(struct net_buf * buf,struct net_buf ** evt)2188 static void le_reject_cis(struct net_buf *buf, struct net_buf **evt)
2189 {
2190 	struct bt_hci_cp_le_reject_cis *cmd = (void *)buf->data;
2191 	struct bt_hci_rp_le_reject_cis *rp;
2192 	uint16_t handle;
2193 	uint8_t status;
2194 
2195 	handle = sys_le16_to_cpu(cmd->handle);
2196 	status = ll_cis_reject(handle, cmd->reason);
2197 
2198 	rp = hci_cmd_complete(evt, sizeof(*rp));
2199 	rp->status = status;
2200 	rp->handle = sys_cpu_to_le16(handle);
2201 }
2202 #endif /* CONFIG_BT_CTLR_PERIPHERAL_ISO */
2203 
2204 #endif /* CONFIG_BT_PERIPHERAL */
2205 
le_read_remote_features(struct net_buf * buf,struct net_buf ** evt)2206 static void le_read_remote_features(struct net_buf *buf, struct net_buf **evt)
2207 {
2208 	struct bt_hci_cp_le_read_remote_features *cmd = (void *)buf->data;
2209 	uint16_t handle;
2210 	uint8_t status;
2211 
2212 	handle = sys_le16_to_cpu(cmd->handle);
2213 	status = ll_feature_req_send(handle);
2214 
2215 	*evt = cmd_status(status);
2216 }
2217 
le_read_chan_map(struct net_buf * buf,struct net_buf ** evt)2218 static void le_read_chan_map(struct net_buf *buf, struct net_buf **evt)
2219 {
2220 	struct bt_hci_cp_le_read_chan_map *cmd = (void *)buf->data;
2221 	struct bt_hci_rp_le_read_chan_map *rp;
2222 	uint16_t handle;
2223 	uint8_t status;
2224 
2225 	handle = sys_le16_to_cpu(cmd->handle);
2226 
2227 	rp = hci_cmd_complete(evt, sizeof(*rp));
2228 
2229 	status = ll_chm_get(handle, rp->ch_map);
2230 
2231 	rp->status = status;
2232 	rp->handle = sys_le16_to_cpu(handle);
2233 }
2234 
le_conn_update(struct net_buf * buf,struct net_buf ** evt)2235 static void le_conn_update(struct net_buf *buf, struct net_buf **evt)
2236 {
2237 	struct hci_cp_le_conn_update *cmd = (void *)buf->data;
2238 	uint16_t supervision_timeout;
2239 	uint16_t conn_interval_min;
2240 	uint16_t conn_interval_max;
2241 	uint16_t conn_latency;
2242 	uint16_t handle;
2243 	uint8_t status;
2244 
2245 	handle = sys_le16_to_cpu(cmd->handle);
2246 	conn_interval_min = sys_le16_to_cpu(cmd->conn_interval_min);
2247 	conn_interval_max = sys_le16_to_cpu(cmd->conn_interval_max);
2248 	conn_latency = sys_le16_to_cpu(cmd->conn_latency);
2249 	supervision_timeout = sys_le16_to_cpu(cmd->supervision_timeout);
2250 
2251 	status = ll_conn_update(handle, 0, 0, conn_interval_min,
2252 				conn_interval_max, conn_latency,
2253 				supervision_timeout);
2254 
2255 	*evt = cmd_status(status);
2256 }
2257 
2258 #if defined(CONFIG_BT_CTLR_CONN_PARAM_REQ)
le_conn_param_req_reply(struct net_buf * buf,struct net_buf ** evt)2259 static void le_conn_param_req_reply(struct net_buf *buf, struct net_buf **evt)
2260 {
2261 	struct bt_hci_cp_le_conn_param_req_reply *cmd = (void *)buf->data;
2262 	struct bt_hci_rp_le_conn_param_req_reply *rp;
2263 	uint16_t interval_min;
2264 	uint16_t interval_max;
2265 	uint16_t latency;
2266 	uint16_t timeout;
2267 	uint16_t handle;
2268 	uint8_t status;
2269 
2270 	handle = sys_le16_to_cpu(cmd->handle);
2271 	interval_min = sys_le16_to_cpu(cmd->interval_min);
2272 	interval_max = sys_le16_to_cpu(cmd->interval_max);
2273 	latency = sys_le16_to_cpu(cmd->latency);
2274 	timeout = sys_le16_to_cpu(cmd->timeout);
2275 
2276 	status = ll_conn_update(handle, 2, 0, interval_min, interval_max,
2277 				latency, timeout);
2278 
2279 	rp = hci_cmd_complete(evt, sizeof(*rp));
2280 	rp->status = status;
2281 	rp->handle = sys_cpu_to_le16(handle);
2282 }
2283 
le_conn_param_req_neg_reply(struct net_buf * buf,struct net_buf ** evt)2284 static void le_conn_param_req_neg_reply(struct net_buf *buf,
2285 					struct net_buf **evt)
2286 {
2287 	struct bt_hci_cp_le_conn_param_req_neg_reply *cmd = (void *)buf->data;
2288 	struct bt_hci_rp_le_conn_param_req_neg_reply *rp;
2289 	uint16_t handle;
2290 	uint8_t status;
2291 
2292 	handle = sys_le16_to_cpu(cmd->handle);
2293 	status = ll_conn_update(handle, 2, cmd->reason, 0, 0, 0, 0);
2294 
2295 	rp = hci_cmd_complete(evt, sizeof(*rp));
2296 	rp->status = status;
2297 	rp->handle = sys_cpu_to_le16(handle);
2298 }
2299 #endif /* CONFIG_BT_CTLR_CONN_PARAM_REQ */
2300 
2301 #if defined(CONFIG_BT_CTLR_DATA_LENGTH)
le_set_data_len(struct net_buf * buf,struct net_buf ** evt)2302 static void le_set_data_len(struct net_buf *buf, struct net_buf **evt)
2303 {
2304 	struct bt_hci_cp_le_set_data_len *cmd = (void *)buf->data;
2305 	struct bt_hci_rp_le_set_data_len *rp;
2306 	uint16_t tx_octets;
2307 	uint16_t tx_time;
2308 	uint16_t handle;
2309 	uint8_t status;
2310 
2311 	handle = sys_le16_to_cpu(cmd->handle);
2312 	tx_octets = sys_le16_to_cpu(cmd->tx_octets);
2313 	tx_time = sys_le16_to_cpu(cmd->tx_time);
2314 	status = ll_length_req_send(handle, tx_octets, tx_time);
2315 
2316 	rp = hci_cmd_complete(evt, sizeof(*rp));
2317 	rp->status = status;
2318 	rp->handle = sys_cpu_to_le16(handle);
2319 }
2320 
le_read_default_data_len(struct net_buf * buf,struct net_buf ** evt)2321 static void le_read_default_data_len(struct net_buf *buf, struct net_buf **evt)
2322 {
2323 	struct bt_hci_rp_le_read_default_data_len *rp;
2324 	uint16_t max_tx_octets;
2325 	uint16_t max_tx_time;
2326 
2327 	rp = hci_cmd_complete(evt, sizeof(*rp));
2328 
2329 	ll_length_default_get(&max_tx_octets, &max_tx_time);
2330 
2331 	rp->max_tx_octets = sys_cpu_to_le16(max_tx_octets);
2332 	rp->max_tx_time = sys_cpu_to_le16(max_tx_time);
2333 	rp->status = 0x00;
2334 }
2335 
le_write_default_data_len(struct net_buf * buf,struct net_buf ** evt)2336 static void le_write_default_data_len(struct net_buf *buf,
2337 				      struct net_buf **evt)
2338 {
2339 	struct bt_hci_cp_le_write_default_data_len *cmd = (void *)buf->data;
2340 	uint16_t max_tx_octets;
2341 	uint16_t max_tx_time;
2342 	uint8_t status;
2343 
2344 	max_tx_octets = sys_le16_to_cpu(cmd->max_tx_octets);
2345 	max_tx_time = sys_le16_to_cpu(cmd->max_tx_time);
2346 	status = ll_length_default_set(max_tx_octets, max_tx_time);
2347 
2348 	*evt = cmd_complete_status(status);
2349 }
2350 
le_read_max_data_len(struct net_buf * buf,struct net_buf ** evt)2351 static void le_read_max_data_len(struct net_buf *buf, struct net_buf **evt)
2352 {
2353 	struct bt_hci_rp_le_read_max_data_len *rp;
2354 	uint16_t max_tx_octets;
2355 	uint16_t max_tx_time;
2356 	uint16_t max_rx_octets;
2357 	uint16_t max_rx_time;
2358 
2359 	rp = hci_cmd_complete(evt, sizeof(*rp));
2360 
2361 	ll_length_max_get(&max_tx_octets, &max_tx_time,
2362 			  &max_rx_octets, &max_rx_time);
2363 
2364 	rp->max_tx_octets = sys_cpu_to_le16(max_tx_octets);
2365 	rp->max_tx_time = sys_cpu_to_le16(max_tx_time);
2366 	rp->max_rx_octets = sys_cpu_to_le16(max_rx_octets);
2367 	rp->max_rx_time = sys_cpu_to_le16(max_rx_time);
2368 	rp->status = 0x00;
2369 }
2370 #endif /* CONFIG_BT_CTLR_DATA_LENGTH */
2371 
2372 #if defined(CONFIG_BT_CTLR_PHY)
le_read_phy(struct net_buf * buf,struct net_buf ** evt)2373 static void le_read_phy(struct net_buf *buf, struct net_buf **evt)
2374 {
2375 	struct bt_hci_cp_le_read_phy *cmd = (void *)buf->data;
2376 	struct bt_hci_rp_le_read_phy *rp;
2377 	uint16_t handle;
2378 	uint8_t status;
2379 
2380 	handle = sys_le16_to_cpu(cmd->handle);
2381 
2382 	rp = hci_cmd_complete(evt, sizeof(*rp));
2383 
2384 	status = ll_phy_get(handle, &rp->tx_phy, &rp->rx_phy);
2385 
2386 	rp->status = status;
2387 	rp->handle = sys_cpu_to_le16(handle);
2388 	rp->tx_phy = find_lsb_set(rp->tx_phy);
2389 	rp->rx_phy = find_lsb_set(rp->rx_phy);
2390 }
2391 
le_set_default_phy(struct net_buf * buf,struct net_buf ** evt)2392 static void le_set_default_phy(struct net_buf *buf, struct net_buf **evt)
2393 {
2394 	struct bt_hci_cp_le_set_default_phy *cmd = (void *)buf->data;
2395 	uint8_t status;
2396 
2397 	if (cmd->all_phys & BT_HCI_LE_PHY_TX_ANY) {
2398 		cmd->tx_phys = 0x07;
2399 	}
2400 	if (cmd->all_phys & BT_HCI_LE_PHY_RX_ANY) {
2401 		cmd->rx_phys = 0x07;
2402 	}
2403 
2404 	status = ll_phy_default_set(cmd->tx_phys, cmd->rx_phys);
2405 
2406 	*evt = cmd_complete_status(status);
2407 }
2408 
le_set_phy(struct net_buf * buf,struct net_buf ** evt)2409 static void le_set_phy(struct net_buf *buf, struct net_buf **evt)
2410 {
2411 	struct bt_hci_cp_le_set_phy *cmd = (void *)buf->data;
2412 	uint16_t phy_opts;
2413 	uint8_t mask_phys;
2414 	uint16_t handle;
2415 	uint8_t status;
2416 
2417 	handle = sys_le16_to_cpu(cmd->handle);
2418 	phy_opts = sys_le16_to_cpu(cmd->phy_opts);
2419 
2420 	mask_phys = BT_HCI_LE_PHY_PREFER_1M;
2421 	if (IS_ENABLED(CONFIG_BT_CTLR_PHY_2M)) {
2422 		mask_phys |= BT_HCI_LE_PHY_PREFER_2M;
2423 	}
2424 	if (IS_ENABLED(CONFIG_BT_CTLR_PHY_CODED)) {
2425 		mask_phys |= BT_HCI_LE_PHY_PREFER_CODED;
2426 	}
2427 
2428 	if (cmd->all_phys & BT_HCI_LE_PHY_TX_ANY) {
2429 		cmd->tx_phys |= mask_phys;
2430 	}
2431 	if (cmd->all_phys & BT_HCI_LE_PHY_RX_ANY) {
2432 		cmd->rx_phys |= mask_phys;
2433 	}
2434 
2435 	if ((cmd->tx_phys | cmd->rx_phys) & ~mask_phys) {
2436 		*evt = cmd_status(BT_HCI_ERR_UNSUPP_FEATURE_PARAM_VAL);
2437 
2438 		return;
2439 	}
2440 
2441 	if (!(cmd->tx_phys & 0x07) ||
2442 	    !(cmd->rx_phys & 0x07)) {
2443 		*evt = cmd_status(BT_HCI_ERR_INVALID_PARAM);
2444 
2445 		return;
2446 	}
2447 
2448 	if (phy_opts & 0x03) {
2449 		phy_opts -= 1U;
2450 		phy_opts &= 1;
2451 	} else {
2452 		phy_opts = 0U;
2453 	}
2454 
2455 	status = ll_phy_req_send(handle, cmd->tx_phys, phy_opts,
2456 				 cmd->rx_phys);
2457 
2458 	*evt = cmd_status(status);
2459 }
2460 #endif /* CONFIG_BT_CTLR_PHY */
2461 #endif /* CONFIG_BT_CONN */
2462 
2463 #if defined(CONFIG_BT_CTLR_PRIVACY)
le_add_dev_to_rl(struct net_buf * buf,struct net_buf ** evt)2464 static void le_add_dev_to_rl(struct net_buf *buf, struct net_buf **evt)
2465 {
2466 	struct bt_hci_cp_le_add_dev_to_rl *cmd = (void *)buf->data;
2467 	uint8_t status;
2468 
2469 	status = ll_rl_add(&cmd->peer_id_addr, cmd->peer_irk, cmd->local_irk);
2470 
2471 	*evt = cmd_complete_status(status);
2472 }
2473 
le_rem_dev_from_rl(struct net_buf * buf,struct net_buf ** evt)2474 static void le_rem_dev_from_rl(struct net_buf *buf, struct net_buf **evt)
2475 {
2476 	struct bt_hci_cp_le_rem_dev_from_rl *cmd = (void *)buf->data;
2477 	uint8_t status;
2478 
2479 	status = ll_rl_remove(&cmd->peer_id_addr);
2480 
2481 	*evt = cmd_complete_status(status);
2482 }
2483 
le_clear_rl(struct net_buf * buf,struct net_buf ** evt)2484 static void le_clear_rl(struct net_buf *buf, struct net_buf **evt)
2485 {
2486 	uint8_t status;
2487 
2488 	status = ll_rl_clear();
2489 
2490 	*evt = cmd_complete_status(status);
2491 }
2492 
le_read_rl_size(struct net_buf * buf,struct net_buf ** evt)2493 static void le_read_rl_size(struct net_buf *buf, struct net_buf **evt)
2494 {
2495 	struct bt_hci_rp_le_read_rl_size *rp;
2496 
2497 	rp = hci_cmd_complete(evt, sizeof(*rp));
2498 
2499 	rp->rl_size = ll_rl_size_get();
2500 	rp->status = 0x00;
2501 }
2502 
le_read_peer_rpa(struct net_buf * buf,struct net_buf ** evt)2503 static void le_read_peer_rpa(struct net_buf *buf, struct net_buf **evt)
2504 {
2505 	struct bt_hci_cp_le_read_peer_rpa *cmd = (void *)buf->data;
2506 	struct bt_hci_rp_le_read_peer_rpa *rp;
2507 	bt_addr_le_t peer_id_addr;
2508 
2509 	bt_addr_le_copy(&peer_id_addr, &cmd->peer_id_addr);
2510 	rp = hci_cmd_complete(evt, sizeof(*rp));
2511 
2512 	rp->status = ll_rl_crpa_get(&peer_id_addr, &rp->peer_rpa);
2513 }
2514 
le_read_local_rpa(struct net_buf * buf,struct net_buf ** evt)2515 static void le_read_local_rpa(struct net_buf *buf, struct net_buf **evt)
2516 {
2517 	struct bt_hci_cp_le_read_local_rpa *cmd = (void *)buf->data;
2518 	struct bt_hci_rp_le_read_local_rpa *rp;
2519 	bt_addr_le_t peer_id_addr;
2520 
2521 	bt_addr_le_copy(&peer_id_addr, &cmd->peer_id_addr);
2522 	rp = hci_cmd_complete(evt, sizeof(*rp));
2523 
2524 	rp->status = ll_rl_lrpa_get(&peer_id_addr, &rp->local_rpa);
2525 }
2526 
le_set_addr_res_enable(struct net_buf * buf,struct net_buf ** evt)2527 static void le_set_addr_res_enable(struct net_buf *buf, struct net_buf **evt)
2528 {
2529 	struct bt_hci_cp_le_set_addr_res_enable *cmd = (void *)buf->data;
2530 	uint8_t status;
2531 
2532 	status = ll_rl_enable(cmd->enable);
2533 
2534 	*evt = cmd_complete_status(status);
2535 }
2536 
le_set_rpa_timeout(struct net_buf * buf,struct net_buf ** evt)2537 static void le_set_rpa_timeout(struct net_buf *buf, struct net_buf **evt)
2538 {
2539 	struct bt_hci_cp_le_set_rpa_timeout *cmd = (void *)buf->data;
2540 	uint16_t timeout = sys_le16_to_cpu(cmd->rpa_timeout);
2541 
2542 	ll_rl_timeout_set(timeout);
2543 
2544 	*evt = cmd_complete_status(0x00);
2545 }
2546 
le_set_privacy_mode(struct net_buf * buf,struct net_buf ** evt)2547 static void le_set_privacy_mode(struct net_buf *buf, struct net_buf **evt)
2548 {
2549 	struct bt_hci_cp_le_set_privacy_mode *cmd = (void *)buf->data;
2550 	uint8_t status;
2551 
2552 	status = ll_priv_mode_set(&cmd->id_addr, cmd->mode);
2553 
2554 	*evt = cmd_complete_status(status);
2555 }
2556 #endif /* CONFIG_BT_CTLR_PRIVACY */
2557 
le_read_tx_power(struct net_buf * buf,struct net_buf ** evt)2558 static void le_read_tx_power(struct net_buf *buf, struct net_buf **evt)
2559 {
2560 	struct bt_hci_rp_le_read_tx_power *rp;
2561 
2562 	rp = hci_cmd_complete(evt, sizeof(*rp));
2563 	rp->status = 0x00;
2564 	ll_tx_pwr_get(&rp->min_tx_power, &rp->max_tx_power);
2565 }
2566 
2567 #if defined(CONFIG_BT_CTLR_DF)
2568 #if defined(CONFIG_BT_CTLR_DF_ADV_CTE_TX)
le_df_set_cl_cte_tx_params(struct net_buf * buf,struct net_buf ** evt)2569 static void le_df_set_cl_cte_tx_params(struct net_buf *buf,
2570 				       struct net_buf **evt)
2571 {
2572 	struct bt_hci_cp_le_set_cl_cte_tx_params *cmd = (void *)buf->data;
2573 	uint8_t adv_handle;
2574 	uint8_t status;
2575 
2576 	if (adv_cmds_ext_check(evt)) {
2577 		return;
2578 	}
2579 
2580 	status = ll_adv_set_by_hci_handle_get(cmd->handle, &adv_handle);
2581 	if (status) {
2582 		*evt = cmd_complete_status(status);
2583 		return;
2584 	}
2585 
2586 	status = ll_df_set_cl_cte_tx_params(adv_handle, cmd->cte_len,
2587 					    cmd->cte_type, cmd->cte_count,
2588 					    cmd->switch_pattern_len,
2589 					    cmd->ant_ids);
2590 
2591 	*evt = cmd_complete_status(status);
2592 }
2593 
le_df_set_cl_cte_enable(struct net_buf * buf,struct net_buf ** evt)2594 static void le_df_set_cl_cte_enable(struct net_buf *buf, struct net_buf **evt)
2595 {
2596 	struct bt_hci_cp_le_set_cl_cte_tx_enable *cmd = (void *)buf->data;
2597 	uint8_t status;
2598 	uint8_t handle;
2599 
2600 	if (adv_cmds_ext_check(evt)) {
2601 		return;
2602 	}
2603 
2604 	status = ll_adv_set_by_hci_handle_get(cmd->handle, &handle);
2605 	if (status) {
2606 		*evt = cmd_complete_status(status);
2607 		return;
2608 	}
2609 
2610 	status = ll_df_set_cl_cte_tx_enable(handle, cmd->cte_enable);
2611 
2612 	*evt = cmd_complete_status(status);
2613 }
2614 #endif /* CONFIG_BT_CTLR_DF_ADV_CTE_TX */
2615 
2616 #if defined(CONFIG_BT_CTLR_DF_SCAN_CTE_RX)
le_df_set_cl_iq_sampling_enable(struct net_buf * buf,struct net_buf ** evt)2617 static void le_df_set_cl_iq_sampling_enable(struct net_buf *buf, struct net_buf **evt)
2618 {
2619 	struct bt_hci_cp_le_set_cl_cte_sampling_enable *cmd = (void *)buf->data;
2620 	struct bt_hci_rp_le_set_cl_cte_sampling_enable *rp;
2621 	uint16_t sync_handle;
2622 	uint8_t status;
2623 
2624 	sync_handle = sys_le16_to_cpu(cmd->sync_handle);
2625 
2626 	status = ll_df_set_cl_iq_sampling_enable(sync_handle,
2627 						 cmd->sampling_enable,
2628 						 cmd->slot_durations,
2629 						 cmd->max_sampled_cte,
2630 						 cmd->switch_pattern_len,
2631 						 cmd->ant_ids);
2632 
2633 	rp = hci_cmd_complete(evt, sizeof(*rp));
2634 
2635 	rp->status = status;
2636 	rp->sync_handle = sys_cpu_to_le16(sync_handle);
2637 }
2638 
le_df_connectionless_iq_report(struct pdu_data * pdu_rx,struct node_rx_pdu * node_rx,struct net_buf * buf)2639 static void le_df_connectionless_iq_report(struct pdu_data *pdu_rx,
2640 					   struct node_rx_pdu *node_rx,
2641 					   struct net_buf *buf)
2642 {
2643 	struct bt_hci_evt_le_connectionless_iq_report *sep;
2644 	struct node_rx_iq_report *iq_report;
2645 	struct ll_sync_set *sync;
2646 	struct lll_sync *lll;
2647 	uint8_t samples_cnt;
2648 	int16_t iq_tmp;
2649 	int16_t rssi;
2650 	uint8_t idx;
2651 
2652 	iq_report =  (struct node_rx_iq_report *)node_rx;
2653 
2654 	if (!(event_mask & BT_EVT_MASK_LE_META_EVENT) ||
2655 	    !(le_event_mask & BT_EVT_MASK_LE_CONNECTIONLESS_IQ_REPORT)) {
2656 		return;
2657 	}
2658 
2659 	lll = iq_report->hdr.rx_ftr.param;
2660 
2661 	/* TX LL thread has higher priority than RX thread. It may happen that
2662 	 * host succefully disables CTE sampling in the meantime.
2663 	 * It should be verified here, to avoid reporint IQ samples after
2664 	 * the functionality was disabled.
2665 	 */
2666 	if (ull_df_sync_cfg_is_disabled_or_requested_to_disable(&lll->df_cfg)) {
2667 		/* Dropp further processing of the event. */
2668 		return;
2669 	}
2670 
2671 	/* If there are no IQ samples due to insufficient resources
2672 	 * HCI event should inform about it by store single octet with
2673 	 * special I_sample and Q_sample data.
2674 	 */
2675 	samples_cnt = (!iq_report->sample_count ? 1 : iq_report->sample_count);
2676 
2677 	sep = meta_evt(buf, BT_HCI_EVT_LE_CONNECTIONLESS_IQ_REPORT,
2678 		       (sizeof(*sep) +
2679 			(samples_cnt * sizeof(struct bt_hci_le_iq_sample))));
2680 
2681 	rssi = RSSI_DBM_TO_DECI_DBM(iq_report->hdr.rx_ftr.rssi);
2682 
2683 	/* Get the sync handle corresponding to the LLL context passed in the
2684 	 * node rx footer field.
2685 	 */
2686 	sync = HDR_LLL2ULL(lll);
2687 
2688 	sep->sync_handle = sys_cpu_to_le16(ull_sync_handle_get(sync));
2689 	sep->rssi = sys_cpu_to_le16(rssi);
2690 	sep->rssi_ant_id = iq_report->rssi_ant_id;
2691 	sep->cte_type = iq_report->cte_info.type;
2692 
2693 	sep->chan_idx = iq_report->chan_idx;
2694 	sep->per_evt_counter = sys_cpu_to_le16(lll->event_counter);
2695 
2696 	if (sep->cte_type == BT_HCI_LE_AOA_CTE) {
2697 		sep->slot_durations = iq_report->local_slot_durations;
2698 	} else if (sep->cte_type == BT_HCI_LE_AOD_CTE_1US) {
2699 		sep->slot_durations = BT_HCI_LE_ANTENNA_SWITCHING_SLOT_1US;
2700 	} else {
2701 		sep->slot_durations = BT_HCI_LE_ANTENNA_SWITCHING_SLOT_2US;
2702 	}
2703 
2704 	sep->packet_status = iq_report->packet_status;
2705 
2706 	if (iq_report->packet_status == BT_HCI_LE_CTE_INSUFFICIENT_RESOURCES) {
2707 		sep->sample[0].i = BT_HCI_LE_CTE_REPORT_NO_VALID_SAMPLE;
2708 		sep->sample[0].q = BT_HCI_LE_CTE_REPORT_NO_VALID_SAMPLE;
2709 		sep->sample_count = 0;
2710 	} else {
2711 		for (idx = 0; idx < samples_cnt; ++idx) {
2712 			iq_tmp = IQ_SHIFT_12_TO_8_BIT(iq_report->sample[idx].i);
2713 			sep->sample[idx].i = (int8_t)iq_tmp;
2714 			iq_tmp = IQ_SHIFT_12_TO_8_BIT(iq_report->sample[idx].q);
2715 			sep->sample[idx].q = (int8_t)iq_tmp;
2716 		}
2717 		sep->sample_count = samples_cnt;
2718 	}
2719 }
2720 #endif /* CONFIG_BT_CTLR_DF_SCAN_CTE_RX */
2721 
2722 #if defined(CONFIG_BT_CTLR_DF_CONN_CTE_RSP)
le_df_set_conn_cte_tx_params(struct net_buf * buf,struct net_buf ** evt)2723 static void le_df_set_conn_cte_tx_params(struct net_buf *buf,
2724 					 struct net_buf **evt)
2725 {
2726 	struct bt_hci_cp_le_set_conn_cte_tx_params *cmd = (void *)buf->data;
2727 	struct bt_hci_rp_le_set_conn_cte_tx_params *rp;
2728 	uint16_t handle, handle_le16;
2729 	uint8_t status;
2730 
2731 	handle_le16 = cmd->handle;
2732 	handle = sys_le16_to_cpu(handle_le16);
2733 
2734 	status = ll_df_set_conn_cte_tx_params(handle, cmd->cte_types,
2735 					      cmd->switch_pattern_len,
2736 					      cmd->ant_ids);
2737 
2738 	rp = hci_cmd_complete(evt, sizeof(*rp));
2739 
2740 	rp->status = status;
2741 	rp->handle = handle_le16;
2742 }
2743 #endif /* CONFIG_BT_CTLR_DF_CONN_CTE_RSP */
2744 
2745 #if defined(CONFIG_BT_CTLR_DF_CONN_CTE_REQ)
le_df_set_conn_cte_rx_params(struct net_buf * buf,struct net_buf ** evt)2746 static void le_df_set_conn_cte_rx_params(struct net_buf *buf, struct net_buf **evt)
2747 {
2748 	struct bt_hci_cp_le_set_conn_cte_rx_params *cmd = (void *)buf->data;
2749 	struct bt_hci_rp_le_set_conn_cte_rx_params *rp;
2750 	uint16_t handle, handle_le16;
2751 	uint8_t status;
2752 
2753 	handle_le16 = cmd->handle;
2754 	handle = sys_le16_to_cpu(handle_le16);
2755 
2756 	status = ll_df_set_conn_cte_rx_params(handle, cmd->sampling_enable, cmd->slot_durations,
2757 					      cmd->switch_pattern_len, cmd->ant_ids);
2758 
2759 	rp = hci_cmd_complete(evt, sizeof(*rp));
2760 
2761 	rp->status = status;
2762 	rp->handle = handle_le16;
2763 }
2764 
le_df_set_conn_cte_req_enable(struct net_buf * buf,struct net_buf ** evt)2765 static void le_df_set_conn_cte_req_enable(struct net_buf *buf, struct net_buf **evt)
2766 {
2767 	struct bt_hci_cp_le_conn_cte_req_enable *cmd = (void *)buf->data;
2768 	struct bt_hci_rp_le_conn_cte_req_enable *rp;
2769 	uint16_t handle, handle_le16;
2770 	uint8_t status;
2771 
2772 	handle_le16 = cmd->handle;
2773 	handle = sys_le16_to_cpu(handle_le16);
2774 
2775 	status = ll_df_set_conn_cte_req_enable(handle, cmd->enable, cmd->cte_request_interval,
2776 					       cmd->requested_cte_length, cmd->requested_cte_type);
2777 	rp = hci_cmd_complete(evt, sizeof(*rp));
2778 
2779 	rp->status = status;
2780 	rp->handle = handle_le16;
2781 }
2782 #endif /* CONFIG_BT_CTLR_DF_CONN_CTE_RSP */
2783 
le_df_read_ant_inf(struct net_buf * buf,struct net_buf ** evt)2784 static void le_df_read_ant_inf(struct net_buf *buf, struct net_buf **evt)
2785 {
2786 	struct bt_hci_rp_le_read_ant_info *rp;
2787 	uint8_t max_switch_pattern_len;
2788 	uint8_t switch_sample_rates;
2789 	uint8_t max_cte_len;
2790 	uint8_t num_ant;
2791 
2792 	ll_df_read_ant_inf(&switch_sample_rates, &num_ant,
2793 			   &max_switch_pattern_len, &max_cte_len);
2794 
2795 	rp = hci_cmd_complete(evt, sizeof(*rp));
2796 
2797 	rp->max_switch_pattern_len = max_switch_pattern_len;
2798 	rp->switch_sample_rates = switch_sample_rates;
2799 	rp->max_cte_len = max_cte_len;
2800 	rp->num_ant = num_ant;
2801 	rp->status = 0x00;
2802 }
2803 #endif /* CONFIG_BT_CTLR_DF */
2804 
2805 #if defined(CONFIG_BT_CTLR_DTM_HCI)
le_rx_test(struct net_buf * buf,struct net_buf ** evt)2806 static void le_rx_test(struct net_buf *buf, struct net_buf **evt)
2807 {
2808 	struct bt_hci_cp_le_rx_test *cmd = (void *)buf->data;
2809 	uint8_t status;
2810 
2811 	status = ll_test_rx(cmd->rx_ch, 0x01, 0);
2812 
2813 	*evt = cmd_complete_status(status);
2814 }
2815 
le_tx_test(struct net_buf * buf,struct net_buf ** evt)2816 static void le_tx_test(struct net_buf *buf, struct net_buf **evt)
2817 {
2818 	struct bt_hci_cp_le_tx_test *cmd = (void *)buf->data;
2819 	uint8_t status;
2820 
2821 	status = ll_test_tx(cmd->tx_ch, cmd->test_data_len, cmd->pkt_payload,
2822 			    0x01);
2823 
2824 	*evt = cmd_complete_status(status);
2825 }
2826 
le_test_end(struct net_buf * buf,struct net_buf ** evt)2827 static void le_test_end(struct net_buf *buf, struct net_buf **evt)
2828 {
2829 	struct bt_hci_rp_le_test_end *rp;
2830 	uint16_t rx_pkt_count;
2831 
2832 	ll_test_end(&rx_pkt_count);
2833 
2834 	rp = hci_cmd_complete(evt, sizeof(*rp));
2835 	rp->status = 0x00;
2836 	rp->rx_pkt_count = sys_cpu_to_le16(rx_pkt_count);
2837 }
2838 
le_enh_rx_test(struct net_buf * buf,struct net_buf ** evt)2839 static void le_enh_rx_test(struct net_buf *buf, struct net_buf **evt)
2840 {
2841 	struct bt_hci_cp_le_enh_rx_test *cmd = (void *)buf->data;
2842 	uint8_t status;
2843 
2844 	status = ll_test_rx(cmd->rx_ch, cmd->phy, cmd->mod_index);
2845 
2846 	*evt = cmd_complete_status(status);
2847 }
2848 
le_enh_tx_test(struct net_buf * buf,struct net_buf ** evt)2849 static void le_enh_tx_test(struct net_buf *buf, struct net_buf **evt)
2850 {
2851 	struct bt_hci_cp_le_enh_tx_test *cmd = (void *)buf->data;
2852 	uint8_t status;
2853 
2854 	status = ll_test_tx(cmd->tx_ch, cmd->test_data_len, cmd->pkt_payload,
2855 			    cmd->phy);
2856 
2857 	*evt = cmd_complete_status(status);
2858 }
2859 #endif /* CONFIG_BT_CTLR_DTM_HCI */
2860 
2861 #if defined(CONFIG_BT_CTLR_ADV_EXT)
2862 #if defined(CONFIG_BT_BROADCASTER)
2863 
le_set_adv_set_random_addr(struct net_buf * buf,struct net_buf ** evt)2864 static void le_set_adv_set_random_addr(struct net_buf *buf,
2865 				       struct net_buf **evt)
2866 {
2867 	struct bt_hci_cp_le_set_adv_set_random_addr *cmd = (void *)buf->data;
2868 	uint8_t status;
2869 	uint8_t handle;
2870 
2871 	if (adv_cmds_ext_check(evt)) {
2872 		return;
2873 	}
2874 
2875 	status = ll_adv_set_by_hci_handle_get(cmd->handle, &handle);
2876 	if (status) {
2877 		*evt = cmd_complete_status(status);
2878 		return;
2879 	}
2880 
2881 	status = ll_adv_aux_random_addr_set(handle, &cmd->bdaddr.val[0]);
2882 
2883 	*evt = cmd_complete_status(status);
2884 }
2885 
le_set_ext_adv_param(struct net_buf * buf,struct net_buf ** evt)2886 static void le_set_ext_adv_param(struct net_buf *buf, struct net_buf **evt)
2887 {
2888 	struct bt_hci_cp_le_set_ext_adv_param *cmd = (void *)buf->data;
2889 	struct bt_hci_rp_le_set_ext_adv_param *rp;
2890 	uint32_t min_interval;
2891 	uint16_t evt_prop;
2892 	uint8_t tx_pwr;
2893 	uint8_t status;
2894 	uint8_t phy_p;
2895 	uint8_t phy_s;
2896 	uint8_t handle;
2897 
2898 	if (adv_cmds_ext_check(evt)) {
2899 		return;
2900 	}
2901 
2902 	if (cmd->handle > BT_HCI_LE_ADV_HANDLE_MAX) {
2903 		*evt = cmd_complete_status(BT_HCI_ERR_INVALID_PARAM);
2904 		return;
2905 	}
2906 
2907 	status = ll_adv_set_by_hci_handle_get_or_new(cmd->handle, &handle);
2908 	if (status) {
2909 		*evt = cmd_complete_status(status);
2910 		return;
2911 	}
2912 
2913 	evt_prop = sys_le16_to_cpu(cmd->props);
2914 	min_interval = sys_get_le24(cmd->prim_min_interval);
2915 	tx_pwr = cmd->tx_power;
2916 	phy_p = BIT(cmd->prim_adv_phy - 1);
2917 	phy_s = BIT(cmd->sec_adv_phy - 1);
2918 
2919 	status = ll_adv_params_set(handle, evt_prop, min_interval,
2920 				   PDU_ADV_TYPE_EXT_IND, cmd->own_addr_type,
2921 				   cmd->peer_addr.type, cmd->peer_addr.a.val,
2922 				   cmd->prim_channel_map, cmd->filter_policy,
2923 				   &tx_pwr, phy_p, cmd->sec_adv_max_skip, phy_s,
2924 				   cmd->sid, cmd->scan_req_notify_enable);
2925 
2926 	rp = hci_cmd_complete(evt, sizeof(*rp));
2927 	rp->status = status;
2928 	rp->tx_power = tx_pwr;
2929 }
2930 
le_set_ext_adv_data(struct net_buf * buf,struct net_buf ** evt)2931 static void le_set_ext_adv_data(struct net_buf *buf, struct net_buf **evt)
2932 {
2933 	struct bt_hci_cp_le_set_ext_adv_data *cmd = (void *)buf->data;
2934 	uint8_t status;
2935 	uint8_t handle;
2936 
2937 	if (adv_cmds_ext_check(evt)) {
2938 		return;
2939 	}
2940 
2941 	status = ll_adv_set_by_hci_handle_get(cmd->handle, &handle);
2942 	if (status) {
2943 		*evt = cmd_complete_status(status);
2944 		return;
2945 	}
2946 
2947 	status = ll_adv_aux_ad_data_set(handle, cmd->op, cmd->frag_pref,
2948 					cmd->len, cmd->data);
2949 
2950 	*evt = cmd_complete_status(status);
2951 }
2952 
le_set_ext_scan_rsp_data(struct net_buf * buf,struct net_buf ** evt)2953 static void le_set_ext_scan_rsp_data(struct net_buf *buf, struct net_buf **evt)
2954 {
2955 	struct bt_hci_cp_le_set_ext_scan_rsp_data *cmd = (void *)buf->data;
2956 	uint8_t status;
2957 	uint8_t handle;
2958 
2959 	if (adv_cmds_ext_check(evt)) {
2960 		return;
2961 	}
2962 
2963 	status = ll_adv_set_by_hci_handle_get(cmd->handle, &handle);
2964 	if (status) {
2965 		*evt = cmd_complete_status(status);
2966 		return;
2967 	}
2968 
2969 	status = ll_adv_aux_sr_data_set(handle, cmd->op, cmd->frag_pref,
2970 					cmd->len, cmd->data);
2971 
2972 	*evt = cmd_complete_status(status);
2973 }
2974 
le_set_ext_adv_enable(struct net_buf * buf,struct net_buf ** evt)2975 static void le_set_ext_adv_enable(struct net_buf *buf, struct net_buf **evt)
2976 {
2977 	struct bt_hci_cp_le_set_ext_adv_enable *cmd = (void *)buf->data;
2978 	struct bt_hci_ext_adv_set *s;
2979 	uint8_t set_num;
2980 	uint8_t enable;
2981 	uint8_t status;
2982 	uint8_t handle;
2983 
2984 	if (adv_cmds_ext_check(evt)) {
2985 		return;
2986 	}
2987 
2988 	set_num = cmd->set_num;
2989 	if (!set_num) {
2990 		if (cmd->enable) {
2991 			*evt = cmd_complete_status(BT_HCI_ERR_INVALID_PARAM);
2992 			return;
2993 		}
2994 
2995 		/* FIXME: Implement disable of all advertising sets */
2996 		*evt = cmd_complete_status(BT_HCI_ERR_UNSUPP_FEATURE_PARAM_VAL);
2997 
2998 		return;
2999 	}
3000 
3001 	s = (void *) cmd->s;
3002 	enable = cmd->enable;
3003 	do {
3004 		status = ll_adv_set_by_hci_handle_get(s->handle, &handle);
3005 		if (status) {
3006 			break;
3007 		}
3008 
3009 		/* TODO: duration and events parameter use. */
3010 #if defined(CONFIG_BT_HCI_MESH_EXT)
3011 		status = ll_adv_enable(handle, cmd->enable, 0, 0, 0, 0, 0);
3012 #else /* !CONFIG_BT_HCI_MESH_EXT */
3013 		status = ll_adv_enable(handle, cmd->enable,
3014 				       s->duration, s->max_ext_adv_evts);
3015 #endif /* !CONFIG_BT_HCI_MESH_EXT */
3016 		if (status) {
3017 			/* TODO: how to handle succeeded ones before this
3018 			 * error.
3019 			 */
3020 			break;
3021 		}
3022 
3023 		s++;
3024 	} while (--set_num);
3025 
3026 	*evt = cmd_complete_status(status);
3027 }
3028 
le_read_max_adv_data_len(struct net_buf * buf,struct net_buf ** evt)3029 static void le_read_max_adv_data_len(struct net_buf *buf, struct net_buf **evt)
3030 {
3031 	struct bt_hci_rp_le_read_max_adv_data_len *rp;
3032 	uint16_t max_adv_data_len;
3033 
3034 	if (adv_cmds_ext_check(evt)) {
3035 		return;
3036 	}
3037 
3038 	rp = hci_cmd_complete(evt, sizeof(*rp));
3039 
3040 	max_adv_data_len = ll_adv_aux_max_data_length_get();
3041 
3042 	rp->max_adv_data_len = sys_cpu_to_le16(max_adv_data_len);
3043 	rp->status = 0x00;
3044 }
3045 
le_read_num_adv_sets(struct net_buf * buf,struct net_buf ** evt)3046 static void le_read_num_adv_sets(struct net_buf *buf, struct net_buf **evt)
3047 {
3048 	struct bt_hci_rp_le_read_num_adv_sets *rp;
3049 
3050 	if (adv_cmds_ext_check(evt)) {
3051 		return;
3052 	}
3053 
3054 	rp = hci_cmd_complete(evt, sizeof(*rp));
3055 
3056 	rp->num_sets = ll_adv_aux_set_count_get();
3057 	rp->status = 0x00;
3058 }
3059 
le_remove_adv_set(struct net_buf * buf,struct net_buf ** evt)3060 static void le_remove_adv_set(struct net_buf *buf, struct net_buf **evt)
3061 {
3062 	struct bt_hci_cp_le_remove_adv_set *cmd = (void *)buf->data;
3063 	uint8_t status;
3064 	uint8_t handle;
3065 
3066 	if (adv_cmds_ext_check(evt)) {
3067 		return;
3068 	}
3069 
3070 	status = ll_adv_set_by_hci_handle_get(cmd->handle, &handle);
3071 	if (status) {
3072 		*evt = cmd_complete_status(status);
3073 		return;
3074 	}
3075 
3076 	status = ll_adv_aux_set_remove(handle);
3077 
3078 	*evt = cmd_complete_status(status);
3079 }
3080 
le_clear_adv_sets(struct net_buf * buf,struct net_buf ** evt)3081 static void le_clear_adv_sets(struct net_buf *buf, struct net_buf **evt)
3082 {
3083 	uint8_t status;
3084 
3085 	if (adv_cmds_ext_check(evt)) {
3086 		return;
3087 	}
3088 
3089 	status = ll_adv_aux_set_clear();
3090 
3091 	*evt = cmd_complete_status(status);
3092 }
3093 
3094 #if defined(CONFIG_BT_CTLR_ADV_PERIODIC)
le_set_per_adv_param(struct net_buf * buf,struct net_buf ** evt)3095 static void le_set_per_adv_param(struct net_buf *buf, struct net_buf **evt)
3096 {
3097 	struct bt_hci_cp_le_set_per_adv_param *cmd = (void *)buf->data;
3098 	uint16_t interval;
3099 	uint16_t flags;
3100 	uint8_t status;
3101 	uint8_t handle;
3102 
3103 	if (adv_cmds_ext_check(evt)) {
3104 		return;
3105 	}
3106 
3107 	status = ll_adv_set_by_hci_handle_get(cmd->handle, &handle);
3108 	if (status) {
3109 		*evt = cmd_complete_status(status);
3110 		return;
3111 	}
3112 
3113 	interval = sys_le16_to_cpu(cmd->max_interval);
3114 	flags = sys_le16_to_cpu(cmd->props);
3115 
3116 	status = ll_adv_sync_param_set(handle, interval, flags);
3117 
3118 	*evt = cmd_complete_status(status);
3119 }
3120 
le_set_per_adv_data(struct net_buf * buf,struct net_buf ** evt)3121 static void le_set_per_adv_data(struct net_buf *buf, struct net_buf **evt)
3122 {
3123 	struct bt_hci_cp_le_set_per_adv_data *cmd = (void *)buf->data;
3124 	uint8_t status;
3125 	uint8_t handle;
3126 
3127 	if (adv_cmds_ext_check(evt)) {
3128 		return;
3129 	}
3130 
3131 	status = ll_adv_set_by_hci_handle_get(cmd->handle, &handle);
3132 	if (status) {
3133 		*evt = cmd_complete_status(status);
3134 		return;
3135 	}
3136 
3137 	status = ll_adv_sync_ad_data_set(handle, cmd->op, cmd->len,
3138 					 cmd->data);
3139 
3140 	*evt = cmd_complete_status(status);
3141 }
3142 
le_set_per_adv_enable(struct net_buf * buf,struct net_buf ** evt)3143 static void le_set_per_adv_enable(struct net_buf *buf, struct net_buf **evt)
3144 {
3145 	struct bt_hci_cp_le_set_per_adv_enable *cmd = (void *)buf->data;
3146 	uint8_t status;
3147 	uint8_t handle;
3148 
3149 	if (adv_cmds_ext_check(evt)) {
3150 		return;
3151 	}
3152 
3153 	status = ll_adv_set_by_hci_handle_get(cmd->handle, &handle);
3154 	if (status) {
3155 		*evt = cmd_complete_status(status);
3156 		return;
3157 	}
3158 
3159 	status = ll_adv_sync_enable(handle, cmd->enable);
3160 
3161 	*evt = cmd_complete_status(status);
3162 }
3163 #endif /* CONFIG_BT_CTLR_ADV_PERIODIC */
3164 #endif /* CONFIG_BT_BROADCASTER */
3165 
3166 #if defined(CONFIG_BT_OBSERVER)
le_set_ext_scan_param(struct net_buf * buf,struct net_buf ** evt)3167 static void le_set_ext_scan_param(struct net_buf *buf, struct net_buf **evt)
3168 {
3169 	struct bt_hci_cp_le_set_ext_scan_param *cmd = (void *)buf->data;
3170 	struct bt_hci_ext_scan_phy *p;
3171 	uint8_t own_addr_type;
3172 	uint8_t filter_policy;
3173 	uint8_t phys_bitmask;
3174 	uint8_t status;
3175 	uint8_t phys;
3176 
3177 	if (adv_cmds_ext_check(evt)) {
3178 		return;
3179 	}
3180 
3181 	/* Number of bits set indicate scan sets to be configured by calling
3182 	 * ll_scan_params_set function.
3183 	 */
3184 	phys_bitmask = BT_HCI_LE_EXT_SCAN_PHY_1M;
3185 	if (IS_ENABLED(CONFIG_BT_CTLR_PHY_CODED)) {
3186 		phys_bitmask |= BT_HCI_LE_EXT_SCAN_PHY_CODED;
3187 	}
3188 
3189 	phys = cmd->phys;
3190 	if (IS_ENABLED(CONFIG_BT_CTLR_PARAM_CHECK) &&
3191 	    (phys > phys_bitmask)) {
3192 		*evt = cmd_complete_status(BT_HCI_ERR_UNSUPP_FEATURE_PARAM_VAL);
3193 
3194 		return;
3195 	}
3196 
3197 	own_addr_type = cmd->own_addr_type;
3198 	filter_policy = cmd->filter_policy;
3199 	p = cmd->p;
3200 
3201 	/* Irrespective of enabled PHYs to scan for, ll_scan_params_set needs
3202 	 * to be called to initialise the scan sets.
3203 	 * Passing interval and window as 0, disable the particular scan set
3204 	 * from being enabled.
3205 	 */
3206 	do {
3207 		uint16_t interval;
3208 		uint16_t window;
3209 		uint8_t type;
3210 		uint8_t phy;
3211 
3212 		/* Get single PHY bit from the loop bitmask */
3213 		phy = BIT(find_lsb_set(phys_bitmask) - 1);
3214 
3215 		/* Pass the PHY (1M or Coded) of scan set in MSbits of type
3216 		 * parameter
3217 		 */
3218 		type = (phy << 1);
3219 
3220 		/* If current PHY is one of the PHY in the Scanning_PHYs,
3221 		 * pick the supplied scan type, interval and window.
3222 		 */
3223 		if (phys & phy) {
3224 			type |= (p->type & 0x01);
3225 			interval = sys_le16_to_cpu(p->interval);
3226 			window = sys_le16_to_cpu(p->window);
3227 			p++;
3228 		} else {
3229 			interval = 0U;
3230 			window = 0U;
3231 		}
3232 
3233 		status = ll_scan_params_set(type, interval, window,
3234 					    own_addr_type, filter_policy);
3235 		if (status) {
3236 			break;
3237 		}
3238 
3239 		phys_bitmask &= (phys_bitmask - 1);
3240 	} while (phys_bitmask);
3241 
3242 	*evt = cmd_complete_status(status);
3243 }
3244 
le_set_ext_scan_enable(struct net_buf * buf,struct net_buf ** evt)3245 static void le_set_ext_scan_enable(struct net_buf *buf, struct net_buf **evt)
3246 {
3247 	struct bt_hci_cp_le_set_ext_scan_enable *cmd = (void *)buf->data;
3248 	uint8_t status;
3249 
3250 	if (adv_cmds_ext_check(evt)) {
3251 		return;
3252 	}
3253 
3254 #if CONFIG_BT_CTLR_DUP_FILTER_LEN > 0
3255 	/* initialize duplicate filtering */
3256 	if (cmd->enable && cmd->filter_dup) {
3257 		dup_count = 0;
3258 		dup_curr = 0U;
3259 	} else {
3260 		dup_count = -1;
3261 	}
3262 #endif
3263 
3264 	status = ll_scan_enable(cmd->enable, cmd->duration, cmd->period);
3265 
3266 	/* NOTE: As filter duplicates is implemented here in HCI source code,
3267 	 *       enabling of already enabled scanning shall succeed after
3268 	 *       updates to filter duplicates is handled in the above
3269 	 *       statements. Refer to BT Spec v5.0 Vol 2 Part E Section 7.8.11.
3270 	 */
3271 	if (!IS_ENABLED(CONFIG_BT_CTLR_SCAN_ENABLE_STRICT) &&
3272 	    (status == BT_HCI_ERR_CMD_DISALLOWED)) {
3273 		status = BT_HCI_ERR_SUCCESS;
3274 	}
3275 
3276 	*evt = cmd_complete_status(status);
3277 }
3278 
3279 #if defined(CONFIG_BT_CTLR_SYNC_PERIODIC)
le_per_adv_create_sync(struct net_buf * buf,struct net_buf ** evt)3280 static void le_per_adv_create_sync(struct net_buf *buf, struct net_buf **evt)
3281 {
3282 	struct bt_hci_cp_le_per_adv_create_sync *cmd = (void *)buf->data;
3283 	uint16_t sync_timeout;
3284 	uint8_t status;
3285 	uint16_t skip;
3286 
3287 	if (adv_cmds_ext_check(NULL)) {
3288 		*evt = cmd_status(BT_HCI_ERR_CMD_DISALLOWED);
3289 		return;
3290 	}
3291 
3292 
3293 	skip = sys_le16_to_cpu(cmd->skip);
3294 	sync_timeout = sys_le16_to_cpu(cmd->sync_timeout);
3295 
3296 #if defined(CONFIG_BT_CTLR_SYNC_PERIODIC_CTE_TYPE_FILTERING)
3297 	if ((cmd->cte_type & BT_HCI_LE_PER_ADV_CREATE_SYNC_CTE_TYPE_INVALID_VALUE) != 0) {
3298 		status = BT_HCI_ERR_CMD_DISALLOWED;
3299 #else
3300 	if (cmd->cte_type != BT_HCI_LE_PER_ADV_CREATE_SYNC_CTE_TYPE_NO_FILTERING) {
3301 		status = BT_HCI_ERR_INVALID_PARAM;
3302 #endif /* CONFIG_BT_CTLR_SYNC_PERIODIC_CTE_TYPE_FILTERING */
3303 	} else {
3304 		status = ll_sync_create(cmd->options, cmd->sid, cmd->addr.type, cmd->addr.a.val,
3305 					skip, sync_timeout, cmd->cte_type);
3306 	}
3307 	*evt = cmd_status(status);
3308 }
3309 
3310 static void le_per_adv_create_sync_cancel(struct net_buf *buf,
3311 					  struct net_buf **evt, void **node_rx)
3312 {
3313 	struct bt_hci_evt_cc_status *ccst;
3314 	uint8_t status;
3315 
3316 	if (adv_cmds_ext_check(evt)) {
3317 		return;
3318 	}
3319 
3320 	status = ll_sync_create_cancel(node_rx);
3321 
3322 	ccst = hci_cmd_complete(evt, sizeof(*ccst));
3323 	ccst->status = status;
3324 }
3325 
3326 static void le_per_adv_terminate_sync(struct net_buf *buf, struct net_buf **evt)
3327 {
3328 	struct bt_hci_cp_le_per_adv_terminate_sync *cmd = (void *)buf->data;
3329 	struct bt_hci_evt_cc_status *ccst;
3330 	uint16_t handle;
3331 	uint8_t status;
3332 
3333 	if (adv_cmds_ext_check(evt)) {
3334 		return;
3335 	}
3336 
3337 	handle = sys_le16_to_cpu(cmd->handle);
3338 
3339 	status = ll_sync_terminate(handle);
3340 
3341 	ccst = hci_cmd_complete(evt, sizeof(*ccst));
3342 	ccst->status = status;
3343 }
3344 
3345 static void le_per_adv_recv_enable(struct net_buf *buf, struct net_buf **evt)
3346 {
3347 	struct bt_hci_cp_le_set_per_adv_recv_enable *cmd = (void *)buf->data;
3348 	struct bt_hci_evt_cc_status *ccst;
3349 	uint16_t handle;
3350 	uint8_t status;
3351 
3352 	if (adv_cmds_ext_check(evt)) {
3353 		return;
3354 	}
3355 
3356 	handle = sys_le16_to_cpu(cmd->handle);
3357 
3358 	status = ll_sync_recv_enable(handle, cmd->enable);
3359 
3360 	ccst = hci_cmd_complete(evt, sizeof(*ccst));
3361 	ccst->status = status;
3362 }
3363 #endif /* CONFIG_BT_CTLR_SYNC_PERIODIC */
3364 #endif /* CONFIG_BT_OBSERVER */
3365 
3366 #if defined(CONFIG_BT_CENTRAL)
3367 static void le_ext_create_connection(struct net_buf *buf, struct net_buf **evt)
3368 {
3369 	struct bt_hci_cp_le_ext_create_conn *cmd = (void *)buf->data;
3370 	struct bt_hci_ext_conn_phy *p;
3371 	uint8_t peer_addr_type;
3372 	uint8_t own_addr_type;
3373 	uint8_t filter_policy;
3374 	uint8_t phys_bitmask;
3375 	uint8_t *peer_addr;
3376 	uint8_t status;
3377 	uint8_t phys;
3378 
3379 	if (adv_cmds_ext_check(NULL)) {
3380 		*evt = cmd_status(BT_HCI_ERR_CMD_DISALLOWED);
3381 		return;
3382 	}
3383 
3384 	/* Number of bits set indicate scan sets to be configured by calling
3385 	 * ll_create_connection function.
3386 	 */
3387 	phys_bitmask = BT_HCI_LE_EXT_SCAN_PHY_1M;
3388 	if (IS_ENABLED(CONFIG_BT_CTLR_PHY_CODED)) {
3389 		phys_bitmask |= BT_HCI_LE_EXT_SCAN_PHY_CODED;
3390 	}
3391 
3392 	phys = cmd->phys;
3393 	if (IS_ENABLED(CONFIG_BT_CTLR_PARAM_CHECK) &&
3394 	    (phys > phys_bitmask)) {
3395 		*evt = cmd_status(BT_HCI_ERR_UNSUPP_FEATURE_PARAM_VAL);
3396 
3397 		return;
3398 	}
3399 
3400 	filter_policy = cmd->filter_policy;
3401 	own_addr_type = cmd->own_addr_type;
3402 	peer_addr_type = cmd->peer_addr.type;
3403 	peer_addr = cmd->peer_addr.a.val;
3404 	p = cmd->p;
3405 
3406 	do {
3407 		uint16_t supervision_timeout;
3408 		uint16_t conn_interval_max;
3409 		uint16_t scan_interval;
3410 		uint16_t conn_latency;
3411 		uint16_t scan_window;
3412 		uint8_t phy;
3413 
3414 		phy = BIT(find_lsb_set(phys_bitmask) - 1);
3415 
3416 		if (phys & phy) {
3417 			scan_interval = sys_le16_to_cpu(p->scan_interval);
3418 			scan_window = sys_le16_to_cpu(p->scan_window);
3419 			conn_interval_max =
3420 				sys_le16_to_cpu(p->conn_interval_max);
3421 			conn_latency = sys_le16_to_cpu(p->conn_latency);
3422 			supervision_timeout =
3423 				sys_le16_to_cpu(p->supervision_timeout);
3424 
3425 			if (IS_ENABLED(CONFIG_BT_CTLR_PARAM_CHECK)) {
3426 				status = check_cconn_params(true, scan_interval,
3427 							    scan_window,
3428 							    conn_interval_max,
3429 							    conn_latency,
3430 							    supervision_timeout);
3431 				if (status) {
3432 					*evt = cmd_status(status);
3433 					return;
3434 				}
3435 			}
3436 
3437 			status = ll_create_connection(scan_interval,
3438 						      scan_window,
3439 						      filter_policy,
3440 						      peer_addr_type,
3441 						      peer_addr,
3442 						      own_addr_type,
3443 						      conn_interval_max,
3444 						      conn_latency,
3445 						      supervision_timeout,
3446 						      phy);
3447 			p++;
3448 		} else {
3449 			uint8_t type;
3450 
3451 			type = (phy << 1);
3452 			/* NOTE: Pass invalid interval value to not start
3453 			 *       scanning using this scan instance.
3454 			 */
3455 			status = ll_scan_params_set(type, 0, 0, 0, 0);
3456 		}
3457 
3458 		if (status) {
3459 			*evt = cmd_status(status);
3460 			return;
3461 		}
3462 
3463 		phys_bitmask &= (phys_bitmask - 1);
3464 	} while (phys_bitmask);
3465 
3466 	status = ll_connect_enable(phys & BT_HCI_LE_EXT_SCAN_PHY_CODED);
3467 
3468 	*evt = cmd_status(status);
3469 }
3470 #endif /* CONFIG_BT_CENTRAL */
3471 #endif /* CONFIG_BT_CTLR_ADV_EXT */
3472 
3473 #if defined(CONFIG_BT_CTLR_PERIPHERAL_ISO)
3474 static void le_cis_request(struct pdu_data *pdu_data,
3475 			   struct node_rx_pdu *node_rx,
3476 			   struct net_buf *buf)
3477 {
3478 	struct bt_hci_evt_le_cis_req *sep;
3479 	struct node_rx_conn_iso_req *req;
3480 
3481 	if (!(event_mask & BT_EVT_MASK_LE_META_EVENT) ||
3482 	    !(le_event_mask & BT_EVT_MASK_LE_CIS_REQ)) {
3483 		return;
3484 	}
3485 
3486 	req = (void *)pdu_data;
3487 
3488 	sep = meta_evt(buf, BT_HCI_EVT_LE_CIS_REQ, sizeof(*sep));
3489 	sep->acl_handle = sys_cpu_to_le16(node_rx->hdr.handle);
3490 	sep->cis_handle = sys_cpu_to_le16(req->cis_handle);
3491 	sep->cig_id = req->cig_id;
3492 	sep->cis_id = req->cis_id;
3493 }
3494 #endif /* CONFIG_BT_CTLR_PERIPHERAL_ISO */
3495 
3496 #if defined(CONFIG_BT_CTLR_CONN_ISO)
3497 static void le_cis_established(struct pdu_data *pdu_data,
3498 			       struct node_rx_pdu *node_rx,
3499 			       struct net_buf *buf)
3500 {
3501 	struct lll_conn_iso_stream_rxtx *lll_cis_c;
3502 	struct lll_conn_iso_stream_rxtx *lll_cis_p;
3503 	struct bt_hci_evt_le_cis_established *sep;
3504 	struct lll_conn_iso_stream *lll_cis;
3505 	struct node_rx_conn_iso_estab *est;
3506 	struct ll_conn_iso_stream *cis;
3507 	struct ll_conn_iso_group *cig;
3508 	bool is_central;
3509 
3510 	if (!(event_mask & BT_EVT_MASK_LE_META_EVENT) ||
3511 	    !(le_event_mask & BT_EVT_MASK_LE_CIS_ESTABLISHED)) {
3512 		return;
3513 	}
3514 
3515 	cis = node_rx->hdr.rx_ftr.param;
3516 	cig = cis->group;
3517 	lll_cis = &cis->lll;
3518 	is_central = cig->lll.role == BT_CONN_ROLE_CENTRAL;
3519 	lll_cis_c = is_central ? &lll_cis->tx : &lll_cis->rx;
3520 	lll_cis_p = is_central ? &lll_cis->rx : &lll_cis->tx;
3521 	est = (void *)pdu_data;
3522 
3523 	sep = meta_evt(buf, BT_HCI_EVT_LE_CIS_ESTABLISHED, sizeof(*sep));
3524 
3525 	sep->status = est->status;
3526 	sep->conn_handle = sys_cpu_to_le16(est->cis_handle);
3527 	sys_put_le24(cig->sync_delay, sep->cig_sync_delay);
3528 	sys_put_le24(cis->sync_delay, sep->cis_sync_delay);
3529 	sys_put_le24(cig->c_latency, sep->c_latency);
3530 	sys_put_le24(cig->p_latency, sep->p_latency);
3531 	sep->c_phy = lll_cis_c->phy;
3532 	sep->p_phy = lll_cis_p->phy;
3533 	sep->nse = lll_cis->num_subevents;
3534 	sep->c_bn = lll_cis_c->burst_number;
3535 	sep->p_bn = lll_cis_p->burst_number;
3536 	sep->c_ft = lll_cis_c->flush_timeout;
3537 	sep->p_ft = lll_cis_p->flush_timeout;
3538 	sep->c_max_pdu = sys_cpu_to_le16(lll_cis_c->max_octets);
3539 	sep->p_max_pdu = sys_cpu_to_le16(lll_cis_p->max_octets);
3540 	sep->interval = sys_cpu_to_le16(cig->iso_interval);
3541 
3542 #if defined(CONFIG_BT_CTLR_CENTRAL_ISO)
3543 	if (is_central) {
3544 		cis_pending_count--;
3545 	}
3546 #endif /* CONFIG_BT_CTLR_CENTRAL_ISO */
3547 }
3548 #endif /* CONFIG_BT_CTLR_CONN_ISO */
3549 
3550 static int controller_cmd_handle(uint16_t  ocf, struct net_buf *cmd,
3551 				 struct net_buf **evt, void **node_rx)
3552 {
3553 	switch (ocf) {
3554 	case BT_OCF(BT_HCI_OP_LE_SET_EVENT_MASK):
3555 		le_set_event_mask(cmd, evt);
3556 		break;
3557 
3558 	case BT_OCF(BT_HCI_OP_LE_READ_BUFFER_SIZE):
3559 		le_read_buffer_size(cmd, evt);
3560 		break;
3561 
3562 #if defined(CONFIG_BT_CTLR_ADV_ISO) || defined(CONFIG_BT_CTLR_CONN_ISO)
3563 	case BT_OCF(BT_HCI_OP_LE_READ_BUFFER_SIZE_V2):
3564 		le_read_buffer_size_v2(cmd, evt);
3565 		break;
3566 #endif /* CONFIG_BT_CTLR_ADV_ISO || CONFIG_BT_CTLR_CONN_ISO */
3567 
3568 	case BT_OCF(BT_HCI_OP_LE_READ_LOCAL_FEATURES):
3569 		le_read_local_features(cmd, evt);
3570 		break;
3571 
3572 	case BT_OCF(BT_HCI_OP_LE_SET_RANDOM_ADDRESS):
3573 		le_set_random_address(cmd, evt);
3574 		break;
3575 
3576 #if defined(CONFIG_BT_CTLR_FILTER_ACCEPT_LIST)
3577 	case BT_OCF(BT_HCI_OP_LE_READ_FAL_SIZE):
3578 		le_read_fal_size(cmd, evt);
3579 		break;
3580 
3581 	case BT_OCF(BT_HCI_OP_LE_CLEAR_FAL):
3582 		le_clear_fal(cmd, evt);
3583 		break;
3584 
3585 	case BT_OCF(BT_HCI_OP_LE_ADD_DEV_TO_FAL):
3586 		le_add_dev_to_fal(cmd, evt);
3587 		break;
3588 
3589 	case BT_OCF(BT_HCI_OP_LE_REM_DEV_FROM_FAL):
3590 		le_rem_dev_from_fal(cmd, evt);
3591 		break;
3592 #endif /* CONFIG_BT_CTLR_FILTER_ACCEPT_LIST */
3593 
3594 	case BT_OCF(BT_HCI_OP_LE_ENCRYPT):
3595 		le_encrypt(cmd, evt);
3596 		break;
3597 
3598 	case BT_OCF(BT_HCI_OP_LE_RAND):
3599 		le_rand(cmd, evt);
3600 		break;
3601 
3602 	case BT_OCF(BT_HCI_OP_LE_READ_SUPP_STATES):
3603 		le_read_supp_states(cmd, evt);
3604 		break;
3605 
3606 #if defined(CONFIG_BT_BROADCASTER)
3607 	case BT_OCF(BT_HCI_OP_LE_SET_ADV_PARAM):
3608 		le_set_adv_param(cmd, evt);
3609 		break;
3610 
3611 	case BT_OCF(BT_HCI_OP_LE_READ_ADV_CHAN_TX_POWER):
3612 		le_read_adv_chan_tx_power(cmd, evt);
3613 		break;
3614 
3615 	case BT_OCF(BT_HCI_OP_LE_SET_ADV_DATA):
3616 		le_set_adv_data(cmd, evt);
3617 		break;
3618 
3619 	case BT_OCF(BT_HCI_OP_LE_SET_SCAN_RSP_DATA):
3620 		le_set_scan_rsp_data(cmd, evt);
3621 		break;
3622 
3623 	case BT_OCF(BT_HCI_OP_LE_SET_ADV_ENABLE):
3624 		le_set_adv_enable(cmd, evt);
3625 		break;
3626 
3627 #if defined(CONFIG_BT_CTLR_ADV_ISO)
3628 	case BT_OCF(BT_HCI_OP_LE_CREATE_BIG):
3629 		le_create_big(cmd, evt);
3630 		break;
3631 
3632 	case BT_OCF(BT_HCI_OP_LE_CREATE_BIG_TEST):
3633 		le_create_big_test(cmd, evt);
3634 		break;
3635 
3636 	case BT_OCF(BT_HCI_OP_LE_TERMINATE_BIG):
3637 		le_terminate_big(cmd, evt);
3638 		break;
3639 #endif /* CONFIG_BT_CTLR_ADV_ISO */
3640 #endif /* CONFIG_BT_BROADCASTER */
3641 
3642 #if defined(CONFIG_BT_OBSERVER)
3643 	case BT_OCF(BT_HCI_OP_LE_SET_SCAN_PARAM):
3644 		le_set_scan_param(cmd, evt);
3645 		break;
3646 
3647 	case BT_OCF(BT_HCI_OP_LE_SET_SCAN_ENABLE):
3648 		le_set_scan_enable(cmd, evt);
3649 		break;
3650 
3651 #if defined(CONFIG_BT_CTLR_SYNC_ISO)
3652 	case BT_OCF(BT_HCI_OP_LE_BIG_CREATE_SYNC):
3653 		le_big_create_sync(cmd, evt);
3654 		break;
3655 
3656 	case BT_OCF(BT_HCI_OP_LE_BIG_TERMINATE_SYNC):
3657 		le_big_terminate_sync(cmd, evt, node_rx);
3658 		break;
3659 #endif /* CONFIG_BT_CTLR_SYNC_ISO */
3660 #endif /* CONFIG_BT_OBSERVER */
3661 
3662 #if defined(CONFIG_BT_CONN)
3663 #if defined(CONFIG_BT_CENTRAL)
3664 	case BT_OCF(BT_HCI_OP_LE_CREATE_CONN):
3665 		le_create_connection(cmd, evt);
3666 		break;
3667 
3668 	case BT_OCF(BT_HCI_OP_LE_CREATE_CONN_CANCEL):
3669 		le_create_conn_cancel(cmd, evt, node_rx);
3670 		break;
3671 
3672 	case BT_OCF(BT_HCI_OP_LE_SET_HOST_CHAN_CLASSIF):
3673 		le_set_host_chan_classif(cmd, evt);
3674 		break;
3675 
3676 #if defined(CONFIG_BT_CTLR_LE_ENC)
3677 	case BT_OCF(BT_HCI_OP_LE_START_ENCRYPTION):
3678 		le_start_encryption(cmd, evt);
3679 		break;
3680 #endif /* CONFIG_BT_CTLR_LE_ENC */
3681 
3682 #if defined(CONFIG_BT_CTLR_CENTRAL_ISO)
3683 	case BT_OCF(BT_HCI_OP_LE_SET_CIG_PARAMS):
3684 		le_set_cig_parameters(cmd, evt);
3685 		break;
3686 	case BT_OCF(BT_HCI_OP_LE_SET_CIG_PARAMS_TEST):
3687 		le_set_cig_params_test(cmd, evt);
3688 		break;
3689 	case BT_OCF(BT_HCI_OP_LE_CREATE_CIS):
3690 		le_create_cis(cmd, evt);
3691 		break;
3692 	case BT_OCF(BT_HCI_OP_LE_REMOVE_CIG):
3693 		le_remove_cig(cmd, evt);
3694 		break;
3695 #endif /* CONFIG_BT_CTLR_CENTRAL_ISO */
3696 #endif /* CONFIG_BT_CENTRAL */
3697 
3698 #if defined(CONFIG_BT_PERIPHERAL)
3699 #if defined(CONFIG_BT_CTLR_LE_ENC)
3700 	case BT_OCF(BT_HCI_OP_LE_LTK_REQ_REPLY):
3701 		le_ltk_req_reply(cmd, evt);
3702 		break;
3703 
3704 	case BT_OCF(BT_HCI_OP_LE_LTK_REQ_NEG_REPLY):
3705 		le_ltk_req_neg_reply(cmd, evt);
3706 		break;
3707 #endif /* CONFIG_BT_CTLR_LE_ENC */
3708 
3709 #if defined(CONFIG_BT_CTLR_PERIPHERAL_ISO)
3710 	case BT_OCF(BT_HCI_OP_LE_ACCEPT_CIS):
3711 		le_accept_cis(cmd, evt);
3712 		break;
3713 	case BT_OCF(BT_HCI_OP_LE_REJECT_CIS):
3714 		le_reject_cis(cmd, evt);
3715 		break;
3716 #endif /* CONFIG_BT_CTLR_PERIPHERAL_ISO */
3717 #endif /* CONFIG_BT_PERIPHERAL */
3718 
3719 #if defined(CONFIG_BT_CTLR_CONN_ISO)
3720 	case BT_OCF(BT_HCI_OP_LE_READ_ISO_TX_SYNC):
3721 		le_read_iso_tx_sync(cmd, evt);
3722 		break;
3723 
3724 	case BT_OCF(BT_HCI_OP_LE_SETUP_ISO_PATH):
3725 		le_setup_iso_path(cmd, evt);
3726 		break;
3727 
3728 	case BT_OCF(BT_HCI_OP_LE_REMOVE_ISO_PATH):
3729 		le_remove_iso_path(cmd, evt);
3730 		break;
3731 
3732 	case BT_OCF(BT_HCI_OP_LE_ISO_RECEIVE_TEST):
3733 		le_iso_receive_test(cmd, evt);
3734 		break;
3735 
3736 	case BT_OCF(BT_HCI_OP_LE_ISO_TRANSMIT_TEST):
3737 		le_iso_transmit_test(cmd, evt);
3738 		break;
3739 
3740 	case BT_OCF(BT_HCI_OP_LE_ISO_TEST_END):
3741 		le_iso_test_end(cmd, evt);
3742 		break;
3743 
3744 	case BT_OCF(BT_HCI_OP_LE_ISO_READ_TEST_COUNTERS):
3745 		le_iso_read_test_counters(cmd, evt);
3746 		break;
3747 
3748 	case BT_OCF(BT_HCI_OP_LE_READ_ISO_LINK_QUALITY):
3749 		le_read_iso_link_quality(cmd, evt);
3750 		break;
3751 #endif /* CONFIG_BT_CTLR_CONN_ISO */
3752 
3753 #if defined(CONFIG_BT_CTLR_SET_HOST_FEATURE)
3754 	case BT_OCF(BT_HCI_OP_LE_SET_HOST_FEATURE):
3755 		le_set_host_feature(cmd, evt);
3756 		break;
3757 #endif /* CONFIG_BT_CTLR_SET_HOST_FEATURE */
3758 
3759 	case BT_OCF(BT_HCI_OP_LE_READ_CHAN_MAP):
3760 		le_read_chan_map(cmd, evt);
3761 		break;
3762 
3763 	case BT_OCF(BT_HCI_OP_LE_READ_REMOTE_FEATURES):
3764 		le_read_remote_features(cmd, evt);
3765 		break;
3766 
3767 	case BT_OCF(BT_HCI_OP_LE_CONN_UPDATE):
3768 		le_conn_update(cmd, evt);
3769 		break;
3770 
3771 #if defined(CONFIG_BT_CTLR_CONN_PARAM_REQ)
3772 	case BT_OCF(BT_HCI_OP_LE_CONN_PARAM_REQ_REPLY):
3773 		le_conn_param_req_reply(cmd, evt);
3774 		break;
3775 
3776 	case BT_OCF(BT_HCI_OP_LE_CONN_PARAM_REQ_NEG_REPLY):
3777 		le_conn_param_req_neg_reply(cmd, evt);
3778 		break;
3779 #endif /* CONFIG_BT_CTLR_CONN_PARAM_REQ */
3780 
3781 #if defined(CONFIG_BT_CTLR_DATA_LENGTH)
3782 	case BT_OCF(BT_HCI_OP_LE_SET_DATA_LEN):
3783 		le_set_data_len(cmd, evt);
3784 		break;
3785 
3786 	case BT_OCF(BT_HCI_OP_LE_READ_DEFAULT_DATA_LEN):
3787 		le_read_default_data_len(cmd, evt);
3788 		break;
3789 
3790 	case BT_OCF(BT_HCI_OP_LE_WRITE_DEFAULT_DATA_LEN):
3791 		le_write_default_data_len(cmd, evt);
3792 		break;
3793 
3794 	case BT_OCF(BT_HCI_OP_LE_READ_MAX_DATA_LEN):
3795 		le_read_max_data_len(cmd, evt);
3796 		break;
3797 #endif /* CONFIG_BT_CTLR_DATA_LENGTH */
3798 
3799 #if defined(CONFIG_BT_CTLR_PHY)
3800 	case BT_OCF(BT_HCI_OP_LE_READ_PHY):
3801 		le_read_phy(cmd, evt);
3802 		break;
3803 
3804 	case BT_OCF(BT_HCI_OP_LE_SET_DEFAULT_PHY):
3805 		le_set_default_phy(cmd, evt);
3806 		break;
3807 
3808 	case BT_OCF(BT_HCI_OP_LE_SET_PHY):
3809 		le_set_phy(cmd, evt);
3810 		break;
3811 #endif /* CONFIG_BT_CTLR_PHY */
3812 #endif /* CONFIG_BT_CONN */
3813 
3814 #if defined(CONFIG_BT_CTLR_ADV_EXT)
3815 #if defined(CONFIG_BT_BROADCASTER)
3816 	case BT_OCF(BT_HCI_OP_LE_SET_ADV_SET_RANDOM_ADDR):
3817 		le_set_adv_set_random_addr(cmd, evt);
3818 		break;
3819 
3820 	case BT_OCF(BT_HCI_OP_LE_SET_EXT_ADV_PARAM):
3821 		le_set_ext_adv_param(cmd, evt);
3822 		break;
3823 
3824 	case BT_OCF(BT_HCI_OP_LE_SET_EXT_ADV_DATA):
3825 		le_set_ext_adv_data(cmd, evt);
3826 		break;
3827 
3828 	case BT_OCF(BT_HCI_OP_LE_SET_EXT_SCAN_RSP_DATA):
3829 		le_set_ext_scan_rsp_data(cmd, evt);
3830 		break;
3831 
3832 	case BT_OCF(BT_HCI_OP_LE_SET_EXT_ADV_ENABLE):
3833 		le_set_ext_adv_enable(cmd, evt);
3834 		break;
3835 
3836 	case BT_OCF(BT_HCI_OP_LE_READ_MAX_ADV_DATA_LEN):
3837 		le_read_max_adv_data_len(cmd, evt);
3838 		break;
3839 
3840 	case BT_OCF(BT_HCI_OP_LE_READ_NUM_ADV_SETS):
3841 		le_read_num_adv_sets(cmd, evt);
3842 		break;
3843 
3844 	case BT_OCF(BT_HCI_OP_LE_REMOVE_ADV_SET):
3845 		le_remove_adv_set(cmd, evt);
3846 		break;
3847 
3848 	case BT_OCF(BT_HCI_OP_CLEAR_ADV_SETS):
3849 		le_clear_adv_sets(cmd, evt);
3850 		break;
3851 
3852 #if defined(CONFIG_BT_CTLR_ADV_PERIODIC)
3853 	case BT_OCF(BT_HCI_OP_LE_SET_PER_ADV_PARAM):
3854 		le_set_per_adv_param(cmd, evt);
3855 		break;
3856 
3857 	case BT_OCF(BT_HCI_OP_LE_SET_PER_ADV_DATA):
3858 		le_set_per_adv_data(cmd, evt);
3859 		break;
3860 
3861 	case BT_OCF(BT_HCI_OP_LE_SET_PER_ADV_ENABLE):
3862 		le_set_per_adv_enable(cmd, evt);
3863 		break;
3864 #endif /* CONFIG_BT_CTLR_ADV_PERIODIC */
3865 #endif /* CONFIG_BT_BROADCASTER */
3866 
3867 #if defined(CONFIG_BT_OBSERVER)
3868 	case BT_OCF(BT_HCI_OP_LE_SET_EXT_SCAN_PARAM):
3869 		le_set_ext_scan_param(cmd, evt);
3870 		break;
3871 
3872 	case BT_OCF(BT_HCI_OP_LE_SET_EXT_SCAN_ENABLE):
3873 		le_set_ext_scan_enable(cmd, evt);
3874 		break;
3875 
3876 #if defined(CONFIG_BT_CTLR_SYNC_PERIODIC)
3877 	case BT_OCF(BT_HCI_OP_LE_PER_ADV_CREATE_SYNC):
3878 		le_per_adv_create_sync(cmd, evt);
3879 		break;
3880 
3881 	case BT_OCF(BT_HCI_OP_LE_PER_ADV_CREATE_SYNC_CANCEL):
3882 		le_per_adv_create_sync_cancel(cmd, evt, node_rx);
3883 		break;
3884 
3885 	case BT_OCF(BT_HCI_OP_LE_PER_ADV_TERMINATE_SYNC):
3886 		le_per_adv_terminate_sync(cmd, evt);
3887 		break;
3888 
3889 	case BT_OCF(BT_HCI_OP_LE_SET_PER_ADV_RECV_ENABLE):
3890 		le_per_adv_recv_enable(cmd, evt);
3891 		break;
3892 #endif /* CONFIG_BT_CTLR_SYNC_PERIODIC */
3893 #endif /* CONFIG_BT_OBSERVER */
3894 
3895 #if defined(CONFIG_BT_CONN)
3896 #if defined(CONFIG_BT_CENTRAL)
3897 	case BT_OCF(BT_HCI_OP_LE_EXT_CREATE_CONN):
3898 		le_ext_create_connection(cmd, evt);
3899 		break;
3900 #endif /* CONFIG_BT_CENTRAL */
3901 #endif /* CONFIG_BT_CONN */
3902 #endif /* CONFIG_BT_CTLR_ADV_EXT */
3903 
3904 #if defined(CONFIG_BT_CTLR_PRIVACY)
3905 	case BT_OCF(BT_HCI_OP_LE_ADD_DEV_TO_RL):
3906 		le_add_dev_to_rl(cmd, evt);
3907 		break;
3908 	case BT_OCF(BT_HCI_OP_LE_REM_DEV_FROM_RL):
3909 		le_rem_dev_from_rl(cmd, evt);
3910 		break;
3911 	case BT_OCF(BT_HCI_OP_LE_CLEAR_RL):
3912 		le_clear_rl(cmd, evt);
3913 		break;
3914 	case BT_OCF(BT_HCI_OP_LE_READ_RL_SIZE):
3915 		le_read_rl_size(cmd, evt);
3916 		break;
3917 	case BT_OCF(BT_HCI_OP_LE_READ_PEER_RPA):
3918 		le_read_peer_rpa(cmd, evt);
3919 		break;
3920 	case BT_OCF(BT_HCI_OP_LE_READ_LOCAL_RPA):
3921 		le_read_local_rpa(cmd, evt);
3922 		break;
3923 	case BT_OCF(BT_HCI_OP_LE_SET_ADDR_RES_ENABLE):
3924 		le_set_addr_res_enable(cmd, evt);
3925 		break;
3926 	case BT_OCF(BT_HCI_OP_LE_SET_RPA_TIMEOUT):
3927 		le_set_rpa_timeout(cmd, evt);
3928 		break;
3929 	case BT_OCF(BT_HCI_OP_LE_SET_PRIVACY_MODE):
3930 		le_set_privacy_mode(cmd, evt);
3931 		break;
3932 #endif /* CONFIG_BT_CTLR_PRIVACY */
3933 
3934 	case BT_OCF(BT_HCI_OP_LE_READ_TX_POWER):
3935 		le_read_tx_power(cmd, evt);
3936 		break;
3937 
3938 #if defined(CONFIG_BT_CTLR_DF)
3939 #if defined(CONFIG_BT_CTLR_DF_ADV_CTE_TX)
3940 	case BT_OCF(BT_HCI_OP_LE_SET_CL_CTE_TX_PARAMS):
3941 		le_df_set_cl_cte_tx_params(cmd, evt);
3942 		break;
3943 	case BT_OCF(BT_HCI_OP_LE_SET_CL_CTE_TX_ENABLE):
3944 		le_df_set_cl_cte_enable(cmd, evt);
3945 		break;
3946 #endif /* CONFIG_BT_CTLR_DF_ADV_CTE_TX */
3947 #if defined(CONFIG_BT_CTLR_DF_SCAN_CTE_RX)
3948 	case BT_OCF(BT_HCI_OP_LE_SET_CL_CTE_SAMPLING_ENABLE):
3949 		le_df_set_cl_iq_sampling_enable(cmd, evt);
3950 		break;
3951 #endif /* CONFIG_BT_CTLR_DF_SCAN_CTE_RX */
3952 	case BT_OCF(BT_HCI_OP_LE_READ_ANT_INFO):
3953 		le_df_read_ant_inf(cmd, evt);
3954 		break;
3955 #if defined(CONFIG_BT_CTLR_DF_CONN_CTE_RSP)
3956 	case BT_OCF(BT_HCI_OP_LE_SET_CONN_CTE_TX_PARAMS):
3957 		le_df_set_conn_cte_tx_params(cmd, evt);
3958 		break;
3959 #endif /* CONFIG_BT_CTLR_DF_CONN_CTE_RSP */
3960 #if defined(CONFIG_BT_CTLR_DF_CONN_CTE_REQ)
3961 	case BT_OCF(BT_HCI_OP_LE_SET_CONN_CTE_RX_PARAMS):
3962 		le_df_set_conn_cte_rx_params(cmd, evt);
3963 		break;
3964 	case BT_OCF(BT_HCI_OP_LE_CONN_CTE_REQ_ENABLE):
3965 		le_df_set_conn_cte_req_enable(cmd, evt);
3966 		break;
3967 #endif /* CONFIG_BT_CTLR_DF_CONN_CTE_REQ */
3968 #endif /* CONFIG_BT_CTLR_DF */
3969 
3970 #if defined(CONFIG_BT_CTLR_DTM_HCI)
3971 	case BT_OCF(BT_HCI_OP_LE_RX_TEST):
3972 		le_rx_test(cmd, evt);
3973 		break;
3974 	case BT_OCF(BT_HCI_OP_LE_TX_TEST):
3975 		le_tx_test(cmd, evt);
3976 		break;
3977 	case BT_OCF(BT_HCI_OP_LE_TEST_END):
3978 		le_test_end(cmd, evt);
3979 		break;
3980 	case BT_OCF(BT_HCI_OP_LE_ENH_RX_TEST):
3981 		le_enh_rx_test(cmd, evt);
3982 		break;
3983 	case BT_OCF(BT_HCI_OP_LE_ENH_TX_TEST):
3984 		le_enh_tx_test(cmd, evt);
3985 		break;
3986 #endif /* CONFIG_BT_CTLR_DTM_HCI */
3987 
3988 	default:
3989 		return -EINVAL;
3990 	}
3991 
3992 	return 0;
3993 }
3994 
3995 #if defined(CONFIG_BT_HCI_VS)
3996 static void vs_read_version_info(struct net_buf *buf, struct net_buf **evt)
3997 {
3998 	struct bt_hci_rp_vs_read_version_info *rp;
3999 
4000 	rp = hci_cmd_complete(evt, sizeof(*rp));
4001 
4002 	rp->status = 0x00;
4003 	rp->hw_platform = sys_cpu_to_le16(BT_HCI_VS_HW_PLAT);
4004 	rp->hw_variant = sys_cpu_to_le16(BT_HCI_VS_HW_VAR);
4005 
4006 	rp->fw_variant = 0U;
4007 	rp->fw_version = (KERNEL_VERSION_MAJOR & 0xff);
4008 	rp->fw_revision = sys_cpu_to_le16(KERNEL_VERSION_MINOR);
4009 	rp->fw_build = sys_cpu_to_le32(KERNEL_PATCHLEVEL & 0xffff);
4010 }
4011 
4012 static void vs_read_supported_commands(struct net_buf *buf,
4013 				       struct net_buf **evt)
4014 {
4015 	struct bt_hci_rp_vs_read_supported_commands *rp;
4016 
4017 	rp = hci_cmd_complete(evt, sizeof(*rp));
4018 
4019 	rp->status = 0x00;
4020 	(void)memset(&rp->commands[0], 0, sizeof(rp->commands));
4021 
4022 	/* Set Version Information, Supported Commands, Supported Features. */
4023 	rp->commands[0] |= BIT(0) | BIT(1) | BIT(2);
4024 #if defined(CONFIG_BT_HCI_VS_EXT)
4025 	/* Write BD_ADDR, Read Build Info */
4026 	rp->commands[0] |= BIT(5) | BIT(7);
4027 	/* Read Static Addresses, Read Key Hierarchy Roots */
4028 	rp->commands[1] |= BIT(0) | BIT(1);
4029 #if defined(CONFIG_BT_CTLR_TX_PWR_DYNAMIC_CONTROL)
4030 	/* Write Tx Power, Read Tx Power */
4031 	rp->commands[1] |= BIT(5) | BIT(6);
4032 #endif /* CONFIG_BT_CTLR_TX_PWR_DYNAMIC_CONTROL */
4033 #if defined(CONFIG_USB_DEVICE_BLUETOOTH_VS_H4)
4034 	/* Read Supported USB Transport Modes */
4035 	rp->commands[1] |= BIT(7);
4036 	/* Set USB Transport Mode */
4037 	rp->commands[2] |= BIT(0);
4038 #endif /* USB_DEVICE_BLUETOOTH_VS_H4 */
4039 #endif /* CONFIG_BT_HCI_VS_EXT */
4040 }
4041 
4042 static void vs_read_supported_features(struct net_buf *buf,
4043 				       struct net_buf **evt)
4044 {
4045 	struct bt_hci_rp_vs_read_supported_features *rp;
4046 
4047 	rp = hci_cmd_complete(evt, sizeof(*rp));
4048 
4049 	rp->status = 0x00;
4050 	(void)memset(&rp->features[0], 0x00, sizeof(rp->features));
4051 }
4052 
4053 uint8_t __weak hci_vendor_read_static_addr(struct bt_hci_vs_static_addr addrs[],
4054 					uint8_t size)
4055 {
4056 	ARG_UNUSED(addrs);
4057 	ARG_UNUSED(size);
4058 
4059 	return 0;
4060 }
4061 
4062 /* If Zephyr VS HCI commands are not enabled provide this functionality directly
4063  */
4064 #if !defined(CONFIG_BT_HCI_VS_EXT)
4065 uint8_t bt_read_static_addr(struct bt_hci_vs_static_addr addrs[], uint8_t size)
4066 {
4067 	return hci_vendor_read_static_addr(addrs, size);
4068 }
4069 #endif /* !defined(CONFIG_BT_HCI_VS_EXT) */
4070 
4071 #if defined(CONFIG_BT_HCI_VS_EXT)
4072 static void vs_write_bd_addr(struct net_buf *buf, struct net_buf **evt)
4073 {
4074 	struct bt_hci_cp_vs_write_bd_addr *cmd = (void *)buf->data;
4075 
4076 	ll_addr_set(0, &cmd->bdaddr.val[0]);
4077 
4078 	*evt = cmd_complete_status(0x00);
4079 }
4080 
4081 static void vs_read_build_info(struct net_buf *buf, struct net_buf **evt)
4082 {
4083 	struct bt_hci_rp_vs_read_build_info *rp;
4084 
4085 #define HCI_VS_BUILD_INFO "Zephyr OS v" \
4086 	KERNEL_VERSION_STRING CONFIG_BT_CTLR_HCI_VS_BUILD_INFO
4087 
4088 	const char build_info[] = HCI_VS_BUILD_INFO;
4089 
4090 #define BUILD_INFO_EVT_LEN (sizeof(struct bt_hci_evt_hdr) + \
4091 			    sizeof(struct bt_hci_evt_cmd_complete) + \
4092 			    sizeof(struct bt_hci_rp_vs_read_build_info) + \
4093 			    sizeof(build_info))
4094 
4095 	BUILD_ASSERT(CONFIG_BT_BUF_EVT_RX_SIZE >= BUILD_INFO_EVT_LEN);
4096 
4097 	rp = hci_cmd_complete(evt, sizeof(*rp) + sizeof(build_info));
4098 	rp->status = 0x00;
4099 	memcpy(rp->info, build_info, sizeof(build_info));
4100 }
4101 
4102 void __weak hci_vendor_read_key_hierarchy_roots(uint8_t ir[16], uint8_t er[16])
4103 {
4104 	/* Mark IR as invalid */
4105 	(void)memset(ir, 0x00, 16);
4106 
4107 	/* Mark ER as invalid */
4108 	(void)memset(er, 0x00, 16);
4109 }
4110 
4111 static void vs_read_static_addrs(struct net_buf *buf, struct net_buf **evt)
4112 {
4113 	struct bt_hci_rp_vs_read_static_addrs *rp;
4114 
4115 	rp = hci_cmd_complete(evt, sizeof(*rp) +
4116 				   sizeof(struct bt_hci_vs_static_addr));
4117 	rp->status = 0x00;
4118 	rp->num_addrs = hci_vendor_read_static_addr(rp->a, 1);
4119 }
4120 
4121 static void vs_read_key_hierarchy_roots(struct net_buf *buf,
4122 					struct net_buf **evt)
4123 {
4124 	struct bt_hci_rp_vs_read_key_hierarchy_roots *rp;
4125 
4126 	rp = hci_cmd_complete(evt, sizeof(*rp));
4127 	rp->status = 0x00;
4128 	hci_vendor_read_key_hierarchy_roots(rp->ir, rp->er);
4129 }
4130 
4131 #if defined(CONFIG_BT_CTLR_TX_PWR_DYNAMIC_CONTROL)
4132 static void vs_write_tx_power_level(struct net_buf *buf, struct net_buf **evt)
4133 {
4134 	struct bt_hci_cp_vs_write_tx_power_level *cmd = (void *)buf->data;
4135 	struct bt_hci_rp_vs_write_tx_power_level *rp;
4136 	uint8_t handle_type;
4137 	uint16_t handle;
4138 	uint8_t status;
4139 
4140 	handle_type = cmd->handle_type;
4141 	handle = sys_le16_to_cpu(cmd->handle);
4142 
4143 	rp = hci_cmd_complete(evt, sizeof(*rp));
4144 	rp->selected_tx_power = cmd->tx_power_level;
4145 
4146 	status = ll_tx_pwr_lvl_set(handle_type, handle, &rp->selected_tx_power);
4147 
4148 	rp->status = status;
4149 	rp->handle_type = handle_type;
4150 	rp->handle = sys_cpu_to_le16(handle);
4151 }
4152 
4153 static void vs_read_tx_power_level(struct net_buf *buf, struct net_buf **evt)
4154 {
4155 	struct bt_hci_cp_vs_read_tx_power_level *cmd = (void *)buf->data;
4156 	struct bt_hci_rp_vs_read_tx_power_level *rp;
4157 	uint8_t handle_type;
4158 	uint16_t handle;
4159 	uint8_t status;
4160 
4161 	handle_type = cmd->handle_type;
4162 	handle = sys_le16_to_cpu(cmd->handle);
4163 
4164 	rp = hci_cmd_complete(evt, sizeof(*rp));
4165 
4166 	status = ll_tx_pwr_lvl_get(handle_type, handle, 0, &rp->tx_power_level);
4167 
4168 	rp->status = status;
4169 	rp->handle_type = handle_type;
4170 	rp->handle = sys_cpu_to_le16(handle);
4171 }
4172 #endif /* CONFIG_BT_CTLR_TX_PWR_DYNAMIC_CONTROL */
4173 #endif /* CONFIG_BT_HCI_VS_EXT */
4174 
4175 #if defined(CONFIG_BT_HCI_MESH_EXT)
4176 static void mesh_get_opts(struct net_buf *buf, struct net_buf **evt)
4177 {
4178 	struct bt_hci_rp_mesh_get_opts *rp;
4179 
4180 	rp = hci_cmd_complete(evt, sizeof(*rp));
4181 
4182 	rp->status = 0x00;
4183 	rp->opcode = BT_HCI_OC_MESH_GET_OPTS;
4184 
4185 	rp->revision = BT_HCI_MESH_REVISION;
4186 	rp->ch_map = 0x7;
4187 	/*@todo: nRF51 only */
4188 	rp->min_tx_power = -30;
4189 	/*@todo: nRF51 only */
4190 	rp->max_tx_power = 4;
4191 	rp->max_scan_filter = CONFIG_BT_CTLR_MESH_SCAN_FILTERS;
4192 	rp->max_filter_pattern = CONFIG_BT_CTLR_MESH_SF_PATTERNS;
4193 	rp->max_adv_slot = 1U;
4194 	rp->evt_prefix_len = 0x01;
4195 	rp->evt_prefix = BT_HCI_MESH_EVT_PREFIX;
4196 }
4197 
4198 static void mesh_set_scan_filter(struct net_buf *buf, struct net_buf **evt)
4199 {
4200 	struct bt_hci_cp_mesh_set_scan_filter *cmd = (void *)buf->data;
4201 	struct bt_hci_rp_mesh_set_scan_filter *rp;
4202 	uint8_t filter = cmd->scan_filter - 1;
4203 	struct scan_filter *f;
4204 	uint8_t status = 0x00;
4205 	uint8_t i;
4206 
4207 	if (filter > ARRAY_SIZE(scan_filters) ||
4208 	    cmd->num_patterns > CONFIG_BT_CTLR_MESH_SF_PATTERNS) {
4209 		status = BT_HCI_ERR_INVALID_PARAM;
4210 		goto exit;
4211 	}
4212 
4213 	if (filter == sf_curr) {
4214 		status = BT_HCI_ERR_CMD_DISALLOWED;
4215 		goto exit;
4216 	}
4217 
4218 	/* duplicate filtering not supported yet */
4219 	if (cmd->filter_dup) {
4220 		status = BT_HCI_ERR_INVALID_PARAM;
4221 		goto exit;
4222 	}
4223 
4224 	f = &scan_filters[filter];
4225 	for (i = 0U; i < cmd->num_patterns; i++) {
4226 		if (!cmd->patterns[i].pattern_len ||
4227 		    cmd->patterns[i].pattern_len >
4228 		    BT_HCI_MESH_PATTERN_LEN_MAX) {
4229 			status = BT_HCI_ERR_INVALID_PARAM;
4230 			goto exit;
4231 		}
4232 		f->lengths[i] = cmd->patterns[i].pattern_len;
4233 		memcpy(f->patterns[i], cmd->patterns[i].pattern, f->lengths[i]);
4234 	}
4235 
4236 	f->count = cmd->num_patterns;
4237 
4238 exit:
4239 	rp = hci_cmd_complete(evt, sizeof(*rp));
4240 	rp->status = status;
4241 	rp->opcode = BT_HCI_OC_MESH_SET_SCAN_FILTER;
4242 	rp->scan_filter = filter + 1;
4243 }
4244 
4245 static void mesh_advertise(struct net_buf *buf, struct net_buf **evt)
4246 {
4247 	struct bt_hci_cp_mesh_advertise *cmd = (void *)buf->data;
4248 	struct bt_hci_rp_mesh_advertise *rp;
4249 	uint8_t adv_slot = cmd->adv_slot;
4250 	uint8_t status;
4251 
4252 	status = ll_mesh_advertise(adv_slot,
4253 				   cmd->own_addr_type, cmd->random_addr.val,
4254 				   cmd->ch_map, cmd->tx_power,
4255 				   cmd->min_tx_delay, cmd->max_tx_delay,
4256 				   cmd->retx_count, cmd->retx_interval,
4257 				   cmd->scan_duration, cmd->scan_delay,
4258 				   cmd->scan_filter, cmd->data_len, cmd->data);
4259 	if (!status) {
4260 		/* Yields 0xFF if no scan filter selected */
4261 		sf_curr = cmd->scan_filter - 1;
4262 	}
4263 
4264 	rp = hci_cmd_complete(evt, sizeof(*rp));
4265 	rp->status = status;
4266 	rp->opcode = BT_HCI_OC_MESH_ADVERTISE;
4267 	rp->adv_slot = adv_slot;
4268 }
4269 
4270 static void mesh_advertise_cancel(struct net_buf *buf, struct net_buf **evt)
4271 {
4272 	struct bt_hci_cp_mesh_advertise_cancel *cmd = (void *)buf->data;
4273 	struct bt_hci_rp_mesh_advertise_cancel *rp;
4274 	uint8_t adv_slot = cmd->adv_slot;
4275 	uint8_t status;
4276 
4277 	status = ll_mesh_advertise_cancel(adv_slot);
4278 	if (!status) {
4279 		/* Yields 0xFF if no scan filter selected */
4280 		sf_curr = 0xFF;
4281 	}
4282 
4283 	rp = hci_cmd_complete(evt, sizeof(*rp));
4284 	rp->status = status;
4285 	rp->opcode = BT_HCI_OC_MESH_ADVERTISE_CANCEL;
4286 	rp->adv_slot = adv_slot;
4287 }
4288 
4289 static int mesh_cmd_handle(struct net_buf *cmd, struct net_buf **evt)
4290 {
4291 	struct bt_hci_cp_mesh *cp_mesh;
4292 	uint8_t mesh_op;
4293 
4294 	if (cmd->len < sizeof(*cp_mesh)) {
4295 		BT_ERR("No HCI VSD Command header");
4296 		return -EINVAL;
4297 	}
4298 
4299 	cp_mesh = net_buf_pull_mem(cmd, sizeof(*cp_mesh));
4300 	mesh_op = cp_mesh->opcode;
4301 
4302 	switch (mesh_op) {
4303 	case BT_HCI_OC_MESH_GET_OPTS:
4304 		mesh_get_opts(cmd, evt);
4305 		break;
4306 
4307 	case BT_HCI_OC_MESH_SET_SCAN_FILTER:
4308 		mesh_set_scan_filter(cmd, evt);
4309 		break;
4310 
4311 	case BT_HCI_OC_MESH_ADVERTISE:
4312 		mesh_advertise(cmd, evt);
4313 		break;
4314 
4315 	case BT_HCI_OC_MESH_ADVERTISE_CANCEL:
4316 		mesh_advertise_cancel(cmd, evt);
4317 		break;
4318 
4319 	default:
4320 		return -EINVAL;
4321 	}
4322 
4323 	return 0;
4324 }
4325 #endif /* CONFIG_BT_HCI_MESH_EXT */
4326 
4327 int hci_vendor_cmd_handle_common(uint16_t ocf, struct net_buf *cmd,
4328 				 struct net_buf **evt)
4329 {
4330 	switch (ocf) {
4331 	case BT_OCF(BT_HCI_OP_VS_READ_VERSION_INFO):
4332 		vs_read_version_info(cmd, evt);
4333 		break;
4334 
4335 	case BT_OCF(BT_HCI_OP_VS_READ_SUPPORTED_COMMANDS):
4336 		vs_read_supported_commands(cmd, evt);
4337 		break;
4338 
4339 	case BT_OCF(BT_HCI_OP_VS_READ_SUPPORTED_FEATURES):
4340 		vs_read_supported_features(cmd, evt);
4341 		break;
4342 
4343 #if defined(CONFIG_USB_DEVICE_BLUETOOTH_VS_H4)
4344 	case BT_OCF(BT_HCI_OP_VS_READ_USB_TRANSPORT_MODE):
4345 		break;
4346 	case BT_OCF(BT_HCI_OP_VS_SET_USB_TRANSPORT_MODE):
4347 		reset(cmd, evt);
4348 		break;
4349 #endif /* CONFIG_USB_DEVICE_BLUETOOTH_VS_H4 */
4350 
4351 #if defined(CONFIG_BT_HCI_VS_EXT)
4352 	case BT_OCF(BT_HCI_OP_VS_READ_BUILD_INFO):
4353 		vs_read_build_info(cmd, evt);
4354 		break;
4355 
4356 	case BT_OCF(BT_HCI_OP_VS_WRITE_BD_ADDR):
4357 		vs_write_bd_addr(cmd, evt);
4358 		break;
4359 
4360 	case BT_OCF(BT_HCI_OP_VS_READ_STATIC_ADDRS):
4361 		vs_read_static_addrs(cmd, evt);
4362 		break;
4363 
4364 	case BT_OCF(BT_HCI_OP_VS_READ_KEY_HIERARCHY_ROOTS):
4365 		vs_read_key_hierarchy_roots(cmd, evt);
4366 		break;
4367 
4368 #if defined(CONFIG_BT_CTLR_TX_PWR_DYNAMIC_CONTROL)
4369 	case BT_OCF(BT_HCI_OP_VS_WRITE_TX_POWER_LEVEL):
4370 		vs_write_tx_power_level(cmd, evt);
4371 		break;
4372 
4373 	case BT_OCF(BT_HCI_OP_VS_READ_TX_POWER_LEVEL):
4374 		vs_read_tx_power_level(cmd, evt);
4375 		break;
4376 #endif /* CONFIG_BT_CTLR_TX_PWR_DYNAMIC_CONTROL */
4377 #endif /* CONFIG_BT_HCI_VS_EXT */
4378 
4379 #if defined(CONFIG_BT_HCI_MESH_EXT)
4380 	case BT_OCF(BT_HCI_OP_VS_MESH):
4381 		mesh_cmd_handle(cmd, evt);
4382 		break;
4383 #endif /* CONFIG_BT_HCI_MESH_EXT */
4384 
4385 	default:
4386 		return -EINVAL;
4387 	}
4388 
4389 	return 0;
4390 }
4391 #endif
4392 
4393 struct net_buf *hci_cmd_handle(struct net_buf *cmd, void **node_rx)
4394 {
4395 	struct bt_hci_cmd_hdr *chdr;
4396 	struct net_buf *evt = NULL;
4397 	uint16_t ocf;
4398 	int err;
4399 
4400 	if (cmd->len < sizeof(*chdr)) {
4401 		BT_ERR("No HCI Command header");
4402 		return NULL;
4403 	}
4404 
4405 	chdr = net_buf_pull_mem(cmd, sizeof(*chdr));
4406 	if (cmd->len < chdr->param_len) {
4407 		BT_ERR("Invalid HCI CMD packet length");
4408 		return NULL;
4409 	}
4410 
4411 	/* store in a global for later CC/CS event creation */
4412 	_opcode = sys_le16_to_cpu(chdr->opcode);
4413 
4414 	ocf = BT_OCF(_opcode);
4415 
4416 	switch (BT_OGF(_opcode)) {
4417 	case BT_OGF_LINK_CTRL:
4418 		err = link_control_cmd_handle(ocf, cmd, &evt);
4419 		break;
4420 	case BT_OGF_BASEBAND:
4421 		err = ctrl_bb_cmd_handle(ocf, cmd, &evt);
4422 		break;
4423 	case BT_OGF_INFO:
4424 		err = info_cmd_handle(ocf, cmd, &evt);
4425 		break;
4426 	case BT_OGF_STATUS:
4427 		err = status_cmd_handle(ocf, cmd, &evt);
4428 		break;
4429 	case BT_OGF_LE:
4430 		err = controller_cmd_handle(ocf, cmd, &evt, node_rx);
4431 		break;
4432 #if defined(CONFIG_BT_HCI_VS)
4433 	case BT_OGF_VS:
4434 		err = hci_vendor_cmd_handle(ocf, cmd, &evt);
4435 		break;
4436 #endif
4437 	default:
4438 		err = -EINVAL;
4439 		break;
4440 	}
4441 
4442 	if (err == -EINVAL) {
4443 		evt = cmd_status(BT_HCI_ERR_UNKNOWN_CMD);
4444 	}
4445 
4446 	return evt;
4447 }
4448 
4449 #if defined(CONFIG_BT_CONN)
4450 static void data_buf_overflow(struct net_buf **buf)
4451 {
4452 	struct bt_hci_evt_data_buf_overflow *ep;
4453 
4454 	if (!(event_mask & BT_EVT_MASK_DATA_BUFFER_OVERFLOW)) {
4455 		return;
4456 	}
4457 
4458 	*buf = bt_buf_get_rx(BT_BUF_EVT, K_FOREVER);
4459 	hci_evt_create(*buf, BT_HCI_EVT_DATA_BUF_OVERFLOW, sizeof(*ep));
4460 	ep = net_buf_add(*buf, sizeof(*ep));
4461 
4462 	ep->link_type = BT_OVERFLOW_LINK_ACL;
4463 }
4464 
4465 int hci_acl_handle(struct net_buf *buf, struct net_buf **evt)
4466 {
4467 	struct node_tx *node_tx;
4468 	struct bt_hci_acl_hdr *acl;
4469 	struct pdu_data *pdu_data;
4470 	uint16_t handle;
4471 	uint8_t flags;
4472 	uint16_t len;
4473 
4474 	*evt = NULL;
4475 
4476 	if (buf->len < sizeof(*acl)) {
4477 		BT_ERR("No HCI ACL header");
4478 		return -EINVAL;
4479 	}
4480 
4481 	acl = net_buf_pull_mem(buf, sizeof(*acl));
4482 	len = sys_le16_to_cpu(acl->len);
4483 	handle = sys_le16_to_cpu(acl->handle);
4484 
4485 	if (buf->len < len) {
4486 		BT_ERR("Invalid HCI ACL packet length");
4487 		return -EINVAL;
4488 	}
4489 
4490 	if (len > CONFIG_BT_BUF_ACL_TX_SIZE) {
4491 		BT_ERR("Invalid HCI ACL Data length");
4492 		return -EINVAL;
4493 	}
4494 
4495 	/* assigning flags first because handle will be overwritten */
4496 	flags = bt_acl_flags(handle);
4497 	handle = bt_acl_handle(handle);
4498 
4499 	node_tx = ll_tx_mem_acquire();
4500 	if (!node_tx) {
4501 		BT_ERR("Tx Buffer Overflow");
4502 		data_buf_overflow(evt);
4503 		return -ENOBUFS;
4504 	}
4505 
4506 	pdu_data = (void *)node_tx->pdu;
4507 
4508 	if (bt_acl_flags_bc(flags) != BT_ACL_POINT_TO_POINT) {
4509 		return -EINVAL;
4510 	}
4511 
4512 	switch (bt_acl_flags_pb(flags)) {
4513 	case BT_ACL_START_NO_FLUSH:
4514 		pdu_data->ll_id = PDU_DATA_LLID_DATA_START;
4515 		break;
4516 	case BT_ACL_CONT:
4517 		pdu_data->ll_id = PDU_DATA_LLID_DATA_CONTINUE;
4518 		break;
4519 	default:
4520 		/* BT_ACL_START and BT_ACL_COMPLETE not allowed on LE-U
4521 		 * from Host to Controller
4522 		 */
4523 		return -EINVAL;
4524 	}
4525 
4526 	pdu_data->len = len;
4527 	memcpy(&pdu_data->lldata[0], buf->data, len);
4528 
4529 	if (ll_tx_mem_enqueue(handle, node_tx)) {
4530 		BT_ERR("Invalid Tx Enqueue");
4531 		ll_tx_mem_release(node_tx);
4532 		return -EINVAL;
4533 	}
4534 
4535 	return 0;
4536 }
4537 #endif /* CONFIG_BT_CONN */
4538 
4539 #if CONFIG_BT_CTLR_DUP_FILTER_LEN > 0
4540 #if defined(CONFIG_BT_CTLR_ADV_EXT)
4541 static void store_adi(int i, struct pdu_adv_adi *adi)
4542 {
4543 	if (adi) {
4544 		memcpy(&dup_filter[i].adi, adi, sizeof(*adi));
4545 	} else {
4546 		memset(&dup_filter[i].adi, 0, sizeof(*adi));
4547 	}
4548 }
4549 #endif /* CONFIG_BT_CTLR_ADV_EXT */
4550 
4551 static inline bool is_dup_or_update(int i, uint8_t adv_type, uint8_t adv_mode,
4552 				    struct pdu_adv_adi *adi,
4553 				    uint8_t data_status)
4554 {
4555 	if (!(dup_filter[i].mask & BIT(adv_type))) {
4556 		/* report different adv types */
4557 		dup_filter[i].mask |= BIT(adv_type);
4558 
4559 #if defined(CONFIG_BT_CTLR_ADV_EXT)
4560 		dup_filter[i].adv_mode = adv_mode;
4561 		dup_filter[i].data_cmplt = !data_status;
4562 		store_adi(i, adi);
4563 
4564 		return false;
4565 	} else if (dup_filter[i].adv_mode != adv_mode) {
4566 		/* report different adv mode */
4567 		dup_filter[i].adv_mode = adv_mode;
4568 
4569 		dup_filter[i].data_cmplt = !data_status;
4570 		store_adi(i, adi);
4571 
4572 		return false;
4573 	} else if (adi && ((dup_filter[i].adi.sid != adi->sid) ||
4574 			   (dup_filter[i].adi.did != adi->did))) {
4575 		/* report different adi */
4576 		store_adi(i, adi);
4577 
4578 		dup_filter[i].data_cmplt = !data_status;
4579 
4580 		return false;
4581 	} else if (!dup_filter[i].data_cmplt && !data_status) {
4582 		/* report data complete */
4583 		dup_filter[i].data_cmplt = !data_status;
4584 #endif /* CONFIG_BT_CTLR_ADV_EXT */
4585 
4586 		return false;
4587 	}
4588 
4589 	return true;
4590 }
4591 
4592 static bool dup_found(uint8_t adv_type, uint8_t addr_type, uint8_t *addr,
4593 		      uint8_t adv_mode, struct pdu_adv_adi *adi,
4594 		      uint8_t data_status)
4595 {
4596 	/* check for duplicate filtering */
4597 	if (dup_count >= 0) {
4598 		int i;
4599 
4600 		/* find for existing entry and update if changed */
4601 		for (i = 0; i < dup_count; i++) {
4602 			if (memcmp(addr, &dup_filter[i].addr.a.val[0],
4603 				   sizeof(bt_addr_t)) ||
4604 			    (addr_type != dup_filter[i].addr.type)) {
4605 				continue;
4606 			}
4607 
4608 			/* still duplicate or update entry with change */
4609 			return is_dup_or_update(i, adv_type, adv_mode, adi,
4610 						data_status);
4611 		}
4612 
4613 		/* insert into the duplicate filter */
4614 		memcpy(&dup_filter[dup_curr].addr.a.val[0], addr,
4615 		       sizeof(bt_addr_t));
4616 		dup_filter[dup_curr].addr.type = addr_type;
4617 		dup_filter[dup_curr].mask = BIT(adv_type);
4618 
4619 #if defined(CONFIG_BT_CTLR_ADV_EXT)
4620 		dup_filter[dup_curr].adv_mode = adv_mode;
4621 		dup_filter[i].data_cmplt = !data_status;
4622 		store_adi(dup_curr, adi);
4623 #endif /* CONFIG_BT_CTLR_ADV_EXT */
4624 
4625 		if (dup_count < CONFIG_BT_CTLR_DUP_FILTER_LEN) {
4626 			dup_count++;
4627 			dup_curr = dup_count;
4628 		} else {
4629 			dup_curr++;
4630 		}
4631 
4632 		if (dup_curr == CONFIG_BT_CTLR_DUP_FILTER_LEN) {
4633 			dup_curr = 0U;
4634 		}
4635 	}
4636 
4637 	return false;
4638 }
4639 #endif /* CONFIG_BT_CTLR_DUP_FILTER_LEN > 0 */
4640 
4641 #if defined(CONFIG_BT_CTLR_EXT_SCAN_FP)
4642 static inline void le_dir_adv_report(struct pdu_adv *adv, struct net_buf *buf,
4643 				     int8_t rssi, uint8_t rl_idx)
4644 {
4645 	struct bt_hci_evt_le_direct_adv_report *drp;
4646 	struct bt_hci_evt_le_direct_adv_info *dir_info;
4647 
4648 	if (!(event_mask & BT_EVT_MASK_LE_META_EVENT) ||
4649 	    !(le_event_mask & BT_EVT_MASK_LE_DIRECT_ADV_REPORT)) {
4650 		return;
4651 	}
4652 
4653 	LL_ASSERT(adv->type == PDU_ADV_TYPE_DIRECT_IND);
4654 
4655 #if CONFIG_BT_CTLR_DUP_FILTER_LEN > 0
4656 	if (dup_found(adv->type, adv->tx_addr, adv->adv_ind.addr, 0, NULL, 0)) {
4657 		return;
4658 	}
4659 #endif /* CONFIG_BT_CTLR_DUP_FILTER_LEN > 0 */
4660 
4661 	drp = meta_evt(buf, BT_HCI_EVT_LE_DIRECT_ADV_REPORT,
4662 		       sizeof(*drp) + sizeof(*dir_info));
4663 
4664 	drp->num_reports = 1U;
4665 	dir_info = (void *)(((uint8_t *)drp) + sizeof(*drp));
4666 
4667 	/* Directed Advertising */
4668 	dir_info->evt_type = BT_HCI_ADV_DIRECT_IND;
4669 
4670 #if defined(CONFIG_BT_CTLR_PRIVACY)
4671 	if (rl_idx < ll_rl_size_get()) {
4672 		/* Store identity address */
4673 		ll_rl_id_addr_get(rl_idx, &dir_info->addr.type,
4674 				  &dir_info->addr.a.val[0]);
4675 		/* Mark it as identity address from RPA (0x02, 0x03) */
4676 		dir_info->addr.type += 2U;
4677 	} else {
4678 #else
4679 	if (1) {
4680 #endif /* CONFIG_BT_CTLR_PRIVACY */
4681 		dir_info->addr.type = adv->tx_addr;
4682 		memcpy(&dir_info->addr.a.val[0], &adv->direct_ind.adv_addr[0],
4683 		       sizeof(bt_addr_t));
4684 	}
4685 
4686 	dir_info->dir_addr.type = adv->rx_addr;
4687 	memcpy(&dir_info->dir_addr.a.val[0],
4688 	       &adv->direct_ind.tgt_addr[0], sizeof(bt_addr_t));
4689 
4690 	dir_info->rssi = rssi;
4691 }
4692 #endif /* CONFIG_BT_CTLR_EXT_SCAN_FP */
4693 
4694 #if defined(CONFIG_BT_OBSERVER)
4695 #if defined(CONFIG_BT_HCI_MESH_EXT)
4696 static inline bool scan_filter_apply(uint8_t filter, uint8_t *data, uint8_t len)
4697 {
4698 	struct scan_filter *f = &scan_filters[filter];
4699 	int i;
4700 
4701 	/* No patterns means filter out all advertising packets */
4702 	for (i = 0; i < f->count; i++) {
4703 		/* Require at least the length of the pattern */
4704 		if (len >= f->lengths[i] &&
4705 		    !memcmp(data, f->patterns[i], f->lengths[i])) {
4706 			return true;
4707 		}
4708 	}
4709 
4710 	return false;
4711 }
4712 
4713 static inline void le_mesh_scan_report(struct pdu_adv *adv,
4714 				       struct node_rx_pdu *node_rx,
4715 				       struct net_buf *buf, int8_t rssi)
4716 {
4717 	uint8_t data_len = (adv->len - BDADDR_SIZE);
4718 	struct bt_hci_evt_mesh_scanning_report *mep;
4719 	struct bt_hci_evt_mesh_scan_report *sr;
4720 	uint32_t instant;
4721 	uint8_t chan;
4722 
4723 	LL_ASSERT(adv->type == PDU_ADV_TYPE_NONCONN_IND);
4724 
4725 	/* Filter based on currently active Scan Filter */
4726 	if (sf_curr < ARRAY_SIZE(scan_filters) &&
4727 	    !scan_filter_apply(sf_curr, &adv->adv_ind.data[0], data_len)) {
4728 		/* Drop the report */
4729 		return;
4730 	}
4731 
4732 	chan = node_rx->hdr.rx_ftr.chan;
4733 	instant = node_rx->hdr.rx_ftr.anchor_ticks;
4734 
4735 	mep = mesh_evt(buf, BT_HCI_EVT_MESH_SCANNING_REPORT,
4736 			    sizeof(*mep) + sizeof(*sr));
4737 
4738 	mep->num_reports = 1U;
4739 	sr = (void *)(((uint8_t *)mep) + sizeof(*mep));
4740 	sr->addr.type = adv->tx_addr;
4741 	memcpy(&sr->addr.a.val[0], &adv->adv_ind.addr[0], sizeof(bt_addr_t));
4742 	sr->chan = chan;
4743 	sr->rssi = rssi;
4744 	sys_put_le32(instant, (uint8_t *)&sr->instant);
4745 
4746 	sr->data_len = data_len;
4747 	memcpy(&sr->data[0], &adv->adv_ind.data[0], data_len);
4748 }
4749 #endif /* CONFIG_BT_HCI_MESH_EXT */
4750 
4751 static void le_advertising_report(struct pdu_data *pdu_data,
4752 				  struct node_rx_pdu *node_rx,
4753 				  struct net_buf *buf)
4754 {
4755 	const uint8_t c_adv_type[] = { 0x00, 0x01, 0x03, 0xff, 0x04,
4756 				    0xff, 0x02 };
4757 	struct bt_hci_evt_le_advertising_report *sep;
4758 	struct pdu_adv *adv = (void *)pdu_data;
4759 	struct bt_hci_evt_le_advertising_info *adv_info;
4760 	uint8_t data_len;
4761 	uint8_t info_len;
4762 	int8_t rssi;
4763 #if defined(CONFIG_BT_CTLR_PRIVACY)
4764 	uint8_t rl_idx;
4765 #endif /* CONFIG_BT_CTLR_PRIVACY */
4766 #if defined(CONFIG_BT_CTLR_EXT_SCAN_FP)
4767 	uint8_t direct;
4768 #endif /* CONFIG_BT_CTLR_EXT_SCAN_FP */
4769 	int8_t *prssi;
4770 
4771 	rssi = -(node_rx->hdr.rx_ftr.rssi);
4772 #if defined(CONFIG_BT_CTLR_PRIVACY)
4773 	rl_idx = node_rx->hdr.rx_ftr.rl_idx;
4774 #endif /* CONFIG_BT_CTLR_PRIVACY */
4775 #if defined(CONFIG_BT_CTLR_EXT_SCAN_FP)
4776 	direct = node_rx->hdr.rx_ftr.direct;
4777 #endif /* CONFIG_BT_CTLR_EXT_SCAN_FP */
4778 
4779 #if defined(CONFIG_BT_CTLR_PRIVACY)
4780 	if (adv->tx_addr) {
4781 		/* Update current RPA */
4782 		ll_rl_crpa_set(0x00, NULL, rl_idx, &adv->adv_ind.addr[0]);
4783 	}
4784 #endif /* CONFIG_BT_CTLR_PRIVACY */
4785 
4786 #if defined(CONFIG_BT_CTLR_EXT_SCAN_FP)
4787 	if (direct) {
4788 #if defined(CONFIG_BT_CTLR_PRIVACY)
4789 		le_dir_adv_report(adv, buf, rssi, rl_idx);
4790 #else
4791 		le_dir_adv_report(adv, buf, rssi, 0xFF);
4792 #endif /* CONFIG_BT_CTLR_PRIVACY */
4793 		return;
4794 	}
4795 #endif /* CONFIG_BT_CTLR_EXT_SCAN_FP */
4796 
4797 #if defined(CONFIG_BT_HCI_MESH_EXT)
4798 	if (node_rx->hdr.type == NODE_RX_TYPE_MESH_REPORT) {
4799 		le_mesh_scan_report(adv, node_rx, buf, rssi);
4800 		return;
4801 	}
4802 #endif /* CONFIG_BT_HCI_MESH_EXT */
4803 
4804 	if (!(event_mask & BT_EVT_MASK_LE_META_EVENT) ||
4805 	    !(le_event_mask & BT_EVT_MASK_LE_ADVERTISING_REPORT)) {
4806 		return;
4807 	}
4808 
4809 #if CONFIG_BT_CTLR_DUP_FILTER_LEN > 0
4810 	if (dup_found(adv->type, adv->tx_addr, adv->adv_ind.addr, 0, NULL, 0)) {
4811 		return;
4812 	}
4813 #endif /* CONFIG_BT_CTLR_DUP_FILTER_LEN > 0 */
4814 
4815 	if (adv->type != PDU_ADV_TYPE_DIRECT_IND) {
4816 		data_len = (adv->len - BDADDR_SIZE);
4817 	} else {
4818 		data_len = 0U;
4819 	}
4820 	info_len = sizeof(struct bt_hci_evt_le_advertising_info) + data_len +
4821 		   sizeof(*prssi);
4822 	sep = meta_evt(buf, BT_HCI_EVT_LE_ADVERTISING_REPORT,
4823 		       sizeof(*sep) + info_len);
4824 
4825 	sep->num_reports = 1U;
4826 	adv_info = (void *)(((uint8_t *)sep) + sizeof(*sep));
4827 
4828 	adv_info->evt_type = c_adv_type[adv->type];
4829 
4830 #if defined(CONFIG_BT_CTLR_PRIVACY)
4831 	if (rl_idx < ll_rl_size_get()) {
4832 		/* Store identity address */
4833 		ll_rl_id_addr_get(rl_idx, &adv_info->addr.type,
4834 				  &adv_info->addr.a.val[0]);
4835 		/* Mark it as identity address from RPA (0x02, 0x03) */
4836 		adv_info->addr.type += 2U;
4837 	} else {
4838 #else
4839 	if (1) {
4840 #endif /* CONFIG_BT_CTLR_PRIVACY */
4841 
4842 		adv_info->addr.type = adv->tx_addr;
4843 		memcpy(&adv_info->addr.a.val[0], &adv->adv_ind.addr[0],
4844 		       sizeof(bt_addr_t));
4845 	}
4846 
4847 	adv_info->length = data_len;
4848 	memcpy(&adv_info->data[0], &adv->adv_ind.data[0], data_len);
4849 	/* RSSI */
4850 	prssi = &adv_info->data[0] + data_len;
4851 	*prssi = rssi;
4852 }
4853 
4854 #if defined(CONFIG_BT_CTLR_ADV_EXT)
4855 static void le_ext_adv_legacy_report(struct pdu_data *pdu_data,
4856 				     struct node_rx_pdu *node_rx,
4857 				     struct net_buf *buf)
4858 {
4859 	/* Lookup event type based on pdu_adv_type set by LLL */
4860 	const uint8_t evt_type_lookup[] = {
4861 		(BT_HCI_LE_ADV_EVT_TYPE_LEGACY | BT_HCI_LE_ADV_EVT_TYPE_SCAN |
4862 		 BT_HCI_LE_ADV_EVT_TYPE_CONN),   /* ADV_IND */
4863 		(BT_HCI_LE_ADV_EVT_TYPE_LEGACY | BT_HCI_LE_ADV_EVT_TYPE_DIRECT |
4864 		 BT_HCI_LE_ADV_EVT_TYPE_CONN),   /* DIRECT_IND */
4865 		(BT_HCI_LE_ADV_EVT_TYPE_LEGACY), /* NONCONN_IND */
4866 		0xff,                            /* Invalid index lookup */
4867 		(BT_HCI_LE_ADV_EVT_TYPE_LEGACY |
4868 		 BT_HCI_LE_ADV_EVT_TYPE_SCAN_RSP |
4869 		 BT_HCI_LE_ADV_EVT_TYPE_SCAN),   /* SCAN_RSP to an ADV_SCAN_IND
4870 						  */
4871 		(BT_HCI_LE_ADV_EVT_TYPE_LEGACY |
4872 		 BT_HCI_LE_ADV_EVT_TYPE_SCAN_RSP |
4873 		 BT_HCI_LE_ADV_EVT_TYPE_SCAN |
4874 		 BT_HCI_LE_ADV_EVT_TYPE_CONN), /* SCAN_RSP to an ADV_IND,
4875 						* NOTE: LLL explicitly sets
4876 						* adv_type to
4877 						* PDU_ADV_TYPE_ADV_IND_SCAN_RSP
4878 						*/
4879 		(BT_HCI_LE_ADV_EVT_TYPE_LEGACY |
4880 		 BT_HCI_LE_ADV_EVT_TYPE_SCAN)    /* SCAN_IND */
4881 	};
4882 	struct bt_hci_evt_le_ext_advertising_info *adv_info;
4883 	struct bt_hci_evt_le_ext_advertising_report *sep;
4884 	struct pdu_adv *adv = (void *)pdu_data;
4885 	uint8_t data_len;
4886 	uint8_t info_len;
4887 	int8_t rssi;
4888 
4889 #if defined(CONFIG_BT_CTLR_PRIVACY)
4890 	uint8_t rl_idx;
4891 #endif /* CONFIG_BT_CTLR_PRIVACY */
4892 #if defined(CONFIG_BT_CTLR_EXT_SCAN_FP)
4893 	uint8_t direct;
4894 #endif /* CONFIG_BT_CTLR_EXT_SCAN_FP */
4895 
4896 	if (!(event_mask & BT_EVT_MASK_LE_META_EVENT) ||
4897 	    !(le_event_mask & BT_EVT_MASK_LE_EXT_ADVERTISING_REPORT)) {
4898 		return;
4899 	}
4900 
4901 	/* The Link Layer currently returns RSSI as an absolute value */
4902 	rssi = -(node_rx->hdr.rx_ftr.rssi);
4903 
4904 #if defined(CONFIG_BT_CTLR_PRIVACY)
4905 	rl_idx = node_rx->hdr.rx_ftr.rl_idx;
4906 #endif /* CONFIG_BT_CTLR_PRIVACY */
4907 
4908 #if defined(CONFIG_BT_CTLR_EXT_SCAN_FP)
4909 	direct = node_rx->hdr.rx_ftr.direct;
4910 #endif /* CONFIG_BT_CTLR_EXT_SCAN_FP */
4911 
4912 #if defined(CONFIG_BT_CTLR_PRIVACY)
4913 	if (adv->tx_addr) {
4914 		/* Update current RPA */
4915 		ll_rl_crpa_set(0x00, NULL, rl_idx, &adv->adv_ind.addr[0]);
4916 	}
4917 #endif /* CONFIG_BT_CTLR_PRIVACY */
4918 
4919 #if CONFIG_BT_CTLR_DUP_FILTER_LEN > 0
4920 	if (dup_found(adv->type, adv->tx_addr, adv->adv_ind.addr, 0, NULL, 0)) {
4921 		return;
4922 	}
4923 #endif /* CONFIG_BT_CTLR_DUP_FILTER_LEN > 0 */
4924 
4925 	if (adv->type != PDU_ADV_TYPE_DIRECT_IND) {
4926 		data_len = (adv->len - BDADDR_SIZE);
4927 	} else {
4928 		data_len = 0U;
4929 	}
4930 
4931 	info_len = sizeof(struct bt_hci_evt_le_ext_advertising_info) +
4932 		   data_len;
4933 	sep = meta_evt(buf, BT_HCI_EVT_LE_EXT_ADVERTISING_REPORT,
4934 		       sizeof(*sep) + info_len);
4935 
4936 	sep->num_reports = 1U;
4937 	adv_info = (void *)(((uint8_t *)sep) + sizeof(*sep));
4938 
4939 	adv_info->evt_type = evt_type_lookup[adv->type];
4940 
4941 #if defined(CONFIG_BT_CTLR_PRIVACY)
4942 	if (rl_idx < ll_rl_size_get()) {
4943 		/* Store identity address */
4944 		ll_rl_id_addr_get(rl_idx, &adv_info->addr.type,
4945 				  &adv_info->addr.a.val[0]);
4946 		/* Mark it as identity address from RPA (0x02, 0x03) */
4947 		adv_info->addr.type += 2U;
4948 	} else
4949 #endif /* CONFIG_BT_CTLR_PRIVACY */
4950 	{
4951 		adv_info->addr.type = adv->tx_addr;
4952 		memcpy(&adv_info->addr.a.val[0], &adv->adv_ind.addr[0],
4953 		       sizeof(bt_addr_t));
4954 	}
4955 
4956 	adv_info->prim_phy = BT_HCI_LE_EXT_SCAN_PHY_1M;
4957 	adv_info->sec_phy = 0U;
4958 	adv_info->sid = 0xff;
4959 	adv_info->tx_power = BT_HCI_LE_ADV_TX_POWER_NO_PREF;
4960 	adv_info->rssi = rssi;
4961 	adv_info->interval = 0U;
4962 
4963 	adv_info->direct_addr.type = adv->rx_addr;
4964 #if defined(CONFIG_BT_CTLR_EXT_SCAN_FP)
4965 	if (direct) {
4966 		memcpy(&adv_info->direct_addr.a.val[0],
4967 		       &adv->direct_ind.tgt_addr[0], sizeof(bt_addr_t));
4968 	} else
4969 #endif /* CONFIG_BT_CTLR_EXT_SCAN_FP */
4970 	{
4971 		memset(&adv_info->direct_addr.a.val[0], 0, sizeof(bt_addr_t));
4972 	}
4973 
4974 	adv_info->length = data_len;
4975 	memcpy(&adv_info->data[0], &adv->adv_ind.data[0], data_len);
4976 }
4977 
4978 static void node_rx_extra_list_release(struct node_rx_pdu *node_rx_extra)
4979 {
4980 	while (node_rx_extra) {
4981 		struct node_rx_pdu *node_rx_curr;
4982 
4983 		node_rx_curr = node_rx_extra;
4984 		node_rx_extra = node_rx_curr->hdr.rx_ftr.extra;
4985 
4986 		node_rx_curr->hdr.next = NULL;
4987 		ll_rx_mem_release((void **)&node_rx_curr);
4988 	}
4989 }
4990 
4991 static void ext_adv_info_fill(uint8_t evt_type, uint8_t phy, uint8_t sec_phy,
4992 			      uint8_t adv_addr_type, uint8_t *adv_addr,
4993 			      uint8_t direct_addr_type, uint8_t *direct_addr,
4994 			      uint8_t rl_idx, int8_t tx_pwr, int8_t rssi,
4995 			      uint16_t interval_le16, struct pdu_adv_adi *adi,
4996 			      uint8_t data_len, uint8_t *data,
4997 			      struct net_buf *buf)
4998 {
4999 	struct bt_hci_evt_le_ext_advertising_info *adv_info;
5000 	struct bt_hci_evt_le_ext_advertising_report *sep;
5001 	uint8_t info_len;
5002 
5003 	info_len = sizeof(struct bt_hci_evt_le_ext_advertising_info) +
5004 		   data_len;
5005 	sep = meta_evt(buf, BT_HCI_EVT_LE_EXT_ADVERTISING_REPORT,
5006 		       sizeof(*sep) + info_len);
5007 
5008 	sep->num_reports = 1U;
5009 	adv_info = (void *)(((uint8_t *)sep) + sizeof(*sep));
5010 
5011 	adv_info->evt_type = evt_type;
5012 
5013 	if (0) {
5014 #if defined(CONFIG_BT_CTLR_PRIVACY)
5015 	} else if (rl_idx < ll_rl_size_get()) {
5016 		/* Store identity address */
5017 		ll_rl_id_addr_get(rl_idx, &adv_info->addr.type,
5018 				  adv_info->addr.a.val);
5019 		/* Mark it as identity address from RPA (0x02, 0x03) */
5020 		adv_info->addr.type += 2U;
5021 #else /* !CONFIG_BT_CTLR_PRIVACY */
5022 		ARG_UNUSED(rl_idx);
5023 #endif /* !CONFIG_BT_CTLR_PRIVACY */
5024 	} else if (adv_addr) {
5025 		adv_info->addr.type = adv_addr_type;
5026 		(void)memcpy(adv_info->addr.a.val, adv_addr, sizeof(bt_addr_t));
5027 	} else {
5028 		adv_info->addr.type = 0U;
5029 		(void)memset(adv_info->addr.a.val, 0, sizeof(bt_addr_t));
5030 	}
5031 
5032 	adv_info->prim_phy = find_lsb_set(phy);
5033 	adv_info->sec_phy = sec_phy;
5034 	adv_info->sid = (adi) ? adi->sid : BT_HCI_LE_EXT_ADV_SID_INVALID;
5035 	adv_info->tx_power = tx_pwr;
5036 	adv_info->rssi = rssi;
5037 	adv_info->interval = interval_le16;
5038 
5039 	if (evt_type & BT_HCI_LE_ADV_EVT_TYPE_DIRECT) {
5040 		adv_info->direct_addr.type = direct_addr_type;
5041 		(void)memcpy(adv_info->direct_addr.a.val, direct_addr,
5042 			     sizeof(bt_addr_t));
5043 	} else {
5044 		adv_info->direct_addr.type = 0U;
5045 		(void)memset(adv_info->direct_addr.a.val, 0, sizeof(bt_addr_t));
5046 	}
5047 
5048 	adv_info->length = data_len;
5049 	(void)memcpy(adv_info->data, data, data_len);
5050 }
5051 
5052 static void le_ext_adv_report(struct pdu_data *pdu_data,
5053 			      struct node_rx_pdu *node_rx,
5054 			      struct net_buf *buf, uint8_t phy)
5055 {
5056 	int8_t tx_pwr = BT_HCI_LE_ADV_TX_POWER_NO_PREF;
5057 	struct pdu_adv *adv = (void *)pdu_data;
5058 	struct node_rx_pdu *node_rx_curr;
5059 	struct node_rx_pdu *node_rx_next;
5060 	uint8_t total_scan_data_len = 0U;
5061 	struct pdu_adv_adi *adi = NULL;
5062 	uint8_t scan_data_status = 0U;
5063 	uint8_t direct_addr_type = 0U;
5064 	struct net_buf *scan_evt_buf;
5065 	uint8_t *direct_addr = NULL;
5066 	uint8_t total_data_len = 0U;
5067 	uint16_t interval_le16 = 0U;
5068 	uint8_t scan_data_len = 0U;
5069 	uint8_t adv_addr_type = 0U;
5070 	uint8_t *scan_data = NULL;
5071 	uint8_t *adv_addr = NULL;
5072 	uint8_t data_status = 0U;
5073 	uint8_t data_len = 0U;
5074 	uint8_t evt_type = 0U;
5075 	uint8_t *data = NULL;
5076 	uint8_t sec_phy = 0U;
5077 	uint8_t data_max_len;
5078 	uint8_t rl_idx = 0U;
5079 	int8_t rssi;
5080 
5081 	/* NOTE: This function uses a lot of initializers before the check and
5082 	 * return below, as an exception to initializing close to their locality
5083 	 * of reference. This is acceptable as the return is unlikely in typical
5084 	 * Controller use.
5085 	 */
5086 	if (!(event_mask & BT_EVT_MASK_LE_META_EVENT) ||
5087 	    !(le_event_mask & BT_EVT_MASK_LE_EXT_ADVERTISING_REPORT)) {
5088 		node_rx_extra_list_release(node_rx->hdr.rx_ftr.extra);
5089 		return;
5090 	}
5091 
5092 #if defined(CONFIG_BT_CTLR_PRIVACY)
5093 	rl_idx = ll_rl_size_get();
5094 #endif /* CONFIG_BT_CTLR_PRIVACY */
5095 
5096 	node_rx_curr = node_rx;
5097 	node_rx_next = node_rx_curr->hdr.rx_ftr.extra;
5098 	do {
5099 		struct pdu_adv_adi *adi_curr = NULL;
5100 		uint8_t direct_addr_type_curr = 0U;
5101 		uint8_t *direct_addr_curr = NULL;
5102 		uint8_t adv_addr_type_curr = 0U;
5103 		struct pdu_adv_com_ext_adv *p;
5104 		uint8_t *adv_addr_curr = NULL;
5105 		uint8_t data_len_curr = 0U;
5106 		uint8_t *data_curr = NULL;
5107 		struct pdu_adv_ext_hdr *h;
5108 		uint8_t sec_phy_curr = 0U;
5109 		uint8_t evt_type_curr;
5110 		uint8_t hdr_buf_len;
5111 		uint8_t hdr_len;
5112 		uint8_t *ptr;
5113 
5114 		/* The Link Layer currently returns RSSI as an absolute value */
5115 		rssi = -(node_rx_curr->hdr.rx_ftr.rssi);
5116 
5117 #if defined(CONFIG_BT_CTLR_PRIVACY)
5118 		uint8_t rl_idx_curr = node_rx_curr->hdr.rx_ftr.rl_idx;
5119 #endif /* CONFIG_BT_CTLR_PRIVACY */
5120 
5121 		BT_DBG("phy= 0x%x, type= 0x%x, len= %u, tat= %u, rat= %u,"
5122 		       " rssi=%d dB", phy, adv->type, adv->len, adv->tx_addr,
5123 		       adv->rx_addr, rssi);
5124 
5125 		p = (void *)&adv->adv_ext_ind;
5126 		h = (void *)p->ext_hdr_adv_data;
5127 		ptr = (void *)h;
5128 
5129 		BT_DBG("    Ext. adv mode= 0x%x, hdr len= %u", p->adv_mode,
5130 		       p->ext_hdr_len);
5131 
5132 		evt_type_curr = p->adv_mode;
5133 
5134 		if (!p->ext_hdr_len) {
5135 			hdr_len = PDU_AC_EXT_HEADER_SIZE_MIN;
5136 
5137 			goto no_ext_hdr;
5138 		}
5139 
5140 		ptr = h->data;
5141 
5142 		if (h->adv_addr) {
5143 			bt_addr_le_t addr;
5144 
5145 			adv_addr_type_curr = adv->tx_addr;
5146 			adv_addr_curr = ptr;
5147 
5148 			addr.type = adv->tx_addr;
5149 			(void)memcpy(addr.a.val, ptr, sizeof(bt_addr_t));
5150 			ptr += BDADDR_SIZE;
5151 
5152 			BT_DBG("    AdvA: %s", bt_addr_le_str(&addr));
5153 		}
5154 
5155 		if (h->tgt_addr) {
5156 			bt_addr_le_t addr;
5157 
5158 			direct_addr_type_curr = adv->rx_addr;
5159 			direct_addr_curr = ptr;
5160 
5161 			addr.type = adv->rx_addr;
5162 			(void)memcpy(addr.a.val, ptr, sizeof(bt_addr_t));
5163 			ptr += BDADDR_SIZE;
5164 
5165 			BT_DBG("    TgtA: %s", bt_addr_le_str(&addr));
5166 		}
5167 
5168 		if (h->adi) {
5169 			adi_curr = (void *)ptr;
5170 
5171 			ptr += sizeof(*adi);
5172 
5173 			BT_DBG("    AdvDataInfo DID = 0x%x, SID = 0x%x",
5174 			       adi->did, adi->sid);
5175 		}
5176 
5177 		if (h->aux_ptr) {
5178 			struct pdu_adv_aux_ptr *aux_ptr;
5179 			uint8_t aux_phy;
5180 
5181 			aux_ptr = (void *)ptr;
5182 			if (aux_ptr->phy > EXT_ADV_AUX_PHY_LE_CODED) {
5183 				struct node_rx_ftr *ftr;
5184 
5185 				ftr = &node_rx->hdr.rx_ftr;
5186 				node_rx_extra_list_release(ftr->extra);
5187 				return;
5188 			}
5189 
5190 			ptr += sizeof(*aux_ptr);
5191 
5192 			sec_phy_curr = aux_ptr->phy + 1;
5193 
5194 			aux_phy = BIT(aux_ptr->phy);
5195 
5196 			BT_DBG("    AuxPtr chan_idx = %u, ca = %u, offs_units "
5197 			       "= %u offs = 0x%x, phy = 0x%x",
5198 			       aux_ptr->chan_idx, aux_ptr->ca,
5199 			       aux_ptr->offs_units, aux_ptr->offs, aux_phy);
5200 		}
5201 
5202 		if (h->sync_info) {
5203 			struct pdu_adv_sync_info *si;
5204 
5205 			si = (void *)ptr;
5206 			ptr += sizeof(*si);
5207 
5208 			interval_le16 = si->interval;
5209 
5210 			BT_DBG("    SyncInfo offs = %u, offs_unit = 0x%x, "
5211 			       "interval = 0x%x, sca = 0x%x, "
5212 			       "chan map = 0x%x 0x%x 0x%x 0x%x 0x%x, "
5213 			       "AA = 0x%x, CRC = 0x%x 0x%x 0x%x, "
5214 			       "evt cntr = 0x%x",
5215 			       sys_le16_to_cpu(si->offs),
5216 			       si->offs_units,
5217 			       sys_le16_to_cpu(si->interval),
5218 			       ((si->sca_chm[PDU_SYNC_INFO_SCA_CHM_SCA_BYTE_OFFSET] &
5219 				 PDU_SYNC_INFO_SCA_CHM_SCA_BIT_MASK) >>
5220 				PDU_SYNC_INFO_SCA_CHM_SCA_BIT_POS),
5221 			       si->sca_chm[0], si->sca_chm[1], si->sca_chm[2],
5222 			       si->sca_chm[3],
5223 			       (si->sca_chm[PDU_SYNC_INFO_SCA_CHM_SCA_BYTE_OFFSET] &
5224 				~PDU_SYNC_INFO_SCA_CHM_SCA_BIT_MASK),
5225 			       sys_le32_to_cpu(si->aa),
5226 			       si->crc_init[0], si->crc_init[1],
5227 			       si->crc_init[2], sys_le16_to_cpu(si->evt_cntr));
5228 		}
5229 
5230 		if (h->tx_pwr) {
5231 			tx_pwr = *(int8_t *)ptr;
5232 			ptr++;
5233 
5234 			BT_DBG("    Tx pwr= %d dB", tx_pwr);
5235 		}
5236 
5237 		hdr_len = ptr - (uint8_t *)p;
5238 		hdr_buf_len = PDU_AC_EXT_HEADER_SIZE_MIN + p->ext_hdr_len;
5239 		if (hdr_len > hdr_buf_len) {
5240 			BT_WARN("    Header length %u/%u, INVALID.", hdr_len,
5241 				p->ext_hdr_len);
5242 		} else {
5243 			uint8_t acad_len = hdr_buf_len - hdr_len;
5244 
5245 			if (acad_len) {
5246 				ptr += acad_len;
5247 				hdr_len += acad_len;
5248 
5249 				BT_DBG("ACAD: <todo>");
5250 			}
5251 		}
5252 
5253 no_ext_hdr:
5254 		if (hdr_len < adv->len) {
5255 			data_len_curr = adv->len - hdr_len;
5256 			data_curr = ptr;
5257 
5258 			BT_DBG("    AD Data (%u): <todo>", data_len);
5259 		}
5260 
5261 		if (node_rx_curr == node_rx) {
5262 			evt_type = evt_type_curr;
5263 			adv_addr_type = adv_addr_type_curr;
5264 			adv_addr = adv_addr_curr;
5265 			direct_addr_type = direct_addr_type_curr;
5266 			direct_addr = direct_addr_curr;
5267 			adi = adi_curr;
5268 			sec_phy = sec_phy_curr;
5269 			data_len = data_len_curr;
5270 			total_data_len = data_len;
5271 			total_scan_data_len = 0U;
5272 			data = data_curr;
5273 #if defined(CONFIG_BT_CTLR_PRIVACY)
5274 			rl_idx = rl_idx_curr;
5275 #endif /* CONFIG_BT_CTLR_PRIVACY */
5276 		} else {
5277 			/* TODO: Validate current value with previous */
5278 
5279 			/* Detect the scan response in the list of node_rx */
5280 			if (node_rx_curr->hdr.rx_ftr.scan_rsp) {
5281 				scan_data_len = data_len_curr;
5282 				scan_data = data_curr;
5283 			}
5284 
5285 			if (!adv_addr) {
5286 				adv_addr_type = adv_addr_type_curr;
5287 				adv_addr = adv_addr_curr;
5288 			}
5289 
5290 			if (!direct_addr) {
5291 				direct_addr_type = direct_addr_type_curr;
5292 				direct_addr = direct_addr_curr;
5293 			}
5294 
5295 			if (scan_data) {
5296 				total_scan_data_len += data_len_curr;
5297 
5298 				/* TODO: construct new HCI event for this
5299 				 * fragment.
5300 				 */
5301 			} else if (!data) {
5302 				data_len = data_len_curr;
5303 				total_data_len = data_len;
5304 				data = data_curr;
5305 			} else {
5306 				total_data_len += data_len_curr;
5307 
5308 				/* TODO: construct new HCI event for this
5309 				 * fragment.
5310 				 */
5311 			}
5312 #if defined(CONFIG_BT_CTLR_PRIVACY)
5313 			if (rl_idx >= ll_rl_size_get()) {
5314 				rl_idx = rl_idx_curr;
5315 			}
5316 #endif /* CONFIG_BT_CTLR_PRIVACY */
5317 		}
5318 
5319 		if (!node_rx_next) {
5320 			bool has_aux_ptr = !!sec_phy_curr;
5321 
5322 			if (scan_data) {
5323 				if (has_aux_ptr) {
5324 					scan_data_status =
5325 				  BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_INCOMPLETE;
5326 				}
5327 			} else if (has_aux_ptr) {
5328 				data_status =
5329 				  BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_INCOMPLETE;
5330 			}
5331 
5332 			break;
5333 		}
5334 
5335 		node_rx_curr = node_rx_next;
5336 		node_rx_next = node_rx_curr->hdr.rx_ftr.extra;
5337 		adv = (void *)node_rx_curr->pdu;
5338 	} while (1);
5339 
5340 #if CONFIG_BT_CTLR_DUP_FILTER_LEN > 0
5341 	if (adv_addr) {
5342 		if (dup_found(PDU_ADV_TYPE_EXT_IND, adv_addr_type, adv_addr,
5343 			      evt_type, adi, data_status)) {
5344 			node_rx_extra_list_release(node_rx->hdr.rx_ftr.extra);
5345 			return;
5346 		}
5347 	}
5348 #endif /* CONFIG_BT_CTLR_DUP_FILTER_LEN > 0 */
5349 
5350 	/* FIXME: move most of below into above loop to dispatch fragments of
5351 	 * data in HCI event.
5352 	 */
5353 	data_max_len = ADV_REPORT_EVT_MAX_LEN -
5354 		       sizeof(struct bt_hci_evt_le_meta_event) -
5355 		       sizeof(struct bt_hci_evt_le_ext_advertising_report) -
5356 		       sizeof(struct bt_hci_evt_le_ext_advertising_info);
5357 
5358 	/* If data complete */
5359 	if (!data_status) {
5360 		/* Only copy data that fit the event buffer size,
5361 		 * mark it as incomplete
5362 		 */
5363 		if (data_len > data_max_len) {
5364 			data_len = data_max_len;
5365 			data_status =
5366 				BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_PARTIAL;
5367 		}
5368 
5369 	/* else, data incomplete */
5370 	} else {
5371 		/* Data incomplete and no more to come */
5372 		if (!(adv_addr ||
5373 		      (adi && ((tx_pwr != BT_HCI_LE_ADV_TX_POWER_NO_PREF) ||
5374 			       data)))) {
5375 			/* No device address and no valid AD data parsed or
5376 			 * Tx Power present for this PDU chain that has ADI,
5377 			 * skip HCI event generation.
5378 			 * In other terms, generate HCI event if device address
5379 			 * is present or if Tx pwr and/or data is present from
5380 			 * anonymous device.
5381 			 */
5382 			node_rx_extra_list_release(node_rx->hdr.rx_ftr.extra);
5383 			return;
5384 		}
5385 
5386 		/* Only copy data that fit the event buffer size */
5387 		if (data_len > data_max_len) {
5388 			data_len = data_max_len;
5389 		}
5390 	}
5391 
5392 	/* Set directed advertising bit */
5393 	if (direct_addr) {
5394 		evt_type |= BT_HCI_LE_ADV_EVT_TYPE_DIRECT;
5395 	}
5396 
5397 	/* Set data status bits */
5398 	evt_type |= (data_status << 5);
5399 
5400 	/* Start constructing the adv event */
5401 	ext_adv_info_fill(evt_type, phy, sec_phy, adv_addr_type, adv_addr,
5402 			  direct_addr_type, direct_addr, rl_idx, tx_pwr, rssi,
5403 			  interval_le16, adi, data_len, data, buf);
5404 
5405 	/* If scan response event to be constructed */
5406 	if (!scan_data) {
5407 		node_rx_extra_list_release(node_rx->hdr.rx_ftr.extra);
5408 
5409 		return;
5410 	}
5411 
5412 	/* Allocate, append as buf fragement and construct the scan response
5413 	 * event.
5414 	 */
5415 	scan_evt_buf = bt_buf_get_rx(BT_BUF_EVT, K_FOREVER);
5416 	net_buf_frag_add(buf, scan_evt_buf);
5417 
5418 	/* If scan data complete */
5419 	if (!scan_data_status) {
5420 		/* Only copy data that fit the event buffer size,
5421 		 * mark it as incomplete
5422 		 */
5423 		if (scan_data_len > data_max_len) {
5424 			scan_data_len = data_max_len;
5425 			scan_data_status =
5426 				BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_PARTIAL;
5427 		}
5428 
5429 	/* else, data incomplete */
5430 	} else {
5431 		/* Only copy data that fit the event buffer size */
5432 		if (scan_data_len > data_max_len) {
5433 			scan_data_len = data_max_len;
5434 		}
5435 	}
5436 
5437 	/* Set scan response bit */
5438 	evt_type |= BT_HCI_LE_ADV_EVT_TYPE_SCAN_RSP;
5439 
5440 	/* set scan data status bits */
5441 	evt_type &= ~(BIT_MASK(2) << 5);
5442 	evt_type |= (scan_data_status << 5);
5443 
5444 	/* Start constructing the event */
5445 	ext_adv_info_fill(evt_type, phy, sec_phy, adv_addr_type, adv_addr,
5446 			  direct_addr_type, direct_addr, rl_idx, tx_pwr, rssi,
5447 			  interval_le16, adi, scan_data_len, scan_data,
5448 			  scan_evt_buf);
5449 
5450 	node_rx_extra_list_release(node_rx->hdr.rx_ftr.extra);
5451 }
5452 
5453 static void le_adv_ext_report(struct pdu_data *pdu_data,
5454 			      struct node_rx_pdu *node_rx,
5455 			      struct net_buf *buf, uint8_t phy)
5456 {
5457 	struct pdu_adv *adv = (void *)pdu_data;
5458 
5459 	if ((adv->type == PDU_ADV_TYPE_EXT_IND) && adv->len) {
5460 		le_ext_adv_report(pdu_data, node_rx, buf, phy);
5461 	} else {
5462 		le_ext_adv_legacy_report(pdu_data, node_rx, buf);
5463 	}
5464 }
5465 
5466 static void le_adv_ext_1M_report(struct pdu_data *pdu_data,
5467 				 struct node_rx_pdu *node_rx,
5468 				 struct net_buf *buf)
5469 {
5470 	le_adv_ext_report(pdu_data, node_rx, buf, BT_HCI_LE_EXT_SCAN_PHY_1M);
5471 }
5472 
5473 static void le_adv_ext_2M_report(struct pdu_data *pdu_data,
5474 				 struct node_rx_pdu *node_rx,
5475 				 struct net_buf *buf)
5476 {
5477 	le_adv_ext_report(pdu_data, node_rx, buf, BT_HCI_LE_EXT_SCAN_PHY_2M);
5478 }
5479 
5480 static void le_adv_ext_coded_report(struct pdu_data *pdu_data,
5481 				    struct node_rx_pdu *node_rx,
5482 				    struct net_buf *buf)
5483 {
5484 	le_adv_ext_report(pdu_data, node_rx, buf, BT_HCI_LE_EXT_SCAN_PHY_CODED);
5485 }
5486 
5487 static void le_scan_timeout(struct pdu_data *pdu_data,
5488 			    struct node_rx_pdu *node_rx, struct net_buf *buf)
5489 {
5490 	if (!(event_mask & BT_EVT_MASK_LE_META_EVENT) ||
5491 	    !(le_event_mask & BT_EVT_MASK_LE_SCAN_TIMEOUT)) {
5492 		return;
5493 	}
5494 
5495 	meta_evt(buf, BT_HCI_EVT_LE_SCAN_TIMEOUT, 0U);
5496 }
5497 
5498 #if defined(CONFIG_BT_CTLR_SYNC_PERIODIC)
5499 static void le_per_adv_sync_established(struct pdu_data *pdu_data,
5500 					struct node_rx_pdu *node_rx,
5501 					struct net_buf *buf)
5502 {
5503 	struct bt_hci_evt_le_per_adv_sync_established *sep;
5504 	struct ll_scan_set *scan;
5505 	struct node_rx_sync *se;
5506 
5507 	if (!(event_mask & BT_EVT_MASK_LE_META_EVENT) ||
5508 	    !(le_event_mask & BT_EVT_MASK_LE_PER_ADV_SYNC_ESTABLISHED)) {
5509 		return;
5510 	}
5511 
5512 	sep = meta_evt(buf, BT_HCI_EVT_LE_PER_ADV_SYNC_ESTABLISHED,
5513 		       sizeof(*sep));
5514 
5515 	se = (void *)pdu_data;
5516 	sep->status = se->status;
5517 	sep->handle = sys_cpu_to_le16(node_rx->hdr.handle);
5518 
5519 	if (sep->status != BT_HCI_ERR_SUCCESS && sep->status != BT_HCI_ERR_UNSUPP_REMOTE_FEATURE) {
5520 		return;
5521 	}
5522 
5523 	scan = node_rx->hdr.rx_ftr.param;
5524 
5525 	sep->sid = scan->per_scan.sid;
5526 	/* FIXME: fill based on filter_policy options */
5527 	sep->adv_addr.type = scan->per_scan.adv_addr_type;
5528 	memcpy(&sep->adv_addr.a.val[0], scan->per_scan.adv_addr, BDADDR_SIZE);
5529 	sep->phy = find_lsb_set(se->phy);
5530 	sep->interval = sys_cpu_to_le16(se->interval);
5531 	sep->clock_accuracy = se->sca;
5532 }
5533 
5534 static void le_per_adv_sync_report(struct pdu_data *pdu_data,
5535 				   struct node_rx_pdu *node_rx,
5536 				   struct net_buf *buf)
5537 {
5538 	struct bt_hci_evt_le_per_advertising_report *sep;
5539 	struct node_rx_ftr *ftr = &node_rx->hdr.rx_ftr;
5540 	int8_t tx_pwr = BT_HCI_LE_ADV_TX_POWER_NO_PREF;
5541 	struct pdu_adv *adv = (void *)pdu_data;
5542 	struct pdu_adv_aux_ptr *aux_ptr = NULL;
5543 	uint8_t cte_type = BT_HCI_LE_NO_CTE;
5544 	struct pdu_adv_com_ext_adv *p;
5545 	struct pdu_adv_ext_hdr *h;
5546 	uint8_t data_status = 0U;
5547 	struct net_buf *evt_buf;
5548 	uint8_t data_len = 0U;
5549 	uint8_t *data = NULL;
5550 	uint8_t data_max_len;
5551 	uint8_t hdr_buf_len;
5552 	uint8_t hdr_len;
5553 	uint8_t *ptr;
5554 	int8_t rssi;
5555 
5556 	if (!(event_mask & BT_EVT_MASK_LE_META_EVENT) ||
5557 	    !(le_event_mask & BT_EVT_MASK_LE_PER_ADVERTISING_REPORT)) {
5558 		return;
5559 	}
5560 
5561 	if (node_rx->hdr.rx_ftr.aux_failed) {
5562 		sep = meta_evt(buf,
5563 			       BT_HCI_EVT_LE_PER_ADVERTISING_REPORT,
5564 			       sizeof(*sep));
5565 
5566 		sep->handle = sys_cpu_to_le16(node_rx->hdr.handle);
5567 		sep->tx_power = BT_HCI_LE_ADV_TX_POWER_NO_PREF;
5568 		sep->rssi = BT_HCI_LE_RSSI_NOT_AVAILABLE;
5569 		sep->cte_type = BT_HCI_LE_NO_CTE;
5570 		sep->data_status = BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_INCOMPLETE;
5571 		sep->length = 0;
5572 
5573 		return;
5574 	}
5575 
5576 	/* The Link Layer currently returns RSSI as an absolute value */
5577 	rssi = -(node_rx->hdr.rx_ftr.rssi);
5578 
5579 	BT_DBG("len = %u, rssi = %d", adv->len, rssi);
5580 
5581 	p = (void *)&adv->adv_ext_ind;
5582 	h = (void *)p->ext_hdr_adv_data;
5583 	ptr = (void *)h;
5584 
5585 	BT_DBG("    Per. adv mode= 0x%x, hdr len= %u", p->adv_mode,
5586 	       p->ext_hdr_len);
5587 
5588 	if (!p->ext_hdr_len) {
5589 		hdr_len = PDU_AC_EXT_HEADER_SIZE_MIN;
5590 
5591 		goto no_ext_hdr;
5592 	}
5593 
5594 	ptr = h->data;
5595 
5596 	if (h->adv_addr) {
5597 		ptr += BDADDR_SIZE;
5598 	}
5599 
5600 	if (h->tgt_addr) {
5601 		ptr += BDADDR_SIZE;
5602 	}
5603 
5604 	if (h->cte_info) {
5605 		struct pdu_cte_info *cte_info;
5606 
5607 		cte_info = (void *)ptr;
5608 		cte_type = cte_info->type;
5609 		ptr++;
5610 
5611 		BT_DBG("    CTE type= %d", cte_type);
5612 	}
5613 
5614 	if (h->adi) {
5615 		ptr += sizeof(struct pdu_adv_adi);
5616 	}
5617 
5618 	/* AuxPtr */
5619 	if (h->aux_ptr) {
5620 		uint8_t aux_phy;
5621 
5622 		aux_ptr = (void *)ptr;
5623 		if (aux_ptr->phy > EXT_ADV_AUX_PHY_LE_CODED) {
5624 			struct node_rx_ftr *ftr;
5625 
5626 			ftr = &node_rx->hdr.rx_ftr;
5627 			node_rx_extra_list_release(ftr->extra);
5628 			return;
5629 		}
5630 
5631 		ptr += sizeof(*aux_ptr);
5632 
5633 		aux_phy = BIT(aux_ptr->phy);
5634 
5635 		BT_DBG("    AuxPtr chan_idx = %u, ca = %u, offs_units "
5636 		       "= %u offs = 0x%x, phy = 0x%x",
5637 		       aux_ptr->chan_idx, aux_ptr->ca,
5638 		       aux_ptr->offs_units, aux_ptr->offs, aux_phy);
5639 	}
5640 
5641 	/* No SyncInfo */
5642 	if (h->sync_info) {
5643 		ptr += sizeof(struct pdu_adv_sync_info);
5644 	}
5645 
5646 	/* Tx Power */
5647 	if (h->tx_pwr) {
5648 		tx_pwr = *(int8_t *)ptr;
5649 		ptr++;
5650 
5651 		BT_DBG("    Tx pwr= %d dB", tx_pwr);
5652 	}
5653 
5654 	hdr_len = ptr - (uint8_t *)p;
5655 	hdr_buf_len = PDU_AC_EXT_HEADER_SIZE_MIN + p->ext_hdr_len;
5656 	if (hdr_len > hdr_buf_len) {
5657 		BT_WARN("    Header length %u/%u, INVALID.", hdr_len,
5658 			p->ext_hdr_len);
5659 	} else {
5660 		uint8_t acad_len = hdr_buf_len - hdr_len;
5661 
5662 		if (acad_len) {
5663 			ptr += acad_len;
5664 			hdr_len += acad_len;
5665 
5666 			BT_DBG("ACAD: <todo>");
5667 		}
5668 	}
5669 
5670 no_ext_hdr:
5671 	if (hdr_len < adv->len) {
5672 		data_len = adv->len - hdr_len;
5673 		data = ptr;
5674 
5675 		BT_DBG("    AD Data (%u): <todo>", data_len);
5676 	}
5677 
5678 	adv = (void *)node_rx->pdu;
5679 
5680 	data_max_len = ADV_REPORT_EVT_MAX_LEN -
5681 		       sizeof(struct bt_hci_evt_le_meta_event) -
5682 		       sizeof(*sep);
5683 
5684 	evt_buf = buf;
5685 
5686 	do {
5687 		uint8_t data_len_frag;
5688 
5689 		data_len_frag = MIN(data_len, data_max_len);
5690 
5691 		/* Start constructing periodic advertising report */
5692 		sep = meta_evt(evt_buf,
5693 			       BT_HCI_EVT_LE_PER_ADVERTISING_REPORT,
5694 			       sizeof(*sep) + data_len_frag);
5695 
5696 		memcpy(&sep->data[0], data, data_len_frag);
5697 		data += data_len_frag;
5698 		data_len -= data_len_frag;
5699 
5700 		if (data_len > 0) {
5701 			/* Some data left in PDU, mark as partial data. */
5702 			data_status = BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_PARTIAL;
5703 		} else if (!aux_ptr) {
5704 			/* No data left, no AuxPtr, mark as complete data. */
5705 			data_status = BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_COMPLETE;
5706 		} else if (ftr->aux_w4next) {
5707 			/* No data left, but have AuxPtr and scheduled aux scan,
5708 			 * mark as partial data.
5709 			 */
5710 			data_status = BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_PARTIAL;
5711 		} else {
5712 			/* No data left, have AuxPtr but not aux scan scheduled,
5713 			 * mark as incomplete data.
5714 			 */
5715 			data_status = BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_INCOMPLETE;
5716 		}
5717 
5718 		sep->handle = sys_cpu_to_le16(node_rx->hdr.handle);
5719 		/* TODO: use actual TX power only on 1st report, subsequent
5720 		 *       reports can use 0x7F
5721 		 */
5722 		sep->tx_power = tx_pwr;
5723 		sep->rssi = rssi;
5724 		sep->cte_type = cte_type;
5725 		sep->data_status = data_status;
5726 		sep->length = data_len_frag;
5727 
5728 		if (data_len > 0) {
5729 			evt_buf = bt_buf_get_rx(BT_BUF_EVT, K_FOREVER);
5730 			net_buf_frag_add(buf, evt_buf);
5731 
5732 			tx_pwr = BT_HCI_LE_ADV_TX_POWER_NO_PREF;
5733 		}
5734 	} while (data_len > 0);
5735 }
5736 
5737 static void le_per_adv_sync_lost(struct pdu_data *pdu_data,
5738 				 struct node_rx_pdu *node_rx,
5739 				 struct net_buf *buf)
5740 {
5741 	struct bt_hci_evt_le_per_adv_sync_lost *sep;
5742 
5743 	if (!(event_mask & BT_EVT_MASK_LE_META_EVENT) ||
5744 	    !(le_event_mask & BT_EVT_MASK_LE_PER_ADV_SYNC_LOST)) {
5745 		return;
5746 	}
5747 
5748 	sep = meta_evt(buf, BT_HCI_EVT_LE_PER_ADV_SYNC_LOST, sizeof(*sep));
5749 	sep->handle = sys_cpu_to_le16(node_rx->hdr.handle);
5750 }
5751 #endif /* CONFIG_BT_CTLR_SYNC_PERIODIC */
5752 #endif /* CONFIG_BT_CTLR_ADV_EXT */
5753 #endif /* CONFIG_BT_OBSERVER */
5754 
5755 #if defined(CONFIG_BT_BROADCASTER)
5756 #if defined(CONFIG_BT_CTLR_ADV_EXT)
5757 static void le_adv_ext_terminate(struct pdu_data *pdu_data,
5758 				    struct node_rx_pdu *node_rx,
5759 				    struct net_buf *buf)
5760 {
5761 	struct bt_hci_evt_le_adv_set_terminated *sep;
5762 
5763 	if (!(event_mask & BT_EVT_MASK_LE_META_EVENT) ||
5764 	    !(le_event_mask & BT_EVT_MASK_LE_ADV_SET_TERMINATED)) {
5765 		return;
5766 	}
5767 
5768 	sep = meta_evt(buf, BT_HCI_EVT_LE_ADV_SET_TERMINATED, sizeof(*sep));
5769 	sep->status = node_rx->hdr.rx_ftr.param_adv_term.status;
5770 	sep->adv_handle = ll_adv_set_hci_handle_get(node_rx->hdr.handle & 0xff);
5771 	sep->conn_handle =
5772 		sys_cpu_to_le16(node_rx->hdr.rx_ftr.param_adv_term.conn_handle);
5773 	sep->num_completed_ext_adv_evts =
5774 		node_rx->hdr.rx_ftr.param_adv_term.num_events;
5775 }
5776 
5777 #if defined(CONFIG_BT_CTLR_ADV_ISO)
5778 static void le_big_complete(struct pdu_data *pdu_data,
5779 			    struct node_rx_pdu *node_rx,
5780 			    struct net_buf *buf)
5781 {
5782 	struct bt_hci_evt_le_big_complete *sep;
5783 	struct ll_adv_iso *adv_iso;
5784 	struct node_rx_sync *se;
5785 	size_t evt_size;
5786 
5787 	adv_iso = node_rx->hdr.rx_ftr.param;
5788 
5789 	evt_size = sizeof(*sep) +
5790 			adv_iso->num_bis * sizeof(adv_iso->bis_handle);
5791 
5792 	adv_iso = node_rx->hdr.rx_ftr.param;
5793 
5794 	sep = meta_evt(buf, BT_HCI_EVT_LE_BIG_COMPLETE, evt_size);
5795 
5796 	se = (void *)pdu_data;
5797 	sep->status = se->status;
5798 	sep->big_handle = sys_cpu_to_le16(node_rx->hdr.handle);
5799 
5800 	if (sep->status) {
5801 		return;
5802 	}
5803 
5804 	/* TODO: Fill values */
5805 	sys_put_le24(0, sep->sync_delay);
5806 	sys_put_le24(0, sep->latency);
5807 	sep->phy = adv_iso->phy;
5808 	sep->nse = 0;
5809 	sep->bn = 0;
5810 	sep->pto = 0;
5811 	sep->irc = 0;
5812 	sep->max_pdu = 0;
5813 	sep->num_bis = adv_iso->num_bis;
5814 	/* TODO: Add support for multiple BIS per BIG */
5815 	LL_ASSERT(sep->num_bis == 1);
5816 	sep->handle[0] = sys_cpu_to_le16(adv_iso->bis_handle);
5817 }
5818 #endif /* CONFIG_BT_CTLR_ADV_ISO */
5819 #endif /* CONFIG_BT_CTLR_ADV_EXT */
5820 #endif /* CONFIG_BT_BROADCASTER */
5821 
5822 #if defined(CONFIG_BT_CTLR_SCAN_REQ_NOTIFY)
5823 static void le_scan_req_received(struct pdu_data *pdu_data,
5824 				 struct node_rx_pdu *node_rx,
5825 				 struct net_buf *buf)
5826 {
5827 	struct pdu_adv *adv = (void *)pdu_data;
5828 	struct bt_hci_evt_le_scan_req_received *sep;
5829 
5830 #if defined(CONFIG_BT_CTLR_PRIVACY)
5831 	uint8_t rl_idx;
5832 #endif
5833 
5834 	if (!(event_mask & BT_EVT_MASK_LE_META_EVENT) ||
5835 	    !(le_event_mask & BT_EVT_MASK_LE_SCAN_REQ_RECEIVED)) {
5836 		bt_addr_le_t addr;
5837 		uint8_t handle;
5838 		int8_t rssi;
5839 
5840 		handle = ll_adv_set_hci_handle_get(node_rx->hdr.handle & 0xff);
5841 		addr.type = adv->tx_addr;
5842 		memcpy(&addr.a.val[0], &adv->scan_req.scan_addr[0],
5843 		       sizeof(bt_addr_t));
5844 
5845 		/* The Link Layer currently returns RSSI as an absolute value */
5846 		rssi = -(node_rx->hdr.rx_ftr.rssi);
5847 
5848 		BT_DBG("handle: %d, addr: %s, rssi: %d dB.",
5849 		       handle, bt_addr_le_str(&addr), rssi);
5850 
5851 		return;
5852 	}
5853 
5854 	sep = meta_evt(buf, BT_HCI_EVT_LE_SCAN_REQ_RECEIVED, sizeof(*sep));
5855 	sep->handle = ll_adv_set_hci_handle_get(node_rx->hdr.handle & 0xff);
5856 	sep->addr.type = adv->tx_addr;
5857 	memcpy(&sep->addr.a.val[0], &adv->scan_req.scan_addr[0],
5858 	       sizeof(bt_addr_t));
5859 
5860 #if defined(CONFIG_BT_CTLR_PRIVACY)
5861 	rl_idx = node_rx->hdr.rx_ftr.rl_idx;
5862 	if (rl_idx < ll_rl_size_get()) {
5863 		/* Store identity address */
5864 		ll_rl_id_addr_get(rl_idx, &sep->addr.type,
5865 				  &sep->addr.a.val[0]);
5866 		/* Mark it as identity address from RPA (0x02, 0x03) */
5867 		sep->addr.type += 2U;
5868 	} else {
5869 #else
5870 	if (1) {
5871 #endif
5872 		sep->addr.type = adv->tx_addr;
5873 		memcpy(&sep->addr.a.val[0], &adv->adv_ind.addr[0],
5874 		       sizeof(bt_addr_t));
5875 	}
5876 }
5877 #endif /* CONFIG_BT_CTLR_SCAN_REQ_NOTIFY */
5878 
5879 #if defined(CONFIG_BT_CONN)
5880 static void le_conn_complete(struct pdu_data *pdu_data, uint16_t handle,
5881 			     struct net_buf *buf)
5882 {
5883 	struct node_rx_cc *cc = (void *)pdu_data;
5884 	struct bt_hci_evt_le_conn_complete *lecc;
5885 	uint8_t status = cc->status;
5886 
5887 #if defined(CONFIG_BT_CTLR_PRIVACY)
5888 	if (!status) {
5889 		/* Update current RPA */
5890 		ll_rl_crpa_set(cc->peer_addr_type,
5891 			       &cc->peer_addr[0], 0xff,
5892 			       &cc->peer_rpa[0]);
5893 	}
5894 #endif
5895 
5896 	if (!(event_mask & BT_EVT_MASK_LE_META_EVENT) ||
5897 	    (!(le_event_mask & BT_EVT_MASK_LE_CONN_COMPLETE) &&
5898 #if defined(CONFIG_BT_CTLR_PRIVACY) || defined(CONFIG_BT_CTLR_ADV_EXT)
5899 	     !(le_event_mask & BT_EVT_MASK_LE_ENH_CONN_COMPLETE))) {
5900 #else
5901 	     1)) {
5902 #endif /* CONFIG_BT_CTLR_PRIVACY || CONFIG_BT_CTLR_ADV_EXT */
5903 		return;
5904 	}
5905 
5906 	if (!status) {
5907 		conn_count++;
5908 	}
5909 
5910 #if defined(CONFIG_BT_CTLR_PRIVACY) || defined(CONFIG_BT_CTLR_ADV_EXT)
5911 	if (le_event_mask & BT_EVT_MASK_LE_ENH_CONN_COMPLETE) {
5912 		struct bt_hci_evt_le_enh_conn_complete *leecc;
5913 
5914 		leecc = meta_evt(buf, BT_HCI_EVT_LE_ENH_CONN_COMPLETE,
5915 				 sizeof(*leecc));
5916 
5917 		if (status) {
5918 			(void)memset(leecc, 0x00, sizeof(*leecc));
5919 			leecc->status = status;
5920 			return;
5921 		}
5922 
5923 		leecc->status = 0x00;
5924 		leecc->handle = sys_cpu_to_le16(handle);
5925 		leecc->role = cc->role;
5926 
5927 		leecc->peer_addr.type = cc->peer_addr_type;
5928 		memcpy(&leecc->peer_addr.a.val[0], &cc->peer_addr[0],
5929 		       BDADDR_SIZE);
5930 
5931 #if defined(CONFIG_BT_CTLR_PRIVACY)
5932 		memcpy(&leecc->local_rpa.val[0], &cc->local_rpa[0],
5933 		       BDADDR_SIZE);
5934 		memcpy(&leecc->peer_rpa.val[0], &cc->peer_rpa[0],
5935 		       BDADDR_SIZE);
5936 #else /* !CONFIG_BT_CTLR_PRIVACY */
5937 		memset(&leecc->local_rpa.val[0], 0, BDADDR_SIZE);
5938 		memset(&leecc->peer_rpa.val[0], 0, BDADDR_SIZE);
5939 #endif /* !CONFIG_BT_CTLR_PRIVACY */
5940 
5941 		leecc->interval = sys_cpu_to_le16(cc->interval);
5942 		leecc->latency = sys_cpu_to_le16(cc->latency);
5943 		leecc->supv_timeout = sys_cpu_to_le16(cc->timeout);
5944 		leecc->clock_accuracy = cc->sca;
5945 		return;
5946 	}
5947 #endif /* CONFIG_BT_CTLR_PRIVACY || CONFIG_BT_CTLR_ADV_EXT */
5948 
5949 	lecc = meta_evt(buf, BT_HCI_EVT_LE_CONN_COMPLETE, sizeof(*lecc));
5950 
5951 	if (status) {
5952 		(void)memset(lecc, 0x00, sizeof(*lecc));
5953 		lecc->status = status;
5954 		return;
5955 	}
5956 
5957 	lecc->status = 0x00;
5958 	lecc->handle = sys_cpu_to_le16(handle);
5959 	lecc->role = cc->role;
5960 	lecc->peer_addr.type = cc->peer_addr_type & 0x1;
5961 	memcpy(&lecc->peer_addr.a.val[0], &cc->peer_addr[0], BDADDR_SIZE);
5962 	lecc->interval = sys_cpu_to_le16(cc->interval);
5963 	lecc->latency = sys_cpu_to_le16(cc->latency);
5964 	lecc->supv_timeout = sys_cpu_to_le16(cc->timeout);
5965 	lecc->clock_accuracy = cc->sca;
5966 }
5967 
5968 void hci_disconn_complete_encode(struct pdu_data *pdu_data, uint16_t handle,
5969 				 struct net_buf *buf)
5970 {
5971 	struct bt_hci_evt_disconn_complete *ep;
5972 
5973 	if (!(event_mask & BT_EVT_MASK_DISCONN_COMPLETE)) {
5974 		return;
5975 	}
5976 
5977 	hci_evt_create(buf, BT_HCI_EVT_DISCONN_COMPLETE, sizeof(*ep));
5978 	ep = net_buf_add(buf, sizeof(*ep));
5979 
5980 	ep->status = 0x00;
5981 	ep->handle = sys_cpu_to_le16(handle);
5982 	ep->reason = *((uint8_t *)pdu_data);
5983 }
5984 
5985 void hci_disconn_complete_process(uint16_t handle)
5986 {
5987 #if defined(CONFIG_BT_HCI_ACL_FLOW_CONTROL)
5988 	/* Clear any pending packets upon disconnection */
5989 	/* Note: This requires linear handle values starting from 0 */
5990 	LL_ASSERT(handle < ARRAY_SIZE(hci_hbuf_pend));
5991 	hci_hbuf_acked += hci_hbuf_pend[handle];
5992 	hci_hbuf_pend[handle] = 0U;
5993 #endif /* CONFIG_BT_HCI_ACL_FLOW_CONTROL */
5994 	conn_count--;
5995 }
5996 
5997 static void le_conn_update_complete(struct pdu_data *pdu_data, uint16_t handle,
5998 				    struct net_buf *buf)
5999 {
6000 	struct bt_hci_evt_le_conn_update_complete *sep;
6001 	struct node_rx_cu *cu;
6002 
6003 	if (!(event_mask & BT_EVT_MASK_LE_META_EVENT) ||
6004 	    !(le_event_mask & BT_EVT_MASK_LE_CONN_UPDATE_COMPLETE)) {
6005 		return;
6006 	}
6007 
6008 	sep = meta_evt(buf, BT_HCI_EVT_LE_CONN_UPDATE_COMPLETE, sizeof(*sep));
6009 
6010 	cu = (void *)pdu_data;
6011 	sep->status = cu->status;
6012 	sep->handle = sys_cpu_to_le16(handle);
6013 	sep->interval = sys_cpu_to_le16(cu->interval);
6014 	sep->latency = sys_cpu_to_le16(cu->latency);
6015 	sep->supv_timeout = sys_cpu_to_le16(cu->timeout);
6016 }
6017 
6018 #if defined(CONFIG_BT_CTLR_LE_ENC)
6019 static void enc_refresh_complete(struct pdu_data *pdu_data, uint16_t handle,
6020 				 struct net_buf *buf)
6021 {
6022 	struct bt_hci_evt_encrypt_key_refresh_complete *ep;
6023 
6024 	if (!(event_mask & BT_EVT_MASK_ENCRYPT_KEY_REFRESH_COMPLETE)) {
6025 		return;
6026 	}
6027 
6028 	hci_evt_create(buf, BT_HCI_EVT_ENCRYPT_KEY_REFRESH_COMPLETE,
6029 		       sizeof(*ep));
6030 	ep = net_buf_add(buf, sizeof(*ep));
6031 
6032 	ep->status = 0x00;
6033 	ep->handle = sys_cpu_to_le16(handle);
6034 }
6035 #endif /* CONFIG_BT_CTLR_LE_ENC */
6036 
6037 #if defined(CONFIG_BT_CTLR_LE_PING)
6038 static void auth_payload_timeout_exp(struct pdu_data *pdu_data, uint16_t handle,
6039 				     struct net_buf *buf)
6040 {
6041 	struct bt_hci_evt_auth_payload_timeout_exp *ep;
6042 
6043 	if (!(event_mask_page_2 & BT_EVT_MASK_AUTH_PAYLOAD_TIMEOUT_EXP)) {
6044 		return;
6045 	}
6046 
6047 	hci_evt_create(buf, BT_HCI_EVT_AUTH_PAYLOAD_TIMEOUT_EXP, sizeof(*ep));
6048 	ep = net_buf_add(buf, sizeof(*ep));
6049 
6050 	ep->handle = sys_cpu_to_le16(handle);
6051 }
6052 #endif /* CONFIG_BT_CTLR_LE_PING */
6053 
6054 #if defined(CONFIG_BT_CTLR_CHAN_SEL_2)
6055 static void le_chan_sel_algo(struct pdu_data *pdu_data, uint16_t handle,
6056 			     struct net_buf *buf)
6057 {
6058 	struct bt_hci_evt_le_chan_sel_algo *sep;
6059 	struct node_rx_cs *cs;
6060 
6061 	cs = (void *)pdu_data;
6062 
6063 	if (!(event_mask & BT_EVT_MASK_LE_META_EVENT) ||
6064 	    !(le_event_mask & BT_EVT_MASK_LE_CHAN_SEL_ALGO)) {
6065 		BT_DBG("handle: 0x%04x, CSA: %x.", handle, cs->csa);
6066 		return;
6067 	}
6068 
6069 	sep = meta_evt(buf, BT_HCI_EVT_LE_CHAN_SEL_ALGO, sizeof(*sep));
6070 
6071 	sep->handle = sys_cpu_to_le16(handle);
6072 	sep->chan_sel_algo = cs->csa;
6073 }
6074 #endif /* CONFIG_BT_CTLR_CHAN_SEL_2 */
6075 
6076 #if defined(CONFIG_BT_CTLR_PHY)
6077 static void le_phy_upd_complete(struct pdu_data *pdu_data, uint16_t handle,
6078 				struct net_buf *buf)
6079 {
6080 	struct bt_hci_evt_le_phy_update_complete *sep;
6081 	struct node_rx_pu *pu;
6082 
6083 	pu = (void *)pdu_data;
6084 
6085 	if (!(event_mask & BT_EVT_MASK_LE_META_EVENT) ||
6086 	    !(le_event_mask & BT_EVT_MASK_LE_PHY_UPDATE_COMPLETE)) {
6087 		BT_WARN("handle: 0x%04x, status: %x, tx: %x, rx: %x.", handle,
6088 			pu->status,
6089 			find_lsb_set(pu->tx),
6090 			find_lsb_set(pu->rx));
6091 		return;
6092 	}
6093 
6094 	sep = meta_evt(buf, BT_HCI_EVT_LE_PHY_UPDATE_COMPLETE, sizeof(*sep));
6095 
6096 	sep->status = pu->status;
6097 	sep->handle = sys_cpu_to_le16(handle);
6098 	sep->tx_phy = find_lsb_set(pu->tx);
6099 	sep->rx_phy = find_lsb_set(pu->rx);
6100 }
6101 #endif /* CONFIG_BT_CTLR_PHY */
6102 #endif /* CONFIG_BT_CONN */
6103 
6104 #if defined(CONFIG_BT_HCI_MESH_EXT)
6105 static void mesh_adv_cplt(struct pdu_data *pdu_data,
6106 			  struct node_rx_pdu *node_rx,
6107 			  struct net_buf *buf)
6108 {
6109 	struct bt_hci_evt_mesh_adv_complete *mep;
6110 
6111 	mep = mesh_evt(buf, BT_HCI_EVT_MESH_ADV_COMPLETE, sizeof(*mep));
6112 	mep->adv_slot = ((uint8_t *)pdu_data)[0];
6113 }
6114 #endif /* CONFIG_BT_HCI_MESH_EXT */
6115 
6116 /**
6117  * @brief Encode a control-PDU into an HCI buffer
6118  * @details Execution context: Host thread
6119  *
6120  * @param node_rx_pdu[in] RX node containing header and PDU
6121  * @param pdu_data[in]    PDU. Same as node_rx_pdu->pdu, but more convenient
6122  * @param net_buf[out]    Upwards-going HCI buffer to fill
6123  */
6124 static void encode_control(struct node_rx_pdu *node_rx,
6125 			   struct pdu_data *pdu_data, struct net_buf *buf)
6126 {
6127 	uint16_t handle;
6128 
6129 	handle = node_rx->hdr.handle;
6130 
6131 	switch (node_rx->hdr.type) {
6132 #if defined(CONFIG_BT_OBSERVER)
6133 	case NODE_RX_TYPE_REPORT:
6134 		le_advertising_report(pdu_data, node_rx, buf);
6135 		break;
6136 
6137 #if defined(CONFIG_BT_CTLR_ADV_EXT)
6138 	case NODE_RX_TYPE_EXT_1M_REPORT:
6139 		le_adv_ext_1M_report(pdu_data, node_rx, buf);
6140 		break;
6141 
6142 	case NODE_RX_TYPE_EXT_2M_REPORT:
6143 		le_adv_ext_2M_report(pdu_data, node_rx, buf);
6144 		break;
6145 
6146 	case NODE_RX_TYPE_EXT_CODED_REPORT:
6147 		le_adv_ext_coded_report(pdu_data, node_rx, buf);
6148 		break;
6149 
6150 	case NODE_RX_TYPE_EXT_SCAN_TERMINATE:
6151 		le_scan_timeout(pdu_data, node_rx, buf);
6152 		break;
6153 
6154 #if defined(CONFIG_BT_CTLR_SYNC_PERIODIC)
6155 	case NODE_RX_TYPE_SYNC:
6156 		le_per_adv_sync_established(pdu_data, node_rx, buf);
6157 		break;
6158 
6159 	case NODE_RX_TYPE_SYNC_REPORT:
6160 		le_per_adv_sync_report(pdu_data, node_rx, buf);
6161 		break;
6162 
6163 	case NODE_RX_TYPE_SYNC_LOST:
6164 		le_per_adv_sync_lost(pdu_data, node_rx, buf);
6165 		break;
6166 #if defined(CONFIG_BT_CTLR_DF_SCAN_CTE_RX)
6167 	case NODE_RX_TYPE_IQ_SAMPLE_REPORT:
6168 		le_df_connectionless_iq_report(pdu_data, node_rx, buf);
6169 		break;
6170 #endif /* CONFIG_BT_CTLR_DF_SCAN_CTE_RX */
6171 #endif /* CONFIG_BT_CTLR_SYNC_PERIODIC */
6172 #endif /* CONFIG_BT_CTLR_ADV_EXT */
6173 #endif /* CONFIG_BT_OBSERVER */
6174 
6175 #if defined(CONFIG_BT_BROADCASTER)
6176 #if defined(CONFIG_BT_CTLR_ADV_EXT)
6177 	case NODE_RX_TYPE_EXT_ADV_TERMINATE:
6178 		le_adv_ext_terminate(pdu_data, node_rx, buf);
6179 		break;
6180 
6181 #if defined(CONFIG_BT_CTLR_ADV_ISO)
6182 	case NODE_RX_TYPE_BIG_COMPLETE:
6183 		le_big_complete(pdu_data, node_rx, buf);
6184 		break;
6185 #endif /* CONFIG_BT_CTLR_ADV_ISO */
6186 #endif /* CONFIG_BT_CTLR_ADV_EXT */
6187 #endif /* CONFIG_BT_BROADCASTER */
6188 
6189 #if defined(CONFIG_BT_CTLR_SCAN_REQ_NOTIFY)
6190 	case NODE_RX_TYPE_SCAN_REQ:
6191 		le_scan_req_received(pdu_data, node_rx, buf);
6192 		break;
6193 #endif /* CONFIG_BT_CTLR_SCAN_REQ_NOTIFY */
6194 
6195 #if defined(CONFIG_BT_CONN)
6196 	case NODE_RX_TYPE_CONNECTION:
6197 		le_conn_complete(pdu_data, handle, buf);
6198 		break;
6199 
6200 	case NODE_RX_TYPE_TERMINATE:
6201 		hci_disconn_complete_encode(pdu_data, handle, buf);
6202 		break;
6203 
6204 	case NODE_RX_TYPE_CONN_UPDATE:
6205 		le_conn_update_complete(pdu_data, handle, buf);
6206 		break;
6207 
6208 #if defined(CONFIG_BT_CTLR_LE_ENC)
6209 	case NODE_RX_TYPE_ENC_REFRESH:
6210 		enc_refresh_complete(pdu_data, handle, buf);
6211 		break;
6212 #endif /* CONFIG_BT_CTLR_LE_ENC */
6213 
6214 #if defined(CONFIG_BT_CTLR_LE_PING)
6215 	case NODE_RX_TYPE_APTO:
6216 		auth_payload_timeout_exp(pdu_data, handle, buf);
6217 		break;
6218 #endif /* CONFIG_BT_CTLR_LE_PING */
6219 
6220 #if defined(CONFIG_BT_CTLR_CHAN_SEL_2)
6221 	case NODE_RX_TYPE_CHAN_SEL_ALGO:
6222 		le_chan_sel_algo(pdu_data, handle, buf);
6223 		break;
6224 #endif /* CONFIG_BT_CTLR_CHAN_SEL_2 */
6225 
6226 #if defined(CONFIG_BT_CTLR_PHY)
6227 	case NODE_RX_TYPE_PHY_UPDATE:
6228 		le_phy_upd_complete(pdu_data, handle, buf);
6229 		return;
6230 #endif /* CONFIG_BT_CTLR_PHY */
6231 
6232 #if defined(CONFIG_BT_CTLR_CONN_RSSI_EVENT)
6233 	case NODE_RX_TYPE_RSSI:
6234 		BT_INFO("handle: 0x%04x, rssi: -%d dB.", handle,
6235 			pdu_data->rssi);
6236 		return;
6237 #endif /* CONFIG_BT_CTLR_CONN_RSSI_EVENT */
6238 
6239 #if defined(CONFIG_BT_CTLR_PERIPHERAL_ISO)
6240 	case NODE_RX_TYPE_CIS_REQUEST:
6241 		le_cis_request(pdu_data, node_rx, buf);
6242 		return;
6243 #endif /* CONFIG_BT_CTLR_PERIPHERAL_ISO */
6244 
6245 #if defined(CONFIG_BT_CTLR_CONN_ISO)
6246 	case NODE_RX_TYPE_CIS_ESTABLISHED:
6247 		le_cis_established(pdu_data, node_rx, buf);
6248 		return;
6249 #endif /* CONFIG_BT_CTLR_CONN_ISO */
6250 
6251 #endif /* CONFIG_BT_CONN */
6252 
6253 #if defined(CONFIG_BT_CTLR_ADV_INDICATION)
6254 	case NODE_RX_TYPE_ADV_INDICATION:
6255 		BT_INFO("Advertised.");
6256 		return;
6257 #endif /* CONFIG_BT_CTLR_ADV_INDICATION */
6258 
6259 #if defined(CONFIG_BT_CTLR_SCAN_INDICATION)
6260 	case NODE_RX_TYPE_SCAN_INDICATION:
6261 		BT_INFO("Scanned.");
6262 		return;
6263 #endif /* CONFIG_BT_CTLR_SCAN_INDICATION */
6264 
6265 #if defined(CONFIG_BT_CTLR_PROFILE_ISR)
6266 	case NODE_RX_TYPE_PROFILE:
6267 		BT_INFO("l: %u, %u, %u; t: %u, %u, %u; cpu: %u, %u, %u, %u.",
6268 			pdu_data->profile.lcur,
6269 			pdu_data->profile.lmin,
6270 			pdu_data->profile.lmax,
6271 			pdu_data->profile.cur,
6272 			pdu_data->profile.min,
6273 			pdu_data->profile.max,
6274 			pdu_data->profile.radio,
6275 			pdu_data->profile.lll,
6276 			pdu_data->profile.ull_high,
6277 			pdu_data->profile.ull_low);
6278 		return;
6279 #endif /* CONFIG_BT_CTLR_PROFILE_ISR */
6280 
6281 #if defined(CONFIG_BT_HCI_MESH_EXT)
6282 	case NODE_RX_TYPE_MESH_ADV_CPLT:
6283 		mesh_adv_cplt(pdu_data, node_rx, buf);
6284 		return;
6285 
6286 	case NODE_RX_TYPE_MESH_REPORT:
6287 		le_advertising_report(pdu_data, node_rx, buf);
6288 		return;
6289 #endif /* CONFIG_BT_HCI_MESH_EXT */
6290 
6291 #if CONFIG_BT_CTLR_USER_EVT_RANGE > 0
6292 	case NODE_RX_TYPE_USER_START ... NODE_RX_TYPE_USER_END - 1:
6293 		hci_user_ext_encode_control(node_rx, pdu_data, buf);
6294 		return;
6295 #endif /* CONFIG_BT_CTLR_USER_EVT_RANGE > 0 */
6296 
6297 	default:
6298 		LL_ASSERT(0);
6299 		return;
6300 	}
6301 }
6302 
6303 #if defined(CONFIG_BT_CTLR_LE_ENC)
6304 static void le_ltk_request(struct pdu_data *pdu_data, uint16_t handle,
6305 			   struct net_buf *buf)
6306 {
6307 	struct bt_hci_evt_le_ltk_request *sep;
6308 
6309 	if (!(event_mask & BT_EVT_MASK_LE_META_EVENT) ||
6310 	    !(le_event_mask & BT_EVT_MASK_LE_LTK_REQUEST)) {
6311 		return;
6312 	}
6313 
6314 	sep = meta_evt(buf, BT_HCI_EVT_LE_LTK_REQUEST, sizeof(*sep));
6315 
6316 	sep->handle = sys_cpu_to_le16(handle);
6317 	memcpy(&sep->rand, pdu_data->llctrl.enc_req.rand, sizeof(uint64_t));
6318 	memcpy(&sep->ediv, pdu_data->llctrl.enc_req.ediv, sizeof(uint16_t));
6319 }
6320 
6321 static void encrypt_change(uint8_t err, uint16_t handle,
6322 			   struct net_buf *buf)
6323 {
6324 	struct bt_hci_evt_encrypt_change *ep;
6325 
6326 	if (!(event_mask & BT_EVT_MASK_ENCRYPT_CHANGE)) {
6327 		return;
6328 	}
6329 
6330 	hci_evt_create(buf, BT_HCI_EVT_ENCRYPT_CHANGE, sizeof(*ep));
6331 	ep = net_buf_add(buf, sizeof(*ep));
6332 
6333 	ep->status = err;
6334 	ep->handle = sys_cpu_to_le16(handle);
6335 	ep->encrypt = !err ? 1 : 0;
6336 }
6337 #endif /* CONFIG_BT_CTLR_LE_ENC */
6338 
6339 static void le_remote_feat_complete(uint8_t status, struct pdu_data *pdu_data,
6340 				    uint16_t handle, struct net_buf *buf)
6341 {
6342 	struct bt_hci_evt_le_remote_feat_complete *sep;
6343 
6344 	if (!(event_mask & BT_EVT_MASK_LE_META_EVENT) ||
6345 	    !(le_event_mask & BT_EVT_MASK_LE_REMOTE_FEAT_COMPLETE)) {
6346 		return;
6347 	}
6348 
6349 	sep = meta_evt(buf, BT_HCI_EVT_LE_REMOTE_FEAT_COMPLETE, sizeof(*sep));
6350 
6351 	sep->status = status;
6352 	sep->handle = sys_cpu_to_le16(handle);
6353 	if (!status) {
6354 		memcpy(&sep->features[0],
6355 		       &pdu_data->llctrl.feature_rsp.features[0],
6356 		       sizeof(sep->features));
6357 	} else {
6358 		(void)memset(&sep->features[0], 0x00, sizeof(sep->features));
6359 	}
6360 }
6361 
6362 static void le_unknown_rsp(struct pdu_data *pdu_data, uint16_t handle,
6363 			   struct net_buf *buf)
6364 {
6365 
6366 	switch (pdu_data->llctrl.unknown_rsp.type) {
6367 	case PDU_DATA_LLCTRL_TYPE_PER_INIT_FEAT_XCHG:
6368 		le_remote_feat_complete(BT_HCI_ERR_UNSUPP_REMOTE_FEATURE,
6369 					    NULL, handle, buf);
6370 		break;
6371 
6372 	default:
6373 		BT_WARN("type: 0x%02x",	pdu_data->llctrl.unknown_rsp.type);
6374 		break;
6375 	}
6376 }
6377 
6378 #if defined(CONFIG_BT_CTLR_CONN_PARAM_REQ)
6379 static void le_conn_param_req(struct pdu_data *pdu_data, uint16_t handle,
6380 			      struct net_buf *buf)
6381 {
6382 	struct bt_hci_evt_le_conn_param_req *sep;
6383 
6384 	if (!(event_mask & BT_EVT_MASK_LE_META_EVENT) ||
6385 	    !(le_event_mask & BT_EVT_MASK_LE_CONN_PARAM_REQ)) {
6386 		/* event masked, reject the conn param req */
6387 		ll_conn_update(handle, 2, BT_HCI_ERR_UNSUPP_REMOTE_FEATURE, 0,
6388 			       0, 0, 0);
6389 
6390 		return;
6391 	}
6392 
6393 	sep = meta_evt(buf, BT_HCI_EVT_LE_CONN_PARAM_REQ, sizeof(*sep));
6394 
6395 	sep->handle = sys_cpu_to_le16(handle);
6396 	sep->interval_min = pdu_data->llctrl.conn_param_req.interval_min;
6397 	sep->interval_max = pdu_data->llctrl.conn_param_req.interval_max;
6398 	sep->latency = pdu_data->llctrl.conn_param_req.latency;
6399 	sep->timeout = pdu_data->llctrl.conn_param_req.timeout;
6400 }
6401 #endif /* CONFIG_BT_CTLR_CONN_PARAM_REQ */
6402 
6403 #if defined(CONFIG_BT_CTLR_DATA_LENGTH)
6404 static void le_data_len_change(struct pdu_data *pdu_data, uint16_t handle,
6405 			       struct net_buf *buf)
6406 {
6407 	struct bt_hci_evt_le_data_len_change *sep;
6408 
6409 	if (!(event_mask & BT_EVT_MASK_LE_META_EVENT) ||
6410 	    !(le_event_mask & BT_EVT_MASK_LE_DATA_LEN_CHANGE)) {
6411 		return;
6412 	}
6413 
6414 	sep = meta_evt(buf, BT_HCI_EVT_LE_DATA_LEN_CHANGE, sizeof(*sep));
6415 
6416 	sep->handle = sys_cpu_to_le16(handle);
6417 	sep->max_tx_octets = pdu_data->llctrl.length_rsp.max_tx_octets;
6418 	sep->max_tx_time = pdu_data->llctrl.length_rsp.max_tx_time;
6419 	sep->max_rx_octets = pdu_data->llctrl.length_rsp.max_rx_octets;
6420 	sep->max_rx_time = pdu_data->llctrl.length_rsp.max_rx_time;
6421 }
6422 #endif /* CONFIG_BT_CTLR_DATA_LENGTH */
6423 
6424 #if defined(CONFIG_BT_REMOTE_VERSION)
6425 static void remote_version_info_encode(struct pdu_data *pdu_data,
6426 				       uint16_t handle, struct net_buf *buf)
6427 {
6428 	struct pdu_data_llctrl_version_ind *ver_ind;
6429 	struct bt_hci_evt_remote_version_info *ep;
6430 
6431 	if (!(event_mask & BT_EVT_MASK_REMOTE_VERSION_INFO)) {
6432 		return;
6433 	}
6434 
6435 	hci_evt_create(buf, BT_HCI_EVT_REMOTE_VERSION_INFO, sizeof(*ep));
6436 	ep = net_buf_add(buf, sizeof(*ep));
6437 
6438 	ver_ind = &pdu_data->llctrl.version_ind;
6439 	ep->status = 0x00;
6440 	ep->handle = sys_cpu_to_le16(handle);
6441 	ep->version = ver_ind->version_number;
6442 	ep->manufacturer = ver_ind->company_id;
6443 	ep->subversion = ver_ind->sub_version_number;
6444 }
6445 #endif /* CONFIG_BT_REMOTE_VERSION */
6446 
6447 static void encode_data_ctrl(struct node_rx_pdu *node_rx,
6448 			     struct pdu_data *pdu_data, struct net_buf *buf)
6449 {
6450 	uint16_t handle = node_rx->hdr.handle;
6451 
6452 	switch (pdu_data->llctrl.opcode) {
6453 
6454 #if defined(CONFIG_BT_CTLR_LE_ENC)
6455 	case PDU_DATA_LLCTRL_TYPE_ENC_REQ:
6456 		le_ltk_request(pdu_data, handle, buf);
6457 		break;
6458 
6459 	case PDU_DATA_LLCTRL_TYPE_START_ENC_RSP:
6460 		encrypt_change(0x00, handle, buf);
6461 		break;
6462 #endif /* CONFIG_BT_CTLR_LE_ENC */
6463 
6464 #if defined(CONFIG_BT_REMOTE_VERSION)
6465 	case PDU_DATA_LLCTRL_TYPE_VERSION_IND:
6466 		remote_version_info_encode(pdu_data, handle, buf);
6467 		break;
6468 #endif /* defined(CONFIG_BT_REMOTE_VERSION) */
6469 
6470 	case PDU_DATA_LLCTRL_TYPE_FEATURE_RSP:
6471 		le_remote_feat_complete(0x00, pdu_data, handle, buf);
6472 		break;
6473 
6474 #if defined(CONFIG_BT_CTLR_LE_ENC)
6475 	case PDU_DATA_LLCTRL_TYPE_REJECT_IND:
6476 		encrypt_change(pdu_data->llctrl.reject_ind.error_code, handle,
6477 			       buf);
6478 		break;
6479 #endif /* CONFIG_BT_CTLR_LE_ENC */
6480 
6481 #if defined(CONFIG_BT_CTLR_CONN_PARAM_REQ)
6482 	case PDU_DATA_LLCTRL_TYPE_CONN_PARAM_REQ:
6483 		le_conn_param_req(pdu_data, handle, buf);
6484 		break;
6485 #endif /* CONFIG_BT_CTLR_CONN_PARAM_REQ */
6486 
6487 #if defined(CONFIG_BT_CTLR_DATA_LENGTH)
6488 	case PDU_DATA_LLCTRL_TYPE_LENGTH_REQ:
6489 	case PDU_DATA_LLCTRL_TYPE_LENGTH_RSP:
6490 		le_data_len_change(pdu_data, handle, buf);
6491 		break;
6492 #endif /* CONFIG_BT_CTLR_DATA_LENGTH */
6493 
6494 	case PDU_DATA_LLCTRL_TYPE_UNKNOWN_RSP:
6495 		le_unknown_rsp(pdu_data, handle, buf);
6496 		break;
6497 
6498 	default:
6499 		LL_ASSERT(0);
6500 		return;
6501 	}
6502 }
6503 
6504 #if defined(CONFIG_BT_CONN)
6505 void hci_acl_encode(struct node_rx_pdu *node_rx, struct net_buf *buf)
6506 {
6507 	struct pdu_data *pdu_data = (void *)node_rx->pdu;
6508 	struct bt_hci_acl_hdr *acl;
6509 	uint16_t handle_flags;
6510 	uint16_t handle;
6511 	uint8_t *data;
6512 
6513 	handle = node_rx->hdr.handle;
6514 
6515 	switch (pdu_data->ll_id) {
6516 	case PDU_DATA_LLID_DATA_CONTINUE:
6517 	case PDU_DATA_LLID_DATA_START:
6518 		acl = (void *)net_buf_add(buf, sizeof(*acl));
6519 		if (pdu_data->ll_id == PDU_DATA_LLID_DATA_START) {
6520 			handle_flags = bt_acl_handle_pack(handle, BT_ACL_START);
6521 		} else {
6522 			handle_flags = bt_acl_handle_pack(handle, BT_ACL_CONT);
6523 		}
6524 		acl->handle = sys_cpu_to_le16(handle_flags);
6525 		acl->len = sys_cpu_to_le16(pdu_data->len);
6526 		data = (void *)net_buf_add(buf, pdu_data->len);
6527 		memcpy(data, pdu_data->lldata, pdu_data->len);
6528 #if defined(CONFIG_BT_HCI_ACL_FLOW_CONTROL)
6529 		if (hci_hbuf_total > 0) {
6530 			LL_ASSERT((hci_hbuf_sent - hci_hbuf_acked) <
6531 				  hci_hbuf_total);
6532 			hci_hbuf_sent++;
6533 			/* Note: This requires linear handle values starting
6534 			 * from 0
6535 			 */
6536 			LL_ASSERT(handle < ARRAY_SIZE(hci_hbuf_pend));
6537 			hci_hbuf_pend[handle]++;
6538 		}
6539 #endif
6540 		break;
6541 
6542 	default:
6543 		LL_ASSERT(0);
6544 		break;
6545 	}
6546 }
6547 #endif /* CONFIG_BT_CONN */
6548 
6549 void hci_evt_encode(struct node_rx_pdu *node_rx, struct net_buf *buf)
6550 {
6551 	struct pdu_data *pdu_data = (void *)node_rx->pdu;
6552 
6553 	if (node_rx->hdr.type != NODE_RX_TYPE_DC_PDU) {
6554 		encode_control(node_rx, pdu_data, buf);
6555 	} else if (IS_ENABLED(CONFIG_BT_CONN)) {
6556 		encode_data_ctrl(node_rx, pdu_data, buf);
6557 	}
6558 }
6559 
6560 #if defined(CONFIG_BT_CONN)
6561 void hci_num_cmplt_encode(struct net_buf *buf, uint16_t handle, uint8_t num)
6562 {
6563 	struct bt_hci_evt_num_completed_packets *ep;
6564 	struct bt_hci_handle_count *hc;
6565 	uint8_t num_handles;
6566 	uint8_t len;
6567 
6568 	num_handles = 1U;
6569 
6570 	len = (sizeof(*ep) + (sizeof(*hc) * num_handles));
6571 	hci_evt_create(buf, BT_HCI_EVT_NUM_COMPLETED_PACKETS, len);
6572 
6573 	ep = net_buf_add(buf, len);
6574 	ep->num_handles = num_handles;
6575 	hc = &ep->h[0];
6576 	hc->handle = sys_cpu_to_le16(handle);
6577 	hc->count = sys_cpu_to_le16(num);
6578 }
6579 #endif /* CONFIG_BT_CONN */
6580 
6581 uint8_t hci_get_class(struct node_rx_pdu *node_rx)
6582 {
6583 #if defined(CONFIG_BT_CONN)
6584 	struct pdu_data *pdu_data = (void *)node_rx->pdu;
6585 #endif
6586 
6587 	if (node_rx->hdr.type != NODE_RX_TYPE_DC_PDU) {
6588 
6589 		switch (node_rx->hdr.type) {
6590 #if defined(CONFIG_BT_OBSERVER) || \
6591 	defined(CONFIG_BT_CTLR_SCAN_REQ_NOTIFY) || \
6592 	defined(CONFIG_BT_CTLR_ADV_INDICATION) || \
6593 	defined(CONFIG_BT_CTLR_SCAN_INDICATION) || \
6594 	defined(CONFIG_BT_CTLR_PROFILE_ISR)
6595 #if defined(CONFIG_BT_OBSERVER)
6596 		case NODE_RX_TYPE_REPORT:
6597 
6598 #if defined(CONFIG_BT_CTLR_ADV_EXT)
6599 			__fallthrough;
6600 		case NODE_RX_TYPE_EXT_1M_REPORT:
6601 		case NODE_RX_TYPE_EXT_2M_REPORT:
6602 		case NODE_RX_TYPE_EXT_CODED_REPORT:
6603 #endif /* CONFIG_BT_CTLR_ADV_EXT */
6604 #endif /* CONFIG_BT_OBSERVER */
6605 
6606 #if defined(CONFIG_BT_CTLR_SCAN_REQ_NOTIFY)
6607 		case NODE_RX_TYPE_SCAN_REQ:
6608 #endif /* CONFIG_BT_CTLR_SCAN_REQ_NOTIFY */
6609 
6610 #if defined(CONFIG_BT_CTLR_ADV_INDICATION)
6611 		case NODE_RX_TYPE_ADV_INDICATION:
6612 #endif /* CONFIG_BT_CTLR_ADV_INDICATION */
6613 
6614 #if defined(CONFIG_BT_CTLR_SCAN_INDICATION)
6615 		case NODE_RX_TYPE_SCAN_INDICATION:
6616 #endif /* CONFIG_BT_CTLR_SCAN_INDICATION */
6617 
6618 #if defined(CONFIG_BT_CTLR_PROFILE_ISR)
6619 		case NODE_RX_TYPE_PROFILE:
6620 #endif /* CONFIG_BT_CTLR_PROFILE_ISR */
6621 			return HCI_CLASS_EVT_DISCARDABLE;
6622 #endif
6623 
6624 #if defined(CONFIG_BT_HCI_MESH_EXT)
6625 		case NODE_RX_TYPE_MESH_ADV_CPLT:
6626 		case NODE_RX_TYPE_MESH_REPORT:
6627 #endif /* CONFIG_BT_HCI_MESH_EXT */
6628 
6629 #if defined(CONFIG_BT_CTLR_ADV_EXT)
6630 #if defined(CONFIG_BT_BROADCASTER)
6631 		case NODE_RX_TYPE_EXT_ADV_TERMINATE:
6632 #endif /* CONFIG_BT_BROADCASTER */
6633 
6634 #if defined(CONFIG_BT_OBSERVER)
6635 			__fallthrough;
6636 		case NODE_RX_TYPE_EXT_SCAN_TERMINATE:
6637 
6638 #if defined(CONFIG_BT_CTLR_SYNC_PERIODIC)
6639 			__fallthrough;
6640 		case NODE_RX_TYPE_SYNC:
6641 		case NODE_RX_TYPE_SYNC_REPORT:
6642 		case NODE_RX_TYPE_SYNC_LOST:
6643 #if defined(CONFIG_BT_CTLR_DF_SCAN_CTE_RX)
6644 		case NODE_RX_TYPE_IQ_SAMPLE_REPORT:
6645 #endif /* CONFIG_BT_CTLR_DF_SCAN_CTE_RX */
6646 #endif /* CONFIG_BT_CTLR_SYNC_PERIODIC */
6647 #endif /* CONFIG_BT_OBSERVER */
6648 
6649 			return HCI_CLASS_EVT_REQUIRED;
6650 #endif /* CONFIG_BT_CTLR_ADV_EXT */
6651 
6652 #if defined(CONFIG_BT_CONN)
6653 		case NODE_RX_TYPE_CONNECTION:
6654 
6655 #if defined(CONFIG_BT_CTLR_PERIPHERAL_ISO)
6656 		case NODE_RX_TYPE_CIS_REQUEST:
6657 #endif /* CONFIG_BT_CTLR_PERIPHERAL_ISO */
6658 
6659 #if defined(CONFIG_BT_CTLR_CONN_ISO)
6660 		case NODE_RX_TYPE_CIS_ESTABLISHED:
6661 #endif /* CONFIG_BT_CTLR_CONN_ISO */
6662 			return HCI_CLASS_EVT_REQUIRED;
6663 
6664 		case NODE_RX_TYPE_TERMINATE:
6665 		case NODE_RX_TYPE_CONN_UPDATE:
6666 
6667 #if defined(CONFIG_BT_CTLR_LE_ENC)
6668 		case NODE_RX_TYPE_ENC_REFRESH:
6669 #endif /* CONFIG_BT_CTLR_LE_ENC */
6670 
6671 #if defined(CONFIG_BT_CTLR_CONN_RSSI_EVENT)
6672 		case NODE_RX_TYPE_RSSI:
6673 #endif /* CONFIG_BT_CTLR_CONN_RSSI_EVENT */
6674 #if defined(CONFIG_BT_CTLR_LE_PING)
6675 		case NODE_RX_TYPE_APTO:
6676 #endif /* CONFIG_BT_CTLR_LE_PING */
6677 #if defined(CONFIG_BT_CTLR_CHAN_SEL_2)
6678 		case NODE_RX_TYPE_CHAN_SEL_ALGO:
6679 #endif /* CONFIG_BT_CTLR_CHAN_SEL_2 */
6680 #if defined(CONFIG_BT_CTLR_PHY)
6681 		case NODE_RX_TYPE_PHY_UPDATE:
6682 #endif /* CONFIG_BT_CTLR_PHY */
6683 			return HCI_CLASS_EVT_CONNECTION;
6684 #endif /* CONFIG_BT_CONN */
6685 #if defined(CONFIG_BT_CTLR_ISO)
6686 		case NODE_RX_TYPE_ISO_PDU:
6687 			return HCI_CLASS_ISO_DATA;
6688 #endif
6689 
6690 #if CONFIG_BT_CTLR_USER_EVT_RANGE > 0
6691 		case NODE_RX_TYPE_USER_START ... NODE_RX_TYPE_USER_END - 1:
6692 			return hci_user_ext_get_class(node_rx);
6693 #endif /* CONFIG_BT_CTLR_USER_EVT_RANGE > 0 */
6694 
6695 		default:
6696 			return HCI_CLASS_NONE;
6697 		}
6698 
6699 #if defined(CONFIG_BT_CONN)
6700 	} else if (pdu_data->ll_id == PDU_DATA_LLID_CTRL) {
6701 		return HCI_CLASS_EVT_LLCP;
6702 	} else {
6703 		return HCI_CLASS_ACL_DATA;
6704 	}
6705 #else
6706 	} else {
6707 		return HCI_CLASS_NONE;
6708 	}
6709 #endif
6710 }
6711 
6712 void hci_init(struct k_poll_signal *signal_host_buf)
6713 {
6714 #if defined(CONFIG_BT_HCI_ACL_FLOW_CONTROL)
6715 	hbuf_signal = signal_host_buf;
6716 #endif
6717 	reset(NULL, NULL);
6718 }
6719