xref: /btstack/src/hci.c (revision 68a0fcf7e245a4ad0824398b64b876fd4233fc06)
11f504dbdSmatthias.ringwald /*
26b64433eSmatthias.ringwald  * Copyright (C) 2009-2012 by Matthias Ringwald
31713bceaSmatthias.ringwald  *
41713bceaSmatthias.ringwald  * Redistribution and use in source and binary forms, with or without
51713bceaSmatthias.ringwald  * modification, are permitted provided that the following conditions
61713bceaSmatthias.ringwald  * are met:
71713bceaSmatthias.ringwald  *
81713bceaSmatthias.ringwald  * 1. Redistributions of source code must retain the above copyright
91713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer.
101713bceaSmatthias.ringwald  * 2. Redistributions in binary form must reproduce the above copyright
111713bceaSmatthias.ringwald  *    notice, this list of conditions and the following disclaimer in the
121713bceaSmatthias.ringwald  *    documentation and/or other materials provided with the distribution.
131713bceaSmatthias.ringwald  * 3. Neither the name of the copyright holders nor the names of
141713bceaSmatthias.ringwald  *    contributors may be used to endorse or promote products derived
151713bceaSmatthias.ringwald  *    from this software without specific prior written permission.
166b64433eSmatthias.ringwald  * 4. Any redistribution, use, or modification is done solely for
176b64433eSmatthias.ringwald  *    personal benefit and not for any commercial purpose or for
186b64433eSmatthias.ringwald  *    monetary gain.
191713bceaSmatthias.ringwald  *
201713bceaSmatthias.ringwald  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
211713bceaSmatthias.ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
221713bceaSmatthias.ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
231713bceaSmatthias.ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
241713bceaSmatthias.ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
251713bceaSmatthias.ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
261713bceaSmatthias.ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
271713bceaSmatthias.ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
281713bceaSmatthias.ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
291713bceaSmatthias.ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
301713bceaSmatthias.ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311713bceaSmatthias.ringwald  * SUCH DAMAGE.
321713bceaSmatthias.ringwald  *
336b64433eSmatthias.ringwald  * Please inquire about commercial licensing options at [email protected]
346b64433eSmatthias.ringwald  *
351713bceaSmatthias.ringwald  */
361713bceaSmatthias.ringwald 
371713bceaSmatthias.ringwald /*
381f504dbdSmatthias.ringwald  *  hci.c
391f504dbdSmatthias.ringwald  *
401f504dbdSmatthias.ringwald  *  Created by Matthias Ringwald on 4/29/09.
411f504dbdSmatthias.ringwald  *
421f504dbdSmatthias.ringwald  */
431f504dbdSmatthias.ringwald 
44bde315ceS[email protected] #include "btstack-config.h"
4528171530Smatthias.ringwald 
467f2435e6Smatthias.ringwald #include "hci.h"
474c57c146S[email protected] #include "gap.h"
487f2435e6Smatthias.ringwald 
4993b8dc03Smatthias.ringwald #include <stdarg.h>
5093b8dc03Smatthias.ringwald #include <string.h>
5156fe0872Smatthias.ringwald #include <stdio.h>
527f2435e6Smatthias.ringwald 
53549e6ebeSmatthias.ringwald #ifndef EMBEDDED
54549e6ebeSmatthias.ringwald #include <unistd.h> // gethostbyname
5509ba8edeSmatthias.ringwald #include <btstack/version.h>
56549e6ebeSmatthias.ringwald #endif
57549e6ebeSmatthias.ringwald 
58a3b02b71Smatthias.ringwald #include "btstack_memory.h"
597f2435e6Smatthias.ringwald #include "debug.h"
60d8905019Smatthias.ringwald #include "hci_dump.h"
6193b8dc03Smatthias.ringwald 
62ae1fd9f3Smatthias.ringwald #include <btstack/hci_cmds.h>
631b0e3922Smatthias.ringwald 
64169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000
65ee091cf1Smatthias.ringwald 
66a45d6b9fS[email protected] #define HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP 11
67da5275c5S[email protected] 
6828171530Smatthias.ringwald #ifdef USE_BLUETOOL
6928171530Smatthias.ringwald #include "bt_control_iphone.h"
7028171530Smatthias.ringwald #endif
7128171530Smatthias.ringwald 
72758b46ceSmatthias.ringwald static void hci_update_scan_enable(void);
73a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection);
74758b46ceSmatthias.ringwald 
7506b35ec0Smatthias.ringwald // the STACK is here
763a9fb326S[email protected] #ifndef HAVE_MALLOC
773a9fb326S[email protected] static hci_stack_t   hci_stack_static;
783a9fb326S[email protected] #endif
793a9fb326S[email protected] static hci_stack_t * hci_stack = NULL;
8016833f0aSmatthias.ringwald 
8166fb9560S[email protected] // test helper
8266fb9560S[email protected] static uint8_t disable_l2cap_timeouts = 0;
8366fb9560S[email protected] 
8466fb9560S[email protected] 
8597addcc5Smatthias.ringwald /**
86ee091cf1Smatthias.ringwald  * get connection for a given handle
87ee091cf1Smatthias.ringwald  *
88ee091cf1Smatthias.ringwald  * @return connection OR NULL, if not found
89ee091cf1Smatthias.ringwald  */
905061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){
91ee091cf1Smatthias.ringwald     linked_item_t *it;
923a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
93ee091cf1Smatthias.ringwald         if ( ((hci_connection_t *) it)->con_handle == con_handle){
94ee091cf1Smatthias.ringwald             return (hci_connection_t *) it;
95ee091cf1Smatthias.ringwald         }
96ee091cf1Smatthias.ringwald     }
97ee091cf1Smatthias.ringwald     return NULL;
98ee091cf1Smatthias.ringwald }
99ee091cf1Smatthias.ringwald 
10062bda3fbS[email protected] hci_connection_t * hci_connection_for_bd_addr(bd_addr_t * addr){
10162bda3fbS[email protected]     linked_item_t *it;
10262bda3fbS[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
10362bda3fbS[email protected]         hci_connection_t * connection = (hci_connection_t *) it;
10462bda3fbS[email protected]         if (memcmp(addr, connection->address, 6) == 0) {
10562bda3fbS[email protected]             return connection;
10662bda3fbS[email protected]         }
10762bda3fbS[email protected]     }
10862bda3fbS[email protected]     return NULL;
10962bda3fbS[email protected] }
11062bda3fbS[email protected] 
1112b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){
11228ca2b46S[email protected]     hci_connection_t * connection = (hci_connection_t *) linked_item_get_user(&timer->item);
113c785ef68Smatthias.ringwald #ifdef HAVE_TIME
114ee091cf1Smatthias.ringwald     struct timeval tv;
115ee091cf1Smatthias.ringwald     gettimeofday(&tv, NULL);
116c21e6239Smatthias.ringwald     if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) {
117ee091cf1Smatthias.ringwald         // connections might be timed out
118ee091cf1Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
119ee091cf1Smatthias.ringwald     }
1202b12a0b9Smatthias.ringwald #endif
121e5780900Smatthias.ringwald #ifdef HAVE_TICK
122c785ef68Smatthias.ringwald     if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){
123c785ef68Smatthias.ringwald         // connections might be timed out
124c785ef68Smatthias.ringwald         hci_emit_l2cap_check_timeout(connection);
125c785ef68Smatthias.ringwald     }
126c785ef68Smatthias.ringwald #endif
127c785ef68Smatthias.ringwald     run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS);
128c785ef68Smatthias.ringwald     run_loop_add_timer(timer);
129c785ef68Smatthias.ringwald }
130ee091cf1Smatthias.ringwald 
131ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){
132c7492964Smatthias.ringwald #ifdef HAVE_TIME
133ee091cf1Smatthias.ringwald     gettimeofday(&connection->timestamp, NULL);
134c7492964Smatthias.ringwald #endif
135e5780900Smatthias.ringwald #ifdef HAVE_TICK
136c785ef68Smatthias.ringwald     connection->timestamp = embedded_get_ticks();
137c785ef68Smatthias.ringwald #endif
138ee091cf1Smatthias.ringwald }
139ee091cf1Smatthias.ringwald 
140ee091cf1Smatthias.ringwald /**
141c8e4258aSmatthias.ringwald  * create connection for given address
142c8e4258aSmatthias.ringwald  *
14317f1ba2aSmatthias.ringwald  * @return connection OR NULL, if no memory left
144c8e4258aSmatthias.ringwald  */
145c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){
14682d8f825S[email protected] 
14782d8f825S[email protected]     printf("create_connection_for_addr %s\n", bd_addr_to_str(addr));
14828ca2b46S[email protected]     hci_connection_t * conn = (hci_connection_t *) btstack_memory_hci_connection_get();
149c8e4258aSmatthias.ringwald     if (!conn) return NULL;
150c8e4258aSmatthias.ringwald     BD_ADDR_COPY(conn->address, addr);
151c8e4258aSmatthias.ringwald     conn->con_handle = 0xffff;
1527d3b3569Smatthias.ringwald     conn->authentication_flags = AUTH_FLAGS_NONE;
153afd4e962S[email protected]     conn->bonding_flags = 0;
15434d2123cS[email protected]     conn->requested_security_level = LEVEL_0;
155ee091cf1Smatthias.ringwald     linked_item_set_user(&conn->timeout.item, conn);
156ee091cf1Smatthias.ringwald     conn->timeout.process = hci_connection_timeout_handler;
157ee091cf1Smatthias.ringwald     hci_connection_timestamp(conn);
158d55db49eSmatthias.ringwald     conn->acl_recombination_length = 0;
1597856c818Smatthias.ringwald     conn->acl_recombination_pos = 0;
16056cf178bSmatthias.ringwald     conn->num_acl_packets_sent = 0;
1613a9fb326S[email protected]     linked_list_add(&hci_stack->connections, (linked_item_t *) conn);
162c8e4258aSmatthias.ringwald     return conn;
163c8e4258aSmatthias.ringwald }
164c8e4258aSmatthias.ringwald 
165c8e4258aSmatthias.ringwald /**
16606b35ec0Smatthias.ringwald  * get connection for given address
16797addcc5Smatthias.ringwald  *
16897addcc5Smatthias.ringwald  * @return connection OR NULL, if not found
16997addcc5Smatthias.ringwald  */
170fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){
17106b35ec0Smatthias.ringwald     linked_item_t *it;
1723a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
17306b35ec0Smatthias.ringwald         if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){
17406b35ec0Smatthias.ringwald             return (hci_connection_t *) it;
17506b35ec0Smatthias.ringwald         }
17606b35ec0Smatthias.ringwald     }
17706b35ec0Smatthias.ringwald     return NULL;
17806b35ec0Smatthias.ringwald }
17906b35ec0Smatthias.ringwald 
18028ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
18128ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags);
18228ca2b46S[email protected] }
18328ca2b46S[email protected] 
18428ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){
18528ca2b46S[email protected]     conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags);
18628ca2b46S[email protected] }
18728ca2b46S[email protected] 
18828ca2b46S[email protected] 
18943bfb1bdSmatthias.ringwald /**
19080ca58a0Smatthias.ringwald  * add authentication flags and reset timer
1917fde4af9Smatthias.ringwald  */
1927fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){
1937fde4af9Smatthias.ringwald     bd_addr_t addr;
1947fde4af9Smatthias.ringwald     bt_flip_addr(addr, *(bd_addr_t *) bd_addr);
1957fde4af9Smatthias.ringwald     hci_connection_t * conn = connection_for_address(addr);
1967fde4af9Smatthias.ringwald     if (conn) {
19728ca2b46S[email protected]         connectionSetAuthenticationFlags(conn, flags);
19880ca58a0Smatthias.ringwald         hci_connection_timestamp(conn);
1997fde4af9Smatthias.ringwald     }
2007fde4af9Smatthias.ringwald }
2017fde4af9Smatthias.ringwald 
20280ca58a0Smatthias.ringwald int  hci_authentication_active_for_handle(hci_con_handle_t handle){
2035061f3afS[email protected]     hci_connection_t * conn = hci_connection_for_handle(handle);
20480ca58a0Smatthias.ringwald     if (!conn) return 0;
2056724cd9eS[email protected]     if (conn->authentication_flags & LEGACY_PAIRING_ACTIVE) return 1;
2066724cd9eS[email protected]     if (conn->authentication_flags & SSP_PAIRING_ACTIVE) return 1;
2076724cd9eS[email protected]     return 0;
20880ca58a0Smatthias.ringwald }
20980ca58a0Smatthias.ringwald 
210c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){
2113a9fb326S[email protected]     if (hci_stack->remote_device_db) {
2123a9fb326S[email protected]         hci_stack->remote_device_db->delete_link_key(addr);
213c12e46e7Smatthias.ringwald     }
214c12e46e7Smatthias.ringwald }
215c12e46e7Smatthias.ringwald 
2167fde4af9Smatthias.ringwald 
2177fde4af9Smatthias.ringwald /**
21843bfb1bdSmatthias.ringwald  * count connections
21943bfb1bdSmatthias.ringwald  */
22040d1c7a4Smatthias.ringwald static int nr_hci_connections(void){
22156c253c9Smatthias.ringwald     int count = 0;
22243bfb1bdSmatthias.ringwald     linked_item_t *it;
2233a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next, count++);
22443bfb1bdSmatthias.ringwald     return count;
22543bfb1bdSmatthias.ringwald }
226c8e4258aSmatthias.ringwald 
22797addcc5Smatthias.ringwald /**
228ba681a6cSmatthias.ringwald  * Dummy handler called by HCI
22916833f0aSmatthias.ringwald  */
2302718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
23116833f0aSmatthias.ringwald }
23216833f0aSmatthias.ringwald 
233998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){
2345061f3afS[email protected]     hci_connection_t * connection = hci_connection_for_handle(handle);
235998906cdSmatthias.ringwald     if (!connection) {
2367d67539fSmatthias.ringwald         log_error("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle);
237998906cdSmatthias.ringwald         return 0;
238998906cdSmatthias.ringwald     }
239998906cdSmatthias.ringwald     return connection->num_acl_packets_sent;
240998906cdSmatthias.ringwald }
241998906cdSmatthias.ringwald 
242998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){
2433a9fb326S[email protected]     uint8_t free_slots = hci_stack->total_num_acl_packets;
244998906cdSmatthias.ringwald     linked_item_t *it;
2453a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
246998906cdSmatthias.ringwald         hci_connection_t * connection = (hci_connection_t *) it;
247998906cdSmatthias.ringwald         if (free_slots < connection->num_acl_packets_sent) {
2487d67539fSmatthias.ringwald             log_error("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n");
249998906cdSmatthias.ringwald             return 0;
250998906cdSmatthias.ringwald         }
251998906cdSmatthias.ringwald         free_slots -= connection->num_acl_packets_sent;
252998906cdSmatthias.ringwald     }
253998906cdSmatthias.ringwald     return free_slots;
254998906cdSmatthias.ringwald }
255998906cdSmatthias.ringwald 
256c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){
2572b12a0b9Smatthias.ringwald 
2582b12a0b9Smatthias.ringwald     // check for async hci transport implementations
2593a9fb326S[email protected]     if (hci_stack->hci_transport->can_send_packet_now){
2603a9fb326S[email protected]         if (!hci_stack->hci_transport->can_send_packet_now(packet_type)){
2612b12a0b9Smatthias.ringwald             return 0;
2622b12a0b9Smatthias.ringwald         }
2632b12a0b9Smatthias.ringwald     }
2642b12a0b9Smatthias.ringwald 
2652b12a0b9Smatthias.ringwald     // check regular Bluetooth flow control
266c24735b1Smatthias.ringwald     switch (packet_type) {
267c24735b1Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
268c24735b1Smatthias.ringwald             return hci_number_free_acl_slots();
269c24735b1Smatthias.ringwald         case HCI_COMMAND_DATA_PACKET:
2703a9fb326S[email protected]             return hci_stack->num_cmd_packets;
271c24735b1Smatthias.ringwald         default:
272c24735b1Smatthias.ringwald             return 0;
273c24735b1Smatthias.ringwald     }
274c24735b1Smatthias.ringwald }
275c24735b1Smatthias.ringwald 
2766b4af23dS[email protected] // same as hci_can_send_packet_now, but also checks if packet buffer is free for use
2776b4af23dS[email protected] int hci_can_send_packet_now_using_packet_buffer(uint8_t packet_type){
2786b4af23dS[email protected]     if (hci_stack->hci_packet_buffer_reserved) return 0;
2796b4af23dS[email protected]     return hci_can_send_packet_now(packet_type);
2806b4af23dS[email protected] }
2816b4af23dS[email protected] 
282c8b9416aS[email protected] // used for internal checks in l2cap[-le].c
283c8b9416aS[email protected] int hci_is_packet_buffer_reserved(void){
284c8b9416aS[email protected]     return hci_stack->hci_packet_buffer_reserved;
285c8b9416aS[email protected] }
286c8b9416aS[email protected] 
2876b4af23dS[email protected] // reserves outgoing packet buffer. @returns 1 if successful
2886b4af23dS[email protected] int hci_reserve_packet_buffer(void){
2896b4af23dS[email protected]     if (hci_stack->hci_packet_buffer_reserved) return 0;
2906b4af23dS[email protected]     hci_stack->hci_packet_buffer_reserved = 1;
2916b4af23dS[email protected]     return 1;
2926b4af23dS[email protected] }
2936b4af23dS[email protected] 
294*68a0fcf7S[email protected] void hci_release_packet_buffer(void){
295*68a0fcf7S[email protected]     hci_stack->hci_packet_buffer_reserved = 0;
296*68a0fcf7S[email protected] }
297*68a0fcf7S[email protected] 
2986b4af23dS[email protected] // assumption: synchronous implementations don't provide can_send_packet_now as they don't keep the buffer after the call
2996b4af23dS[email protected] int hci_transport_synchronous(void){
3006b4af23dS[email protected]     return hci_stack->hci_transport->can_send_packet_now == NULL;
3016b4af23dS[email protected] }
3026b4af23dS[email protected] 
303ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
3047856c818Smatthias.ringwald 
3056218e6f1Smatthias.ringwald     // check for free places on BT module
3065932f1b4Smatthias.ringwald     if (!hci_number_free_acl_slots()) return BTSTACK_ACL_BUFFERS_FULL;
3076218e6f1Smatthias.ringwald 
3087856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
3095061f3afS[email protected]     hci_connection_t *connection = hci_connection_for_handle( con_handle);
31056cf178bSmatthias.ringwald     if (!connection) return 0;
31156cf178bSmatthias.ringwald     hci_connection_timestamp(connection);
31256cf178bSmatthias.ringwald 
31356cf178bSmatthias.ringwald     // count packet
31456cf178bSmatthias.ringwald     connection->num_acl_packets_sent++;
3157b5fbe1fSmatthias.ringwald     // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent);
3167856c818Smatthias.ringwald 
31700d8e42eSmatthias.ringwald     // send packet
3183a9fb326S[email protected]     int err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size);
3196218e6f1Smatthias.ringwald 
3206b4af23dS[email protected]     // free packet buffer for synchronous transport implementations
321c8b9416aS[email protected]     if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){
3226b4af23dS[email protected]         hci_stack->hci_packet_buffer_reserved = 0;
3236b4af23dS[email protected]     }
3246b4af23dS[email protected] 
32500d8e42eSmatthias.ringwald     return err;
326ee091cf1Smatthias.ringwald }
327ee091cf1Smatthias.ringwald 
32816833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
3297856c818Smatthias.ringwald 
330e76a89eeS[email protected]     // log_info("acl_handler: size %u", size);
331e76a89eeS[email protected] 
3327856c818Smatthias.ringwald     // get info
3337856c818Smatthias.ringwald     hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet);
3345061f3afS[email protected]     hci_connection_t *conn      = hci_connection_for_handle(con_handle);
3357856c818Smatthias.ringwald     uint8_t  acl_flags          = READ_ACL_FLAGS(packet);
3367856c818Smatthias.ringwald     uint16_t acl_length         = READ_ACL_LENGTH(packet);
3377856c818Smatthias.ringwald 
3387856c818Smatthias.ringwald     // ignore non-registered handle
3397856c818Smatthias.ringwald     if (!conn){
3407d67539fSmatthias.ringwald         log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle);
3417856c818Smatthias.ringwald         return;
3427856c818Smatthias.ringwald     }
3437856c818Smatthias.ringwald 
344e76a89eeS[email protected]     // assert packet is complete
3459ecc3e17S[email protected]     if (acl_length + 4 != size){
346e76a89eeS[email protected]         log_error("hci.c: acl_handler called with ACL packet of wrong size %u, expected %u => dropping packet", size, acl_length + 4);
347e76a89eeS[email protected]         return;
348e76a89eeS[email protected]     }
349e76a89eeS[email protected] 
3507856c818Smatthias.ringwald     // update idle timestamp
3517856c818Smatthias.ringwald     hci_connection_timestamp(conn);
3527856c818Smatthias.ringwald 
3537856c818Smatthias.ringwald     // handle different packet types
3547856c818Smatthias.ringwald     switch (acl_flags & 0x03) {
3557856c818Smatthias.ringwald 
3567856c818Smatthias.ringwald         case 0x01: // continuation fragment
3577856c818Smatthias.ringwald 
3587856c818Smatthias.ringwald             // sanity check
3597856c818Smatthias.ringwald             if (conn->acl_recombination_pos == 0) {
3607d67539fSmatthias.ringwald                 log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle);
3617856c818Smatthias.ringwald                 return;
3627856c818Smatthias.ringwald             }
3637856c818Smatthias.ringwald 
3647856c818Smatthias.ringwald             // append fragment payload (header already stored)
3657856c818Smatthias.ringwald             memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length );
3667856c818Smatthias.ringwald             conn->acl_recombination_pos += acl_length;
3677856c818Smatthias.ringwald 
3687d67539fSmatthias.ringwald             // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length,
369decc01a8Smatthias.ringwald             //        conn->acl_recombination_pos, conn->acl_recombination_length);
3707856c818Smatthias.ringwald 
3717856c818Smatthias.ringwald             // forward complete L2CAP packet if complete.
3727856c818Smatthias.ringwald             if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header
3737856c818Smatthias.ringwald 
3743a9fb326S[email protected]                 hci_stack->packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos);
3757856c818Smatthias.ringwald                 // reset recombination buffer
3767856c818Smatthias.ringwald                 conn->acl_recombination_length = 0;
3777856c818Smatthias.ringwald                 conn->acl_recombination_pos = 0;
3787856c818Smatthias.ringwald             }
3797856c818Smatthias.ringwald             break;
3807856c818Smatthias.ringwald 
3817856c818Smatthias.ringwald         case 0x02: { // first fragment
3827856c818Smatthias.ringwald 
3837856c818Smatthias.ringwald             // sanity check
3847856c818Smatthias.ringwald             if (conn->acl_recombination_pos) {
3857d67539fSmatthias.ringwald                 log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle);
3867856c818Smatthias.ringwald                 return;
3877856c818Smatthias.ringwald             }
3887856c818Smatthias.ringwald 
3897856c818Smatthias.ringwald             // peek into L2CAP packet!
3907856c818Smatthias.ringwald             uint16_t l2cap_length = READ_L2CAP_LENGTH( packet );
3917856c818Smatthias.ringwald 
392e76a89eeS[email protected]             // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length);
393decc01a8Smatthias.ringwald 
3947856c818Smatthias.ringwald             // compare fragment size to L2CAP packet size
3957856c818Smatthias.ringwald             if (acl_length >= l2cap_length + 4){
3967856c818Smatthias.ringwald 
3977856c818Smatthias.ringwald                 // forward fragment as L2CAP packet
3983a9fb326S[email protected]                 hci_stack->packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4);
3997856c818Smatthias.ringwald 
4007856c818Smatthias.ringwald             } else {
4017856c818Smatthias.ringwald                 // store first fragment and tweak acl length for complete package
4027856c818Smatthias.ringwald                 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4);
4037856c818Smatthias.ringwald                 conn->acl_recombination_pos    = acl_length + 4;
4047856c818Smatthias.ringwald                 conn->acl_recombination_length = l2cap_length;
405decc01a8Smatthias.ringwald                 bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4);
4067856c818Smatthias.ringwald             }
4077856c818Smatthias.ringwald             break;
4087856c818Smatthias.ringwald 
4097856c818Smatthias.ringwald         }
4107856c818Smatthias.ringwald         default:
4117d67539fSmatthias.ringwald             log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03);
4127856c818Smatthias.ringwald             return;
4137856c818Smatthias.ringwald     }
41494ab26f8Smatthias.ringwald 
41594ab26f8Smatthias.ringwald     // execute main loop
41694ab26f8Smatthias.ringwald     hci_run();
41716833f0aSmatthias.ringwald }
41822909952Smatthias.ringwald 
41967a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){
420339b6768Smatthias.ringwald     log_info("Connection closed: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
4213c4d4b90Smatthias.ringwald 
4223c4d4b90Smatthias.ringwald     // cancel all l2cap connections
4233c4d4b90Smatthias.ringwald     hci_emit_disconnection_complete(conn->con_handle, 0x16);    // terminated by local host
4243c4d4b90Smatthias.ringwald 
425c7e0c5f6Smatthias.ringwald     run_loop_remove_timer(&conn->timeout);
426c785ef68Smatthias.ringwald 
4273a9fb326S[email protected]     linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
428a3b02b71Smatthias.ringwald     btstack_memory_hci_connection_free( conn );
4293c4d4b90Smatthias.ringwald 
4303c4d4b90Smatthias.ringwald     // now it's gone
431c7e0c5f6Smatthias.ringwald     hci_emit_nr_connections_changed();
432c7e0c5f6Smatthias.ringwald }
433c7e0c5f6Smatthias.ringwald 
4340c042179S[email protected] static const uint16_t packet_type_sizes[] = {
4358f8108aaSmatthias.ringwald     0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE,
4368f8108aaSmatthias.ringwald     HCI_ACL_DH1_SIZE, 0, 0, 0,
4378f8108aaSmatthias.ringwald     HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE,
4388f8108aaSmatthias.ringwald     HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE
4398f8108aaSmatthias.ringwald };
44065389bfcS[email protected] static const uint8_t  packet_type_feature_requirement_bit[] = {
44165389bfcS[email protected]      0, // 3 slot packets
44265389bfcS[email protected]      1, // 5 slot packets
44365389bfcS[email protected]     25, // EDR 2 mpbs
44465389bfcS[email protected]     26, // EDR 3 mbps
44565389bfcS[email protected]     39, // 3 slot EDR packts
44665389bfcS[email protected]     40, // 5 slot EDR packet
44765389bfcS[email protected] };
44865389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = {
44965389bfcS[email protected]     0x0f00, // 3 slot packets
45065389bfcS[email protected]     0xf000, // 5 slot packets
45165389bfcS[email protected]     0x1102, // EDR 2 mpbs
45265389bfcS[email protected]     0x2204, // EDR 3 mbps
45365389bfcS[email protected]     0x0300, // 3 slot EDR packts
45465389bfcS[email protected]     0x3000, // 5 slot EDR packet
45565389bfcS[email protected] };
4568f8108aaSmatthias.ringwald 
45765389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){
45865389bfcS[email protected]     // enable packet types based on size
4598f8108aaSmatthias.ringwald     uint16_t packet_types = 0;
4608f8108aaSmatthias.ringwald     int i;
4618f8108aaSmatthias.ringwald     for (i=0;i<16;i++){
4628f8108aaSmatthias.ringwald         if (packet_type_sizes[i] == 0) continue;
4638f8108aaSmatthias.ringwald         if (packet_type_sizes[i] <= buffer_size){
4648f8108aaSmatthias.ringwald             packet_types |= 1 << i;
4658f8108aaSmatthias.ringwald         }
4668f8108aaSmatthias.ringwald     }
46765389bfcS[email protected]     // disable packet types due to missing local supported features
46865389bfcS[email protected]     for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){
46965389bfcS[email protected]         int bit_idx = packet_type_feature_requirement_bit[i];
47065389bfcS[email protected]         int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0;
47165389bfcS[email protected]         if (feature_set) continue;
47265389bfcS[email protected]         log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]);
47365389bfcS[email protected]         packet_types &= ~packet_type_feature_packet_mask[i];
47465389bfcS[email protected]     }
4758f8108aaSmatthias.ringwald     // flip bits for "may not be used"
4768f8108aaSmatthias.ringwald     packet_types ^= 0x3306;
4778f8108aaSmatthias.ringwald     return packet_types;
4788f8108aaSmatthias.ringwald }
4798f8108aaSmatthias.ringwald 
4808f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){
4813a9fb326S[email protected]     return hci_stack->packet_types;
4828f8108aaSmatthias.ringwald }
4838f8108aaSmatthias.ringwald 
484facf93fdS[email protected] uint8_t* hci_get_outgoing_packet_buffer(void){
4857dc17943Smatthias.ringwald     // hci packet buffer is >= acl data packet length
4863a9fb326S[email protected]     return hci_stack->hci_packet_buffer;
4877dc17943Smatthias.ringwald }
4887dc17943Smatthias.ringwald 
489f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){
4903a9fb326S[email protected]     return hci_stack->acl_data_packet_length;
4917dc17943Smatthias.ringwald }
4927dc17943Smatthias.ringwald 
493f5d8d141S[email protected] int hci_ssp_supported(void){
494f5d8d141S[email protected]     // No 51, byte 6, bit 3
4953a9fb326S[email protected]     return (hci_stack->local_supported_features[6] & (1 << 3)) != 0;
496f5d8d141S[email protected] }
497f5d8d141S[email protected] 
498f5d8d141S[email protected] int hci_classic_supported(void){
499f5d8d141S[email protected]     // No 37, byte 4, bit 5, = No BR/EDR Support
5003a9fb326S[email protected]     return (hci_stack->local_supported_features[4] & (1 << 5)) == 0;
501f5d8d141S[email protected] }
502f5d8d141S[email protected] 
503f5d8d141S[email protected] int hci_le_supported(void){
504f5d8d141S[email protected]     // No 37, byte 4, bit 6 = LE Supported (Controller)
505f5d8d141S[email protected] #ifdef HAVE_BLE
5063a9fb326S[email protected]     return (hci_stack->local_supported_features[4] & (1 << 6)) != 0;
507f5d8d141S[email protected] #else
508f5d8d141S[email protected]     return 0;
509f5d8d141S[email protected] #endif
510f5d8d141S[email protected] }
511f5d8d141S[email protected] 
51269a97523S[email protected] // get addr type and address used in advertisement packets
51318904abdS[email protected] void hci_le_advertisement_address(uint8_t * addr_type, bd_addr_t * addr){
5143a9fb326S[email protected]     *addr_type = hci_stack->adv_addr_type;
5153a9fb326S[email protected]     if (hci_stack->adv_addr_type){
5163a9fb326S[email protected]         memcpy(addr, hci_stack->adv_address, 6);
51769a97523S[email protected]     } else {
5183a9fb326S[email protected]         memcpy(addr, hci_stack->local_bd_addr, 6);
51969a97523S[email protected]     }
52069a97523S[email protected] }
52169a97523S[email protected] 
52274ec757aSmatthias.ringwald // avoid huge local variables
523223aafc1Smatthias.ringwald #ifndef EMBEDDED
52474ec757aSmatthias.ringwald static device_name_t device_name;
525223aafc1Smatthias.ringwald #endif
52616833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
527e76a89eeS[email protected] 
528e76a89eeS[email protected]     uint16_t event_length = packet[1];
529e76a89eeS[email protected] 
530e76a89eeS[email protected]     // assert packet is complete
531e76a89eeS[email protected]     if (size != event_length + 2){
532e76a89eeS[email protected]         log_error("hci.c: event_handler called with event packet of wrong size %u, expected %u => dropping packet", size, event_length + 2);
533e76a89eeS[email protected]         return;
534e76a89eeS[email protected]     }
535e76a89eeS[email protected] 
5361281a47eSmatthias.ringwald     bd_addr_t addr;
5372d00edd4Smatthias.ringwald     uint8_t link_type;
538fe1ed1b8Smatthias.ringwald     hci_con_handle_t handle;
5391f7b95a1Smatthias.ringwald     hci_connection_t * conn;
54056cf178bSmatthias.ringwald     int i;
54122909952Smatthias.ringwald 
5425909f7f2Smatthias.ringwald     // printf("HCI:EVENT:%02x\n", packet[0]);
5435909f7f2Smatthias.ringwald 
5446772a24cSmatthias.ringwald     switch (packet[0]) {
54522909952Smatthias.ringwald 
5466772a24cSmatthias.ringwald         case HCI_EVENT_COMMAND_COMPLETE:
5477ec5eeaaSmatthias.ringwald             // get num cmd packets
5483a9fb326S[email protected]             // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack->num_cmd_packets, packet[2]);
5493a9fb326S[email protected]             hci_stack->num_cmd_packets = packet[2];
5507ec5eeaaSmatthias.ringwald 
551e2edc0c3Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){
552e2edc0c3Smatthias.ringwald                 // from offset 5
553e2edc0c3Smatthias.ringwald                 // status
5541d279b20Smatthias.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"
5553a9fb326S[email protected]                 hci_stack->acl_data_packet_length = READ_BT_16(packet, 6);
55656cf178bSmatthias.ringwald                 // ignore: SCO data packet len (8)
5573a9fb326S[email protected]                 hci_stack->total_num_acl_packets  = packet[9];
55856cf178bSmatthias.ringwald                 // ignore: total num SCO packets
5593a9fb326S[email protected]                 if (hci_stack->state == HCI_STATE_INITIALIZING){
560a7a04bd9Smatthias.ringwald                     // determine usable ACL payload size
5613a9fb326S[email protected]                     if (HCI_ACL_PAYLOAD_SIZE < hci_stack->acl_data_packet_length){
5623a9fb326S[email protected]                         hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
5638f8108aaSmatthias.ringwald                     }
56465389bfcS[email protected]                     log_info("hci_read_buffer_size: used size %u, count %u\n",
5653a9fb326S[email protected]                              hci_stack->acl_data_packet_length, hci_stack->total_num_acl_packets);
566e2edc0c3Smatthias.ringwald                 }
56756cf178bSmatthias.ringwald             }
56865a46ef3S[email protected] #ifdef HAVE_BLE
56965a46ef3S[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)){
5703a9fb326S[email protected]                 hci_stack->le_data_packet_length = READ_BT_16(packet, 6);
5713a9fb326S[email protected]                 hci_stack->total_num_le_packets  = packet[8];
5723a9fb326S[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);
57365a46ef3S[email protected]             }
57465a46ef3S[email protected] #endif
575188981d2Smatthias.ringwald             // Dump local address
576188981d2Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) {
5773a9fb326S[email protected]                 bt_flip_addr(hci_stack->local_bd_addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]);
578188981d2Smatthias.ringwald                 log_info("Local Address, Status: 0x%02x: Addr: %s\n",
5793a9fb326S[email protected]                     packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr));
580188981d2Smatthias.ringwald             }
581381fbed8Smatthias.ringwald             if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
5823a9fb326S[email protected]                 hci_emit_discoverable_enabled(hci_stack->discoverable);
583381fbed8Smatthias.ringwald             }
58465389bfcS[email protected]             // Note: HCI init checks
585559e517eS[email protected]             if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){
5863a9fb326S[email protected]                 memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8);
587559e517eS[email protected]                 log_info("Local Supported Features: 0x%02x%02x%02x%02x%02x%02x%02x%02x",
5883a9fb326S[email protected]                     hci_stack->local_supported_features[0], hci_stack->local_supported_features[1],
5893a9fb326S[email protected]                     hci_stack->local_supported_features[2], hci_stack->local_supported_features[3],
5903a9fb326S[email protected]                     hci_stack->local_supported_features[4], hci_stack->local_supported_features[5],
5913a9fb326S[email protected]                     hci_stack->local_supported_features[6], hci_stack->local_supported_features[7]);
59265389bfcS[email protected] 
59365389bfcS[email protected]                 // determine usable ACL packet types based buffer size and supported features
5943a9fb326S[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]);
5953a9fb326S[email protected]                 log_info("packet types %04x", hci_stack->packet_types);
596f5d8d141S[email protected] 
597f5d8d141S[email protected]                 // Classic/LE
598f5d8d141S[email protected]                 log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported());
599559e517eS[email protected]             }
60056cf178bSmatthias.ringwald             break;
60156cf178bSmatthias.ringwald 
6027ec5eeaaSmatthias.ringwald         case HCI_EVENT_COMMAND_STATUS:
6037ec5eeaaSmatthias.ringwald             // get num cmd packets
6043a9fb326S[email protected]             // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack->num_cmd_packets, packet[3]);
6053a9fb326S[email protected]             hci_stack->num_cmd_packets = packet[3];
6067ec5eeaaSmatthias.ringwald             break;
6077ec5eeaaSmatthias.ringwald 
60856cf178bSmatthias.ringwald         case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
60956cf178bSmatthias.ringwald             for (i=0; i<packet[2];i++){
61056cf178bSmatthias.ringwald                 handle = READ_BT_16(packet, 3 + 2*i);
61156cf178bSmatthias.ringwald                 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i);
6125061f3afS[email protected]                 conn = hci_connection_for_handle(handle);
61356cf178bSmatthias.ringwald                 if (!conn){
6147d67539fSmatthias.ringwald                     log_error("hci_number_completed_packet lists unused con handle %u\n", handle);
61556cf178bSmatthias.ringwald                     continue;
61656cf178bSmatthias.ringwald                 }
61756cf178bSmatthias.ringwald                 conn->num_acl_packets_sent -= num_packets;
6187b5fbe1fSmatthias.ringwald                 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent);
61956cf178bSmatthias.ringwald             }
6206772a24cSmatthias.ringwald             break;
6216772a24cSmatthias.ringwald 
6221f7b95a1Smatthias.ringwald         case HCI_EVENT_CONNECTION_REQUEST:
62337eaa4cfSmatthias.ringwald             bt_flip_addr(addr, &packet[2]);
62437eaa4cfSmatthias.ringwald             // TODO: eval COD 8-10
6252d00edd4Smatthias.ringwald             link_type = packet[11];
626339b6768Smatthias.ringwald             log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type);
62737eaa4cfSmatthias.ringwald             if (link_type == 1) { // ACL
6281f7b95a1Smatthias.ringwald                 conn = connection_for_address(addr);
6291f7b95a1Smatthias.ringwald                 if (!conn) {
6301f7b95a1Smatthias.ringwald                     conn = create_connection_for_addr(addr);
6311f7b95a1Smatthias.ringwald                 }
632ce4c8fabSmatthias.ringwald                 if (!conn) {
633ce4c8fabSmatthias.ringwald                     // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D)
6343a9fb326S[email protected]                     hci_stack->decline_reason = 0x0d;
6353a9fb326S[email protected]                     BD_ADDR_COPY(hci_stack->decline_addr, addr);
636ce4c8fabSmatthias.ringwald                     break;
637ce4c8fabSmatthias.ringwald                 }
63832ab9390Smatthias.ringwald                 conn->state = RECEIVED_CONNECTION_REQUEST;
63932ab9390Smatthias.ringwald                 hci_run();
64037eaa4cfSmatthias.ringwald             } else {
641ce4c8fabSmatthias.ringwald                 // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A)
6423a9fb326S[email protected]                 hci_stack->decline_reason = 0x0a;
6433a9fb326S[email protected]                 BD_ADDR_COPY(hci_stack->decline_addr, addr);
64437eaa4cfSmatthias.ringwald             }
6451f7b95a1Smatthias.ringwald             break;
6461f7b95a1Smatthias.ringwald 
6476772a24cSmatthias.ringwald         case HCI_EVENT_CONNECTION_COMPLETE:
648fe1ed1b8Smatthias.ringwald             // Connection management
649fe1ed1b8Smatthias.ringwald             bt_flip_addr(addr, &packet[5]);
650339b6768Smatthias.ringwald             log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr));
6511f7b95a1Smatthias.ringwald             conn = connection_for_address(addr);
652fe1ed1b8Smatthias.ringwald             if (conn) {
653b448a0e7Smatthias.ringwald                 if (!packet[2]){
654c8e4258aSmatthias.ringwald                     conn->state = OPEN;
655fe1ed1b8Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 3);
656ad83dc6aS[email protected]                     conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES;
657ee091cf1Smatthias.ringwald 
658c785ef68Smatthias.ringwald                     // restart timer
659c21e6239Smatthias.ringwald                     run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
660ee091cf1Smatthias.ringwald                     run_loop_add_timer(&conn->timeout);
661c785ef68Smatthias.ringwald 
662339b6768Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
66343bfb1bdSmatthias.ringwald 
66443bfb1bdSmatthias.ringwald                     hci_emit_nr_connections_changed();
665b448a0e7Smatthias.ringwald                 } else {
666ad83dc6aS[email protected]                     // notify client if dedicated bonding
667ad83dc6aS[email protected]                     if (conn->bonding_flags & BONDING_DEDICATED){
668ad83dc6aS[email protected]                         hci_emit_dedicated_bonding_result(conn, packet[2]);
669ad83dc6aS[email protected]                     }
670ad83dc6aS[email protected] 
671b448a0e7Smatthias.ringwald                     // connection failed, remove entry
6723a9fb326S[email protected]                     linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
673a3b02b71Smatthias.ringwald                     btstack_memory_hci_connection_free( conn );
674c12e46e7Smatthias.ringwald 
675c12e46e7Smatthias.ringwald                     // if authentication error, also delete link key
676c12e46e7Smatthias.ringwald                     if (packet[2] == 0x05) {
677c12e46e7Smatthias.ringwald                         hci_drop_link_key_for_bd_addr(&addr);
678c12e46e7Smatthias.ringwald                     }
679fe1ed1b8Smatthias.ringwald                 }
680fe1ed1b8Smatthias.ringwald             }
6816772a24cSmatthias.ringwald             break;
682fe1ed1b8Smatthias.ringwald 
683afd4e962S[email protected]         case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
684afd4e962S[email protected]             handle = READ_BT_16(packet, 3);
685afd4e962S[email protected]             conn = hci_connection_for_handle(handle);
686afd4e962S[email protected]             if (!conn) break;
687afd4e962S[email protected]             if (!packet[2]){
688afd4e962S[email protected]                 uint8_t * features = &packet[5];
689afd4e962S[email protected]                 if (features[6] & (1 << 3)){
690afd4e962S[email protected]                     conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP;
691afd4e962S[email protected]                 }
692afd4e962S[email protected]             }
693afd4e962S[email protected]             conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES;
694ad83dc6aS[email protected]             log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x", conn->bonding_flags);
695ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
696ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
697ad83dc6aS[email protected]             }
698afd4e962S[email protected]             break;
699afd4e962S[email protected] 
7007fde4af9Smatthias.ringwald         case HCI_EVENT_LINK_KEY_REQUEST:
7017b5fbe1fSmatthias.ringwald             log_info("HCI_EVENT_LINK_KEY_REQUEST\n");
7027fde4af9Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST);
70374d716b5S[email protected]             // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST
7043a9fb326S[email protected]             if (hci_stack->bondable && !hci_stack->remote_device_db) break;
70532ab9390Smatthias.ringwald             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST);
70664472d52Smatthias.ringwald             hci_run();
707d9a327f9Smatthias.ringwald             // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set
70829d53098Smatthias.ringwald             return;
7097fde4af9Smatthias.ringwald 
7109ab95c90S[email protected]         case HCI_EVENT_LINK_KEY_NOTIFICATION: {
71129d53098Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
7129ab95c90S[email protected]             conn = connection_for_address(addr);
7139ab95c90S[email protected]             if (!conn) break;
7149ab95c90S[email protected]             conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION;
7159ab95c90S[email protected]             link_key_type_t link_key_type = packet[24];
7169ab95c90S[email protected]             // Change Connection Encryption keeps link key type
7179ab95c90S[email protected]             if (link_key_type != CHANGED_COMBINATION_KEY){
7189ab95c90S[email protected]                 conn->link_key_type = link_key_type;
7199ab95c90S[email protected]             }
7203a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
7213a9fb326S[email protected]             hci_stack->remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8], conn->link_key_type);
72229d53098Smatthias.ringwald             // still forward event to allow dismiss of pairing dialog
7237fde4af9Smatthias.ringwald             break;
7249ab95c90S[email protected]         }
7257fde4af9Smatthias.ringwald 
7267fde4af9Smatthias.ringwald         case HCI_EVENT_PIN_CODE_REQUEST:
7276724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE);
7284c57c146S[email protected]             // non-bondable mode: pin code negative reply will be sent
7293a9fb326S[email protected]             if (!hci_stack->bondable){
730899283eaS[email protected]                 hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST);
731f8fb5f6eS[email protected]                 hci_run();
732f8fb5f6eS[email protected]                 return;
7334c57c146S[email protected]             }
734d9a327f9Smatthias.ringwald             // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key
7353a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
736d9a327f9Smatthias.ringwald             bt_flip_addr(addr, &packet[2]);
7373a9fb326S[email protected]             hci_stack->remote_device_db->delete_link_key(&addr);
7387fde4af9Smatthias.ringwald             break;
7397fde4af9Smatthias.ringwald 
7401d6b20aeS[email protected]         case HCI_EVENT_IO_CAPABILITY_REQUEST:
7411d6b20aeS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST);
742dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY);
743dbe1a790S[email protected]             break;
744dbe1a790S[email protected] 
745dbe1a790S[email protected]         case HCI_EVENT_USER_CONFIRMATION_REQUEST:
7466724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
7473a9fb326S[email protected]             if (!hci_stack->ssp_auto_accept) break;
748dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY);
749dbe1a790S[email protected]             break;
750dbe1a790S[email protected] 
751dbe1a790S[email protected]         case HCI_EVENT_USER_PASSKEY_REQUEST:
7526724cd9eS[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE);
7533a9fb326S[email protected]             if (!hci_stack->ssp_auto_accept) break;
754dbe1a790S[email protected]             hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY);
7551d6b20aeS[email protected]             break;
7561d6b20aeS[email protected] 
757f0944df2S[email protected]         case HCI_EVENT_ENCRYPTION_CHANGE:
758f0944df2S[email protected]             handle = READ_BT_16(packet, 3);
759f0944df2S[email protected]             conn = hci_connection_for_handle(handle);
760f0944df2S[email protected]             if (!conn) break;
761ad83dc6aS[email protected]             if (packet[2] == 0) {
762f0944df2S[email protected]                 if (packet[5]){
763f0944df2S[email protected]                     conn->authentication_flags |= CONNECTION_ENCRYPTED;
764f0944df2S[email protected]                 } else {
765536f9994S[email protected]                     conn->authentication_flags &= ~CONNECTION_ENCRYPTED;
766f0944df2S[email protected]                 }
767ad83dc6aS[email protected]             }
768a00031e2S[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
769f0944df2S[email protected]             break;
770f0944df2S[email protected] 
7711eb2563eS[email protected]         case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT:
7721eb2563eS[email protected]             handle = READ_BT_16(packet, 3);
7731eb2563eS[email protected]             conn = hci_connection_for_handle(handle);
7741eb2563eS[email protected]             if (!conn) break;
775ad83dc6aS[email protected] 
776ad83dc6aS[email protected]             // dedicated bonding: send result and disconnect
777ad83dc6aS[email protected]             if (conn->bonding_flags & BONDING_DEDICATED){
778ad83dc6aS[email protected]                 conn->bonding_flags &= ~BONDING_DEDICATED;
779ad83dc6aS[email protected]                 hci_emit_dedicated_bonding_result( conn, packet[2]);
780ad83dc6aS[email protected]                 conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE;
781ad83dc6aS[email protected]                 break;
782ad83dc6aS[email protected]             }
783ad83dc6aS[email protected] 
784b5cb6874S[email protected]             if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){
7851eb2563eS[email protected]                 // link key sufficient for requested security
7861eb2563eS[email protected]                 conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
787ad83dc6aS[email protected]                 break;
788ad83dc6aS[email protected]             }
7891eb2563eS[email protected]             // not enough
7901eb2563eS[email protected]             hci_emit_security_level(handle, gap_security_level_for_connection(conn));
7911eb2563eS[email protected]             break;
79234d2123cS[email protected] 
793223aafc1Smatthias.ringwald #ifndef EMBEDDED
79474ec757aSmatthias.ringwald         case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE:
7953a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
79674ec757aSmatthias.ringwald             if (packet[2]) break; // status not ok
79774ec757aSmatthias.ringwald             bt_flip_addr(addr, &packet[3]);
7985a06394aSmatthias.ringwald             // fix for invalid remote names - terminate on 0xff
7995a06394aSmatthias.ringwald             for (i=0; i<248;i++){
8005a06394aSmatthias.ringwald                 if (packet[9+i] == 0xff){
8015a06394aSmatthias.ringwald                     packet[9+i] = 0;
8025a06394aSmatthias.ringwald                     break;
803cdc9101dSmatthias.ringwald                 }
8045a06394aSmatthias.ringwald             }
805d2fe945cS[email protected]             memset(&device_name, 0, sizeof(device_name_t));
80674ec757aSmatthias.ringwald             strncpy((char*) device_name, (char*) &packet[9], 248);
8073a9fb326S[email protected]             hci_stack->remote_device_db->put_name(&addr, &device_name);
80874ec757aSmatthias.ringwald             break;
80974ec757aSmatthias.ringwald 
81074ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT:
81174ec757aSmatthias.ringwald         case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:
8123a9fb326S[email protected]             if (!hci_stack->remote_device_db) break;
81374ec757aSmatthias.ringwald             // first send inq result packet
8143a9fb326S[email protected]             hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size);
81574ec757aSmatthias.ringwald             // then send cached remote names
81674ec757aSmatthias.ringwald             for (i=0; i<packet[2];i++){
81774ec757aSmatthias.ringwald                 bt_flip_addr(addr, &packet[3+i*6]);
8183a9fb326S[email protected]                 if (hci_stack->remote_device_db->get_name(&addr, &device_name)){
81974ec757aSmatthias.ringwald                     hci_emit_remote_name_cached(&addr, &device_name);
82074ec757aSmatthias.ringwald                 }
82174ec757aSmatthias.ringwald             }
82274ec757aSmatthias.ringwald             return;
823223aafc1Smatthias.ringwald #endif
82474ec757aSmatthias.ringwald 
8256772a24cSmatthias.ringwald         case HCI_EVENT_DISCONNECTION_COMPLETE:
826fe1ed1b8Smatthias.ringwald             if (!packet[2]){
827fe1ed1b8Smatthias.ringwald                 handle = READ_BT_16(packet, 3);
8285061f3afS[email protected]                 hci_connection_t * conn = hci_connection_for_handle(handle);
829fe1ed1b8Smatthias.ringwald                 if (conn) {
830c7e0c5f6Smatthias.ringwald                     hci_shutdown_connection(conn);
831fe1ed1b8Smatthias.ringwald                 }
832fe1ed1b8Smatthias.ringwald             }
8336772a24cSmatthias.ringwald             break;
8346772a24cSmatthias.ringwald 
835c68bdf90Smatthias.ringwald         case HCI_EVENT_HARDWARE_ERROR:
8363a9fb326S[email protected]             if(hci_stack->control && hci_stack->control->hw_error){
8373a9fb326S[email protected]                 (*hci_stack->control->hw_error)();
838c68bdf90Smatthias.ringwald             }
839c68bdf90Smatthias.ringwald             break;
840c68bdf90Smatthias.ringwald 
8416b4af23dS[email protected]         case DAEMON_EVENT_HCI_PACKET_SENT:
8426b4af23dS[email protected]             // free packet buffer for asynchronous transport
8436b4af23dS[email protected]             if (hci_transport_synchronous()) break;
8446b4af23dS[email protected]             hci_stack->hci_packet_buffer_reserved = 0;
8456b4af23dS[email protected]             break;
8466b4af23dS[email protected] 
8475909f7f2Smatthias.ringwald #ifdef HAVE_BLE
8485909f7f2Smatthias.ringwald         case HCI_EVENT_LE_META:
8495909f7f2Smatthias.ringwald             switch (packet[2]) {
8505909f7f2Smatthias.ringwald                 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
8515909f7f2Smatthias.ringwald                     // Connection management
8525909f7f2Smatthias.ringwald                     bt_flip_addr(addr, &packet[8]);
8535909f7f2Smatthias.ringwald                     log_info("LE Connection_complete (status=%u) %s\n", packet[3], bd_addr_to_str(addr));
8545909f7f2Smatthias.ringwald                     // LE connections are auto-accepted, so just create a connection if there isn't one already
8555909f7f2Smatthias.ringwald                     conn = connection_for_address(addr);
8565909f7f2Smatthias.ringwald                     if (packet[3]){
8575909f7f2Smatthias.ringwald                         if (conn){
8585909f7f2Smatthias.ringwald                             // outgoing connection failed, remove entry
8593a9fb326S[email protected]                             linked_list_remove(&hci_stack->connections, (linked_item_t *) conn);
8605909f7f2Smatthias.ringwald                             btstack_memory_hci_connection_free( conn );
8615909f7f2Smatthias.ringwald 
8625909f7f2Smatthias.ringwald                         }
8635909f7f2Smatthias.ringwald                         // if authentication error, also delete link key
8645909f7f2Smatthias.ringwald                         if (packet[3] == 0x05) {
8655909f7f2Smatthias.ringwald                             hci_drop_link_key_for_bd_addr(&addr);
8665909f7f2Smatthias.ringwald                         }
8675909f7f2Smatthias.ringwald                         break;
8685909f7f2Smatthias.ringwald                     }
8695909f7f2Smatthias.ringwald                     if (!conn){
8705909f7f2Smatthias.ringwald                         conn = create_connection_for_addr(addr);
8715909f7f2Smatthias.ringwald                     }
8725909f7f2Smatthias.ringwald                     if (!conn){
8735909f7f2Smatthias.ringwald                         // no memory
8745909f7f2Smatthias.ringwald                         break;
8755909f7f2Smatthias.ringwald                     }
8765909f7f2Smatthias.ringwald 
8775909f7f2Smatthias.ringwald                     conn->state = OPEN;
8785909f7f2Smatthias.ringwald                     conn->con_handle = READ_BT_16(packet, 4);
8795909f7f2Smatthias.ringwald 
8805909f7f2Smatthias.ringwald                     // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock
8815909f7f2Smatthias.ringwald 
8825909f7f2Smatthias.ringwald                     // restart timer
8835909f7f2Smatthias.ringwald                     // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS);
8845909f7f2Smatthias.ringwald                     // run_loop_add_timer(&conn->timeout);
8855909f7f2Smatthias.ringwald 
8865909f7f2Smatthias.ringwald                     log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address));
8875909f7f2Smatthias.ringwald 
8885909f7f2Smatthias.ringwald                     hci_emit_nr_connections_changed();
8895909f7f2Smatthias.ringwald                     break;
8905909f7f2Smatthias.ringwald 
89165a46ef3S[email protected]             // printf("LE buffer size: %u, count %u\n", READ_BT_16(packet,6), packet[8]);
89265a46ef3S[email protected] 
8935909f7f2Smatthias.ringwald                 default:
8945909f7f2Smatthias.ringwald                     break;
8955909f7f2Smatthias.ringwald             }
8965909f7f2Smatthias.ringwald             break;
8975909f7f2Smatthias.ringwald #endif
8985909f7f2Smatthias.ringwald 
8996772a24cSmatthias.ringwald         default:
9006772a24cSmatthias.ringwald             break;
901fe1ed1b8Smatthias.ringwald     }
902fe1ed1b8Smatthias.ringwald 
9033429f56bSmatthias.ringwald     // handle BT initialization
9043a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_INITIALIZING){
9053a9fb326S[email protected]         if (hci_stack->substate % 2){
9063429f56bSmatthias.ringwald             // odd: waiting for event
907f155fca3Smatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || packet[0] == HCI_EVENT_COMMAND_STATUS){
908c9af4d3fS[email protected]                 // wait for explicit COMMAND COMPLETE on RESET
9093a9fb326S[email protected]                 if (hci_stack->substate > 1 || COMMAND_COMPLETE_EVENT(packet, hci_reset)) {
9103a9fb326S[email protected]                     hci_stack->substate++;
9113429f56bSmatthias.ringwald                 }
9123429f56bSmatthias.ringwald             }
91322909952Smatthias.ringwald         }
914c9af4d3fS[email protected]     }
91522909952Smatthias.ringwald 
91689db417bSmatthias.ringwald     // help with BT sleep
9173a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_FALLING_ASLEEP
9183a9fb326S[email protected]         && hci_stack->substate == 1
91989db417bSmatthias.ringwald         && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){
9203a9fb326S[email protected]         hci_stack->substate++;
92189db417bSmatthias.ringwald     }
92289db417bSmatthias.ringwald 
9233a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size);
92494ab26f8Smatthias.ringwald 
92594ab26f8Smatthias.ringwald 	// execute main loop
92694ab26f8Smatthias.ringwald 	hci_run();
92716833f0aSmatthias.ringwald }
92816833f0aSmatthias.ringwald 
9290a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
93010e830c9Smatthias.ringwald     switch (packet_type) {
93110e830c9Smatthias.ringwald         case HCI_EVENT_PACKET:
93210e830c9Smatthias.ringwald             event_handler(packet, size);
93310e830c9Smatthias.ringwald             break;
93410e830c9Smatthias.ringwald         case HCI_ACL_DATA_PACKET:
93510e830c9Smatthias.ringwald             acl_handler(packet, size);
93610e830c9Smatthias.ringwald             break;
93710e830c9Smatthias.ringwald         default:
93810e830c9Smatthias.ringwald             break;
93910e830c9Smatthias.ringwald     }
94010e830c9Smatthias.ringwald }
94110e830c9Smatthias.ringwald 
942fcadd0caSmatthias.ringwald /** Register HCI packet handlers */
9432718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
9443a9fb326S[email protected]     hci_stack->packet_handler = handler;
94516833f0aSmatthias.ringwald }
94616833f0aSmatthias.ringwald 
9474f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){
948475c8125Smatthias.ringwald 
9493a9fb326S[email protected] #ifdef HAVE_MALLOC
9503a9fb326S[email protected]     if (!hci_stack) {
9513a9fb326S[email protected]         hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t));
9523a9fb326S[email protected]     }
9533a9fb326S[email protected] #else
9543a9fb326S[email protected]     hci_stack = &hci_stack_static;
9553a9fb326S[email protected] #endif
95666fb9560S[email protected]     memset(hci_stack, 0, sizeof(hci_stack_t));
9573a9fb326S[email protected] 
958475c8125Smatthias.ringwald     // reference to use transport layer implementation
9593a9fb326S[email protected]     hci_stack->hci_transport = transport;
960475c8125Smatthias.ringwald 
96111e23e5fSmatthias.ringwald     // references to used control implementation
9623a9fb326S[email protected]     hci_stack->control = control;
96311e23e5fSmatthias.ringwald 
96411e23e5fSmatthias.ringwald     // reference to used config
9653a9fb326S[email protected]     hci_stack->config = config;
96611e23e5fSmatthias.ringwald 
967fe1ed1b8Smatthias.ringwald     // no connections yet
9683a9fb326S[email protected]     hci_stack->connections = NULL;
9693a9fb326S[email protected]     hci_stack->discoverable = 0;
9703a9fb326S[email protected]     hci_stack->connectable = 0;
9713a9fb326S[email protected]     hci_stack->bondable = 1;
972758b46ceSmatthias.ringwald 
973b031bebbSmatthias.ringwald     // no pending cmds
9743a9fb326S[email protected]     hci_stack->decline_reason = 0;
9753a9fb326S[email protected]     hci_stack->new_scan_enable_value = 0xff;
976b031bebbSmatthias.ringwald 
97716833f0aSmatthias.ringwald     // higher level handler
9783a9fb326S[email protected]     hci_stack->packet_handler = dummy_handler;
97916833f0aSmatthias.ringwald 
980404843c1Smatthias.ringwald     // store and open remote device db
9813a9fb326S[email protected]     hci_stack->remote_device_db = remote_device_db;
9823a9fb326S[email protected]     if (hci_stack->remote_device_db) {
9833a9fb326S[email protected]         hci_stack->remote_device_db->open();
984404843c1Smatthias.ringwald     }
98529d53098Smatthias.ringwald 
9868fcba05dSmatthias.ringwald     // max acl payload size defined in config.h
9873a9fb326S[email protected]     hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE;
9888fcba05dSmatthias.ringwald 
98916833f0aSmatthias.ringwald     // register packet handlers with transport
99010e830c9Smatthias.ringwald     transport->register_packet_handler(&packet_handler);
991f5454fc6Smatthias.ringwald 
9923a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
993e2386ba1S[email protected] 
994e2386ba1S[email protected]     // class of device
9953a9fb326S[email protected]     hci_stack->class_of_device = 0x007a020c; // Smartphone
996a45d6b9fS[email protected] 
99763048403S[email protected]     // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept
9983a9fb326S[email protected]     hci_stack->ssp_enable = 1;
9993a9fb326S[email protected]     hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT;
10003a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING;
10013a9fb326S[email protected]     hci_stack->ssp_auto_accept = 1;
100269a97523S[email protected] 
100369a97523S[email protected]     // LE
10043a9fb326S[email protected]     hci_stack->adv_addr_type = 0;
10053a9fb326S[email protected]     memset(hci_stack->adv_address, 0, 6);
1006475c8125Smatthias.ringwald }
1007475c8125Smatthias.ringwald 
1008404843c1Smatthias.ringwald void hci_close(){
1009404843c1Smatthias.ringwald     // close remote device db
10103a9fb326S[email protected]     if (hci_stack->remote_device_db) {
10113a9fb326S[email protected]         hci_stack->remote_device_db->close();
1012404843c1Smatthias.ringwald     }
10133a9fb326S[email protected]     while (hci_stack->connections) {
10143a9fb326S[email protected]         hci_shutdown_connection((hci_connection_t *) hci_stack->connections);
1015f5454fc6Smatthias.ringwald     }
1016f5454fc6Smatthias.ringwald     hci_power_control(HCI_POWER_OFF);
10173a9fb326S[email protected] 
10183a9fb326S[email protected] #ifdef HAVE_MALLOC
10193a9fb326S[email protected]     free(hci_stack);
10203a9fb326S[email protected] #endif
10213a9fb326S[email protected]     hci_stack = NULL;
1022404843c1Smatthias.ringwald }
1023404843c1Smatthias.ringwald 
10249e61646fS[email protected] void hci_set_class_of_device(uint32_t class_of_device){
10259e61646fS[email protected]     hci_stack->class_of_device = class_of_device;
10269e61646fS[email protected] }
10279e61646fS[email protected] 
102866fb9560S[email protected] void hci_disable_l2cap_timeout_check(){
102966fb9560S[email protected]     disable_l2cap_timeouts = 1;
103066fb9560S[email protected] }
10318d213e1aSmatthias.ringwald // State-Module-Driver overview
10328d213e1aSmatthias.ringwald // state                    module  low-level
10338d213e1aSmatthias.ringwald // HCI_STATE_OFF             off      close
10348d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING,   on       open
10358d213e1aSmatthias.ringwald // HCI_STATE_WORKING,        on       open
10368d213e1aSmatthias.ringwald // HCI_STATE_HALTING,        on       open
1037d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING,    off/sleep   close
1038d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP  on       open
1039c7e0c5f6Smatthias.ringwald 
104040d1c7a4Smatthias.ringwald static int hci_power_control_on(void){
10417301ad89Smatthias.ringwald 
1042038bc64cSmatthias.ringwald     // power on
1043f9a30166Smatthias.ringwald     int err = 0;
10443a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->on){
10453a9fb326S[email protected]         err = (*hci_stack->control->on)(hci_stack->config);
1046f9a30166Smatthias.ringwald     }
1047038bc64cSmatthias.ringwald     if (err){
10487d67539fSmatthias.ringwald         log_error( "POWER_ON failed\n");
1049038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
1050038bc64cSmatthias.ringwald         return err;
1051038bc64cSmatthias.ringwald     }
1052038bc64cSmatthias.ringwald 
1053038bc64cSmatthias.ringwald     // open low-level device
10543a9fb326S[email protected]     err = hci_stack->hci_transport->open(hci_stack->config);
1055038bc64cSmatthias.ringwald     if (err){
10567d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
10573a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
10583a9fb326S[email protected]             (*hci_stack->control->off)(hci_stack->config);
1059f9a30166Smatthias.ringwald         }
1060038bc64cSmatthias.ringwald         hci_emit_hci_open_failed();
1061038bc64cSmatthias.ringwald         return err;
1062038bc64cSmatthias.ringwald     }
10638d213e1aSmatthias.ringwald     return 0;
10648d213e1aSmatthias.ringwald }
1065038bc64cSmatthias.ringwald 
106640d1c7a4Smatthias.ringwald static void hci_power_control_off(void){
10678d213e1aSmatthias.ringwald 
10687b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off\n");
10699418f9c9Smatthias.ringwald 
10708d213e1aSmatthias.ringwald     // close low-level device
10713a9fb326S[email protected]     hci_stack->hci_transport->close(hci_stack->config);
10728d213e1aSmatthias.ringwald 
10737b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - hci_transport closed\n");
10749418f9c9Smatthias.ringwald 
10758d213e1aSmatthias.ringwald     // power off
10763a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->off){
10773a9fb326S[email protected]         (*hci_stack->control->off)(hci_stack->config);
10788d213e1aSmatthias.ringwald     }
10799418f9c9Smatthias.ringwald 
10807b5fbe1fSmatthias.ringwald     log_info("hci_power_control_off - control closed\n");
10819418f9c9Smatthias.ringwald 
10823a9fb326S[email protected]     hci_stack->state = HCI_STATE_OFF;
108372ea5239Smatthias.ringwald }
108472ea5239Smatthias.ringwald 
108540d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){
108672ea5239Smatthias.ringwald 
10877b5fbe1fSmatthias.ringwald     log_info("hci_power_control_sleep\n");
10883144bce4Smatthias.ringwald 
1089b429b9b7Smatthias.ringwald #if 0
1090b429b9b7Smatthias.ringwald     // don't close serial port during sleep
1091b429b9b7Smatthias.ringwald 
109272ea5239Smatthias.ringwald     // close low-level device
10933a9fb326S[email protected]     hci_stack->hci_transport->close(hci_stack->config);
1094b429b9b7Smatthias.ringwald #endif
109572ea5239Smatthias.ringwald 
109672ea5239Smatthias.ringwald     // sleep mode
10973a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->sleep){
10983a9fb326S[email protected]         (*hci_stack->control->sleep)(hci_stack->config);
109972ea5239Smatthias.ringwald     }
1100b429b9b7Smatthias.ringwald 
11013a9fb326S[email protected]     hci_stack->state = HCI_STATE_SLEEPING;
11028d213e1aSmatthias.ringwald }
11038d213e1aSmatthias.ringwald 
110440d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){
1105b429b9b7Smatthias.ringwald 
11067b5fbe1fSmatthias.ringwald     log_info("hci_power_control_wake\n");
1107b429b9b7Smatthias.ringwald 
1108b429b9b7Smatthias.ringwald     // wake on
11093a9fb326S[email protected]     if (hci_stack->control && hci_stack->control->wake){
11103a9fb326S[email protected]         (*hci_stack->control->wake)(hci_stack->config);
1111b429b9b7Smatthias.ringwald     }
1112b429b9b7Smatthias.ringwald 
1113b429b9b7Smatthias.ringwald #if 0
1114b429b9b7Smatthias.ringwald     // open low-level device
11153a9fb326S[email protected]     int err = hci_stack->hci_transport->open(hci_stack->config);
1116b429b9b7Smatthias.ringwald     if (err){
11177d67539fSmatthias.ringwald         log_error( "HCI_INIT failed, turning Bluetooth off again\n");
11183a9fb326S[email protected]         if (hci_stack->control && hci_stack->control->off){
11193a9fb326S[email protected]             (*hci_stack->control->off)(hci_stack->config);
1120b429b9b7Smatthias.ringwald         }
1121b429b9b7Smatthias.ringwald         hci_emit_hci_open_failed();
1122b429b9b7Smatthias.ringwald         return err;
1123b429b9b7Smatthias.ringwald     }
1124b429b9b7Smatthias.ringwald #endif
1125b429b9b7Smatthias.ringwald 
1126b429b9b7Smatthias.ringwald     return 0;
1127b429b9b7Smatthias.ringwald }
1128b429b9b7Smatthias.ringwald 
1129b429b9b7Smatthias.ringwald 
11308d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
1131d661ed19Smatthias.ringwald 
11323a9fb326S[email protected]     log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack->state);
1133d661ed19Smatthias.ringwald 
11348d213e1aSmatthias.ringwald     int err = 0;
11353a9fb326S[email protected]     switch (hci_stack->state){
11368d213e1aSmatthias.ringwald 
11378d213e1aSmatthias.ringwald         case HCI_STATE_OFF:
11388d213e1aSmatthias.ringwald             switch (power_mode){
11398d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
11408d213e1aSmatthias.ringwald                     err = hci_power_control_on();
11418d213e1aSmatthias.ringwald                     if (err) return err;
11427301ad89Smatthias.ringwald                     // set up state machine
11433a9fb326S[email protected]                     hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent
11443a9fb326S[email protected]                     hci_stack->state = HCI_STATE_INITIALIZING;
11453a9fb326S[email protected]                     hci_stack->substate = 0;
11468d213e1aSmatthias.ringwald                     break;
11478d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
11488d213e1aSmatthias.ringwald                     // do nothing
11498d213e1aSmatthias.ringwald                     break;
11508d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1151b546ac54Smatthias.ringwald                     // do nothing (with SLEEP == OFF)
11528d213e1aSmatthias.ringwald                     break;
11538d213e1aSmatthias.ringwald             }
11548d213e1aSmatthias.ringwald             break;
11557301ad89Smatthias.ringwald 
11568d213e1aSmatthias.ringwald         case HCI_STATE_INITIALIZING:
11578d213e1aSmatthias.ringwald             switch (power_mode){
11588d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
11598d213e1aSmatthias.ringwald                     // do nothing
11608d213e1aSmatthias.ringwald                     break;
11618d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
11628d213e1aSmatthias.ringwald                     // no connections yet, just turn it off
11638d213e1aSmatthias.ringwald                     hci_power_control_off();
11648d213e1aSmatthias.ringwald                     break;
11658d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1166b546ac54Smatthias.ringwald                     // no connections yet, just turn it off
116772ea5239Smatthias.ringwald                     hci_power_control_sleep();
11688d213e1aSmatthias.ringwald                     break;
11698d213e1aSmatthias.ringwald             }
11708d213e1aSmatthias.ringwald             break;
11717301ad89Smatthias.ringwald 
11728d213e1aSmatthias.ringwald         case HCI_STATE_WORKING:
11738d213e1aSmatthias.ringwald             switch (power_mode){
11748d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
11758d213e1aSmatthias.ringwald                     // do nothing
11768d213e1aSmatthias.ringwald                     break;
11778d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1178c7e0c5f6Smatthias.ringwald                     // see hci_run
11793a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
11808d213e1aSmatthias.ringwald                     break;
11818d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1182b546ac54Smatthias.ringwald                     // see hci_run
11833a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
11843a9fb326S[email protected]                     hci_stack->substate = 0;
11858d213e1aSmatthias.ringwald                     break;
11868d213e1aSmatthias.ringwald             }
11878d213e1aSmatthias.ringwald             break;
11887301ad89Smatthias.ringwald 
11898d213e1aSmatthias.ringwald         case HCI_STATE_HALTING:
11908d213e1aSmatthias.ringwald             switch (power_mode){
11918d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
11928d213e1aSmatthias.ringwald                     // set up state machine
11933a9fb326S[email protected]                     hci_stack->state = HCI_STATE_INITIALIZING;
11943a9fb326S[email protected]                     hci_stack->substate = 0;
11958d213e1aSmatthias.ringwald                     break;
11968d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
11978d213e1aSmatthias.ringwald                     // do nothing
11988d213e1aSmatthias.ringwald                     break;
11998d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1200b546ac54Smatthias.ringwald                     // see hci_run
12013a9fb326S[email protected]                     hci_stack->state = HCI_STATE_FALLING_ASLEEP;
12023a9fb326S[email protected]                     hci_stack->substate = 0;
12038d213e1aSmatthias.ringwald                     break;
12048d213e1aSmatthias.ringwald             }
12058d213e1aSmatthias.ringwald             break;
12068d213e1aSmatthias.ringwald 
12078d213e1aSmatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
12088d213e1aSmatthias.ringwald             switch (power_mode){
12098d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
121028171530Smatthias.ringwald 
121128171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
121228171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
121328171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
12143a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
12153a9fb326S[email protected]                         hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
121628171530Smatthias.ringwald                         break;
121728171530Smatthias.ringwald                     }
121828171530Smatthias.ringwald #endif
1219b546ac54Smatthias.ringwald                     // set up state machine
12203a9fb326S[email protected]                     hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent
12213a9fb326S[email protected]                     hci_stack->state = HCI_STATE_INITIALIZING;
12223a9fb326S[email protected]                     hci_stack->substate = 0;
12238d213e1aSmatthias.ringwald                     break;
12248d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
1225b546ac54Smatthias.ringwald                     // see hci_run
12263a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
12278d213e1aSmatthias.ringwald                     break;
12288d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1229b546ac54Smatthias.ringwald                     // do nothing
12308d213e1aSmatthias.ringwald                     break;
12318d213e1aSmatthias.ringwald             }
12328d213e1aSmatthias.ringwald             break;
12338d213e1aSmatthias.ringwald 
12348d213e1aSmatthias.ringwald         case HCI_STATE_SLEEPING:
12358d213e1aSmatthias.ringwald             switch (power_mode){
12368d213e1aSmatthias.ringwald                 case HCI_POWER_ON:
123728171530Smatthias.ringwald 
123828171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
123928171530Smatthias.ringwald                     // nothing to do, if H4 supports power management
124028171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
12413a9fb326S[email protected]                         hci_stack->state = HCI_STATE_INITIALIZING;
12423a9fb326S[email protected]                         hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP;
1243758b46ceSmatthias.ringwald                         hci_update_scan_enable();
124428171530Smatthias.ringwald                         break;
124528171530Smatthias.ringwald                     }
124628171530Smatthias.ringwald #endif
12473144bce4Smatthias.ringwald                     err = hci_power_control_wake();
12483144bce4Smatthias.ringwald                     if (err) return err;
1249b546ac54Smatthias.ringwald                     // set up state machine
12503a9fb326S[email protected]                     hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent
12513a9fb326S[email protected]                     hci_stack->state = HCI_STATE_INITIALIZING;
12523a9fb326S[email protected]                     hci_stack->substate = 0;
12538d213e1aSmatthias.ringwald                     break;
12548d213e1aSmatthias.ringwald                 case HCI_POWER_OFF:
12553a9fb326S[email protected]                     hci_stack->state = HCI_STATE_HALTING;
12568d213e1aSmatthias.ringwald                     break;
12578d213e1aSmatthias.ringwald                 case HCI_POWER_SLEEP:
1258b546ac54Smatthias.ringwald                     // do nothing
12598d213e1aSmatthias.ringwald                     break;
12608d213e1aSmatthias.ringwald             }
12618d213e1aSmatthias.ringwald             break;
126211e23e5fSmatthias.ringwald     }
126368d92d03Smatthias.ringwald 
1264038bc64cSmatthias.ringwald     // create internal event
1265ee8bf225Smatthias.ringwald 	hci_emit_state();
1266ee8bf225Smatthias.ringwald 
126768d92d03Smatthias.ringwald 	// trigger next/first action
126868d92d03Smatthias.ringwald 	hci_run();
126968d92d03Smatthias.ringwald 
1270475c8125Smatthias.ringwald     return 0;
1271475c8125Smatthias.ringwald }
1272475c8125Smatthias.ringwald 
1273758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){
1274758b46ceSmatthias.ringwald     // 2 = page scan, 1 = inq scan
12753a9fb326S[email protected]     hci_stack->new_scan_enable_value  = hci_stack->connectable << 1 | hci_stack->discoverable;
1276758b46ceSmatthias.ringwald     hci_run();
1277758b46ceSmatthias.ringwald }
1278758b46ceSmatthias.ringwald 
1279381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){
1280381fbed8Smatthias.ringwald     if (enable) enable = 1; // normalize argument
1281381fbed8Smatthias.ringwald 
12823a9fb326S[email protected]     if (hci_stack->discoverable == enable){
12833a9fb326S[email protected]         hci_emit_discoverable_enabled(hci_stack->discoverable);
1284381fbed8Smatthias.ringwald         return;
1285381fbed8Smatthias.ringwald     }
1286381fbed8Smatthias.ringwald 
12873a9fb326S[email protected]     hci_stack->discoverable = enable;
1288758b46ceSmatthias.ringwald     hci_update_scan_enable();
1289758b46ceSmatthias.ringwald }
1290b031bebbSmatthias.ringwald 
1291758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){
1292758b46ceSmatthias.ringwald     if (enable) enable = 1; // normalize argument
1293758b46ceSmatthias.ringwald 
1294758b46ceSmatthias.ringwald     // don't emit event
12953a9fb326S[email protected]     if (hci_stack->connectable == enable) return;
1296758b46ceSmatthias.ringwald 
12973a9fb326S[email protected]     hci_stack->connectable = enable;
1298758b46ceSmatthias.ringwald     hci_update_scan_enable();
1299381fbed8Smatthias.ringwald }
1300381fbed8Smatthias.ringwald 
13015061f3afS[email protected] bd_addr_t * hci_local_bd_addr(void){
13023a9fb326S[email protected]     return &hci_stack->local_bd_addr;
13035061f3afS[email protected] }
13045061f3afS[email protected] 
130506b35ec0Smatthias.ringwald void hci_run(){
13068a485f27Smatthias.ringwald 
130732ab9390Smatthias.ringwald     hci_connection_t * connection;
130832ab9390Smatthias.ringwald     linked_item_t * it;
130932ab9390Smatthias.ringwald 
131064f0b431S[email protected]     if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) return;
1311ce4c8fabSmatthias.ringwald 
1312b031bebbSmatthias.ringwald     // global/non-connection oriented commands
1313b031bebbSmatthias.ringwald 
1314b031bebbSmatthias.ringwald     // decline incoming connections
13153a9fb326S[email protected]     if (hci_stack->decline_reason){
13163a9fb326S[email protected]         uint8_t reason = hci_stack->decline_reason;
13173a9fb326S[email protected]         hci_stack->decline_reason = 0;
13183a9fb326S[email protected]         hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason);
1319dbe1a790S[email protected]         return;
1320ce4c8fabSmatthias.ringwald     }
1321ce4c8fabSmatthias.ringwald 
1322b031bebbSmatthias.ringwald     // send scan enable
13233a9fb326S[email protected]     if (hci_stack->state == HCI_STATE_WORKING && hci_stack->new_scan_enable_value != 0xff && hci_classic_supported()){
13243a9fb326S[email protected]         hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value);
13253a9fb326S[email protected]         hci_stack->new_scan_enable_value = 0xff;
1326dbe1a790S[email protected]         return;
1327b031bebbSmatthias.ringwald     }
1328b031bebbSmatthias.ringwald 
132932ab9390Smatthias.ringwald     // send pending HCI commands
13303a9fb326S[email protected]     for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){
133132ab9390Smatthias.ringwald 
1332b031bebbSmatthias.ringwald         connection = (hci_connection_t *) it;
133332ab9390Smatthias.ringwald 
1334ad83dc6aS[email protected]         if (connection->state == SEND_CREATE_CONNECTION){
1335ad83dc6aS[email protected]             log_info("sending hci_create_connection\n");
1336ad83dc6aS[email protected]             hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1);
1337ad83dc6aS[email protected]             return;
1338ad83dc6aS[email protected]         }
1339ad83dc6aS[email protected] 
134032ab9390Smatthias.ringwald         if (connection->state == RECEIVED_CONNECTION_REQUEST){
13417b5fbe1fSmatthias.ringwald             log_info("sending hci_accept_connection_request\n");
134232ab9390Smatthias.ringwald             connection->state = ACCEPTED_CONNECTION_REQUEST;
134334d2123cS[email protected]             hci_send_cmd(&hci_accept_connection_request, connection->address, 1);
1344dbe1a790S[email protected]             return;
1345c7e0c5f6Smatthias.ringwald         }
1346c7e0c5f6Smatthias.ringwald 
134732ab9390Smatthias.ringwald         if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){
134834d2123cS[email protected]             log_info("responding to link key request\n");
134934d2123cS[email protected]             connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST);
135032ab9390Smatthias.ringwald             link_key_t link_key;
1351c77e8838S[email protected]             link_key_type_t link_key_type;
13523a9fb326S[email protected]             if ( hci_stack->remote_device_db
13533a9fb326S[email protected]               && hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)
135434d2123cS[email protected]               && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){
13559ab95c90S[email protected]                connection->link_key_type = link_key_type;
135632ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key);
135732ab9390Smatthias.ringwald             } else {
135832ab9390Smatthias.ringwald                hci_send_cmd(&hci_link_key_request_negative_reply, connection->address);
135932ab9390Smatthias.ringwald             }
1360dbe1a790S[email protected]             return;
136132ab9390Smatthias.ringwald         }
13621d6b20aeS[email protected] 
1363899283eaS[email protected]         if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){
13644c57c146S[email protected]             log_info("denying to pin request\n");
1365899283eaS[email protected]             connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST);
136634d2123cS[email protected]             hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address);
13674c57c146S[email protected]             return;
13684c57c146S[email protected]         }
13694c57c146S[email protected] 
1370dbe1a790S[email protected]         if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){
137134d2123cS[email protected]             connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY);
137282d8f825S[email protected]             log_info("IO Capability Request received, stack bondable %u, io cap %u", hci_stack->bondable, hci_stack->ssp_io_capability);
137382d8f825S[email protected]             if (hci_stack->bondable && (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN)){
1374106d6d11S[email protected]                 // tweak authentication requirements
13753a9fb326S[email protected]                 uint8_t authreq = hci_stack->ssp_authentication_requirement;
1376106d6d11S[email protected]                 if (connection->bonding_flags & BONDING_DEDICATED){
13779faad3abS[email protected]                     authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING;
13789faad3abS[email protected]                 }
13799faad3abS[email protected]                 if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){
13809faad3abS[email protected]                     authreq |= 1;
1381106d6d11S[email protected]                 }
13823a9fb326S[email protected]                 hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, NULL, authreq);
1383f8fb5f6eS[email protected]             } else {
1384f8fb5f6eS[email protected]                 hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED);
1385f8fb5f6eS[email protected]             }
1386dbe1a790S[email protected]             return;
138732ab9390Smatthias.ringwald         }
138832ab9390Smatthias.ringwald 
1389dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){
1390dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY);
139134d2123cS[email protected]             hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address);
1392dbe1a790S[email protected]             return;
1393dbe1a790S[email protected]         }
1394dbe1a790S[email protected] 
1395dbe1a790S[email protected]         if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){
1396dbe1a790S[email protected]             connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY);
139734d2123cS[email protected]             hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000);
1398dbe1a790S[email protected]             return;
1399dbe1a790S[email protected]         }
1400afd4e962S[email protected] 
1401afd4e962S[email protected]         if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){
1402afd4e962S[email protected]             connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES;
140334d2123cS[email protected]             hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle);
14042bd8b7e7S[email protected]             return;
14052bd8b7e7S[email protected]         }
14062bd8b7e7S[email protected] 
14072bd8b7e7S[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){
14082bd8b7e7S[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK;
140934d2123cS[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005);  // authentication failure
141034d2123cS[email protected]             return;
141134d2123cS[email protected]         }
1412ad83dc6aS[email protected]         if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){
1413ad83dc6aS[email protected]             connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE;
141452d34f98S[email protected]             hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // authentication done
1415ad83dc6aS[email protected]             return;
1416ad83dc6aS[email protected]         }
141734d2123cS[email protected]         if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){
141834d2123cS[email protected]             connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST;
141934d2123cS[email protected]             hci_send_cmd(&hci_authentication_requested, connection->con_handle);
14202bd8b7e7S[email protected]             return;
1421afd4e962S[email protected]         }
1422dce78009S[email protected]         if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){
1423dce78009S[email protected]             connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST;
1424dce78009S[email protected]             hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1);
1425dce78009S[email protected]             return;
1426dce78009S[email protected]         }
1427dbe1a790S[email protected]     }
1428c7e0c5f6Smatthias.ringwald 
14293a9fb326S[email protected]     switch (hci_stack->state){
14303429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
14313a9fb326S[email protected]             // log_info("hci_init: substate %u\n", hci_stack->substate);
14323a9fb326S[email protected]             if (hci_stack->substate % 2) {
14333429f56bSmatthias.ringwald                 // odd: waiting for command completion
143406b35ec0Smatthias.ringwald                 return;
14353429f56bSmatthias.ringwald             }
14363a9fb326S[email protected]             switch (hci_stack->substate >> 1){
1437c7492964Smatthias.ringwald                 case 0: // RESET
143822909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
143924052c2aS[email protected] 
14403a9fb326S[email protected]                     if (hci_stack->config == 0 || ((hci_uart_config_t *)hci_stack->config)->baudrate_main == 0){
1441c7492964Smatthias.ringwald                         // skip baud change
14423a9fb326S[email protected]                         hci_stack->substate = 4; // >> 1 = 2
1443c7492964Smatthias.ringwald                     }
14443429f56bSmatthias.ringwald                     break;
1445c7492964Smatthias.ringwald                 case 1: // SEND BAUD CHANGE
14463a9fb326S[email protected]                     hci_stack->control->baudrate_cmd(hci_stack->config, ((hci_uart_config_t *)hci_stack->config)->baudrate_main, hci_stack->hci_packet_buffer);
14473a9fb326S[email protected]                     hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]);
1448c7492964Smatthias.ringwald                     break;
1449c7492964Smatthias.ringwald                 case 2: // LOCAL BAUD CHANGE
14503a9fb326S[email protected]                     hci_stack->hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack->config)->baudrate_main);
14513a9fb326S[email protected]                     hci_stack->substate += 2;
1452c7492964Smatthias.ringwald                     // break missing here for fall through
1453c7492964Smatthias.ringwald 
1454c7492964Smatthias.ringwald                 case 3:
145524052c2aS[email protected]                     // Custom initialization
14563a9fb326S[email protected]                     if (hci_stack->control && hci_stack->control->next_cmd){
14573a9fb326S[email protected]                         int valid_cmd = (*hci_stack->control->next_cmd)(hci_stack->config, hci_stack->hci_packet_buffer);
1458d62aca9fSmatthias.ringwald                         if (valid_cmd){
14593a9fb326S[email protected]                             int size = 3 + hci_stack->hci_packet_buffer[2];
14603a9fb326S[email protected]                             hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size);
14613a9fb326S[email protected]                             hci_stack->substate = 4; // more init commands
146290919203Smatthias.ringwald                             break;
146390919203Smatthias.ringwald                         }
1464d62aca9fSmatthias.ringwald                         log_info("hci_run: init script done\n\r");
146590919203Smatthias.ringwald                     }
14662f6c30e1Smatthias.ringwald                     // otherwise continue
1467f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
1468f432a6ddSmatthias.ringwald 					break;
1469c7492964Smatthias.ringwald 				case 4:
1470e2edc0c3Smatthias.ringwald 					hci_send_cmd(&hci_read_buffer_size);
1471e2edc0c3Smatthias.ringwald 					break;
1472c7492964Smatthias.ringwald                 case 5:
147365389bfcS[email protected]                     hci_send_cmd(&hci_read_local_supported_features);
14743429f56bSmatthias.ringwald                     break;
1475c7492964Smatthias.ringwald                 case 6:
1476d7e0e3a8S[email protected]                     if (hci_le_supported()){
1477d7e0e3a8S[email protected]                         hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF);
1478d7e0e3a8S[email protected]                     } else {
1479d7e0e3a8S[email protected]                         // Kensington Bluetoot 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff...
1480d7e0e3a8S[email protected]                         hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF);
1481d7e0e3a8S[email protected]                     }
1482f5d8d141S[email protected] 
1483f5d8d141S[email protected]                     // skip Classic init commands for LE only chipsets
148424052c2aS[email protected]                     if (!hci_classic_supported()){
148524052c2aS[email protected]                         if (hci_le_supported()){
14863a9fb326S[email protected]                             hci_stack->substate = 11 << 1;    // skip all classic command
148724052c2aS[email protected]                         } else {
148824052c2aS[email protected]                             log_error("Neither BR/EDR nor LE supported");
14893a9fb326S[email protected]                             hci_stack->substate = 13 << 1;    // skip all
149024052c2aS[email protected]                         }
1491f5d8d141S[email protected]                     }
1492f5d8d141S[email protected]                     break;
1493f5d8d141S[email protected]                 case 7:
149424052c2aS[email protected]                     if (hci_ssp_supported()){
14953a9fb326S[email protected]                         hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable);
1496f5d8d141S[email protected]                         break;
149724052c2aS[email protected]                     }
14983a9fb326S[email protected]                     hci_stack->substate += 2;
149924052c2aS[email protected]                     // break missing here for fall through
150024052c2aS[email protected] 
1501f5d8d141S[email protected]                 case 8:
150265389bfcS[email protected]                     // ca. 15 sec
150365389bfcS[email protected]                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
1504559e517eS[email protected]                     break;
1505f5d8d141S[email protected]                 case 9:
15063a9fb326S[email protected]                     hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device);
1507e2386ba1S[email protected]                     break;
1508f5d8d141S[email protected]                 case 10:
15093a9fb326S[email protected]                     if (hci_stack->local_name){
15103a9fb326S[email protected]                         hci_send_cmd(&hci_write_local_name, hci_stack->local_name);
1511e2386ba1S[email protected]                     } else {
15128a485f27Smatthias.ringwald                         char hostname[30];
1513e2386ba1S[email protected] #ifdef EMBEDDED
1514e2386ba1S[email protected]                         // BTstack-11:22:33:44:55:66
1515e2386ba1S[email protected]                         strcpy(hostname, "BTstack ");
15163a9fb326S[email protected]                         strcat(hostname, bd_addr_to_str(hci_stack->local_bd_addr));
1517e2386ba1S[email protected]                         printf("---> Name %s\n", hostname);
1518e2386ba1S[email protected] #else
1519e2386ba1S[email protected]                         // hostname for POSIX systems
15208a485f27Smatthias.ringwald                         gethostname(hostname, 30);
15218a485f27Smatthias.ringwald                         hostname[29] = '\0';
1522e2386ba1S[email protected] #endif
15238a485f27Smatthias.ringwald                         hci_send_cmd(&hci_write_local_name, hostname);
15248a485f27Smatthias.ringwald                     }
15253c4d4b90Smatthias.ringwald                     break;
1526e0dbca83S[email protected]                 case 11:
15273a9fb326S[email protected] 					hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan
152824052c2aS[email protected]                     if (!hci_le_supported()){
152924052c2aS[email protected]                         // SKIP LE init for Classic only configuration
15303a9fb326S[email protected]                         hci_stack->substate = 13 << 1;
153124052c2aS[email protected]                     }
1532a45d6b9fS[email protected] 					break;
153324052c2aS[email protected] 
153443aa7007S[email protected] #ifdef HAVE_BLE
153524052c2aS[email protected]                 // LE INIT
1536a45d6b9fS[email protected]                 case 12:
153724052c2aS[email protected]                     hci_send_cmd(&hci_le_read_buffer_size);
153824052c2aS[email protected]                     break;
153924052c2aS[email protected]                 case 13:
154024052c2aS[email protected]                     // LE Supported Host = 1, Simultaneous Host = 0
154124052c2aS[email protected]                     hci_send_cmd(&hci_write_le_host_supported, 1, 0);
154224052c2aS[email protected]                     break;
154343aa7007S[email protected] #endif
154424052c2aS[email protected] 
154524052c2aS[email protected]                 // DONE
154624052c2aS[email protected]                 case 14:
15473429f56bSmatthias.ringwald                     // done.
15483a9fb326S[email protected]                     hci_stack->state = HCI_STATE_WORKING;
1549b360b6adSmatthias.ringwald                     hci_emit_state();
15503429f56bSmatthias.ringwald                     break;
15513429f56bSmatthias.ringwald                 default:
15523429f56bSmatthias.ringwald                     break;
1553475c8125Smatthias.ringwald             }
15543a9fb326S[email protected]             hci_stack->substate++;
15553429f56bSmatthias.ringwald             break;
1556c7e0c5f6Smatthias.ringwald 
1557c7e0c5f6Smatthias.ringwald         case HCI_STATE_HALTING:
1558c7e0c5f6Smatthias.ringwald 
15597b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING\n");
1560c7e0c5f6Smatthias.ringwald             // close all open connections
15613a9fb326S[email protected]             connection =  (hci_connection_t *) hci_stack->connections;
1562c7e0c5f6Smatthias.ringwald             if (connection){
156332ab9390Smatthias.ringwald 
1564c7e0c5f6Smatthias.ringwald                 // send disconnect
156564f0b431S[email protected]                 if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) return;
156632ab9390Smatthias.ringwald 
156779bbfa0bSmatthias.ringwald                 log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
15686ad890d3Smatthias.ringwald                 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
1569c7e0c5f6Smatthias.ringwald 
1570c7e0c5f6Smatthias.ringwald                 // send disconnected event right away - causes higher layer connections to get closed, too.
1571c7e0c5f6Smatthias.ringwald                 hci_shutdown_connection(connection);
1572c7e0c5f6Smatthias.ringwald                 return;
1573c7e0c5f6Smatthias.ringwald             }
15747b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, calling off\n");
1575c7e0c5f6Smatthias.ringwald 
157672ea5239Smatthias.ringwald             // switch mode
1577c7e0c5f6Smatthias.ringwald             hci_power_control_off();
15789418f9c9Smatthias.ringwald 
15797b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, emitting state\n");
158072ea5239Smatthias.ringwald             hci_emit_state();
15817b5fbe1fSmatthias.ringwald             log_info("HCI_STATE_HALTING, done\n");
158272ea5239Smatthias.ringwald             break;
1583c7e0c5f6Smatthias.ringwald 
158472ea5239Smatthias.ringwald         case HCI_STATE_FALLING_ASLEEP:
15853a9fb326S[email protected]             switch(hci_stack->substate) {
158689db417bSmatthias.ringwald                 case 0:
15877b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_FALLING_ASLEEP\n");
158872ea5239Smatthias.ringwald                     // close all open connections
15893a9fb326S[email protected]                     connection =  (hci_connection_t *) hci_stack->connections;
159066da7044Smatthias.ringwald 
159128171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
159266da7044Smatthias.ringwald                     // don't close connections, if H4 supports power management
159366da7044Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
159466da7044Smatthias.ringwald                         connection = NULL;
159566da7044Smatthias.ringwald                     }
159666da7044Smatthias.ringwald #endif
159772ea5239Smatthias.ringwald                     if (connection){
159832ab9390Smatthias.ringwald 
159972ea5239Smatthias.ringwald                         // send disconnect
160032ab9390Smatthias.ringwald                         if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return;
160132ab9390Smatthias.ringwald 
160279bbfa0bSmatthias.ringwald                         log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle);
16036ad890d3Smatthias.ringwald                         hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13);  // remote closed connection
160472ea5239Smatthias.ringwald 
160572ea5239Smatthias.ringwald                         // send disconnected event right away - causes higher layer connections to get closed, too.
160672ea5239Smatthias.ringwald                         hci_shutdown_connection(connection);
160772ea5239Smatthias.ringwald                         return;
160872ea5239Smatthias.ringwald                     }
160972ea5239Smatthias.ringwald 
161092368cd3S[email protected]                     if (hci_classic_supported()){
161189db417bSmatthias.ringwald                         // disable page and inquiry scan
161264f0b431S[email protected]                         if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) return;
161332ab9390Smatthias.ringwald 
161492368cd3S[email protected]                         log_info("HCI_STATE_HALTING, disabling inq scans\n");
16153a9fb326S[email protected]                         hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan
161689db417bSmatthias.ringwald 
161789db417bSmatthias.ringwald                         // continue in next sub state
16183a9fb326S[email protected]                         hci_stack->substate++;
161989db417bSmatthias.ringwald                         break;
162092368cd3S[email protected]                     }
162192368cd3S[email protected]                     // fall through for ble-only chips
162292368cd3S[email protected] 
162389db417bSmatthias.ringwald                 case 2:
16247b5fbe1fSmatthias.ringwald                     log_info("HCI_STATE_HALTING, calling sleep\n");
162528171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL)
162628171530Smatthias.ringwald                     // don't actually go to sleep, if H4 supports power management
162728171530Smatthias.ringwald                     if (bt_control_iphone_power_management_enabled()){
162828171530Smatthias.ringwald                         // SLEEP MODE reached
16293a9fb326S[email protected]                         hci_stack->state = HCI_STATE_SLEEPING;
163028171530Smatthias.ringwald                         hci_emit_state();
163128171530Smatthias.ringwald                         break;
163228171530Smatthias.ringwald                     }
163328171530Smatthias.ringwald #endif
163472ea5239Smatthias.ringwald                     // switch mode
16353a9fb326S[email protected]                     hci_power_control_sleep();  // changes hci_stack->state to SLEEP
1636c7e0c5f6Smatthias.ringwald                     hci_emit_state();
163728171530Smatthias.ringwald                     break;
163828171530Smatthias.ringwald 
163989db417bSmatthias.ringwald                 default:
164089db417bSmatthias.ringwald                     break;
164189db417bSmatthias.ringwald             }
1642c7e0c5f6Smatthias.ringwald             break;
1643c7e0c5f6Smatthias.ringwald 
16443429f56bSmatthias.ringwald         default:
16453429f56bSmatthias.ringwald             break;
16461f504dbdSmatthias.ringwald     }
16473429f56bSmatthias.ringwald }
164816833f0aSmatthias.ringwald 
164931452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
1650c8e4258aSmatthias.ringwald     bd_addr_t addr;
1651c8e4258aSmatthias.ringwald     hci_connection_t * conn;
1652c8e4258aSmatthias.ringwald     // house-keeping
1653c8e4258aSmatthias.ringwald 
1654c8e4258aSmatthias.ringwald     // create_connection?
1655c8e4258aSmatthias.ringwald     if (IS_COMMAND(packet, hci_create_connection)){
1656c8e4258aSmatthias.ringwald         bt_flip_addr(addr, &packet[3]);
1657339b6768Smatthias.ringwald         log_info("Create_connection to %s\n", bd_addr_to_str(addr));
1658c8e4258aSmatthias.ringwald 
1659ad83dc6aS[email protected]         conn = connection_for_address(addr);
1660ad83dc6aS[email protected]         if (!conn){
166117f1ba2aSmatthias.ringwald             conn = create_connection_for_addr(addr);
166217f1ba2aSmatthias.ringwald             if (!conn){
166317f1ba2aSmatthias.ringwald                 // notify client that alloc failed
166417f1ba2aSmatthias.ringwald                 hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED);
166517f1ba2aSmatthias.ringwald                 return 0; // don't sent packet to controller
166617f1ba2aSmatthias.ringwald             }
1667ad83dc6aS[email protected]             conn->state = SEND_CREATE_CONNECTION;
1668ad83dc6aS[email protected]         }
1669ad83dc6aS[email protected]         log_info("conn state %u", conn->state);
1670ad83dc6aS[email protected]         switch (conn->state){
1671ad83dc6aS[email protected]             // if connection active exists
1672ad83dc6aS[email protected]             case OPEN:
167362bda3fbS[email protected]                 // and OPEN, emit connection complete command, don't send to controller
1674ad83dc6aS[email protected]                 hci_emit_connection_complete(conn, 0);
167562bda3fbS[email protected]                 return 0;
1676ad83dc6aS[email protected]             case SEND_CREATE_CONNECTION:
1677ad83dc6aS[email protected]                 // connection created by hci, e.g. dedicated bonding
1678ad83dc6aS[email protected]                 break;
1679ad83dc6aS[email protected]             default:
1680ad83dc6aS[email protected]                 // otherwise, just ignore as it is already in the open process
1681ad83dc6aS[email protected]                 return 0;
1682ad83dc6aS[email protected]         }
1683c8e4258aSmatthias.ringwald         conn->state = SENT_CREATE_CONNECTION;
1684c8e4258aSmatthias.ringwald     }
1685c8e4258aSmatthias.ringwald 
16867fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_reply)){
16877fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY);
16887fde4af9Smatthias.ringwald     }
16897fde4af9Smatthias.ringwald     if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){
16907fde4af9Smatthias.ringwald         hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST);
16917fde4af9Smatthias.ringwald     }
16927fde4af9Smatthias.ringwald 
16938ef73945Smatthias.ringwald     if (IS_COMMAND(packet, hci_delete_stored_link_key)){
16943a9fb326S[email protected]         if (hci_stack->remote_device_db){
16958ef73945Smatthias.ringwald             bt_flip_addr(addr, &packet[3]);
16963a9fb326S[email protected]             hci_stack->remote_device_db->delete_link_key(&addr);
16978ef73945Smatthias.ringwald         }
16988ef73945Smatthias.ringwald     }
1699c8e4258aSmatthias.ringwald 
17006724cd9eS[email protected]     if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)
17016724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_pin_code_request_reply)){
17026724cd9eS[email protected]         bt_flip_addr(addr, &packet[3]);
17036724cd9eS[email protected]         conn = connection_for_address(addr);
17046724cd9eS[email protected]         if (conn){
17056724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE);
17066724cd9eS[email protected]         }
17076724cd9eS[email protected]     }
17086724cd9eS[email protected] 
17096724cd9eS[email protected]     if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply)
17106724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_confirmation_request_reply)
17116724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_negative_reply)
17126724cd9eS[email protected]     ||  IS_COMMAND(packet, hci_user_passkey_request_reply)) {
17136724cd9eS[email protected]         bt_flip_addr(addr, &packet[3]);
17146724cd9eS[email protected]         conn = connection_for_address(addr);
17156724cd9eS[email protected]         if (conn){
17166724cd9eS[email protected]             connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE);
17176724cd9eS[email protected]         }
17186724cd9eS[email protected]     }
17196724cd9eS[email protected] 
172069a97523S[email protected] #ifdef HAVE_BLE
172169a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_advertising_parameters)){
17223a9fb326S[email protected]         hci_stack->adv_addr_type = packet[8];
172369a97523S[email protected]     }
172469a97523S[email protected]     if (IS_COMMAND(packet, hci_le_set_random_address)){
17253a9fb326S[email protected]         bt_flip_addr(hci_stack->adv_address, &packet[3]);
172669a97523S[email protected]     }
172769a97523S[email protected] #endif
172869a97523S[email protected] 
17293a9fb326S[email protected]     hci_stack->num_cmd_packets--;
17306b4af23dS[email protected]     int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size);
17316b4af23dS[email protected] 
17326b4af23dS[email protected]     // free packet buffer for synchronous transport implementations
1733c8b9416aS[email protected]     if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){
17346b4af23dS[email protected]         hci_stack->hci_packet_buffer_reserved = 0;
17356b4af23dS[email protected]     }
17366b4af23dS[email protected] 
17376b4af23dS[email protected]     return err;
173831452debSmatthias.ringwald }
17398adf0ddaSmatthias.ringwald 
17402bd8b7e7S[email protected] // disconnect because of security block
17412bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){
17422bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
17432bd8b7e7S[email protected]     if (!connection) return;
17442bd8b7e7S[email protected]     connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK;
17452bd8b7e7S[email protected] }
17462bd8b7e7S[email protected] 
17472bd8b7e7S[email protected] 
1748dbe1a790S[email protected] // Configure Secure Simple Pairing
1749dbe1a790S[email protected] 
1750dbe1a790S[email protected] // enable will enable SSP during init
1751dbe1a790S[email protected] void hci_ssp_set_enable(int enable){
17523a9fb326S[email protected]     hci_stack->ssp_enable = enable;
1753dbe1a790S[email protected] }
1754dbe1a790S[email protected] 
17552bd8b7e7S[email protected] int hci_local_ssp_activated(){
17563a9fb326S[email protected]     return hci_ssp_supported() && hci_stack->ssp_enable;
17572bd8b7e7S[email protected] }
17582bd8b7e7S[email protected] 
1759dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement
1760dbe1a790S[email protected] void hci_ssp_set_io_capability(int io_capability){
17613a9fb326S[email protected]     hci_stack->ssp_io_capability = io_capability;
1762dbe1a790S[email protected] }
1763dbe1a790S[email protected] void hci_ssp_set_authentication_requirement(int authentication_requirement){
17643a9fb326S[email protected]     hci_stack->ssp_authentication_requirement = authentication_requirement;
1765dbe1a790S[email protected] }
1766dbe1a790S[email protected] 
1767dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested
1768dbe1a790S[email protected] void hci_ssp_set_auto_accept(int auto_accept){
17693a9fb326S[email protected]     hci_stack->ssp_auto_accept = auto_accept;
1770dbe1a790S[email protected] }
1771dbe1a790S[email protected] 
17721cd208adSmatthias.ringwald /**
17731cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
17741cd208adSmatthias.ringwald  */
1775fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){
17761cd208adSmatthias.ringwald     va_list argptr;
17771cd208adSmatthias.ringwald     va_start(argptr, cmd);
17783a9fb326S[email protected]     uint16_t size = hci_create_cmd_internal(hci_stack->hci_packet_buffer, cmd, argptr);
17791cd208adSmatthias.ringwald     va_end(argptr);
17803a9fb326S[email protected]     return hci_send_cmd_packet(hci_stack->hci_packet_buffer, size);
178193b8dc03Smatthias.ringwald }
1782c8e4258aSmatthias.ringwald 
1783ee091cf1Smatthias.ringwald // Create various non-HCI events.
1784ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command
1785ee091cf1Smatthias.ringwald 
1786c8e4258aSmatthias.ringwald void hci_emit_state(){
17873a9fb326S[email protected]     log_info("BTSTACK_EVENT_STATE %u", hci_stack->state);
1788425d1371Smatthias.ringwald     uint8_t event[3];
178980d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_STATE;
1790425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
17913a9fb326S[email protected]     event[2] = hci_stack->state;
1792425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
17933a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1794c8e4258aSmatthias.ringwald }
1795c8e4258aSmatthias.ringwald 
179617f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){
1797425d1371Smatthias.ringwald     uint8_t event[13];
1798c8e4258aSmatthias.ringwald     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
1799425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
180017f1ba2aSmatthias.ringwald     event[2] = status;
1801c8e4258aSmatthias.ringwald     bt_store_16(event, 3, conn->con_handle);
1802c8e4258aSmatthias.ringwald     bt_flip_addr(&event[5], conn->address);
1803c8e4258aSmatthias.ringwald     event[11] = 1; // ACL connection
1804c8e4258aSmatthias.ringwald     event[12] = 0; // encryption disabled
1805425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18063a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1807c8e4258aSmatthias.ringwald }
1808c8e4258aSmatthias.ringwald 
18093c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
1810425d1371Smatthias.ringwald     uint8_t event[6];
18113c4d4b90Smatthias.ringwald     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
1812e518c4b8Smatthias.ringwald     event[1] = sizeof(event) - 2;
18133c4d4b90Smatthias.ringwald     event[2] = 0; // status = OK
18143c4d4b90Smatthias.ringwald     bt_store_16(event, 3, handle);
18153c4d4b90Smatthias.ringwald     event[5] = reason;
1816425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18173a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
18183c4d4b90Smatthias.ringwald }
18193c4d4b90Smatthias.ringwald 
1820ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){
182166fb9560S[email protected]     if (disable_l2cap_timeouts) return;
1822e0abb8e7S[email protected]     log_info("L2CAP_EVENT_TIMEOUT_CHECK");
1823425d1371Smatthias.ringwald     uint8_t event[4];
182480d52d6bSmatthias.ringwald     event[0] = L2CAP_EVENT_TIMEOUT_CHECK;
1825425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1826ee091cf1Smatthias.ringwald     bt_store_16(event, 2, conn->con_handle);
1827425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18283a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1829ee091cf1Smatthias.ringwald }
183043bfb1bdSmatthias.ringwald 
183143bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){
1832e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections());
1833425d1371Smatthias.ringwald     uint8_t event[3];
183480d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED;
1835425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
183643bfb1bdSmatthias.ringwald     event[2] = nr_hci_connections();
1837425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18383a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
183943bfb1bdSmatthias.ringwald }
1840038bc64cSmatthias.ringwald 
1841038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){
1842e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_POWERON_FAILED");
1843425d1371Smatthias.ringwald     uint8_t event[2];
184480d52d6bSmatthias.ringwald     event[0] = BTSTACK_EVENT_POWERON_FAILED;
1845425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1846425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18473a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1848038bc64cSmatthias.ringwald }
18491b0e3922Smatthias.ringwald 
185009ba8edeSmatthias.ringwald #ifndef EMBEDDED
18511b0e3922Smatthias.ringwald void hci_emit_btstack_version() {
1852e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR);
1853425d1371Smatthias.ringwald     uint8_t event[6];
18541b0e3922Smatthias.ringwald     event[0] = BTSTACK_EVENT_VERSION;
1855425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1856425d1371Smatthias.ringwald     event[2] = BTSTACK_MAJOR;
1857425d1371Smatthias.ringwald     event[3] = BTSTACK_MINOR;
1858425d1371Smatthias.ringwald     bt_store_16(event, 4, BTSTACK_REVISION);
1859425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18603a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
18611b0e3922Smatthias.ringwald }
186209ba8edeSmatthias.ringwald #endif
18631b0e3922Smatthias.ringwald 
18642ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){
1865e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled);
1866425d1371Smatthias.ringwald     uint8_t event[3];
18672ed6235cSmatthias.ringwald     event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED;
1868425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
18692ed6235cSmatthias.ringwald     event[2] = enabled;
1870425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18713a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
18722ed6235cSmatthias.ringwald }
1873627c2f45Smatthias.ringwald 
1874627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){
1875e0abb8e7S[email protected]     uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info
1876627c2f45Smatthias.ringwald     event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED;
1877e0abb8e7S[email protected]     event[1] = sizeof(event) - 2 - 1;
1878f653b6bdSmatthias.ringwald     event[2] = 0;   // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE
18792f89d309Smatthias.ringwald     bt_flip_addr(&event[3], *addr);
1880f653b6bdSmatthias.ringwald     memcpy(&event[9], name, 248);
1881e0abb8e7S[email protected] 
1882e0abb8e7S[email protected]     event[9+248] = 0;   // assert \0 for log_info
1883e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]);
1884e0abb8e7S[email protected] 
1885e0abb8e7S[email protected]     hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1);
18863a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1);
1887627c2f45Smatthias.ringwald }
1888381fbed8Smatthias.ringwald 
1889381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){
1890e0abb8e7S[email protected]     log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled);
1891425d1371Smatthias.ringwald     uint8_t event[3];
1892381fbed8Smatthias.ringwald     event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED;
1893425d1371Smatthias.ringwald     event[1] = sizeof(event) - 2;
1894381fbed8Smatthias.ringwald     event[2] = enabled;
1895425d1371Smatthias.ringwald     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
18963a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1897381fbed8Smatthias.ringwald }
1898458bf4e8S[email protected] 
1899a00031e2S[email protected] void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){
1900df3354fcS[email protected]     log_info("hci_emit_security_level %u for handle %x", level, con_handle);
1901a00031e2S[email protected]     uint8_t event[5];
1902e00caf9cS[email protected]     int pos = 0;
1903a00031e2S[email protected]     event[pos++] = GAP_SECURITY_LEVEL;
1904e00caf9cS[email protected]     event[pos++] = sizeof(event) - 2;
1905a00031e2S[email protected]     bt_store_16(event, 2, con_handle);
1906e00caf9cS[email protected]     pos += 2;
1907e00caf9cS[email protected]     event[pos++] = level;
1908e00caf9cS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
19093a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1910e00caf9cS[email protected] }
1911e00caf9cS[email protected] 
1912ad83dc6aS[email protected] void hci_emit_dedicated_bonding_result(hci_connection_t * connection, uint8_t status){
1913ad83dc6aS[email protected]     log_info("hci_emit_dedicated_bonding_result %u ", status);
1914ad83dc6aS[email protected]     uint8_t event[9];
1915ad83dc6aS[email protected]     int pos = 0;
1916ad83dc6aS[email protected]     event[pos++] = GAP_DEDICATED_BONDING_COMPLETED;
1917ad83dc6aS[email protected]     event[pos++] = sizeof(event) - 2;
1918ad83dc6aS[email protected]     event[pos++] = status;
1919ad83dc6aS[email protected]     bt_flip_addr( * (bd_addr_t *) &event[pos], connection->address);
1920ad83dc6aS[email protected]     pos += 6;
1921ad83dc6aS[email protected]     hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
19223a9fb326S[email protected]     hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event));
1923ad83dc6aS[email protected] }
1924ad83dc6aS[email protected] 
19252bd8b7e7S[email protected] // query if remote side supports SSP
19262bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){
19272bd8b7e7S[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
19282bd8b7e7S[email protected]     if (!connection) return 0;
19292bd8b7e7S[email protected]     return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0;
19302bd8b7e7S[email protected] }
19312bd8b7e7S[email protected] 
1932df3354fcS[email protected] int hci_ssp_supported_on_both_sides(hci_con_handle_t handle){
1933df3354fcS[email protected]     return hci_local_ssp_activated() && hci_remote_ssp_supported(handle);
1934df3354fcS[email protected] }
1935df3354fcS[email protected] 
1936458bf4e8S[email protected] // GAP API
1937458bf4e8S[email protected] /**
1938458bf4e8S[email protected]  * @bbrief enable/disable bonding. default is enabled
1939458bf4e8S[email protected]  * @praram enabled
1940458bf4e8S[email protected]  */
19414c57c146S[email protected] void gap_set_bondable_mode(int enable){
19423a9fb326S[email protected]     hci_stack->bondable = enable ? 1 : 0;
1943458bf4e8S[email protected] }
1944cb230b9dS[email protected] 
1945cb230b9dS[email protected] /**
194634d2123cS[email protected]  * @brief map link keys to security levels
1947cb230b9dS[email protected]  */
194834d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){
194934d2123cS[email protected]     switch (link_key_type){
19503c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256:
19513c68dfa9S[email protected]             return LEVEL_4;
19523c68dfa9S[email protected]         case COMBINATION_KEY:
19533c68dfa9S[email protected]         case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192:
19543c68dfa9S[email protected]             return LEVEL_3;
19553c68dfa9S[email protected]         default:
19563c68dfa9S[email protected]             return LEVEL_2;
19573c68dfa9S[email protected]     }
1958cb230b9dS[email protected] }
1959cb230b9dS[email protected] 
1960a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){
196134d2123cS[email protected]     if (!connection) return LEVEL_0;
196234d2123cS[email protected]     if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0;
196334d2123cS[email protected]     return gap_security_level_for_link_key_type(connection->link_key_type);
196434d2123cS[email protected] }
196534d2123cS[email protected] 
196634d2123cS[email protected] 
1967106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){
196882d8f825S[email protected]     printf("gap_mitm_protection_required_for_security_level %u\n", level);
1969106d6d11S[email protected]     return level > LEVEL_2;
1970106d6d11S[email protected] }
1971106d6d11S[email protected] 
197234d2123cS[email protected] /**
197334d2123cS[email protected]  * @brief get current security level
197434d2123cS[email protected]  */
197534d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){
197634d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
197734d2123cS[email protected]     if (!connection) return LEVEL_0;
197834d2123cS[email protected]     return gap_security_level_for_connection(connection);
197934d2123cS[email protected] }
198034d2123cS[email protected] 
1981cb230b9dS[email protected] /**
1982cb230b9dS[email protected]  * @brief request connection to device to
1983cb230b9dS[email protected]  * @result GAP_AUTHENTICATION_RESULT
1984cb230b9dS[email protected]  */
198534d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){
198634d2123cS[email protected]     hci_connection_t * connection = hci_connection_for_handle(con_handle);
198734d2123cS[email protected]     if (!connection){
1988a00031e2S[email protected]         hci_emit_security_level(con_handle, LEVEL_0);
198934d2123cS[email protected]         return;
199034d2123cS[email protected]     }
199134d2123cS[email protected]     gap_security_level_t current_level = gap_security_level(con_handle);
199234d2123cS[email protected]     log_info("gap_request_security_level %u, current level %u", requested_level, current_level);
199334d2123cS[email protected]     if (current_level >= requested_level){
1994a00031e2S[email protected]         hci_emit_security_level(con_handle, current_level);
199534d2123cS[email protected]         return;
199634d2123cS[email protected]     }
1997a00031e2S[email protected] 
199834d2123cS[email protected]     connection->requested_security_level = requested_level;
1999a00031e2S[email protected] 
2000fb8ba0dbS[email protected]     // would enabling ecnryption suffice (>= LEVEL_2)?
20013a9fb326S[email protected]     if (hci_stack->remote_device_db){
2002a00031e2S[email protected]         link_key_type_t link_key_type;
2003a00031e2S[email protected]         link_key_t      link_key;
20043a9fb326S[email protected]         if (hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)){
2005a00031e2S[email protected]             if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){
2006a00031e2S[email protected]                 connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST;
2007a00031e2S[email protected]                 return;
2008a00031e2S[email protected]             }
2009a00031e2S[email protected]         }
2010a00031e2S[email protected]     }
2011a00031e2S[email protected] 
20121eb2563eS[email protected]     // try to authenticate connection
20131eb2563eS[email protected]     connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST;
2014e80b2cf9S[email protected]     hci_run();
2015e00caf9cS[email protected] }
2016ad83dc6aS[email protected] 
2017ad83dc6aS[email protected] /**
2018ad83dc6aS[email protected]  * @brief start dedicated bonding with device. disconnect after bonding
2019ad83dc6aS[email protected]  * @param device
2020ad83dc6aS[email protected]  * @param request MITM protection
2021ad83dc6aS[email protected]  * @result GAP_DEDICATED_BONDING_COMPLETE
2022ad83dc6aS[email protected]  */
2023ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){
2024ad83dc6aS[email protected] 
2025ad83dc6aS[email protected]     // create connection state machine
2026ad83dc6aS[email protected]     hci_connection_t * connection = create_connection_for_addr(device);
2027ad83dc6aS[email protected] 
2028ad83dc6aS[email protected]     if (!connection){
2029ad83dc6aS[email protected]         return BTSTACK_MEMORY_ALLOC_FAILED;
2030ad83dc6aS[email protected]     }
2031ad83dc6aS[email protected] 
2032ad83dc6aS[email protected]     // delete linkn key
2033ad83dc6aS[email protected]     hci_drop_link_key_for_bd_addr( (bd_addr_t *) &device);
2034ad83dc6aS[email protected] 
2035ad83dc6aS[email protected]     // configure LEVEL_2/3, dedicated bonding
2036ad83dc6aS[email protected]     connection->state = SEND_CREATE_CONNECTION;
2037ad83dc6aS[email protected]     connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2;
203882d8f825S[email protected]     printf("gap_dedicated_bonding, mitm %u -> level %u\n", mitm_protection_required, connection->requested_security_level);
2039ad83dc6aS[email protected]     connection->bonding_flags = BONDING_DEDICATED;
2040ad83dc6aS[email protected] 
2041ad83dc6aS[email protected]     // wait for GAP Security Result and send GAP Dedicated Bonding complete
2042ad83dc6aS[email protected] 
2043ad83dc6aS[email protected]     // handle: connnection failure (connection complete != ok)
2044ad83dc6aS[email protected]     // handle: authentication failure
2045ad83dc6aS[email protected]     // handle: disconnect on done
2046ad83dc6aS[email protected] 
2047ad83dc6aS[email protected]     hci_run();
2048ad83dc6aS[email protected] 
2049ad83dc6aS[email protected]     return 0;
2050ad83dc6aS[email protected] }
20518e618f72S[email protected] 
20528e618f72S[email protected] void gap_set_local_name(const char * local_name){
20538e618f72S[email protected]     hci_stack->local_name = local_name;
20548e618f72S[email protected] }
20558e618f72S[email protected] 
20568e618f72S[email protected] 
2057