xref: /btstack/src/hci.c (revision 62bda3fbc556e58081814e1a181c44350bc08b72)
11f504dbdSmatthias.ringwald /*
26b64433eSmatthias.ringwald  * Copyright (C) 2009-2012 by Matthias Ringwald
31713bceaSmatthias.ringwald  *
41713bceaSmatthias.ringwald  * Redistribution and use in source and binary forms, with or without
51713bceaSmatthias.ringwald  * modification, are permitted provided that the following conditions
61713bceaSmatthias.ringwald  * are met:
71713bceaSmatthias.ringwald  *
81713bceaSmatthias.ringwald  * 1. Redistributions of source code must retain the above copyright
91713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer.
101713bceaSmatthias.ringwald  * 2. Redistributions in binary form must reproduce the above copyright
111713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer in the
121713bceaSmatthias.ringwald  *    documentation and/or other materials provided with the distribution.
131713bceaSmatthias.ringwald  * 3. Neither the name of the copyright holders nor the names of
141713bceaSmatthias.ringwald  *    contributors may be used to endorse or promote products derived
151713bceaSmatthias.ringwald  *    from this software without specific prior written permission.
166b64433eSmatthias.ringwald  * 4. Any redistribution, use, or modification is done solely for
176b64433eSmatthias.ringwald  *    personal benefit and not for any commercial purpose or for
186b64433eSmatthias.ringwald  *    monetary gain.
191713bceaSmatthias.ringwald  *
201713bceaSmatthias.ringwald  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
211713bceaSmatthias.ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
221713bceaSmatthias.ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
231713bceaSmatthias.ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
241713bceaSmatthias.ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
251713bceaSmatthias.ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
261713bceaSmatthias.ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
271713bceaSmatthias.ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
281713bceaSmatthias.ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
291713bceaSmatthias.ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
301713bceaSmatthias.ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311713bceaSmatthias.ringwald  * SUCH DAMAGE.
321713bceaSmatthias.ringwald  *
336b64433eSmatthias.ringwald  * Please inquire about commercial licensing options at [email protected]
346b64433eSmatthias.ringwald  *
351713bceaSmatthias.ringwald  */
361713bceaSmatthias.ringwald 
371713bceaSmatthias.ringwald /*
381f504dbdSmatthias.ringwald  *  hci.c
391f504dbdSmatthias.ringwald  *
401f504dbdSmatthias.ringwald  *  Created by Matthias Ringwald on 4/29/09.
411f504dbdSmatthias.ringwald  *
421f504dbdSmatthias.ringwald  */
431f504dbdSmatthias.ringwald 
44bde315ceS[email protected] #include "btstack-config.h"
4528171530Smatthias.ringwald 
467f2435e6Smatthias.ringwald #include "hci.h"
474c57c146S[email protected] #include "gap.h"
487f2435e6Smatthias.ringwald 
4993b8dc03Smatthias.ringwald #include <stdarg.h>
5093b8dc03Smatthias.ringwald #include <string.h>
5156fe0872Smatthias.ringwald #include <stdio.h>
527f2435e6Smatthias.ringwald 
53549e6ebeSmatthias.ringwald #ifndef EMBEDDED
54549e6ebeSmatthias.ringwald #include <unistd.h> // gethostbyname
5509ba8edeSmatthias.ringwald #include <btstack/version.h>
56549e6ebeSmatthias.ringwald #endif
57549e6ebeSmatthias.ringwald 
58a3b02b71Smatthias.ringwald #include "btstack_memory.h"
597f2435e6Smatthias.ringwald #include "debug.h"
60d8905019Smatthias.ringwald #include "hci_dump.h"
6193b8dc03Smatthias.ringwald 
62ae1fd9f3Smatthias.ringwald #include <btstack/hci_cmds.h>
631b0e3922Smatthias.ringwald 
64169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000
65ee091cf1Smatthias.ringwald 
66a45d6b9fS[email protected] #define HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP 11
67da5275c5S[email protected] 
6828171530Smatthias.ringwald #ifdef USE_BLUETOOL
6928171530Smatthias.ringwald #include "bt_control_iphone.h"
7028171530Smatthias.ringwald #endif
7128171530Smatthias.ringwald 
72758b46ceSmatthias.ringwald static void hci_update_scan_enable(void);
73a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection);
74758b46ceSmatthias.ringwald 
7506b35ec0Smatthias.ringwald // the STACK is here
763a9fb326S[email protected] #ifndef HAVE_MALLOC
773a9fb326S[email protected] static hci_stack_t   hci_stack_static;
783a9fb326S[email protected] #endif
793a9fb326S[email protected] static hci_stack_t * hci_stack = NULL;
8016833f0aSmatthias.ringwald 
8197addcc5Smatthias.ringwald /**
82ee091cf1Smatthias.ringwald  * get connection for a given handle
83ee091cf1Smatthias.ringwald  *
84ee091cf1Smatthias.ringwald  * @return connection OR NULL, if not found
85ee091cf1Smatthias.ringwald  */
865061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){
87ee091cf1Smatthias.ringwald     linked_item_t *it;
883a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
89ee091cf1Smatthias.ringwald         if ( ((hci_connection_t *) it)->con_handle == con_handle){
90ee091cf1Smatthias.ringwald             return (hci_connection_t *) it;
91ee091cf1Smatthias.ringwald         }
92ee091cf1Smatthias.ringwald     }
93ee091cf1Smatthias.ringwald     return NULL;
94ee091cf1Smatthias.ringwald }
95ee091cf1Smatthias.ringwald 
96*62bda3fbS[email protected] hci_connection_t * hci_connection_for_bd_addr(bd_addr_t * addr){
97*62bda3fbS[email protected]     linked_item_t *it;
98*62bda3fbS[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
99*62bda3fbS[email protected]         hci_connection_t * connection = (hci_connection_t *) it;
100*62bda3fbS[email protected]         if (memcmp(addr, connection->address, 6) == 0) {
101*62bda3fbS[email protected]             return connection;
102*62bda3fbS[email protected]         }
103*62bda3fbS[email protected]     }
104*62bda3fbS[email protected]     return NULL;
105*62bda3fbS[email protected] }
106*62bda3fbS[email protected] 
1072b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){
10828ca2b46S[email protected]     hci_connection_t * connection = (hci_connection_t *) linked_item_get_user(&timer->item);
109c785ef68Smatthias.ringwald #ifdef HAVE_TIME
110ee091cf1Smatthias.ringwald     struct timeval tv;
111ee091cf1Smatthias.ringwald     gettimeofday(&tv, NULL);
112c21e6239Smatthias.ringwald     if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) {
113ee091cf1Smatthias.ringwald         // connections might be timed out
114ee091cf1Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
115ee091cf1Smatthias.ringwald     }
1162b12a0b9Smatthias.ringwald #endif
117e5780900Smatthias.ringwald #ifdef HAVE_TICK
118c785ef68Smatthias.ringwald     if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){
119c785ef68Smatthias.ringwald         // connections might be timed out
120c785ef68Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
121c785ef68Smatthias.ringwald     }
122c785ef68Smatthias.ringwald #endif
123c785ef68Smatthias.ringwald     run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
124c785ef68Smatthias.ringwald     run_loop_add_timer(timer);
125c785ef68Smatthias.ringwald }
126ee091cf1Smatthias.ringwald 
127ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){
128c7492964Smatthias.ringwald #ifdef HAVE_TIME
129ee091cf1Smatthias.ringwald     gettimeofday(&connection->timestamp, NULL);
130c7492964Smatthias.ringwald #endif
131e5780900Smatthias.ringwald #ifdef HAVE_TICK
132c785ef68Smatthias.ringwald     connection->timestamp = embedded_get_ticks();
133c785ef68Smatthias.ringwald #endif
134ee091cf1Smatthias.ringwald }
135ee091cf1Smatthias.ringwald 
136ee091cf1Smatthias.ringwald /**
137c8e4258aSmatthias.ringwald  * create connection for given address
138c8e4258aSmatthias.ringwald  *
13917f1ba2aSmatthias.ringwald  * @return connection OR NULL, if no memory left
140c8e4258aSmatthias.ringwald  */
141c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){
14228ca2b46S[email protected]     hci_connection_t * conn = (hci_connection_t *) btstack_memory_hci_connection_get();
143c8e4258aSmatthias.ringwald     if (!conn) return NULL;
144c8e4258aSmatthias.ringwald     BD_ADDR_COPY(conn->address, addr);
145c8e4258aSmatthias.ringwald     conn->con_handle = 0xffff;
1467d3b3569Smatthias.ringwald     conn->authentication_flags = AUTH_FLAGS_NONE;
147afd4e962S[email protected]     conn->bonding_flags = 0;
14834d2123cS[email protected]     conn->requested_security_level = LEVEL_0;
149ee091cf1Smatthias.ringwald     linked_item_set_user(&conn->timeout.item, conn);
150ee091cf1Smatthias.ringwald     conn->timeout.process = hci_connection_timeout_handler;
151ee091cf1Smatthias.ringwald     hci_connection_timestamp(conn);
152d55db49eSmatthias.ringwald     conn->acl_recombination_length = 0;
1537856c818Smatthias.ringwald     conn->acl_recombination_pos = 0;
15456cf178bSmatthias.ringwald     conn->num_acl_packets_sent = 0;
1553a9fb326S[email protected]     linked_list_add(&hci_stack->connections, (linked_item_t *) conn);
156c8e4258aSmatthias.ringwald     return conn;
157c8e4258aSmatthias.ringwald }
158c8e4258aSmatthias.ringwald 
159c8e4258aSmatthias.ringwald /**
16006b35ec0Smatthias.ringwald  * get connection for given address
16197addcc5Smatthias.ringwald  *
16297addcc5Smatthias.ringwald  * @return connection OR NULL, if not found
16397addcc5Smatthias.ringwald  */
164fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){
16506b35ec0Smatthias.ringwald     linked_item_t *it;
1663a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
16706b35ec0Smatthias.ringwald         if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){
16806b35ec0Smatthias.ringwald             return (hci_connection_t *) it;
16906b35ec0Smatthias.ringwald         }
17006b35ec0Smatthias.ringwald     }
17106b35ec0Smatthias.ringwald     return NULL;
17206b35ec0Smatthias.ringwald }
17306b35ec0Smatthias.ringwald 
17428ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
17528ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags);
17628ca2b46S[email protected] }
17728ca2b46S[email protected] 
17828ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
17928ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags);
18028ca2b46S[email protected] }
18128ca2b46S[email protected] 
18228ca2b46S[email protected] 
18343bfb1bdSmatthias.ringwald /**
18480ca58a0Smatthias.ringwald  * add authentication flags and reset timer
1857fde4af9Smatthias.ringwald  */
1867fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){
1877fde4af9Smatthias.ringwald     bd_addr_t addr;
1887fde4af9Smatthias.ringwald     bt_flip_addr(addr, *(bd_addr_t *) bd_addr);
1897fde4af9Smatthias.ringwald     hci_connection_t * conn = connection_for_address(addr);
1907fde4af9Smatthias.ringwald     if (conn) {
19128ca2b46S[email protected]         connectionSetAuthenticationFlags(conn, flags);
19280ca58a0Smatthias.ringwald         hci_connection_timestamp(conn);
1937fde4af9Smatthias.ringwald     }
1947fde4af9Smatthias.ringwald }
1957fde4af9Smatthias.ringwald 
19680ca58a0Smatthias.ringwald int  hci_authentication_active_for_handle(hci_con_handle_t handle){
1975061f3afS[email protected]     hci_connection_t * conn = hci_connection_for_handle(handle);
19880ca58a0Smatthias.ringwald     if (!conn) return 0;
1996724cd9eS[email protected]     if (conn->authentication_flags & LEGACY_PAIRING_ACTIVE) return 1;
2006724cd9eS[email protected]     if (conn->authentication_flags & SSP_PAIRING_ACTIVE) return 1;
2016724cd9eS[email protected]     return 0;
20280ca58a0Smatthias.ringwald }
20380ca58a0Smatthias.ringwald 
204c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){
2053a9fb326S[email protected]     if (hci_stack->remote_device_db) {
2063a9fb326S[email protected]         hci_stack->remote_device_db->delete_link_key(addr);
207c12e46e7Smatthias.ringwald     }
208c12e46e7Smatthias.ringwald }
209c12e46e7Smatthias.ringwald 
2107fde4af9Smatthias.ringwald 
2117fde4af9Smatthias.ringwald /**
21243bfb1bdSmatthias.ringwald  * count connections
21343bfb1bdSmatthias.ringwald  */
21440d1c7a4Smatthias.ringwald static int nr_hci_connections(void){
21556c253c9Smatthias.ringwald     int count = 0;
21643bfb1bdSmatthias.ringwald     linked_item_t *it;
2173a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next, count++);
21843bfb1bdSmatthias.ringwald     return count;
21943bfb1bdSmatthias.ringwald }
220c8e4258aSmatthias.ringwald 
22197addcc5Smatthias.ringwald /**
222ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
22316833f0aSmatthias.ringwald  */
2242718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
22516833f0aSmatthias.ringwald }
22616833f0aSmatthias.ringwald 
227998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){
2285061f3afS[email protected]     hci_connection_t * connection = hci_connection_for_handle(handle);
229998906cdSmatthias.ringwald     if (!connection) {
2307d67539fSmatthias.ringwald         log_error("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle);
231998906cdSmatthias.ringwald         return 0;
232998906cdSmatthias.ringwald     }
233998906cdSmatthias.ringwald     return connection->num_acl_packets_sent;
234998906cdSmatthias.ringwald }
235998906cdSmatthias.ringwald 
236998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){
2373a9fb326S[email protected]     uint8_t free_slots = hci_stack->total_num_acl_packets;
238998906cdSmatthias.ringwald     linked_item_t *it;
2393a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
240998906cdSmatthias.ringwald         hci_connection_t * connection = (hci_connection_t *) it;
241998906cdSmatthias.ringwald         if (free_slots < connection->num_acl_packets_sent) {
2427d67539fSmatthias.ringwald             log_error("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n");
243998906cdSmatthias.ringwald             return 0;
244998906cdSmatthias.ringwald         }
245998906cdSmatthias.ringwald         free_slots -= connection->num_acl_packets_sent;
246998906cdSmatthias.ringwald     }
247998906cdSmatthias.ringwald     return free_slots;
248998906cdSmatthias.ringwald }
249998906cdSmatthias.ringwald 
250c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){
2512b12a0b9Smatthias.ringwald 
2522b12a0b9Smatthias.ringwald     // check for async hci transport implementations
2533a9fb326S[email protected]     if (hci_stack->hci_transport->can_send_packet_now){
2543a9fb326S[email protected]         if (!hci_stack->hci_transport->can_send_packet_now(packet_type)){
2552b12a0b9Smatthias.ringwald             return 0;
2562b12a0b9Smatthias.ringwald         }
2572b12a0b9Smatthias.ringwald     }
2582b12a0b9Smatthias.ringwald 
2592b12a0b9Smatthias.ringwald     // check regular Bluetooth flow control
260c24735b1Smatthias.ringwald     switch (packet_type) {
261c24735b1Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
262c24735b1Smatthias.ringwald             return hci_number_free_acl_slots();
263c24735b1Smatthias.ringwald         case HCI_COMMAND_DATA_PACKET:
2643a9fb326S[email protected]             return hci_stack->num_cmd_packets;
265c24735b1Smatthias.ringwald         default:
266c24735b1Smatthias.ringwald             return 0;
267c24735b1Smatthias.ringwald     }
268c24735b1Smatthias.ringwald }
269c24735b1Smatthias.ringwald 
270ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
2717856c818Smatthias.ringwald 
2726218e6f1Smatthias.ringwald     // check for free places on BT module
2735932f1b4Smatthias.ringwald     if (!hci_number_free_acl_slots()) return BTSTACK_ACL_BUFFERS_FULL;
2746218e6f1Smatthias.ringwald 
2757856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2765061f3afS[email protected]     hci_connection_t *connection = hci_connection_for_handle( con_handle);
27756cf178bSmatthias.ringwald     if (!connection) return 0;
27856cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
27956cf178bSmatthias.ringwald 
28056cf178bSmatthias.ringwald     // count packet
28156cf178bSmatthias.ringwald     connection->num_acl_packets_sent++;
2827b5fbe1fSmatthias.ringwald     // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent);
2837856c818Smatthias.ringwald 
28400d8e42eSmatthias.ringwald     // send packet
2853a9fb326S[email protected]     int err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
2866218e6f1Smatthias.ringwald 
28700d8e42eSmatthias.ringwald     return err;
288ee091cf1Smatthias.ringwald }
289ee091cf1Smatthias.ringwald 
29016833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
2917856c818Smatthias.ringwald 
292e76a89eeS[email protected]     // log_info("acl_handler: size %u", size);
293e76a89eeS[email protected] 
2947856c818Smatthias.ringwald     // get info
2957856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2965061f3afS[email protected]     hci_connection_t *conn      = hci_connection_for_handle(con_handle);
2977856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
2987856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
2997856c818Smatthias.ringwald 
3007856c818Smatthias.ringwald     // ignore non-registered handle
3017856c818Smatthias.ringwald     if (!conn){
3027d67539fSmatthias.ringwald         log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle);
3037856c818Smatthias.ringwald         return;
3047856c818Smatthias.ringwald     }
3057856c818Smatthias.ringwald 
306e76a89eeS[email protected]     // assert packet is complete
3079ecc3e17S[email protected]     if (acl_length + 4 != size){
308e76a89eeS[email protected]         log_error("hci.c: acl_handler called with ACL packet of wrong size %u, expected %u => dropping packet", size, acl_length + 4);
309e76a89eeS[email protected]         return;
310e76a89eeS[email protected]     }
311e76a89eeS[email protected] 
3127856c818Smatthias.ringwald     // update idle timestamp
3137856c818Smatthias.ringwald     hci_connection_timestamp(conn);
3147856c818Smatthias.ringwald 
3157856c818Smatthias.ringwald     // handle different packet types
3167856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
3177856c818Smatthias.ringwald 
3187856c818Smatthias.ringwald         case 0x01: // continuation fragment
3197856c818Smatthias.ringwald 
3207856c818Smatthias.ringwald             // sanity check
3217856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
3227d67539fSmatthias.ringwald                 log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle);
3237856c818Smatthias.ringwald                 return;
3247856c818Smatthias.ringwald             }
3257856c818Smatthias.ringwald 
3267856c818Smatthias.ringwald             // append fragment payload (header already stored)
3277856c818Smatthias.ringwald             memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
3287856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
3297856c818Smatthias.ringwald 
3307d67539fSmatthias.ringwald             // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length,
331decc01a8Smatthias.ringwald             //        conn->acl_recombination_pos, conn->acl_recombination_length);
3327856c818Smatthias.ringwald 
3337856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
3347856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
3357856c818Smatthias.ringwald 
3363a9fb326S[email protected]                 hci_stack->packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos);
3377856c818Smatthias.ringwald                 // reset recombination buffer
3387856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
3397856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
3407856c818Smatthias.ringwald             }
3417856c818Smatthias.ringwald             break;
3427856c818Smatthias.ringwald 
3437856c818Smatthias.ringwald         case 0x02: { // first fragment
3447856c818Smatthias.ringwald 
3457856c818Smatthias.ringwald             // sanity check
3467856c818Smatthias.ringwald             if (conn->acl_recombination_pos) {
3477d67539fSmatthias.ringwald                 log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
3487856c818Smatthias.ringwald                 return;
3497856c818Smatthias.ringwald             }
3507856c818Smatthias.ringwald 
3517856c818Smatthias.ringwald             // peek into L2CAP packet!
3527856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
3537856c818Smatthias.ringwald 
354e76a89eeS[email protected]             // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
355decc01a8Smatthias.ringwald 
3567856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
3577856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
3587856c818Smatthias.ringwald 
3597856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
3603a9fb326S[email protected]                 hci_stack->packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
3617856c818Smatthias.ringwald 
3627856c818Smatthias.ringwald             } else {
3637856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
3647856c818Smatthias.ringwald                 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
3657856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
3667856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
367decc01a8Smatthias.ringwald                 bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4);
3687856c818Smatthias.ringwald             }
3697856c818Smatthias.ringwald             break;
3707856c818Smatthias.ringwald 
3717856c818Smatthias.ringwald         }
3727856c818Smatthias.ringwald         default:
3737d67539fSmatthias.ringwald             log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03);
3747856c818Smatthias.ringwald             return;
3757856c818Smatthias.ringwald     }
37694ab26f8Smatthias.ringwald 
37794ab26f8Smatthias.ringwald     // execute main loop
37894ab26f8Smatthias.ringwald     hci_run();
37916833f0aSmatthias.ringwald }
38022909952Smatthias.ringwald 
38167a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){
382339b6768Smatthias.ringwald     log_info("Connection closed: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
3833c4d4b90Smatthias.ringwald 
3843c4d4b90Smatthias.ringwald     // cancel all l2cap connections
3853c4d4b90Smatthias.ringwald     hci_emit_disconnection_complete(conn->con_handle, 0x16);    // terminated by local host
3863c4d4b90Smatthias.ringwald 
387c7e0c5f6Smatthias.ringwald     run_loop_remove_timer(&conn->timeout);
388c785ef68Smatthias.ringwald 
3893a9fb326S[email protected]     linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
390a3b02b71Smatthias.ringwald     btstack_memory_hci_connection_free( conn );
3913c4d4b90Smatthias.ringwald 
3923c4d4b90Smatthias.ringwald     // now it's gone
393c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
394c7e0c5f6Smatthias.ringwald }
395c7e0c5f6Smatthias.ringwald 
3960c042179S[email protected] static const uint16_t packet_type_sizes[] = {
3978f8108aaSmatthias.ringwald     0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE,
3988f8108aaSmatthias.ringwald     HCI_ACL_DH1_SIZE, 0, 0, 0,
3998f8108aaSmatthias.ringwald     HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE,
4008f8108aaSmatthias.ringwald     HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE
4018f8108aaSmatthias.ringwald };
40265389bfcS[email protected] static const uint8_t  packet_type_feature_requirement_bit[] = {
40365389bfcS[email protected]      0, // 3 slot packets
40465389bfcS[email protected]      1, // 5 slot packets
40565389bfcS[email protected]     25, // EDR 2 mpbs
40665389bfcS[email protected]     26, // EDR 3 mbps
40765389bfcS[email protected]     39, // 3 slot EDR packts
40865389bfcS[email protected]     40, // 5 slot EDR packet
40965389bfcS[email protected] };
41065389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = {
41165389bfcS[email protected]     0x0f00, // 3 slot packets
41265389bfcS[email protected]     0xf000, // 5 slot packets
41365389bfcS[email protected]     0x1102, // EDR 2 mpbs
41465389bfcS[email protected]     0x2204, // EDR 3 mbps
41565389bfcS[email protected]     0x0300, // 3 slot EDR packts
41665389bfcS[email protected]     0x3000, // 5 slot EDR packet
41765389bfcS[email protected] };
4188f8108aaSmatthias.ringwald 
41965389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){
42065389bfcS[email protected]     // enable packet types based on size
4218f8108aaSmatthias.ringwald     uint16_t packet_types = 0;
4228f8108aaSmatthias.ringwald     int i;
4238f8108aaSmatthias.ringwald     for (i=0;i<16;i++){
4248f8108aaSmatthias.ringwald         if (packet_type_sizes[i] == 0) continue;
4258f8108aaSmatthias.ringwald         if (packet_type_sizes[i] <= buffer_size){
4268f8108aaSmatthias.ringwald             packet_types |= 1 << i;
4278f8108aaSmatthias.ringwald         }
4288f8108aaSmatthias.ringwald     }
42965389bfcS[email protected]     // disable packet types due to missing local supported features
43065389bfcS[email protected]     for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){
43165389bfcS[email protected]         int bit_idx = packet_type_feature_requirement_bit[i];
43265389bfcS[email protected]         int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0;
43365389bfcS[email protected]         if (feature_set) continue;
43465389bfcS[email protected]         log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]);
43565389bfcS[email protected]         packet_types &= ~packet_type_feature_packet_mask[i];
43665389bfcS[email protected]     }
4378f8108aaSmatthias.ringwald     // flip bits for "may not be used"
4388f8108aaSmatthias.ringwald     packet_types ^= 0x3306;
4398f8108aaSmatthias.ringwald     return packet_types;
4408f8108aaSmatthias.ringwald }
4418f8108aaSmatthias.ringwald 
4428f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){
4433a9fb326S[email protected]     return hci_stack->packet_types;
4448f8108aaSmatthias.ringwald }
4458f8108aaSmatthias.ringwald 
4467dc17943Smatthias.ringwald uint8_t* hci_get_outgoing_acl_packet_buffer(void){
4477dc17943Smatthias.ringwald     // hci packet buffer is >= acl data packet length
4483a9fb326S[email protected]     return hci_stack->hci_packet_buffer;
4497dc17943Smatthias.ringwald }
4507dc17943Smatthias.ringwald 
451f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){
4523a9fb326S[email protected]     return hci_stack->acl_data_packet_length;
4537dc17943Smatthias.ringwald }
4547dc17943Smatthias.ringwald 
455f5d8d141S[email protected] int hci_ssp_supported(void){
456f5d8d141S[email protected]     // No 51, byte 6, bit 3
4573a9fb326S[email protected]     return (hci_stack->local_supported_features[6] & (1 << 3)) != 0;
458f5d8d141S[email protected] }
459f5d8d141S[email protected] 
460f5d8d141S[email protected] int hci_classic_supported(void){
461f5d8d141S[email protected]     // No 37, byte 4, bit 5, = No BR/EDR Support
4623a9fb326S[email protected]     return (hci_stack->local_supported_features[4] & (1 << 5)) == 0;
463f5d8d141S[email protected] }
464f5d8d141S[email protected] 
465f5d8d141S[email protected] int hci_le_supported(void){
466f5d8d141S[email protected]     // No 37, byte 4, bit 6 = LE Supported (Controller)
467f5d8d141S[email protected] #ifdef HAVE_BLE
4683a9fb326S[email protected]     return (hci_stack->local_supported_features[4] & (1 << 6)) != 0;
469f5d8d141S[email protected] #else
470f5d8d141S[email protected]     return 0;
471f5d8d141S[email protected] #endif
472f5d8d141S[email protected] }
473f5d8d141S[email protected] 
47469a97523S[email protected] // get addr type and address used in advertisement packets
47518904abdS[email protected] void hci_le_advertisement_address(uint8_t * addr_type, bd_addr_t * addr){
4763a9fb326S[email protected]     *addr_type = hci_stack->adv_addr_type;
4773a9fb326S[email protected]     if (hci_stack->adv_addr_type){
4783a9fb326S[email protected]         memcpy(addr, hci_stack->adv_address, 6);
47969a97523S[email protected]     } else {
4803a9fb326S[email protected]         memcpy(addr, hci_stack->local_bd_addr, 6);
48169a97523S[email protected]     }
48269a97523S[email protected] }
48369a97523S[email protected] 
48474ec757aSmatthias.ringwald // avoid huge local variables
485223aafc1Smatthias.ringwald #ifndef EMBEDDED
48674ec757aSmatthias.ringwald static device_name_t device_name;
487223aafc1Smatthias.ringwald #endif
48816833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
489e76a89eeS[email protected] 
490e76a89eeS[email protected]     uint16_t event_length = packet[1];
491e76a89eeS[email protected] 
492e76a89eeS[email protected]     // assert packet is complete
493e76a89eeS[email protected]     if (size != event_length + 2){
494e76a89eeS[email protected]         log_error("hci.c: event_handler called with event packet of wrong size %u, expected %u => dropping packet", size, event_length + 2);
495e76a89eeS[email protected]         return;
496e76a89eeS[email protected]     }
497e76a89eeS[email protected] 
4981281a47eSmatthias.ringwald     bd_addr_t addr;
4992d00edd4Smatthias.ringwald     uint8_t link_type;
500fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
5011f7b95a1Smatthias.ringwald     hci_connection_t * conn;
50256cf178bSmatthias.ringwald     int i;
50322909952Smatthias.ringwald 
5045909f7f2Smatthias.ringwald     // printf("HCI:EVENT:%02x\n", packet[0]);
5055909f7f2Smatthias.ringwald 
5066772a24cSmatthias.ringwald     switch (packet[0]) {
50722909952Smatthias.ringwald 
5086772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
5097ec5eeaaSmatthias.ringwald             // get num cmd packets
5103a9fb326S[email protected]             // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack->num_cmd_packets, packet[2]);
5113a9fb326S[email protected]             hci_stack->num_cmd_packets = packet[2];
5127ec5eeaaSmatthias.ringwald 
513e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
514e2edc0c3Smatthias.ringwald                 // from offset 5
515e2edc0c3Smatthias.ringwald                 // status
5161d279b20Smatthias.ringwald                 // "The HC_ACL_Data_Packet_Length return parameter will be used to determine the size of the L2CAP segments contained in ACL Data Packets"
5173a9fb326S[email protected]                 hci_stack->acl_data_packet_length = READ_BT_16(packet, 6);
51856cf178bSmatthias.ringwald                 // ignore: SCO data packet len (8)
5193a9fb326S[email protected]                 hci_stack->total_num_acl_packets  = packet[9];
52056cf178bSmatthias.ringwald                 // ignore: total num SCO packets
5213a9fb326S[email protected]                 if (hci_stack->state == HCI_STATE_INITIALIZING){
522a7a04bd9Smatthias.ringwald                     // determine usable ACL payload size
5233a9fb326S[email protected]                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack->acl_data_packet_length){
5243a9fb326S[email protected]                         hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
5258f8108aaSmatthias.ringwald                     }
52665389bfcS[email protected]                     log_info("hci_read_buffer_size: used size %u, count %u\n",
5273a9fb326S[email protected]                              hci_stack->acl_data_packet_length, hci_stack->total_num_acl_packets);
528e2edc0c3Smatthias.ringwald                 }
52956cf178bSmatthias.ringwald             }
53065a46ef3S[email protected] #ifdef HAVE_BLE
53165a46ef3S[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)){
5323a9fb326S[email protected]                 hci_stack->le_data_packet_length = READ_BT_16(packet, 6);
5333a9fb326S[email protected]                 hci_stack->total_num_le_packets  = packet[8];
5343a9fb326S[email protected]                 log_info("hci_le_read_buffer_size: size %u, count %u\n", hci_stack->le_data_packet_length, hci_stack->total_num_le_packets);
53565a46ef3S[email protected]             }
53665a46ef3S[email protected] #endif
537188981d2Smatthias.ringwald             // Dump local address
538188981d2Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) {
5393a9fb326S[email protected]                 bt_flip_addr(hci_stack->local_bd_addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]);
540188981d2Smatthias.ringwald                 log_info("Local Address, Status: 0x%02x: Addr: %s\n",
5413a9fb326S[email protected]                     packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr));
542188981d2Smatthias.ringwald             }
543381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
5443a9fb326S[email protected]                 hci_emit_discoverable_enabled(hci_stack->discoverable);
545381fbed8Smatthias.ringwald             }
54665389bfcS[email protected]             // Note: HCI init checks
547559e517eS[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){
5483a9fb326S[email protected]                 memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8);
549559e517eS[email protected]                 log_info("Local Supported Features: 0x%02x%02x%02x%02x%02x%02x%02x%02x",
5503a9fb326S[email protected]                     hci_stack->local_supported_features[0], hci_stack->local_supported_features[1],
5513a9fb326S[email protected]                     hci_stack->local_supported_features[2], hci_stack->local_supported_features[3],
5523a9fb326S[email protected]                     hci_stack->local_supported_features[4], hci_stack->local_supported_features[5],
5533a9fb326S[email protected]                     hci_stack->local_supported_features[6], hci_stack->local_supported_features[7]);
55465389bfcS[email protected] 
55565389bfcS[email protected]                 // determine usable ACL packet types based buffer size and supported features
5563a9fb326S[email protected]                 hci_stack->packet_types = hci_acl_packet_types_for_buffer_size_and_local_features(hci_stack->acl_data_packet_length, &hci_stack->local_supported_features[0]);
5573a9fb326S[email protected]                 log_info("packet types %04x", hci_stack->packet_types);
558f5d8d141S[email protected] 
559f5d8d141S[email protected]                 // Classic/LE
560f5d8d141S[email protected]                 log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported());
561559e517eS[email protected]             }
56256cf178bSmatthias.ringwald             break;
56356cf178bSmatthias.ringwald 
5647ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
5657ec5eeaaSmatthias.ringwald             // get num cmd packets
5663a9fb326S[email protected]             // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack->num_cmd_packets, packet[3]);
5673a9fb326S[email protected]             hci_stack->num_cmd_packets = packet[3];
5687ec5eeaaSmatthias.ringwald             break;
5697ec5eeaaSmatthias.ringwald 
57056cf178bSmatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
57156cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
57256cf178bSmatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
57356cf178bSmatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
5745061f3afS[email protected]                 conn = hci_connection_for_handle(handle);
57556cf178bSmatthias.ringwald                 if (!conn){
5767d67539fSmatthias.ringwald                     log_error("hci_number_completed_packet lists unused con handle %u\n", handle);
57756cf178bSmatthias.ringwald                     continue;
57856cf178bSmatthias.ringwald                 }
57956cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
5807b5fbe1fSmatthias.ringwald                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
58156cf178bSmatthias.ringwald             }
5826772a24cSmatthias.ringwald             break;
5836772a24cSmatthias.ringwald 
5841f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
58537eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
58637eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
5872d00edd4Smatthias.ringwald             link_type = packet[11];
588339b6768Smatthias.ringwald             log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type);
58937eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
5901f7b95a1Smatthias.ringwald                 conn = connection_for_address(addr);
5911f7b95a1Smatthias.ringwald                 if (!conn) {
5921f7b95a1Smatthias.ringwald                     conn = create_connection_for_addr(addr);
5931f7b95a1Smatthias.ringwald                 }
594ce4c8fabSmatthias.ringwald                 if (!conn) {
595ce4c8fabSmatthias.ringwald                     // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
5963a9fb326S[email protected]                     hci_stack->decline_reason = 0x0d;
5973a9fb326S[email protected]                     BD_ADDR_COPY(hci_stack->decline_addr, addr);
598ce4c8fabSmatthias.ringwald                     break;
599ce4c8fabSmatthias.ringwald                 }
60032ab9390Smatthias.ringwald                 conn->state = RECEIVED_CONNECTION_REQUEST;
60132ab9390Smatthias.ringwald                 hci_run();
60237eaa4cfSmatthias.ringwald             } else {
603ce4c8fabSmatthias.ringwald                 // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A)
6043a9fb326S[email protected]                 hci_stack->decline_reason = 0x0a;
6053a9fb326S[email protected]                 BD_ADDR_COPY(hci_stack->decline_addr, addr);
60637eaa4cfSmatthias.ringwald             }
6071f7b95a1Smatthias.ringwald             break;
6081f7b95a1Smatthias.ringwald 
6096772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
610fe1ed1b8Smatthias.ringwald             // Connection management
611fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
612339b6768Smatthias.ringwald             log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr));
6131f7b95a1Smatthias.ringwald             conn = connection_for_address(addr);
614fe1ed1b8Smatthias.ringwald             if (conn) {
615b448a0e7Smatthias.ringwald                 if (!packet[2]){
616c8e4258aSmatthias.ringwald                     conn->state = OPEN;
617fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
618ad83dc6aS[email protected]                     conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES;
619ee091cf1Smatthias.ringwald 
620c785ef68Smatthias.ringwald                     // restart timer
621c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
622ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
623c785ef68Smatthias.ringwald 
624339b6768Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
62543bfb1bdSmatthias.ringwald 
62643bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
627b448a0e7Smatthias.ringwald                 } else {
628ad83dc6aS[email protected]                     // notify client if dedicated bonding
629ad83dc6aS[email protected]                     if (conn->bonding_flags & BONDING_DEDICATED){
630ad83dc6aS[email protected]                         hci_emit_dedicated_bonding_result(conn, packet[2]);
631ad83dc6aS[email protected]                     }
632ad83dc6aS[email protected] 
633b448a0e7Smatthias.ringwald                     // connection failed, remove entry
6343a9fb326S[email protected]                     linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
635a3b02b71Smatthias.ringwald                     btstack_memory_hci_connection_free( conn );
636c12e46e7Smatthias.ringwald 
637c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
638c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
639c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
640c12e46e7Smatthias.ringwald                     }
641fe1ed1b8Smatthias.ringwald                 }
642fe1ed1b8Smatthias.ringwald             }
6436772a24cSmatthias.ringwald             break;
644fe1ed1b8Smatthias.ringwald 
645afd4e962S[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
646afd4e962S[email protected]             handle = READ_BT_16(packet, 3);
647afd4e962S[email protected]             conn = hci_connection_for_handle(handle);
648afd4e962S[email protected]             if (!conn) break;
649afd4e962S[email protected]             if (!packet[2]){
650afd4e962S[email protected]                 uint8_t * features = &packet[5];
651afd4e962S[email protected]                 if (features[6] & (1 << 3)){
652afd4e962S[email protected]                     conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP;
653afd4e962S[email protected]                 }
654afd4e962S[email protected]             }
655afd4e962S[email protected]             conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES;
656ad83dc6aS[email protected]             log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x", conn->bonding_flags);
657ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
658ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
659ad83dc6aS[email protected]             }
660afd4e962S[email protected]             break;
661afd4e962S[email protected] 
6627fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
6637b5fbe1fSmatthias.ringwald             log_info("HCI_EVENT_LINK_KEY_REQUEST\n");
6647fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
66574d716b5S[email protected]             // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST
6663a9fb326S[email protected]             if (hci_stack->bondable && !hci_stack->remote_device_db) break;
66732ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
66864472d52Smatthias.ringwald             hci_run();
669d9a327f9Smatthias.ringwald             // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set
67029d53098Smatthias.ringwald             return;
6717fde4af9Smatthias.ringwald 
6729ab95c90S[email protected]         case HCI_EVENT_LINK_KEY_NOTIFICATION: {
67329d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
6749ab95c90S[email protected]             conn = connection_for_address(addr);
6759ab95c90S[email protected]             if (!conn) break;
6769ab95c90S[email protected]             conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION;
6779ab95c90S[email protected]             link_key_type_t link_key_type = packet[24];
6789ab95c90S[email protected]             // Change Connection Encryption keeps link key type
6799ab95c90S[email protected]             if (link_key_type != CHANGED_COMBINATION_KEY){
6809ab95c90S[email protected]                 conn->link_key_type = link_key_type;
6819ab95c90S[email protected]             }
6823a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
6833a9fb326S[email protected]             hci_stack->remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8], conn->link_key_type);
68429d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
6857fde4af9Smatthias.ringwald             break;
6869ab95c90S[email protected]         }
6877fde4af9Smatthias.ringwald 
6887fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
6896724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE);
6904c57c146S[email protected]             // non-bondable mode: pin code negative reply will be sent
6913a9fb326S[email protected]             if (!hci_stack->bondable){
692899283eaS[email protected]                 hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST);
693f8fb5f6eS[email protected]                 hci_run();
694f8fb5f6eS[email protected]                 return;
6954c57c146S[email protected]             }
696d9a327f9Smatthias.ringwald             // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key
6973a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
698d9a327f9Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
6993a9fb326S[email protected]             hci_stack->remote_device_db->delete_link_key(&addr);
7007fde4af9Smatthias.ringwald             break;
7017fde4af9Smatthias.ringwald 
7021d6b20aeS[email protected]         case HCI_EVENT_IO_CAPABILITY_REQUEST:
7031d6b20aeS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST);
704dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY);
705dbe1a790S[email protected]             break;
706dbe1a790S[email protected] 
707dbe1a790S[email protected]         case HCI_EVENT_USER_CONFIRMATION_REQUEST:
7086724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
7093a9fb326S[email protected]             if (!hci_stack->ssp_auto_accept) break;
710dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY);
711dbe1a790S[email protected]             break;
712dbe1a790S[email protected] 
713dbe1a790S[email protected]         case HCI_EVENT_USER_PASSKEY_REQUEST:
7146724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
7153a9fb326S[email protected]             if (!hci_stack->ssp_auto_accept) break;
716dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY);
7171d6b20aeS[email protected]             break;
7181d6b20aeS[email protected] 
719f0944df2S[email protected]         case HCI_EVENT_ENCRYPTION_CHANGE:
720f0944df2S[email protected]             handle = READ_BT_16(packet, 3);
721f0944df2S[email protected]             conn = hci_connection_for_handle(handle);
722f0944df2S[email protected]             if (!conn) break;
723ad83dc6aS[email protected]             if (packet[2] == 0) {
724f0944df2S[email protected]                 if (packet[5]){
725f0944df2S[email protected]                     conn->authentication_flags |= CONNECTION_ENCRYPTED;
726f0944df2S[email protected]                 } else {
727536f9994S[email protected]                     conn->authentication_flags &= ~CONNECTION_ENCRYPTED;
728f0944df2S[email protected]                 }
729ad83dc6aS[email protected]             }
730a00031e2S[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
731f0944df2S[email protected]             break;
732f0944df2S[email protected] 
7331eb2563eS[email protected]         case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT:
7341eb2563eS[email protected]             handle = READ_BT_16(packet, 3);
7351eb2563eS[email protected]             conn = hci_connection_for_handle(handle);
7361eb2563eS[email protected]             if (!conn) break;
737ad83dc6aS[email protected] 
738ad83dc6aS[email protected]             // dedicated bonding: send result and disconnect
739ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
740ad83dc6aS[email protected]                 conn->bonding_flags &= ~BONDING_DEDICATED;
741ad83dc6aS[email protected]                 hci_emit_dedicated_bonding_result( conn, packet[2]);
742ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE;
743ad83dc6aS[email protected]                 break;
744ad83dc6aS[email protected]             }
745ad83dc6aS[email protected] 
746b5cb6874S[email protected]             if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){
7471eb2563eS[email protected]                 // link key sufficient for requested security
7481eb2563eS[email protected]                 conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
749ad83dc6aS[email protected]                 break;
750ad83dc6aS[email protected]             }
7511eb2563eS[email protected]             // not enough
7521eb2563eS[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
7531eb2563eS[email protected]             break;
75434d2123cS[email protected] 
755223aafc1Smatthias.ringwald #ifndef EMBEDDED
75674ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
7573a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
75874ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
75974ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
7605a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
7615a06394aSmatthias.ringwald             for (i=0; i<248;i++){
7625a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
7635a06394aSmatthias.ringwald                     packet[9+i] = 0;
7645a06394aSmatthias.ringwald                     break;
765cdc9101dSmatthias.ringwald                 }
7665a06394aSmatthias.ringwald             }
767d2fe945cS[email protected]             memset(&device_name, 0, sizeof(device_name_t));
76874ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
7693a9fb326S[email protected]             hci_stack->remote_device_db->put_name(&addr, &device_name);
77074ec757aSmatthias.ringwald             break;
77174ec757aSmatthias.ringwald 
77274ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
77374ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
7743a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
77574ec757aSmatthias.ringwald             // first send inq result packet
7763a9fb326S[email protected]             hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size);
77774ec757aSmatthias.ringwald             // then send cached remote names
77874ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
77974ec757aSmatthias.ringwald                 bt_flip_addr(addr, &packet[3+i*6]);
7803a9fb326S[email protected]                 if (hci_stack->remote_device_db->get_name(&addr, &device_name)){
78174ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
78274ec757aSmatthias.ringwald                 }
78374ec757aSmatthias.ringwald             }
78474ec757aSmatthias.ringwald             return;
785223aafc1Smatthias.ringwald #endif
78674ec757aSmatthias.ringwald 
7876772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
788fe1ed1b8Smatthias.ringwald             if (!packet[2]){
789fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
7905061f3afS[email protected]                 hci_connection_t * conn = hci_connection_for_handle(handle);
791fe1ed1b8Smatthias.ringwald                 if (conn) {
792c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
793fe1ed1b8Smatthias.ringwald                 }
794fe1ed1b8Smatthias.ringwald             }
7956772a24cSmatthias.ringwald             break;
7966772a24cSmatthias.ringwald 
797c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
7983a9fb326S[email protected]             if(hci_stack->control && hci_stack->control->hw_error){
7993a9fb326S[email protected]                 (*hci_stack->control->hw_error)();
800c68bdf90Smatthias.ringwald             }
801c68bdf90Smatthias.ringwald             break;
802c68bdf90Smatthias.ringwald 
8035909f7f2Smatthias.ringwald #ifdef HAVE_BLE
8045909f7f2Smatthias.ringwald         case HCI_EVENT_LE_META:
8055909f7f2Smatthias.ringwald             switch (packet[2]) {
8065909f7f2Smatthias.ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
8075909f7f2Smatthias.ringwald                     // Connection management
8085909f7f2Smatthias.ringwald                     bt_flip_addr(addr, &packet[8]);
8095909f7f2Smatthias.ringwald                     log_info("LE Connection_complete (status=%u) %s\n", packet[3], bd_addr_to_str(addr));
8105909f7f2Smatthias.ringwald                     // LE connections are auto-accepted, so just create a connection if there isn't one already
8115909f7f2Smatthias.ringwald                     conn = connection_for_address(addr);
8125909f7f2Smatthias.ringwald                     if (packet[3]){
8135909f7f2Smatthias.ringwald                         if (conn){
8145909f7f2Smatthias.ringwald                             // outgoing connection failed, remove entry
8153a9fb326S[email protected]                             linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
8165909f7f2Smatthias.ringwald                             btstack_memory_hci_connection_free( conn );
8175909f7f2Smatthias.ringwald 
8185909f7f2Smatthias.ringwald                         }
8195909f7f2Smatthias.ringwald                         // if authentication error, also delete link key
8205909f7f2Smatthias.ringwald                         if (packet[3] == 0x05) {
8215909f7f2Smatthias.ringwald                             hci_drop_link_key_for_bd_addr(&addr);
8225909f7f2Smatthias.ringwald                         }
8235909f7f2Smatthias.ringwald                         break;
8245909f7f2Smatthias.ringwald                     }
8255909f7f2Smatthias.ringwald                     if (!conn){
8265909f7f2Smatthias.ringwald                         conn = create_connection_for_addr(addr);
8275909f7f2Smatthias.ringwald                     }
8285909f7f2Smatthias.ringwald                     if (!conn){
8295909f7f2Smatthias.ringwald                         // no memory
8305909f7f2Smatthias.ringwald                         break;
8315909f7f2Smatthias.ringwald                     }
8325909f7f2Smatthias.ringwald 
8335909f7f2Smatthias.ringwald                     conn->state = OPEN;
8345909f7f2Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 4);
8355909f7f2Smatthias.ringwald 
8365909f7f2Smatthias.ringwald                     // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock
8375909f7f2Smatthias.ringwald 
8385909f7f2Smatthias.ringwald                     // restart timer
8395909f7f2Smatthias.ringwald                     // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
8405909f7f2Smatthias.ringwald                     // run_loop_add_timer(&conn->timeout);
8415909f7f2Smatthias.ringwald 
8425909f7f2Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
8435909f7f2Smatthias.ringwald 
8445909f7f2Smatthias.ringwald                     hci_emit_nr_connections_changed();
8455909f7f2Smatthias.ringwald                     break;
8465909f7f2Smatthias.ringwald 
84765a46ef3S[email protected]             // printf("LE buffer size: %u, count %u\n", READ_BT_16(packet,6), packet[8]);
84865a46ef3S[email protected] 
8495909f7f2Smatthias.ringwald                 default:
8505909f7f2Smatthias.ringwald                     break;
8515909f7f2Smatthias.ringwald             }
8525909f7f2Smatthias.ringwald             break;
8535909f7f2Smatthias.ringwald #endif
8545909f7f2Smatthias.ringwald 
8556772a24cSmatthias.ringwald         default:
8566772a24cSmatthias.ringwald             break;
857fe1ed1b8Smatthias.ringwald     }
858fe1ed1b8Smatthias.ringwald 
8593429f56bSmatthias.ringwald     // handle BT initialization
8603a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_INITIALIZING){
8613a9fb326S[email protected]         if (hci_stack->substate % 2){
8623429f56bSmatthias.ringwald             // odd: waiting for event
863f155fca3Smatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || packet[0] == HCI_EVENT_COMMAND_STATUS){
864c9af4d3fS[email protected]                 // wait for explicit COMMAND COMPLETE on RESET
8653a9fb326S[email protected]                 if (hci_stack->substate > 1 || COMMAND_COMPLETE_EVENT(packet, hci_reset)) {
8663a9fb326S[email protected]                     hci_stack->substate++;
8673429f56bSmatthias.ringwald                 }
8683429f56bSmatthias.ringwald             }
86922909952Smatthias.ringwald         }
870c9af4d3fS[email protected]     }
87122909952Smatthias.ringwald 
87289db417bSmatthias.ringwald     // help with BT sleep
8733a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_FALLING_ASLEEP
8743a9fb326S[email protected]         && hci_stack->substate == 1
87589db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
8763a9fb326S[email protected]         hci_stack->substate++;
87789db417bSmatthias.ringwald     }
87889db417bSmatthias.ringwald 
8793a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size);
88094ab26f8Smatthias.ringwald 
88194ab26f8Smatthias.ringwald 	// execute main loop
88294ab26f8Smatthias.ringwald 	hci_run();
88316833f0aSmatthias.ringwald }
88416833f0aSmatthias.ringwald 
8850a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
88610e830c9Smatthias.ringwald     switch (packet_type) {
88710e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
88810e830c9Smatthias.ringwald             event_handler(packet, size);
88910e830c9Smatthias.ringwald             break;
89010e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
89110e830c9Smatthias.ringwald             acl_handler(packet, size);
89210e830c9Smatthias.ringwald             break;
89310e830c9Smatthias.ringwald         default:
89410e830c9Smatthias.ringwald             break;
89510e830c9Smatthias.ringwald     }
89610e830c9Smatthias.ringwald }
89710e830c9Smatthias.ringwald 
898fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
8992718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
9003a9fb326S[email protected]     hci_stack->packet_handler = handler;
90116833f0aSmatthias.ringwald }
90216833f0aSmatthias.ringwald 
9034f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){
904475c8125Smatthias.ringwald 
9053a9fb326S[email protected] #ifdef HAVE_MALLOC
9063a9fb326S[email protected]     if (!hci_stack) {
9073a9fb326S[email protected]         hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t));
9083a9fb326S[email protected]     }
9093a9fb326S[email protected] #else
9103a9fb326S[email protected]     hci_stack = &hci_stack_static;
9113a9fb326S[email protected] #endif
9123a9fb326S[email protected] 
913475c8125Smatthias.ringwald     // reference to use transport layer implementation
9143a9fb326S[email protected]     hci_stack->hci_transport = transport;
915475c8125Smatthias.ringwald 
91611e23e5fSmatthias.ringwald     // references to used control implementation
9173a9fb326S[email protected]     hci_stack->control = control;
91811e23e5fSmatthias.ringwald 
91911e23e5fSmatthias.ringwald     // reference to used config
9203a9fb326S[email protected]     hci_stack->config = config;
92111e23e5fSmatthias.ringwald 
922fe1ed1b8Smatthias.ringwald     // no connections yet
9233a9fb326S[email protected]     hci_stack->connections = NULL;
9243a9fb326S[email protected]     hci_stack->discoverable = 0;
9253a9fb326S[email protected]     hci_stack->connectable = 0;
9263a9fb326S[email protected]     hci_stack->bondable = 1;
927758b46ceSmatthias.ringwald 
928b031bebbSmatthias.ringwald     // no pending cmds
9293a9fb326S[email protected]     hci_stack->decline_reason = 0;
9303a9fb326S[email protected]     hci_stack->new_scan_enable_value = 0xff;
931b031bebbSmatthias.ringwald 
93216833f0aSmatthias.ringwald     // higher level handler
9333a9fb326S[email protected]     hci_stack->packet_handler = dummy_handler;
93416833f0aSmatthias.ringwald 
935404843c1Smatthias.ringwald     // store and open remote device db
9363a9fb326S[email protected]     hci_stack->remote_device_db = remote_device_db;
9373a9fb326S[email protected]     if (hci_stack->remote_device_db) {
9383a9fb326S[email protected]         hci_stack->remote_device_db->open();
939404843c1Smatthias.ringwald     }
94029d53098Smatthias.ringwald 
9418fcba05dSmatthias.ringwald     // max acl payload size defined in config.h
9423a9fb326S[email protected]     hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
9438fcba05dSmatthias.ringwald 
94416833f0aSmatthias.ringwald     // register packet handlers with transport
94510e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
946f5454fc6Smatthias.ringwald 
9473a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
948e2386ba1S[email protected] 
949e2386ba1S[email protected]     // class of device
9503a9fb326S[email protected]     hci_stack->class_of_device = 0x007a020c; // Smartphone
951a45d6b9fS[email protected] 
95263048403S[email protected]     // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept
9533a9fb326S[email protected]     hci_stack->ssp_enable = 1;
9543a9fb326S[email protected]     hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT;
9553a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING;
9563a9fb326S[email protected]     hci_stack->ssp_auto_accept = 1;
95769a97523S[email protected] 
95869a97523S[email protected]     // LE
9593a9fb326S[email protected]     hci_stack->adv_addr_type = 0;
9603a9fb326S[email protected]     memset(hci_stack->adv_address, 0, 6);
961475c8125Smatthias.ringwald }
962475c8125Smatthias.ringwald 
963404843c1Smatthias.ringwald void hci_close(){
964404843c1Smatthias.ringwald     // close remote device db
9653a9fb326S[email protected]     if (hci_stack->remote_device_db) {
9663a9fb326S[email protected]         hci_stack->remote_device_db->close();
967404843c1Smatthias.ringwald     }
9683a9fb326S[email protected]     while (hci_stack->connections) {
9693a9fb326S[email protected]         hci_shutdown_connection((hci_connection_t *) hci_stack->connections);
970f5454fc6Smatthias.ringwald     }
971f5454fc6Smatthias.ringwald     hci_power_control(HCI_POWER_OFF);
9723a9fb326S[email protected] 
9733a9fb326S[email protected] #ifdef HAVE_MALLOC
9743a9fb326S[email protected]     free(hci_stack);
9753a9fb326S[email protected] #endif
9763a9fb326S[email protected]     hci_stack = NULL;
977404843c1Smatthias.ringwald }
978404843c1Smatthias.ringwald 
9799e61646fS[email protected] void hci_set_class_of_device(uint32_t class_of_device){
9809e61646fS[email protected]     hci_stack->class_of_device = class_of_device;
9819e61646fS[email protected] }
9829e61646fS[email protected] 
9838d213e1aSmatthias.ringwald // State-Module-Driver overview
9848d213e1aSmatthias.ringwald // state                    module  low-level
9858d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
9868d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
9878d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
9888d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
989d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
990d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
991c7e0c5f6Smatthias.ringwald 
99240d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
9937301ad89Smatthias.ringwald 
994038bc64cSmatthias.ringwald     // power on
995f9a30166Smatthias.ringwald     int err = 0;
9963a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->on){
9973a9fb326S[email protected]         err = (*hci_stack->control->on)(hci_stack->config);
998f9a30166Smatthias.ringwald     }
999038bc64cSmatthias.ringwald     if (err){
10007d67539fSmatthias.ringwald         log_error( "POWER_ON failed\n");
1001038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
1002038bc64cSmatthias.ringwald         return err;
1003038bc64cSmatthias.ringwald     }
1004038bc64cSmatthias.ringwald 
1005038bc64cSmatthias.ringwald     // open low-level device
10063a9fb326S[email protected]     err = hci_stack->hci_transport->open(hci_stack->config);
1007038bc64cSmatthias.ringwald     if (err){
10087d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
10093a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
10103a9fb326S[email protected]             (*hci_stack->control->off)(hci_stack->config);
1011f9a30166Smatthias.ringwald         }
1012038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
1013038bc64cSmatthias.ringwald         return err;
1014038bc64cSmatthias.ringwald     }
10158d213e1aSmatthias.ringwald     return 0;
10168d213e1aSmatthias.ringwald }
1017038bc64cSmatthias.ringwald 
101840d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
10198d213e1aSmatthias.ringwald 
10207b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off\n");
10219418f9c9Smatthias.ringwald 
10228d213e1aSmatthias.ringwald     // close low-level device
10233a9fb326S[email protected]     hci_stack->hci_transport->close(hci_stack->config);
10248d213e1aSmatthias.ringwald 
10257b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - hci_transport closed\n");
10269418f9c9Smatthias.ringwald 
10278d213e1aSmatthias.ringwald     // power off
10283a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->off){
10293a9fb326S[email protected]         (*hci_stack->control->off)(hci_stack->config);
10308d213e1aSmatthias.ringwald     }
10319418f9c9Smatthias.ringwald 
10327b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - control closed\n");
10339418f9c9Smatthias.ringwald 
10343a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
103572ea5239Smatthias.ringwald }
103672ea5239Smatthias.ringwald 
103740d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
103872ea5239Smatthias.ringwald 
10397b5fbe1fSmatthias.ringwald     log_info("hci_power_control_sleep\n");
10403144bce4Smatthias.ringwald 
1041b429b9b7Smatthias.ringwald #if 0
1042b429b9b7Smatthias.ringwald     // don't close serial port during sleep
1043b429b9b7Smatthias.ringwald 
104472ea5239Smatthias.ringwald     // close low-level device
10453a9fb326S[email protected]     hci_stack->hci_transport->close(hci_stack->config);
1046b429b9b7Smatthias.ringwald #endif
104772ea5239Smatthias.ringwald 
104872ea5239Smatthias.ringwald     // sleep mode
10493a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->sleep){
10503a9fb326S[email protected]         (*hci_stack->control->sleep)(hci_stack->config);
105172ea5239Smatthias.ringwald     }
1052b429b9b7Smatthias.ringwald 
10533a9fb326S[email protected]     hci_stack->state = HCI_STATE_SLEEPING;
10548d213e1aSmatthias.ringwald }
10558d213e1aSmatthias.ringwald 
105640d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
1057b429b9b7Smatthias.ringwald 
10587b5fbe1fSmatthias.ringwald     log_info("hci_power_control_wake\n");
1059b429b9b7Smatthias.ringwald 
1060b429b9b7Smatthias.ringwald     // wake on
10613a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->wake){
10623a9fb326S[email protected]         (*hci_stack->control->wake)(hci_stack->config);
1063b429b9b7Smatthias.ringwald     }
1064b429b9b7Smatthias.ringwald 
1065b429b9b7Smatthias.ringwald #if 0
1066b429b9b7Smatthias.ringwald     // open low-level device
10673a9fb326S[email protected]     int err = hci_stack->hci_transport->open(hci_stack->config);
1068b429b9b7Smatthias.ringwald     if (err){
10697d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
10703a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
10713a9fb326S[email protected]             (*hci_stack->control->off)(hci_stack->config);
1072b429b9b7Smatthias.ringwald         }
1073b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
1074b429b9b7Smatthias.ringwald         return err;
1075b429b9b7Smatthias.ringwald     }
1076b429b9b7Smatthias.ringwald #endif
1077b429b9b7Smatthias.ringwald 
1078b429b9b7Smatthias.ringwald     return 0;
1079b429b9b7Smatthias.ringwald }
1080b429b9b7Smatthias.ringwald 
1081b429b9b7Smatthias.ringwald 
10828d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
1083d661ed19Smatthias.ringwald 
10843a9fb326S[email protected]     log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack->state);
1085d661ed19Smatthias.ringwald 
10868d213e1aSmatthias.ringwald     int err = 0;
10873a9fb326S[email protected]     switch (hci_stack->state){
10888d213e1aSmatthias.ringwald 
10898d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
10908d213e1aSmatthias.ringwald             switch (power_mode){
10918d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
10928d213e1aSmatthias.ringwald                     err = hci_power_control_on();
10938d213e1aSmatthias.ringwald                     if (err) return err;
10947301ad89Smatthias.ringwald                     // set up state machine
10953a9fb326S[email protected]                     hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent
10963a9fb326S[email protected]                     hci_stack->state = HCI_STATE_INITIALIZING;
10973a9fb326S[email protected]                     hci_stack->substate = 0;
10988d213e1aSmatthias.ringwald                     break;
10998d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
11008d213e1aSmatthias.ringwald                     // do nothing
11018d213e1aSmatthias.ringwald                     break;
11028d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1103b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
11048d213e1aSmatthias.ringwald                     break;
11058d213e1aSmatthias.ringwald             }
11068d213e1aSmatthias.ringwald             break;
11077301ad89Smatthias.ringwald 
11088d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
11098d213e1aSmatthias.ringwald             switch (power_mode){
11108d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
11118d213e1aSmatthias.ringwald                     // do nothing
11128d213e1aSmatthias.ringwald                     break;
11138d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
11148d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
11158d213e1aSmatthias.ringwald                     hci_power_control_off();
11168d213e1aSmatthias.ringwald                     break;
11178d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1118b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
111972ea5239Smatthias.ringwald                     hci_power_control_sleep();
11208d213e1aSmatthias.ringwald                     break;
11218d213e1aSmatthias.ringwald             }
11228d213e1aSmatthias.ringwald             break;
11237301ad89Smatthias.ringwald 
11248d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
11258d213e1aSmatthias.ringwald             switch (power_mode){
11268d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
11278d213e1aSmatthias.ringwald                     // do nothing
11288d213e1aSmatthias.ringwald                     break;
11298d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1130c7e0c5f6Smatthias.ringwald                     // see hci_run
11313a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
11328d213e1aSmatthias.ringwald                     break;
11338d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1134b546ac54Smatthias.ringwald                     // see hci_run
11353a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
11363a9fb326S[email protected]                     hci_stack->substate = 0;
11378d213e1aSmatthias.ringwald                     break;
11388d213e1aSmatthias.ringwald             }
11398d213e1aSmatthias.ringwald             break;
11407301ad89Smatthias.ringwald 
11418d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
11428d213e1aSmatthias.ringwald             switch (power_mode){
11438d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
11448d213e1aSmatthias.ringwald                     // set up state machine
11453a9fb326S[email protected]                     hci_stack->state = HCI_STATE_INITIALIZING;
11463a9fb326S[email protected]                     hci_stack->substate = 0;
11478d213e1aSmatthias.ringwald                     break;
11488d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
11498d213e1aSmatthias.ringwald                     // do nothing
11508d213e1aSmatthias.ringwald                     break;
11518d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1152b546ac54Smatthias.ringwald                     // see hci_run
11533a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
11543a9fb326S[email protected]                     hci_stack->substate = 0;
11558d213e1aSmatthias.ringwald                     break;
11568d213e1aSmatthias.ringwald             }
11578d213e1aSmatthias.ringwald             break;
11588d213e1aSmatthias.ringwald 
11598d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
11608d213e1aSmatthias.ringwald             switch (power_mode){
11618d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
116228171530Smatthias.ringwald 
116328171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
116428171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
116528171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
11663a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
11673a9fb326S[email protected]                         hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
116828171530Smatthias.ringwald                         break;
116928171530Smatthias.ringwald                     }
117028171530Smatthias.ringwald #endif
1171b546ac54Smatthias.ringwald                     // set up state machine
11723a9fb326S[email protected]                     hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent
11733a9fb326S[email protected]                     hci_stack->state = HCI_STATE_INITIALIZING;
11743a9fb326S[email protected]                     hci_stack->substate = 0;
11758d213e1aSmatthias.ringwald                     break;
11768d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1177b546ac54Smatthias.ringwald                     // see hci_run
11783a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
11798d213e1aSmatthias.ringwald                     break;
11808d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1181b546ac54Smatthias.ringwald                     // do nothing
11828d213e1aSmatthias.ringwald                     break;
11838d213e1aSmatthias.ringwald             }
11848d213e1aSmatthias.ringwald             break;
11858d213e1aSmatthias.ringwald 
11868d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
11878d213e1aSmatthias.ringwald             switch (power_mode){
11888d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
118928171530Smatthias.ringwald 
119028171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
119128171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
119228171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
11933a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
11943a9fb326S[email protected]                         hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
1195758b46ceSmatthias.ringwald                         hci_update_scan_enable();
119628171530Smatthias.ringwald                         break;
119728171530Smatthias.ringwald                     }
119828171530Smatthias.ringwald #endif
11993144bce4Smatthias.ringwald                     err = hci_power_control_wake();
12003144bce4Smatthias.ringwald                     if (err) return err;
1201b546ac54Smatthias.ringwald                     // set up state machine
12023a9fb326S[email protected]                     hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent
12033a9fb326S[email protected]                     hci_stack->state = HCI_STATE_INITIALIZING;
12043a9fb326S[email protected]                     hci_stack->substate = 0;
12058d213e1aSmatthias.ringwald                     break;
12068d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
12073a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
12088d213e1aSmatthias.ringwald                     break;
12098d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1210b546ac54Smatthias.ringwald                     // do nothing
12118d213e1aSmatthias.ringwald                     break;
12128d213e1aSmatthias.ringwald             }
12138d213e1aSmatthias.ringwald             break;
121411e23e5fSmatthias.ringwald     }
121568d92d03Smatthias.ringwald 
1216038bc64cSmatthias.ringwald     // create internal event
1217ee8bf225Smatthias.ringwald 	hci_emit_state();
1218ee8bf225Smatthias.ringwald 
121968d92d03Smatthias.ringwald 	// trigger next/first action
122068d92d03Smatthias.ringwald 	hci_run();
122168d92d03Smatthias.ringwald 
1222475c8125Smatthias.ringwald     return 0;
1223475c8125Smatthias.ringwald }
1224475c8125Smatthias.ringwald 
1225758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){
1226758b46ceSmatthias.ringwald     // 2 = page scan, 1 = inq scan
12273a9fb326S[email protected]     hci_stack->new_scan_enable_value  = hci_stack->connectable << 1 | hci_stack->discoverable;
1228758b46ceSmatthias.ringwald     hci_run();
1229758b46ceSmatthias.ringwald }
1230758b46ceSmatthias.ringwald 
1231381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
1232381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
1233381fbed8Smatthias.ringwald 
12343a9fb326S[email protected]     if (hci_stack->discoverable == enable){
12353a9fb326S[email protected]         hci_emit_discoverable_enabled(hci_stack->discoverable);
1236381fbed8Smatthias.ringwald         return;
1237381fbed8Smatthias.ringwald     }
1238381fbed8Smatthias.ringwald 
12393a9fb326S[email protected]     hci_stack->discoverable = enable;
1240758b46ceSmatthias.ringwald     hci_update_scan_enable();
1241758b46ceSmatthias.ringwald }
1242b031bebbSmatthias.ringwald 
1243758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){
1244758b46ceSmatthias.ringwald     if (enable) enable = 1; // normalize argument
1245758b46ceSmatthias.ringwald 
1246758b46ceSmatthias.ringwald     // don't emit event
12473a9fb326S[email protected]     if (hci_stack->connectable == enable) return;
1248758b46ceSmatthias.ringwald 
12493a9fb326S[email protected]     hci_stack->connectable = enable;
1250758b46ceSmatthias.ringwald     hci_update_scan_enable();
1251381fbed8Smatthias.ringwald }
1252381fbed8Smatthias.ringwald 
12535061f3afS[email protected] bd_addr_t * hci_local_bd_addr(void){
12543a9fb326S[email protected]     return &hci_stack->local_bd_addr;
12555061f3afS[email protected] }
12565061f3afS[email protected] 
125706b35ec0Smatthias.ringwald void hci_run(){
12588a485f27Smatthias.ringwald 
125932ab9390Smatthias.ringwald     hci_connection_t * connection;
126032ab9390Smatthias.ringwald     linked_item_t * it;
126132ab9390Smatthias.ringwald 
1262ce4c8fabSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
1263ce4c8fabSmatthias.ringwald 
1264b031bebbSmatthias.ringwald     // global/non-connection oriented commands
1265b031bebbSmatthias.ringwald 
1266b031bebbSmatthias.ringwald     // decline incoming connections
12673a9fb326S[email protected]     if (hci_stack->decline_reason){
12683a9fb326S[email protected]         uint8_t reason = hci_stack->decline_reason;
12693a9fb326S[email protected]         hci_stack->decline_reason = 0;
12703a9fb326S[email protected]         hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason);
1271dbe1a790S[email protected]         return;
1272ce4c8fabSmatthias.ringwald     }
1273ce4c8fabSmatthias.ringwald 
1274b031bebbSmatthias.ringwald     // send scan enable
12753a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_WORKING && hci_stack->new_scan_enable_value != 0xff && hci_classic_supported()){
12763a9fb326S[email protected]         hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value);
12773a9fb326S[email protected]         hci_stack->new_scan_enable_value = 0xff;
1278dbe1a790S[email protected]         return;
1279b031bebbSmatthias.ringwald     }
1280b031bebbSmatthias.ringwald 
128132ab9390Smatthias.ringwald     // send pending HCI commands
12823a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
128332ab9390Smatthias.ringwald 
1284b031bebbSmatthias.ringwald         connection = (hci_connection_t *) it;
128532ab9390Smatthias.ringwald 
1286ad83dc6aS[email protected]         if (connection->state == SEND_CREATE_CONNECTION){
1287ad83dc6aS[email protected]             log_info("sending hci_create_connection\n");
1288ad83dc6aS[email protected]             hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
1289ad83dc6aS[email protected]             return;
1290ad83dc6aS[email protected]         }
1291ad83dc6aS[email protected] 
129232ab9390Smatthias.ringwald         if (connection->state == RECEIVED_CONNECTION_REQUEST){
12937b5fbe1fSmatthias.ringwald             log_info("sending hci_accept_connection_request\n");
129432ab9390Smatthias.ringwald             connection->state = ACCEPTED_CONNECTION_REQUEST;
129534d2123cS[email protected]             hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
1296dbe1a790S[email protected]             return;
1297c7e0c5f6Smatthias.ringwald         }
1298c7e0c5f6Smatthias.ringwald 
129932ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
130034d2123cS[email protected]             log_info("responding to link key request\n");
130134d2123cS[email protected]             connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST);
130232ab9390Smatthias.ringwald             link_key_t link_key;
1303c77e8838S[email protected]             link_key_type_t link_key_type;
13043a9fb326S[email protected]             if ( hci_stack->remote_device_db
13053a9fb326S[email protected]               && hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)
130634d2123cS[email protected]               && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){
13079ab95c90S[email protected]                connection->link_key_type = link_key_type;
130832ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
130932ab9390Smatthias.ringwald             } else {
131032ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
131132ab9390Smatthias.ringwald             }
1312dbe1a790S[email protected]             return;
131332ab9390Smatthias.ringwald         }
13141d6b20aeS[email protected] 
1315899283eaS[email protected]         if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){
13164c57c146S[email protected]             log_info("denying to pin request\n");
1317899283eaS[email protected]             connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST);
131834d2123cS[email protected]             hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address);
13194c57c146S[email protected]             return;
13204c57c146S[email protected]         }
13214c57c146S[email protected] 
1322dbe1a790S[email protected]         if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){
132334d2123cS[email protected]             connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY);
13243a9fb326S[email protected]             if (hci_stack->bondable && hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN){
1325106d6d11S[email protected]                 // tweak authentication requirements
13263a9fb326S[email protected]                 uint8_t authreq = hci_stack->ssp_authentication_requirement;
1327106d6d11S[email protected]                 if (connection->bonding_flags & BONDING_DEDICATED){
13289faad3abS[email protected]                     authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING;
13299faad3abS[email protected]                 }
13309faad3abS[email protected]                 if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){
13319faad3abS[email protected]                     authreq |= 1;
1332106d6d11S[email protected]                 }
13333a9fb326S[email protected]                 hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, NULL, authreq);
1334f8fb5f6eS[email protected]             } else {
1335f8fb5f6eS[email protected]                 hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED);
1336f8fb5f6eS[email protected]             }
1337dbe1a790S[email protected]             return;
133832ab9390Smatthias.ringwald         }
133932ab9390Smatthias.ringwald 
1340dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){
1341dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY);
134234d2123cS[email protected]             hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address);
1343dbe1a790S[email protected]             return;
1344dbe1a790S[email protected]         }
1345dbe1a790S[email protected] 
1346dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){
1347dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY);
134834d2123cS[email protected]             hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000);
1349dbe1a790S[email protected]             return;
1350dbe1a790S[email protected]         }
1351afd4e962S[email protected] 
1352afd4e962S[email protected]         if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){
1353afd4e962S[email protected]             connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES;
135434d2123cS[email protected]             hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle);
13552bd8b7e7S[email protected]             return;
13562bd8b7e7S[email protected]         }
13572bd8b7e7S[email protected] 
13582bd8b7e7S[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){
13592bd8b7e7S[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK;
136034d2123cS[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005);  // authentication failure
136134d2123cS[email protected]             return;
136234d2123cS[email protected]         }
1363ad83dc6aS[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){
1364ad83dc6aS[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE;
1365ad83dc6aS[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0);  // authentication done
1366ad83dc6aS[email protected]             return;
1367ad83dc6aS[email protected]         }
136834d2123cS[email protected]         if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){
136934d2123cS[email protected]             connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST;
137034d2123cS[email protected]             hci_send_cmd(&hci_authentication_requested, connection->con_handle);
13712bd8b7e7S[email protected]             return;
1372afd4e962S[email protected]         }
1373dce78009S[email protected]         if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){
1374dce78009S[email protected]             connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST;
1375dce78009S[email protected]             hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1);
1376dce78009S[email protected]             return;
1377dce78009S[email protected]         }
1378dbe1a790S[email protected]     }
1379c7e0c5f6Smatthias.ringwald 
13803a9fb326S[email protected]     switch (hci_stack->state){
13813429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
13823a9fb326S[email protected]             // log_info("hci_init: substate %u\n", hci_stack->substate);
13833a9fb326S[email protected]             if (hci_stack->substate % 2) {
13843429f56bSmatthias.ringwald                 // odd: waiting for command completion
138506b35ec0Smatthias.ringwald                 return;
13863429f56bSmatthias.ringwald             }
13873a9fb326S[email protected]             switch (hci_stack->substate >> 1){
1388c7492964Smatthias.ringwald                 case 0: // RESET
138922909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
139024052c2aS[email protected] 
13913a9fb326S[email protected]                     if (hci_stack->config == 0 || ((hci_uart_config_t *)hci_stack->config)->baudrate_main == 0){
1392c7492964Smatthias.ringwald                         // skip baud change
13933a9fb326S[email protected]                         hci_stack->substate = 4; // >> 1 = 2
1394c7492964Smatthias.ringwald                     }
13953429f56bSmatthias.ringwald                     break;
1396c7492964Smatthias.ringwald                 case 1: // SEND BAUD CHANGE
13973a9fb326S[email protected]                     hci_stack->control->baudrate_cmd(hci_stack->config, ((hci_uart_config_t *)hci_stack->config)->baudrate_main, hci_stack->hci_packet_buffer);
13983a9fb326S[email protected]                     hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]);
1399c7492964Smatthias.ringwald                     break;
1400c7492964Smatthias.ringwald                 case 2: // LOCAL BAUD CHANGE
14013a9fb326S[email protected]                     hci_stack->hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack->config)->baudrate_main);
14023a9fb326S[email protected]                     hci_stack->substate += 2;
1403c7492964Smatthias.ringwald                     // break missing here for fall through
1404c7492964Smatthias.ringwald 
1405c7492964Smatthias.ringwald                 case 3:
140624052c2aS[email protected]                     // Custom initialization
14073a9fb326S[email protected]                     if (hci_stack->control && hci_stack->control->next_cmd){
14083a9fb326S[email protected]                         int valid_cmd = (*hci_stack->control->next_cmd)(hci_stack->config, hci_stack->hci_packet_buffer);
1409d62aca9fSmatthias.ringwald                         if (valid_cmd){
14103a9fb326S[email protected]                             int size = 3 + hci_stack->hci_packet_buffer[2];
14113a9fb326S[email protected]                             hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size);
14123a9fb326S[email protected]                             hci_stack->substate = 4; // more init commands
141390919203Smatthias.ringwald                             break;
141490919203Smatthias.ringwald                         }
1415d62aca9fSmatthias.ringwald                         log_info("hci_run: init script done\n\r");
141690919203Smatthias.ringwald                     }
14172f6c30e1Smatthias.ringwald                     // otherwise continue
1418f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
1419f432a6ddSmatthias.ringwald 					break;
1420c7492964Smatthias.ringwald 				case 4:
1421e2edc0c3Smatthias.ringwald 					hci_send_cmd(&hci_read_buffer_size);
1422e2edc0c3Smatthias.ringwald 					break;
1423c7492964Smatthias.ringwald                 case 5:
142465389bfcS[email protected]                     hci_send_cmd(&hci_read_local_supported_features);
14253429f56bSmatthias.ringwald                     break;
1426c7492964Smatthias.ringwald                 case 6:
1427d7e0e3a8S[email protected]                     if (hci_le_supported()){
1428d7e0e3a8S[email protected]                         hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF);
1429d7e0e3a8S[email protected]                     } else {
1430d7e0e3a8S[email protected]                         // Kensington Bluetoot 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff...
1431d7e0e3a8S[email protected]                         hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF);
1432d7e0e3a8S[email protected]                     }
1433f5d8d141S[email protected] 
1434f5d8d141S[email protected]                     // skip Classic init commands for LE only chipsets
143524052c2aS[email protected]                     if (!hci_classic_supported()){
143624052c2aS[email protected]                         if (hci_le_supported()){
14373a9fb326S[email protected]                             hci_stack->substate = 11 << 1;    // skip all classic command
143824052c2aS[email protected]                         } else {
143924052c2aS[email protected]                             log_error("Neither BR/EDR nor LE supported");
14403a9fb326S[email protected]                             hci_stack->substate = 13 << 1;    // skip all
144124052c2aS[email protected]                         }
1442f5d8d141S[email protected]                     }
1443f5d8d141S[email protected]                     break;
1444f5d8d141S[email protected]                 case 7:
144524052c2aS[email protected]                     if (hci_ssp_supported()){
14463a9fb326S[email protected]                         hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable);
1447f5d8d141S[email protected]                         break;
144824052c2aS[email protected]                     }
14493a9fb326S[email protected]                     hci_stack->substate += 2;
145024052c2aS[email protected]                     // break missing here for fall through
145124052c2aS[email protected] 
1452f5d8d141S[email protected]                 case 8:
145365389bfcS[email protected]                     // ca. 15 sec
145465389bfcS[email protected]                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
1455559e517eS[email protected]                     break;
1456f5d8d141S[email protected]                 case 9:
14573a9fb326S[email protected]                     hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device);
1458e2386ba1S[email protected]                     break;
1459f5d8d141S[email protected]                 case 10:
14603a9fb326S[email protected]                     if (hci_stack->local_name){
14613a9fb326S[email protected]                         hci_send_cmd(&hci_write_local_name, hci_stack->local_name);
1462e2386ba1S[email protected]                     } else {
14638a485f27Smatthias.ringwald                         char hostname[30];
1464e2386ba1S[email protected] #ifdef EMBEDDED
1465e2386ba1S[email protected]                         // BTstack-11:22:33:44:55:66
1466e2386ba1S[email protected]                         strcpy(hostname, "BTstack ");
14673a9fb326S[email protected]                         strcat(hostname, bd_addr_to_str(hci_stack->local_bd_addr));
1468e2386ba1S[email protected]                         printf("---> Name %s\n", hostname);
1469e2386ba1S[email protected] #else
1470e2386ba1S[email protected]                         // hostname for POSIX systems
14718a485f27Smatthias.ringwald                         gethostname(hostname, 30);
14728a485f27Smatthias.ringwald                         hostname[29] = '\0';
1473e2386ba1S[email protected] #endif
14748a485f27Smatthias.ringwald                         hci_send_cmd(&hci_write_local_name, hostname);
14758a485f27Smatthias.ringwald                     }
14763c4d4b90Smatthias.ringwald                     break;
1477e0dbca83S[email protected]                 case 11:
14783a9fb326S[email protected] 					hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan
147924052c2aS[email protected]                     if (!hci_le_supported()){
148024052c2aS[email protected]                         // SKIP LE init for Classic only configuration
14813a9fb326S[email protected]                         hci_stack->substate = 13 << 1;
148224052c2aS[email protected]                     }
1483a45d6b9fS[email protected] 					break;
148424052c2aS[email protected] 
148543aa7007S[email protected] #ifdef HAVE_BLE
148624052c2aS[email protected]                 // LE INIT
1487a45d6b9fS[email protected]                 case 12:
148824052c2aS[email protected]                     hci_send_cmd(&hci_le_read_buffer_size);
148924052c2aS[email protected]                     break;
149024052c2aS[email protected]                 case 13:
149124052c2aS[email protected]                     // LE Supported Host = 1, Simultaneous Host = 0
149224052c2aS[email protected]                     hci_send_cmd(&hci_write_le_host_supported, 1, 0);
149324052c2aS[email protected]                     break;
149443aa7007S[email protected] #endif
149524052c2aS[email protected] 
149624052c2aS[email protected]                 // DONE
149724052c2aS[email protected]                 case 14:
14983429f56bSmatthias.ringwald                     // done.
14993a9fb326S[email protected]                     hci_stack->state = HCI_STATE_WORKING;
1500b360b6adSmatthias.ringwald                     hci_emit_state();
15013429f56bSmatthias.ringwald                     break;
15023429f56bSmatthias.ringwald                 default:
15033429f56bSmatthias.ringwald                     break;
1504475c8125Smatthias.ringwald             }
15053a9fb326S[email protected]             hci_stack->substate++;
15063429f56bSmatthias.ringwald             break;
1507c7e0c5f6Smatthias.ringwald 
1508c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
1509c7e0c5f6Smatthias.ringwald 
15107b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING\n");
1511c7e0c5f6Smatthias.ringwald             // close all open connections
15123a9fb326S[email protected]             connection =  (hci_connection_t *) hci_stack->connections;
1513c7e0c5f6Smatthias.ringwald             if (connection){
151432ab9390Smatthias.ringwald 
1515c7e0c5f6Smatthias.ringwald                 // send disconnect
151632ab9390Smatthias.ringwald                 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
151732ab9390Smatthias.ringwald 
151879bbfa0bSmatthias.ringwald                 log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
15196ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
1520c7e0c5f6Smatthias.ringwald 
1521c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
1522c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
1523c7e0c5f6Smatthias.ringwald                 return;
1524c7e0c5f6Smatthias.ringwald             }
15257b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, calling off\n");
1526c7e0c5f6Smatthias.ringwald 
152772ea5239Smatthias.ringwald             // switch mode
1528c7e0c5f6Smatthias.ringwald             hci_power_control_off();
15299418f9c9Smatthias.ringwald 
15307b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, emitting state\n");
153172ea5239Smatthias.ringwald             hci_emit_state();
15327b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, done\n");
153372ea5239Smatthias.ringwald             break;
1534c7e0c5f6Smatthias.ringwald 
153572ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
15363a9fb326S[email protected]             switch(hci_stack->substate) {
153789db417bSmatthias.ringwald                 case 0:
15387b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_FALLING_ASLEEP\n");
153972ea5239Smatthias.ringwald                     // close all open connections
15403a9fb326S[email protected]                     connection =  (hci_connection_t *) hci_stack->connections;
154166da7044Smatthias.ringwald 
154228171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
154366da7044Smatthias.ringwald                     // don't close connections, if H4 supports power management
154466da7044Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
154566da7044Smatthias.ringwald                         connection = NULL;
154666da7044Smatthias.ringwald                     }
154766da7044Smatthias.ringwald #endif
154872ea5239Smatthias.ringwald                     if (connection){
154932ab9390Smatthias.ringwald 
155072ea5239Smatthias.ringwald                         // send disconnect
155132ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
155232ab9390Smatthias.ringwald 
155379bbfa0bSmatthias.ringwald                         log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
15546ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
155572ea5239Smatthias.ringwald 
155672ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
155772ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
155872ea5239Smatthias.ringwald                         return;
155972ea5239Smatthias.ringwald                     }
156072ea5239Smatthias.ringwald 
156192368cd3S[email protected]                     if (hci_classic_supported()){
156289db417bSmatthias.ringwald                         // disable page and inquiry scan
156332ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
156432ab9390Smatthias.ringwald 
156592368cd3S[email protected]                         log_info("HCI_STATE_HALTING, disabling inq scans\n");
15663a9fb326S[email protected]                         hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan
156789db417bSmatthias.ringwald 
156889db417bSmatthias.ringwald                         // continue in next sub state
15693a9fb326S[email protected]                         hci_stack->substate++;
157089db417bSmatthias.ringwald                         break;
157192368cd3S[email protected]                     }
157292368cd3S[email protected]                     // fall through for ble-only chips
157392368cd3S[email protected] 
157489db417bSmatthias.ringwald                 case 2:
15757b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, calling sleep\n");
157628171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
157728171530Smatthias.ringwald                     // don't actually go to sleep, if H4 supports power management
157828171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
157928171530Smatthias.ringwald                         // SLEEP MODE reached
15803a9fb326S[email protected]                         hci_stack->state = HCI_STATE_SLEEPING;
158128171530Smatthias.ringwald                         hci_emit_state();
158228171530Smatthias.ringwald                         break;
158328171530Smatthias.ringwald                     }
158428171530Smatthias.ringwald #endif
158572ea5239Smatthias.ringwald                     // switch mode
15863a9fb326S[email protected]                     hci_power_control_sleep();  // changes hci_stack->state to SLEEP
1587c7e0c5f6Smatthias.ringwald                     hci_emit_state();
158828171530Smatthias.ringwald                     break;
158928171530Smatthias.ringwald 
159089db417bSmatthias.ringwald                 default:
159189db417bSmatthias.ringwald                     break;
159289db417bSmatthias.ringwald             }
1593c7e0c5f6Smatthias.ringwald             break;
1594c7e0c5f6Smatthias.ringwald 
15953429f56bSmatthias.ringwald         default:
15963429f56bSmatthias.ringwald             break;
15971f504dbdSmatthias.ringwald     }
15983429f56bSmatthias.ringwald }
159916833f0aSmatthias.ringwald 
160031452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1601c8e4258aSmatthias.ringwald     bd_addr_t addr;
1602c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1603c8e4258aSmatthias.ringwald     // house-keeping
1604c8e4258aSmatthias.ringwald 
1605c8e4258aSmatthias.ringwald     // create_connection?
1606c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1607c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
1608339b6768Smatthias.ringwald         log_info("Create_connection to %s\n", bd_addr_to_str(addr));
1609c8e4258aSmatthias.ringwald 
1610ad83dc6aS[email protected]         conn = connection_for_address(addr);
1611ad83dc6aS[email protected]         if (!conn){
161217f1ba2aSmatthias.ringwald             conn = create_connection_for_addr(addr);
161317f1ba2aSmatthias.ringwald             if (!conn){
161417f1ba2aSmatthias.ringwald                 // notify client that alloc failed
161517f1ba2aSmatthias.ringwald                 hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
161617f1ba2aSmatthias.ringwald                 return 0; // don't sent packet to controller
161717f1ba2aSmatthias.ringwald             }
1618ad83dc6aS[email protected]             conn->state = SEND_CREATE_CONNECTION;
1619ad83dc6aS[email protected]         }
1620ad83dc6aS[email protected]         log_info("conn state %u", conn->state);
1621ad83dc6aS[email protected]         switch (conn->state){
1622ad83dc6aS[email protected]             // if connection active exists
1623ad83dc6aS[email protected]             case OPEN:
1624*62bda3fbS[email protected]                 // and OPEN, emit connection complete command, don't send to controller
1625ad83dc6aS[email protected]                 hci_emit_connection_complete(conn, 0);
1626*62bda3fbS[email protected]                 return 0;
1627ad83dc6aS[email protected]             case SEND_CREATE_CONNECTION:
1628ad83dc6aS[email protected]                 // connection created by hci, e.g. dedicated bonding
1629ad83dc6aS[email protected]                 break;
1630ad83dc6aS[email protected]             default:
1631ad83dc6aS[email protected]                 // otherwise, just ignore as it is already in the open process
1632ad83dc6aS[email protected]                 return 0;
1633ad83dc6aS[email protected]         }
1634c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
1635c8e4258aSmatthias.ringwald     }
1636c8e4258aSmatthias.ringwald 
16377fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
16387fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
16397fde4af9Smatthias.ringwald     }
16407fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
16417fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
16427fde4af9Smatthias.ringwald     }
16437fde4af9Smatthias.ringwald 
16448ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
16453a9fb326S[email protected]         if (hci_stack->remote_device_db){
16468ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
16473a9fb326S[email protected]             hci_stack->remote_device_db->delete_link_key(&addr);
16488ef73945Smatthias.ringwald         }
16498ef73945Smatthias.ringwald     }
1650c8e4258aSmatthias.ringwald 
16516724cd9eS[email protected]     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)
16526724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_pin_code_request_reply)){
16536724cd9eS[email protected]         bt_flip_addr(addr, &packet[3]);
16546724cd9eS[email protected]         conn = connection_for_address(addr);
16556724cd9eS[email protected]         if (conn){
16566724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE);
16576724cd9eS[email protected]         }
16586724cd9eS[email protected]     }
16596724cd9eS[email protected] 
16606724cd9eS[email protected]     if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply)
16616724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_confirmation_request_reply)
16626724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_negative_reply)
16636724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_reply)) {
16646724cd9eS[email protected]         bt_flip_addr(addr, &packet[3]);
16656724cd9eS[email protected]         conn = connection_for_address(addr);
16666724cd9eS[email protected]         if (conn){
16676724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE);
16686724cd9eS[email protected]         }
16696724cd9eS[email protected]     }
16706724cd9eS[email protected] 
167169a97523S[email protected] #ifdef HAVE_BLE
167269a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_advertising_parameters)){
16733a9fb326S[email protected]         hci_stack->adv_addr_type = packet[8];
167469a97523S[email protected]     }
167569a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_random_address)){
16763a9fb326S[email protected]         bt_flip_addr(hci_stack->adv_address, &packet[3]);
167769a97523S[email protected]     }
167869a97523S[email protected] #endif
167969a97523S[email protected] 
168069a97523S[email protected] 
16813a9fb326S[email protected]     hci_stack->num_cmd_packets--;
16823a9fb326S[email protected]     return hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
168331452debSmatthias.ringwald }
16848adf0ddaSmatthias.ringwald 
16852bd8b7e7S[email protected] // disconnect because of security block
16862bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){
16872bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
16882bd8b7e7S[email protected]     if (!connection) return;
16892bd8b7e7S[email protected]     connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK;
16902bd8b7e7S[email protected] }
16912bd8b7e7S[email protected] 
16922bd8b7e7S[email protected] 
1693dbe1a790S[email protected] // Configure Secure Simple Pairing
1694dbe1a790S[email protected] 
1695dbe1a790S[email protected] // enable will enable SSP during init
1696dbe1a790S[email protected] void hci_ssp_set_enable(int enable){
16973a9fb326S[email protected]     hci_stack->ssp_enable = enable;
1698dbe1a790S[email protected] }
1699dbe1a790S[email protected] 
17002bd8b7e7S[email protected] int hci_local_ssp_activated(){
17013a9fb326S[email protected]     return hci_ssp_supported() && hci_stack->ssp_enable;
17022bd8b7e7S[email protected] }
17032bd8b7e7S[email protected] 
1704dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement
1705dbe1a790S[email protected] void hci_ssp_set_io_capability(int io_capability){
17063a9fb326S[email protected]     hci_stack->ssp_io_capability = io_capability;
1707dbe1a790S[email protected] }
1708dbe1a790S[email protected] void hci_ssp_set_authentication_requirement(int authentication_requirement){
17093a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = authentication_requirement;
1710dbe1a790S[email protected] }
1711dbe1a790S[email protected] 
1712dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested
1713dbe1a790S[email protected] void hci_ssp_set_auto_accept(int auto_accept){
17143a9fb326S[email protected]     hci_stack->ssp_auto_accept = auto_accept;
1715dbe1a790S[email protected] }
1716dbe1a790S[email protected] 
17171cd208adSmatthias.ringwald /**
17181cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
17191cd208adSmatthias.ringwald  */
1720fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
17211cd208adSmatthias.ringwald     va_list argptr;
17221cd208adSmatthias.ringwald     va_start(argptr, cmd);
17233a9fb326S[email protected]     uint16_t size = hci_create_cmd_internal(hci_stack->hci_packet_buffer, cmd, argptr);
17241cd208adSmatthias.ringwald     va_end(argptr);
17253a9fb326S[email protected]     return hci_send_cmd_packet(hci_stack->hci_packet_buffer, size);
172693b8dc03Smatthias.ringwald }
1727c8e4258aSmatthias.ringwald 
1728ee091cf1Smatthias.ringwald // Create various non-HCI events.
1729ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
1730ee091cf1Smatthias.ringwald 
1731c8e4258aSmatthias.ringwald void hci_emit_state(){
17323a9fb326S[email protected]     log_info("BTSTACK_EVENT_STATE %u", hci_stack->state);
1733425d1371Smatthias.ringwald     uint8_t event[3];
173480d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
1735425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
17363a9fb326S[email protected]     event[2] = hci_stack->state;
1737425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
17383a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1739c8e4258aSmatthias.ringwald }
1740c8e4258aSmatthias.ringwald 
174117f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){
1742425d1371Smatthias.ringwald     uint8_t event[13];
1743c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
1744425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
174517f1ba2aSmatthias.ringwald     event[2] = status;
1746c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
1747c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
1748c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
1749c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
1750425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
17513a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1752c8e4258aSmatthias.ringwald }
1753c8e4258aSmatthias.ringwald 
17543c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
1755425d1371Smatthias.ringwald     uint8_t event[6];
17563c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
1757e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
17583c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
17593c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
17603c4d4b90Smatthias.ringwald     event[5] = reason;
1761425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
17623a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
17633c4d4b90Smatthias.ringwald }
17643c4d4b90Smatthias.ringwald 
1765ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
1766e0abb8e7S[email protected]     log_info("L2CAP_EVENT_TIMEOUT_CHECK");
1767425d1371Smatthias.ringwald     uint8_t event[4];
176880d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
1769425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1770ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
1771425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
17723a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1773ee091cf1Smatthias.ringwald }
177443bfb1bdSmatthias.ringwald 
177543bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
1776e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections());
1777425d1371Smatthias.ringwald     uint8_t event[3];
177880d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
1779425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
178043bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
1781425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
17823a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
178343bfb1bdSmatthias.ringwald }
1784038bc64cSmatthias.ringwald 
1785038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
1786e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_POWERON_FAILED");
1787425d1371Smatthias.ringwald     uint8_t event[2];
178880d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
1789425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1790425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
17913a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1792038bc64cSmatthias.ringwald }
17931b0e3922Smatthias.ringwald 
179409ba8edeSmatthias.ringwald #ifndef EMBEDDED
17951b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
1796e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR);
1797425d1371Smatthias.ringwald     uint8_t event[6];
17981b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
1799425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1800425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
1801425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
1802425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
1803425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18043a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
18051b0e3922Smatthias.ringwald }
180609ba8edeSmatthias.ringwald #endif
18071b0e3922Smatthias.ringwald 
18082ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
1809e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled);
1810425d1371Smatthias.ringwald     uint8_t event[3];
18112ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
1812425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
18132ed6235cSmatthias.ringwald     event[2] = enabled;
1814425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18153a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
18162ed6235cSmatthias.ringwald }
1817627c2f45Smatthias.ringwald 
1818627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
1819e0abb8e7S[email protected]     uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info
1820627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
1821e0abb8e7S[email protected]     event[1] = sizeof(event) - 2 - 1;
1822f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
18232f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
1824f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
1825e0abb8e7S[email protected] 
1826e0abb8e7S[email protected]     event[9+248] = 0;   // assert \0 for log_info
1827e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]);
1828e0abb8e7S[email protected] 
1829e0abb8e7S[email protected]     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1);
18303a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1);
1831627c2f45Smatthias.ringwald }
1832381fbed8Smatthias.ringwald 
1833381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
1834e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled);
1835425d1371Smatthias.ringwald     uint8_t event[3];
1836381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
1837425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1838381fbed8Smatthias.ringwald     event[2] = enabled;
1839425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18403a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1841381fbed8Smatthias.ringwald }
1842458bf4e8S[email protected] 
1843a00031e2S[email protected] void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){
1844df3354fcS[email protected]     log_info("hci_emit_security_level %u for handle %x", level, con_handle);
1845a00031e2S[email protected]     uint8_t event[5];
1846e00caf9cS[email protected]     int pos = 0;
1847a00031e2S[email protected]     event[pos++] = GAP_SECURITY_LEVEL;
1848e00caf9cS[email protected]     event[pos++] = sizeof(event) - 2;
1849a00031e2S[email protected]     bt_store_16(event, 2, con_handle);
1850e00caf9cS[email protected]     pos += 2;
1851e00caf9cS[email protected]     event[pos++] = level;
1852e00caf9cS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18533a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1854e00caf9cS[email protected] }
1855e00caf9cS[email protected] 
1856ad83dc6aS[email protected] void hci_emit_dedicated_bonding_result(hci_connection_t * connection, uint8_t status){
1857ad83dc6aS[email protected]     log_info("hci_emit_dedicated_bonding_result %u ", status);
1858ad83dc6aS[email protected]     uint8_t event[9];
1859ad83dc6aS[email protected]     int pos = 0;
1860ad83dc6aS[email protected]     event[pos++] = GAP_DEDICATED_BONDING_COMPLETED;
1861ad83dc6aS[email protected]     event[pos++] = sizeof(event) - 2;
1862ad83dc6aS[email protected]     event[pos++] = status;
1863ad83dc6aS[email protected]     bt_flip_addr( * (bd_addr_t *) &event[pos], connection->address);
1864ad83dc6aS[email protected]     pos += 6;
1865ad83dc6aS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18663a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1867ad83dc6aS[email protected] }
1868ad83dc6aS[email protected] 
18692bd8b7e7S[email protected] // query if remote side supports SSP
18702bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){
18712bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
18722bd8b7e7S[email protected]     if (!connection) return 0;
18732bd8b7e7S[email protected]     return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0;
18742bd8b7e7S[email protected] }
18752bd8b7e7S[email protected] 
1876df3354fcS[email protected] int hci_ssp_supported_on_both_sides(hci_con_handle_t handle){
1877df3354fcS[email protected]     return hci_local_ssp_activated() && hci_remote_ssp_supported(handle);
1878df3354fcS[email protected] }
1879df3354fcS[email protected] 
1880458bf4e8S[email protected] // GAP API
1881458bf4e8S[email protected] /**
1882458bf4e8S[email protected]  * @bbrief enable/disable bonding. default is enabled
1883458bf4e8S[email protected]  * @praram enabled
1884458bf4e8S[email protected]  */
18854c57c146S[email protected] void gap_set_bondable_mode(int enable){
18863a9fb326S[email protected]     hci_stack->bondable = enable ? 1 : 0;
1887458bf4e8S[email protected] }
1888cb230b9dS[email protected] 
1889cb230b9dS[email protected] /**
189034d2123cS[email protected]  * @brief map link keys to security levels
1891cb230b9dS[email protected]  */
189234d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){
189334d2123cS[email protected]     switch (link_key_type){
18943c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256:
18953c68dfa9S[email protected]             return LEVEL_4;
18963c68dfa9S[email protected]         case COMBINATION_KEY:
18973c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192:
18983c68dfa9S[email protected]             return LEVEL_3;
18993c68dfa9S[email protected]         default:
19003c68dfa9S[email protected]             return LEVEL_2;
19013c68dfa9S[email protected]     }
1902cb230b9dS[email protected] }
1903cb230b9dS[email protected] 
1904a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){
190534d2123cS[email protected]     if (!connection) return LEVEL_0;
190634d2123cS[email protected]     if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0;
190734d2123cS[email protected]     return gap_security_level_for_link_key_type(connection->link_key_type);
190834d2123cS[email protected] }
190934d2123cS[email protected] 
191034d2123cS[email protected] 
1911106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){
1912106d6d11S[email protected]     return level > LEVEL_2;
1913106d6d11S[email protected] }
1914106d6d11S[email protected] 
191534d2123cS[email protected] /**
191634d2123cS[email protected]  * @brief get current security level
191734d2123cS[email protected]  */
191834d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){
191934d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
192034d2123cS[email protected]     if (!connection) return LEVEL_0;
192134d2123cS[email protected]     return gap_security_level_for_connection(connection);
192234d2123cS[email protected] }
192334d2123cS[email protected] 
1924cb230b9dS[email protected] /**
1925cb230b9dS[email protected]  * @brief request connection to device to
1926cb230b9dS[email protected]  * @result GAP_AUTHENTICATION_RESULT
1927cb230b9dS[email protected]  */
192834d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){
192934d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
193034d2123cS[email protected]     if (!connection){
1931a00031e2S[email protected]         hci_emit_security_level(con_handle, LEVEL_0);
193234d2123cS[email protected]         return;
193334d2123cS[email protected]     }
193434d2123cS[email protected]     gap_security_level_t current_level = gap_security_level(con_handle);
193534d2123cS[email protected]     log_info("gap_request_security_level %u, current level %u", requested_level, current_level);
193634d2123cS[email protected]     if (current_level >= requested_level){
1937a00031e2S[email protected]         hci_emit_security_level(con_handle, current_level);
193834d2123cS[email protected]         return;
193934d2123cS[email protected]     }
1940a00031e2S[email protected] 
194134d2123cS[email protected]     connection->requested_security_level = requested_level;
1942a00031e2S[email protected] 
1943fb8ba0dbS[email protected]     // would enabling ecnryption suffice (>= LEVEL_2)?
19443a9fb326S[email protected]     if (hci_stack->remote_device_db){
1945a00031e2S[email protected]         link_key_type_t link_key_type;
1946a00031e2S[email protected]         link_key_t      link_key;
19473a9fb326S[email protected]         if (hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)){
1948a00031e2S[email protected]             if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){
1949a00031e2S[email protected]                 connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
1950a00031e2S[email protected]                 return;
1951a00031e2S[email protected]             }
1952a00031e2S[email protected]         }
1953a00031e2S[email protected]     }
1954a00031e2S[email protected] 
19551eb2563eS[email protected]     // try to authenticate connection
19561eb2563eS[email protected]     connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
1957e00caf9cS[email protected] }
1958ad83dc6aS[email protected] 
1959ad83dc6aS[email protected] /**
1960ad83dc6aS[email protected]  * @brief start dedicated bonding with device. disconnect after bonding
1961ad83dc6aS[email protected]  * @param device
1962ad83dc6aS[email protected]  * @param request MITM protection
1963ad83dc6aS[email protected]  * @result GAP_DEDICATED_BONDING_COMPLETE
1964ad83dc6aS[email protected]  */
1965ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){
1966ad83dc6aS[email protected] 
1967ad83dc6aS[email protected] 
1968ad83dc6aS[email protected]     printf("gap_dedicated_bonding clled\n");
1969ad83dc6aS[email protected]     // create connection state machine
1970ad83dc6aS[email protected]     hci_connection_t * connection = create_connection_for_addr(device);
1971ad83dc6aS[email protected] 
1972ad83dc6aS[email protected]     if (!connection){
1973ad83dc6aS[email protected]         return BTSTACK_MEMORY_ALLOC_FAILED;
1974ad83dc6aS[email protected]     }
1975ad83dc6aS[email protected] 
1976ad83dc6aS[email protected]     printf("gap_dedicated_bonding 2\n");
1977ad83dc6aS[email protected] 
1978ad83dc6aS[email protected]     // delete linkn key
1979ad83dc6aS[email protected]     hci_drop_link_key_for_bd_addr( (bd_addr_t *) &device);
1980ad83dc6aS[email protected] 
1981ad83dc6aS[email protected]     // configure LEVEL_2/3, dedicated bonding
1982ad83dc6aS[email protected]     connection->state = SEND_CREATE_CONNECTION;
1983ad83dc6aS[email protected]     connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2;
1984ad83dc6aS[email protected]     connection->bonding_flags = BONDING_DEDICATED;
1985ad83dc6aS[email protected] 
1986ad83dc6aS[email protected]     // wait for GAP Security Result and send GAP Dedicated Bonding complete
1987ad83dc6aS[email protected] 
1988ad83dc6aS[email protected]     // handle: connnection failure (connection complete != ok)
1989ad83dc6aS[email protected]     // handle: authentication failure
1990ad83dc6aS[email protected]     // handle: disconnect on done
1991ad83dc6aS[email protected] 
1992ad83dc6aS[email protected]     hci_run();
1993ad83dc6aS[email protected] 
1994ad83dc6aS[email protected]     return 0;
1995ad83dc6aS[email protected] }
1996