-
Notifications
You must be signed in to change notification settings - Fork 680
Expand file tree
/
Copy pathhci.c
More file actions
11238 lines (10060 loc) · 463 KB
/
hci.c
File metadata and controls
11238 lines (10060 loc) · 463 KB
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
/*
* Copyright (C) 2014 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
* GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
#define BTSTACK_FILE__ "hci.c"
/*
* hci.c
*
* Created by Matthias Ringwald on 4/29/09.
*
*/
#include "btstack_config.h"
#ifdef ENABLE_CLASSIC
#ifdef HAVE_EMBEDDED_TICK
#include "btstack_run_loop_embedded.h"
#endif
#endif
#ifdef ENABLE_BLE
#include "gap.h"
#include "ble/le_device_db.h"
#endif
#include <stdarg.h>
#include <string.h>
#include <inttypes.h>
#include "btstack_debug.h"
#include "btstack_event.h"
#include "btstack_linked_list.h"
#include "btstack_memory.h"
#include "bluetooth_company_id.h"
#include "bluetooth_data_types.h"
#include "gap.h"
#include "hci.h"
#include "hci_cmd.h"
#include "hci_dump.h"
#include "ad_parser.h"
#ifdef ENABLE_CONTROLLER_DUMP_PACKETS
#include <stdio.h> // sprintf
#endif
#ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL
#ifndef HCI_HOST_ACL_PACKET_NUM
#error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_ACL_PACKET_NUM"
#endif
#ifndef HCI_HOST_ACL_PACKET_LEN
#error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_ACL_PACKET_LEN"
#endif
#ifndef HCI_HOST_SCO_PACKET_NUM
#error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_SCO_PACKET_NUM"
#endif
#ifndef HCI_HOST_SCO_PACKET_LEN
#error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_SCO_PACKET_LEN"
#endif
#endif
#ifndef MAX_NR_CONTROLLER_ACL_BUFFERS
#define MAX_NR_CONTROLLER_ACL_BUFFERS 255
#endif
#ifndef MAX_NR_CONTROLLER_SCO_PACKETS
#define MAX_NR_CONTROLLER_SCO_PACKETS 255
#endif
#ifndef HCI_ACL_CHUNK_SIZE_ALIGNMENT
#define HCI_ACL_CHUNK_SIZE_ALIGNMENT 1
#endif
#if defined(ENABLE_SCO_OVER_HCI) && defined(ENABLE_SCO_OVER_PCM)
#error "SCO data can either be routed over HCI or over PCM, but not over both. Please only enable ENABLE_SCO_OVER_HCI or ENABLE_SCO_OVER_PCM."
#endif
#if defined(ENABLE_SCO_OVER_HCI) && defined(HAVE_SCO_TRANSPORT)
#error "SCO data can either be routed over HCI or over PCM, but not over both. Please only enable ENABLE_SCO_OVER_HCI or HAVE_SCO_TRANSPORT."
#endif
#define HCI_CONNECTION_TIMEOUT_MS 10000
#ifndef HCI_RESET_RESEND_TIMEOUT_MS
#define HCI_RESET_RESEND_TIMEOUT_MS 200
#endif
// Names are arbitrarily shortened to 32 bytes if not requested otherwise
#ifndef GAP_INQUIRY_MAX_NAME_LEN
#define GAP_INQUIRY_MAX_NAME_LEN 32
#endif
// GAP inquiry state: 0 = off, 0x01 - 0x30 = requested duration, 0xfe = active, 0xff = stop requested
#define GAP_INQUIRY_DURATION_MIN 0x01
#define GAP_INQUIRY_DURATION_MAX 0x30
#define GAP_INQUIRY_MIN_PERIODIC_LEN_MIN 0x02
#define GAP_INQUIRY_MAX_PERIODIC_LEN_MIN 0x03
#define GAP_INQUIRY_STATE_IDLE 0x00
#define GAP_INQUIRY_STATE_W4_ACTIVE 0x80
#define GAP_INQUIRY_STATE_ACTIVE 0x81
#define GAP_INQUIRY_STATE_W2_CANCEL 0x82
#define GAP_INQUIRY_STATE_W4_CANCELLED 0x83
#define GAP_INQUIRY_STATE_PERIODIC 0x84
#define GAP_INQUIRY_STATE_W2_EXIT_PERIODIC 0x85
// GAP Remote Name Request
#define GAP_REMOTE_NAME_STATE_IDLE 0
#define GAP_REMOTE_NAME_STATE_W2_SEND 1
#define GAP_REMOTE_NAME_STATE_W4_COMPLETE 2
// GAP Pairing
#define GAP_PAIRING_STATE_IDLE 0
#define GAP_PAIRING_STATE_SEND_PIN 1
#define GAP_PAIRING_STATE_SEND_PIN_NEGATIVE 2
#define GAP_PAIRING_STATE_SEND_PASSKEY 3
#define GAP_PAIRING_STATE_SEND_PASSKEY_NEGATIVE 4
#define GAP_PAIRING_STATE_SEND_CONFIRMATION 5
#define GAP_PAIRING_STATE_SEND_CONFIRMATION_NEGATIVE 6
#define GAP_PAIRING_STATE_WAIT_FOR_COMMAND_COMPLETE 7
//
// compact storage of relevant supported HCI Commands.
// X-Macro below provides enumeration and mapping table into the supported
// commands bitmap (64 bytes) from HCI Read Local Supported Commands
//
// format: command name, byte offset, bit nr in 64-byte supported commands
// currently stored in 32-bit variable
#define SUPPORTED_HCI_COMMANDS \
X( SUPPORTED_HCI_COMMAND_READ_REMOTE_EXTENDED_FEATURES , 2, 6) \
X( SUPPORTED_HCI_COMMAND_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE , 10, 4) \
X( SUPPORTED_HCI_COMMAND_READ_BUFFER_SIZE , 14, 7) \
X( SUPPORTED_HCI_COMMAND_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING, 18, 3) \
X( SUPPORTED_HCI_COMMAND_READ_ENCRYPTION_KEY_SIZE , 20, 4) \
X( SUPPORTED_HCI_COMMAND_SET_EVENT_MASK_PAGE_2 , 22, 2) \
X( SUPPORTED_HCI_COMMAND_WRITE_LE_HOST_SUPPORTED , 24, 6) \
X( SUPPORTED_HCI_COMMAND_LE_READ_REMOTE_FEATURES , 27, 5) \
X( SUPPORTED_HCI_COMMAND_REMOTE_OOB_EXTENDED_DATA_REQUEST_REPLY, 32, 1) \
X( SUPPORTED_HCI_COMMAND_WRITE_SECURE_CONNECTIONS_HOST , 32, 3) \
X( SUPPORTED_HCI_COMMAND_READ_LOCAL_OOB_EXTENDED_DATA_COMMAND , 32, 6) \
X( SUPPORTED_HCI_COMMAND_LE_WRITE_SUGGESTED_DEFAULT_DATA_LENGTH, 34, 0) \
X( SUPPORTED_HCI_COMMAND_LE_SET_ADDRESS_RESOLUTION_ENABLE , 35, 1) \
X( SUPPORTED_HCI_COMMAND_LE_READ_MAXIMUM_DATA_LENGTH , 35, 3) \
X( SUPPORTED_HCI_COMMAND_LE_SET_DEFAULT_PHY , 35, 5) \
X( SUPPORTED_HCI_COMMAND_LE_SET_EXTENDED_ADVERTISING_ENABLE , 36, 5) \
X( SUPPORTED_HCI_COMMAND_LE_READ_BUFFER_SIZE_V2 , 41, 5) \
X( SUPPORTED_HCI_COMMAND_LE_SET_HOST_FEATURE_V1 , 44, 1) \
X( SUPPORTED_HCI_COMMAND_SET_MIN_ENCRYPTION_KEY_SIZE , 45, 7) \
// enumerate supported commands
#define X(name, offset, bit) name,
enum {
SUPPORTED_HCI_COMMANDS
SUPPORTED_HCI_COMMANDS_COUNT
};
#undef X
// prototypes
#ifdef ENABLE_CLASSIC
static void hci_update_scan_enable(void);
static void hci_emit_scan_mode_changed(uint8_t discoverable, uint8_t connectable);
static int hci_local_ssp_activated(void);
static bool hci_remote_ssp_supported(hci_con_handle_t con_handle);
static bool hci_ssp_supported(hci_connection_t * connection);
static void hci_notify_if_sco_can_send_now(void);
static void hci_emit_connection_complete(bd_addr_t address, hci_con_handle_t con_handle, uint8_t status);
static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection);
static void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level, uint8_t status);
static void hci_connection_timeout_handler(btstack_timer_source_t *timer);
static void hci_connection_timestamp(hci_connection_t *connection);
static void hci_emit_l2cap_check_timeout(hci_connection_t *conn);
static void gap_inquiry_explode(uint8_t *packet, uint16_t size);
#endif
static int hci_power_control_on(void);
static void hci_power_control_off(void);
static void hci_state_reset(void);
static void hci_halting_timeout_handler(btstack_timer_source_t * ds);
static void hci_emit_transport_packet_sent(void);
static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason);
static void hci_emit_nr_connections_changed(void);
static void hci_emit_hci_open_failed(void);
static void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status);
static void hci_emit_event(uint8_t * event, uint16_t size, int dump);
static void hci_emit_acl_packet(uint8_t * packet, uint16_t size);
static void hci_run(void);
static bool hci_is_le_connection(hci_connection_t * connection);
static uint8_t hci_send_prepared_cmd_packet(void);
#ifdef ENABLE_CLASSIC
static int hci_have_usb_transport(void);
static void hci_trigger_remote_features_for_connection(hci_connection_t * connection);
#endif
#ifdef ENABLE_BLE
static bool hci_run_general_gap_le(void);
static void gap_privacy_clients_handle_ready(void);
static void gap_privacy_clients_notify(bd_addr_t new_random_address);
#ifdef ENABLE_LE_CENTRAL
static uint8_t hci_whitelist_remove(bd_addr_type_t address_type, const bd_addr_t address);
static hci_connection_t * gap_get_outgoing_le_connection(void);
static void hci_le_scan_stop(void);
#ifdef ENABLE_LE_EXTENDED_ADVERTISING
static void le_handle_extended_advertisement_report(uint8_t *packet, uint16_t size);
#endif
#endif
#ifdef ENABLE_LE_PERIPHERAL
#ifdef ENABLE_LE_EXTENDED_ADVERTISING
static le_advertising_set_t * hci_advertising_set_for_handle(uint8_t advertising_handle);
static uint8_t hci_le_extended_advertising_operation_for_chunk(uint16_t pos, uint16_t len);
#endif /* ENABLE_LE_EXTENDED_ADVERTISING */
#endif /* ENABLE_LE_PERIPHERAL */
#ifdef ENABLE_LE_ISOCHRONOUS_STREAMS
static hci_iso_stream_t * hci_iso_stream_create(hci_iso_type_t iso_type, hci_role_t role, hci_iso_stream_state_t state, uint8_t group_id, uint8_t stream_id);
static void hci_iso_stream_finalize(hci_iso_stream_t * iso_stream);
static void hci_iso_stream_finalize_by_type_and_group_id(hci_iso_type_t iso_type, uint8_t group_id);
static hci_iso_stream_t * hci_iso_stream_for_con_handle(hci_con_handle_t con_handle);
static void hci_iso_stream_requested_finalize(uint8_t big_handle);
static void hci_iso_stream_requested_confirm(uint8_t big_handle);
static void hci_iso_packet_handler(hci_iso_stream_t *iso_stream, uint8_t *packet, uint16_t size);
static le_audio_big_t * hci_big_for_handle(uint8_t big_handle);
static le_audio_cig_t * hci_cig_for_id(uint8_t cig_id);
static void hci_iso_notify_can_send_now(void);
static void hci_emit_big_created(const le_audio_big_t * big, uint8_t status);
static void hci_emit_big_terminated(const le_audio_big_t * big);
static void hci_emit_big_sync_created(const le_audio_big_sync_t * big_sync, uint8_t status);
static void hci_emit_big_sync_stopped(uint8_t big_handle);
static void hci_emit_cig_created(const le_audio_cig_t * cig, uint8_t status);
static void hci_cis_handle_created(hci_iso_stream_t * iso_stream, uint8_t status);
static le_audio_big_sync_t * hci_big_sync_for_handle(uint8_t big_handle);
#endif /* ENABLE_LE_ISOCHRONOUS_STREAMS */
#endif /* ENABLE_BLE */
// the STACK is here
#ifndef HAVE_MALLOC
static hci_stack_t hci_stack_static;
#endif
static hci_stack_t * hci_stack = NULL;
#ifdef ENABLE_CLASSIC
// default name
static const char * default_classic_name = "BTstack 00:00:00:00:00:00";
// test helper
static uint8_t disable_l2cap_timeouts = 0;
#endif
// reset connection state on create and on reconnect
// don't overwrite addr, con handle, role
static void hci_connection_init(hci_connection_t * conn){
conn->authentication_flags = AUTH_FLAG_NONE;
conn->bonding_flags = 0;
conn->requested_security_level = LEVEL_0;
conn->link_key_type = INVALID_LINK_KEY;
conn->encryption_key_type = INVALID_LINK_KEY;
#ifdef ENABLE_CLASSIC
conn->request_role = HCI_ROLE_INVALID;
conn->sniff_subrating_max_latency = 0xFFFFu;
conn->qos_service_type = HCI_SERVICE_TYPE_INVALID;
btstack_run_loop_set_timer_handler(&conn->timeout, hci_connection_timeout_handler);
btstack_run_loop_set_timer_context(&conn->timeout, conn);
hci_connection_timestamp(conn);
#endif
conn->acl_recombination_length = 0;
conn->acl_recombination_pos = 0;
conn->num_packets_sent = 0;
conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
#ifdef ENABLE_BLE
conn->le_phy_update_all_phys = 0xff;
#endif
#ifdef ENABLE_LE_LIMIT_ACL_FRAGMENT_BY_MAX_OCTETS
conn->le_max_tx_octets = 27;
#endif
#ifdef ENABLE_CLASSIC_PAIRING_OOB
conn->classic_oob_c_192 = NULL;
conn->classic_oob_r_192 = NULL;
conn->classic_oob_c_256 = NULL;
conn->classic_oob_r_256 = NULL;
#endif
#ifdef ENABLE_LE_PERIODIC_ADVERTISING
conn->le_past_sync_handle = HCI_CON_HANDLE_INVALID;
conn->le_past_advertising_handle = 0xff;
#endif
}
/**
* create connection for given address
*
* @return connection OR NULL, if no memory left
*/
static hci_connection_t *
create_connection_for_bd_addr_and_type(const bd_addr_t addr, bd_addr_type_t addr_type, hci_role_t role) {
log_info("create_connection_for_addr %s, type %x", bd_addr_to_str(addr), addr_type);
hci_connection_t * conn = btstack_memory_hci_connection_get();
if (!conn) return NULL;
hci_connection_init(conn);
bd_addr_copy(conn->address, addr);
conn->address_type = addr_type;
conn->con_handle = HCI_CON_HANDLE_INVALID;
conn->role = role;
btstack_linked_list_add(&hci_stack->connections, (btstack_linked_item_t *) conn);
return conn;
}
/**
* get le connection parameter range
*
* @return le connection parameter range struct
*/
void gap_get_connection_parameter_range(le_connection_parameter_range_t * range){
*range = hci_stack->le_connection_parameter_range;
}
/**
* set le connection parameter range
*
*/
void gap_set_connection_parameter_range(le_connection_parameter_range_t *range){
hci_stack->le_connection_parameter_range = *range;
}
/**
* @brief Test if connection parameters are inside in existing rage
* @param conn_interval_min (unit: 1.25ms)
* @param conn_interval_max (unit: 1.25ms)
* @param conn_latency
* @param supervision_timeout (unit: 10ms)
* @return 1 if included
*/
int gap_connection_parameter_range_included(le_connection_parameter_range_t * existing_range, uint16_t le_conn_interval_min, uint16_t le_conn_interval_max, uint16_t le_conn_latency, uint16_t le_supervision_timeout){
if (le_conn_interval_min < existing_range->le_conn_interval_min) return 0;
if (le_conn_interval_max > existing_range->le_conn_interval_max) return 0;
if (le_conn_latency < existing_range->le_conn_latency_min) return 0;
if (le_conn_latency > existing_range->le_conn_latency_max) return 0;
if (le_supervision_timeout < existing_range->le_supervision_timeout_min) return 0;
if (le_supervision_timeout > existing_range->le_supervision_timeout_max) return 0;
return 1;
}
/**
* @brief Set max number of connections in LE Peripheral role (if Bluetooth Controller supports it)
* @note: default: 1
* @param max_peripheral_connections
*/
#ifdef ENABLE_LE_PERIPHERAL
void gap_set_max_number_peripheral_connections(int max_peripheral_connections){
hci_stack->le_max_number_peripheral_connections = max_peripheral_connections;
}
#endif
/**
* get hci connections iterator
*
* @return hci connections iterator
*/
void hci_connections_get_iterator(btstack_linked_list_iterator_t *it){
btstack_linked_list_iterator_init(it, &hci_stack->connections);
}
/**
* get connection for a given handle
*
* @return connection OR NULL, if not found
*/
hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &hci_stack->connections);
while (btstack_linked_list_iterator_has_next(&it)){
hci_connection_t * item = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
if ( item->con_handle == con_handle ) {
return item;
}
}
return NULL;
}
/**
* get connection for given address
*
* @return connection OR NULL, if not found
*/
hci_connection_t * hci_connection_for_bd_addr_and_type(const bd_addr_t addr, bd_addr_type_t addr_type){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &hci_stack->connections);
while (btstack_linked_list_iterator_has_next(&it)){
hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
if (connection->address_type != addr_type) continue;
if (memcmp(addr, connection->address, 6) != 0) continue;
return connection;
}
return NULL;
}
inline static void hci_connection_clear_authentication_flags(hci_connection_t * conn, hci_authentication_flags_t flags){
conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags);
log_info("Authentication flags clear 0x%04x => 0x%04x for %s", flags, conn->authentication_flags, bd_addr_to_str(conn->address));
}
inline static void hci_connection_set_authentication_flags(hci_connection_t * conn, hci_authentication_flags_t flags){
conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags);
log_info("Authentication flags set 0x%04x => 0x%04x for %s", flags, conn->authentication_flags, bd_addr_to_str(conn->address));
}
#ifdef ENABLE_CLASSIC
#ifdef ENABLE_SCO_OVER_HCI
static int hci_number_sco_connections(void){
int connections = 0;
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &hci_stack->connections);
while (btstack_linked_list_iterator_has_next(&it)){
hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
if (connection->address_type != BD_ADDR_TYPE_SCO) continue;
connections++;
}
return connections;
}
#endif
static void hci_connection_timeout_handler(btstack_timer_source_t *timer){
hci_connection_t * connection = (hci_connection_t *) btstack_run_loop_get_timer_context(timer);
#ifdef HAVE_EMBEDDED_TICK
if (btstack_run_loop_embedded_get_ticks() > connection->timestamp + btstack_run_loop_embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){
// connections might be timed out
hci_emit_l2cap_check_timeout(connection);
}
#else
if (btstack_run_loop_get_time_ms() > (connection->timestamp + HCI_CONNECTION_TIMEOUT_MS)){
// connections might be timed out
hci_emit_l2cap_check_timeout(connection);
}
#endif
}
static void hci_connection_timestamp(hci_connection_t *connection){
#ifdef HAVE_EMBEDDED_TICK
connection->timestamp = btstack_run_loop_embedded_get_ticks();
#else
connection->timestamp = btstack_run_loop_get_time_ms();
#endif
}
static bool hci_pairing_active(hci_connection_t * hci_connection){
return (hci_connection->authentication_flags & AUTH_FLAG_PAIRING_ACTIVE_MASK) != 0;
}
static void hci_pairing_started(hci_connection_t * hci_connection, bool ssp){
if (hci_pairing_active(hci_connection)) return;
if (ssp){
hci_connection_set_authentication_flags(hci_connection, AUTH_FLAG_SSP_PAIRING_ACTIVE);
} else {
hci_connection_set_authentication_flags(hci_connection, AUTH_FLAG_LEGACY_PAIRING_ACTIVE);
}
// if we are initiator, we have sent an HCI Authenticate Request
bool initiator = (hci_connection->bonding_flags & BONDING_SENT_AUTHENTICATE_REQUEST) != 0;
// if we are responder, use minimal service security level as required level
if (!initiator){
hci_connection->requested_security_level = (gap_security_level_t) btstack_max((uint32_t) hci_connection->requested_security_level, (uint32_t) hci_stack->gap_minimal_service_security_level);
}
log_info("pairing started, ssp %u, initiator %u, requested level %u", (int) ssp, (int) initiator, hci_connection->requested_security_level);
uint8_t event[12];
event[0] = GAP_EVENT_PAIRING_STARTED;
event[1] = 10;
little_endian_store_16(event, 2, (uint16_t) hci_connection->con_handle);
reverse_bd_addr(hci_connection->address, &event[4]);
event[10] = (uint8_t) ssp;
event[11] = (uint8_t) initiator;
hci_emit_btstack_event(event, sizeof(event), 1);
}
static void hci_dedicated_bonding_handle_complete(hci_connection_t* hci_connection, uint8_t status) {
// handle dedicated bonding done
if ((hci_connection->bonding_flags & BONDING_DEDICATED) != 0){
hci_connection->bonding_flags &= ~BONDING_DEDICATED;
hci_connection->bonding_status = status;
#ifdef ENABLE_EXPLICIT_DEDICATED_BONDING_DISCONNECT
if (status == ERROR_CODE_SUCCESS) {
// emit dedicated bonding complete, don't disconnect
hci_emit_dedicated_bonding_result(hci_connection->address, hci_connection->bonding_status);
} else
#endif
{
// request disconnect, event is emitted after disconnect
hci_connection->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE;
}
}
}
static void hci_pairing_complete(hci_connection_t * hci_connection, uint8_t status){
hci_connection->requested_security_level = LEVEL_0;
if (!hci_pairing_active(hci_connection)) return;
hci_connection_clear_authentication_flags(hci_connection, AUTH_FLAG_PAIRING_ACTIVE_MASK);
#ifdef ENABLE_CLASSIC_PAIRING_OOB
hci_connection->classic_oob_c_192 = NULL;
hci_connection->classic_oob_r_192 = NULL;
hci_connection->classic_oob_c_256 = NULL;
hci_connection->classic_oob_r_256 = NULL;
#endif
log_info("pairing complete, status %02x", status);
uint8_t event[11];
event[0] = GAP_EVENT_PAIRING_COMPLETE;
event[1] = 9;
little_endian_store_16(event, 2, (uint16_t) hci_connection->con_handle);
reverse_bd_addr(hci_connection->address, &event[4]);
event[10] = status;
hci_emit_btstack_event(event, sizeof(event), 1);
}
bool hci_authentication_active_for_handle(hci_con_handle_t handle){
hci_connection_t * conn = hci_connection_for_handle(handle);
if (!conn) return false;
return hci_pairing_active(conn);
}
void gap_drop_link_key_for_bd_addr(bd_addr_t addr){
log_info("gap_drop_link_key_for_bd_addr: %s", bd_addr_to_str(addr));
hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL);
if (conn) {
// reset link key stored in connection
conn->link_key_type = INVALID_LINK_KEY;
}
if (hci_stack->link_key_db != NULL) {
hci_stack->link_key_db->delete_link_key(addr);
}
}
void gap_store_link_key_for_bd_addr(bd_addr_t addr, link_key_t link_key, link_key_type_t type){
if (!hci_stack->link_key_db) return;
log_info("gap_store_link_key_for_bd_addr: %s, type %u", bd_addr_to_str(addr), type);
hci_stack->link_key_db->put_link_key(addr, link_key, type);
}
bool gap_get_link_key_for_bd_addr(bd_addr_t addr, link_key_t link_key, link_key_type_t * type){
if (!hci_stack->link_key_db) return false;
int result = hci_stack->link_key_db->get_link_key(addr, link_key, type) != 0;
log_info("link key for %s available %u, type %u", bd_addr_to_str(addr), result, (int) *type);
return result;
}
void gap_delete_all_link_keys(void){
bd_addr_t addr;
link_key_t link_key;
link_key_type_t type;
btstack_link_key_iterator_t it;
int ok = gap_link_key_iterator_init(&it);
if (!ok) {
log_error("could not initialize iterator");
return;
}
while (gap_link_key_iterator_get_next(&it, addr, link_key, &type)){
gap_drop_link_key_for_bd_addr(addr);
}
gap_link_key_iterator_done(&it);
}
int gap_link_key_iterator_init(btstack_link_key_iterator_t * it){
if (!hci_stack->link_key_db) return 0;
if (!hci_stack->link_key_db->iterator_init) return 0;
return hci_stack->link_key_db->iterator_init(it);
}
int gap_link_key_iterator_get_next(btstack_link_key_iterator_t * it, bd_addr_t bd_addr, link_key_t link_key, link_key_type_t * type){
if (!hci_stack->link_key_db) return 0;
return hci_stack->link_key_db->iterator_get_next(it, bd_addr, link_key, type);
}
void gap_link_key_iterator_done(btstack_link_key_iterator_t * it){
if (!hci_stack->link_key_db) return;
hci_stack->link_key_db->iterator_done(it);
}
#endif
bool hci_is_le_connection_type(bd_addr_type_t address_type){
switch (address_type){
case BD_ADDR_TYPE_LE_PUBLIC:
case BD_ADDR_TYPE_LE_RANDOM:
case BD_ADDR_TYPE_LE_PUBLIC_IDENTITY:
case BD_ADDR_TYPE_LE_RANDOM_IDENTITY:
return true;
default:
return false;
}
}
bool hci_is_le_identity_address_type(bd_addr_type_t address_type){
switch (address_type){
case BD_ADDR_TYPE_LE_PUBLIC_IDENTITY:
case BD_ADDR_TYPE_LE_RANDOM_IDENTITY:
return true;
default:
return false;
}
}
static bool hci_is_le_connection(hci_connection_t * connection){
return hci_is_le_connection_type(connection->address_type);
}
/**
* count connections
*/
static int nr_hci_connections(void){
int count = 0;
btstack_linked_item_t *it;
for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL ; it = it->next){
count++;
}
return count;
}
uint16_t hci_number_free_acl_slots_for_connection_type(bd_addr_type_t address_type){
int num_packets_sent_classic = 0;
int num_packets_sent_le = 0;
btstack_linked_item_t *it;
for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL; it = it->next){
hci_connection_t * connection = (hci_connection_t *) it;
if (hci_is_le_connection(connection)){
num_packets_sent_le += connection->num_packets_sent;
}
if (connection->address_type == BD_ADDR_TYPE_ACL){
num_packets_sent_classic += connection->num_packets_sent;
}
}
btstack_assert(hci_stack->acl_packets_total_num >= num_packets_sent_classic);
int free_slots_classic = hci_stack->acl_packets_total_num - num_packets_sent_classic;
int free_slots_le = 0;
if (hci_stack->le_acl_packets_total_num){
// if we have LE slots, they are used
btstack_assert( hci_stack->le_acl_packets_total_num >= num_packets_sent_le);
free_slots_le = hci_stack->le_acl_packets_total_num - num_packets_sent_le;
} else {
// otherwise, classic slots are used for LE, too
btstack_assert(free_slots_classic >= num_packets_sent_le);
free_slots_classic -= num_packets_sent_le;
}
switch (address_type){
case BD_ADDR_TYPE_UNKNOWN:
log_error("hci_number_free_acl_slots: unknown address type");
return 0;
case BD_ADDR_TYPE_ACL:
return (uint16_t) free_slots_classic;
default:
if (hci_stack->le_acl_packets_total_num > 0){
return (uint16_t) free_slots_le;
}
return (uint16_t) free_slots_classic;
}
}
int hci_number_free_acl_slots_for_handle(hci_con_handle_t con_handle){
// get connection type
hci_connection_t * connection = hci_connection_for_handle(con_handle);
if (!connection){
log_error("hci_number_free_acl_slots: handle 0x%04x not in connection list", con_handle);
return 0;
}
return hci_number_free_acl_slots_for_connection_type(connection->address_type);
}
#ifdef ENABLE_CLASSIC
static int hci_number_free_sco_slots(void){
unsigned int num_sco_packets_sent = 0;
btstack_linked_item_t *it;
if (hci_stack->synchronous_flow_control_enabled){
// explicit flow control
for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){
hci_connection_t * connection = (hci_connection_t *) it;
if (connection->address_type != BD_ADDR_TYPE_SCO) continue;
num_sco_packets_sent += connection->num_packets_sent;
}
if (num_sco_packets_sent > hci_stack->sco_packets_total_num){
log_info("hci_number_free_sco_slots:packets (%u) > total packets (%u)", num_sco_packets_sent, hci_stack->sco_packets_total_num);
return 0;
}
return hci_stack->sco_packets_total_num - num_sco_packets_sent;
} else {
// implicit flow control
int num_ready = 0;
for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){
hci_connection_t * connection = (hci_connection_t *) it;
if (connection->address_type != BD_ADDR_TYPE_SCO) continue;
if (connection->sco_tx_ready == 0) continue;
num_ready++;
}
return num_ready;
}
}
#endif
// only used to send HCI Host Number Completed Packets
static int hci_can_send_command_packet_transport(void){
if (hci_stack->hci_packet_buffer_reserved) return 0;
// check for async hci transport implementations
if (hci_stack->hci_transport->can_send_packet_now){
if (!hci_stack->hci_transport->can_send_packet_now(HCI_COMMAND_DATA_PACKET)){
return 0;
}
}
return 1;
}
// new functions replacing hci_can_send_packet_now[_using_packet_buffer]
bool hci_can_send_command_packet_now(void){
if (hci_can_send_command_packet_transport() == 0) return false;
return hci_stack->num_cmd_packets > 0u;
}
static int hci_transport_can_send_prepared_packet_now(uint8_t packet_type){
// check for async hci transport implementations
if (!hci_stack->hci_transport->can_send_packet_now) return true;
return hci_stack->hci_transport->can_send_packet_now(packet_type);
}
static bool hci_can_send_prepared_acl_packet_for_address_type(bd_addr_type_t address_type){
if (!hci_transport_can_send_prepared_packet_now(HCI_ACL_DATA_PACKET)) return false;
return hci_number_free_acl_slots_for_connection_type(address_type) > 0;
}
bool hci_can_send_acl_le_packet_now(void){
if (hci_stack->hci_packet_buffer_reserved) return false;
return hci_can_send_prepared_acl_packet_for_address_type(BD_ADDR_TYPE_LE_PUBLIC);
}
bool hci_can_send_prepared_acl_packet_now(hci_con_handle_t con_handle) {
if (!hci_transport_can_send_prepared_packet_now(HCI_ACL_DATA_PACKET)) return false;
return hci_number_free_acl_slots_for_handle(con_handle) > 0;
}
bool hci_can_send_acl_packet_now(hci_con_handle_t con_handle){
if (hci_stack->hci_packet_buffer_reserved) return false;
return hci_can_send_prepared_acl_packet_now(con_handle);
}
#ifdef ENABLE_CLASSIC
bool hci_can_send_acl_classic_packet_now(void){
if (hci_stack->hci_packet_buffer_reserved) return false;
return hci_can_send_prepared_acl_packet_for_address_type(BD_ADDR_TYPE_ACL);
}
static bool hci_controller_can_send_sco_for_connection(hci_connection_t * connection) {
if (hci_have_usb_transport()) {
return hci_stack->sco_can_send_now;
} else if (hci_stack->synchronous_flow_control_enabled) {
return hci_number_free_sco_slots() > 0;
} else {
return connection->sco_tx_ready;
}
}
// Old
bool hci_can_send_prepared_sco_packet_now(void){
if (!hci_transport_can_send_prepared_packet_now(HCI_SCO_DATA_PACKET)) return false;
if (hci_have_usb_transport()){
return hci_stack->sco_can_send_now;
} else {
return hci_number_free_sco_slots() > 0;
}
}
bool hci_can_send_sco_packet_now(void){
if (hci_stack->hci_packet_buffer_reserved) return false;
return hci_can_send_prepared_sco_packet_now();
}
void hci_request_sco_can_send_now_event(void){
hci_stack->sco_waiting_for_can_send_now = 1;
hci_notify_if_sco_can_send_now();
}
// New
bool hci_can_send_sco_packet_now_for_con_handle(hci_con_handle_t con_handle) {
if (hci_stack->hci_packet_buffer_reserved) return false;
if (!hci_transport_can_send_prepared_packet_now(HCI_SCO_DATA_PACKET)) return false;
hci_connection_t * connection = hci_connection_for_handle(con_handle);
if (connection == NULL) return false;
return connection->sco_tx_ready > 0;
}
void hci_request_sco_can_send_now_event_for_con_handle(hci_con_handle_t con_handle) {
hci_connection_t * connection = hci_connection_for_handle(con_handle);
if (connection != NULL) {
connection->sco_request_to_send = 1;
hci_notify_if_sco_can_send_now();
}
}
#endif
// used for internal checks in l2cap.c
bool hci_is_packet_buffer_reserved(void){
return hci_stack->hci_packet_buffer_reserved;
}
void hci_reserve_packet_buffer(void){
btstack_assert(hci_stack->hci_packet_buffer_reserved == false);
hci_stack->hci_packet_buffer_reserved = true;
}
void hci_release_packet_buffer(void){
btstack_assert(hci_stack->hci_packet_buffer_reserved);
hci_stack->hci_packet_buffer_reserved = false;
hci_emit_transport_packet_sent();
}
// assumption: synchronous implementations don't provide can_send_packet_now as they don't keep the buffer after the call
static int hci_transport_synchronous(void){
return hci_stack->hci_transport->can_send_packet_now == NULL;
}
// used for debugging
#ifdef ENABLE_CONTROLLER_DUMP_PACKETS
static void hci_controller_dump_packets(void){
// format: "{handle:04x}:{count:02d} "
char summaries[3][7 * 8 + 1];
uint16_t totals[3];
uint8_t index;
for (index = 0 ; index < 3 ; index++){
summaries[index][0] = 0;
totals[index] = 0;
}
btstack_linked_item_t *it;
for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL; it = it->next){
hci_connection_t * connection = (hci_connection_t *) it;
switch (connection->address_type){
case BD_ADDR_TYPE_ACL:
index = 0;
break;
case BD_ADDR_TYPE_SCO:
index = 2;
break;
default:
index = 1;
break;
}
totals[index] += connection->num_packets_sent;
char item_text[10];
sprintf(item_text, "%04x:%02d ", connection->con_handle,connection->num_packets_sent);
btstack_strcat(summaries[index], sizeof(summaries[0]), item_text);
}
for (index = 0 ; index < 3 ; index++){
if (summaries[index][0] == 0){
summaries[index][0] = '-';
summaries[index][1] = 0;
}
}
log_info("Controller ACL BR/EDR: %s total %u / LE: %s total %u / SCO: %s total %u", summaries[0], totals[0], summaries[1], totals[1], summaries[2], totals[2]);
}
#endif
static uint8_t hci_send_acl_packet_fragments(hci_connection_t *connection){
// log_info("hci_send_acl_packet_fragments %u/%u (con 0x%04x)", hci_stack->acl_fragmentation_pos, hci_stack->acl_fragmentation_total_size, connection->con_handle);
// max ACL data packet length depends on connection type (LE vs. Classic) and available buffers
uint16_t max_acl_data_packet_length = hci_stack->acl_data_packet_length;
if (hci_is_le_connection(connection) && (hci_stack->le_data_packets_length > 0u)){
max_acl_data_packet_length = hci_stack->le_data_packets_length;
}
#ifdef ENABLE_LE_LIMIT_ACL_FRAGMENT_BY_MAX_OCTETS
if (hci_is_le_connection(connection) && (connection->le_max_tx_octets < max_acl_data_packet_length)){
max_acl_data_packet_length = connection->le_max_tx_octets;
}
#endif
log_debug("hci_send_acl_packet_fragments entered");
uint8_t status = ERROR_CODE_SUCCESS;
// multiple packets could be sent on a synchronous HCI transport
while (true){
log_debug("hci_send_acl_packet_fragments loop entered");
// get current data
const uint16_t acl_header_pos = hci_stack->acl_fragmentation_pos - 4u;
int current_acl_data_packet_length = hci_stack->acl_fragmentation_total_size - hci_stack->acl_fragmentation_pos;
bool more_fragments = false;
// if ACL packet is larger than Bluetooth packet buffer, only send max_acl_data_packet_length
if (current_acl_data_packet_length > max_acl_data_packet_length){
more_fragments = true;
current_acl_data_packet_length = max_acl_data_packet_length & (~(HCI_ACL_CHUNK_SIZE_ALIGNMENT-1));
}
// copy handle_and_flags if not first fragment and update packet boundary flags to be 01 (continuing fragment)
if (acl_header_pos > 0u){
uint16_t handle_and_flags = little_endian_read_16(hci_stack->hci_packet_buffer, 0);
handle_and_flags = (handle_and_flags & 0xcfffu) | (1u << 12u);
little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos, handle_and_flags);
}
// update header len
little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos + 2u, current_acl_data_packet_length);
// count packet
connection->num_packets_sent++;
log_debug("hci_send_acl_packet_fragments loop before send (more fragments %d)", (int) more_fragments);
// update state for next fragment (if any) as "transport done" might be sent during send_packet already
if (more_fragments){
// update start of next fragment to send
hci_stack->acl_fragmentation_pos += current_acl_data_packet_length;
} else {
// done
hci_stack->acl_fragmentation_pos = 0;
hci_stack->acl_fragmentation_total_size = 0;
}
// send packet
uint8_t * packet = &hci_stack->hci_packet_buffer[acl_header_pos];
const int size = current_acl_data_packet_length + 4;
hci_dump_packet(HCI_ACL_DATA_PACKET, 0, packet, size);
hci_stack->acl_fragmentation_tx_active = 1;
int err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
if (err != 0){
// no error from HCI Transport expected
status = ERROR_CODE_HARDWARE_FAILURE;
break;
}
#ifdef ENABLE_CONTROLLER_DUMP_PACKETS
hci_controller_dump_packets();
#endif
log_debug("hci_send_acl_packet_fragments loop after send (more fragments %d)", (int) more_fragments);
// done yet?
if (!more_fragments) break;
// can send more?
if (!hci_can_send_prepared_acl_packet_now(connection->con_handle)) return status;
}
log_debug("hci_send_acl_packet_fragments loop over");
// release buffer now for synchronous transport
if (hci_transport_synchronous()){
hci_stack->acl_fragmentation_tx_active = 0;
hci_release_packet_buffer();
}
return status;
}
// pre: caller has reserved the packet buffer
uint8_t hci_send_acl_packet_buffer(int size){
btstack_assert(hci_stack->hci_packet_buffer_reserved);