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); 7496a45072S[email protected] static void hci_connection_timeout_handler(timer_source_t *timer); 7596a45072S[email protected] static void hci_connection_timestamp(hci_connection_t *connection); 76758b46ceSmatthias.ringwald 7706b35ec0Smatthias.ringwald // the STACK is here 783a9fb326S[email protected] #ifndef HAVE_MALLOC 793a9fb326S[email protected] static hci_stack_t hci_stack_static; 803a9fb326S[email protected] #endif 813a9fb326S[email protected] static hci_stack_t * hci_stack = NULL; 8216833f0aSmatthias.ringwald 8366fb9560S[email protected] // test helper 8466fb9560S[email protected] static uint8_t disable_l2cap_timeouts = 0; 8566fb9560S[email protected] 8696a45072S[email protected] /** 8796a45072S[email protected] * create connection for given address 8896a45072S[email protected] * 8996a45072S[email protected] * @return connection OR NULL, if no memory left 9096a45072S[email protected] */ 9196a45072S[email protected] static hci_connection_t * create_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){ 9296a45072S[email protected] 9396a45072S[email protected] printf("create_connection_for_addr %s\n", bd_addr_to_str(addr)); 9496a45072S[email protected] hci_connection_t * conn = (hci_connection_t *) btstack_memory_hci_connection_get(); 9596a45072S[email protected] if (!conn) return NULL; 9696a45072S[email protected] BD_ADDR_COPY(conn->address, addr); 9796a45072S[email protected] conn->address_type = addr_type; 9896a45072S[email protected] conn->con_handle = 0xffff; 9996a45072S[email protected] conn->authentication_flags = AUTH_FLAGS_NONE; 10096a45072S[email protected] conn->bonding_flags = 0; 10196a45072S[email protected] conn->requested_security_level = LEVEL_0; 10296a45072S[email protected] linked_item_set_user(&conn->timeout.item, conn); 10396a45072S[email protected] conn->timeout.process = hci_connection_timeout_handler; 10496a45072S[email protected] hci_connection_timestamp(conn); 10596a45072S[email protected] conn->acl_recombination_length = 0; 10696a45072S[email protected] conn->acl_recombination_pos = 0; 10796a45072S[email protected] conn->num_acl_packets_sent = 0; 10896a45072S[email protected] linked_list_add(&hci_stack->connections, (linked_item_t *) conn); 10996a45072S[email protected] return conn; 11096a45072S[email protected] } 11166fb9560S[email protected] 11297addcc5Smatthias.ringwald /** 113ee091cf1Smatthias.ringwald * get connection for a given handle 114ee091cf1Smatthias.ringwald * 115ee091cf1Smatthias.ringwald * @return connection OR NULL, if not found 116ee091cf1Smatthias.ringwald */ 1175061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){ 118ee091cf1Smatthias.ringwald linked_item_t *it; 1193a9fb326S[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){ 120ee091cf1Smatthias.ringwald if ( ((hci_connection_t *) it)->con_handle == con_handle){ 121ee091cf1Smatthias.ringwald return (hci_connection_t *) it; 122ee091cf1Smatthias.ringwald } 123ee091cf1Smatthias.ringwald } 124ee091cf1Smatthias.ringwald return NULL; 125ee091cf1Smatthias.ringwald } 126ee091cf1Smatthias.ringwald 12796a45072S[email protected] /** 12896a45072S[email protected] * get connection for given address 12996a45072S[email protected] * 13096a45072S[email protected] * @return connection OR NULL, if not found 13196a45072S[email protected] */ 13296a45072S[email protected] hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t * addr, bd_addr_type_t addr_type){ 13362bda3fbS[email protected] linked_item_t *it; 13462bda3fbS[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){ 13562bda3fbS[email protected] hci_connection_t * connection = (hci_connection_t *) it; 13696a45072S[email protected] if (connection->address_type != addr_type) continue; 13796a45072S[email protected] if (memcmp(addr, connection->address, 6) != 0) continue; 13862bda3fbS[email protected] return connection; 13962bda3fbS[email protected] } 14062bda3fbS[email protected] return NULL; 14162bda3fbS[email protected] } 14262bda3fbS[email protected] 1432b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){ 14428ca2b46S[email protected] hci_connection_t * connection = (hci_connection_t *) linked_item_get_user(&timer->item); 145c785ef68Smatthias.ringwald #ifdef HAVE_TIME 146ee091cf1Smatthias.ringwald struct timeval tv; 147ee091cf1Smatthias.ringwald gettimeofday(&tv, NULL); 148c21e6239Smatthias.ringwald if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) { 149ee091cf1Smatthias.ringwald // connections might be timed out 150ee091cf1Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 151ee091cf1Smatthias.ringwald } 1522b12a0b9Smatthias.ringwald #endif 153e5780900Smatthias.ringwald #ifdef HAVE_TICK 154c785ef68Smatthias.ringwald if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){ 155c785ef68Smatthias.ringwald // connections might be timed out 156c785ef68Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 157c785ef68Smatthias.ringwald } 158c785ef68Smatthias.ringwald #endif 159c785ef68Smatthias.ringwald run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS); 160c785ef68Smatthias.ringwald run_loop_add_timer(timer); 161c785ef68Smatthias.ringwald } 162ee091cf1Smatthias.ringwald 163ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){ 164c7492964Smatthias.ringwald #ifdef HAVE_TIME 165ee091cf1Smatthias.ringwald gettimeofday(&connection->timestamp, NULL); 166c7492964Smatthias.ringwald #endif 167e5780900Smatthias.ringwald #ifdef HAVE_TICK 168c785ef68Smatthias.ringwald connection->timestamp = embedded_get_ticks(); 169c785ef68Smatthias.ringwald #endif 170ee091cf1Smatthias.ringwald } 171ee091cf1Smatthias.ringwald 17206b35ec0Smatthias.ringwald 17328ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 17428ca2b46S[email protected] conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags); 17528ca2b46S[email protected] } 17628ca2b46S[email protected] 17728ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 17828ca2b46S[email protected] conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags); 17928ca2b46S[email protected] } 18028ca2b46S[email protected] 18128ca2b46S[email protected] 18243bfb1bdSmatthias.ringwald /** 18380ca58a0Smatthias.ringwald * add authentication flags and reset timer 18496a45072S[email protected] * @note: assumes classic connection 1857fde4af9Smatthias.ringwald */ 1867fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){ 1877fde4af9Smatthias.ringwald bd_addr_t addr; 1887fde4af9Smatthias.ringwald bt_flip_addr(addr, *(bd_addr_t *) bd_addr); 18996a45072S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 1907fde4af9Smatthias.ringwald if (conn) { 19128ca2b46S[email protected] connectionSetAuthenticationFlags(conn, flags); 19280ca58a0Smatthias.ringwald hci_connection_timestamp(conn); 1937fde4af9Smatthias.ringwald } 1947fde4af9Smatthias.ringwald } 1957fde4af9Smatthias.ringwald 19680ca58a0Smatthias.ringwald int hci_authentication_active_for_handle(hci_con_handle_t handle){ 1975061f3afS[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 19880ca58a0Smatthias.ringwald if (!conn) return 0; 1996724cd9eS[email protected] if (conn->authentication_flags & LEGACY_PAIRING_ACTIVE) return 1; 2006724cd9eS[email protected] if (conn->authentication_flags & SSP_PAIRING_ACTIVE) return 1; 2016724cd9eS[email protected] return 0; 20280ca58a0Smatthias.ringwald } 20380ca58a0Smatthias.ringwald 204c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){ 2053a9fb326S[email protected] if (hci_stack->remote_device_db) { 2063a9fb326S[email protected] hci_stack->remote_device_db->delete_link_key(addr); 207c12e46e7Smatthias.ringwald } 208c12e46e7Smatthias.ringwald } 209c12e46e7Smatthias.ringwald 2107fde4af9Smatthias.ringwald 2117fde4af9Smatthias.ringwald /** 21243bfb1bdSmatthias.ringwald * count connections 21343bfb1bdSmatthias.ringwald */ 21440d1c7a4Smatthias.ringwald static int nr_hci_connections(void){ 21556c253c9Smatthias.ringwald int count = 0; 21643bfb1bdSmatthias.ringwald linked_item_t *it; 2173a9fb326S[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next, count++); 21843bfb1bdSmatthias.ringwald return count; 21943bfb1bdSmatthias.ringwald } 220c8e4258aSmatthias.ringwald 22197addcc5Smatthias.ringwald /** 222ba681a6cSmatthias.ringwald * Dummy handler called by HCI 22316833f0aSmatthias.ringwald */ 2242718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 22516833f0aSmatthias.ringwald } 22616833f0aSmatthias.ringwald 227998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){ 2285061f3afS[email protected] hci_connection_t * connection = hci_connection_for_handle(handle); 229998906cdSmatthias.ringwald if (!connection) { 2307d67539fSmatthias.ringwald log_error("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle); 231998906cdSmatthias.ringwald return 0; 232998906cdSmatthias.ringwald } 233998906cdSmatthias.ringwald return connection->num_acl_packets_sent; 234998906cdSmatthias.ringwald } 235998906cdSmatthias.ringwald 236998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){ 2373a9fb326S[email protected] uint8_t free_slots = hci_stack->total_num_acl_packets; 238998906cdSmatthias.ringwald linked_item_t *it; 2393a9fb326S[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){ 240998906cdSmatthias.ringwald hci_connection_t * connection = (hci_connection_t *) it; 241998906cdSmatthias.ringwald if (free_slots < connection->num_acl_packets_sent) { 2427d67539fSmatthias.ringwald log_error("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n"); 243998906cdSmatthias.ringwald return 0; 244998906cdSmatthias.ringwald } 245998906cdSmatthias.ringwald free_slots -= connection->num_acl_packets_sent; 246998906cdSmatthias.ringwald } 247998906cdSmatthias.ringwald return free_slots; 248998906cdSmatthias.ringwald } 249998906cdSmatthias.ringwald 250c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){ 2512b12a0b9Smatthias.ringwald 2522b12a0b9Smatthias.ringwald // check for async hci transport implementations 2533a9fb326S[email protected] if (hci_stack->hci_transport->can_send_packet_now){ 2543a9fb326S[email protected] if (!hci_stack->hci_transport->can_send_packet_now(packet_type)){ 2552b12a0b9Smatthias.ringwald return 0; 2562b12a0b9Smatthias.ringwald } 2572b12a0b9Smatthias.ringwald } 2582b12a0b9Smatthias.ringwald 2592b12a0b9Smatthias.ringwald // check regular Bluetooth flow control 260c24735b1Smatthias.ringwald switch (packet_type) { 261c24735b1Smatthias.ringwald case HCI_ACL_DATA_PACKET: 262c24735b1Smatthias.ringwald return hci_number_free_acl_slots(); 263c24735b1Smatthias.ringwald case HCI_COMMAND_DATA_PACKET: 2643a9fb326S[email protected] return hci_stack->num_cmd_packets; 265c24735b1Smatthias.ringwald default: 266c24735b1Smatthias.ringwald return 0; 267c24735b1Smatthias.ringwald } 268c24735b1Smatthias.ringwald } 269c24735b1Smatthias.ringwald 2706b4af23dS[email protected] // same as hci_can_send_packet_now, but also checks if packet buffer is free for use 2716b4af23dS[email protected] int hci_can_send_packet_now_using_packet_buffer(uint8_t packet_type){ 2726b4af23dS[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 2736b4af23dS[email protected] return hci_can_send_packet_now(packet_type); 2746b4af23dS[email protected] } 2756b4af23dS[email protected] 276c8b9416aS[email protected] // used for internal checks in l2cap[-le].c 277c8b9416aS[email protected] int hci_is_packet_buffer_reserved(void){ 278c8b9416aS[email protected] return hci_stack->hci_packet_buffer_reserved; 279c8b9416aS[email protected] } 280c8b9416aS[email protected] 2816b4af23dS[email protected] // reserves outgoing packet buffer. @returns 1 if successful 2826b4af23dS[email protected] int hci_reserve_packet_buffer(void){ 2836b4af23dS[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 2846b4af23dS[email protected] hci_stack->hci_packet_buffer_reserved = 1; 2856b4af23dS[email protected] return 1; 2866b4af23dS[email protected] } 2876b4af23dS[email protected] 28868a0fcf7S[email protected] void hci_release_packet_buffer(void){ 28968a0fcf7S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 29068a0fcf7S[email protected] } 29168a0fcf7S[email protected] 2926b4af23dS[email protected] // assumption: synchronous implementations don't provide can_send_packet_now as they don't keep the buffer after the call 2936b4af23dS[email protected] int hci_transport_synchronous(void){ 2946b4af23dS[email protected] return hci_stack->hci_transport->can_send_packet_now == NULL; 2956b4af23dS[email protected] } 2966b4af23dS[email protected] 297ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){ 2987856c818Smatthias.ringwald 2996218e6f1Smatthias.ringwald // check for free places on BT module 3005932f1b4Smatthias.ringwald if (!hci_number_free_acl_slots()) return BTSTACK_ACL_BUFFERS_FULL; 3016218e6f1Smatthias.ringwald 3027856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 3035061f3afS[email protected] hci_connection_t *connection = hci_connection_for_handle( con_handle); 30456cf178bSmatthias.ringwald if (!connection) return 0; 30556cf178bSmatthias.ringwald hci_connection_timestamp(connection); 30656cf178bSmatthias.ringwald 30756cf178bSmatthias.ringwald // count packet 30856cf178bSmatthias.ringwald connection->num_acl_packets_sent++; 3097b5fbe1fSmatthias.ringwald // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent); 3107856c818Smatthias.ringwald 31100d8e42eSmatthias.ringwald // send packet 3123a9fb326S[email protected] int err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size); 3136218e6f1Smatthias.ringwald 3146b4af23dS[email protected] // free packet buffer for synchronous transport implementations 315c8b9416aS[email protected] if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){ 3166b4af23dS[email protected] hci_stack->hci_packet_buffer_reserved = 0; 3176b4af23dS[email protected] } 3186b4af23dS[email protected] 31900d8e42eSmatthias.ringwald return err; 320ee091cf1Smatthias.ringwald } 321ee091cf1Smatthias.ringwald 32216833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){ 3237856c818Smatthias.ringwald 324e76a89eeS[email protected] // log_info("acl_handler: size %u", size); 325e76a89eeS[email protected] 3267856c818Smatthias.ringwald // get info 3277856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 3285061f3afS[email protected] hci_connection_t *conn = hci_connection_for_handle(con_handle); 3297856c818Smatthias.ringwald uint8_t acl_flags = READ_ACL_FLAGS(packet); 3307856c818Smatthias.ringwald uint16_t acl_length = READ_ACL_LENGTH(packet); 3317856c818Smatthias.ringwald 3327856c818Smatthias.ringwald // ignore non-registered handle 3337856c818Smatthias.ringwald if (!conn){ 3347d67539fSmatthias.ringwald log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle); 3357856c818Smatthias.ringwald return; 3367856c818Smatthias.ringwald } 3377856c818Smatthias.ringwald 338e76a89eeS[email protected] // assert packet is complete 3399ecc3e17S[email protected] if (acl_length + 4 != size){ 340e76a89eeS[email protected] log_error("hci.c: acl_handler called with ACL packet of wrong size %u, expected %u => dropping packet", size, acl_length + 4); 341e76a89eeS[email protected] return; 342e76a89eeS[email protected] } 343e76a89eeS[email protected] 3447856c818Smatthias.ringwald // update idle timestamp 3457856c818Smatthias.ringwald hci_connection_timestamp(conn); 3467856c818Smatthias.ringwald 3477856c818Smatthias.ringwald // handle different packet types 3487856c818Smatthias.ringwald switch (acl_flags & 0x03) { 3497856c818Smatthias.ringwald 3507856c818Smatthias.ringwald case 0x01: // continuation fragment 3517856c818Smatthias.ringwald 3527856c818Smatthias.ringwald // sanity check 3537856c818Smatthias.ringwald if (conn->acl_recombination_pos == 0) { 3547d67539fSmatthias.ringwald log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle); 3557856c818Smatthias.ringwald return; 3567856c818Smatthias.ringwald } 3577856c818Smatthias.ringwald 3587856c818Smatthias.ringwald // append fragment payload (header already stored) 3597856c818Smatthias.ringwald memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length ); 3607856c818Smatthias.ringwald conn->acl_recombination_pos += acl_length; 3617856c818Smatthias.ringwald 3627d67539fSmatthias.ringwald // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length, 363decc01a8Smatthias.ringwald // conn->acl_recombination_pos, conn->acl_recombination_length); 3647856c818Smatthias.ringwald 3657856c818Smatthias.ringwald // forward complete L2CAP packet if complete. 3667856c818Smatthias.ringwald if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header 3677856c818Smatthias.ringwald 3683a9fb326S[email protected] hci_stack->packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos); 3697856c818Smatthias.ringwald // reset recombination buffer 3707856c818Smatthias.ringwald conn->acl_recombination_length = 0; 3717856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 3727856c818Smatthias.ringwald } 3737856c818Smatthias.ringwald break; 3747856c818Smatthias.ringwald 3757856c818Smatthias.ringwald case 0x02: { // first fragment 3767856c818Smatthias.ringwald 3777856c818Smatthias.ringwald // sanity check 3787856c818Smatthias.ringwald if (conn->acl_recombination_pos) { 3797d67539fSmatthias.ringwald log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle); 3807856c818Smatthias.ringwald return; 3817856c818Smatthias.ringwald } 3827856c818Smatthias.ringwald 3837856c818Smatthias.ringwald // peek into L2CAP packet! 3847856c818Smatthias.ringwald uint16_t l2cap_length = READ_L2CAP_LENGTH( packet ); 3857856c818Smatthias.ringwald 386e76a89eeS[email protected] // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length); 387decc01a8Smatthias.ringwald 3887856c818Smatthias.ringwald // compare fragment size to L2CAP packet size 3897856c818Smatthias.ringwald if (acl_length >= l2cap_length + 4){ 3907856c818Smatthias.ringwald 3917856c818Smatthias.ringwald // forward fragment as L2CAP packet 3923a9fb326S[email protected] hci_stack->packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4); 3937856c818Smatthias.ringwald 3947856c818Smatthias.ringwald } else { 3957856c818Smatthias.ringwald // store first fragment and tweak acl length for complete package 3967856c818Smatthias.ringwald memcpy(conn->acl_recombination_buffer, packet, acl_length + 4); 3977856c818Smatthias.ringwald conn->acl_recombination_pos = acl_length + 4; 3987856c818Smatthias.ringwald conn->acl_recombination_length = l2cap_length; 399decc01a8Smatthias.ringwald bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4); 4007856c818Smatthias.ringwald } 4017856c818Smatthias.ringwald break; 4027856c818Smatthias.ringwald 4037856c818Smatthias.ringwald } 4047856c818Smatthias.ringwald default: 4057d67539fSmatthias.ringwald log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03); 4067856c818Smatthias.ringwald return; 4077856c818Smatthias.ringwald } 40894ab26f8Smatthias.ringwald 40994ab26f8Smatthias.ringwald // execute main loop 41094ab26f8Smatthias.ringwald hci_run(); 41116833f0aSmatthias.ringwald } 41222909952Smatthias.ringwald 41367a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){ 414339b6768Smatthias.ringwald log_info("Connection closed: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 4153c4d4b90Smatthias.ringwald 4163c4d4b90Smatthias.ringwald // cancel all l2cap connections 4173c4d4b90Smatthias.ringwald hci_emit_disconnection_complete(conn->con_handle, 0x16); // terminated by local host 4183c4d4b90Smatthias.ringwald 419c7e0c5f6Smatthias.ringwald run_loop_remove_timer(&conn->timeout); 420c785ef68Smatthias.ringwald 4213a9fb326S[email protected] linked_list_remove(&hci_stack->connections, (linked_item_t *) conn); 422a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 4233c4d4b90Smatthias.ringwald 4243c4d4b90Smatthias.ringwald // now it's gone 425c7e0c5f6Smatthias.ringwald hci_emit_nr_connections_changed(); 426c7e0c5f6Smatthias.ringwald } 427c7e0c5f6Smatthias.ringwald 4280c042179S[email protected] static const uint16_t packet_type_sizes[] = { 4298f8108aaSmatthias.ringwald 0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE, 4308f8108aaSmatthias.ringwald HCI_ACL_DH1_SIZE, 0, 0, 0, 4318f8108aaSmatthias.ringwald HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE, 4328f8108aaSmatthias.ringwald HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE 4338f8108aaSmatthias.ringwald }; 43465389bfcS[email protected] static const uint8_t packet_type_feature_requirement_bit[] = { 43565389bfcS[email protected] 0, // 3 slot packets 43665389bfcS[email protected] 1, // 5 slot packets 43765389bfcS[email protected] 25, // EDR 2 mpbs 43865389bfcS[email protected] 26, // EDR 3 mbps 43965389bfcS[email protected] 39, // 3 slot EDR packts 44065389bfcS[email protected] 40, // 5 slot EDR packet 44165389bfcS[email protected] }; 44265389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = { 44365389bfcS[email protected] 0x0f00, // 3 slot packets 44465389bfcS[email protected] 0xf000, // 5 slot packets 44565389bfcS[email protected] 0x1102, // EDR 2 mpbs 44665389bfcS[email protected] 0x2204, // EDR 3 mbps 44765389bfcS[email protected] 0x0300, // 3 slot EDR packts 44865389bfcS[email protected] 0x3000, // 5 slot EDR packet 44965389bfcS[email protected] }; 4508f8108aaSmatthias.ringwald 45165389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){ 45265389bfcS[email protected] // enable packet types based on size 4538f8108aaSmatthias.ringwald uint16_t packet_types = 0; 4548f8108aaSmatthias.ringwald int i; 4558f8108aaSmatthias.ringwald for (i=0;i<16;i++){ 4568f8108aaSmatthias.ringwald if (packet_type_sizes[i] == 0) continue; 4578f8108aaSmatthias.ringwald if (packet_type_sizes[i] <= buffer_size){ 4588f8108aaSmatthias.ringwald packet_types |= 1 << i; 4598f8108aaSmatthias.ringwald } 4608f8108aaSmatthias.ringwald } 46165389bfcS[email protected] // disable packet types due to missing local supported features 46265389bfcS[email protected] for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){ 46365389bfcS[email protected] int bit_idx = packet_type_feature_requirement_bit[i]; 46465389bfcS[email protected] int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0; 46565389bfcS[email protected] if (feature_set) continue; 46665389bfcS[email protected] log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]); 46765389bfcS[email protected] packet_types &= ~packet_type_feature_packet_mask[i]; 46865389bfcS[email protected] } 4698f8108aaSmatthias.ringwald // flip bits for "may not be used" 4708f8108aaSmatthias.ringwald packet_types ^= 0x3306; 4718f8108aaSmatthias.ringwald return packet_types; 4728f8108aaSmatthias.ringwald } 4738f8108aaSmatthias.ringwald 4748f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){ 4753a9fb326S[email protected] return hci_stack->packet_types; 4768f8108aaSmatthias.ringwald } 4778f8108aaSmatthias.ringwald 478facf93fdS[email protected] uint8_t* hci_get_outgoing_packet_buffer(void){ 4797dc17943Smatthias.ringwald // hci packet buffer is >= acl data packet length 4803a9fb326S[email protected] return hci_stack->hci_packet_buffer; 4817dc17943Smatthias.ringwald } 4827dc17943Smatthias.ringwald 483f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){ 4843a9fb326S[email protected] return hci_stack->acl_data_packet_length; 4857dc17943Smatthias.ringwald } 4867dc17943Smatthias.ringwald 487f5d8d141S[email protected] int hci_ssp_supported(void){ 488f5d8d141S[email protected] // No 51, byte 6, bit 3 4893a9fb326S[email protected] return (hci_stack->local_supported_features[6] & (1 << 3)) != 0; 490f5d8d141S[email protected] } 491f5d8d141S[email protected] 492f5d8d141S[email protected] int hci_classic_supported(void){ 493f5d8d141S[email protected] // No 37, byte 4, bit 5, = No BR/EDR Support 4943a9fb326S[email protected] return (hci_stack->local_supported_features[4] & (1 << 5)) == 0; 495f5d8d141S[email protected] } 496f5d8d141S[email protected] 497f5d8d141S[email protected] int hci_le_supported(void){ 498f5d8d141S[email protected] // No 37, byte 4, bit 6 = LE Supported (Controller) 499f5d8d141S[email protected] #ifdef HAVE_BLE 5003a9fb326S[email protected] return (hci_stack->local_supported_features[4] & (1 << 6)) != 0; 501f5d8d141S[email protected] #else 502f5d8d141S[email protected] return 0; 503f5d8d141S[email protected] #endif 504f5d8d141S[email protected] } 505f5d8d141S[email protected] 50669a97523S[email protected] // get addr type and address used in advertisement packets 50718904abdS[email protected] void hci_le_advertisement_address(uint8_t * addr_type, bd_addr_t * addr){ 5083a9fb326S[email protected] *addr_type = hci_stack->adv_addr_type; 5093a9fb326S[email protected] if (hci_stack->adv_addr_type){ 5103a9fb326S[email protected] memcpy(addr, hci_stack->adv_address, 6); 51169a97523S[email protected] } else { 5123a9fb326S[email protected] memcpy(addr, hci_stack->local_bd_addr, 6); 51369a97523S[email protected] } 51469a97523S[email protected] } 51569a97523S[email protected] 51657c9da5bS[email protected] static void le_handle_advertisement_report(uint8_t *packet, int size){ 51757c9da5bS[email protected] int num_reports = packet[3]; 51857c9da5bS[email protected] int i; 51957c9da5bS[email protected] int total_data_length = 0; 52057c9da5bS[email protected] int data_offset = 0; 52157c9da5bS[email protected] 52257c9da5bS[email protected] for (i=0; i<num_reports;i++){ 52357c9da5bS[email protected] total_data_length += packet[4+num_reports*8+i]; 52457c9da5bS[email protected] } 52557c9da5bS[email protected] 52657c9da5bS[email protected] for (i=0; i<num_reports;i++){ 52757c9da5bS[email protected] int pos = 0; 52857c9da5bS[email protected] uint8_t data_length = packet[4+num_reports*8+i]; 529*2b552b23S[email protected] uint8_t event_size = 10 + data_length; 530*2b552b23S[email protected] uint8_t event[2 + event_size ]; 531*2b552b23S[email protected] event[pos++] = GAP_LE_ADVERTISING_REPORT; 53257c9da5bS[email protected] event[pos++] = event_size; 53357c9da5bS[email protected] event[pos++] = packet[4+i]; // event_type; 53457c9da5bS[email protected] event[pos++] = packet[4+num_reports+i]; // address_type; 53557c9da5bS[email protected] bt_flip_addr(&event[pos], &packet[4+num_reports*2+i*6]); // bt address 53657c9da5bS[email protected] pos += 6; 537*2b552b23S[email protected] event[pos++] = packet[4+num_reports*9+total_data_length + i]; 53857c9da5bS[email protected] event[pos++] = data_length; 53957c9da5bS[email protected] memcpy(&packet[4+num_reports*9+data_offset], &event[pos], data_length); 54057c9da5bS[email protected] data_offset += data_length; 54157c9da5bS[email protected] pos += data_length; 54257c9da5bS[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 54357c9da5bS[email protected] } 54457c9da5bS[email protected] } 54557c9da5bS[email protected] 54674ec757aSmatthias.ringwald // avoid huge local variables 547223aafc1Smatthias.ringwald #ifndef EMBEDDED 54874ec757aSmatthias.ringwald static device_name_t device_name; 549223aafc1Smatthias.ringwald #endif 55016833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){ 551e76a89eeS[email protected] 552e76a89eeS[email protected] uint16_t event_length = packet[1]; 553e76a89eeS[email protected] 554e76a89eeS[email protected] // assert packet is complete 555e76a89eeS[email protected] if (size != event_length + 2){ 556e76a89eeS[email protected] log_error("hci.c: event_handler called with event packet of wrong size %u, expected %u => dropping packet", size, event_length + 2); 557e76a89eeS[email protected] return; 558e76a89eeS[email protected] } 559e76a89eeS[email protected] 5601281a47eSmatthias.ringwald bd_addr_t addr; 56196a45072S[email protected] bd_addr_type_t addr_type; 5622d00edd4Smatthias.ringwald uint8_t link_type; 563fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 5641f7b95a1Smatthias.ringwald hci_connection_t * conn; 56556cf178bSmatthias.ringwald int i; 56622909952Smatthias.ringwald 5675909f7f2Smatthias.ringwald // printf("HCI:EVENT:%02x\n", packet[0]); 5685909f7f2Smatthias.ringwald 5696772a24cSmatthias.ringwald switch (packet[0]) { 57022909952Smatthias.ringwald 5716772a24cSmatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 5727ec5eeaaSmatthias.ringwald // get num cmd packets 5733a9fb326S[email protected] // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack->num_cmd_packets, packet[2]); 5743a9fb326S[email protected] hci_stack->num_cmd_packets = packet[2]; 5757ec5eeaaSmatthias.ringwald 576e2edc0c3Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){ 577e2edc0c3Smatthias.ringwald // from offset 5 578e2edc0c3Smatthias.ringwald // status 5791d279b20Smatthias.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" 5803a9fb326S[email protected] hci_stack->acl_data_packet_length = READ_BT_16(packet, 6); 58156cf178bSmatthias.ringwald // ignore: SCO data packet len (8) 5823a9fb326S[email protected] hci_stack->total_num_acl_packets = packet[9]; 58356cf178bSmatthias.ringwald // ignore: total num SCO packets 5843a9fb326S[email protected] if (hci_stack->state == HCI_STATE_INITIALIZING){ 585a7a04bd9Smatthias.ringwald // determine usable ACL payload size 5863a9fb326S[email protected] if (HCI_ACL_PAYLOAD_SIZE < hci_stack->acl_data_packet_length){ 5873a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 5888f8108aaSmatthias.ringwald } 58965389bfcS[email protected] log_info("hci_read_buffer_size: used size %u, count %u\n", 5903a9fb326S[email protected] hci_stack->acl_data_packet_length, hci_stack->total_num_acl_packets); 591e2edc0c3Smatthias.ringwald } 59256cf178bSmatthias.ringwald } 59365a46ef3S[email protected] #ifdef HAVE_BLE 59465a46ef3S[email protected] if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)){ 5953a9fb326S[email protected] hci_stack->le_data_packet_length = READ_BT_16(packet, 6); 5963a9fb326S[email protected] hci_stack->total_num_le_packets = packet[8]; 5973a9fb326S[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); 59865a46ef3S[email protected] } 59965a46ef3S[email protected] #endif 600188981d2Smatthias.ringwald // Dump local address 601188981d2Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) { 6023a9fb326S[email protected] bt_flip_addr(hci_stack->local_bd_addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]); 603188981d2Smatthias.ringwald log_info("Local Address, Status: 0x%02x: Addr: %s\n", 6043a9fb326S[email protected] packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr)); 605188981d2Smatthias.ringwald } 606381fbed8Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 6073a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 608381fbed8Smatthias.ringwald } 60965389bfcS[email protected] // Note: HCI init checks 610559e517eS[email protected] if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){ 6113a9fb326S[email protected] memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8); 612559e517eS[email protected] log_info("Local Supported Features: 0x%02x%02x%02x%02x%02x%02x%02x%02x", 6133a9fb326S[email protected] hci_stack->local_supported_features[0], hci_stack->local_supported_features[1], 6143a9fb326S[email protected] hci_stack->local_supported_features[2], hci_stack->local_supported_features[3], 6153a9fb326S[email protected] hci_stack->local_supported_features[4], hci_stack->local_supported_features[5], 6163a9fb326S[email protected] hci_stack->local_supported_features[6], hci_stack->local_supported_features[7]); 61765389bfcS[email protected] 61865389bfcS[email protected] // determine usable ACL packet types based buffer size and supported features 6193a9fb326S[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]); 6203a9fb326S[email protected] log_info("packet types %04x", hci_stack->packet_types); 621f5d8d141S[email protected] 622f5d8d141S[email protected] // Classic/LE 623f5d8d141S[email protected] log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported()); 624559e517eS[email protected] } 62556cf178bSmatthias.ringwald break; 62656cf178bSmatthias.ringwald 6277ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 6287ec5eeaaSmatthias.ringwald // get num cmd packets 6293a9fb326S[email protected] // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack->num_cmd_packets, packet[3]); 6303a9fb326S[email protected] hci_stack->num_cmd_packets = packet[3]; 6317ec5eeaaSmatthias.ringwald break; 6327ec5eeaaSmatthias.ringwald 63356cf178bSmatthias.ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 63456cf178bSmatthias.ringwald for (i=0; i<packet[2];i++){ 63556cf178bSmatthias.ringwald handle = READ_BT_16(packet, 3 + 2*i); 63656cf178bSmatthias.ringwald uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i); 6375061f3afS[email protected] conn = hci_connection_for_handle(handle); 63856cf178bSmatthias.ringwald if (!conn){ 6397d67539fSmatthias.ringwald log_error("hci_number_completed_packet lists unused con handle %u\n", handle); 64056cf178bSmatthias.ringwald continue; 64156cf178bSmatthias.ringwald } 64256cf178bSmatthias.ringwald conn->num_acl_packets_sent -= num_packets; 6437b5fbe1fSmatthias.ringwald // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent); 64456cf178bSmatthias.ringwald } 6456772a24cSmatthias.ringwald break; 6466772a24cSmatthias.ringwald 6471f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 64837eaa4cfSmatthias.ringwald bt_flip_addr(addr, &packet[2]); 64937eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 6502d00edd4Smatthias.ringwald link_type = packet[11]; 651339b6768Smatthias.ringwald log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type); 65237eaa4cfSmatthias.ringwald if (link_type == 1) { // ACL 65396a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 6541f7b95a1Smatthias.ringwald if (!conn) { 65596a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 6561f7b95a1Smatthias.ringwald } 657ce4c8fabSmatthias.ringwald if (!conn) { 658ce4c8fabSmatthias.ringwald // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 6593a9fb326S[email protected] hci_stack->decline_reason = 0x0d; 6603a9fb326S[email protected] BD_ADDR_COPY(hci_stack->decline_addr, addr); 661ce4c8fabSmatthias.ringwald break; 662ce4c8fabSmatthias.ringwald } 66332ab9390Smatthias.ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 66432ab9390Smatthias.ringwald hci_run(); 66537eaa4cfSmatthias.ringwald } else { 666ce4c8fabSmatthias.ringwald // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A) 6673a9fb326S[email protected] hci_stack->decline_reason = 0x0a; 6683a9fb326S[email protected] BD_ADDR_COPY(hci_stack->decline_addr, addr); 66937eaa4cfSmatthias.ringwald } 6701f7b95a1Smatthias.ringwald break; 6711f7b95a1Smatthias.ringwald 6726772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 673fe1ed1b8Smatthias.ringwald // Connection management 674fe1ed1b8Smatthias.ringwald bt_flip_addr(addr, &packet[5]); 675339b6768Smatthias.ringwald log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr)); 67696a45072S[email protected] addr_type = BD_ADDR_TYPE_CLASSIC; 67796a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, addr_type); 678fe1ed1b8Smatthias.ringwald if (conn) { 679b448a0e7Smatthias.ringwald if (!packet[2]){ 680c8e4258aSmatthias.ringwald conn->state = OPEN; 681fe1ed1b8Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 3); 682ad83dc6aS[email protected] conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES; 683ee091cf1Smatthias.ringwald 684c785ef68Smatthias.ringwald // restart timer 685c21e6239Smatthias.ringwald run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 686ee091cf1Smatthias.ringwald run_loop_add_timer(&conn->timeout); 687c785ef68Smatthias.ringwald 688339b6768Smatthias.ringwald log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 68943bfb1bdSmatthias.ringwald 69043bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 691b448a0e7Smatthias.ringwald } else { 692ad83dc6aS[email protected] // notify client if dedicated bonding 693ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 694ad83dc6aS[email protected] hci_emit_dedicated_bonding_result(conn, packet[2]); 695ad83dc6aS[email protected] } 696ad83dc6aS[email protected] 697b448a0e7Smatthias.ringwald // connection failed, remove entry 6983a9fb326S[email protected] linked_list_remove(&hci_stack->connections, (linked_item_t *) conn); 699a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 700c12e46e7Smatthias.ringwald 701c12e46e7Smatthias.ringwald // if authentication error, also delete link key 702c12e46e7Smatthias.ringwald if (packet[2] == 0x05) { 703c12e46e7Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 704c12e46e7Smatthias.ringwald } 705fe1ed1b8Smatthias.ringwald } 706fe1ed1b8Smatthias.ringwald } 7076772a24cSmatthias.ringwald break; 708fe1ed1b8Smatthias.ringwald 709afd4e962S[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 710afd4e962S[email protected] handle = READ_BT_16(packet, 3); 711afd4e962S[email protected] conn = hci_connection_for_handle(handle); 712afd4e962S[email protected] if (!conn) break; 713afd4e962S[email protected] if (!packet[2]){ 714afd4e962S[email protected] uint8_t * features = &packet[5]; 715afd4e962S[email protected] if (features[6] & (1 << 3)){ 716afd4e962S[email protected] conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP; 717afd4e962S[email protected] } 718afd4e962S[email protected] } 719afd4e962S[email protected] conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES; 720ad83dc6aS[email protected] log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x", conn->bonding_flags); 721ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 722ad83dc6aS[email protected] conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 723ad83dc6aS[email protected] } 724afd4e962S[email protected] break; 725afd4e962S[email protected] 7267fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 7277b5fbe1fSmatthias.ringwald log_info("HCI_EVENT_LINK_KEY_REQUEST\n"); 7287fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST); 72974d716b5S[email protected] // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST 7303a9fb326S[email protected] if (hci_stack->bondable && !hci_stack->remote_device_db) break; 73132ab9390Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST); 73264472d52Smatthias.ringwald hci_run(); 733d9a327f9Smatthias.ringwald // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set 73429d53098Smatthias.ringwald return; 7357fde4af9Smatthias.ringwald 7369ab95c90S[email protected] case HCI_EVENT_LINK_KEY_NOTIFICATION: { 73729d53098Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 73896a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 7399ab95c90S[email protected] if (!conn) break; 7409ab95c90S[email protected] conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION; 7417bdc6798S[email protected] link_key_type_t link_key_type = (link_key_type_t)packet[24]; 7429ab95c90S[email protected] // Change Connection Encryption keeps link key type 7439ab95c90S[email protected] if (link_key_type != CHANGED_COMBINATION_KEY){ 7449ab95c90S[email protected] conn->link_key_type = link_key_type; 7459ab95c90S[email protected] } 7463a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 7473a9fb326S[email protected] hci_stack->remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8], conn->link_key_type); 74829d53098Smatthias.ringwald // still forward event to allow dismiss of pairing dialog 7497fde4af9Smatthias.ringwald break; 7509ab95c90S[email protected] } 7517fde4af9Smatthias.ringwald 7527fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 7536724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE); 7544c57c146S[email protected] // non-bondable mode: pin code negative reply will be sent 7553a9fb326S[email protected] if (!hci_stack->bondable){ 756899283eaS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST); 757f8fb5f6eS[email protected] hci_run(); 758f8fb5f6eS[email protected] return; 7594c57c146S[email protected] } 760d9a327f9Smatthias.ringwald // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key 7613a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 762d9a327f9Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 7633a9fb326S[email protected] hci_stack->remote_device_db->delete_link_key(&addr); 7647fde4af9Smatthias.ringwald break; 7657fde4af9Smatthias.ringwald 7661d6b20aeS[email protected] case HCI_EVENT_IO_CAPABILITY_REQUEST: 7671d6b20aeS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST); 768dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY); 769dbe1a790S[email protected] break; 770dbe1a790S[email protected] 771dbe1a790S[email protected] case HCI_EVENT_USER_CONFIRMATION_REQUEST: 7726724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 7733a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 774dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY); 775dbe1a790S[email protected] break; 776dbe1a790S[email protected] 777dbe1a790S[email protected] case HCI_EVENT_USER_PASSKEY_REQUEST: 7786724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 7793a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 780dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY); 7811d6b20aeS[email protected] break; 7821d6b20aeS[email protected] 783f0944df2S[email protected] case HCI_EVENT_ENCRYPTION_CHANGE: 784f0944df2S[email protected] handle = READ_BT_16(packet, 3); 785f0944df2S[email protected] conn = hci_connection_for_handle(handle); 786f0944df2S[email protected] if (!conn) break; 787ad83dc6aS[email protected] if (packet[2] == 0) { 788f0944df2S[email protected] if (packet[5]){ 789f0944df2S[email protected] conn->authentication_flags |= CONNECTION_ENCRYPTED; 790f0944df2S[email protected] } else { 791536f9994S[email protected] conn->authentication_flags &= ~CONNECTION_ENCRYPTED; 792f0944df2S[email protected] } 793ad83dc6aS[email protected] } 794a00031e2S[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 795f0944df2S[email protected] break; 796f0944df2S[email protected] 7971eb2563eS[email protected] case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT: 7981eb2563eS[email protected] handle = READ_BT_16(packet, 3); 7991eb2563eS[email protected] conn = hci_connection_for_handle(handle); 8001eb2563eS[email protected] if (!conn) break; 801ad83dc6aS[email protected] 802ad83dc6aS[email protected] // dedicated bonding: send result and disconnect 803ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 804ad83dc6aS[email protected] conn->bonding_flags &= ~BONDING_DEDICATED; 805ad83dc6aS[email protected] hci_emit_dedicated_bonding_result( conn, packet[2]); 806ad83dc6aS[email protected] conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE; 807ad83dc6aS[email protected] break; 808ad83dc6aS[email protected] } 809ad83dc6aS[email protected] 810b5cb6874S[email protected] if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){ 8111eb2563eS[email protected] // link key sufficient for requested security 8121eb2563eS[email protected] conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 813ad83dc6aS[email protected] break; 814ad83dc6aS[email protected] } 8151eb2563eS[email protected] // not enough 8161eb2563eS[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 8171eb2563eS[email protected] break; 81834d2123cS[email protected] 819223aafc1Smatthias.ringwald #ifndef EMBEDDED 82074ec757aSmatthias.ringwald case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 8213a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 82274ec757aSmatthias.ringwald if (packet[2]) break; // status not ok 82374ec757aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 8245a06394aSmatthias.ringwald // fix for invalid remote names - terminate on 0xff 8255a06394aSmatthias.ringwald for (i=0; i<248;i++){ 8265a06394aSmatthias.ringwald if (packet[9+i] == 0xff){ 8275a06394aSmatthias.ringwald packet[9+i] = 0; 8285a06394aSmatthias.ringwald break; 829cdc9101dSmatthias.ringwald } 8305a06394aSmatthias.ringwald } 831d2fe945cS[email protected] memset(&device_name, 0, sizeof(device_name_t)); 83274ec757aSmatthias.ringwald strncpy((char*) device_name, (char*) &packet[9], 248); 8333a9fb326S[email protected] hci_stack->remote_device_db->put_name(&addr, &device_name); 83474ec757aSmatthias.ringwald break; 83574ec757aSmatthias.ringwald 83674ec757aSmatthias.ringwald case HCI_EVENT_INQUIRY_RESULT: 83774ec757aSmatthias.ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 8383a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 83974ec757aSmatthias.ringwald // first send inq result packet 8403a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size); 84174ec757aSmatthias.ringwald // then send cached remote names 84274ec757aSmatthias.ringwald for (i=0; i<packet[2];i++){ 84374ec757aSmatthias.ringwald bt_flip_addr(addr, &packet[3+i*6]); 8443a9fb326S[email protected] if (hci_stack->remote_device_db->get_name(&addr, &device_name)){ 84574ec757aSmatthias.ringwald hci_emit_remote_name_cached(&addr, &device_name); 84674ec757aSmatthias.ringwald } 84774ec757aSmatthias.ringwald } 84874ec757aSmatthias.ringwald return; 849223aafc1Smatthias.ringwald #endif 85074ec757aSmatthias.ringwald 8516772a24cSmatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 852fe1ed1b8Smatthias.ringwald if (!packet[2]){ 853fe1ed1b8Smatthias.ringwald handle = READ_BT_16(packet, 3); 8545061f3afS[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 855fe1ed1b8Smatthias.ringwald if (conn) { 856c7e0c5f6Smatthias.ringwald hci_shutdown_connection(conn); 857fe1ed1b8Smatthias.ringwald } 858fe1ed1b8Smatthias.ringwald } 8596772a24cSmatthias.ringwald break; 8606772a24cSmatthias.ringwald 861c68bdf90Smatthias.ringwald case HCI_EVENT_HARDWARE_ERROR: 8623a9fb326S[email protected] if(hci_stack->control && hci_stack->control->hw_error){ 8633a9fb326S[email protected] (*hci_stack->control->hw_error)(); 864c68bdf90Smatthias.ringwald } 865c68bdf90Smatthias.ringwald break; 866c68bdf90Smatthias.ringwald 8676b4af23dS[email protected] case DAEMON_EVENT_HCI_PACKET_SENT: 8686b4af23dS[email protected] // free packet buffer for asynchronous transport 8696b4af23dS[email protected] if (hci_transport_synchronous()) break; 8706b4af23dS[email protected] hci_stack->hci_packet_buffer_reserved = 0; 8716b4af23dS[email protected] break; 8726b4af23dS[email protected] 8735909f7f2Smatthias.ringwald #ifdef HAVE_BLE 8745909f7f2Smatthias.ringwald case HCI_EVENT_LE_META: 8755909f7f2Smatthias.ringwald switch (packet[2]){ 87657c9da5bS[email protected] case HCI_SUBEVENT_LE_ADVERTISING_REPORT: 8777bdc6798S[email protected] if (hci_stack->le_scanning_state != LE_SCANNING) break; 87857c9da5bS[email protected] le_handle_advertisement_report(packet, size); 8797bdc6798S[email protected] break; 8805909f7f2Smatthias.ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 8815909f7f2Smatthias.ringwald // Connection management 8825909f7f2Smatthias.ringwald bt_flip_addr(addr, &packet[8]); 8837bdc6798S[email protected] addr_type = (bd_addr_type_t)packet[7]; 88496a45072S[email protected] log_info("LE Connection_complete (status=%u) type %u, %s\n", packet[3], addr_type, bd_addr_to_str(addr)); 8855909f7f2Smatthias.ringwald // LE connections are auto-accepted, so just create a connection if there isn't one already 88696a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, addr_type); 8875909f7f2Smatthias.ringwald if (packet[3]){ 8885909f7f2Smatthias.ringwald if (conn){ 8895909f7f2Smatthias.ringwald // outgoing connection failed, remove entry 8903a9fb326S[email protected] linked_list_remove(&hci_stack->connections, (linked_item_t *) conn); 8915909f7f2Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 8925909f7f2Smatthias.ringwald 8935909f7f2Smatthias.ringwald } 8945909f7f2Smatthias.ringwald // if authentication error, also delete link key 8955909f7f2Smatthias.ringwald if (packet[3] == 0x05) { 8965909f7f2Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 8975909f7f2Smatthias.ringwald } 8985909f7f2Smatthias.ringwald break; 8995909f7f2Smatthias.ringwald } 9005909f7f2Smatthias.ringwald if (!conn){ 90196a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 9025909f7f2Smatthias.ringwald } 9035909f7f2Smatthias.ringwald if (!conn){ 9045909f7f2Smatthias.ringwald // no memory 9055909f7f2Smatthias.ringwald break; 9065909f7f2Smatthias.ringwald } 9075909f7f2Smatthias.ringwald 9085909f7f2Smatthias.ringwald conn->state = OPEN; 9095909f7f2Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 4); 9105909f7f2Smatthias.ringwald 9115909f7f2Smatthias.ringwald // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock 9125909f7f2Smatthias.ringwald 9135909f7f2Smatthias.ringwald // restart timer 9145909f7f2Smatthias.ringwald // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 9155909f7f2Smatthias.ringwald // run_loop_add_timer(&conn->timeout); 9165909f7f2Smatthias.ringwald 9175909f7f2Smatthias.ringwald log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 9185909f7f2Smatthias.ringwald 9195909f7f2Smatthias.ringwald hci_emit_nr_connections_changed(); 9205909f7f2Smatthias.ringwald break; 9215909f7f2Smatthias.ringwald 92265a46ef3S[email protected] // printf("LE buffer size: %u, count %u\n", READ_BT_16(packet,6), packet[8]); 92365a46ef3S[email protected] 9245909f7f2Smatthias.ringwald default: 9255909f7f2Smatthias.ringwald break; 9265909f7f2Smatthias.ringwald } 9275909f7f2Smatthias.ringwald break; 9285909f7f2Smatthias.ringwald #endif 9295909f7f2Smatthias.ringwald 9306772a24cSmatthias.ringwald default: 9316772a24cSmatthias.ringwald break; 932fe1ed1b8Smatthias.ringwald } 933fe1ed1b8Smatthias.ringwald 9343429f56bSmatthias.ringwald // handle BT initialization 9353a9fb326S[email protected] if (hci_stack->state == HCI_STATE_INITIALIZING){ 9363a9fb326S[email protected] if (hci_stack->substate % 2){ 9373429f56bSmatthias.ringwald // odd: waiting for event 938f155fca3Smatthias.ringwald if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || packet[0] == HCI_EVENT_COMMAND_STATUS){ 939c9af4d3fS[email protected] // wait for explicit COMMAND COMPLETE on RESET 9403a9fb326S[email protected] if (hci_stack->substate > 1 || COMMAND_COMPLETE_EVENT(packet, hci_reset)) { 9413a9fb326S[email protected] hci_stack->substate++; 9423429f56bSmatthias.ringwald } 9433429f56bSmatthias.ringwald } 94422909952Smatthias.ringwald } 945c9af4d3fS[email protected] } 94622909952Smatthias.ringwald 94789db417bSmatthias.ringwald // help with BT sleep 9483a9fb326S[email protected] if (hci_stack->state == HCI_STATE_FALLING_ASLEEP 9493a9fb326S[email protected] && hci_stack->substate == 1 95089db417bSmatthias.ringwald && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 9513a9fb326S[email protected] hci_stack->substate++; 95289db417bSmatthias.ringwald } 95389db417bSmatthias.ringwald 9543a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size); 95594ab26f8Smatthias.ringwald 95694ab26f8Smatthias.ringwald // execute main loop 95794ab26f8Smatthias.ringwald hci_run(); 95816833f0aSmatthias.ringwald } 95916833f0aSmatthias.ringwald 9600a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 96110e830c9Smatthias.ringwald switch (packet_type) { 96210e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 96310e830c9Smatthias.ringwald event_handler(packet, size); 96410e830c9Smatthias.ringwald break; 96510e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 96610e830c9Smatthias.ringwald acl_handler(packet, size); 96710e830c9Smatthias.ringwald break; 96810e830c9Smatthias.ringwald default: 96910e830c9Smatthias.ringwald break; 97010e830c9Smatthias.ringwald } 97110e830c9Smatthias.ringwald } 97210e830c9Smatthias.ringwald 973fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 9742718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 9753a9fb326S[email protected] hci_stack->packet_handler = handler; 97616833f0aSmatthias.ringwald } 97716833f0aSmatthias.ringwald 9784f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){ 979475c8125Smatthias.ringwald 9803a9fb326S[email protected] #ifdef HAVE_MALLOC 9813a9fb326S[email protected] if (!hci_stack) { 9823a9fb326S[email protected] hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t)); 9833a9fb326S[email protected] } 9843a9fb326S[email protected] #else 9853a9fb326S[email protected] hci_stack = &hci_stack_static; 9863a9fb326S[email protected] #endif 98766fb9560S[email protected] memset(hci_stack, 0, sizeof(hci_stack_t)); 9883a9fb326S[email protected] 989475c8125Smatthias.ringwald // reference to use transport layer implementation 9903a9fb326S[email protected] hci_stack->hci_transport = transport; 991475c8125Smatthias.ringwald 99211e23e5fSmatthias.ringwald // references to used control implementation 9933a9fb326S[email protected] hci_stack->control = control; 99411e23e5fSmatthias.ringwald 99511e23e5fSmatthias.ringwald // reference to used config 9963a9fb326S[email protected] hci_stack->config = config; 99711e23e5fSmatthias.ringwald 998fe1ed1b8Smatthias.ringwald // no connections yet 9993a9fb326S[email protected] hci_stack->connections = NULL; 10003a9fb326S[email protected] hci_stack->discoverable = 0; 10013a9fb326S[email protected] hci_stack->connectable = 0; 10023a9fb326S[email protected] hci_stack->bondable = 1; 1003758b46ceSmatthias.ringwald 1004b031bebbSmatthias.ringwald // no pending cmds 10053a9fb326S[email protected] hci_stack->decline_reason = 0; 10063a9fb326S[email protected] hci_stack->new_scan_enable_value = 0xff; 1007b031bebbSmatthias.ringwald 100816833f0aSmatthias.ringwald // higher level handler 10093a9fb326S[email protected] hci_stack->packet_handler = dummy_handler; 101016833f0aSmatthias.ringwald 1011404843c1Smatthias.ringwald // store and open remote device db 10123a9fb326S[email protected] hci_stack->remote_device_db = remote_device_db; 10133a9fb326S[email protected] if (hci_stack->remote_device_db) { 10143a9fb326S[email protected] hci_stack->remote_device_db->open(); 1015404843c1Smatthias.ringwald } 101629d53098Smatthias.ringwald 10178fcba05dSmatthias.ringwald // max acl payload size defined in config.h 10183a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 10198fcba05dSmatthias.ringwald 102016833f0aSmatthias.ringwald // register packet handlers with transport 102110e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 1022f5454fc6Smatthias.ringwald 10233a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 1024e2386ba1S[email protected] 1025e2386ba1S[email protected] // class of device 10263a9fb326S[email protected] hci_stack->class_of_device = 0x007a020c; // Smartphone 1027a45d6b9fS[email protected] 102863048403S[email protected] // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept 10293a9fb326S[email protected] hci_stack->ssp_enable = 1; 10303a9fb326S[email protected] hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT; 10313a9fb326S[email protected] hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING; 10323a9fb326S[email protected] hci_stack->ssp_auto_accept = 1; 103369a97523S[email protected] 103469a97523S[email protected] // LE 10353a9fb326S[email protected] hci_stack->adv_addr_type = 0; 10363a9fb326S[email protected] memset(hci_stack->adv_address, 0, 6); 10377bdc6798S[email protected] hci_stack->le_scanning_state = LE_SCAN_IDLE; 1038475c8125Smatthias.ringwald } 1039475c8125Smatthias.ringwald 1040404843c1Smatthias.ringwald void hci_close(){ 1041404843c1Smatthias.ringwald // close remote device db 10423a9fb326S[email protected] if (hci_stack->remote_device_db) { 10433a9fb326S[email protected] hci_stack->remote_device_db->close(); 1044404843c1Smatthias.ringwald } 10453a9fb326S[email protected] while (hci_stack->connections) { 10463a9fb326S[email protected] hci_shutdown_connection((hci_connection_t *) hci_stack->connections); 1047f5454fc6Smatthias.ringwald } 1048f5454fc6Smatthias.ringwald hci_power_control(HCI_POWER_OFF); 10493a9fb326S[email protected] 10503a9fb326S[email protected] #ifdef HAVE_MALLOC 10513a9fb326S[email protected] free(hci_stack); 10523a9fb326S[email protected] #endif 10533a9fb326S[email protected] hci_stack = NULL; 1054404843c1Smatthias.ringwald } 1055404843c1Smatthias.ringwald 10569e61646fS[email protected] void hci_set_class_of_device(uint32_t class_of_device){ 10579e61646fS[email protected] hci_stack->class_of_device = class_of_device; 10589e61646fS[email protected] } 10599e61646fS[email protected] 106066fb9560S[email protected] void hci_disable_l2cap_timeout_check(){ 106166fb9560S[email protected] disable_l2cap_timeouts = 1; 106266fb9560S[email protected] } 10638d213e1aSmatthias.ringwald // State-Module-Driver overview 10648d213e1aSmatthias.ringwald // state module low-level 10658d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 10668d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 10678d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 10688d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 1069d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 1070d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 1071c7e0c5f6Smatthias.ringwald 107240d1c7a4Smatthias.ringwald static int hci_power_control_on(void){ 10737301ad89Smatthias.ringwald 1074038bc64cSmatthias.ringwald // power on 1075f9a30166Smatthias.ringwald int err = 0; 10763a9fb326S[email protected] if (hci_stack->control && hci_stack->control->on){ 10773a9fb326S[email protected] err = (*hci_stack->control->on)(hci_stack->config); 1078f9a30166Smatthias.ringwald } 1079038bc64cSmatthias.ringwald if (err){ 10807d67539fSmatthias.ringwald log_error( "POWER_ON failed\n"); 1081038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 1082038bc64cSmatthias.ringwald return err; 1083038bc64cSmatthias.ringwald } 1084038bc64cSmatthias.ringwald 1085038bc64cSmatthias.ringwald // open low-level device 10863a9fb326S[email protected] err = hci_stack->hci_transport->open(hci_stack->config); 1087038bc64cSmatthias.ringwald if (err){ 10887d67539fSmatthias.ringwald log_error( "HCI_INIT failed, turning Bluetooth off again\n"); 10893a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 10903a9fb326S[email protected] (*hci_stack->control->off)(hci_stack->config); 1091f9a30166Smatthias.ringwald } 1092038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 1093038bc64cSmatthias.ringwald return err; 1094038bc64cSmatthias.ringwald } 10958d213e1aSmatthias.ringwald return 0; 10968d213e1aSmatthias.ringwald } 1097038bc64cSmatthias.ringwald 109840d1c7a4Smatthias.ringwald static void hci_power_control_off(void){ 10998d213e1aSmatthias.ringwald 11007b5fbe1fSmatthias.ringwald log_info("hci_power_control_off\n"); 11019418f9c9Smatthias.ringwald 11028d213e1aSmatthias.ringwald // close low-level device 11033a9fb326S[email protected] hci_stack->hci_transport->close(hci_stack->config); 11048d213e1aSmatthias.ringwald 11057b5fbe1fSmatthias.ringwald log_info("hci_power_control_off - hci_transport closed\n"); 11069418f9c9Smatthias.ringwald 11078d213e1aSmatthias.ringwald // power off 11083a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 11093a9fb326S[email protected] (*hci_stack->control->off)(hci_stack->config); 11108d213e1aSmatthias.ringwald } 11119418f9c9Smatthias.ringwald 11127b5fbe1fSmatthias.ringwald log_info("hci_power_control_off - control closed\n"); 11139418f9c9Smatthias.ringwald 11143a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 111572ea5239Smatthias.ringwald } 111672ea5239Smatthias.ringwald 111740d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){ 111872ea5239Smatthias.ringwald 11197b5fbe1fSmatthias.ringwald log_info("hci_power_control_sleep\n"); 11203144bce4Smatthias.ringwald 1121b429b9b7Smatthias.ringwald #if 0 1122b429b9b7Smatthias.ringwald // don't close serial port during sleep 1123b429b9b7Smatthias.ringwald 112472ea5239Smatthias.ringwald // close low-level device 11253a9fb326S[email protected] hci_stack->hci_transport->close(hci_stack->config); 1126b429b9b7Smatthias.ringwald #endif 112772ea5239Smatthias.ringwald 112872ea5239Smatthias.ringwald // sleep mode 11293a9fb326S[email protected] if (hci_stack->control && hci_stack->control->sleep){ 11303a9fb326S[email protected] (*hci_stack->control->sleep)(hci_stack->config); 113172ea5239Smatthias.ringwald } 1132b429b9b7Smatthias.ringwald 11333a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 11348d213e1aSmatthias.ringwald } 11358d213e1aSmatthias.ringwald 113640d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){ 1137b429b9b7Smatthias.ringwald 11387b5fbe1fSmatthias.ringwald log_info("hci_power_control_wake\n"); 1139b429b9b7Smatthias.ringwald 1140b429b9b7Smatthias.ringwald // wake on 11413a9fb326S[email protected] if (hci_stack->control && hci_stack->control->wake){ 11423a9fb326S[email protected] (*hci_stack->control->wake)(hci_stack->config); 1143b429b9b7Smatthias.ringwald } 1144b429b9b7Smatthias.ringwald 1145b429b9b7Smatthias.ringwald #if 0 1146b429b9b7Smatthias.ringwald // open low-level device 11473a9fb326S[email protected] int err = hci_stack->hci_transport->open(hci_stack->config); 1148b429b9b7Smatthias.ringwald if (err){ 11497d67539fSmatthias.ringwald log_error( "HCI_INIT failed, turning Bluetooth off again\n"); 11503a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 11513a9fb326S[email protected] (*hci_stack->control->off)(hci_stack->config); 1152b429b9b7Smatthias.ringwald } 1153b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 1154b429b9b7Smatthias.ringwald return err; 1155b429b9b7Smatthias.ringwald } 1156b429b9b7Smatthias.ringwald #endif 1157b429b9b7Smatthias.ringwald 1158b429b9b7Smatthias.ringwald return 0; 1159b429b9b7Smatthias.ringwald } 1160b429b9b7Smatthias.ringwald 1161b429b9b7Smatthias.ringwald 11628d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 1163d661ed19Smatthias.ringwald 11643a9fb326S[email protected] log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack->state); 1165d661ed19Smatthias.ringwald 11668d213e1aSmatthias.ringwald int err = 0; 11673a9fb326S[email protected] switch (hci_stack->state){ 11688d213e1aSmatthias.ringwald 11698d213e1aSmatthias.ringwald case HCI_STATE_OFF: 11708d213e1aSmatthias.ringwald switch (power_mode){ 11718d213e1aSmatthias.ringwald case HCI_POWER_ON: 11728d213e1aSmatthias.ringwald err = hci_power_control_on(); 11738d213e1aSmatthias.ringwald if (err) return err; 11747301ad89Smatthias.ringwald // set up state machine 11753a9fb326S[email protected] hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent 11763a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 11773a9fb326S[email protected] hci_stack->substate = 0; 11788d213e1aSmatthias.ringwald break; 11798d213e1aSmatthias.ringwald case HCI_POWER_OFF: 11808d213e1aSmatthias.ringwald // do nothing 11818d213e1aSmatthias.ringwald break; 11828d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1183b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 11848d213e1aSmatthias.ringwald break; 11858d213e1aSmatthias.ringwald } 11868d213e1aSmatthias.ringwald break; 11877301ad89Smatthias.ringwald 11888d213e1aSmatthias.ringwald case HCI_STATE_INITIALIZING: 11898d213e1aSmatthias.ringwald switch (power_mode){ 11908d213e1aSmatthias.ringwald case HCI_POWER_ON: 11918d213e1aSmatthias.ringwald // do nothing 11928d213e1aSmatthias.ringwald break; 11938d213e1aSmatthias.ringwald case HCI_POWER_OFF: 11948d213e1aSmatthias.ringwald // no connections yet, just turn it off 11958d213e1aSmatthias.ringwald hci_power_control_off(); 11968d213e1aSmatthias.ringwald break; 11978d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1198b546ac54Smatthias.ringwald // no connections yet, just turn it off 119972ea5239Smatthias.ringwald hci_power_control_sleep(); 12008d213e1aSmatthias.ringwald break; 12018d213e1aSmatthias.ringwald } 12028d213e1aSmatthias.ringwald break; 12037301ad89Smatthias.ringwald 12048d213e1aSmatthias.ringwald case HCI_STATE_WORKING: 12058d213e1aSmatthias.ringwald switch (power_mode){ 12068d213e1aSmatthias.ringwald case HCI_POWER_ON: 12078d213e1aSmatthias.ringwald // do nothing 12088d213e1aSmatthias.ringwald break; 12098d213e1aSmatthias.ringwald case HCI_POWER_OFF: 1210c7e0c5f6Smatthias.ringwald // see hci_run 12113a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 12128d213e1aSmatthias.ringwald break; 12138d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1214b546ac54Smatthias.ringwald // see hci_run 12153a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 12163a9fb326S[email protected] hci_stack->substate = 0; 12178d213e1aSmatthias.ringwald break; 12188d213e1aSmatthias.ringwald } 12198d213e1aSmatthias.ringwald break; 12207301ad89Smatthias.ringwald 12218d213e1aSmatthias.ringwald case HCI_STATE_HALTING: 12228d213e1aSmatthias.ringwald switch (power_mode){ 12238d213e1aSmatthias.ringwald case HCI_POWER_ON: 12248d213e1aSmatthias.ringwald // set up state machine 12253a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 12263a9fb326S[email protected] hci_stack->substate = 0; 12278d213e1aSmatthias.ringwald break; 12288d213e1aSmatthias.ringwald case HCI_POWER_OFF: 12298d213e1aSmatthias.ringwald // do nothing 12308d213e1aSmatthias.ringwald break; 12318d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1232b546ac54Smatthias.ringwald // see hci_run 12333a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 12343a9fb326S[email protected] hci_stack->substate = 0; 12358d213e1aSmatthias.ringwald break; 12368d213e1aSmatthias.ringwald } 12378d213e1aSmatthias.ringwald break; 12388d213e1aSmatthias.ringwald 12398d213e1aSmatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 12408d213e1aSmatthias.ringwald switch (power_mode){ 12418d213e1aSmatthias.ringwald case HCI_POWER_ON: 124228171530Smatthias.ringwald 124328171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 124428171530Smatthias.ringwald // nothing to do, if H4 supports power management 124528171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 12463a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 12473a9fb326S[email protected] hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP; 124828171530Smatthias.ringwald break; 124928171530Smatthias.ringwald } 125028171530Smatthias.ringwald #endif 1251b546ac54Smatthias.ringwald // set up state machine 12523a9fb326S[email protected] hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent 12533a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 12543a9fb326S[email protected] hci_stack->substate = 0; 12558d213e1aSmatthias.ringwald break; 12568d213e1aSmatthias.ringwald case HCI_POWER_OFF: 1257b546ac54Smatthias.ringwald // see hci_run 12583a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 12598d213e1aSmatthias.ringwald break; 12608d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1261b546ac54Smatthias.ringwald // do nothing 12628d213e1aSmatthias.ringwald break; 12638d213e1aSmatthias.ringwald } 12648d213e1aSmatthias.ringwald break; 12658d213e1aSmatthias.ringwald 12668d213e1aSmatthias.ringwald case HCI_STATE_SLEEPING: 12678d213e1aSmatthias.ringwald switch (power_mode){ 12688d213e1aSmatthias.ringwald case HCI_POWER_ON: 126928171530Smatthias.ringwald 127028171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 127128171530Smatthias.ringwald // nothing to do, if H4 supports power management 127228171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 12733a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 12743a9fb326S[email protected] hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP; 1275758b46ceSmatthias.ringwald hci_update_scan_enable(); 127628171530Smatthias.ringwald break; 127728171530Smatthias.ringwald } 127828171530Smatthias.ringwald #endif 12793144bce4Smatthias.ringwald err = hci_power_control_wake(); 12803144bce4Smatthias.ringwald if (err) return err; 1281b546ac54Smatthias.ringwald // set up state machine 12823a9fb326S[email protected] hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent 12833a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 12843a9fb326S[email protected] hci_stack->substate = 0; 12858d213e1aSmatthias.ringwald break; 12868d213e1aSmatthias.ringwald case HCI_POWER_OFF: 12873a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 12888d213e1aSmatthias.ringwald break; 12898d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1290b546ac54Smatthias.ringwald // do nothing 12918d213e1aSmatthias.ringwald break; 12928d213e1aSmatthias.ringwald } 12938d213e1aSmatthias.ringwald break; 129411e23e5fSmatthias.ringwald } 129568d92d03Smatthias.ringwald 1296038bc64cSmatthias.ringwald // create internal event 1297ee8bf225Smatthias.ringwald hci_emit_state(); 1298ee8bf225Smatthias.ringwald 129968d92d03Smatthias.ringwald // trigger next/first action 130068d92d03Smatthias.ringwald hci_run(); 130168d92d03Smatthias.ringwald 1302475c8125Smatthias.ringwald return 0; 1303475c8125Smatthias.ringwald } 1304475c8125Smatthias.ringwald 1305758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){ 1306758b46ceSmatthias.ringwald // 2 = page scan, 1 = inq scan 13073a9fb326S[email protected] hci_stack->new_scan_enable_value = hci_stack->connectable << 1 | hci_stack->discoverable; 1308758b46ceSmatthias.ringwald hci_run(); 1309758b46ceSmatthias.ringwald } 1310758b46ceSmatthias.ringwald 1311381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){ 1312381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 1313381fbed8Smatthias.ringwald 13143a9fb326S[email protected] if (hci_stack->discoverable == enable){ 13153a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 1316381fbed8Smatthias.ringwald return; 1317381fbed8Smatthias.ringwald } 1318381fbed8Smatthias.ringwald 13193a9fb326S[email protected] hci_stack->discoverable = enable; 1320758b46ceSmatthias.ringwald hci_update_scan_enable(); 1321758b46ceSmatthias.ringwald } 1322b031bebbSmatthias.ringwald 1323758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){ 1324758b46ceSmatthias.ringwald if (enable) enable = 1; // normalize argument 1325758b46ceSmatthias.ringwald 1326758b46ceSmatthias.ringwald // don't emit event 13273a9fb326S[email protected] if (hci_stack->connectable == enable) return; 1328758b46ceSmatthias.ringwald 13293a9fb326S[email protected] hci_stack->connectable = enable; 1330758b46ceSmatthias.ringwald hci_update_scan_enable(); 1331381fbed8Smatthias.ringwald } 1332381fbed8Smatthias.ringwald 13335061f3afS[email protected] bd_addr_t * hci_local_bd_addr(void){ 13343a9fb326S[email protected] return &hci_stack->local_bd_addr; 13355061f3afS[email protected] } 13365061f3afS[email protected] 133706b35ec0Smatthias.ringwald void hci_run(){ 13388a485f27Smatthias.ringwald 133932ab9390Smatthias.ringwald hci_connection_t * connection; 134032ab9390Smatthias.ringwald linked_item_t * it; 134132ab9390Smatthias.ringwald 134264f0b431S[email protected] if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) return; 1343ce4c8fabSmatthias.ringwald 1344b031bebbSmatthias.ringwald // global/non-connection oriented commands 1345b031bebbSmatthias.ringwald 1346b031bebbSmatthias.ringwald // decline incoming connections 13473a9fb326S[email protected] if (hci_stack->decline_reason){ 13483a9fb326S[email protected] uint8_t reason = hci_stack->decline_reason; 13493a9fb326S[email protected] hci_stack->decline_reason = 0; 13503a9fb326S[email protected] hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason); 1351dbe1a790S[email protected] return; 1352ce4c8fabSmatthias.ringwald } 1353ce4c8fabSmatthias.ringwald 1354b031bebbSmatthias.ringwald // send scan enable 13553a9fb326S[email protected] if (hci_stack->state == HCI_STATE_WORKING && hci_stack->new_scan_enable_value != 0xff && hci_classic_supported()){ 13563a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value); 13573a9fb326S[email protected] hci_stack->new_scan_enable_value = 0xff; 1358dbe1a790S[email protected] return; 1359b031bebbSmatthias.ringwald } 1360b031bebbSmatthias.ringwald 13617bdc6798S[email protected] // handle le scan 13627bdc6798S[email protected] if (hci_stack->state == HCI_STATE_WORKING){ 13637bdc6798S[email protected] switch(hci_stack->le_scanning_state){ 13647bdc6798S[email protected] case LE_START_SCAN: 13657bdc6798S[email protected] hci_stack->le_scanning_state = LE_SCANNING; 13667bdc6798S[email protected] hci_send_cmd(&hci_le_set_scan_enable, 1, 0); 13677bdc6798S[email protected] return; 13687bdc6798S[email protected] 13697bdc6798S[email protected] case LE_STOP_SCAN: 13707bdc6798S[email protected] hci_stack->le_scanning_state = LE_SCAN_IDLE; 13717bdc6798S[email protected] hci_send_cmd(&hci_le_set_scan_enable, 0, 0); 13727bdc6798S[email protected] return; 13737bdc6798S[email protected] default: 13747bdc6798S[email protected] break; 13757bdc6798S[email protected] } 13767bdc6798S[email protected] } 13777bdc6798S[email protected] 137832ab9390Smatthias.ringwald // send pending HCI commands 13793a9fb326S[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){ 1380b031bebbSmatthias.ringwald connection = (hci_connection_t *) it; 138132ab9390Smatthias.ringwald 13825917a5c5S[email protected] if (connection->state == SEND_DISCONNECT){ 13835917a5c5S[email protected] hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 13845917a5c5S[email protected] connection->state = SENT_DISCONNECT; 13855917a5c5S[email protected] return; 13865917a5c5S[email protected] } 13875917a5c5S[email protected] 1388ad83dc6aS[email protected] if (connection->state == SEND_CREATE_CONNECTION){ 1389ad83dc6aS[email protected] log_info("sending hci_create_connection\n"); 13904f3229d8S[email protected] switch(connection->address_type){ 13914f3229d8S[email protected] case BD_ADDR_TYPE_CLASSIC: 1392ad83dc6aS[email protected] hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 13934f3229d8S[email protected] break; 13944f3229d8S[email protected] default: 13954f3229d8S[email protected] hci_send_cmd(&hci_le_create_connection, 13964f3229d8S[email protected] 1000, // scan interval: 625 ms 13974f3229d8S[email protected] 1000, // scan interval: 625 ms 13984f3229d8S[email protected] 0, // don't use whitelist 13994f3229d8S[email protected] connection->address_type, // peer address type 14004f3229d8S[email protected] connection->address, // peer bd addr 14014f3229d8S[email protected] 0, // our addr type: public 14024f3229d8S[email protected] 80, // conn interval min 14034f3229d8S[email protected] 80, // conn interval max (3200 * 0.625) 14044f3229d8S[email protected] 0, // conn latency 14054f3229d8S[email protected] 2000, // supervision timeout 14064f3229d8S[email protected] 0, // min ce length 14074f3229d8S[email protected] 1000 // max ce length 14084f3229d8S[email protected] ); 14094f3229d8S[email protected] 14104f3229d8S[email protected] connection->state = SENT_CREATE_CONNECTION; 14114f3229d8S[email protected] break; 14124f3229d8S[email protected] } 1413ad83dc6aS[email protected] return; 1414ad83dc6aS[email protected] } 1415ad83dc6aS[email protected] 141632ab9390Smatthias.ringwald if (connection->state == RECEIVED_CONNECTION_REQUEST){ 14177b5fbe1fSmatthias.ringwald log_info("sending hci_accept_connection_request\n"); 141832ab9390Smatthias.ringwald connection->state = ACCEPTED_CONNECTION_REQUEST; 141934d2123cS[email protected] hci_send_cmd(&hci_accept_connection_request, connection->address, 1); 1420dbe1a790S[email protected] return; 1421c7e0c5f6Smatthias.ringwald } 1422c7e0c5f6Smatthias.ringwald 142332ab9390Smatthias.ringwald if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){ 142434d2123cS[email protected] log_info("responding to link key request\n"); 142534d2123cS[email protected] connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST); 142632ab9390Smatthias.ringwald link_key_t link_key; 1427c77e8838S[email protected] link_key_type_t link_key_type; 14283a9fb326S[email protected] if ( hci_stack->remote_device_db 14293a9fb326S[email protected] && hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type) 143034d2123cS[email protected] && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){ 14319ab95c90S[email protected] connection->link_key_type = link_key_type; 143232ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key); 143332ab9390Smatthias.ringwald } else { 143432ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 143532ab9390Smatthias.ringwald } 1436dbe1a790S[email protected] return; 143732ab9390Smatthias.ringwald } 14381d6b20aeS[email protected] 1439899283eaS[email protected] if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){ 14404c57c146S[email protected] log_info("denying to pin request\n"); 1441899283eaS[email protected] connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST); 144234d2123cS[email protected] hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address); 14434c57c146S[email protected] return; 14444c57c146S[email protected] } 14454c57c146S[email protected] 1446dbe1a790S[email protected] if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){ 144734d2123cS[email protected] connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY); 144882d8f825S[email protected] log_info("IO Capability Request received, stack bondable %u, io cap %u", hci_stack->bondable, hci_stack->ssp_io_capability); 144982d8f825S[email protected] if (hci_stack->bondable && (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN)){ 1450106d6d11S[email protected] // tweak authentication requirements 14513a9fb326S[email protected] uint8_t authreq = hci_stack->ssp_authentication_requirement; 1452106d6d11S[email protected] if (connection->bonding_flags & BONDING_DEDICATED){ 14539faad3abS[email protected] authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 14549faad3abS[email protected] } 14559faad3abS[email protected] if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){ 14569faad3abS[email protected] authreq |= 1; 1457106d6d11S[email protected] } 14583a9fb326S[email protected] hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, NULL, authreq); 1459f8fb5f6eS[email protected] } else { 1460f8fb5f6eS[email protected] hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED); 1461f8fb5f6eS[email protected] } 1462dbe1a790S[email protected] return; 146332ab9390Smatthias.ringwald } 146432ab9390Smatthias.ringwald 1465dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){ 1466dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY); 146734d2123cS[email protected] hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address); 1468dbe1a790S[email protected] return; 1469dbe1a790S[email protected] } 1470dbe1a790S[email protected] 1471dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){ 1472dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY); 147334d2123cS[email protected] hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000); 1474dbe1a790S[email protected] return; 1475dbe1a790S[email protected] } 1476afd4e962S[email protected] 1477afd4e962S[email protected] if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){ 1478afd4e962S[email protected] connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES; 147934d2123cS[email protected] hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle); 14802bd8b7e7S[email protected] return; 14812bd8b7e7S[email protected] } 14822bd8b7e7S[email protected] 14832bd8b7e7S[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){ 14842bd8b7e7S[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK; 148534d2123cS[email protected] hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005); // authentication failure 148634d2123cS[email protected] return; 148734d2123cS[email protected] } 1488ad83dc6aS[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){ 1489ad83dc6aS[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE; 149052d34f98S[email protected] hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // authentication done 1491ad83dc6aS[email protected] return; 1492ad83dc6aS[email protected] } 149334d2123cS[email protected] if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){ 149434d2123cS[email protected] connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST; 149534d2123cS[email protected] hci_send_cmd(&hci_authentication_requested, connection->con_handle); 14962bd8b7e7S[email protected] return; 1497afd4e962S[email protected] } 1498dce78009S[email protected] if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){ 1499dce78009S[email protected] connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST; 1500dce78009S[email protected] hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1); 1501dce78009S[email protected] return; 1502dce78009S[email protected] } 1503dbe1a790S[email protected] } 1504c7e0c5f6Smatthias.ringwald 15053a9fb326S[email protected] switch (hci_stack->state){ 15063429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 15073a9fb326S[email protected] // log_info("hci_init: substate %u\n", hci_stack->substate); 15083a9fb326S[email protected] if (hci_stack->substate % 2) { 15093429f56bSmatthias.ringwald // odd: waiting for command completion 151006b35ec0Smatthias.ringwald return; 15113429f56bSmatthias.ringwald } 15123a9fb326S[email protected] switch (hci_stack->substate >> 1){ 1513c7492964Smatthias.ringwald case 0: // RESET 151422909952Smatthias.ringwald hci_send_cmd(&hci_reset); 151524052c2aS[email protected] 15163a9fb326S[email protected] if (hci_stack->config == 0 || ((hci_uart_config_t *)hci_stack->config)->baudrate_main == 0){ 1517c7492964Smatthias.ringwald // skip baud change 15183a9fb326S[email protected] hci_stack->substate = 4; // >> 1 = 2 1519c7492964Smatthias.ringwald } 15203429f56bSmatthias.ringwald break; 1521c7492964Smatthias.ringwald case 1: // SEND BAUD CHANGE 15223a9fb326S[email protected] hci_stack->control->baudrate_cmd(hci_stack->config, ((hci_uart_config_t *)hci_stack->config)->baudrate_main, hci_stack->hci_packet_buffer); 15233a9fb326S[email protected] hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]); 1524c7492964Smatthias.ringwald break; 1525c7492964Smatthias.ringwald case 2: // LOCAL BAUD CHANGE 15263a9fb326S[email protected] hci_stack->hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack->config)->baudrate_main); 15273a9fb326S[email protected] hci_stack->substate += 2; 1528c7492964Smatthias.ringwald // break missing here for fall through 1529c7492964Smatthias.ringwald 1530c7492964Smatthias.ringwald case 3: 153124052c2aS[email protected] // Custom initialization 15323a9fb326S[email protected] if (hci_stack->control && hci_stack->control->next_cmd){ 15333a9fb326S[email protected] int valid_cmd = (*hci_stack->control->next_cmd)(hci_stack->config, hci_stack->hci_packet_buffer); 1534d62aca9fSmatthias.ringwald if (valid_cmd){ 15353a9fb326S[email protected] int size = 3 + hci_stack->hci_packet_buffer[2]; 15363a9fb326S[email protected] hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size); 15373a9fb326S[email protected] hci_stack->substate = 4; // more init commands 153890919203Smatthias.ringwald break; 153990919203Smatthias.ringwald } 1540d62aca9fSmatthias.ringwald log_info("hci_run: init script done\n\r"); 154190919203Smatthias.ringwald } 15422f6c30e1Smatthias.ringwald // otherwise continue 1543f432a6ddSmatthias.ringwald hci_send_cmd(&hci_read_bd_addr); 1544f432a6ddSmatthias.ringwald break; 1545c7492964Smatthias.ringwald case 4: 1546e2edc0c3Smatthias.ringwald hci_send_cmd(&hci_read_buffer_size); 1547e2edc0c3Smatthias.ringwald break; 1548c7492964Smatthias.ringwald case 5: 154965389bfcS[email protected] hci_send_cmd(&hci_read_local_supported_features); 15503429f56bSmatthias.ringwald break; 1551c7492964Smatthias.ringwald case 6: 1552d7e0e3a8S[email protected] if (hci_le_supported()){ 1553d7e0e3a8S[email protected] hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF); 1554d7e0e3a8S[email protected] } else { 1555d7e0e3a8S[email protected] // Kensington Bluetoot 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff... 1556d7e0e3a8S[email protected] hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF); 1557d7e0e3a8S[email protected] } 1558f5d8d141S[email protected] 1559f5d8d141S[email protected] // skip Classic init commands for LE only chipsets 156024052c2aS[email protected] if (!hci_classic_supported()){ 156124052c2aS[email protected] if (hci_le_supported()){ 15623a9fb326S[email protected] hci_stack->substate = 11 << 1; // skip all classic command 156324052c2aS[email protected] } else { 156424052c2aS[email protected] log_error("Neither BR/EDR nor LE supported"); 15653a9fb326S[email protected] hci_stack->substate = 13 << 1; // skip all 156624052c2aS[email protected] } 1567f5d8d141S[email protected] } 1568f5d8d141S[email protected] break; 1569f5d8d141S[email protected] case 7: 157024052c2aS[email protected] if (hci_ssp_supported()){ 15713a9fb326S[email protected] hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable); 1572f5d8d141S[email protected] break; 157324052c2aS[email protected] } 15743a9fb326S[email protected] hci_stack->substate += 2; 157524052c2aS[email protected] // break missing here for fall through 157624052c2aS[email protected] 1577f5d8d141S[email protected] case 8: 157865389bfcS[email protected] // ca. 15 sec 157965389bfcS[email protected] hci_send_cmd(&hci_write_page_timeout, 0x6000); 1580559e517eS[email protected] break; 1581f5d8d141S[email protected] case 9: 15823a9fb326S[email protected] hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device); 1583e2386ba1S[email protected] break; 1584f5d8d141S[email protected] case 10: 15853a9fb326S[email protected] if (hci_stack->local_name){ 15863a9fb326S[email protected] hci_send_cmd(&hci_write_local_name, hci_stack->local_name); 1587e2386ba1S[email protected] } else { 15888a485f27Smatthias.ringwald char hostname[30]; 1589e2386ba1S[email protected] #ifdef EMBEDDED 1590e2386ba1S[email protected] // BTstack-11:22:33:44:55:66 1591e2386ba1S[email protected] strcpy(hostname, "BTstack "); 15923a9fb326S[email protected] strcat(hostname, bd_addr_to_str(hci_stack->local_bd_addr)); 1593e2386ba1S[email protected] printf("---> Name %s\n", hostname); 1594e2386ba1S[email protected] #else 1595e2386ba1S[email protected] // hostname for POSIX systems 15968a485f27Smatthias.ringwald gethostname(hostname, 30); 15978a485f27Smatthias.ringwald hostname[29] = '\0'; 1598e2386ba1S[email protected] #endif 15998a485f27Smatthias.ringwald hci_send_cmd(&hci_write_local_name, hostname); 16008a485f27Smatthias.ringwald } 16013c4d4b90Smatthias.ringwald break; 1602e0dbca83S[email protected] case 11: 16033a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan 160424052c2aS[email protected] if (!hci_le_supported()){ 160524052c2aS[email protected] // SKIP LE init for Classic only configuration 16063a9fb326S[email protected] hci_stack->substate = 13 << 1; 160724052c2aS[email protected] } 1608a45d6b9fS[email protected] break; 160924052c2aS[email protected] 161043aa7007S[email protected] #ifdef HAVE_BLE 161124052c2aS[email protected] // LE INIT 1612a45d6b9fS[email protected] case 12: 161324052c2aS[email protected] hci_send_cmd(&hci_le_read_buffer_size); 161424052c2aS[email protected] break; 161524052c2aS[email protected] case 13: 161624052c2aS[email protected] // LE Supported Host = 1, Simultaneous Host = 0 161724052c2aS[email protected] hci_send_cmd(&hci_write_le_host_supported, 1, 0); 161824052c2aS[email protected] break; 161943aa7007S[email protected] #endif 162024052c2aS[email protected] 162124052c2aS[email protected] // DONE 162224052c2aS[email protected] case 14: 16233429f56bSmatthias.ringwald // done. 16243a9fb326S[email protected] hci_stack->state = HCI_STATE_WORKING; 1625b360b6adSmatthias.ringwald hci_emit_state(); 16263429f56bSmatthias.ringwald break; 16273429f56bSmatthias.ringwald default: 16283429f56bSmatthias.ringwald break; 1629475c8125Smatthias.ringwald } 16303a9fb326S[email protected] hci_stack->substate++; 16313429f56bSmatthias.ringwald break; 1632c7e0c5f6Smatthias.ringwald 1633c7e0c5f6Smatthias.ringwald case HCI_STATE_HALTING: 1634c7e0c5f6Smatthias.ringwald 16357b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING\n"); 1636c7e0c5f6Smatthias.ringwald // close all open connections 16373a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 1638c7e0c5f6Smatthias.ringwald if (connection){ 163932ab9390Smatthias.ringwald 1640c7e0c5f6Smatthias.ringwald // send disconnect 164164f0b431S[email protected] if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) return; 164232ab9390Smatthias.ringwald 164379bbfa0bSmatthias.ringwald log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle); 16446ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 1645c7e0c5f6Smatthias.ringwald 1646c7e0c5f6Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 1647c7e0c5f6Smatthias.ringwald hci_shutdown_connection(connection); 1648c7e0c5f6Smatthias.ringwald return; 1649c7e0c5f6Smatthias.ringwald } 16507b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, calling off\n"); 1651c7e0c5f6Smatthias.ringwald 165272ea5239Smatthias.ringwald // switch mode 1653c7e0c5f6Smatthias.ringwald hci_power_control_off(); 16549418f9c9Smatthias.ringwald 16557b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, emitting state\n"); 165672ea5239Smatthias.ringwald hci_emit_state(); 16577b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, done\n"); 165872ea5239Smatthias.ringwald break; 1659c7e0c5f6Smatthias.ringwald 166072ea5239Smatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 16613a9fb326S[email protected] switch(hci_stack->substate) { 166289db417bSmatthias.ringwald case 0: 16637b5fbe1fSmatthias.ringwald log_info("HCI_STATE_FALLING_ASLEEP\n"); 166472ea5239Smatthias.ringwald // close all open connections 16653a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 166666da7044Smatthias.ringwald 166728171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 166866da7044Smatthias.ringwald // don't close connections, if H4 supports power management 166966da7044Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 167066da7044Smatthias.ringwald connection = NULL; 167166da7044Smatthias.ringwald } 167266da7044Smatthias.ringwald #endif 167372ea5239Smatthias.ringwald if (connection){ 167432ab9390Smatthias.ringwald 167572ea5239Smatthias.ringwald // send disconnect 167632ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 167732ab9390Smatthias.ringwald 167879bbfa0bSmatthias.ringwald log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle); 16796ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 168072ea5239Smatthias.ringwald 168172ea5239Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 168272ea5239Smatthias.ringwald hci_shutdown_connection(connection); 168372ea5239Smatthias.ringwald return; 168472ea5239Smatthias.ringwald } 168572ea5239Smatthias.ringwald 168692368cd3S[email protected] if (hci_classic_supported()){ 168789db417bSmatthias.ringwald // disable page and inquiry scan 168864f0b431S[email protected] if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) return; 168932ab9390Smatthias.ringwald 169092368cd3S[email protected] log_info("HCI_STATE_HALTING, disabling inq scans\n"); 16913a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan 169289db417bSmatthias.ringwald 169389db417bSmatthias.ringwald // continue in next sub state 16943a9fb326S[email protected] hci_stack->substate++; 169589db417bSmatthias.ringwald break; 169692368cd3S[email protected] } 169792368cd3S[email protected] // fall through for ble-only chips 169892368cd3S[email protected] 169989db417bSmatthias.ringwald case 2: 17007b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, calling sleep\n"); 170128171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 170228171530Smatthias.ringwald // don't actually go to sleep, if H4 supports power management 170328171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 170428171530Smatthias.ringwald // SLEEP MODE reached 17053a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 170628171530Smatthias.ringwald hci_emit_state(); 170728171530Smatthias.ringwald break; 170828171530Smatthias.ringwald } 170928171530Smatthias.ringwald #endif 171072ea5239Smatthias.ringwald // switch mode 17113a9fb326S[email protected] hci_power_control_sleep(); // changes hci_stack->state to SLEEP 1712c7e0c5f6Smatthias.ringwald hci_emit_state(); 171328171530Smatthias.ringwald break; 171428171530Smatthias.ringwald 171589db417bSmatthias.ringwald default: 171689db417bSmatthias.ringwald break; 171789db417bSmatthias.ringwald } 1718c7e0c5f6Smatthias.ringwald break; 1719c7e0c5f6Smatthias.ringwald 17203429f56bSmatthias.ringwald default: 17213429f56bSmatthias.ringwald break; 17221f504dbdSmatthias.ringwald } 17233429f56bSmatthias.ringwald } 172416833f0aSmatthias.ringwald 172531452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 1726c8e4258aSmatthias.ringwald bd_addr_t addr; 1727c8e4258aSmatthias.ringwald hci_connection_t * conn; 1728c8e4258aSmatthias.ringwald // house-keeping 1729c8e4258aSmatthias.ringwald 1730c8e4258aSmatthias.ringwald // create_connection? 1731c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 1732c8e4258aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 1733339b6768Smatthias.ringwald log_info("Create_connection to %s\n", bd_addr_to_str(addr)); 1734c8e4258aSmatthias.ringwald 173596a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 1736ad83dc6aS[email protected] if (!conn){ 173796a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 173817f1ba2aSmatthias.ringwald if (!conn){ 173917f1ba2aSmatthias.ringwald // notify client that alloc failed 174017f1ba2aSmatthias.ringwald hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED); 174117f1ba2aSmatthias.ringwald return 0; // don't sent packet to controller 174217f1ba2aSmatthias.ringwald } 1743ad83dc6aS[email protected] conn->state = SEND_CREATE_CONNECTION; 1744ad83dc6aS[email protected] } 1745ad83dc6aS[email protected] log_info("conn state %u", conn->state); 1746ad83dc6aS[email protected] switch (conn->state){ 1747ad83dc6aS[email protected] // if connection active exists 1748ad83dc6aS[email protected] case OPEN: 174962bda3fbS[email protected] // and OPEN, emit connection complete command, don't send to controller 1750ad83dc6aS[email protected] hci_emit_connection_complete(conn, 0); 175162bda3fbS[email protected] return 0; 1752ad83dc6aS[email protected] case SEND_CREATE_CONNECTION: 1753ad83dc6aS[email protected] // connection created by hci, e.g. dedicated bonding 1754ad83dc6aS[email protected] break; 1755ad83dc6aS[email protected] default: 1756ad83dc6aS[email protected] // otherwise, just ignore as it is already in the open process 1757ad83dc6aS[email protected] return 0; 1758ad83dc6aS[email protected] } 1759c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 1760c8e4258aSmatthias.ringwald } 1761c8e4258aSmatthias.ringwald 17627fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_reply)){ 17637fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY); 17647fde4af9Smatthias.ringwald } 17657fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){ 17667fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST); 17677fde4af9Smatthias.ringwald } 17687fde4af9Smatthias.ringwald 17698ef73945Smatthias.ringwald if (IS_COMMAND(packet, hci_delete_stored_link_key)){ 17703a9fb326S[email protected] if (hci_stack->remote_device_db){ 17718ef73945Smatthias.ringwald bt_flip_addr(addr, &packet[3]); 17723a9fb326S[email protected] hci_stack->remote_device_db->delete_link_key(&addr); 17738ef73945Smatthias.ringwald } 17748ef73945Smatthias.ringwald } 1775c8e4258aSmatthias.ringwald 17766724cd9eS[email protected] if (IS_COMMAND(packet, hci_pin_code_request_negative_reply) 17776724cd9eS[email protected] || IS_COMMAND(packet, hci_pin_code_request_reply)){ 17786724cd9eS[email protected] bt_flip_addr(addr, &packet[3]); 177996a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 17806724cd9eS[email protected] if (conn){ 17816724cd9eS[email protected] connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE); 17826724cd9eS[email protected] } 17836724cd9eS[email protected] } 17846724cd9eS[email protected] 17856724cd9eS[email protected] if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply) 17866724cd9eS[email protected] || IS_COMMAND(packet, hci_user_confirmation_request_reply) 17876724cd9eS[email protected] || IS_COMMAND(packet, hci_user_passkey_request_negative_reply) 17886724cd9eS[email protected] || IS_COMMAND(packet, hci_user_passkey_request_reply)) { 17896724cd9eS[email protected] bt_flip_addr(addr, &packet[3]); 179096a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 17916724cd9eS[email protected] if (conn){ 17926724cd9eS[email protected] connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE); 17936724cd9eS[email protected] } 17946724cd9eS[email protected] } 17956724cd9eS[email protected] 179669a97523S[email protected] #ifdef HAVE_BLE 179769a97523S[email protected] if (IS_COMMAND(packet, hci_le_set_advertising_parameters)){ 17983a9fb326S[email protected] hci_stack->adv_addr_type = packet[8]; 179969a97523S[email protected] } 180069a97523S[email protected] if (IS_COMMAND(packet, hci_le_set_random_address)){ 18013a9fb326S[email protected] bt_flip_addr(hci_stack->adv_address, &packet[3]); 180269a97523S[email protected] } 180369a97523S[email protected] #endif 180469a97523S[email protected] 18053a9fb326S[email protected] hci_stack->num_cmd_packets--; 18066b4af23dS[email protected] int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 18076b4af23dS[email protected] 18086b4af23dS[email protected] // free packet buffer for synchronous transport implementations 1809c8b9416aS[email protected] if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){ 18106b4af23dS[email protected] hci_stack->hci_packet_buffer_reserved = 0; 18116b4af23dS[email protected] } 18126b4af23dS[email protected] 18136b4af23dS[email protected] return err; 181431452debSmatthias.ringwald } 18158adf0ddaSmatthias.ringwald 18162bd8b7e7S[email protected] // disconnect because of security block 18172bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){ 18182bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 18192bd8b7e7S[email protected] if (!connection) return; 18202bd8b7e7S[email protected] connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 18212bd8b7e7S[email protected] } 18222bd8b7e7S[email protected] 18232bd8b7e7S[email protected] 1824dbe1a790S[email protected] // Configure Secure Simple Pairing 1825dbe1a790S[email protected] 1826dbe1a790S[email protected] // enable will enable SSP during init 1827dbe1a790S[email protected] void hci_ssp_set_enable(int enable){ 18283a9fb326S[email protected] hci_stack->ssp_enable = enable; 1829dbe1a790S[email protected] } 1830dbe1a790S[email protected] 18312bd8b7e7S[email protected] int hci_local_ssp_activated(){ 18323a9fb326S[email protected] return hci_ssp_supported() && hci_stack->ssp_enable; 18332bd8b7e7S[email protected] } 18342bd8b7e7S[email protected] 1835dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement 1836dbe1a790S[email protected] void hci_ssp_set_io_capability(int io_capability){ 18373a9fb326S[email protected] hci_stack->ssp_io_capability = io_capability; 1838dbe1a790S[email protected] } 1839dbe1a790S[email protected] void hci_ssp_set_authentication_requirement(int authentication_requirement){ 18403a9fb326S[email protected] hci_stack->ssp_authentication_requirement = authentication_requirement; 1841dbe1a790S[email protected] } 1842dbe1a790S[email protected] 1843dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested 1844dbe1a790S[email protected] void hci_ssp_set_auto_accept(int auto_accept){ 18453a9fb326S[email protected] hci_stack->ssp_auto_accept = auto_accept; 1846dbe1a790S[email protected] } 1847dbe1a790S[email protected] 18481cd208adSmatthias.ringwald /** 18491cd208adSmatthias.ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 18501cd208adSmatthias.ringwald */ 1851fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){ 18521cd208adSmatthias.ringwald va_list argptr; 18531cd208adSmatthias.ringwald va_start(argptr, cmd); 18543a9fb326S[email protected] uint16_t size = hci_create_cmd_internal(hci_stack->hci_packet_buffer, cmd, argptr); 18551cd208adSmatthias.ringwald va_end(argptr); 18563a9fb326S[email protected] return hci_send_cmd_packet(hci_stack->hci_packet_buffer, size); 185793b8dc03Smatthias.ringwald } 1858c8e4258aSmatthias.ringwald 1859ee091cf1Smatthias.ringwald // Create various non-HCI events. 1860ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 1861ee091cf1Smatthias.ringwald 1862c8e4258aSmatthias.ringwald void hci_emit_state(){ 18633a9fb326S[email protected] log_info("BTSTACK_EVENT_STATE %u", hci_stack->state); 1864425d1371Smatthias.ringwald uint8_t event[3]; 186580d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 1866425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 18673a9fb326S[email protected] event[2] = hci_stack->state; 1868425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 18693a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1870c8e4258aSmatthias.ringwald } 1871c8e4258aSmatthias.ringwald 187217f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){ 1873425d1371Smatthias.ringwald uint8_t event[13]; 1874c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 1875425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 187617f1ba2aSmatthias.ringwald event[2] = status; 1877c8e4258aSmatthias.ringwald bt_store_16(event, 3, conn->con_handle); 1878c8e4258aSmatthias.ringwald bt_flip_addr(&event[5], conn->address); 1879c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 1880c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 1881425d1371Smatthias.ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 18823a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1883c8e4258aSmatthias.ringwald } 1884c8e4258aSmatthias.ringwald 18854f3229d8S[email protected] void hci_emit_le_connection_complete(hci_connection_t *conn, uint8_t status){ 18864f3229d8S[email protected] uint8_t event[21]; 18874f3229d8S[email protected] event[0] = HCI_EVENT_LE_META; 18884f3229d8S[email protected] event[1] = sizeof(event) - 2; 18894f3229d8S[email protected] event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE; 18904f3229d8S[email protected] event[3] = status; 18914f3229d8S[email protected] bt_store_16(event, 4, conn->con_handle); 18924f3229d8S[email protected] event[6] = 0; // TODO: role 18934f3229d8S[email protected] event[7] = conn->address_type; 18944f3229d8S[email protected] bt_flip_addr(&event[8], conn->address); 18954f3229d8S[email protected] bt_store_16(event, 14, 0); // interval 18964f3229d8S[email protected] bt_store_16(event, 16, 0); // latency 18974f3229d8S[email protected] bt_store_16(event, 18, 0); // supervision timeout 18984f3229d8S[email protected] event[20] = 0; // master clock accuracy 18994f3229d8S[email protected] hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 19004f3229d8S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 19014f3229d8S[email protected] } 19024f3229d8S[email protected] 19033c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){ 1904425d1371Smatthias.ringwald uint8_t event[6]; 19053c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 1906e518c4b8Smatthias.ringwald event[1] = sizeof(event) - 2; 19073c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 19083c4d4b90Smatthias.ringwald bt_store_16(event, 3, handle); 19093c4d4b90Smatthias.ringwald event[5] = reason; 1910425d1371Smatthias.ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 19113a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 19123c4d4b90Smatthias.ringwald } 19133c4d4b90Smatthias.ringwald 1914ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 191566fb9560S[email protected] if (disable_l2cap_timeouts) return; 1916e0abb8e7S[email protected] log_info("L2CAP_EVENT_TIMEOUT_CHECK"); 1917425d1371Smatthias.ringwald uint8_t event[4]; 191880d52d6bSmatthias.ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 1919425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1920ee091cf1Smatthias.ringwald bt_store_16(event, 2, conn->con_handle); 1921425d1371Smatthias.ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 19223a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1923ee091cf1Smatthias.ringwald } 192443bfb1bdSmatthias.ringwald 192543bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){ 1926e0abb8e7S[email protected] log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections()); 1927425d1371Smatthias.ringwald uint8_t event[3]; 192880d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 1929425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 193043bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 1931425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 19323a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 193343bfb1bdSmatthias.ringwald } 1934038bc64cSmatthias.ringwald 1935038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){ 1936e0abb8e7S[email protected] log_info("BTSTACK_EVENT_POWERON_FAILED"); 1937425d1371Smatthias.ringwald uint8_t event[2]; 193880d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 1939425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1940425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 19413a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1942038bc64cSmatthias.ringwald } 19431b0e3922Smatthias.ringwald 194409ba8edeSmatthias.ringwald #ifndef EMBEDDED 19451b0e3922Smatthias.ringwald void hci_emit_btstack_version() { 1946e0abb8e7S[email protected] log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR); 1947425d1371Smatthias.ringwald uint8_t event[6]; 19481b0e3922Smatthias.ringwald event[0] = BTSTACK_EVENT_VERSION; 1949425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1950425d1371Smatthias.ringwald event[2] = BTSTACK_MAJOR; 1951425d1371Smatthias.ringwald event[3] = BTSTACK_MINOR; 1952425d1371Smatthias.ringwald bt_store_16(event, 4, BTSTACK_REVISION); 1953425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 19543a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 19551b0e3922Smatthias.ringwald } 195609ba8edeSmatthias.ringwald #endif 19571b0e3922Smatthias.ringwald 19582ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){ 1959e0abb8e7S[email protected] log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled); 1960425d1371Smatthias.ringwald uint8_t event[3]; 19612ed6235cSmatthias.ringwald event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED; 1962425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 19632ed6235cSmatthias.ringwald event[2] = enabled; 1964425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 19653a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 19662ed6235cSmatthias.ringwald } 1967627c2f45Smatthias.ringwald 1968627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){ 1969e0abb8e7S[email protected] uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info 1970627c2f45Smatthias.ringwald event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED; 1971e0abb8e7S[email protected] event[1] = sizeof(event) - 2 - 1; 1972f653b6bdSmatthias.ringwald event[2] = 0; // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 19732f89d309Smatthias.ringwald bt_flip_addr(&event[3], *addr); 1974f653b6bdSmatthias.ringwald memcpy(&event[9], name, 248); 1975e0abb8e7S[email protected] 1976e0abb8e7S[email protected] event[9+248] = 0; // assert \0 for log_info 1977e0abb8e7S[email protected] log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]); 1978e0abb8e7S[email protected] 1979e0abb8e7S[email protected] hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1); 19803a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1); 1981627c2f45Smatthias.ringwald } 1982381fbed8Smatthias.ringwald 1983381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){ 1984e0abb8e7S[email protected] log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled); 1985425d1371Smatthias.ringwald uint8_t event[3]; 1986381fbed8Smatthias.ringwald event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED; 1987425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1988381fbed8Smatthias.ringwald event[2] = enabled; 1989425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 19903a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1991381fbed8Smatthias.ringwald } 1992458bf4e8S[email protected] 1993a00031e2S[email protected] void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){ 1994df3354fcS[email protected] log_info("hci_emit_security_level %u for handle %x", level, con_handle); 1995a00031e2S[email protected] uint8_t event[5]; 1996e00caf9cS[email protected] int pos = 0; 1997a00031e2S[email protected] event[pos++] = GAP_SECURITY_LEVEL; 1998e00caf9cS[email protected] event[pos++] = sizeof(event) - 2; 1999a00031e2S[email protected] bt_store_16(event, 2, con_handle); 2000e00caf9cS[email protected] pos += 2; 2001e00caf9cS[email protected] event[pos++] = level; 2002e00caf9cS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 20033a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 2004e00caf9cS[email protected] } 2005e00caf9cS[email protected] 2006ad83dc6aS[email protected] void hci_emit_dedicated_bonding_result(hci_connection_t * connection, uint8_t status){ 2007ad83dc6aS[email protected] log_info("hci_emit_dedicated_bonding_result %u ", status); 2008ad83dc6aS[email protected] uint8_t event[9]; 2009ad83dc6aS[email protected] int pos = 0; 2010ad83dc6aS[email protected] event[pos++] = GAP_DEDICATED_BONDING_COMPLETED; 2011ad83dc6aS[email protected] event[pos++] = sizeof(event) - 2; 2012ad83dc6aS[email protected] event[pos++] = status; 2013ad83dc6aS[email protected] bt_flip_addr( * (bd_addr_t *) &event[pos], connection->address); 2014ad83dc6aS[email protected] pos += 6; 2015ad83dc6aS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 20163a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 2017ad83dc6aS[email protected] } 2018ad83dc6aS[email protected] 20192bd8b7e7S[email protected] // query if remote side supports SSP 20202bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){ 20212bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 20222bd8b7e7S[email protected] if (!connection) return 0; 20232bd8b7e7S[email protected] return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0; 20242bd8b7e7S[email protected] } 20252bd8b7e7S[email protected] 2026df3354fcS[email protected] int hci_ssp_supported_on_both_sides(hci_con_handle_t handle){ 2027df3354fcS[email protected] return hci_local_ssp_activated() && hci_remote_ssp_supported(handle); 2028df3354fcS[email protected] } 2029df3354fcS[email protected] 2030458bf4e8S[email protected] // GAP API 2031458bf4e8S[email protected] /** 2032458bf4e8S[email protected] * @bbrief enable/disable bonding. default is enabled 2033458bf4e8S[email protected] * @praram enabled 2034458bf4e8S[email protected] */ 20354c57c146S[email protected] void gap_set_bondable_mode(int enable){ 20363a9fb326S[email protected] hci_stack->bondable = enable ? 1 : 0; 2037458bf4e8S[email protected] } 2038cb230b9dS[email protected] 2039cb230b9dS[email protected] /** 204034d2123cS[email protected] * @brief map link keys to security levels 2041cb230b9dS[email protected] */ 204234d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){ 204334d2123cS[email protected] switch (link_key_type){ 20443c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 20453c68dfa9S[email protected] return LEVEL_4; 20463c68dfa9S[email protected] case COMBINATION_KEY: 20473c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 20483c68dfa9S[email protected] return LEVEL_3; 20493c68dfa9S[email protected] default: 20503c68dfa9S[email protected] return LEVEL_2; 20513c68dfa9S[email protected] } 2052cb230b9dS[email protected] } 2053cb230b9dS[email protected] 2054a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){ 205534d2123cS[email protected] if (!connection) return LEVEL_0; 205634d2123cS[email protected] if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0; 205734d2123cS[email protected] return gap_security_level_for_link_key_type(connection->link_key_type); 205834d2123cS[email protected] } 205934d2123cS[email protected] 206034d2123cS[email protected] 2061106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){ 206282d8f825S[email protected] printf("gap_mitm_protection_required_for_security_level %u\n", level); 2063106d6d11S[email protected] return level > LEVEL_2; 2064106d6d11S[email protected] } 2065106d6d11S[email protected] 206634d2123cS[email protected] /** 206734d2123cS[email protected] * @brief get current security level 206834d2123cS[email protected] */ 206934d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){ 207034d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 207134d2123cS[email protected] if (!connection) return LEVEL_0; 207234d2123cS[email protected] return gap_security_level_for_connection(connection); 207334d2123cS[email protected] } 207434d2123cS[email protected] 2075cb230b9dS[email protected] /** 2076cb230b9dS[email protected] * @brief request connection to device to 2077cb230b9dS[email protected] * @result GAP_AUTHENTICATION_RESULT 2078cb230b9dS[email protected] */ 207934d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){ 208034d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 208134d2123cS[email protected] if (!connection){ 2082a00031e2S[email protected] hci_emit_security_level(con_handle, LEVEL_0); 208334d2123cS[email protected] return; 208434d2123cS[email protected] } 208534d2123cS[email protected] gap_security_level_t current_level = gap_security_level(con_handle); 208634d2123cS[email protected] log_info("gap_request_security_level %u, current level %u", requested_level, current_level); 208734d2123cS[email protected] if (current_level >= requested_level){ 2088a00031e2S[email protected] hci_emit_security_level(con_handle, current_level); 208934d2123cS[email protected] return; 209034d2123cS[email protected] } 2091a00031e2S[email protected] 209234d2123cS[email protected] connection->requested_security_level = requested_level; 2093a00031e2S[email protected] 2094fb8ba0dbS[email protected] // would enabling ecnryption suffice (>= LEVEL_2)? 20953a9fb326S[email protected] if (hci_stack->remote_device_db){ 2096a00031e2S[email protected] link_key_type_t link_key_type; 2097a00031e2S[email protected] link_key_t link_key; 20983a9fb326S[email protected] if (hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)){ 2099a00031e2S[email protected] if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){ 2100a00031e2S[email protected] connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 2101a00031e2S[email protected] return; 2102a00031e2S[email protected] } 2103a00031e2S[email protected] } 2104a00031e2S[email protected] } 2105a00031e2S[email protected] 21061eb2563eS[email protected] // try to authenticate connection 21071eb2563eS[email protected] connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 2108e80b2cf9S[email protected] hci_run(); 2109e00caf9cS[email protected] } 2110ad83dc6aS[email protected] 2111ad83dc6aS[email protected] /** 2112ad83dc6aS[email protected] * @brief start dedicated bonding with device. disconnect after bonding 2113ad83dc6aS[email protected] * @param device 2114ad83dc6aS[email protected] * @param request MITM protection 2115ad83dc6aS[email protected] * @result GAP_DEDICATED_BONDING_COMPLETE 2116ad83dc6aS[email protected] */ 2117ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){ 2118ad83dc6aS[email protected] 2119ad83dc6aS[email protected] // create connection state machine 212096a45072S[email protected] hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_CLASSIC); 2121ad83dc6aS[email protected] 2122ad83dc6aS[email protected] if (!connection){ 2123ad83dc6aS[email protected] return BTSTACK_MEMORY_ALLOC_FAILED; 2124ad83dc6aS[email protected] } 2125ad83dc6aS[email protected] 2126ad83dc6aS[email protected] // delete linkn key 2127ad83dc6aS[email protected] hci_drop_link_key_for_bd_addr( (bd_addr_t *) &device); 2128ad83dc6aS[email protected] 2129ad83dc6aS[email protected] // configure LEVEL_2/3, dedicated bonding 2130ad83dc6aS[email protected] connection->state = SEND_CREATE_CONNECTION; 2131ad83dc6aS[email protected] connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2; 213282d8f825S[email protected] printf("gap_dedicated_bonding, mitm %u -> level %u\n", mitm_protection_required, connection->requested_security_level); 2133ad83dc6aS[email protected] connection->bonding_flags = BONDING_DEDICATED; 2134ad83dc6aS[email protected] 2135ad83dc6aS[email protected] // wait for GAP Security Result and send GAP Dedicated Bonding complete 2136ad83dc6aS[email protected] 2137ad83dc6aS[email protected] // handle: connnection failure (connection complete != ok) 2138ad83dc6aS[email protected] // handle: authentication failure 2139ad83dc6aS[email protected] // handle: disconnect on done 2140ad83dc6aS[email protected] 2141ad83dc6aS[email protected] hci_run(); 2142ad83dc6aS[email protected] 2143ad83dc6aS[email protected] return 0; 2144ad83dc6aS[email protected] } 21458e618f72S[email protected] 21468e618f72S[email protected] void gap_set_local_name(const char * local_name){ 21478e618f72S[email protected] hci_stack->local_name = local_name; 21488e618f72S[email protected] } 21498e618f72S[email protected] 21507bdc6798S[email protected] le_command_status_t le_central_start_scan(){ 21517bdc6798S[email protected] printf("hci_init: scanning state START_SCAN\n"); 21527bdc6798S[email protected] hci_stack->le_scanning_state = LE_START_SCAN; 21537bdc6798S[email protected] hci_run(); 21547bdc6798S[email protected] return BLE_PERIPHERAL_OK; 21557bdc6798S[email protected] } 21568e618f72S[email protected] 21577bdc6798S[email protected] le_command_status_t le_central_stop_scan(){ 21587bdc6798S[email protected] hci_stack->le_scanning_state = LE_STOP_SCAN; 21597bdc6798S[email protected] hci_run(); 21607bdc6798S[email protected] return BLE_PERIPHERAL_OK; 21617bdc6798S[email protected] } 21624f3229d8S[email protected] 21634f3229d8S[email protected] 21644f3229d8S[email protected] le_command_status_t le_central_connect(bd_addr_t * addr, bd_addr_type_t addr_type){ 21654f3229d8S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 21664f3229d8S[email protected] if (!conn){ 21674f3229d8S[email protected] conn = create_connection_for_bd_addr_and_type(*addr, addr_type); 21684f3229d8S[email protected] if (!conn){ 21694f3229d8S[email protected] // notify client that alloc failed 21704f3229d8S[email protected] hci_emit_le_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED); 21714f3229d8S[email protected] return BLE_PERIPHERAL_NOT_CONNECTED; // don't sent packet to controller 21724f3229d8S[email protected] } 21734f3229d8S[email protected] conn->state = SEND_CREATE_CONNECTION; 21744f3229d8S[email protected] return BLE_PERIPHERAL_OK; 21754f3229d8S[email protected] } 21764f3229d8S[email protected] conn->state = OPEN; 21774f3229d8S[email protected] hci_emit_le_connection_complete(conn, 0); 21784f3229d8S[email protected] hci_run(); 21794f3229d8S[email protected] return BLE_PERIPHERAL_OK; 21804f3229d8S[email protected] } 21814f3229d8S[email protected] 21824f3229d8S[email protected] 21835917a5c5S[email protected] le_command_status_t gap_disconnect(hci_con_handle_t handle){ 21845917a5c5S[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 21855917a5c5S[email protected] if (!conn){ 21865917a5c5S[email protected] hci_emit_le_connection_complete(conn, 0); 21875917a5c5S[email protected] return BLE_PERIPHERAL_OK; 21885917a5c5S[email protected] } 21895917a5c5S[email protected] conn->state = SEND_DISCONNECT; 21905917a5c5S[email protected] hci_run(); 21914f3229d8S[email protected] return BLE_PERIPHERAL_OK; 21924f3229d8S[email protected] } 2193