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 <sys/byteorder.h>
10 #include <sys/check.h>
11 
12 #include <bluetooth/bluetooth.h>
13 #include <bluetooth/iso.h>
14 #include <bluetooth/buf.h>
15 #include <bluetooth/direction.h>
16 
17 #include "hci_core.h"
18 #include "conn_internal.h"
19 #include "direction_internal.h"
20 #include "id.h"
21 
22 #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_DEBUG_HCI_CORE)
23 #define LOG_MODULE_NAME bt_scan
24 #include "common/log.h"
25 
26 static bt_le_scan_cb_t *scan_dev_found_cb;
27 static sys_slist_t scan_cbs = SYS_SLIST_STATIC_INIT(&scan_cbs);
28 
29 #if defined(CONFIG_BT_EXT_ADV)
30 #if defined(CONFIG_BT_PER_ADV_SYNC)
31 static struct bt_le_per_adv_sync *get_pending_per_adv_sync(void);
32 static struct bt_le_per_adv_sync per_adv_sync_pool[CONFIG_BT_PER_ADV_SYNC_MAX];
33 static sys_slist_t pa_sync_cbs = SYS_SLIST_STATIC_INIT(&pa_sync_cbs);
34 #endif /* defined(CONFIG_BT_PER_ADV_SYNC) */
35 #endif /* defined(CONFIG_BT_EXT_ADV) */
36 
bt_scan_reset(void)37 void bt_scan_reset(void)
38 {
39 	scan_dev_found_cb = NULL;
40 }
41 
set_le_ext_scan_enable(uint8_t enable,uint16_t duration)42 static int set_le_ext_scan_enable(uint8_t enable, uint16_t duration)
43 {
44 	struct bt_hci_cp_le_set_ext_scan_enable *cp;
45 	struct bt_hci_cmd_state_set state;
46 	struct net_buf *buf;
47 	int err;
48 
49 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_EXT_SCAN_ENABLE, sizeof(*cp));
50 	if (!buf) {
51 		return -ENOBUFS;
52 	}
53 
54 	cp = net_buf_add(buf, sizeof(*cp));
55 
56 	if (enable == BT_HCI_LE_SCAN_ENABLE) {
57 		cp->filter_dup = atomic_test_bit(bt_dev.flags,
58 						 BT_DEV_SCAN_FILTER_DUP);
59 	} else {
60 		cp->filter_dup = BT_HCI_LE_SCAN_FILTER_DUP_DISABLE;
61 	}
62 
63 	cp->enable = enable;
64 	cp->duration = sys_cpu_to_le16(duration);
65 	cp->period = 0;
66 
67 	bt_hci_cmd_state_set_init(buf, &state, bt_dev.flags, BT_DEV_SCANNING,
68 				  enable == BT_HCI_LE_SCAN_ENABLE);
69 
70 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_EXT_SCAN_ENABLE, buf, NULL);
71 	if (err) {
72 		return err;
73 	}
74 
75 	return 0;
76 }
77 
bt_le_scan_set_enable_legacy(uint8_t enable)78 static int bt_le_scan_set_enable_legacy(uint8_t enable)
79 {
80 	struct bt_hci_cp_le_set_scan_enable *cp;
81 	struct bt_hci_cmd_state_set state;
82 	struct net_buf *buf;
83 	int err;
84 
85 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_SCAN_ENABLE, sizeof(*cp));
86 	if (!buf) {
87 		return -ENOBUFS;
88 	}
89 
90 	cp = net_buf_add(buf, sizeof(*cp));
91 
92 	if (enable == BT_HCI_LE_SCAN_ENABLE) {
93 		cp->filter_dup = atomic_test_bit(bt_dev.flags,
94 						 BT_DEV_SCAN_FILTER_DUP);
95 	} else {
96 		cp->filter_dup = BT_HCI_LE_SCAN_FILTER_DUP_DISABLE;
97 	}
98 
99 	cp->enable = enable;
100 
101 	bt_hci_cmd_state_set_init(buf, &state, bt_dev.flags, BT_DEV_SCANNING,
102 				  enable == BT_HCI_LE_SCAN_ENABLE);
103 
104 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_SCAN_ENABLE, buf, NULL);
105 	if (err) {
106 		return err;
107 	}
108 
109 	return 0;
110 }
111 
bt_le_scan_set_enable(uint8_t enable)112 int bt_le_scan_set_enable(uint8_t enable)
113 {
114 	if (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
115 	    BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
116 		return set_le_ext_scan_enable(enable, 0);
117 	}
118 
119 	return bt_le_scan_set_enable_legacy(enable);
120 }
121 
start_le_scan_ext(struct bt_hci_ext_scan_phy * phy_1m,struct bt_hci_ext_scan_phy * phy_coded,uint16_t duration)122 static int start_le_scan_ext(struct bt_hci_ext_scan_phy *phy_1m,
123 			     struct bt_hci_ext_scan_phy *phy_coded,
124 			     uint16_t duration)
125 {
126 	struct bt_hci_cp_le_set_ext_scan_param *set_param;
127 	struct net_buf *buf;
128 	uint8_t own_addr_type;
129 	bool active_scan;
130 	int err;
131 
132 	active_scan = (phy_1m && phy_1m->type == BT_HCI_LE_SCAN_ACTIVE) ||
133 		      (phy_coded && phy_coded->type == BT_HCI_LE_SCAN_ACTIVE);
134 
135 	if (duration > 0) {
136 		atomic_set_bit(bt_dev.flags, BT_DEV_SCAN_LIMITED);
137 
138 		/* Allow bt_le_oob_get_local to be called directly before
139 		 * starting a scan limited by timeout.
140 		 */
141 		if (IS_ENABLED(CONFIG_BT_PRIVACY) && !bt_id_rpa_is_new()) {
142 			atomic_clear_bit(bt_dev.flags, BT_DEV_RPA_VALID);
143 		}
144 	}
145 
146 	err = bt_id_set_scan_own_addr(active_scan, &own_addr_type);
147 	if (err) {
148 		return err;
149 	}
150 
151 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_EXT_SCAN_PARAM,
152 				sizeof(*set_param) +
153 				(phy_1m ? sizeof(*phy_1m) : 0) +
154 				(phy_coded ? sizeof(*phy_coded) : 0));
155 	if (!buf) {
156 		return -ENOBUFS;
157 	}
158 
159 	set_param = net_buf_add(buf, sizeof(*set_param));
160 	set_param->own_addr_type = own_addr_type;
161 	set_param->phys = 0;
162 
163 	if (IS_ENABLED(CONFIG_BT_FILTER_ACCEPT_LIST) &&
164 	    atomic_test_bit(bt_dev.flags, BT_DEV_SCAN_FILTERED)) {
165 		set_param->filter_policy = BT_HCI_LE_SCAN_FP_BASIC_FILTER;
166 	} else {
167 		set_param->filter_policy = BT_HCI_LE_SCAN_FP_BASIC_NO_FILTER;
168 	}
169 
170 	if (phy_1m) {
171 		set_param->phys |= BT_HCI_LE_EXT_SCAN_PHY_1M;
172 		net_buf_add_mem(buf, phy_1m, sizeof(*phy_1m));
173 	}
174 
175 	if (phy_coded) {
176 		set_param->phys |= BT_HCI_LE_EXT_SCAN_PHY_CODED;
177 		net_buf_add_mem(buf, phy_coded, sizeof(*phy_coded));
178 	}
179 
180 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_EXT_SCAN_PARAM, buf, NULL);
181 	if (err) {
182 		return err;
183 	}
184 
185 	err = set_le_ext_scan_enable(BT_HCI_LE_SCAN_ENABLE, duration);
186 	if (err) {
187 		return err;
188 	}
189 
190 	atomic_set_bit_to(bt_dev.flags, BT_DEV_ACTIVE_SCAN, active_scan);
191 
192 	return 0;
193 }
194 
start_le_scan_legacy(uint8_t scan_type,uint16_t interval,uint16_t window)195 static int start_le_scan_legacy(uint8_t scan_type, uint16_t interval, uint16_t window)
196 {
197 	struct bt_hci_cp_le_set_scan_param set_param;
198 	struct net_buf *buf;
199 	int err;
200 	bool active_scan;
201 
202 	(void)memset(&set_param, 0, sizeof(set_param));
203 
204 	set_param.scan_type = scan_type;
205 
206 	/* for the rest parameters apply default values according to
207 	 *  spec 4.2, vol2, part E, 7.8.10
208 	 */
209 	set_param.interval = sys_cpu_to_le16(interval);
210 	set_param.window = sys_cpu_to_le16(window);
211 
212 	if (IS_ENABLED(CONFIG_BT_FILTER_ACCEPT_LIST) &&
213 	    atomic_test_bit(bt_dev.flags, BT_DEV_SCAN_FILTERED)) {
214 		set_param.filter_policy = BT_HCI_LE_SCAN_FP_BASIC_FILTER;
215 	} else {
216 		set_param.filter_policy = BT_HCI_LE_SCAN_FP_BASIC_NO_FILTER;
217 	}
218 
219 	active_scan = scan_type == BT_HCI_LE_SCAN_ACTIVE;
220 	err = bt_id_set_scan_own_addr(active_scan, &set_param.addr_type);
221 	if (err) {
222 		return err;
223 	}
224 
225 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_SCAN_PARAM, sizeof(set_param));
226 	if (!buf) {
227 		return -ENOBUFS;
228 	}
229 
230 	net_buf_add_mem(buf, &set_param, sizeof(set_param));
231 
232 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_SCAN_PARAM, buf, NULL);
233 	if (err) {
234 		return err;
235 	}
236 
237 	err = bt_le_scan_set_enable(BT_HCI_LE_SCAN_ENABLE);
238 	if (err) {
239 		return err;
240 	}
241 
242 	atomic_set_bit_to(bt_dev.flags, BT_DEV_ACTIVE_SCAN, active_scan);
243 
244 	return 0;
245 }
246 
start_passive_scan(bool fast_scan)247 static int start_passive_scan(bool fast_scan)
248 {
249 	uint16_t interval, window;
250 
251 	if (fast_scan) {
252 		interval = BT_GAP_SCAN_FAST_INTERVAL;
253 		window = BT_GAP_SCAN_FAST_WINDOW;
254 	} else {
255 		interval = CONFIG_BT_BACKGROUND_SCAN_INTERVAL;
256 		window = CONFIG_BT_BACKGROUND_SCAN_WINDOW;
257 	}
258 
259 	if (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
260 	    BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
261 		struct bt_hci_ext_scan_phy scan;
262 
263 		scan.type = BT_HCI_LE_SCAN_PASSIVE;
264 		scan.interval = sys_cpu_to_le16(interval);
265 		scan.window = sys_cpu_to_le16(window);
266 
267 		return start_le_scan_ext(&scan, NULL, 0);
268 	}
269 
270 	return start_le_scan_legacy(BT_HCI_LE_SCAN_PASSIVE, interval, window);
271 }
272 
bt_le_scan_update(bool fast_scan)273 int bt_le_scan_update(bool fast_scan)
274 {
275 	if (atomic_test_bit(bt_dev.flags, BT_DEV_EXPLICIT_SCAN)) {
276 		return 0;
277 	}
278 
279 	if (atomic_test_bit(bt_dev.flags, BT_DEV_SCANNING)) {
280 		int err;
281 
282 		err = bt_le_scan_set_enable(BT_HCI_LE_SCAN_DISABLE);
283 		if (err) {
284 			return err;
285 		}
286 	}
287 
288 	if (IS_ENABLED(CONFIG_BT_CENTRAL)) {
289 		struct bt_conn *conn;
290 
291 		/* don't restart scan if we have pending connection */
292 		conn = bt_conn_lookup_state_le(BT_ID_DEFAULT, NULL,
293 					       BT_CONN_CONNECT);
294 		if (conn) {
295 			bt_conn_unref(conn);
296 			return 0;
297 		}
298 
299 		conn = bt_conn_lookup_state_le(BT_ID_DEFAULT, NULL,
300 					       BT_CONN_CONNECT_SCAN);
301 		if (conn) {
302 			atomic_set_bit(bt_dev.flags, BT_DEV_SCAN_FILTER_DUP);
303 
304 			bt_conn_unref(conn);
305 
306 			return start_passive_scan(fast_scan);
307 		}
308 	}
309 
310 #if defined(CONFIG_BT_PER_ADV_SYNC)
311 	if (get_pending_per_adv_sync()) {
312 		return start_passive_scan(fast_scan);
313 	}
314 #endif
315 
316 	return 0;
317 }
318 
319 #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)320 static void check_pending_conn(const bt_addr_le_t *id_addr,
321 			       const bt_addr_le_t *addr, uint8_t adv_props)
322 {
323 	struct bt_conn *conn;
324 
325 	/* No connections are allowed during explicit scanning */
326 	if (atomic_test_bit(bt_dev.flags, BT_DEV_EXPLICIT_SCAN)) {
327 		return;
328 	}
329 
330 	/* Return if event is not connectable */
331 	if (!(adv_props & BT_HCI_LE_ADV_EVT_TYPE_CONN)) {
332 		return;
333 	}
334 
335 	conn = bt_conn_lookup_state_le(BT_ID_DEFAULT, id_addr,
336 				       BT_CONN_CONNECT_SCAN);
337 	if (!conn) {
338 		return;
339 	}
340 
341 	if (atomic_test_bit(bt_dev.flags, BT_DEV_SCANNING) &&
342 	    bt_le_scan_set_enable(BT_HCI_LE_SCAN_DISABLE)) {
343 		goto failed;
344 	}
345 
346 	bt_addr_le_copy(&conn->le.resp_addr, addr);
347 	if (bt_le_create_conn(conn)) {
348 		goto failed;
349 	}
350 
351 	bt_conn_set_state(conn, BT_CONN_CONNECT);
352 	bt_conn_unref(conn);
353 	return;
354 
355 failed:
356 	conn->err = BT_HCI_ERR_UNSPECIFIED;
357 	bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
358 	bt_conn_unref(conn);
359 	bt_le_scan_update(false);
360 }
361 #endif /* CONFIG_BT_CENTRAL */
362 
363 /* Convert Legacy adv report evt_type field to adv props */
get_adv_props(uint8_t evt_type)364 static uint8_t get_adv_props(uint8_t evt_type)
365 {
366 	switch (evt_type) {
367 	case BT_GAP_ADV_TYPE_ADV_IND:
368 		return BT_GAP_ADV_PROP_CONNECTABLE |
369 		       BT_GAP_ADV_PROP_SCANNABLE;
370 
371 	case BT_GAP_ADV_TYPE_ADV_DIRECT_IND:
372 		return BT_GAP_ADV_PROP_CONNECTABLE |
373 		       BT_GAP_ADV_PROP_DIRECTED;
374 
375 	case BT_GAP_ADV_TYPE_ADV_SCAN_IND:
376 		return BT_GAP_ADV_PROP_SCANNABLE;
377 
378 	case BT_GAP_ADV_TYPE_ADV_NONCONN_IND:
379 		return 0;
380 
381 	/* In legacy advertising report, we don't know if the scan
382 	 * response come from a connectable advertiser, so don't
383 	 * set connectable property bit.
384 	 */
385 	case BT_GAP_ADV_TYPE_SCAN_RSP:
386 		return BT_GAP_ADV_PROP_SCAN_RESPONSE |
387 		       BT_GAP_ADV_PROP_SCANNABLE;
388 
389 	default:
390 		return 0;
391 	}
392 }
393 
le_adv_recv(bt_addr_le_t * addr,struct bt_le_scan_recv_info * info,struct net_buf * buf,uint8_t len)394 static void le_adv_recv(bt_addr_le_t *addr, struct bt_le_scan_recv_info *info,
395 			struct net_buf *buf, uint8_t len)
396 {
397 	struct bt_le_scan_cb *listener, *next;
398 	struct net_buf_simple_state state;
399 	bt_addr_le_t id_addr;
400 
401 	BT_DBG("%s event %u, len %u, rssi %d dBm", bt_addr_le_str(addr),
402 	       info->adv_type, len, info->rssi);
403 
404 	if (!IS_ENABLED(CONFIG_BT_PRIVACY) &&
405 	    !IS_ENABLED(CONFIG_BT_SCAN_WITH_IDENTITY) &&
406 	    atomic_test_bit(bt_dev.flags, BT_DEV_EXPLICIT_SCAN) &&
407 	    (info->adv_props & BT_HCI_LE_ADV_PROP_DIRECT)) {
408 		BT_DBG("Dropped direct adv report");
409 		return;
410 	}
411 
412 	if (addr->type == BT_ADDR_LE_PUBLIC_ID ||
413 	    addr->type == BT_ADDR_LE_RANDOM_ID) {
414 		bt_addr_le_copy(&id_addr, addr);
415 		id_addr.type -= BT_ADDR_LE_PUBLIC_ID;
416 	} else if (addr->type == BT_HCI_PEER_ADDR_ANONYMOUS) {
417 		bt_addr_le_copy(&id_addr, BT_ADDR_LE_ANY);
418 	} else {
419 		bt_addr_le_copy(&id_addr,
420 				bt_lookup_id_addr(BT_ID_DEFAULT, addr));
421 	}
422 
423 	info->addr = &id_addr;
424 
425 	if (scan_dev_found_cb) {
426 		net_buf_simple_save(&buf->b, &state);
427 
428 		buf->len = len;
429 		scan_dev_found_cb(&id_addr, info->rssi, info->adv_type,
430 				  &buf->b);
431 
432 		net_buf_simple_restore(&buf->b, &state);
433 	}
434 
435 	SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&scan_cbs, listener, next, node) {
436 		if (listener->recv) {
437 			net_buf_simple_save(&buf->b, &state);
438 
439 			buf->len = len;
440 			listener->recv(info, &buf->b);
441 
442 			net_buf_simple_restore(&buf->b, &state);
443 		}
444 	}
445 
446 #if defined(CONFIG_BT_CENTRAL)
447 	check_pending_conn(&id_addr, addr, info->adv_props);
448 #endif /* CONFIG_BT_CENTRAL */
449 }
450 
451 #if defined(CONFIG_BT_EXT_ADV)
bt_hci_le_scan_timeout(struct net_buf * buf)452 void bt_hci_le_scan_timeout(struct net_buf *buf)
453 {
454 	struct bt_le_scan_cb *listener, *next;
455 
456 	atomic_clear_bit(bt_dev.flags, BT_DEV_SCANNING);
457 	atomic_clear_bit(bt_dev.flags, BT_DEV_EXPLICIT_SCAN);
458 
459 	atomic_clear_bit(bt_dev.flags, BT_DEV_SCAN_LIMITED);
460 	atomic_clear_bit(bt_dev.flags, BT_DEV_RPA_VALID);
461 
462 #if defined(CONFIG_BT_SMP)
463 	bt_id_pending_keys_update();
464 #endif
465 
466 	SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&scan_cbs, listener, next, node) {
467 		if (listener->timeout) {
468 			listener->timeout();
469 		}
470 	}
471 }
472 
473 /* Convert Extended adv report evt_type field into adv type */
get_adv_type(uint8_t evt_type)474 static uint8_t get_adv_type(uint8_t evt_type)
475 {
476 	switch (evt_type) {
477 	case (BT_HCI_LE_ADV_EVT_TYPE_CONN |
478 	      BT_HCI_LE_ADV_EVT_TYPE_SCAN |
479 	      BT_HCI_LE_ADV_EVT_TYPE_LEGACY):
480 		return BT_GAP_ADV_TYPE_ADV_IND;
481 
482 	case (BT_HCI_LE_ADV_EVT_TYPE_CONN |
483 	      BT_HCI_LE_ADV_EVT_TYPE_DIRECT |
484 	      BT_HCI_LE_ADV_EVT_TYPE_LEGACY):
485 		return BT_GAP_ADV_TYPE_ADV_DIRECT_IND;
486 
487 	case (BT_HCI_LE_ADV_EVT_TYPE_SCAN |
488 	      BT_HCI_LE_ADV_EVT_TYPE_LEGACY):
489 		return BT_GAP_ADV_TYPE_ADV_SCAN_IND;
490 
491 	case BT_HCI_LE_ADV_EVT_TYPE_LEGACY:
492 		return BT_GAP_ADV_TYPE_ADV_NONCONN_IND;
493 
494 	case (BT_HCI_LE_ADV_EVT_TYPE_SCAN_RSP |
495 	      BT_HCI_LE_ADV_EVT_TYPE_CONN |
496 	      BT_HCI_LE_ADV_EVT_TYPE_SCAN |
497 	      BT_HCI_LE_ADV_EVT_TYPE_LEGACY):
498 	case (BT_HCI_LE_ADV_EVT_TYPE_SCAN_RSP |
499 	      BT_HCI_LE_ADV_EVT_TYPE_SCAN |
500 	      BT_HCI_LE_ADV_EVT_TYPE_LEGACY):
501 		/* Scan response from connectable or non-connectable advertiser.
502 		 */
503 		return BT_GAP_ADV_TYPE_SCAN_RSP;
504 
505 	default:
506 		return BT_GAP_ADV_TYPE_EXT_ADV;
507 	}
508 }
509 
bt_hci_le_adv_ext_report(struct net_buf * buf)510 void bt_hci_le_adv_ext_report(struct net_buf *buf)
511 {
512 	uint8_t num_reports = net_buf_pull_u8(buf);
513 
514 	BT_DBG("Adv number of reports %u",  num_reports);
515 
516 	while (num_reports--) {
517 		struct bt_hci_evt_le_ext_advertising_info *evt;
518 		struct bt_le_scan_recv_info adv_info;
519 
520 		if (buf->len < sizeof(*evt)) {
521 			BT_ERR("Unexpected end of buffer");
522 			break;
523 		}
524 
525 		evt = net_buf_pull_mem(buf, sizeof(*evt));
526 
527 		adv_info.primary_phy = bt_get_phy(evt->prim_phy);
528 		adv_info.secondary_phy = bt_get_phy(evt->sec_phy);
529 		adv_info.tx_power = evt->tx_power;
530 		adv_info.rssi = evt->rssi;
531 		adv_info.sid = evt->sid;
532 		adv_info.interval = sys_le16_to_cpu(evt->interval);
533 
534 		adv_info.adv_type = get_adv_type(evt->evt_type);
535 		/* Convert "Legacy" property to Extended property. */
536 		adv_info.adv_props = evt->evt_type ^ BT_HCI_LE_ADV_PROP_LEGACY;
537 
538 		le_adv_recv(&evt->addr, &adv_info, buf, evt->length);
539 
540 		net_buf_pull(buf, evt->length);
541 	}
542 }
543 
544 
545 #if defined(CONFIG_BT_PER_ADV_SYNC)
per_adv_sync_delete(struct bt_le_per_adv_sync * per_adv_sync)546 static void per_adv_sync_delete(struct bt_le_per_adv_sync *per_adv_sync)
547 {
548 	atomic_clear(per_adv_sync->flags);
549 }
550 
per_adv_sync_new(void)551 static struct bt_le_per_adv_sync *per_adv_sync_new(void)
552 {
553 	struct bt_le_per_adv_sync *per_adv_sync = NULL;
554 
555 	for (int i = 0; i < ARRAY_SIZE(per_adv_sync_pool); i++) {
556 		if (!atomic_test_bit(per_adv_sync_pool[i].flags,
557 				     BT_PER_ADV_SYNC_CREATED)) {
558 			per_adv_sync = &per_adv_sync_pool[i];
559 			break;
560 		}
561 	}
562 
563 	if (!per_adv_sync) {
564 		return NULL;
565 	}
566 
567 	(void)memset(per_adv_sync, 0, sizeof(*per_adv_sync));
568 	atomic_set_bit(per_adv_sync->flags, BT_PER_ADV_SYNC_CREATED);
569 
570 	return per_adv_sync;
571 }
572 
get_pending_per_adv_sync(void)573 static struct bt_le_per_adv_sync *get_pending_per_adv_sync(void)
574 {
575 	for (int i = 0; i < ARRAY_SIZE(per_adv_sync_pool); i++) {
576 		if (atomic_test_bit(per_adv_sync_pool[i].flags,
577 				    BT_PER_ADV_SYNC_SYNCING)) {
578 			return &per_adv_sync_pool[i];
579 		}
580 	}
581 
582 	return NULL;
583 }
584 
bt_hci_get_per_adv_sync(uint16_t handle)585 struct bt_le_per_adv_sync *bt_hci_get_per_adv_sync(uint16_t handle)
586 {
587 	for (int i = 0; i < ARRAY_SIZE(per_adv_sync_pool); i++) {
588 		if (per_adv_sync_pool[i].handle == handle &&
589 		    atomic_test_bit(per_adv_sync_pool[i].flags,
590 				    BT_PER_ADV_SYNC_SYNCED)) {
591 			return &per_adv_sync_pool[i];
592 		}
593 	}
594 
595 	return NULL;
596 }
597 
bt_hci_le_per_adv_report(struct net_buf * buf)598 void bt_hci_le_per_adv_report(struct net_buf *buf)
599 {
600 	struct bt_hci_evt_le_per_advertising_report *evt;
601 	struct bt_le_per_adv_sync *per_adv_sync;
602 	struct bt_le_per_adv_sync_recv_info info;
603 	struct bt_le_per_adv_sync_cb *listener;
604 	struct net_buf_simple_state state;
605 
606 	if (buf->len < sizeof(*evt)) {
607 		BT_ERR("Unexpected end of buffer");
608 		return;
609 	}
610 
611 	evt = net_buf_pull_mem(buf, sizeof(*evt));
612 
613 	per_adv_sync = bt_hci_get_per_adv_sync(sys_le16_to_cpu(evt->handle));
614 
615 	if (!per_adv_sync) {
616 		BT_ERR("Unknown handle 0x%04X for periodic advertising report",
617 		       sys_le16_to_cpu(evt->handle));
618 		return;
619 	}
620 
621 	if (atomic_test_bit(per_adv_sync->flags,
622 			    BT_PER_ADV_SYNC_RECV_DISABLED)) {
623 		BT_ERR("Received PA adv report when receive disabled");
624 		return;
625 	}
626 
627 	info.tx_power = evt->tx_power;
628 	info.rssi = evt->rssi;
629 	info.cte_type = BIT(evt->cte_type);
630 	info.addr = &per_adv_sync->addr;
631 	info.sid = per_adv_sync->sid;
632 
633 	SYS_SLIST_FOR_EACH_CONTAINER(&pa_sync_cbs, listener, node) {
634 		if (listener->recv) {
635 			net_buf_simple_save(&buf->b, &state);
636 
637 			buf->len = evt->length;
638 			listener->recv(per_adv_sync, &info, &buf->b);
639 
640 			net_buf_simple_restore(&buf->b, &state);
641 		}
642 	}
643 }
644 
per_adv_sync_terminate(uint16_t handle)645 static int per_adv_sync_terminate(uint16_t handle)
646 {
647 	struct bt_hci_cp_le_per_adv_terminate_sync *cp;
648 	struct net_buf *buf;
649 
650 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_PER_ADV_TERMINATE_SYNC,
651 				sizeof(*cp));
652 	if (!buf) {
653 		return -ENOBUFS;
654 	}
655 
656 	cp = net_buf_add(buf, sizeof(*cp));
657 	(void)memset(cp, 0, sizeof(*cp));
658 
659 	cp->handle = sys_cpu_to_le16(handle);
660 
661 	return bt_hci_cmd_send_sync(BT_HCI_OP_LE_PER_ADV_TERMINATE_SYNC, buf,
662 				    NULL);
663 }
664 
bt_hci_le_per_adv_sync_established(struct net_buf * buf)665 void bt_hci_le_per_adv_sync_established(struct net_buf *buf)
666 {
667 	struct bt_hci_evt_le_per_adv_sync_established *evt =
668 		(struct bt_hci_evt_le_per_adv_sync_established *)buf->data;
669 	struct bt_le_per_adv_sync_synced_info sync_info;
670 	struct bt_le_per_adv_sync *pending_per_adv_sync;
671 	struct bt_le_per_adv_sync_cb *listener;
672 	bool unexpected_evt;
673 	int err;
674 
675 	pending_per_adv_sync = get_pending_per_adv_sync();
676 
677 	if (pending_per_adv_sync) {
678 		atomic_clear_bit(pending_per_adv_sync->flags,
679 				 BT_PER_ADV_SYNC_SYNCING);
680 		err = bt_le_scan_update(false);
681 
682 		if (err) {
683 			BT_ERR("Could not update scan (%d)", err);
684 		}
685 	}
686 
687 	if (evt->status == BT_HCI_ERR_OP_CANCELLED_BY_HOST) {
688 		/* Cancelled locally, don't call CB */
689 		if (pending_per_adv_sync) {
690 			per_adv_sync_delete(pending_per_adv_sync);
691 		} else {
692 			BT_ERR("Unexpected per adv sync cancelled event");
693 		}
694 
695 		return;
696 	}
697 
698 	if (!pending_per_adv_sync ||
699 	    (!atomic_test_bit(pending_per_adv_sync->flags,
700 			      BT_PER_ADV_SYNC_SYNCING_USE_LIST) &&
701 	     ((pending_per_adv_sync->sid != evt->sid) ||
702 	      bt_addr_le_cmp(&pending_per_adv_sync->addr, &evt->adv_addr)))) {
703 		BT_ERR("Unexpected per adv sync established event");
704 		/* Request terminate of pending periodic advertising in controller */
705 		per_adv_sync_terminate(sys_le16_to_cpu(evt->handle));
706 
707 		unexpected_evt = true;
708 	} else {
709 		unexpected_evt = false;
710 	}
711 
712 	if (unexpected_evt || evt->status != BT_HCI_ERR_SUCCESS) {
713 		if (pending_per_adv_sync) {
714 			struct bt_le_per_adv_sync_term_info term_info;
715 
716 			/* Terminate the pending PA sync and notify app */
717 			term_info.addr = &pending_per_adv_sync->addr;
718 			term_info.sid = pending_per_adv_sync->sid;
719 			term_info.reason = unexpected_evt ? BT_HCI_ERR_UNSPECIFIED : evt->status;
720 
721 			/* Deleting before callback, so the caller will be able
722 			 * to restart sync in the callback.
723 			 */
724 			per_adv_sync_delete(pending_per_adv_sync);
725 
726 			SYS_SLIST_FOR_EACH_CONTAINER(&pa_sync_cbs,
727 						     listener,
728 						     node) {
729 				if (listener->term) {
730 					listener->term(pending_per_adv_sync,
731 						       &term_info);
732 				}
733 			}
734 		}
735 		return;
736 	}
737 
738 	atomic_set_bit(pending_per_adv_sync->flags, BT_PER_ADV_SYNC_SYNCED);
739 
740 	pending_per_adv_sync->handle = sys_le16_to_cpu(evt->handle);
741 	pending_per_adv_sync->interval = sys_le16_to_cpu(evt->interval);
742 	pending_per_adv_sync->clock_accuracy =
743 		sys_le16_to_cpu(evt->clock_accuracy);
744 	pending_per_adv_sync->phy = evt->phy;
745 
746 	memset(&sync_info, 0, sizeof(sync_info));
747 	sync_info.interval = pending_per_adv_sync->interval;
748 	sync_info.phy = bt_get_phy(pending_per_adv_sync->phy);
749 	sync_info.addr = &pending_per_adv_sync->addr;
750 	sync_info.sid = pending_per_adv_sync->sid;
751 
752 	sync_info.recv_enabled =
753 		!atomic_test_bit(pending_per_adv_sync->flags,
754 				 BT_PER_ADV_SYNC_RECV_DISABLED);
755 
756 	SYS_SLIST_FOR_EACH_CONTAINER(&pa_sync_cbs, listener, node) {
757 		if (listener->synced) {
758 			listener->synced(pending_per_adv_sync, &sync_info);
759 		}
760 	}
761 }
762 
bt_hci_le_per_adv_sync_lost(struct net_buf * buf)763 void bt_hci_le_per_adv_sync_lost(struct net_buf *buf)
764 {
765 	struct bt_hci_evt_le_per_adv_sync_lost *evt =
766 		(struct bt_hci_evt_le_per_adv_sync_lost *)buf->data;
767 	struct bt_le_per_adv_sync_term_info term_info;
768 	struct bt_le_per_adv_sync *per_adv_sync;
769 	struct bt_le_per_adv_sync_cb *listener;
770 
771 	per_adv_sync = bt_hci_get_per_adv_sync(sys_le16_to_cpu(evt->handle));
772 
773 	if (!per_adv_sync) {
774 		BT_ERR("Unknown handle 0x%04Xfor periodic adv sync lost",
775 		       sys_le16_to_cpu(evt->handle));
776 		return;
777 	}
778 
779 	term_info.addr = &per_adv_sync->addr;
780 	term_info.sid = per_adv_sync->sid;
781 	/* There is no status in the per. adv. sync lost event */
782 	term_info.reason = BT_HCI_ERR_UNSPECIFIED;
783 
784 	/* Deleting before callback, so the caller will be able to restart
785 	 * sync in the callback
786 	 */
787 	per_adv_sync_delete(per_adv_sync);
788 
789 
790 	SYS_SLIST_FOR_EACH_CONTAINER(&pa_sync_cbs, listener, node) {
791 		if (listener->term) {
792 			listener->term(per_adv_sync, &term_info);
793 		}
794 	}
795 }
796 
797 #if defined(CONFIG_BT_CONN)
bt_hci_le_past_received(struct net_buf * buf)798 void bt_hci_le_past_received(struct net_buf *buf)
799 {
800 	struct bt_hci_evt_le_past_received *evt =
801 		(struct bt_hci_evt_le_past_received *)buf->data;
802 	struct bt_le_per_adv_sync_synced_info sync_info;
803 	struct bt_le_per_adv_sync_cb *listener;
804 	struct bt_le_per_adv_sync *per_adv_sync;
805 
806 	if (evt->status) {
807 		/* No sync created, don't notify app */
808 		BT_DBG("PAST receive failed with status 0x%02X", evt->status);
809 		return;
810 	}
811 
812 	sync_info.conn = bt_conn_lookup_handle(
813 				sys_le16_to_cpu(evt->conn_handle));
814 
815 	if (!sync_info.conn) {
816 		BT_ERR("Could not lookup connection handle from PAST");
817 		per_adv_sync_terminate(sys_le16_to_cpu(evt->sync_handle));
818 		return;
819 	}
820 
821 	per_adv_sync = per_adv_sync_new();
822 	if (!per_adv_sync) {
823 		BT_WARN("Could not allocate new PA sync from PAST");
824 		per_adv_sync_terminate(sys_le16_to_cpu(evt->sync_handle));
825 		return;
826 	}
827 
828 	atomic_set_bit(per_adv_sync->flags, BT_PER_ADV_SYNC_SYNCED);
829 
830 	per_adv_sync->handle = sys_le16_to_cpu(evt->sync_handle);
831 	per_adv_sync->interval = sys_le16_to_cpu(evt->interval);
832 	per_adv_sync->clock_accuracy = sys_le16_to_cpu(evt->clock_accuracy);
833 	per_adv_sync->phy = evt->phy;
834 	bt_addr_le_copy(&per_adv_sync->addr, &evt->addr);
835 	per_adv_sync->sid = evt->adv_sid;
836 
837 	sync_info.interval = per_adv_sync->interval;
838 	sync_info.phy = bt_get_phy(per_adv_sync->phy);
839 	sync_info.addr = &per_adv_sync->addr;
840 	sync_info.sid = per_adv_sync->sid;
841 	sync_info.service_data = sys_le16_to_cpu(evt->service_data);
842 
843 	SYS_SLIST_FOR_EACH_CONTAINER(&pa_sync_cbs, listener, node) {
844 		if (listener->synced) {
845 			listener->synced(per_adv_sync, &sync_info);
846 		}
847 	}
848 }
849 #endif /* CONFIG_BT_CONN */
850 
851 #if defined(CONFIG_BT_ISO_BROADCAST)
bt_hci_le_biginfo_adv_report(struct net_buf * buf)852 void bt_hci_le_biginfo_adv_report(struct net_buf *buf)
853 {
854 	struct bt_hci_evt_le_biginfo_adv_report *evt;
855 	struct bt_le_per_adv_sync *per_adv_sync;
856 	struct bt_le_per_adv_sync_cb *listener;
857 	struct bt_iso_biginfo biginfo;
858 
859 	evt = net_buf_pull_mem(buf, sizeof(*evt));
860 
861 	per_adv_sync = bt_hci_get_per_adv_sync(sys_le16_to_cpu(evt->sync_handle));
862 
863 	if (!per_adv_sync) {
864 		BT_ERR("Unknown handle 0x%04X for periodic advertising report",
865 		       sys_le16_to_cpu(evt->sync_handle));
866 		return;
867 	}
868 
869 	biginfo.addr = &per_adv_sync->addr;
870 	biginfo.sid = per_adv_sync->sid;
871 	biginfo.num_bis = evt->num_bis;
872 	biginfo.sub_evt_count = evt->nse;
873 	biginfo.iso_interval = sys_le16_to_cpu(evt->iso_interval);
874 	biginfo.burst_number = evt->bn;
875 	biginfo.offset = evt->pto;
876 	biginfo.rep_count = evt->irc;
877 	biginfo.max_pdu = sys_le16_to_cpu(evt->max_pdu);
878 	biginfo.sdu_interval = sys_get_le24(evt->sdu_interval);
879 	biginfo.max_sdu = sys_le16_to_cpu(evt->max_sdu);
880 	biginfo.phy = evt->phy;
881 	biginfo.framing = evt->framing;
882 	biginfo.encryption = evt->encryption ? true : false;
883 
884 	SYS_SLIST_FOR_EACH_CONTAINER(&pa_sync_cbs, listener, node) {
885 		if (listener->biginfo) {
886 			listener->biginfo(per_adv_sync, &biginfo);
887 		}
888 	}
889 }
890 #endif /* defined(CONFIG_BT_ISO_BROADCAST) */
891 #if defined(CONFIG_BT_DF_CONNECTIONLESS_CTE_RX)
bt_hci_le_df_connectionless_iq_report(struct net_buf * buf)892 void bt_hci_le_df_connectionless_iq_report(struct net_buf *buf)
893 {
894 	struct bt_df_per_adv_sync_iq_samples_report cte_report;
895 	struct bt_le_per_adv_sync *per_adv_sync;
896 	struct bt_le_per_adv_sync_cb *listener;
897 
898 	hci_df_prepare_connectionless_iq_report(buf, &cte_report, &per_adv_sync);
899 
900 	SYS_SLIST_FOR_EACH_CONTAINER(&pa_sync_cbs, listener, node) {
901 		if (listener->cte_report_cb) {
902 			listener->cte_report_cb(per_adv_sync, &cte_report);
903 		}
904 	}
905 }
906 #endif /* CONFIG_BT_DF_CONNECTIONLESS_CTE_RX */
907 #endif /* defined(CONFIG_BT_PER_ADV_SYNC) */
908 #endif /* defined(CONFIG_BT_EXT_ADV) */
909 
bt_hci_le_adv_report(struct net_buf * buf)910 void bt_hci_le_adv_report(struct net_buf *buf)
911 {
912 	uint8_t num_reports = net_buf_pull_u8(buf);
913 	struct bt_hci_evt_le_advertising_info *evt;
914 
915 	BT_DBG("Adv number of reports %u",  num_reports);
916 
917 	while (num_reports--) {
918 		struct bt_le_scan_recv_info adv_info;
919 
920 		if (buf->len < sizeof(*evt)) {
921 			BT_ERR("Unexpected end of buffer");
922 			break;
923 		}
924 
925 		evt = net_buf_pull_mem(buf, sizeof(*evt));
926 
927 		adv_info.primary_phy = BT_GAP_LE_PHY_1M;
928 		adv_info.secondary_phy = 0;
929 		adv_info.tx_power = BT_GAP_TX_POWER_INVALID;
930 		adv_info.rssi = evt->data[evt->length];
931 		adv_info.sid = BT_GAP_SID_INVALID;
932 		adv_info.interval = 0U;
933 
934 		adv_info.adv_type = evt->evt_type;
935 		adv_info.adv_props = get_adv_props(evt->evt_type);
936 
937 		le_adv_recv(&evt->addr, &adv_info, buf, evt->length);
938 
939 		net_buf_pull(buf, evt->length + sizeof(adv_info.rssi));
940 	}
941 }
942 
valid_le_scan_param(const struct bt_le_scan_param * param)943 static bool valid_le_scan_param(const struct bt_le_scan_param *param)
944 {
945 	if (param->type != BT_HCI_LE_SCAN_PASSIVE &&
946 	    param->type != BT_HCI_LE_SCAN_ACTIVE) {
947 		return false;
948 	}
949 
950 	if (param->options & ~(BT_LE_SCAN_OPT_FILTER_DUPLICATE |
951 			       BT_LE_SCAN_OPT_FILTER_ACCEPT_LIST |
952 			       BT_LE_SCAN_OPT_CODED |
953 			       BT_LE_SCAN_OPT_NO_1M)) {
954 		return false;
955 	}
956 
957 	if (param->interval < 0x0004 || param->interval > 0x4000) {
958 		return false;
959 	}
960 
961 	if (param->window < 0x0004 || param->window > 0x4000) {
962 		return false;
963 	}
964 
965 	if (param->window > param->interval) {
966 		return false;
967 	}
968 
969 	return true;
970 }
971 
bt_le_scan_start(const struct bt_le_scan_param * param,bt_le_scan_cb_t cb)972 int bt_le_scan_start(const struct bt_le_scan_param *param, bt_le_scan_cb_t cb)
973 {
974 	int err;
975 
976 	if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
977 		return -EAGAIN;
978 	}
979 
980 	/* Check that the parameters have valid values */
981 	if (!valid_le_scan_param(param)) {
982 		return -EINVAL;
983 	}
984 
985 	if (param->type && !bt_id_scan_random_addr_check()) {
986 		return -EINVAL;
987 	}
988 
989 	/* Return if active scan is already enabled */
990 	if (atomic_test_and_set_bit(bt_dev.flags, BT_DEV_EXPLICIT_SCAN)) {
991 		return -EALREADY;
992 	}
993 
994 	if (atomic_test_bit(bt_dev.flags, BT_DEV_SCANNING)) {
995 		err = bt_le_scan_set_enable(BT_HCI_LE_SCAN_DISABLE);
996 		if (err) {
997 			atomic_clear_bit(bt_dev.flags, BT_DEV_EXPLICIT_SCAN);
998 			return err;
999 		}
1000 	}
1001 
1002 	atomic_set_bit_to(bt_dev.flags, BT_DEV_SCAN_FILTER_DUP,
1003 			  param->options & BT_LE_SCAN_OPT_FILTER_DUPLICATE);
1004 
1005 #if defined(CONFIG_BT_FILTER_ACCEPT_LIST)
1006 	atomic_set_bit_to(bt_dev.flags, BT_DEV_SCAN_FILTERED,
1007 			  param->options & BT_LE_SCAN_OPT_FILTER_ACCEPT_LIST);
1008 #endif /* defined(CONFIG_BT_FILTER_ACCEPT_LIST) */
1009 
1010 	if (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
1011 	    BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
1012 		struct bt_hci_ext_scan_phy param_1m;
1013 		struct bt_hci_ext_scan_phy param_coded;
1014 
1015 		struct bt_hci_ext_scan_phy *phy_1m = NULL;
1016 		struct bt_hci_ext_scan_phy *phy_coded = NULL;
1017 
1018 		if (!(param->options & BT_LE_SCAN_OPT_NO_1M)) {
1019 			param_1m.type = param->type;
1020 			param_1m.interval = sys_cpu_to_le16(param->interval);
1021 			param_1m.window = sys_cpu_to_le16(param->window);
1022 
1023 			phy_1m = &param_1m;
1024 		}
1025 
1026 		if (param->options & BT_LE_SCAN_OPT_CODED) {
1027 			uint16_t interval = param->interval_coded ?
1028 				param->interval_coded :
1029 				param->interval;
1030 			uint16_t window = param->window_coded ?
1031 				param->window_coded :
1032 				param->window;
1033 
1034 			param_coded.type = param->type;
1035 			param_coded.interval = sys_cpu_to_le16(interval);
1036 			param_coded.window = sys_cpu_to_le16(window);
1037 			phy_coded = &param_coded;
1038 		}
1039 
1040 		err = start_le_scan_ext(phy_1m, phy_coded, param->timeout);
1041 	} else {
1042 		if (param->timeout) {
1043 			atomic_clear_bit(bt_dev.flags, BT_DEV_EXPLICIT_SCAN);
1044 			return -ENOTSUP;
1045 		}
1046 
1047 		err = start_le_scan_legacy(param->type, param->interval,
1048 					   param->window);
1049 	}
1050 
1051 	if (err) {
1052 		atomic_clear_bit(bt_dev.flags, BT_DEV_EXPLICIT_SCAN);
1053 		return err;
1054 	}
1055 
1056 	scan_dev_found_cb = cb;
1057 
1058 	return 0;
1059 }
1060 
bt_le_scan_stop(void)1061 int bt_le_scan_stop(void)
1062 {
1063 	/* Return if active scanning is already disabled */
1064 	if (!atomic_test_and_clear_bit(bt_dev.flags, BT_DEV_EXPLICIT_SCAN)) {
1065 		return -EALREADY;
1066 	}
1067 
1068 	scan_dev_found_cb = NULL;
1069 
1070 	if (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
1071 	    atomic_test_and_clear_bit(bt_dev.flags, BT_DEV_SCAN_LIMITED)) {
1072 		atomic_clear_bit(bt_dev.flags, BT_DEV_RPA_VALID);
1073 
1074 #if defined(CONFIG_BT_SMP)
1075 		bt_id_pending_keys_update();
1076 #endif
1077 	}
1078 
1079 	return bt_le_scan_update(false);
1080 }
1081 
bt_le_scan_cb_register(struct bt_le_scan_cb * cb)1082 void bt_le_scan_cb_register(struct bt_le_scan_cb *cb)
1083 {
1084 	sys_slist_append(&scan_cbs, &cb->node);
1085 }
1086 
bt_le_scan_cb_unregister(struct bt_le_scan_cb * cb)1087 void bt_le_scan_cb_unregister(struct bt_le_scan_cb *cb)
1088 {
1089 	sys_slist_find_and_remove(&scan_cbs, &cb->node);
1090 }
1091 
1092 #if defined(CONFIG_BT_PER_ADV_SYNC)
bt_le_per_adv_sync_get_index(struct bt_le_per_adv_sync * per_adv_sync)1093 uint8_t bt_le_per_adv_sync_get_index(struct bt_le_per_adv_sync *per_adv_sync)
1094 {
1095 	ptrdiff_t index = per_adv_sync - per_adv_sync_pool;
1096 
1097 	__ASSERT(index >= 0 && ARRAY_SIZE(per_adv_sync_pool) > index,
1098 		 "Invalid per_adv_sync pointer");
1099 	return (uint8_t)index;
1100 }
1101 
bt_le_per_adv_sync_get_info(struct bt_le_per_adv_sync * per_adv_sync,struct bt_le_per_adv_sync_info * info)1102 int bt_le_per_adv_sync_get_info(struct bt_le_per_adv_sync *per_adv_sync,
1103 				struct bt_le_per_adv_sync_info *info)
1104 {
1105 	CHECKIF(per_adv_sync == NULL || info == NULL) {
1106 		return -EINVAL;
1107 	}
1108 
1109 	bt_addr_le_copy(&info->addr, &per_adv_sync->addr);
1110 	info->sid = per_adv_sync->sid;
1111 	info->phy = per_adv_sync->phy;
1112 	info->interval = per_adv_sync->interval;
1113 
1114 	return 0;
1115 }
1116 
bt_le_per_adv_sync_lookup_addr(const bt_addr_le_t * adv_addr,uint8_t sid)1117 struct bt_le_per_adv_sync *bt_le_per_adv_sync_lookup_addr(const bt_addr_le_t *adv_addr,
1118 							  uint8_t sid)
1119 {
1120 	for (int i = 0; i < ARRAY_SIZE(per_adv_sync_pool); i++) {
1121 		struct bt_le_per_adv_sync *sync = &per_adv_sync_pool[i];
1122 
1123 		if (!atomic_test_bit(per_adv_sync_pool[i].flags,
1124 				     BT_PER_ADV_SYNC_CREATED)) {
1125 			continue;
1126 		}
1127 
1128 		if (!bt_addr_le_cmp(&sync->addr, adv_addr) && sync->sid == sid) {
1129 			return sync;
1130 		}
1131 	}
1132 
1133 	return NULL;
1134 }
1135 
bt_le_per_adv_sync_create(const struct bt_le_per_adv_sync_param * param,struct bt_le_per_adv_sync ** out_sync)1136 int bt_le_per_adv_sync_create(const struct bt_le_per_adv_sync_param *param,
1137 			      struct bt_le_per_adv_sync **out_sync)
1138 {
1139 	struct bt_hci_cp_le_per_adv_create_sync *cp;
1140 	struct net_buf *buf;
1141 	struct bt_le_per_adv_sync *per_adv_sync;
1142 	int err;
1143 
1144 	if (!BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
1145 		return -ENOTSUP;
1146 	}
1147 
1148 	if (get_pending_per_adv_sync()) {
1149 		return -EBUSY;
1150 	}
1151 
1152 	if (param->sid > BT_GAP_SID_MAX ||
1153 		   param->skip > BT_GAP_PER_ADV_MAX_SKIP ||
1154 		   param->timeout > BT_GAP_PER_ADV_MAX_TIMEOUT ||
1155 		   param->timeout < BT_GAP_PER_ADV_MIN_TIMEOUT) {
1156 		return -EINVAL;
1157 	}
1158 
1159 	per_adv_sync = per_adv_sync_new();
1160 	if (!per_adv_sync) {
1161 		return -ENOMEM;
1162 	}
1163 
1164 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_PER_ADV_CREATE_SYNC, sizeof(*cp));
1165 	if (!buf) {
1166 		per_adv_sync_delete(per_adv_sync);
1167 		return -ENOBUFS;
1168 	}
1169 
1170 	cp = net_buf_add(buf, sizeof(*cp));
1171 	(void)memset(cp, 0, sizeof(*cp));
1172 
1173 
1174 	bt_addr_le_copy(&cp->addr, &param->addr);
1175 
1176 	if (param->options & BT_LE_PER_ADV_SYNC_OPT_USE_PER_ADV_LIST) {
1177 		atomic_set_bit(per_adv_sync->flags,
1178 			       BT_PER_ADV_SYNC_SYNCING_USE_LIST);
1179 
1180 		cp->options |= BT_HCI_LE_PER_ADV_CREATE_SYNC_FP_USE_LIST;
1181 	}
1182 
1183 	if (param->options & BT_LE_PER_ADV_SYNC_OPT_DONT_SYNC_AOA) {
1184 		cp->cte_type |= BT_HCI_LE_PER_ADV_CREATE_SYNC_CTE_TYPE_NO_AOA;
1185 	}
1186 
1187 	if (param->options & BT_LE_PER_ADV_SYNC_OPT_DONT_SYNC_AOD_1US) {
1188 		cp->cte_type |=
1189 			BT_HCI_LE_PER_ADV_CREATE_SYNC_CTE_TYPE_NO_AOD_1US;
1190 	}
1191 
1192 	if (param->options & BT_LE_PER_ADV_SYNC_OPT_DONT_SYNC_AOD_2US) {
1193 		cp->cte_type |=
1194 			BT_HCI_LE_PER_ADV_CREATE_SYNC_CTE_TYPE_NO_AOD_2US;
1195 	}
1196 
1197 	if (param->options & BT_LE_PER_ADV_SYNC_OPT_SYNC_ONLY_CONST_TONE_EXT) {
1198 		cp->cte_type |= BT_HCI_LE_PER_ADV_CREATE_SYNC_CTE_TYPE_ONLY_CTE;
1199 	}
1200 
1201 	if (param->options &
1202 	    BT_LE_PER_ADV_SYNC_OPT_REPORTING_INITIALLY_DISABLED) {
1203 		cp->options |=
1204 			BT_HCI_LE_PER_ADV_CREATE_SYNC_FP_REPORTS_DISABLED;
1205 
1206 		atomic_set_bit(per_adv_sync->flags,
1207 			       BT_PER_ADV_SYNC_RECV_DISABLED);
1208 	}
1209 
1210 	cp->sid = param->sid;
1211 	cp->skip = sys_cpu_to_le16(param->skip);
1212 	cp->sync_timeout = sys_cpu_to_le16(param->timeout);
1213 
1214 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_PER_ADV_CREATE_SYNC, buf, NULL);
1215 	if (err) {
1216 		per_adv_sync_delete(per_adv_sync);
1217 		return err;
1218 	}
1219 
1220 	atomic_set_bit(per_adv_sync->flags, BT_PER_ADV_SYNC_SYNCING);
1221 
1222 	/* Syncing requires that scan is enabled. If the caller doesn't enable
1223 	 * scan first, we enable it here, and disable it once the sync has been
1224 	 * established. We don't need to use any callbacks since we rely on
1225 	 * the advertiser address in the sync params.
1226 	 */
1227 	if (!atomic_test_bit(bt_dev.flags, BT_DEV_SCANNING)) {
1228 		err = bt_le_scan_update(true);
1229 
1230 		if (err) {
1231 			bt_le_per_adv_sync_delete(per_adv_sync);
1232 			return err;
1233 		}
1234 	}
1235 
1236 	*out_sync = per_adv_sync;
1237 	bt_addr_le_copy(&per_adv_sync->addr, &param->addr);
1238 	per_adv_sync->sid = param->sid;
1239 
1240 	return 0;
1241 }
1242 
bt_le_per_adv_sync_create_cancel(struct bt_le_per_adv_sync * per_adv_sync)1243 static int bt_le_per_adv_sync_create_cancel(
1244 	struct bt_le_per_adv_sync *per_adv_sync)
1245 {
1246 	struct net_buf *buf;
1247 	int err;
1248 
1249 	if (get_pending_per_adv_sync() != per_adv_sync) {
1250 		return -EINVAL;
1251 	}
1252 
1253 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_PER_ADV_CREATE_SYNC_CANCEL, 0);
1254 	if (!buf) {
1255 		return -ENOBUFS;
1256 	}
1257 
1258 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_PER_ADV_CREATE_SYNC_CANCEL, buf,
1259 				   NULL);
1260 	if (err) {
1261 		return err;
1262 	}
1263 
1264 	return 0;
1265 }
1266 
bt_le_per_adv_sync_terminate(struct bt_le_per_adv_sync * per_adv_sync)1267 static int bt_le_per_adv_sync_terminate(struct bt_le_per_adv_sync *per_adv_sync)
1268 {
1269 	int err;
1270 
1271 	if (!atomic_test_bit(per_adv_sync->flags, BT_PER_ADV_SYNC_SYNCED)) {
1272 		return -EINVAL;
1273 	}
1274 
1275 	err = per_adv_sync_terminate(per_adv_sync->handle);
1276 
1277 	if (err) {
1278 		return err;
1279 	}
1280 
1281 	return 0;
1282 }
1283 
bt_le_per_adv_sync_delete(struct bt_le_per_adv_sync * per_adv_sync)1284 int bt_le_per_adv_sync_delete(struct bt_le_per_adv_sync *per_adv_sync)
1285 {
1286 	int err = 0;
1287 
1288 	if (!BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
1289 		return -ENOTSUP;
1290 	}
1291 
1292 	if (atomic_test_bit(per_adv_sync->flags, BT_PER_ADV_SYNC_SYNCED)) {
1293 		err = bt_le_per_adv_sync_terminate(per_adv_sync);
1294 
1295 		if (!err) {
1296 			per_adv_sync_delete(per_adv_sync);
1297 		}
1298 	} else if (get_pending_per_adv_sync() == per_adv_sync) {
1299 		err = bt_le_per_adv_sync_create_cancel(per_adv_sync);
1300 		/* Delete of the per_adv_sync will be done in the event
1301 		 * handler when cancelling.
1302 		 */
1303 	}
1304 
1305 	return err;
1306 }
1307 
bt_le_per_adv_sync_cb_register(struct bt_le_per_adv_sync_cb * cb)1308 void bt_le_per_adv_sync_cb_register(struct bt_le_per_adv_sync_cb *cb)
1309 {
1310 	sys_slist_append(&pa_sync_cbs, &cb->node);
1311 }
1312 
bt_le_set_per_adv_recv_enable(struct bt_le_per_adv_sync * per_adv_sync,bool enable)1313 static int bt_le_set_per_adv_recv_enable(
1314 	struct bt_le_per_adv_sync *per_adv_sync, bool enable)
1315 {
1316 	struct bt_hci_cp_le_set_per_adv_recv_enable *cp;
1317 	struct bt_le_per_adv_sync_cb *listener;
1318 	struct bt_le_per_adv_sync_state_info info;
1319 	struct net_buf *buf;
1320 	struct bt_hci_cmd_state_set state;
1321 	int err;
1322 
1323 	if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
1324 		return -EAGAIN;
1325 	}
1326 
1327 	if (!BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
1328 		return -ENOTSUP;
1329 	}
1330 
1331 	if (!atomic_test_bit(per_adv_sync->flags, BT_PER_ADV_SYNC_SYNCED)) {
1332 		return -EINVAL;
1333 	}
1334 
1335 	if ((enable && !atomic_test_bit(per_adv_sync->flags,
1336 					BT_PER_ADV_SYNC_RECV_DISABLED)) ||
1337 	    (!enable && atomic_test_bit(per_adv_sync->flags,
1338 					BT_PER_ADV_SYNC_RECV_DISABLED))) {
1339 		return -EALREADY;
1340 	}
1341 
1342 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_PER_ADV_RECV_ENABLE,
1343 				sizeof(*cp));
1344 	if (!buf) {
1345 		return -ENOBUFS;
1346 	}
1347 
1348 	cp = net_buf_add(buf, sizeof(*cp));
1349 	(void)memset(cp, 0, sizeof(*cp));
1350 
1351 	cp->handle = sys_cpu_to_le16(per_adv_sync->handle);
1352 	cp->enable = enable ? 1 : 0;
1353 
1354 	bt_hci_cmd_state_set_init(buf, &state, per_adv_sync->flags,
1355 				  BT_PER_ADV_SYNC_RECV_DISABLED,
1356 				  enable);
1357 
1358 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_PER_ADV_RECV_ENABLE,
1359 				   buf, NULL);
1360 
1361 	if (err) {
1362 		return err;
1363 	}
1364 
1365 	info.recv_enabled = !atomic_test_bit(per_adv_sync->flags,
1366 					     BT_PER_ADV_SYNC_RECV_DISABLED);
1367 
1368 	SYS_SLIST_FOR_EACH_CONTAINER(&pa_sync_cbs, listener, node) {
1369 		if (listener->state_changed) {
1370 			listener->state_changed(per_adv_sync, &info);
1371 		}
1372 	}
1373 
1374 	return 0;
1375 }
1376 
bt_le_per_adv_sync_recv_enable(struct bt_le_per_adv_sync * per_adv_sync)1377 int bt_le_per_adv_sync_recv_enable(struct bt_le_per_adv_sync *per_adv_sync)
1378 {
1379 	return bt_le_set_per_adv_recv_enable(per_adv_sync, true);
1380 }
1381 
bt_le_per_adv_sync_recv_disable(struct bt_le_per_adv_sync * per_adv_sync)1382 int bt_le_per_adv_sync_recv_disable(struct bt_le_per_adv_sync *per_adv_sync)
1383 {
1384 	return bt_le_set_per_adv_recv_enable(per_adv_sync, false);
1385 }
1386 
1387 #if defined(CONFIG_BT_CONN)
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)1388 int bt_le_per_adv_sync_transfer(const struct bt_le_per_adv_sync *per_adv_sync,
1389 				const struct bt_conn *conn,
1390 				uint16_t service_data)
1391 {
1392 	struct bt_hci_cp_le_per_adv_sync_transfer *cp;
1393 	struct net_buf *buf;
1394 
1395 
1396 	if (!BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
1397 		return -ENOTSUP;
1398 	} else if (!BT_FEAT_LE_PAST_SEND(bt_dev.le.features)) {
1399 		return -ENOTSUP;
1400 	}
1401 
1402 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_PER_ADV_SYNC_TRANSFER,
1403 				sizeof(*cp));
1404 	if (!buf) {
1405 		return -ENOBUFS;
1406 	}
1407 
1408 	cp = net_buf_add(buf, sizeof(*cp));
1409 	(void)memset(cp, 0, sizeof(*cp));
1410 
1411 	cp->conn_handle = sys_cpu_to_le16(conn->handle);
1412 	cp->sync_handle = sys_cpu_to_le16(per_adv_sync->handle);
1413 	cp->service_data = sys_cpu_to_le16(service_data);
1414 
1415 	return bt_hci_cmd_send_sync(BT_HCI_OP_LE_PER_ADV_SYNC_TRANSFER, buf,
1416 				    NULL);
1417 }
1418 
valid_past_param(const struct bt_le_per_adv_sync_transfer_param * param)1419 static bool valid_past_param(
1420 	const struct bt_le_per_adv_sync_transfer_param *param)
1421 {
1422 	if (param->skip > 0x01f3 ||
1423 	    param->timeout < 0x000A ||
1424 	    param->timeout > 0x4000) {
1425 		return false;
1426 	}
1427 
1428 	return true;
1429 }
1430 
past_param_set(const struct bt_conn * conn,uint8_t mode,uint16_t skip,uint16_t timeout,uint8_t cte_type)1431 static int past_param_set(const struct bt_conn *conn, uint8_t mode,
1432 			  uint16_t skip, uint16_t timeout, uint8_t cte_type)
1433 {
1434 	struct bt_hci_cp_le_past_param *cp;
1435 	struct net_buf *buf;
1436 
1437 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_PAST_PARAM, sizeof(*cp));
1438 	if (!buf) {
1439 		return -ENOBUFS;
1440 	}
1441 
1442 	cp = net_buf_add(buf, sizeof(*cp));
1443 	(void)memset(cp, 0, sizeof(*cp));
1444 
1445 	cp->conn_handle = sys_cpu_to_le16(conn->handle);
1446 	cp->mode = mode;
1447 	cp->skip = sys_cpu_to_le16(skip);
1448 	cp->timeout = sys_cpu_to_le16(timeout);
1449 	cp->cte_type = cte_type;
1450 
1451 	return bt_hci_cmd_send_sync(BT_HCI_OP_LE_PAST_PARAM, buf, NULL);
1452 }
1453 
default_past_param_set(uint8_t mode,uint16_t skip,uint16_t timeout,uint8_t cte_type)1454 static int default_past_param_set(uint8_t mode, uint16_t skip, uint16_t timeout,
1455 				  uint8_t cte_type)
1456 {
1457 	struct bt_hci_cp_le_default_past_param *cp;
1458 	struct net_buf *buf;
1459 
1460 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_DEFAULT_PAST_PARAM, sizeof(*cp));
1461 	if (!buf) {
1462 		return -ENOBUFS;
1463 	}
1464 
1465 	cp = net_buf_add(buf, sizeof(*cp));
1466 	(void)memset(cp, 0, sizeof(*cp));
1467 
1468 	cp->mode = mode;
1469 	cp->skip = sys_cpu_to_le16(skip);
1470 	cp->timeout = sys_cpu_to_le16(timeout);
1471 	cp->cte_type = cte_type;
1472 
1473 	return bt_hci_cmd_send_sync(BT_HCI_OP_LE_DEFAULT_PAST_PARAM, buf, NULL);
1474 }
1475 
bt_le_per_adv_sync_transfer_subscribe(const struct bt_conn * conn,const struct bt_le_per_adv_sync_transfer_param * param)1476 int bt_le_per_adv_sync_transfer_subscribe(
1477 	const struct bt_conn *conn,
1478 	const struct bt_le_per_adv_sync_transfer_param *param)
1479 {
1480 	uint8_t cte_type = 0;
1481 
1482 	if (!BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
1483 		return -ENOTSUP;
1484 	} else if (!BT_FEAT_LE_PAST_RECV(bt_dev.le.features)) {
1485 		return -ENOTSUP;
1486 	}
1487 
1488 	if (!valid_past_param(param)) {
1489 		return -EINVAL;
1490 	}
1491 
1492 	if (param->options & BT_LE_PER_ADV_SYNC_TRANSFER_OPT_SYNC_NO_AOA) {
1493 		cte_type |= BT_HCI_LE_PAST_CTE_TYPE_NO_AOA;
1494 	}
1495 
1496 	if (param->options & BT_LE_PER_ADV_SYNC_TRANSFER_OPT_SYNC_NO_AOD_1US) {
1497 		cte_type |= BT_HCI_LE_PAST_CTE_TYPE_NO_AOD_1US;
1498 	}
1499 
1500 	if (param->options & BT_LE_PER_ADV_SYNC_TRANSFER_OPT_SYNC_NO_AOD_2US) {
1501 		cte_type |= BT_HCI_LE_PAST_CTE_TYPE_NO_AOD_2US;
1502 	}
1503 
1504 	if (param->options & BT_LE_PER_ADV_SYNC_TRANSFER_OPT_SYNC_ONLY_CTE) {
1505 		cte_type |= BT_HCI_LE_PAST_CTE_TYPE_ONLY_CTE;
1506 	}
1507 
1508 	if (conn) {
1509 		return past_param_set(conn, BT_HCI_LE_PAST_MODE_SYNC,
1510 				      param->skip, param->timeout, cte_type);
1511 	} else {
1512 		return default_past_param_set(BT_HCI_LE_PAST_MODE_SYNC,
1513 					      param->skip, param->timeout,
1514 					      cte_type);
1515 	}
1516 }
1517 
bt_le_per_adv_sync_transfer_unsubscribe(const struct bt_conn * conn)1518 int bt_le_per_adv_sync_transfer_unsubscribe(const struct bt_conn *conn)
1519 {
1520 	if (!BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
1521 		return -ENOTSUP;
1522 	} else if (!BT_FEAT_LE_PAST_RECV(bt_dev.le.features)) {
1523 		return -ENOTSUP;
1524 	}
1525 
1526 	if (conn) {
1527 		return past_param_set(conn, BT_HCI_LE_PAST_MODE_NO_SYNC, 0,
1528 				      0x0a, 0);
1529 	} else {
1530 		return default_past_param_set(BT_HCI_LE_PAST_MODE_NO_SYNC, 0,
1531 					      0x0a, 0);
1532 	}
1533 }
1534 #endif /* CONFIG_BT_CONN */
1535 
bt_le_per_adv_list_add(const bt_addr_le_t * addr,uint8_t sid)1536 int bt_le_per_adv_list_add(const bt_addr_le_t *addr, uint8_t sid)
1537 {
1538 	struct bt_hci_cp_le_add_dev_to_per_adv_list *cp;
1539 	struct net_buf *buf;
1540 	int err;
1541 
1542 	if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
1543 		return -EAGAIN;
1544 	}
1545 
1546 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_ADD_DEV_TO_PER_ADV_LIST,
1547 				sizeof(*cp));
1548 	if (!buf) {
1549 		return -ENOBUFS;
1550 	}
1551 
1552 	cp = net_buf_add(buf, sizeof(*cp));
1553 	bt_addr_le_copy(&cp->addr, addr);
1554 	cp->sid = sid;
1555 
1556 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_ADD_DEV_TO_PER_ADV_LIST, buf,
1557 				   NULL);
1558 	if (err) {
1559 		BT_ERR("Failed to add device to periodic advertiser list");
1560 
1561 		return err;
1562 	}
1563 
1564 	return 0;
1565 }
1566 
bt_le_per_adv_list_remove(const bt_addr_le_t * addr,uint8_t sid)1567 int bt_le_per_adv_list_remove(const bt_addr_le_t *addr, uint8_t sid)
1568 {
1569 	struct bt_hci_cp_le_rem_dev_from_per_adv_list *cp;
1570 	struct net_buf *buf;
1571 	int err;
1572 
1573 	if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
1574 		return -EAGAIN;
1575 	}
1576 
1577 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_REM_DEV_FROM_PER_ADV_LIST,
1578 				sizeof(*cp));
1579 	if (!buf) {
1580 		return -ENOBUFS;
1581 	}
1582 
1583 	cp = net_buf_add(buf, sizeof(*cp));
1584 	bt_addr_le_copy(&cp->addr, addr);
1585 	cp->sid = sid;
1586 
1587 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_REM_DEV_FROM_PER_ADV_LIST, buf,
1588 				   NULL);
1589 	if (err) {
1590 		BT_ERR("Failed to remove device from periodic advertiser list");
1591 		return err;
1592 	}
1593 
1594 	return 0;
1595 }
1596 
bt_le_per_adv_list_clear(void)1597 int bt_le_per_adv_list_clear(void)
1598 {
1599 	int err;
1600 
1601 	if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
1602 		return -EAGAIN;
1603 	}
1604 
1605 	err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_CLEAR_PER_ADV_LIST, NULL, NULL);
1606 	if (err) {
1607 		BT_ERR("Failed to clear periodic advertiser list");
1608 		return err;
1609 	}
1610 
1611 	return 0;
1612 }
1613 #endif /* defined(CONFIG_BT_PER_ADV_SYNC) */
1614