1 /* conn.c - Bluetooth connection handling */
2
3 /*
4 * Copyright (c) 2015-2016 Intel Corporation
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9 #include <zephyr/kernel.h>
10 #include <string.h>
11 #include <errno.h>
12 #include <stdbool.h>
13 #include <zephyr/sys/atomic.h>
14 #include <zephyr/sys/byteorder.h>
15 #include <zephyr/sys/check.h>
16 #include <zephyr/sys/iterable_sections.h>
17 #include <zephyr/sys/util.h>
18 #include <zephyr/sys/util_macro.h>
19 #include <zephyr/sys/slist.h>
20 #include <zephyr/debug/stack.h>
21 #include <zephyr/sys/__assert.h>
22
23 #include <zephyr/bluetooth/hci.h>
24 #include <zephyr/bluetooth/bluetooth.h>
25 #include <zephyr/bluetooth/direction.h>
26 #include <zephyr/bluetooth/conn.h>
27 #include <zephyr/drivers/bluetooth/hci_driver.h>
28 #include <zephyr/bluetooth/att.h>
29
30 #include "common/assert.h"
31 #include "common/bt_str.h"
32
33 #include "addr_internal.h"
34 #include "hci_core.h"
35 #include "id.h"
36 #include "adv.h"
37 #include "conn_internal.h"
38 #include "l2cap_internal.h"
39 #include "keys.h"
40 #include "smp.h"
41 #include "ssp.h"
42 #include "att_internal.h"
43 #include "iso_internal.h"
44 #include "direction_internal.h"
45
46 #define LOG_LEVEL CONFIG_BT_CONN_LOG_LEVEL
47 #include <zephyr/logging/log.h>
48 LOG_MODULE_REGISTER(bt_conn);
49
50 struct tx_meta {
51 struct bt_conn_tx *tx;
52 /* This flag indicates if the current buffer has already been partially
53 * sent to the controller (ie, the next fragments should be sent as
54 * continuations).
55 */
56 bool is_cont;
57 /* Indicates whether the ISO PDU contains a timestamp */
58 bool iso_has_ts;
59 };
60
61 BUILD_ASSERT(sizeof(struct tx_meta) == CONFIG_BT_CONN_TX_USER_DATA_SIZE,
62 "User data size is wrong!");
63
64 #define tx_data(buf) ((struct tx_meta *)net_buf_user_data(buf))
65 K_FIFO_DEFINE(free_tx);
66
67 static void tx_free(struct bt_conn_tx *tx);
68
conn_tx_destroy(struct bt_conn * conn,struct bt_conn_tx * tx)69 static void conn_tx_destroy(struct bt_conn *conn, struct bt_conn_tx *tx)
70 {
71 __ASSERT_NO_MSG(tx);
72
73 bt_conn_tx_cb_t cb = tx->cb;
74 void *user_data = tx->user_data;
75
76 /* Free up TX metadata before calling callback in case the callback
77 * tries to allocate metadata
78 */
79 tx_free(tx);
80
81 cb(conn, user_data, -ESHUTDOWN);
82 }
83
84 #if defined(CONFIG_BT_CONN_TX)
85 static void tx_complete_work(struct k_work *work);
86 #endif /* CONFIG_BT_CONN_TX */
87
88 /* Group Connected BT_CONN only in this */
89 #if defined(CONFIG_BT_CONN)
90 /* Peripheral timeout to initialize Connection Parameter Update procedure */
91 #define CONN_UPDATE_TIMEOUT K_MSEC(CONFIG_BT_CONN_PARAM_UPDATE_TIMEOUT)
92
93 static void deferred_work(struct k_work *work);
94 static void notify_connected(struct bt_conn *conn);
95
96 static struct bt_conn acl_conns[CONFIG_BT_MAX_CONN];
97 NET_BUF_POOL_DEFINE(acl_tx_pool, CONFIG_BT_L2CAP_TX_BUF_COUNT,
98 BT_L2CAP_BUF_SIZE(CONFIG_BT_L2CAP_TX_MTU),
99 CONFIG_BT_CONN_TX_USER_DATA_SIZE, NULL);
100
101 #if CONFIG_BT_L2CAP_TX_FRAG_COUNT > 0
102 /* Dedicated pool for fragment buffers in case queued up TX buffers don't
103 * fit the controllers buffer size. We can't use the acl_tx_pool for the
104 * fragmentation, since it's possible that pool is empty and all buffers
105 * are queued up in the TX queue. In such a situation, trying to allocate
106 * another buffer from the acl_tx_pool would result in a deadlock.
107 */
108 NET_BUF_POOL_FIXED_DEFINE(frag_pool, CONFIG_BT_L2CAP_TX_FRAG_COUNT,
109 BT_BUF_ACL_SIZE(CONFIG_BT_BUF_ACL_TX_SIZE),
110 CONFIG_BT_CONN_TX_USER_DATA_SIZE, NULL);
111
112 #endif /* CONFIG_BT_L2CAP_TX_FRAG_COUNT > 0 */
113
114 #if defined(CONFIG_BT_SMP) || defined(CONFIG_BT_BREDR)
115 const struct bt_conn_auth_cb *bt_auth;
116 sys_slist_t bt_auth_info_cbs = SYS_SLIST_STATIC_INIT(&bt_auth_info_cbs);
117 #endif /* CONFIG_BT_SMP || CONFIG_BT_BREDR */
118
119 static struct bt_conn_cb *callback_list;
120
121 static struct bt_conn_tx conn_tx[CONFIG_BT_CONN_TX_MAX];
122
123 #if defined(CONFIG_BT_BREDR)
124 static int bt_hci_connect_br_cancel(struct bt_conn *conn);
125
126 static struct bt_conn sco_conns[CONFIG_BT_MAX_SCO_CONN];
127 #endif /* CONFIG_BT_BREDR */
128 #endif /* CONFIG_BT_CONN */
129
130 #if defined(CONFIG_BT_ISO)
131 extern struct bt_conn iso_conns[CONFIG_BT_ISO_MAX_CHAN];
132
133 /* Callback TX buffers for ISO */
134 static struct bt_conn_tx iso_tx[CONFIG_BT_ISO_TX_BUF_COUNT];
135
bt_conn_iso_init(void)136 int bt_conn_iso_init(void)
137 {
138 for (size_t i = 0; i < ARRAY_SIZE(iso_tx); i++) {
139 k_fifo_put(&free_tx, &iso_tx[i]);
140 }
141
142 return 0;
143 }
144 #endif /* CONFIG_BT_ISO */
145
bt_conn_get_pkts(struct bt_conn * conn)146 struct k_sem *bt_conn_get_pkts(struct bt_conn *conn)
147 {
148 #if defined(CONFIG_BT_BREDR)
149 if (conn->type == BT_CONN_TYPE_BR || !bt_dev.le.acl_mtu) {
150 return &bt_dev.br.pkts;
151 }
152 #endif /* CONFIG_BT_BREDR */
153
154 #if defined(CONFIG_BT_ISO)
155 /* Use ISO pkts semaphore if LE Read Buffer Size command returned
156 * dedicated ISO buffers.
157 */
158 if (conn->type == BT_CONN_TYPE_ISO) {
159 if (bt_dev.le.iso_mtu && bt_dev.le.iso_limit != 0) {
160 return &bt_dev.le.iso_pkts;
161 }
162
163 return NULL;
164 }
165 #endif /* CONFIG_BT_ISO */
166
167 #if defined(CONFIG_BT_CONN)
168 if (bt_dev.le.acl_mtu) {
169 return &bt_dev.le.acl_pkts;
170 }
171 #endif /* CONFIG_BT_CONN */
172
173 return NULL;
174 }
175
state2str(bt_conn_state_t state)176 static inline const char *state2str(bt_conn_state_t state)
177 {
178 switch (state) {
179 case BT_CONN_DISCONNECTED:
180 return "disconnected";
181 case BT_CONN_DISCONNECT_COMPLETE:
182 return "disconnect-complete";
183 case BT_CONN_CONNECTING_SCAN:
184 return "connecting-scan";
185 case BT_CONN_CONNECTING_DIR_ADV:
186 return "connecting-dir-adv";
187 case BT_CONN_CONNECTING_ADV:
188 return "connecting-adv";
189 case BT_CONN_CONNECTING_AUTO:
190 return "connecting-auto";
191 case BT_CONN_CONNECTING:
192 return "connecting";
193 case BT_CONN_CONNECTED:
194 return "connected";
195 case BT_CONN_DISCONNECTING:
196 return "disconnecting";
197 default:
198 return "(unknown)";
199 }
200 }
201
tx_free(struct bt_conn_tx * tx)202 static void tx_free(struct bt_conn_tx *tx)
203 {
204 tx->cb = NULL;
205 tx->user_data = NULL;
206 tx->pending_no_cb = 0U;
207 k_fifo_put(&free_tx, tx);
208 }
209
tx_notify(struct bt_conn * conn)210 static void tx_notify(struct bt_conn *conn)
211 {
212 LOG_DBG("conn %p", conn);
213
214 while (1) {
215 struct bt_conn_tx *tx = NULL;
216 unsigned int key;
217 bt_conn_tx_cb_t cb;
218 void *user_data;
219
220 key = irq_lock();
221 if (!sys_slist_is_empty(&conn->tx_complete)) {
222 tx = CONTAINER_OF(sys_slist_get_not_empty(&conn->tx_complete),
223 struct bt_conn_tx, node);
224 }
225 irq_unlock(key);
226
227 if (!tx) {
228 return;
229 }
230
231 LOG_DBG("tx %p cb %p user_data %p", tx, tx->cb, tx->user_data);
232
233 /* Copy over the params */
234 cb = tx->cb;
235 user_data = tx->user_data;
236
237 /* Free up TX notify since there may be user waiting */
238 tx_free(tx);
239
240 /* Run the callback, at this point it should be safe to
241 * allocate new buffers since the TX should have been
242 * unblocked by tx_free.
243 */
244 cb(conn, user_data, 0);
245 }
246 }
247
bt_conn_new(struct bt_conn * conns,size_t size)248 struct bt_conn *bt_conn_new(struct bt_conn *conns, size_t size)
249 {
250 struct bt_conn *conn = NULL;
251 int i;
252
253 for (i = 0; i < size; i++) {
254 if (atomic_cas(&conns[i].ref, 0, 1)) {
255 conn = &conns[i];
256 break;
257 }
258 }
259
260 if (!conn) {
261 return NULL;
262 }
263
264 (void)memset(conn, 0, offsetof(struct bt_conn, ref));
265
266 #if defined(CONFIG_BT_CONN)
267 k_work_init_delayable(&conn->deferred_work, deferred_work);
268 #endif /* CONFIG_BT_CONN */
269 #if defined(CONFIG_BT_CONN_TX)
270 k_work_init(&conn->tx_complete_work, tx_complete_work);
271 #endif /* CONFIG_BT_CONN_TX */
272
273 return conn;
274 }
275
bt_conn_reset_rx_state(struct bt_conn * conn)276 void bt_conn_reset_rx_state(struct bt_conn *conn)
277 {
278 if (!conn->rx) {
279 return;
280 }
281
282 net_buf_unref(conn->rx);
283 conn->rx = NULL;
284 }
285
bt_acl_recv(struct bt_conn * conn,struct net_buf * buf,uint8_t flags)286 static void bt_acl_recv(struct bt_conn *conn, struct net_buf *buf,
287 uint8_t flags)
288 {
289 uint16_t acl_total_len;
290
291 /* Check packet boundary flags */
292 switch (flags) {
293 case BT_ACL_START:
294 if (conn->rx) {
295 LOG_ERR("Unexpected first L2CAP frame");
296 bt_conn_reset_rx_state(conn);
297 }
298
299 LOG_DBG("First, len %u final %u", buf->len,
300 (buf->len < sizeof(uint16_t)) ? 0 : sys_get_le16(buf->data));
301
302 conn->rx = buf;
303 break;
304 case BT_ACL_CONT:
305 if (!conn->rx) {
306 LOG_ERR("Unexpected L2CAP continuation");
307 bt_conn_reset_rx_state(conn);
308 net_buf_unref(buf);
309 return;
310 }
311
312 if (!buf->len) {
313 LOG_DBG("Empty ACL_CONT");
314 net_buf_unref(buf);
315 return;
316 }
317
318 if (buf->len > net_buf_tailroom(conn->rx)) {
319 LOG_ERR("Not enough buffer space for L2CAP data");
320
321 /* Frame is not complete but we still pass it to L2CAP
322 * so that it may handle error on protocol level
323 * eg disconnect channel.
324 */
325 bt_l2cap_recv(conn, conn->rx, false);
326 conn->rx = NULL;
327 net_buf_unref(buf);
328 return;
329 }
330
331 net_buf_add_mem(conn->rx, buf->data, buf->len);
332 net_buf_unref(buf);
333 break;
334 default:
335 /* BT_ACL_START_NO_FLUSH and BT_ACL_COMPLETE are not allowed on
336 * LE-U from Controller to Host.
337 * Only BT_ACL_POINT_TO_POINT is supported.
338 */
339 LOG_ERR("Unexpected ACL flags (0x%02x)", flags);
340 bt_conn_reset_rx_state(conn);
341 net_buf_unref(buf);
342 return;
343 }
344
345 if (conn->rx->len < sizeof(uint16_t)) {
346 /* Still not enough data received to retrieve the L2CAP header
347 * length field.
348 */
349 return;
350 }
351
352 acl_total_len = sys_get_le16(conn->rx->data) + sizeof(struct bt_l2cap_hdr);
353
354 if (conn->rx->len < acl_total_len) {
355 /* L2CAP frame not complete. */
356 return;
357 }
358
359 if (conn->rx->len > acl_total_len) {
360 LOG_ERR("ACL len mismatch (%u > %u)", conn->rx->len, acl_total_len);
361 bt_conn_reset_rx_state(conn);
362 return;
363 }
364
365 /* L2CAP frame complete. */
366 buf = conn->rx;
367 conn->rx = NULL;
368
369 LOG_DBG("Successfully parsed %u byte L2CAP packet", buf->len);
370 bt_l2cap_recv(conn, buf, true);
371 }
372
bt_conn_recv(struct bt_conn * conn,struct net_buf * buf,uint8_t flags)373 void bt_conn_recv(struct bt_conn *conn, struct net_buf *buf, uint8_t flags)
374 {
375 /* Make sure we notify any pending TX callbacks before processing
376 * new data for this connection.
377 */
378 tx_notify(conn);
379
380 LOG_DBG("handle %u len %u flags %02x", conn->handle, buf->len, flags);
381
382 if ((IS_ENABLED(CONFIG_BT_ISO_UNICAST) ||
383 IS_ENABLED(CONFIG_BT_ISO_SYNC_RECEIVER)) &&
384 conn->type == BT_CONN_TYPE_ISO) {
385 bt_iso_recv(conn, buf, flags);
386 return;
387 } else if (IS_ENABLED(CONFIG_BT_CONN)) {
388 bt_acl_recv(conn, buf, flags);
389 } else {
390 __ASSERT(false, "Invalid connection type %u", conn->type);
391 }
392 }
393
conn_tx_alloc(void)394 static struct bt_conn_tx *conn_tx_alloc(void)
395 {
396 /* The TX context always get freed in the system workqueue,
397 * so if we're in the same workqueue but there are no immediate
398 * contexts available, there's no chance we'll get one by waiting.
399 */
400 if (k_current_get() == &k_sys_work_q.thread) {
401 return k_fifo_get(&free_tx, K_NO_WAIT);
402 }
403
404 if (IS_ENABLED(CONFIG_BT_CONN_LOG_LEVEL_DBG)) {
405 struct bt_conn_tx *tx = k_fifo_get(&free_tx, K_NO_WAIT);
406
407 if (tx) {
408 return tx;
409 }
410
411 LOG_WRN("Unable to get an immediate free conn_tx");
412 }
413
414 return k_fifo_get(&free_tx, K_FOREVER);
415 }
416
bt_conn_send_iso_cb(struct bt_conn * conn,struct net_buf * buf,bt_conn_tx_cb_t cb,bool has_ts)417 int bt_conn_send_iso_cb(struct bt_conn *conn, struct net_buf *buf,
418 bt_conn_tx_cb_t cb, bool has_ts)
419 {
420 if (buf->user_data_size < CONFIG_BT_CONN_TX_USER_DATA_SIZE) {
421 LOG_ERR("not enough room in user_data %d < %d",
422 buf->user_data_size,
423 CONFIG_BT_CONN_TX_USER_DATA_SIZE);
424 return -EINVAL;
425 }
426
427 /* Necessary for setting the TS_Flag bit when we pop the buffer from the
428 * send queue. The flag needs to be set before adding the buffer to the queue.
429 */
430 tx_data(buf)->iso_has_ts = has_ts;
431
432 int err = bt_conn_send_cb(conn, buf, cb, NULL);
433
434 if (err) {
435 return err;
436 }
437
438 return 0;
439 }
440
bt_conn_send_cb(struct bt_conn * conn,struct net_buf * buf,bt_conn_tx_cb_t cb,void * user_data)441 int bt_conn_send_cb(struct bt_conn *conn, struct net_buf *buf,
442 bt_conn_tx_cb_t cb, void *user_data)
443 {
444 struct bt_conn_tx *tx;
445
446 LOG_DBG("conn handle %u buf len %u cb %p user_data %p", conn->handle, buf->len, cb,
447 user_data);
448
449 if (buf->user_data_size < CONFIG_BT_CONN_TX_USER_DATA_SIZE) {
450 LOG_ERR("not enough room in user_data %d < %d",
451 buf->user_data_size,
452 CONFIG_BT_CONN_TX_USER_DATA_SIZE);
453 return -EINVAL;
454 }
455
456 if (conn->state != BT_CONN_CONNECTED) {
457 LOG_ERR("not connected!");
458 return -ENOTCONN;
459 }
460
461 if (cb) {
462 tx = conn_tx_alloc();
463 if (!tx) {
464 LOG_DBG("Unable to allocate TX context");
465 return -ENOBUFS;
466 }
467
468 /* Verify that we're still connected after blocking */
469 if (conn->state != BT_CONN_CONNECTED) {
470 LOG_WRN("Disconnected while allocating context");
471 tx_free(tx);
472 return -ENOTCONN;
473 }
474
475 tx->cb = cb;
476 tx->user_data = user_data;
477 tx->pending_no_cb = 0U;
478
479 tx_data(buf)->tx = tx;
480 } else {
481 tx_data(buf)->tx = NULL;
482 }
483
484 tx_data(buf)->is_cont = false;
485
486 net_buf_put(&conn->tx_queue, buf);
487 return 0;
488 }
489
490 enum {
491 FRAG_START,
492 FRAG_CONT,
493 FRAG_SINGLE,
494 FRAG_END
495 };
496
send_acl(struct bt_conn * conn,struct net_buf * buf,uint8_t flags)497 static int send_acl(struct bt_conn *conn, struct net_buf *buf, uint8_t flags)
498 {
499 struct bt_hci_acl_hdr *hdr;
500
501 switch (flags) {
502 case FRAG_START:
503 case FRAG_SINGLE:
504 flags = BT_ACL_START_NO_FLUSH;
505 break;
506 case FRAG_CONT:
507 case FRAG_END:
508 flags = BT_ACL_CONT;
509 break;
510 default:
511 return -EINVAL;
512 }
513
514 hdr = net_buf_push(buf, sizeof(*hdr));
515 hdr->handle = sys_cpu_to_le16(bt_acl_handle_pack(conn->handle, flags));
516 hdr->len = sys_cpu_to_le16(buf->len - sizeof(*hdr));
517
518 bt_buf_set_type(buf, BT_BUF_ACL_OUT);
519
520 return bt_send(buf);
521 }
522
send_iso(struct bt_conn * conn,struct net_buf * buf,uint8_t flags)523 static int send_iso(struct bt_conn *conn, struct net_buf *buf, uint8_t flags)
524 {
525 struct bt_hci_iso_hdr *hdr;
526 bool ts;
527
528 switch (flags) {
529 case FRAG_START:
530 flags = BT_ISO_START;
531 break;
532 case FRAG_CONT:
533 flags = BT_ISO_CONT;
534 break;
535 case FRAG_SINGLE:
536 flags = BT_ISO_SINGLE;
537 break;
538 case FRAG_END:
539 flags = BT_ISO_END;
540 break;
541 default:
542 return -EINVAL;
543 }
544
545 hdr = net_buf_push(buf, sizeof(*hdr));
546
547 ts = tx_data(buf)->iso_has_ts &&
548 (flags == BT_ISO_START || flags == BT_ISO_SINGLE);
549
550 hdr->handle = sys_cpu_to_le16(bt_iso_handle_pack(conn->handle, flags, ts));
551
552 hdr->len = sys_cpu_to_le16(buf->len - sizeof(*hdr));
553
554 bt_buf_set_type(buf, BT_BUF_ISO_OUT);
555
556 return bt_send(buf);
557 }
558
conn_mtu(struct bt_conn * conn)559 static inline uint16_t conn_mtu(struct bt_conn *conn)
560 {
561 #if defined(CONFIG_BT_BREDR)
562 if (conn->type == BT_CONN_TYPE_BR ||
563 (conn->type != BT_CONN_TYPE_ISO && !bt_dev.le.acl_mtu)) {
564 return bt_dev.br.mtu;
565 }
566 #endif /* CONFIG_BT_BREDR */
567 #if defined(CONFIG_BT_ISO)
568 if (conn->type == BT_CONN_TYPE_ISO) {
569 return bt_dev.le.iso_mtu;
570 }
571 #endif /* CONFIG_BT_ISO */
572 #if defined(CONFIG_BT_CONN)
573 return bt_dev.le.acl_mtu;
574 #else
575 return 0;
576 #endif /* CONFIG_BT_CONN */
577 }
578
do_send_frag(struct bt_conn * conn,struct net_buf * buf,uint8_t flags)579 static int do_send_frag(struct bt_conn *conn, struct net_buf *buf, uint8_t flags)
580 {
581 struct bt_conn_tx *tx = tx_data(buf)->tx;
582 uint32_t *pending_no_cb = NULL;
583 unsigned int key;
584 int err = 0;
585
586 /* Check for disconnection while waiting for pkts_sem */
587 if (conn->state != BT_CONN_CONNECTED) {
588 err = -ENOTCONN;
589 goto fail;
590 }
591
592 LOG_DBG("conn %p buf %p len %u flags 0x%02x", conn, buf, buf->len,
593 flags);
594
595 /* Add to pending, it must be done before bt_buf_set_type */
596 key = irq_lock();
597 if (tx) {
598 sys_slist_append(&conn->tx_pending, &tx->node);
599 } else {
600 struct bt_conn_tx *tail_tx;
601
602 tail_tx = (void *)sys_slist_peek_tail(&conn->tx_pending);
603 if (tail_tx) {
604 pending_no_cb = &tail_tx->pending_no_cb;
605 } else {
606 pending_no_cb = &conn->pending_no_cb;
607 }
608
609 (*pending_no_cb)++;
610 }
611 irq_unlock(key);
612
613 if (IS_ENABLED(CONFIG_BT_ISO) && conn->type == BT_CONN_TYPE_ISO) {
614 err = send_iso(conn, buf, flags);
615 } else if (IS_ENABLED(CONFIG_BT_CONN)) {
616 err = send_acl(conn, buf, flags);
617 } else {
618 __ASSERT(false, "Invalid connection type %u", conn->type);
619 }
620
621 if (err) {
622 LOG_ERR("Unable to send to driver (err %d)", err);
623 key = irq_lock();
624 /* Roll back the pending TX info */
625 if (tx) {
626 sys_slist_find_and_remove(&conn->tx_pending, &tx->node);
627 } else {
628 __ASSERT_NO_MSG(*pending_no_cb > 0);
629 (*pending_no_cb)--;
630 }
631 irq_unlock(key);
632
633 /* We don't want to end up in a situation where send_acl/iso
634 * returns the same error code as when we don't get a buffer in
635 * time.
636 */
637 err = -EIO;
638 goto fail;
639 }
640
641 return 0;
642
643 fail:
644 /* If we get here, something has seriously gone wrong:
645 * We also need to destroy the `parent` buf.
646 */
647 k_sem_give(bt_conn_get_pkts(conn));
648 if (tx) {
649 /* `buf` might not get destroyed, and its `tx` pointer will still be reachable.
650 * Make sure that we don't try to use the destroyed context later.
651 */
652 tx_data(buf)->tx = NULL;
653 conn_tx_destroy(conn, tx);
654 }
655
656 return err;
657 }
658
send_frag(struct bt_conn * conn,struct net_buf * buf,struct net_buf * frag,uint8_t flags)659 static int send_frag(struct bt_conn *conn,
660 struct net_buf *buf, struct net_buf *frag,
661 uint8_t flags)
662 {
663 /* Check if the controller can accept ACL packets */
664 if (k_sem_take(bt_conn_get_pkts(conn), K_NO_WAIT)) {
665 LOG_DBG("no controller bufs");
666 return -ENOBUFS;
667 }
668
669 /* Add the data to the buffer */
670 if (frag) {
671 uint16_t frag_len = MIN(conn_mtu(conn), net_buf_tailroom(frag));
672
673 net_buf_add_mem(frag, buf->data, frag_len);
674 net_buf_pull(buf, frag_len);
675 } else {
676 /* De-queue the buffer now that we know we can send it.
677 * Only applies if the buffer to be sent is the original buffer,
678 * and not one of its fragments.
679 * This buffer was fetched from the FIFO using a peek operation.
680 */
681 buf = net_buf_get(&conn->tx_queue, K_NO_WAIT);
682 frag = buf;
683 }
684
685 return do_send_frag(conn, frag, flags);
686 }
687
create_frag(struct bt_conn * conn,struct net_buf * buf)688 static struct net_buf *create_frag(struct bt_conn *conn, struct net_buf *buf)
689 {
690 struct net_buf *frag;
691
692 switch (conn->type) {
693 #if defined(CONFIG_BT_ISO)
694 case BT_CONN_TYPE_ISO:
695 frag = bt_iso_create_frag(0);
696 break;
697 #endif
698 default:
699 #if defined(CONFIG_BT_CONN)
700 frag = bt_conn_create_frag(0);
701 #else
702 return NULL;
703 #endif /* CONFIG_BT_CONN */
704
705 }
706
707 if (conn->state != BT_CONN_CONNECTED) {
708 net_buf_unref(frag);
709 return NULL;
710 }
711
712 /* Fragments never have a TX completion callback */
713 tx_data(frag)->tx = NULL;
714 tx_data(frag)->is_cont = false;
715 tx_data(frag)->iso_has_ts = tx_data(buf)->iso_has_ts;
716
717 return frag;
718 }
719
send_buf(struct bt_conn * conn,struct net_buf * buf)720 static int send_buf(struct bt_conn *conn, struct net_buf *buf)
721 {
722 struct net_buf *frag;
723 uint8_t flags;
724 int err;
725
726 LOG_DBG("conn %p buf %p len %u", conn, buf, buf->len);
727
728 /* Send directly if the packet fits the ACL MTU */
729 if (buf->len <= conn_mtu(conn) && !tx_data(buf)->is_cont) {
730 LOG_DBG("send single");
731 return send_frag(conn, buf, NULL, FRAG_SINGLE);
732 }
733
734 LOG_DBG("start fragmenting");
735 /*
736 * Send the fragments. For the last one simply use the original
737 * buffer (which works since we've used net_buf_pull on it).
738 */
739 flags = FRAG_START;
740 if (tx_data(buf)->is_cont) {
741 flags = FRAG_CONT;
742 }
743
744 while (buf->len > conn_mtu(conn)) {
745 frag = create_frag(conn, buf);
746 if (!frag) {
747 return -ENOMEM;
748 }
749
750 err = send_frag(conn, buf, frag, flags);
751 if (err) {
752 LOG_DBG("%p failed, mark as existing frag", buf);
753 tx_data(buf)->is_cont = flags != FRAG_START;
754 net_buf_unref(frag);
755 return err;
756 }
757
758 flags = FRAG_CONT;
759 }
760
761 LOG_DBG("last frag");
762 tx_data(buf)->is_cont = true;
763 return send_frag(conn, buf, NULL, FRAG_END);
764 }
765
766 static struct k_poll_signal conn_change =
767 K_POLL_SIGNAL_INITIALIZER(conn_change);
768
conn_cleanup(struct bt_conn * conn)769 static void conn_cleanup(struct bt_conn *conn)
770 {
771 struct net_buf *buf;
772
773 /* Give back any allocated buffers */
774 while ((buf = net_buf_get(&conn->tx_queue, K_NO_WAIT))) {
775 struct bt_conn_tx *tx = tx_data(buf)->tx;
776
777 tx_data(buf)->tx = NULL;
778
779 /* destroy the buffer */
780 net_buf_unref(buf);
781
782 /* destroy the tx context (and any associated meta-data) */
783 if (tx) {
784 conn_tx_destroy(conn, tx);
785 }
786 }
787
788 __ASSERT(sys_slist_is_empty(&conn->tx_pending), "Pending TX packets");
789 __ASSERT_NO_MSG(conn->pending_no_cb == 0);
790
791 bt_conn_reset_rx_state(conn);
792
793 k_work_reschedule(&conn->deferred_work, K_NO_WAIT);
794 }
795
conn_destroy(struct bt_conn * conn,void * data)796 static void conn_destroy(struct bt_conn *conn, void *data)
797 {
798 if (conn->state == BT_CONN_CONNECTED ||
799 conn->state == BT_CONN_DISCONNECTING) {
800 bt_conn_set_state(conn, BT_CONN_DISCONNECT_COMPLETE);
801 }
802
803 bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
804 }
805
bt_conn_cleanup_all(void)806 void bt_conn_cleanup_all(void)
807 {
808 bt_conn_foreach(BT_CONN_TYPE_ALL, conn_destroy, NULL);
809 }
810
conn_prepare_events(struct bt_conn * conn,struct k_poll_event * events)811 static int conn_prepare_events(struct bt_conn *conn,
812 struct k_poll_event *events)
813 {
814 if (!atomic_get(&conn->ref)) {
815 return -ENOTCONN;
816 }
817
818 if (conn->state == BT_CONN_DISCONNECTED &&
819 atomic_test_and_clear_bit(conn->flags, BT_CONN_CLEANUP)) {
820 conn_cleanup(conn);
821 return -ENOTCONN;
822 }
823
824 if (conn->state != BT_CONN_CONNECTED) {
825 return -ENOTCONN;
826 }
827
828 LOG_DBG("Adding conn %p to poll list", conn);
829
830 /* ISO Synchronized Receiver only builds do not transmit and hence
831 * may not have any tx buffers allocated in a Controller.
832 */
833 struct k_sem *conn_pkts = bt_conn_get_pkts(conn);
834
835 if (!conn_pkts) {
836 return -ENOTCONN;
837 }
838
839 bool buffers_available = k_sem_count_get(conn_pkts) > 0;
840 bool packets_waiting = !k_fifo_is_empty(&conn->tx_queue);
841
842 if (packets_waiting && !buffers_available) {
843 /* Only resume sending when the controller has buffer space
844 * available for this connection.
845 */
846 LOG_DBG("wait on ctlr buffers");
847 k_poll_event_init(&events[0],
848 K_POLL_TYPE_SEM_AVAILABLE,
849 K_POLL_MODE_NOTIFY_ONLY,
850 conn_pkts);
851 } else {
852 /* Wait until there is more data to send. */
853 LOG_DBG("wait on host fifo");
854 k_poll_event_init(&events[0],
855 K_POLL_TYPE_FIFO_DATA_AVAILABLE,
856 K_POLL_MODE_NOTIFY_ONLY,
857 &conn->tx_queue);
858 }
859 events[0].tag = BT_EVENT_CONN_TX_QUEUE;
860
861 return 0;
862 }
863
bt_conn_prepare_events(struct k_poll_event events[])864 int bt_conn_prepare_events(struct k_poll_event events[])
865 {
866 int i, ev_count = 0;
867 struct bt_conn *conn;
868
869 LOG_DBG("");
870
871 k_poll_signal_init(&conn_change);
872
873 k_poll_event_init(&events[ev_count++], K_POLL_TYPE_SIGNAL,
874 K_POLL_MODE_NOTIFY_ONLY, &conn_change);
875
876 #if defined(CONFIG_BT_CONN)
877 for (i = 0; i < ARRAY_SIZE(acl_conns); i++) {
878 conn = &acl_conns[i];
879
880 if (!conn_prepare_events(conn, &events[ev_count])) {
881 ev_count++;
882 }
883 }
884 #endif /* CONFIG_BT_CONN */
885
886 #if defined(CONFIG_BT_ISO)
887 for (i = 0; i < ARRAY_SIZE(iso_conns); i++) {
888 conn = &iso_conns[i];
889
890 if (!conn_prepare_events(conn, &events[ev_count])) {
891 ev_count++;
892 }
893 }
894 #endif
895
896 return ev_count;
897 }
898
bt_conn_process_tx(struct bt_conn * conn)899 void bt_conn_process_tx(struct bt_conn *conn)
900 {
901 struct net_buf *buf;
902 int err;
903
904 LOG_DBG("conn %p", conn);
905
906 if (conn->state == BT_CONN_DISCONNECTED &&
907 atomic_test_and_clear_bit(conn->flags, BT_CONN_CLEANUP)) {
908 LOG_DBG("handle %u disconnected - cleaning up", conn->handle);
909 conn_cleanup(conn);
910 return;
911 }
912
913 /* Get next ACL packet for connection. The buffer will only get dequeued
914 * if there is a free controller buffer to put it in.
915 *
916 * Important: no operations should be done on `buf` until it is properly
917 * dequeued from the FIFO, using the `net_buf_get()` API.
918 */
919 buf = k_fifo_peek_head(&conn->tx_queue);
920 BT_ASSERT(buf);
921
922 /* Since we used `peek`, the queue still owns the reference to the
923 * buffer, so we need to take an explicit additional reference here.
924 */
925 buf = net_buf_ref(buf);
926 err = send_buf(conn, buf);
927 net_buf_unref(buf);
928
929 if (err == -EIO) {
930 struct bt_conn_tx *tx = tx_data(buf)->tx;
931
932 tx_data(buf)->tx = NULL;
933
934 /* destroy the buffer */
935 net_buf_unref(buf);
936
937 /* destroy the tx context (and any associated meta-data) */
938 if (tx) {
939 conn_tx_destroy(conn, tx);
940 }
941 }
942 }
943
process_unack_tx(struct bt_conn * conn)944 static void process_unack_tx(struct bt_conn *conn)
945 {
946 /* Return any unacknowledged packets */
947 while (1) {
948 struct bt_conn_tx *tx;
949 sys_snode_t *node;
950 unsigned int key;
951
952 key = irq_lock();
953
954 if (conn->pending_no_cb) {
955 conn->pending_no_cb--;
956 irq_unlock(key);
957 k_sem_give(bt_conn_get_pkts(conn));
958 continue;
959 }
960
961 node = sys_slist_get(&conn->tx_pending);
962 irq_unlock(key);
963
964 if (!node) {
965 break;
966 }
967
968 tx = CONTAINER_OF(node, struct bt_conn_tx, node);
969
970 key = irq_lock();
971 conn->pending_no_cb = tx->pending_no_cb;
972 tx->pending_no_cb = 0U;
973 irq_unlock(key);
974
975 conn_tx_destroy(conn, tx);
976
977 k_sem_give(bt_conn_get_pkts(conn));
978 }
979 }
980
conn_lookup_handle(struct bt_conn * conns,size_t size,uint16_t handle)981 struct bt_conn *conn_lookup_handle(struct bt_conn *conns, size_t size,
982 uint16_t handle)
983 {
984 int i;
985
986 for (i = 0; i < size; i++) {
987 struct bt_conn *conn = bt_conn_ref(&conns[i]);
988
989 if (!conn) {
990 continue;
991 }
992
993 /* We only care about connections with a valid handle */
994 if (!bt_conn_is_handle_valid(conn)) {
995 bt_conn_unref(conn);
996 continue;
997 }
998
999 if (conn->handle != handle) {
1000 bt_conn_unref(conn);
1001 continue;
1002 }
1003
1004 return conn;
1005 }
1006
1007 return NULL;
1008 }
1009
bt_conn_set_state(struct bt_conn * conn,bt_conn_state_t state)1010 void bt_conn_set_state(struct bt_conn *conn, bt_conn_state_t state)
1011 {
1012 bt_conn_state_t old_state;
1013
1014 LOG_DBG("%s -> %s", state2str(conn->state), state2str(state));
1015
1016 if (conn->state == state) {
1017 LOG_WRN("no transition %s", state2str(state));
1018 return;
1019 }
1020
1021 old_state = conn->state;
1022 conn->state = state;
1023
1024 /* Actions needed for exiting the old state */
1025 switch (old_state) {
1026 case BT_CONN_DISCONNECTED:
1027 /* Take a reference for the first state transition after
1028 * bt_conn_add_le() and keep it until reaching DISCONNECTED
1029 * again.
1030 */
1031 if (conn->type != BT_CONN_TYPE_ISO) {
1032 bt_conn_ref(conn);
1033 }
1034 break;
1035 case BT_CONN_CONNECTING:
1036 if (IS_ENABLED(CONFIG_BT_CENTRAL) &&
1037 conn->type == BT_CONN_TYPE_LE) {
1038 k_work_cancel_delayable(&conn->deferred_work);
1039 }
1040 break;
1041 default:
1042 break;
1043 }
1044
1045 /* Actions needed for entering the new state */
1046 switch (conn->state) {
1047 case BT_CONN_CONNECTED:
1048 if (conn->type == BT_CONN_TYPE_SCO) {
1049 /* TODO: Notify sco connected */
1050 break;
1051 }
1052 k_fifo_init(&conn->tx_queue);
1053 k_poll_signal_raise(&conn_change, 0);
1054
1055 if (IS_ENABLED(CONFIG_BT_ISO) &&
1056 conn->type == BT_CONN_TYPE_ISO) {
1057 bt_iso_connected(conn);
1058 break;
1059 }
1060
1061 #if defined(CONFIG_BT_CONN)
1062 sys_slist_init(&conn->channels);
1063
1064 if (IS_ENABLED(CONFIG_BT_PERIPHERAL) &&
1065 conn->role == BT_CONN_ROLE_PERIPHERAL) {
1066
1067 #if defined(CONFIG_BT_GAP_AUTO_UPDATE_CONN_PARAMS)
1068 if (conn->type == BT_CONN_TYPE_LE) {
1069 conn->le.conn_param_retry_countdown =
1070 CONFIG_BT_CONN_PARAM_RETRY_COUNT;
1071 }
1072 #endif /* CONFIG_BT_GAP_AUTO_UPDATE_CONN_PARAMS */
1073
1074 k_work_schedule(&conn->deferred_work,
1075 CONN_UPDATE_TIMEOUT);
1076 }
1077 #endif /* CONFIG_BT_CONN */
1078
1079 break;
1080 case BT_CONN_DISCONNECTED:
1081 #if defined(CONFIG_BT_CONN)
1082 if (conn->type == BT_CONN_TYPE_SCO) {
1083 /* TODO: Notify sco disconnected */
1084 bt_conn_unref(conn);
1085 break;
1086 }
1087
1088 /* Notify disconnection and queue a dummy buffer to wake
1089 * up and stop the tx thread for states where it was
1090 * running.
1091 */
1092 switch (old_state) {
1093 case BT_CONN_DISCONNECT_COMPLETE:
1094 tx_notify(conn);
1095
1096 /* Cancel Connection Update if it is pending */
1097 if ((conn->type == BT_CONN_TYPE_LE) &&
1098 (k_work_delayable_busy_get(&conn->deferred_work) &
1099 (K_WORK_QUEUED | K_WORK_DELAYED))) {
1100 k_work_cancel_delayable(&conn->deferred_work);
1101 }
1102
1103 atomic_set_bit(conn->flags, BT_CONN_CLEANUP);
1104 k_poll_signal_raise(&conn_change, 0);
1105 /* The last ref will be dropped during cleanup */
1106 break;
1107 case BT_CONN_CONNECTING:
1108 /* LE Create Connection command failed. This might be
1109 * directly from the API, don't notify application in
1110 * this case.
1111 */
1112 if (conn->err) {
1113 notify_connected(conn);
1114 }
1115
1116 bt_conn_unref(conn);
1117 break;
1118 case BT_CONN_CONNECTING_SCAN:
1119 /* this indicate LE Create Connection with peer address
1120 * has been stopped. This could either be triggered by
1121 * the application through bt_conn_disconnect or by
1122 * timeout set by bt_conn_le_create_param.timeout.
1123 */
1124 if (conn->err) {
1125 notify_connected(conn);
1126 }
1127
1128 bt_conn_unref(conn);
1129 break;
1130 case BT_CONN_CONNECTING_DIR_ADV:
1131 /* this indicate Directed advertising stopped */
1132 if (conn->err) {
1133 notify_connected(conn);
1134 }
1135
1136 bt_conn_unref(conn);
1137 break;
1138 case BT_CONN_CONNECTING_AUTO:
1139 /* this indicates LE Create Connection with filter
1140 * policy has been stopped. This can only be triggered
1141 * by the application, so don't notify.
1142 */
1143 bt_conn_unref(conn);
1144 break;
1145 case BT_CONN_CONNECTING_ADV:
1146 /* This can only happen when application stops the
1147 * advertiser, conn->err is never set in this case.
1148 */
1149 bt_conn_unref(conn);
1150 break;
1151 case BT_CONN_CONNECTED:
1152 case BT_CONN_DISCONNECTING:
1153 case BT_CONN_DISCONNECTED:
1154 /* Cannot happen. */
1155 LOG_WRN("Invalid (%u) old state", state);
1156 break;
1157 }
1158 break;
1159 case BT_CONN_CONNECTING_AUTO:
1160 break;
1161 case BT_CONN_CONNECTING_ADV:
1162 break;
1163 case BT_CONN_CONNECTING_SCAN:
1164 break;
1165 case BT_CONN_CONNECTING_DIR_ADV:
1166 break;
1167 case BT_CONN_CONNECTING:
1168 if (conn->type == BT_CONN_TYPE_SCO) {
1169 break;
1170 }
1171 /*
1172 * Timer is needed only for LE. For other link types controller
1173 * will handle connection timeout.
1174 */
1175 if (IS_ENABLED(CONFIG_BT_CENTRAL) &&
1176 conn->type == BT_CONN_TYPE_LE &&
1177 bt_dev.create_param.timeout != 0) {
1178 k_work_schedule(&conn->deferred_work,
1179 K_MSEC(10 * bt_dev.create_param.timeout));
1180 }
1181
1182 break;
1183 case BT_CONN_DISCONNECTING:
1184 break;
1185 #endif /* CONFIG_BT_CONN */
1186 case BT_CONN_DISCONNECT_COMPLETE:
1187 process_unack_tx(conn);
1188 break;
1189 default:
1190 LOG_WRN("no valid (%u) state was set", state);
1191
1192 break;
1193 }
1194 }
1195
bt_conn_lookup_handle(uint16_t handle,enum bt_conn_type type)1196 struct bt_conn *bt_conn_lookup_handle(uint16_t handle, enum bt_conn_type type)
1197 {
1198 struct bt_conn *conn;
1199
1200 #if defined(CONFIG_BT_CONN)
1201 conn = conn_lookup_handle(acl_conns, ARRAY_SIZE(acl_conns), handle);
1202 if (conn) {
1203 goto found;
1204 }
1205 #endif /* CONFIG_BT_CONN */
1206
1207 #if defined(CONFIG_BT_ISO)
1208 conn = conn_lookup_handle(iso_conns, ARRAY_SIZE(iso_conns), handle);
1209 if (conn) {
1210 goto found;
1211 }
1212 #endif
1213
1214 #if defined(CONFIG_BT_BREDR)
1215 conn = conn_lookup_handle(sco_conns, ARRAY_SIZE(sco_conns), handle);
1216 if (conn) {
1217 goto found;
1218 }
1219 #endif
1220
1221 found:
1222 if (conn) {
1223 if (type & conn->type) {
1224 return conn;
1225 }
1226 LOG_WRN("incompatible handle %u", handle);
1227 bt_conn_unref(conn);
1228 }
1229 return NULL;
1230 }
1231
bt_conn_foreach(enum bt_conn_type type,void (* func)(struct bt_conn * conn,void * data),void * data)1232 void bt_conn_foreach(enum bt_conn_type type,
1233 void (*func)(struct bt_conn *conn, void *data),
1234 void *data)
1235 {
1236 int i;
1237
1238 #if defined(CONFIG_BT_CONN)
1239 for (i = 0; i < ARRAY_SIZE(acl_conns); i++) {
1240 struct bt_conn *conn = bt_conn_ref(&acl_conns[i]);
1241
1242 if (!conn) {
1243 continue;
1244 }
1245
1246 if (!(conn->type & type)) {
1247 bt_conn_unref(conn);
1248 continue;
1249 }
1250
1251 func(conn, data);
1252 bt_conn_unref(conn);
1253 }
1254 #if defined(CONFIG_BT_BREDR)
1255 if (type & BT_CONN_TYPE_SCO) {
1256 for (i = 0; i < ARRAY_SIZE(sco_conns); i++) {
1257 struct bt_conn *conn = bt_conn_ref(&sco_conns[i]);
1258
1259 if (!conn) {
1260 continue;
1261 }
1262
1263 func(conn, data);
1264 bt_conn_unref(conn);
1265 }
1266 }
1267 #endif /* defined(CONFIG_BT_BREDR) */
1268 #endif /* CONFIG_BT_CONN */
1269
1270 #if defined(CONFIG_BT_ISO)
1271 if (type & BT_CONN_TYPE_ISO) {
1272 for (i = 0; i < ARRAY_SIZE(iso_conns); i++) {
1273 struct bt_conn *conn = bt_conn_ref(&iso_conns[i]);
1274
1275 if (!conn) {
1276 continue;
1277 }
1278
1279 func(conn, data);
1280 bt_conn_unref(conn);
1281 }
1282 }
1283 #endif /* defined(CONFIG_BT_ISO) */
1284 }
1285
bt_conn_ref(struct bt_conn * conn)1286 struct bt_conn *bt_conn_ref(struct bt_conn *conn)
1287 {
1288 atomic_val_t old;
1289
1290 __ASSERT_NO_MSG(conn);
1291
1292 /* Reference counter must be checked to avoid incrementing ref from
1293 * zero, then we should return NULL instead.
1294 * Loop on clear-and-set in case someone has modified the reference
1295 * count since the read, and start over again when that happens.
1296 */
1297 do {
1298 old = atomic_get(&conn->ref);
1299
1300 if (!old) {
1301 return NULL;
1302 }
1303 } while (!atomic_cas(&conn->ref, old, old + 1));
1304
1305 LOG_DBG("handle %u ref %ld -> %ld", conn->handle, old, old + 1);
1306
1307 return conn;
1308 }
1309
bt_conn_unref(struct bt_conn * conn)1310 void bt_conn_unref(struct bt_conn *conn)
1311 {
1312 atomic_val_t old;
1313
1314 old = atomic_dec(&conn->ref);
1315
1316 LOG_DBG("handle %u ref %ld -> %ld", conn->handle, old, atomic_get(&conn->ref));
1317
1318 __ASSERT(old > 0, "Conn reference counter is 0");
1319
1320 if (IS_ENABLED(CONFIG_BT_PERIPHERAL) && conn->type == BT_CONN_TYPE_LE &&
1321 conn->role == BT_CONN_ROLE_PERIPHERAL && atomic_get(&conn->ref) == 0) {
1322 bt_le_adv_resume();
1323 }
1324 }
1325
bt_conn_index(const struct bt_conn * conn)1326 uint8_t bt_conn_index(const struct bt_conn *conn)
1327 {
1328 ptrdiff_t index = 0;
1329
1330 switch (conn->type) {
1331 #if defined(CONFIG_BT_ISO)
1332 case BT_CONN_TYPE_ISO:
1333 index = conn - iso_conns;
1334 __ASSERT(index >= 0 && index < ARRAY_SIZE(iso_conns),
1335 "Invalid bt_conn pointer");
1336 break;
1337 #endif
1338 #if defined(CONFIG_BT_BREDR)
1339 case BT_CONN_TYPE_SCO:
1340 index = conn - sco_conns;
1341 __ASSERT(index >= 0 && index < ARRAY_SIZE(sco_conns),
1342 "Invalid bt_conn pointer");
1343 break;
1344 #endif
1345 default:
1346 #if defined(CONFIG_BT_CONN)
1347 index = conn - acl_conns;
1348 __ASSERT(index >= 0 && index < ARRAY_SIZE(acl_conns),
1349 "Invalid bt_conn pointer");
1350 #else
1351 __ASSERT(false, "Invalid connection type %u", conn->type);
1352 #endif /* CONFIG_BT_CONN */
1353 break;
1354 }
1355
1356 return (uint8_t)index;
1357 }
1358
1359
1360 #if defined(CONFIG_NET_BUF_LOG)
bt_conn_create_pdu_timeout_debug(struct net_buf_pool * pool,size_t reserve,k_timeout_t timeout,const char * func,int line)1361 struct net_buf *bt_conn_create_pdu_timeout_debug(struct net_buf_pool *pool,
1362 size_t reserve,
1363 k_timeout_t timeout,
1364 const char *func, int line)
1365 #else
1366 struct net_buf *bt_conn_create_pdu_timeout(struct net_buf_pool *pool,
1367 size_t reserve, k_timeout_t timeout)
1368 #endif
1369 {
1370 struct net_buf *buf;
1371
1372 /*
1373 * PDU must not be allocated from ISR as we block with 'K_FOREVER'
1374 * during the allocation
1375 */
1376 __ASSERT_NO_MSG(!k_is_in_isr());
1377
1378 if (!pool) {
1379 #if defined(CONFIG_BT_CONN)
1380 pool = &acl_tx_pool;
1381 #else
1382 return NULL;
1383 #endif /* CONFIG_BT_CONN */
1384 }
1385
1386 if (IS_ENABLED(CONFIG_BT_CONN_LOG_LEVEL_DBG)) {
1387 #if defined(CONFIG_NET_BUF_LOG)
1388 buf = net_buf_alloc_fixed_debug(pool, K_NO_WAIT, func, line);
1389 #else
1390 buf = net_buf_alloc(pool, K_NO_WAIT);
1391 #endif
1392 if (!buf) {
1393 LOG_WRN("Unable to allocate buffer with K_NO_WAIT");
1394 #if defined(CONFIG_NET_BUF_LOG)
1395 buf = net_buf_alloc_fixed_debug(pool, timeout, func,
1396 line);
1397 #else
1398 buf = net_buf_alloc(pool, timeout);
1399 #endif
1400 }
1401 } else {
1402 #if defined(CONFIG_NET_BUF_LOG)
1403 buf = net_buf_alloc_fixed_debug(pool, timeout, func,
1404 line);
1405 #else
1406 buf = net_buf_alloc(pool, timeout);
1407 #endif
1408 }
1409
1410 if (!buf) {
1411 LOG_WRN("Unable to allocate buffer within timeout");
1412 return NULL;
1413 }
1414
1415 reserve += sizeof(struct bt_hci_acl_hdr) + BT_BUF_RESERVE;
1416 net_buf_reserve(buf, reserve);
1417
1418 return buf;
1419 }
1420
1421 #if defined(CONFIG_BT_CONN_TX)
tx_complete_work(struct k_work * work)1422 static void tx_complete_work(struct k_work *work)
1423 {
1424 struct bt_conn *conn = CONTAINER_OF(work, struct bt_conn,
1425 tx_complete_work);
1426
1427 LOG_DBG("conn %p", conn);
1428
1429 tx_notify(conn);
1430 }
1431 #endif /* CONFIG_BT_CONN_TX */
1432
1433 /* Group Connected BT_CONN only in this */
1434 #if defined(CONFIG_BT_CONN)
1435
bt_conn_connected(struct bt_conn * conn)1436 void bt_conn_connected(struct bt_conn *conn)
1437 {
1438 bt_l2cap_connected(conn);
1439 notify_connected(conn);
1440 }
1441
conn_disconnect(struct bt_conn * conn,uint8_t reason)1442 static int conn_disconnect(struct bt_conn *conn, uint8_t reason)
1443 {
1444 int err;
1445
1446 err = bt_hci_disconnect(conn->handle, reason);
1447 if (err) {
1448 return err;
1449 }
1450
1451 if (conn->state == BT_CONN_CONNECTED) {
1452 bt_conn_set_state(conn, BT_CONN_DISCONNECTING);
1453 }
1454
1455 return 0;
1456 }
1457
bt_conn_disconnect(struct bt_conn * conn,uint8_t reason)1458 int bt_conn_disconnect(struct bt_conn *conn, uint8_t reason)
1459 {
1460 /* Disconnection is initiated by us, so auto connection shall
1461 * be disabled. Otherwise the passive scan would be enabled
1462 * and we could send LE Create Connection as soon as the remote
1463 * starts advertising.
1464 */
1465 #if !defined(CONFIG_BT_FILTER_ACCEPT_LIST)
1466 if (IS_ENABLED(CONFIG_BT_CENTRAL) &&
1467 conn->type == BT_CONN_TYPE_LE) {
1468 bt_le_set_auto_conn(&conn->le.dst, NULL);
1469 }
1470 #endif /* !defined(CONFIG_BT_FILTER_ACCEPT_LIST) */
1471
1472 switch (conn->state) {
1473 case BT_CONN_CONNECTING_SCAN:
1474 conn->err = reason;
1475 bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
1476 if (IS_ENABLED(CONFIG_BT_CENTRAL)) {
1477 bt_le_scan_update(false);
1478 }
1479 return 0;
1480 case BT_CONN_CONNECTING:
1481 if (conn->type == BT_CONN_TYPE_LE) {
1482 if (IS_ENABLED(CONFIG_BT_CENTRAL)) {
1483 k_work_cancel_delayable(&conn->deferred_work);
1484 return bt_le_create_conn_cancel();
1485 }
1486 }
1487 #if defined(CONFIG_BT_ISO)
1488 else if (conn->type == BT_CONN_TYPE_ISO) {
1489 return conn_disconnect(conn, reason);
1490 }
1491 #endif /* CONFIG_BT_ISO */
1492 #if defined(CONFIG_BT_BREDR)
1493 else if (conn->type == BT_CONN_TYPE_BR) {
1494 return bt_hci_connect_br_cancel(conn);
1495 }
1496 #endif /* CONFIG_BT_BREDR */
1497 else {
1498 __ASSERT(false, "Invalid conn type %u", conn->type);
1499 }
1500
1501 return 0;
1502 case BT_CONN_CONNECTED:
1503 return conn_disconnect(conn, reason);
1504 case BT_CONN_DISCONNECTING:
1505 return 0;
1506 case BT_CONN_DISCONNECTED:
1507 default:
1508 return -ENOTCONN;
1509 }
1510 }
1511
notify_connected(struct bt_conn * conn)1512 static void notify_connected(struct bt_conn *conn)
1513 {
1514 for (struct bt_conn_cb *cb = callback_list; cb; cb = cb->_next) {
1515 if (cb->connected) {
1516 cb->connected(conn, conn->err);
1517 }
1518 }
1519
1520 STRUCT_SECTION_FOREACH(bt_conn_cb, cb) {
1521 if (cb->connected) {
1522 cb->connected(conn, conn->err);
1523 }
1524 }
1525 }
1526
notify_disconnected(struct bt_conn * conn)1527 static void notify_disconnected(struct bt_conn *conn)
1528 {
1529 for (struct bt_conn_cb *cb = callback_list; cb; cb = cb->_next) {
1530 if (cb->disconnected) {
1531 cb->disconnected(conn, conn->err);
1532 }
1533 }
1534
1535 STRUCT_SECTION_FOREACH(bt_conn_cb, cb) {
1536 if (cb->disconnected) {
1537 cb->disconnected(conn, conn->err);
1538 }
1539 }
1540 }
1541
1542 #if defined(CONFIG_BT_REMOTE_INFO)
notify_remote_info(struct bt_conn * conn)1543 void notify_remote_info(struct bt_conn *conn)
1544 {
1545 struct bt_conn_remote_info remote_info;
1546 int err;
1547
1548 err = bt_conn_get_remote_info(conn, &remote_info);
1549 if (err) {
1550 LOG_DBG("Notify remote info failed %d", err);
1551 return;
1552 }
1553
1554 for (struct bt_conn_cb *cb = callback_list; cb; cb = cb->_next) {
1555 if (cb->remote_info_available) {
1556 cb->remote_info_available(conn, &remote_info);
1557 }
1558 }
1559
1560 STRUCT_SECTION_FOREACH(bt_conn_cb, cb) {
1561 if (cb->remote_info_available) {
1562 cb->remote_info_available(conn, &remote_info);
1563 }
1564 }
1565 }
1566 #endif /* defined(CONFIG_BT_REMOTE_INFO) */
1567
notify_le_param_updated(struct bt_conn * conn)1568 void notify_le_param_updated(struct bt_conn *conn)
1569 {
1570 /* If new connection parameters meet requirement of pending
1571 * parameters don't send peripheral conn param request anymore on timeout
1572 */
1573 if (atomic_test_bit(conn->flags, BT_CONN_PERIPHERAL_PARAM_SET) &&
1574 conn->le.interval >= conn->le.interval_min &&
1575 conn->le.interval <= conn->le.interval_max &&
1576 conn->le.latency == conn->le.pending_latency &&
1577 conn->le.timeout == conn->le.pending_timeout) {
1578 atomic_clear_bit(conn->flags, BT_CONN_PERIPHERAL_PARAM_SET);
1579 }
1580
1581 for (struct bt_conn_cb *cb = callback_list; cb; cb = cb->_next) {
1582 if (cb->le_param_updated) {
1583 cb->le_param_updated(conn, conn->le.interval,
1584 conn->le.latency,
1585 conn->le.timeout);
1586 }
1587 }
1588
1589 STRUCT_SECTION_FOREACH(bt_conn_cb, cb) {
1590 if (cb->le_param_updated) {
1591 cb->le_param_updated(conn, conn->le.interval,
1592 conn->le.latency,
1593 conn->le.timeout);
1594 }
1595 }
1596 }
1597
1598 #if defined(CONFIG_BT_USER_DATA_LEN_UPDATE)
notify_le_data_len_updated(struct bt_conn * conn)1599 void notify_le_data_len_updated(struct bt_conn *conn)
1600 {
1601 for (struct bt_conn_cb *cb = callback_list; cb; cb = cb->_next) {
1602 if (cb->le_data_len_updated) {
1603 cb->le_data_len_updated(conn, &conn->le.data_len);
1604 }
1605 }
1606
1607 STRUCT_SECTION_FOREACH(bt_conn_cb, cb) {
1608 if (cb->le_data_len_updated) {
1609 cb->le_data_len_updated(conn, &conn->le.data_len);
1610 }
1611 }
1612 }
1613 #endif
1614
1615 #if defined(CONFIG_BT_USER_PHY_UPDATE)
notify_le_phy_updated(struct bt_conn * conn)1616 void notify_le_phy_updated(struct bt_conn *conn)
1617 {
1618 for (struct bt_conn_cb *cb = callback_list; cb; cb = cb->_next) {
1619 if (cb->le_phy_updated) {
1620 cb->le_phy_updated(conn, &conn->le.phy);
1621 }
1622 }
1623
1624 STRUCT_SECTION_FOREACH(bt_conn_cb, cb) {
1625 if (cb->le_phy_updated) {
1626 cb->le_phy_updated(conn, &conn->le.phy);
1627 }
1628 }
1629 }
1630 #endif
1631
le_param_req(struct bt_conn * conn,struct bt_le_conn_param * param)1632 bool le_param_req(struct bt_conn *conn, struct bt_le_conn_param *param)
1633 {
1634 if (!bt_le_conn_params_valid(param)) {
1635 return false;
1636 }
1637
1638 for (struct bt_conn_cb *cb = callback_list; cb; cb = cb->_next) {
1639 if (!cb->le_param_req) {
1640 continue;
1641 }
1642
1643 if (!cb->le_param_req(conn, param)) {
1644 return false;
1645 }
1646
1647 /* The callback may modify the parameters so we need to
1648 * double-check that it returned valid parameters.
1649 */
1650 if (!bt_le_conn_params_valid(param)) {
1651 return false;
1652 }
1653 }
1654
1655 STRUCT_SECTION_FOREACH(bt_conn_cb, cb) {
1656 if (!cb->le_param_req) {
1657 continue;
1658 }
1659
1660 if (!cb->le_param_req(conn, param)) {
1661 return false;
1662 }
1663
1664 /* The callback may modify the parameters so we need to
1665 * double-check that it returned valid parameters.
1666 */
1667 if (!bt_le_conn_params_valid(param)) {
1668 return false;
1669 }
1670 }
1671
1672 /* Default to accepting if there's no app callback */
1673 return true;
1674 }
1675
send_conn_le_param_update(struct bt_conn * conn,const struct bt_le_conn_param * param)1676 static int send_conn_le_param_update(struct bt_conn *conn,
1677 const struct bt_le_conn_param *param)
1678 {
1679 LOG_DBG("conn %p features 0x%02x params (%d-%d %d %d)", conn, conn->le.features[0],
1680 param->interval_min, param->interval_max, param->latency, param->timeout);
1681
1682 /* Proceed only if connection parameters contains valid values*/
1683 if (!bt_le_conn_params_valid(param)) {
1684 return -EINVAL;
1685 }
1686
1687 /* Use LE connection parameter request if both local and remote support
1688 * it; or if local role is central then use LE connection update.
1689 */
1690 if ((BT_FEAT_LE_CONN_PARAM_REQ_PROC(bt_dev.le.features) &&
1691 BT_FEAT_LE_CONN_PARAM_REQ_PROC(conn->le.features) &&
1692 !atomic_test_bit(conn->flags, BT_CONN_PERIPHERAL_PARAM_L2CAP)) ||
1693 (conn->role == BT_HCI_ROLE_CENTRAL)) {
1694 int rc;
1695
1696 rc = bt_conn_le_conn_update(conn, param);
1697
1698 /* store those in case of fallback to L2CAP */
1699 if (rc == 0) {
1700 conn->le.interval_min = param->interval_min;
1701 conn->le.interval_max = param->interval_max;
1702 conn->le.pending_latency = param->latency;
1703 conn->le.pending_timeout = param->timeout;
1704 }
1705
1706 return rc;
1707 }
1708
1709 /* If remote central does not support LL Connection Parameters Request
1710 * Procedure
1711 */
1712 return bt_l2cap_update_conn_param(conn, param);
1713 }
1714
1715 #if defined(CONFIG_BT_ISO_UNICAST)
conn_lookup_iso(struct bt_conn * conn)1716 static struct bt_conn *conn_lookup_iso(struct bt_conn *conn)
1717 {
1718 int i;
1719
1720 for (i = 0; i < ARRAY_SIZE(iso_conns); i++) {
1721 struct bt_conn *iso = bt_conn_ref(&iso_conns[i]);
1722
1723 if (iso == NULL) {
1724 continue;
1725 }
1726
1727 if (iso->iso.acl == conn) {
1728 return iso;
1729 }
1730
1731 bt_conn_unref(iso);
1732 }
1733
1734 return NULL;
1735 }
1736 #endif /* CONFIG_BT_ISO */
1737
deferred_work(struct k_work * work)1738 static void deferred_work(struct k_work *work)
1739 {
1740 struct k_work_delayable *dwork = k_work_delayable_from_work(work);
1741 struct bt_conn *conn = CONTAINER_OF(dwork, struct bt_conn, deferred_work);
1742 const struct bt_le_conn_param *param;
1743
1744 LOG_DBG("conn %p", conn);
1745
1746 if (conn->state == BT_CONN_DISCONNECTED) {
1747 #if defined(CONFIG_BT_ISO_UNICAST)
1748 struct bt_conn *iso;
1749
1750 if (conn->type == BT_CONN_TYPE_ISO) {
1751 /* bt_iso_disconnected is responsible for unref'ing the
1752 * connection pointer, as it is conditional on whether
1753 * the connection is a central or peripheral.
1754 */
1755 bt_iso_disconnected(conn);
1756 return;
1757 }
1758
1759 /* Mark all ISO channels associated
1760 * with ACL conn as not connected, and
1761 * remove ACL reference
1762 */
1763 iso = conn_lookup_iso(conn);
1764 while (iso != NULL) {
1765 struct bt_iso_chan *chan = iso->iso.chan;
1766
1767 if (chan != NULL) {
1768 bt_iso_chan_set_state(chan,
1769 BT_ISO_STATE_DISCONNECTING);
1770 }
1771
1772 bt_iso_cleanup_acl(iso);
1773
1774 bt_conn_unref(iso);
1775 iso = conn_lookup_iso(conn);
1776 }
1777 #endif
1778
1779 bt_l2cap_disconnected(conn);
1780 notify_disconnected(conn);
1781
1782 /* Release the reference we took for the very first
1783 * state transition.
1784 */
1785 bt_conn_unref(conn);
1786 return;
1787 }
1788
1789 if (conn->type != BT_CONN_TYPE_LE) {
1790 return;
1791 }
1792
1793 if (IS_ENABLED(CONFIG_BT_CENTRAL) &&
1794 conn->role == BT_CONN_ROLE_CENTRAL) {
1795 /* we don't call bt_conn_disconnect as it would also clear
1796 * auto connect flag if it was set, instead just cancel
1797 * connection directly
1798 */
1799 bt_le_create_conn_cancel();
1800 return;
1801 }
1802
1803 /* if application set own params use those, otherwise use defaults. */
1804 if (atomic_test_and_clear_bit(conn->flags,
1805 BT_CONN_PERIPHERAL_PARAM_SET)) {
1806 int err;
1807
1808 param = BT_LE_CONN_PARAM(conn->le.interval_min,
1809 conn->le.interval_max,
1810 conn->le.pending_latency,
1811 conn->le.pending_timeout);
1812
1813 err = send_conn_le_param_update(conn, param);
1814 if (!err) {
1815 atomic_clear_bit(conn->flags,
1816 BT_CONN_PERIPHERAL_PARAM_AUTO_UPDATE);
1817 } else {
1818 LOG_WRN("Send LE param update failed (err %d)", err);
1819 }
1820 } else if (IS_ENABLED(CONFIG_BT_GAP_AUTO_UPDATE_CONN_PARAMS)) {
1821 #if defined(CONFIG_BT_GAP_PERIPHERAL_PREF_PARAMS)
1822 int err;
1823
1824 param = BT_LE_CONN_PARAM(
1825 CONFIG_BT_PERIPHERAL_PREF_MIN_INT,
1826 CONFIG_BT_PERIPHERAL_PREF_MAX_INT,
1827 CONFIG_BT_PERIPHERAL_PREF_LATENCY,
1828 CONFIG_BT_PERIPHERAL_PREF_TIMEOUT);
1829
1830 err = send_conn_le_param_update(conn, param);
1831 if (!err) {
1832 atomic_set_bit(conn->flags,
1833 BT_CONN_PERIPHERAL_PARAM_AUTO_UPDATE);
1834 } else {
1835 LOG_WRN("Send auto LE param update failed (err %d)",
1836 err);
1837 }
1838 #endif
1839 }
1840
1841 atomic_set_bit(conn->flags, BT_CONN_PERIPHERAL_PARAM_UPDATE);
1842 }
1843
acl_conn_new(void)1844 static struct bt_conn *acl_conn_new(void)
1845 {
1846 return bt_conn_new(acl_conns, ARRAY_SIZE(acl_conns));
1847 }
1848
1849 #if defined(CONFIG_BT_BREDR)
bt_sco_cleanup(struct bt_conn * sco_conn)1850 void bt_sco_cleanup(struct bt_conn *sco_conn)
1851 {
1852 bt_conn_unref(sco_conn->sco.acl);
1853 sco_conn->sco.acl = NULL;
1854 bt_conn_unref(sco_conn);
1855 }
1856
sco_conn_new(void)1857 static struct bt_conn *sco_conn_new(void)
1858 {
1859 return bt_conn_new(sco_conns, ARRAY_SIZE(sco_conns));
1860 }
1861
bt_conn_create_br(const bt_addr_t * peer,const struct bt_br_conn_param * param)1862 struct bt_conn *bt_conn_create_br(const bt_addr_t *peer,
1863 const struct bt_br_conn_param *param)
1864 {
1865 struct bt_hci_cp_connect *cp;
1866 struct bt_conn *conn;
1867 struct net_buf *buf;
1868
1869 conn = bt_conn_lookup_addr_br(peer);
1870 if (conn) {
1871 switch (conn->state) {
1872 case BT_CONN_CONNECTING:
1873 case BT_CONN_CONNECTED:
1874 return conn;
1875 default:
1876 bt_conn_unref(conn);
1877 return NULL;
1878 }
1879 }
1880
1881 conn = bt_conn_add_br(peer);
1882 if (!conn) {
1883 return NULL;
1884 }
1885
1886 buf = bt_hci_cmd_create(BT_HCI_OP_CONNECT, sizeof(*cp));
1887 if (!buf) {
1888 bt_conn_unref(conn);
1889 return NULL;
1890 }
1891
1892 cp = net_buf_add(buf, sizeof(*cp));
1893
1894 (void)memset(cp, 0, sizeof(*cp));
1895
1896 memcpy(&cp->bdaddr, peer, sizeof(cp->bdaddr));
1897 cp->packet_type = sys_cpu_to_le16(0xcc18); /* DM1 DH1 DM3 DH5 DM5 DH5 */
1898 cp->pscan_rep_mode = 0x02; /* R2 */
1899 cp->allow_role_switch = param->allow_role_switch ? 0x01 : 0x00;
1900 cp->clock_offset = 0x0000; /* TODO used cached clock offset */
1901
1902 if (bt_hci_cmd_send_sync(BT_HCI_OP_CONNECT, buf, NULL) < 0) {
1903 bt_conn_unref(conn);
1904 return NULL;
1905 }
1906
1907 bt_conn_set_state(conn, BT_CONN_CONNECTING);
1908 conn->role = BT_CONN_ROLE_CENTRAL;
1909
1910 return conn;
1911 }
1912
bt_conn_create_sco(const bt_addr_t * peer)1913 struct bt_conn *bt_conn_create_sco(const bt_addr_t *peer)
1914 {
1915 struct bt_hci_cp_setup_sync_conn *cp;
1916 struct bt_conn *sco_conn;
1917 struct net_buf *buf;
1918 int link_type;
1919
1920 sco_conn = bt_conn_lookup_addr_sco(peer);
1921 if (sco_conn) {
1922 switch (sco_conn->state) {
1923 case BT_CONN_CONNECTING:
1924 case BT_CONN_CONNECTED:
1925 return sco_conn;
1926 default:
1927 bt_conn_unref(sco_conn);
1928 return NULL;
1929 }
1930 }
1931
1932 if (BT_FEAT_LMP_ESCO_CAPABLE(bt_dev.features)) {
1933 link_type = BT_HCI_ESCO;
1934 } else {
1935 link_type = BT_HCI_SCO;
1936 }
1937
1938 sco_conn = bt_conn_add_sco(peer, link_type);
1939 if (!sco_conn) {
1940 return NULL;
1941 }
1942
1943 buf = bt_hci_cmd_create(BT_HCI_OP_SETUP_SYNC_CONN, sizeof(*cp));
1944 if (!buf) {
1945 bt_sco_cleanup(sco_conn);
1946 return NULL;
1947 }
1948
1949 cp = net_buf_add(buf, sizeof(*cp));
1950
1951 (void)memset(cp, 0, sizeof(*cp));
1952
1953 LOG_ERR("handle : %x", sco_conn->sco.acl->handle);
1954
1955 cp->handle = sco_conn->sco.acl->handle;
1956 cp->pkt_type = sco_conn->sco.pkt_type;
1957 cp->tx_bandwidth = 0x00001f40;
1958 cp->rx_bandwidth = 0x00001f40;
1959 cp->max_latency = 0x0007;
1960 cp->retrans_effort = 0x01;
1961 cp->content_format = BT_VOICE_CVSD_16BIT;
1962
1963 if (bt_hci_cmd_send_sync(BT_HCI_OP_SETUP_SYNC_CONN, buf,
1964 NULL) < 0) {
1965 bt_sco_cleanup(sco_conn);
1966 return NULL;
1967 }
1968
1969 bt_conn_set_state(sco_conn, BT_CONN_CONNECTING);
1970
1971 return sco_conn;
1972 }
1973
bt_conn_lookup_addr_sco(const bt_addr_t * peer)1974 struct bt_conn *bt_conn_lookup_addr_sco(const bt_addr_t *peer)
1975 {
1976 int i;
1977
1978 for (i = 0; i < ARRAY_SIZE(sco_conns); i++) {
1979 struct bt_conn *conn = bt_conn_ref(&sco_conns[i]);
1980
1981 if (!conn) {
1982 continue;
1983 }
1984
1985 if (conn->type != BT_CONN_TYPE_SCO) {
1986 bt_conn_unref(conn);
1987 continue;
1988 }
1989
1990 if (!bt_addr_eq(peer, &conn->sco.acl->br.dst)) {
1991 bt_conn_unref(conn);
1992 continue;
1993 }
1994
1995 return conn;
1996 }
1997
1998 return NULL;
1999 }
2000
bt_conn_lookup_addr_br(const bt_addr_t * peer)2001 struct bt_conn *bt_conn_lookup_addr_br(const bt_addr_t *peer)
2002 {
2003 int i;
2004
2005 for (i = 0; i < ARRAY_SIZE(acl_conns); i++) {
2006 struct bt_conn *conn = bt_conn_ref(&acl_conns[i]);
2007
2008 if (!conn) {
2009 continue;
2010 }
2011
2012 if (conn->type != BT_CONN_TYPE_BR) {
2013 bt_conn_unref(conn);
2014 continue;
2015 }
2016
2017 if (!bt_addr_eq(peer, &conn->br.dst)) {
2018 bt_conn_unref(conn);
2019 continue;
2020 }
2021
2022 return conn;
2023 }
2024
2025 return NULL;
2026 }
2027
bt_conn_add_sco(const bt_addr_t * peer,int link_type)2028 struct bt_conn *bt_conn_add_sco(const bt_addr_t *peer, int link_type)
2029 {
2030 struct bt_conn *sco_conn = sco_conn_new();
2031
2032 if (!sco_conn) {
2033 return NULL;
2034 }
2035
2036 sco_conn->sco.acl = bt_conn_lookup_addr_br(peer);
2037 if (!sco_conn->sco.acl) {
2038 bt_conn_unref(sco_conn);
2039 return NULL;
2040 }
2041
2042 sco_conn->type = BT_CONN_TYPE_SCO;
2043
2044 if (link_type == BT_HCI_SCO) {
2045 if (BT_FEAT_LMP_ESCO_CAPABLE(bt_dev.features)) {
2046 sco_conn->sco.pkt_type = (bt_dev.br.esco_pkt_type &
2047 ESCO_PKT_MASK);
2048 } else {
2049 sco_conn->sco.pkt_type = (bt_dev.br.esco_pkt_type &
2050 SCO_PKT_MASK);
2051 }
2052 } else if (link_type == BT_HCI_ESCO) {
2053 sco_conn->sco.pkt_type = (bt_dev.br.esco_pkt_type &
2054 ~EDR_ESCO_PKT_MASK);
2055 }
2056
2057 return sco_conn;
2058 }
2059
bt_conn_add_br(const bt_addr_t * peer)2060 struct bt_conn *bt_conn_add_br(const bt_addr_t *peer)
2061 {
2062 struct bt_conn *conn = acl_conn_new();
2063
2064 if (!conn) {
2065 return NULL;
2066 }
2067
2068 bt_addr_copy(&conn->br.dst, peer);
2069 conn->type = BT_CONN_TYPE_BR;
2070
2071 return conn;
2072 }
2073
bt_hci_connect_br_cancel(struct bt_conn * conn)2074 static int bt_hci_connect_br_cancel(struct bt_conn *conn)
2075 {
2076 struct bt_hci_cp_connect_cancel *cp;
2077 struct bt_hci_rp_connect_cancel *rp;
2078 struct net_buf *buf, *rsp;
2079 int err;
2080
2081 buf = bt_hci_cmd_create(BT_HCI_OP_CONNECT_CANCEL, sizeof(*cp));
2082 if (!buf) {
2083 return -ENOBUFS;
2084 }
2085
2086 cp = net_buf_add(buf, sizeof(*cp));
2087 memcpy(&cp->bdaddr, &conn->br.dst, sizeof(cp->bdaddr));
2088
2089 err = bt_hci_cmd_send_sync(BT_HCI_OP_CONNECT_CANCEL, buf, &rsp);
2090 if (err) {
2091 return err;
2092 }
2093
2094 rp = (void *)rsp->data;
2095
2096 err = rp->status ? -EIO : 0;
2097
2098 net_buf_unref(rsp);
2099
2100 return err;
2101 }
2102
2103 #endif /* CONFIG_BT_BREDR */
2104
2105 #if defined(CONFIG_BT_SMP)
bt_conn_ltk_present(const struct bt_conn * conn)2106 bool bt_conn_ltk_present(const struct bt_conn *conn)
2107 {
2108 const struct bt_keys *keys = conn->le.keys;
2109
2110 if (!keys) {
2111 keys = bt_keys_find_addr(conn->id, &conn->le.dst);
2112 }
2113
2114 if (keys) {
2115 if (conn->role == BT_HCI_ROLE_CENTRAL) {
2116 return keys->keys & (BT_KEYS_LTK_P256 | BT_KEYS_PERIPH_LTK);
2117 } else {
2118 return keys->keys & (BT_KEYS_LTK_P256 | BT_KEYS_LTK);
2119 }
2120 }
2121
2122 return false;
2123 }
2124
bt_conn_identity_resolved(struct bt_conn * conn)2125 void bt_conn_identity_resolved(struct bt_conn *conn)
2126 {
2127 const bt_addr_le_t *rpa;
2128
2129 if (conn->role == BT_HCI_ROLE_CENTRAL) {
2130 rpa = &conn->le.resp_addr;
2131 } else {
2132 rpa = &conn->le.init_addr;
2133 }
2134
2135 for (struct bt_conn_cb *cb = callback_list; cb; cb = cb->_next) {
2136 if (cb->identity_resolved) {
2137 cb->identity_resolved(conn, rpa, &conn->le.dst);
2138 }
2139 }
2140
2141 STRUCT_SECTION_FOREACH(bt_conn_cb, cb) {
2142 if (cb->identity_resolved) {
2143 cb->identity_resolved(conn, rpa, &conn->le.dst);
2144 }
2145 }
2146 }
2147
bt_conn_le_start_encryption(struct bt_conn * conn,uint8_t rand[8],uint8_t ediv[2],const uint8_t * ltk,size_t len)2148 int bt_conn_le_start_encryption(struct bt_conn *conn, uint8_t rand[8],
2149 uint8_t ediv[2], const uint8_t *ltk, size_t len)
2150 {
2151 struct bt_hci_cp_le_start_encryption *cp;
2152 struct net_buf *buf;
2153
2154 if (len > sizeof(cp->ltk)) {
2155 return -EINVAL;
2156 }
2157
2158 buf = bt_hci_cmd_create(BT_HCI_OP_LE_START_ENCRYPTION, sizeof(*cp));
2159 if (!buf) {
2160 return -ENOBUFS;
2161 }
2162
2163 cp = net_buf_add(buf, sizeof(*cp));
2164 cp->handle = sys_cpu_to_le16(conn->handle);
2165 memcpy(&cp->rand, rand, sizeof(cp->rand));
2166 memcpy(&cp->ediv, ediv, sizeof(cp->ediv));
2167
2168 memcpy(cp->ltk, ltk, len);
2169 if (len < sizeof(cp->ltk)) {
2170 (void)memset(cp->ltk + len, 0, sizeof(cp->ltk) - len);
2171 }
2172
2173 return bt_hci_cmd_send_sync(BT_HCI_OP_LE_START_ENCRYPTION, buf, NULL);
2174 }
2175 #endif /* CONFIG_BT_SMP */
2176
2177 #if defined(CONFIG_BT_SMP) || defined(CONFIG_BT_BREDR)
bt_conn_enc_key_size(const struct bt_conn * conn)2178 uint8_t bt_conn_enc_key_size(const struct bt_conn *conn)
2179 {
2180 if (!conn->encrypt) {
2181 return 0;
2182 }
2183
2184 if (IS_ENABLED(CONFIG_BT_BREDR) &&
2185 conn->type == BT_CONN_TYPE_BR) {
2186 struct bt_hci_cp_read_encryption_key_size *cp;
2187 struct bt_hci_rp_read_encryption_key_size *rp;
2188 struct net_buf *buf;
2189 struct net_buf *rsp;
2190 uint8_t key_size;
2191
2192 buf = bt_hci_cmd_create(BT_HCI_OP_READ_ENCRYPTION_KEY_SIZE,
2193 sizeof(*cp));
2194 if (!buf) {
2195 return 0;
2196 }
2197
2198 cp = net_buf_add(buf, sizeof(*cp));
2199 cp->handle = sys_cpu_to_le16(conn->handle);
2200
2201 if (bt_hci_cmd_send_sync(BT_HCI_OP_READ_ENCRYPTION_KEY_SIZE,
2202 buf, &rsp)) {
2203 return 0;
2204 }
2205
2206 rp = (void *)rsp->data;
2207
2208 key_size = rp->status ? 0 : rp->key_size;
2209
2210 net_buf_unref(rsp);
2211
2212 return key_size;
2213 }
2214
2215 if (IS_ENABLED(CONFIG_BT_SMP)) {
2216 return conn->le.keys ? conn->le.keys->enc_size : 0;
2217 }
2218
2219 return 0;
2220 }
2221
reset_pairing(struct bt_conn * conn)2222 static void reset_pairing(struct bt_conn *conn)
2223 {
2224 #if defined(CONFIG_BT_BREDR)
2225 if (conn->type == BT_CONN_TYPE_BR) {
2226 atomic_clear_bit(conn->flags, BT_CONN_BR_PAIRING);
2227 atomic_clear_bit(conn->flags, BT_CONN_BR_PAIRING_INITIATOR);
2228 atomic_clear_bit(conn->flags, BT_CONN_BR_LEGACY_SECURE);
2229 }
2230 #endif /* CONFIG_BT_BREDR */
2231
2232 /* Reset required security level to current operational */
2233 conn->required_sec_level = conn->sec_level;
2234 }
2235
bt_conn_security_changed(struct bt_conn * conn,uint8_t hci_err,enum bt_security_err err)2236 void bt_conn_security_changed(struct bt_conn *conn, uint8_t hci_err,
2237 enum bt_security_err err)
2238 {
2239 reset_pairing(conn);
2240 bt_l2cap_security_changed(conn, hci_err);
2241 if (IS_ENABLED(CONFIG_BT_ISO_CENTRAL)) {
2242 bt_iso_security_changed(conn, hci_err);
2243 }
2244
2245 for (struct bt_conn_cb *cb = callback_list; cb; cb = cb->_next) {
2246 if (cb->security_changed) {
2247 cb->security_changed(conn, conn->sec_level, err);
2248 }
2249 }
2250
2251 STRUCT_SECTION_FOREACH(bt_conn_cb, cb) {
2252 if (cb->security_changed) {
2253 cb->security_changed(conn, conn->sec_level, err);
2254 }
2255 }
2256
2257 #if defined(CONFIG_BT_KEYS_OVERWRITE_OLDEST)
2258 if (!err && conn->sec_level >= BT_SECURITY_L2) {
2259 if (conn->type == BT_CONN_TYPE_LE) {
2260 bt_keys_update_usage(conn->id, bt_conn_get_dst(conn));
2261 }
2262
2263 #if defined(CONFIG_BT_BREDR)
2264 if (conn->type == BT_CONN_TYPE_BR) {
2265 bt_keys_link_key_update_usage(&conn->br.dst);
2266 }
2267 #endif /* CONFIG_BT_BREDR */
2268
2269 }
2270 #endif
2271 }
2272
start_security(struct bt_conn * conn)2273 static int start_security(struct bt_conn *conn)
2274 {
2275 if (IS_ENABLED(CONFIG_BT_BREDR) && conn->type == BT_CONN_TYPE_BR) {
2276 return bt_ssp_start_security(conn);
2277 }
2278
2279 if (IS_ENABLED(CONFIG_BT_SMP)) {
2280 return bt_smp_start_security(conn);
2281 }
2282
2283 return -EINVAL;
2284 }
2285
bt_conn_set_security(struct bt_conn * conn,bt_security_t sec)2286 int bt_conn_set_security(struct bt_conn *conn, bt_security_t sec)
2287 {
2288 bool force_pair;
2289 int err;
2290
2291 if (conn->state != BT_CONN_CONNECTED) {
2292 return -ENOTCONN;
2293 }
2294
2295 force_pair = sec & BT_SECURITY_FORCE_PAIR;
2296 sec &= ~BT_SECURITY_FORCE_PAIR;
2297
2298 if (IS_ENABLED(CONFIG_BT_SMP_SC_ONLY)) {
2299 sec = BT_SECURITY_L4;
2300 }
2301
2302 if (IS_ENABLED(CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY)) {
2303 sec = BT_SECURITY_L3;
2304 }
2305
2306 /* nothing to do */
2307 if (!force_pair && (conn->sec_level >= sec || conn->required_sec_level >= sec)) {
2308 return 0;
2309 }
2310
2311 atomic_set_bit_to(conn->flags, BT_CONN_FORCE_PAIR, force_pair);
2312 conn->required_sec_level = sec;
2313
2314 err = start_security(conn);
2315
2316 /* reset required security level in case of error */
2317 if (err) {
2318 conn->required_sec_level = conn->sec_level;
2319 }
2320
2321 return err;
2322 }
2323
bt_conn_get_security(const struct bt_conn * conn)2324 bt_security_t bt_conn_get_security(const struct bt_conn *conn)
2325 {
2326 return conn->sec_level;
2327 }
2328 #else
bt_conn_get_security(const struct bt_conn * conn)2329 bt_security_t bt_conn_get_security(const struct bt_conn *conn)
2330 {
2331 return BT_SECURITY_L1;
2332 }
2333 #endif /* CONFIG_BT_SMP */
2334
bt_conn_cb_register(struct bt_conn_cb * cb)2335 void bt_conn_cb_register(struct bt_conn_cb *cb)
2336 {
2337 cb->_next = callback_list;
2338 callback_list = cb;
2339 }
2340
bt_conn_exists_le(uint8_t id,const bt_addr_le_t * peer)2341 bool bt_conn_exists_le(uint8_t id, const bt_addr_le_t *peer)
2342 {
2343 struct bt_conn *conn = bt_conn_lookup_addr_le(id, peer);
2344
2345 if (conn) {
2346 /* Connection object already exists.
2347 * If the connection state is not "disconnected",then the
2348 * connection was created but has not yet been disconnected.
2349 * If the connection state is "disconnected" then the connection
2350 * still has valid references. The last reference of the stack
2351 * is released after the disconnected callback.
2352 */
2353 LOG_WRN("Found valid connection (%p) with address %s in %s state ", conn,
2354 bt_addr_le_str(peer), state2str(conn->state));
2355 bt_conn_unref(conn);
2356 return true;
2357 }
2358
2359 return false;
2360 }
2361
bt_conn_add_le(uint8_t id,const bt_addr_le_t * peer)2362 struct bt_conn *bt_conn_add_le(uint8_t id, const bt_addr_le_t *peer)
2363 {
2364 struct bt_conn *conn = acl_conn_new();
2365
2366 if (!conn) {
2367 return NULL;
2368 }
2369
2370 conn->id = id;
2371 bt_addr_le_copy(&conn->le.dst, peer);
2372 #if defined(CONFIG_BT_SMP)
2373 conn->sec_level = BT_SECURITY_L1;
2374 conn->required_sec_level = BT_SECURITY_L1;
2375 #endif /* CONFIG_BT_SMP */
2376 conn->type = BT_CONN_TYPE_LE;
2377 conn->le.interval_min = BT_GAP_INIT_CONN_INT_MIN;
2378 conn->le.interval_max = BT_GAP_INIT_CONN_INT_MAX;
2379
2380 return conn;
2381 }
2382
bt_conn_is_peer_addr_le(const struct bt_conn * conn,uint8_t id,const bt_addr_le_t * peer)2383 bool bt_conn_is_peer_addr_le(const struct bt_conn *conn, uint8_t id,
2384 const bt_addr_le_t *peer)
2385 {
2386 if (id != conn->id) {
2387 return false;
2388 }
2389
2390 /* Check against conn dst address as it may be the identity address */
2391 if (bt_addr_le_eq(peer, &conn->le.dst)) {
2392 return true;
2393 }
2394
2395 /* Check against initial connection address */
2396 if (conn->role == BT_HCI_ROLE_CENTRAL) {
2397 return bt_addr_le_eq(peer, &conn->le.resp_addr);
2398 }
2399
2400 return bt_addr_le_eq(peer, &conn->le.init_addr);
2401 }
2402
bt_conn_lookup_addr_le(uint8_t id,const bt_addr_le_t * peer)2403 struct bt_conn *bt_conn_lookup_addr_le(uint8_t id, const bt_addr_le_t *peer)
2404 {
2405 int i;
2406
2407 for (i = 0; i < ARRAY_SIZE(acl_conns); i++) {
2408 struct bt_conn *conn = bt_conn_ref(&acl_conns[i]);
2409
2410 if (!conn) {
2411 continue;
2412 }
2413
2414 if (conn->type != BT_CONN_TYPE_LE) {
2415 bt_conn_unref(conn);
2416 continue;
2417 }
2418
2419 if (!bt_conn_is_peer_addr_le(conn, id, peer)) {
2420 bt_conn_unref(conn);
2421 continue;
2422 }
2423
2424 return conn;
2425 }
2426
2427 return NULL;
2428 }
2429
bt_conn_lookup_state_le(uint8_t id,const bt_addr_le_t * peer,const bt_conn_state_t state)2430 struct bt_conn *bt_conn_lookup_state_le(uint8_t id, const bt_addr_le_t *peer,
2431 const bt_conn_state_t state)
2432 {
2433 int i;
2434
2435 for (i = 0; i < ARRAY_SIZE(acl_conns); i++) {
2436 struct bt_conn *conn = bt_conn_ref(&acl_conns[i]);
2437
2438 if (!conn) {
2439 continue;
2440 }
2441
2442 if (conn->type != BT_CONN_TYPE_LE) {
2443 bt_conn_unref(conn);
2444 continue;
2445 }
2446
2447 if (peer && !bt_conn_is_peer_addr_le(conn, id, peer)) {
2448 bt_conn_unref(conn);
2449 continue;
2450 }
2451
2452 if (!(conn->state == state && conn->id == id)) {
2453 bt_conn_unref(conn);
2454 continue;
2455 }
2456
2457 return conn;
2458 }
2459
2460 return NULL;
2461 }
2462
bt_conn_get_dst(const struct bt_conn * conn)2463 const bt_addr_le_t *bt_conn_get_dst(const struct bt_conn *conn)
2464 {
2465 return &conn->le.dst;
2466 }
2467
conn_internal_to_public_state(bt_conn_state_t state)2468 static enum bt_conn_state conn_internal_to_public_state(bt_conn_state_t state)
2469 {
2470 switch (state) {
2471 case BT_CONN_DISCONNECTED:
2472 case BT_CONN_DISCONNECT_COMPLETE:
2473 return BT_CONN_STATE_DISCONNECTED;
2474 case BT_CONN_CONNECTING_SCAN:
2475 case BT_CONN_CONNECTING_AUTO:
2476 case BT_CONN_CONNECTING_ADV:
2477 case BT_CONN_CONNECTING_DIR_ADV:
2478 case BT_CONN_CONNECTING:
2479 return BT_CONN_STATE_CONNECTING;
2480 case BT_CONN_CONNECTED:
2481 return BT_CONN_STATE_CONNECTED;
2482 case BT_CONN_DISCONNECTING:
2483 return BT_CONN_STATE_DISCONNECTING;
2484 default:
2485 __ASSERT(false, "Invalid conn state %u", state);
2486 return 0;
2487 }
2488 }
2489
bt_conn_get_info(const struct bt_conn * conn,struct bt_conn_info * info)2490 int bt_conn_get_info(const struct bt_conn *conn, struct bt_conn_info *info)
2491 {
2492 info->type = conn->type;
2493 info->role = conn->role;
2494 info->id = conn->id;
2495 info->state = conn_internal_to_public_state(conn->state);
2496 info->security.flags = 0;
2497 info->security.level = bt_conn_get_security(conn);
2498 #if defined(CONFIG_BT_SMP) || defined(CONFIG_BT_BREDR)
2499 info->security.enc_key_size = bt_conn_enc_key_size(conn);
2500 #else
2501 info->security.enc_key_size = 0;
2502 #endif /* CONFIG_BT_SMP || CONFIG_BT_BREDR */
2503
2504 switch (conn->type) {
2505 case BT_CONN_TYPE_LE:
2506 info->le.dst = &conn->le.dst;
2507 info->le.src = &bt_dev.id_addr[conn->id];
2508 if (conn->role == BT_HCI_ROLE_CENTRAL) {
2509 info->le.local = &conn->le.init_addr;
2510 info->le.remote = &conn->le.resp_addr;
2511 } else {
2512 info->le.local = &conn->le.resp_addr;
2513 info->le.remote = &conn->le.init_addr;
2514 }
2515 info->le.interval = conn->le.interval;
2516 info->le.latency = conn->le.latency;
2517 info->le.timeout = conn->le.timeout;
2518 #if defined(CONFIG_BT_USER_PHY_UPDATE)
2519 info->le.phy = &conn->le.phy;
2520 #endif
2521 #if defined(CONFIG_BT_USER_DATA_LEN_UPDATE)
2522 info->le.data_len = &conn->le.data_len;
2523 #endif
2524 if (conn->le.keys && (conn->le.keys->flags & BT_KEYS_SC)) {
2525 info->security.flags |= BT_SECURITY_FLAG_SC;
2526 }
2527 if (conn->le.keys && (conn->le.keys->flags & BT_KEYS_OOB)) {
2528 info->security.flags |= BT_SECURITY_FLAG_OOB;
2529 }
2530 return 0;
2531 #if defined(CONFIG_BT_BREDR)
2532 case BT_CONN_TYPE_BR:
2533 info->br.dst = &conn->br.dst;
2534 return 0;
2535 #endif
2536 #if defined(CONFIG_BT_ISO)
2537 case BT_CONN_TYPE_ISO:
2538 if (IS_ENABLED(CONFIG_BT_ISO_UNICAST) &&
2539 conn->iso.info.type == BT_ISO_CHAN_TYPE_CONNECTED && conn->iso.acl != NULL) {
2540 info->le.dst = &conn->iso.acl->le.dst;
2541 info->le.src = &bt_dev.id_addr[conn->iso.acl->id];
2542 } else {
2543 info->le.src = BT_ADDR_LE_NONE;
2544 info->le.dst = BT_ADDR_LE_NONE;
2545 }
2546 return 0;
2547 #endif
2548 default:
2549 break;
2550 }
2551
2552 return -EINVAL;
2553 }
2554
bt_conn_get_remote_info(struct bt_conn * conn,struct bt_conn_remote_info * remote_info)2555 int bt_conn_get_remote_info(struct bt_conn *conn,
2556 struct bt_conn_remote_info *remote_info)
2557 {
2558 if (!atomic_test_bit(conn->flags, BT_CONN_AUTO_FEATURE_EXCH) ||
2559 (IS_ENABLED(CONFIG_BT_REMOTE_VERSION) &&
2560 !atomic_test_bit(conn->flags, BT_CONN_AUTO_VERSION_INFO))) {
2561 return -EBUSY;
2562 }
2563
2564 remote_info->type = conn->type;
2565 #if defined(CONFIG_BT_REMOTE_VERSION)
2566 /* The conn->rv values will be just zeroes if the operation failed */
2567 remote_info->version = conn->rv.version;
2568 remote_info->manufacturer = conn->rv.manufacturer;
2569 remote_info->subversion = conn->rv.subversion;
2570 #else
2571 remote_info->version = 0;
2572 remote_info->manufacturer = 0;
2573 remote_info->subversion = 0;
2574 #endif
2575
2576 switch (conn->type) {
2577 case BT_CONN_TYPE_LE:
2578 remote_info->le.features = conn->le.features;
2579 return 0;
2580 #if defined(CONFIG_BT_BREDR)
2581 case BT_CONN_TYPE_BR:
2582 /* TODO: Make sure the HCI commands to read br features and
2583 * extended features has finished. */
2584 return -ENOTSUP;
2585 #endif
2586 default:
2587 return -EINVAL;
2588 }
2589 }
2590
2591 /* Read Transmit Power Level HCI command */
bt_conn_get_tx_power_level(struct bt_conn * conn,uint8_t type,int8_t * tx_power_level)2592 static int bt_conn_get_tx_power_level(struct bt_conn *conn, uint8_t type,
2593 int8_t *tx_power_level)
2594 {
2595 int err;
2596 struct bt_hci_rp_read_tx_power_level *rp;
2597 struct net_buf *rsp;
2598 struct bt_hci_cp_read_tx_power_level *cp;
2599 struct net_buf *buf;
2600
2601 buf = bt_hci_cmd_create(BT_HCI_OP_READ_TX_POWER_LEVEL, sizeof(*cp));
2602 if (!buf) {
2603 return -ENOBUFS;
2604 }
2605
2606 cp = net_buf_add(buf, sizeof(*cp));
2607 cp->type = type;
2608 cp->handle = sys_cpu_to_le16(conn->handle);
2609
2610 err = bt_hci_cmd_send_sync(BT_HCI_OP_READ_TX_POWER_LEVEL, buf, &rsp);
2611 if (err) {
2612 return err;
2613 }
2614
2615 rp = (void *) rsp->data;
2616 *tx_power_level = rp->tx_power_level;
2617 net_buf_unref(rsp);
2618
2619 return 0;
2620 }
2621
bt_conn_le_get_tx_power_level(struct bt_conn * conn,struct bt_conn_le_tx_power * tx_power_level)2622 int bt_conn_le_get_tx_power_level(struct bt_conn *conn,
2623 struct bt_conn_le_tx_power *tx_power_level)
2624 {
2625 int err;
2626
2627 if (tx_power_level->phy != 0) {
2628 /* Extend the implementation when LE Enhanced Read Transmit
2629 * Power Level HCI command is available for use.
2630 */
2631 return -ENOTSUP;
2632 }
2633
2634 err = bt_conn_get_tx_power_level(conn, BT_TX_POWER_LEVEL_CURRENT,
2635 &tx_power_level->current_level);
2636 if (err) {
2637 return err;
2638 }
2639
2640 err = bt_conn_get_tx_power_level(conn, BT_TX_POWER_LEVEL_MAX,
2641 &tx_power_level->max_level);
2642 return err;
2643 }
2644
bt_conn_le_param_update(struct bt_conn * conn,const struct bt_le_conn_param * param)2645 int bt_conn_le_param_update(struct bt_conn *conn,
2646 const struct bt_le_conn_param *param)
2647 {
2648 LOG_DBG("conn %p features 0x%02x params (%d-%d %d %d)", conn, conn->le.features[0],
2649 param->interval_min, param->interval_max, param->latency, param->timeout);
2650
2651 /* Check if there's a need to update conn params */
2652 if (conn->le.interval >= param->interval_min &&
2653 conn->le.interval <= param->interval_max &&
2654 conn->le.latency == param->latency &&
2655 conn->le.timeout == param->timeout) {
2656 atomic_clear_bit(conn->flags, BT_CONN_PERIPHERAL_PARAM_SET);
2657 return -EALREADY;
2658 }
2659
2660 if (IS_ENABLED(CONFIG_BT_CENTRAL) &&
2661 conn->role == BT_CONN_ROLE_CENTRAL) {
2662 return send_conn_le_param_update(conn, param);
2663 }
2664
2665 if (IS_ENABLED(CONFIG_BT_PERIPHERAL)) {
2666 /* if peripheral conn param update timer expired just send request */
2667 if (atomic_test_bit(conn->flags, BT_CONN_PERIPHERAL_PARAM_UPDATE)) {
2668 return send_conn_le_param_update(conn, param);
2669 }
2670
2671 /* store new conn params to be used by update timer */
2672 conn->le.interval_min = param->interval_min;
2673 conn->le.interval_max = param->interval_max;
2674 conn->le.pending_latency = param->latency;
2675 conn->le.pending_timeout = param->timeout;
2676 atomic_set_bit(conn->flags, BT_CONN_PERIPHERAL_PARAM_SET);
2677 }
2678
2679 return 0;
2680 }
2681
2682 #if defined(CONFIG_BT_USER_DATA_LEN_UPDATE)
bt_conn_le_data_len_update(struct bt_conn * conn,const struct bt_conn_le_data_len_param * param)2683 int bt_conn_le_data_len_update(struct bt_conn *conn,
2684 const struct bt_conn_le_data_len_param *param)
2685 {
2686 if (conn->le.data_len.tx_max_len == param->tx_max_len &&
2687 conn->le.data_len.tx_max_time == param->tx_max_time) {
2688 return -EALREADY;
2689 }
2690
2691 return bt_le_set_data_len(conn, param->tx_max_len, param->tx_max_time);
2692 }
2693 #endif /* CONFIG_BT_USER_DATA_LEN_UPDATE */
2694
2695 #if defined(CONFIG_BT_USER_PHY_UPDATE)
bt_conn_le_phy_update(struct bt_conn * conn,const struct bt_conn_le_phy_param * param)2696 int bt_conn_le_phy_update(struct bt_conn *conn,
2697 const struct bt_conn_le_phy_param *param)
2698 {
2699 uint8_t phy_opts, all_phys;
2700
2701 if ((param->options & BT_CONN_LE_PHY_OPT_CODED_S2) &&
2702 (param->options & BT_CONN_LE_PHY_OPT_CODED_S8)) {
2703 phy_opts = BT_HCI_LE_PHY_CODED_ANY;
2704 } else if (param->options & BT_CONN_LE_PHY_OPT_CODED_S2) {
2705 phy_opts = BT_HCI_LE_PHY_CODED_S2;
2706 } else if (param->options & BT_CONN_LE_PHY_OPT_CODED_S8) {
2707 phy_opts = BT_HCI_LE_PHY_CODED_S8;
2708 } else {
2709 phy_opts = BT_HCI_LE_PHY_CODED_ANY;
2710 }
2711
2712 all_phys = 0U;
2713 if (param->pref_tx_phy == BT_GAP_LE_PHY_NONE) {
2714 all_phys |= BT_HCI_LE_PHY_TX_ANY;
2715 }
2716
2717 if (param->pref_rx_phy == BT_GAP_LE_PHY_NONE) {
2718 all_phys |= BT_HCI_LE_PHY_RX_ANY;
2719 }
2720
2721 return bt_le_set_phy(conn, all_phys, param->pref_tx_phy,
2722 param->pref_rx_phy, phy_opts);
2723 }
2724 #endif
2725
2726 #if defined(CONFIG_BT_CENTRAL)
bt_conn_set_param_le(struct bt_conn * conn,const struct bt_le_conn_param * param)2727 static void bt_conn_set_param_le(struct bt_conn *conn,
2728 const struct bt_le_conn_param *param)
2729 {
2730 conn->le.interval_min = param->interval_min;
2731 conn->le.interval_max = param->interval_max;
2732 conn->le.latency = param->latency;
2733 conn->le.timeout = param->timeout;
2734 }
2735
create_param_validate(const struct bt_conn_le_create_param * param)2736 static bool create_param_validate(const struct bt_conn_le_create_param *param)
2737 {
2738 #if defined(CONFIG_BT_PRIVACY)
2739 /* Initiation timeout cannot be greater than the RPA timeout */
2740 const uint32_t timeout_max = (MSEC_PER_SEC / 10) * bt_dev.rpa_timeout;
2741
2742 if (param->timeout > timeout_max) {
2743 return false;
2744 }
2745 #endif
2746
2747 return true;
2748 }
2749
create_param_setup(const struct bt_conn_le_create_param * param)2750 static void create_param_setup(const struct bt_conn_le_create_param *param)
2751 {
2752 bt_dev.create_param = *param;
2753
2754 bt_dev.create_param.timeout =
2755 (bt_dev.create_param.timeout != 0) ?
2756 bt_dev.create_param.timeout :
2757 (MSEC_PER_SEC / 10) * CONFIG_BT_CREATE_CONN_TIMEOUT;
2758
2759 bt_dev.create_param.interval_coded =
2760 (bt_dev.create_param.interval_coded != 0) ?
2761 bt_dev.create_param.interval_coded :
2762 bt_dev.create_param.interval;
2763
2764 bt_dev.create_param.window_coded =
2765 (bt_dev.create_param.window_coded != 0) ?
2766 bt_dev.create_param.window_coded :
2767 bt_dev.create_param.window;
2768 }
2769
2770 #if defined(CONFIG_BT_FILTER_ACCEPT_LIST)
bt_conn_le_create_auto(const struct bt_conn_le_create_param * create_param,const struct bt_le_conn_param * param)2771 int bt_conn_le_create_auto(const struct bt_conn_le_create_param *create_param,
2772 const struct bt_le_conn_param *param)
2773 {
2774 struct bt_conn *conn;
2775 int err;
2776
2777 if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
2778 return -EAGAIN;
2779 }
2780
2781 if (!bt_le_conn_params_valid(param)) {
2782 return -EINVAL;
2783 }
2784
2785 conn = bt_conn_lookup_state_le(BT_ID_DEFAULT, BT_ADDR_LE_NONE,
2786 BT_CONN_CONNECTING_AUTO);
2787 if (conn) {
2788 bt_conn_unref(conn);
2789 return -EALREADY;
2790 }
2791
2792 /* Scanning either to connect or explicit scan, either case scanner was
2793 * started by application and should not be stopped.
2794 */
2795 if (atomic_test_bit(bt_dev.flags, BT_DEV_SCANNING)) {
2796 return -EINVAL;
2797 }
2798
2799 if (atomic_test_bit(bt_dev.flags, BT_DEV_INITIATING)) {
2800 return -EINVAL;
2801 }
2802
2803 if (!bt_id_scan_random_addr_check()) {
2804 return -EINVAL;
2805 }
2806
2807 conn = bt_conn_add_le(BT_ID_DEFAULT, BT_ADDR_LE_NONE);
2808 if (!conn) {
2809 return -ENOMEM;
2810 }
2811
2812 bt_conn_set_param_le(conn, param);
2813 create_param_setup(create_param);
2814
2815 atomic_set_bit(conn->flags, BT_CONN_AUTO_CONNECT);
2816 bt_conn_set_state(conn, BT_CONN_CONNECTING_AUTO);
2817
2818 err = bt_le_create_conn(conn);
2819 if (err) {
2820 LOG_ERR("Failed to start filtered scan");
2821 conn->err = 0;
2822 bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
2823 bt_conn_unref(conn);
2824 return err;
2825 }
2826
2827 /* Since we don't give the application a reference to manage in
2828 * this case, we need to release this reference here.
2829 */
2830 bt_conn_unref(conn);
2831 return 0;
2832 }
2833
bt_conn_create_auto_stop(void)2834 int bt_conn_create_auto_stop(void)
2835 {
2836 struct bt_conn *conn;
2837 int err;
2838
2839 if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
2840 return -EINVAL;
2841 }
2842
2843 conn = bt_conn_lookup_state_le(BT_ID_DEFAULT, BT_ADDR_LE_NONE,
2844 BT_CONN_CONNECTING_AUTO);
2845 if (!conn) {
2846 return -EINVAL;
2847 }
2848
2849 if (!atomic_test_bit(bt_dev.flags, BT_DEV_INITIATING)) {
2850 return -EINVAL;
2851 }
2852
2853 bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
2854 bt_conn_unref(conn);
2855
2856 err = bt_le_create_conn_cancel();
2857 if (err) {
2858 LOG_ERR("Failed to stop initiator");
2859 return err;
2860 }
2861
2862 return 0;
2863 }
2864 #endif /* defined(CONFIG_BT_FILTER_ACCEPT_LIST) */
2865
conn_le_create_common_checks(const bt_addr_le_t * peer,const struct bt_le_conn_param * conn_param)2866 static int conn_le_create_common_checks(const bt_addr_le_t *peer,
2867 const struct bt_le_conn_param *conn_param)
2868 {
2869
2870 if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
2871 return -EAGAIN;
2872 }
2873
2874 if (!bt_le_conn_params_valid(conn_param)) {
2875 return -EINVAL;
2876 }
2877
2878 if (atomic_test_bit(bt_dev.flags, BT_DEV_EXPLICIT_SCAN)) {
2879 return -EAGAIN;
2880 }
2881
2882 if (atomic_test_bit(bt_dev.flags, BT_DEV_INITIATING)) {
2883 return -EALREADY;
2884 }
2885
2886 if (!bt_id_scan_random_addr_check()) {
2887 return -EINVAL;
2888 }
2889
2890 if (bt_conn_exists_le(BT_ID_DEFAULT, peer)) {
2891 return -EINVAL;
2892 }
2893
2894 return 0;
2895 }
2896
conn_le_create_helper(const bt_addr_le_t * peer,const struct bt_le_conn_param * conn_param)2897 static struct bt_conn *conn_le_create_helper(const bt_addr_le_t *peer,
2898 const struct bt_le_conn_param *conn_param)
2899 {
2900 bt_addr_le_t dst;
2901 struct bt_conn *conn;
2902
2903 if (bt_addr_le_is_resolved(peer)) {
2904 bt_addr_le_copy_resolved(&dst, peer);
2905 } else {
2906 bt_addr_le_copy(&dst, bt_lookup_id_addr(BT_ID_DEFAULT, peer));
2907 }
2908
2909 /* Only default identity supported for now */
2910 conn = bt_conn_add_le(BT_ID_DEFAULT, &dst);
2911 if (!conn) {
2912 return NULL;
2913 }
2914
2915 bt_conn_set_param_le(conn, conn_param);
2916
2917 return conn;
2918 }
2919
bt_conn_le_create(const bt_addr_le_t * peer,const struct bt_conn_le_create_param * create_param,const struct bt_le_conn_param * conn_param,struct bt_conn ** ret_conn)2920 int bt_conn_le_create(const bt_addr_le_t *peer, const struct bt_conn_le_create_param *create_param,
2921 const struct bt_le_conn_param *conn_param, struct bt_conn **ret_conn)
2922 {
2923 struct bt_conn *conn;
2924 int err;
2925
2926 err = conn_le_create_common_checks(peer, conn_param);
2927 if (err) {
2928 return err;
2929 }
2930
2931 if (!create_param_validate(create_param)) {
2932 return -EINVAL;
2933 }
2934
2935 conn = conn_le_create_helper(peer, conn_param);
2936 if (!conn) {
2937 return -ENOMEM;
2938 }
2939
2940 create_param_setup(create_param);
2941
2942 #if defined(CONFIG_BT_SMP)
2943 if (bt_dev.le.rl_entries > bt_dev.le.rl_size) {
2944 /* Use host-based identity resolving. */
2945 bt_conn_set_state(conn, BT_CONN_CONNECTING_SCAN);
2946
2947 err = bt_le_scan_update(true);
2948 if (err) {
2949 bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
2950 bt_conn_unref(conn);
2951
2952 return err;
2953 }
2954
2955 *ret_conn = conn;
2956 return 0;
2957 }
2958 #endif
2959
2960 bt_conn_set_state(conn, BT_CONN_CONNECTING);
2961
2962 err = bt_le_create_conn(conn);
2963 if (err) {
2964 conn->err = 0;
2965 bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
2966 bt_conn_unref(conn);
2967
2968 bt_le_scan_update(false);
2969 return err;
2970 }
2971
2972 *ret_conn = conn;
2973 return 0;
2974 }
2975
bt_conn_le_create_synced(const struct bt_le_ext_adv * adv,const struct bt_conn_le_create_synced_param * synced_param,const struct bt_le_conn_param * conn_param,struct bt_conn ** ret_conn)2976 int bt_conn_le_create_synced(const struct bt_le_ext_adv *adv,
2977 const struct bt_conn_le_create_synced_param *synced_param,
2978 const struct bt_le_conn_param *conn_param, struct bt_conn **ret_conn)
2979 {
2980 struct bt_conn *conn;
2981 int err;
2982
2983 err = conn_le_create_common_checks(synced_param->peer, conn_param);
2984 if (err) {
2985 return err;
2986 }
2987
2988 if (!atomic_test_bit(adv->flags, BT_PER_ADV_ENABLED)) {
2989 return -EINVAL;
2990 }
2991
2992 if (!BT_FEAT_LE_PAWR_ADVERTISER(bt_dev.le.features)) {
2993 return -ENOTSUP;
2994 }
2995
2996 if (synced_param->subevent >= BT_HCI_PAWR_SUBEVENT_MAX) {
2997 return -EINVAL;
2998 }
2999
3000 conn = conn_le_create_helper(synced_param->peer, conn_param);
3001 if (!conn) {
3002 return -ENOMEM;
3003 }
3004
3005 /* The connection creation timeout is not really useful for PAwR.
3006 * The controller will give a result for the connection attempt
3007 * within a periodic interval. We do not know the periodic interval
3008 * used, so disable the timeout.
3009 */
3010 bt_dev.create_param.timeout = 0;
3011 bt_conn_set_state(conn, BT_CONN_CONNECTING);
3012
3013 err = bt_le_create_conn_synced(conn, adv, synced_param->subevent);
3014 if (err) {
3015 conn->err = 0;
3016 bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
3017 bt_conn_unref(conn);
3018
3019 return err;
3020 }
3021
3022 *ret_conn = conn;
3023 return 0;
3024 }
3025
3026 #if !defined(CONFIG_BT_FILTER_ACCEPT_LIST)
bt_le_set_auto_conn(const bt_addr_le_t * addr,const struct bt_le_conn_param * param)3027 int bt_le_set_auto_conn(const bt_addr_le_t *addr,
3028 const struct bt_le_conn_param *param)
3029 {
3030 struct bt_conn *conn;
3031
3032 if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
3033 return -EAGAIN;
3034 }
3035
3036 if (param && !bt_le_conn_params_valid(param)) {
3037 return -EINVAL;
3038 }
3039
3040 if (!bt_id_scan_random_addr_check()) {
3041 return -EINVAL;
3042 }
3043
3044 /* Only default identity is supported */
3045 conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, addr);
3046 if (!conn) {
3047 conn = bt_conn_add_le(BT_ID_DEFAULT, addr);
3048 if (!conn) {
3049 return -ENOMEM;
3050 }
3051 }
3052
3053 if (param) {
3054 bt_conn_set_param_le(conn, param);
3055
3056 if (!atomic_test_and_set_bit(conn->flags,
3057 BT_CONN_AUTO_CONNECT)) {
3058 bt_conn_ref(conn);
3059 }
3060 } else {
3061 if (atomic_test_and_clear_bit(conn->flags,
3062 BT_CONN_AUTO_CONNECT)) {
3063 bt_conn_unref(conn);
3064 if (conn->state == BT_CONN_CONNECTING_SCAN) {
3065 bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
3066 }
3067 }
3068 }
3069
3070 if (conn->state == BT_CONN_DISCONNECTED &&
3071 atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
3072 if (param) {
3073 bt_conn_set_state(conn, BT_CONN_CONNECTING_SCAN);
3074 }
3075 bt_le_scan_update(false);
3076 }
3077
3078 bt_conn_unref(conn);
3079
3080 return 0;
3081 }
3082 #endif /* !defined(CONFIG_BT_FILTER_ACCEPT_LIST) */
3083 #endif /* CONFIG_BT_CENTRAL */
3084
bt_conn_le_conn_update(struct bt_conn * conn,const struct bt_le_conn_param * param)3085 int bt_conn_le_conn_update(struct bt_conn *conn,
3086 const struct bt_le_conn_param *param)
3087 {
3088 struct hci_cp_le_conn_update *conn_update;
3089 struct net_buf *buf;
3090
3091 buf = bt_hci_cmd_create(BT_HCI_OP_LE_CONN_UPDATE,
3092 sizeof(*conn_update));
3093 if (!buf) {
3094 return -ENOBUFS;
3095 }
3096
3097 conn_update = net_buf_add(buf, sizeof(*conn_update));
3098 (void)memset(conn_update, 0, sizeof(*conn_update));
3099 conn_update->handle = sys_cpu_to_le16(conn->handle);
3100 conn_update->conn_interval_min = sys_cpu_to_le16(param->interval_min);
3101 conn_update->conn_interval_max = sys_cpu_to_le16(param->interval_max);
3102 conn_update->conn_latency = sys_cpu_to_le16(param->latency);
3103 conn_update->supervision_timeout = sys_cpu_to_le16(param->timeout);
3104
3105 return bt_hci_cmd_send_sync(BT_HCI_OP_LE_CONN_UPDATE, buf, NULL);
3106 }
3107
3108 #if defined(CONFIG_NET_BUF_LOG)
bt_conn_create_frag_timeout_debug(size_t reserve,k_timeout_t timeout,const char * func,int line)3109 struct net_buf *bt_conn_create_frag_timeout_debug(size_t reserve,
3110 k_timeout_t timeout,
3111 const char *func, int line)
3112 #else
3113 struct net_buf *bt_conn_create_frag_timeout(size_t reserve, k_timeout_t timeout)
3114 #endif
3115 {
3116 struct net_buf_pool *pool = NULL;
3117
3118 #if CONFIG_BT_L2CAP_TX_FRAG_COUNT > 0
3119 pool = &frag_pool;
3120 #endif
3121
3122 #if defined(CONFIG_NET_BUF_LOG)
3123 return bt_conn_create_pdu_timeout_debug(pool, reserve, timeout,
3124 func, line);
3125 #else
3126 return bt_conn_create_pdu_timeout(pool, reserve, timeout);
3127 #endif /* CONFIG_NET_BUF_LOG */
3128 }
3129
3130 #if defined(CONFIG_BT_SMP) || defined(CONFIG_BT_BREDR)
bt_conn_auth_cb_register(const struct bt_conn_auth_cb * cb)3131 int bt_conn_auth_cb_register(const struct bt_conn_auth_cb *cb)
3132 {
3133 if (!cb) {
3134 bt_auth = NULL;
3135 return 0;
3136 }
3137
3138 if (bt_auth) {
3139 return -EALREADY;
3140 }
3141
3142 /* The cancel callback must always be provided if the app provides
3143 * interactive callbacks.
3144 */
3145 if (!cb->cancel &&
3146 (cb->passkey_display || cb->passkey_entry || cb->passkey_confirm ||
3147 #if defined(CONFIG_BT_BREDR)
3148 cb->pincode_entry ||
3149 #endif
3150 cb->pairing_confirm)) {
3151 return -EINVAL;
3152 }
3153
3154 bt_auth = cb;
3155 return 0;
3156 }
3157
3158 #if defined(CONFIG_BT_SMP)
bt_conn_auth_cb_overlay(struct bt_conn * conn,const struct bt_conn_auth_cb * cb)3159 int bt_conn_auth_cb_overlay(struct bt_conn *conn, const struct bt_conn_auth_cb *cb)
3160 {
3161 CHECKIF(conn == NULL) {
3162 return -EINVAL;
3163 }
3164
3165 /* The cancel callback must always be provided if the app provides
3166 * interactive callbacks.
3167 */
3168 if (cb && !cb->cancel &&
3169 (cb->passkey_display || cb->passkey_entry || cb->passkey_confirm ||
3170 cb->pairing_confirm)) {
3171 return -EINVAL;
3172 }
3173
3174 if (conn->type == BT_CONN_TYPE_LE) {
3175 return bt_smp_auth_cb_overlay(conn, cb);
3176 }
3177
3178 return -ENOTSUP;
3179 }
3180 #endif
3181
bt_conn_auth_info_cb_register(struct bt_conn_auth_info_cb * cb)3182 int bt_conn_auth_info_cb_register(struct bt_conn_auth_info_cb *cb)
3183 {
3184 CHECKIF(cb == NULL) {
3185 return -EINVAL;
3186 }
3187
3188 sys_slist_append(&bt_auth_info_cbs, &cb->node);
3189
3190 return 0;
3191 }
3192
bt_conn_auth_info_cb_unregister(struct bt_conn_auth_info_cb * cb)3193 int bt_conn_auth_info_cb_unregister(struct bt_conn_auth_info_cb *cb)
3194 {
3195 CHECKIF(cb == NULL) {
3196 return -EINVAL;
3197 }
3198
3199 if (!sys_slist_find_and_remove(&bt_auth_info_cbs, &cb->node)) {
3200 return -EALREADY;
3201 }
3202
3203 return 0;
3204 }
3205
bt_conn_auth_passkey_entry(struct bt_conn * conn,unsigned int passkey)3206 int bt_conn_auth_passkey_entry(struct bt_conn *conn, unsigned int passkey)
3207 {
3208 if (IS_ENABLED(CONFIG_BT_SMP) && conn->type == BT_CONN_TYPE_LE) {
3209 return bt_smp_auth_passkey_entry(conn, passkey);
3210 }
3211
3212 if (IS_ENABLED(CONFIG_BT_BREDR) && conn->type == BT_CONN_TYPE_BR) {
3213 if (!bt_auth) {
3214 return -EINVAL;
3215 }
3216
3217 return bt_ssp_auth_passkey_entry(conn, passkey);
3218 }
3219
3220 return -EINVAL;
3221 }
3222
3223 #if defined(CONFIG_BT_PASSKEY_KEYPRESS)
bt_conn_auth_keypress_notify(struct bt_conn * conn,enum bt_conn_auth_keypress type)3224 int bt_conn_auth_keypress_notify(struct bt_conn *conn,
3225 enum bt_conn_auth_keypress type)
3226 {
3227 if (IS_ENABLED(CONFIG_BT_SMP) && conn->type == BT_CONN_TYPE_LE) {
3228 return bt_smp_auth_keypress_notify(conn, type);
3229 }
3230
3231 LOG_ERR("Not implemented for conn type %d", conn->type);
3232 return -EINVAL;
3233 }
3234 #endif
3235
bt_conn_auth_passkey_confirm(struct bt_conn * conn)3236 int bt_conn_auth_passkey_confirm(struct bt_conn *conn)
3237 {
3238 if (IS_ENABLED(CONFIG_BT_SMP) && conn->type == BT_CONN_TYPE_LE) {
3239 return bt_smp_auth_passkey_confirm(conn);
3240 }
3241
3242 if (IS_ENABLED(CONFIG_BT_BREDR) && conn->type == BT_CONN_TYPE_BR) {
3243 if (!bt_auth) {
3244 return -EINVAL;
3245 }
3246
3247 return bt_ssp_auth_passkey_confirm(conn);
3248 }
3249
3250 return -EINVAL;
3251 }
3252
bt_conn_auth_cancel(struct bt_conn * conn)3253 int bt_conn_auth_cancel(struct bt_conn *conn)
3254 {
3255 if (IS_ENABLED(CONFIG_BT_SMP) && conn->type == BT_CONN_TYPE_LE) {
3256 return bt_smp_auth_cancel(conn);
3257 }
3258
3259 if (IS_ENABLED(CONFIG_BT_BREDR) && conn->type == BT_CONN_TYPE_BR) {
3260 if (!bt_auth) {
3261 return -EINVAL;
3262 }
3263
3264 return bt_ssp_auth_cancel(conn);
3265 }
3266
3267 return -EINVAL;
3268 }
3269
bt_conn_auth_pairing_confirm(struct bt_conn * conn)3270 int bt_conn_auth_pairing_confirm(struct bt_conn *conn)
3271 {
3272 if (IS_ENABLED(CONFIG_BT_SMP) && conn->type == BT_CONN_TYPE_LE) {
3273 return bt_smp_auth_pairing_confirm(conn);
3274 }
3275
3276 if (IS_ENABLED(CONFIG_BT_BREDR) && conn->type == BT_CONN_TYPE_BR) {
3277 if (!bt_auth) {
3278 return -EINVAL;
3279 }
3280
3281 return bt_ssp_auth_pairing_confirm(conn);
3282 }
3283
3284 return -EINVAL;
3285 }
3286 #endif /* CONFIG_BT_SMP || CONFIG_BT_BREDR */
3287
bt_conn_lookup_index(uint8_t index)3288 struct bt_conn *bt_conn_lookup_index(uint8_t index)
3289 {
3290 if (index >= ARRAY_SIZE(acl_conns)) {
3291 return NULL;
3292 }
3293
3294 return bt_conn_ref(&acl_conns[index]);
3295 }
3296
bt_conn_init(void)3297 int bt_conn_init(void)
3298 {
3299 int err, i;
3300
3301 k_fifo_init(&free_tx);
3302 for (i = 0; i < ARRAY_SIZE(conn_tx); i++) {
3303 k_fifo_put(&free_tx, &conn_tx[i]);
3304 }
3305
3306 bt_att_init();
3307
3308 err = bt_smp_init();
3309 if (err) {
3310 return err;
3311 }
3312
3313 bt_l2cap_init();
3314
3315 /* Initialize background scan */
3316 if (IS_ENABLED(CONFIG_BT_CENTRAL)) {
3317 for (i = 0; i < ARRAY_SIZE(acl_conns); i++) {
3318 struct bt_conn *conn = bt_conn_ref(&acl_conns[i]);
3319
3320 if (!conn) {
3321 continue;
3322 }
3323
3324 #if !defined(CONFIG_BT_FILTER_ACCEPT_LIST)
3325 if (atomic_test_bit(conn->flags,
3326 BT_CONN_AUTO_CONNECT)) {
3327 /* Only the default identity is supported */
3328 conn->id = BT_ID_DEFAULT;
3329 bt_conn_set_state(conn,
3330 BT_CONN_CONNECTING_SCAN);
3331 }
3332 #endif /* !defined(CONFIG_BT_FILTER_ACCEPT_LIST) */
3333
3334 bt_conn_unref(conn);
3335 }
3336 }
3337
3338 return 0;
3339 }
3340
3341 #if defined(CONFIG_BT_DF_CONNECTION_CTE_RX)
bt_hci_le_df_connection_iq_report_common(uint8_t event,struct net_buf * buf)3342 void bt_hci_le_df_connection_iq_report_common(uint8_t event, struct net_buf *buf)
3343 {
3344 struct bt_df_conn_iq_samples_report iq_report;
3345 struct bt_conn *conn;
3346 int err;
3347
3348 if (event == BT_HCI_EVT_LE_CONNECTION_IQ_REPORT) {
3349 err = hci_df_prepare_connection_iq_report(buf, &iq_report, &conn);
3350 if (err) {
3351 LOG_ERR("Prepare CTE conn IQ report failed %d", err);
3352 return;
3353 }
3354 } else if (IS_ENABLED(CONFIG_BT_DF_VS_CONN_IQ_REPORT_16_BITS_IQ_SAMPLES) &&
3355 event == BT_HCI_EVT_VS_LE_CONNECTION_IQ_REPORT) {
3356 err = hci_df_vs_prepare_connection_iq_report(buf, &iq_report, &conn);
3357 if (err) {
3358 LOG_ERR("Prepare CTE conn IQ report failed %d", err);
3359 return;
3360 }
3361 } else {
3362 LOG_ERR("Unhandled VS connection IQ report");
3363 return;
3364 }
3365
3366 for (struct bt_conn_cb *cb = callback_list; cb; cb = cb->_next) {
3367 if (cb->cte_report_cb) {
3368 cb->cte_report_cb(conn, &iq_report);
3369 }
3370 }
3371
3372 STRUCT_SECTION_FOREACH(bt_conn_cb, cb)
3373 {
3374 if (cb->cte_report_cb) {
3375 cb->cte_report_cb(conn, &iq_report);
3376 }
3377 }
3378
3379 bt_conn_unref(conn);
3380 }
3381
bt_hci_le_df_connection_iq_report(struct net_buf * buf)3382 void bt_hci_le_df_connection_iq_report(struct net_buf *buf)
3383 {
3384 bt_hci_le_df_connection_iq_report_common(BT_HCI_EVT_LE_CONNECTION_IQ_REPORT, buf);
3385 }
3386
3387 #if defined(CONFIG_BT_DF_VS_CONN_IQ_REPORT_16_BITS_IQ_SAMPLES)
bt_hci_le_vs_df_connection_iq_report(struct net_buf * buf)3388 void bt_hci_le_vs_df_connection_iq_report(struct net_buf *buf)
3389 {
3390 bt_hci_le_df_connection_iq_report_common(BT_HCI_EVT_VS_LE_CONNECTION_IQ_REPORT, buf);
3391 }
3392 #endif /* CONFIG_BT_DF_VS_CONN_IQ_REPORT_16_BITS_IQ_SAMPLES */
3393 #endif /* CONFIG_BT_DF_CONNECTION_CTE_RX */
3394
3395 #if defined(CONFIG_BT_DF_CONNECTION_CTE_REQ)
bt_hci_le_df_cte_req_failed(struct net_buf * buf)3396 void bt_hci_le_df_cte_req_failed(struct net_buf *buf)
3397 {
3398 struct bt_df_conn_iq_samples_report iq_report;
3399 struct bt_conn *conn;
3400 int err;
3401
3402 err = hci_df_prepare_conn_cte_req_failed(buf, &iq_report, &conn);
3403 if (err) {
3404 LOG_ERR("Prepare CTE REQ failed IQ report failed %d", err);
3405 return;
3406 }
3407
3408 for (struct bt_conn_cb *cb = callback_list; cb; cb = cb->_next) {
3409 if (cb->cte_report_cb) {
3410 cb->cte_report_cb(conn, &iq_report);
3411 }
3412 }
3413
3414 STRUCT_SECTION_FOREACH(bt_conn_cb, cb)
3415 {
3416 if (cb->cte_report_cb) {
3417 cb->cte_report_cb(conn, &iq_report);
3418 }
3419 }
3420
3421 bt_conn_unref(conn);
3422 }
3423 #endif /* CONFIG_BT_DF_CONNECTION_CTE_REQ */
3424
3425 #endif /* CONFIG_BT_CONN */
3426