xref: /btstack/src/hci.h (revision a8b124471dfe21102ceed42648b4103360000c12)
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/hci_cmds.h>
51 #include <btstack/utils.h>
52 #include "hci_transport.h"
53 #include "bt_control.h"
54 #include "remote_device_db.h"
55 
56 #include <stdint.h>
57 #include <stdlib.h>
58 #include <stdarg.h>
59 #include <btstack/linked_list.h>
60 
61 #if defined __cplusplus
62 extern "C" {
63 #endif
64 
65 // packet header sizes
66 #define HCI_CMD_HEADER_SIZE          3
67 #define HCI_ACL_HEADER_SIZE   	     4
68 #define HCI_SCO_HEADER_SIZE  	     3
69 #define HCI_EVENT_HEADER_SIZE        2
70 
71 // packet sizes (max payload)
72 #define HCI_ACL_DM1_SIZE            17
73 #define HCI_ACL_DH1_SIZE            27
74 #define HCI_ACL_2DH1_SIZE           54
75 #define HCI_ACL_3DH1_SIZE           83
76 #define HCI_ACL_DM3_SIZE           121
77 #define HCI_ACL_DH3_SIZE           183
78 #define HCI_ACL_DM5_SIZE           224
79 #define HCI_ACL_DH5_SIZE           339
80 #define HCI_ACL_2DH3_SIZE          367
81 #define HCI_ACL_3DH3_SIZE          552
82 #define HCI_ACL_2DH5_SIZE          679
83 #define HCI_ACL_3DH5_SIZE         1021
84 
85 #define HCI_EVENT_PAYLOAD_SIZE     255
86 #define HCI_CMD_PAYLOAD_SIZE       255
87 
88 #define LE_ADVERTISING_DATA_SIZE    31
89 
90 // packet buffer sizes
91 // HCI_ACL_PAYLOAD_SIZE is configurable and defined in config.h
92 #define HCI_EVENT_BUFFER_SIZE      (HCI_EVENT_HEADER_SIZE + HCI_EVENT_PAYLOAD_SIZE)
93 #define HCI_CMD_BUFFER_SIZE        (HCI_CMD_HEADER_SIZE   + HCI_CMD_PAYLOAD_SIZE)
94 #define HCI_ACL_BUFFER_SIZE        (HCI_ACL_HEADER_SIZE   + HCI_ACL_PAYLOAD_SIZE)
95 
96 // size of hci buffers, big enough for command, event, or acl packet without H4 packet type
97 // @note cmd buffer is bigger than event buffer
98 #ifdef HCI_PACKET_BUFFER_SIZE
99     #if HCI_PACKET_BUFFER_SIZE < HCI_ACL_BUFFER_SIZE
100         #error HCI_PACKET_BUFFER_SIZE must be equal or larger than HCI_ACL_BUFFER_SIZE
101     #endif
102     #if HCI_PACKET_BUFFER_SIZE < HCI_CMD_BUFFER_SIZE
103         #error HCI_PACKET_BUFFER_SIZE must be equal or larger than HCI_CMD_BUFFER_SIZE
104     #endif
105 #else
106     #if HCI_ACL_BUFFER_SIZE > HCI_CMD_BUFFER_SIZE
107         #define HCI_PACKET_BUFFER_SIZE HCI_ACL_BUFFER_SIZE
108     #else
109         #define HCI_PACKET_BUFFER_SIZE HCI_CMD_BUFFER_SIZE
110     #endif
111 #endif
112 
113 // additional pre-buffer space for packets to Bluetooth module, for now, used for HCI Transport H4 DMA
114 #define HCI_OUTGOING_PRE_BUFFER_SIZE 1
115 
116 // BNEP may uncompress the IP Header by 16 bytes
117 #ifdef HAVE_BNEP
118 #define HCI_INCOMING_PRE_BUFFER_SIZE (16 - HCI_ACL_HEADER_SIZE - 4)
119 #endif
120 #ifndef HCI_INCOMING_PRE_BUFFER_SIZE
121     #define HCI_INCOMING_PRE_BUFFER_SIZE 0
122 #endif
123 
124 // OGFs
125 #define OGF_LINK_CONTROL          0x01
126 #define OGF_LINK_POLICY           0x02
127 #define OGF_CONTROLLER_BASEBAND   0x03
128 #define OGF_INFORMATIONAL_PARAMETERS 0x04
129 #define OGF_STATUS_PARAMETERS     0x05
130 #define OGF_LE_CONTROLLER 0x08
131 #define OGF_BTSTACK 0x3d
132 #define OGF_VENDOR  0x3f
133 
134 // cmds for BTstack
135 // get state: @returns HCI_STATE
136 #define BTSTACK_GET_STATE                                  0x01
137 
138 // set power mode: @param HCI_POWER_MODE
139 #define BTSTACK_SET_POWER_MODE                             0x02
140 
141 // set capture mode: @param on
142 #define BTSTACK_SET_ACL_CAPTURE_MODE                       0x03
143 
144 // get BTstack version
145 #define BTSTACK_GET_VERSION                                0x04
146 
147 // get system Bluetooth state
148 #define BTSTACK_GET_SYSTEM_BLUETOOTH_ENABLED               0x05
149 
150 // set system Bluetooth state
151 #define BTSTACK_SET_SYSTEM_BLUETOOTH_ENABLED               0x06
152 
153 // enable inquiry scan for this client
154 #define BTSTACK_SET_DISCOVERABLE                           0x07
155 
156 // set global Bluetooth state
157 #define BTSTACK_SET_BLUETOOTH_ENABLED                      0x08
158 
159 // create l2cap channel: @param bd_addr(48), psm (16)
160 #define L2CAP_CREATE_CHANNEL                               0x20
161 
162 // disconnect l2cap disconnect, @param channel(16), reason(8)
163 #define L2CAP_DISCONNECT                                   0x21
164 
165 // register l2cap service: @param psm(16), mtu (16)
166 #define L2CAP_REGISTER_SERVICE                             0x22
167 
168 // unregister l2cap disconnect, @param psm(16)
169 #define L2CAP_UNREGISTER_SERVICE                           0x23
170 
171 // accept connection @param bd_addr(48), dest cid (16)
172 #define L2CAP_ACCEPT_CONNECTION                            0x24
173 
174 // decline l2cap disconnect,@param bd_addr(48), dest cid (16), reason(8)
175 #define L2CAP_DECLINE_CONNECTION                           0x25
176 
177 // create l2cap channel: @param bd_addr(48), psm (16), mtu (16)
178 #define L2CAP_CREATE_CHANNEL_MTU                           0x26
179 
180 // register SDP Service Record: service record (size)
181 #define SDP_REGISTER_SERVICE_RECORD                        0x30
182 
183 // unregister SDP Service Record
184 #define SDP_UNREGISTER_SERVICE_RECORD                      0x31
185 
186 // Get remote RFCOMM services
187 #define SDP_CLIENT_QUERY_RFCOMM_SERVICES                   0x32
188 
189 // Get remote SDP services
190 #define SDP_CLIENT_QUERY_SERVICES                          0x33
191 
192 // RFCOMM "HCI" Commands
193 #define RFCOMM_CREATE_CHANNEL       0x40
194 #define RFCOMM_DISCONNECT			0x41
195 #define RFCOMM_REGISTER_SERVICE     0x42
196 #define RFCOMM_UNREGISTER_SERVICE   0x43
197 #define RFCOMM_ACCEPT_CONNECTION    0x44
198 #define RFCOMM_DECLINE_CONNECTION   0x45
199 #define RFCOMM_PERSISTENT_CHANNEL   0x46
200 #define RFCOMM_CREATE_CHANNEL_WITH_CREDITS   0x47
201 #define RFCOMM_REGISTER_SERVICE_WITH_CREDITS 0x48
202 #define RFCOMM_GRANT_CREDITS                 0x49
203 
204 // GAP Classic 0x50
205 #define GAP_DISCONNECT              0x50
206 
207 // GAP LE      0x60
208 #define GAP_LE_SCAN_START           0x60
209 #define GAP_LE_SCAN_STOP            0x61
210 #define GAP_LE_CONNECT              0x62
211 #define GAP_LE_CONNECT_CANCEL       0x63
212 #define GAP_LE_SET_SCAN_PARAMETERS  0x64
213 
214 // GATT (Client) 0x70
215 #define GATT_DISCOVER_ALL_PRIMARY_SERVICES                       0x70
216 #define GATT_DISCOVER_PRIMARY_SERVICES_BY_UUID16                 0x71
217 #define GATT_DISCOVER_PRIMARY_SERVICES_BY_UUID128                0x72
218 #define GATT_FIND_INCLUDED_SERVICES_FOR_SERVICE                  0x73
219 #define GATT_DISCOVER_CHARACTERISTICS_FOR_SERVICE                0x74
220 #define GATT_DISCOVER_CHARACTERISTICS_FOR_SERVICE_BY_UUID128     0x75
221 #define GATT_DISCOVER_CHARACTERISTIC_DESCRIPTORS                 0x76
222 #define GATT_READ_VALUE_OF_CHARACTERISTIC                        0x77
223 #define GATT_READ_LONG_VALUE_OF_CHARACTERISTIC                   0x78
224 #define GATT_WRITE_VALUE_OF_CHARACTERISTIC_WITHOUT_RESPONSE      0x79
225 #define GATT_WRITE_VALUE_OF_CHARACTERISTIC                       0x7A
226 #define GATT_WRITE_LONG_VALUE_OF_CHARACTERISTIC                  0x7B
227 #define GATT_RELIABLE_WRITE_LONG_VALUE_OF_CHARACTERISTIC         0x7C
228 #define GATT_READ_CHARACTERISTIC_DESCRIPTOR                      0X7D
229 #define GATT_READ_LONG_CHARACTERISTIC_DESCRIPTOR                 0X7E
230 #define GATT_WRITE_CHARACTERISTIC_DESCRIPTOR                     0X7F
231 #define GATT_WRITE_LONG_CHARACTERISTIC_DESCRIPTOR                0X80
232 #define GATT_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION           0X81
233 
234 //
235 #define IS_COMMAND(packet, command) (READ_BT_16(packet,0) == command.opcode)
236 
237 // data: event(8)
238 #define DAEMON_EVENT_CONNECTION_OPENED                     0x50
239 
240 // data: event(8)
241 #define DAEMON_EVENT_CONNECTION_CLOSED                     0x51
242 
243 // data: event(8), nr_connections(8)
244 #define DAEMON_NR_CONNECTIONS_CHANGED                      0x52
245 
246 // data: event(8)
247 #define DAEMON_EVENT_NEW_RFCOMM_CREDITS                    0x53
248 
249 // data: event(8)
250 #define DAEMON_EVENT_HCI_PACKET_SENT                       0x54
251 
252 /**
253  * LE connection parameter update state
254  */
255 
256 typedef enum {
257     CON_PARAMETER_UPDATE_NONE,
258     CON_PARAMETER_UPDATE_SEND_RESPONSE,
259     CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS,
260     CON_PARAMETER_UPDATE_DENY
261 } le_con_parameter_update_state_t;
262 
263 typedef struct le_connection_parameter_range{
264     uint16_t le_conn_interval_min;
265     uint16_t le_conn_interval_max;
266     uint16_t le_conn_latency_min;
267     uint16_t le_conn_latency_max;
268     uint16_t le_supervision_timeout_min;
269     uint16_t le_supervision_timeout_max;
270 } le_connection_parameter_range_t;
271 
272 // Authentication flags
273 typedef enum {
274     AUTH_FLAGS_NONE                = 0x0000,
275     RECV_LINK_KEY_REQUEST          = 0x0001,
276     HANDLE_LINK_KEY_REQUEST        = 0x0002,
277     SENT_LINK_KEY_REPLY            = 0x0004,
278     SENT_LINK_KEY_NEGATIVE_REQUEST = 0x0008,
279     RECV_LINK_KEY_NOTIFICATION     = 0x0010,
280     DENY_PIN_CODE_REQUEST          = 0x0040,
281     RECV_IO_CAPABILITIES_REQUEST   = 0x0080,
282     SEND_IO_CAPABILITIES_REPLY     = 0x0100,
283     SEND_USER_CONFIRM_REPLY        = 0x0200,
284     SEND_USER_PASSKEY_REPLY        = 0x0400,
285 
286     // pairing status
287     LEGACY_PAIRING_ACTIVE          = 0x2000,
288     SSP_PAIRING_ACTIVE             = 0x4000,
289 
290     // connection status
291     CONNECTION_ENCRYPTED           = 0x8000,
292 } hci_authentication_flags_t;
293 
294 /**
295  * Connection State
296  */
297 typedef enum {
298     SEND_CREATE_CONNECTION = 0,
299     SENT_CREATE_CONNECTION,
300     SEND_CANCEL_CONNECTION,
301     SENT_CANCEL_CONNECTION,
302     RECEIVED_CONNECTION_REQUEST,
303     ACCEPTED_CONNECTION_REQUEST,
304     REJECTED_CONNECTION_REQUEST,
305     OPEN,
306     SEND_DISCONNECT,
307     SENT_DISCONNECT,
308     RECEIVED_DISCONNECTION_COMPLETE
309 } CONNECTION_STATE;
310 
311 // bonding flags
312 enum {
313     BONDING_REQUEST_REMOTE_FEATURES   = 0x01,
314     BONDING_RECEIVED_REMOTE_FEATURES  = 0x02,
315     BONDING_REMOTE_SUPPORTS_SSP       = 0x04,
316     BONDING_DISCONNECT_SECURITY_BLOCK = 0x08,
317     BONDING_DISCONNECT_DEDICATED_DONE = 0x10,
318     BONDING_SEND_AUTHENTICATE_REQUEST = 0x20,
319     BONDING_SEND_ENCRYPTION_REQUEST   = 0x40,
320     BONDING_DEDICATED                 = 0x80,
321     BONDING_EMIT_COMPLETE_ON_DISCONNECT = 0x100
322 };
323 
324 typedef enum {
325     BLUETOOTH_OFF = 1,
326     BLUETOOTH_ON,
327     BLUETOOTH_ACTIVE
328 } BLUETOOTH_STATE;
329 
330 // le central scanning state
331 typedef enum {
332     LE_SCAN_IDLE,
333     LE_START_SCAN,
334     LE_SCANNING,
335     LE_STOP_SCAN,
336 } le_scanning_state_t;
337 
338 //
339 // SM internal types and globals
340 //
341 
342 typedef enum {
343 
344     // general states
345     SM_GENERAL_IDLE,
346     SM_GENERAL_SEND_PAIRING_FAILED,
347     SM_GENERAL_TIMEOUT, // no other security messages are exchanged
348 
349     // Phase 1: Pairing Feature Exchange
350     SM_PH1_W4_USER_RESPONSE,
351 
352     // Phase 2: Authenticating and Encrypting
353 
354     // get random number for use as TK Passkey if we show it
355     SM_PH2_GET_RANDOM_TK,
356     SM_PH2_W4_RANDOM_TK,
357 
358     // get local random number for confirm c1
359     SM_PH2_C1_GET_RANDOM_A,
360     SM_PH2_C1_W4_RANDOM_A,
361     SM_PH2_C1_GET_RANDOM_B,
362     SM_PH2_C1_W4_RANDOM_B,
363 
364     // calculate confirm value for local side
365     SM_PH2_C1_GET_ENC_A,
366     SM_PH2_C1_W4_ENC_A,
367     SM_PH2_C1_GET_ENC_B,
368     SM_PH2_C1_W4_ENC_B,
369 
370     // calculate confirm value for remote side
371     SM_PH2_C1_GET_ENC_C,
372     SM_PH2_C1_W4_ENC_C,
373     SM_PH2_C1_GET_ENC_D,
374     SM_PH2_C1_W4_ENC_D,
375 
376     SM_PH2_C1_SEND_PAIRING_CONFIRM,
377     SM_PH2_SEND_PAIRING_RANDOM,
378 
379     // calc STK
380     SM_PH2_CALC_STK,
381     SM_PH2_W4_STK,
382 
383     SM_PH2_W4_CONNECTION_ENCRYPTED,
384 
385     // Phase 3: Transport Specific Key Distribution
386 
387     // calculate DHK, Y, EDIV, and LTK
388     SM_PH3_GET_RANDOM,
389     SM_PH3_W4_RANDOM,
390     SM_PH3_GET_DIV,
391     SM_PH3_W4_DIV,
392     SM_PH3_Y_GET_ENC,
393     SM_PH3_Y_W4_ENC,
394     SM_PH3_LTK_GET_ENC,
395     SM_PH3_LTK_W4_ENC,
396     SM_PH3_CSRK_GET_ENC,
397     SM_PH3_CSRK_W4_ENC,
398 
399     // exchange keys
400     SM_PH3_DISTRIBUTE_KEYS,
401     SM_PH3_RECEIVE_KEYS,
402 
403     // Phase 4: re-establish previously distributed LTK
404     SM_PH4_Y_GET_ENC,
405     SM_PH4_Y_W4_ENC,
406     SM_PH4_LTK_GET_ENC,
407     SM_PH4_LTK_W4_ENC,
408     SM_PH4_SEND_LTK,
409 
410     // RESPONDER ROLE
411     SM_RESPONDER_SEND_SECURITY_REQUEST,
412     SM_RESPONDER_SEND_LTK_REQUESTED_NEGATIVE_REPLY,
413     SM_RESPONDER_PH1_W4_PAIRING_REQUEST,
414     SM_RESPONDER_PH1_SEND_PAIRING_RESPONSE,
415     SM_RESPONDER_PH1_W4_PAIRING_CONFIRM,
416     SM_RESPONDER_PH2_W4_PAIRING_RANDOM,
417     SM_RESPONDER_PH2_W4_LTK_REQUEST,
418     SM_RESPONDER_PH2_SEND_LTK_REPLY,
419 
420     // INITITIATOR ROLE
421     SM_INITIATOR_CONNECTED,
422     SM_INITIATOR_PH1_SEND_PAIRING_REQUEST,
423     SM_INITIATOR_PH1_W4_PAIRING_RESPONSE,
424     SM_INITIATOR_PH2_W4_PAIRING_CONFIRM,
425     SM_INITIATOR_PH2_W4_PAIRING_RANDOM,
426     SM_INITIATOR_PH3_SEND_START_ENCRYPTION,
427     SM_INITIATOR_PH3_XXXX,
428 
429 } security_manager_state_t;
430 
431 typedef enum {
432     CSRK_LOOKUP_IDLE,
433     CSRK_LOOKUP_W4_READY,
434     CSRK_LOOKUP_STARTED,
435 } csrk_lookup_state_t;
436 
437 // Authorization state
438 typedef enum {
439     AUTHORIZATION_UNKNOWN,
440     AUTHORIZATION_PENDING,
441     AUTHORIZATION_DECLINED,
442     AUTHORIZATION_GRANTED
443 } authorization_state_t;
444 
445 // connection info available as long as connection exists
446 typedef struct sm_connection {
447     uint16_t  sm_handle;
448     uint8_t   sm_role;   // 0 - IamMaster, 1 = IamSlave
449     bd_addr_t sm_peer_address;
450     uint8_t   sm_peer_addr_type;
451     security_manager_state_t sm_engine_state;
452     csrk_lookup_state_t      sm_csrk_lookup_state;
453     uint8_t   sm_connection_encrypted;
454     uint8_t   sm_connection_authenticated;   // [0..1]
455     uint8_t   sm_actual_encryption_key_size;
456     authorization_state_t sm_connection_authorization_state;
457     timer_source_t sm_timeout;
458 } sm_connection_t;
459 
460 typedef struct {
461     // linked list - assert: first field
462     linked_item_t    item;
463 
464     // remote side
465     bd_addr_t address;
466 
467     // module handle
468     hci_con_handle_t con_handle;
469 
470     // le public, le random, classic
471     bd_addr_type_t address_type;
472 
473     // connection state
474     CONNECTION_STATE state;
475 
476     // bonding
477     uint16_t bonding_flags;
478     uint8_t  bonding_status;
479     // requested security level
480     gap_security_level_t requested_security_level;
481 
482     //
483     link_key_type_t link_key_type;
484 
485     // errands
486     uint32_t authentication_flags;
487 
488     timer_source_t timeout;
489 
490 #ifdef HAVE_TIME
491     // timer
492     struct timeval timestamp;
493 #endif
494 #ifdef HAVE_TICK
495     uint32_t timestamp; // timeout in system ticks
496 #endif
497 
498     // ACL packet recombination - PRE_BUFFER + ACL Header + ACL payload
499     uint8_t  acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 4 + HCI_ACL_BUFFER_SIZE];
500     uint16_t acl_recombination_pos;
501     uint16_t acl_recombination_length;
502 
503     // number ACL packets sent to controller
504     uint8_t num_acl_packets_sent;
505 
506     // connection parameter update
507     le_con_parameter_update_state_t le_con_parameter_update_state;
508     uint16_t le_conn_interval_min;
509     uint16_t le_conn_interval_max;
510     uint16_t le_conn_latency;
511     uint16_t le_supervision_timeout;
512     uint16_t le_update_con_parameter_response;
513 
514     // LE Security Manager
515     // sm_connection_t sm_connection;
516 
517 } hci_connection_t;
518 
519 /**
520  * main data structure
521  */
522 typedef struct {
523     // transport component with configuration
524     hci_transport_t  * hci_transport;
525     void             * config;
526 
527     // basic configuration
528     const char         * local_name;
529     uint32_t           class_of_device;
530     bd_addr_t          local_bd_addr;
531     uint8_t            ssp_enable;
532     uint8_t            ssp_io_capability;
533     uint8_t            ssp_authentication_requirement;
534     uint8_t            ssp_auto_accept;
535 
536     // hardware power controller
537     bt_control_t     * control;
538 
539     // list of existing baseband connections
540     linked_list_t     connections;
541 
542     // single buffer for HCI packet assembly + additional prebuffer for H4 drivers
543     uint8_t   hci_packet_buffer_prefix[HCI_OUTGOING_PRE_BUFFER_SIZE];
544     uint8_t   hci_packet_buffer[HCI_PACKET_BUFFER_SIZE]; // opcode (16), len(8)
545     uint8_t   hci_packet_buffer_reserved;
546     uint16_t  acl_fragmentation_pos;
547     uint16_t  acl_fragmentation_total_size;
548 
549     /* host to controller flow control */
550     uint8_t  num_cmd_packets;
551     uint8_t  acl_packets_total_num;
552     uint16_t acl_data_packet_length;
553     uint8_t  sco_packets_total_num;
554     uint8_t  sco_data_packet_length;
555     uint8_t  le_acl_packets_total_num;
556     uint16_t le_data_packets_length;
557 
558     /* local supported features */
559     uint8_t local_supported_features[8];
560 
561     // usable packet types given acl_data_packet_length and HCI_ACL_BUFFER_SIZE
562     uint16_t packet_types;
563 
564     /* callback to L2CAP layer */
565     void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size);
566 
567     /* remote device db */
568     remote_device_db_t const*remote_device_db;
569 
570     /* hci state machine */
571     HCI_STATE state;
572     uint8_t   substate;
573     uint8_t   cmds_ready;
574 
575     uint16_t  last_cmd_opcode;
576 
577     uint8_t   discoverable;
578     uint8_t   connectable;
579     uint8_t   bondable;
580 
581     /* buffer for scan enable cmd - 0xff no change */
582     uint8_t   new_scan_enable_value;
583 
584     // buffer for single connection decline
585     uint8_t   decline_reason;
586     bd_addr_t decline_addr;
587 
588     uint8_t   adv_addr_type;
589     bd_addr_t adv_address;
590 
591     le_scanning_state_t le_scanning_state;
592 
593     // buffer for le scan type command - 0xff not set
594     uint8_t  le_scan_type;
595     uint16_t le_scan_interval;
596     uint16_t le_scan_window;
597 
598     le_connection_parameter_range_t le_connection_parameter_range;
599 
600     // custom BD ADDR
601     bd_addr_t custom_bd_addr;
602     uint8_t   custom_bd_addr_set;
603 
604 } hci_stack_t;
605 
606 /**
607  * set connection iterator
608  */
609 void hci_connections_get_iterator(linked_list_iterator_t *it);
610 
611 le_connection_parameter_range_t gap_le_get_connection_parameter_range();
612 void gap_le_set_connection_parameter_range(le_connection_parameter_range_t range);
613 
614 // *************** le client start
615 
616 le_command_status_t le_central_start_scan(void);
617 le_command_status_t le_central_stop_scan(void);
618 le_command_status_t le_central_connect(bd_addr_t * addr, bd_addr_type_t addr_type);
619 le_command_status_t le_central_connect_cancel(void);
620 le_command_status_t gap_disconnect(hci_con_handle_t handle);
621 void le_central_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window);
622 
623 // *************** le client end
624 
625 // create and send hci command packets based on a template and a list of parameters
626 uint16_t hci_create_cmd(uint8_t *hci_cmd_buffer, hci_cmd_t *cmd, ...);
627 uint16_t hci_create_cmd_internal(uint8_t *hci_cmd_buffer, const hci_cmd_t *cmd, va_list argptr);
628 
629 void hci_connectable_control(uint8_t enable);
630 void hci_close(void);
631 
632 /**
633  * run the hci control loop once
634  */
635 void hci_run(void);
636 
637 // send complete CMD packet
638 int hci_send_cmd_packet(uint8_t *packet, int size);
639 
640 // send ACL packet prepared in hci packet buffer
641 int hci_send_acl_packet_buffer(int size);
642 
643 // new functions replacing hci_can_send_packet_now[_using_packet_buffer]
644 int hci_can_send_command_packet_now(void);
645 int hci_can_send_acl_packet_now(hci_con_handle_t con_handle);
646 int hci_can_send_prepared_acl_packet_now(hci_con_handle_t con_handle);
647 
648 // reserves outgoing packet buffer. @returns 1 if successful
649 int  hci_reserve_packet_buffer(void);
650 void hci_release_packet_buffer(void);
651 
652 // used for internal checks in l2cap[-le].c
653 int hci_is_packet_buffer_reserved(void);
654 
655 // get point to packet buffer
656 uint8_t* hci_get_outgoing_packet_buffer(void);
657 
658 bd_addr_t * hci_local_bd_addr(void);
659 hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle);
660 hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t *addr, bd_addr_type_t addr_type);
661 int hci_is_le_connection(hci_connection_t * connection);
662 uint8_t  hci_number_outgoing_packets(hci_con_handle_t handle);
663 uint8_t  hci_number_free_acl_slots_for_handle(hci_con_handle_t con_handle);
664 int      hci_authentication_active_for_handle(hci_con_handle_t handle);
665 uint16_t hci_max_acl_data_packet_length(void);
666 uint16_t hci_max_acl_le_data_packet_length(void);
667 uint16_t hci_usable_acl_packet_types(void);
668 int      hci_non_flushable_packet_boundary_flag_supported(void);
669 
670 void hci_disconnect_all(void);
671 
672 void hci_emit_state(void);
673 void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status);
674 void hci_emit_l2cap_check_timeout(hci_connection_t *conn);
675 void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason);
676 void hci_emit_nr_connections_changed(void);
677 void hci_emit_hci_open_failed(void);
678 void hci_emit_btstack_version(void);
679 void hci_emit_system_bluetooth_enabled(uint8_t enabled);
680 void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name);
681 void hci_emit_discoverable_enabled(uint8_t enabled);
682 void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level);
683 void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status);
684 
685 // query if remote side supports SSP
686 // query if the local side supports SSP
687 int hci_local_ssp_activated(void);
688 
689 // query if the remote side supports SSP
690 int hci_remote_ssp_supported(hci_con_handle_t con_handle);
691 
692 // query if both sides support SSP
693 int hci_ssp_supported_on_both_sides(hci_con_handle_t handle);
694 
695 // disable automatic l2cap disconnect for testing
696 void hci_disable_l2cap_timeout_check(void);
697 
698 // disconnect because of security block
699 void hci_disconnect_security_block(hci_con_handle_t con_handle);
700 
701 /** Embedded API **/
702 
703 // Set up HCI. Needs to be called before any other function
704 void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db);
705 
706 // Set class of device that will be set during Bluetooth init
707 void hci_set_class_of_device(uint32_t class_of_device);
708 
709 // Set Public BD ADDR - passed on to Bluetooth chipset if supported in bt_control_h
710 void hci_set_bd_addr(bd_addr_t addr);
711 
712 // Registers a packet handler. Used if L2CAP is not used (rarely).
713 void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size));
714 
715 // Requests the change of BTstack power mode.
716 int  hci_power_control(HCI_POWER_MODE mode);
717 
718 // Allows to control if device is discoverable. OFF by default.
719 void hci_discoverable_control(uint8_t enable);
720 
721 // Creates and sends HCI command packets based on a template and
722 // a list of parameters. Will return error if outgoing data buffer
723 // is occupied.
724 int hci_send_cmd(const hci_cmd_t *cmd, ...);
725 
726 // Deletes link key for remote device with baseband address.
727 void hci_drop_link_key_for_bd_addr(bd_addr_t *addr);
728 
729 // Configure Secure Simple Pairing
730 
731 // enable will enable SSP during init
732 void hci_ssp_set_enable(int enable);
733 
734 // if set, BTstack will respond to io capability request using authentication requirement
735 void hci_ssp_set_io_capability(int ssp_io_capability);
736 void hci_ssp_set_authentication_requirement(int authentication_requirement);
737 
738 // if set, BTstack will confirm a numberic comparion and enter '000000' if requested
739 void hci_ssp_set_auto_accept(int auto_accept);
740 
741 // get addr type and address used in advertisement packets
742 void hci_le_advertisement_address(uint8_t * addr_type, bd_addr_t * addr);
743 
744 
745 #if defined __cplusplus
746 }
747 #endif
748 
749 #endif // __HCI_H
750