1 /*
2  * Copyright (c) 2017-2021 Nordic Semiconductor ASA
3  * Copyright (c) 2015-2016 Intel Corporation
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 #include <sys/types.h>
8 
9 #include <zephyr/sys/byteorder.h>
10 #include <zephyr/sys/check.h>
11 
12 #include <zephyr/bluetooth/bluetooth.h>
13 #include <zephyr/bluetooth/iso.h>
14 #include <zephyr/bluetooth/buf.h>
15 #include <zephyr/bluetooth/direction.h>
16 #include <zephyr/bluetooth/addr.h>
17 #include <zephyr/bluetooth/hci.h>
18 #include <zephyr/bluetooth/hci_vs.h>
19 
20 #include "addr_internal.h"
21 #include "hci_core.h"
22 #include "conn_internal.h"
23 #include "direction_internal.h"
24 #include "id.h"
25 
26 #include "common/bt_str.h"
27 
28 #define LOG_LEVEL CONFIG_BT_HCI_CORE_LOG_LEVEL
29 #include <zephyr/logging/log.h>
30 LOG_MODULE_REGISTER(bt_scan);
31 
32 static bt_le_scan_cb_t *scan_dev_found_cb;
33 static sys_slist_t scan_cbs = SYS_SLIST_STATIC_INIT(&scan_cbs);
34 
35 #if defined(CONFIG_BT_EXT_ADV)
36 /* A buffer used to reassemble advertisement data from the controller. */
37 NET_BUF_SIMPLE_DEFINE(ext_scan_buf, CONFIG_BT_EXT_SCAN_BUF_SIZE);
38 
39 struct fragmented_advertiser {
40 	bt_addr_le_t addr;
41 	uint8_t sid;
42 	enum {
43 		FRAG_ADV_INACTIVE,
44 		FRAG_ADV_REASSEMBLING,
45 		FRAG_ADV_DISCARDING,
46 	} state;
47 };
48 
49 static struct fragmented_advertiser reassembling_advertiser;
50 
fragmented_advertisers_equal(const struct fragmented_advertiser * a,const bt_addr_le_t * addr,uint8_t sid)51 static bool fragmented_advertisers_equal(const struct fragmented_advertiser *a,
52 					 const bt_addr_le_t *addr, uint8_t sid)
53 {
54 	/* Two advertisers are equal if they are the same adv set from the same device */
55 	return a->sid == sid && bt_addr_le_eq(&a->addr, addr);
56 }
57 
58 /* Sets the address and sid of the advertiser to be reassembled. */
init_reassembling_advertiser(const bt_addr_le_t * addr,uint8_t sid)59 static void init_reassembling_advertiser(const bt_addr_le_t *addr, uint8_t sid)
60 {
61 	bt_addr_le_copy(&reassembling_advertiser.addr, addr);
62 	reassembling_advertiser.sid = sid;
63 	reassembling_advertiser.state = FRAG_ADV_REASSEMBLING;
64 }
65 
reset_reassembling_advertiser(void)66 static void reset_reassembling_advertiser(void)
67 {
68 	net_buf_simple_reset(&ext_scan_buf);
69 	reassembling_advertiser.state = FRAG_ADV_INACTIVE;
70 }
71 
72 #if defined(CONFIG_BT_PER_ADV_SYNC)
73 static struct bt_le_per_adv_sync *get_pending_per_adv_sync(void);
74 static struct bt_le_per_adv_sync per_adv_sync_pool[CONFIG_BT_PER_ADV_SYNC_MAX];
75 static sys_slist_t pa_sync_cbs = SYS_SLIST_STATIC_INIT(&pa_sync_cbs);
76 #endif /* defined(CONFIG_BT_PER_ADV_SYNC) */
77 #endif /* defined(CONFIG_BT_EXT_ADV) */
78 
bt_scan_reset(void)79 void bt_scan_reset(void)
80 {
81 	scan_dev_found_cb = NULL;
82 #if defined(CONFIG_BT_EXT_ADV)
83 	reset_reassembling_advertiser();
84 #endif
85 }
86 
set_le_ext_scan_enable(uint8_t enable,uint16_t duration)87 static int set_le_ext_scan_enable(uint8_t enable, uint16_t duration)
88 {
89 	struct bt_hci_cp_le_set_ext_scan_enable *cp;
90 	struct bt_hci_cmd_state_set state;
91 	struct net_buf *buf;
92 	int err;
93 
94 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_EXT_SCAN_ENABLE, sizeof(*cp));
95 	if (!buf) {
96 		return -ENOBUFS;
97 	}
98 
99 	cp = net_buf_add(buf, sizeof(*cp));
100 
101 	if (enable == BT_HCI_LE_SCAN_ENABLE) {
102 		cp->filter_dup = atomic_test_bit(bt_dev.flags,
103 						 BT_DEV_SCAN_FILTER_DUP);
104 	} else {
105 		cp->filter_dup = BT_HCI_LE_SCAN_FILTER_DUP_DISABLE;
106 	}
107 
108 	cp->enable = enable;
109 	cp->duration = sys_cpu_to_le16(duration);
110 	cp->period = 0;
111 
112 	bt_hci_cmd_state_set_init(buf, &state, bt_dev.flags, BT_DEV_SCANNING,
113 				  enable == BT_HCI_LE_SCAN_ENABLE);
114 
115 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_EXT_SCAN_ENABLE, buf, NULL);
116 	if (err) {
117 		return err;
118 	}
119 
120 	return 0;
121 }
122 
bt_le_scan_set_enable_legacy(uint8_t enable)123 static int bt_le_scan_set_enable_legacy(uint8_t enable)
124 {
125 	struct bt_hci_cp_le_set_scan_enable *cp;
126 	struct bt_hci_cmd_state_set state;
127 	struct net_buf *buf;
128 	int err;
129 
130 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_SCAN_ENABLE, sizeof(*cp));
131 	if (!buf) {
132 		return -ENOBUFS;
133 	}
134 
135 	cp = net_buf_add(buf, sizeof(*cp));
136 
137 	if (enable == BT_HCI_LE_SCAN_ENABLE) {
138 		cp->filter_dup = atomic_test_bit(bt_dev.flags,
139 						 BT_DEV_SCAN_FILTER_DUP);
140 	} else {
141 		cp->filter_dup = BT_HCI_LE_SCAN_FILTER_DUP_DISABLE;
142 	}
143 
144 	cp->enable = enable;
145 
146 	bt_hci_cmd_state_set_init(buf, &state, bt_dev.flags, BT_DEV_SCANNING,
147 				  enable == BT_HCI_LE_SCAN_ENABLE);
148 
149 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_SCAN_ENABLE, buf, NULL);
150 	if (err) {
151 		return err;
152 	}
153 
154 	return 0;
155 }
156 
bt_le_scan_set_enable(uint8_t enable)157 int bt_le_scan_set_enable(uint8_t enable)
158 {
159 	if (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
160 	    BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
161 		return set_le_ext_scan_enable(enable, 0);
162 	}
163 
164 	return bt_le_scan_set_enable_legacy(enable);
165 }
166 
start_le_scan_ext(struct bt_hci_ext_scan_phy * phy_1m,struct bt_hci_ext_scan_phy * phy_coded,uint16_t duration)167 static int start_le_scan_ext(struct bt_hci_ext_scan_phy *phy_1m,
168 			     struct bt_hci_ext_scan_phy *phy_coded,
169 			     uint16_t duration)
170 {
171 	struct bt_hci_cp_le_set_ext_scan_param *set_param;
172 	struct net_buf *buf;
173 	uint8_t own_addr_type;
174 	bool active_scan;
175 	int err;
176 
177 	active_scan = (phy_1m && phy_1m->type == BT_HCI_LE_SCAN_ACTIVE) ||
178 		      (phy_coded && phy_coded->type == BT_HCI_LE_SCAN_ACTIVE);
179 
180 	if (duration > 0) {
181 		atomic_set_bit(bt_dev.flags, BT_DEV_SCAN_LIMITED);
182 
183 		/* Allow bt_le_oob_get_local to be called directly before
184 		 * starting a scan limited by timeout.
185 		 */
186 		if (IS_ENABLED(CONFIG_BT_PRIVACY) && !bt_id_rpa_is_new()) {
187 			atomic_clear_bit(bt_dev.flags, BT_DEV_RPA_VALID);
188 		}
189 	}
190 
191 	err = bt_id_set_scan_own_addr(active_scan, &own_addr_type);
192 	if (err) {
193 		return err;
194 	}
195 
196 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_EXT_SCAN_PARAM,
197 				sizeof(*set_param) +
198 				(phy_1m ? sizeof(*phy_1m) : 0) +
199 				(phy_coded ? sizeof(*phy_coded) : 0));
200 	if (!buf) {
201 		return -ENOBUFS;
202 	}
203 
204 	set_param = net_buf_add(buf, sizeof(*set_param));
205 	set_param->own_addr_type = own_addr_type;
206 	set_param->phys = 0;
207 
208 	if (IS_ENABLED(CONFIG_BT_FILTER_ACCEPT_LIST) &&
209 	    atomic_test_bit(bt_dev.flags, BT_DEV_SCAN_FILTERED)) {
210 		set_param->filter_policy = BT_HCI_LE_SCAN_FP_BASIC_FILTER;
211 	} else {
212 		set_param->filter_policy = BT_HCI_LE_SCAN_FP_BASIC_NO_FILTER;
213 	}
214 
215 	if (phy_1m) {
216 		set_param->phys |= BT_HCI_LE_EXT_SCAN_PHY_1M;
217 		net_buf_add_mem(buf, phy_1m, sizeof(*phy_1m));
218 	}
219 
220 	if (phy_coded) {
221 		set_param->phys |= BT_HCI_LE_EXT_SCAN_PHY_CODED;
222 		net_buf_add_mem(buf, phy_coded, sizeof(*phy_coded));
223 	}
224 
225 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_EXT_SCAN_PARAM, buf, NULL);
226 	if (err) {
227 		return err;
228 	}
229 
230 	err = set_le_ext_scan_enable(BT_HCI_LE_SCAN_ENABLE, duration);
231 	if (err) {
232 		return err;
233 	}
234 
235 	atomic_set_bit_to(bt_dev.flags, BT_DEV_ACTIVE_SCAN, active_scan);
236 
237 	return 0;
238 }
239 
start_le_scan_legacy(uint8_t scan_type,uint16_t interval,uint16_t window)240 static int start_le_scan_legacy(uint8_t scan_type, uint16_t interval, uint16_t window)
241 {
242 	struct bt_hci_cp_le_set_scan_param set_param;
243 	struct net_buf *buf;
244 	int err;
245 	bool active_scan;
246 
247 	(void)memset(&set_param, 0, sizeof(set_param));
248 
249 	set_param.scan_type = scan_type;
250 
251 	/* for the rest parameters apply default values according to
252 	 *  spec 4.2, vol2, part E, 7.8.10
253 	 */
254 	set_param.interval = sys_cpu_to_le16(interval);
255 	set_param.window = sys_cpu_to_le16(window);
256 
257 	if (IS_ENABLED(CONFIG_BT_FILTER_ACCEPT_LIST) &&
258 	    atomic_test_bit(bt_dev.flags, BT_DEV_SCAN_FILTERED)) {
259 		set_param.filter_policy = BT_HCI_LE_SCAN_FP_BASIC_FILTER;
260 	} else {
261 		set_param.filter_policy = BT_HCI_LE_SCAN_FP_BASIC_NO_FILTER;
262 	}
263 
264 	active_scan = scan_type == BT_HCI_LE_SCAN_ACTIVE;
265 	err = bt_id_set_scan_own_addr(active_scan, &set_param.addr_type);
266 	if (err) {
267 		return err;
268 	}
269 
270 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_SCAN_PARAM, sizeof(set_param));
271 	if (!buf) {
272 		return -ENOBUFS;
273 	}
274 
275 	net_buf_add_mem(buf, &set_param, sizeof(set_param));
276 
277 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_SCAN_PARAM, buf, NULL);
278 	if (err) {
279 		return err;
280 	}
281 
282 	err = bt_le_scan_set_enable(BT_HCI_LE_SCAN_ENABLE);
283 	if (err) {
284 		return err;
285 	}
286 
287 	atomic_set_bit_to(bt_dev.flags, BT_DEV_ACTIVE_SCAN, active_scan);
288 
289 	return 0;
290 }
291 
start_passive_scan(bool fast_scan)292 static int start_passive_scan(bool fast_scan)
293 {
294 	uint16_t interval, window;
295 
296 	if (fast_scan) {
297 		interval = BT_GAP_SCAN_FAST_INTERVAL;
298 		window = BT_GAP_SCAN_FAST_WINDOW;
299 	} else {
300 		interval = CONFIG_BT_BACKGROUND_SCAN_INTERVAL;
301 		window = CONFIG_BT_BACKGROUND_SCAN_WINDOW;
302 	}
303 
304 	if (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
305 	    BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
306 		struct bt_hci_ext_scan_phy scan;
307 
308 		scan.type = BT_HCI_LE_SCAN_PASSIVE;
309 		scan.interval = sys_cpu_to_le16(interval);
310 		scan.window = sys_cpu_to_le16(window);
311 
312 		return start_le_scan_ext(&scan, NULL, 0);
313 	}
314 
315 	return start_le_scan_legacy(BT_HCI_LE_SCAN_PASSIVE, interval, window);
316 }
317 
bt_le_scan_update(bool fast_scan)318 int bt_le_scan_update(bool fast_scan)
319 {
320 	if (atomic_test_bit(bt_dev.flags, BT_DEV_EXPLICIT_SCAN)) {
321 		return 0;
322 	}
323 
324 	if (atomic_test_bit(bt_dev.flags, BT_DEV_SCANNING)) {
325 		int err;
326 
327 		err = bt_le_scan_set_enable(BT_HCI_LE_SCAN_DISABLE);
328 		if (err) {
329 			return err;
330 		}
331 	}
332 
333 	if (IS_ENABLED(CONFIG_BT_CENTRAL)) {
334 		struct bt_conn *conn;
335 
336 		/* don't restart scan if we have pending connection */
337 		conn = bt_conn_lookup_state_le(BT_ID_DEFAULT, NULL,
338 					       BT_CONN_CONNECTING);
339 		if (conn) {
340 			bt_conn_unref(conn);
341 			return 0;
342 		}
343 
344 		conn = bt_conn_lookup_state_le(BT_ID_DEFAULT, NULL,
345 					       BT_CONN_CONNECTING_SCAN);
346 		if (conn) {
347 			atomic_set_bit(bt_dev.flags, BT_DEV_SCAN_FILTER_DUP);
348 
349 			bt_conn_unref(conn);
350 
351 			return start_passive_scan(fast_scan);
352 		}
353 	}
354 
355 #if defined(CONFIG_BT_PER_ADV_SYNC)
356 	if (get_pending_per_adv_sync()) {
357 		return start_passive_scan(fast_scan);
358 	}
359 #endif
360 
361 	return 0;
362 }
363 
364 #if defined(CONFIG_BT_CENTRAL)
check_pending_conn(const bt_addr_le_t * id_addr,const bt_addr_le_t * addr,uint8_t adv_props)365 static void check_pending_conn(const bt_addr_le_t *id_addr,
366 			       const bt_addr_le_t *addr, uint8_t adv_props)
367 {
368 	struct bt_conn *conn;
369 
370 	/* No connections are allowed during explicit scanning */
371 	if (atomic_test_bit(bt_dev.flags, BT_DEV_EXPLICIT_SCAN)) {
372 		return;
373 	}
374 
375 	/* Return if event is not connectable */
376 	if (!(adv_props & BT_HCI_LE_ADV_EVT_TYPE_CONN)) {
377 		return;
378 	}
379 
380 	conn = bt_conn_lookup_state_le(BT_ID_DEFAULT, id_addr,
381 				       BT_CONN_CONNECTING_SCAN);
382 	if (!conn) {
383 		return;
384 	}
385 
386 	if (atomic_test_bit(bt_dev.flags, BT_DEV_SCANNING) &&
387 	    bt_le_scan_set_enable(BT_HCI_LE_SCAN_DISABLE)) {
388 		goto failed;
389 	}
390 
391 	bt_addr_le_copy(&conn->le.resp_addr, addr);
392 	if (bt_le_create_conn(conn)) {
393 		goto failed;
394 	}
395 
396 	bt_conn_set_state(conn, BT_CONN_CONNECTING);
397 	bt_conn_unref(conn);
398 	return;
399 
400 failed:
401 	conn->err = BT_HCI_ERR_UNSPECIFIED;
402 	bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
403 	bt_conn_unref(conn);
404 	bt_le_scan_update(false);
405 }
406 #endif /* CONFIG_BT_CENTRAL */
407 
408 /* Convert Legacy adv report evt_type field to adv props */
get_adv_props_legacy(uint8_t evt_type)409 static uint8_t get_adv_props_legacy(uint8_t evt_type)
410 {
411 	switch (evt_type) {
412 	case BT_GAP_ADV_TYPE_ADV_IND:
413 		return BT_GAP_ADV_PROP_CONNECTABLE |
414 		       BT_GAP_ADV_PROP_SCANNABLE;
415 
416 	case BT_GAP_ADV_TYPE_ADV_DIRECT_IND:
417 		return BT_GAP_ADV_PROP_CONNECTABLE |
418 		       BT_GAP_ADV_PROP_DIRECTED;
419 
420 	case BT_GAP_ADV_TYPE_ADV_SCAN_IND:
421 		return BT_GAP_ADV_PROP_SCANNABLE;
422 
423 	case BT_GAP_ADV_TYPE_ADV_NONCONN_IND:
424 		return 0;
425 
426 	/* In legacy advertising report, we don't know if the scan
427 	 * response come from a connectable advertiser, so don't
428 	 * set connectable property bit.
429 	 */
430 	case BT_GAP_ADV_TYPE_SCAN_RSP:
431 		return BT_GAP_ADV_PROP_SCAN_RESPONSE |
432 		       BT_GAP_ADV_PROP_SCANNABLE;
433 
434 	default:
435 		return 0;
436 	}
437 }
438 
le_adv_recv(bt_addr_le_t * addr,struct bt_le_scan_recv_info * info,struct net_buf_simple * buf,uint16_t len)439 static void le_adv_recv(bt_addr_le_t *addr, struct bt_le_scan_recv_info *info,
440 			struct net_buf_simple *buf, uint16_t len)
441 {
442 	struct bt_le_scan_cb *listener, *next;
443 	struct net_buf_simple_state state;
444 	bt_addr_le_t id_addr;
445 
446 	LOG_DBG("%s event %u, len %u, rssi %d dBm", bt_addr_le_str(addr), info->adv_type, len,
447 		info->rssi);
448 
449 	if (!IS_ENABLED(CONFIG_BT_PRIVACY) &&
450 	    !IS_ENABLED(CONFIG_BT_SCAN_WITH_IDENTITY) &&
451 	    atomic_test_bit(bt_dev.flags, BT_DEV_EXPLICIT_SCAN) &&
452 	    (info->adv_props & BT_HCI_LE_ADV_PROP_DIRECT)) {
453 		LOG_DBG("Dropped direct adv report");
454 		return;
455 	}
456 
457 	if (bt_addr_le_is_resolved(addr)) {
458 		bt_addr_le_copy_resolved(&id_addr, addr);
459 	} else if (addr->type == BT_HCI_PEER_ADDR_ANONYMOUS) {
460 		bt_addr_le_copy(&id_addr, BT_ADDR_LE_ANY);
461 	} else {
462 		bt_addr_le_copy(&id_addr,
463 				bt_lookup_id_addr(BT_ID_DEFAULT, addr));
464 	}
465 
466 	if (scan_dev_found_cb) {
467 		net_buf_simple_save(buf, &state);
468 
469 		buf->len = len;
470 		scan_dev_found_cb(&id_addr, info->rssi, info->adv_type, buf);
471 
472 		net_buf_simple_restore(buf, &state);
473 	}
474 
475 	info->addr = &id_addr;
476 
477 	SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&scan_cbs, listener, next, node) {
478 		if (listener->recv) {
479 			net_buf_simple_save(buf, &state);
480 
481 			buf->len = len;
482 			listener->recv(info, buf);
483 
484 			net_buf_simple_restore(buf, &state);
485 		}
486 	}
487 
488 	/* Clear pointer to this stack frame before returning to calling function */
489 	info->addr = NULL;
490 
491 #if defined(CONFIG_BT_CENTRAL)
492 	check_pending_conn(&id_addr, addr, info->adv_props);
493 #endif /* CONFIG_BT_CENTRAL */
494 }
495 
496 #if defined(CONFIG_BT_EXT_ADV)
bt_hci_le_scan_timeout(struct net_buf * buf)497 void bt_hci_le_scan_timeout(struct net_buf *buf)
498 {
499 	struct bt_le_scan_cb *listener, *next;
500 
501 	atomic_clear_bit(bt_dev.flags, BT_DEV_SCANNING);
502 	atomic_clear_bit(bt_dev.flags, BT_DEV_EXPLICIT_SCAN);
503 
504 	atomic_clear_bit(bt_dev.flags, BT_DEV_SCAN_LIMITED);
505 	atomic_clear_bit(bt_dev.flags, BT_DEV_RPA_VALID);
506 
507 #if defined(CONFIG_BT_SMP)
508 	bt_id_pending_keys_update();
509 #endif
510 
511 	SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&scan_cbs, listener, next, node) {
512 		if (listener->timeout) {
513 			listener->timeout();
514 		}
515 	}
516 }
517 
518 /* Convert Extended adv report evt_type field into adv type */
get_adv_type(uint8_t evt_type)519 static uint8_t get_adv_type(uint8_t evt_type)
520 {
521 	switch (evt_type) {
522 	case (BT_HCI_LE_ADV_EVT_TYPE_CONN |
523 	      BT_HCI_LE_ADV_EVT_TYPE_SCAN |
524 	      BT_HCI_LE_ADV_EVT_TYPE_LEGACY):
525 		return BT_GAP_ADV_TYPE_ADV_IND;
526 
527 	case (BT_HCI_LE_ADV_EVT_TYPE_CONN |
528 	      BT_HCI_LE_ADV_EVT_TYPE_DIRECT |
529 	      BT_HCI_LE_ADV_EVT_TYPE_LEGACY):
530 		return BT_GAP_ADV_TYPE_ADV_DIRECT_IND;
531 
532 	case (BT_HCI_LE_ADV_EVT_TYPE_SCAN |
533 	      BT_HCI_LE_ADV_EVT_TYPE_LEGACY):
534 		return BT_GAP_ADV_TYPE_ADV_SCAN_IND;
535 
536 	case BT_HCI_LE_ADV_EVT_TYPE_LEGACY:
537 		return BT_GAP_ADV_TYPE_ADV_NONCONN_IND;
538 
539 	case (BT_HCI_LE_ADV_EVT_TYPE_SCAN_RSP |
540 	      BT_HCI_LE_ADV_EVT_TYPE_CONN |
541 	      BT_HCI_LE_ADV_EVT_TYPE_SCAN |
542 	      BT_HCI_LE_ADV_EVT_TYPE_LEGACY):
543 	case (BT_HCI_LE_ADV_EVT_TYPE_SCAN_RSP |
544 	      BT_HCI_LE_ADV_EVT_TYPE_SCAN |
545 	      BT_HCI_LE_ADV_EVT_TYPE_LEGACY):
546 		/* Scan response from connectable or non-connectable advertiser.
547 		 */
548 		return BT_GAP_ADV_TYPE_SCAN_RSP;
549 
550 	default:
551 		return BT_GAP_ADV_TYPE_EXT_ADV;
552 	}
553 }
554 
555 /* Convert extended adv report evt_type field to adv props */
get_adv_props_extended(uint16_t evt_type)556 static uint16_t get_adv_props_extended(uint16_t evt_type)
557 {
558 	/* Converts from BT_HCI_LE_ADV_EVT_TYPE_* to BT_GAP_ADV_PROP_*
559 	 * The first 4 bits are the same (conn, scan, direct, scan_rsp).
560 	 * Bit 4 must be flipped as the meaning of 1 is opposite (legacy -> extended)
561 	 * The rest of the bits are zeroed out.
562 	 */
563 	return (evt_type ^ BT_HCI_LE_ADV_EVT_TYPE_LEGACY) & BIT_MASK(5);
564 }
565 
create_ext_adv_info(struct bt_hci_evt_le_ext_advertising_info const * const evt,struct bt_le_scan_recv_info * const scan_info)566 static void create_ext_adv_info(struct bt_hci_evt_le_ext_advertising_info const *const evt,
567 				struct bt_le_scan_recv_info *const scan_info)
568 {
569 	scan_info->primary_phy = bt_get_phy(evt->prim_phy);
570 	scan_info->secondary_phy = bt_get_phy(evt->sec_phy);
571 	scan_info->tx_power = evt->tx_power;
572 	scan_info->rssi = evt->rssi;
573 	scan_info->sid = evt->sid;
574 	scan_info->interval = sys_le16_to_cpu(evt->interval);
575 	scan_info->adv_type = get_adv_type(sys_le16_to_cpu(evt->evt_type));
576 	scan_info->adv_props = get_adv_props_extended(sys_le16_to_cpu(evt->evt_type));
577 }
578 
bt_hci_le_adv_ext_report(struct net_buf * buf)579 void bt_hci_le_adv_ext_report(struct net_buf *buf)
580 {
581 	uint8_t num_reports = net_buf_pull_u8(buf);
582 
583 	LOG_DBG("Adv number of reports %u", num_reports);
584 
585 	while (num_reports--) {
586 		struct bt_hci_evt_le_ext_advertising_info *evt;
587 		struct bt_le_scan_recv_info scan_info;
588 		uint16_t data_status;
589 		uint16_t evt_type;
590 		bool is_report_complete;
591 		bool more_to_come;
592 		bool is_new_advertiser;
593 
594 		if (buf->len < sizeof(*evt)) {
595 			LOG_ERR("Unexpected end of buffer");
596 			break;
597 		}
598 
599 		evt = net_buf_pull_mem(buf, sizeof(*evt));
600 		evt_type = sys_le16_to_cpu(evt->evt_type);
601 		data_status = BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS(evt_type);
602 		is_report_complete = data_status == BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_COMPLETE;
603 		more_to_come = data_status == BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_PARTIAL;
604 
605 		if (evt_type & BT_HCI_LE_ADV_EVT_TYPE_LEGACY) {
606 			/* Legacy advertising reports are complete.
607 			 * Create event immediately.
608 			 */
609 			create_ext_adv_info(evt, &scan_info);
610 			le_adv_recv(&evt->addr, &scan_info, &buf->b, evt->length);
611 			goto cont;
612 		}
613 
614 		is_new_advertiser = reassembling_advertiser.state == FRAG_ADV_INACTIVE ||
615 				    !fragmented_advertisers_equal(&reassembling_advertiser,
616 								  &evt->addr, evt->sid);
617 
618 		if (is_new_advertiser && is_report_complete) {
619 			/* Only advertising report from this advertiser.
620 			 * Create event immediately.
621 			 */
622 			create_ext_adv_info(evt, &scan_info);
623 			le_adv_recv(&evt->addr, &scan_info, &buf->b, evt->length);
624 			goto cont;
625 		}
626 
627 		if (is_new_advertiser && reassembling_advertiser.state == FRAG_ADV_REASSEMBLING) {
628 			LOG_WRN("Received an incomplete advertising report while reassembling "
629 				"advertising reports from a different advertiser. The advertising "
630 				"report is discarded and future scan results may be incomplete. "
631 				"Interleaving of fragmented advertising reports from different "
632 				"advertisers is not yet supported.");
633 			goto cont;
634 		}
635 
636 		if (data_status == BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_INCOMPLETE) {
637 			/* Got HCI_LE_Extended_Advertising_Report: Incomplete, data truncated, no
638 			 * more to come. This means the Controller is aborting the reassembly. We
639 			 * discard the partially received report, and the application is not
640 			 * notified.
641 			 *
642 			 * See the Controller's documentation for possible reasons for aborting.
643 			 * Hint: CONFIG_BT_CTLR_SCAN_DATA_LEN_MAX.
644 			 */
645 			LOG_DBG("Discarding incomplete advertisement.");
646 			reset_reassembling_advertiser();
647 			goto cont;
648 		}
649 
650 		if (is_new_advertiser) {
651 			/* We are not reassembling reports from an advertiser and
652 			 * this is the first report from the new advertiser.
653 			 * Initialize the new advertiser.
654 			 */
655 			__ASSERT_NO_MSG(reassembling_advertiser.state == FRAG_ADV_INACTIVE);
656 			init_reassembling_advertiser(&evt->addr, evt->sid);
657 		}
658 
659 		if (evt->length + ext_scan_buf.len > ext_scan_buf.size) {
660 			/* The report does not fit in the reassemby buffer
661 			 * Discard this and future reports from the advertiser.
662 			 */
663 			reassembling_advertiser.state = FRAG_ADV_DISCARDING;
664 		}
665 
666 		if (reassembling_advertiser.state == FRAG_ADV_DISCARDING) {
667 			if (!more_to_come) {
668 				/* We do no longer need to keep track of this advertiser as
669 				 * all the expected data is received.
670 				 */
671 				reset_reassembling_advertiser();
672 			}
673 			goto cont;
674 		}
675 
676 		net_buf_simple_add_mem(&ext_scan_buf, buf->data, evt->length);
677 		if (more_to_come) {
678 			/* The controller will send additional reports to be reassembled */
679 			continue;
680 		}
681 
682 		/* No more data coming from the controller.
683 		 * Create event.
684 		 */
685 		__ASSERT_NO_MSG(is_report_complete);
686 		create_ext_adv_info(evt, &scan_info);
687 		le_adv_recv(&evt->addr, &scan_info, &ext_scan_buf, ext_scan_buf.len);
688 
689 		/* We do no longer need to keep track of this advertiser. */
690 		reset_reassembling_advertiser();
691 
692 cont:
693 		net_buf_pull(buf, evt->length);
694 	}
695 }
696 
697 #if defined(CONFIG_BT_PER_ADV_SYNC)
per_adv_sync_delete(struct bt_le_per_adv_sync * per_adv_sync)698 static void per_adv_sync_delete(struct bt_le_per_adv_sync *per_adv_sync)
699 {
700 	atomic_clear(per_adv_sync->flags);
701 }
702 
per_adv_sync_new(void)703 static struct bt_le_per_adv_sync *per_adv_sync_new(void)
704 {
705 	struct bt_le_per_adv_sync *per_adv_sync = NULL;
706 
707 	for (int i = 0; i < ARRAY_SIZE(per_adv_sync_pool); i++) {
708 		if (!atomic_test_bit(per_adv_sync_pool[i].flags,
709 				     BT_PER_ADV_SYNC_CREATED)) {
710 			per_adv_sync = &per_adv_sync_pool[i];
711 			break;
712 		}
713 	}
714 
715 	if (!per_adv_sync) {
716 		return NULL;
717 	}
718 
719 	(void)memset(per_adv_sync, 0, sizeof(*per_adv_sync));
720 	atomic_set_bit(per_adv_sync->flags, BT_PER_ADV_SYNC_CREATED);
721 
722 #if CONFIG_BT_PER_ADV_SYNC_BUF_SIZE > 0
723 	net_buf_simple_init_with_data(&per_adv_sync->reassembly,
724 				      per_adv_sync->reassembly_data,
725 				      CONFIG_BT_PER_ADV_SYNC_BUF_SIZE);
726 	net_buf_simple_reset(&per_adv_sync->reassembly);
727 #endif /* CONFIG_BT_PER_ADV_SYNC_BUF_SIZE > 0 */
728 
729 	return per_adv_sync;
730 }
731 
get_pending_per_adv_sync(void)732 static struct bt_le_per_adv_sync *get_pending_per_adv_sync(void)
733 {
734 	for (int i = 0; i < ARRAY_SIZE(per_adv_sync_pool); i++) {
735 		if (atomic_test_bit(per_adv_sync_pool[i].flags,
736 				    BT_PER_ADV_SYNC_SYNCING)) {
737 			return &per_adv_sync_pool[i];
738 		}
739 	}
740 
741 	return NULL;
742 }
743 
bt_periodic_sync_disable(void)744 void bt_periodic_sync_disable(void)
745 {
746 	for (size_t i = 0; i < ARRAY_SIZE(per_adv_sync_pool); i++) {
747 		per_adv_sync_delete(&per_adv_sync_pool[i]);
748 	}
749 }
750 
bt_hci_get_per_adv_sync(uint16_t handle)751 struct bt_le_per_adv_sync *bt_hci_get_per_adv_sync(uint16_t handle)
752 {
753 	for (int i = 0; i < ARRAY_SIZE(per_adv_sync_pool); i++) {
754 		if (per_adv_sync_pool[i].handle == handle &&
755 		    atomic_test_bit(per_adv_sync_pool[i].flags,
756 				    BT_PER_ADV_SYNC_SYNCED)) {
757 			return &per_adv_sync_pool[i];
758 		}
759 	}
760 
761 	return NULL;
762 }
763 
bt_hci_le_per_adv_report_recv(struct bt_le_per_adv_sync * per_adv_sync,struct net_buf_simple * buf,const struct bt_le_per_adv_sync_recv_info * info)764 void bt_hci_le_per_adv_report_recv(struct bt_le_per_adv_sync *per_adv_sync,
765 				   struct net_buf_simple *buf,
766 				   const struct bt_le_per_adv_sync_recv_info *info)
767 {
768 	struct net_buf_simple_state state;
769 	struct bt_le_per_adv_sync_cb *listener;
770 
771 	SYS_SLIST_FOR_EACH_CONTAINER(&pa_sync_cbs, listener, node) {
772 		if (listener->recv) {
773 			net_buf_simple_save(buf, &state);
774 			listener->recv(per_adv_sync, info, buf);
775 			net_buf_simple_restore(buf, &state);
776 		}
777 	}
778 }
779 
780 #if defined(CONFIG_BT_PER_ADV_SYNC_RSP) && (CONFIG_BT_PER_ADV_SYNC_BUF_SIZE > 0)
bt_hci_le_per_adv_report_recv_failure(struct bt_le_per_adv_sync * per_adv_sync,const struct bt_le_per_adv_sync_recv_info * info)781 static void bt_hci_le_per_adv_report_recv_failure(struct bt_le_per_adv_sync *per_adv_sync,
782 				   const struct bt_le_per_adv_sync_recv_info *info)
783 {
784 	struct bt_le_per_adv_sync_cb *listener;
785 
786 	SYS_SLIST_FOR_EACH_CONTAINER(&pa_sync_cbs, listener, node) {
787 		if (listener->recv) {
788 			listener->recv(per_adv_sync, info, NULL);
789 		}
790 	}
791 }
792 #endif /* defined(CONFIG_BT_PER_ADV_SYNC_RSP) && (CONFIG_BT_PER_ADV_SYNC_BUF_SIZE > 0) */
793 
bt_hci_le_per_adv_report_common(struct net_buf * buf)794 static void bt_hci_le_per_adv_report_common(struct net_buf *buf)
795 {
796 #if defined(CONFIG_BT_PER_ADV_SYNC_RSP)
797 	struct bt_hci_evt_le_per_advertising_report_v2 *evt;
798 #else
799 	struct bt_hci_evt_le_per_advertising_report *evt;
800 #endif /* defined(CONFIG_BT_PER_ADV_SYNC_RSP) */
801 
802 	struct bt_le_per_adv_sync *per_adv_sync;
803 	struct bt_le_per_adv_sync_recv_info info;
804 
805 	if (buf->len < sizeof(*evt)) {
806 		LOG_ERR("Unexpected end of buffer");
807 		return;
808 	}
809 
810 	evt = net_buf_pull_mem(buf, sizeof(*evt));
811 
812 	per_adv_sync = bt_hci_get_per_adv_sync(sys_le16_to_cpu(evt->handle));
813 
814 	if (!per_adv_sync) {
815 		LOG_ERR("Unknown handle 0x%04X for periodic advertising report",
816 			sys_le16_to_cpu(evt->handle));
817 		return;
818 	}
819 
820 	if (atomic_test_bit(per_adv_sync->flags,
821 			    BT_PER_ADV_SYNC_RECV_DISABLED)) {
822 		LOG_ERR("Received PA adv report when receive disabled");
823 		return;
824 	}
825 
826 	info.tx_power = evt->tx_power;
827 	info.rssi = evt->rssi;
828 	info.cte_type = BIT(evt->cte_type);
829 	info.addr = &per_adv_sync->addr;
830 	info.sid = per_adv_sync->sid;
831 
832 #if defined(CONFIG_BT_PER_ADV_SYNC_RSP)
833 	info.periodic_event_counter = sys_le16_to_cpu(evt->periodic_event_counter);
834 	info.subevent = evt->subevent;
835 #endif /* CONFIG_BT_PER_ADV_SYNC_RSP */
836 
837 	if (!per_adv_sync->report_truncated) {
838 #if CONFIG_BT_PER_ADV_SYNC_BUF_SIZE > 0
839 		if (net_buf_simple_tailroom(&per_adv_sync->reassembly) < evt->length) {
840 			/* The buffer is too small for the entire report. Drop it */
841 			LOG_WRN("Buffer is too small to reassemble the report. "
842 				"Use CONFIG_BT_PER_ADV_SYNC_BUF_SIZE to change "
843 				"the buffer size.");
844 
845 			per_adv_sync->report_truncated = true;
846 			net_buf_simple_reset(&per_adv_sync->reassembly);
847 			return;
848 		}
849 
850 		if (evt->data_status == BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_COMPLETE) {
851 			if (per_adv_sync->reassembly.len == 0) {
852 				/* We have not received any partial data before.
853 				 * This buffer can be forwarded without an extra copy.
854 				 */
855 				bt_hci_le_per_adv_report_recv(per_adv_sync, &buf->b, &info);
856 			} else {
857 				net_buf_simple_add_mem(&per_adv_sync->reassembly,
858 						       buf->data, evt->length);
859 				bt_hci_le_per_adv_report_recv(per_adv_sync,
860 							      &per_adv_sync->reassembly, &info);
861 				net_buf_simple_reset(&per_adv_sync->reassembly);
862 			}
863 		} else if (evt->data_status == BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_INCOMPLETE) {
864 			LOG_DBG("Received incomplete advertising data. "
865 				"Advertising report dropped.");
866 
867 			net_buf_simple_reset(&per_adv_sync->reassembly);
868 
869 		} else if (evt->data_status == BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_PARTIAL) {
870 			net_buf_simple_add_mem(&per_adv_sync->reassembly, buf->data, evt->length);
871 #if defined(CONFIG_BT_PER_ADV_SYNC_RSP)
872 		} else if (evt->data_status == BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_RX_FAILED &&
873 			   per_adv_sync->num_subevents) {
874 			bt_hci_le_per_adv_report_recv_failure(per_adv_sync, &info);
875 #endif /* CONFIG_BT_PER_ADV_SYNC_RSP */
876 		} else {
877 			__ASSERT(false, "Invalid data status 0x%02X", evt->data_status);
878 		}
879 #else /* CONFIG_BT_PER_ADV_SYNC_BUF_SIZE > 0 */
880 		if (evt->data_status == BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_COMPLETE) {
881 			bt_hci_le_per_adv_report_recv(per_adv_sync, &buf->b, &info);
882 		} else {
883 			per_adv_sync->report_truncated = true;
884 		}
885 #endif /* CONFIG_BT_PER_ADV_SYNC_BUF_SIZE > 0 */
886 	} else if (evt->data_status == BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_COMPLETE) {
887 		per_adv_sync->report_truncated = false;
888 	}
889 }
890 
bt_hci_le_per_adv_report(struct net_buf * buf)891 void bt_hci_le_per_adv_report(struct net_buf *buf)
892 {
893 	if (IS_ENABLED(CONFIG_BT_PER_ADV_SYNC_RSP)) {
894 		LOG_ERR("The controller shall raise the latest unmasked version of the event");
895 
896 		return;
897 	}
898 
899 	bt_hci_le_per_adv_report_common(buf);
900 }
901 
per_adv_sync_terminate(uint16_t handle)902 static int per_adv_sync_terminate(uint16_t handle)
903 {
904 	struct bt_hci_cp_le_per_adv_terminate_sync *cp;
905 	struct net_buf *buf;
906 
907 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_PER_ADV_TERMINATE_SYNC,
908 				sizeof(*cp));
909 	if (!buf) {
910 		return -ENOBUFS;
911 	}
912 
913 	cp = net_buf_add(buf, sizeof(*cp));
914 	(void)memset(cp, 0, sizeof(*cp));
915 
916 	cp->handle = sys_cpu_to_le16(handle);
917 
918 	return bt_hci_cmd_send_sync(BT_HCI_OP_LE_PER_ADV_TERMINATE_SYNC, buf,
919 				    NULL);
920 }
921 
per_adv_sync_terminated(struct bt_le_per_adv_sync * per_adv_sync,uint8_t reason)922 static void per_adv_sync_terminated(struct bt_le_per_adv_sync *per_adv_sync,
923 				    uint8_t reason)
924 {
925 	/* Terminate the PA sync and notify app */
926 	const struct bt_le_per_adv_sync_term_info term_info = {
927 		.addr = &per_adv_sync->addr,
928 		.sid = per_adv_sync->sid,
929 		.reason = reason,
930 	};
931 	struct bt_le_per_adv_sync_cb *listener;
932 
933 	/* Deleting before callback, so the caller will be able
934 	 * to restart sync in the callback.
935 	 */
936 	per_adv_sync_delete(per_adv_sync);
937 
938 	SYS_SLIST_FOR_EACH_CONTAINER(&pa_sync_cbs, listener, node) {
939 		if (listener->term) {
940 			listener->term(per_adv_sync, &term_info);
941 		}
942 	}
943 }
944 
bt_hci_le_per_adv_sync_established_common(struct net_buf * buf)945 static void bt_hci_le_per_adv_sync_established_common(struct net_buf *buf)
946 {
947 #if defined(CONFIG_BT_PER_ADV_SYNC_RSP)
948 	struct bt_hci_evt_le_per_adv_sync_established_v2 *evt =
949 		(struct bt_hci_evt_le_per_adv_sync_established_v2 *)buf->data;
950 #else
951 	struct bt_hci_evt_le_per_adv_sync_established *evt =
952 		(struct bt_hci_evt_le_per_adv_sync_established *)buf->data;
953 #endif /* defined(CONFIG_BT_PER_ADV_SYNC_RSP) */
954 
955 	struct bt_le_per_adv_sync_synced_info sync_info;
956 	struct bt_le_per_adv_sync *pending_per_adv_sync;
957 	struct bt_le_per_adv_sync_cb *listener;
958 	bt_addr_le_t id_addr;
959 	bool unexpected_evt;
960 	int err;
961 
962 	pending_per_adv_sync = get_pending_per_adv_sync();
963 
964 	if (pending_per_adv_sync) {
965 		atomic_clear_bit(pending_per_adv_sync->flags,
966 				 BT_PER_ADV_SYNC_SYNCING);
967 		err = bt_le_scan_update(false);
968 
969 		if (err) {
970 			LOG_ERR("Could not update scan (%d)", err);
971 		}
972 	}
973 
974 	if (evt->status == BT_HCI_ERR_OP_CANCELLED_BY_HOST) {
975 		/* Cancelled locally, don't call CB */
976 		if (pending_per_adv_sync) {
977 			per_adv_sync_delete(pending_per_adv_sync);
978 		} else {
979 			LOG_ERR("Unexpected per adv sync cancelled event");
980 		}
981 
982 		return;
983 	}
984 
985 	if (bt_addr_le_is_resolved(&evt->adv_addr)) {
986 		bt_addr_le_copy_resolved(&id_addr, &evt->adv_addr);
987 	} else {
988 		bt_addr_le_copy(&id_addr,
989 				bt_lookup_id_addr(BT_ID_DEFAULT,
990 						  &evt->adv_addr));
991 	}
992 
993 	if (!pending_per_adv_sync ||
994 	    (!atomic_test_bit(pending_per_adv_sync->flags,
995 			      BT_PER_ADV_SYNC_SYNCING_USE_LIST) &&
996 	     ((pending_per_adv_sync->sid != evt->sid) ||
997 	      !bt_addr_le_eq(&pending_per_adv_sync->addr, &id_addr)))) {
998 		LOG_ERR("Unexpected per adv sync established event");
999 		/* Request terminate of pending periodic advertising in controller */
1000 		per_adv_sync_terminate(sys_le16_to_cpu(evt->handle));
1001 
1002 		unexpected_evt = true;
1003 	} else {
1004 		unexpected_evt = false;
1005 	}
1006 
1007 	if (unexpected_evt || evt->status != BT_HCI_ERR_SUCCESS) {
1008 		if (pending_per_adv_sync) {
1009 			const uint8_t reason = unexpected_evt ? BT_HCI_ERR_UNSPECIFIED
1010 							      : evt->status;
1011 
1012 			if (atomic_test_bit(pending_per_adv_sync->flags,
1013 					    BT_PER_ADV_SYNC_SYNCING_USE_LIST)) {
1014 				/* Update the addr and sid for the callback
1015 				 * Already set if not using the sync list
1016 				 */
1017 				bt_addr_le_copy(&pending_per_adv_sync->addr,
1018 						&id_addr);
1019 				pending_per_adv_sync->sid = evt->sid;
1020 			}
1021 
1022 			per_adv_sync_terminated(pending_per_adv_sync, reason);
1023 		}
1024 		return;
1025 	}
1026 
1027 	pending_per_adv_sync->report_truncated = false;
1028 
1029 	atomic_set_bit(pending_per_adv_sync->flags, BT_PER_ADV_SYNC_SYNCED);
1030 
1031 	pending_per_adv_sync->handle = sys_le16_to_cpu(evt->handle);
1032 	pending_per_adv_sync->interval = sys_le16_to_cpu(evt->interval);
1033 	pending_per_adv_sync->clock_accuracy =
1034 		sys_le16_to_cpu(evt->clock_accuracy);
1035 	pending_per_adv_sync->phy = bt_get_phy(evt->phy);
1036 
1037 	memset(&sync_info, 0, sizeof(sync_info));
1038 	sync_info.interval = pending_per_adv_sync->interval;
1039 	sync_info.phy = pending_per_adv_sync->phy;
1040 
1041 	if (atomic_test_bit(pending_per_adv_sync->flags,
1042 			    BT_PER_ADV_SYNC_SYNCING_USE_LIST)) {
1043 		/* Now we know which address and SID we synchronized to. */
1044 		pending_per_adv_sync->sid = evt->sid;
1045 
1046 		if (bt_addr_le_is_resolved(&pending_per_adv_sync->addr)) {
1047 			bt_addr_le_copy_resolved(&pending_per_adv_sync->addr,
1048 						 &id_addr);
1049 		} else {
1050 			bt_addr_le_copy(&pending_per_adv_sync->addr, &id_addr);
1051 		}
1052 	}
1053 
1054 	sync_info.addr = &pending_per_adv_sync->addr;
1055 	sync_info.sid = pending_per_adv_sync->sid;
1056 #if defined(CONFIG_BT_PER_ADV_SYNC_RSP)
1057 	sync_info.num_subevents = evt->num_subevents;
1058 	sync_info.subevent_interval = evt->subevent_interval;
1059 	sync_info.response_slot_delay = evt->response_slot_delay;
1060 	sync_info.response_slot_spacing = evt->response_slot_spacing;
1061 
1062 	pending_per_adv_sync->num_subevents = evt->num_subevents;
1063 	pending_per_adv_sync->subevent_interval = evt->subevent_interval;
1064 	pending_per_adv_sync->response_slot_delay = evt->response_slot_delay;
1065 	pending_per_adv_sync->response_slot_spacing = evt->response_slot_spacing;
1066 #endif /* CONFIG_BT_PER_ADV_SYNC_RSP */
1067 
1068 	sync_info.recv_enabled =
1069 		!atomic_test_bit(pending_per_adv_sync->flags,
1070 				 BT_PER_ADV_SYNC_RECV_DISABLED);
1071 
1072 	SYS_SLIST_FOR_EACH_CONTAINER(&pa_sync_cbs, listener, node) {
1073 		if (listener->synced) {
1074 			listener->synced(pending_per_adv_sync, &sync_info);
1075 		}
1076 	}
1077 }
1078 
bt_hci_le_per_adv_sync_established(struct net_buf * buf)1079 void bt_hci_le_per_adv_sync_established(struct net_buf *buf)
1080 {
1081 	if (IS_ENABLED(CONFIG_BT_PER_ADV_SYNC_RSP)) {
1082 		LOG_ERR("The controller shall raise the latest unmasked version of the event");
1083 
1084 		return;
1085 	}
1086 
1087 	bt_hci_le_per_adv_sync_established_common(buf);
1088 }
1089 
1090 #if defined(CONFIG_BT_PER_ADV_SYNC_RSP)
bt_le_per_adv_sync_subevent(struct bt_le_per_adv_sync * per_adv_sync,struct bt_le_per_adv_sync_subevent_params * params)1091 int bt_le_per_adv_sync_subevent(struct bt_le_per_adv_sync *per_adv_sync,
1092 				struct bt_le_per_adv_sync_subevent_params *params)
1093 {
1094 	struct bt_hci_cp_le_set_pawr_sync_subevent *cp;
1095 	struct net_buf *buf;
1096 
1097 	if (params->num_subevents > BT_HCI_PAWR_SUBEVENT_MAX) {
1098 		return -EINVAL;
1099 	}
1100 
1101 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_PER_ADV_SYNC_SUBEVENT,
1102 				sizeof(*cp) + params->num_subevents);
1103 
1104 	if (!buf) {
1105 		return -ENOBUFS;
1106 	}
1107 
1108 	cp = net_buf_add(buf, sizeof(*cp));
1109 	(void)memset(cp, 0, sizeof(*cp));
1110 	cp->sync_handle = sys_cpu_to_le16(per_adv_sync->handle);
1111 	cp->periodic_adv_properties = sys_cpu_to_le16(params->properties);
1112 	cp->num_subevents = params->num_subevents;
1113 	net_buf_add_mem(buf, params->subevents, cp->num_subevents);
1114 
1115 	return bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_PER_ADV_SYNC_SUBEVENT, buf, NULL);
1116 }
1117 
bt_le_per_adv_set_response_data(struct bt_le_per_adv_sync * per_adv_sync,const struct bt_le_per_adv_response_params * param,const struct net_buf_simple * data)1118 int bt_le_per_adv_set_response_data(struct bt_le_per_adv_sync *per_adv_sync,
1119 				    const struct bt_le_per_adv_response_params *param,
1120 				    const struct net_buf_simple *data)
1121 {
1122 	struct bt_hci_cp_le_set_pawr_response_data *cp;
1123 	struct net_buf *buf;
1124 
1125 	if (per_adv_sync->num_subevents == 0) {
1126 		return -EINVAL;
1127 	}
1128 
1129 	if (param->request_subevent >= per_adv_sync->num_subevents) {
1130 		return -EINVAL;
1131 	}
1132 
1133 	if (param->response_subevent >= per_adv_sync->num_subevents) {
1134 		return -EINVAL;
1135 	}
1136 
1137 	if (data->len > 247) {
1138 		return -EINVAL;
1139 	}
1140 
1141 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_PER_ADV_RESPONSE_DATA,
1142 				sizeof(*cp) + data->len);
1143 
1144 	if (!buf) {
1145 		return -ENOBUFS;
1146 	}
1147 
1148 	cp = net_buf_add(buf, sizeof(*cp));
1149 	(void)memset(cp, 0, sizeof(*cp));
1150 	cp->sync_handle = sys_cpu_to_le16(per_adv_sync->handle);
1151 	cp->request_event = sys_cpu_to_le16(param->request_event);
1152 	cp->request_subevent = param->request_subevent;
1153 	cp->response_subevent = param->response_subevent;
1154 	cp->response_slot = param->response_slot;
1155 	cp->response_data_length = data->len;
1156 
1157 	net_buf_add_mem(buf, data->data, cp->response_data_length);
1158 
1159 	return bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_PER_ADV_RESPONSE_DATA, buf, NULL);
1160 }
1161 #endif /* CONFIG_BT_PER_ADV_SYNC_RSP */
1162 
bt_hci_le_per_adv_sync_lost(struct net_buf * buf)1163 void bt_hci_le_per_adv_sync_lost(struct net_buf *buf)
1164 {
1165 	struct bt_hci_evt_le_per_adv_sync_lost *evt =
1166 		(struct bt_hci_evt_le_per_adv_sync_lost *)buf->data;
1167 	struct bt_le_per_adv_sync *per_adv_sync;
1168 
1169 	per_adv_sync = bt_hci_get_per_adv_sync(sys_le16_to_cpu(evt->handle));
1170 
1171 	if (!per_adv_sync) {
1172 		LOG_ERR("Unknown handle 0x%04Xfor periodic adv sync lost",
1173 			sys_le16_to_cpu(evt->handle));
1174 		return;
1175 	}
1176 
1177 	/* There is no status in the per. adv. sync lost event */
1178 	per_adv_sync_terminated(per_adv_sync, BT_HCI_ERR_UNSPECIFIED);
1179 }
1180 
1181 #if defined(CONFIG_BT_CONN)
bt_hci_le_past_received_common(struct net_buf * buf)1182 static void bt_hci_le_past_received_common(struct net_buf *buf)
1183 {
1184 #if defined(CONFIG_BT_PER_ADV_SYNC_RSP)
1185 	struct bt_hci_evt_le_past_received_v2 *evt =
1186 		(struct bt_hci_evt_le_past_received_v2 *)buf->data;
1187 #else
1188 	struct bt_hci_evt_le_past_received *evt =
1189 		(struct bt_hci_evt_le_past_received *)buf->data;
1190 #endif /* defined(CONFIG_BT_PER_ADV_SYNC_RSP) */
1191 
1192 	struct bt_le_per_adv_sync_synced_info sync_info;
1193 	struct bt_le_per_adv_sync_cb *listener;
1194 	struct bt_le_per_adv_sync *per_adv_sync;
1195 	bt_addr_le_t id_addr;
1196 
1197 	if (evt->status) {
1198 		/* No sync created, don't notify app */
1199 		LOG_DBG("PAST receive failed with status 0x%02X", evt->status);
1200 		return;
1201 	}
1202 
1203 	sync_info.conn = bt_conn_lookup_handle(
1204 				sys_le16_to_cpu(evt->conn_handle),
1205 				BT_CONN_TYPE_LE);
1206 
1207 	if (!sync_info.conn) {
1208 		LOG_ERR("Could not lookup connection handle from PAST");
1209 		per_adv_sync_terminate(sys_le16_to_cpu(evt->sync_handle));
1210 		return;
1211 	}
1212 
1213 	per_adv_sync = per_adv_sync_new();
1214 	if (!per_adv_sync) {
1215 		LOG_WRN("Could not allocate new PA sync from PAST");
1216 		per_adv_sync_terminate(sys_le16_to_cpu(evt->sync_handle));
1217 		bt_conn_unref(sync_info.conn);
1218 		return;
1219 	}
1220 
1221 	atomic_set_bit(per_adv_sync->flags, BT_PER_ADV_SYNC_SYNCED);
1222 
1223 	if (bt_addr_le_is_resolved(&evt->addr)) {
1224 		bt_addr_le_copy_resolved(&id_addr, &evt->addr);
1225 	} else {
1226 		bt_addr_le_copy(&id_addr,
1227 				bt_lookup_id_addr(BT_ID_DEFAULT, &evt->addr));
1228 	}
1229 
1230 	per_adv_sync->handle = sys_le16_to_cpu(evt->sync_handle);
1231 	per_adv_sync->interval = sys_le16_to_cpu(evt->interval);
1232 	per_adv_sync->clock_accuracy = sys_le16_to_cpu(evt->clock_accuracy);
1233 	per_adv_sync->phy = bt_get_phy(evt->phy);
1234 	bt_addr_le_copy(&per_adv_sync->addr, &id_addr);
1235 	per_adv_sync->sid = evt->adv_sid;
1236 
1237 #if defined(CONFIG_BT_PER_ADV_SYNC_RSP)
1238 	per_adv_sync->num_subevents = evt->num_subevents;
1239 	per_adv_sync->subevent_interval = evt->subevent_interval;
1240 	per_adv_sync->response_slot_delay = evt->response_slot_delay;
1241 	per_adv_sync->response_slot_spacing = evt->response_slot_spacing;
1242 #endif /* defined(CONFIG_BT_PER_ADV_SYNC_RSP) */
1243 
1244 	sync_info.interval = per_adv_sync->interval;
1245 	sync_info.phy = per_adv_sync->phy;
1246 	sync_info.addr = &per_adv_sync->addr;
1247 	sync_info.sid = per_adv_sync->sid;
1248 	sync_info.service_data = sys_le16_to_cpu(evt->service_data);
1249 
1250 #if defined(CONFIG_BT_PER_ADV_SYNC_RSP)
1251 	sync_info.num_subevents =  per_adv_sync->num_subevents;
1252 	sync_info.subevent_interval =  per_adv_sync->subevent_interval;
1253 	sync_info.response_slot_delay =  per_adv_sync->response_slot_delay;
1254 	sync_info.response_slot_spacing =  per_adv_sync->response_slot_spacing;
1255 #endif /* defined(CONFIG_BT_PER_ADV_SYNC_RSP) */
1256 
1257 	SYS_SLIST_FOR_EACH_CONTAINER(&pa_sync_cbs, listener, node) {
1258 		if (listener->synced) {
1259 			listener->synced(per_adv_sync, &sync_info);
1260 		}
1261 	}
1262 
1263 	bt_conn_unref(sync_info.conn);
1264 }
1265 
bt_hci_le_past_received(struct net_buf * buf)1266 void bt_hci_le_past_received(struct net_buf *buf)
1267 {
1268 	if (IS_ENABLED(CONFIG_BT_PER_ADV_SYNC_RSP)) {
1269 		LOG_ERR("The controller shall raise the latest unmasked version of the event");
1270 
1271 		return;
1272 	}
1273 
1274 	bt_hci_le_past_received_common(buf);
1275 }
1276 
1277 #if defined(CONFIG_BT_PER_ADV_SYNC_RSP)
bt_hci_le_past_received_v2(struct net_buf * buf)1278 void bt_hci_le_past_received_v2(struct net_buf *buf)
1279 {
1280 	bt_hci_le_past_received_common(buf);
1281 }
1282 #endif /* CONFIG_BT_PER_ADV_SYNC_RSP */
1283 #endif /* CONFIG_BT_CONN */
1284 
1285 #if defined(CONFIG_BT_PER_ADV_SYNC_RSP)
bt_hci_le_per_adv_sync_established_v2(struct net_buf * buf)1286 void bt_hci_le_per_adv_sync_established_v2(struct net_buf *buf)
1287 {
1288 	bt_hci_le_per_adv_sync_established_common(buf);
1289 }
1290 
bt_hci_le_per_adv_report_v2(struct net_buf * buf)1291 void bt_hci_le_per_adv_report_v2(struct net_buf *buf)
1292 {
1293 	bt_hci_le_per_adv_report_common(buf);
1294 }
1295 #endif /* CONFIG_BT_PER_ADV_SYNC_RSP */
1296 
1297 #if defined(CONFIG_BT_ISO_BROADCAST)
bt_hci_le_biginfo_adv_report(struct net_buf * buf)1298 void bt_hci_le_biginfo_adv_report(struct net_buf *buf)
1299 {
1300 	struct bt_hci_evt_le_biginfo_adv_report *evt;
1301 	struct bt_le_per_adv_sync *per_adv_sync;
1302 	struct bt_le_per_adv_sync_cb *listener;
1303 	struct bt_iso_biginfo biginfo;
1304 
1305 	evt = net_buf_pull_mem(buf, sizeof(*evt));
1306 
1307 	per_adv_sync = bt_hci_get_per_adv_sync(sys_le16_to_cpu(evt->sync_handle));
1308 
1309 	if (!per_adv_sync) {
1310 		LOG_ERR("Unknown handle 0x%04X for periodic advertising report",
1311 			sys_le16_to_cpu(evt->sync_handle));
1312 		return;
1313 	}
1314 
1315 	biginfo.addr = &per_adv_sync->addr;
1316 	biginfo.sid = per_adv_sync->sid;
1317 	biginfo.num_bis = evt->num_bis;
1318 	biginfo.sub_evt_count = evt->nse;
1319 	biginfo.iso_interval = sys_le16_to_cpu(evt->iso_interval);
1320 	biginfo.burst_number = evt->bn;
1321 	biginfo.offset = evt->pto;
1322 	biginfo.rep_count = evt->irc;
1323 	biginfo.max_pdu = sys_le16_to_cpu(evt->max_pdu);
1324 	biginfo.sdu_interval = sys_get_le24(evt->sdu_interval);
1325 	biginfo.max_sdu = sys_le16_to_cpu(evt->max_sdu);
1326 	biginfo.phy = bt_get_phy(evt->phy);
1327 	biginfo.framing = evt->framing;
1328 	biginfo.encryption = evt->encryption ? true : false;
1329 
1330 	SYS_SLIST_FOR_EACH_CONTAINER(&pa_sync_cbs, listener, node) {
1331 		if (listener->biginfo) {
1332 			listener->biginfo(per_adv_sync, &biginfo);
1333 		}
1334 	}
1335 }
1336 #endif /* CONFIG_BT_ISO_BROADCAST */
1337 #if defined(CONFIG_BT_DF_CONNECTIONLESS_CTE_RX)
bt_hci_le_df_connectionless_iq_report_common(uint8_t event,struct net_buf * buf)1338 static void bt_hci_le_df_connectionless_iq_report_common(uint8_t event, struct net_buf *buf)
1339 {
1340 	int err;
1341 
1342 	struct bt_df_per_adv_sync_iq_samples_report cte_report;
1343 	struct bt_le_per_adv_sync *per_adv_sync;
1344 	struct bt_le_per_adv_sync_cb *listener;
1345 
1346 	if (event == BT_HCI_EVT_LE_CONNECTIONLESS_IQ_REPORT) {
1347 		err = hci_df_prepare_connectionless_iq_report(buf, &cte_report, &per_adv_sync);
1348 		if (err) {
1349 			LOG_ERR("Prepare CTE conn IQ report failed %d", err);
1350 			return;
1351 		}
1352 	} else if (IS_ENABLED(CONFIG_BT_DF_VS_CL_IQ_REPORT_16_BITS_IQ_SAMPLES) &&
1353 		   event == BT_HCI_EVT_VS_LE_CONNECTIONLESS_IQ_REPORT) {
1354 		err = hci_df_vs_prepare_connectionless_iq_report(buf, &cte_report, &per_adv_sync);
1355 		if (err) {
1356 			LOG_ERR("Prepare CTE conn IQ report failed %d", err);
1357 			return;
1358 		}
1359 	} else {
1360 		LOG_ERR("Unhandled VS connectionless IQ report");
1361 		return;
1362 	}
1363 
1364 	SYS_SLIST_FOR_EACH_CONTAINER(&pa_sync_cbs, listener, node) {
1365 		if (listener->cte_report_cb) {
1366 			listener->cte_report_cb(per_adv_sync, &cte_report);
1367 		}
1368 	}
1369 }
1370 
bt_hci_le_df_connectionless_iq_report(struct net_buf * buf)1371 void bt_hci_le_df_connectionless_iq_report(struct net_buf *buf)
1372 {
1373 	bt_hci_le_df_connectionless_iq_report_common(BT_HCI_EVT_LE_CONNECTIONLESS_IQ_REPORT, buf);
1374 }
1375 
1376 #if defined(CONFIG_BT_DF_VS_CL_IQ_REPORT_16_BITS_IQ_SAMPLES)
bt_hci_le_vs_df_connectionless_iq_report(struct net_buf * buf)1377 void bt_hci_le_vs_df_connectionless_iq_report(struct net_buf *buf)
1378 {
1379 	bt_hci_le_df_connectionless_iq_report_common(BT_HCI_EVT_VS_LE_CONNECTIONLESS_IQ_REPORT,
1380 						     buf);
1381 }
1382 #endif /* CONFIG_BT_DF_VS_CL_IQ_REPORT_16_BITS_IQ_SAMPLES */
1383 #endif /* CONFIG_BT_DF_CONNECTIONLESS_CTE_RX */
1384 #endif /* defined(CONFIG_BT_PER_ADV_SYNC) */
1385 #endif /* defined(CONFIG_BT_EXT_ADV) */
1386 
bt_hci_le_adv_report(struct net_buf * buf)1387 void bt_hci_le_adv_report(struct net_buf *buf)
1388 {
1389 	uint8_t num_reports = net_buf_pull_u8(buf);
1390 	struct bt_hci_evt_le_advertising_info *evt;
1391 
1392 	LOG_DBG("Adv number of reports %u",  num_reports);
1393 
1394 	while (num_reports--) {
1395 		struct bt_le_scan_recv_info adv_info;
1396 
1397 		if (buf->len < sizeof(*evt)) {
1398 			LOG_ERR("Unexpected end of buffer");
1399 			break;
1400 		}
1401 
1402 		evt = net_buf_pull_mem(buf, sizeof(*evt));
1403 
1404 		if (buf->len < evt->length + sizeof(adv_info.rssi)) {
1405 			LOG_ERR("Unexpected end of buffer");
1406 			break;
1407 		}
1408 
1409 		adv_info.primary_phy = BT_GAP_LE_PHY_1M;
1410 		adv_info.secondary_phy = 0;
1411 		adv_info.tx_power = BT_GAP_TX_POWER_INVALID;
1412 		adv_info.rssi = evt->data[evt->length];
1413 		adv_info.sid = BT_GAP_SID_INVALID;
1414 		adv_info.interval = 0U;
1415 
1416 		adv_info.adv_type = evt->evt_type;
1417 		adv_info.adv_props = get_adv_props_legacy(evt->evt_type);
1418 
1419 		le_adv_recv(&evt->addr, &adv_info, &buf->b, evt->length);
1420 
1421 		net_buf_pull(buf, evt->length + sizeof(adv_info.rssi));
1422 	}
1423 }
1424 
valid_le_scan_param(const struct bt_le_scan_param * param)1425 static bool valid_le_scan_param(const struct bt_le_scan_param *param)
1426 {
1427 	if (param->type != BT_HCI_LE_SCAN_PASSIVE &&
1428 	    param->type != BT_HCI_LE_SCAN_ACTIVE) {
1429 		return false;
1430 	}
1431 
1432 	if (param->options & ~(BT_LE_SCAN_OPT_FILTER_DUPLICATE |
1433 			       BT_LE_SCAN_OPT_FILTER_ACCEPT_LIST |
1434 			       BT_LE_SCAN_OPT_CODED |
1435 			       BT_LE_SCAN_OPT_NO_1M)) {
1436 		return false;
1437 	}
1438 
1439 	if (param->interval < 0x0004 || param->interval > 0x4000) {
1440 		return false;
1441 	}
1442 
1443 	if (param->window < 0x0004 || param->window > 0x4000) {
1444 		return false;
1445 	}
1446 
1447 	if (param->window > param->interval) {
1448 		return false;
1449 	}
1450 
1451 	return true;
1452 }
1453 
bt_le_scan_start(const struct bt_le_scan_param * param,bt_le_scan_cb_t cb)1454 int bt_le_scan_start(const struct bt_le_scan_param *param, bt_le_scan_cb_t cb)
1455 {
1456 	int err;
1457 
1458 	if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
1459 		return -EAGAIN;
1460 	}
1461 
1462 	/* Check that the parameters have valid values */
1463 	if (!valid_le_scan_param(param)) {
1464 		return -EINVAL;
1465 	}
1466 
1467 	if (param->type && !bt_id_scan_random_addr_check()) {
1468 		return -EINVAL;
1469 	}
1470 
1471 	/* Return if active scan is already enabled */
1472 	if (atomic_test_and_set_bit(bt_dev.flags, BT_DEV_EXPLICIT_SCAN)) {
1473 		return -EALREADY;
1474 	}
1475 
1476 	if (atomic_test_bit(bt_dev.flags, BT_DEV_SCANNING)) {
1477 		err = bt_le_scan_set_enable(BT_HCI_LE_SCAN_DISABLE);
1478 		if (err) {
1479 			atomic_clear_bit(bt_dev.flags, BT_DEV_EXPLICIT_SCAN);
1480 			return err;
1481 		}
1482 	}
1483 
1484 	atomic_set_bit_to(bt_dev.flags, BT_DEV_SCAN_FILTER_DUP,
1485 			  param->options & BT_LE_SCAN_OPT_FILTER_DUPLICATE);
1486 
1487 #if defined(CONFIG_BT_FILTER_ACCEPT_LIST)
1488 	atomic_set_bit_to(bt_dev.flags, BT_DEV_SCAN_FILTERED,
1489 			  param->options & BT_LE_SCAN_OPT_FILTER_ACCEPT_LIST);
1490 #endif /* defined(CONFIG_BT_FILTER_ACCEPT_LIST) */
1491 
1492 	if (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
1493 	    BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
1494 		struct bt_hci_ext_scan_phy param_1m;
1495 		struct bt_hci_ext_scan_phy param_coded;
1496 
1497 		struct bt_hci_ext_scan_phy *phy_1m = NULL;
1498 		struct bt_hci_ext_scan_phy *phy_coded = NULL;
1499 
1500 		if (!(param->options & BT_LE_SCAN_OPT_NO_1M)) {
1501 			param_1m.type = param->type;
1502 			param_1m.interval = sys_cpu_to_le16(param->interval);
1503 			param_1m.window = sys_cpu_to_le16(param->window);
1504 
1505 			phy_1m = &param_1m;
1506 		}
1507 
1508 		if (param->options & BT_LE_SCAN_OPT_CODED) {
1509 			uint16_t interval = param->interval_coded ?
1510 				param->interval_coded :
1511 				param->interval;
1512 			uint16_t window = param->window_coded ?
1513 				param->window_coded :
1514 				param->window;
1515 
1516 			param_coded.type = param->type;
1517 			param_coded.interval = sys_cpu_to_le16(interval);
1518 			param_coded.window = sys_cpu_to_le16(window);
1519 			phy_coded = &param_coded;
1520 		}
1521 
1522 		err = start_le_scan_ext(phy_1m, phy_coded, param->timeout);
1523 	} else {
1524 		if (param->timeout) {
1525 			atomic_clear_bit(bt_dev.flags, BT_DEV_EXPLICIT_SCAN);
1526 			return -ENOTSUP;
1527 		}
1528 
1529 		err = start_le_scan_legacy(param->type, param->interval,
1530 					   param->window);
1531 	}
1532 
1533 	if (err) {
1534 		atomic_clear_bit(bt_dev.flags, BT_DEV_EXPLICIT_SCAN);
1535 		return err;
1536 	}
1537 
1538 	scan_dev_found_cb = cb;
1539 
1540 	return 0;
1541 }
1542 
bt_le_scan_stop(void)1543 int bt_le_scan_stop(void)
1544 {
1545 	/* Return if active scanning is already disabled */
1546 	if (!atomic_test_and_clear_bit(bt_dev.flags, BT_DEV_EXPLICIT_SCAN)) {
1547 		return -EALREADY;
1548 	}
1549 
1550 	bt_scan_reset();
1551 
1552 	if (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
1553 	    atomic_test_and_clear_bit(bt_dev.flags, BT_DEV_SCAN_LIMITED)) {
1554 		atomic_clear_bit(bt_dev.flags, BT_DEV_RPA_VALID);
1555 
1556 #if defined(CONFIG_BT_SMP)
1557 		bt_id_pending_keys_update();
1558 #endif
1559 	}
1560 
1561 	return bt_le_scan_update(false);
1562 }
1563 
bt_le_scan_cb_register(struct bt_le_scan_cb * cb)1564 void bt_le_scan_cb_register(struct bt_le_scan_cb *cb)
1565 {
1566 	sys_slist_append(&scan_cbs, &cb->node);
1567 }
1568 
bt_le_scan_cb_unregister(struct bt_le_scan_cb * cb)1569 void bt_le_scan_cb_unregister(struct bt_le_scan_cb *cb)
1570 {
1571 	sys_slist_find_and_remove(&scan_cbs, &cb->node);
1572 }
1573 
1574 #if defined(CONFIG_BT_PER_ADV_SYNC)
bt_le_per_adv_sync_get_index(struct bt_le_per_adv_sync * per_adv_sync)1575 uint8_t bt_le_per_adv_sync_get_index(struct bt_le_per_adv_sync *per_adv_sync)
1576 {
1577 	ptrdiff_t index = per_adv_sync - per_adv_sync_pool;
1578 
1579 	__ASSERT(index >= 0 && ARRAY_SIZE(per_adv_sync_pool) > index,
1580 		 "Invalid per_adv_sync pointer");
1581 	return (uint8_t)index;
1582 }
1583 
bt_le_per_adv_sync_lookup_index(uint8_t index)1584 struct bt_le_per_adv_sync *bt_le_per_adv_sync_lookup_index(uint8_t index)
1585 {
1586 	if (index >= ARRAY_SIZE(per_adv_sync_pool)) {
1587 		return NULL;
1588 	}
1589 
1590 	return &per_adv_sync_pool[index];
1591 }
1592 
bt_le_per_adv_sync_get_info(struct bt_le_per_adv_sync * per_adv_sync,struct bt_le_per_adv_sync_info * info)1593 int bt_le_per_adv_sync_get_info(struct bt_le_per_adv_sync *per_adv_sync,
1594 				struct bt_le_per_adv_sync_info *info)
1595 {
1596 	CHECKIF(per_adv_sync == NULL || info == NULL) {
1597 		return -EINVAL;
1598 	}
1599 
1600 	bt_addr_le_copy(&info->addr, &per_adv_sync->addr);
1601 	info->sid = per_adv_sync->sid;
1602 	info->phy = per_adv_sync->phy;
1603 	info->interval = per_adv_sync->interval;
1604 
1605 	return 0;
1606 }
1607 
bt_le_per_adv_sync_lookup_addr(const bt_addr_le_t * adv_addr,uint8_t sid)1608 struct bt_le_per_adv_sync *bt_le_per_adv_sync_lookup_addr(const bt_addr_le_t *adv_addr,
1609 							  uint8_t sid)
1610 {
1611 	for (int i = 0; i < ARRAY_SIZE(per_adv_sync_pool); i++) {
1612 		struct bt_le_per_adv_sync *sync = &per_adv_sync_pool[i];
1613 
1614 		if (!atomic_test_bit(per_adv_sync_pool[i].flags,
1615 				     BT_PER_ADV_SYNC_CREATED)) {
1616 			continue;
1617 		}
1618 
1619 		if (bt_addr_le_eq(&sync->addr, adv_addr) && sync->sid == sid) {
1620 			return sync;
1621 		}
1622 	}
1623 
1624 	return NULL;
1625 }
1626 
bt_le_per_adv_sync_create(const struct bt_le_per_adv_sync_param * param,struct bt_le_per_adv_sync ** out_sync)1627 int bt_le_per_adv_sync_create(const struct bt_le_per_adv_sync_param *param,
1628 			      struct bt_le_per_adv_sync **out_sync)
1629 {
1630 	struct bt_hci_cp_le_per_adv_create_sync *cp;
1631 	struct net_buf *buf;
1632 	struct bt_le_per_adv_sync *per_adv_sync;
1633 	int err;
1634 
1635 	if (!BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
1636 		return -ENOTSUP;
1637 	}
1638 
1639 	if (get_pending_per_adv_sync()) {
1640 		return -EBUSY;
1641 	}
1642 
1643 	if (param->sid > BT_GAP_SID_MAX ||
1644 		   param->skip > BT_GAP_PER_ADV_MAX_SKIP ||
1645 		   param->timeout > BT_GAP_PER_ADV_MAX_TIMEOUT ||
1646 		   param->timeout < BT_GAP_PER_ADV_MIN_TIMEOUT) {
1647 		return -EINVAL;
1648 	}
1649 
1650 	per_adv_sync = per_adv_sync_new();
1651 	if (!per_adv_sync) {
1652 		return -ENOMEM;
1653 	}
1654 
1655 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_PER_ADV_CREATE_SYNC, sizeof(*cp));
1656 	if (!buf) {
1657 		per_adv_sync_delete(per_adv_sync);
1658 		return -ENOBUFS;
1659 	}
1660 
1661 	cp = net_buf_add(buf, sizeof(*cp));
1662 	(void)memset(cp, 0, sizeof(*cp));
1663 
1664 	if (param->options & BT_LE_PER_ADV_SYNC_OPT_USE_PER_ADV_LIST) {
1665 		atomic_set_bit(per_adv_sync->flags,
1666 			       BT_PER_ADV_SYNC_SYNCING_USE_LIST);
1667 
1668 		cp->options |= BT_HCI_LE_PER_ADV_CREATE_SYNC_FP_USE_LIST;
1669 	} else {
1670 		/* If BT_LE_PER_ADV_SYNC_OPT_USE_PER_ADV_LIST is set, then the
1671 		 * address and SID are ignored by the controller, so we only
1672 		 * copy/assign them in case that the periodic advertising list
1673 		 * is not used.
1674 		 */
1675 		bt_addr_le_copy(&cp->addr, &param->addr);
1676 		cp->sid = param->sid;
1677 	}
1678 
1679 	if (param->options &
1680 	    BT_LE_PER_ADV_SYNC_OPT_REPORTING_INITIALLY_DISABLED) {
1681 		cp->options |=
1682 			BT_HCI_LE_PER_ADV_CREATE_SYNC_FP_REPORTS_DISABLED;
1683 
1684 		atomic_set_bit(per_adv_sync->flags,
1685 			       BT_PER_ADV_SYNC_RECV_DISABLED);
1686 	}
1687 
1688 	if (param->options & BT_LE_PER_ADV_SYNC_OPT_FILTER_DUPLICATE) {
1689 		cp->options |=
1690 			BT_HCI_LE_PER_ADV_CREATE_SYNC_FP_FILTER_DUPLICATE;
1691 	}
1692 
1693 	if (param->options & BT_LE_PER_ADV_SYNC_OPT_DONT_SYNC_AOA) {
1694 		cp->cte_type |= BT_HCI_LE_PER_ADV_CREATE_SYNC_CTE_TYPE_NO_AOA;
1695 	}
1696 
1697 	if (param->options & BT_LE_PER_ADV_SYNC_OPT_DONT_SYNC_AOD_1US) {
1698 		cp->cte_type |=
1699 			BT_HCI_LE_PER_ADV_CREATE_SYNC_CTE_TYPE_NO_AOD_1US;
1700 	}
1701 
1702 	if (param->options & BT_LE_PER_ADV_SYNC_OPT_DONT_SYNC_AOD_2US) {
1703 		cp->cte_type |=
1704 			BT_HCI_LE_PER_ADV_CREATE_SYNC_CTE_TYPE_NO_AOD_2US;
1705 	}
1706 
1707 	if (param->options & BT_LE_PER_ADV_SYNC_OPT_SYNC_ONLY_CONST_TONE_EXT) {
1708 		cp->cte_type |= BT_HCI_LE_PER_ADV_CREATE_SYNC_CTE_TYPE_ONLY_CTE;
1709 	}
1710 
1711 	cp->skip = sys_cpu_to_le16(param->skip);
1712 	cp->sync_timeout = sys_cpu_to_le16(param->timeout);
1713 
1714 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_PER_ADV_CREATE_SYNC, buf, NULL);
1715 	if (err) {
1716 		per_adv_sync_delete(per_adv_sync);
1717 		return err;
1718 	}
1719 
1720 	atomic_set_bit(per_adv_sync->flags, BT_PER_ADV_SYNC_SYNCING);
1721 
1722 	/* Syncing requires that scan is enabled. If the caller doesn't enable
1723 	 * scan first, we enable it here, and disable it once the sync has been
1724 	 * established. We don't need to use any callbacks since we rely on
1725 	 * the advertiser address in the sync params.
1726 	 */
1727 	if (!atomic_test_bit(bt_dev.flags, BT_DEV_SCANNING)) {
1728 		err = bt_le_scan_update(true);
1729 
1730 		if (err) {
1731 			bt_le_per_adv_sync_delete(per_adv_sync);
1732 			return err;
1733 		}
1734 	}
1735 
1736 	*out_sync = per_adv_sync;
1737 	bt_addr_le_copy(&per_adv_sync->addr, &param->addr);
1738 	per_adv_sync->sid = param->sid;
1739 
1740 	return 0;
1741 }
1742 
bt_le_per_adv_sync_create_cancel(struct bt_le_per_adv_sync * per_adv_sync)1743 static int bt_le_per_adv_sync_create_cancel(
1744 	struct bt_le_per_adv_sync *per_adv_sync)
1745 {
1746 	struct net_buf *buf;
1747 	int err;
1748 
1749 	if (get_pending_per_adv_sync() != per_adv_sync) {
1750 		return -EINVAL;
1751 	}
1752 
1753 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_PER_ADV_CREATE_SYNC_CANCEL, 0);
1754 	if (!buf) {
1755 		return -ENOBUFS;
1756 	}
1757 
1758 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_PER_ADV_CREATE_SYNC_CANCEL, buf,
1759 				   NULL);
1760 	if (err) {
1761 		return err;
1762 	}
1763 
1764 	return 0;
1765 }
1766 
bt_le_per_adv_sync_terminate(struct bt_le_per_adv_sync * per_adv_sync)1767 static int bt_le_per_adv_sync_terminate(struct bt_le_per_adv_sync *per_adv_sync)
1768 {
1769 	int err;
1770 
1771 	if (!atomic_test_bit(per_adv_sync->flags, BT_PER_ADV_SYNC_SYNCED)) {
1772 		return -EINVAL;
1773 	}
1774 
1775 	err = per_adv_sync_terminate(per_adv_sync->handle);
1776 
1777 	if (err) {
1778 		return err;
1779 	}
1780 
1781 	return 0;
1782 }
1783 
bt_le_per_adv_sync_delete(struct bt_le_per_adv_sync * per_adv_sync)1784 int bt_le_per_adv_sync_delete(struct bt_le_per_adv_sync *per_adv_sync)
1785 {
1786 	int err = 0;
1787 
1788 	if (!BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
1789 		return -ENOTSUP;
1790 	}
1791 
1792 	if (atomic_test_bit(per_adv_sync->flags, BT_PER_ADV_SYNC_SYNCED)) {
1793 		err = bt_le_per_adv_sync_terminate(per_adv_sync);
1794 
1795 		if (!err) {
1796 			per_adv_sync_terminated(per_adv_sync,
1797 						BT_HCI_ERR_LOCALHOST_TERM_CONN);
1798 		}
1799 	} else if (get_pending_per_adv_sync() == per_adv_sync) {
1800 		err = bt_le_per_adv_sync_create_cancel(per_adv_sync);
1801 		/* Delete of the per_adv_sync will be done in the event
1802 		 * handler when cancelling.
1803 		 */
1804 	}
1805 
1806 	return err;
1807 }
1808 
bt_le_per_adv_sync_cb_register(struct bt_le_per_adv_sync_cb * cb)1809 void bt_le_per_adv_sync_cb_register(struct bt_le_per_adv_sync_cb *cb)
1810 {
1811 	sys_slist_append(&pa_sync_cbs, &cb->node);
1812 }
1813 
bt_le_set_per_adv_recv_enable(struct bt_le_per_adv_sync * per_adv_sync,bool enable)1814 static int bt_le_set_per_adv_recv_enable(
1815 	struct bt_le_per_adv_sync *per_adv_sync, bool enable)
1816 {
1817 	struct bt_hci_cp_le_set_per_adv_recv_enable *cp;
1818 	struct bt_le_per_adv_sync_cb *listener;
1819 	struct bt_le_per_adv_sync_state_info info;
1820 	struct net_buf *buf;
1821 	struct bt_hci_cmd_state_set state;
1822 	int err;
1823 
1824 	if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
1825 		return -EAGAIN;
1826 	}
1827 
1828 	if (!BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
1829 		return -ENOTSUP;
1830 	}
1831 
1832 	if (!atomic_test_bit(per_adv_sync->flags, BT_PER_ADV_SYNC_SYNCED)) {
1833 		return -EINVAL;
1834 	}
1835 
1836 	if ((enable && !atomic_test_bit(per_adv_sync->flags,
1837 					BT_PER_ADV_SYNC_RECV_DISABLED)) ||
1838 	    (!enable && atomic_test_bit(per_adv_sync->flags,
1839 					BT_PER_ADV_SYNC_RECV_DISABLED))) {
1840 		return -EALREADY;
1841 	}
1842 
1843 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_PER_ADV_RECV_ENABLE,
1844 				sizeof(*cp));
1845 	if (!buf) {
1846 		return -ENOBUFS;
1847 	}
1848 
1849 	cp = net_buf_add(buf, sizeof(*cp));
1850 	(void)memset(cp, 0, sizeof(*cp));
1851 
1852 	cp->handle = sys_cpu_to_le16(per_adv_sync->handle);
1853 	cp->enable = enable ? 1 : 0;
1854 
1855 	bt_hci_cmd_state_set_init(buf, &state, per_adv_sync->flags,
1856 				  BT_PER_ADV_SYNC_RECV_DISABLED, !enable);
1857 
1858 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_PER_ADV_RECV_ENABLE,
1859 				   buf, NULL);
1860 
1861 	if (err) {
1862 		return err;
1863 	}
1864 
1865 	info.recv_enabled = !atomic_test_bit(per_adv_sync->flags,
1866 					     BT_PER_ADV_SYNC_RECV_DISABLED);
1867 
1868 	SYS_SLIST_FOR_EACH_CONTAINER(&pa_sync_cbs, listener, node) {
1869 		if (listener->state_changed) {
1870 			listener->state_changed(per_adv_sync, &info);
1871 		}
1872 	}
1873 
1874 	return 0;
1875 }
1876 
bt_le_per_adv_sync_recv_enable(struct bt_le_per_adv_sync * per_adv_sync)1877 int bt_le_per_adv_sync_recv_enable(struct bt_le_per_adv_sync *per_adv_sync)
1878 {
1879 	return bt_le_set_per_adv_recv_enable(per_adv_sync, true);
1880 }
1881 
bt_le_per_adv_sync_recv_disable(struct bt_le_per_adv_sync * per_adv_sync)1882 int bt_le_per_adv_sync_recv_disable(struct bt_le_per_adv_sync *per_adv_sync)
1883 {
1884 	return bt_le_set_per_adv_recv_enable(per_adv_sync, false);
1885 }
1886 
1887 #if defined(CONFIG_BT_PER_ADV_SYNC_TRANSFER_SENDER)
bt_le_per_adv_sync_transfer(const struct bt_le_per_adv_sync * per_adv_sync,const struct bt_conn * conn,uint16_t service_data)1888 int bt_le_per_adv_sync_transfer(const struct bt_le_per_adv_sync *per_adv_sync,
1889 				const struct bt_conn *conn,
1890 				uint16_t service_data)
1891 {
1892 	struct bt_hci_cp_le_per_adv_sync_transfer *cp;
1893 	struct net_buf *buf;
1894 
1895 
1896 	if (!BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
1897 		return -ENOTSUP;
1898 	} else if (!BT_FEAT_LE_PAST_SEND(bt_dev.le.features)) {
1899 		return -ENOTSUP;
1900 	}
1901 
1902 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_PER_ADV_SYNC_TRANSFER,
1903 				sizeof(*cp));
1904 	if (!buf) {
1905 		return -ENOBUFS;
1906 	}
1907 
1908 	cp = net_buf_add(buf, sizeof(*cp));
1909 	(void)memset(cp, 0, sizeof(*cp));
1910 
1911 	cp->conn_handle = sys_cpu_to_le16(conn->handle);
1912 	cp->sync_handle = sys_cpu_to_le16(per_adv_sync->handle);
1913 	cp->service_data = sys_cpu_to_le16(service_data);
1914 
1915 	return bt_hci_cmd_send_sync(BT_HCI_OP_LE_PER_ADV_SYNC_TRANSFER, buf,
1916 				    NULL);
1917 }
1918 #endif /* CONFIG_BT_PER_ADV_SYNC_TRANSFER_SENDER */
1919 
1920 #if defined(CONFIG_BT_PER_ADV_SYNC_TRANSFER_RECEIVER)
valid_past_param(const struct bt_le_per_adv_sync_transfer_param * param)1921 static bool valid_past_param(
1922 	const struct bt_le_per_adv_sync_transfer_param *param)
1923 {
1924 	if (param->skip > 0x01f3 ||
1925 	    param->timeout < 0x000A ||
1926 	    param->timeout > 0x4000) {
1927 		return false;
1928 	}
1929 	if ((param->options & BT_LE_PER_ADV_SYNC_TRANSFER_OPT_REPORTING_INITIALLY_DISABLED) &&
1930 	    (param->options & BT_LE_PER_ADV_SYNC_TRANSFER_OPT_FILTER_DUPLICATES)) {
1931 		return false;
1932 	}
1933 
1934 	return true;
1935 }
1936 
past_param_set(const struct bt_conn * conn,uint8_t mode,uint16_t skip,uint16_t timeout,uint8_t cte_type)1937 static int past_param_set(const struct bt_conn *conn, uint8_t mode,
1938 			  uint16_t skip, uint16_t timeout, uint8_t cte_type)
1939 {
1940 	struct bt_hci_cp_le_past_param *cp;
1941 	struct net_buf *buf;
1942 
1943 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_PAST_PARAM, sizeof(*cp));
1944 	if (!buf) {
1945 		return -ENOBUFS;
1946 	}
1947 
1948 	cp = net_buf_add(buf, sizeof(*cp));
1949 	(void)memset(cp, 0, sizeof(*cp));
1950 
1951 	cp->conn_handle = sys_cpu_to_le16(conn->handle);
1952 	cp->mode = mode;
1953 	cp->skip = sys_cpu_to_le16(skip);
1954 	cp->timeout = sys_cpu_to_le16(timeout);
1955 	cp->cte_type = cte_type;
1956 
1957 	return bt_hci_cmd_send_sync(BT_HCI_OP_LE_PAST_PARAM, buf, NULL);
1958 }
1959 
default_past_param_set(uint8_t mode,uint16_t skip,uint16_t timeout,uint8_t cte_type)1960 static int default_past_param_set(uint8_t mode, uint16_t skip, uint16_t timeout,
1961 				  uint8_t cte_type)
1962 {
1963 	struct bt_hci_cp_le_default_past_param *cp;
1964 	struct net_buf *buf;
1965 
1966 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_DEFAULT_PAST_PARAM, sizeof(*cp));
1967 	if (!buf) {
1968 		return -ENOBUFS;
1969 	}
1970 
1971 	cp = net_buf_add(buf, sizeof(*cp));
1972 	(void)memset(cp, 0, sizeof(*cp));
1973 
1974 	cp->mode = mode;
1975 	cp->skip = sys_cpu_to_le16(skip);
1976 	cp->timeout = sys_cpu_to_le16(timeout);
1977 	cp->cte_type = cte_type;
1978 
1979 	return bt_hci_cmd_send_sync(BT_HCI_OP_LE_DEFAULT_PAST_PARAM, buf, NULL);
1980 }
1981 
bt_le_per_adv_sync_transfer_subscribe(const struct bt_conn * conn,const struct bt_le_per_adv_sync_transfer_param * param)1982 int bt_le_per_adv_sync_transfer_subscribe(
1983 	const struct bt_conn *conn,
1984 	const struct bt_le_per_adv_sync_transfer_param *param)
1985 {
1986 	uint8_t cte_type = 0;
1987 	uint8_t mode = BT_HCI_LE_PAST_MODE_SYNC;
1988 
1989 	if (!BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
1990 		return -ENOTSUP;
1991 	} else if (!BT_FEAT_LE_PAST_RECV(bt_dev.le.features)) {
1992 		return -ENOTSUP;
1993 	}
1994 
1995 	if (!valid_past_param(param)) {
1996 		return -EINVAL;
1997 	}
1998 
1999 	if (param->options & BT_LE_PER_ADV_SYNC_TRANSFER_OPT_SYNC_NO_AOA) {
2000 		cte_type |= BT_HCI_LE_PAST_CTE_TYPE_NO_AOA;
2001 	}
2002 
2003 	if (param->options & BT_LE_PER_ADV_SYNC_TRANSFER_OPT_SYNC_NO_AOD_1US) {
2004 		cte_type |= BT_HCI_LE_PAST_CTE_TYPE_NO_AOD_1US;
2005 	}
2006 
2007 	if (param->options & BT_LE_PER_ADV_SYNC_TRANSFER_OPT_SYNC_NO_AOD_2US) {
2008 		cte_type |= BT_HCI_LE_PAST_CTE_TYPE_NO_AOD_2US;
2009 	}
2010 
2011 	if (param->options & BT_LE_PER_ADV_SYNC_TRANSFER_OPT_SYNC_ONLY_CTE) {
2012 		cte_type |= BT_HCI_LE_PAST_CTE_TYPE_ONLY_CTE;
2013 	}
2014 
2015 	if (param->options & BT_LE_PER_ADV_SYNC_TRANSFER_OPT_REPORTING_INITIALLY_DISABLED) {
2016 		mode = BT_HCI_LE_PAST_MODE_NO_REPORTS;
2017 	} else if (param->options & BT_LE_PER_ADV_SYNC_TRANSFER_OPT_FILTER_DUPLICATES) {
2018 		mode = BT_HCI_LE_PAST_MODE_SYNC_FILTER_DUPLICATES;
2019 	}
2020 
2021 	if (conn) {
2022 		return past_param_set(conn, mode, param->skip, param->timeout, cte_type);
2023 	} else {
2024 		return default_past_param_set(mode, param->skip, param->timeout, cte_type);
2025 	}
2026 }
2027 
bt_le_per_adv_sync_transfer_unsubscribe(const struct bt_conn * conn)2028 int bt_le_per_adv_sync_transfer_unsubscribe(const struct bt_conn *conn)
2029 {
2030 	if (!BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
2031 		return -ENOTSUP;
2032 	} else if (!BT_FEAT_LE_PAST_RECV(bt_dev.le.features)) {
2033 		return -ENOTSUP;
2034 	}
2035 
2036 	if (conn) {
2037 		return past_param_set(conn, BT_HCI_LE_PAST_MODE_NO_SYNC, 0,
2038 				      0x0a, 0);
2039 	} else {
2040 		return default_past_param_set(BT_HCI_LE_PAST_MODE_NO_SYNC, 0,
2041 					      0x0a, 0);
2042 	}
2043 }
2044 #endif /* CONFIG_BT_PER_ADV_SYNC_TRANSFER_RECEIVER */
2045 
bt_le_per_adv_list_add(const bt_addr_le_t * addr,uint8_t sid)2046 int bt_le_per_adv_list_add(const bt_addr_le_t *addr, uint8_t sid)
2047 {
2048 	struct bt_hci_cp_le_add_dev_to_per_adv_list *cp;
2049 	struct net_buf *buf;
2050 	int err;
2051 
2052 	if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
2053 		return -EAGAIN;
2054 	}
2055 
2056 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_ADD_DEV_TO_PER_ADV_LIST,
2057 				sizeof(*cp));
2058 	if (!buf) {
2059 		return -ENOBUFS;
2060 	}
2061 
2062 	cp = net_buf_add(buf, sizeof(*cp));
2063 	bt_addr_le_copy(&cp->addr, addr);
2064 	cp->sid = sid;
2065 
2066 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_ADD_DEV_TO_PER_ADV_LIST, buf,
2067 				   NULL);
2068 	if (err) {
2069 		LOG_ERR("Failed to add device to periodic advertiser list");
2070 
2071 		return err;
2072 	}
2073 
2074 	return 0;
2075 }
2076 
bt_le_per_adv_list_remove(const bt_addr_le_t * addr,uint8_t sid)2077 int bt_le_per_adv_list_remove(const bt_addr_le_t *addr, uint8_t sid)
2078 {
2079 	struct bt_hci_cp_le_rem_dev_from_per_adv_list *cp;
2080 	struct net_buf *buf;
2081 	int err;
2082 
2083 	if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
2084 		return -EAGAIN;
2085 	}
2086 
2087 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_REM_DEV_FROM_PER_ADV_LIST,
2088 				sizeof(*cp));
2089 	if (!buf) {
2090 		return -ENOBUFS;
2091 	}
2092 
2093 	cp = net_buf_add(buf, sizeof(*cp));
2094 	bt_addr_le_copy(&cp->addr, addr);
2095 	cp->sid = sid;
2096 
2097 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_REM_DEV_FROM_PER_ADV_LIST, buf,
2098 				   NULL);
2099 	if (err) {
2100 		LOG_ERR("Failed to remove device from periodic advertiser list");
2101 		return err;
2102 	}
2103 
2104 	return 0;
2105 }
2106 
bt_le_per_adv_list_clear(void)2107 int bt_le_per_adv_list_clear(void)
2108 {
2109 	int err;
2110 
2111 	if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
2112 		return -EAGAIN;
2113 	}
2114 
2115 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_CLEAR_PER_ADV_LIST, NULL, NULL);
2116 	if (err) {
2117 		LOG_ERR("Failed to clear periodic advertiser list");
2118 		return err;
2119 	}
2120 
2121 	return 0;
2122 }
2123 #endif /* defined(CONFIG_BT_PER_ADV_SYNC) */
2124