xref: /btstack/src/hci.h (revision 27c32905a32c402d20f6b4c3689c0fba56ad9387)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 /*
39  *  hci.h
40  *
41  *  Created by Matthias Ringwald on 4/29/09.
42  *
43  */
44 
45 #ifndef __HCI_H
46 #define __HCI_H
47 
48 #include "btstack_config.h"
49 
50 #include "btstack_chipset.h"
51 #include "btstack_control.h"
52 #include "btstack_linked_list.h"
53 #include "btstack_util.h"
54 #include "classic/btstack_link_key_db.h"
55 #include "hci_cmd.h"
56 #include "gap.h"
57 #include "hci_transport.h"
58 
59 #include <stdint.h>
60 #include <stdlib.h>
61 #include <stdarg.h>
62 
63 #if defined __cplusplus
64 extern "C" {
65 #endif
66 
67 // packet buffer sizes
68 // HCI_ACL_PAYLOAD_SIZE is configurable and defined in config.h
69 // addition byte in even to terminate remote name request with '\0'
70 #define HCI_EVENT_BUFFER_SIZE      (HCI_EVENT_HEADER_SIZE + HCI_EVENT_PAYLOAD_SIZE + 1)
71 #define HCI_CMD_BUFFER_SIZE        (HCI_CMD_HEADER_SIZE   + HCI_CMD_PAYLOAD_SIZE)
72 #define HCI_ACL_BUFFER_SIZE        (HCI_ACL_HEADER_SIZE   + HCI_ACL_PAYLOAD_SIZE)
73 
74 // size of hci buffers, big enough for command, event, or acl packet without H4 packet type
75 // @note cmd buffer is bigger than event buffer
76 #ifdef HCI_PACKET_BUFFER_SIZE
77     #if HCI_PACKET_BUFFER_SIZE < HCI_ACL_BUFFER_SIZE
78         #error HCI_PACKET_BUFFER_SIZE must be equal or larger than HCI_ACL_BUFFER_SIZE
79     #endif
80     #if HCI_PACKET_BUFFER_SIZE < HCI_CMD_BUFFER_SIZE
81         #error HCI_PACKET_BUFFER_SIZE must be equal or larger than HCI_CMD_BUFFER_SIZE
82     #endif
83 #else
84     #if HCI_ACL_BUFFER_SIZE > HCI_CMD_BUFFER_SIZE
85         #define HCI_PACKET_BUFFER_SIZE HCI_ACL_BUFFER_SIZE
86     #else
87         #define HCI_PACKET_BUFFER_SIZE HCI_CMD_BUFFER_SIZE
88     #endif
89 #endif
90 
91 // additional pre-buffer space for packets to Bluetooth module, for now, used for HCI Transport H4 DMA
92 #define HCI_OUTGOING_PRE_BUFFER_SIZE 1
93 
94 // BNEP may uncompress the IP Header by 16 bytes
95 #ifndef HCI_INCOMING_PRE_BUFFER_SIZE
96 #define HCI_INCOMING_PRE_BUFFER_SIZE (16 - HCI_ACL_HEADER_SIZE - 4)
97 #endif
98 
99 //
100 #define IS_COMMAND(packet, command) (little_endian_read_16(packet,0) == command.opcode)
101 
102 // check if command complete event for given command
103 #define HCI_EVENT_IS_COMMAND_COMPLETE(event,cmd) ( event[0] == HCI_EVENT_COMMAND_COMPLETE && little_endian_read_16(event,3) == cmd.opcode)
104 #define HCI_EVENT_IS_COMMAND_STATUS(event,cmd) ( event[0] == HCI_EVENT_COMMAND_STATUS && little_endian_read_16(event,4) == cmd.opcode)
105 
106 // Code+Len=2, Pkts+Opcode=3; total=5
107 #define OFFSET_OF_DATA_IN_COMMAND_COMPLETE 5
108 
109 // ACL Packet
110 #define READ_ACL_CONNECTION_HANDLE( buffer ) ( little_endian_read_16(buffer,0) & 0x0fff)
111 #define READ_ACL_FLAGS( buffer )      ( buffer[1] >> 4 )
112 #define READ_ACL_LENGTH( buffer )     (little_endian_read_16(buffer, 2))
113 
114 // Sneak peak into L2CAP Packet
115 #define READ_L2CAP_LENGTH(buffer)     ( little_endian_read_16(buffer, 4))
116 #define READ_L2CAP_CHANNEL_ID(buffer) ( little_endian_read_16(buffer, 6))
117 
118 /**
119  * LE connection parameter update state
120  */
121 
122 typedef enum {
123     CON_PARAMETER_UPDATE_NONE,
124     CON_PARAMETER_UPDATE_SEND_REQUEST,
125     CON_PARAMETER_UPDATE_SEND_RESPONSE,
126     CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS,
127     CON_PARAMETER_UPDATE_DENY
128 } le_con_parameter_update_state_t;
129 
130 // Authentication flags
131 typedef enum {
132     AUTH_FLAGS_NONE                = 0x0000,
133     RECV_LINK_KEY_REQUEST          = 0x0001,
134     HANDLE_LINK_KEY_REQUEST        = 0x0002,
135     SENT_LINK_KEY_REPLY            = 0x0004,
136     SENT_LINK_KEY_NEGATIVE_REQUEST = 0x0008,
137     RECV_LINK_KEY_NOTIFICATION     = 0x0010,
138     DENY_PIN_CODE_REQUEST          = 0x0040,
139     RECV_IO_CAPABILITIES_REQUEST   = 0x0080,
140     SEND_IO_CAPABILITIES_REPLY     = 0x0100,
141     SEND_USER_CONFIRM_REPLY        = 0x0200,
142     SEND_USER_PASSKEY_REPLY        = 0x0400,
143 
144     // pairing status
145     LEGACY_PAIRING_ACTIVE          = 0x2000,
146     SSP_PAIRING_ACTIVE             = 0x4000,
147 
148     // connection status
149     CONNECTION_ENCRYPTED           = 0x8000,
150 } hci_authentication_flags_t;
151 
152 /**
153  * Connection State
154  */
155 typedef enum {
156     SEND_CREATE_CONNECTION = 0,
157     SENT_CREATE_CONNECTION,
158     SEND_CANCEL_CONNECTION,
159     SENT_CANCEL_CONNECTION,
160     RECEIVED_CONNECTION_REQUEST,
161     ACCEPTED_CONNECTION_REQUEST,
162     REJECTED_CONNECTION_REQUEST,
163     OPEN,
164     SEND_DISCONNECT,
165     SENT_DISCONNECT,
166     RECEIVED_DISCONNECTION_COMPLETE
167 } CONNECTION_STATE;
168 
169 // bonding flags
170 enum {
171     BONDING_REQUEST_REMOTE_FEATURES   = 0x01,
172     BONDING_RECEIVED_REMOTE_FEATURES  = 0x02,
173     BONDING_REMOTE_SUPPORTS_SSP       = 0x04,
174     BONDING_DISCONNECT_SECURITY_BLOCK = 0x08,
175     BONDING_DISCONNECT_DEDICATED_DONE = 0x10,
176     BONDING_SEND_AUTHENTICATE_REQUEST = 0x20,
177     BONDING_SEND_ENCRYPTION_REQUEST   = 0x40,
178     BONDING_DEDICATED                 = 0x80,
179     BONDING_EMIT_COMPLETE_ON_DISCONNECT = 0x100
180 };
181 
182 typedef enum {
183     BLUETOOTH_OFF = 1,
184     BLUETOOTH_ON,
185     BLUETOOTH_ACTIVE
186 } BLUETOOTH_STATE;
187 
188 // le central scanning state
189 typedef enum {
190     LE_SCAN_IDLE,
191     LE_START_SCAN,
192     LE_SCANNING,
193     LE_STOP_SCAN,
194 } le_scanning_state_t;
195 
196 typedef enum {
197     LE_CONNECTING_IDLE,
198     LE_CONNECTING_DIRECT,
199     LE_CONNECTING_WHITELIST,
200 } le_connecting_state_t;
201 
202 //
203 // SM internal types and globals
204 //
205 
206 typedef enum {
207 
208     // general states
209     // state = 0
210     SM_GENERAL_IDLE,
211     SM_GENERAL_SEND_PAIRING_FAILED,
212     SM_GENERAL_TIMEOUT, // no other security messages are exchanged
213 
214     // Phase 1: Pairing Feature Exchange
215     SM_PH1_W4_USER_RESPONSE,
216 
217     // Phase 2: Authenticating and Encrypting
218 
219     // get random number for use as TK Passkey if we show it
220     SM_PH2_GET_RANDOM_TK,
221     SM_PH2_W4_RANDOM_TK,
222 
223     // get local random number for confirm c1
224     SM_PH2_C1_GET_RANDOM_A,
225     SM_PH2_C1_W4_RANDOM_A,
226     SM_PH2_C1_GET_RANDOM_B,
227     SM_PH2_C1_W4_RANDOM_B,
228 
229     // calculate confirm value for local side
230     // state = 10
231     SM_PH2_C1_GET_ENC_A,
232     SM_PH2_C1_W4_ENC_A,
233     SM_PH2_C1_GET_ENC_B,
234     SM_PH2_C1_W4_ENC_B,
235 
236     // calculate confirm value for remote side
237     SM_PH2_C1_GET_ENC_C,
238     SM_PH2_C1_W4_ENC_C,
239     SM_PH2_C1_GET_ENC_D,
240     SM_PH2_C1_W4_ENC_D,
241 
242     SM_PH2_C1_SEND_PAIRING_CONFIRM,
243     SM_PH2_SEND_PAIRING_RANDOM,
244 
245     // calc STK
246     // state = 20
247     SM_PH2_CALC_STK,
248     SM_PH2_W4_STK,
249 
250     SM_PH2_W4_CONNECTION_ENCRYPTED,
251 
252     //
253     SM_PH2_SEND_PUBLIC_KEY_COMMAND,
254 
255     // Phase 3: Transport Specific Key Distribution
256     // calculate DHK, Y, EDIV, and LTK
257     SM_PH3_GET_RANDOM,
258     SM_PH3_W4_RANDOM,
259     SM_PH3_GET_DIV,
260     SM_PH3_W4_DIV,
261     SM_PH3_Y_GET_ENC,
262     SM_PH3_Y_W4_ENC,
263     SM_PH3_LTK_GET_ENC,
264     // state = 30
265     SM_PH3_LTK_W4_ENC,
266     SM_PH3_CSRK_GET_ENC,
267     SM_PH3_CSRK_W4_ENC,
268 
269     // exchange keys
270     SM_PH3_DISTRIBUTE_KEYS,
271     SM_PH3_RECEIVE_KEYS,
272 
273     // RESPONDER ROLE
274     // state = 35
275     SM_RESPONDER_IDLE,
276     SM_RESPONDER_SEND_SECURITY_REQUEST,
277     SM_RESPONDER_PH0_RECEIVED_LTK,
278     SM_RESPONDER_PH0_SEND_LTK_REQUESTED_NEGATIVE_REPLY,
279     SM_RESPONDER_PH1_W4_PAIRING_REQUEST,
280     SM_RESPONDER_PH1_PAIRING_REQUEST_RECEIVED,
281     SM_RESPONDER_PH1_SEND_PAIRING_RESPONSE,
282     SM_RESPONDER_PH1_W4_PAIRING_CONFIRM,
283     SM_RESPONDER_PH2_W4_PAIRING_RANDOM,
284     SM_RESPONDER_PH2_W4_LTK_REQUEST,
285     SM_RESPONDER_PH2_SEND_LTK_REPLY,
286     SM_RESPONDER_PH2_W4_PUBLIC_KEY_COMMAND,
287 
288     // Phase 4: re-establish previously distributed LTK
289     // state == 46
290     SM_RESPONDER_PH4_Y_GET_ENC,
291     SM_RESPONDER_PH4_Y_W4_ENC,
292     SM_RESPONDER_PH4_LTK_GET_ENC,
293     SM_RESPONDER_PH4_LTK_W4_ENC,
294     SM_RESPONDER_PH4_SEND_LTK,
295 
296     // INITITIATOR ROLE
297     // state = 51
298     SM_INITIATOR_CONNECTED,
299     SM_INITIATOR_PH0_HAS_LTK,
300     SM_INITIATOR_PH0_SEND_START_ENCRYPTION,
301     SM_INITIATOR_PH0_W4_CONNECTION_ENCRYPTED,
302     SM_INITIATOR_PH1_W2_SEND_PAIRING_REQUEST,
303     SM_INITIATOR_PH1_SEND_PAIRING_REQUEST,
304     SM_INITIATOR_PH1_W4_PAIRING_RESPONSE,
305     SM_INITIATOR_PH2_W4_PAIRING_CONFIRM,
306     SM_INITIATOR_PH2_W4_PAIRING_RANDOM,
307     SM_INITIATOR_PH3_SEND_START_ENCRYPTION,
308     //
309 } security_manager_state_t;
310 
311 typedef enum {
312     IRK_LOOKUP_IDLE,
313     IRK_LOOKUP_W4_READY,
314     IRK_LOOKUP_STARTED,
315     IRK_LOOKUP_SUCCEEDED,
316     IRK_LOOKUP_FAILED
317 } irk_lookup_state_t;
318 
319 // Authorization state
320 typedef enum {
321     AUTHORIZATION_UNKNOWN,
322     AUTHORIZATION_PENDING,
323     AUTHORIZATION_DECLINED,
324     AUTHORIZATION_GRANTED
325 } authorization_state_t;
326 
327 typedef uint8_t sm_pairing_packet_t[7];
328 
329 // connection info available as long as connection exists
330 typedef struct sm_connection {
331     hci_con_handle_t         sm_handle;
332     uint8_t                  sm_role;   // 0 - IamMaster, 1 = IamSlave
333     uint8_t                  sm_security_request_received;
334     uint8_t                  sm_bonding_requested;
335     uint8_t                  sm_peer_addr_type;
336     bd_addr_t                sm_peer_address;
337     security_manager_state_t sm_engine_state;
338     irk_lookup_state_t      sm_irk_lookup_state;
339     uint8_t                  sm_connection_encrypted;
340     uint8_t                  sm_connection_authenticated;   // [0..1]
341     uint8_t                  sm_actual_encryption_key_size;
342     sm_pairing_packet_t      sm_m_preq;  // only used during c1
343     authorization_state_t    sm_connection_authorization_state;
344     uint16_t                 sm_local_ediv;
345     uint8_t                  sm_local_rand[8];
346     int                      sm_le_db_index;
347 } sm_connection_t;
348 
349 typedef struct {
350     // linked list - assert: first field
351     btstack_linked_item_t    item;
352 
353     // remote side
354     bd_addr_t address;
355 
356     // module handle
357     hci_con_handle_t con_handle;
358 
359     // le public, le random, classic
360     bd_addr_type_t address_type;
361 
362     // role: 0 - master, 1 - slave
363     uint8_t role;
364 
365     // connection state
366     CONNECTION_STATE state;
367 
368     // bonding
369     uint16_t bonding_flags;
370     uint8_t  bonding_status;
371     // requested security level
372     gap_security_level_t requested_security_level;
373 
374     //
375     link_key_type_t link_key_type;
376 
377     // remote supported features
378     uint8_t remote_supported_feature_eSCO;
379 
380     // errands
381     uint32_t authentication_flags;
382 
383     btstack_timer_source_t timeout;
384 
385     // timeout in system ticks (HAVE_EMBEDDED_TICK) or milliseconds (HAVE_EMBEDDED_TIME_MS)
386     uint32_t timestamp;
387 
388     // ACL packet recombination - PRE_BUFFER + ACL Header + ACL payload
389     uint8_t  acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 4 + HCI_ACL_BUFFER_SIZE];
390     uint16_t acl_recombination_pos;
391     uint16_t acl_recombination_length;
392 
393     // number packets sent to controller
394     uint8_t num_acl_packets_sent;
395     uint8_t num_sco_packets_sent;
396 
397     // LE Connection parameter update
398     le_con_parameter_update_state_t le_con_parameter_update_state;
399     uint8_t  le_con_param_update_identifier;
400     uint16_t le_conn_interval_min;
401     uint16_t le_conn_interval_max;
402     uint16_t le_conn_latency;
403     uint16_t le_supervision_timeout;
404 
405 #ifdef ENABLE_BLE
406     // LE Security Manager
407     sm_connection_t sm_connection;
408 #endif
409 
410 } hci_connection_t;
411 
412 
413 /**
414  * HCI Inititizlization State Machine
415  */
416 typedef enum hci_init_state{
417     HCI_INIT_SEND_RESET = 0,
418     HCI_INIT_W4_SEND_RESET,
419     HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION,
420     HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION,
421 
422     HCI_INIT_SEND_BAUD_CHANGE,
423     HCI_INIT_W4_SEND_BAUD_CHANGE,
424     HCI_INIT_CUSTOM_INIT,
425     HCI_INIT_W4_CUSTOM_INIT,
426     HCI_INIT_SEND_RESET_CSR_WARM_BOOT,
427     HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT,
428     HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET,
429 
430     HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS,
431     HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS,
432 
433     HCI_INIT_SEND_BAUD_CHANGE_BCM,
434     HCI_INIT_W4_SEND_BAUD_CHANGE_BCM,
435 
436     HCI_INIT_SET_BD_ADDR,
437     HCI_INIT_W4_SET_BD_ADDR,
438 
439     HCI_INIT_SEND_RESET_ST_WARM_BOOT,
440     HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT,
441 
442     HCI_INIT_READ_BD_ADDR,
443     HCI_INIT_W4_READ_BD_ADDR,
444 
445     HCI_INIT_READ_BUFFER_SIZE,
446     HCI_INIT_W4_READ_BUFFER_SIZE,
447     HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES,
448     HCI_INIT_W4_READ_LOCAL_SUPPORTED_FEATURES,
449     HCI_INIT_SET_EVENT_MASK,
450     HCI_INIT_W4_SET_EVENT_MASK,
451     HCI_INIT_WRITE_SIMPLE_PAIRING_MODE,
452     HCI_INIT_W4_WRITE_SIMPLE_PAIRING_MODE,
453     HCI_INIT_WRITE_PAGE_TIMEOUT,
454     HCI_INIT_W4_WRITE_PAGE_TIMEOUT,
455     HCI_INIT_WRITE_CLASS_OF_DEVICE,
456     HCI_INIT_W4_WRITE_CLASS_OF_DEVICE,
457     HCI_INIT_WRITE_LOCAL_NAME,
458     HCI_INIT_W4_WRITE_LOCAL_NAME,
459     HCI_INIT_WRITE_SCAN_ENABLE,
460     HCI_INIT_W4_WRITE_SCAN_ENABLE,
461 
462     HCI_INIT_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE,
463     HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE,
464     HCI_INIT_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING,
465     HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING,
466 
467     HCI_INIT_LE_READ_BUFFER_SIZE,
468     HCI_INIT_W4_LE_READ_BUFFER_SIZE,
469     HCI_INIT_WRITE_LE_HOST_SUPPORTED,
470     HCI_INIT_W4_WRITE_LE_HOST_SUPPORTED,
471     HCI_INIT_READ_WHITE_LIST_SIZE,
472     HCI_INIT_W4_READ_WHITE_LIST_SIZE,
473 
474     HCI_INIT_LE_SET_SCAN_PARAMETERS,
475     HCI_INIT_W4_LE_SET_SCAN_PARAMETERS,
476 
477     HCI_INIT_DONE,
478 
479     HCI_FALLING_ASLEEP_DISCONNECT,
480     HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE,
481     HCI_FALLING_ASLEEP_COMPLETE,
482 
483     HCI_INIT_AFTER_SLEEP
484 
485 } hci_substate_t;
486 
487 enum {
488     LE_ADVERTISEMENT_TASKS_DISABLE       = 1 << 0,
489     LE_ADVERTISEMENT_TASKS_SET_ADV_DATA  = 1 << 1,
490     LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA = 1 << 2,
491     LE_ADVERTISEMENT_TASKS_SET_PARAMS    = 1 << 3,
492     LE_ADVERTISEMENT_TASKS_ENABLE        = 1 << 4,
493 };
494 
495 enum {
496     LE_WHITELIST_ON_CONTROLLER          = 1 << 0,
497     LE_WHITELIST_ADD_TO_CONTROLLER      = 1 << 1,
498     LE_WHITELIST_REMOVE_FROM_CONTROLLER = 1 << 2,
499 };
500 
501 typedef struct {
502     btstack_linked_item_t  item;
503     bd_addr_t      address;
504     bd_addr_type_t address_type;
505     uint8_t        state;
506 } whitelist_entry_t;
507 
508 /**
509  * main data structure
510  */
511 typedef struct {
512     // transport component with configuration
513     const hci_transport_t * hci_transport;
514     const void            * config;
515 
516     // chipset driver
517     const btstack_chipset_t * chipset;
518 
519     // hardware power controller
520     const btstack_control_t * control;
521 
522     /* link key db */
523     const btstack_link_key_db_t * link_key_db;
524 
525     // list of existing baseband connections
526     btstack_linked_list_t     connections;
527 
528     /* callback to L2CAP layer */
529     btstack_packet_handler_t acl_packet_handler;
530 
531     /* callback for SCO data */
532     btstack_packet_handler_t sco_packet_handler;
533 
534     /* callbacks for events */
535     btstack_linked_list_t event_handlers;
536 
537     // local version information callback
538     void (*local_version_information_callback)(uint8_t * local_version_information);
539 
540     // hardware error callback
541     void (*hardware_error_callback)(void);
542 
543     // basic configuration
544     const char *       local_name;
545     uint32_t           class_of_device;
546     bd_addr_t          local_bd_addr;
547     uint8_t            ssp_enable;
548     uint8_t            ssp_io_capability;
549     uint8_t            ssp_authentication_requirement;
550     uint8_t            ssp_auto_accept;
551 
552     // single buffer for HCI packet assembly + additional prebuffer for H4 drivers
553     uint8_t   hci_packet_buffer_prefix[HCI_OUTGOING_PRE_BUFFER_SIZE];
554     uint8_t   hci_packet_buffer[HCI_PACKET_BUFFER_SIZE]; // opcode (16), len(8)
555     uint8_t   hci_packet_buffer_reserved;
556     uint16_t  acl_fragmentation_pos;
557     uint16_t  acl_fragmentation_total_size;
558 
559     /* host to controller flow control */
560     uint8_t  num_cmd_packets;
561     uint8_t  acl_packets_total_num;
562     uint16_t acl_data_packet_length;
563     uint8_t  sco_packets_total_num;
564     uint8_t  sco_data_packet_length;
565     uint8_t  synchronous_flow_control_enabled;
566     uint8_t  le_acl_packets_total_num;
567     uint16_t le_data_packets_length;
568     uint8_t  sco_waiting_for_can_send_now;
569 
570     /* local supported features */
571     uint8_t local_supported_features[8];
572 
573     /* local supported commands summary - complete info is 64 bytes */
574     /* 0 - read buffer size */
575     /* 1 - write le host supported */
576     uint8_t local_supported_commands[1];
577 
578     /* bluetooth device information from hci read local version information */
579     // uint16_t hci_version;
580     // uint16_t hci_revision;
581     // uint16_t lmp_version;
582     uint16_t manufacturer;
583     // uint16_t lmp_subversion;
584 
585     // usable packet types given acl_data_packet_length and HCI_ACL_BUFFER_SIZE
586     uint16_t packet_types;
587 
588 
589     /* hci state machine */
590     HCI_STATE      state;
591     hci_substate_t substate;
592     btstack_timer_source_t timeout;
593     uint8_t   cmds_ready;
594 
595     uint16_t  last_cmd_opcode;
596 
597     uint8_t   discoverable;
598     uint8_t   connectable;
599     uint8_t   bondable;
600 
601     /* buffer for scan enable cmd - 0xff no change */
602     uint8_t   new_scan_enable_value;
603 
604     uint16_t   sco_voice_setting;
605 
606     uint8_t   loopback_mode;
607 
608     // buffer for single connection decline
609     uint8_t   decline_reason;
610     bd_addr_t decline_addr;
611 
612     uint8_t   adv_addr_type;
613     bd_addr_t adv_address;
614 
615     le_scanning_state_t   le_scanning_state;
616     le_connecting_state_t le_connecting_state;
617 
618     // buffer for le scan type command - 0xff not set
619     uint8_t  le_scan_type;
620     uint16_t le_scan_interval;
621     uint16_t le_scan_window;
622 
623     le_connection_parameter_range_t le_connection_parameter_range;
624 
625     uint8_t  * le_advertisements_data;
626     uint8_t    le_advertisements_data_len;
627 
628     uint8_t  * le_scan_response_data;
629     uint8_t    le_scan_response_data_len;
630 
631     uint8_t  le_advertisements_active;
632     uint8_t  le_advertisements_enabled;
633     uint8_t  le_advertisements_todo;
634 
635     uint16_t le_advertisements_interval_min;
636     uint16_t le_advertisements_interval_max;
637     uint8_t  le_advertisements_type;
638     uint8_t  le_advertisements_own_address_type;
639     uint8_t  le_advertisements_direct_address_type;
640     uint8_t  le_advertisements_channel_map;
641     uint8_t  le_advertisements_filter_policy;
642     bd_addr_t le_advertisements_direct_address;
643 
644     // LE Whitelist Management
645     uint16_t      le_whitelist_capacity;
646     btstack_linked_list_t le_whitelist;
647 
648     // custom BD ADDR
649     bd_addr_t custom_bd_addr;
650     uint8_t   custom_bd_addr_set;
651 
652 } hci_stack_t;
653 
654 
655 /* API_START */
656 
657 
658 // HCI init and configuration
659 
660 
661 /**
662  * @brief Set up HCI. Needs to be called before any other function.
663  */
664 void hci_init(const hci_transport_t *transport, const void *config);
665 
666 /**
667  * @brief Configure Bluetooth chipset driver. Has to be called before power on, or right after receiving the local version information.
668  */
669 void hci_set_chipset(const btstack_chipset_t *chipset_driver);
670 
671 /**
672  * @brief Configure Bluetooth hardware control. Has to be called before power on.
673  */
674 void hci_set_control(const btstack_control_t *hardware_control);
675 
676 /**
677  * @brief Configure Bluetooth hardware control. Has to be called before power on.
678  */
679 void hci_set_link_key_db(btstack_link_key_db_t const * link_key_db);
680 
681 /**
682  * @brief Set callback for Bluetooth Hardware Error
683  */
684 void hci_set_hardware_error_callback(void (*fn)(void));
685 
686 /**
687  * @brief Set callback for local information from Bluetooth controller right after HCI Reset
688  * @note Can be used to select chipset driver dynamically during startup
689  */
690 void hci_set_local_version_information_callback(void (*fn)(uint8_t * local_version_information));
691 
692 /**
693  * @brief Set Public BD ADDR - passed on to Bluetooth chipset during init if supported in bt_control_h
694  */
695 void hci_set_bd_addr(bd_addr_t addr);
696 
697 /**
698  * @brief Configure Voice Setting for use with SCO data in HSP/HFP
699  */
700 void hci_set_sco_voice_setting(uint16_t voice_setting);
701 
702 /**
703  * @brief Get SCO Voice Setting
704  * @return current voice setting
705  */
706 uint16_t hci_get_sco_voice_setting(void);
707 
708 /**
709  * @brief Requests the change of BTstack power mode.
710  */
711 int  hci_power_control(HCI_POWER_MODE mode);
712 
713 /**
714  * @brief Shutdown HCI
715  */
716 void hci_close(void);
717 
718 
719 // Callback registration
720 
721 
722 /**
723  * @brief Add event packet handler.
724  */
725 void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler);
726 
727 /**
728  * @brief Registers a packet handler for ACL data. Used by L2CAP
729  */
730 void hci_register_acl_packet_handler(btstack_packet_handler_t handler);
731 
732 /**
733  * @brief Registers a packet handler for SCO data. Used for HSP and HFP profiles.
734  */
735 void hci_register_sco_packet_handler(btstack_packet_handler_t handler);
736 
737 
738 // Sending HCI Commands
739 
740 /**
741  * @brief Check if CMD packet can be sent to controller
742  */
743 int hci_can_send_command_packet_now(void);
744 
745 /**
746  * @brief Creates and sends HCI command packets based on a template and a list of parameters. Will return error if outgoing data buffer is occupied.
747  */
748 int hci_send_cmd(const hci_cmd_t *cmd, ...);
749 
750 
751 // Sending SCO Packets
752 
753 /** @brief Get SCO packet length for current SCO Voice setting
754  *  @note  Using SCO packets of the exact length is required for USB transfer
755  *  @return Length of SCO packets in bytes (not audio frames) incl. 3 byte header
756  */
757 int hci_get_sco_packet_length(void);
758 
759 /**
760  * @brief Request emission of HCI_EVENT_SCO_CAN_SEND_NOW as soon as possible
761  * @note HCI_EVENT_SCO_CAN_SEND_NOW might be emitted during call to this function
762  *       so packet handler should be ready to handle it
763  */
764 void hci_request_sco_can_send_now_event(void);
765 
766 /**
767  * @brief Check HCI packet buffer and if SCO packet can be sent to controller
768  */
769 int hci_can_send_sco_packet_now(void);
770 
771 /**
772  * @brief Check if SCO packet can be sent to controller
773  */
774 int hci_can_send_prepared_sco_packet_now(void);
775 
776 /**
777  * @brief Send SCO packet prepared in HCI packet buffer
778  */
779 int hci_send_sco_packet_buffer(int size);
780 
781 
782 // Outgoing packet buffer, also used for SCO packets
783 // see hci_can_send_prepared_sco_packet_now amn hci_send_sco_packet_buffer
784 
785 /**
786  * Reserves outgoing packet buffer.
787  * @return 1 on success
788  */
789 int hci_reserve_packet_buffer(void);
790 
791 /**
792  * Get pointer for outgoing packet buffer
793  */
794 uint8_t* hci_get_outgoing_packet_buffer(void);
795 
796 /**
797  * Release outgoing packet buffer\
798  * @note only called instead of hci_send_preparared
799  */
800 void hci_release_packet_buffer(void);
801 
802 
803 /* API_END */
804 
805 
806 
807 /**
808  * Get connection iterator. Only used by l2cap.c and sm.c
809  */
810 void hci_connections_get_iterator(btstack_linked_list_iterator_t *it);
811 
812 /**
813  * Get internal hci_connection_t for given handle. Used by L2CAP, SM, daemon
814  */
815 hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle);
816 
817 /**
818  * Get internal hci_connection_t for given Bluetooth addres. Called by L2CAP
819  */
820 hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type);
821 
822 /**
823  * Check if outgoing packet buffer is reserved. Used for internal checks in l2cap.c
824  */
825 int hci_is_packet_buffer_reserved(void);
826 
827 /**
828  * Check hci packet buffer is free and a classic acl packet can be sent to controller
829  */
830 int hci_can_send_acl_classic_packet_now(void);
831 
832 /**
833  * Check hci packet buffer is free and an LE acl packet can be sent to controller
834  */
835 int hci_can_send_acl_le_packet_now(void);
836 
837 /**
838  * Check hci packet buffer is free and an acl packet for the given handle can be sent to controller
839  */
840 int hci_can_send_acl_packet_now(hci_con_handle_t con_handle);
841 
842 /**
843  * Check if acl packet for the given handle can be sent to controller
844  */
845 int hci_can_send_prepared_acl_packet_now(hci_con_handle_t con_handle);
846 
847 /**
848  * Send acl packet prepared in hci packet buffer
849  */
850 int hci_send_acl_packet_buffer(int size);
851 
852 /**
853  * Check if authentication is active. It delays automatic disconnect while no L2CAP connection
854  * Called by l2cap.
855  */
856 int hci_authentication_active_for_handle(hci_con_handle_t handle);
857 
858 /**
859  * Get maximal ACL Classic data packet length based on used buffer size. Called by L2CAP
860  */
861 uint16_t hci_max_acl_data_packet_length(void);
862 
863 /**
864  * Get supported packet types. Called by L2CAP
865  */
866 uint16_t hci_usable_acl_packet_types(void);
867 
868 /**
869  * Check if ACL packets marked as non flushable can be sent. Called by L2CAP
870  */
871 int hci_non_flushable_packet_boundary_flag_supported(void);
872 
873 /**
874  * Check if SSP is supported on both sides. Called by L2CAP
875  */
876 int gap_ssp_supported_on_both_sides(hci_con_handle_t handle);
877 
878 /**
879  * Disconn because of security block. Called by L2CAP
880  */
881 void hci_disconnect_security_block(hci_con_handle_t con_handle);
882 
883 /**
884  * Query if remote side supports eSCO
885  */
886 int hci_remote_esco_supported(hci_con_handle_t con_handle);
887 
888 /**
889  * Emit current HCI state. Called by daemon
890  */
891 void hci_emit_state(void);
892 
893 /**
894  * Send complete CMD packet. Called by daemon
895  */
896 int hci_send_cmd_packet(uint8_t *packet, int size);
897 
898 /**
899  * Disconnect all HCI connections. Called by daemon
900  */
901 void hci_disconnect_all(void);
902 
903 /**
904  * Get number of free acl slots for packets of given handle. Called by daemon
905  */
906 int hci_number_free_acl_slots_for_handle(hci_con_handle_t con_handle);
907 
908 /**
909  * @brief Set Advertisement Parameters
910  * @param adv_int_min
911  * @param adv_int_max
912  * @param adv_type
913  * @param own_address_type
914  * @param direct_address_type
915  * @param direct_address
916  * @param channel_map
917  * @param filter_policy
918  *
919  * @note internal use. use gap_advertisements_set_params from gap_le.h instead.
920  */
921 void hci_le_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type,
922     uint8_t own_address_type, uint8_t direct_address_typ, bd_addr_t direct_address,
923     uint8_t channel_map, uint8_t filter_policy);
924 
925 
926 // Only for PTS testing
927 
928 /**
929  * Disable automatic L2CAP disconnect if no L2CAP connection is established
930  */
931 void hci_disable_l2cap_timeout_check(void);
932 
933 #if defined __cplusplus
934 }
935 #endif
936 
937 #endif // __HCI_H
938