xref: /btstack/src/hci.c (revision 96a45072cb69a02da1b2c66c7644224d8da7370d)
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);
74*96a45072S[email protected] static void hci_connection_timeout_handler(timer_source_t *timer);
75*96a45072S[email protected] static void hci_connection_timestamp(hci_connection_t *connection);
76758b46ceSmatthias.ringwald 
7706b35ec0Smatthias.ringwald // the STACK is here
783a9fb326S[email protected] #ifndef HAVE_MALLOC
793a9fb326S[email protected] static hci_stack_t   hci_stack_static;
803a9fb326S[email protected] #endif
813a9fb326S[email protected] static hci_stack_t * hci_stack = NULL;
8216833f0aSmatthias.ringwald 
8366fb9560S[email protected] // test helper
8466fb9560S[email protected] static uint8_t disable_l2cap_timeouts = 0;
8566fb9560S[email protected] 
86*96a45072S[email protected] /**
87*96a45072S[email protected]  * create connection for given address
88*96a45072S[email protected]  *
89*96a45072S[email protected]  * @return connection OR NULL, if no memory left
90*96a45072S[email protected]  */
91*96a45072S[email protected] static hci_connection_t * create_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){
92*96a45072S[email protected] 
93*96a45072S[email protected]     printf("create_connection_for_addr %s\n", bd_addr_to_str(addr));
94*96a45072S[email protected]     hci_connection_t * conn = (hci_connection_t *) btstack_memory_hci_connection_get();
95*96a45072S[email protected]     if (!conn) return NULL;
96*96a45072S[email protected]     BD_ADDR_COPY(conn->address, addr);
97*96a45072S[email protected]     conn->address_type = addr_type;
98*96a45072S[email protected]     conn->con_handle = 0xffff;
99*96a45072S[email protected]     conn->authentication_flags = AUTH_FLAGS_NONE;
100*96a45072S[email protected]     conn->bonding_flags = 0;
101*96a45072S[email protected]     conn->requested_security_level = LEVEL_0;
102*96a45072S[email protected]     linked_item_set_user(&conn->timeout.item, conn);
103*96a45072S[email protected]     conn->timeout.process = hci_connection_timeout_handler;
104*96a45072S[email protected]     hci_connection_timestamp(conn);
105*96a45072S[email protected]     conn->acl_recombination_length = 0;
106*96a45072S[email protected]     conn->acl_recombination_pos = 0;
107*96a45072S[email protected]     conn->num_acl_packets_sent = 0;
108*96a45072S[email protected]     linked_list_add(&hci_stack->connections, (linked_item_t *) conn);
109*96a45072S[email protected]     return conn;
110*96a45072S[email protected] }
11166fb9560S[email protected] 
11297addcc5Smatthias.ringwald /**
113ee091cf1Smatthias.ringwald  * get connection for a given handle
114ee091cf1Smatthias.ringwald  *
115ee091cf1Smatthias.ringwald  * @return connection OR NULL, if not found
116ee091cf1Smatthias.ringwald  */
1175061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){
118ee091cf1Smatthias.ringwald     linked_item_t *it;
1193a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
120ee091cf1Smatthias.ringwald         if ( ((hci_connection_t *) it)->con_handle == con_handle){
121ee091cf1Smatthias.ringwald             return (hci_connection_t *) it;
122ee091cf1Smatthias.ringwald         }
123ee091cf1Smatthias.ringwald     }
124ee091cf1Smatthias.ringwald     return NULL;
125ee091cf1Smatthias.ringwald }
126ee091cf1Smatthias.ringwald 
127*96a45072S[email protected] /**
128*96a45072S[email protected]  * get connection for given address
129*96a45072S[email protected]  *
130*96a45072S[email protected]  * @return connection OR NULL, if not found
131*96a45072S[email protected]  */
132*96a45072S[email protected] hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t * addr, bd_addr_type_t addr_type){
13362bda3fbS[email protected]     linked_item_t *it;
13462bda3fbS[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
13562bda3fbS[email protected]         hci_connection_t * connection = (hci_connection_t *) it;
136*96a45072S[email protected]         if (connection->address_type != addr_type)  continue;
137*96a45072S[email protected]         if (memcmp(addr, connection->address, 6) != 0) continue;
13862bda3fbS[email protected]         return connection;
13962bda3fbS[email protected]     }
14062bda3fbS[email protected]     return NULL;
14162bda3fbS[email protected] }
14262bda3fbS[email protected] 
1432b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){
14428ca2b46S[email protected]     hci_connection_t * connection = (hci_connection_t *) linked_item_get_user(&timer->item);
145c785ef68Smatthias.ringwald #ifdef HAVE_TIME
146ee091cf1Smatthias.ringwald     struct timeval tv;
147ee091cf1Smatthias.ringwald     gettimeofday(&tv, NULL);
148c21e6239Smatthias.ringwald     if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) {
149ee091cf1Smatthias.ringwald         // connections might be timed out
150ee091cf1Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
151ee091cf1Smatthias.ringwald     }
1522b12a0b9Smatthias.ringwald #endif
153e5780900Smatthias.ringwald #ifdef HAVE_TICK
154c785ef68Smatthias.ringwald     if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){
155c785ef68Smatthias.ringwald         // connections might be timed out
156c785ef68Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
157c785ef68Smatthias.ringwald     }
158c785ef68Smatthias.ringwald #endif
159c785ef68Smatthias.ringwald     run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
160c785ef68Smatthias.ringwald     run_loop_add_timer(timer);
161c785ef68Smatthias.ringwald }
162ee091cf1Smatthias.ringwald 
163ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){
164c7492964Smatthias.ringwald #ifdef HAVE_TIME
165ee091cf1Smatthias.ringwald     gettimeofday(&connection->timestamp, NULL);
166c7492964Smatthias.ringwald #endif
167e5780900Smatthias.ringwald #ifdef HAVE_TICK
168c785ef68Smatthias.ringwald     connection->timestamp = embedded_get_ticks();
169c785ef68Smatthias.ringwald #endif
170ee091cf1Smatthias.ringwald }
171ee091cf1Smatthias.ringwald 
17206b35ec0Smatthias.ringwald 
17328ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
17428ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags);
17528ca2b46S[email protected] }
17628ca2b46S[email protected] 
17728ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
17828ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags);
17928ca2b46S[email protected] }
18028ca2b46S[email protected] 
18128ca2b46S[email protected] 
18243bfb1bdSmatthias.ringwald /**
18380ca58a0Smatthias.ringwald  * add authentication flags and reset timer
184*96a45072S[email protected]  * @note: assumes classic connection
1857fde4af9Smatthias.ringwald  */
1867fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){
1877fde4af9Smatthias.ringwald     bd_addr_t addr;
1887fde4af9Smatthias.ringwald     bt_flip_addr(addr, *(bd_addr_t *) bd_addr);
189*96a45072S[email protected]     hci_connection_t * conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
1907fde4af9Smatthias.ringwald     if (conn) {
19128ca2b46S[email protected]         connectionSetAuthenticationFlags(conn, flags);
19280ca58a0Smatthias.ringwald         hci_connection_timestamp(conn);
1937fde4af9Smatthias.ringwald     }
1947fde4af9Smatthias.ringwald }
1957fde4af9Smatthias.ringwald 
19680ca58a0Smatthias.ringwald int  hci_authentication_active_for_handle(hci_con_handle_t handle){
1975061f3afS[email protected]     hci_connection_t * conn = hci_connection_for_handle(handle);
19880ca58a0Smatthias.ringwald     if (!conn) return 0;
1996724cd9eS[email protected]     if (conn->authentication_flags & LEGACY_PAIRING_ACTIVE) return 1;
2006724cd9eS[email protected]     if (conn->authentication_flags & SSP_PAIRING_ACTIVE) return 1;
2016724cd9eS[email protected]     return 0;
20280ca58a0Smatthias.ringwald }
20380ca58a0Smatthias.ringwald 
204c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){
2053a9fb326S[email protected]     if (hci_stack->remote_device_db) {
2063a9fb326S[email protected]         hci_stack->remote_device_db->delete_link_key(addr);
207c12e46e7Smatthias.ringwald     }
208c12e46e7Smatthias.ringwald }
209c12e46e7Smatthias.ringwald 
2107fde4af9Smatthias.ringwald 
2117fde4af9Smatthias.ringwald /**
21243bfb1bdSmatthias.ringwald  * count connections
21343bfb1bdSmatthias.ringwald  */
21440d1c7a4Smatthias.ringwald static int nr_hci_connections(void){
21556c253c9Smatthias.ringwald     int count = 0;
21643bfb1bdSmatthias.ringwald     linked_item_t *it;
2173a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next, count++);
21843bfb1bdSmatthias.ringwald     return count;
21943bfb1bdSmatthias.ringwald }
220c8e4258aSmatthias.ringwald 
22197addcc5Smatthias.ringwald /**
222ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
22316833f0aSmatthias.ringwald  */
2242718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
22516833f0aSmatthias.ringwald }
22616833f0aSmatthias.ringwald 
227998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){
2285061f3afS[email protected]     hci_connection_t * connection = hci_connection_for_handle(handle);
229998906cdSmatthias.ringwald     if (!connection) {
2307d67539fSmatthias.ringwald         log_error("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle);
231998906cdSmatthias.ringwald         return 0;
232998906cdSmatthias.ringwald     }
233998906cdSmatthias.ringwald     return connection->num_acl_packets_sent;
234998906cdSmatthias.ringwald }
235998906cdSmatthias.ringwald 
236998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){
2373a9fb326S[email protected]     uint8_t free_slots = hci_stack->total_num_acl_packets;
238998906cdSmatthias.ringwald     linked_item_t *it;
2393a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
240998906cdSmatthias.ringwald         hci_connection_t * connection = (hci_connection_t *) it;
241998906cdSmatthias.ringwald         if (free_slots < connection->num_acl_packets_sent) {
2427d67539fSmatthias.ringwald             log_error("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n");
243998906cdSmatthias.ringwald             return 0;
244998906cdSmatthias.ringwald         }
245998906cdSmatthias.ringwald         free_slots -= connection->num_acl_packets_sent;
246998906cdSmatthias.ringwald     }
247998906cdSmatthias.ringwald     return free_slots;
248998906cdSmatthias.ringwald }
249998906cdSmatthias.ringwald 
250c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){
2512b12a0b9Smatthias.ringwald 
2522b12a0b9Smatthias.ringwald     // check for async hci transport implementations
2533a9fb326S[email protected]     if (hci_stack->hci_transport->can_send_packet_now){
2543a9fb326S[email protected]         if (!hci_stack->hci_transport->can_send_packet_now(packet_type)){
2552b12a0b9Smatthias.ringwald             return 0;
2562b12a0b9Smatthias.ringwald         }
2572b12a0b9Smatthias.ringwald     }
2582b12a0b9Smatthias.ringwald 
2592b12a0b9Smatthias.ringwald     // check regular Bluetooth flow control
260c24735b1Smatthias.ringwald     switch (packet_type) {
261c24735b1Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
262c24735b1Smatthias.ringwald             return hci_number_free_acl_slots();
263c24735b1Smatthias.ringwald         case HCI_COMMAND_DATA_PACKET:
2643a9fb326S[email protected]             return hci_stack->num_cmd_packets;
265c24735b1Smatthias.ringwald         default:
266c24735b1Smatthias.ringwald             return 0;
267c24735b1Smatthias.ringwald     }
268c24735b1Smatthias.ringwald }
269c24735b1Smatthias.ringwald 
2706b4af23dS[email protected] // same as hci_can_send_packet_now, but also checks if packet buffer is free for use
2716b4af23dS[email protected] int hci_can_send_packet_now_using_packet_buffer(uint8_t packet_type){
2726b4af23dS[email protected]     if (hci_stack->hci_packet_buffer_reserved) return 0;
2736b4af23dS[email protected]     return hci_can_send_packet_now(packet_type);
2746b4af23dS[email protected] }
2756b4af23dS[email protected] 
276c8b9416aS[email protected] // used for internal checks in l2cap[-le].c
277c8b9416aS[email protected] int hci_is_packet_buffer_reserved(void){
278c8b9416aS[email protected]     return hci_stack->hci_packet_buffer_reserved;
279c8b9416aS[email protected] }
280c8b9416aS[email protected] 
2816b4af23dS[email protected] // reserves outgoing packet buffer. @returns 1 if successful
2826b4af23dS[email protected] int hci_reserve_packet_buffer(void){
2836b4af23dS[email protected]     if (hci_stack->hci_packet_buffer_reserved) return 0;
2846b4af23dS[email protected]     hci_stack->hci_packet_buffer_reserved = 1;
2856b4af23dS[email protected]     return 1;
2866b4af23dS[email protected] }
2876b4af23dS[email protected] 
28868a0fcf7S[email protected] void hci_release_packet_buffer(void){
28968a0fcf7S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
29068a0fcf7S[email protected] }
29168a0fcf7S[email protected] 
2926b4af23dS[email protected] // assumption: synchronous implementations don't provide can_send_packet_now as they don't keep the buffer after the call
2936b4af23dS[email protected] int hci_transport_synchronous(void){
2946b4af23dS[email protected]     return hci_stack->hci_transport->can_send_packet_now == NULL;
2956b4af23dS[email protected] }
2966b4af23dS[email protected] 
297ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
2987856c818Smatthias.ringwald 
2996218e6f1Smatthias.ringwald     // check for free places on BT module
3005932f1b4Smatthias.ringwald     if (!hci_number_free_acl_slots()) return BTSTACK_ACL_BUFFERS_FULL;
3016218e6f1Smatthias.ringwald 
3027856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
3035061f3afS[email protected]     hci_connection_t *connection = hci_connection_for_handle( con_handle);
30456cf178bSmatthias.ringwald     if (!connection) return 0;
30556cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
30656cf178bSmatthias.ringwald 
30756cf178bSmatthias.ringwald     // count packet
30856cf178bSmatthias.ringwald     connection->num_acl_packets_sent++;
3097b5fbe1fSmatthias.ringwald     // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent);
3107856c818Smatthias.ringwald 
31100d8e42eSmatthias.ringwald     // send packet
3123a9fb326S[email protected]     int err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
3136218e6f1Smatthias.ringwald 
3146b4af23dS[email protected]     // free packet buffer for synchronous transport implementations
315c8b9416aS[email protected]     if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){
3166b4af23dS[email protected]         hci_stack->hci_packet_buffer_reserved = 0;
3176b4af23dS[email protected]     }
3186b4af23dS[email protected] 
31900d8e42eSmatthias.ringwald     return err;
320ee091cf1Smatthias.ringwald }
321ee091cf1Smatthias.ringwald 
32216833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
3237856c818Smatthias.ringwald 
324e76a89eeS[email protected]     // log_info("acl_handler: size %u", size);
325e76a89eeS[email protected] 
3267856c818Smatthias.ringwald     // get info
3277856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
3285061f3afS[email protected]     hci_connection_t *conn      = hci_connection_for_handle(con_handle);
3297856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
3307856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
3317856c818Smatthias.ringwald 
3327856c818Smatthias.ringwald     // ignore non-registered handle
3337856c818Smatthias.ringwald     if (!conn){
3347d67539fSmatthias.ringwald         log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle);
3357856c818Smatthias.ringwald         return;
3367856c818Smatthias.ringwald     }
3377856c818Smatthias.ringwald 
338e76a89eeS[email protected]     // assert packet is complete
3399ecc3e17S[email protected]     if (acl_length + 4 != size){
340e76a89eeS[email protected]         log_error("hci.c: acl_handler called with ACL packet of wrong size %u, expected %u => dropping packet", size, acl_length + 4);
341e76a89eeS[email protected]         return;
342e76a89eeS[email protected]     }
343e76a89eeS[email protected] 
3447856c818Smatthias.ringwald     // update idle timestamp
3457856c818Smatthias.ringwald     hci_connection_timestamp(conn);
3467856c818Smatthias.ringwald 
3477856c818Smatthias.ringwald     // handle different packet types
3487856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
3497856c818Smatthias.ringwald 
3507856c818Smatthias.ringwald         case 0x01: // continuation fragment
3517856c818Smatthias.ringwald 
3527856c818Smatthias.ringwald             // sanity check
3537856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
3547d67539fSmatthias.ringwald                 log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle);
3557856c818Smatthias.ringwald                 return;
3567856c818Smatthias.ringwald             }
3577856c818Smatthias.ringwald 
3587856c818Smatthias.ringwald             // append fragment payload (header already stored)
3597856c818Smatthias.ringwald             memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
3607856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
3617856c818Smatthias.ringwald 
3627d67539fSmatthias.ringwald             // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length,
363decc01a8Smatthias.ringwald             //        conn->acl_recombination_pos, conn->acl_recombination_length);
3647856c818Smatthias.ringwald 
3657856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
3667856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
3677856c818Smatthias.ringwald 
3683a9fb326S[email protected]                 hci_stack->packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos);
3697856c818Smatthias.ringwald                 // reset recombination buffer
3707856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
3717856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
3727856c818Smatthias.ringwald             }
3737856c818Smatthias.ringwald             break;
3747856c818Smatthias.ringwald 
3757856c818Smatthias.ringwald         case 0x02: { // first fragment
3767856c818Smatthias.ringwald 
3777856c818Smatthias.ringwald             // sanity check
3787856c818Smatthias.ringwald             if (conn->acl_recombination_pos) {
3797d67539fSmatthias.ringwald                 log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
3807856c818Smatthias.ringwald                 return;
3817856c818Smatthias.ringwald             }
3827856c818Smatthias.ringwald 
3837856c818Smatthias.ringwald             // peek into L2CAP packet!
3847856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
3857856c818Smatthias.ringwald 
386e76a89eeS[email protected]             // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
387decc01a8Smatthias.ringwald 
3887856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
3897856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
3907856c818Smatthias.ringwald 
3917856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
3923a9fb326S[email protected]                 hci_stack->packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
3937856c818Smatthias.ringwald 
3947856c818Smatthias.ringwald             } else {
3957856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
3967856c818Smatthias.ringwald                 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
3977856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
3987856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
399decc01a8Smatthias.ringwald                 bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4);
4007856c818Smatthias.ringwald             }
4017856c818Smatthias.ringwald             break;
4027856c818Smatthias.ringwald 
4037856c818Smatthias.ringwald         }
4047856c818Smatthias.ringwald         default:
4057d67539fSmatthias.ringwald             log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03);
4067856c818Smatthias.ringwald             return;
4077856c818Smatthias.ringwald     }
40894ab26f8Smatthias.ringwald 
40994ab26f8Smatthias.ringwald     // execute main loop
41094ab26f8Smatthias.ringwald     hci_run();
41116833f0aSmatthias.ringwald }
41222909952Smatthias.ringwald 
41367a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){
414339b6768Smatthias.ringwald     log_info("Connection closed: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
4153c4d4b90Smatthias.ringwald 
4163c4d4b90Smatthias.ringwald     // cancel all l2cap connections
4173c4d4b90Smatthias.ringwald     hci_emit_disconnection_complete(conn->con_handle, 0x16);    // terminated by local host
4183c4d4b90Smatthias.ringwald 
419c7e0c5f6Smatthias.ringwald     run_loop_remove_timer(&conn->timeout);
420c785ef68Smatthias.ringwald 
4213a9fb326S[email protected]     linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
422a3b02b71Smatthias.ringwald     btstack_memory_hci_connection_free( conn );
4233c4d4b90Smatthias.ringwald 
4243c4d4b90Smatthias.ringwald     // now it's gone
425c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
426c7e0c5f6Smatthias.ringwald }
427c7e0c5f6Smatthias.ringwald 
4280c042179S[email protected] static const uint16_t packet_type_sizes[] = {
4298f8108aaSmatthias.ringwald     0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE,
4308f8108aaSmatthias.ringwald     HCI_ACL_DH1_SIZE, 0, 0, 0,
4318f8108aaSmatthias.ringwald     HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE,
4328f8108aaSmatthias.ringwald     HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE
4338f8108aaSmatthias.ringwald };
43465389bfcS[email protected] static const uint8_t  packet_type_feature_requirement_bit[] = {
43565389bfcS[email protected]      0, // 3 slot packets
43665389bfcS[email protected]      1, // 5 slot packets
43765389bfcS[email protected]     25, // EDR 2 mpbs
43865389bfcS[email protected]     26, // EDR 3 mbps
43965389bfcS[email protected]     39, // 3 slot EDR packts
44065389bfcS[email protected]     40, // 5 slot EDR packet
44165389bfcS[email protected] };
44265389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = {
44365389bfcS[email protected]     0x0f00, // 3 slot packets
44465389bfcS[email protected]     0xf000, // 5 slot packets
44565389bfcS[email protected]     0x1102, // EDR 2 mpbs
44665389bfcS[email protected]     0x2204, // EDR 3 mbps
44765389bfcS[email protected]     0x0300, // 3 slot EDR packts
44865389bfcS[email protected]     0x3000, // 5 slot EDR packet
44965389bfcS[email protected] };
4508f8108aaSmatthias.ringwald 
45165389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){
45265389bfcS[email protected]     // enable packet types based on size
4538f8108aaSmatthias.ringwald     uint16_t packet_types = 0;
4548f8108aaSmatthias.ringwald     int i;
4558f8108aaSmatthias.ringwald     for (i=0;i<16;i++){
4568f8108aaSmatthias.ringwald         if (packet_type_sizes[i] == 0) continue;
4578f8108aaSmatthias.ringwald         if (packet_type_sizes[i] <= buffer_size){
4588f8108aaSmatthias.ringwald             packet_types |= 1 << i;
4598f8108aaSmatthias.ringwald         }
4608f8108aaSmatthias.ringwald     }
46165389bfcS[email protected]     // disable packet types due to missing local supported features
46265389bfcS[email protected]     for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){
46365389bfcS[email protected]         int bit_idx = packet_type_feature_requirement_bit[i];
46465389bfcS[email protected]         int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0;
46565389bfcS[email protected]         if (feature_set) continue;
46665389bfcS[email protected]         log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]);
46765389bfcS[email protected]         packet_types &= ~packet_type_feature_packet_mask[i];
46865389bfcS[email protected]     }
4698f8108aaSmatthias.ringwald     // flip bits for "may not be used"
4708f8108aaSmatthias.ringwald     packet_types ^= 0x3306;
4718f8108aaSmatthias.ringwald     return packet_types;
4728f8108aaSmatthias.ringwald }
4738f8108aaSmatthias.ringwald 
4748f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){
4753a9fb326S[email protected]     return hci_stack->packet_types;
4768f8108aaSmatthias.ringwald }
4778f8108aaSmatthias.ringwald 
478facf93fdS[email protected] uint8_t* hci_get_outgoing_packet_buffer(void){
4797dc17943Smatthias.ringwald     // hci packet buffer is >= acl data packet length
4803a9fb326S[email protected]     return hci_stack->hci_packet_buffer;
4817dc17943Smatthias.ringwald }
4827dc17943Smatthias.ringwald 
483f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){
4843a9fb326S[email protected]     return hci_stack->acl_data_packet_length;
4857dc17943Smatthias.ringwald }
4867dc17943Smatthias.ringwald 
487f5d8d141S[email protected] int hci_ssp_supported(void){
488f5d8d141S[email protected]     // No 51, byte 6, bit 3
4893a9fb326S[email protected]     return (hci_stack->local_supported_features[6] & (1 << 3)) != 0;
490f5d8d141S[email protected] }
491f5d8d141S[email protected] 
492f5d8d141S[email protected] int hci_classic_supported(void){
493f5d8d141S[email protected]     // No 37, byte 4, bit 5, = No BR/EDR Support
4943a9fb326S[email protected]     return (hci_stack->local_supported_features[4] & (1 << 5)) == 0;
495f5d8d141S[email protected] }
496f5d8d141S[email protected] 
497f5d8d141S[email protected] int hci_le_supported(void){
498f5d8d141S[email protected]     // No 37, byte 4, bit 6 = LE Supported (Controller)
499f5d8d141S[email protected] #ifdef HAVE_BLE
5003a9fb326S[email protected]     return (hci_stack->local_supported_features[4] & (1 << 6)) != 0;
501f5d8d141S[email protected] #else
502f5d8d141S[email protected]     return 0;
503f5d8d141S[email protected] #endif
504f5d8d141S[email protected] }
505f5d8d141S[email protected] 
50669a97523S[email protected] // get addr type and address used in advertisement packets
50718904abdS[email protected] void hci_le_advertisement_address(uint8_t * addr_type, bd_addr_t * addr){
5083a9fb326S[email protected]     *addr_type = hci_stack->adv_addr_type;
5093a9fb326S[email protected]     if (hci_stack->adv_addr_type){
5103a9fb326S[email protected]         memcpy(addr, hci_stack->adv_address, 6);
51169a97523S[email protected]     } else {
5123a9fb326S[email protected]         memcpy(addr, hci_stack->local_bd_addr, 6);
51369a97523S[email protected]     }
51469a97523S[email protected] }
51569a97523S[email protected] 
51674ec757aSmatthias.ringwald // avoid huge local variables
517223aafc1Smatthias.ringwald #ifndef EMBEDDED
51874ec757aSmatthias.ringwald static device_name_t device_name;
519223aafc1Smatthias.ringwald #endif
52016833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
521e76a89eeS[email protected] 
522e76a89eeS[email protected]     uint16_t event_length = packet[1];
523e76a89eeS[email protected] 
524e76a89eeS[email protected]     // assert packet is complete
525e76a89eeS[email protected]     if (size != event_length + 2){
526e76a89eeS[email protected]         log_error("hci.c: event_handler called with event packet of wrong size %u, expected %u => dropping packet", size, event_length + 2);
527e76a89eeS[email protected]         return;
528e76a89eeS[email protected]     }
529e76a89eeS[email protected] 
5301281a47eSmatthias.ringwald     bd_addr_t addr;
531*96a45072S[email protected]     bd_addr_type_t addr_type;
5322d00edd4Smatthias.ringwald     uint8_t link_type;
533fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
5341f7b95a1Smatthias.ringwald     hci_connection_t * conn;
53556cf178bSmatthias.ringwald     int i;
53622909952Smatthias.ringwald 
5375909f7f2Smatthias.ringwald     // printf("HCI:EVENT:%02x\n", packet[0]);
5385909f7f2Smatthias.ringwald 
5396772a24cSmatthias.ringwald     switch (packet[0]) {
54022909952Smatthias.ringwald 
5416772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
5427ec5eeaaSmatthias.ringwald             // get num cmd packets
5433a9fb326S[email protected]             // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack->num_cmd_packets, packet[2]);
5443a9fb326S[email protected]             hci_stack->num_cmd_packets = packet[2];
5457ec5eeaaSmatthias.ringwald 
546e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
547e2edc0c3Smatthias.ringwald                 // from offset 5
548e2edc0c3Smatthias.ringwald                 // status
5491d279b20Smatthias.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"
5503a9fb326S[email protected]                 hci_stack->acl_data_packet_length = READ_BT_16(packet, 6);
55156cf178bSmatthias.ringwald                 // ignore: SCO data packet len (8)
5523a9fb326S[email protected]                 hci_stack->total_num_acl_packets  = packet[9];
55356cf178bSmatthias.ringwald                 // ignore: total num SCO packets
5543a9fb326S[email protected]                 if (hci_stack->state == HCI_STATE_INITIALIZING){
555a7a04bd9Smatthias.ringwald                     // determine usable ACL payload size
5563a9fb326S[email protected]                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack->acl_data_packet_length){
5573a9fb326S[email protected]                         hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
5588f8108aaSmatthias.ringwald                     }
55965389bfcS[email protected]                     log_info("hci_read_buffer_size: used size %u, count %u\n",
5603a9fb326S[email protected]                              hci_stack->acl_data_packet_length, hci_stack->total_num_acl_packets);
561e2edc0c3Smatthias.ringwald                 }
56256cf178bSmatthias.ringwald             }
56365a46ef3S[email protected] #ifdef HAVE_BLE
56465a46ef3S[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)){
5653a9fb326S[email protected]                 hci_stack->le_data_packet_length = READ_BT_16(packet, 6);
5663a9fb326S[email protected]                 hci_stack->total_num_le_packets  = packet[8];
5673a9fb326S[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);
56865a46ef3S[email protected]             }
56965a46ef3S[email protected] #endif
570188981d2Smatthias.ringwald             // Dump local address
571188981d2Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) {
5723a9fb326S[email protected]                 bt_flip_addr(hci_stack->local_bd_addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]);
573188981d2Smatthias.ringwald                 log_info("Local Address, Status: 0x%02x: Addr: %s\n",
5743a9fb326S[email protected]                     packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr));
575188981d2Smatthias.ringwald             }
576381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
5773a9fb326S[email protected]                 hci_emit_discoverable_enabled(hci_stack->discoverable);
578381fbed8Smatthias.ringwald             }
57965389bfcS[email protected]             // Note: HCI init checks
580559e517eS[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){
5813a9fb326S[email protected]                 memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8);
582559e517eS[email protected]                 log_info("Local Supported Features: 0x%02x%02x%02x%02x%02x%02x%02x%02x",
5833a9fb326S[email protected]                     hci_stack->local_supported_features[0], hci_stack->local_supported_features[1],
5843a9fb326S[email protected]                     hci_stack->local_supported_features[2], hci_stack->local_supported_features[3],
5853a9fb326S[email protected]                     hci_stack->local_supported_features[4], hci_stack->local_supported_features[5],
5863a9fb326S[email protected]                     hci_stack->local_supported_features[6], hci_stack->local_supported_features[7]);
58765389bfcS[email protected] 
58865389bfcS[email protected]                 // determine usable ACL packet types based buffer size and supported features
5893a9fb326S[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]);
5903a9fb326S[email protected]                 log_info("packet types %04x", hci_stack->packet_types);
591f5d8d141S[email protected] 
592f5d8d141S[email protected]                 // Classic/LE
593f5d8d141S[email protected]                 log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported());
594559e517eS[email protected]             }
59556cf178bSmatthias.ringwald             break;
59656cf178bSmatthias.ringwald 
5977ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
5987ec5eeaaSmatthias.ringwald             // get num cmd packets
5993a9fb326S[email protected]             // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack->num_cmd_packets, packet[3]);
6003a9fb326S[email protected]             hci_stack->num_cmd_packets = packet[3];
6017ec5eeaaSmatthias.ringwald             break;
6027ec5eeaaSmatthias.ringwald 
60356cf178bSmatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
60456cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
60556cf178bSmatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
60656cf178bSmatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
6075061f3afS[email protected]                 conn = hci_connection_for_handle(handle);
60856cf178bSmatthias.ringwald                 if (!conn){
6097d67539fSmatthias.ringwald                     log_error("hci_number_completed_packet lists unused con handle %u\n", handle);
61056cf178bSmatthias.ringwald                     continue;
61156cf178bSmatthias.ringwald                 }
61256cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
6137b5fbe1fSmatthias.ringwald                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
61456cf178bSmatthias.ringwald             }
6156772a24cSmatthias.ringwald             break;
6166772a24cSmatthias.ringwald 
6171f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
61837eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
61937eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
6202d00edd4Smatthias.ringwald             link_type = packet[11];
621339b6768Smatthias.ringwald             log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type);
62237eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
623*96a45072S[email protected]                 conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
6241f7b95a1Smatthias.ringwald                 if (!conn) {
625*96a45072S[email protected]                     conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
6261f7b95a1Smatthias.ringwald                 }
627ce4c8fabSmatthias.ringwald                 if (!conn) {
628ce4c8fabSmatthias.ringwald                     // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
6293a9fb326S[email protected]                     hci_stack->decline_reason = 0x0d;
6303a9fb326S[email protected]                     BD_ADDR_COPY(hci_stack->decline_addr, addr);
631ce4c8fabSmatthias.ringwald                     break;
632ce4c8fabSmatthias.ringwald                 }
63332ab9390Smatthias.ringwald                 conn->state = RECEIVED_CONNECTION_REQUEST;
63432ab9390Smatthias.ringwald                 hci_run();
63537eaa4cfSmatthias.ringwald             } else {
636ce4c8fabSmatthias.ringwald                 // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A)
6373a9fb326S[email protected]                 hci_stack->decline_reason = 0x0a;
6383a9fb326S[email protected]                 BD_ADDR_COPY(hci_stack->decline_addr, addr);
63937eaa4cfSmatthias.ringwald             }
6401f7b95a1Smatthias.ringwald             break;
6411f7b95a1Smatthias.ringwald 
6426772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
643fe1ed1b8Smatthias.ringwald             // Connection management
644fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
645339b6768Smatthias.ringwald             log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr));
646*96a45072S[email protected]             addr_type = BD_ADDR_TYPE_CLASSIC;
647*96a45072S[email protected]             conn = hci_connection_for_bd_addr_and_type(&addr, addr_type);
648fe1ed1b8Smatthias.ringwald             if (conn) {
649b448a0e7Smatthias.ringwald                 if (!packet[2]){
650c8e4258aSmatthias.ringwald                     conn->state = OPEN;
651fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
652ad83dc6aS[email protected]                     conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES;
653ee091cf1Smatthias.ringwald 
654c785ef68Smatthias.ringwald                     // restart timer
655c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
656ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
657c785ef68Smatthias.ringwald 
658339b6768Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
65943bfb1bdSmatthias.ringwald 
66043bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
661b448a0e7Smatthias.ringwald                 } else {
662ad83dc6aS[email protected]                     // notify client if dedicated bonding
663ad83dc6aS[email protected]                     if (conn->bonding_flags & BONDING_DEDICATED){
664ad83dc6aS[email protected]                         hci_emit_dedicated_bonding_result(conn, packet[2]);
665ad83dc6aS[email protected]                     }
666ad83dc6aS[email protected] 
667b448a0e7Smatthias.ringwald                     // connection failed, remove entry
6683a9fb326S[email protected]                     linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
669a3b02b71Smatthias.ringwald                     btstack_memory_hci_connection_free( conn );
670c12e46e7Smatthias.ringwald 
671c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
672c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
673c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
674c12e46e7Smatthias.ringwald                     }
675fe1ed1b8Smatthias.ringwald                 }
676fe1ed1b8Smatthias.ringwald             }
6776772a24cSmatthias.ringwald             break;
678fe1ed1b8Smatthias.ringwald 
679afd4e962S[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
680afd4e962S[email protected]             handle = READ_BT_16(packet, 3);
681afd4e962S[email protected]             conn = hci_connection_for_handle(handle);
682afd4e962S[email protected]             if (!conn) break;
683afd4e962S[email protected]             if (!packet[2]){
684afd4e962S[email protected]                 uint8_t * features = &packet[5];
685afd4e962S[email protected]                 if (features[6] & (1 << 3)){
686afd4e962S[email protected]                     conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP;
687afd4e962S[email protected]                 }
688afd4e962S[email protected]             }
689afd4e962S[email protected]             conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES;
690ad83dc6aS[email protected]             log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x", conn->bonding_flags);
691ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
692ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
693ad83dc6aS[email protected]             }
694afd4e962S[email protected]             break;
695afd4e962S[email protected] 
6967fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
6977b5fbe1fSmatthias.ringwald             log_info("HCI_EVENT_LINK_KEY_REQUEST\n");
6987fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
69974d716b5S[email protected]             // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST
7003a9fb326S[email protected]             if (hci_stack->bondable && !hci_stack->remote_device_db) break;
70132ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
70264472d52Smatthias.ringwald             hci_run();
703d9a327f9Smatthias.ringwald             // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set
70429d53098Smatthias.ringwald             return;
7057fde4af9Smatthias.ringwald 
7069ab95c90S[email protected]         case HCI_EVENT_LINK_KEY_NOTIFICATION: {
70729d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
708*96a45072S[email protected]             conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
7099ab95c90S[email protected]             if (!conn) break;
7109ab95c90S[email protected]             conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION;
7119ab95c90S[email protected]             link_key_type_t link_key_type = packet[24];
7129ab95c90S[email protected]             // Change Connection Encryption keeps link key type
7139ab95c90S[email protected]             if (link_key_type != CHANGED_COMBINATION_KEY){
7149ab95c90S[email protected]                 conn->link_key_type = link_key_type;
7159ab95c90S[email protected]             }
7163a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
7173a9fb326S[email protected]             hci_stack->remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8], conn->link_key_type);
71829d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
7197fde4af9Smatthias.ringwald             break;
7209ab95c90S[email protected]         }
7217fde4af9Smatthias.ringwald 
7227fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
7236724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE);
7244c57c146S[email protected]             // non-bondable mode: pin code negative reply will be sent
7253a9fb326S[email protected]             if (!hci_stack->bondable){
726899283eaS[email protected]                 hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST);
727f8fb5f6eS[email protected]                 hci_run();
728f8fb5f6eS[email protected]                 return;
7294c57c146S[email protected]             }
730d9a327f9Smatthias.ringwald             // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key
7313a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
732d9a327f9Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
7333a9fb326S[email protected]             hci_stack->remote_device_db->delete_link_key(&addr);
7347fde4af9Smatthias.ringwald             break;
7357fde4af9Smatthias.ringwald 
7361d6b20aeS[email protected]         case HCI_EVENT_IO_CAPABILITY_REQUEST:
7371d6b20aeS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST);
738dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY);
739dbe1a790S[email protected]             break;
740dbe1a790S[email protected] 
741dbe1a790S[email protected]         case HCI_EVENT_USER_CONFIRMATION_REQUEST:
7426724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
7433a9fb326S[email protected]             if (!hci_stack->ssp_auto_accept) break;
744dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY);
745dbe1a790S[email protected]             break;
746dbe1a790S[email protected] 
747dbe1a790S[email protected]         case HCI_EVENT_USER_PASSKEY_REQUEST:
7486724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
7493a9fb326S[email protected]             if (!hci_stack->ssp_auto_accept) break;
750dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY);
7511d6b20aeS[email protected]             break;
7521d6b20aeS[email protected] 
753f0944df2S[email protected]         case HCI_EVENT_ENCRYPTION_CHANGE:
754f0944df2S[email protected]             handle = READ_BT_16(packet, 3);
755f0944df2S[email protected]             conn = hci_connection_for_handle(handle);
756f0944df2S[email protected]             if (!conn) break;
757ad83dc6aS[email protected]             if (packet[2] == 0) {
758f0944df2S[email protected]                 if (packet[5]){
759f0944df2S[email protected]                     conn->authentication_flags |= CONNECTION_ENCRYPTED;
760f0944df2S[email protected]                 } else {
761536f9994S[email protected]                     conn->authentication_flags &= ~CONNECTION_ENCRYPTED;
762f0944df2S[email protected]                 }
763ad83dc6aS[email protected]             }
764a00031e2S[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
765f0944df2S[email protected]             break;
766f0944df2S[email protected] 
7671eb2563eS[email protected]         case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT:
7681eb2563eS[email protected]             handle = READ_BT_16(packet, 3);
7691eb2563eS[email protected]             conn = hci_connection_for_handle(handle);
7701eb2563eS[email protected]             if (!conn) break;
771ad83dc6aS[email protected] 
772ad83dc6aS[email protected]             // dedicated bonding: send result and disconnect
773ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
774ad83dc6aS[email protected]                 conn->bonding_flags &= ~BONDING_DEDICATED;
775ad83dc6aS[email protected]                 hci_emit_dedicated_bonding_result( conn, packet[2]);
776ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE;
777ad83dc6aS[email protected]                 break;
778ad83dc6aS[email protected]             }
779ad83dc6aS[email protected] 
780b5cb6874S[email protected]             if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){
7811eb2563eS[email protected]                 // link key sufficient for requested security
7821eb2563eS[email protected]                 conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
783ad83dc6aS[email protected]                 break;
784ad83dc6aS[email protected]             }
7851eb2563eS[email protected]             // not enough
7861eb2563eS[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
7871eb2563eS[email protected]             break;
78834d2123cS[email protected] 
789223aafc1Smatthias.ringwald #ifndef EMBEDDED
79074ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
7913a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
79274ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
79374ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
7945a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
7955a06394aSmatthias.ringwald             for (i=0; i<248;i++){
7965a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
7975a06394aSmatthias.ringwald                     packet[9+i] = 0;
7985a06394aSmatthias.ringwald                     break;
799cdc9101dSmatthias.ringwald                 }
8005a06394aSmatthias.ringwald             }
801d2fe945cS[email protected]             memset(&device_name, 0, sizeof(device_name_t));
80274ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
8033a9fb326S[email protected]             hci_stack->remote_device_db->put_name(&addr, &device_name);
80474ec757aSmatthias.ringwald             break;
80574ec757aSmatthias.ringwald 
80674ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
80774ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
8083a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
80974ec757aSmatthias.ringwald             // first send inq result packet
8103a9fb326S[email protected]             hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size);
81174ec757aSmatthias.ringwald             // then send cached remote names
81274ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
81374ec757aSmatthias.ringwald                 bt_flip_addr(addr, &packet[3+i*6]);
8143a9fb326S[email protected]                 if (hci_stack->remote_device_db->get_name(&addr, &device_name)){
81574ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
81674ec757aSmatthias.ringwald                 }
81774ec757aSmatthias.ringwald             }
81874ec757aSmatthias.ringwald             return;
819223aafc1Smatthias.ringwald #endif
82074ec757aSmatthias.ringwald 
8216772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
822fe1ed1b8Smatthias.ringwald             if (!packet[2]){
823fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
8245061f3afS[email protected]                 hci_connection_t * conn = hci_connection_for_handle(handle);
825fe1ed1b8Smatthias.ringwald                 if (conn) {
826c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
827fe1ed1b8Smatthias.ringwald                 }
828fe1ed1b8Smatthias.ringwald             }
8296772a24cSmatthias.ringwald             break;
8306772a24cSmatthias.ringwald 
831c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
8323a9fb326S[email protected]             if(hci_stack->control && hci_stack->control->hw_error){
8333a9fb326S[email protected]                 (*hci_stack->control->hw_error)();
834c68bdf90Smatthias.ringwald             }
835c68bdf90Smatthias.ringwald             break;
836c68bdf90Smatthias.ringwald 
8376b4af23dS[email protected]         case DAEMON_EVENT_HCI_PACKET_SENT:
8386b4af23dS[email protected]             // free packet buffer for asynchronous transport
8396b4af23dS[email protected]             if (hci_transport_synchronous()) break;
8406b4af23dS[email protected]             hci_stack->hci_packet_buffer_reserved = 0;
8416b4af23dS[email protected]             break;
8426b4af23dS[email protected] 
8435909f7f2Smatthias.ringwald #ifdef HAVE_BLE
8445909f7f2Smatthias.ringwald         case HCI_EVENT_LE_META:
8455909f7f2Smatthias.ringwald             switch (packet[2]) {
8465909f7f2Smatthias.ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
8475909f7f2Smatthias.ringwald                     // Connection management
8485909f7f2Smatthias.ringwald                     bt_flip_addr(addr, &packet[8]);
849*96a45072S[email protected]                     addr_type = packet[7];
850*96a45072S[email protected]                     log_info("LE Connection_complete (status=%u) type %u, %s\n", packet[3], addr_type, bd_addr_to_str(addr));
8515909f7f2Smatthias.ringwald                     // LE connections are auto-accepted, so just create a connection if there isn't one already
852*96a45072S[email protected]                     conn = hci_connection_for_bd_addr_and_type(&addr, addr_type);
8535909f7f2Smatthias.ringwald                     if (packet[3]){
8545909f7f2Smatthias.ringwald                         if (conn){
8555909f7f2Smatthias.ringwald                             // outgoing connection failed, remove entry
8563a9fb326S[email protected]                             linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
8575909f7f2Smatthias.ringwald                             btstack_memory_hci_connection_free( conn );
8585909f7f2Smatthias.ringwald 
8595909f7f2Smatthias.ringwald                         }
8605909f7f2Smatthias.ringwald                         // if authentication error, also delete link key
8615909f7f2Smatthias.ringwald                         if (packet[3] == 0x05) {
8625909f7f2Smatthias.ringwald                             hci_drop_link_key_for_bd_addr(&addr);
8635909f7f2Smatthias.ringwald                         }
8645909f7f2Smatthias.ringwald                         break;
8655909f7f2Smatthias.ringwald                     }
8665909f7f2Smatthias.ringwald                     if (!conn){
867*96a45072S[email protected]                         conn = create_connection_for_bd_addr_and_type(addr, addr_type);
8685909f7f2Smatthias.ringwald                     }
8695909f7f2Smatthias.ringwald                     if (!conn){
8705909f7f2Smatthias.ringwald                         // no memory
8715909f7f2Smatthias.ringwald                         break;
8725909f7f2Smatthias.ringwald                     }
8735909f7f2Smatthias.ringwald 
8745909f7f2Smatthias.ringwald                     conn->state = OPEN;
8755909f7f2Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 4);
8765909f7f2Smatthias.ringwald 
8775909f7f2Smatthias.ringwald                     // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock
8785909f7f2Smatthias.ringwald 
8795909f7f2Smatthias.ringwald                     // restart timer
8805909f7f2Smatthias.ringwald                     // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
8815909f7f2Smatthias.ringwald                     // run_loop_add_timer(&conn->timeout);
8825909f7f2Smatthias.ringwald 
8835909f7f2Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
8845909f7f2Smatthias.ringwald 
8855909f7f2Smatthias.ringwald                     hci_emit_nr_connections_changed();
8865909f7f2Smatthias.ringwald                     break;
8875909f7f2Smatthias.ringwald 
88865a46ef3S[email protected]             // printf("LE buffer size: %u, count %u\n", READ_BT_16(packet,6), packet[8]);
88965a46ef3S[email protected] 
8905909f7f2Smatthias.ringwald                 default:
8915909f7f2Smatthias.ringwald                     break;
8925909f7f2Smatthias.ringwald             }
8935909f7f2Smatthias.ringwald             break;
8945909f7f2Smatthias.ringwald #endif
8955909f7f2Smatthias.ringwald 
8966772a24cSmatthias.ringwald         default:
8976772a24cSmatthias.ringwald             break;
898fe1ed1b8Smatthias.ringwald     }
899fe1ed1b8Smatthias.ringwald 
9003429f56bSmatthias.ringwald     // handle BT initialization
9013a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_INITIALIZING){
9023a9fb326S[email protected]         if (hci_stack->substate % 2){
9033429f56bSmatthias.ringwald             // odd: waiting for event
904f155fca3Smatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || packet[0] == HCI_EVENT_COMMAND_STATUS){
905c9af4d3fS[email protected]                 // wait for explicit COMMAND COMPLETE on RESET
9063a9fb326S[email protected]                 if (hci_stack->substate > 1 || COMMAND_COMPLETE_EVENT(packet, hci_reset)) {
9073a9fb326S[email protected]                     hci_stack->substate++;
9083429f56bSmatthias.ringwald                 }
9093429f56bSmatthias.ringwald             }
91022909952Smatthias.ringwald         }
911c9af4d3fS[email protected]     }
91222909952Smatthias.ringwald 
91389db417bSmatthias.ringwald     // help with BT sleep
9143a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_FALLING_ASLEEP
9153a9fb326S[email protected]         && hci_stack->substate == 1
91689db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
9173a9fb326S[email protected]         hci_stack->substate++;
91889db417bSmatthias.ringwald     }
91989db417bSmatthias.ringwald 
9203a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size);
92194ab26f8Smatthias.ringwald 
92294ab26f8Smatthias.ringwald 	// execute main loop
92394ab26f8Smatthias.ringwald 	hci_run();
92416833f0aSmatthias.ringwald }
92516833f0aSmatthias.ringwald 
9260a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
92710e830c9Smatthias.ringwald     switch (packet_type) {
92810e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
92910e830c9Smatthias.ringwald             event_handler(packet, size);
93010e830c9Smatthias.ringwald             break;
93110e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
93210e830c9Smatthias.ringwald             acl_handler(packet, size);
93310e830c9Smatthias.ringwald             break;
93410e830c9Smatthias.ringwald         default:
93510e830c9Smatthias.ringwald             break;
93610e830c9Smatthias.ringwald     }
93710e830c9Smatthias.ringwald }
93810e830c9Smatthias.ringwald 
939fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
9402718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
9413a9fb326S[email protected]     hci_stack->packet_handler = handler;
94216833f0aSmatthias.ringwald }
94316833f0aSmatthias.ringwald 
9444f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){
945475c8125Smatthias.ringwald 
9463a9fb326S[email protected] #ifdef HAVE_MALLOC
9473a9fb326S[email protected]     if (!hci_stack) {
9483a9fb326S[email protected]         hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t));
9493a9fb326S[email protected]     }
9503a9fb326S[email protected] #else
9513a9fb326S[email protected]     hci_stack = &hci_stack_static;
9523a9fb326S[email protected] #endif
95366fb9560S[email protected]     memset(hci_stack, 0, sizeof(hci_stack_t));
9543a9fb326S[email protected] 
955475c8125Smatthias.ringwald     // reference to use transport layer implementation
9563a9fb326S[email protected]     hci_stack->hci_transport = transport;
957475c8125Smatthias.ringwald 
95811e23e5fSmatthias.ringwald     // references to used control implementation
9593a9fb326S[email protected]     hci_stack->control = control;
96011e23e5fSmatthias.ringwald 
96111e23e5fSmatthias.ringwald     // reference to used config
9623a9fb326S[email protected]     hci_stack->config = config;
96311e23e5fSmatthias.ringwald 
964fe1ed1b8Smatthias.ringwald     // no connections yet
9653a9fb326S[email protected]     hci_stack->connections = NULL;
9663a9fb326S[email protected]     hci_stack->discoverable = 0;
9673a9fb326S[email protected]     hci_stack->connectable = 0;
9683a9fb326S[email protected]     hci_stack->bondable = 1;
969758b46ceSmatthias.ringwald 
970b031bebbSmatthias.ringwald     // no pending cmds
9713a9fb326S[email protected]     hci_stack->decline_reason = 0;
9723a9fb326S[email protected]     hci_stack->new_scan_enable_value = 0xff;
973b031bebbSmatthias.ringwald 
97416833f0aSmatthias.ringwald     // higher level handler
9753a9fb326S[email protected]     hci_stack->packet_handler = dummy_handler;
97616833f0aSmatthias.ringwald 
977404843c1Smatthias.ringwald     // store and open remote device db
9783a9fb326S[email protected]     hci_stack->remote_device_db = remote_device_db;
9793a9fb326S[email protected]     if (hci_stack->remote_device_db) {
9803a9fb326S[email protected]         hci_stack->remote_device_db->open();
981404843c1Smatthias.ringwald     }
98229d53098Smatthias.ringwald 
9838fcba05dSmatthias.ringwald     // max acl payload size defined in config.h
9843a9fb326S[email protected]     hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
9858fcba05dSmatthias.ringwald 
98616833f0aSmatthias.ringwald     // register packet handlers with transport
98710e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
988f5454fc6Smatthias.ringwald 
9893a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
990e2386ba1S[email protected] 
991e2386ba1S[email protected]     // class of device
9923a9fb326S[email protected]     hci_stack->class_of_device = 0x007a020c; // Smartphone
993a45d6b9fS[email protected] 
99463048403S[email protected]     // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept
9953a9fb326S[email protected]     hci_stack->ssp_enable = 1;
9963a9fb326S[email protected]     hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT;
9973a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING;
9983a9fb326S[email protected]     hci_stack->ssp_auto_accept = 1;
99969a97523S[email protected] 
100069a97523S[email protected]     // LE
10013a9fb326S[email protected]     hci_stack->adv_addr_type = 0;
10023a9fb326S[email protected]     memset(hci_stack->adv_address, 0, 6);
1003475c8125Smatthias.ringwald }
1004475c8125Smatthias.ringwald 
1005404843c1Smatthias.ringwald void hci_close(){
1006404843c1Smatthias.ringwald     // close remote device db
10073a9fb326S[email protected]     if (hci_stack->remote_device_db) {
10083a9fb326S[email protected]         hci_stack->remote_device_db->close();
1009404843c1Smatthias.ringwald     }
10103a9fb326S[email protected]     while (hci_stack->connections) {
10113a9fb326S[email protected]         hci_shutdown_connection((hci_connection_t *) hci_stack->connections);
1012f5454fc6Smatthias.ringwald     }
1013f5454fc6Smatthias.ringwald     hci_power_control(HCI_POWER_OFF);
10143a9fb326S[email protected] 
10153a9fb326S[email protected] #ifdef HAVE_MALLOC
10163a9fb326S[email protected]     free(hci_stack);
10173a9fb326S[email protected] #endif
10183a9fb326S[email protected]     hci_stack = NULL;
1019404843c1Smatthias.ringwald }
1020404843c1Smatthias.ringwald 
10219e61646fS[email protected] void hci_set_class_of_device(uint32_t class_of_device){
10229e61646fS[email protected]     hci_stack->class_of_device = class_of_device;
10239e61646fS[email protected] }
10249e61646fS[email protected] 
102566fb9560S[email protected] void hci_disable_l2cap_timeout_check(){
102666fb9560S[email protected]     disable_l2cap_timeouts = 1;
102766fb9560S[email protected] }
10288d213e1aSmatthias.ringwald // State-Module-Driver overview
10298d213e1aSmatthias.ringwald // state                    module  low-level
10308d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
10318d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
10328d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
10338d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
1034d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
1035d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
1036c7e0c5f6Smatthias.ringwald 
103740d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
10387301ad89Smatthias.ringwald 
1039038bc64cSmatthias.ringwald     // power on
1040f9a30166Smatthias.ringwald     int err = 0;
10413a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->on){
10423a9fb326S[email protected]         err = (*hci_stack->control->on)(hci_stack->config);
1043f9a30166Smatthias.ringwald     }
1044038bc64cSmatthias.ringwald     if (err){
10457d67539fSmatthias.ringwald         log_error( "POWER_ON failed\n");
1046038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
1047038bc64cSmatthias.ringwald         return err;
1048038bc64cSmatthias.ringwald     }
1049038bc64cSmatthias.ringwald 
1050038bc64cSmatthias.ringwald     // open low-level device
10513a9fb326S[email protected]     err = hci_stack->hci_transport->open(hci_stack->config);
1052038bc64cSmatthias.ringwald     if (err){
10537d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
10543a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
10553a9fb326S[email protected]             (*hci_stack->control->off)(hci_stack->config);
1056f9a30166Smatthias.ringwald         }
1057038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
1058038bc64cSmatthias.ringwald         return err;
1059038bc64cSmatthias.ringwald     }
10608d213e1aSmatthias.ringwald     return 0;
10618d213e1aSmatthias.ringwald }
1062038bc64cSmatthias.ringwald 
106340d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
10648d213e1aSmatthias.ringwald 
10657b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off\n");
10669418f9c9Smatthias.ringwald 
10678d213e1aSmatthias.ringwald     // close low-level device
10683a9fb326S[email protected]     hci_stack->hci_transport->close(hci_stack->config);
10698d213e1aSmatthias.ringwald 
10707b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - hci_transport closed\n");
10719418f9c9Smatthias.ringwald 
10728d213e1aSmatthias.ringwald     // power off
10733a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->off){
10743a9fb326S[email protected]         (*hci_stack->control->off)(hci_stack->config);
10758d213e1aSmatthias.ringwald     }
10769418f9c9Smatthias.ringwald 
10777b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - control closed\n");
10789418f9c9Smatthias.ringwald 
10793a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
108072ea5239Smatthias.ringwald }
108172ea5239Smatthias.ringwald 
108240d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
108372ea5239Smatthias.ringwald 
10847b5fbe1fSmatthias.ringwald     log_info("hci_power_control_sleep\n");
10853144bce4Smatthias.ringwald 
1086b429b9b7Smatthias.ringwald #if 0
1087b429b9b7Smatthias.ringwald     // don't close serial port during sleep
1088b429b9b7Smatthias.ringwald 
108972ea5239Smatthias.ringwald     // close low-level device
10903a9fb326S[email protected]     hci_stack->hci_transport->close(hci_stack->config);
1091b429b9b7Smatthias.ringwald #endif
109272ea5239Smatthias.ringwald 
109372ea5239Smatthias.ringwald     // sleep mode
10943a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->sleep){
10953a9fb326S[email protected]         (*hci_stack->control->sleep)(hci_stack->config);
109672ea5239Smatthias.ringwald     }
1097b429b9b7Smatthias.ringwald 
10983a9fb326S[email protected]     hci_stack->state = HCI_STATE_SLEEPING;
10998d213e1aSmatthias.ringwald }
11008d213e1aSmatthias.ringwald 
110140d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
1102b429b9b7Smatthias.ringwald 
11037b5fbe1fSmatthias.ringwald     log_info("hci_power_control_wake\n");
1104b429b9b7Smatthias.ringwald 
1105b429b9b7Smatthias.ringwald     // wake on
11063a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->wake){
11073a9fb326S[email protected]         (*hci_stack->control->wake)(hci_stack->config);
1108b429b9b7Smatthias.ringwald     }
1109b429b9b7Smatthias.ringwald 
1110b429b9b7Smatthias.ringwald #if 0
1111b429b9b7Smatthias.ringwald     // open low-level device
11123a9fb326S[email protected]     int err = hci_stack->hci_transport->open(hci_stack->config);
1113b429b9b7Smatthias.ringwald     if (err){
11147d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
11153a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
11163a9fb326S[email protected]             (*hci_stack->control->off)(hci_stack->config);
1117b429b9b7Smatthias.ringwald         }
1118b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
1119b429b9b7Smatthias.ringwald         return err;
1120b429b9b7Smatthias.ringwald     }
1121b429b9b7Smatthias.ringwald #endif
1122b429b9b7Smatthias.ringwald 
1123b429b9b7Smatthias.ringwald     return 0;
1124b429b9b7Smatthias.ringwald }
1125b429b9b7Smatthias.ringwald 
1126b429b9b7Smatthias.ringwald 
11278d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
1128d661ed19Smatthias.ringwald 
11293a9fb326S[email protected]     log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack->state);
1130d661ed19Smatthias.ringwald 
11318d213e1aSmatthias.ringwald     int err = 0;
11323a9fb326S[email protected]     switch (hci_stack->state){
11338d213e1aSmatthias.ringwald 
11348d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
11358d213e1aSmatthias.ringwald             switch (power_mode){
11368d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
11378d213e1aSmatthias.ringwald                     err = hci_power_control_on();
11388d213e1aSmatthias.ringwald                     if (err) return err;
11397301ad89Smatthias.ringwald                     // set up state machine
11403a9fb326S[email protected]                     hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent
11413a9fb326S[email protected]                     hci_stack->state = HCI_STATE_INITIALIZING;
11423a9fb326S[email protected]                     hci_stack->substate = 0;
11438d213e1aSmatthias.ringwald                     break;
11448d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
11458d213e1aSmatthias.ringwald                     // do nothing
11468d213e1aSmatthias.ringwald                     break;
11478d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1148b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
11498d213e1aSmatthias.ringwald                     break;
11508d213e1aSmatthias.ringwald             }
11518d213e1aSmatthias.ringwald             break;
11527301ad89Smatthias.ringwald 
11538d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
11548d213e1aSmatthias.ringwald             switch (power_mode){
11558d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
11568d213e1aSmatthias.ringwald                     // do nothing
11578d213e1aSmatthias.ringwald                     break;
11588d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
11598d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
11608d213e1aSmatthias.ringwald                     hci_power_control_off();
11618d213e1aSmatthias.ringwald                     break;
11628d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1163b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
116472ea5239Smatthias.ringwald                     hci_power_control_sleep();
11658d213e1aSmatthias.ringwald                     break;
11668d213e1aSmatthias.ringwald             }
11678d213e1aSmatthias.ringwald             break;
11687301ad89Smatthias.ringwald 
11698d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
11708d213e1aSmatthias.ringwald             switch (power_mode){
11718d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
11728d213e1aSmatthias.ringwald                     // do nothing
11738d213e1aSmatthias.ringwald                     break;
11748d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1175c7e0c5f6Smatthias.ringwald                     // see hci_run
11763a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
11778d213e1aSmatthias.ringwald                     break;
11788d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1179b546ac54Smatthias.ringwald                     // see hci_run
11803a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
11813a9fb326S[email protected]                     hci_stack->substate = 0;
11828d213e1aSmatthias.ringwald                     break;
11838d213e1aSmatthias.ringwald             }
11848d213e1aSmatthias.ringwald             break;
11857301ad89Smatthias.ringwald 
11868d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
11878d213e1aSmatthias.ringwald             switch (power_mode){
11888d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
11898d213e1aSmatthias.ringwald                     // set up state machine
11903a9fb326S[email protected]                     hci_stack->state = HCI_STATE_INITIALIZING;
11913a9fb326S[email protected]                     hci_stack->substate = 0;
11928d213e1aSmatthias.ringwald                     break;
11938d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
11948d213e1aSmatthias.ringwald                     // do nothing
11958d213e1aSmatthias.ringwald                     break;
11968d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1197b546ac54Smatthias.ringwald                     // see hci_run
11983a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
11993a9fb326S[email protected]                     hci_stack->substate = 0;
12008d213e1aSmatthias.ringwald                     break;
12018d213e1aSmatthias.ringwald             }
12028d213e1aSmatthias.ringwald             break;
12038d213e1aSmatthias.ringwald 
12048d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
12058d213e1aSmatthias.ringwald             switch (power_mode){
12068d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
120728171530Smatthias.ringwald 
120828171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
120928171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
121028171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
12113a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
12123a9fb326S[email protected]                         hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
121328171530Smatthias.ringwald                         break;
121428171530Smatthias.ringwald                     }
121528171530Smatthias.ringwald #endif
1216b546ac54Smatthias.ringwald                     // set up state machine
12173a9fb326S[email protected]                     hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent
12183a9fb326S[email protected]                     hci_stack->state = HCI_STATE_INITIALIZING;
12193a9fb326S[email protected]                     hci_stack->substate = 0;
12208d213e1aSmatthias.ringwald                     break;
12218d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1222b546ac54Smatthias.ringwald                     // see hci_run
12233a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
12248d213e1aSmatthias.ringwald                     break;
12258d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1226b546ac54Smatthias.ringwald                     // do nothing
12278d213e1aSmatthias.ringwald                     break;
12288d213e1aSmatthias.ringwald             }
12298d213e1aSmatthias.ringwald             break;
12308d213e1aSmatthias.ringwald 
12318d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
12328d213e1aSmatthias.ringwald             switch (power_mode){
12338d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
123428171530Smatthias.ringwald 
123528171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
123628171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
123728171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
12383a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
12393a9fb326S[email protected]                         hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
1240758b46ceSmatthias.ringwald                         hci_update_scan_enable();
124128171530Smatthias.ringwald                         break;
124228171530Smatthias.ringwald                     }
124328171530Smatthias.ringwald #endif
12443144bce4Smatthias.ringwald                     err = hci_power_control_wake();
12453144bce4Smatthias.ringwald                     if (err) return err;
1246b546ac54Smatthias.ringwald                     // set up state machine
12473a9fb326S[email protected]                     hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent
12483a9fb326S[email protected]                     hci_stack->state = HCI_STATE_INITIALIZING;
12493a9fb326S[email protected]                     hci_stack->substate = 0;
12508d213e1aSmatthias.ringwald                     break;
12518d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
12523a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
12538d213e1aSmatthias.ringwald                     break;
12548d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1255b546ac54Smatthias.ringwald                     // do nothing
12568d213e1aSmatthias.ringwald                     break;
12578d213e1aSmatthias.ringwald             }
12588d213e1aSmatthias.ringwald             break;
125911e23e5fSmatthias.ringwald     }
126068d92d03Smatthias.ringwald 
1261038bc64cSmatthias.ringwald     // create internal event
1262ee8bf225Smatthias.ringwald 	hci_emit_state();
1263ee8bf225Smatthias.ringwald 
126468d92d03Smatthias.ringwald 	// trigger next/first action
126568d92d03Smatthias.ringwald 	hci_run();
126668d92d03Smatthias.ringwald 
1267475c8125Smatthias.ringwald     return 0;
1268475c8125Smatthias.ringwald }
1269475c8125Smatthias.ringwald 
1270758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){
1271758b46ceSmatthias.ringwald     // 2 = page scan, 1 = inq scan
12723a9fb326S[email protected]     hci_stack->new_scan_enable_value  = hci_stack->connectable << 1 | hci_stack->discoverable;
1273758b46ceSmatthias.ringwald     hci_run();
1274758b46ceSmatthias.ringwald }
1275758b46ceSmatthias.ringwald 
1276381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
1277381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
1278381fbed8Smatthias.ringwald 
12793a9fb326S[email protected]     if (hci_stack->discoverable == enable){
12803a9fb326S[email protected]         hci_emit_discoverable_enabled(hci_stack->discoverable);
1281381fbed8Smatthias.ringwald         return;
1282381fbed8Smatthias.ringwald     }
1283381fbed8Smatthias.ringwald 
12843a9fb326S[email protected]     hci_stack->discoverable = enable;
1285758b46ceSmatthias.ringwald     hci_update_scan_enable();
1286758b46ceSmatthias.ringwald }
1287b031bebbSmatthias.ringwald 
1288758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){
1289758b46ceSmatthias.ringwald     if (enable) enable = 1; // normalize argument
1290758b46ceSmatthias.ringwald 
1291758b46ceSmatthias.ringwald     // don't emit event
12923a9fb326S[email protected]     if (hci_stack->connectable == enable) return;
1293758b46ceSmatthias.ringwald 
12943a9fb326S[email protected]     hci_stack->connectable = enable;
1295758b46ceSmatthias.ringwald     hci_update_scan_enable();
1296381fbed8Smatthias.ringwald }
1297381fbed8Smatthias.ringwald 
12985061f3afS[email protected] bd_addr_t * hci_local_bd_addr(void){
12993a9fb326S[email protected]     return &hci_stack->local_bd_addr;
13005061f3afS[email protected] }
13015061f3afS[email protected] 
130206b35ec0Smatthias.ringwald void hci_run(){
13038a485f27Smatthias.ringwald 
130432ab9390Smatthias.ringwald     hci_connection_t * connection;
130532ab9390Smatthias.ringwald     linked_item_t * it;
130632ab9390Smatthias.ringwald 
130764f0b431S[email protected]     if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) return;
1308ce4c8fabSmatthias.ringwald 
1309b031bebbSmatthias.ringwald     // global/non-connection oriented commands
1310b031bebbSmatthias.ringwald 
1311b031bebbSmatthias.ringwald     // decline incoming connections
13123a9fb326S[email protected]     if (hci_stack->decline_reason){
13133a9fb326S[email protected]         uint8_t reason = hci_stack->decline_reason;
13143a9fb326S[email protected]         hci_stack->decline_reason = 0;
13153a9fb326S[email protected]         hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason);
1316dbe1a790S[email protected]         return;
1317ce4c8fabSmatthias.ringwald     }
1318ce4c8fabSmatthias.ringwald 
1319b031bebbSmatthias.ringwald     // send scan enable
13203a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_WORKING && hci_stack->new_scan_enable_value != 0xff && hci_classic_supported()){
13213a9fb326S[email protected]         hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value);
13223a9fb326S[email protected]         hci_stack->new_scan_enable_value = 0xff;
1323dbe1a790S[email protected]         return;
1324b031bebbSmatthias.ringwald     }
1325b031bebbSmatthias.ringwald 
132632ab9390Smatthias.ringwald     // send pending HCI commands
13273a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
132832ab9390Smatthias.ringwald 
1329b031bebbSmatthias.ringwald         connection = (hci_connection_t *) it;
133032ab9390Smatthias.ringwald 
1331ad83dc6aS[email protected]         if (connection->state == SEND_CREATE_CONNECTION){
1332ad83dc6aS[email protected]             log_info("sending hci_create_connection\n");
1333ad83dc6aS[email protected]             hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
1334ad83dc6aS[email protected]             return;
1335ad83dc6aS[email protected]         }
1336ad83dc6aS[email protected] 
133732ab9390Smatthias.ringwald         if (connection->state == RECEIVED_CONNECTION_REQUEST){
13387b5fbe1fSmatthias.ringwald             log_info("sending hci_accept_connection_request\n");
133932ab9390Smatthias.ringwald             connection->state = ACCEPTED_CONNECTION_REQUEST;
134034d2123cS[email protected]             hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
1341dbe1a790S[email protected]             return;
1342c7e0c5f6Smatthias.ringwald         }
1343c7e0c5f6Smatthias.ringwald 
134432ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
134534d2123cS[email protected]             log_info("responding to link key request\n");
134634d2123cS[email protected]             connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST);
134732ab9390Smatthias.ringwald             link_key_t link_key;
1348c77e8838S[email protected]             link_key_type_t link_key_type;
13493a9fb326S[email protected]             if ( hci_stack->remote_device_db
13503a9fb326S[email protected]               && hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)
135134d2123cS[email protected]               && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){
13529ab95c90S[email protected]                connection->link_key_type = link_key_type;
135332ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
135432ab9390Smatthias.ringwald             } else {
135532ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
135632ab9390Smatthias.ringwald             }
1357dbe1a790S[email protected]             return;
135832ab9390Smatthias.ringwald         }
13591d6b20aeS[email protected] 
1360899283eaS[email protected]         if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){
13614c57c146S[email protected]             log_info("denying to pin request\n");
1362899283eaS[email protected]             connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST);
136334d2123cS[email protected]             hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address);
13644c57c146S[email protected]             return;
13654c57c146S[email protected]         }
13664c57c146S[email protected] 
1367dbe1a790S[email protected]         if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){
136834d2123cS[email protected]             connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY);
136982d8f825S[email protected]             log_info("IO Capability Request received, stack bondable %u, io cap %u", hci_stack->bondable, hci_stack->ssp_io_capability);
137082d8f825S[email protected]             if (hci_stack->bondable && (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN)){
1371106d6d11S[email protected]                 // tweak authentication requirements
13723a9fb326S[email protected]                 uint8_t authreq = hci_stack->ssp_authentication_requirement;
1373106d6d11S[email protected]                 if (connection->bonding_flags & BONDING_DEDICATED){
13749faad3abS[email protected]                     authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING;
13759faad3abS[email protected]                 }
13769faad3abS[email protected]                 if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){
13779faad3abS[email protected]                     authreq |= 1;
1378106d6d11S[email protected]                 }
13793a9fb326S[email protected]                 hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, NULL, authreq);
1380f8fb5f6eS[email protected]             } else {
1381f8fb5f6eS[email protected]                 hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED);
1382f8fb5f6eS[email protected]             }
1383dbe1a790S[email protected]             return;
138432ab9390Smatthias.ringwald         }
138532ab9390Smatthias.ringwald 
1386dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){
1387dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY);
138834d2123cS[email protected]             hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address);
1389dbe1a790S[email protected]             return;
1390dbe1a790S[email protected]         }
1391dbe1a790S[email protected] 
1392dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){
1393dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY);
139434d2123cS[email protected]             hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000);
1395dbe1a790S[email protected]             return;
1396dbe1a790S[email protected]         }
1397afd4e962S[email protected] 
1398afd4e962S[email protected]         if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){
1399afd4e962S[email protected]             connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES;
140034d2123cS[email protected]             hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle);
14012bd8b7e7S[email protected]             return;
14022bd8b7e7S[email protected]         }
14032bd8b7e7S[email protected] 
14042bd8b7e7S[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){
14052bd8b7e7S[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK;
140634d2123cS[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005);  // authentication failure
140734d2123cS[email protected]             return;
140834d2123cS[email protected]         }
1409ad83dc6aS[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){
1410ad83dc6aS[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE;
141152d34f98S[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // authentication done
1412ad83dc6aS[email protected]             return;
1413ad83dc6aS[email protected]         }
141434d2123cS[email protected]         if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){
141534d2123cS[email protected]             connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST;
141634d2123cS[email protected]             hci_send_cmd(&hci_authentication_requested, connection->con_handle);
14172bd8b7e7S[email protected]             return;
1418afd4e962S[email protected]         }
1419dce78009S[email protected]         if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){
1420dce78009S[email protected]             connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST;
1421dce78009S[email protected]             hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1);
1422dce78009S[email protected]             return;
1423dce78009S[email protected]         }
1424dbe1a790S[email protected]     }
1425c7e0c5f6Smatthias.ringwald 
14263a9fb326S[email protected]     switch (hci_stack->state){
14273429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
14283a9fb326S[email protected]             // log_info("hci_init: substate %u\n", hci_stack->substate);
14293a9fb326S[email protected]             if (hci_stack->substate % 2) {
14303429f56bSmatthias.ringwald                 // odd: waiting for command completion
143106b35ec0Smatthias.ringwald                 return;
14323429f56bSmatthias.ringwald             }
14333a9fb326S[email protected]             switch (hci_stack->substate >> 1){
1434c7492964Smatthias.ringwald                 case 0: // RESET
143522909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
143624052c2aS[email protected] 
14373a9fb326S[email protected]                     if (hci_stack->config == 0 || ((hci_uart_config_t *)hci_stack->config)->baudrate_main == 0){
1438c7492964Smatthias.ringwald                         // skip baud change
14393a9fb326S[email protected]                         hci_stack->substate = 4; // >> 1 = 2
1440c7492964Smatthias.ringwald                     }
14413429f56bSmatthias.ringwald                     break;
1442c7492964Smatthias.ringwald                 case 1: // SEND BAUD CHANGE
14433a9fb326S[email protected]                     hci_stack->control->baudrate_cmd(hci_stack->config, ((hci_uart_config_t *)hci_stack->config)->baudrate_main, hci_stack->hci_packet_buffer);
14443a9fb326S[email protected]                     hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]);
1445c7492964Smatthias.ringwald                     break;
1446c7492964Smatthias.ringwald                 case 2: // LOCAL BAUD CHANGE
14473a9fb326S[email protected]                     hci_stack->hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack->config)->baudrate_main);
14483a9fb326S[email protected]                     hci_stack->substate += 2;
1449c7492964Smatthias.ringwald                     // break missing here for fall through
1450c7492964Smatthias.ringwald 
1451c7492964Smatthias.ringwald                 case 3:
145224052c2aS[email protected]                     // Custom initialization
14533a9fb326S[email protected]                     if (hci_stack->control && hci_stack->control->next_cmd){
14543a9fb326S[email protected]                         int valid_cmd = (*hci_stack->control->next_cmd)(hci_stack->config, hci_stack->hci_packet_buffer);
1455d62aca9fSmatthias.ringwald                         if (valid_cmd){
14563a9fb326S[email protected]                             int size = 3 + hci_stack->hci_packet_buffer[2];
14573a9fb326S[email protected]                             hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size);
14583a9fb326S[email protected]                             hci_stack->substate = 4; // more init commands
145990919203Smatthias.ringwald                             break;
146090919203Smatthias.ringwald                         }
1461d62aca9fSmatthias.ringwald                         log_info("hci_run: init script done\n\r");
146290919203Smatthias.ringwald                     }
14632f6c30e1Smatthias.ringwald                     // otherwise continue
1464f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
1465f432a6ddSmatthias.ringwald 					break;
1466c7492964Smatthias.ringwald 				case 4:
1467e2edc0c3Smatthias.ringwald 					hci_send_cmd(&hci_read_buffer_size);
1468e2edc0c3Smatthias.ringwald 					break;
1469c7492964Smatthias.ringwald                 case 5:
147065389bfcS[email protected]                     hci_send_cmd(&hci_read_local_supported_features);
14713429f56bSmatthias.ringwald                     break;
1472c7492964Smatthias.ringwald                 case 6:
1473d7e0e3a8S[email protected]                     if (hci_le_supported()){
1474d7e0e3a8S[email protected]                         hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF);
1475d7e0e3a8S[email protected]                     } else {
1476d7e0e3a8S[email protected]                         // Kensington Bluetoot 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff...
1477d7e0e3a8S[email protected]                         hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF);
1478d7e0e3a8S[email protected]                     }
1479f5d8d141S[email protected] 
1480f5d8d141S[email protected]                     // skip Classic init commands for LE only chipsets
148124052c2aS[email protected]                     if (!hci_classic_supported()){
148224052c2aS[email protected]                         if (hci_le_supported()){
14833a9fb326S[email protected]                             hci_stack->substate = 11 << 1;    // skip all classic command
148424052c2aS[email protected]                         } else {
148524052c2aS[email protected]                             log_error("Neither BR/EDR nor LE supported");
14863a9fb326S[email protected]                             hci_stack->substate = 13 << 1;    // skip all
148724052c2aS[email protected]                         }
1488f5d8d141S[email protected]                     }
1489f5d8d141S[email protected]                     break;
1490f5d8d141S[email protected]                 case 7:
149124052c2aS[email protected]                     if (hci_ssp_supported()){
14923a9fb326S[email protected]                         hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable);
1493f5d8d141S[email protected]                         break;
149424052c2aS[email protected]                     }
14953a9fb326S[email protected]                     hci_stack->substate += 2;
149624052c2aS[email protected]                     // break missing here for fall through
149724052c2aS[email protected] 
1498f5d8d141S[email protected]                 case 8:
149965389bfcS[email protected]                     // ca. 15 sec
150065389bfcS[email protected]                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
1501559e517eS[email protected]                     break;
1502f5d8d141S[email protected]                 case 9:
15033a9fb326S[email protected]                     hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device);
1504e2386ba1S[email protected]                     break;
1505f5d8d141S[email protected]                 case 10:
15063a9fb326S[email protected]                     if (hci_stack->local_name){
15073a9fb326S[email protected]                         hci_send_cmd(&hci_write_local_name, hci_stack->local_name);
1508e2386ba1S[email protected]                     } else {
15098a485f27Smatthias.ringwald                         char hostname[30];
1510e2386ba1S[email protected] #ifdef EMBEDDED
1511e2386ba1S[email protected]                         // BTstack-11:22:33:44:55:66
1512e2386ba1S[email protected]                         strcpy(hostname, "BTstack ");
15133a9fb326S[email protected]                         strcat(hostname, bd_addr_to_str(hci_stack->local_bd_addr));
1514e2386ba1S[email protected]                         printf("---> Name %s\n", hostname);
1515e2386ba1S[email protected] #else
1516e2386ba1S[email protected]                         // hostname for POSIX systems
15178a485f27Smatthias.ringwald                         gethostname(hostname, 30);
15188a485f27Smatthias.ringwald                         hostname[29] = '\0';
1519e2386ba1S[email protected] #endif
15208a485f27Smatthias.ringwald                         hci_send_cmd(&hci_write_local_name, hostname);
15218a485f27Smatthias.ringwald                     }
15223c4d4b90Smatthias.ringwald                     break;
1523e0dbca83S[email protected]                 case 11:
15243a9fb326S[email protected] 					hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan
152524052c2aS[email protected]                     if (!hci_le_supported()){
152624052c2aS[email protected]                         // SKIP LE init for Classic only configuration
15273a9fb326S[email protected]                         hci_stack->substate = 13 << 1;
152824052c2aS[email protected]                     }
1529a45d6b9fS[email protected] 					break;
153024052c2aS[email protected] 
153143aa7007S[email protected] #ifdef HAVE_BLE
153224052c2aS[email protected]                 // LE INIT
1533a45d6b9fS[email protected]                 case 12:
153424052c2aS[email protected]                     hci_send_cmd(&hci_le_read_buffer_size);
153524052c2aS[email protected]                     break;
153624052c2aS[email protected]                 case 13:
153724052c2aS[email protected]                     // LE Supported Host = 1, Simultaneous Host = 0
153824052c2aS[email protected]                     hci_send_cmd(&hci_write_le_host_supported, 1, 0);
153924052c2aS[email protected]                     break;
154043aa7007S[email protected] #endif
154124052c2aS[email protected] 
154224052c2aS[email protected]                 // DONE
154324052c2aS[email protected]                 case 14:
15443429f56bSmatthias.ringwald                     // done.
15453a9fb326S[email protected]                     hci_stack->state = HCI_STATE_WORKING;
1546b360b6adSmatthias.ringwald                     hci_emit_state();
15473429f56bSmatthias.ringwald                     break;
15483429f56bSmatthias.ringwald                 default:
15493429f56bSmatthias.ringwald                     break;
1550475c8125Smatthias.ringwald             }
15513a9fb326S[email protected]             hci_stack->substate++;
15523429f56bSmatthias.ringwald             break;
1553c7e0c5f6Smatthias.ringwald 
1554c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
1555c7e0c5f6Smatthias.ringwald 
15567b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING\n");
1557c7e0c5f6Smatthias.ringwald             // close all open connections
15583a9fb326S[email protected]             connection =  (hci_connection_t *) hci_stack->connections;
1559c7e0c5f6Smatthias.ringwald             if (connection){
156032ab9390Smatthias.ringwald 
1561c7e0c5f6Smatthias.ringwald                 // send disconnect
156264f0b431S[email protected]                 if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) return;
156332ab9390Smatthias.ringwald 
156479bbfa0bSmatthias.ringwald                 log_info("HCI_STATE_HALTING, 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
1566c7e0c5f6Smatthias.ringwald 
1567c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
1568c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
1569c7e0c5f6Smatthias.ringwald                 return;
1570c7e0c5f6Smatthias.ringwald             }
15717b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, calling off\n");
1572c7e0c5f6Smatthias.ringwald 
157372ea5239Smatthias.ringwald             // switch mode
1574c7e0c5f6Smatthias.ringwald             hci_power_control_off();
15759418f9c9Smatthias.ringwald 
15767b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, emitting state\n");
157772ea5239Smatthias.ringwald             hci_emit_state();
15787b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, done\n");
157972ea5239Smatthias.ringwald             break;
1580c7e0c5f6Smatthias.ringwald 
158172ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
15823a9fb326S[email protected]             switch(hci_stack->substate) {
158389db417bSmatthias.ringwald                 case 0:
15847b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_FALLING_ASLEEP\n");
158572ea5239Smatthias.ringwald                     // close all open connections
15863a9fb326S[email protected]                     connection =  (hci_connection_t *) hci_stack->connections;
158766da7044Smatthias.ringwald 
158828171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
158966da7044Smatthias.ringwald                     // don't close connections, if H4 supports power management
159066da7044Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
159166da7044Smatthias.ringwald                         connection = NULL;
159266da7044Smatthias.ringwald                     }
159366da7044Smatthias.ringwald #endif
159472ea5239Smatthias.ringwald                     if (connection){
159532ab9390Smatthias.ringwald 
159672ea5239Smatthias.ringwald                         // send disconnect
159732ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
159832ab9390Smatthias.ringwald 
159979bbfa0bSmatthias.ringwald                         log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
16006ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
160172ea5239Smatthias.ringwald 
160272ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
160372ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
160472ea5239Smatthias.ringwald                         return;
160572ea5239Smatthias.ringwald                     }
160672ea5239Smatthias.ringwald 
160792368cd3S[email protected]                     if (hci_classic_supported()){
160889db417bSmatthias.ringwald                         // disable page and inquiry scan
160964f0b431S[email protected]                         if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) return;
161032ab9390Smatthias.ringwald 
161192368cd3S[email protected]                         log_info("HCI_STATE_HALTING, disabling inq scans\n");
16123a9fb326S[email protected]                         hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan
161389db417bSmatthias.ringwald 
161489db417bSmatthias.ringwald                         // continue in next sub state
16153a9fb326S[email protected]                         hci_stack->substate++;
161689db417bSmatthias.ringwald                         break;
161792368cd3S[email protected]                     }
161892368cd3S[email protected]                     // fall through for ble-only chips
161992368cd3S[email protected] 
162089db417bSmatthias.ringwald                 case 2:
16217b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, calling sleep\n");
162228171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
162328171530Smatthias.ringwald                     // don't actually go to sleep, if H4 supports power management
162428171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
162528171530Smatthias.ringwald                         // SLEEP MODE reached
16263a9fb326S[email protected]                         hci_stack->state = HCI_STATE_SLEEPING;
162728171530Smatthias.ringwald                         hci_emit_state();
162828171530Smatthias.ringwald                         break;
162928171530Smatthias.ringwald                     }
163028171530Smatthias.ringwald #endif
163172ea5239Smatthias.ringwald                     // switch mode
16323a9fb326S[email protected]                     hci_power_control_sleep();  // changes hci_stack->state to SLEEP
1633c7e0c5f6Smatthias.ringwald                     hci_emit_state();
163428171530Smatthias.ringwald                     break;
163528171530Smatthias.ringwald 
163689db417bSmatthias.ringwald                 default:
163789db417bSmatthias.ringwald                     break;
163889db417bSmatthias.ringwald             }
1639c7e0c5f6Smatthias.ringwald             break;
1640c7e0c5f6Smatthias.ringwald 
16413429f56bSmatthias.ringwald         default:
16423429f56bSmatthias.ringwald             break;
16431f504dbdSmatthias.ringwald     }
16443429f56bSmatthias.ringwald }
164516833f0aSmatthias.ringwald 
164631452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1647c8e4258aSmatthias.ringwald     bd_addr_t addr;
1648c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1649c8e4258aSmatthias.ringwald     // house-keeping
1650c8e4258aSmatthias.ringwald 
1651c8e4258aSmatthias.ringwald     // create_connection?
1652c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1653c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
1654339b6768Smatthias.ringwald         log_info("Create_connection to %s\n", bd_addr_to_str(addr));
1655c8e4258aSmatthias.ringwald 
1656*96a45072S[email protected]         conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
1657ad83dc6aS[email protected]         if (!conn){
1658*96a45072S[email protected]             conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC);
165917f1ba2aSmatthias.ringwald             if (!conn){
166017f1ba2aSmatthias.ringwald                 // notify client that alloc failed
166117f1ba2aSmatthias.ringwald                 hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
166217f1ba2aSmatthias.ringwald                 return 0; // don't sent packet to controller
166317f1ba2aSmatthias.ringwald             }
1664ad83dc6aS[email protected]             conn->state = SEND_CREATE_CONNECTION;
1665ad83dc6aS[email protected]         }
1666ad83dc6aS[email protected]         log_info("conn state %u", conn->state);
1667ad83dc6aS[email protected]         switch (conn->state){
1668ad83dc6aS[email protected]             // if connection active exists
1669ad83dc6aS[email protected]             case OPEN:
167062bda3fbS[email protected]                 // and OPEN, emit connection complete command, don't send to controller
1671ad83dc6aS[email protected]                 hci_emit_connection_complete(conn, 0);
167262bda3fbS[email protected]                 return 0;
1673ad83dc6aS[email protected]             case SEND_CREATE_CONNECTION:
1674ad83dc6aS[email protected]                 // connection created by hci, e.g. dedicated bonding
1675ad83dc6aS[email protected]                 break;
1676ad83dc6aS[email protected]             default:
1677ad83dc6aS[email protected]                 // otherwise, just ignore as it is already in the open process
1678ad83dc6aS[email protected]                 return 0;
1679ad83dc6aS[email protected]         }
1680c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
1681c8e4258aSmatthias.ringwald     }
1682c8e4258aSmatthias.ringwald 
16837fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
16847fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
16857fde4af9Smatthias.ringwald     }
16867fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
16877fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
16887fde4af9Smatthias.ringwald     }
16897fde4af9Smatthias.ringwald 
16908ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
16913a9fb326S[email protected]         if (hci_stack->remote_device_db){
16928ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
16933a9fb326S[email protected]             hci_stack->remote_device_db->delete_link_key(&addr);
16948ef73945Smatthias.ringwald         }
16958ef73945Smatthias.ringwald     }
1696c8e4258aSmatthias.ringwald 
16976724cd9eS[email protected]     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)
16986724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_pin_code_request_reply)){
16996724cd9eS[email protected]         bt_flip_addr(addr, &packet[3]);
1700*96a45072S[email protected]         conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
17016724cd9eS[email protected]         if (conn){
17026724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE);
17036724cd9eS[email protected]         }
17046724cd9eS[email protected]     }
17056724cd9eS[email protected] 
17066724cd9eS[email protected]     if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply)
17076724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_confirmation_request_reply)
17086724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_negative_reply)
17096724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_reply)) {
17106724cd9eS[email protected]         bt_flip_addr(addr, &packet[3]);
1711*96a45072S[email protected]         conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC);
17126724cd9eS[email protected]         if (conn){
17136724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE);
17146724cd9eS[email protected]         }
17156724cd9eS[email protected]     }
17166724cd9eS[email protected] 
171769a97523S[email protected] #ifdef HAVE_BLE
171869a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_advertising_parameters)){
17193a9fb326S[email protected]         hci_stack->adv_addr_type = packet[8];
172069a97523S[email protected]     }
172169a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_random_address)){
17223a9fb326S[email protected]         bt_flip_addr(hci_stack->adv_address, &packet[3]);
172369a97523S[email protected]     }
172469a97523S[email protected] #endif
172569a97523S[email protected] 
17263a9fb326S[email protected]     hci_stack->num_cmd_packets--;
17276b4af23dS[email protected]     int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
17286b4af23dS[email protected] 
17296b4af23dS[email protected]     // free packet buffer for synchronous transport implementations
1730c8b9416aS[email protected]     if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){
17316b4af23dS[email protected]         hci_stack->hci_packet_buffer_reserved = 0;
17326b4af23dS[email protected]     }
17336b4af23dS[email protected] 
17346b4af23dS[email protected]     return err;
173531452debSmatthias.ringwald }
17368adf0ddaSmatthias.ringwald 
17372bd8b7e7S[email protected] // disconnect because of security block
17382bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){
17392bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
17402bd8b7e7S[email protected]     if (!connection) return;
17412bd8b7e7S[email protected]     connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK;
17422bd8b7e7S[email protected] }
17432bd8b7e7S[email protected] 
17442bd8b7e7S[email protected] 
1745dbe1a790S[email protected] // Configure Secure Simple Pairing
1746dbe1a790S[email protected] 
1747dbe1a790S[email protected] // enable will enable SSP during init
1748dbe1a790S[email protected] void hci_ssp_set_enable(int enable){
17493a9fb326S[email protected]     hci_stack->ssp_enable = enable;
1750dbe1a790S[email protected] }
1751dbe1a790S[email protected] 
17522bd8b7e7S[email protected] int hci_local_ssp_activated(){
17533a9fb326S[email protected]     return hci_ssp_supported() && hci_stack->ssp_enable;
17542bd8b7e7S[email protected] }
17552bd8b7e7S[email protected] 
1756dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement
1757dbe1a790S[email protected] void hci_ssp_set_io_capability(int io_capability){
17583a9fb326S[email protected]     hci_stack->ssp_io_capability = io_capability;
1759dbe1a790S[email protected] }
1760dbe1a790S[email protected] void hci_ssp_set_authentication_requirement(int authentication_requirement){
17613a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = authentication_requirement;
1762dbe1a790S[email protected] }
1763dbe1a790S[email protected] 
1764dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested
1765dbe1a790S[email protected] void hci_ssp_set_auto_accept(int auto_accept){
17663a9fb326S[email protected]     hci_stack->ssp_auto_accept = auto_accept;
1767dbe1a790S[email protected] }
1768dbe1a790S[email protected] 
17691cd208adSmatthias.ringwald /**
17701cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
17711cd208adSmatthias.ringwald  */
1772fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
17731cd208adSmatthias.ringwald     va_list argptr;
17741cd208adSmatthias.ringwald     va_start(argptr, cmd);
17753a9fb326S[email protected]     uint16_t size = hci_create_cmd_internal(hci_stack->hci_packet_buffer, cmd, argptr);
17761cd208adSmatthias.ringwald     va_end(argptr);
17773a9fb326S[email protected]     return hci_send_cmd_packet(hci_stack->hci_packet_buffer, size);
177893b8dc03Smatthias.ringwald }
1779c8e4258aSmatthias.ringwald 
1780ee091cf1Smatthias.ringwald // Create various non-HCI events.
1781ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
1782ee091cf1Smatthias.ringwald 
1783c8e4258aSmatthias.ringwald void hci_emit_state(){
17843a9fb326S[email protected]     log_info("BTSTACK_EVENT_STATE %u", hci_stack->state);
1785425d1371Smatthias.ringwald     uint8_t event[3];
178680d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
1787425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
17883a9fb326S[email protected]     event[2] = hci_stack->state;
1789425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
17903a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1791c8e4258aSmatthias.ringwald }
1792c8e4258aSmatthias.ringwald 
179317f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){
1794425d1371Smatthias.ringwald     uint8_t event[13];
1795c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
1796425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
179717f1ba2aSmatthias.ringwald     event[2] = status;
1798c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
1799c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
1800c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
1801c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
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));
1804c8e4258aSmatthias.ringwald }
1805c8e4258aSmatthias.ringwald 
18063c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
1807425d1371Smatthias.ringwald     uint8_t event[6];
18083c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
1809e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
18103c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
18113c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
18123c4d4b90Smatthias.ringwald     event[5] = reason;
1813425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18143a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
18153c4d4b90Smatthias.ringwald }
18163c4d4b90Smatthias.ringwald 
1817ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
181866fb9560S[email protected]     if (disable_l2cap_timeouts) return;
1819e0abb8e7S[email protected]     log_info("L2CAP_EVENT_TIMEOUT_CHECK");
1820425d1371Smatthias.ringwald     uint8_t event[4];
182180d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
1822425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1823ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
1824425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18253a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1826ee091cf1Smatthias.ringwald }
182743bfb1bdSmatthias.ringwald 
182843bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
1829e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections());
1830425d1371Smatthias.ringwald     uint8_t event[3];
183180d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
1832425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
183343bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
1834425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18353a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
183643bfb1bdSmatthias.ringwald }
1837038bc64cSmatthias.ringwald 
1838038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
1839e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_POWERON_FAILED");
1840425d1371Smatthias.ringwald     uint8_t event[2];
184180d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
1842425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1843425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18443a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1845038bc64cSmatthias.ringwald }
18461b0e3922Smatthias.ringwald 
184709ba8edeSmatthias.ringwald #ifndef EMBEDDED
18481b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
1849e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR);
1850425d1371Smatthias.ringwald     uint8_t event[6];
18511b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
1852425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1853425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
1854425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
1855425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
1856425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18573a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
18581b0e3922Smatthias.ringwald }
185909ba8edeSmatthias.ringwald #endif
18601b0e3922Smatthias.ringwald 
18612ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
1862e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled);
1863425d1371Smatthias.ringwald     uint8_t event[3];
18642ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
1865425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
18662ed6235cSmatthias.ringwald     event[2] = enabled;
1867425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18683a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
18692ed6235cSmatthias.ringwald }
1870627c2f45Smatthias.ringwald 
1871627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
1872e0abb8e7S[email protected]     uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info
1873627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
1874e0abb8e7S[email protected]     event[1] = sizeof(event) - 2 - 1;
1875f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
18762f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
1877f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
1878e0abb8e7S[email protected] 
1879e0abb8e7S[email protected]     event[9+248] = 0;   // assert \0 for log_info
1880e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]);
1881e0abb8e7S[email protected] 
1882e0abb8e7S[email protected]     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1);
18833a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1);
1884627c2f45Smatthias.ringwald }
1885381fbed8Smatthias.ringwald 
1886381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
1887e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled);
1888425d1371Smatthias.ringwald     uint8_t event[3];
1889381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
1890425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1891381fbed8Smatthias.ringwald     event[2] = enabled;
1892425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18933a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1894381fbed8Smatthias.ringwald }
1895458bf4e8S[email protected] 
1896a00031e2S[email protected] void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){
1897df3354fcS[email protected]     log_info("hci_emit_security_level %u for handle %x", level, con_handle);
1898a00031e2S[email protected]     uint8_t event[5];
1899e00caf9cS[email protected]     int pos = 0;
1900a00031e2S[email protected]     event[pos++] = GAP_SECURITY_LEVEL;
1901e00caf9cS[email protected]     event[pos++] = sizeof(event) - 2;
1902a00031e2S[email protected]     bt_store_16(event, 2, con_handle);
1903e00caf9cS[email protected]     pos += 2;
1904e00caf9cS[email protected]     event[pos++] = level;
1905e00caf9cS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
19063a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1907e00caf9cS[email protected] }
1908e00caf9cS[email protected] 
1909ad83dc6aS[email protected] void hci_emit_dedicated_bonding_result(hci_connection_t * connection, uint8_t status){
1910ad83dc6aS[email protected]     log_info("hci_emit_dedicated_bonding_result %u ", status);
1911ad83dc6aS[email protected]     uint8_t event[9];
1912ad83dc6aS[email protected]     int pos = 0;
1913ad83dc6aS[email protected]     event[pos++] = GAP_DEDICATED_BONDING_COMPLETED;
1914ad83dc6aS[email protected]     event[pos++] = sizeof(event) - 2;
1915ad83dc6aS[email protected]     event[pos++] = status;
1916ad83dc6aS[email protected]     bt_flip_addr( * (bd_addr_t *) &event[pos], connection->address);
1917ad83dc6aS[email protected]     pos += 6;
1918ad83dc6aS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
19193a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1920ad83dc6aS[email protected] }
1921ad83dc6aS[email protected] 
19222bd8b7e7S[email protected] // query if remote side supports SSP
19232bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){
19242bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
19252bd8b7e7S[email protected]     if (!connection) return 0;
19262bd8b7e7S[email protected]     return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0;
19272bd8b7e7S[email protected] }
19282bd8b7e7S[email protected] 
1929df3354fcS[email protected] int hci_ssp_supported_on_both_sides(hci_con_handle_t handle){
1930df3354fcS[email protected]     return hci_local_ssp_activated() && hci_remote_ssp_supported(handle);
1931df3354fcS[email protected] }
1932df3354fcS[email protected] 
1933458bf4e8S[email protected] // GAP API
1934458bf4e8S[email protected] /**
1935458bf4e8S[email protected]  * @bbrief enable/disable bonding. default is enabled
1936458bf4e8S[email protected]  * @praram enabled
1937458bf4e8S[email protected]  */
19384c57c146S[email protected] void gap_set_bondable_mode(int enable){
19393a9fb326S[email protected]     hci_stack->bondable = enable ? 1 : 0;
1940458bf4e8S[email protected] }
1941cb230b9dS[email protected] 
1942cb230b9dS[email protected] /**
194334d2123cS[email protected]  * @brief map link keys to security levels
1944cb230b9dS[email protected]  */
194534d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){
194634d2123cS[email protected]     switch (link_key_type){
19473c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256:
19483c68dfa9S[email protected]             return LEVEL_4;
19493c68dfa9S[email protected]         case COMBINATION_KEY:
19503c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192:
19513c68dfa9S[email protected]             return LEVEL_3;
19523c68dfa9S[email protected]         default:
19533c68dfa9S[email protected]             return LEVEL_2;
19543c68dfa9S[email protected]     }
1955cb230b9dS[email protected] }
1956cb230b9dS[email protected] 
1957a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){
195834d2123cS[email protected]     if (!connection) return LEVEL_0;
195934d2123cS[email protected]     if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0;
196034d2123cS[email protected]     return gap_security_level_for_link_key_type(connection->link_key_type);
196134d2123cS[email protected] }
196234d2123cS[email protected] 
196334d2123cS[email protected] 
1964106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){
196582d8f825S[email protected]     printf("gap_mitm_protection_required_for_security_level %u\n", level);
1966106d6d11S[email protected]     return level > LEVEL_2;
1967106d6d11S[email protected] }
1968106d6d11S[email protected] 
196934d2123cS[email protected] /**
197034d2123cS[email protected]  * @brief get current security level
197134d2123cS[email protected]  */
197234d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){
197334d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
197434d2123cS[email protected]     if (!connection) return LEVEL_0;
197534d2123cS[email protected]     return gap_security_level_for_connection(connection);
197634d2123cS[email protected] }
197734d2123cS[email protected] 
1978cb230b9dS[email protected] /**
1979cb230b9dS[email protected]  * @brief request connection to device to
1980cb230b9dS[email protected]  * @result GAP_AUTHENTICATION_RESULT
1981cb230b9dS[email protected]  */
198234d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){
198334d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
198434d2123cS[email protected]     if (!connection){
1985a00031e2S[email protected]         hci_emit_security_level(con_handle, LEVEL_0);
198634d2123cS[email protected]         return;
198734d2123cS[email protected]     }
198834d2123cS[email protected]     gap_security_level_t current_level = gap_security_level(con_handle);
198934d2123cS[email protected]     log_info("gap_request_security_level %u, current level %u", requested_level, current_level);
199034d2123cS[email protected]     if (current_level >= requested_level){
1991a00031e2S[email protected]         hci_emit_security_level(con_handle, current_level);
199234d2123cS[email protected]         return;
199334d2123cS[email protected]     }
1994a00031e2S[email protected] 
199534d2123cS[email protected]     connection->requested_security_level = requested_level;
1996a00031e2S[email protected] 
1997fb8ba0dbS[email protected]     // would enabling ecnryption suffice (>= LEVEL_2)?
19983a9fb326S[email protected]     if (hci_stack->remote_device_db){
1999a00031e2S[email protected]         link_key_type_t link_key_type;
2000a00031e2S[email protected]         link_key_t      link_key;
20013a9fb326S[email protected]         if (hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)){
2002a00031e2S[email protected]             if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){
2003a00031e2S[email protected]                 connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
2004a00031e2S[email protected]                 return;
2005a00031e2S[email protected]             }
2006a00031e2S[email protected]         }
2007a00031e2S[email protected]     }
2008a00031e2S[email protected] 
20091eb2563eS[email protected]     // try to authenticate connection
20101eb2563eS[email protected]     connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
2011e80b2cf9S[email protected]     hci_run();
2012e00caf9cS[email protected] }
2013ad83dc6aS[email protected] 
2014ad83dc6aS[email protected] /**
2015ad83dc6aS[email protected]  * @brief start dedicated bonding with device. disconnect after bonding
2016ad83dc6aS[email protected]  * @param device
2017ad83dc6aS[email protected]  * @param request MITM protection
2018ad83dc6aS[email protected]  * @result GAP_DEDICATED_BONDING_COMPLETE
2019ad83dc6aS[email protected]  */
2020ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){
2021ad83dc6aS[email protected] 
2022ad83dc6aS[email protected]     // create connection state machine
2023*96a45072S[email protected]     hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_CLASSIC);
2024ad83dc6aS[email protected] 
2025ad83dc6aS[email protected]     if (!connection){
2026ad83dc6aS[email protected]         return BTSTACK_MEMORY_ALLOC_FAILED;
2027ad83dc6aS[email protected]     }
2028ad83dc6aS[email protected] 
2029ad83dc6aS[email protected]     // delete linkn key
2030ad83dc6aS[email protected]     hci_drop_link_key_for_bd_addr( (bd_addr_t *) &device);
2031ad83dc6aS[email protected] 
2032ad83dc6aS[email protected]     // configure LEVEL_2/3, dedicated bonding
2033ad83dc6aS[email protected]     connection->state = SEND_CREATE_CONNECTION;
2034ad83dc6aS[email protected]     connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2;
203582d8f825S[email protected]     printf("gap_dedicated_bonding, mitm %u -> level %u\n", mitm_protection_required, connection->requested_security_level);
2036ad83dc6aS[email protected]     connection->bonding_flags = BONDING_DEDICATED;
2037ad83dc6aS[email protected] 
2038ad83dc6aS[email protected]     // wait for GAP Security Result and send GAP Dedicated Bonding complete
2039ad83dc6aS[email protected] 
2040ad83dc6aS[email protected]     // handle: connnection failure (connection complete != ok)
2041ad83dc6aS[email protected]     // handle: authentication failure
2042ad83dc6aS[email protected]     // handle: disconnect on done
2043ad83dc6aS[email protected] 
2044ad83dc6aS[email protected]     hci_run();
2045ad83dc6aS[email protected] 
2046ad83dc6aS[email protected]     return 0;
2047ad83dc6aS[email protected] }
20488e618f72S[email protected] 
20498e618f72S[email protected] void gap_set_local_name(const char * local_name){
20508e618f72S[email protected]     hci_stack->local_name = local_name;
20518e618f72S[email protected] }
20528e618f72S[email protected] 
20538e618f72S[email protected] 
2054