xref: /btstack/src/hci.h (revision c9b8fdd9982935a4c59b27cea8669d75772fb366)
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 #ifdef HAVE_BNEP
96 #define HCI_INCOMING_PRE_BUFFER_SIZE (16 - HCI_ACL_HEADER_SIZE - 4)
97 #endif
98 #ifndef HCI_INCOMING_PRE_BUFFER_SIZE
99     #define HCI_INCOMING_PRE_BUFFER_SIZE 0
100 #endif
101 
102 //
103 #define IS_COMMAND(packet, command) (little_endian_read_16(packet,0) == command.opcode)
104 
105 // check if command complete event for given command
106 #define COMMAND_COMPLETE_EVENT(event,cmd) ( event[0] == HCI_EVENT_COMMAND_COMPLETE && little_endian_read_16(event,3) == cmd.opcode)
107 #define COMMAND_STATUS_EVENT(event,cmd) ( event[0] == HCI_EVENT_COMMAND_STATUS && little_endian_read_16(event,4) == cmd.opcode)
108 
109 // Code+Len=2, Pkts+Opcode=3; total=5
110 #define OFFSET_OF_DATA_IN_COMMAND_COMPLETE 5
111 
112 // ACL Packet
113 #define READ_ACL_CONNECTION_HANDLE( buffer ) ( little_endian_read_16(buffer,0) & 0x0fff)
114 #define READ_ACL_FLAGS( buffer )      ( buffer[1] >> 4 )
115 #define READ_ACL_LENGTH( buffer )     (little_endian_read_16(buffer, 2))
116 
117 // Sneak peak into L2CAP Packet
118 #define READ_L2CAP_LENGTH(buffer)     ( little_endian_read_16(buffer, 4))
119 #define READ_L2CAP_CHANNEL_ID(buffer) ( little_endian_read_16(buffer, 6))
120 
121 /**
122  * LE connection parameter update state
123  */
124 
125 typedef enum {
126     CON_PARAMETER_UPDATE_NONE,
127     CON_PARAMETER_UPDATE_SEND_REQUEST,
128     CON_PARAMETER_UPDATE_SEND_RESPONSE,
129     CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS,
130     CON_PARAMETER_UPDATE_DENY
131 } le_con_parameter_update_state_t;
132 
133 typedef struct le_connection_parameter_range{
134     uint16_t le_conn_interval_min;
135     uint16_t le_conn_interval_max;
136     uint16_t le_conn_latency_min;
137     uint16_t le_conn_latency_max;
138     uint16_t le_supervision_timeout_min;
139     uint16_t le_supervision_timeout_max;
140 } le_connection_parameter_range_t;
141 
142 // Authentication flags
143 typedef enum {
144     AUTH_FLAGS_NONE                = 0x0000,
145     RECV_LINK_KEY_REQUEST          = 0x0001,
146     HANDLE_LINK_KEY_REQUEST        = 0x0002,
147     SENT_LINK_KEY_REPLY            = 0x0004,
148     SENT_LINK_KEY_NEGATIVE_REQUEST = 0x0008,
149     RECV_LINK_KEY_NOTIFICATION     = 0x0010,
150     DENY_PIN_CODE_REQUEST          = 0x0040,
151     RECV_IO_CAPABILITIES_REQUEST   = 0x0080,
152     SEND_IO_CAPABILITIES_REPLY     = 0x0100,
153     SEND_USER_CONFIRM_REPLY        = 0x0200,
154     SEND_USER_PASSKEY_REPLY        = 0x0400,
155 
156     // pairing status
157     LEGACY_PAIRING_ACTIVE          = 0x2000,
158     SSP_PAIRING_ACTIVE             = 0x4000,
159 
160     // connection status
161     CONNECTION_ENCRYPTED           = 0x8000,
162 } hci_authentication_flags_t;
163 
164 /**
165  * Connection State
166  */
167 typedef enum {
168     SEND_CREATE_CONNECTION = 0,
169     SENT_CREATE_CONNECTION,
170     SEND_CANCEL_CONNECTION,
171     SENT_CANCEL_CONNECTION,
172     RECEIVED_CONNECTION_REQUEST,
173     ACCEPTED_CONNECTION_REQUEST,
174     REJECTED_CONNECTION_REQUEST,
175     OPEN,
176     SEND_DISCONNECT,
177     SENT_DISCONNECT,
178     RECEIVED_DISCONNECTION_COMPLETE
179 } CONNECTION_STATE;
180 
181 // bonding flags
182 enum {
183     BONDING_REQUEST_REMOTE_FEATURES   = 0x01,
184     BONDING_RECEIVED_REMOTE_FEATURES  = 0x02,
185     BONDING_REMOTE_SUPPORTS_SSP       = 0x04,
186     BONDING_DISCONNECT_SECURITY_BLOCK = 0x08,
187     BONDING_DISCONNECT_DEDICATED_DONE = 0x10,
188     BONDING_SEND_AUTHENTICATE_REQUEST = 0x20,
189     BONDING_SEND_ENCRYPTION_REQUEST   = 0x40,
190     BONDING_DEDICATED                 = 0x80,
191     BONDING_EMIT_COMPLETE_ON_DISCONNECT = 0x100
192 };
193 
194 typedef enum {
195     BLUETOOTH_OFF = 1,
196     BLUETOOTH_ON,
197     BLUETOOTH_ACTIVE
198 } BLUETOOTH_STATE;
199 
200 // le central scanning state
201 typedef enum {
202     LE_SCAN_IDLE,
203     LE_START_SCAN,
204     LE_SCANNING,
205     LE_STOP_SCAN,
206 } le_scanning_state_t;
207 
208 typedef enum {
209     LE_CONNECTING_IDLE,
210     LE_CONNECTING_DIRECT,
211     LE_CONNECTING_WHITELIST,
212 } le_connecting_state_t;
213 
214 //
215 // SM internal types and globals
216 //
217 
218 typedef enum {
219 
220     // general states
221     // state = 0
222     SM_GENERAL_IDLE,
223     SM_GENERAL_SEND_PAIRING_FAILED,
224     SM_GENERAL_TIMEOUT, // no other security messages are exchanged
225 
226     // Phase 1: Pairing Feature Exchange
227     SM_PH1_W4_USER_RESPONSE,
228 
229     // Phase 2: Authenticating and Encrypting
230 
231     // get random number for use as TK Passkey if we show it
232     SM_PH2_GET_RANDOM_TK,
233     SM_PH2_W4_RANDOM_TK,
234 
235     // get local random number for confirm c1
236     SM_PH2_C1_GET_RANDOM_A,
237     SM_PH2_C1_W4_RANDOM_A,
238     SM_PH2_C1_GET_RANDOM_B,
239     SM_PH2_C1_W4_RANDOM_B,
240 
241     // calculate confirm value for local side
242     // state = 10
243     SM_PH2_C1_GET_ENC_A,
244     SM_PH2_C1_W4_ENC_A,
245     SM_PH2_C1_GET_ENC_B,
246     SM_PH2_C1_W4_ENC_B,
247 
248     // calculate confirm value for remote side
249     SM_PH2_C1_GET_ENC_C,
250     SM_PH2_C1_W4_ENC_C,
251     SM_PH2_C1_GET_ENC_D,
252     SM_PH2_C1_W4_ENC_D,
253 
254     SM_PH2_C1_SEND_PAIRING_CONFIRM,
255     SM_PH2_SEND_PAIRING_RANDOM,
256 
257     // calc STK
258     // state = 20
259     SM_PH2_CALC_STK,
260     SM_PH2_W4_STK,
261 
262     SM_PH2_W4_CONNECTION_ENCRYPTED,
263 
264     // Phase 3: Transport Specific Key Distribution
265     // calculate DHK, Y, EDIV, and LTK
266     SM_PH3_GET_RANDOM,
267     SM_PH3_W4_RANDOM,
268     SM_PH3_GET_DIV,
269     SM_PH3_W4_DIV,
270     SM_PH3_Y_GET_ENC,
271     SM_PH3_Y_W4_ENC,
272     SM_PH3_LTK_GET_ENC,
273     // state = 30
274     SM_PH3_LTK_W4_ENC,
275     SM_PH3_CSRK_GET_ENC,
276     SM_PH3_CSRK_W4_ENC,
277 
278     // exchange keys
279     SM_PH3_DISTRIBUTE_KEYS,
280     SM_PH3_RECEIVE_KEYS,
281 
282     // RESPONDER ROLE
283     // state = 35
284     SM_RESPONDER_IDLE,
285     SM_RESPONDER_SEND_SECURITY_REQUEST,
286     SM_RESPONDER_PH0_RECEIVED_LTK,
287     SM_RESPONDER_PH0_SEND_LTK_REQUESTED_NEGATIVE_REPLY,
288     SM_RESPONDER_PH1_W4_PAIRING_REQUEST,
289     SM_RESPONDER_PH1_PAIRING_REQUEST_RECEIVED,
290     SM_RESPONDER_PH1_SEND_PAIRING_RESPONSE,
291     SM_RESPONDER_PH1_W4_PAIRING_CONFIRM,
292     SM_RESPONDER_PH2_W4_PAIRING_RANDOM,
293     SM_RESPONDER_PH2_W4_LTK_REQUEST,
294     SM_RESPONDER_PH2_SEND_LTK_REPLY,
295 
296     // Phase 4: re-establish previously distributed LTK
297     // state == 46
298     SM_RESPONDER_PH4_Y_GET_ENC,
299     SM_RESPONDER_PH4_Y_W4_ENC,
300     SM_RESPONDER_PH4_LTK_GET_ENC,
301     SM_RESPONDER_PH4_LTK_W4_ENC,
302     SM_RESPONDER_PH4_SEND_LTK,
303 
304     // INITITIATOR ROLE
305     // state = 51
306     SM_INITIATOR_CONNECTED,
307     SM_INITIATOR_PH0_HAS_LTK,
308     SM_INITIATOR_PH0_SEND_START_ENCRYPTION,
309     SM_INITIATOR_PH0_W4_CONNECTION_ENCRYPTED,
310     SM_INITIATOR_PH1_W2_SEND_PAIRING_REQUEST,
311     SM_INITIATOR_PH1_SEND_PAIRING_REQUEST,
312     SM_INITIATOR_PH1_W4_PAIRING_RESPONSE,
313     SM_INITIATOR_PH2_W4_PAIRING_CONFIRM,
314     SM_INITIATOR_PH2_W4_PAIRING_RANDOM,
315     SM_INITIATOR_PH3_SEND_START_ENCRYPTION,
316 
317 } security_manager_state_t;
318 
319 typedef enum {
320     IRK_LOOKUP_IDLE,
321     IRK_LOOKUP_W4_READY,
322     IRK_LOOKUP_STARTED,
323     IRK_LOOKUP_SUCCEEDED,
324     IRK_LOOKUP_FAILED
325 } irk_lookup_state_t;
326 
327 // Authorization state
328 typedef enum {
329     AUTHORIZATION_UNKNOWN,
330     AUTHORIZATION_PENDING,
331     AUTHORIZATION_DECLINED,
332     AUTHORIZATION_GRANTED
333 } authorization_state_t;
334 
335 typedef struct sm_pairing_packet {
336     uint8_t code;
337     uint8_t io_capability;
338     uint8_t oob_data_flag;
339     uint8_t auth_req;
340     uint8_t max_encryption_key_size;
341     uint8_t initiator_key_distribution;
342     uint8_t responder_key_distribution;
343 } sm_pairing_packet_t;
344 
345 // connection info available as long as connection exists
346 typedef struct sm_connection {
347     uint16_t                 sm_handle;
348     uint8_t                  sm_role;   // 0 - IamMaster, 1 = IamSlave
349     uint8_t                  sm_security_request_received;
350     uint8_t                  sm_bonding_requested;
351     uint8_t                  sm_peer_addr_type;
352     bd_addr_t                sm_peer_address;
353     security_manager_state_t sm_engine_state;
354     irk_lookup_state_t      sm_irk_lookup_state;
355     uint8_t                  sm_connection_encrypted;
356     uint8_t                  sm_connection_authenticated;   // [0..1]
357     uint8_t                  sm_actual_encryption_key_size;
358     sm_pairing_packet_t      sm_m_preq;  // only used during c1
359     authorization_state_t    sm_connection_authorization_state;
360     uint16_t                 sm_local_ediv;
361     uint8_t                  sm_local_rand[8];
362     int                      sm_le_db_index;
363 } sm_connection_t;
364 
365 typedef struct {
366     // linked list - assert: first field
367     btstack_linked_item_t    item;
368 
369     // remote side
370     bd_addr_t address;
371 
372     // module handle
373     hci_con_handle_t con_handle;
374 
375     // le public, le random, classic
376     bd_addr_type_t address_type;
377 
378     // role: 0 - master, 1 - slave
379     uint8_t role;
380 
381     // connection state
382     CONNECTION_STATE state;
383 
384     // bonding
385     uint16_t bonding_flags;
386     uint8_t  bonding_status;
387     // requested security level
388     gap_security_level_t requested_security_level;
389 
390     //
391     link_key_type_t link_key_type;
392 
393     // remote supported features
394     uint8_t remote_supported_feature_eSCO;
395 
396     // errands
397     uint32_t authentication_flags;
398 
399     btstack_timer_source_t timeout;
400 
401 #ifdef HAVE_TIME
402     // timer
403     struct timeval timestamp;
404 #endif
405 #ifdef HAVE_TICK
406     uint32_t timestamp; // timestamp in system ticks
407 #endif
408 #ifdef HAVE_TIME_MS
409     uint32_t timestamp; // timestamp in ms
410 #endif
411 
412     // ACL packet recombination - PRE_BUFFER + ACL Header + ACL payload
413     uint8_t  acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 4 + HCI_ACL_BUFFER_SIZE];
414     uint16_t acl_recombination_pos;
415     uint16_t acl_recombination_length;
416 
417     // number packets sent to controller
418     uint8_t num_acl_packets_sent;
419     uint8_t num_sco_packets_sent;
420 
421     // LE Connection parameter update
422     le_con_parameter_update_state_t le_con_parameter_update_state;
423     uint8_t  le_con_param_update_identifier;
424     uint16_t le_conn_interval_min;
425     uint16_t le_conn_interval_max;
426     uint16_t le_conn_latency;
427     uint16_t le_supervision_timeout;
428 
429 #ifdef ENABLE_BLE
430     // LE Security Manager
431     sm_connection_t sm_connection;
432 #endif
433 
434 } hci_connection_t;
435 
436 
437 /**
438  * HCI Inititizlization State Machine
439  */
440 typedef enum hci_init_state{
441     HCI_INIT_SEND_RESET = 0,
442     HCI_INIT_W4_SEND_RESET,
443     HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION,
444     HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION,
445 
446     HCI_INIT_SEND_BAUD_CHANGE,
447     HCI_INIT_W4_SEND_BAUD_CHANGE,
448     HCI_INIT_CUSTOM_INIT,
449     HCI_INIT_W4_CUSTOM_INIT,
450     HCI_INIT_SEND_RESET_CSR_WARM_BOOT,
451     HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT,
452 
453     HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS,
454     HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS,
455 
456     HCI_INIT_SEND_BAUD_CHANGE_BCM,
457     HCI_INIT_W4_SEND_BAUD_CHANGE_BCM,
458 
459     HCI_INIT_SET_BD_ADDR,
460     HCI_INIT_W4_SET_BD_ADDR,
461 
462     HCI_INIT_SEND_RESET_ST_WARM_BOOT,
463     HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT,
464 
465     HCI_INIT_READ_BD_ADDR,
466     HCI_INIT_W4_READ_BD_ADDR,
467 
468     HCI_INIT_READ_BUFFER_SIZE,
469     HCI_INIT_W4_READ_BUFFER_SIZE,
470     HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES,
471     HCI_INIT_W4_READ_LOCAL_SUPPORTED_FEATURES,
472     HCI_INIT_SET_EVENT_MASK,
473     HCI_INIT_W4_SET_EVENT_MASK,
474     HCI_INIT_WRITE_SIMPLE_PAIRING_MODE,
475     HCI_INIT_W4_WRITE_SIMPLE_PAIRING_MODE,
476     HCI_INIT_WRITE_PAGE_TIMEOUT,
477     HCI_INIT_W4_WRITE_PAGE_TIMEOUT,
478     HCI_INIT_WRITE_CLASS_OF_DEVICE,
479     HCI_INIT_W4_WRITE_CLASS_OF_DEVICE,
480     HCI_INIT_WRITE_LOCAL_NAME,
481     HCI_INIT_W4_WRITE_LOCAL_NAME,
482     HCI_INIT_WRITE_SCAN_ENABLE,
483     HCI_INIT_W4_WRITE_SCAN_ENABLE,
484 
485     HCI_INIT_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE,
486     HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE,
487 
488     HCI_INIT_LE_READ_BUFFER_SIZE,
489     HCI_INIT_W4_LE_READ_BUFFER_SIZE,
490     HCI_INIT_WRITE_LE_HOST_SUPPORTED,
491     HCI_INIT_W4_WRITE_LE_HOST_SUPPORTED,
492     HCI_INIT_READ_WHITE_LIST_SIZE,
493     HCI_INIT_W4_READ_WHITE_LIST_SIZE,
494 
495     HCI_INIT_LE_SET_SCAN_PARAMETERS,
496     HCI_INIT_W4_LE_SET_SCAN_PARAMETERS,
497 
498     HCI_INIT_DONE,
499 
500     HCI_FALLING_ASLEEP_DISCONNECT,
501     HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE,
502     HCI_FALLING_ASLEEP_COMPLETE,
503 
504     HCI_INIT_AFTER_SLEEP
505 
506 } hci_substate_t;
507 
508 enum {
509     LE_ADVERTISEMENT_TASKS_DISABLE      = 1 << 0,
510     LE_ADVERTISEMENT_TASKS_SET_DATA     = 1 << 1,
511     LE_ADVERTISEMENT_TASKS_SET_PARAMS   = 1 << 2,
512     LE_ADVERTISEMENT_TASKS_ENABLE       = 1 << 3,
513 };
514 
515 enum {
516     LE_WHITELIST_ON_CONTROLLER          = 1 << 0,
517     LE_WHITELIST_ADD_TO_CONTROLLER      = 1 << 1,
518     LE_WHITELIST_REMOVE_FROM_CONTROLLER = 1 << 2,
519 };
520 
521 typedef struct {
522     btstack_linked_item_t  item;
523     bd_addr_t      address;
524     bd_addr_type_t address_type;
525     uint8_t        state;
526 } whitelist_entry_t;
527 
528 /**
529  * main data structure
530  */
531 typedef struct {
532     // transport component with configuration
533     const hci_transport_t * hci_transport;
534     const void            * config;
535 
536     // chipset driver
537     const btstack_chipset_t * chipset;
538 
539     // hardware power controller
540     const btstack_control_t * control;
541 
542     /* link key db */
543     const btstack_link_key_db_t * link_key_db;
544 
545     // list of existing baseband connections
546     btstack_linked_list_t     connections;
547 
548     /* callback to L2CAP layer */
549     void (*acl_packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size);
550 
551     /* callback for SCO data */
552     void (*sco_packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size);
553 
554     /* callbacks for events */
555     btstack_linked_list_t event_handlers;
556 
557     // hardware error callback
558     void (*hardware_error_callback)(void);
559 
560     // basic configuration
561     const char *       local_name;
562     uint32_t           class_of_device;
563     bd_addr_t          local_bd_addr;
564     uint8_t            ssp_enable;
565     uint8_t            ssp_io_capability;
566     uint8_t            ssp_authentication_requirement;
567     uint8_t            ssp_auto_accept;
568 
569     // single buffer for HCI packet assembly + additional prebuffer for H4 drivers
570     uint8_t   hci_packet_buffer_prefix[HCI_OUTGOING_PRE_BUFFER_SIZE];
571     uint8_t   hci_packet_buffer[HCI_PACKET_BUFFER_SIZE]; // opcode (16), len(8)
572     uint8_t   hci_packet_buffer_reserved;
573     uint16_t  acl_fragmentation_pos;
574     uint16_t  acl_fragmentation_total_size;
575 
576     /* host to controller flow control */
577     uint8_t  num_cmd_packets;
578     uint8_t  acl_packets_total_num;
579     uint16_t acl_data_packet_length;
580     uint8_t  sco_packets_total_num;
581     uint8_t  sco_data_packet_length;
582     uint8_t  synchronous_flow_control_enabled;
583     uint8_t  le_acl_packets_total_num;
584     uint16_t le_data_packets_length;
585     uint8_t  sco_waiting_for_can_send_now;
586 
587     /* local supported features */
588     uint8_t local_supported_features[8];
589 
590     /* local supported commands summary - complete info is 64 bytes */
591     /* 0 - read buffer size */
592     /* 1 - write le host supported */
593     uint8_t local_supported_commands[1];
594 
595     /* bluetooth device information from hci read local version information */
596     // uint16_t hci_version;
597     // uint16_t hci_revision;
598     // uint16_t lmp_version;
599     uint16_t manufacturer;
600     // uint16_t lmp_subversion;
601 
602     // usable packet types given acl_data_packet_length and HCI_ACL_BUFFER_SIZE
603     uint16_t packet_types;
604 
605 
606     /* hci state machine */
607     HCI_STATE      state;
608     hci_substate_t substate;
609     btstack_timer_source_t timeout;
610     uint8_t   cmds_ready;
611 
612     uint16_t  last_cmd_opcode;
613 
614     uint8_t   discoverable;
615     uint8_t   connectable;
616     uint8_t   bondable;
617 
618     /* buffer for scan enable cmd - 0xff no change */
619     uint8_t   new_scan_enable_value;
620 
621     uint16_t   sco_voice_setting;
622 
623     uint8_t   loopback_mode;
624 
625     // buffer for single connection decline
626     uint8_t   decline_reason;
627     bd_addr_t decline_addr;
628 
629     uint8_t   adv_addr_type;
630     bd_addr_t adv_address;
631 
632     le_scanning_state_t   le_scanning_state;
633     le_connecting_state_t le_connecting_state;
634 
635     // buffer for le scan type command - 0xff not set
636     uint8_t  le_scan_type;
637     uint16_t le_scan_interval;
638     uint16_t le_scan_window;
639 
640     le_connection_parameter_range_t le_connection_parameter_range;
641 
642     uint8_t  * le_advertisements_data;
643     uint8_t    le_advertisements_data_len;
644 
645     uint8_t  le_advertisements_active;
646     uint8_t  le_advertisements_enabled;
647     uint8_t  le_advertisements_todo;
648 
649     uint16_t le_advertisements_interval_min;
650     uint16_t le_advertisements_interval_max;
651     uint8_t  le_advertisements_type;
652     uint8_t  le_advertisements_own_address_type;
653     uint8_t  le_advertisements_direct_address_type;
654     uint8_t  le_advertisements_channel_map;
655     uint8_t  le_advertisements_filter_policy;
656     bd_addr_t le_advertisements_direct_address;
657 
658     // LE Whitelist Management
659     uint16_t      le_whitelist_capacity;
660     btstack_linked_list_t le_whitelist;
661 
662     // custom BD ADDR
663     bd_addr_t custom_bd_addr;
664     uint8_t   custom_bd_addr_set;
665 
666 } hci_stack_t;
667 
668 /**
669  * set connection iterator
670  */
671 void hci_connections_get_iterator(btstack_linked_list_iterator_t *it);
672 
673 // create and send hci command packets based on a template and a list of parameters
674 uint16_t hci_create_cmd(uint8_t *hci_cmd_buffer, hci_cmd_t *cmd, ...);
675 
676 /**
677  * run the hci control loop once
678  */
679 void hci_run(void);
680 
681 // send ACL packet prepared in hci packet buffer
682 int hci_send_acl_packet_buffer(int size);
683 
684 // send SCO packet prepared in hci packet buffer
685 int hci_send_sco_packet_buffer(int size);
686 
687 
688 int hci_can_send_acl_classic_packet_now(void);
689 int hci_can_send_acl_le_packet_now(void);
690 int hci_can_send_acl_packet_now(hci_con_handle_t con_handle);
691 int hci_can_send_prepared_acl_packet_now(hci_con_handle_t con_handle);
692 int hci_can_send_sco_packet_now(void);
693 int hci_can_send_prepared_sco_packet_now(void);
694 
695 // reserves outgoing packet buffer. @returns 1 if successful
696 int  hci_reserve_packet_buffer(void);
697 void hci_release_packet_buffer(void);
698 
699 // used for internal checks in l2cap[-le].c
700 int hci_is_packet_buffer_reserved(void);
701 
702 // get point to packet buffer
703 uint8_t* hci_get_outgoing_packet_buffer(void);
704 
705 
706 hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle);
707 hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type);
708 int      hci_is_le_connection(hci_connection_t * connection);
709 int      hci_number_outgoing_packets(hci_con_handle_t handle);
710 int      hci_number_free_acl_slots_for_connection_type( bd_addr_type_t address_type);
711 int      hci_number_free_acl_slots_for_handle(hci_con_handle_t con_handle);
712 int      hci_authentication_active_for_handle(hci_con_handle_t handle);
713 uint16_t hci_max_acl_data_packet_length(void);
714 uint16_t hci_max_acl_le_data_packet_length(void);
715 uint16_t hci_usable_acl_packet_types(void);
716 int      hci_non_flushable_packet_boundary_flag_supported(void);
717 
718 void hci_disconnect_all(void);
719 
720 void hci_emit_state(void);
721 void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status);
722 void hci_emit_l2cap_check_timeout(hci_connection_t *conn);
723 void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason);
724 void hci_emit_nr_connections_changed(void);
725 void hci_emit_hci_open_failed(void);
726 void hci_emit_system_bluetooth_enabled(uint8_t enabled);
727 void hci_emit_remote_name_cached(bd_addr_t addr, device_name_t *name);
728 void hci_emit_discoverable_enabled(uint8_t enabled);
729 void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level);
730 void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status);
731 
732 // query if the local side supports SSP
733 int hci_local_ssp_activated(void);
734 
735 // query if the remote side supports SSP
736 int hci_remote_ssp_supported(hci_con_handle_t con_handle);
737 
738 // query if both sides support SSP
739 int hci_ssp_supported_on_both_sides(hci_con_handle_t handle);
740 
741 // disable automatic L2CAP disconnect for testing
742 void hci_disable_l2cap_timeout_check(void);
743 
744 // disconnect because of security block
745 void hci_disconnect_security_block(hci_con_handle_t con_handle);
746 
747 // send complete CMD packet
748 int hci_send_cmd_packet(uint8_t *packet, int size);
749 
750 // query if remote side supports eSCO
751 int hci_remote_eSCO_supported(hci_con_handle_t con_handle);
752 
753 /* API_START */
754 
755 void gap_le_get_connection_parameter_range(le_connection_parameter_range_t range);
756 void gap_le_set_connection_parameter_range(le_connection_parameter_range_t range);
757 
758 /* LE Client Start */
759 
760 uint8_t le_central_start_scan(void);
761 uint8_t le_central_stop_scan(void);
762 uint8_t le_central_connect(bd_addr_t addr, bd_addr_type_t addr_type);
763 uint8_t le_central_connect_cancel(void);
764 uint8_t gap_disconnect(hci_con_handle_t handle);
765 void    le_central_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window);
766 
767 /* LE Client End */
768 
769 void hci_connectable_control(uint8_t enable);
770 void hci_close(void);
771 
772 /**
773  * @note New functions replacing: hci_can_send_packet_now[_using_packet_buffer]
774  */
775 int hci_can_send_command_packet_now(void);
776 
777 /**
778  * @brief Gets local address.
779  */
780 void hci_local_bd_addr(bd_addr_t address_buffer);
781 
782 /**
783  * @brief Set up HCI. Needs to be called before any other function.
784  */
785 void hci_init(const hci_transport_t *transport, void *config, btstack_link_key_db_t const * btstack_link_key_db);
786 
787 /**
788  * @brief Configure Bluetooth chipset driver. Has to be called before power on, or right after receiving the local version information.
789  */
790 void hci_set_chipset(const btstack_chipset_t *chipset_driver);
791 
792 /**
793  * @brief Configure Bluetooth hardware control. Has to be called before power on.
794  */
795 void hci_set_control(const btstack_control_t *hardware_control);
796 
797 /**
798  * @brief Set class of device that will be set during Bluetooth init.
799  */
800 void hci_set_class_of_device(uint32_t class_of_device);
801 
802 /**
803  * @brief Set Public BD ADDR - passed on to Bluetooth chipset if supported in bt_control_h
804  */
805 void hci_set_bd_addr(bd_addr_t addr);
806 
807 /**
808  * @brief Add event packet handler.
809  */
810 void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler);
811 
812 /**
813  * @brief Registers a packet handler for ACL data. Used by L2CAP
814  */
815 void hci_register_acl_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size));
816 
817 /**
818  * @brief Registers a packet handler for SCO data. Used for HSP and HFP profiles.
819  */
820 void hci_register_sco_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size));
821 
822 /**
823  * @brief Requests the change of BTstack power mode.
824  */
825 int  hci_power_control(HCI_POWER_MODE mode);
826 
827 /**
828  * @brief Allows to control if device is discoverable. OFF by default.
829  */
830 void hci_discoverable_control(uint8_t enable);
831 
832 /**
833  * @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.
834  */
835 int hci_send_cmd(const hci_cmd_t *cmd, ...);
836 
837 /**
838  * @brief Deletes link key for remote device with baseband address.
839  */
840 void hci_drop_link_key_for_bd_addr(bd_addr_t addr);
841 
842 /* Configure Secure Simple Pairing */
843 
844 /**
845  * @brief Enable will enable SSP during init.
846  */
847 void hci_ssp_set_enable(int enable);
848 
849 /**
850  * @brief If set, BTstack will respond to io capability request using authentication requirement.
851  */
852 void hci_ssp_set_io_capability(int ssp_io_capability);
853 void hci_ssp_set_authentication_requirement(int authentication_requirement);
854 
855 /**
856  * @brief If set, BTstack will confirm a numeric comparison and enter '000000' if requested.
857  */
858 void hci_ssp_set_auto_accept(int auto_accept);
859 
860 /**
861  * @brief Get addr type and address used in advertisement packets.
862  */
863 void hci_le_advertisement_address(uint8_t * addr_type, bd_addr_t addr);
864 
865 /**
866  * @brief Set callback for Bluetooth Hardware Error
867  */
868 void hci_set_hardware_error_callback(void (*fn)(void));
869 
870 /**
871  * @brief Configure Voice Setting for use with SCO data in HSP/HFP
872  */
873 void hci_set_sco_voice_setting(uint16_t voice_setting);
874 
875 /**
876  * @brief Get SCO Voice Setting
877  * @return current voice setting
878  */
879 uint16_t hci_get_sco_voice_setting(void);
880 
881 /** @brief Get SCO packet length for current SCO Voice setting
882  *  @note  Using SCO packets of the exact length is required for USB transfer
883  *  @return Length of SCO packets in bytes (not audio frames) incl. 3 byte header
884  */
885 int hci_get_sco_packet_length(void);
886 
887 /* API_END */
888 
889 /**
890  * @brief Set Advertisement Parameters
891  * @param adv_int_min
892  * @param adv_int_max
893  * @param adv_type
894  * @param own_address_type
895  * @param direct_address_type
896  * @param direct_address
897  * @param channel_map
898  * @param filter_policy
899  *
900  * @note internal use. use gap_advertisements_set_params from gap_le.h instead.
901  */
902 void hci_le_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type,
903     uint8_t own_address_type, uint8_t direct_address_typ, bd_addr_t direct_address,
904     uint8_t channel_map, uint8_t filter_policy);
905 
906 #if defined __cplusplus
907 }
908 #endif
909 
910 #endif // __HCI_H
911