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