-
Notifications
You must be signed in to change notification settings - Fork 6.1k
/
hci_core.c
4475 lines (3678 loc) · 115 KB
/
hci_core.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* hci_core.c - HCI core Bluetooth handling */
/*
* Copyright (c) 2017-2021 Nordic Semiconductor ASA
* Copyright (c) 2015-2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <zephyr/sys/atomic.h>
#include <zephyr/sys/check.h>
#include <zephyr/sys/util.h>
#include <zephyr/sys/slist.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/debug/stack.h>
#include <zephyr/sys/__assert.h>
#include <soc.h>
#include <zephyr/settings/settings.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/conn.h>
#include <zephyr/bluetooth/l2cap.h>
#include <zephyr/bluetooth/hci.h>
#include <zephyr/bluetooth/hci_vs.h>
#include <zephyr/drivers/bluetooth/hci_driver.h>
#include "common/bt_str.h"
#include "common/assert.h"
#include "common/rpa.h"
#include "keys.h"
#include "monitor.h"
#include "hci_core.h"
#include "hci_ecc.h"
#include "ecc.h"
#include "id.h"
#include "adv.h"
#include "scan.h"
#include "addr_internal.h"
#include "conn_internal.h"
#include "iso_internal.h"
#include "l2cap_internal.h"
#include "gatt_internal.h"
#include "smp.h"
#include "crypto.h"
#include "settings.h"
#if defined(CONFIG_BT_CLASSIC)
#include "classic/br.h"
#endif
#if defined(CONFIG_BT_DF)
#include "direction_internal.h"
#endif /* CONFIG_BT_DF */
#define LOG_LEVEL CONFIG_BT_HCI_CORE_LOG_LEVEL
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(bt_hci_core);
#define HCI_CMD_TIMEOUT K_SECONDS(10)
/* Stacks for the threads */
#if !defined(CONFIG_BT_RECV_BLOCKING)
static void rx_work_handler(struct k_work *work);
static K_WORK_DEFINE(rx_work, rx_work_handler);
#if defined(CONFIG_BT_RECV_WORKQ_BT)
static struct k_work_q bt_workq;
static K_KERNEL_STACK_DEFINE(rx_thread_stack, CONFIG_BT_RX_STACK_SIZE);
#endif /* CONFIG_BT_RECV_WORKQ_BT */
#endif /* !CONFIG_BT_RECV_BLOCKING */
static struct k_thread tx_thread_data;
static K_KERNEL_STACK_DEFINE(tx_thread_stack, CONFIG_BT_HCI_TX_STACK_SIZE);
static void init_work(struct k_work *work);
struct bt_dev bt_dev = {
.init = Z_WORK_INITIALIZER(init_work),
#if defined(CONFIG_BT_PRIVACY)
.rpa_timeout = CONFIG_BT_RPA_TIMEOUT,
#endif
#if defined(CONFIG_BT_DEVICE_APPEARANCE_DYNAMIC)
.appearance = CONFIG_BT_DEVICE_APPEARANCE,
#endif
};
static bt_ready_cb_t ready_cb;
#if defined(CONFIG_BT_HCI_VS_EVT_USER)
static bt_hci_vnd_evt_cb_t *hci_vnd_evt_cb;
#endif /* CONFIG_BT_HCI_VS_EVT_USER */
struct cmd_data {
/** HCI status of the command completion */
uint8_t status;
/** The command OpCode that the buffer contains */
uint16_t opcode;
/** The state to update when command completes with success. */
struct bt_hci_cmd_state_set *state;
/** Used by bt_hci_cmd_send_sync. */
struct k_sem *sync;
};
static struct cmd_data cmd_data[CONFIG_BT_BUF_CMD_TX_COUNT];
#define cmd(buf) (&cmd_data[net_buf_id(buf)])
#define acl(buf) ((struct acl_data *)net_buf_user_data(buf))
void bt_hci_cmd_state_set_init(struct net_buf *buf,
struct bt_hci_cmd_state_set *state,
atomic_t *target, int bit, bool val)
{
state->target = target;
state->bit = bit;
state->val = val;
cmd(buf)->state = state;
}
/* HCI command buffers. Derive the needed size from both Command and Event
* buffer length since the buffer is also used for the response event i.e
* command complete or command status.
*/
#define CMD_BUF_SIZE MAX(BT_BUF_EVT_RX_SIZE, BT_BUF_CMD_TX_SIZE)
NET_BUF_POOL_FIXED_DEFINE(hci_cmd_pool, CONFIG_BT_BUF_CMD_TX_COUNT,
CMD_BUF_SIZE, sizeof(struct bt_buf_data), NULL);
struct event_handler {
uint8_t event;
uint8_t min_len;
void (*handler)(struct net_buf *buf);
};
#define EVENT_HANDLER(_evt, _handler, _min_len) \
{ \
.event = _evt, \
.handler = _handler, \
.min_len = _min_len, \
}
static int handle_event_common(uint8_t event, struct net_buf *buf,
const struct event_handler *handlers, size_t num_handlers)
{
size_t i;
for (i = 0; i < num_handlers; i++) {
const struct event_handler *handler = &handlers[i];
if (handler->event != event) {
continue;
}
if (buf->len < handler->min_len) {
LOG_ERR("Too small (%u bytes) event 0x%02x", buf->len, event);
return -EINVAL;
}
handler->handler(buf);
return 0;
}
return -EOPNOTSUPP;
}
static void handle_event(uint8_t event, struct net_buf *buf, const struct event_handler *handlers,
size_t num_handlers)
{
int err;
err = handle_event_common(event, buf, handlers, num_handlers);
if (err == -EOPNOTSUPP) {
LOG_WRN("Unhandled event 0x%02x len %u: %s", event, buf->len,
bt_hex(buf->data, buf->len));
}
/* Other possible errors are handled by handle_event_common function */
}
static void handle_vs_event(uint8_t event, struct net_buf *buf,
const struct event_handler *handlers, size_t num_handlers)
{
int err;
err = handle_event_common(event, buf, handlers, num_handlers);
if (err == -EOPNOTSUPP) {
LOG_WRN("Unhandled vendor-specific event: %s", bt_hex(buf->data, buf->len));
}
/* Other possible errors are handled by handle_event_common function */
}
#if defined(CONFIG_BT_HCI_ACL_FLOW_CONTROL)
void bt_hci_host_num_completed_packets(struct net_buf *buf)
{
struct bt_hci_cp_host_num_completed_packets *cp;
uint16_t handle = acl(buf)->handle;
struct bt_hci_handle_count *hc;
struct bt_conn *conn;
uint8_t index = acl(buf)->index;
net_buf_destroy(buf);
/* Do nothing if controller to host flow control is not supported */
if (!BT_CMD_TEST(bt_dev.supported_commands, 10, 5)) {
return;
}
conn = bt_conn_lookup_index(index);
if (!conn) {
LOG_WRN("Unable to look up conn with index 0x%02x", index);
return;
}
if (conn->state != BT_CONN_CONNECTED &&
conn->state != BT_CONN_DISCONNECTING) {
LOG_WRN("Not reporting packet for non-connected conn");
bt_conn_unref(conn);
return;
}
bt_conn_unref(conn);
LOG_DBG("Reporting completed packet for handle %u", handle);
buf = bt_hci_cmd_create(BT_HCI_OP_HOST_NUM_COMPLETED_PACKETS,
sizeof(*cp) + sizeof(*hc));
if (!buf) {
LOG_ERR("Unable to allocate new HCI command");
return;
}
cp = net_buf_add(buf, sizeof(*cp));
cp->num_handles = sys_cpu_to_le16(1);
hc = net_buf_add(buf, sizeof(*hc));
hc->handle = sys_cpu_to_le16(handle);
hc->count = sys_cpu_to_le16(1);
bt_hci_cmd_send(BT_HCI_OP_HOST_NUM_COMPLETED_PACKETS, buf);
}
#endif /* defined(CONFIG_BT_HCI_ACL_FLOW_CONTROL) */
struct net_buf *bt_hci_cmd_create(uint16_t opcode, uint8_t param_len)
{
struct bt_hci_cmd_hdr *hdr;
struct net_buf *buf;
LOG_DBG("opcode 0x%04x param_len %u", opcode, param_len);
buf = net_buf_alloc(&hci_cmd_pool, K_FOREVER);
__ASSERT_NO_MSG(buf);
LOG_DBG("buf %p", buf);
net_buf_reserve(buf, BT_BUF_RESERVE);
bt_buf_set_type(buf, BT_BUF_CMD);
cmd(buf)->opcode = opcode;
cmd(buf)->sync = NULL;
cmd(buf)->state = NULL;
hdr = net_buf_add(buf, sizeof(*hdr));
hdr->opcode = sys_cpu_to_le16(opcode);
hdr->param_len = param_len;
return buf;
}
int bt_hci_cmd_send(uint16_t opcode, struct net_buf *buf)
{
if (!buf) {
buf = bt_hci_cmd_create(opcode, 0);
if (!buf) {
return -ENOBUFS;
}
}
LOG_DBG("opcode 0x%04x len %u", opcode, buf->len);
/* Host Number of Completed Packets can ignore the ncmd value
* and does not generate any cmd complete/status events.
*/
if (opcode == BT_HCI_OP_HOST_NUM_COMPLETED_PACKETS) {
int err;
err = bt_send(buf);
if (err) {
LOG_ERR("Unable to send to driver (err %d)", err);
net_buf_unref(buf);
}
return err;
}
net_buf_put(&bt_dev.cmd_tx_queue, buf);
return 0;
}
int bt_hci_cmd_send_sync(uint16_t opcode, struct net_buf *buf,
struct net_buf **rsp)
{
struct k_sem sync_sem;
uint8_t status;
int err;
if (!buf) {
buf = bt_hci_cmd_create(opcode, 0);
if (!buf) {
return -ENOBUFS;
}
}
LOG_DBG("buf %p opcode 0x%04x len %u", buf, opcode, buf->len);
k_sem_init(&sync_sem, 0, 1);
cmd(buf)->sync = &sync_sem;
net_buf_put(&bt_dev.cmd_tx_queue, net_buf_ref(buf));
err = k_sem_take(&sync_sem, HCI_CMD_TIMEOUT);
BT_ASSERT_MSG(err == 0, "command opcode 0x%04x timeout with err %d", opcode, err);
status = cmd(buf)->status;
if (status) {
LOG_WRN("opcode 0x%04x status 0x%02x", opcode, status);
net_buf_unref(buf);
switch (status) {
case BT_HCI_ERR_CONN_LIMIT_EXCEEDED:
return -ECONNREFUSED;
case BT_HCI_ERR_INSUFFICIENT_RESOURCES:
return -ENOMEM;
case BT_HCI_ERR_INVALID_PARAM:
return -EINVAL;
default:
return -EIO;
}
}
LOG_DBG("rsp %p opcode 0x%04x len %u", buf, opcode, buf->len);
if (rsp) {
*rsp = buf;
} else {
net_buf_unref(buf);
}
return 0;
}
int bt_hci_le_rand(void *buffer, size_t len)
{
struct bt_hci_rp_le_rand *rp;
struct net_buf *rsp;
size_t count;
int err;
/* Check first that HCI_LE_Rand is supported */
if (!BT_CMD_TEST(bt_dev.supported_commands, 27, 7)) {
return -ENOTSUP;
}
while (len > 0) {
/* Number of bytes to fill on this iteration */
count = MIN(len, sizeof(rp->rand));
/* Request the next 8 bytes over HCI */
err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_RAND, NULL, &rsp);
if (err) {
return err;
}
/* Copy random data into buffer */
rp = (void *)rsp->data;
memcpy(buffer, rp->rand, count);
net_buf_unref(rsp);
buffer = (uint8_t *)buffer + count;
len -= count;
}
return 0;
}
static int hci_le_read_max_data_len(uint16_t *tx_octets, uint16_t *tx_time)
{
struct bt_hci_rp_le_read_max_data_len *rp;
struct net_buf *rsp;
int err;
err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_READ_MAX_DATA_LEN, NULL, &rsp);
if (err) {
LOG_ERR("Failed to read DLE max data len");
return err;
}
rp = (void *)rsp->data;
*tx_octets = sys_le16_to_cpu(rp->max_tx_octets);
*tx_time = sys_le16_to_cpu(rp->max_tx_time);
net_buf_unref(rsp);
if (*tx_octets < BT_HCI_LE_MAX_TX_OCTETS_MIN ||
*tx_octets > BT_HCI_LE_MAX_TX_OCTETS_MAX) {
LOG_WRN("tx_octets exceeds the valid range %u", *tx_octets);
}
if (*tx_time < BT_HCI_LE_MAX_TX_TIME_MIN ||
*tx_time > BT_HCI_LE_MAX_TX_TIME_MAX) {
LOG_WRN("tx_time exceeds the valid range %u", *tx_time);
}
return 0;
}
uint8_t bt_get_phy(uint8_t hci_phy)
{
switch (hci_phy) {
case BT_HCI_LE_PHY_1M:
return BT_GAP_LE_PHY_1M;
case BT_HCI_LE_PHY_2M:
return BT_GAP_LE_PHY_2M;
case BT_HCI_LE_PHY_CODED:
return BT_GAP_LE_PHY_CODED;
default:
return 0;
}
}
int bt_get_df_cte_type(uint8_t hci_cte_type)
{
switch (hci_cte_type) {
case BT_HCI_LE_AOA_CTE:
return BT_DF_CTE_TYPE_AOA;
case BT_HCI_LE_AOD_CTE_1US:
return BT_DF_CTE_TYPE_AOD_1US;
case BT_HCI_LE_AOD_CTE_2US:
return BT_DF_CTE_TYPE_AOD_2US;
case BT_HCI_LE_NO_CTE:
return BT_DF_CTE_TYPE_NONE;
default:
return BT_DF_CTE_TYPE_NONE;
}
}
#if defined(CONFIG_BT_CONN_TX)
static void hci_num_completed_packets(struct net_buf *buf)
{
struct bt_hci_evt_num_completed_packets *evt = (void *)buf->data;
int i;
if (sizeof(*evt) + sizeof(evt->h[0]) * evt->num_handles > buf->len) {
LOG_ERR("evt num_handles (=%u) too large (%u > %u)",
evt->num_handles,
sizeof(*evt) + sizeof(evt->h[0]) * evt->num_handles,
buf->len);
return;
}
LOG_DBG("num_handles %u", evt->num_handles);
for (i = 0; i < evt->num_handles; i++) {
uint16_t handle, count;
struct bt_conn *conn;
handle = sys_le16_to_cpu(evt->h[i].handle);
count = sys_le16_to_cpu(evt->h[i].count);
LOG_DBG("handle %u count %u", handle, count);
conn = bt_conn_lookup_handle(handle, BT_CONN_TYPE_ALL);
if (!conn) {
LOG_ERR("No connection for handle %u", handle);
continue;
}
while (count--) {
struct bt_conn_tx *tx;
sys_snode_t *node;
unsigned int key;
key = irq_lock();
if (conn->pending_no_cb) {
conn->pending_no_cb--;
irq_unlock(key);
k_sem_give(bt_conn_get_pkts(conn));
continue;
}
node = sys_slist_get(&conn->tx_pending);
irq_unlock(key);
if (!node) {
LOG_ERR("packets count mismatch");
break;
}
tx = CONTAINER_OF(node, struct bt_conn_tx, node);
key = irq_lock();
conn->pending_no_cb = tx->pending_no_cb;
tx->pending_no_cb = 0U;
sys_slist_append(&conn->tx_complete, &tx->node);
irq_unlock(key);
k_work_submit(&conn->tx_complete_work);
k_sem_give(bt_conn_get_pkts(conn));
}
bt_conn_unref(conn);
}
}
#endif /* CONFIG_BT_CONN_TX */
#if defined(CONFIG_BT_CONN)
static void hci_acl(struct net_buf *buf)
{
struct bt_hci_acl_hdr *hdr;
uint16_t handle, len;
struct bt_conn *conn;
uint8_t flags;
LOG_DBG("buf %p", buf);
if (buf->len < sizeof(*hdr)) {
LOG_ERR("Invalid HCI ACL packet size (%u)", buf->len);
net_buf_unref(buf);
return;
}
hdr = net_buf_pull_mem(buf, sizeof(*hdr));
len = sys_le16_to_cpu(hdr->len);
handle = sys_le16_to_cpu(hdr->handle);
flags = bt_acl_flags(handle);
acl(buf)->handle = bt_acl_handle(handle);
acl(buf)->index = BT_CONN_INDEX_INVALID;
LOG_DBG("handle %u len %u flags %u", acl(buf)->handle, len, flags);
if (buf->len != len) {
LOG_ERR("ACL data length mismatch (%u != %u)", buf->len, len);
net_buf_unref(buf);
return;
}
conn = bt_conn_lookup_handle(acl(buf)->handle, BT_CONN_TYPE_ALL);
if (!conn) {
LOG_ERR("Unable to find conn for handle %u", acl(buf)->handle);
net_buf_unref(buf);
return;
}
acl(buf)->index = bt_conn_index(conn);
bt_conn_recv(conn, buf, flags);
bt_conn_unref(conn);
}
static void hci_data_buf_overflow(struct net_buf *buf)
{
struct bt_hci_evt_data_buf_overflow *evt = (void *)buf->data;
LOG_WRN("Data buffer overflow (link type 0x%02x)", evt->link_type);
}
#if defined(CONFIG_BT_CENTRAL)
static void set_phy_conn_param(const struct bt_conn *conn,
struct bt_hci_ext_conn_phy *phy)
{
phy->conn_interval_min = sys_cpu_to_le16(conn->le.interval_min);
phy->conn_interval_max = sys_cpu_to_le16(conn->le.interval_max);
phy->conn_latency = sys_cpu_to_le16(conn->le.latency);
phy->supervision_timeout = sys_cpu_to_le16(conn->le.timeout);
phy->min_ce_len = 0;
phy->max_ce_len = 0;
}
int bt_le_create_conn_ext(const struct bt_conn *conn)
{
struct bt_hci_cp_le_ext_create_conn *cp;
struct bt_hci_ext_conn_phy *phy;
struct bt_hci_cmd_state_set state;
bool use_filter = false;
struct net_buf *buf;
uint8_t own_addr_type;
uint8_t num_phys;
int err;
if (IS_ENABLED(CONFIG_BT_FILTER_ACCEPT_LIST)) {
use_filter = atomic_test_bit(conn->flags, BT_CONN_AUTO_CONNECT);
}
err = bt_id_set_create_conn_own_addr(use_filter, &own_addr_type);
if (err) {
return err;
}
num_phys = (!(bt_dev.create_param.options &
BT_CONN_LE_OPT_NO_1M) ? 1 : 0) +
((bt_dev.create_param.options &
BT_CONN_LE_OPT_CODED) ? 1 : 0);
buf = bt_hci_cmd_create(BT_HCI_OP_LE_EXT_CREATE_CONN, sizeof(*cp) +
num_phys * sizeof(*phy));
if (!buf) {
return -ENOBUFS;
}
cp = net_buf_add(buf, sizeof(*cp));
(void)memset(cp, 0, sizeof(*cp));
if (use_filter) {
/* User Initiated procedure use fast scan parameters. */
bt_addr_le_copy(&cp->peer_addr, BT_ADDR_LE_ANY);
cp->filter_policy = BT_HCI_LE_CREATE_CONN_FP_FILTER;
} else {
const bt_addr_le_t *peer_addr = &conn->le.dst;
#if defined(CONFIG_BT_SMP)
if (bt_dev.le.rl_entries > bt_dev.le.rl_size) {
/* Host resolving is used, use the RPA directly. */
peer_addr = &conn->le.resp_addr;
}
#endif
bt_addr_le_copy(&cp->peer_addr, peer_addr);
cp->filter_policy = BT_HCI_LE_CREATE_CONN_FP_NO_FILTER;
}
cp->own_addr_type = own_addr_type;
cp->phys = 0;
if (!(bt_dev.create_param.options & BT_CONN_LE_OPT_NO_1M)) {
cp->phys |= BT_HCI_LE_EXT_SCAN_PHY_1M;
phy = net_buf_add(buf, sizeof(*phy));
phy->scan_interval = sys_cpu_to_le16(
bt_dev.create_param.interval);
phy->scan_window = sys_cpu_to_le16(
bt_dev.create_param.window);
set_phy_conn_param(conn, phy);
}
if (bt_dev.create_param.options & BT_CONN_LE_OPT_CODED) {
cp->phys |= BT_HCI_LE_EXT_SCAN_PHY_CODED;
phy = net_buf_add(buf, sizeof(*phy));
phy->scan_interval = sys_cpu_to_le16(
bt_dev.create_param.interval_coded);
phy->scan_window = sys_cpu_to_le16(
bt_dev.create_param.window_coded);
set_phy_conn_param(conn, phy);
}
bt_hci_cmd_state_set_init(buf, &state, bt_dev.flags,
BT_DEV_INITIATING, true);
return bt_hci_cmd_send_sync(BT_HCI_OP_LE_EXT_CREATE_CONN, buf, NULL);
}
int bt_le_create_conn_synced(const struct bt_conn *conn, const struct bt_le_ext_adv *adv,
uint8_t subevent)
{
struct bt_hci_cp_le_ext_create_conn_v2 *cp;
struct bt_hci_ext_conn_phy *phy;
struct bt_hci_cmd_state_set state;
struct net_buf *buf;
uint8_t own_addr_type;
int err;
err = bt_id_set_create_conn_own_addr(false, &own_addr_type);
if (err) {
return err;
}
/* There shall only be one Initiating_PHYs */
buf = bt_hci_cmd_create(BT_HCI_OP_LE_EXT_CREATE_CONN_V2, sizeof(*cp) + sizeof(*phy));
if (!buf) {
return -ENOBUFS;
}
cp = net_buf_add(buf, sizeof(*cp));
(void)memset(cp, 0, sizeof(*cp));
cp->subevent = subevent;
cp->adv_handle = adv->handle;
bt_addr_le_copy(&cp->peer_addr, &conn->le.dst);
cp->filter_policy = BT_HCI_LE_CREATE_CONN_FP_NO_FILTER;
cp->own_addr_type = own_addr_type;
/* The Initiating_PHY is the secondary phy of the corresponding ext adv set */
if (adv->options & BT_LE_ADV_OPT_CODED) {
cp->phys = BT_HCI_LE_EXT_SCAN_PHY_CODED;
} else if (adv->options & BT_LE_ADV_OPT_NO_2M) {
cp->phys = BT_HCI_LE_EXT_SCAN_PHY_1M;
} else {
cp->phys = BT_HCI_LE_EXT_SCAN_PHY_2M;
}
phy = net_buf_add(buf, sizeof(*phy));
(void)memset(phy, 0, sizeof(*phy));
set_phy_conn_param(conn, phy);
bt_hci_cmd_state_set_init(buf, &state, bt_dev.flags, BT_DEV_INITIATING, true);
return bt_hci_cmd_send_sync(BT_HCI_OP_LE_EXT_CREATE_CONN_V2, buf, NULL);
}
static int bt_le_create_conn_legacy(const struct bt_conn *conn)
{
struct bt_hci_cp_le_create_conn *cp;
struct bt_hci_cmd_state_set state;
bool use_filter = false;
struct net_buf *buf;
uint8_t own_addr_type;
int err;
if (IS_ENABLED(CONFIG_BT_FILTER_ACCEPT_LIST)) {
use_filter = atomic_test_bit(conn->flags, BT_CONN_AUTO_CONNECT);
}
err = bt_id_set_create_conn_own_addr(use_filter, &own_addr_type);
if (err) {
return err;
}
buf = bt_hci_cmd_create(BT_HCI_OP_LE_CREATE_CONN, sizeof(*cp));
if (!buf) {
return -ENOBUFS;
}
cp = net_buf_add(buf, sizeof(*cp));
memset(cp, 0, sizeof(*cp));
cp->own_addr_type = own_addr_type;
if (use_filter) {
/* User Initiated procedure use fast scan parameters. */
bt_addr_le_copy(&cp->peer_addr, BT_ADDR_LE_ANY);
cp->filter_policy = BT_HCI_LE_CREATE_CONN_FP_FILTER;
} else {
const bt_addr_le_t *peer_addr = &conn->le.dst;
#if defined(CONFIG_BT_SMP)
if (bt_dev.le.rl_entries > bt_dev.le.rl_size) {
/* Host resolving is used, use the RPA directly. */
peer_addr = &conn->le.resp_addr;
}
#endif
bt_addr_le_copy(&cp->peer_addr, peer_addr);
cp->filter_policy = BT_HCI_LE_CREATE_CONN_FP_NO_FILTER;
}
cp->scan_interval = sys_cpu_to_le16(bt_dev.create_param.interval);
cp->scan_window = sys_cpu_to_le16(bt_dev.create_param.window);
cp->conn_interval_min = sys_cpu_to_le16(conn->le.interval_min);
cp->conn_interval_max = sys_cpu_to_le16(conn->le.interval_max);
cp->conn_latency = sys_cpu_to_le16(conn->le.latency);
cp->supervision_timeout = sys_cpu_to_le16(conn->le.timeout);
bt_hci_cmd_state_set_init(buf, &state, bt_dev.flags,
BT_DEV_INITIATING, true);
return bt_hci_cmd_send_sync(BT_HCI_OP_LE_CREATE_CONN, buf, NULL);
}
int bt_le_create_conn(const struct bt_conn *conn)
{
if (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
return bt_le_create_conn_ext(conn);
}
return bt_le_create_conn_legacy(conn);
}
int bt_le_create_conn_cancel(void)
{
struct net_buf *buf;
struct bt_hci_cmd_state_set state;
buf = bt_hci_cmd_create(BT_HCI_OP_LE_CREATE_CONN_CANCEL, 0);
bt_hci_cmd_state_set_init(buf, &state, bt_dev.flags,
BT_DEV_INITIATING, false);
return bt_hci_cmd_send_sync(BT_HCI_OP_LE_CREATE_CONN_CANCEL, buf, NULL);
}
#endif /* CONFIG_BT_CENTRAL */
int bt_hci_disconnect(uint16_t handle, uint8_t reason)
{
struct net_buf *buf;
struct bt_hci_cp_disconnect *disconn;
buf = bt_hci_cmd_create(BT_HCI_OP_DISCONNECT, sizeof(*disconn));
if (!buf) {
return -ENOBUFS;
}
disconn = net_buf_add(buf, sizeof(*disconn));
disconn->handle = sys_cpu_to_le16(handle);
disconn->reason = reason;
return bt_hci_cmd_send_sync(BT_HCI_OP_DISCONNECT, buf, NULL);
}
static uint16_t disconnected_handles[CONFIG_BT_MAX_CONN];
static void disconnected_handles_reset(void)
{
(void)memset(disconnected_handles, 0, sizeof(disconnected_handles));
}
static void conn_handle_disconnected(uint16_t handle)
{
for (int i = 0; i < ARRAY_SIZE(disconnected_handles); i++) {
if (!disconnected_handles[i]) {
/* Use invalid connection handle bits so that connection
* handle 0 can be used as a valid non-zero handle.
*/
disconnected_handles[i] = ~BT_ACL_HANDLE_MASK | handle;
}
}
}
static bool conn_handle_is_disconnected(uint16_t handle)
{
handle |= ~BT_ACL_HANDLE_MASK;
for (int i = 0; i < ARRAY_SIZE(disconnected_handles); i++) {
if (disconnected_handles[i] == handle) {
disconnected_handles[i] = 0;
return true;
}
}
return false;
}
static void hci_disconn_complete_prio(struct net_buf *buf)
{
struct bt_hci_evt_disconn_complete *evt = (void *)buf->data;
uint16_t handle = sys_le16_to_cpu(evt->handle);
struct bt_conn *conn;
LOG_DBG("status 0x%02x handle %u reason 0x%02x", evt->status, handle, evt->reason);
if (evt->status) {
return;
}
conn = bt_conn_lookup_handle(handle, BT_CONN_TYPE_ALL);
if (!conn) {
/* Priority disconnect complete event received before normal
* connection complete event.
*/
conn_handle_disconnected(handle);
return;
}
conn->err = evt->reason;
bt_conn_set_state(conn, BT_CONN_DISCONNECT_COMPLETE);
bt_conn_unref(conn);
}
static void hci_disconn_complete(struct net_buf *buf)
{
struct bt_hci_evt_disconn_complete *evt = (void *)buf->data;
uint16_t handle = sys_le16_to_cpu(evt->handle);
struct bt_conn *conn;
LOG_DBG("status 0x%02x handle %u reason 0x%02x", evt->status, handle, evt->reason);
if (evt->status) {
return;
}
conn = bt_conn_lookup_handle(handle, BT_CONN_TYPE_ALL);
if (!conn) {
LOG_ERR("Unable to look up conn with handle %u", handle);
return;
}
bt_conn_set_state(conn, BT_CONN_DISCONNECTED);
if (conn->type != BT_CONN_TYPE_LE) {
#if defined(CONFIG_BT_CLASSIC)
if (conn->type == BT_CONN_TYPE_SCO) {
bt_sco_cleanup(conn);
return;
}
/*
* If only for one connection session bond was set, clear keys
* database row for this connection.
*/
if (conn->type == BT_CONN_TYPE_BR &&
atomic_test_and_clear_bit(conn->flags, BT_CONN_BR_NOBOND)) {
bt_keys_link_key_clear(conn->br.link_key);
}
#endif
bt_conn_unref(conn);
return;
}
#if defined(CONFIG_BT_CENTRAL) && !defined(CONFIG_BT_FILTER_ACCEPT_LIST)
if (atomic_test_bit(conn->flags, BT_CONN_AUTO_CONNECT)) {
bt_conn_set_state(conn, BT_CONN_CONNECTING_SCAN);
bt_le_scan_update(false);
}
#endif /* defined(CONFIG_BT_CENTRAL) && !defined(CONFIG_BT_FILTER_ACCEPT_LIST) */
bt_conn_unref(conn);
}
static int hci_le_read_remote_features(struct bt_conn *conn)
{
struct bt_hci_cp_le_read_remote_features *cp;
struct net_buf *buf;
buf = bt_hci_cmd_create(BT_HCI_OP_LE_READ_REMOTE_FEATURES,
sizeof(*cp));
if (!buf) {
return -ENOBUFS;
}
cp = net_buf_add(buf, sizeof(*cp));
cp->handle = sys_cpu_to_le16(conn->handle);
return bt_hci_cmd_send_sync(BT_HCI_OP_LE_READ_REMOTE_FEATURES, buf, NULL);
}
static int hci_read_remote_version(struct bt_conn *conn)
{
struct bt_hci_cp_read_remote_version_info *cp;
struct net_buf *buf;
if (conn->state != BT_CONN_CONNECTED) {
return -ENOTCONN;
}
/* Remote version cannot change. */
if (atomic_test_bit(conn->flags, BT_CONN_AUTO_VERSION_INFO)) {
return 0;
}
buf = bt_hci_cmd_create(BT_HCI_OP_READ_REMOTE_VERSION_INFO,
sizeof(*cp));
if (!buf) {
return -ENOBUFS;
}
cp = net_buf_add(buf, sizeof(*cp));
cp->handle = sys_cpu_to_le16(conn->handle);
return bt_hci_cmd_send_sync(BT_HCI_OP_READ_REMOTE_VERSION_INFO, buf,
NULL);
}
/* LE Data Length Change Event is optional so this function just ignore
* error and stack will continue to use default values.
*/
int bt_le_set_data_len(struct bt_conn *conn, uint16_t tx_octets, uint16_t tx_time)
{
struct bt_hci_cp_le_set_data_len *cp;
struct net_buf *buf;
buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_DATA_LEN, sizeof(*cp));
if (!buf) {
return -ENOBUFS;
}
cp = net_buf_add(buf, sizeof(*cp));
cp->handle = sys_cpu_to_le16(conn->handle);
cp->tx_octets = sys_cpu_to_le16(tx_octets);
cp->tx_time = sys_cpu_to_le16(tx_time);
return bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_DATA_LEN, buf, NULL);
}
#if defined(CONFIG_BT_USER_PHY_UPDATE)
static int hci_le_read_phy(struct bt_conn *conn)
{
struct bt_hci_cp_le_read_phy *cp;
struct bt_hci_rp_le_read_phy *rp;
struct net_buf *buf, *rsp;
int err;
buf = bt_hci_cmd_create(BT_HCI_OP_LE_READ_PHY, sizeof(*cp));
if (!buf) {
return -ENOBUFS;
}
cp = net_buf_add(buf, sizeof(*cp));
cp->handle = sys_cpu_to_le16(conn->handle);