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 evt_type field to adv props */
get_adv_props_extended(uint16_t evt_type)745 static uint16_t get_adv_props_extended(uint16_t evt_type)
746 {
747 /* Converts from BT_HCI_LE_ADV_EVT_TYPE_* to BT_GAP_ADV_PROP_*
748 * The first 4 bits are the same (conn, scan, direct, scan_rsp).
749 * Bit 4 must be flipped as the meaning of 1 is opposite (legacy -> extended)
750 * The rest of the bits are zeroed out.
751 */
752 return (evt_type ^ BT_HCI_LE_ADV_EVT_TYPE_LEGACY) & BIT_MASK(5);
753 }
754
create_ext_adv_info(struct bt_hci_evt_le_ext_advertising_info const * const evt,struct bt_le_scan_recv_info * const scan_info)755 static void create_ext_adv_info(struct bt_hci_evt_le_ext_advertising_info const *const evt,
756 struct bt_le_scan_recv_info *const scan_info)
757 {
758 scan_info->primary_phy = bt_get_phy(evt->prim_phy);
759 scan_info->secondary_phy = bt_get_phy(evt->sec_phy);
760 scan_info->tx_power = evt->tx_power;
761 scan_info->rssi = evt->rssi;
762 scan_info->sid = evt->sid;
763 scan_info->interval = sys_le16_to_cpu(evt->interval);
764 scan_info->adv_type = get_adv_type(sys_le16_to_cpu(evt->evt_type));
765 scan_info->adv_props = get_adv_props_extended(sys_le16_to_cpu(evt->evt_type));
766 }
767
bt_hci_le_adv_ext_report(struct net_buf * buf)768 void bt_hci_le_adv_ext_report(struct net_buf *buf)
769 {
770 uint8_t num_reports = net_buf_pull_u8(buf);
771
772 LOG_DBG("Adv number of reports %u", num_reports);
773
774 while (num_reports--) {
775 struct bt_hci_evt_le_ext_advertising_info *evt;
776 struct bt_le_scan_recv_info scan_info;
777 uint16_t data_status;
778 uint16_t evt_type;
779 bool is_report_complete;
780 bool more_to_come;
781 bool is_new_advertiser;
782
783 if (!atomic_test_bit(scan_state.scan_flags, BT_LE_SCAN_USER_EXPLICIT_SCAN)) {
784 /* The application has not requested explicit scan, so it is not expecting
785 * advertising reports. Discard, and reset the reassembler if not inactive
786 * This is done in the loop as this flag can change between each iteration,
787 * and it is not uncommon that scanning is disabled in the callback called
788 * from le_adv_recv
789 */
790
791 if (reassembling_advertiser.state != FRAG_ADV_INACTIVE) {
792 reset_reassembling_advertiser();
793 }
794
795 break;
796 }
797
798 if (buf->len < sizeof(*evt)) {
799 LOG_ERR("Unexpected end of buffer");
800 break;
801 }
802
803 evt = net_buf_pull_mem(buf, sizeof(*evt));
804 evt_type = sys_le16_to_cpu(evt->evt_type);
805 data_status = BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS(evt_type);
806 is_report_complete = data_status == BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_COMPLETE;
807 more_to_come = data_status == BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_PARTIAL;
808
809 if (evt->length > buf->len) {
810 LOG_WRN("Adv report corrupted (wants %u out of %u)", evt->length, buf->len);
811
812 net_buf_reset(buf);
813
814 if (evt_type & BT_HCI_LE_ADV_EVT_TYPE_LEGACY) {
815 return;
816 }
817
818 /* Start discarding irrespective of the `more_to_come` flag. We
819 * assume we may have lost a partial adv report in the truncated
820 * data.
821 */
822 reassembling_advertiser.state = FRAG_ADV_DISCARDING;
823
824 return;
825 }
826
827 if (evt_type & BT_HCI_LE_ADV_EVT_TYPE_LEGACY) {
828 /* Legacy advertising reports are complete.
829 * Create event immediately.
830 */
831 create_ext_adv_info(evt, &scan_info);
832 le_adv_recv(&evt->addr, &scan_info, &buf->b, evt->length);
833 goto cont;
834 }
835
836 is_new_advertiser = reassembling_advertiser.state == FRAG_ADV_INACTIVE ||
837 !fragmented_advertisers_equal(&reassembling_advertiser,
838 &evt->addr, evt->sid);
839
840 if (is_new_advertiser && is_report_complete) {
841 /* Only advertising report from this advertiser.
842 * Create event immediately.
843 */
844 create_ext_adv_info(evt, &scan_info);
845 le_adv_recv(&evt->addr, &scan_info, &buf->b, evt->length);
846 goto cont;
847 }
848
849 if (is_new_advertiser && reassembling_advertiser.state == FRAG_ADV_REASSEMBLING) {
850 LOG_WRN("Received an incomplete advertising report while reassembling "
851 "advertising reports from a different advertiser. The advertising "
852 "report is discarded and future scan results may be incomplete. "
853 "Interleaving of fragmented advertising reports from different "
854 "advertisers is not yet supported.");
855 goto cont;
856 }
857
858 if (data_status == BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_INCOMPLETE) {
859 /* Got HCI_LE_Extended_Advertising_Report: Incomplete, data truncated, no
860 * more to come. This means the Controller is aborting the reassembly. We
861 * discard the partially received report, and the application is not
862 * notified.
863 *
864 * See the Controller's documentation for possible reasons for aborting.
865 * Hint: CONFIG_BT_CTLR_SCAN_DATA_LEN_MAX.
866 */
867 LOG_DBG("Discarding incomplete advertisement.");
868 reset_reassembling_advertiser();
869 goto cont;
870 }
871
872 if (is_new_advertiser) {
873 /* We are not reassembling reports from an advertiser and
874 * this is the first report from the new advertiser.
875 * Initialize the new advertiser.
876 */
877 __ASSERT_NO_MSG(reassembling_advertiser.state == FRAG_ADV_INACTIVE);
878 init_reassembling_advertiser(&evt->addr, evt->sid);
879 }
880
881 if (evt->length + ext_scan_buf.len > ext_scan_buf.size) {
882 /* The report does not fit in the reassemby buffer
883 * Discard this and future reports from the advertiser.
884 */
885 reassembling_advertiser.state = FRAG_ADV_DISCARDING;
886 }
887
888 if (reassembling_advertiser.state == FRAG_ADV_DISCARDING) {
889 if (!more_to_come) {
890 /* We do no longer need to keep track of this advertiser as
891 * all the expected data is received.
892 */
893 reset_reassembling_advertiser();
894 }
895 goto cont;
896 }
897
898 net_buf_simple_add_mem(&ext_scan_buf, buf->data, evt->length);
899 if (more_to_come) {
900 /* The controller will send additional reports to be reassembled */
901 continue;
902 }
903
904 /* No more data coming from the controller.
905 * Create event.
906 */
907 __ASSERT_NO_MSG(is_report_complete);
908 create_ext_adv_info(evt, &scan_info);
909 le_adv_recv(&evt->addr, &scan_info, &ext_scan_buf, ext_scan_buf.len);
910
911 /* We do no longer need to keep track of this advertiser. */
912 reset_reassembling_advertiser();
913
914 cont:
915 net_buf_pull(buf, evt->length);
916 }
917 }
918
919 #if defined(CONFIG_BT_PER_ADV_SYNC)
per_adv_sync_delete(struct bt_le_per_adv_sync * per_adv_sync)920 static void per_adv_sync_delete(struct bt_le_per_adv_sync *per_adv_sync)
921 {
922 atomic_clear(per_adv_sync->flags);
923 }
924
per_adv_sync_new(void)925 static struct bt_le_per_adv_sync *per_adv_sync_new(void)
926 {
927 struct bt_le_per_adv_sync *per_adv_sync = NULL;
928
929 for (int i = 0; i < ARRAY_SIZE(per_adv_sync_pool); i++) {
930 if (!atomic_test_bit(per_adv_sync_pool[i].flags,
931 BT_PER_ADV_SYNC_CREATED)) {
932 per_adv_sync = &per_adv_sync_pool[i];
933 break;
934 }
935 }
936
937 if (!per_adv_sync) {
938 return NULL;
939 }
940
941 (void)memset(per_adv_sync, 0, sizeof(*per_adv_sync));
942 atomic_set_bit(per_adv_sync->flags, BT_PER_ADV_SYNC_CREATED);
943
944 #if CONFIG_BT_PER_ADV_SYNC_BUF_SIZE > 0
945 net_buf_simple_init_with_data(&per_adv_sync->reassembly,
946 per_adv_sync->reassembly_data,
947 CONFIG_BT_PER_ADV_SYNC_BUF_SIZE);
948 net_buf_simple_reset(&per_adv_sync->reassembly);
949 #endif /* CONFIG_BT_PER_ADV_SYNC_BUF_SIZE > 0 */
950
951 return per_adv_sync;
952 }
953
get_pending_per_adv_sync(void)954 static struct bt_le_per_adv_sync *get_pending_per_adv_sync(void)
955 {
956 for (int i = 0; i < ARRAY_SIZE(per_adv_sync_pool); i++) {
957 if (atomic_test_bit(per_adv_sync_pool[i].flags,
958 BT_PER_ADV_SYNC_SYNCING)) {
959 return &per_adv_sync_pool[i];
960 }
961 }
962
963 return NULL;
964 }
965
bt_periodic_sync_disable(void)966 void bt_periodic_sync_disable(void)
967 {
968 for (size_t i = 0; i < ARRAY_SIZE(per_adv_sync_pool); i++) {
969 per_adv_sync_delete(&per_adv_sync_pool[i]);
970 }
971 }
972
bt_hci_per_adv_sync_lookup_handle(uint16_t handle)973 struct bt_le_per_adv_sync *bt_hci_per_adv_sync_lookup_handle(uint16_t handle)
974 {
975 for (int i = 0; i < ARRAY_SIZE(per_adv_sync_pool); i++) {
976 if (per_adv_sync_pool[i].handle == handle &&
977 atomic_test_bit(per_adv_sync_pool[i].flags,
978 BT_PER_ADV_SYNC_SYNCED)) {
979 return &per_adv_sync_pool[i];
980 }
981 }
982
983 return NULL;
984 }
985
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)986 void bt_hci_le_per_adv_report_recv(struct bt_le_per_adv_sync *per_adv_sync,
987 struct net_buf_simple *buf,
988 const struct bt_le_per_adv_sync_recv_info *info)
989 {
990 struct net_buf_simple_state state;
991 struct bt_le_per_adv_sync_cb *listener;
992
993 SYS_SLIST_FOR_EACH_CONTAINER(&pa_sync_cbs, listener, node) {
994 if (listener->recv) {
995 net_buf_simple_save(buf, &state);
996 listener->recv(per_adv_sync, info, buf);
997 net_buf_simple_restore(buf, &state);
998 }
999 }
1000 }
1001
1002 #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)1003 static void bt_hci_le_per_adv_report_recv_failure(struct bt_le_per_adv_sync *per_adv_sync,
1004 const struct bt_le_per_adv_sync_recv_info *info)
1005 {
1006 struct bt_le_per_adv_sync_cb *listener;
1007
1008 SYS_SLIST_FOR_EACH_CONTAINER(&pa_sync_cbs, listener, node) {
1009 if (listener->recv) {
1010 listener->recv(per_adv_sync, info, NULL);
1011 }
1012 }
1013 }
1014 #endif /* defined(CONFIG_BT_PER_ADV_SYNC_RSP) && (CONFIG_BT_PER_ADV_SYNC_BUF_SIZE > 0) */
1015
bt_hci_le_per_adv_report_common(struct net_buf * buf)1016 static void bt_hci_le_per_adv_report_common(struct net_buf *buf)
1017 {
1018 #if defined(CONFIG_BT_PER_ADV_SYNC_RSP)
1019 struct bt_hci_evt_le_per_advertising_report_v2 *evt;
1020 #else
1021 struct bt_hci_evt_le_per_advertising_report *evt;
1022 #endif /* defined(CONFIG_BT_PER_ADV_SYNC_RSP) */
1023
1024 struct bt_le_per_adv_sync *per_adv_sync;
1025 struct bt_le_per_adv_sync_recv_info info;
1026
1027 if (buf->len < sizeof(*evt)) {
1028 LOG_ERR("Unexpected end of buffer");
1029 return;
1030 }
1031
1032 evt = net_buf_pull_mem(buf, sizeof(*evt));
1033
1034 per_adv_sync = bt_hci_per_adv_sync_lookup_handle(sys_le16_to_cpu(evt->handle));
1035
1036 if (!per_adv_sync) {
1037 LOG_ERR("Unknown handle 0x%04X for periodic advertising report",
1038 sys_le16_to_cpu(evt->handle));
1039 return;
1040 }
1041
1042 if (atomic_test_bit(per_adv_sync->flags,
1043 BT_PER_ADV_SYNC_RECV_DISABLED)) {
1044 LOG_ERR("Received PA adv report when receive disabled");
1045 return;
1046 }
1047
1048 info.tx_power = evt->tx_power;
1049 info.rssi = evt->rssi;
1050 info.cte_type = bt_get_df_cte_type(evt->cte_type);
1051 info.addr = &per_adv_sync->addr;
1052 info.sid = per_adv_sync->sid;
1053
1054 #if defined(CONFIG_BT_PER_ADV_SYNC_RSP)
1055 info.periodic_event_counter = sys_le16_to_cpu(evt->periodic_event_counter);
1056 info.subevent = evt->subevent;
1057 #endif /* CONFIG_BT_PER_ADV_SYNC_RSP */
1058
1059 if (!per_adv_sync->report_truncated) {
1060 #if CONFIG_BT_PER_ADV_SYNC_BUF_SIZE > 0
1061 if (net_buf_simple_tailroom(&per_adv_sync->reassembly) < evt->length) {
1062 /* The buffer is too small for the entire report. Drop it */
1063 LOG_WRN("Buffer is too small to reassemble the report. "
1064 "Use CONFIG_BT_PER_ADV_SYNC_BUF_SIZE to change "
1065 "the buffer size.");
1066
1067 per_adv_sync->report_truncated = true;
1068 net_buf_simple_reset(&per_adv_sync->reassembly);
1069 return;
1070 }
1071
1072 if (evt->data_status == BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_COMPLETE) {
1073 if (per_adv_sync->reassembly.len == 0) {
1074 /* We have not received any partial data before.
1075 * This buffer can be forwarded without an extra copy.
1076 */
1077 bt_hci_le_per_adv_report_recv(per_adv_sync, &buf->b, &info);
1078 } else {
1079 net_buf_simple_add_mem(&per_adv_sync->reassembly,
1080 buf->data, evt->length);
1081 bt_hci_le_per_adv_report_recv(per_adv_sync,
1082 &per_adv_sync->reassembly, &info);
1083 net_buf_simple_reset(&per_adv_sync->reassembly);
1084 }
1085 } else if (evt->data_status == BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_INCOMPLETE) {
1086 LOG_DBG("Received incomplete advertising data. "
1087 "Advertising report dropped.");
1088
1089 net_buf_simple_reset(&per_adv_sync->reassembly);
1090
1091 } else if (evt->data_status == BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_PARTIAL) {
1092 net_buf_simple_add_mem(&per_adv_sync->reassembly, buf->data, evt->length);
1093 #if defined(CONFIG_BT_PER_ADV_SYNC_RSP)
1094 } else if (evt->data_status == BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_RX_FAILED &&
1095 per_adv_sync->num_subevents) {
1096 bt_hci_le_per_adv_report_recv_failure(per_adv_sync, &info);
1097 #endif /* CONFIG_BT_PER_ADV_SYNC_RSP */
1098 } else {
1099 __ASSERT(false, "Invalid data status 0x%02X", evt->data_status);
1100 }
1101 #else /* CONFIG_BT_PER_ADV_SYNC_BUF_SIZE > 0 */
1102 if (evt->data_status == BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_COMPLETE) {
1103 bt_hci_le_per_adv_report_recv(per_adv_sync, &buf->b, &info);
1104 } else {
1105 per_adv_sync->report_truncated = true;
1106 }
1107 #endif /* CONFIG_BT_PER_ADV_SYNC_BUF_SIZE > 0 */
1108 } else if (evt->data_status == BT_HCI_LE_ADV_EVT_TYPE_DATA_STATUS_COMPLETE) {
1109 per_adv_sync->report_truncated = false;
1110 }
1111 }
1112
bt_hci_le_per_adv_report(struct net_buf * buf)1113 void bt_hci_le_per_adv_report(struct net_buf *buf)
1114 {
1115 if (IS_ENABLED(CONFIG_BT_PER_ADV_SYNC_RSP)) {
1116 LOG_ERR("The controller shall raise the latest unmasked version of the event");
1117
1118 return;
1119 }
1120
1121 bt_hci_le_per_adv_report_common(buf);
1122 }
1123
per_adv_sync_terminate(uint16_t handle)1124 static int per_adv_sync_terminate(uint16_t handle)
1125 {
1126 struct bt_hci_cp_le_per_adv_terminate_sync *cp;
1127 struct net_buf *buf;
1128
1129 buf = bt_hci_cmd_create(BT_HCI_OP_LE_PER_ADV_TERMINATE_SYNC,
1130 sizeof(*cp));
1131 if (!buf) {
1132 return -ENOBUFS;
1133 }
1134
1135 cp = net_buf_add(buf, sizeof(*cp));
1136 (void)memset(cp, 0, sizeof(*cp));
1137
1138 cp->handle = sys_cpu_to_le16(handle);
1139
1140 return bt_hci_cmd_send_sync(BT_HCI_OP_LE_PER_ADV_TERMINATE_SYNC, buf,
1141 NULL);
1142 }
1143
per_adv_sync_terminated(struct bt_le_per_adv_sync * per_adv_sync,uint8_t reason)1144 static void per_adv_sync_terminated(struct bt_le_per_adv_sync *per_adv_sync,
1145 uint8_t reason)
1146 {
1147 /* Terminate the PA sync and notify app */
1148 const struct bt_le_per_adv_sync_term_info term_info = {
1149 .addr = &per_adv_sync->addr,
1150 .sid = per_adv_sync->sid,
1151 .reason = reason,
1152 };
1153 struct bt_le_per_adv_sync_cb *listener;
1154
1155 /* Deleting before callback, so the caller will be able
1156 * to restart sync in the callback.
1157 */
1158 per_adv_sync_delete(per_adv_sync);
1159
1160 SYS_SLIST_FOR_EACH_CONTAINER(&pa_sync_cbs, listener, node) {
1161 if (listener->term) {
1162 listener->term(per_adv_sync, &term_info);
1163 }
1164 }
1165 }
1166
bt_hci_le_per_adv_sync_established_common(struct net_buf * buf)1167 static void bt_hci_le_per_adv_sync_established_common(struct net_buf *buf)
1168 {
1169 #if defined(CONFIG_BT_PER_ADV_SYNC_RSP)
1170 struct bt_hci_evt_le_per_adv_sync_established_v2 *evt =
1171 (struct bt_hci_evt_le_per_adv_sync_established_v2 *)buf->data;
1172 #else
1173 struct bt_hci_evt_le_per_adv_sync_established *evt =
1174 (struct bt_hci_evt_le_per_adv_sync_established *)buf->data;
1175 #endif /* defined(CONFIG_BT_PER_ADV_SYNC_RSP) */
1176
1177 struct bt_le_per_adv_sync_synced_info sync_info;
1178 struct bt_le_per_adv_sync *pending_per_adv_sync;
1179 struct bt_le_per_adv_sync_cb *listener;
1180 bt_addr_le_t id_addr;
1181 bool unexpected_evt;
1182 int err;
1183
1184 pending_per_adv_sync = get_pending_per_adv_sync();
1185
1186 if (pending_per_adv_sync) {
1187 atomic_clear_bit(pending_per_adv_sync->flags, BT_PER_ADV_SYNC_SYNCING);
1188 err = bt_le_scan_user_remove(BT_LE_SCAN_USER_PER_SYNC);
1189
1190 if (err) {
1191 LOG_ERR("Could not update scan (%d)", err);
1192 }
1193 }
1194
1195 if (evt->status == BT_HCI_ERR_OP_CANCELLED_BY_HOST) {
1196 /* Cancelled locally, don't call CB */
1197 if (pending_per_adv_sync) {
1198 per_adv_sync_delete(pending_per_adv_sync);
1199 } else {
1200 LOG_ERR("Unexpected per adv sync cancelled event");
1201 }
1202
1203 return;
1204 }
1205
1206 if (bt_addr_le_is_resolved(&evt->adv_addr)) {
1207 bt_addr_le_copy_resolved(&id_addr, &evt->adv_addr);
1208 } else {
1209 bt_addr_le_copy(&id_addr,
1210 bt_lookup_id_addr(BT_ID_DEFAULT,
1211 &evt->adv_addr));
1212 }
1213
1214 if (!pending_per_adv_sync ||
1215 (!atomic_test_bit(pending_per_adv_sync->flags,
1216 BT_PER_ADV_SYNC_SYNCING_USE_LIST) &&
1217 ((pending_per_adv_sync->sid != evt->sid) ||
1218 !bt_addr_le_eq(&pending_per_adv_sync->addr, &id_addr)))) {
1219 LOG_ERR("Unexpected per adv sync established event");
1220 /* Request terminate of pending periodic advertising in controller */
1221 per_adv_sync_terminate(sys_le16_to_cpu(evt->handle));
1222
1223 unexpected_evt = true;
1224 } else {
1225 unexpected_evt = false;
1226 }
1227
1228 if (unexpected_evt || evt->status != BT_HCI_ERR_SUCCESS) {
1229 if (pending_per_adv_sync) {
1230 const uint8_t reason = unexpected_evt ? BT_HCI_ERR_UNSPECIFIED
1231 : evt->status;
1232
1233 if (atomic_test_bit(pending_per_adv_sync->flags,
1234 BT_PER_ADV_SYNC_SYNCING_USE_LIST)) {
1235 /* Update the addr and sid for the callback
1236 * Already set if not using the sync list
1237 */
1238 bt_addr_le_copy(&pending_per_adv_sync->addr,
1239 &id_addr);
1240 pending_per_adv_sync->sid = evt->sid;
1241 }
1242
1243 per_adv_sync_terminated(pending_per_adv_sync, reason);
1244 }
1245 return;
1246 }
1247
1248 pending_per_adv_sync->report_truncated = false;
1249
1250 atomic_set_bit(pending_per_adv_sync->flags, BT_PER_ADV_SYNC_SYNCED);
1251
1252 pending_per_adv_sync->handle = sys_le16_to_cpu(evt->handle);
1253 pending_per_adv_sync->interval = sys_le16_to_cpu(evt->interval);
1254 pending_per_adv_sync->clock_accuracy =
1255 sys_le16_to_cpu(evt->clock_accuracy);
1256 pending_per_adv_sync->phy = bt_get_phy(evt->phy);
1257
1258 memset(&sync_info, 0, sizeof(sync_info));
1259 sync_info.interval = pending_per_adv_sync->interval;
1260 sync_info.phy = pending_per_adv_sync->phy;
1261
1262 if (atomic_test_bit(pending_per_adv_sync->flags,
1263 BT_PER_ADV_SYNC_SYNCING_USE_LIST)) {
1264 /* Now we know which address and SID we synchronized to. */
1265 pending_per_adv_sync->sid = evt->sid;
1266
1267 if (bt_addr_le_is_resolved(&pending_per_adv_sync->addr)) {
1268 bt_addr_le_copy_resolved(&pending_per_adv_sync->addr,
1269 &id_addr);
1270 } else {
1271 bt_addr_le_copy(&pending_per_adv_sync->addr, &id_addr);
1272 }
1273 }
1274
1275 sync_info.addr = &pending_per_adv_sync->addr;
1276 sync_info.sid = pending_per_adv_sync->sid;
1277 #if defined(CONFIG_BT_PER_ADV_SYNC_RSP)
1278 sync_info.num_subevents = evt->num_subevents;
1279 sync_info.subevent_interval = evt->subevent_interval;
1280 sync_info.response_slot_delay = evt->response_slot_delay;
1281 sync_info.response_slot_spacing = evt->response_slot_spacing;
1282
1283 pending_per_adv_sync->num_subevents = evt->num_subevents;
1284 pending_per_adv_sync->subevent_interval = evt->subevent_interval;
1285 pending_per_adv_sync->response_slot_delay = evt->response_slot_delay;
1286 pending_per_adv_sync->response_slot_spacing = evt->response_slot_spacing;
1287 #endif /* CONFIG_BT_PER_ADV_SYNC_RSP */
1288
1289 sync_info.recv_enabled =
1290 !atomic_test_bit(pending_per_adv_sync->flags,
1291 BT_PER_ADV_SYNC_RECV_DISABLED);
1292
1293 SYS_SLIST_FOR_EACH_CONTAINER(&pa_sync_cbs, listener, node) {
1294 if (listener->synced) {
1295 listener->synced(pending_per_adv_sync, &sync_info);
1296 }
1297 }
1298 }
1299
bt_hci_le_per_adv_sync_established(struct net_buf * buf)1300 void bt_hci_le_per_adv_sync_established(struct net_buf *buf)
1301 {
1302 if (IS_ENABLED(CONFIG_BT_PER_ADV_SYNC_RSP)) {
1303 LOG_ERR("The controller shall raise the latest unmasked version of the event");
1304
1305 return;
1306 }
1307
1308 bt_hci_le_per_adv_sync_established_common(buf);
1309 }
1310
1311 #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)1312 int bt_le_per_adv_sync_subevent(struct bt_le_per_adv_sync *per_adv_sync,
1313 struct bt_le_per_adv_sync_subevent_params *params)
1314 {
1315 struct bt_hci_cp_le_set_pawr_sync_subevent *cp;
1316 struct net_buf *buf;
1317
1318 if (params->num_subevents > BT_HCI_PAWR_SUBEVENT_MAX) {
1319 return -EINVAL;
1320 }
1321
1322 buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_PER_ADV_SYNC_SUBEVENT,
1323 sizeof(*cp) + params->num_subevents);
1324
1325 if (!buf) {
1326 return -ENOBUFS;
1327 }
1328
1329 cp = net_buf_add(buf, sizeof(*cp));
1330 (void)memset(cp, 0, sizeof(*cp));
1331 cp->sync_handle = sys_cpu_to_le16(per_adv_sync->handle);
1332 cp->periodic_adv_properties = sys_cpu_to_le16(params->properties);
1333 cp->num_subevents = params->num_subevents;
1334 net_buf_add_mem(buf, params->subevents, cp->num_subevents);
1335
1336 return bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_PER_ADV_SYNC_SUBEVENT, buf, NULL);
1337 }
1338
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)1339 int bt_le_per_adv_set_response_data(struct bt_le_per_adv_sync *per_adv_sync,
1340 const struct bt_le_per_adv_response_params *param,
1341 const struct net_buf_simple *data)
1342 {
1343 struct bt_hci_cp_le_set_pawr_response_data *cp;
1344 struct net_buf *buf;
1345
1346 if (per_adv_sync->num_subevents == 0) {
1347 return -EINVAL;
1348 }
1349
1350 if (param->request_subevent >= per_adv_sync->num_subevents) {
1351 return -EINVAL;
1352 }
1353
1354 if (param->response_subevent >= per_adv_sync->num_subevents) {
1355 return -EINVAL;
1356 }
1357
1358 if (data->len > 247) {
1359 return -EINVAL;
1360 }
1361
1362 buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_PER_ADV_RESPONSE_DATA,
1363 sizeof(*cp) + data->len);
1364
1365 if (!buf) {
1366 return -ENOBUFS;
1367 }
1368
1369 cp = net_buf_add(buf, sizeof(*cp));
1370 (void)memset(cp, 0, sizeof(*cp));
1371 cp->sync_handle = sys_cpu_to_le16(per_adv_sync->handle);
1372 cp->request_event = sys_cpu_to_le16(param->request_event);
1373 cp->request_subevent = param->request_subevent;
1374 cp->response_subevent = param->response_subevent;
1375 cp->response_slot = param->response_slot;
1376 cp->response_data_length = data->len;
1377
1378 net_buf_add_mem(buf, data->data, cp->response_data_length);
1379
1380 return bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_PER_ADV_RESPONSE_DATA, buf, NULL);
1381 }
1382 #endif /* CONFIG_BT_PER_ADV_SYNC_RSP */
1383
bt_hci_le_per_adv_sync_lost(struct net_buf * buf)1384 void bt_hci_le_per_adv_sync_lost(struct net_buf *buf)
1385 {
1386 struct bt_hci_evt_le_per_adv_sync_lost *evt =
1387 (struct bt_hci_evt_le_per_adv_sync_lost *)buf->data;
1388 struct bt_le_per_adv_sync *per_adv_sync;
1389
1390 per_adv_sync = bt_hci_per_adv_sync_lookup_handle(sys_le16_to_cpu(evt->handle));
1391
1392 if (!per_adv_sync) {
1393 LOG_ERR("Unknown handle 0x%04Xfor periodic adv sync lost",
1394 sys_le16_to_cpu(evt->handle));
1395 return;
1396 }
1397
1398 /* There is no status in the per. adv. sync lost event */
1399 per_adv_sync_terminated(per_adv_sync, BT_HCI_ERR_UNSPECIFIED);
1400 }
1401
1402 #if defined(CONFIG_BT_PER_ADV_SYNC_TRANSFER_RECEIVER)
1403 static uint8_t conn_past_modes[CONFIG_BT_MAX_CONN];
1404 static uint8_t default_past_mode;
1405
past_disconnected_cb(struct bt_conn * conn,uint8_t reason)1406 static void past_disconnected_cb(struct bt_conn *conn, uint8_t reason)
1407 {
1408 /* The core spec does not explicit state that the mode of a connection handle is cleared on
1409 * disconnect, but let's assume it is.
1410 */
1411 conn_past_modes[bt_conn_index(conn)] = BT_HCI_LE_PAST_MODE_NO_SYNC;
1412 }
1413
1414 BT_CONN_CB_DEFINE(past_conn_callbacks) = {
1415 .disconnected = past_disconnected_cb,
1416 };
1417
bt_hci_le_past_received_common(struct net_buf * buf)1418 static void bt_hci_le_past_received_common(struct net_buf *buf)
1419 {
1420 #if defined(CONFIG_BT_PER_ADV_SYNC_RSP)
1421 struct bt_hci_evt_le_past_received_v2 *evt =
1422 (struct bt_hci_evt_le_past_received_v2 *)buf->data;
1423 #else
1424 struct bt_hci_evt_le_past_received *evt =
1425 (struct bt_hci_evt_le_past_received *)buf->data;
1426 #endif /* defined(CONFIG_BT_PER_ADV_SYNC_RSP) */
1427
1428 struct bt_le_per_adv_sync_synced_info sync_info;
1429 struct bt_le_per_adv_sync_cb *listener;
1430 struct bt_le_per_adv_sync *per_adv_sync;
1431 bt_addr_le_t id_addr;
1432
1433 if (evt->status) {
1434 /* No sync created, don't notify app */
1435 LOG_DBG("PAST receive failed with status 0x%02X %s",
1436 evt->status, bt_hci_err_to_str(evt->status));
1437 return;
1438 }
1439
1440 sync_info.conn = bt_conn_lookup_handle(
1441 sys_le16_to_cpu(evt->conn_handle),
1442 BT_CONN_TYPE_LE);
1443
1444 if (!sync_info.conn) {
1445 LOG_ERR("Could not lookup connection handle from PAST");
1446 per_adv_sync_terminate(sys_le16_to_cpu(evt->sync_handle));
1447 return;
1448 }
1449
1450 per_adv_sync = per_adv_sync_new();
1451 if (!per_adv_sync) {
1452 LOG_WRN("Could not allocate new PA sync from PAST");
1453 per_adv_sync_terminate(sys_le16_to_cpu(evt->sync_handle));
1454 bt_conn_unref(sync_info.conn);
1455 return;
1456 }
1457
1458 atomic_set_bit(per_adv_sync->flags, BT_PER_ADV_SYNC_SYNCED);
1459
1460 if (bt_addr_le_is_resolved(&evt->addr)) {
1461 bt_addr_le_copy_resolved(&id_addr, &evt->addr);
1462 } else {
1463 bt_addr_le_copy(&id_addr,
1464 bt_lookup_id_addr(BT_ID_DEFAULT, &evt->addr));
1465 }
1466
1467 per_adv_sync->handle = sys_le16_to_cpu(evt->sync_handle);
1468 per_adv_sync->interval = sys_le16_to_cpu(evt->interval);
1469 per_adv_sync->clock_accuracy = sys_le16_to_cpu(evt->clock_accuracy);
1470 per_adv_sync->phy = bt_get_phy(evt->phy);
1471 bt_addr_le_copy(&per_adv_sync->addr, &id_addr);
1472 per_adv_sync->sid = evt->adv_sid;
1473
1474 #if defined(CONFIG_BT_PER_ADV_SYNC_RSP)
1475 per_adv_sync->num_subevents = evt->num_subevents;
1476 per_adv_sync->subevent_interval = evt->subevent_interval;
1477 per_adv_sync->response_slot_delay = evt->response_slot_delay;
1478 per_adv_sync->response_slot_spacing = evt->response_slot_spacing;
1479 #endif /* defined(CONFIG_BT_PER_ADV_SYNC_RSP) */
1480
1481 sync_info.interval = per_adv_sync->interval;
1482 sync_info.phy = per_adv_sync->phy;
1483 sync_info.addr = &per_adv_sync->addr;
1484 sync_info.sid = per_adv_sync->sid;
1485 sync_info.service_data = sys_le16_to_cpu(evt->service_data);
1486
1487 const uint8_t mode = conn_past_modes[bt_conn_index(sync_info.conn)];
1488
1489 if (mode == BT_HCI_LE_PAST_MODE_NO_SYNC) {
1490 /* Use the default parameter mode as the conn specific mode is not set */
1491 sync_info.recv_enabled =
1492 default_past_mode == BT_HCI_LE_PAST_MODE_SYNC ||
1493 default_past_mode == BT_HCI_LE_PAST_MODE_SYNC_FILTER_DUPLICATES;
1494 } else {
1495 sync_info.recv_enabled = mode == BT_HCI_LE_PAST_MODE_SYNC ||
1496 mode == BT_HCI_LE_PAST_MODE_SYNC_FILTER_DUPLICATES;
1497 }
1498
1499 #if defined(CONFIG_BT_PER_ADV_SYNC_RSP)
1500 sync_info.num_subevents = per_adv_sync->num_subevents;
1501 sync_info.subevent_interval = per_adv_sync->subevent_interval;
1502 sync_info.response_slot_delay = per_adv_sync->response_slot_delay;
1503 sync_info.response_slot_spacing = per_adv_sync->response_slot_spacing;
1504 #endif /* defined(CONFIG_BT_PER_ADV_SYNC_RSP) */
1505
1506 SYS_SLIST_FOR_EACH_CONTAINER(&pa_sync_cbs, listener, node) {
1507 if (listener->synced) {
1508 listener->synced(per_adv_sync, &sync_info);
1509 }
1510 }
1511
1512 bt_conn_unref(sync_info.conn);
1513 }
1514
bt_hci_le_past_received(struct net_buf * buf)1515 void bt_hci_le_past_received(struct net_buf *buf)
1516 {
1517 if (IS_ENABLED(CONFIG_BT_PER_ADV_SYNC_RSP)) {
1518 LOG_ERR("The controller shall raise the latest unmasked version of the event");
1519
1520 return;
1521 }
1522
1523 bt_hci_le_past_received_common(buf);
1524 }
1525
1526 #if defined(CONFIG_BT_PER_ADV_SYNC_RSP)
bt_hci_le_past_received_v2(struct net_buf * buf)1527 void bt_hci_le_past_received_v2(struct net_buf *buf)
1528 {
1529 bt_hci_le_past_received_common(buf);
1530 }
1531 #endif /* CONFIG_BT_PER_ADV_SYNC_RSP */
1532 #endif /* CONFIG_BT_PER_ADV_SYNC_TRANSFER_RECEIVER */
1533
1534 #if defined(CONFIG_BT_PER_ADV_SYNC_RSP)
bt_hci_le_per_adv_sync_established_v2(struct net_buf * buf)1535 void bt_hci_le_per_adv_sync_established_v2(struct net_buf *buf)
1536 {
1537 bt_hci_le_per_adv_sync_established_common(buf);
1538 }
1539
bt_hci_le_per_adv_report_v2(struct net_buf * buf)1540 void bt_hci_le_per_adv_report_v2(struct net_buf *buf)
1541 {
1542 bt_hci_le_per_adv_report_common(buf);
1543 }
1544 #endif /* CONFIG_BT_PER_ADV_SYNC_RSP */
1545
1546 #if defined(CONFIG_BT_ISO_BROADCAST)
bt_hci_le_biginfo_adv_report(struct net_buf * buf)1547 void bt_hci_le_biginfo_adv_report(struct net_buf *buf)
1548 {
1549 struct bt_hci_evt_le_biginfo_adv_report *evt;
1550 struct bt_le_per_adv_sync *per_adv_sync;
1551 struct bt_le_per_adv_sync_cb *listener;
1552 struct bt_iso_biginfo biginfo;
1553
1554 evt = net_buf_pull_mem(buf, sizeof(*evt));
1555
1556 per_adv_sync = bt_hci_per_adv_sync_lookup_handle(sys_le16_to_cpu(evt->sync_handle));
1557
1558 if (!per_adv_sync) {
1559 LOG_ERR("Unknown handle 0x%04X for periodic advertising report",
1560 sys_le16_to_cpu(evt->sync_handle));
1561 return;
1562 }
1563
1564 biginfo.addr = &per_adv_sync->addr;
1565 biginfo.sid = per_adv_sync->sid;
1566 biginfo.num_bis = evt->num_bis;
1567 biginfo.sub_evt_count = evt->nse;
1568 biginfo.iso_interval = sys_le16_to_cpu(evt->iso_interval);
1569 biginfo.burst_number = evt->bn;
1570 biginfo.offset = evt->pto;
1571 biginfo.rep_count = evt->irc;
1572 biginfo.max_pdu = sys_le16_to_cpu(evt->max_pdu);
1573 biginfo.sdu_interval = sys_get_le24(evt->sdu_interval);
1574 biginfo.max_sdu = sys_le16_to_cpu(evt->max_sdu);
1575 biginfo.phy = bt_get_phy(evt->phy);
1576 biginfo.framing = evt->framing;
1577 biginfo.encryption = evt->encryption ? true : false;
1578
1579 SYS_SLIST_FOR_EACH_CONTAINER(&pa_sync_cbs, listener, node) {
1580 if (listener->biginfo) {
1581 listener->biginfo(per_adv_sync, &biginfo);
1582 }
1583 }
1584 }
1585 #endif /* CONFIG_BT_ISO_BROADCAST */
1586 #if defined(CONFIG_BT_DF_CONNECTIONLESS_CTE_RX)
bt_hci_le_df_connectionless_iq_report_common(uint8_t event,struct net_buf * buf)1587 static void bt_hci_le_df_connectionless_iq_report_common(uint8_t event, struct net_buf *buf)
1588 {
1589 int err;
1590
1591 struct bt_df_per_adv_sync_iq_samples_report cte_report;
1592 struct bt_le_per_adv_sync *per_adv_sync;
1593 struct bt_le_per_adv_sync_cb *listener;
1594
1595 if (event == BT_HCI_EVT_LE_CONNECTIONLESS_IQ_REPORT) {
1596 err = hci_df_prepare_connectionless_iq_report(buf, &cte_report, &per_adv_sync);
1597 if (err) {
1598 LOG_ERR("Prepare CTE conn IQ report failed %d", err);
1599 return;
1600 }
1601 } else if (IS_ENABLED(CONFIG_BT_DF_VS_CL_IQ_REPORT_16_BITS_IQ_SAMPLES) &&
1602 event == BT_HCI_EVT_VS_LE_CONNECTIONLESS_IQ_REPORT) {
1603 err = hci_df_vs_prepare_connectionless_iq_report(buf, &cte_report, &per_adv_sync);
1604 if (err) {
1605 LOG_ERR("Prepare CTE conn IQ report failed %d", err);
1606 return;
1607 }
1608 } else {
1609 LOG_ERR("Unhandled VS connectionless IQ report");
1610 return;
1611 }
1612
1613 SYS_SLIST_FOR_EACH_CONTAINER(&pa_sync_cbs, listener, node) {
1614 if (listener->cte_report_cb) {
1615 listener->cte_report_cb(per_adv_sync, &cte_report);
1616 }
1617 }
1618 }
1619
bt_hci_le_df_connectionless_iq_report(struct net_buf * buf)1620 void bt_hci_le_df_connectionless_iq_report(struct net_buf *buf)
1621 {
1622 bt_hci_le_df_connectionless_iq_report_common(BT_HCI_EVT_LE_CONNECTIONLESS_IQ_REPORT, buf);
1623 }
1624
1625 #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)1626 void bt_hci_le_vs_df_connectionless_iq_report(struct net_buf *buf)
1627 {
1628 bt_hci_le_df_connectionless_iq_report_common(BT_HCI_EVT_VS_LE_CONNECTIONLESS_IQ_REPORT,
1629 buf);
1630 }
1631 #endif /* CONFIG_BT_DF_VS_CL_IQ_REPORT_16_BITS_IQ_SAMPLES */
1632 #endif /* CONFIG_BT_DF_CONNECTIONLESS_CTE_RX */
1633 #endif /* defined(CONFIG_BT_PER_ADV_SYNC) */
1634 #endif /* defined(CONFIG_BT_EXT_ADV) */
1635
bt_hci_le_adv_report(struct net_buf * buf)1636 void bt_hci_le_adv_report(struct net_buf *buf)
1637 {
1638 uint8_t num_reports = net_buf_pull_u8(buf);
1639 struct bt_hci_evt_le_advertising_info *evt;
1640
1641 LOG_DBG("Adv number of reports %u", num_reports);
1642
1643 while (num_reports--) {
1644 struct bt_le_scan_recv_info adv_info;
1645
1646 if (!atomic_test_bit(scan_state.scan_flags, BT_LE_SCAN_USER_EXPLICIT_SCAN)) {
1647 /* The application has not requested explicit scan, so it is not expecting
1648 * advertising reports. Discard.
1649 * This is done in the loop as this flag can change between each iteration,
1650 * and it is not uncommon that scanning is disabled in the callback called
1651 * from le_adv_recv
1652 */
1653
1654 break;
1655 }
1656
1657 if (buf->len < sizeof(*evt)) {
1658 LOG_ERR("Unexpected end of buffer");
1659 break;
1660 }
1661
1662 evt = net_buf_pull_mem(buf, sizeof(*evt));
1663
1664 if (buf->len < evt->length + sizeof(adv_info.rssi)) {
1665 LOG_ERR("Unexpected end of buffer");
1666 break;
1667 }
1668
1669 adv_info.primary_phy = BT_GAP_LE_PHY_1M;
1670 adv_info.secondary_phy = 0;
1671 adv_info.tx_power = BT_GAP_TX_POWER_INVALID;
1672 adv_info.rssi = evt->data[evt->length];
1673 adv_info.sid = BT_GAP_SID_INVALID;
1674 adv_info.interval = 0U;
1675
1676 adv_info.adv_type = evt->evt_type;
1677 adv_info.adv_props = get_adv_props_legacy(evt->evt_type);
1678
1679 le_adv_recv(&evt->addr, &adv_info, &buf->b, evt->length);
1680
1681 net_buf_pull(buf, evt->length + sizeof(adv_info.rssi));
1682 }
1683 }
1684
valid_le_scan_param(const struct bt_le_scan_param * param)1685 static bool valid_le_scan_param(const struct bt_le_scan_param *param)
1686 {
1687 if (IS_ENABLED(CONFIG_BT_PRIVACY) &&
1688 param->type == BT_LE_SCAN_TYPE_ACTIVE &&
1689 param->timeout != 0) {
1690 /* This is marked as not supported as a stopgap until the (scan,
1691 * adv, init) roles are reworked into proper state machines.
1692 *
1693 * Having proper state machines is necessary to be able to
1694 * suspend all roles that use the (resolvable) private address,
1695 * update the RPA and resume them again with the right
1696 * parameters.
1697 *
1698 * Else we lower the privacy of the device as either the RPA
1699 * update will fail or the scanner will not use the newly
1700 * generated RPA.
1701 */
1702 return false;
1703 }
1704
1705 if (param->type != BT_LE_SCAN_TYPE_PASSIVE &&
1706 param->type != BT_LE_SCAN_TYPE_ACTIVE) {
1707 return false;
1708 }
1709
1710 if (param->options & ~(BT_LE_SCAN_OPT_FILTER_DUPLICATE |
1711 BT_LE_SCAN_OPT_FILTER_ACCEPT_LIST |
1712 BT_LE_SCAN_OPT_CODED |
1713 BT_LE_SCAN_OPT_NO_1M)) {
1714 return false;
1715 }
1716
1717 if (param->interval < 0x0004 || param->interval > 0x4000) {
1718 return false;
1719 }
1720
1721 if (param->window < 0x0004 || param->window > 0x4000) {
1722 return false;
1723 }
1724
1725 if (param->window > param->interval) {
1726 return false;
1727 }
1728
1729 return true;
1730 }
1731
bt_le_scan_start(const struct bt_le_scan_param * param,bt_le_scan_cb_t cb)1732 int bt_le_scan_start(const struct bt_le_scan_param *param, bt_le_scan_cb_t cb)
1733 {
1734 int err;
1735
1736 if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
1737 return -EAGAIN;
1738 }
1739
1740 /* Check that the parameters have valid values */
1741 if (!valid_le_scan_param(param)) {
1742 return -EINVAL;
1743 }
1744
1745 if (param->type && !bt_id_scan_random_addr_check()) {
1746 return -EINVAL;
1747 }
1748
1749 /* Prevent multiple threads to try to enable explicit scanning at the same time.
1750 * That could lead to unwanted overwriting of scan_state.explicit_scan_param.
1751 */
1752 err = k_mutex_lock(&scan_state.scan_explicit_params_mutex, K_NO_WAIT);
1753
1754 if (err) {
1755 return err;
1756 }
1757
1758 err = scan_check_if_state_allowed(BT_LE_SCAN_USER_EXPLICIT_SCAN);
1759
1760 if (err) {
1761 k_mutex_unlock(&scan_state.scan_explicit_params_mutex);
1762 return err;
1763 }
1764
1765 /* store the parameters that were used to start the scanner */
1766 memcpy(&scan_state.explicit_scan_param, param,
1767 sizeof(scan_state.explicit_scan_param));
1768
1769 scan_dev_found_cb = cb;
1770 err = bt_le_scan_user_add(BT_LE_SCAN_USER_EXPLICIT_SCAN);
1771 k_mutex_unlock(&scan_state.scan_explicit_params_mutex);
1772
1773 return err;
1774 }
1775
bt_le_scan_stop(void)1776 int bt_le_scan_stop(void)
1777 {
1778 bt_scan_softreset();
1779 scan_dev_found_cb = NULL;
1780
1781 if (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
1782 atomic_test_and_clear_bit(bt_dev.flags, BT_DEV_SCAN_LIMITED)) {
1783 atomic_clear_bit(bt_dev.flags, BT_DEV_RPA_VALID);
1784
1785 #if defined(CONFIG_BT_SMP)
1786 bt_id_pending_keys_update();
1787 #endif
1788 }
1789
1790 return bt_le_scan_user_remove(BT_LE_SCAN_USER_EXPLICIT_SCAN);
1791 }
1792
bt_le_scan_cb_register(struct bt_le_scan_cb * cb)1793 int bt_le_scan_cb_register(struct bt_le_scan_cb *cb)
1794 {
1795 if (sys_slist_find(&scan_cbs, &cb->node, NULL)) {
1796 return -EEXIST;
1797 }
1798
1799 sys_slist_append(&scan_cbs, &cb->node);
1800
1801 return 0;
1802 }
1803
bt_le_scan_cb_unregister(struct bt_le_scan_cb * cb)1804 void bt_le_scan_cb_unregister(struct bt_le_scan_cb *cb)
1805 {
1806 sys_slist_find_and_remove(&scan_cbs, &cb->node);
1807 }
1808
1809 #if defined(CONFIG_BT_PER_ADV_SYNC)
bt_le_per_adv_sync_get_index(struct bt_le_per_adv_sync * per_adv_sync)1810 uint8_t bt_le_per_adv_sync_get_index(struct bt_le_per_adv_sync *per_adv_sync)
1811 {
1812 ptrdiff_t index = per_adv_sync - per_adv_sync_pool;
1813
1814 __ASSERT(index >= 0 && ARRAY_SIZE(per_adv_sync_pool) > index,
1815 "Invalid per_adv_sync pointer");
1816 return (uint8_t)index;
1817 }
1818
bt_le_per_adv_sync_lookup_index(uint8_t index)1819 struct bt_le_per_adv_sync *bt_le_per_adv_sync_lookup_index(uint8_t index)
1820 {
1821 if (index >= ARRAY_SIZE(per_adv_sync_pool)) {
1822 return NULL;
1823 }
1824
1825 return &per_adv_sync_pool[index];
1826 }
1827
bt_le_per_adv_sync_get_info(struct bt_le_per_adv_sync * per_adv_sync,struct bt_le_per_adv_sync_info * info)1828 int bt_le_per_adv_sync_get_info(struct bt_le_per_adv_sync *per_adv_sync,
1829 struct bt_le_per_adv_sync_info *info)
1830 {
1831 CHECKIF(per_adv_sync == NULL || info == NULL) {
1832 return -EINVAL;
1833 }
1834
1835 bt_addr_le_copy(&info->addr, &per_adv_sync->addr);
1836 info->sid = per_adv_sync->sid;
1837 info->phy = per_adv_sync->phy;
1838 info->interval = per_adv_sync->interval;
1839
1840 return 0;
1841 }
1842
bt_le_per_adv_sync_lookup_addr(const bt_addr_le_t * adv_addr,uint8_t sid)1843 struct bt_le_per_adv_sync *bt_le_per_adv_sync_lookup_addr(const bt_addr_le_t *adv_addr,
1844 uint8_t sid)
1845 {
1846 for (int i = 0; i < ARRAY_SIZE(per_adv_sync_pool); i++) {
1847 struct bt_le_per_adv_sync *sync = &per_adv_sync_pool[i];
1848
1849 if (!atomic_test_bit(per_adv_sync_pool[i].flags,
1850 BT_PER_ADV_SYNC_CREATED)) {
1851 continue;
1852 }
1853
1854 if (bt_addr_le_eq(&sync->addr, adv_addr) && sync->sid == sid) {
1855 return sync;
1856 }
1857 }
1858
1859 return NULL;
1860 }
1861
bt_le_per_adv_sync_create(const struct bt_le_per_adv_sync_param * param,struct bt_le_per_adv_sync ** out_sync)1862 int bt_le_per_adv_sync_create(const struct bt_le_per_adv_sync_param *param,
1863 struct bt_le_per_adv_sync **out_sync)
1864 {
1865 struct bt_hci_cp_le_per_adv_create_sync *cp;
1866 struct net_buf *buf;
1867 struct bt_le_per_adv_sync *per_adv_sync;
1868 int err;
1869
1870 if (!BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
1871 return -ENOTSUP;
1872 }
1873
1874 if (get_pending_per_adv_sync()) {
1875 return -EBUSY;
1876 }
1877
1878 if (param->sid > BT_GAP_SID_MAX ||
1879 param->skip > BT_GAP_PER_ADV_MAX_SKIP ||
1880 param->timeout > BT_GAP_PER_ADV_MAX_TIMEOUT ||
1881 param->timeout < BT_GAP_PER_ADV_MIN_TIMEOUT) {
1882 return -EINVAL;
1883 }
1884
1885 per_adv_sync = per_adv_sync_new();
1886 if (!per_adv_sync) {
1887 return -ENOMEM;
1888 }
1889
1890 buf = bt_hci_cmd_create(BT_HCI_OP_LE_PER_ADV_CREATE_SYNC, sizeof(*cp));
1891 if (!buf) {
1892 per_adv_sync_delete(per_adv_sync);
1893 return -ENOBUFS;
1894 }
1895
1896 cp = net_buf_add(buf, sizeof(*cp));
1897 (void)memset(cp, 0, sizeof(*cp));
1898
1899 if (param->options & BT_LE_PER_ADV_SYNC_OPT_USE_PER_ADV_LIST) {
1900 atomic_set_bit(per_adv_sync->flags,
1901 BT_PER_ADV_SYNC_SYNCING_USE_LIST);
1902
1903 cp->options |= BT_HCI_LE_PER_ADV_CREATE_SYNC_FP_USE_LIST;
1904 } else {
1905 /* If BT_LE_PER_ADV_SYNC_OPT_USE_PER_ADV_LIST is set, then the
1906 * address and SID are ignored by the controller, so we only
1907 * copy/assign them in case that the periodic advertising list
1908 * is not used.
1909 */
1910 bt_addr_le_copy(&cp->addr, ¶m->addr);
1911 cp->sid = param->sid;
1912 }
1913
1914 if (param->options &
1915 BT_LE_PER_ADV_SYNC_OPT_REPORTING_INITIALLY_DISABLED) {
1916 cp->options |=
1917 BT_HCI_LE_PER_ADV_CREATE_SYNC_FP_REPORTS_DISABLED;
1918
1919 atomic_set_bit(per_adv_sync->flags,
1920 BT_PER_ADV_SYNC_RECV_DISABLED);
1921 }
1922
1923 if (param->options & BT_LE_PER_ADV_SYNC_OPT_FILTER_DUPLICATE) {
1924 cp->options |=
1925 BT_HCI_LE_PER_ADV_CREATE_SYNC_FP_FILTER_DUPLICATE;
1926 }
1927
1928 if (param->options & BT_LE_PER_ADV_SYNC_OPT_DONT_SYNC_AOA) {
1929 cp->cte_type |= BT_HCI_LE_PER_ADV_CREATE_SYNC_CTE_TYPE_NO_AOA;
1930 }
1931
1932 if (param->options & BT_LE_PER_ADV_SYNC_OPT_DONT_SYNC_AOD_1US) {
1933 cp->cte_type |=
1934 BT_HCI_LE_PER_ADV_CREATE_SYNC_CTE_TYPE_NO_AOD_1US;
1935 }
1936
1937 if (param->options & BT_LE_PER_ADV_SYNC_OPT_DONT_SYNC_AOD_2US) {
1938 cp->cte_type |=
1939 BT_HCI_LE_PER_ADV_CREATE_SYNC_CTE_TYPE_NO_AOD_2US;
1940 }
1941
1942 if (param->options & BT_LE_PER_ADV_SYNC_OPT_SYNC_ONLY_CONST_TONE_EXT) {
1943 cp->cte_type |= BT_HCI_LE_PER_ADV_CREATE_SYNC_CTE_TYPE_ONLY_CTE;
1944 }
1945
1946 cp->skip = sys_cpu_to_le16(param->skip);
1947 cp->sync_timeout = sys_cpu_to_le16(param->timeout);
1948
1949 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_PER_ADV_CREATE_SYNC, buf, NULL);
1950 if (err) {
1951 per_adv_sync_delete(per_adv_sync);
1952 return err;
1953 }
1954
1955 atomic_set_bit(per_adv_sync->flags, BT_PER_ADV_SYNC_SYNCING);
1956
1957 /* Syncing requires that scan is enabled. If the caller doesn't enable
1958 * scan first, we enable it here, and disable it once the sync has been
1959 * established. We don't need to use any callbacks since we rely on
1960 * the advertiser address in the sync params.
1961 */
1962 err = bt_le_scan_user_add(BT_LE_SCAN_USER_PER_SYNC);
1963 if (err) {
1964 int per_sync_remove_err = bt_le_scan_user_remove(BT_LE_SCAN_USER_PER_SYNC);
1965
1966 if (per_sync_remove_err) {
1967 LOG_WRN("Error while updating the scanner (%d)", per_sync_remove_err);
1968 }
1969
1970 bt_le_per_adv_sync_delete(per_adv_sync);
1971 return err;
1972 }
1973
1974 *out_sync = per_adv_sync;
1975 bt_addr_le_copy(&per_adv_sync->addr, ¶m->addr);
1976 per_adv_sync->sid = param->sid;
1977
1978 return 0;
1979 }
1980
bt_le_per_adv_sync_create_cancel(struct bt_le_per_adv_sync * per_adv_sync)1981 static int bt_le_per_adv_sync_create_cancel(
1982 struct bt_le_per_adv_sync *per_adv_sync)
1983 {
1984 struct net_buf *buf;
1985 int err;
1986
1987 if (get_pending_per_adv_sync() != per_adv_sync) {
1988 return -EINVAL;
1989 }
1990
1991 err = bt_le_scan_user_remove(BT_LE_SCAN_USER_PER_SYNC);
1992
1993 if (err) {
1994 return err;
1995 }
1996
1997 buf = bt_hci_cmd_create(BT_HCI_OP_LE_PER_ADV_CREATE_SYNC_CANCEL, 0);
1998 if (!buf) {
1999 return -ENOBUFS;
2000 }
2001
2002 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_PER_ADV_CREATE_SYNC_CANCEL, buf,
2003 NULL);
2004 if (err) {
2005 return err;
2006 }
2007
2008 return 0;
2009 }
2010
bt_le_per_adv_sync_terminate(struct bt_le_per_adv_sync * per_adv_sync)2011 static int bt_le_per_adv_sync_terminate(struct bt_le_per_adv_sync *per_adv_sync)
2012 {
2013 int err;
2014
2015 if (!atomic_test_bit(per_adv_sync->flags, BT_PER_ADV_SYNC_SYNCED)) {
2016 return -EINVAL;
2017 }
2018
2019 err = per_adv_sync_terminate(per_adv_sync->handle);
2020
2021 if (err) {
2022 return err;
2023 }
2024
2025 return 0;
2026 }
2027
bt_le_per_adv_sync_delete(struct bt_le_per_adv_sync * per_adv_sync)2028 int bt_le_per_adv_sync_delete(struct bt_le_per_adv_sync *per_adv_sync)
2029 {
2030 int err = 0;
2031
2032 if (!BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
2033 return -ENOTSUP;
2034 }
2035
2036 if (atomic_test_bit(per_adv_sync->flags, BT_PER_ADV_SYNC_SYNCED)) {
2037 err = bt_le_per_adv_sync_terminate(per_adv_sync);
2038
2039 if (!err) {
2040 per_adv_sync_terminated(per_adv_sync,
2041 BT_HCI_ERR_LOCALHOST_TERM_CONN);
2042 }
2043 } else if (get_pending_per_adv_sync() == per_adv_sync) {
2044 err = bt_le_per_adv_sync_create_cancel(per_adv_sync);
2045 /* Delete of the per_adv_sync will be done in the event
2046 * handler when cancelling.
2047 */
2048 }
2049
2050 return err;
2051 }
2052
bt_le_per_adv_sync_cb_register(struct bt_le_per_adv_sync_cb * cb)2053 int bt_le_per_adv_sync_cb_register(struct bt_le_per_adv_sync_cb *cb)
2054 {
2055 if (sys_slist_find(&pa_sync_cbs, &cb->node, NULL)) {
2056 return -EEXIST;
2057 }
2058
2059 sys_slist_append(&pa_sync_cbs, &cb->node);
2060
2061 return 0;
2062 }
2063
bt_le_set_per_adv_recv_enable(struct bt_le_per_adv_sync * per_adv_sync,bool enable)2064 static int bt_le_set_per_adv_recv_enable(
2065 struct bt_le_per_adv_sync *per_adv_sync, bool enable)
2066 {
2067 struct bt_hci_cp_le_set_per_adv_recv_enable *cp;
2068 struct bt_le_per_adv_sync_cb *listener;
2069 struct bt_le_per_adv_sync_state_info info;
2070 struct net_buf *buf;
2071 struct bt_hci_cmd_state_set state;
2072 int err;
2073
2074 if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
2075 return -EAGAIN;
2076 }
2077
2078 if (!BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
2079 return -ENOTSUP;
2080 }
2081
2082 if (!atomic_test_bit(per_adv_sync->flags, BT_PER_ADV_SYNC_SYNCED)) {
2083 return -EINVAL;
2084 }
2085
2086 if ((enable && !atomic_test_bit(per_adv_sync->flags,
2087 BT_PER_ADV_SYNC_RECV_DISABLED)) ||
2088 (!enable && atomic_test_bit(per_adv_sync->flags,
2089 BT_PER_ADV_SYNC_RECV_DISABLED))) {
2090 return -EALREADY;
2091 }
2092
2093 buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_PER_ADV_RECV_ENABLE,
2094 sizeof(*cp));
2095 if (!buf) {
2096 return -ENOBUFS;
2097 }
2098
2099 cp = net_buf_add(buf, sizeof(*cp));
2100 (void)memset(cp, 0, sizeof(*cp));
2101
2102 cp->handle = sys_cpu_to_le16(per_adv_sync->handle);
2103 cp->enable = enable ? 1 : 0;
2104
2105 bt_hci_cmd_state_set_init(buf, &state, per_adv_sync->flags,
2106 BT_PER_ADV_SYNC_RECV_DISABLED, !enable);
2107
2108 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_PER_ADV_RECV_ENABLE,
2109 buf, NULL);
2110
2111 if (err) {
2112 return err;
2113 }
2114
2115 info.recv_enabled = !atomic_test_bit(per_adv_sync->flags,
2116 BT_PER_ADV_SYNC_RECV_DISABLED);
2117
2118 SYS_SLIST_FOR_EACH_CONTAINER(&pa_sync_cbs, listener, node) {
2119 if (listener->state_changed) {
2120 listener->state_changed(per_adv_sync, &info);
2121 }
2122 }
2123
2124 return 0;
2125 }
2126
bt_le_per_adv_sync_recv_enable(struct bt_le_per_adv_sync * per_adv_sync)2127 int bt_le_per_adv_sync_recv_enable(struct bt_le_per_adv_sync *per_adv_sync)
2128 {
2129 return bt_le_set_per_adv_recv_enable(per_adv_sync, true);
2130 }
2131
bt_le_per_adv_sync_recv_disable(struct bt_le_per_adv_sync * per_adv_sync)2132 int bt_le_per_adv_sync_recv_disable(struct bt_le_per_adv_sync *per_adv_sync)
2133 {
2134 return bt_le_set_per_adv_recv_enable(per_adv_sync, false);
2135 }
2136
2137 #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)2138 int bt_le_per_adv_sync_transfer(const struct bt_le_per_adv_sync *per_adv_sync,
2139 const struct bt_conn *conn,
2140 uint16_t service_data)
2141 {
2142 struct bt_hci_cp_le_per_adv_sync_transfer *cp;
2143 struct net_buf *buf;
2144
2145
2146 if (!BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
2147 return -ENOTSUP;
2148 } else if (!BT_FEAT_LE_PAST_SEND(bt_dev.le.features)) {
2149 return -ENOTSUP;
2150 }
2151
2152 buf = bt_hci_cmd_create(BT_HCI_OP_LE_PER_ADV_SYNC_TRANSFER,
2153 sizeof(*cp));
2154 if (!buf) {
2155 return -ENOBUFS;
2156 }
2157
2158 cp = net_buf_add(buf, sizeof(*cp));
2159 (void)memset(cp, 0, sizeof(*cp));
2160
2161 cp->conn_handle = sys_cpu_to_le16(conn->handle);
2162 cp->sync_handle = sys_cpu_to_le16(per_adv_sync->handle);
2163 cp->service_data = sys_cpu_to_le16(service_data);
2164
2165 return bt_hci_cmd_send_sync(BT_HCI_OP_LE_PER_ADV_SYNC_TRANSFER, buf,
2166 NULL);
2167 }
2168 #endif /* CONFIG_BT_PER_ADV_SYNC_TRANSFER_SENDER */
2169
2170 #if defined(CONFIG_BT_PER_ADV_SYNC_TRANSFER_RECEIVER)
valid_past_param(const struct bt_le_per_adv_sync_transfer_param * param)2171 static bool valid_past_param(
2172 const struct bt_le_per_adv_sync_transfer_param *param)
2173 {
2174 if (param->skip > 0x01f3 ||
2175 param->timeout < 0x000A ||
2176 param->timeout > 0x4000) {
2177 return false;
2178 }
2179 if ((param->options & BT_LE_PER_ADV_SYNC_TRANSFER_OPT_REPORTING_INITIALLY_DISABLED) &&
2180 (param->options & BT_LE_PER_ADV_SYNC_TRANSFER_OPT_FILTER_DUPLICATES)) {
2181 return false;
2182 }
2183
2184 return true;
2185 }
2186
past_param_set(const struct bt_conn * conn,uint8_t mode,uint16_t skip,uint16_t timeout,uint8_t cte_type)2187 static int past_param_set(const struct bt_conn *conn, uint8_t mode,
2188 uint16_t skip, uint16_t timeout, uint8_t cte_type)
2189 {
2190 struct bt_hci_cp_le_past_param *cp;
2191 struct net_buf *buf;
2192
2193 buf = bt_hci_cmd_create(BT_HCI_OP_LE_PAST_PARAM, sizeof(*cp));
2194 if (!buf) {
2195 return -ENOBUFS;
2196 }
2197
2198 cp = net_buf_add(buf, sizeof(*cp));
2199 (void)memset(cp, 0, sizeof(*cp));
2200
2201 cp->conn_handle = sys_cpu_to_le16(conn->handle);
2202 cp->mode = mode;
2203 cp->skip = sys_cpu_to_le16(skip);
2204 cp->timeout = sys_cpu_to_le16(timeout);
2205 cp->cte_type = cte_type;
2206
2207 return bt_hci_cmd_send_sync(BT_HCI_OP_LE_PAST_PARAM, buf, NULL);
2208 }
2209
default_past_param_set(uint8_t mode,uint16_t skip,uint16_t timeout,uint8_t cte_type)2210 static int default_past_param_set(uint8_t mode, uint16_t skip, uint16_t timeout,
2211 uint8_t cte_type)
2212 {
2213 struct bt_hci_cp_le_default_past_param *cp;
2214 struct net_buf *buf;
2215
2216 buf = bt_hci_cmd_create(BT_HCI_OP_LE_DEFAULT_PAST_PARAM, sizeof(*cp));
2217 if (!buf) {
2218 return -ENOBUFS;
2219 }
2220
2221 cp = net_buf_add(buf, sizeof(*cp));
2222 (void)memset(cp, 0, sizeof(*cp));
2223
2224 cp->mode = mode;
2225 cp->skip = sys_cpu_to_le16(skip);
2226 cp->timeout = sys_cpu_to_le16(timeout);
2227 cp->cte_type = cte_type;
2228
2229 return bt_hci_cmd_send_sync(BT_HCI_OP_LE_DEFAULT_PAST_PARAM, buf, NULL);
2230 }
2231
bt_le_per_adv_sync_transfer_subscribe(const struct bt_conn * conn,const struct bt_le_per_adv_sync_transfer_param * param)2232 int bt_le_per_adv_sync_transfer_subscribe(
2233 const struct bt_conn *conn,
2234 const struct bt_le_per_adv_sync_transfer_param *param)
2235 {
2236 uint8_t cte_type = 0;
2237 uint8_t mode = BT_HCI_LE_PAST_MODE_SYNC;
2238 int err;
2239
2240 if (!BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
2241 return -ENOTSUP;
2242 } else if (!BT_FEAT_LE_PAST_RECV(bt_dev.le.features)) {
2243 return -ENOTSUP;
2244 }
2245
2246 if (!valid_past_param(param)) {
2247 return -EINVAL;
2248 }
2249
2250 if (param->options & BT_LE_PER_ADV_SYNC_TRANSFER_OPT_SYNC_NO_AOA) {
2251 cte_type |= BT_HCI_LE_PAST_CTE_TYPE_NO_AOA;
2252 }
2253
2254 if (param->options & BT_LE_PER_ADV_SYNC_TRANSFER_OPT_SYNC_NO_AOD_1US) {
2255 cte_type |= BT_HCI_LE_PAST_CTE_TYPE_NO_AOD_1US;
2256 }
2257
2258 if (param->options & BT_LE_PER_ADV_SYNC_TRANSFER_OPT_SYNC_NO_AOD_2US) {
2259 cte_type |= BT_HCI_LE_PAST_CTE_TYPE_NO_AOD_2US;
2260 }
2261
2262 if (param->options & BT_LE_PER_ADV_SYNC_TRANSFER_OPT_SYNC_ONLY_CTE) {
2263 cte_type |= BT_HCI_LE_PAST_CTE_TYPE_ONLY_CTE;
2264 }
2265
2266 if (param->options & BT_LE_PER_ADV_SYNC_TRANSFER_OPT_REPORTING_INITIALLY_DISABLED) {
2267 mode = BT_HCI_LE_PAST_MODE_NO_REPORTS;
2268 } else if (param->options & BT_LE_PER_ADV_SYNC_TRANSFER_OPT_FILTER_DUPLICATES) {
2269 mode = BT_HCI_LE_PAST_MODE_SYNC_FILTER_DUPLICATES;
2270 }
2271
2272 if (conn) {
2273 const uint8_t conn_idx = bt_conn_index(conn);
2274 const uint8_t old_mode = conn_past_modes[conn_idx];
2275
2276 conn_past_modes[conn_idx] = mode;
2277
2278 err = past_param_set(conn, mode, param->skip, param->timeout, cte_type);
2279 if (err != 0) {
2280 /* Restore old mode */
2281 conn_past_modes[conn_idx] = old_mode;
2282 }
2283 } else {
2284 const uint8_t old_mode = default_past_mode;
2285
2286 default_past_mode = mode;
2287
2288 err = default_past_param_set(mode, param->skip, param->timeout, cte_type);
2289 if (err != 0) {
2290 /* Restore old mode */
2291 default_past_mode = old_mode;
2292 }
2293 }
2294
2295 return err;
2296 }
2297
bt_le_per_adv_sync_transfer_unsubscribe(const struct bt_conn * conn)2298 int bt_le_per_adv_sync_transfer_unsubscribe(const struct bt_conn *conn)
2299 {
2300 int err;
2301
2302 if (!BT_FEAT_LE_EXT_PER_ADV(bt_dev.le.features)) {
2303 return -ENOTSUP;
2304 } else if (!BT_FEAT_LE_PAST_RECV(bt_dev.le.features)) {
2305 return -ENOTSUP;
2306 }
2307
2308 if (conn) {
2309 const uint8_t conn_idx = bt_conn_index(conn);
2310 const uint8_t old_mode = conn_past_modes[conn_idx];
2311
2312 conn_past_modes[conn_idx] = BT_HCI_LE_PAST_MODE_NO_SYNC;
2313
2314 err = past_param_set(conn, BT_HCI_LE_PAST_MODE_NO_SYNC, 0, 0x0a, 0);
2315 if (err != 0) {
2316 /* Restore old mode */
2317 conn_past_modes[conn_idx] = old_mode;
2318 }
2319 } else {
2320 const uint8_t old_mode = default_past_mode;
2321
2322 default_past_mode = BT_HCI_LE_PAST_MODE_NO_SYNC;
2323 err = default_past_param_set(BT_HCI_LE_PAST_MODE_NO_SYNC, 0, 0x0a, 0);
2324 if (err != 0) {
2325 /* Restore old mode */
2326 default_past_mode = old_mode;
2327 }
2328 }
2329
2330 return err;
2331 }
2332 #endif /* CONFIG_BT_PER_ADV_SYNC_TRANSFER_RECEIVER */
2333
bt_le_per_adv_list_add(const bt_addr_le_t * addr,uint8_t sid)2334 int bt_le_per_adv_list_add(const bt_addr_le_t *addr, uint8_t sid)
2335 {
2336 struct bt_hci_cp_le_add_dev_to_per_adv_list *cp;
2337 struct net_buf *buf;
2338 int err;
2339
2340 if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
2341 return -EAGAIN;
2342 }
2343
2344 buf = bt_hci_cmd_create(BT_HCI_OP_LE_ADD_DEV_TO_PER_ADV_LIST,
2345 sizeof(*cp));
2346 if (!buf) {
2347 return -ENOBUFS;
2348 }
2349
2350 cp = net_buf_add(buf, sizeof(*cp));
2351 bt_addr_le_copy(&cp->addr, addr);
2352 cp->sid = sid;
2353
2354 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_ADD_DEV_TO_PER_ADV_LIST, buf,
2355 NULL);
2356 if (err) {
2357 LOG_ERR("Failed to add device to periodic advertiser list");
2358
2359 return err;
2360 }
2361
2362 return 0;
2363 }
2364
bt_le_per_adv_list_remove(const bt_addr_le_t * addr,uint8_t sid)2365 int bt_le_per_adv_list_remove(const bt_addr_le_t *addr, uint8_t sid)
2366 {
2367 struct bt_hci_cp_le_rem_dev_from_per_adv_list *cp;
2368 struct net_buf *buf;
2369 int err;
2370
2371 if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
2372 return -EAGAIN;
2373 }
2374
2375 buf = bt_hci_cmd_create(BT_HCI_OP_LE_REM_DEV_FROM_PER_ADV_LIST,
2376 sizeof(*cp));
2377 if (!buf) {
2378 return -ENOBUFS;
2379 }
2380
2381 cp = net_buf_add(buf, sizeof(*cp));
2382 bt_addr_le_copy(&cp->addr, addr);
2383 cp->sid = sid;
2384
2385 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_REM_DEV_FROM_PER_ADV_LIST, buf,
2386 NULL);
2387 if (err) {
2388 LOG_ERR("Failed to remove device from periodic advertiser list");
2389 return err;
2390 }
2391
2392 return 0;
2393 }
2394
bt_le_per_adv_list_clear(void)2395 int bt_le_per_adv_list_clear(void)
2396 {
2397 int err;
2398
2399 if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
2400 return -EAGAIN;
2401 }
2402
2403 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_CLEAR_PER_ADV_LIST, NULL, NULL);
2404 if (err) {
2405 LOG_ERR("Failed to clear periodic advertiser list");
2406 return err;
2407 }
2408
2409 return 0;
2410 }
2411 #endif /* defined(CONFIG_BT_PER_ADV_SYNC) */
2412
bt_le_explicit_scanner_running(void)2413 bool bt_le_explicit_scanner_running(void)
2414 {
2415 return atomic_test_bit(scan_state.scan_flags, BT_LE_SCAN_USER_EXPLICIT_SCAN);
2416 }
2417
bt_le_explicit_scanner_uses_same_params(const struct bt_conn_le_create_param * create_param)2418 bool bt_le_explicit_scanner_uses_same_params(const struct bt_conn_le_create_param *create_param)
2419 {
2420 if (scan_state.explicit_scan_param.window != create_param->window ||
2421 scan_state.explicit_scan_param.interval != create_param->interval){
2422 return false;
2423 }
2424
2425 if (scan_state.explicit_scan_param.options & BT_LE_SCAN_OPT_CODED) {
2426 if (scan_state.explicit_scan_param.window_coded != create_param->window_coded ||
2427 scan_state.explicit_scan_param.interval_coded != create_param->interval_coded){
2428 return false;
2429 }
2430 }
2431
2432 return true;
2433 }
2434