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