xref: /btstack/src/hci.h (revision 435e3c4e0d833cf29306bcbe25c84a0f767a8539)
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     // Phase 3: Transport Specific Key Distribution
253     // calculate DHK, Y, EDIV, and LTK
254     SM_PH3_GET_RANDOM,
255     SM_PH3_W4_RANDOM,
256     SM_PH3_GET_DIV,
257     SM_PH3_W4_DIV,
258     SM_PH3_Y_GET_ENC,
259     SM_PH3_Y_W4_ENC,
260     SM_PH3_LTK_GET_ENC,
261     // state = 30
262     SM_PH3_LTK_W4_ENC,
263     SM_PH3_CSRK_GET_ENC,
264     SM_PH3_CSRK_W4_ENC,
265 
266     // exchange keys
267     SM_PH3_DISTRIBUTE_KEYS,
268     SM_PH3_RECEIVE_KEYS,
269 
270     // RESPONDER ROLE
271     // state = 35
272     SM_RESPONDER_IDLE,
273     SM_RESPONDER_SEND_SECURITY_REQUEST,
274     SM_RESPONDER_PH0_RECEIVED_LTK,
275     SM_RESPONDER_PH0_SEND_LTK_REQUESTED_NEGATIVE_REPLY,
276     SM_RESPONDER_PH1_W4_PAIRING_REQUEST,
277     SM_RESPONDER_PH1_PAIRING_REQUEST_RECEIVED,
278     SM_RESPONDER_PH1_SEND_PAIRING_RESPONSE,
279     SM_RESPONDER_PH1_W4_PAIRING_CONFIRM,
280     SM_RESPONDER_PH2_W4_PAIRING_RANDOM,
281     SM_RESPONDER_PH2_W4_LTK_REQUEST,
282     SM_RESPONDER_PH2_SEND_LTK_REPLY,
283 
284     // Phase 4: re-establish previously distributed LTK
285     // state == 46
286     SM_RESPONDER_PH4_Y_GET_ENC,
287     SM_RESPONDER_PH4_Y_W4_ENC,
288     SM_RESPONDER_PH4_LTK_GET_ENC,
289     SM_RESPONDER_PH4_LTK_W4_ENC,
290     SM_RESPONDER_PH4_SEND_LTK,
291 
292     // INITITIATOR ROLE
293     // state = 51
294     SM_INITIATOR_CONNECTED,
295     SM_INITIATOR_PH0_HAS_LTK,
296     SM_INITIATOR_PH0_SEND_START_ENCRYPTION,
297     SM_INITIATOR_PH0_W4_CONNECTION_ENCRYPTED,
298     SM_INITIATOR_PH1_W2_SEND_PAIRING_REQUEST,
299     SM_INITIATOR_PH1_SEND_PAIRING_REQUEST,
300     SM_INITIATOR_PH1_W4_PAIRING_RESPONSE,
301     SM_INITIATOR_PH2_W4_PAIRING_CONFIRM,
302     SM_INITIATOR_PH2_W4_PAIRING_RANDOM,
303     SM_INITIATOR_PH3_SEND_START_ENCRYPTION,
304 
305 } security_manager_state_t;
306 
307 typedef enum {
308     IRK_LOOKUP_IDLE,
309     IRK_LOOKUP_W4_READY,
310     IRK_LOOKUP_STARTED,
311     IRK_LOOKUP_SUCCEEDED,
312     IRK_LOOKUP_FAILED
313 } irk_lookup_state_t;
314 
315 // Authorization state
316 typedef enum {
317     AUTHORIZATION_UNKNOWN,
318     AUTHORIZATION_PENDING,
319     AUTHORIZATION_DECLINED,
320     AUTHORIZATION_GRANTED
321 } authorization_state_t;
322 
323 typedef uint8_t sm_pairing_packet_t[7];
324 
325 // connection info available as long as connection exists
326 typedef struct sm_connection {
327     hci_con_handle_t         sm_handle;
328     uint8_t                  sm_role;   // 0 - IamMaster, 1 = IamSlave
329     uint8_t                  sm_security_request_received;
330     uint8_t                  sm_bonding_requested;
331     uint8_t                  sm_peer_addr_type;
332     bd_addr_t                sm_peer_address;
333     security_manager_state_t sm_engine_state;
334     irk_lookup_state_t      sm_irk_lookup_state;
335     uint8_t                  sm_connection_encrypted;
336     uint8_t                  sm_connection_authenticated;   // [0..1]
337     uint8_t                  sm_actual_encryption_key_size;
338     sm_pairing_packet_t      sm_m_preq;  // only used during c1
339     authorization_state_t    sm_connection_authorization_state;
340     uint16_t                 sm_local_ediv;
341     uint8_t                  sm_local_rand[8];
342     int                      sm_le_db_index;
343 } sm_connection_t;
344 
345 typedef struct {
346     // linked list - assert: first field
347     btstack_linked_item_t    item;
348 
349     // remote side
350     bd_addr_t address;
351 
352     // module handle
353     hci_con_handle_t con_handle;
354 
355     // le public, le random, classic
356     bd_addr_type_t address_type;
357 
358     // role: 0 - master, 1 - slave
359     uint8_t role;
360 
361     // connection state
362     CONNECTION_STATE state;
363 
364     // bonding
365     uint16_t bonding_flags;
366     uint8_t  bonding_status;
367     // requested security level
368     gap_security_level_t requested_security_level;
369 
370     //
371     link_key_type_t link_key_type;
372 
373     // remote supported features
374     uint8_t remote_supported_feature_eSCO;
375 
376     // errands
377     uint32_t authentication_flags;
378 
379     btstack_timer_source_t timeout;
380 
381     // timeout in system ticks (HAVE_EMBEDDED_TICK) or milliseconds (HAVE_EMBEDDED_TIME_MS)
382     uint32_t timestamp;
383 
384     // ACL packet recombination - PRE_BUFFER + ACL Header + ACL payload
385     uint8_t  acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 4 + HCI_ACL_BUFFER_SIZE];
386     uint16_t acl_recombination_pos;
387     uint16_t acl_recombination_length;
388 
389     // number packets sent to controller
390     uint8_t num_acl_packets_sent;
391     uint8_t num_sco_packets_sent;
392 
393     // LE Connection parameter update
394     le_con_parameter_update_state_t le_con_parameter_update_state;
395     uint8_t  le_con_param_update_identifier;
396     uint16_t le_conn_interval_min;
397     uint16_t le_conn_interval_max;
398     uint16_t le_conn_latency;
399     uint16_t le_supervision_timeout;
400 
401 #ifdef ENABLE_BLE
402     // LE Security Manager
403     sm_connection_t sm_connection;
404 #endif
405 
406 } hci_connection_t;
407 
408 
409 /**
410  * HCI Inititizlization State Machine
411  */
412 typedef enum hci_init_state{
413     HCI_INIT_SEND_RESET = 0,
414     HCI_INIT_W4_SEND_RESET,
415     HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION,
416     HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION,
417 
418     HCI_INIT_SEND_BAUD_CHANGE,
419     HCI_INIT_W4_SEND_BAUD_CHANGE,
420     HCI_INIT_CUSTOM_INIT,
421     HCI_INIT_W4_CUSTOM_INIT,
422     HCI_INIT_SEND_RESET_CSR_WARM_BOOT,
423     HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT,
424     HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET,
425 
426     HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS,
427     HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS,
428 
429     HCI_INIT_SEND_BAUD_CHANGE_BCM,
430     HCI_INIT_W4_SEND_BAUD_CHANGE_BCM,
431 
432     HCI_INIT_SET_BD_ADDR,
433     HCI_INIT_W4_SET_BD_ADDR,
434 
435     HCI_INIT_SEND_RESET_ST_WARM_BOOT,
436     HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT,
437 
438     HCI_INIT_READ_BD_ADDR,
439     HCI_INIT_W4_READ_BD_ADDR,
440 
441     HCI_INIT_READ_BUFFER_SIZE,
442     HCI_INIT_W4_READ_BUFFER_SIZE,
443     HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES,
444     HCI_INIT_W4_READ_LOCAL_SUPPORTED_FEATURES,
445     HCI_INIT_SET_EVENT_MASK,
446     HCI_INIT_W4_SET_EVENT_MASK,
447     HCI_INIT_WRITE_SIMPLE_PAIRING_MODE,
448     HCI_INIT_W4_WRITE_SIMPLE_PAIRING_MODE,
449     HCI_INIT_WRITE_PAGE_TIMEOUT,
450     HCI_INIT_W4_WRITE_PAGE_TIMEOUT,
451     HCI_INIT_WRITE_CLASS_OF_DEVICE,
452     HCI_INIT_W4_WRITE_CLASS_OF_DEVICE,
453     HCI_INIT_WRITE_LOCAL_NAME,
454     HCI_INIT_W4_WRITE_LOCAL_NAME,
455     HCI_INIT_WRITE_SCAN_ENABLE,
456     HCI_INIT_W4_WRITE_SCAN_ENABLE,
457 
458     HCI_INIT_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE,
459     HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE,
460     HCI_INIT_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING,
461     HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING,
462 
463     HCI_INIT_LE_READ_BUFFER_SIZE,
464     HCI_INIT_W4_LE_READ_BUFFER_SIZE,
465     HCI_INIT_WRITE_LE_HOST_SUPPORTED,
466     HCI_INIT_W4_WRITE_LE_HOST_SUPPORTED,
467     HCI_INIT_READ_WHITE_LIST_SIZE,
468     HCI_INIT_W4_READ_WHITE_LIST_SIZE,
469 
470     HCI_INIT_LE_SET_SCAN_PARAMETERS,
471     HCI_INIT_W4_LE_SET_SCAN_PARAMETERS,
472 
473     HCI_INIT_DONE,
474 
475     HCI_FALLING_ASLEEP_DISCONNECT,
476     HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE,
477     HCI_FALLING_ASLEEP_COMPLETE,
478 
479     HCI_INIT_AFTER_SLEEP
480 
481 } hci_substate_t;
482 
483 enum {
484     LE_ADVERTISEMENT_TASKS_DISABLE       = 1 << 0,
485     LE_ADVERTISEMENT_TASKS_SET_ADV_DATA  = 1 << 1,
486     LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA = 1 << 2,
487     LE_ADVERTISEMENT_TASKS_SET_PARAMS    = 1 << 3,
488     LE_ADVERTISEMENT_TASKS_ENABLE        = 1 << 4,
489 };
490 
491 enum {
492     LE_WHITELIST_ON_CONTROLLER          = 1 << 0,
493     LE_WHITELIST_ADD_TO_CONTROLLER      = 1 << 1,
494     LE_WHITELIST_REMOVE_FROM_CONTROLLER = 1 << 2,
495 };
496 
497 typedef struct {
498     btstack_linked_item_t  item;
499     bd_addr_t      address;
500     bd_addr_type_t address_type;
501     uint8_t        state;
502 } whitelist_entry_t;
503 
504 /**
505  * main data structure
506  */
507 typedef struct {
508     // transport component with configuration
509     const hci_transport_t * hci_transport;
510     const void            * config;
511 
512     // chipset driver
513     const btstack_chipset_t * chipset;
514 
515     // hardware power controller
516     const btstack_control_t * control;
517 
518     /* link key db */
519     const btstack_link_key_db_t * link_key_db;
520 
521     // list of existing baseband connections
522     btstack_linked_list_t     connections;
523 
524     /* callback to L2CAP layer */
525     btstack_packet_handler_t acl_packet_handler;
526 
527     /* callback for SCO data */
528     btstack_packet_handler_t sco_packet_handler;
529 
530     /* callbacks for events */
531     btstack_linked_list_t event_handlers;
532 
533     // local version information callback
534     void (*local_version_information_callback)(uint8_t * local_version_information);
535 
536     // hardware error callback
537     void (*hardware_error_callback)(void);
538 
539     // basic configuration
540     const char *       local_name;
541     uint32_t           class_of_device;
542     bd_addr_t          local_bd_addr;
543     uint8_t            ssp_enable;
544     uint8_t            ssp_io_capability;
545     uint8_t            ssp_authentication_requirement;
546     uint8_t            ssp_auto_accept;
547 
548     // single buffer for HCI packet assembly + additional prebuffer for H4 drivers
549     uint8_t   hci_packet_buffer_prefix[HCI_OUTGOING_PRE_BUFFER_SIZE];
550     uint8_t   hci_packet_buffer[HCI_PACKET_BUFFER_SIZE]; // opcode (16), len(8)
551     uint8_t   hci_packet_buffer_reserved;
552     uint16_t  acl_fragmentation_pos;
553     uint16_t  acl_fragmentation_total_size;
554 
555     /* host to controller flow control */
556     uint8_t  num_cmd_packets;
557     uint8_t  acl_packets_total_num;
558     uint16_t acl_data_packet_length;
559     uint8_t  sco_packets_total_num;
560     uint8_t  sco_data_packet_length;
561     uint8_t  synchronous_flow_control_enabled;
562     uint8_t  le_acl_packets_total_num;
563     uint16_t le_data_packets_length;
564     uint8_t  sco_waiting_for_can_send_now;
565 
566     /* local supported features */
567     uint8_t local_supported_features[8];
568 
569     /* local supported commands summary - complete info is 64 bytes */
570     /* 0 - read buffer size */
571     /* 1 - write le host supported */
572     uint8_t local_supported_commands[1];
573 
574     /* bluetooth device information from hci read local version information */
575     // uint16_t hci_version;
576     // uint16_t hci_revision;
577     // uint16_t lmp_version;
578     uint16_t manufacturer;
579     // uint16_t lmp_subversion;
580 
581     // usable packet types given acl_data_packet_length and HCI_ACL_BUFFER_SIZE
582     uint16_t packet_types;
583 
584 
585     /* hci state machine */
586     HCI_STATE      state;
587     hci_substate_t substate;
588     btstack_timer_source_t timeout;
589     uint8_t   cmds_ready;
590 
591     uint16_t  last_cmd_opcode;
592 
593     uint8_t   discoverable;
594     uint8_t   connectable;
595     uint8_t   bondable;
596 
597     /* buffer for scan enable cmd - 0xff no change */
598     uint8_t   new_scan_enable_value;
599 
600     uint16_t   sco_voice_setting;
601 
602     uint8_t   loopback_mode;
603 
604     // buffer for single connection decline
605     uint8_t   decline_reason;
606     bd_addr_t decline_addr;
607 
608     uint8_t   adv_addr_type;
609     bd_addr_t adv_address;
610 
611     le_scanning_state_t   le_scanning_state;
612     le_connecting_state_t le_connecting_state;
613 
614     // buffer for le scan type command - 0xff not set
615     uint8_t  le_scan_type;
616     uint16_t le_scan_interval;
617     uint16_t le_scan_window;
618 
619     le_connection_parameter_range_t le_connection_parameter_range;
620 
621     uint8_t  * le_advertisements_data;
622     uint8_t    le_advertisements_data_len;
623 
624     uint8_t  * le_scan_response_data;
625     uint8_t    le_scan_response_data_len;
626 
627     uint8_t  le_advertisements_active;
628     uint8_t  le_advertisements_enabled;
629     uint8_t  le_advertisements_todo;
630 
631     uint16_t le_advertisements_interval_min;
632     uint16_t le_advertisements_interval_max;
633     uint8_t  le_advertisements_type;
634     uint8_t  le_advertisements_own_address_type;
635     uint8_t  le_advertisements_direct_address_type;
636     uint8_t  le_advertisements_channel_map;
637     uint8_t  le_advertisements_filter_policy;
638     bd_addr_t le_advertisements_direct_address;
639 
640     // LE Whitelist Management
641     uint16_t      le_whitelist_capacity;
642     btstack_linked_list_t le_whitelist;
643 
644     // custom BD ADDR
645     bd_addr_t custom_bd_addr;
646     uint8_t   custom_bd_addr_set;
647 
648 } hci_stack_t;
649 
650 
651 /* API_START */
652 
653 
654 // HCI init and configuration
655 
656 
657 /**
658  * @brief Set up HCI. Needs to be called before any other function.
659  */
660 void hci_init(const hci_transport_t *transport, const void *config);
661 
662 /**
663  * @brief Configure Bluetooth chipset driver. Has to be called before power on, or right after receiving the local version information.
664  */
665 void hci_set_chipset(const btstack_chipset_t *chipset_driver);
666 
667 /**
668  * @brief Configure Bluetooth hardware control. Has to be called before power on.
669  */
670 void hci_set_control(const btstack_control_t *hardware_control);
671 
672 /**
673  * @brief Configure Bluetooth hardware control. Has to be called before power on.
674  */
675 void hci_set_link_key_db(btstack_link_key_db_t const * link_key_db);
676 
677 /**
678  * @brief Set callback for Bluetooth Hardware Error
679  */
680 void hci_set_hardware_error_callback(void (*fn)(void));
681 
682 /**
683  * @brief Set callback for local information from Bluetooth controller right after HCI Reset
684  * @note Can be used to select chipset driver dynamically during startup
685  */
686 void hci_set_local_version_information_callback(void (*fn)(uint8_t * local_version_information));
687 
688 /**
689  * @brief Set Public BD ADDR - passed on to Bluetooth chipset during init if supported in bt_control_h
690  */
691 void hci_set_bd_addr(bd_addr_t addr);
692 
693 /**
694  * @brief Configure Voice Setting for use with SCO data in HSP/HFP
695  */
696 void hci_set_sco_voice_setting(uint16_t voice_setting);
697 
698 /**
699  * @brief Get SCO Voice Setting
700  * @return current voice setting
701  */
702 uint16_t hci_get_sco_voice_setting(void);
703 
704 /**
705  * @brief Requests the change of BTstack power mode.
706  */
707 int  hci_power_control(HCI_POWER_MODE mode);
708 
709 /**
710  * @brief Shutdown HCI
711  */
712 void hci_close(void);
713 
714 
715 // Callback registration
716 
717 
718 /**
719  * @brief Add event packet handler.
720  */
721 void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler);
722 
723 /**
724  * @brief Registers a packet handler for ACL data. Used by L2CAP
725  */
726 void hci_register_acl_packet_handler(btstack_packet_handler_t handler);
727 
728 /**
729  * @brief Registers a packet handler for SCO data. Used for HSP and HFP profiles.
730  */
731 void hci_register_sco_packet_handler(btstack_packet_handler_t handler);
732 
733 
734 // Sending HCI Commands
735 
736 /**
737  * @brief Check if CMD packet can be sent to controller
738  */
739 int hci_can_send_command_packet_now(void);
740 
741 /**
742  * @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.
743  */
744 int hci_send_cmd(const hci_cmd_t *cmd, ...);
745 
746 
747 // Sending SCO Packets
748 
749 /** @brief Get SCO packet length for current SCO Voice setting
750  *  @note  Using SCO packets of the exact length is required for USB transfer
751  *  @return Length of SCO packets in bytes (not audio frames) incl. 3 byte header
752  */
753 int hci_get_sco_packet_length(void);
754 
755 /**
756  * @brief Request emission of HCI_EVENT_SCO_CAN_SEND_NOW as soon as possible
757  * @note HCI_EVENT_SCO_CAN_SEND_NOW might be emitted during call to this function
758  *       so packet handler should be ready to handle it
759  */
760 void hci_request_sco_can_send_now_event(void);
761 
762 /**
763  * @brief Check HCI packet buffer and if SCO packet can be sent to controller
764  */
765 int hci_can_send_sco_packet_now(void);
766 
767 /**
768  * @brief Check if SCO packet can be sent to controller
769  */
770 int hci_can_send_prepared_sco_packet_now(void);
771 
772 /**
773  * @brief Send SCO packet prepared in HCI packet buffer
774  */
775 int hci_send_sco_packet_buffer(int size);
776 
777 
778 // Outgoing packet buffer, also used for SCO packets
779 // see hci_can_send_prepared_sco_packet_now amn hci_send_sco_packet_buffer
780 
781 /**
782  * Reserves outgoing packet buffer.
783  * @return 1 on success
784  */
785 int hci_reserve_packet_buffer(void);
786 
787 /**
788  * Get pointer for outgoing packet buffer
789  */
790 uint8_t* hci_get_outgoing_packet_buffer(void);
791 
792 /**
793  * Release outgoing packet buffer\
794  * @note only called instead of hci_send_preparared
795  */
796 void hci_release_packet_buffer(void);
797 
798 
799 /* API_END */
800 
801 
802 
803 /**
804  * Get connection iterator. Only used by l2cap.c and sm.c
805  */
806 void hci_connections_get_iterator(btstack_linked_list_iterator_t *it);
807 
808 /**
809  * Get internal hci_connection_t for given handle. Used by L2CAP, SM, daemon
810  */
811 hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle);
812 
813 /**
814  * Get internal hci_connection_t for given Bluetooth addres. Called by L2CAP
815  */
816 hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type);
817 
818 /**
819  * Check if outgoing packet buffer is reserved. Used for internal checks in l2cap.c
820  */
821 int hci_is_packet_buffer_reserved(void);
822 
823 /**
824  * Check hci packet buffer is free and a classic acl packet can be sent to controller
825  */
826 int hci_can_send_acl_classic_packet_now(void);
827 
828 /**
829  * Check hci packet buffer is free and an LE acl packet can be sent to controller
830  */
831 int hci_can_send_acl_le_packet_now(void);
832 
833 /**
834  * Check hci packet buffer is free and an acl packet for the given handle can be sent to controller
835  */
836 int hci_can_send_acl_packet_now(hci_con_handle_t con_handle);
837 
838 /**
839  * Check if acl packet for the given handle can be sent to controller
840  */
841 int hci_can_send_prepared_acl_packet_now(hci_con_handle_t con_handle);
842 
843 /**
844  * Send acl packet prepared in hci packet buffer
845  */
846 int hci_send_acl_packet_buffer(int size);
847 
848 /**
849  * Check if authentication is active. It delays automatic disconnect while no L2CAP connection
850  * Called by l2cap.
851  */
852 int hci_authentication_active_for_handle(hci_con_handle_t handle);
853 
854 /**
855  * Get maximal ACL Classic data packet length based on used buffer size. Called by L2CAP
856  */
857 uint16_t hci_max_acl_data_packet_length(void);
858 
859 /**
860  * Get supported packet types. Called by L2CAP
861  */
862 uint16_t hci_usable_acl_packet_types(void);
863 
864 /**
865  * Check if ACL packets marked as non flushable can be sent. Called by L2CAP
866  */
867 int hci_non_flushable_packet_boundary_flag_supported(void);
868 
869 /**
870  * Check if SSP is supported on both sides. Called by L2CAP
871  */
872 int gap_ssp_supported_on_both_sides(hci_con_handle_t handle);
873 
874 /**
875  * Disconn because of security block. Called by L2CAP
876  */
877 void hci_disconnect_security_block(hci_con_handle_t con_handle);
878 
879 /**
880  * Query if remote side supports eSCO
881  */
882 int hci_remote_esco_supported(hci_con_handle_t con_handle);
883 
884 /**
885  * Emit current HCI state. Called by daemon
886  */
887 void hci_emit_state(void);
888 
889 /**
890  * Send complete CMD packet. Called by daemon
891  */
892 int hci_send_cmd_packet(uint8_t *packet, int size);
893 
894 /**
895  * Disconnect all HCI connections. Called by daemon
896  */
897 void hci_disconnect_all(void);
898 
899 /**
900  * Get number of free acl slots for packets of given handle. Called by daemon
901  */
902 int hci_number_free_acl_slots_for_handle(hci_con_handle_t con_handle);
903 
904 /**
905  * @brief Set Advertisement Parameters
906  * @param adv_int_min
907  * @param adv_int_max
908  * @param adv_type
909  * @param own_address_type
910  * @param direct_address_type
911  * @param direct_address
912  * @param channel_map
913  * @param filter_policy
914  *
915  * @note internal use. use gap_advertisements_set_params from gap_le.h instead.
916  */
917 void hci_le_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type,
918     uint8_t own_address_type, uint8_t direct_address_typ, bd_addr_t direct_address,
919     uint8_t channel_map, uint8_t filter_policy);
920 
921 
922 // Only for PTS testing
923 
924 /**
925  * Disable automatic L2CAP disconnect if no L2CAP connection is established
926  */
927 void hci_disable_l2cap_timeout_check(void);
928 
929 #if defined __cplusplus
930 }
931 #endif
932 
933 #endif // __HCI_H
934