xref: /btstack/src/hci.c (revision e80b2cf9cd9c47115bc5aab790e38c01f5894211)
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 
8166fb9560S[email protected] // test helper
8266fb9560S[email protected] static uint8_t disable_l2cap_timeouts = 0;
8366fb9560S[email protected] 
8466fb9560S[email protected] 
8597addcc5Smatthias.ringwald /**
86ee091cf1Smatthias.ringwald  * get connection for a given handle
87ee091cf1Smatthias.ringwald  *
88ee091cf1Smatthias.ringwald  * @return connection OR NULL, if not found
89ee091cf1Smatthias.ringwald  */
905061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){
91ee091cf1Smatthias.ringwald     linked_item_t *it;
923a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
93ee091cf1Smatthias.ringwald         if ( ((hci_connection_t *) it)->con_handle == con_handle){
94ee091cf1Smatthias.ringwald             return (hci_connection_t *) it;
95ee091cf1Smatthias.ringwald         }
96ee091cf1Smatthias.ringwald     }
97ee091cf1Smatthias.ringwald     return NULL;
98ee091cf1Smatthias.ringwald }
99ee091cf1Smatthias.ringwald 
10062bda3fbS[email protected] hci_connection_t * hci_connection_for_bd_addr(bd_addr_t * addr){
10162bda3fbS[email protected]     linked_item_t *it;
10262bda3fbS[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
10362bda3fbS[email protected]         hci_connection_t * connection = (hci_connection_t *) it;
10462bda3fbS[email protected]         if (memcmp(addr, connection->address, 6) == 0) {
10562bda3fbS[email protected]             return connection;
10662bda3fbS[email protected]         }
10762bda3fbS[email protected]     }
10862bda3fbS[email protected]     return NULL;
10962bda3fbS[email protected] }
11062bda3fbS[email protected] 
1112b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){
11228ca2b46S[email protected]     hci_connection_t * connection = (hci_connection_t *) linked_item_get_user(&timer->item);
113c785ef68Smatthias.ringwald #ifdef HAVE_TIME
114ee091cf1Smatthias.ringwald     struct timeval tv;
115ee091cf1Smatthias.ringwald     gettimeofday(&tv, NULL);
116c21e6239Smatthias.ringwald     if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) {
117ee091cf1Smatthias.ringwald         // connections might be timed out
118ee091cf1Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
119ee091cf1Smatthias.ringwald     }
1202b12a0b9Smatthias.ringwald #endif
121e5780900Smatthias.ringwald #ifdef HAVE_TICK
122c785ef68Smatthias.ringwald     if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){
123c785ef68Smatthias.ringwald         // connections might be timed out
124c785ef68Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
125c785ef68Smatthias.ringwald     }
126c785ef68Smatthias.ringwald #endif
127c785ef68Smatthias.ringwald     run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
128c785ef68Smatthias.ringwald     run_loop_add_timer(timer);
129c785ef68Smatthias.ringwald }
130ee091cf1Smatthias.ringwald 
131ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){
132c7492964Smatthias.ringwald #ifdef HAVE_TIME
133ee091cf1Smatthias.ringwald     gettimeofday(&connection->timestamp, NULL);
134c7492964Smatthias.ringwald #endif
135e5780900Smatthias.ringwald #ifdef HAVE_TICK
136c785ef68Smatthias.ringwald     connection->timestamp = embedded_get_ticks();
137c785ef68Smatthias.ringwald #endif
138ee091cf1Smatthias.ringwald }
139ee091cf1Smatthias.ringwald 
140ee091cf1Smatthias.ringwald /**
141c8e4258aSmatthias.ringwald  * create connection for given address
142c8e4258aSmatthias.ringwald  *
14317f1ba2aSmatthias.ringwald  * @return connection OR NULL, if no memory left
144c8e4258aSmatthias.ringwald  */
145c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){
14682d8f825S[email protected] 
14782d8f825S[email protected]     printf("create_connection_for_addr %s\n", bd_addr_to_str(addr));
14828ca2b46S[email protected]     hci_connection_t * conn = (hci_connection_t *) btstack_memory_hci_connection_get();
149c8e4258aSmatthias.ringwald     if (!conn) return NULL;
150c8e4258aSmatthias.ringwald     BD_ADDR_COPY(conn->address, addr);
151c8e4258aSmatthias.ringwald     conn->con_handle = 0xffff;
1527d3b3569Smatthias.ringwald     conn->authentication_flags = AUTH_FLAGS_NONE;
153afd4e962S[email protected]     conn->bonding_flags = 0;
15434d2123cS[email protected]     conn->requested_security_level = LEVEL_0;
155ee091cf1Smatthias.ringwald     linked_item_set_user(&conn->timeout.item, conn);
156ee091cf1Smatthias.ringwald     conn->timeout.process = hci_connection_timeout_handler;
157ee091cf1Smatthias.ringwald     hci_connection_timestamp(conn);
158d55db49eSmatthias.ringwald     conn->acl_recombination_length = 0;
1597856c818Smatthias.ringwald     conn->acl_recombination_pos = 0;
16056cf178bSmatthias.ringwald     conn->num_acl_packets_sent = 0;
1613a9fb326S[email protected]     linked_list_add(&hci_stack->connections, (linked_item_t *) conn);
162c8e4258aSmatthias.ringwald     return conn;
163c8e4258aSmatthias.ringwald }
164c8e4258aSmatthias.ringwald 
165c8e4258aSmatthias.ringwald /**
16606b35ec0Smatthias.ringwald  * get connection for given address
16797addcc5Smatthias.ringwald  *
16897addcc5Smatthias.ringwald  * @return connection OR NULL, if not found
16997addcc5Smatthias.ringwald  */
170fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){
17106b35ec0Smatthias.ringwald     linked_item_t *it;
1723a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
17306b35ec0Smatthias.ringwald         if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){
17406b35ec0Smatthias.ringwald             return (hci_connection_t *) it;
17506b35ec0Smatthias.ringwald         }
17606b35ec0Smatthias.ringwald     }
17706b35ec0Smatthias.ringwald     return NULL;
17806b35ec0Smatthias.ringwald }
17906b35ec0Smatthias.ringwald 
18028ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
18128ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags);
18228ca2b46S[email protected] }
18328ca2b46S[email protected] 
18428ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
18528ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags);
18628ca2b46S[email protected] }
18728ca2b46S[email protected] 
18828ca2b46S[email protected] 
18943bfb1bdSmatthias.ringwald /**
19080ca58a0Smatthias.ringwald  * add authentication flags and reset timer
1917fde4af9Smatthias.ringwald  */
1927fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){
1937fde4af9Smatthias.ringwald     bd_addr_t addr;
1947fde4af9Smatthias.ringwald     bt_flip_addr(addr, *(bd_addr_t *) bd_addr);
1957fde4af9Smatthias.ringwald     hci_connection_t * conn = connection_for_address(addr);
1967fde4af9Smatthias.ringwald     if (conn) {
19728ca2b46S[email protected]         connectionSetAuthenticationFlags(conn, flags);
19880ca58a0Smatthias.ringwald         hci_connection_timestamp(conn);
1997fde4af9Smatthias.ringwald     }
2007fde4af9Smatthias.ringwald }
2017fde4af9Smatthias.ringwald 
20280ca58a0Smatthias.ringwald int  hci_authentication_active_for_handle(hci_con_handle_t handle){
2035061f3afS[email protected]     hci_connection_t * conn = hci_connection_for_handle(handle);
20480ca58a0Smatthias.ringwald     if (!conn) return 0;
2056724cd9eS[email protected]     if (conn->authentication_flags & LEGACY_PAIRING_ACTIVE) return 1;
2066724cd9eS[email protected]     if (conn->authentication_flags & SSP_PAIRING_ACTIVE) return 1;
2076724cd9eS[email protected]     return 0;
20880ca58a0Smatthias.ringwald }
20980ca58a0Smatthias.ringwald 
210c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){
2113a9fb326S[email protected]     if (hci_stack->remote_device_db) {
2123a9fb326S[email protected]         hci_stack->remote_device_db->delete_link_key(addr);
213c12e46e7Smatthias.ringwald     }
214c12e46e7Smatthias.ringwald }
215c12e46e7Smatthias.ringwald 
2167fde4af9Smatthias.ringwald 
2177fde4af9Smatthias.ringwald /**
21843bfb1bdSmatthias.ringwald  * count connections
21943bfb1bdSmatthias.ringwald  */
22040d1c7a4Smatthias.ringwald static int nr_hci_connections(void){
22156c253c9Smatthias.ringwald     int count = 0;
22243bfb1bdSmatthias.ringwald     linked_item_t *it;
2233a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next, count++);
22443bfb1bdSmatthias.ringwald     return count;
22543bfb1bdSmatthias.ringwald }
226c8e4258aSmatthias.ringwald 
22797addcc5Smatthias.ringwald /**
228ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
22916833f0aSmatthias.ringwald  */
2302718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
23116833f0aSmatthias.ringwald }
23216833f0aSmatthias.ringwald 
233998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){
2345061f3afS[email protected]     hci_connection_t * connection = hci_connection_for_handle(handle);
235998906cdSmatthias.ringwald     if (!connection) {
2367d67539fSmatthias.ringwald         log_error("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle);
237998906cdSmatthias.ringwald         return 0;
238998906cdSmatthias.ringwald     }
239998906cdSmatthias.ringwald     return connection->num_acl_packets_sent;
240998906cdSmatthias.ringwald }
241998906cdSmatthias.ringwald 
242998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){
2433a9fb326S[email protected]     uint8_t free_slots = hci_stack->total_num_acl_packets;
244998906cdSmatthias.ringwald     linked_item_t *it;
2453a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
246998906cdSmatthias.ringwald         hci_connection_t * connection = (hci_connection_t *) it;
247998906cdSmatthias.ringwald         if (free_slots < connection->num_acl_packets_sent) {
2487d67539fSmatthias.ringwald             log_error("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n");
249998906cdSmatthias.ringwald             return 0;
250998906cdSmatthias.ringwald         }
251998906cdSmatthias.ringwald         free_slots -= connection->num_acl_packets_sent;
252998906cdSmatthias.ringwald     }
253998906cdSmatthias.ringwald     return free_slots;
254998906cdSmatthias.ringwald }
255998906cdSmatthias.ringwald 
256c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){
2572b12a0b9Smatthias.ringwald 
2582b12a0b9Smatthias.ringwald     // check for async hci transport implementations
2593a9fb326S[email protected]     if (hci_stack->hci_transport->can_send_packet_now){
2603a9fb326S[email protected]         if (!hci_stack->hci_transport->can_send_packet_now(packet_type)){
2612b12a0b9Smatthias.ringwald             return 0;
2622b12a0b9Smatthias.ringwald         }
2632b12a0b9Smatthias.ringwald     }
2642b12a0b9Smatthias.ringwald 
2652b12a0b9Smatthias.ringwald     // check regular Bluetooth flow control
266c24735b1Smatthias.ringwald     switch (packet_type) {
267c24735b1Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
268c24735b1Smatthias.ringwald             return hci_number_free_acl_slots();
269c24735b1Smatthias.ringwald         case HCI_COMMAND_DATA_PACKET:
2703a9fb326S[email protected]             return hci_stack->num_cmd_packets;
271c24735b1Smatthias.ringwald         default:
272c24735b1Smatthias.ringwald             return 0;
273c24735b1Smatthias.ringwald     }
274c24735b1Smatthias.ringwald }
275c24735b1Smatthias.ringwald 
276ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
2777856c818Smatthias.ringwald 
2786218e6f1Smatthias.ringwald     // check for free places on BT module
2795932f1b4Smatthias.ringwald     if (!hci_number_free_acl_slots()) return BTSTACK_ACL_BUFFERS_FULL;
2806218e6f1Smatthias.ringwald 
2817856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
2825061f3afS[email protected]     hci_connection_t *connection = hci_connection_for_handle( con_handle);
28356cf178bSmatthias.ringwald     if (!connection) return 0;
28456cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
28556cf178bSmatthias.ringwald 
28656cf178bSmatthias.ringwald     // count packet
28756cf178bSmatthias.ringwald     connection->num_acl_packets_sent++;
2887b5fbe1fSmatthias.ringwald     // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent);
2897856c818Smatthias.ringwald 
29000d8e42eSmatthias.ringwald     // send packet
2913a9fb326S[email protected]     int err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
2926218e6f1Smatthias.ringwald 
29300d8e42eSmatthias.ringwald     return err;
294ee091cf1Smatthias.ringwald }
295ee091cf1Smatthias.ringwald 
29616833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
2977856c818Smatthias.ringwald 
298e76a89eeS[email protected]     // log_info("acl_handler: size %u", size);
299e76a89eeS[email protected] 
3007856c818Smatthias.ringwald     // get info
3017856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
3025061f3afS[email protected]     hci_connection_t *conn      = hci_connection_for_handle(con_handle);
3037856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
3047856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
3057856c818Smatthias.ringwald 
3067856c818Smatthias.ringwald     // ignore non-registered handle
3077856c818Smatthias.ringwald     if (!conn){
3087d67539fSmatthias.ringwald         log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle);
3097856c818Smatthias.ringwald         return;
3107856c818Smatthias.ringwald     }
3117856c818Smatthias.ringwald 
312e76a89eeS[email protected]     // assert packet is complete
3139ecc3e17S[email protected]     if (acl_length + 4 != size){
314e76a89eeS[email protected]         log_error("hci.c: acl_handler called with ACL packet of wrong size %u, expected %u => dropping packet", size, acl_length + 4);
315e76a89eeS[email protected]         return;
316e76a89eeS[email protected]     }
317e76a89eeS[email protected] 
3187856c818Smatthias.ringwald     // update idle timestamp
3197856c818Smatthias.ringwald     hci_connection_timestamp(conn);
3207856c818Smatthias.ringwald 
3217856c818Smatthias.ringwald     // handle different packet types
3227856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
3237856c818Smatthias.ringwald 
3247856c818Smatthias.ringwald         case 0x01: // continuation fragment
3257856c818Smatthias.ringwald 
3267856c818Smatthias.ringwald             // sanity check
3277856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
3287d67539fSmatthias.ringwald                 log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle);
3297856c818Smatthias.ringwald                 return;
3307856c818Smatthias.ringwald             }
3317856c818Smatthias.ringwald 
3327856c818Smatthias.ringwald             // append fragment payload (header already stored)
3337856c818Smatthias.ringwald             memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
3347856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
3357856c818Smatthias.ringwald 
3367d67539fSmatthias.ringwald             // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length,
337decc01a8Smatthias.ringwald             //        conn->acl_recombination_pos, conn->acl_recombination_length);
3387856c818Smatthias.ringwald 
3397856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
3407856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
3417856c818Smatthias.ringwald 
3423a9fb326S[email protected]                 hci_stack->packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos);
3437856c818Smatthias.ringwald                 // reset recombination buffer
3447856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
3457856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
3467856c818Smatthias.ringwald             }
3477856c818Smatthias.ringwald             break;
3487856c818Smatthias.ringwald 
3497856c818Smatthias.ringwald         case 0x02: { // first fragment
3507856c818Smatthias.ringwald 
3517856c818Smatthias.ringwald             // sanity check
3527856c818Smatthias.ringwald             if (conn->acl_recombination_pos) {
3537d67539fSmatthias.ringwald                 log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
3547856c818Smatthias.ringwald                 return;
3557856c818Smatthias.ringwald             }
3567856c818Smatthias.ringwald 
3577856c818Smatthias.ringwald             // peek into L2CAP packet!
3587856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
3597856c818Smatthias.ringwald 
360e76a89eeS[email protected]             // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
361decc01a8Smatthias.ringwald 
3627856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
3637856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
3647856c818Smatthias.ringwald 
3657856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
3663a9fb326S[email protected]                 hci_stack->packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
3677856c818Smatthias.ringwald 
3687856c818Smatthias.ringwald             } else {
3697856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
3707856c818Smatthias.ringwald                 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
3717856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
3727856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
373decc01a8Smatthias.ringwald                 bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4);
3747856c818Smatthias.ringwald             }
3757856c818Smatthias.ringwald             break;
3767856c818Smatthias.ringwald 
3777856c818Smatthias.ringwald         }
3787856c818Smatthias.ringwald         default:
3797d67539fSmatthias.ringwald             log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03);
3807856c818Smatthias.ringwald             return;
3817856c818Smatthias.ringwald     }
38294ab26f8Smatthias.ringwald 
38394ab26f8Smatthias.ringwald     // execute main loop
38494ab26f8Smatthias.ringwald     hci_run();
38516833f0aSmatthias.ringwald }
38622909952Smatthias.ringwald 
38767a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){
388339b6768Smatthias.ringwald     log_info("Connection closed: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
3893c4d4b90Smatthias.ringwald 
3903c4d4b90Smatthias.ringwald     // cancel all l2cap connections
3913c4d4b90Smatthias.ringwald     hci_emit_disconnection_complete(conn->con_handle, 0x16);    // terminated by local host
3923c4d4b90Smatthias.ringwald 
393c7e0c5f6Smatthias.ringwald     run_loop_remove_timer(&conn->timeout);
394c785ef68Smatthias.ringwald 
3953a9fb326S[email protected]     linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
396a3b02b71Smatthias.ringwald     btstack_memory_hci_connection_free( conn );
3973c4d4b90Smatthias.ringwald 
3983c4d4b90Smatthias.ringwald     // now it's gone
399c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
400c7e0c5f6Smatthias.ringwald }
401c7e0c5f6Smatthias.ringwald 
4020c042179S[email protected] static const uint16_t packet_type_sizes[] = {
4038f8108aaSmatthias.ringwald     0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE,
4048f8108aaSmatthias.ringwald     HCI_ACL_DH1_SIZE, 0, 0, 0,
4058f8108aaSmatthias.ringwald     HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE,
4068f8108aaSmatthias.ringwald     HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE
4078f8108aaSmatthias.ringwald };
40865389bfcS[email protected] static const uint8_t  packet_type_feature_requirement_bit[] = {
40965389bfcS[email protected]      0, // 3 slot packets
41065389bfcS[email protected]      1, // 5 slot packets
41165389bfcS[email protected]     25, // EDR 2 mpbs
41265389bfcS[email protected]     26, // EDR 3 mbps
41365389bfcS[email protected]     39, // 3 slot EDR packts
41465389bfcS[email protected]     40, // 5 slot EDR packet
41565389bfcS[email protected] };
41665389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = {
41765389bfcS[email protected]     0x0f00, // 3 slot packets
41865389bfcS[email protected]     0xf000, // 5 slot packets
41965389bfcS[email protected]     0x1102, // EDR 2 mpbs
42065389bfcS[email protected]     0x2204, // EDR 3 mbps
42165389bfcS[email protected]     0x0300, // 3 slot EDR packts
42265389bfcS[email protected]     0x3000, // 5 slot EDR packet
42365389bfcS[email protected] };
4248f8108aaSmatthias.ringwald 
42565389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){
42665389bfcS[email protected]     // enable packet types based on size
4278f8108aaSmatthias.ringwald     uint16_t packet_types = 0;
4288f8108aaSmatthias.ringwald     int i;
4298f8108aaSmatthias.ringwald     for (i=0;i<16;i++){
4308f8108aaSmatthias.ringwald         if (packet_type_sizes[i] == 0) continue;
4318f8108aaSmatthias.ringwald         if (packet_type_sizes[i] <= buffer_size){
4328f8108aaSmatthias.ringwald             packet_types |= 1 << i;
4338f8108aaSmatthias.ringwald         }
4348f8108aaSmatthias.ringwald     }
43565389bfcS[email protected]     // disable packet types due to missing local supported features
43665389bfcS[email protected]     for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){
43765389bfcS[email protected]         int bit_idx = packet_type_feature_requirement_bit[i];
43865389bfcS[email protected]         int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0;
43965389bfcS[email protected]         if (feature_set) continue;
44065389bfcS[email protected]         log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]);
44165389bfcS[email protected]         packet_types &= ~packet_type_feature_packet_mask[i];
44265389bfcS[email protected]     }
4438f8108aaSmatthias.ringwald     // flip bits for "may not be used"
4448f8108aaSmatthias.ringwald     packet_types ^= 0x3306;
4458f8108aaSmatthias.ringwald     return packet_types;
4468f8108aaSmatthias.ringwald }
4478f8108aaSmatthias.ringwald 
4488f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){
4493a9fb326S[email protected]     return hci_stack->packet_types;
4508f8108aaSmatthias.ringwald }
4518f8108aaSmatthias.ringwald 
4527dc17943Smatthias.ringwald uint8_t* hci_get_outgoing_acl_packet_buffer(void){
4537dc17943Smatthias.ringwald     // hci packet buffer is >= acl data packet length
4543a9fb326S[email protected]     return hci_stack->hci_packet_buffer;
4557dc17943Smatthias.ringwald }
4567dc17943Smatthias.ringwald 
457f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){
4583a9fb326S[email protected]     return hci_stack->acl_data_packet_length;
4597dc17943Smatthias.ringwald }
4607dc17943Smatthias.ringwald 
461f5d8d141S[email protected] int hci_ssp_supported(void){
462f5d8d141S[email protected]     // No 51, byte 6, bit 3
4633a9fb326S[email protected]     return (hci_stack->local_supported_features[6] & (1 << 3)) != 0;
464f5d8d141S[email protected] }
465f5d8d141S[email protected] 
466f5d8d141S[email protected] int hci_classic_supported(void){
467f5d8d141S[email protected]     // No 37, byte 4, bit 5, = No BR/EDR Support
4683a9fb326S[email protected]     return (hci_stack->local_supported_features[4] & (1 << 5)) == 0;
469f5d8d141S[email protected] }
470f5d8d141S[email protected] 
471f5d8d141S[email protected] int hci_le_supported(void){
472f5d8d141S[email protected]     // No 37, byte 4, bit 6 = LE Supported (Controller)
473f5d8d141S[email protected] #ifdef HAVE_BLE
4743a9fb326S[email protected]     return (hci_stack->local_supported_features[4] & (1 << 6)) != 0;
475f5d8d141S[email protected] #else
476f5d8d141S[email protected]     return 0;
477f5d8d141S[email protected] #endif
478f5d8d141S[email protected] }
479f5d8d141S[email protected] 
48069a97523S[email protected] // get addr type and address used in advertisement packets
48118904abdS[email protected] void hci_le_advertisement_address(uint8_t * addr_type, bd_addr_t * addr){
4823a9fb326S[email protected]     *addr_type = hci_stack->adv_addr_type;
4833a9fb326S[email protected]     if (hci_stack->adv_addr_type){
4843a9fb326S[email protected]         memcpy(addr, hci_stack->adv_address, 6);
48569a97523S[email protected]     } else {
4863a9fb326S[email protected]         memcpy(addr, hci_stack->local_bd_addr, 6);
48769a97523S[email protected]     }
48869a97523S[email protected] }
48969a97523S[email protected] 
49074ec757aSmatthias.ringwald // avoid huge local variables
491223aafc1Smatthias.ringwald #ifndef EMBEDDED
49274ec757aSmatthias.ringwald static device_name_t device_name;
493223aafc1Smatthias.ringwald #endif
49416833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
495e76a89eeS[email protected] 
496e76a89eeS[email protected]     uint16_t event_length = packet[1];
497e76a89eeS[email protected] 
498e76a89eeS[email protected]     // assert packet is complete
499e76a89eeS[email protected]     if (size != event_length + 2){
500e76a89eeS[email protected]         log_error("hci.c: event_handler called with event packet of wrong size %u, expected %u => dropping packet", size, event_length + 2);
501e76a89eeS[email protected]         return;
502e76a89eeS[email protected]     }
503e76a89eeS[email protected] 
5041281a47eSmatthias.ringwald     bd_addr_t addr;
5052d00edd4Smatthias.ringwald     uint8_t link_type;
506fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
5071f7b95a1Smatthias.ringwald     hci_connection_t * conn;
50856cf178bSmatthias.ringwald     int i;
50922909952Smatthias.ringwald 
5105909f7f2Smatthias.ringwald     // printf("HCI:EVENT:%02x\n", packet[0]);
5115909f7f2Smatthias.ringwald 
5126772a24cSmatthias.ringwald     switch (packet[0]) {
51322909952Smatthias.ringwald 
5146772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
5157ec5eeaaSmatthias.ringwald             // get num cmd packets
5163a9fb326S[email protected]             // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack->num_cmd_packets, packet[2]);
5173a9fb326S[email protected]             hci_stack->num_cmd_packets = packet[2];
5187ec5eeaaSmatthias.ringwald 
519e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
520e2edc0c3Smatthias.ringwald                 // from offset 5
521e2edc0c3Smatthias.ringwald                 // status
5221d279b20Smatthias.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"
5233a9fb326S[email protected]                 hci_stack->acl_data_packet_length = READ_BT_16(packet, 6);
52456cf178bSmatthias.ringwald                 // ignore: SCO data packet len (8)
5253a9fb326S[email protected]                 hci_stack->total_num_acl_packets  = packet[9];
52656cf178bSmatthias.ringwald                 // ignore: total num SCO packets
5273a9fb326S[email protected]                 if (hci_stack->state == HCI_STATE_INITIALIZING){
528a7a04bd9Smatthias.ringwald                     // determine usable ACL payload size
5293a9fb326S[email protected]                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack->acl_data_packet_length){
5303a9fb326S[email protected]                         hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
5318f8108aaSmatthias.ringwald                     }
53265389bfcS[email protected]                     log_info("hci_read_buffer_size: used size %u, count %u\n",
5333a9fb326S[email protected]                              hci_stack->acl_data_packet_length, hci_stack->total_num_acl_packets);
534e2edc0c3Smatthias.ringwald                 }
53556cf178bSmatthias.ringwald             }
53665a46ef3S[email protected] #ifdef HAVE_BLE
53765a46ef3S[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)){
5383a9fb326S[email protected]                 hci_stack->le_data_packet_length = READ_BT_16(packet, 6);
5393a9fb326S[email protected]                 hci_stack->total_num_le_packets  = packet[8];
5403a9fb326S[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);
54165a46ef3S[email protected]             }
54265a46ef3S[email protected] #endif
543188981d2Smatthias.ringwald             // Dump local address
544188981d2Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) {
5453a9fb326S[email protected]                 bt_flip_addr(hci_stack->local_bd_addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]);
546188981d2Smatthias.ringwald                 log_info("Local Address, Status: 0x%02x: Addr: %s\n",
5473a9fb326S[email protected]                     packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr));
548188981d2Smatthias.ringwald             }
549381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
5503a9fb326S[email protected]                 hci_emit_discoverable_enabled(hci_stack->discoverable);
551381fbed8Smatthias.ringwald             }
55265389bfcS[email protected]             // Note: HCI init checks
553559e517eS[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){
5543a9fb326S[email protected]                 memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8);
555559e517eS[email protected]                 log_info("Local Supported Features: 0x%02x%02x%02x%02x%02x%02x%02x%02x",
5563a9fb326S[email protected]                     hci_stack->local_supported_features[0], hci_stack->local_supported_features[1],
5573a9fb326S[email protected]                     hci_stack->local_supported_features[2], hci_stack->local_supported_features[3],
5583a9fb326S[email protected]                     hci_stack->local_supported_features[4], hci_stack->local_supported_features[5],
5593a9fb326S[email protected]                     hci_stack->local_supported_features[6], hci_stack->local_supported_features[7]);
56065389bfcS[email protected] 
56165389bfcS[email protected]                 // determine usable ACL packet types based buffer size and supported features
5623a9fb326S[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]);
5633a9fb326S[email protected]                 log_info("packet types %04x", hci_stack->packet_types);
564f5d8d141S[email protected] 
565f5d8d141S[email protected]                 // Classic/LE
566f5d8d141S[email protected]                 log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported());
567559e517eS[email protected]             }
56856cf178bSmatthias.ringwald             break;
56956cf178bSmatthias.ringwald 
5707ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
5717ec5eeaaSmatthias.ringwald             // get num cmd packets
5723a9fb326S[email protected]             // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack->num_cmd_packets, packet[3]);
5733a9fb326S[email protected]             hci_stack->num_cmd_packets = packet[3];
5747ec5eeaaSmatthias.ringwald             break;
5757ec5eeaaSmatthias.ringwald 
57656cf178bSmatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
57756cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
57856cf178bSmatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
57956cf178bSmatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
5805061f3afS[email protected]                 conn = hci_connection_for_handle(handle);
58156cf178bSmatthias.ringwald                 if (!conn){
5827d67539fSmatthias.ringwald                     log_error("hci_number_completed_packet lists unused con handle %u\n", handle);
58356cf178bSmatthias.ringwald                     continue;
58456cf178bSmatthias.ringwald                 }
58556cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
5867b5fbe1fSmatthias.ringwald                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
58756cf178bSmatthias.ringwald             }
5886772a24cSmatthias.ringwald             break;
5896772a24cSmatthias.ringwald 
5901f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
59137eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
59237eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
5932d00edd4Smatthias.ringwald             link_type = packet[11];
594339b6768Smatthias.ringwald             log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type);
59537eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
5961f7b95a1Smatthias.ringwald                 conn = connection_for_address(addr);
5971f7b95a1Smatthias.ringwald                 if (!conn) {
5981f7b95a1Smatthias.ringwald                     conn = create_connection_for_addr(addr);
5991f7b95a1Smatthias.ringwald                 }
600ce4c8fabSmatthias.ringwald                 if (!conn) {
601ce4c8fabSmatthias.ringwald                     // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
6023a9fb326S[email protected]                     hci_stack->decline_reason = 0x0d;
6033a9fb326S[email protected]                     BD_ADDR_COPY(hci_stack->decline_addr, addr);
604ce4c8fabSmatthias.ringwald                     break;
605ce4c8fabSmatthias.ringwald                 }
60632ab9390Smatthias.ringwald                 conn->state = RECEIVED_CONNECTION_REQUEST;
60732ab9390Smatthias.ringwald                 hci_run();
60837eaa4cfSmatthias.ringwald             } else {
609ce4c8fabSmatthias.ringwald                 // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A)
6103a9fb326S[email protected]                 hci_stack->decline_reason = 0x0a;
6113a9fb326S[email protected]                 BD_ADDR_COPY(hci_stack->decline_addr, addr);
61237eaa4cfSmatthias.ringwald             }
6131f7b95a1Smatthias.ringwald             break;
6141f7b95a1Smatthias.ringwald 
6156772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
616fe1ed1b8Smatthias.ringwald             // Connection management
617fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
618339b6768Smatthias.ringwald             log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr));
6191f7b95a1Smatthias.ringwald             conn = connection_for_address(addr);
620fe1ed1b8Smatthias.ringwald             if (conn) {
621b448a0e7Smatthias.ringwald                 if (!packet[2]){
622c8e4258aSmatthias.ringwald                     conn->state = OPEN;
623fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
624ad83dc6aS[email protected]                     conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES;
625ee091cf1Smatthias.ringwald 
626c785ef68Smatthias.ringwald                     // restart timer
627c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
628ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
629c785ef68Smatthias.ringwald 
630339b6768Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
63143bfb1bdSmatthias.ringwald 
63243bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
633b448a0e7Smatthias.ringwald                 } else {
634ad83dc6aS[email protected]                     // notify client if dedicated bonding
635ad83dc6aS[email protected]                     if (conn->bonding_flags & BONDING_DEDICATED){
636ad83dc6aS[email protected]                         hci_emit_dedicated_bonding_result(conn, packet[2]);
637ad83dc6aS[email protected]                     }
638ad83dc6aS[email protected] 
639b448a0e7Smatthias.ringwald                     // connection failed, remove entry
6403a9fb326S[email protected]                     linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
641a3b02b71Smatthias.ringwald                     btstack_memory_hci_connection_free( conn );
642c12e46e7Smatthias.ringwald 
643c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
644c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
645c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
646c12e46e7Smatthias.ringwald                     }
647fe1ed1b8Smatthias.ringwald                 }
648fe1ed1b8Smatthias.ringwald             }
6496772a24cSmatthias.ringwald             break;
650fe1ed1b8Smatthias.ringwald 
651afd4e962S[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
652afd4e962S[email protected]             handle = READ_BT_16(packet, 3);
653afd4e962S[email protected]             conn = hci_connection_for_handle(handle);
654afd4e962S[email protected]             if (!conn) break;
655afd4e962S[email protected]             if (!packet[2]){
656afd4e962S[email protected]                 uint8_t * features = &packet[5];
657afd4e962S[email protected]                 if (features[6] & (1 << 3)){
658afd4e962S[email protected]                     conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP;
659afd4e962S[email protected]                 }
660afd4e962S[email protected]             }
661afd4e962S[email protected]             conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES;
662ad83dc6aS[email protected]             log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x", conn->bonding_flags);
663ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
664ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
665ad83dc6aS[email protected]             }
666afd4e962S[email protected]             break;
667afd4e962S[email protected] 
6687fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
6697b5fbe1fSmatthias.ringwald             log_info("HCI_EVENT_LINK_KEY_REQUEST\n");
6707fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
67174d716b5S[email protected]             // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST
6723a9fb326S[email protected]             if (hci_stack->bondable && !hci_stack->remote_device_db) break;
67332ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
67464472d52Smatthias.ringwald             hci_run();
675d9a327f9Smatthias.ringwald             // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set
67629d53098Smatthias.ringwald             return;
6777fde4af9Smatthias.ringwald 
6789ab95c90S[email protected]         case HCI_EVENT_LINK_KEY_NOTIFICATION: {
67929d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
6809ab95c90S[email protected]             conn = connection_for_address(addr);
6819ab95c90S[email protected]             if (!conn) break;
6829ab95c90S[email protected]             conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION;
6839ab95c90S[email protected]             link_key_type_t link_key_type = packet[24];
6849ab95c90S[email protected]             // Change Connection Encryption keeps link key type
6859ab95c90S[email protected]             if (link_key_type != CHANGED_COMBINATION_KEY){
6869ab95c90S[email protected]                 conn->link_key_type = link_key_type;
6879ab95c90S[email protected]             }
6883a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
6893a9fb326S[email protected]             hci_stack->remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8], conn->link_key_type);
69029d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
6917fde4af9Smatthias.ringwald             break;
6929ab95c90S[email protected]         }
6937fde4af9Smatthias.ringwald 
6947fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
6956724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE);
6964c57c146S[email protected]             // non-bondable mode: pin code negative reply will be sent
6973a9fb326S[email protected]             if (!hci_stack->bondable){
698899283eaS[email protected]                 hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST);
699f8fb5f6eS[email protected]                 hci_run();
700f8fb5f6eS[email protected]                 return;
7014c57c146S[email protected]             }
702d9a327f9Smatthias.ringwald             // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key
7033a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
704d9a327f9Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
7053a9fb326S[email protected]             hci_stack->remote_device_db->delete_link_key(&addr);
7067fde4af9Smatthias.ringwald             break;
7077fde4af9Smatthias.ringwald 
7081d6b20aeS[email protected]         case HCI_EVENT_IO_CAPABILITY_REQUEST:
7091d6b20aeS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST);
710dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY);
711dbe1a790S[email protected]             break;
712dbe1a790S[email protected] 
713dbe1a790S[email protected]         case HCI_EVENT_USER_CONFIRMATION_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_CONFIRM_REPLY);
717dbe1a790S[email protected]             break;
718dbe1a790S[email protected] 
719dbe1a790S[email protected]         case HCI_EVENT_USER_PASSKEY_REQUEST:
7206724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
7213a9fb326S[email protected]             if (!hci_stack->ssp_auto_accept) break;
722dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY);
7231d6b20aeS[email protected]             break;
7241d6b20aeS[email protected] 
725f0944df2S[email protected]         case HCI_EVENT_ENCRYPTION_CHANGE:
726f0944df2S[email protected]             handle = READ_BT_16(packet, 3);
727f0944df2S[email protected]             conn = hci_connection_for_handle(handle);
728f0944df2S[email protected]             if (!conn) break;
729ad83dc6aS[email protected]             if (packet[2] == 0) {
730f0944df2S[email protected]                 if (packet[5]){
731f0944df2S[email protected]                     conn->authentication_flags |= CONNECTION_ENCRYPTED;
732f0944df2S[email protected]                 } else {
733536f9994S[email protected]                     conn->authentication_flags &= ~CONNECTION_ENCRYPTED;
734f0944df2S[email protected]                 }
735ad83dc6aS[email protected]             }
736a00031e2S[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
737f0944df2S[email protected]             break;
738f0944df2S[email protected] 
7391eb2563eS[email protected]         case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT:
7401eb2563eS[email protected]             handle = READ_BT_16(packet, 3);
7411eb2563eS[email protected]             conn = hci_connection_for_handle(handle);
7421eb2563eS[email protected]             if (!conn) break;
743ad83dc6aS[email protected] 
744ad83dc6aS[email protected]             // dedicated bonding: send result and disconnect
745ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
746ad83dc6aS[email protected]                 conn->bonding_flags &= ~BONDING_DEDICATED;
747ad83dc6aS[email protected]                 hci_emit_dedicated_bonding_result( conn, packet[2]);
748ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE;
749ad83dc6aS[email protected]                 break;
750ad83dc6aS[email protected]             }
751ad83dc6aS[email protected] 
752b5cb6874S[email protected]             if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){
7531eb2563eS[email protected]                 // link key sufficient for requested security
7541eb2563eS[email protected]                 conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
755ad83dc6aS[email protected]                 break;
756ad83dc6aS[email protected]             }
7571eb2563eS[email protected]             // not enough
7581eb2563eS[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
7591eb2563eS[email protected]             break;
76034d2123cS[email protected] 
761223aafc1Smatthias.ringwald #ifndef EMBEDDED
76274ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
7633a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
76474ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
76574ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
7665a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
7675a06394aSmatthias.ringwald             for (i=0; i<248;i++){
7685a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
7695a06394aSmatthias.ringwald                     packet[9+i] = 0;
7705a06394aSmatthias.ringwald                     break;
771cdc9101dSmatthias.ringwald                 }
7725a06394aSmatthias.ringwald             }
773d2fe945cS[email protected]             memset(&device_name, 0, sizeof(device_name_t));
77474ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
7753a9fb326S[email protected]             hci_stack->remote_device_db->put_name(&addr, &device_name);
77674ec757aSmatthias.ringwald             break;
77774ec757aSmatthias.ringwald 
77874ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
77974ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
7803a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
78174ec757aSmatthias.ringwald             // first send inq result packet
7823a9fb326S[email protected]             hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size);
78374ec757aSmatthias.ringwald             // then send cached remote names
78474ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
78574ec757aSmatthias.ringwald                 bt_flip_addr(addr, &packet[3+i*6]);
7863a9fb326S[email protected]                 if (hci_stack->remote_device_db->get_name(&addr, &device_name)){
78774ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
78874ec757aSmatthias.ringwald                 }
78974ec757aSmatthias.ringwald             }
79074ec757aSmatthias.ringwald             return;
791223aafc1Smatthias.ringwald #endif
79274ec757aSmatthias.ringwald 
7936772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
794fe1ed1b8Smatthias.ringwald             if (!packet[2]){
795fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
7965061f3afS[email protected]                 hci_connection_t * conn = hci_connection_for_handle(handle);
797fe1ed1b8Smatthias.ringwald                 if (conn) {
798c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
799fe1ed1b8Smatthias.ringwald                 }
800fe1ed1b8Smatthias.ringwald             }
8016772a24cSmatthias.ringwald             break;
8026772a24cSmatthias.ringwald 
803c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
8043a9fb326S[email protected]             if(hci_stack->control && hci_stack->control->hw_error){
8053a9fb326S[email protected]                 (*hci_stack->control->hw_error)();
806c68bdf90Smatthias.ringwald             }
807c68bdf90Smatthias.ringwald             break;
808c68bdf90Smatthias.ringwald 
8095909f7f2Smatthias.ringwald #ifdef HAVE_BLE
8105909f7f2Smatthias.ringwald         case HCI_EVENT_LE_META:
8115909f7f2Smatthias.ringwald             switch (packet[2]) {
8125909f7f2Smatthias.ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
8135909f7f2Smatthias.ringwald                     // Connection management
8145909f7f2Smatthias.ringwald                     bt_flip_addr(addr, &packet[8]);
8155909f7f2Smatthias.ringwald                     log_info("LE Connection_complete (status=%u) %s\n", packet[3], bd_addr_to_str(addr));
8165909f7f2Smatthias.ringwald                     // LE connections are auto-accepted, so just create a connection if there isn't one already
8175909f7f2Smatthias.ringwald                     conn = connection_for_address(addr);
8185909f7f2Smatthias.ringwald                     if (packet[3]){
8195909f7f2Smatthias.ringwald                         if (conn){
8205909f7f2Smatthias.ringwald                             // outgoing connection failed, remove entry
8213a9fb326S[email protected]                             linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
8225909f7f2Smatthias.ringwald                             btstack_memory_hci_connection_free( conn );
8235909f7f2Smatthias.ringwald 
8245909f7f2Smatthias.ringwald                         }
8255909f7f2Smatthias.ringwald                         // if authentication error, also delete link key
8265909f7f2Smatthias.ringwald                         if (packet[3] == 0x05) {
8275909f7f2Smatthias.ringwald                             hci_drop_link_key_for_bd_addr(&addr);
8285909f7f2Smatthias.ringwald                         }
8295909f7f2Smatthias.ringwald                         break;
8305909f7f2Smatthias.ringwald                     }
8315909f7f2Smatthias.ringwald                     if (!conn){
8325909f7f2Smatthias.ringwald                         conn = create_connection_for_addr(addr);
8335909f7f2Smatthias.ringwald                     }
8345909f7f2Smatthias.ringwald                     if (!conn){
8355909f7f2Smatthias.ringwald                         // no memory
8365909f7f2Smatthias.ringwald                         break;
8375909f7f2Smatthias.ringwald                     }
8385909f7f2Smatthias.ringwald 
8395909f7f2Smatthias.ringwald                     conn->state = OPEN;
8405909f7f2Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 4);
8415909f7f2Smatthias.ringwald 
8425909f7f2Smatthias.ringwald                     // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock
8435909f7f2Smatthias.ringwald 
8445909f7f2Smatthias.ringwald                     // restart timer
8455909f7f2Smatthias.ringwald                     // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
8465909f7f2Smatthias.ringwald                     // run_loop_add_timer(&conn->timeout);
8475909f7f2Smatthias.ringwald 
8485909f7f2Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
8495909f7f2Smatthias.ringwald 
8505909f7f2Smatthias.ringwald                     hci_emit_nr_connections_changed();
8515909f7f2Smatthias.ringwald                     break;
8525909f7f2Smatthias.ringwald 
85365a46ef3S[email protected]             // printf("LE buffer size: %u, count %u\n", READ_BT_16(packet,6), packet[8]);
85465a46ef3S[email protected] 
8555909f7f2Smatthias.ringwald                 default:
8565909f7f2Smatthias.ringwald                     break;
8575909f7f2Smatthias.ringwald             }
8585909f7f2Smatthias.ringwald             break;
8595909f7f2Smatthias.ringwald #endif
8605909f7f2Smatthias.ringwald 
8616772a24cSmatthias.ringwald         default:
8626772a24cSmatthias.ringwald             break;
863fe1ed1b8Smatthias.ringwald     }
864fe1ed1b8Smatthias.ringwald 
8653429f56bSmatthias.ringwald     // handle BT initialization
8663a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_INITIALIZING){
8673a9fb326S[email protected]         if (hci_stack->substate % 2){
8683429f56bSmatthias.ringwald             // odd: waiting for event
869f155fca3Smatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || packet[0] == HCI_EVENT_COMMAND_STATUS){
870c9af4d3fS[email protected]                 // wait for explicit COMMAND COMPLETE on RESET
8713a9fb326S[email protected]                 if (hci_stack->substate > 1 || COMMAND_COMPLETE_EVENT(packet, hci_reset)) {
8723a9fb326S[email protected]                     hci_stack->substate++;
8733429f56bSmatthias.ringwald                 }
8743429f56bSmatthias.ringwald             }
87522909952Smatthias.ringwald         }
876c9af4d3fS[email protected]     }
87722909952Smatthias.ringwald 
87889db417bSmatthias.ringwald     // help with BT sleep
8793a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_FALLING_ASLEEP
8803a9fb326S[email protected]         && hci_stack->substate == 1
88189db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
8823a9fb326S[email protected]         hci_stack->substate++;
88389db417bSmatthias.ringwald     }
88489db417bSmatthias.ringwald 
8853a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size);
88694ab26f8Smatthias.ringwald 
88794ab26f8Smatthias.ringwald 	// execute main loop
88894ab26f8Smatthias.ringwald 	hci_run();
88916833f0aSmatthias.ringwald }
89016833f0aSmatthias.ringwald 
8910a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
89210e830c9Smatthias.ringwald     switch (packet_type) {
89310e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
89410e830c9Smatthias.ringwald             event_handler(packet, size);
89510e830c9Smatthias.ringwald             break;
89610e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
89710e830c9Smatthias.ringwald             acl_handler(packet, size);
89810e830c9Smatthias.ringwald             break;
89910e830c9Smatthias.ringwald         default:
90010e830c9Smatthias.ringwald             break;
90110e830c9Smatthias.ringwald     }
90210e830c9Smatthias.ringwald }
90310e830c9Smatthias.ringwald 
904fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
9052718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
9063a9fb326S[email protected]     hci_stack->packet_handler = handler;
90716833f0aSmatthias.ringwald }
90816833f0aSmatthias.ringwald 
9094f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){
910475c8125Smatthias.ringwald 
9113a9fb326S[email protected] #ifdef HAVE_MALLOC
9123a9fb326S[email protected]     if (!hci_stack) {
9133a9fb326S[email protected]         hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t));
9143a9fb326S[email protected]     }
9153a9fb326S[email protected] #else
9163a9fb326S[email protected]     hci_stack = &hci_stack_static;
9173a9fb326S[email protected] #endif
91866fb9560S[email protected]     memset(hci_stack, 0, sizeof(hci_stack_t));
9193a9fb326S[email protected] 
920475c8125Smatthias.ringwald     // reference to use transport layer implementation
9213a9fb326S[email protected]     hci_stack->hci_transport = transport;
922475c8125Smatthias.ringwald 
92311e23e5fSmatthias.ringwald     // references to used control implementation
9243a9fb326S[email protected]     hci_stack->control = control;
92511e23e5fSmatthias.ringwald 
92611e23e5fSmatthias.ringwald     // reference to used config
9273a9fb326S[email protected]     hci_stack->config = config;
92811e23e5fSmatthias.ringwald 
929fe1ed1b8Smatthias.ringwald     // no connections yet
9303a9fb326S[email protected]     hci_stack->connections = NULL;
9313a9fb326S[email protected]     hci_stack->discoverable = 0;
9323a9fb326S[email protected]     hci_stack->connectable = 0;
9333a9fb326S[email protected]     hci_stack->bondable = 1;
934758b46ceSmatthias.ringwald 
935b031bebbSmatthias.ringwald     // no pending cmds
9363a9fb326S[email protected]     hci_stack->decline_reason = 0;
9373a9fb326S[email protected]     hci_stack->new_scan_enable_value = 0xff;
938b031bebbSmatthias.ringwald 
93916833f0aSmatthias.ringwald     // higher level handler
9403a9fb326S[email protected]     hci_stack->packet_handler = dummy_handler;
94116833f0aSmatthias.ringwald 
942404843c1Smatthias.ringwald     // store and open remote device db
9433a9fb326S[email protected]     hci_stack->remote_device_db = remote_device_db;
9443a9fb326S[email protected]     if (hci_stack->remote_device_db) {
9453a9fb326S[email protected]         hci_stack->remote_device_db->open();
946404843c1Smatthias.ringwald     }
94729d53098Smatthias.ringwald 
9488fcba05dSmatthias.ringwald     // max acl payload size defined in config.h
9493a9fb326S[email protected]     hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
9508fcba05dSmatthias.ringwald 
95116833f0aSmatthias.ringwald     // register packet handlers with transport
95210e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
953f5454fc6Smatthias.ringwald 
9543a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
955e2386ba1S[email protected] 
956e2386ba1S[email protected]     // class of device
9573a9fb326S[email protected]     hci_stack->class_of_device = 0x007a020c; // Smartphone
958a45d6b9fS[email protected] 
95963048403S[email protected]     // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept
9603a9fb326S[email protected]     hci_stack->ssp_enable = 1;
9613a9fb326S[email protected]     hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT;
9623a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING;
9633a9fb326S[email protected]     hci_stack->ssp_auto_accept = 1;
96469a97523S[email protected] 
96569a97523S[email protected]     // LE
9663a9fb326S[email protected]     hci_stack->adv_addr_type = 0;
9673a9fb326S[email protected]     memset(hci_stack->adv_address, 0, 6);
968475c8125Smatthias.ringwald }
969475c8125Smatthias.ringwald 
970404843c1Smatthias.ringwald void hci_close(){
971404843c1Smatthias.ringwald     // close remote device db
9723a9fb326S[email protected]     if (hci_stack->remote_device_db) {
9733a9fb326S[email protected]         hci_stack->remote_device_db->close();
974404843c1Smatthias.ringwald     }
9753a9fb326S[email protected]     while (hci_stack->connections) {
9763a9fb326S[email protected]         hci_shutdown_connection((hci_connection_t *) hci_stack->connections);
977f5454fc6Smatthias.ringwald     }
978f5454fc6Smatthias.ringwald     hci_power_control(HCI_POWER_OFF);
9793a9fb326S[email protected] 
9803a9fb326S[email protected] #ifdef HAVE_MALLOC
9813a9fb326S[email protected]     free(hci_stack);
9823a9fb326S[email protected] #endif
9833a9fb326S[email protected]     hci_stack = NULL;
984404843c1Smatthias.ringwald }
985404843c1Smatthias.ringwald 
9869e61646fS[email protected] void hci_set_class_of_device(uint32_t class_of_device){
9879e61646fS[email protected]     hci_stack->class_of_device = class_of_device;
9889e61646fS[email protected] }
9899e61646fS[email protected] 
99066fb9560S[email protected] void hci_disable_l2cap_timeout_check(){
99166fb9560S[email protected]     disable_l2cap_timeouts = 1;
99266fb9560S[email protected] }
9938d213e1aSmatthias.ringwald // State-Module-Driver overview
9948d213e1aSmatthias.ringwald // state                    module  low-level
9958d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
9968d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
9978d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
9988d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
999d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
1000d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
1001c7e0c5f6Smatthias.ringwald 
100240d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
10037301ad89Smatthias.ringwald 
1004038bc64cSmatthias.ringwald     // power on
1005f9a30166Smatthias.ringwald     int err = 0;
10063a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->on){
10073a9fb326S[email protected]         err = (*hci_stack->control->on)(hci_stack->config);
1008f9a30166Smatthias.ringwald     }
1009038bc64cSmatthias.ringwald     if (err){
10107d67539fSmatthias.ringwald         log_error( "POWER_ON failed\n");
1011038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
1012038bc64cSmatthias.ringwald         return err;
1013038bc64cSmatthias.ringwald     }
1014038bc64cSmatthias.ringwald 
1015038bc64cSmatthias.ringwald     // open low-level device
10163a9fb326S[email protected]     err = hci_stack->hci_transport->open(hci_stack->config);
1017038bc64cSmatthias.ringwald     if (err){
10187d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
10193a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
10203a9fb326S[email protected]             (*hci_stack->control->off)(hci_stack->config);
1021f9a30166Smatthias.ringwald         }
1022038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
1023038bc64cSmatthias.ringwald         return err;
1024038bc64cSmatthias.ringwald     }
10258d213e1aSmatthias.ringwald     return 0;
10268d213e1aSmatthias.ringwald }
1027038bc64cSmatthias.ringwald 
102840d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
10298d213e1aSmatthias.ringwald 
10307b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off\n");
10319418f9c9Smatthias.ringwald 
10328d213e1aSmatthias.ringwald     // close low-level device
10333a9fb326S[email protected]     hci_stack->hci_transport->close(hci_stack->config);
10348d213e1aSmatthias.ringwald 
10357b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - hci_transport closed\n");
10369418f9c9Smatthias.ringwald 
10378d213e1aSmatthias.ringwald     // power off
10383a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->off){
10393a9fb326S[email protected]         (*hci_stack->control->off)(hci_stack->config);
10408d213e1aSmatthias.ringwald     }
10419418f9c9Smatthias.ringwald 
10427b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - control closed\n");
10439418f9c9Smatthias.ringwald 
10443a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
104572ea5239Smatthias.ringwald }
104672ea5239Smatthias.ringwald 
104740d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
104872ea5239Smatthias.ringwald 
10497b5fbe1fSmatthias.ringwald     log_info("hci_power_control_sleep\n");
10503144bce4Smatthias.ringwald 
1051b429b9b7Smatthias.ringwald #if 0
1052b429b9b7Smatthias.ringwald     // don't close serial port during sleep
1053b429b9b7Smatthias.ringwald 
105472ea5239Smatthias.ringwald     // close low-level device
10553a9fb326S[email protected]     hci_stack->hci_transport->close(hci_stack->config);
1056b429b9b7Smatthias.ringwald #endif
105772ea5239Smatthias.ringwald 
105872ea5239Smatthias.ringwald     // sleep mode
10593a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->sleep){
10603a9fb326S[email protected]         (*hci_stack->control->sleep)(hci_stack->config);
106172ea5239Smatthias.ringwald     }
1062b429b9b7Smatthias.ringwald 
10633a9fb326S[email protected]     hci_stack->state = HCI_STATE_SLEEPING;
10648d213e1aSmatthias.ringwald }
10658d213e1aSmatthias.ringwald 
106640d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
1067b429b9b7Smatthias.ringwald 
10687b5fbe1fSmatthias.ringwald     log_info("hci_power_control_wake\n");
1069b429b9b7Smatthias.ringwald 
1070b429b9b7Smatthias.ringwald     // wake on
10713a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->wake){
10723a9fb326S[email protected]         (*hci_stack->control->wake)(hci_stack->config);
1073b429b9b7Smatthias.ringwald     }
1074b429b9b7Smatthias.ringwald 
1075b429b9b7Smatthias.ringwald #if 0
1076b429b9b7Smatthias.ringwald     // open low-level device
10773a9fb326S[email protected]     int err = hci_stack->hci_transport->open(hci_stack->config);
1078b429b9b7Smatthias.ringwald     if (err){
10797d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
10803a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
10813a9fb326S[email protected]             (*hci_stack->control->off)(hci_stack->config);
1082b429b9b7Smatthias.ringwald         }
1083b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
1084b429b9b7Smatthias.ringwald         return err;
1085b429b9b7Smatthias.ringwald     }
1086b429b9b7Smatthias.ringwald #endif
1087b429b9b7Smatthias.ringwald 
1088b429b9b7Smatthias.ringwald     return 0;
1089b429b9b7Smatthias.ringwald }
1090b429b9b7Smatthias.ringwald 
1091b429b9b7Smatthias.ringwald 
10928d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
1093d661ed19Smatthias.ringwald 
10943a9fb326S[email protected]     log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack->state);
1095d661ed19Smatthias.ringwald 
10968d213e1aSmatthias.ringwald     int err = 0;
10973a9fb326S[email protected]     switch (hci_stack->state){
10988d213e1aSmatthias.ringwald 
10998d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
11008d213e1aSmatthias.ringwald             switch (power_mode){
11018d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
11028d213e1aSmatthias.ringwald                     err = hci_power_control_on();
11038d213e1aSmatthias.ringwald                     if (err) return err;
11047301ad89Smatthias.ringwald                     // set up state machine
11053a9fb326S[email protected]                     hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent
11063a9fb326S[email protected]                     hci_stack->state = HCI_STATE_INITIALIZING;
11073a9fb326S[email protected]                     hci_stack->substate = 0;
11088d213e1aSmatthias.ringwald                     break;
11098d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
11108d213e1aSmatthias.ringwald                     // do nothing
11118d213e1aSmatthias.ringwald                     break;
11128d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1113b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
11148d213e1aSmatthias.ringwald                     break;
11158d213e1aSmatthias.ringwald             }
11168d213e1aSmatthias.ringwald             break;
11177301ad89Smatthias.ringwald 
11188d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
11198d213e1aSmatthias.ringwald             switch (power_mode){
11208d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
11218d213e1aSmatthias.ringwald                     // do nothing
11228d213e1aSmatthias.ringwald                     break;
11238d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
11248d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
11258d213e1aSmatthias.ringwald                     hci_power_control_off();
11268d213e1aSmatthias.ringwald                     break;
11278d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1128b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
112972ea5239Smatthias.ringwald                     hci_power_control_sleep();
11308d213e1aSmatthias.ringwald                     break;
11318d213e1aSmatthias.ringwald             }
11328d213e1aSmatthias.ringwald             break;
11337301ad89Smatthias.ringwald 
11348d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
11358d213e1aSmatthias.ringwald             switch (power_mode){
11368d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
11378d213e1aSmatthias.ringwald                     // do nothing
11388d213e1aSmatthias.ringwald                     break;
11398d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1140c7e0c5f6Smatthias.ringwald                     // see hci_run
11413a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
11428d213e1aSmatthias.ringwald                     break;
11438d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1144b546ac54Smatthias.ringwald                     // see hci_run
11453a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
11463a9fb326S[email protected]                     hci_stack->substate = 0;
11478d213e1aSmatthias.ringwald                     break;
11488d213e1aSmatthias.ringwald             }
11498d213e1aSmatthias.ringwald             break;
11507301ad89Smatthias.ringwald 
11518d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
11528d213e1aSmatthias.ringwald             switch (power_mode){
11538d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
11548d213e1aSmatthias.ringwald                     // set up state machine
11553a9fb326S[email protected]                     hci_stack->state = HCI_STATE_INITIALIZING;
11563a9fb326S[email protected]                     hci_stack->substate = 0;
11578d213e1aSmatthias.ringwald                     break;
11588d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
11598d213e1aSmatthias.ringwald                     // do nothing
11608d213e1aSmatthias.ringwald                     break;
11618d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1162b546ac54Smatthias.ringwald                     // see hci_run
11633a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
11643a9fb326S[email protected]                     hci_stack->substate = 0;
11658d213e1aSmatthias.ringwald                     break;
11668d213e1aSmatthias.ringwald             }
11678d213e1aSmatthias.ringwald             break;
11688d213e1aSmatthias.ringwald 
11698d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
11708d213e1aSmatthias.ringwald             switch (power_mode){
11718d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
117228171530Smatthias.ringwald 
117328171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
117428171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
117528171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
11763a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
11773a9fb326S[email protected]                         hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
117828171530Smatthias.ringwald                         break;
117928171530Smatthias.ringwald                     }
118028171530Smatthias.ringwald #endif
1181b546ac54Smatthias.ringwald                     // set up state machine
11823a9fb326S[email protected]                     hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent
11833a9fb326S[email protected]                     hci_stack->state = HCI_STATE_INITIALIZING;
11843a9fb326S[email protected]                     hci_stack->substate = 0;
11858d213e1aSmatthias.ringwald                     break;
11868d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1187b546ac54Smatthias.ringwald                     // see hci_run
11883a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
11898d213e1aSmatthias.ringwald                     break;
11908d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1191b546ac54Smatthias.ringwald                     // do nothing
11928d213e1aSmatthias.ringwald                     break;
11938d213e1aSmatthias.ringwald             }
11948d213e1aSmatthias.ringwald             break;
11958d213e1aSmatthias.ringwald 
11968d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
11978d213e1aSmatthias.ringwald             switch (power_mode){
11988d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
119928171530Smatthias.ringwald 
120028171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
120128171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
120228171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
12033a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
12043a9fb326S[email protected]                         hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
1205758b46ceSmatthias.ringwald                         hci_update_scan_enable();
120628171530Smatthias.ringwald                         break;
120728171530Smatthias.ringwald                     }
120828171530Smatthias.ringwald #endif
12093144bce4Smatthias.ringwald                     err = hci_power_control_wake();
12103144bce4Smatthias.ringwald                     if (err) return err;
1211b546ac54Smatthias.ringwald                     // set up state machine
12123a9fb326S[email protected]                     hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent
12133a9fb326S[email protected]                     hci_stack->state = HCI_STATE_INITIALIZING;
12143a9fb326S[email protected]                     hci_stack->substate = 0;
12158d213e1aSmatthias.ringwald                     break;
12168d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
12173a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
12188d213e1aSmatthias.ringwald                     break;
12198d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1220b546ac54Smatthias.ringwald                     // do nothing
12218d213e1aSmatthias.ringwald                     break;
12228d213e1aSmatthias.ringwald             }
12238d213e1aSmatthias.ringwald             break;
122411e23e5fSmatthias.ringwald     }
122568d92d03Smatthias.ringwald 
1226038bc64cSmatthias.ringwald     // create internal event
1227ee8bf225Smatthias.ringwald 	hci_emit_state();
1228ee8bf225Smatthias.ringwald 
122968d92d03Smatthias.ringwald 	// trigger next/first action
123068d92d03Smatthias.ringwald 	hci_run();
123168d92d03Smatthias.ringwald 
1232475c8125Smatthias.ringwald     return 0;
1233475c8125Smatthias.ringwald }
1234475c8125Smatthias.ringwald 
1235758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){
1236758b46ceSmatthias.ringwald     // 2 = page scan, 1 = inq scan
12373a9fb326S[email protected]     hci_stack->new_scan_enable_value  = hci_stack->connectable << 1 | hci_stack->discoverable;
1238758b46ceSmatthias.ringwald     hci_run();
1239758b46ceSmatthias.ringwald }
1240758b46ceSmatthias.ringwald 
1241381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
1242381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
1243381fbed8Smatthias.ringwald 
12443a9fb326S[email protected]     if (hci_stack->discoverable == enable){
12453a9fb326S[email protected]         hci_emit_discoverable_enabled(hci_stack->discoverable);
1246381fbed8Smatthias.ringwald         return;
1247381fbed8Smatthias.ringwald     }
1248381fbed8Smatthias.ringwald 
12493a9fb326S[email protected]     hci_stack->discoverable = enable;
1250758b46ceSmatthias.ringwald     hci_update_scan_enable();
1251758b46ceSmatthias.ringwald }
1252b031bebbSmatthias.ringwald 
1253758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){
1254758b46ceSmatthias.ringwald     if (enable) enable = 1; // normalize argument
1255758b46ceSmatthias.ringwald 
1256758b46ceSmatthias.ringwald     // don't emit event
12573a9fb326S[email protected]     if (hci_stack->connectable == enable) return;
1258758b46ceSmatthias.ringwald 
12593a9fb326S[email protected]     hci_stack->connectable = enable;
1260758b46ceSmatthias.ringwald     hci_update_scan_enable();
1261381fbed8Smatthias.ringwald }
1262381fbed8Smatthias.ringwald 
12635061f3afS[email protected] bd_addr_t * hci_local_bd_addr(void){
12643a9fb326S[email protected]     return &hci_stack->local_bd_addr;
12655061f3afS[email protected] }
12665061f3afS[email protected] 
126706b35ec0Smatthias.ringwald void hci_run(){
12688a485f27Smatthias.ringwald 
126932ab9390Smatthias.ringwald     hci_connection_t * connection;
127032ab9390Smatthias.ringwald     linked_item_t * it;
127132ab9390Smatthias.ringwald 
1272ce4c8fabSmatthias.ringwald     if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
1273ce4c8fabSmatthias.ringwald 
1274b031bebbSmatthias.ringwald     // global/non-connection oriented commands
1275b031bebbSmatthias.ringwald 
1276b031bebbSmatthias.ringwald     // decline incoming connections
12773a9fb326S[email protected]     if (hci_stack->decline_reason){
12783a9fb326S[email protected]         uint8_t reason = hci_stack->decline_reason;
12793a9fb326S[email protected]         hci_stack->decline_reason = 0;
12803a9fb326S[email protected]         hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason);
1281dbe1a790S[email protected]         return;
1282ce4c8fabSmatthias.ringwald     }
1283ce4c8fabSmatthias.ringwald 
1284b031bebbSmatthias.ringwald     // send scan enable
12853a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_WORKING && hci_stack->new_scan_enable_value != 0xff && hci_classic_supported()){
12863a9fb326S[email protected]         hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value);
12873a9fb326S[email protected]         hci_stack->new_scan_enable_value = 0xff;
1288dbe1a790S[email protected]         return;
1289b031bebbSmatthias.ringwald     }
1290b031bebbSmatthias.ringwald 
129132ab9390Smatthias.ringwald     // send pending HCI commands
12923a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
129332ab9390Smatthias.ringwald 
1294b031bebbSmatthias.ringwald         connection = (hci_connection_t *) it;
129532ab9390Smatthias.ringwald 
1296ad83dc6aS[email protected]         if (connection->state == SEND_CREATE_CONNECTION){
1297ad83dc6aS[email protected]             log_info("sending hci_create_connection\n");
1298ad83dc6aS[email protected]             hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
1299ad83dc6aS[email protected]             return;
1300ad83dc6aS[email protected]         }
1301ad83dc6aS[email protected] 
130232ab9390Smatthias.ringwald         if (connection->state == RECEIVED_CONNECTION_REQUEST){
13037b5fbe1fSmatthias.ringwald             log_info("sending hci_accept_connection_request\n");
130432ab9390Smatthias.ringwald             connection->state = ACCEPTED_CONNECTION_REQUEST;
130534d2123cS[email protected]             hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
1306dbe1a790S[email protected]             return;
1307c7e0c5f6Smatthias.ringwald         }
1308c7e0c5f6Smatthias.ringwald 
130932ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
131034d2123cS[email protected]             log_info("responding to link key request\n");
131134d2123cS[email protected]             connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST);
131232ab9390Smatthias.ringwald             link_key_t link_key;
1313c77e8838S[email protected]             link_key_type_t link_key_type;
13143a9fb326S[email protected]             if ( hci_stack->remote_device_db
13153a9fb326S[email protected]               && hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)
131634d2123cS[email protected]               && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){
13179ab95c90S[email protected]                connection->link_key_type = link_key_type;
131832ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
131932ab9390Smatthias.ringwald             } else {
132032ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
132132ab9390Smatthias.ringwald             }
1322dbe1a790S[email protected]             return;
132332ab9390Smatthias.ringwald         }
13241d6b20aeS[email protected] 
1325899283eaS[email protected]         if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){
13264c57c146S[email protected]             log_info("denying to pin request\n");
1327899283eaS[email protected]             connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST);
132834d2123cS[email protected]             hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address);
13294c57c146S[email protected]             return;
13304c57c146S[email protected]         }
13314c57c146S[email protected] 
1332dbe1a790S[email protected]         if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){
133334d2123cS[email protected]             connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY);
133482d8f825S[email protected]             log_info("IO Capability Request received, stack bondable %u, io cap %u", hci_stack->bondable, hci_stack->ssp_io_capability);
133582d8f825S[email protected]             if (hci_stack->bondable && (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN)){
1336106d6d11S[email protected]                 // tweak authentication requirements
13373a9fb326S[email protected]                 uint8_t authreq = hci_stack->ssp_authentication_requirement;
1338106d6d11S[email protected]                 if (connection->bonding_flags & BONDING_DEDICATED){
13399faad3abS[email protected]                     authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING;
13409faad3abS[email protected]                 }
13419faad3abS[email protected]                 if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){
13429faad3abS[email protected]                     authreq |= 1;
1343106d6d11S[email protected]                 }
13443a9fb326S[email protected]                 hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, NULL, authreq);
1345f8fb5f6eS[email protected]             } else {
1346f8fb5f6eS[email protected]                 hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED);
1347f8fb5f6eS[email protected]             }
1348dbe1a790S[email protected]             return;
134932ab9390Smatthias.ringwald         }
135032ab9390Smatthias.ringwald 
1351dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){
1352dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY);
135334d2123cS[email protected]             hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address);
1354dbe1a790S[email protected]             return;
1355dbe1a790S[email protected]         }
1356dbe1a790S[email protected] 
1357dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){
1358dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY);
135934d2123cS[email protected]             hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000);
1360dbe1a790S[email protected]             return;
1361dbe1a790S[email protected]         }
1362afd4e962S[email protected] 
1363afd4e962S[email protected]         if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){
1364afd4e962S[email protected]             connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES;
136534d2123cS[email protected]             hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle);
13662bd8b7e7S[email protected]             return;
13672bd8b7e7S[email protected]         }
13682bd8b7e7S[email protected] 
13692bd8b7e7S[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){
13702bd8b7e7S[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK;
137134d2123cS[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005);  // authentication failure
137234d2123cS[email protected]             return;
137334d2123cS[email protected]         }
1374ad83dc6aS[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){
1375ad83dc6aS[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE;
1376ad83dc6aS[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0);  // authentication done
1377ad83dc6aS[email protected]             return;
1378ad83dc6aS[email protected]         }
137934d2123cS[email protected]         if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){
138034d2123cS[email protected]             connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST;
138134d2123cS[email protected]             hci_send_cmd(&hci_authentication_requested, connection->con_handle);
13822bd8b7e7S[email protected]             return;
1383afd4e962S[email protected]         }
1384dce78009S[email protected]         if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){
1385dce78009S[email protected]             connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST;
1386dce78009S[email protected]             hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1);
1387dce78009S[email protected]             return;
1388dce78009S[email protected]         }
1389dbe1a790S[email protected]     }
1390c7e0c5f6Smatthias.ringwald 
13913a9fb326S[email protected]     switch (hci_stack->state){
13923429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
13933a9fb326S[email protected]             // log_info("hci_init: substate %u\n", hci_stack->substate);
13943a9fb326S[email protected]             if (hci_stack->substate % 2) {
13953429f56bSmatthias.ringwald                 // odd: waiting for command completion
139606b35ec0Smatthias.ringwald                 return;
13973429f56bSmatthias.ringwald             }
13983a9fb326S[email protected]             switch (hci_stack->substate >> 1){
1399c7492964Smatthias.ringwald                 case 0: // RESET
140022909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
140124052c2aS[email protected] 
14023a9fb326S[email protected]                     if (hci_stack->config == 0 || ((hci_uart_config_t *)hci_stack->config)->baudrate_main == 0){
1403c7492964Smatthias.ringwald                         // skip baud change
14043a9fb326S[email protected]                         hci_stack->substate = 4; // >> 1 = 2
1405c7492964Smatthias.ringwald                     }
14063429f56bSmatthias.ringwald                     break;
1407c7492964Smatthias.ringwald                 case 1: // SEND BAUD CHANGE
14083a9fb326S[email protected]                     hci_stack->control->baudrate_cmd(hci_stack->config, ((hci_uart_config_t *)hci_stack->config)->baudrate_main, hci_stack->hci_packet_buffer);
14093a9fb326S[email protected]                     hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]);
1410c7492964Smatthias.ringwald                     break;
1411c7492964Smatthias.ringwald                 case 2: // LOCAL BAUD CHANGE
14123a9fb326S[email protected]                     hci_stack->hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack->config)->baudrate_main);
14133a9fb326S[email protected]                     hci_stack->substate += 2;
1414c7492964Smatthias.ringwald                     // break missing here for fall through
1415c7492964Smatthias.ringwald 
1416c7492964Smatthias.ringwald                 case 3:
141724052c2aS[email protected]                     // Custom initialization
14183a9fb326S[email protected]                     if (hci_stack->control && hci_stack->control->next_cmd){
14193a9fb326S[email protected]                         int valid_cmd = (*hci_stack->control->next_cmd)(hci_stack->config, hci_stack->hci_packet_buffer);
1420d62aca9fSmatthias.ringwald                         if (valid_cmd){
14213a9fb326S[email protected]                             int size = 3 + hci_stack->hci_packet_buffer[2];
14223a9fb326S[email protected]                             hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size);
14233a9fb326S[email protected]                             hci_stack->substate = 4; // more init commands
142490919203Smatthias.ringwald                             break;
142590919203Smatthias.ringwald                         }
1426d62aca9fSmatthias.ringwald                         log_info("hci_run: init script done\n\r");
142790919203Smatthias.ringwald                     }
14282f6c30e1Smatthias.ringwald                     // otherwise continue
1429f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
1430f432a6ddSmatthias.ringwald 					break;
1431c7492964Smatthias.ringwald 				case 4:
1432e2edc0c3Smatthias.ringwald 					hci_send_cmd(&hci_read_buffer_size);
1433e2edc0c3Smatthias.ringwald 					break;
1434c7492964Smatthias.ringwald                 case 5:
143565389bfcS[email protected]                     hci_send_cmd(&hci_read_local_supported_features);
14363429f56bSmatthias.ringwald                     break;
1437c7492964Smatthias.ringwald                 case 6:
1438d7e0e3a8S[email protected]                     if (hci_le_supported()){
1439d7e0e3a8S[email protected]                         hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF);
1440d7e0e3a8S[email protected]                     } else {
1441d7e0e3a8S[email protected]                         // Kensington Bluetoot 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff...
1442d7e0e3a8S[email protected]                         hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF);
1443d7e0e3a8S[email protected]                     }
1444f5d8d141S[email protected] 
1445f5d8d141S[email protected]                     // skip Classic init commands for LE only chipsets
144624052c2aS[email protected]                     if (!hci_classic_supported()){
144724052c2aS[email protected]                         if (hci_le_supported()){
14483a9fb326S[email protected]                             hci_stack->substate = 11 << 1;    // skip all classic command
144924052c2aS[email protected]                         } else {
145024052c2aS[email protected]                             log_error("Neither BR/EDR nor LE supported");
14513a9fb326S[email protected]                             hci_stack->substate = 13 << 1;    // skip all
145224052c2aS[email protected]                         }
1453f5d8d141S[email protected]                     }
1454f5d8d141S[email protected]                     break;
1455f5d8d141S[email protected]                 case 7:
145624052c2aS[email protected]                     if (hci_ssp_supported()){
14573a9fb326S[email protected]                         hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable);
1458f5d8d141S[email protected]                         break;
145924052c2aS[email protected]                     }
14603a9fb326S[email protected]                     hci_stack->substate += 2;
146124052c2aS[email protected]                     // break missing here for fall through
146224052c2aS[email protected] 
1463f5d8d141S[email protected]                 case 8:
146465389bfcS[email protected]                     // ca. 15 sec
146565389bfcS[email protected]                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
1466559e517eS[email protected]                     break;
1467f5d8d141S[email protected]                 case 9:
14683a9fb326S[email protected]                     hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device);
1469e2386ba1S[email protected]                     break;
1470f5d8d141S[email protected]                 case 10:
14713a9fb326S[email protected]                     if (hci_stack->local_name){
14723a9fb326S[email protected]                         hci_send_cmd(&hci_write_local_name, hci_stack->local_name);
1473e2386ba1S[email protected]                     } else {
14748a485f27Smatthias.ringwald                         char hostname[30];
1475e2386ba1S[email protected] #ifdef EMBEDDED
1476e2386ba1S[email protected]                         // BTstack-11:22:33:44:55:66
1477e2386ba1S[email protected]                         strcpy(hostname, "BTstack ");
14783a9fb326S[email protected]                         strcat(hostname, bd_addr_to_str(hci_stack->local_bd_addr));
1479e2386ba1S[email protected]                         printf("---> Name %s\n", hostname);
1480e2386ba1S[email protected] #else
1481e2386ba1S[email protected]                         // hostname for POSIX systems
14828a485f27Smatthias.ringwald                         gethostname(hostname, 30);
14838a485f27Smatthias.ringwald                         hostname[29] = '\0';
1484e2386ba1S[email protected] #endif
14858a485f27Smatthias.ringwald                         hci_send_cmd(&hci_write_local_name, hostname);
14868a485f27Smatthias.ringwald                     }
14873c4d4b90Smatthias.ringwald                     break;
1488e0dbca83S[email protected]                 case 11:
14893a9fb326S[email protected] 					hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan
149024052c2aS[email protected]                     if (!hci_le_supported()){
149124052c2aS[email protected]                         // SKIP LE init for Classic only configuration
14923a9fb326S[email protected]                         hci_stack->substate = 13 << 1;
149324052c2aS[email protected]                     }
1494a45d6b9fS[email protected] 					break;
149524052c2aS[email protected] 
149643aa7007S[email protected] #ifdef HAVE_BLE
149724052c2aS[email protected]                 // LE INIT
1498a45d6b9fS[email protected]                 case 12:
149924052c2aS[email protected]                     hci_send_cmd(&hci_le_read_buffer_size);
150024052c2aS[email protected]                     break;
150124052c2aS[email protected]                 case 13:
150224052c2aS[email protected]                     // LE Supported Host = 1, Simultaneous Host = 0
150324052c2aS[email protected]                     hci_send_cmd(&hci_write_le_host_supported, 1, 0);
150424052c2aS[email protected]                     break;
150543aa7007S[email protected] #endif
150624052c2aS[email protected] 
150724052c2aS[email protected]                 // DONE
150824052c2aS[email protected]                 case 14:
15093429f56bSmatthias.ringwald                     // done.
15103a9fb326S[email protected]                     hci_stack->state = HCI_STATE_WORKING;
1511b360b6adSmatthias.ringwald                     hci_emit_state();
15123429f56bSmatthias.ringwald                     break;
15133429f56bSmatthias.ringwald                 default:
15143429f56bSmatthias.ringwald                     break;
1515475c8125Smatthias.ringwald             }
15163a9fb326S[email protected]             hci_stack->substate++;
15173429f56bSmatthias.ringwald             break;
1518c7e0c5f6Smatthias.ringwald 
1519c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
1520c7e0c5f6Smatthias.ringwald 
15217b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING\n");
1522c7e0c5f6Smatthias.ringwald             // close all open connections
15233a9fb326S[email protected]             connection =  (hci_connection_t *) hci_stack->connections;
1524c7e0c5f6Smatthias.ringwald             if (connection){
152532ab9390Smatthias.ringwald 
1526c7e0c5f6Smatthias.ringwald                 // send disconnect
152732ab9390Smatthias.ringwald                 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
152832ab9390Smatthias.ringwald 
152979bbfa0bSmatthias.ringwald                 log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
15306ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
1531c7e0c5f6Smatthias.ringwald 
1532c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
1533c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
1534c7e0c5f6Smatthias.ringwald                 return;
1535c7e0c5f6Smatthias.ringwald             }
15367b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, calling off\n");
1537c7e0c5f6Smatthias.ringwald 
153872ea5239Smatthias.ringwald             // switch mode
1539c7e0c5f6Smatthias.ringwald             hci_power_control_off();
15409418f9c9Smatthias.ringwald 
15417b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, emitting state\n");
154272ea5239Smatthias.ringwald             hci_emit_state();
15437b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, done\n");
154472ea5239Smatthias.ringwald             break;
1545c7e0c5f6Smatthias.ringwald 
154672ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
15473a9fb326S[email protected]             switch(hci_stack->substate) {
154889db417bSmatthias.ringwald                 case 0:
15497b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_FALLING_ASLEEP\n");
155072ea5239Smatthias.ringwald                     // close all open connections
15513a9fb326S[email protected]                     connection =  (hci_connection_t *) hci_stack->connections;
155266da7044Smatthias.ringwald 
155328171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
155466da7044Smatthias.ringwald                     // don't close connections, if H4 supports power management
155566da7044Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
155666da7044Smatthias.ringwald                         connection = NULL;
155766da7044Smatthias.ringwald                     }
155866da7044Smatthias.ringwald #endif
155972ea5239Smatthias.ringwald                     if (connection){
156032ab9390Smatthias.ringwald 
156172ea5239Smatthias.ringwald                         // send disconnect
156232ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
156332ab9390Smatthias.ringwald 
156479bbfa0bSmatthias.ringwald                         log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
15656ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
156672ea5239Smatthias.ringwald 
156772ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
156872ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
156972ea5239Smatthias.ringwald                         return;
157072ea5239Smatthias.ringwald                     }
157172ea5239Smatthias.ringwald 
157292368cd3S[email protected]                     if (hci_classic_supported()){
157389db417bSmatthias.ringwald                         // disable page and inquiry scan
157432ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
157532ab9390Smatthias.ringwald 
157692368cd3S[email protected]                         log_info("HCI_STATE_HALTING, disabling inq scans\n");
15773a9fb326S[email protected]                         hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan
157889db417bSmatthias.ringwald 
157989db417bSmatthias.ringwald                         // continue in next sub state
15803a9fb326S[email protected]                         hci_stack->substate++;
158189db417bSmatthias.ringwald                         break;
158292368cd3S[email protected]                     }
158392368cd3S[email protected]                     // fall through for ble-only chips
158492368cd3S[email protected] 
158589db417bSmatthias.ringwald                 case 2:
15867b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, calling sleep\n");
158728171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
158828171530Smatthias.ringwald                     // don't actually go to sleep, if H4 supports power management
158928171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
159028171530Smatthias.ringwald                         // SLEEP MODE reached
15913a9fb326S[email protected]                         hci_stack->state = HCI_STATE_SLEEPING;
159228171530Smatthias.ringwald                         hci_emit_state();
159328171530Smatthias.ringwald                         break;
159428171530Smatthias.ringwald                     }
159528171530Smatthias.ringwald #endif
159672ea5239Smatthias.ringwald                     // switch mode
15973a9fb326S[email protected]                     hci_power_control_sleep();  // changes hci_stack->state to SLEEP
1598c7e0c5f6Smatthias.ringwald                     hci_emit_state();
159928171530Smatthias.ringwald                     break;
160028171530Smatthias.ringwald 
160189db417bSmatthias.ringwald                 default:
160289db417bSmatthias.ringwald                     break;
160389db417bSmatthias.ringwald             }
1604c7e0c5f6Smatthias.ringwald             break;
1605c7e0c5f6Smatthias.ringwald 
16063429f56bSmatthias.ringwald         default:
16073429f56bSmatthias.ringwald             break;
16081f504dbdSmatthias.ringwald     }
16093429f56bSmatthias.ringwald }
161016833f0aSmatthias.ringwald 
161131452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1612c8e4258aSmatthias.ringwald     bd_addr_t addr;
1613c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1614c8e4258aSmatthias.ringwald     // house-keeping
1615c8e4258aSmatthias.ringwald 
1616c8e4258aSmatthias.ringwald     // create_connection?
1617c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1618c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
1619339b6768Smatthias.ringwald         log_info("Create_connection to %s\n", bd_addr_to_str(addr));
1620c8e4258aSmatthias.ringwald 
1621ad83dc6aS[email protected]         conn = connection_for_address(addr);
1622ad83dc6aS[email protected]         if (!conn){
162317f1ba2aSmatthias.ringwald             conn = create_connection_for_addr(addr);
162417f1ba2aSmatthias.ringwald             if (!conn){
162517f1ba2aSmatthias.ringwald                 // notify client that alloc failed
162617f1ba2aSmatthias.ringwald                 hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
162717f1ba2aSmatthias.ringwald                 return 0; // don't sent packet to controller
162817f1ba2aSmatthias.ringwald             }
1629ad83dc6aS[email protected]             conn->state = SEND_CREATE_CONNECTION;
1630ad83dc6aS[email protected]         }
1631ad83dc6aS[email protected]         log_info("conn state %u", conn->state);
1632ad83dc6aS[email protected]         switch (conn->state){
1633ad83dc6aS[email protected]             // if connection active exists
1634ad83dc6aS[email protected]             case OPEN:
163562bda3fbS[email protected]                 // and OPEN, emit connection complete command, don't send to controller
1636ad83dc6aS[email protected]                 hci_emit_connection_complete(conn, 0);
163762bda3fbS[email protected]                 return 0;
1638ad83dc6aS[email protected]             case SEND_CREATE_CONNECTION:
1639ad83dc6aS[email protected]                 // connection created by hci, e.g. dedicated bonding
1640ad83dc6aS[email protected]                 break;
1641ad83dc6aS[email protected]             default:
1642ad83dc6aS[email protected]                 // otherwise, just ignore as it is already in the open process
1643ad83dc6aS[email protected]                 return 0;
1644ad83dc6aS[email protected]         }
1645c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
1646c8e4258aSmatthias.ringwald     }
1647c8e4258aSmatthias.ringwald 
16487fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
16497fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
16507fde4af9Smatthias.ringwald     }
16517fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
16527fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
16537fde4af9Smatthias.ringwald     }
16547fde4af9Smatthias.ringwald 
16558ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
16563a9fb326S[email protected]         if (hci_stack->remote_device_db){
16578ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
16583a9fb326S[email protected]             hci_stack->remote_device_db->delete_link_key(&addr);
16598ef73945Smatthias.ringwald         }
16608ef73945Smatthias.ringwald     }
1661c8e4258aSmatthias.ringwald 
16626724cd9eS[email protected]     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)
16636724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_pin_code_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, LEGACY_PAIRING_ACTIVE);
16686724cd9eS[email protected]         }
16696724cd9eS[email protected]     }
16706724cd9eS[email protected] 
16716724cd9eS[email protected]     if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply)
16726724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_confirmation_request_reply)
16736724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_negative_reply)
16746724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_reply)) {
16756724cd9eS[email protected]         bt_flip_addr(addr, &packet[3]);
16766724cd9eS[email protected]         conn = connection_for_address(addr);
16776724cd9eS[email protected]         if (conn){
16786724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE);
16796724cd9eS[email protected]         }
16806724cd9eS[email protected]     }
16816724cd9eS[email protected] 
168269a97523S[email protected] #ifdef HAVE_BLE
168369a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_advertising_parameters)){
16843a9fb326S[email protected]         hci_stack->adv_addr_type = packet[8];
168569a97523S[email protected]     }
168669a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_random_address)){
16873a9fb326S[email protected]         bt_flip_addr(hci_stack->adv_address, &packet[3]);
168869a97523S[email protected]     }
168969a97523S[email protected] #endif
169069a97523S[email protected] 
169169a97523S[email protected] 
16923a9fb326S[email protected]     hci_stack->num_cmd_packets--;
16933a9fb326S[email protected]     return hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
169431452debSmatthias.ringwald }
16958adf0ddaSmatthias.ringwald 
16962bd8b7e7S[email protected] // disconnect because of security block
16972bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){
16982bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
16992bd8b7e7S[email protected]     if (!connection) return;
17002bd8b7e7S[email protected]     connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK;
17012bd8b7e7S[email protected] }
17022bd8b7e7S[email protected] 
17032bd8b7e7S[email protected] 
1704dbe1a790S[email protected] // Configure Secure Simple Pairing
1705dbe1a790S[email protected] 
1706dbe1a790S[email protected] // enable will enable SSP during init
1707dbe1a790S[email protected] void hci_ssp_set_enable(int enable){
17083a9fb326S[email protected]     hci_stack->ssp_enable = enable;
1709dbe1a790S[email protected] }
1710dbe1a790S[email protected] 
17112bd8b7e7S[email protected] int hci_local_ssp_activated(){
17123a9fb326S[email protected]     return hci_ssp_supported() && hci_stack->ssp_enable;
17132bd8b7e7S[email protected] }
17142bd8b7e7S[email protected] 
1715dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement
1716dbe1a790S[email protected] void hci_ssp_set_io_capability(int io_capability){
17173a9fb326S[email protected]     hci_stack->ssp_io_capability = io_capability;
1718dbe1a790S[email protected] }
1719dbe1a790S[email protected] void hci_ssp_set_authentication_requirement(int authentication_requirement){
17203a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = authentication_requirement;
1721dbe1a790S[email protected] }
1722dbe1a790S[email protected] 
1723dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested
1724dbe1a790S[email protected] void hci_ssp_set_auto_accept(int auto_accept){
17253a9fb326S[email protected]     hci_stack->ssp_auto_accept = auto_accept;
1726dbe1a790S[email protected] }
1727dbe1a790S[email protected] 
17281cd208adSmatthias.ringwald /**
17291cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
17301cd208adSmatthias.ringwald  */
1731fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
17321cd208adSmatthias.ringwald     va_list argptr;
17331cd208adSmatthias.ringwald     va_start(argptr, cmd);
17343a9fb326S[email protected]     uint16_t size = hci_create_cmd_internal(hci_stack->hci_packet_buffer, cmd, argptr);
17351cd208adSmatthias.ringwald     va_end(argptr);
17363a9fb326S[email protected]     return hci_send_cmd_packet(hci_stack->hci_packet_buffer, size);
173793b8dc03Smatthias.ringwald }
1738c8e4258aSmatthias.ringwald 
1739ee091cf1Smatthias.ringwald // Create various non-HCI events.
1740ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
1741ee091cf1Smatthias.ringwald 
1742c8e4258aSmatthias.ringwald void hci_emit_state(){
17433a9fb326S[email protected]     log_info("BTSTACK_EVENT_STATE %u", hci_stack->state);
1744425d1371Smatthias.ringwald     uint8_t event[3];
174580d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
1746425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
17473a9fb326S[email protected]     event[2] = hci_stack->state;
1748425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
17493a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1750c8e4258aSmatthias.ringwald }
1751c8e4258aSmatthias.ringwald 
175217f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){
1753425d1371Smatthias.ringwald     uint8_t event[13];
1754c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
1755425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
175617f1ba2aSmatthias.ringwald     event[2] = status;
1757c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
1758c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
1759c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
1760c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
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));
1763c8e4258aSmatthias.ringwald }
1764c8e4258aSmatthias.ringwald 
17653c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
1766425d1371Smatthias.ringwald     uint8_t event[6];
17673c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
1768e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
17693c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
17703c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
17713c4d4b90Smatthias.ringwald     event[5] = reason;
1772425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
17733a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
17743c4d4b90Smatthias.ringwald }
17753c4d4b90Smatthias.ringwald 
1776ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
177766fb9560S[email protected]     if (disable_l2cap_timeouts) return;
1778e0abb8e7S[email protected]     log_info("L2CAP_EVENT_TIMEOUT_CHECK");
1779425d1371Smatthias.ringwald     uint8_t event[4];
178080d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
1781425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1782ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
1783425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
17843a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1785ee091cf1Smatthias.ringwald }
178643bfb1bdSmatthias.ringwald 
178743bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
1788e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections());
1789425d1371Smatthias.ringwald     uint8_t event[3];
179080d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
1791425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
179243bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
1793425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
17943a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
179543bfb1bdSmatthias.ringwald }
1796038bc64cSmatthias.ringwald 
1797038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
1798e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_POWERON_FAILED");
1799425d1371Smatthias.ringwald     uint8_t event[2];
180080d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
1801425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1802425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18033a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1804038bc64cSmatthias.ringwald }
18051b0e3922Smatthias.ringwald 
180609ba8edeSmatthias.ringwald #ifndef EMBEDDED
18071b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
1808e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR);
1809425d1371Smatthias.ringwald     uint8_t event[6];
18101b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
1811425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1812425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
1813425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
1814425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
1815425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18163a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
18171b0e3922Smatthias.ringwald }
181809ba8edeSmatthias.ringwald #endif
18191b0e3922Smatthias.ringwald 
18202ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
1821e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled);
1822425d1371Smatthias.ringwald     uint8_t event[3];
18232ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
1824425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
18252ed6235cSmatthias.ringwald     event[2] = enabled;
1826425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18273a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
18282ed6235cSmatthias.ringwald }
1829627c2f45Smatthias.ringwald 
1830627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
1831e0abb8e7S[email protected]     uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info
1832627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
1833e0abb8e7S[email protected]     event[1] = sizeof(event) - 2 - 1;
1834f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
18352f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
1836f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
1837e0abb8e7S[email protected] 
1838e0abb8e7S[email protected]     event[9+248] = 0;   // assert \0 for log_info
1839e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]);
1840e0abb8e7S[email protected] 
1841e0abb8e7S[email protected]     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1);
18423a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1);
1843627c2f45Smatthias.ringwald }
1844381fbed8Smatthias.ringwald 
1845381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
1846e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled);
1847425d1371Smatthias.ringwald     uint8_t event[3];
1848381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
1849425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1850381fbed8Smatthias.ringwald     event[2] = enabled;
1851425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18523a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1853381fbed8Smatthias.ringwald }
1854458bf4e8S[email protected] 
1855a00031e2S[email protected] void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){
1856df3354fcS[email protected]     log_info("hci_emit_security_level %u for handle %x", level, con_handle);
1857a00031e2S[email protected]     uint8_t event[5];
1858e00caf9cS[email protected]     int pos = 0;
1859a00031e2S[email protected]     event[pos++] = GAP_SECURITY_LEVEL;
1860e00caf9cS[email protected]     event[pos++] = sizeof(event) - 2;
1861a00031e2S[email protected]     bt_store_16(event, 2, con_handle);
1862e00caf9cS[email protected]     pos += 2;
1863e00caf9cS[email protected]     event[pos++] = level;
1864e00caf9cS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18653a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1866e00caf9cS[email protected] }
1867e00caf9cS[email protected] 
1868ad83dc6aS[email protected] void hci_emit_dedicated_bonding_result(hci_connection_t * connection, uint8_t status){
1869ad83dc6aS[email protected]     log_info("hci_emit_dedicated_bonding_result %u ", status);
1870ad83dc6aS[email protected]     uint8_t event[9];
1871ad83dc6aS[email protected]     int pos = 0;
1872ad83dc6aS[email protected]     event[pos++] = GAP_DEDICATED_BONDING_COMPLETED;
1873ad83dc6aS[email protected]     event[pos++] = sizeof(event) - 2;
1874ad83dc6aS[email protected]     event[pos++] = status;
1875ad83dc6aS[email protected]     bt_flip_addr( * (bd_addr_t *) &event[pos], connection->address);
1876ad83dc6aS[email protected]     pos += 6;
1877ad83dc6aS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18783a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1879ad83dc6aS[email protected] }
1880ad83dc6aS[email protected] 
18812bd8b7e7S[email protected] // query if remote side supports SSP
18822bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){
18832bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
18842bd8b7e7S[email protected]     if (!connection) return 0;
18852bd8b7e7S[email protected]     return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0;
18862bd8b7e7S[email protected] }
18872bd8b7e7S[email protected] 
1888df3354fcS[email protected] int hci_ssp_supported_on_both_sides(hci_con_handle_t handle){
1889df3354fcS[email protected]     return hci_local_ssp_activated() && hci_remote_ssp_supported(handle);
1890df3354fcS[email protected] }
1891df3354fcS[email protected] 
1892458bf4e8S[email protected] // GAP API
1893458bf4e8S[email protected] /**
1894458bf4e8S[email protected]  * @bbrief enable/disable bonding. default is enabled
1895458bf4e8S[email protected]  * @praram enabled
1896458bf4e8S[email protected]  */
18974c57c146S[email protected] void gap_set_bondable_mode(int enable){
18983a9fb326S[email protected]     hci_stack->bondable = enable ? 1 : 0;
1899458bf4e8S[email protected] }
1900cb230b9dS[email protected] 
1901cb230b9dS[email protected] /**
190234d2123cS[email protected]  * @brief map link keys to security levels
1903cb230b9dS[email protected]  */
190434d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){
190534d2123cS[email protected]     switch (link_key_type){
19063c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256:
19073c68dfa9S[email protected]             return LEVEL_4;
19083c68dfa9S[email protected]         case COMBINATION_KEY:
19093c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192:
19103c68dfa9S[email protected]             return LEVEL_3;
19113c68dfa9S[email protected]         default:
19123c68dfa9S[email protected]             return LEVEL_2;
19133c68dfa9S[email protected]     }
1914cb230b9dS[email protected] }
1915cb230b9dS[email protected] 
1916a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){
191734d2123cS[email protected]     if (!connection) return LEVEL_0;
191834d2123cS[email protected]     if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0;
191934d2123cS[email protected]     return gap_security_level_for_link_key_type(connection->link_key_type);
192034d2123cS[email protected] }
192134d2123cS[email protected] 
192234d2123cS[email protected] 
1923106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){
192482d8f825S[email protected]     printf("gap_mitm_protection_required_for_security_level %u\n", level);
1925106d6d11S[email protected]     return level > LEVEL_2;
1926106d6d11S[email protected] }
1927106d6d11S[email protected] 
192834d2123cS[email protected] /**
192934d2123cS[email protected]  * @brief get current security level
193034d2123cS[email protected]  */
193134d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){
193234d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
193334d2123cS[email protected]     if (!connection) return LEVEL_0;
193434d2123cS[email protected]     return gap_security_level_for_connection(connection);
193534d2123cS[email protected] }
193634d2123cS[email protected] 
1937cb230b9dS[email protected] /**
1938cb230b9dS[email protected]  * @brief request connection to device to
1939cb230b9dS[email protected]  * @result GAP_AUTHENTICATION_RESULT
1940cb230b9dS[email protected]  */
194134d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){
194234d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
194334d2123cS[email protected]     if (!connection){
1944a00031e2S[email protected]         hci_emit_security_level(con_handle, LEVEL_0);
194534d2123cS[email protected]         return;
194634d2123cS[email protected]     }
194734d2123cS[email protected]     gap_security_level_t current_level = gap_security_level(con_handle);
194834d2123cS[email protected]     log_info("gap_request_security_level %u, current level %u", requested_level, current_level);
194934d2123cS[email protected]     if (current_level >= requested_level){
1950a00031e2S[email protected]         hci_emit_security_level(con_handle, current_level);
195134d2123cS[email protected]         return;
195234d2123cS[email protected]     }
1953a00031e2S[email protected] 
195434d2123cS[email protected]     connection->requested_security_level = requested_level;
1955a00031e2S[email protected] 
1956fb8ba0dbS[email protected]     // would enabling ecnryption suffice (>= LEVEL_2)?
19573a9fb326S[email protected]     if (hci_stack->remote_device_db){
1958a00031e2S[email protected]         link_key_type_t link_key_type;
1959a00031e2S[email protected]         link_key_t      link_key;
19603a9fb326S[email protected]         if (hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)){
1961a00031e2S[email protected]             if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){
1962a00031e2S[email protected]                 connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
1963a00031e2S[email protected]                 return;
1964a00031e2S[email protected]             }
1965a00031e2S[email protected]         }
1966a00031e2S[email protected]     }
1967a00031e2S[email protected] 
19681eb2563eS[email protected]     // try to authenticate connection
19691eb2563eS[email protected]     connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
1970*e80b2cf9S[email protected]     hci_run();
1971e00caf9cS[email protected] }
1972ad83dc6aS[email protected] 
1973ad83dc6aS[email protected] /**
1974ad83dc6aS[email protected]  * @brief start dedicated bonding with device. disconnect after bonding
1975ad83dc6aS[email protected]  * @param device
1976ad83dc6aS[email protected]  * @param request MITM protection
1977ad83dc6aS[email protected]  * @result GAP_DEDICATED_BONDING_COMPLETE
1978ad83dc6aS[email protected]  */
1979ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){
1980ad83dc6aS[email protected] 
1981ad83dc6aS[email protected]     // create connection state machine
1982ad83dc6aS[email protected]     hci_connection_t * connection = create_connection_for_addr(device);
1983ad83dc6aS[email protected] 
1984ad83dc6aS[email protected]     if (!connection){
1985ad83dc6aS[email protected]         return BTSTACK_MEMORY_ALLOC_FAILED;
1986ad83dc6aS[email protected]     }
1987ad83dc6aS[email protected] 
1988ad83dc6aS[email protected]     // delete linkn key
1989ad83dc6aS[email protected]     hci_drop_link_key_for_bd_addr( (bd_addr_t *) &device);
1990ad83dc6aS[email protected] 
1991ad83dc6aS[email protected]     // configure LEVEL_2/3, dedicated bonding
1992ad83dc6aS[email protected]     connection->state = SEND_CREATE_CONNECTION;
1993ad83dc6aS[email protected]     connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2;
199482d8f825S[email protected]     printf("gap_dedicated_bonding, mitm %u -> level %u\n", mitm_protection_required, connection->requested_security_level);
1995ad83dc6aS[email protected]     connection->bonding_flags = BONDING_DEDICATED;
1996ad83dc6aS[email protected] 
1997ad83dc6aS[email protected]     // wait for GAP Security Result and send GAP Dedicated Bonding complete
1998ad83dc6aS[email protected] 
1999ad83dc6aS[email protected]     // handle: connnection failure (connection complete != ok)
2000ad83dc6aS[email protected]     // handle: authentication failure
2001ad83dc6aS[email protected]     // handle: disconnect on done
2002ad83dc6aS[email protected] 
2003ad83dc6aS[email protected]     hci_run();
2004ad83dc6aS[email protected] 
2005ad83dc6aS[email protected]     return 0;
2006ad83dc6aS[email protected] }
2007