11f504dbdSmatthias.ringwald /* 26b64433eSmatthias.ringwald * Copyright (C) 2009-2012 by Matthias Ringwald 31713bceaSmatthias.ringwald * 41713bceaSmatthias.ringwald * Redistribution and use in source and binary forms, with or without 51713bceaSmatthias.ringwald * modification, are permitted provided that the following conditions 61713bceaSmatthias.ringwald * are met: 71713bceaSmatthias.ringwald * 81713bceaSmatthias.ringwald * 1. Redistributions of source code must retain the above copyright 91713bceaSmatthias.ringwald * notice, this list of conditions and the following disclaimer. 101713bceaSmatthias.ringwald * 2. Redistributions in binary form must reproduce the above copyright 111713bceaSmatthias.ringwald * notice, this list of conditions and the following disclaimer in the 121713bceaSmatthias.ringwald * documentation and/or other materials provided with the distribution. 131713bceaSmatthias.ringwald * 3. Neither the name of the copyright holders nor the names of 141713bceaSmatthias.ringwald * contributors may be used to endorse or promote products derived 151713bceaSmatthias.ringwald * from this software without specific prior written permission. 166b64433eSmatthias.ringwald * 4. Any redistribution, use, or modification is done solely for 176b64433eSmatthias.ringwald * personal benefit and not for any commercial purpose or for 186b64433eSmatthias.ringwald * monetary gain. 191713bceaSmatthias.ringwald * 201713bceaSmatthias.ringwald * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS 211713bceaSmatthias.ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 221713bceaSmatthias.ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 231713bceaSmatthias.ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 241713bceaSmatthias.ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 251713bceaSmatthias.ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 261713bceaSmatthias.ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 271713bceaSmatthias.ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 281713bceaSmatthias.ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 291713bceaSmatthias.ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 301713bceaSmatthias.ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311713bceaSmatthias.ringwald * SUCH DAMAGE. 321713bceaSmatthias.ringwald * 336b64433eSmatthias.ringwald * Please inquire about commercial licensing options at [email protected] 346b64433eSmatthias.ringwald * 351713bceaSmatthias.ringwald */ 361713bceaSmatthias.ringwald 371713bceaSmatthias.ringwald /* 381f504dbdSmatthias.ringwald * hci.c 391f504dbdSmatthias.ringwald * 401f504dbdSmatthias.ringwald * Created by Matthias Ringwald on 4/29/09. 411f504dbdSmatthias.ringwald * 421f504dbdSmatthias.ringwald */ 431f504dbdSmatthias.ringwald 44bde315ceS[email protected] #include "btstack-config.h" 4528171530Smatthias.ringwald 467f2435e6Smatthias.ringwald #include "hci.h" 474c57c146S[email protected] #include "gap.h" 487f2435e6Smatthias.ringwald 4993b8dc03Smatthias.ringwald #include <stdarg.h> 5093b8dc03Smatthias.ringwald #include <string.h> 5156fe0872Smatthias.ringwald #include <stdio.h> 527f2435e6Smatthias.ringwald 53549e6ebeSmatthias.ringwald #ifndef EMBEDDED 54549e6ebeSmatthias.ringwald #include <unistd.h> // gethostbyname 5509ba8edeSmatthias.ringwald #include <btstack/version.h> 56549e6ebeSmatthias.ringwald #endif 57549e6ebeSmatthias.ringwald 58a3b02b71Smatthias.ringwald #include "btstack_memory.h" 597f2435e6Smatthias.ringwald #include "debug.h" 60d8905019Smatthias.ringwald #include "hci_dump.h" 6193b8dc03Smatthias.ringwald 62ae1fd9f3Smatthias.ringwald #include <btstack/hci_cmds.h> 631b0e3922Smatthias.ringwald 64169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000 65ee091cf1Smatthias.ringwald 66a45d6b9fS[email protected] #define HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP 11 67da5275c5S[email protected] 6828171530Smatthias.ringwald #ifdef USE_BLUETOOL 6928171530Smatthias.ringwald #include "bt_control_iphone.h" 7028171530Smatthias.ringwald #endif 7128171530Smatthias.ringwald 72758b46ceSmatthias.ringwald static void hci_update_scan_enable(void); 73a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection); 74758b46ceSmatthias.ringwald 7506b35ec0Smatthias.ringwald // the STACK is here 76*3a9fb326S[email protected] #ifndef HAVE_MALLOC 77*3a9fb326S[email protected] static hci_stack_t hci_stack_static; 78*3a9fb326S[email protected] #endif 79*3a9fb326S[email protected] static hci_stack_t * hci_stack = NULL; 8016833f0aSmatthias.ringwald 8197addcc5Smatthias.ringwald /** 82ee091cf1Smatthias.ringwald * get connection for a given handle 83ee091cf1Smatthias.ringwald * 84ee091cf1Smatthias.ringwald * @return connection OR NULL, if not found 85ee091cf1Smatthias.ringwald */ 865061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){ 87ee091cf1Smatthias.ringwald linked_item_t *it; 88*3a9fb326S[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){ 89ee091cf1Smatthias.ringwald if ( ((hci_connection_t *) it)->con_handle == con_handle){ 90ee091cf1Smatthias.ringwald return (hci_connection_t *) it; 91ee091cf1Smatthias.ringwald } 92ee091cf1Smatthias.ringwald } 93ee091cf1Smatthias.ringwald return NULL; 94ee091cf1Smatthias.ringwald } 95ee091cf1Smatthias.ringwald 962b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){ 9728ca2b46S[email protected] hci_connection_t * connection = (hci_connection_t *) linked_item_get_user(&timer->item); 98c785ef68Smatthias.ringwald #ifdef HAVE_TIME 99ee091cf1Smatthias.ringwald struct timeval tv; 100ee091cf1Smatthias.ringwald gettimeofday(&tv, NULL); 101c21e6239Smatthias.ringwald if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) { 102ee091cf1Smatthias.ringwald // connections might be timed out 103ee091cf1Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 104ee091cf1Smatthias.ringwald } 1052b12a0b9Smatthias.ringwald #endif 106e5780900Smatthias.ringwald #ifdef HAVE_TICK 107c785ef68Smatthias.ringwald if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){ 108c785ef68Smatthias.ringwald // connections might be timed out 109c785ef68Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 110c785ef68Smatthias.ringwald } 111c785ef68Smatthias.ringwald #endif 112c785ef68Smatthias.ringwald run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS); 113c785ef68Smatthias.ringwald run_loop_add_timer(timer); 114c785ef68Smatthias.ringwald } 115ee091cf1Smatthias.ringwald 116ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){ 117c7492964Smatthias.ringwald #ifdef HAVE_TIME 118ee091cf1Smatthias.ringwald gettimeofday(&connection->timestamp, NULL); 119c7492964Smatthias.ringwald #endif 120e5780900Smatthias.ringwald #ifdef HAVE_TICK 121c785ef68Smatthias.ringwald connection->timestamp = embedded_get_ticks(); 122c785ef68Smatthias.ringwald #endif 123ee091cf1Smatthias.ringwald } 124ee091cf1Smatthias.ringwald 125ee091cf1Smatthias.ringwald /** 126c8e4258aSmatthias.ringwald * create connection for given address 127c8e4258aSmatthias.ringwald * 12817f1ba2aSmatthias.ringwald * @return connection OR NULL, if no memory left 129c8e4258aSmatthias.ringwald */ 130c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){ 13128ca2b46S[email protected] hci_connection_t * conn = (hci_connection_t *) btstack_memory_hci_connection_get(); 132c8e4258aSmatthias.ringwald if (!conn) return NULL; 133c8e4258aSmatthias.ringwald BD_ADDR_COPY(conn->address, addr); 134c8e4258aSmatthias.ringwald conn->con_handle = 0xffff; 1357d3b3569Smatthias.ringwald conn->authentication_flags = AUTH_FLAGS_NONE; 136afd4e962S[email protected] conn->bonding_flags = 0; 13734d2123cS[email protected] conn->requested_security_level = LEVEL_0; 138ee091cf1Smatthias.ringwald linked_item_set_user(&conn->timeout.item, conn); 139ee091cf1Smatthias.ringwald conn->timeout.process = hci_connection_timeout_handler; 140ee091cf1Smatthias.ringwald hci_connection_timestamp(conn); 141d55db49eSmatthias.ringwald conn->acl_recombination_length = 0; 1427856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 14356cf178bSmatthias.ringwald conn->num_acl_packets_sent = 0; 144*3a9fb326S[email protected] linked_list_add(&hci_stack->connections, (linked_item_t *) conn); 145c8e4258aSmatthias.ringwald return conn; 146c8e4258aSmatthias.ringwald } 147c8e4258aSmatthias.ringwald 148c8e4258aSmatthias.ringwald /** 14906b35ec0Smatthias.ringwald * get connection for given address 15097addcc5Smatthias.ringwald * 15197addcc5Smatthias.ringwald * @return connection OR NULL, if not found 15297addcc5Smatthias.ringwald */ 153fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){ 15406b35ec0Smatthias.ringwald linked_item_t *it; 155*3a9fb326S[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){ 15606b35ec0Smatthias.ringwald if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){ 15706b35ec0Smatthias.ringwald return (hci_connection_t *) it; 15806b35ec0Smatthias.ringwald } 15906b35ec0Smatthias.ringwald } 16006b35ec0Smatthias.ringwald return NULL; 16106b35ec0Smatthias.ringwald } 16206b35ec0Smatthias.ringwald 16328ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 16428ca2b46S[email protected] conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags); 16528ca2b46S[email protected] } 16628ca2b46S[email protected] 16728ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 16828ca2b46S[email protected] conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags); 16928ca2b46S[email protected] } 17028ca2b46S[email protected] 17128ca2b46S[email protected] 17243bfb1bdSmatthias.ringwald /** 17380ca58a0Smatthias.ringwald * add authentication flags and reset timer 1747fde4af9Smatthias.ringwald */ 1757fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){ 1767fde4af9Smatthias.ringwald bd_addr_t addr; 1777fde4af9Smatthias.ringwald bt_flip_addr(addr, *(bd_addr_t *) bd_addr); 1787fde4af9Smatthias.ringwald hci_connection_t * conn = connection_for_address(addr); 1797fde4af9Smatthias.ringwald if (conn) { 18028ca2b46S[email protected] connectionSetAuthenticationFlags(conn, flags); 18180ca58a0Smatthias.ringwald hci_connection_timestamp(conn); 1827fde4af9Smatthias.ringwald } 1837fde4af9Smatthias.ringwald } 1847fde4af9Smatthias.ringwald 18580ca58a0Smatthias.ringwald int hci_authentication_active_for_handle(hci_con_handle_t handle){ 1865061f3afS[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 18780ca58a0Smatthias.ringwald if (!conn) return 0; 1886724cd9eS[email protected] if (conn->authentication_flags & LEGACY_PAIRING_ACTIVE) return 1; 1896724cd9eS[email protected] if (conn->authentication_flags & SSP_PAIRING_ACTIVE) return 1; 1906724cd9eS[email protected] return 0; 19180ca58a0Smatthias.ringwald } 19280ca58a0Smatthias.ringwald 193c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){ 194*3a9fb326S[email protected] if (hci_stack->remote_device_db) { 195*3a9fb326S[email protected] hci_stack->remote_device_db->delete_link_key(addr); 196c12e46e7Smatthias.ringwald } 197c12e46e7Smatthias.ringwald } 198c12e46e7Smatthias.ringwald 1997fde4af9Smatthias.ringwald 2007fde4af9Smatthias.ringwald /** 20143bfb1bdSmatthias.ringwald * count connections 20243bfb1bdSmatthias.ringwald */ 20340d1c7a4Smatthias.ringwald static int nr_hci_connections(void){ 20456c253c9Smatthias.ringwald int count = 0; 20543bfb1bdSmatthias.ringwald linked_item_t *it; 206*3a9fb326S[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next, count++); 20743bfb1bdSmatthias.ringwald return count; 20843bfb1bdSmatthias.ringwald } 209c8e4258aSmatthias.ringwald 21097addcc5Smatthias.ringwald /** 211ba681a6cSmatthias.ringwald * Dummy handler called by HCI 21216833f0aSmatthias.ringwald */ 2132718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 21416833f0aSmatthias.ringwald } 21516833f0aSmatthias.ringwald 216998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){ 2175061f3afS[email protected] hci_connection_t * connection = hci_connection_for_handle(handle); 218998906cdSmatthias.ringwald if (!connection) { 2197d67539fSmatthias.ringwald log_error("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle); 220998906cdSmatthias.ringwald return 0; 221998906cdSmatthias.ringwald } 222998906cdSmatthias.ringwald return connection->num_acl_packets_sent; 223998906cdSmatthias.ringwald } 224998906cdSmatthias.ringwald 225998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){ 226*3a9fb326S[email protected] uint8_t free_slots = hci_stack->total_num_acl_packets; 227998906cdSmatthias.ringwald linked_item_t *it; 228*3a9fb326S[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){ 229998906cdSmatthias.ringwald hci_connection_t * connection = (hci_connection_t *) it; 230998906cdSmatthias.ringwald if (free_slots < connection->num_acl_packets_sent) { 2317d67539fSmatthias.ringwald log_error("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n"); 232998906cdSmatthias.ringwald return 0; 233998906cdSmatthias.ringwald } 234998906cdSmatthias.ringwald free_slots -= connection->num_acl_packets_sent; 235998906cdSmatthias.ringwald } 236998906cdSmatthias.ringwald return free_slots; 237998906cdSmatthias.ringwald } 238998906cdSmatthias.ringwald 239c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){ 2402b12a0b9Smatthias.ringwald 2412b12a0b9Smatthias.ringwald // check for async hci transport implementations 242*3a9fb326S[email protected] if (hci_stack->hci_transport->can_send_packet_now){ 243*3a9fb326S[email protected] if (!hci_stack->hci_transport->can_send_packet_now(packet_type)){ 2442b12a0b9Smatthias.ringwald return 0; 2452b12a0b9Smatthias.ringwald } 2462b12a0b9Smatthias.ringwald } 2472b12a0b9Smatthias.ringwald 2482b12a0b9Smatthias.ringwald // check regular Bluetooth flow control 249c24735b1Smatthias.ringwald switch (packet_type) { 250c24735b1Smatthias.ringwald case HCI_ACL_DATA_PACKET: 251c24735b1Smatthias.ringwald return hci_number_free_acl_slots(); 252c24735b1Smatthias.ringwald case HCI_COMMAND_DATA_PACKET: 253*3a9fb326S[email protected] return hci_stack->num_cmd_packets; 254c24735b1Smatthias.ringwald default: 255c24735b1Smatthias.ringwald return 0; 256c24735b1Smatthias.ringwald } 257c24735b1Smatthias.ringwald } 258c24735b1Smatthias.ringwald 259ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){ 2607856c818Smatthias.ringwald 2616218e6f1Smatthias.ringwald // check for free places on BT module 2625932f1b4Smatthias.ringwald if (!hci_number_free_acl_slots()) return BTSTACK_ACL_BUFFERS_FULL; 2636218e6f1Smatthias.ringwald 2647856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 2655061f3afS[email protected] hci_connection_t *connection = hci_connection_for_handle( con_handle); 26656cf178bSmatthias.ringwald if (!connection) return 0; 26756cf178bSmatthias.ringwald hci_connection_timestamp(connection); 26856cf178bSmatthias.ringwald 26956cf178bSmatthias.ringwald // count packet 27056cf178bSmatthias.ringwald connection->num_acl_packets_sent++; 2717b5fbe1fSmatthias.ringwald // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent); 2727856c818Smatthias.ringwald 27300d8e42eSmatthias.ringwald // send packet 274*3a9fb326S[email protected] int err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size); 2756218e6f1Smatthias.ringwald 27600d8e42eSmatthias.ringwald return err; 277ee091cf1Smatthias.ringwald } 278ee091cf1Smatthias.ringwald 27916833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){ 2807856c818Smatthias.ringwald 281e76a89eeS[email protected] // log_info("acl_handler: size %u", size); 282e76a89eeS[email protected] 2837856c818Smatthias.ringwald // get info 2847856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 2855061f3afS[email protected] hci_connection_t *conn = hci_connection_for_handle(con_handle); 2867856c818Smatthias.ringwald uint8_t acl_flags = READ_ACL_FLAGS(packet); 2877856c818Smatthias.ringwald uint16_t acl_length = READ_ACL_LENGTH(packet); 2887856c818Smatthias.ringwald 2897856c818Smatthias.ringwald // ignore non-registered handle 2907856c818Smatthias.ringwald if (!conn){ 2917d67539fSmatthias.ringwald log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle); 2927856c818Smatthias.ringwald return; 2937856c818Smatthias.ringwald } 2947856c818Smatthias.ringwald 295e76a89eeS[email protected] // assert packet is complete 2969ecc3e17S[email protected] if (acl_length + 4 != size){ 297e76a89eeS[email protected] log_error("hci.c: acl_handler called with ACL packet of wrong size %u, expected %u => dropping packet", size, acl_length + 4); 298e76a89eeS[email protected] return; 299e76a89eeS[email protected] } 300e76a89eeS[email protected] 3017856c818Smatthias.ringwald // update idle timestamp 3027856c818Smatthias.ringwald hci_connection_timestamp(conn); 3037856c818Smatthias.ringwald 3047856c818Smatthias.ringwald // handle different packet types 3057856c818Smatthias.ringwald switch (acl_flags & 0x03) { 3067856c818Smatthias.ringwald 3077856c818Smatthias.ringwald case 0x01: // continuation fragment 3087856c818Smatthias.ringwald 3097856c818Smatthias.ringwald // sanity check 3107856c818Smatthias.ringwald if (conn->acl_recombination_pos == 0) { 3117d67539fSmatthias.ringwald log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle); 3127856c818Smatthias.ringwald return; 3137856c818Smatthias.ringwald } 3147856c818Smatthias.ringwald 3157856c818Smatthias.ringwald // append fragment payload (header already stored) 3167856c818Smatthias.ringwald memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length ); 3177856c818Smatthias.ringwald conn->acl_recombination_pos += acl_length; 3187856c818Smatthias.ringwald 3197d67539fSmatthias.ringwald // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length, 320decc01a8Smatthias.ringwald // conn->acl_recombination_pos, conn->acl_recombination_length); 3217856c818Smatthias.ringwald 3227856c818Smatthias.ringwald // forward complete L2CAP packet if complete. 3237856c818Smatthias.ringwald if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header 3247856c818Smatthias.ringwald 325*3a9fb326S[email protected] hci_stack->packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos); 3267856c818Smatthias.ringwald // reset recombination buffer 3277856c818Smatthias.ringwald conn->acl_recombination_length = 0; 3287856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 3297856c818Smatthias.ringwald } 3307856c818Smatthias.ringwald break; 3317856c818Smatthias.ringwald 3327856c818Smatthias.ringwald case 0x02: { // first fragment 3337856c818Smatthias.ringwald 3347856c818Smatthias.ringwald // sanity check 3357856c818Smatthias.ringwald if (conn->acl_recombination_pos) { 3367d67539fSmatthias.ringwald log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle); 3377856c818Smatthias.ringwald return; 3387856c818Smatthias.ringwald } 3397856c818Smatthias.ringwald 3407856c818Smatthias.ringwald // peek into L2CAP packet! 3417856c818Smatthias.ringwald uint16_t l2cap_length = READ_L2CAP_LENGTH( packet ); 3427856c818Smatthias.ringwald 343e76a89eeS[email protected] // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length); 344decc01a8Smatthias.ringwald 3457856c818Smatthias.ringwald // compare fragment size to L2CAP packet size 3467856c818Smatthias.ringwald if (acl_length >= l2cap_length + 4){ 3477856c818Smatthias.ringwald 3487856c818Smatthias.ringwald // forward fragment as L2CAP packet 349*3a9fb326S[email protected] hci_stack->packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4); 3507856c818Smatthias.ringwald 3517856c818Smatthias.ringwald } else { 3527856c818Smatthias.ringwald // store first fragment and tweak acl length for complete package 3537856c818Smatthias.ringwald memcpy(conn->acl_recombination_buffer, packet, acl_length + 4); 3547856c818Smatthias.ringwald conn->acl_recombination_pos = acl_length + 4; 3557856c818Smatthias.ringwald conn->acl_recombination_length = l2cap_length; 356decc01a8Smatthias.ringwald bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4); 3577856c818Smatthias.ringwald } 3587856c818Smatthias.ringwald break; 3597856c818Smatthias.ringwald 3607856c818Smatthias.ringwald } 3617856c818Smatthias.ringwald default: 3627d67539fSmatthias.ringwald log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03); 3637856c818Smatthias.ringwald return; 3647856c818Smatthias.ringwald } 36594ab26f8Smatthias.ringwald 36694ab26f8Smatthias.ringwald // execute main loop 36794ab26f8Smatthias.ringwald hci_run(); 36816833f0aSmatthias.ringwald } 36922909952Smatthias.ringwald 37067a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){ 371339b6768Smatthias.ringwald log_info("Connection closed: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 3723c4d4b90Smatthias.ringwald 3733c4d4b90Smatthias.ringwald // cancel all l2cap connections 3743c4d4b90Smatthias.ringwald hci_emit_disconnection_complete(conn->con_handle, 0x16); // terminated by local host 3753c4d4b90Smatthias.ringwald 376c7e0c5f6Smatthias.ringwald run_loop_remove_timer(&conn->timeout); 377c785ef68Smatthias.ringwald 378*3a9fb326S[email protected] linked_list_remove(&hci_stack->connections, (linked_item_t *) conn); 379a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 3803c4d4b90Smatthias.ringwald 3813c4d4b90Smatthias.ringwald // now it's gone 382c7e0c5f6Smatthias.ringwald hci_emit_nr_connections_changed(); 383c7e0c5f6Smatthias.ringwald } 384c7e0c5f6Smatthias.ringwald 3850c042179S[email protected] static const uint16_t packet_type_sizes[] = { 3868f8108aaSmatthias.ringwald 0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE, 3878f8108aaSmatthias.ringwald HCI_ACL_DH1_SIZE, 0, 0, 0, 3888f8108aaSmatthias.ringwald HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE, 3898f8108aaSmatthias.ringwald HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE 3908f8108aaSmatthias.ringwald }; 39165389bfcS[email protected] static const uint8_t packet_type_feature_requirement_bit[] = { 39265389bfcS[email protected] 0, // 3 slot packets 39365389bfcS[email protected] 1, // 5 slot packets 39465389bfcS[email protected] 25, // EDR 2 mpbs 39565389bfcS[email protected] 26, // EDR 3 mbps 39665389bfcS[email protected] 39, // 3 slot EDR packts 39765389bfcS[email protected] 40, // 5 slot EDR packet 39865389bfcS[email protected] }; 39965389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = { 40065389bfcS[email protected] 0x0f00, // 3 slot packets 40165389bfcS[email protected] 0xf000, // 5 slot packets 40265389bfcS[email protected] 0x1102, // EDR 2 mpbs 40365389bfcS[email protected] 0x2204, // EDR 3 mbps 40465389bfcS[email protected] 0x0300, // 3 slot EDR packts 40565389bfcS[email protected] 0x3000, // 5 slot EDR packet 40665389bfcS[email protected] }; 4078f8108aaSmatthias.ringwald 40865389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){ 40965389bfcS[email protected] // enable packet types based on size 4108f8108aaSmatthias.ringwald uint16_t packet_types = 0; 4118f8108aaSmatthias.ringwald int i; 4128f8108aaSmatthias.ringwald for (i=0;i<16;i++){ 4138f8108aaSmatthias.ringwald if (packet_type_sizes[i] == 0) continue; 4148f8108aaSmatthias.ringwald if (packet_type_sizes[i] <= buffer_size){ 4158f8108aaSmatthias.ringwald packet_types |= 1 << i; 4168f8108aaSmatthias.ringwald } 4178f8108aaSmatthias.ringwald } 41865389bfcS[email protected] // disable packet types due to missing local supported features 41965389bfcS[email protected] for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){ 42065389bfcS[email protected] int bit_idx = packet_type_feature_requirement_bit[i]; 42165389bfcS[email protected] int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0; 42265389bfcS[email protected] if (feature_set) continue; 42365389bfcS[email protected] log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]); 42465389bfcS[email protected] packet_types &= ~packet_type_feature_packet_mask[i]; 42565389bfcS[email protected] } 4268f8108aaSmatthias.ringwald // flip bits for "may not be used" 4278f8108aaSmatthias.ringwald packet_types ^= 0x3306; 4288f8108aaSmatthias.ringwald return packet_types; 4298f8108aaSmatthias.ringwald } 4308f8108aaSmatthias.ringwald 4318f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){ 432*3a9fb326S[email protected] return hci_stack->packet_types; 4338f8108aaSmatthias.ringwald } 4348f8108aaSmatthias.ringwald 4357dc17943Smatthias.ringwald uint8_t* hci_get_outgoing_acl_packet_buffer(void){ 4367dc17943Smatthias.ringwald // hci packet buffer is >= acl data packet length 437*3a9fb326S[email protected] return hci_stack->hci_packet_buffer; 4387dc17943Smatthias.ringwald } 4397dc17943Smatthias.ringwald 440f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){ 441*3a9fb326S[email protected] return hci_stack->acl_data_packet_length; 4427dc17943Smatthias.ringwald } 4437dc17943Smatthias.ringwald 444f5d8d141S[email protected] int hci_ssp_supported(void){ 445f5d8d141S[email protected] // No 51, byte 6, bit 3 446*3a9fb326S[email protected] return (hci_stack->local_supported_features[6] & (1 << 3)) != 0; 447f5d8d141S[email protected] } 448f5d8d141S[email protected] 449f5d8d141S[email protected] int hci_classic_supported(void){ 450f5d8d141S[email protected] // No 37, byte 4, bit 5, = No BR/EDR Support 451*3a9fb326S[email protected] return (hci_stack->local_supported_features[4] & (1 << 5)) == 0; 452f5d8d141S[email protected] } 453f5d8d141S[email protected] 454f5d8d141S[email protected] int hci_le_supported(void){ 455f5d8d141S[email protected] // No 37, byte 4, bit 6 = LE Supported (Controller) 456f5d8d141S[email protected] #ifdef HAVE_BLE 457*3a9fb326S[email protected] return (hci_stack->local_supported_features[4] & (1 << 6)) != 0; 458f5d8d141S[email protected] #else 459f5d8d141S[email protected] return 0; 460f5d8d141S[email protected] #endif 461f5d8d141S[email protected] } 462f5d8d141S[email protected] 46369a97523S[email protected] // get addr type and address used in advertisement packets 46418904abdS[email protected] void hci_le_advertisement_address(uint8_t * addr_type, bd_addr_t * addr){ 465*3a9fb326S[email protected] *addr_type = hci_stack->adv_addr_type; 466*3a9fb326S[email protected] if (hci_stack->adv_addr_type){ 467*3a9fb326S[email protected] memcpy(addr, hci_stack->adv_address, 6); 46869a97523S[email protected] } else { 469*3a9fb326S[email protected] memcpy(addr, hci_stack->local_bd_addr, 6); 47069a97523S[email protected] } 47169a97523S[email protected] } 47269a97523S[email protected] 47374ec757aSmatthias.ringwald // avoid huge local variables 474223aafc1Smatthias.ringwald #ifndef EMBEDDED 47574ec757aSmatthias.ringwald static device_name_t device_name; 476223aafc1Smatthias.ringwald #endif 47716833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){ 478e76a89eeS[email protected] 479e76a89eeS[email protected] uint16_t event_length = packet[1]; 480e76a89eeS[email protected] 481e76a89eeS[email protected] // assert packet is complete 482e76a89eeS[email protected] if (size != event_length + 2){ 483e76a89eeS[email protected] log_error("hci.c: event_handler called with event packet of wrong size %u, expected %u => dropping packet", size, event_length + 2); 484e76a89eeS[email protected] return; 485e76a89eeS[email protected] } 486e76a89eeS[email protected] 4871281a47eSmatthias.ringwald bd_addr_t addr; 4882d00edd4Smatthias.ringwald uint8_t link_type; 489fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 4901f7b95a1Smatthias.ringwald hci_connection_t * conn; 49156cf178bSmatthias.ringwald int i; 49222909952Smatthias.ringwald 4935909f7f2Smatthias.ringwald // printf("HCI:EVENT:%02x\n", packet[0]); 4945909f7f2Smatthias.ringwald 4956772a24cSmatthias.ringwald switch (packet[0]) { 49622909952Smatthias.ringwald 4976772a24cSmatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 4987ec5eeaaSmatthias.ringwald // get num cmd packets 499*3a9fb326S[email protected] // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack->num_cmd_packets, packet[2]); 500*3a9fb326S[email protected] hci_stack->num_cmd_packets = packet[2]; 5017ec5eeaaSmatthias.ringwald 502e2edc0c3Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){ 503e2edc0c3Smatthias.ringwald // from offset 5 504e2edc0c3Smatthias.ringwald // status 5051d279b20Smatthias.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" 506*3a9fb326S[email protected] hci_stack->acl_data_packet_length = READ_BT_16(packet, 6); 50756cf178bSmatthias.ringwald // ignore: SCO data packet len (8) 508*3a9fb326S[email protected] hci_stack->total_num_acl_packets = packet[9]; 50956cf178bSmatthias.ringwald // ignore: total num SCO packets 510*3a9fb326S[email protected] if (hci_stack->state == HCI_STATE_INITIALIZING){ 511a7a04bd9Smatthias.ringwald // determine usable ACL payload size 512*3a9fb326S[email protected] if (HCI_ACL_PAYLOAD_SIZE < hci_stack->acl_data_packet_length){ 513*3a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 5148f8108aaSmatthias.ringwald } 51565389bfcS[email protected] log_info("hci_read_buffer_size: used size %u, count %u\n", 516*3a9fb326S[email protected] hci_stack->acl_data_packet_length, hci_stack->total_num_acl_packets); 517e2edc0c3Smatthias.ringwald } 51856cf178bSmatthias.ringwald } 51965a46ef3S[email protected] #ifdef HAVE_BLE 52065a46ef3S[email protected] if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)){ 521*3a9fb326S[email protected] hci_stack->le_data_packet_length = READ_BT_16(packet, 6); 522*3a9fb326S[email protected] hci_stack->total_num_le_packets = packet[8]; 523*3a9fb326S[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); 52465a46ef3S[email protected] } 52565a46ef3S[email protected] #endif 526188981d2Smatthias.ringwald // Dump local address 527188981d2Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) { 528*3a9fb326S[email protected] bt_flip_addr(hci_stack->local_bd_addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]); 529188981d2Smatthias.ringwald log_info("Local Address, Status: 0x%02x: Addr: %s\n", 530*3a9fb326S[email protected] packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr)); 531188981d2Smatthias.ringwald } 532381fbed8Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 533*3a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 534381fbed8Smatthias.ringwald } 53565389bfcS[email protected] // Note: HCI init checks 536559e517eS[email protected] if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){ 537*3a9fb326S[email protected] memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8); 538559e517eS[email protected] log_info("Local Supported Features: 0x%02x%02x%02x%02x%02x%02x%02x%02x", 539*3a9fb326S[email protected] hci_stack->local_supported_features[0], hci_stack->local_supported_features[1], 540*3a9fb326S[email protected] hci_stack->local_supported_features[2], hci_stack->local_supported_features[3], 541*3a9fb326S[email protected] hci_stack->local_supported_features[4], hci_stack->local_supported_features[5], 542*3a9fb326S[email protected] hci_stack->local_supported_features[6], hci_stack->local_supported_features[7]); 54365389bfcS[email protected] 54465389bfcS[email protected] // determine usable ACL packet types based buffer size and supported features 545*3a9fb326S[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]); 546*3a9fb326S[email protected] log_info("packet types %04x", hci_stack->packet_types); 547f5d8d141S[email protected] 548f5d8d141S[email protected] // Classic/LE 549f5d8d141S[email protected] log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported()); 550559e517eS[email protected] } 55156cf178bSmatthias.ringwald break; 55256cf178bSmatthias.ringwald 5537ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 5547ec5eeaaSmatthias.ringwald // get num cmd packets 555*3a9fb326S[email protected] // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack->num_cmd_packets, packet[3]); 556*3a9fb326S[email protected] hci_stack->num_cmd_packets = packet[3]; 5577ec5eeaaSmatthias.ringwald break; 5587ec5eeaaSmatthias.ringwald 55956cf178bSmatthias.ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 56056cf178bSmatthias.ringwald for (i=0; i<packet[2];i++){ 56156cf178bSmatthias.ringwald handle = READ_BT_16(packet, 3 + 2*i); 56256cf178bSmatthias.ringwald uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i); 5635061f3afS[email protected] conn = hci_connection_for_handle(handle); 56456cf178bSmatthias.ringwald if (!conn){ 5657d67539fSmatthias.ringwald log_error("hci_number_completed_packet lists unused con handle %u\n", handle); 56656cf178bSmatthias.ringwald continue; 56756cf178bSmatthias.ringwald } 56856cf178bSmatthias.ringwald conn->num_acl_packets_sent -= num_packets; 5697b5fbe1fSmatthias.ringwald // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent); 57056cf178bSmatthias.ringwald } 5716772a24cSmatthias.ringwald break; 5726772a24cSmatthias.ringwald 5731f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 57437eaa4cfSmatthias.ringwald bt_flip_addr(addr, &packet[2]); 57537eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 5762d00edd4Smatthias.ringwald link_type = packet[11]; 577339b6768Smatthias.ringwald log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type); 57837eaa4cfSmatthias.ringwald if (link_type == 1) { // ACL 5791f7b95a1Smatthias.ringwald conn = connection_for_address(addr); 5801f7b95a1Smatthias.ringwald if (!conn) { 5811f7b95a1Smatthias.ringwald conn = create_connection_for_addr(addr); 5821f7b95a1Smatthias.ringwald } 583ce4c8fabSmatthias.ringwald if (!conn) { 584ce4c8fabSmatthias.ringwald // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 585*3a9fb326S[email protected] hci_stack->decline_reason = 0x0d; 586*3a9fb326S[email protected] BD_ADDR_COPY(hci_stack->decline_addr, addr); 587ce4c8fabSmatthias.ringwald break; 588ce4c8fabSmatthias.ringwald } 58932ab9390Smatthias.ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 59032ab9390Smatthias.ringwald hci_run(); 59137eaa4cfSmatthias.ringwald } else { 592ce4c8fabSmatthias.ringwald // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A) 593*3a9fb326S[email protected] hci_stack->decline_reason = 0x0a; 594*3a9fb326S[email protected] BD_ADDR_COPY(hci_stack->decline_addr, addr); 59537eaa4cfSmatthias.ringwald } 5961f7b95a1Smatthias.ringwald break; 5971f7b95a1Smatthias.ringwald 5986772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 599fe1ed1b8Smatthias.ringwald // Connection management 600fe1ed1b8Smatthias.ringwald bt_flip_addr(addr, &packet[5]); 601339b6768Smatthias.ringwald log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr)); 6021f7b95a1Smatthias.ringwald conn = connection_for_address(addr); 603fe1ed1b8Smatthias.ringwald if (conn) { 604b448a0e7Smatthias.ringwald if (!packet[2]){ 605c8e4258aSmatthias.ringwald conn->state = OPEN; 606fe1ed1b8Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 3); 607ad83dc6aS[email protected] conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES; 608ee091cf1Smatthias.ringwald 609c785ef68Smatthias.ringwald // restart timer 610c21e6239Smatthias.ringwald run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 611ee091cf1Smatthias.ringwald run_loop_add_timer(&conn->timeout); 612c785ef68Smatthias.ringwald 613339b6768Smatthias.ringwald log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 61443bfb1bdSmatthias.ringwald 61543bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 616b448a0e7Smatthias.ringwald } else { 617ad83dc6aS[email protected] // notify client if dedicated bonding 618ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 619ad83dc6aS[email protected] hci_emit_dedicated_bonding_result(conn, packet[2]); 620ad83dc6aS[email protected] } 621ad83dc6aS[email protected] 622b448a0e7Smatthias.ringwald // connection failed, remove entry 623*3a9fb326S[email protected] linked_list_remove(&hci_stack->connections, (linked_item_t *) conn); 624a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 625c12e46e7Smatthias.ringwald 626c12e46e7Smatthias.ringwald // if authentication error, also delete link key 627c12e46e7Smatthias.ringwald if (packet[2] == 0x05) { 628c12e46e7Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 629c12e46e7Smatthias.ringwald } 630fe1ed1b8Smatthias.ringwald } 631fe1ed1b8Smatthias.ringwald } 6326772a24cSmatthias.ringwald break; 633fe1ed1b8Smatthias.ringwald 634afd4e962S[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 635afd4e962S[email protected] handle = READ_BT_16(packet, 3); 636afd4e962S[email protected] conn = hci_connection_for_handle(handle); 637afd4e962S[email protected] if (!conn) break; 638afd4e962S[email protected] if (!packet[2]){ 639afd4e962S[email protected] uint8_t * features = &packet[5]; 640afd4e962S[email protected] if (features[6] & (1 << 3)){ 641afd4e962S[email protected] conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP; 642afd4e962S[email protected] } 643afd4e962S[email protected] } 644afd4e962S[email protected] conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES; 645ad83dc6aS[email protected] log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x", conn->bonding_flags); 646ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 647ad83dc6aS[email protected] conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 648ad83dc6aS[email protected] } 649afd4e962S[email protected] break; 650afd4e962S[email protected] 6517fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 6527b5fbe1fSmatthias.ringwald log_info("HCI_EVENT_LINK_KEY_REQUEST\n"); 6537fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST); 65474d716b5S[email protected] // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST 655*3a9fb326S[email protected] if (hci_stack->bondable && !hci_stack->remote_device_db) break; 65632ab9390Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST); 65764472d52Smatthias.ringwald hci_run(); 658d9a327f9Smatthias.ringwald // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set 65929d53098Smatthias.ringwald return; 6607fde4af9Smatthias.ringwald 6619ab95c90S[email protected] case HCI_EVENT_LINK_KEY_NOTIFICATION: { 66229d53098Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 6639ab95c90S[email protected] conn = connection_for_address(addr); 6649ab95c90S[email protected] if (!conn) break; 6659ab95c90S[email protected] conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION; 6669ab95c90S[email protected] link_key_type_t link_key_type = packet[24]; 6679ab95c90S[email protected] // Change Connection Encryption keeps link key type 6689ab95c90S[email protected] if (link_key_type != CHANGED_COMBINATION_KEY){ 6699ab95c90S[email protected] conn->link_key_type = link_key_type; 6709ab95c90S[email protected] } 671*3a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 672*3a9fb326S[email protected] hci_stack->remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8], conn->link_key_type); 67329d53098Smatthias.ringwald // still forward event to allow dismiss of pairing dialog 6747fde4af9Smatthias.ringwald break; 6759ab95c90S[email protected] } 6767fde4af9Smatthias.ringwald 6777fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 6786724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE); 6794c57c146S[email protected] // non-bondable mode: pin code negative reply will be sent 680*3a9fb326S[email protected] if (!hci_stack->bondable){ 681899283eaS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST); 682f8fb5f6eS[email protected] hci_run(); 683f8fb5f6eS[email protected] return; 6844c57c146S[email protected] } 685d9a327f9Smatthias.ringwald // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key 686*3a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 687d9a327f9Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 688*3a9fb326S[email protected] hci_stack->remote_device_db->delete_link_key(&addr); 6897fde4af9Smatthias.ringwald break; 6907fde4af9Smatthias.ringwald 6911d6b20aeS[email protected] case HCI_EVENT_IO_CAPABILITY_REQUEST: 6921d6b20aeS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST); 693dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY); 694dbe1a790S[email protected] break; 695dbe1a790S[email protected] 696dbe1a790S[email protected] case HCI_EVENT_USER_CONFIRMATION_REQUEST: 6976724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 698*3a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 699dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY); 700dbe1a790S[email protected] break; 701dbe1a790S[email protected] 702dbe1a790S[email protected] case HCI_EVENT_USER_PASSKEY_REQUEST: 7036724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 704*3a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 705dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY); 7061d6b20aeS[email protected] break; 7071d6b20aeS[email protected] 708f0944df2S[email protected] case HCI_EVENT_ENCRYPTION_CHANGE: 709f0944df2S[email protected] handle = READ_BT_16(packet, 3); 710f0944df2S[email protected] conn = hci_connection_for_handle(handle); 711f0944df2S[email protected] if (!conn) break; 712ad83dc6aS[email protected] if (packet[2] == 0) { 713f0944df2S[email protected] if (packet[5]){ 714f0944df2S[email protected] conn->authentication_flags |= CONNECTION_ENCRYPTED; 715f0944df2S[email protected] } else { 716536f9994S[email protected] conn->authentication_flags &= ~CONNECTION_ENCRYPTED; 717f0944df2S[email protected] } 718ad83dc6aS[email protected] } 719a00031e2S[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 720f0944df2S[email protected] break; 721f0944df2S[email protected] 7221eb2563eS[email protected] case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT: 7231eb2563eS[email protected] handle = READ_BT_16(packet, 3); 7241eb2563eS[email protected] conn = hci_connection_for_handle(handle); 7251eb2563eS[email protected] if (!conn) break; 726ad83dc6aS[email protected] 727ad83dc6aS[email protected] // dedicated bonding: send result and disconnect 728ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 729ad83dc6aS[email protected] conn->bonding_flags &= ~BONDING_DEDICATED; 730ad83dc6aS[email protected] hci_emit_dedicated_bonding_result( conn, packet[2]); 731ad83dc6aS[email protected] conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE; 732ad83dc6aS[email protected] break; 733ad83dc6aS[email protected] } 734ad83dc6aS[email protected] 735b5cb6874S[email protected] if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){ 7361eb2563eS[email protected] // link key sufficient for requested security 7371eb2563eS[email protected] conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 738ad83dc6aS[email protected] break; 739ad83dc6aS[email protected] } 7401eb2563eS[email protected] // not enough 7411eb2563eS[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 7421eb2563eS[email protected] break; 74334d2123cS[email protected] 744223aafc1Smatthias.ringwald #ifndef EMBEDDED 74574ec757aSmatthias.ringwald case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 746*3a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 74774ec757aSmatthias.ringwald if (packet[2]) break; // status not ok 74874ec757aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 7495a06394aSmatthias.ringwald // fix for invalid remote names - terminate on 0xff 7505a06394aSmatthias.ringwald for (i=0; i<248;i++){ 7515a06394aSmatthias.ringwald if (packet[9+i] == 0xff){ 7525a06394aSmatthias.ringwald packet[9+i] = 0; 7535a06394aSmatthias.ringwald break; 754cdc9101dSmatthias.ringwald } 7555a06394aSmatthias.ringwald } 756d2fe945cS[email protected] memset(&device_name, 0, sizeof(device_name_t)); 75774ec757aSmatthias.ringwald strncpy((char*) device_name, (char*) &packet[9], 248); 758*3a9fb326S[email protected] hci_stack->remote_device_db->put_name(&addr, &device_name); 75974ec757aSmatthias.ringwald break; 76074ec757aSmatthias.ringwald 76174ec757aSmatthias.ringwald case HCI_EVENT_INQUIRY_RESULT: 76274ec757aSmatthias.ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 763*3a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 76474ec757aSmatthias.ringwald // first send inq result packet 765*3a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size); 76674ec757aSmatthias.ringwald // then send cached remote names 76774ec757aSmatthias.ringwald for (i=0; i<packet[2];i++){ 76874ec757aSmatthias.ringwald bt_flip_addr(addr, &packet[3+i*6]); 769*3a9fb326S[email protected] if (hci_stack->remote_device_db->get_name(&addr, &device_name)){ 77074ec757aSmatthias.ringwald hci_emit_remote_name_cached(&addr, &device_name); 77174ec757aSmatthias.ringwald } 77274ec757aSmatthias.ringwald } 77374ec757aSmatthias.ringwald return; 774223aafc1Smatthias.ringwald #endif 77574ec757aSmatthias.ringwald 7766772a24cSmatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 777fe1ed1b8Smatthias.ringwald if (!packet[2]){ 778fe1ed1b8Smatthias.ringwald handle = READ_BT_16(packet, 3); 7795061f3afS[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 780fe1ed1b8Smatthias.ringwald if (conn) { 781c7e0c5f6Smatthias.ringwald hci_shutdown_connection(conn); 782fe1ed1b8Smatthias.ringwald } 783fe1ed1b8Smatthias.ringwald } 7846772a24cSmatthias.ringwald break; 7856772a24cSmatthias.ringwald 786c68bdf90Smatthias.ringwald case HCI_EVENT_HARDWARE_ERROR: 787*3a9fb326S[email protected] if(hci_stack->control && hci_stack->control->hw_error){ 788*3a9fb326S[email protected] (*hci_stack->control->hw_error)(); 789c68bdf90Smatthias.ringwald } 790c68bdf90Smatthias.ringwald break; 791c68bdf90Smatthias.ringwald 7925909f7f2Smatthias.ringwald #ifdef HAVE_BLE 7935909f7f2Smatthias.ringwald case HCI_EVENT_LE_META: 7945909f7f2Smatthias.ringwald switch (packet[2]) { 7955909f7f2Smatthias.ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 7965909f7f2Smatthias.ringwald // Connection management 7975909f7f2Smatthias.ringwald bt_flip_addr(addr, &packet[8]); 7985909f7f2Smatthias.ringwald log_info("LE Connection_complete (status=%u) %s\n", packet[3], bd_addr_to_str(addr)); 7995909f7f2Smatthias.ringwald // LE connections are auto-accepted, so just create a connection if there isn't one already 8005909f7f2Smatthias.ringwald conn = connection_for_address(addr); 8015909f7f2Smatthias.ringwald if (packet[3]){ 8025909f7f2Smatthias.ringwald if (conn){ 8035909f7f2Smatthias.ringwald // outgoing connection failed, remove entry 804*3a9fb326S[email protected] linked_list_remove(&hci_stack->connections, (linked_item_t *) conn); 8055909f7f2Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 8065909f7f2Smatthias.ringwald 8075909f7f2Smatthias.ringwald } 8085909f7f2Smatthias.ringwald // if authentication error, also delete link key 8095909f7f2Smatthias.ringwald if (packet[3] == 0x05) { 8105909f7f2Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 8115909f7f2Smatthias.ringwald } 8125909f7f2Smatthias.ringwald break; 8135909f7f2Smatthias.ringwald } 8145909f7f2Smatthias.ringwald if (!conn){ 8155909f7f2Smatthias.ringwald conn = create_connection_for_addr(addr); 8165909f7f2Smatthias.ringwald } 8175909f7f2Smatthias.ringwald if (!conn){ 8185909f7f2Smatthias.ringwald // no memory 8195909f7f2Smatthias.ringwald break; 8205909f7f2Smatthias.ringwald } 8215909f7f2Smatthias.ringwald 8225909f7f2Smatthias.ringwald conn->state = OPEN; 8235909f7f2Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 4); 8245909f7f2Smatthias.ringwald 8255909f7f2Smatthias.ringwald // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock 8265909f7f2Smatthias.ringwald 8275909f7f2Smatthias.ringwald // restart timer 8285909f7f2Smatthias.ringwald // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 8295909f7f2Smatthias.ringwald // run_loop_add_timer(&conn->timeout); 8305909f7f2Smatthias.ringwald 8315909f7f2Smatthias.ringwald log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 8325909f7f2Smatthias.ringwald 8335909f7f2Smatthias.ringwald hci_emit_nr_connections_changed(); 8345909f7f2Smatthias.ringwald break; 8355909f7f2Smatthias.ringwald 83665a46ef3S[email protected] // printf("LE buffer size: %u, count %u\n", READ_BT_16(packet,6), packet[8]); 83765a46ef3S[email protected] 8385909f7f2Smatthias.ringwald default: 8395909f7f2Smatthias.ringwald break; 8405909f7f2Smatthias.ringwald } 8415909f7f2Smatthias.ringwald break; 8425909f7f2Smatthias.ringwald #endif 8435909f7f2Smatthias.ringwald 8446772a24cSmatthias.ringwald default: 8456772a24cSmatthias.ringwald break; 846fe1ed1b8Smatthias.ringwald } 847fe1ed1b8Smatthias.ringwald 8483429f56bSmatthias.ringwald // handle BT initialization 849*3a9fb326S[email protected] if (hci_stack->state == HCI_STATE_INITIALIZING){ 850*3a9fb326S[email protected] if (hci_stack->substate % 2){ 8513429f56bSmatthias.ringwald // odd: waiting for event 852f155fca3Smatthias.ringwald if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || packet[0] == HCI_EVENT_COMMAND_STATUS){ 853c9af4d3fS[email protected] // wait for explicit COMMAND COMPLETE on RESET 854*3a9fb326S[email protected] if (hci_stack->substate > 1 || COMMAND_COMPLETE_EVENT(packet, hci_reset)) { 855*3a9fb326S[email protected] hci_stack->substate++; 8563429f56bSmatthias.ringwald } 8573429f56bSmatthias.ringwald } 85822909952Smatthias.ringwald } 859c9af4d3fS[email protected] } 86022909952Smatthias.ringwald 86189db417bSmatthias.ringwald // help with BT sleep 862*3a9fb326S[email protected] if (hci_stack->state == HCI_STATE_FALLING_ASLEEP 863*3a9fb326S[email protected] && hci_stack->substate == 1 86489db417bSmatthias.ringwald && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 865*3a9fb326S[email protected] hci_stack->substate++; 86689db417bSmatthias.ringwald } 86789db417bSmatthias.ringwald 868*3a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size); 86994ab26f8Smatthias.ringwald 87094ab26f8Smatthias.ringwald // execute main loop 87194ab26f8Smatthias.ringwald hci_run(); 87216833f0aSmatthias.ringwald } 87316833f0aSmatthias.ringwald 8740a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 87510e830c9Smatthias.ringwald switch (packet_type) { 87610e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 87710e830c9Smatthias.ringwald event_handler(packet, size); 87810e830c9Smatthias.ringwald break; 87910e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 88010e830c9Smatthias.ringwald acl_handler(packet, size); 88110e830c9Smatthias.ringwald break; 88210e830c9Smatthias.ringwald default: 88310e830c9Smatthias.ringwald break; 88410e830c9Smatthias.ringwald } 88510e830c9Smatthias.ringwald } 88610e830c9Smatthias.ringwald 887fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 8882718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 889*3a9fb326S[email protected] hci_stack->packet_handler = handler; 89016833f0aSmatthias.ringwald } 89116833f0aSmatthias.ringwald 8924f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){ 893475c8125Smatthias.ringwald 894*3a9fb326S[email protected] #ifdef HAVE_MALLOC 895*3a9fb326S[email protected] if (!hci_stack) { 896*3a9fb326S[email protected] hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t)); 897*3a9fb326S[email protected] } 898*3a9fb326S[email protected] #else 899*3a9fb326S[email protected] hci_stack = &hci_stack_static; 900*3a9fb326S[email protected] #endif 901*3a9fb326S[email protected] 902475c8125Smatthias.ringwald // reference to use transport layer implementation 903*3a9fb326S[email protected] hci_stack->hci_transport = transport; 904475c8125Smatthias.ringwald 90511e23e5fSmatthias.ringwald // references to used control implementation 906*3a9fb326S[email protected] hci_stack->control = control; 90711e23e5fSmatthias.ringwald 90811e23e5fSmatthias.ringwald // reference to used config 909*3a9fb326S[email protected] hci_stack->config = config; 91011e23e5fSmatthias.ringwald 911fe1ed1b8Smatthias.ringwald // no connections yet 912*3a9fb326S[email protected] hci_stack->connections = NULL; 913*3a9fb326S[email protected] hci_stack->discoverable = 0; 914*3a9fb326S[email protected] hci_stack->connectable = 0; 915*3a9fb326S[email protected] hci_stack->bondable = 1; 916758b46ceSmatthias.ringwald 917b031bebbSmatthias.ringwald // no pending cmds 918*3a9fb326S[email protected] hci_stack->decline_reason = 0; 919*3a9fb326S[email protected] hci_stack->new_scan_enable_value = 0xff; 920b031bebbSmatthias.ringwald 92116833f0aSmatthias.ringwald // higher level handler 922*3a9fb326S[email protected] hci_stack->packet_handler = dummy_handler; 92316833f0aSmatthias.ringwald 924404843c1Smatthias.ringwald // store and open remote device db 925*3a9fb326S[email protected] hci_stack->remote_device_db = remote_device_db; 926*3a9fb326S[email protected] if (hci_stack->remote_device_db) { 927*3a9fb326S[email protected] hci_stack->remote_device_db->open(); 928404843c1Smatthias.ringwald } 92929d53098Smatthias.ringwald 9308fcba05dSmatthias.ringwald // max acl payload size defined in config.h 931*3a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 9328fcba05dSmatthias.ringwald 93316833f0aSmatthias.ringwald // register packet handlers with transport 93410e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 935f5454fc6Smatthias.ringwald 936*3a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 937e2386ba1S[email protected] 938e2386ba1S[email protected] // class of device 939*3a9fb326S[email protected] hci_stack->class_of_device = 0x007a020c; // Smartphone 940a45d6b9fS[email protected] 94163048403S[email protected] // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept 942*3a9fb326S[email protected] hci_stack->ssp_enable = 1; 943*3a9fb326S[email protected] hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT; 944*3a9fb326S[email protected] hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING; 945*3a9fb326S[email protected] hci_stack->ssp_auto_accept = 1; 94669a97523S[email protected] 94769a97523S[email protected] // LE 948*3a9fb326S[email protected] hci_stack->adv_addr_type = 0; 949*3a9fb326S[email protected] memset(hci_stack->adv_address, 0, 6); 950475c8125Smatthias.ringwald } 951475c8125Smatthias.ringwald 952404843c1Smatthias.ringwald void hci_close(){ 953404843c1Smatthias.ringwald // close remote device db 954*3a9fb326S[email protected] if (hci_stack->remote_device_db) { 955*3a9fb326S[email protected] hci_stack->remote_device_db->close(); 956404843c1Smatthias.ringwald } 957*3a9fb326S[email protected] while (hci_stack->connections) { 958*3a9fb326S[email protected] hci_shutdown_connection((hci_connection_t *) hci_stack->connections); 959f5454fc6Smatthias.ringwald } 960f5454fc6Smatthias.ringwald hci_power_control(HCI_POWER_OFF); 961*3a9fb326S[email protected] 962*3a9fb326S[email protected] #ifdef HAVE_MALLOC 963*3a9fb326S[email protected] free(hci_stack); 964*3a9fb326S[email protected] #endif 965*3a9fb326S[email protected] hci_stack = NULL; 966404843c1Smatthias.ringwald } 967404843c1Smatthias.ringwald 9688d213e1aSmatthias.ringwald // State-Module-Driver overview 9698d213e1aSmatthias.ringwald // state module low-level 9708d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 9718d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 9728d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 9738d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 974d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 975d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 976c7e0c5f6Smatthias.ringwald 97740d1c7a4Smatthias.ringwald static int hci_power_control_on(void){ 9787301ad89Smatthias.ringwald 979038bc64cSmatthias.ringwald // power on 980f9a30166Smatthias.ringwald int err = 0; 981*3a9fb326S[email protected] if (hci_stack->control && hci_stack->control->on){ 982*3a9fb326S[email protected] err = (*hci_stack->control->on)(hci_stack->config); 983f9a30166Smatthias.ringwald } 984038bc64cSmatthias.ringwald if (err){ 9857d67539fSmatthias.ringwald log_error( "POWER_ON failed\n"); 986038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 987038bc64cSmatthias.ringwald return err; 988038bc64cSmatthias.ringwald } 989038bc64cSmatthias.ringwald 990038bc64cSmatthias.ringwald // open low-level device 991*3a9fb326S[email protected] err = hci_stack->hci_transport->open(hci_stack->config); 992038bc64cSmatthias.ringwald if (err){ 9937d67539fSmatthias.ringwald log_error( "HCI_INIT failed, turning Bluetooth off again\n"); 994*3a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 995*3a9fb326S[email protected] (*hci_stack->control->off)(hci_stack->config); 996f9a30166Smatthias.ringwald } 997038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 998038bc64cSmatthias.ringwald return err; 999038bc64cSmatthias.ringwald } 10008d213e1aSmatthias.ringwald return 0; 10018d213e1aSmatthias.ringwald } 1002038bc64cSmatthias.ringwald 100340d1c7a4Smatthias.ringwald static void hci_power_control_off(void){ 10048d213e1aSmatthias.ringwald 10057b5fbe1fSmatthias.ringwald log_info("hci_power_control_off\n"); 10069418f9c9Smatthias.ringwald 10078d213e1aSmatthias.ringwald // close low-level device 1008*3a9fb326S[email protected] hci_stack->hci_transport->close(hci_stack->config); 10098d213e1aSmatthias.ringwald 10107b5fbe1fSmatthias.ringwald log_info("hci_power_control_off - hci_transport closed\n"); 10119418f9c9Smatthias.ringwald 10128d213e1aSmatthias.ringwald // power off 1013*3a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 1014*3a9fb326S[email protected] (*hci_stack->control->off)(hci_stack->config); 10158d213e1aSmatthias.ringwald } 10169418f9c9Smatthias.ringwald 10177b5fbe1fSmatthias.ringwald log_info("hci_power_control_off - control closed\n"); 10189418f9c9Smatthias.ringwald 1019*3a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 102072ea5239Smatthias.ringwald } 102172ea5239Smatthias.ringwald 102240d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){ 102372ea5239Smatthias.ringwald 10247b5fbe1fSmatthias.ringwald log_info("hci_power_control_sleep\n"); 10253144bce4Smatthias.ringwald 1026b429b9b7Smatthias.ringwald #if 0 1027b429b9b7Smatthias.ringwald // don't close serial port during sleep 1028b429b9b7Smatthias.ringwald 102972ea5239Smatthias.ringwald // close low-level device 1030*3a9fb326S[email protected] hci_stack->hci_transport->close(hci_stack->config); 1031b429b9b7Smatthias.ringwald #endif 103272ea5239Smatthias.ringwald 103372ea5239Smatthias.ringwald // sleep mode 1034*3a9fb326S[email protected] if (hci_stack->control && hci_stack->control->sleep){ 1035*3a9fb326S[email protected] (*hci_stack->control->sleep)(hci_stack->config); 103672ea5239Smatthias.ringwald } 1037b429b9b7Smatthias.ringwald 1038*3a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 10398d213e1aSmatthias.ringwald } 10408d213e1aSmatthias.ringwald 104140d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){ 1042b429b9b7Smatthias.ringwald 10437b5fbe1fSmatthias.ringwald log_info("hci_power_control_wake\n"); 1044b429b9b7Smatthias.ringwald 1045b429b9b7Smatthias.ringwald // wake on 1046*3a9fb326S[email protected] if (hci_stack->control && hci_stack->control->wake){ 1047*3a9fb326S[email protected] (*hci_stack->control->wake)(hci_stack->config); 1048b429b9b7Smatthias.ringwald } 1049b429b9b7Smatthias.ringwald 1050b429b9b7Smatthias.ringwald #if 0 1051b429b9b7Smatthias.ringwald // open low-level device 1052*3a9fb326S[email protected] int err = hci_stack->hci_transport->open(hci_stack->config); 1053b429b9b7Smatthias.ringwald if (err){ 10547d67539fSmatthias.ringwald log_error( "HCI_INIT failed, turning Bluetooth off again\n"); 1055*3a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 1056*3a9fb326S[email protected] (*hci_stack->control->off)(hci_stack->config); 1057b429b9b7Smatthias.ringwald } 1058b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 1059b429b9b7Smatthias.ringwald return err; 1060b429b9b7Smatthias.ringwald } 1061b429b9b7Smatthias.ringwald #endif 1062b429b9b7Smatthias.ringwald 1063b429b9b7Smatthias.ringwald return 0; 1064b429b9b7Smatthias.ringwald } 1065b429b9b7Smatthias.ringwald 1066b429b9b7Smatthias.ringwald 10678d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 1068d661ed19Smatthias.ringwald 1069*3a9fb326S[email protected] log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack->state); 1070d661ed19Smatthias.ringwald 10718d213e1aSmatthias.ringwald int err = 0; 1072*3a9fb326S[email protected] switch (hci_stack->state){ 10738d213e1aSmatthias.ringwald 10748d213e1aSmatthias.ringwald case HCI_STATE_OFF: 10758d213e1aSmatthias.ringwald switch (power_mode){ 10768d213e1aSmatthias.ringwald case HCI_POWER_ON: 10778d213e1aSmatthias.ringwald err = hci_power_control_on(); 10788d213e1aSmatthias.ringwald if (err) return err; 10797301ad89Smatthias.ringwald // set up state machine 1080*3a9fb326S[email protected] hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent 1081*3a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 1082*3a9fb326S[email protected] hci_stack->substate = 0; 10838d213e1aSmatthias.ringwald break; 10848d213e1aSmatthias.ringwald case HCI_POWER_OFF: 10858d213e1aSmatthias.ringwald // do nothing 10868d213e1aSmatthias.ringwald break; 10878d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1088b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 10898d213e1aSmatthias.ringwald break; 10908d213e1aSmatthias.ringwald } 10918d213e1aSmatthias.ringwald break; 10927301ad89Smatthias.ringwald 10938d213e1aSmatthias.ringwald case HCI_STATE_INITIALIZING: 10948d213e1aSmatthias.ringwald switch (power_mode){ 10958d213e1aSmatthias.ringwald case HCI_POWER_ON: 10968d213e1aSmatthias.ringwald // do nothing 10978d213e1aSmatthias.ringwald break; 10988d213e1aSmatthias.ringwald case HCI_POWER_OFF: 10998d213e1aSmatthias.ringwald // no connections yet, just turn it off 11008d213e1aSmatthias.ringwald hci_power_control_off(); 11018d213e1aSmatthias.ringwald break; 11028d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1103b546ac54Smatthias.ringwald // no connections yet, just turn it off 110472ea5239Smatthias.ringwald hci_power_control_sleep(); 11058d213e1aSmatthias.ringwald break; 11068d213e1aSmatthias.ringwald } 11078d213e1aSmatthias.ringwald break; 11087301ad89Smatthias.ringwald 11098d213e1aSmatthias.ringwald case HCI_STATE_WORKING: 11108d213e1aSmatthias.ringwald switch (power_mode){ 11118d213e1aSmatthias.ringwald case HCI_POWER_ON: 11128d213e1aSmatthias.ringwald // do nothing 11138d213e1aSmatthias.ringwald break; 11148d213e1aSmatthias.ringwald case HCI_POWER_OFF: 1115c7e0c5f6Smatthias.ringwald // see hci_run 1116*3a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 11178d213e1aSmatthias.ringwald break; 11188d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1119b546ac54Smatthias.ringwald // see hci_run 1120*3a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 1121*3a9fb326S[email protected] hci_stack->substate = 0; 11228d213e1aSmatthias.ringwald break; 11238d213e1aSmatthias.ringwald } 11248d213e1aSmatthias.ringwald break; 11257301ad89Smatthias.ringwald 11268d213e1aSmatthias.ringwald case HCI_STATE_HALTING: 11278d213e1aSmatthias.ringwald switch (power_mode){ 11288d213e1aSmatthias.ringwald case HCI_POWER_ON: 11298d213e1aSmatthias.ringwald // set up state machine 1130*3a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 1131*3a9fb326S[email protected] hci_stack->substate = 0; 11328d213e1aSmatthias.ringwald break; 11338d213e1aSmatthias.ringwald case HCI_POWER_OFF: 11348d213e1aSmatthias.ringwald // do nothing 11358d213e1aSmatthias.ringwald break; 11368d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1137b546ac54Smatthias.ringwald // see hci_run 1138*3a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 1139*3a9fb326S[email protected] hci_stack->substate = 0; 11408d213e1aSmatthias.ringwald break; 11418d213e1aSmatthias.ringwald } 11428d213e1aSmatthias.ringwald break; 11438d213e1aSmatthias.ringwald 11448d213e1aSmatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 11458d213e1aSmatthias.ringwald switch (power_mode){ 11468d213e1aSmatthias.ringwald case HCI_POWER_ON: 114728171530Smatthias.ringwald 114828171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 114928171530Smatthias.ringwald // nothing to do, if H4 supports power management 115028171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 1151*3a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 1152*3a9fb326S[email protected] hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP; 115328171530Smatthias.ringwald break; 115428171530Smatthias.ringwald } 115528171530Smatthias.ringwald #endif 1156b546ac54Smatthias.ringwald // set up state machine 1157*3a9fb326S[email protected] hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent 1158*3a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 1159*3a9fb326S[email protected] hci_stack->substate = 0; 11608d213e1aSmatthias.ringwald break; 11618d213e1aSmatthias.ringwald case HCI_POWER_OFF: 1162b546ac54Smatthias.ringwald // see hci_run 1163*3a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 11648d213e1aSmatthias.ringwald break; 11658d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1166b546ac54Smatthias.ringwald // do nothing 11678d213e1aSmatthias.ringwald break; 11688d213e1aSmatthias.ringwald } 11698d213e1aSmatthias.ringwald break; 11708d213e1aSmatthias.ringwald 11718d213e1aSmatthias.ringwald case HCI_STATE_SLEEPING: 11728d213e1aSmatthias.ringwald switch (power_mode){ 11738d213e1aSmatthias.ringwald case HCI_POWER_ON: 117428171530Smatthias.ringwald 117528171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 117628171530Smatthias.ringwald // nothing to do, if H4 supports power management 117728171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 1178*3a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 1179*3a9fb326S[email protected] hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP; 1180758b46ceSmatthias.ringwald hci_update_scan_enable(); 118128171530Smatthias.ringwald break; 118228171530Smatthias.ringwald } 118328171530Smatthias.ringwald #endif 11843144bce4Smatthias.ringwald err = hci_power_control_wake(); 11853144bce4Smatthias.ringwald if (err) return err; 1186b546ac54Smatthias.ringwald // set up state machine 1187*3a9fb326S[email protected] hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent 1188*3a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 1189*3a9fb326S[email protected] hci_stack->substate = 0; 11908d213e1aSmatthias.ringwald break; 11918d213e1aSmatthias.ringwald case HCI_POWER_OFF: 1192*3a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 11938d213e1aSmatthias.ringwald break; 11948d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1195b546ac54Smatthias.ringwald // do nothing 11968d213e1aSmatthias.ringwald break; 11978d213e1aSmatthias.ringwald } 11988d213e1aSmatthias.ringwald break; 119911e23e5fSmatthias.ringwald } 120068d92d03Smatthias.ringwald 1201038bc64cSmatthias.ringwald // create internal event 1202ee8bf225Smatthias.ringwald hci_emit_state(); 1203ee8bf225Smatthias.ringwald 120468d92d03Smatthias.ringwald // trigger next/first action 120568d92d03Smatthias.ringwald hci_run(); 120668d92d03Smatthias.ringwald 1207475c8125Smatthias.ringwald return 0; 1208475c8125Smatthias.ringwald } 1209475c8125Smatthias.ringwald 1210758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){ 1211758b46ceSmatthias.ringwald // 2 = page scan, 1 = inq scan 1212*3a9fb326S[email protected] hci_stack->new_scan_enable_value = hci_stack->connectable << 1 | hci_stack->discoverable; 1213758b46ceSmatthias.ringwald hci_run(); 1214758b46ceSmatthias.ringwald } 1215758b46ceSmatthias.ringwald 1216381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){ 1217381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 1218381fbed8Smatthias.ringwald 1219*3a9fb326S[email protected] if (hci_stack->discoverable == enable){ 1220*3a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 1221381fbed8Smatthias.ringwald return; 1222381fbed8Smatthias.ringwald } 1223381fbed8Smatthias.ringwald 1224*3a9fb326S[email protected] hci_stack->discoverable = enable; 1225758b46ceSmatthias.ringwald hci_update_scan_enable(); 1226758b46ceSmatthias.ringwald } 1227b031bebbSmatthias.ringwald 1228758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){ 1229758b46ceSmatthias.ringwald if (enable) enable = 1; // normalize argument 1230758b46ceSmatthias.ringwald 1231758b46ceSmatthias.ringwald // don't emit event 1232*3a9fb326S[email protected] if (hci_stack->connectable == enable) return; 1233758b46ceSmatthias.ringwald 1234*3a9fb326S[email protected] hci_stack->connectable = enable; 1235758b46ceSmatthias.ringwald hci_update_scan_enable(); 1236381fbed8Smatthias.ringwald } 1237381fbed8Smatthias.ringwald 12385061f3afS[email protected] bd_addr_t * hci_local_bd_addr(void){ 1239*3a9fb326S[email protected] return &hci_stack->local_bd_addr; 12405061f3afS[email protected] } 12415061f3afS[email protected] 124206b35ec0Smatthias.ringwald void hci_run(){ 12438a485f27Smatthias.ringwald 124432ab9390Smatthias.ringwald hci_connection_t * connection; 124532ab9390Smatthias.ringwald linked_item_t * it; 124632ab9390Smatthias.ringwald 1247ce4c8fabSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 1248ce4c8fabSmatthias.ringwald 1249b031bebbSmatthias.ringwald // global/non-connection oriented commands 1250b031bebbSmatthias.ringwald 1251b031bebbSmatthias.ringwald // decline incoming connections 1252*3a9fb326S[email protected] if (hci_stack->decline_reason){ 1253*3a9fb326S[email protected] uint8_t reason = hci_stack->decline_reason; 1254*3a9fb326S[email protected] hci_stack->decline_reason = 0; 1255*3a9fb326S[email protected] hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason); 1256dbe1a790S[email protected] return; 1257ce4c8fabSmatthias.ringwald } 1258ce4c8fabSmatthias.ringwald 1259b031bebbSmatthias.ringwald // send scan enable 1260*3a9fb326S[email protected] if (hci_stack->state == HCI_STATE_WORKING && hci_stack->new_scan_enable_value != 0xff && hci_classic_supported()){ 1261*3a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value); 1262*3a9fb326S[email protected] hci_stack->new_scan_enable_value = 0xff; 1263dbe1a790S[email protected] return; 1264b031bebbSmatthias.ringwald } 1265b031bebbSmatthias.ringwald 126632ab9390Smatthias.ringwald // send pending HCI commands 1267*3a9fb326S[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){ 126832ab9390Smatthias.ringwald 1269b031bebbSmatthias.ringwald connection = (hci_connection_t *) it; 127032ab9390Smatthias.ringwald 1271ad83dc6aS[email protected] if (connection->state == SEND_CREATE_CONNECTION){ 1272ad83dc6aS[email protected] log_info("sending hci_create_connection\n"); 1273ad83dc6aS[email protected] hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 1274ad83dc6aS[email protected] return; 1275ad83dc6aS[email protected] } 1276ad83dc6aS[email protected] 127732ab9390Smatthias.ringwald if (connection->state == RECEIVED_CONNECTION_REQUEST){ 12787b5fbe1fSmatthias.ringwald log_info("sending hci_accept_connection_request\n"); 127932ab9390Smatthias.ringwald connection->state = ACCEPTED_CONNECTION_REQUEST; 128034d2123cS[email protected] hci_send_cmd(&hci_accept_connection_request, connection->address, 1); 1281dbe1a790S[email protected] return; 1282c7e0c5f6Smatthias.ringwald } 1283c7e0c5f6Smatthias.ringwald 128432ab9390Smatthias.ringwald if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){ 128534d2123cS[email protected] log_info("responding to link key request\n"); 128634d2123cS[email protected] connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST); 128732ab9390Smatthias.ringwald link_key_t link_key; 1288c77e8838S[email protected] link_key_type_t link_key_type; 1289*3a9fb326S[email protected] if ( hci_stack->remote_device_db 1290*3a9fb326S[email protected] && hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type) 129134d2123cS[email protected] && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){ 12929ab95c90S[email protected] connection->link_key_type = link_key_type; 129332ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key); 129432ab9390Smatthias.ringwald } else { 129532ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 129632ab9390Smatthias.ringwald } 1297dbe1a790S[email protected] return; 129832ab9390Smatthias.ringwald } 12991d6b20aeS[email protected] 1300899283eaS[email protected] if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){ 13014c57c146S[email protected] log_info("denying to pin request\n"); 1302899283eaS[email protected] connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST); 130334d2123cS[email protected] hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address); 13044c57c146S[email protected] return; 13054c57c146S[email protected] } 13064c57c146S[email protected] 1307dbe1a790S[email protected] if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){ 130834d2123cS[email protected] connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY); 1309*3a9fb326S[email protected] if (hci_stack->bondable && hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN){ 1310106d6d11S[email protected] // tweak authentication requirements 1311*3a9fb326S[email protected] uint8_t authreq = hci_stack->ssp_authentication_requirement; 1312106d6d11S[email protected] if (connection->bonding_flags & BONDING_DEDICATED){ 13139faad3abS[email protected] authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 13149faad3abS[email protected] } 13159faad3abS[email protected] if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){ 13169faad3abS[email protected] authreq |= 1; 1317106d6d11S[email protected] } 1318*3a9fb326S[email protected] hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, NULL, authreq); 1319f8fb5f6eS[email protected] } else { 1320f8fb5f6eS[email protected] hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED); 1321f8fb5f6eS[email protected] } 1322dbe1a790S[email protected] return; 132332ab9390Smatthias.ringwald } 132432ab9390Smatthias.ringwald 1325dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){ 1326dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY); 132734d2123cS[email protected] hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address); 1328dbe1a790S[email protected] return; 1329dbe1a790S[email protected] } 1330dbe1a790S[email protected] 1331dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){ 1332dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY); 133334d2123cS[email protected] hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000); 1334dbe1a790S[email protected] return; 1335dbe1a790S[email protected] } 1336afd4e962S[email protected] 1337afd4e962S[email protected] if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){ 1338afd4e962S[email protected] connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES; 133934d2123cS[email protected] hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle); 13402bd8b7e7S[email protected] return; 13412bd8b7e7S[email protected] } 13422bd8b7e7S[email protected] 13432bd8b7e7S[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){ 13442bd8b7e7S[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK; 134534d2123cS[email protected] hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005); // authentication failure 134634d2123cS[email protected] return; 134734d2123cS[email protected] } 1348ad83dc6aS[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){ 1349ad83dc6aS[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE; 1350ad83dc6aS[email protected] hci_send_cmd(&hci_disconnect, connection->con_handle, 0); // authentication done 1351ad83dc6aS[email protected] return; 1352ad83dc6aS[email protected] } 135334d2123cS[email protected] if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){ 135434d2123cS[email protected] connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST; 135534d2123cS[email protected] hci_send_cmd(&hci_authentication_requested, connection->con_handle); 13562bd8b7e7S[email protected] return; 1357afd4e962S[email protected] } 1358dce78009S[email protected] if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){ 1359dce78009S[email protected] connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST; 1360dce78009S[email protected] hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1); 1361dce78009S[email protected] return; 1362dce78009S[email protected] } 1363dbe1a790S[email protected] } 1364c7e0c5f6Smatthias.ringwald 1365*3a9fb326S[email protected] switch (hci_stack->state){ 13663429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 1367*3a9fb326S[email protected] // log_info("hci_init: substate %u\n", hci_stack->substate); 1368*3a9fb326S[email protected] if (hci_stack->substate % 2) { 13693429f56bSmatthias.ringwald // odd: waiting for command completion 137006b35ec0Smatthias.ringwald return; 13713429f56bSmatthias.ringwald } 1372*3a9fb326S[email protected] switch (hci_stack->substate >> 1){ 1373c7492964Smatthias.ringwald case 0: // RESET 137422909952Smatthias.ringwald hci_send_cmd(&hci_reset); 137524052c2aS[email protected] 1376*3a9fb326S[email protected] if (hci_stack->config == 0 || ((hci_uart_config_t *)hci_stack->config)->baudrate_main == 0){ 1377c7492964Smatthias.ringwald // skip baud change 1378*3a9fb326S[email protected] hci_stack->substate = 4; // >> 1 = 2 1379c7492964Smatthias.ringwald } 13803429f56bSmatthias.ringwald break; 1381c7492964Smatthias.ringwald case 1: // SEND BAUD CHANGE 1382*3a9fb326S[email protected] hci_stack->control->baudrate_cmd(hci_stack->config, ((hci_uart_config_t *)hci_stack->config)->baudrate_main, hci_stack->hci_packet_buffer); 1383*3a9fb326S[email protected] hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]); 1384c7492964Smatthias.ringwald break; 1385c7492964Smatthias.ringwald case 2: // LOCAL BAUD CHANGE 1386*3a9fb326S[email protected] hci_stack->hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack->config)->baudrate_main); 1387*3a9fb326S[email protected] hci_stack->substate += 2; 1388c7492964Smatthias.ringwald // break missing here for fall through 1389c7492964Smatthias.ringwald 1390c7492964Smatthias.ringwald case 3: 139124052c2aS[email protected] // Custom initialization 1392*3a9fb326S[email protected] if (hci_stack->control && hci_stack->control->next_cmd){ 1393*3a9fb326S[email protected] int valid_cmd = (*hci_stack->control->next_cmd)(hci_stack->config, hci_stack->hci_packet_buffer); 1394d62aca9fSmatthias.ringwald if (valid_cmd){ 1395*3a9fb326S[email protected] int size = 3 + hci_stack->hci_packet_buffer[2]; 1396*3a9fb326S[email protected] hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size); 1397*3a9fb326S[email protected] hci_stack->substate = 4; // more init commands 139890919203Smatthias.ringwald break; 139990919203Smatthias.ringwald } 1400d62aca9fSmatthias.ringwald log_info("hci_run: init script done\n\r"); 140190919203Smatthias.ringwald } 14022f6c30e1Smatthias.ringwald // otherwise continue 1403f432a6ddSmatthias.ringwald hci_send_cmd(&hci_read_bd_addr); 1404f432a6ddSmatthias.ringwald break; 1405c7492964Smatthias.ringwald case 4: 1406e2edc0c3Smatthias.ringwald hci_send_cmd(&hci_read_buffer_size); 1407e2edc0c3Smatthias.ringwald break; 1408c7492964Smatthias.ringwald case 5: 140965389bfcS[email protected] hci_send_cmd(&hci_read_local_supported_features); 14103429f56bSmatthias.ringwald break; 1411c7492964Smatthias.ringwald case 6: 1412d7e0e3a8S[email protected] if (hci_le_supported()){ 1413d7e0e3a8S[email protected] hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF); 1414d7e0e3a8S[email protected] } else { 1415d7e0e3a8S[email protected] // Kensington Bluetoot 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff... 1416d7e0e3a8S[email protected] hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF); 1417d7e0e3a8S[email protected] } 1418f5d8d141S[email protected] 1419f5d8d141S[email protected] // skip Classic init commands for LE only chipsets 142024052c2aS[email protected] if (!hci_classic_supported()){ 142124052c2aS[email protected] if (hci_le_supported()){ 1422*3a9fb326S[email protected] hci_stack->substate = 11 << 1; // skip all classic command 142324052c2aS[email protected] } else { 142424052c2aS[email protected] log_error("Neither BR/EDR nor LE supported"); 1425*3a9fb326S[email protected] hci_stack->substate = 13 << 1; // skip all 142624052c2aS[email protected] } 1427f5d8d141S[email protected] } 1428f5d8d141S[email protected] break; 1429f5d8d141S[email protected] case 7: 143024052c2aS[email protected] if (hci_ssp_supported()){ 1431*3a9fb326S[email protected] hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable); 1432f5d8d141S[email protected] break; 143324052c2aS[email protected] } 1434*3a9fb326S[email protected] hci_stack->substate += 2; 143524052c2aS[email protected] // break missing here for fall through 143624052c2aS[email protected] 1437f5d8d141S[email protected] case 8: 143865389bfcS[email protected] // ca. 15 sec 143965389bfcS[email protected] hci_send_cmd(&hci_write_page_timeout, 0x6000); 1440559e517eS[email protected] break; 1441f5d8d141S[email protected] case 9: 1442*3a9fb326S[email protected] hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device); 1443e2386ba1S[email protected] break; 1444f5d8d141S[email protected] case 10: 1445*3a9fb326S[email protected] if (hci_stack->local_name){ 1446*3a9fb326S[email protected] hci_send_cmd(&hci_write_local_name, hci_stack->local_name); 1447e2386ba1S[email protected] } else { 14488a485f27Smatthias.ringwald char hostname[30]; 1449e2386ba1S[email protected] #ifdef EMBEDDED 1450e2386ba1S[email protected] // BTstack-11:22:33:44:55:66 1451e2386ba1S[email protected] strcpy(hostname, "BTstack "); 1452*3a9fb326S[email protected] strcat(hostname, bd_addr_to_str(hci_stack->local_bd_addr)); 1453e2386ba1S[email protected] printf("---> Name %s\n", hostname); 1454e2386ba1S[email protected] #else 1455e2386ba1S[email protected] // hostname for POSIX systems 14568a485f27Smatthias.ringwald gethostname(hostname, 30); 14578a485f27Smatthias.ringwald hostname[29] = '\0'; 1458e2386ba1S[email protected] #endif 14598a485f27Smatthias.ringwald hci_send_cmd(&hci_write_local_name, hostname); 14608a485f27Smatthias.ringwald } 14613c4d4b90Smatthias.ringwald break; 1462e0dbca83S[email protected] case 11: 1463*3a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan 146424052c2aS[email protected] if (!hci_le_supported()){ 146524052c2aS[email protected] // SKIP LE init for Classic only configuration 1466*3a9fb326S[email protected] hci_stack->substate = 13 << 1; 146724052c2aS[email protected] } 1468a45d6b9fS[email protected] break; 146924052c2aS[email protected] 147043aa7007S[email protected] #ifdef HAVE_BLE 147124052c2aS[email protected] // LE INIT 1472a45d6b9fS[email protected] case 12: 147324052c2aS[email protected] hci_send_cmd(&hci_le_read_buffer_size); 147424052c2aS[email protected] break; 147524052c2aS[email protected] case 13: 147624052c2aS[email protected] // LE Supported Host = 1, Simultaneous Host = 0 147724052c2aS[email protected] hci_send_cmd(&hci_write_le_host_supported, 1, 0); 147824052c2aS[email protected] break; 147943aa7007S[email protected] #endif 148024052c2aS[email protected] 148124052c2aS[email protected] // DONE 148224052c2aS[email protected] case 14: 14833429f56bSmatthias.ringwald // done. 1484*3a9fb326S[email protected] hci_stack->state = HCI_STATE_WORKING; 1485b360b6adSmatthias.ringwald hci_emit_state(); 14863429f56bSmatthias.ringwald break; 14873429f56bSmatthias.ringwald default: 14883429f56bSmatthias.ringwald break; 1489475c8125Smatthias.ringwald } 1490*3a9fb326S[email protected] hci_stack->substate++; 14913429f56bSmatthias.ringwald break; 1492c7e0c5f6Smatthias.ringwald 1493c7e0c5f6Smatthias.ringwald case HCI_STATE_HALTING: 1494c7e0c5f6Smatthias.ringwald 14957b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING\n"); 1496c7e0c5f6Smatthias.ringwald // close all open connections 1497*3a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 1498c7e0c5f6Smatthias.ringwald if (connection){ 149932ab9390Smatthias.ringwald 1500c7e0c5f6Smatthias.ringwald // send disconnect 150132ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 150232ab9390Smatthias.ringwald 150379bbfa0bSmatthias.ringwald log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle); 15046ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 1505c7e0c5f6Smatthias.ringwald 1506c7e0c5f6Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 1507c7e0c5f6Smatthias.ringwald hci_shutdown_connection(connection); 1508c7e0c5f6Smatthias.ringwald return; 1509c7e0c5f6Smatthias.ringwald } 15107b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, calling off\n"); 1511c7e0c5f6Smatthias.ringwald 151272ea5239Smatthias.ringwald // switch mode 1513c7e0c5f6Smatthias.ringwald hci_power_control_off(); 15149418f9c9Smatthias.ringwald 15157b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, emitting state\n"); 151672ea5239Smatthias.ringwald hci_emit_state(); 15177b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, done\n"); 151872ea5239Smatthias.ringwald break; 1519c7e0c5f6Smatthias.ringwald 152072ea5239Smatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 1521*3a9fb326S[email protected] switch(hci_stack->substate) { 152289db417bSmatthias.ringwald case 0: 15237b5fbe1fSmatthias.ringwald log_info("HCI_STATE_FALLING_ASLEEP\n"); 152472ea5239Smatthias.ringwald // close all open connections 1525*3a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 152666da7044Smatthias.ringwald 152728171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 152866da7044Smatthias.ringwald // don't close connections, if H4 supports power management 152966da7044Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 153066da7044Smatthias.ringwald connection = NULL; 153166da7044Smatthias.ringwald } 153266da7044Smatthias.ringwald #endif 153372ea5239Smatthias.ringwald if (connection){ 153432ab9390Smatthias.ringwald 153572ea5239Smatthias.ringwald // send disconnect 153632ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 153732ab9390Smatthias.ringwald 153879bbfa0bSmatthias.ringwald log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle); 15396ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 154072ea5239Smatthias.ringwald 154172ea5239Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 154272ea5239Smatthias.ringwald hci_shutdown_connection(connection); 154372ea5239Smatthias.ringwald return; 154472ea5239Smatthias.ringwald } 154572ea5239Smatthias.ringwald 154692368cd3S[email protected] if (hci_classic_supported()){ 154789db417bSmatthias.ringwald // disable page and inquiry scan 154832ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 154932ab9390Smatthias.ringwald 155092368cd3S[email protected] log_info("HCI_STATE_HALTING, disabling inq scans\n"); 1551*3a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan 155289db417bSmatthias.ringwald 155389db417bSmatthias.ringwald // continue in next sub state 1554*3a9fb326S[email protected] hci_stack->substate++; 155589db417bSmatthias.ringwald break; 155692368cd3S[email protected] } 155792368cd3S[email protected] // fall through for ble-only chips 155892368cd3S[email protected] 155989db417bSmatthias.ringwald case 2: 15607b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, calling sleep\n"); 156128171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 156228171530Smatthias.ringwald // don't actually go to sleep, if H4 supports power management 156328171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 156428171530Smatthias.ringwald // SLEEP MODE reached 1565*3a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 156628171530Smatthias.ringwald hci_emit_state(); 156728171530Smatthias.ringwald break; 156828171530Smatthias.ringwald } 156928171530Smatthias.ringwald #endif 157072ea5239Smatthias.ringwald // switch mode 1571*3a9fb326S[email protected] hci_power_control_sleep(); // changes hci_stack->state to SLEEP 1572c7e0c5f6Smatthias.ringwald hci_emit_state(); 157328171530Smatthias.ringwald break; 157428171530Smatthias.ringwald 157589db417bSmatthias.ringwald default: 157689db417bSmatthias.ringwald break; 157789db417bSmatthias.ringwald } 1578c7e0c5f6Smatthias.ringwald break; 1579c7e0c5f6Smatthias.ringwald 15803429f56bSmatthias.ringwald default: 15813429f56bSmatthias.ringwald break; 15821f504dbdSmatthias.ringwald } 15833429f56bSmatthias.ringwald } 158416833f0aSmatthias.ringwald 158531452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 1586c8e4258aSmatthias.ringwald bd_addr_t addr; 1587c8e4258aSmatthias.ringwald hci_connection_t * conn; 1588c8e4258aSmatthias.ringwald // house-keeping 1589c8e4258aSmatthias.ringwald 1590c8e4258aSmatthias.ringwald // create_connection? 1591c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 1592c8e4258aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 1593339b6768Smatthias.ringwald log_info("Create_connection to %s\n", bd_addr_to_str(addr)); 1594c8e4258aSmatthias.ringwald 1595ad83dc6aS[email protected] conn = connection_for_address(addr); 1596ad83dc6aS[email protected] if (!conn){ 159717f1ba2aSmatthias.ringwald conn = create_connection_for_addr(addr); 159817f1ba2aSmatthias.ringwald if (!conn){ 159917f1ba2aSmatthias.ringwald // notify client that alloc failed 160017f1ba2aSmatthias.ringwald hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED); 160117f1ba2aSmatthias.ringwald return 0; // don't sent packet to controller 160217f1ba2aSmatthias.ringwald } 1603ad83dc6aS[email protected] conn->state = SEND_CREATE_CONNECTION; 1604ad83dc6aS[email protected] } 1605ad83dc6aS[email protected] log_info("conn state %u", conn->state); 1606ad83dc6aS[email protected] switch (conn->state){ 1607ad83dc6aS[email protected] // if connection active exists 1608ad83dc6aS[email protected] case OPEN: 1609ad83dc6aS[email protected] // and OPEN, emit connection complete command 1610ad83dc6aS[email protected] hci_emit_connection_complete(conn, 0); 1611ad83dc6aS[email protected] break; 1612ad83dc6aS[email protected] case SEND_CREATE_CONNECTION: 1613ad83dc6aS[email protected] // connection created by hci, e.g. dedicated bonding 1614ad83dc6aS[email protected] break; 1615ad83dc6aS[email protected] default: 1616ad83dc6aS[email protected] // otherwise, just ignore as it is already in the open process 1617ad83dc6aS[email protected] return 0; 1618ad83dc6aS[email protected] } 1619c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 1620c8e4258aSmatthias.ringwald } 1621c8e4258aSmatthias.ringwald 16227fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_reply)){ 16237fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY); 16247fde4af9Smatthias.ringwald } 16257fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){ 16267fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST); 16277fde4af9Smatthias.ringwald } 16287fde4af9Smatthias.ringwald 16298ef73945Smatthias.ringwald if (IS_COMMAND(packet, hci_delete_stored_link_key)){ 1630*3a9fb326S[email protected] if (hci_stack->remote_device_db){ 16318ef73945Smatthias.ringwald bt_flip_addr(addr, &packet[3]); 1632*3a9fb326S[email protected] hci_stack->remote_device_db->delete_link_key(&addr); 16338ef73945Smatthias.ringwald } 16348ef73945Smatthias.ringwald } 1635c8e4258aSmatthias.ringwald 16366724cd9eS[email protected] if (IS_COMMAND(packet, hci_pin_code_request_negative_reply) 16376724cd9eS[email protected] || IS_COMMAND(packet, hci_pin_code_request_reply)){ 16386724cd9eS[email protected] bt_flip_addr(addr, &packet[3]); 16396724cd9eS[email protected] conn = connection_for_address(addr); 16406724cd9eS[email protected] if (conn){ 16416724cd9eS[email protected] connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE); 16426724cd9eS[email protected] } 16436724cd9eS[email protected] } 16446724cd9eS[email protected] 16456724cd9eS[email protected] if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply) 16466724cd9eS[email protected] || IS_COMMAND(packet, hci_user_confirmation_request_reply) 16476724cd9eS[email protected] || IS_COMMAND(packet, hci_user_passkey_request_negative_reply) 16486724cd9eS[email protected] || IS_COMMAND(packet, hci_user_passkey_request_reply)) { 16496724cd9eS[email protected] bt_flip_addr(addr, &packet[3]); 16506724cd9eS[email protected] conn = connection_for_address(addr); 16516724cd9eS[email protected] if (conn){ 16526724cd9eS[email protected] connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE); 16536724cd9eS[email protected] } 16546724cd9eS[email protected] } 16556724cd9eS[email protected] 165669a97523S[email protected] #ifdef HAVE_BLE 165769a97523S[email protected] if (IS_COMMAND(packet, hci_le_set_advertising_parameters)){ 1658*3a9fb326S[email protected] hci_stack->adv_addr_type = packet[8]; 165969a97523S[email protected] } 166069a97523S[email protected] if (IS_COMMAND(packet, hci_le_set_random_address)){ 1661*3a9fb326S[email protected] bt_flip_addr(hci_stack->adv_address, &packet[3]); 166269a97523S[email protected] } 166369a97523S[email protected] #endif 166469a97523S[email protected] 166569a97523S[email protected] 1666*3a9fb326S[email protected] hci_stack->num_cmd_packets--; 1667*3a9fb326S[email protected] return hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 166831452debSmatthias.ringwald } 16698adf0ddaSmatthias.ringwald 16702bd8b7e7S[email protected] // disconnect because of security block 16712bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){ 16722bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 16732bd8b7e7S[email protected] if (!connection) return; 16742bd8b7e7S[email protected] connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 16752bd8b7e7S[email protected] } 16762bd8b7e7S[email protected] 16772bd8b7e7S[email protected] 1678dbe1a790S[email protected] // Configure Secure Simple Pairing 1679dbe1a790S[email protected] 1680dbe1a790S[email protected] // enable will enable SSP during init 1681dbe1a790S[email protected] void hci_ssp_set_enable(int enable){ 1682*3a9fb326S[email protected] hci_stack->ssp_enable = enable; 1683dbe1a790S[email protected] } 1684dbe1a790S[email protected] 16852bd8b7e7S[email protected] int hci_local_ssp_activated(){ 1686*3a9fb326S[email protected] return hci_ssp_supported() && hci_stack->ssp_enable; 16872bd8b7e7S[email protected] } 16882bd8b7e7S[email protected] 1689dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement 1690dbe1a790S[email protected] void hci_ssp_set_io_capability(int io_capability){ 1691*3a9fb326S[email protected] hci_stack->ssp_io_capability = io_capability; 1692dbe1a790S[email protected] } 1693dbe1a790S[email protected] void hci_ssp_set_authentication_requirement(int authentication_requirement){ 1694*3a9fb326S[email protected] hci_stack->ssp_authentication_requirement = authentication_requirement; 1695dbe1a790S[email protected] } 1696dbe1a790S[email protected] 1697dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested 1698dbe1a790S[email protected] void hci_ssp_set_auto_accept(int auto_accept){ 1699*3a9fb326S[email protected] hci_stack->ssp_auto_accept = auto_accept; 1700dbe1a790S[email protected] } 1701dbe1a790S[email protected] 17021cd208adSmatthias.ringwald /** 17031cd208adSmatthias.ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 17041cd208adSmatthias.ringwald */ 1705fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){ 17061cd208adSmatthias.ringwald va_list argptr; 17071cd208adSmatthias.ringwald va_start(argptr, cmd); 1708*3a9fb326S[email protected] uint16_t size = hci_create_cmd_internal(hci_stack->hci_packet_buffer, cmd, argptr); 17091cd208adSmatthias.ringwald va_end(argptr); 1710*3a9fb326S[email protected] return hci_send_cmd_packet(hci_stack->hci_packet_buffer, size); 171193b8dc03Smatthias.ringwald } 1712c8e4258aSmatthias.ringwald 1713ee091cf1Smatthias.ringwald // Create various non-HCI events. 1714ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 1715ee091cf1Smatthias.ringwald 1716c8e4258aSmatthias.ringwald void hci_emit_state(){ 1717*3a9fb326S[email protected] log_info("BTSTACK_EVENT_STATE %u", hci_stack->state); 1718425d1371Smatthias.ringwald uint8_t event[3]; 171980d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 1720425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1721*3a9fb326S[email protected] event[2] = hci_stack->state; 1722425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1723*3a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1724c8e4258aSmatthias.ringwald } 1725c8e4258aSmatthias.ringwald 172617f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){ 1727425d1371Smatthias.ringwald uint8_t event[13]; 1728c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 1729425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 173017f1ba2aSmatthias.ringwald event[2] = status; 1731c8e4258aSmatthias.ringwald bt_store_16(event, 3, conn->con_handle); 1732c8e4258aSmatthias.ringwald bt_flip_addr(&event[5], conn->address); 1733c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 1734c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 1735425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1736*3a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1737c8e4258aSmatthias.ringwald } 1738c8e4258aSmatthias.ringwald 17393c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){ 1740425d1371Smatthias.ringwald uint8_t event[6]; 17413c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 1742e518c4b8Smatthias.ringwald event[1] = sizeof(event) - 2; 17433c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 17443c4d4b90Smatthias.ringwald bt_store_16(event, 3, handle); 17453c4d4b90Smatthias.ringwald event[5] = reason; 1746425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1747*3a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 17483c4d4b90Smatthias.ringwald } 17493c4d4b90Smatthias.ringwald 1750ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 1751e0abb8e7S[email protected] log_info("L2CAP_EVENT_TIMEOUT_CHECK"); 1752425d1371Smatthias.ringwald uint8_t event[4]; 175380d52d6bSmatthias.ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 1754425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1755ee091cf1Smatthias.ringwald bt_store_16(event, 2, conn->con_handle); 1756425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1757*3a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1758ee091cf1Smatthias.ringwald } 175943bfb1bdSmatthias.ringwald 176043bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){ 1761e0abb8e7S[email protected] log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections()); 1762425d1371Smatthias.ringwald uint8_t event[3]; 176380d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 1764425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 176543bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 1766425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1767*3a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 176843bfb1bdSmatthias.ringwald } 1769038bc64cSmatthias.ringwald 1770038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){ 1771e0abb8e7S[email protected] log_info("BTSTACK_EVENT_POWERON_FAILED"); 1772425d1371Smatthias.ringwald uint8_t event[2]; 177380d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 1774425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1775425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1776*3a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1777038bc64cSmatthias.ringwald } 17781b0e3922Smatthias.ringwald 177909ba8edeSmatthias.ringwald #ifndef EMBEDDED 17801b0e3922Smatthias.ringwald void hci_emit_btstack_version() { 1781e0abb8e7S[email protected] log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR); 1782425d1371Smatthias.ringwald uint8_t event[6]; 17831b0e3922Smatthias.ringwald event[0] = BTSTACK_EVENT_VERSION; 1784425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1785425d1371Smatthias.ringwald event[2] = BTSTACK_MAJOR; 1786425d1371Smatthias.ringwald event[3] = BTSTACK_MINOR; 1787425d1371Smatthias.ringwald bt_store_16(event, 4, BTSTACK_REVISION); 1788425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1789*3a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 17901b0e3922Smatthias.ringwald } 179109ba8edeSmatthias.ringwald #endif 17921b0e3922Smatthias.ringwald 17932ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){ 1794e0abb8e7S[email protected] log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled); 1795425d1371Smatthias.ringwald uint8_t event[3]; 17962ed6235cSmatthias.ringwald event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED; 1797425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 17982ed6235cSmatthias.ringwald event[2] = enabled; 1799425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1800*3a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 18012ed6235cSmatthias.ringwald } 1802627c2f45Smatthias.ringwald 1803627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){ 1804e0abb8e7S[email protected] uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info 1805627c2f45Smatthias.ringwald event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED; 1806e0abb8e7S[email protected] event[1] = sizeof(event) - 2 - 1; 1807f653b6bdSmatthias.ringwald event[2] = 0; // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 18082f89d309Smatthias.ringwald bt_flip_addr(&event[3], *addr); 1809f653b6bdSmatthias.ringwald memcpy(&event[9], name, 248); 1810e0abb8e7S[email protected] 1811e0abb8e7S[email protected] event[9+248] = 0; // assert \0 for log_info 1812e0abb8e7S[email protected] log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]); 1813e0abb8e7S[email protected] 1814e0abb8e7S[email protected] hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1); 1815*3a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1); 1816627c2f45Smatthias.ringwald } 1817381fbed8Smatthias.ringwald 1818381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){ 1819e0abb8e7S[email protected] log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled); 1820425d1371Smatthias.ringwald uint8_t event[3]; 1821381fbed8Smatthias.ringwald event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED; 1822425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1823381fbed8Smatthias.ringwald event[2] = enabled; 1824425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1825*3a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1826381fbed8Smatthias.ringwald } 1827458bf4e8S[email protected] 1828a00031e2S[email protected] void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){ 1829df3354fcS[email protected] log_info("hci_emit_security_level %u for handle %x", level, con_handle); 1830a00031e2S[email protected] uint8_t event[5]; 1831e00caf9cS[email protected] int pos = 0; 1832a00031e2S[email protected] event[pos++] = GAP_SECURITY_LEVEL; 1833e00caf9cS[email protected] event[pos++] = sizeof(event) - 2; 1834a00031e2S[email protected] bt_store_16(event, 2, con_handle); 1835e00caf9cS[email protected] pos += 2; 1836e00caf9cS[email protected] event[pos++] = level; 1837e00caf9cS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1838*3a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1839e00caf9cS[email protected] } 1840e00caf9cS[email protected] 1841ad83dc6aS[email protected] void hci_emit_dedicated_bonding_result(hci_connection_t * connection, uint8_t status){ 1842ad83dc6aS[email protected] log_info("hci_emit_dedicated_bonding_result %u ", status); 1843ad83dc6aS[email protected] uint8_t event[9]; 1844ad83dc6aS[email protected] int pos = 0; 1845ad83dc6aS[email protected] event[pos++] = GAP_DEDICATED_BONDING_COMPLETED; 1846ad83dc6aS[email protected] event[pos++] = sizeof(event) - 2; 1847ad83dc6aS[email protected] event[pos++] = status; 1848ad83dc6aS[email protected] bt_flip_addr( * (bd_addr_t *) &event[pos], connection->address); 1849ad83dc6aS[email protected] pos += 6; 1850ad83dc6aS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1851*3a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1852ad83dc6aS[email protected] } 1853ad83dc6aS[email protected] 18542bd8b7e7S[email protected] // query if remote side supports SSP 18552bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){ 18562bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 18572bd8b7e7S[email protected] if (!connection) return 0; 18582bd8b7e7S[email protected] return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0; 18592bd8b7e7S[email protected] } 18602bd8b7e7S[email protected] 1861df3354fcS[email protected] int hci_ssp_supported_on_both_sides(hci_con_handle_t handle){ 1862df3354fcS[email protected] return hci_local_ssp_activated() && hci_remote_ssp_supported(handle); 1863df3354fcS[email protected] } 1864df3354fcS[email protected] 1865458bf4e8S[email protected] // GAP API 1866458bf4e8S[email protected] /** 1867458bf4e8S[email protected] * @bbrief enable/disable bonding. default is enabled 1868458bf4e8S[email protected] * @praram enabled 1869458bf4e8S[email protected] */ 18704c57c146S[email protected] void gap_set_bondable_mode(int enable){ 1871*3a9fb326S[email protected] hci_stack->bondable = enable ? 1 : 0; 1872458bf4e8S[email protected] } 1873cb230b9dS[email protected] 1874cb230b9dS[email protected] /** 187534d2123cS[email protected] * @brief map link keys to security levels 1876cb230b9dS[email protected] */ 187734d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){ 187834d2123cS[email protected] switch (link_key_type){ 18793c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 18803c68dfa9S[email protected] return LEVEL_4; 18813c68dfa9S[email protected] case COMBINATION_KEY: 18823c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 18833c68dfa9S[email protected] return LEVEL_3; 18843c68dfa9S[email protected] default: 18853c68dfa9S[email protected] return LEVEL_2; 18863c68dfa9S[email protected] } 1887cb230b9dS[email protected] } 1888cb230b9dS[email protected] 1889a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){ 189034d2123cS[email protected] if (!connection) return LEVEL_0; 189134d2123cS[email protected] if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0; 189234d2123cS[email protected] return gap_security_level_for_link_key_type(connection->link_key_type); 189334d2123cS[email protected] } 189434d2123cS[email protected] 189534d2123cS[email protected] 1896106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){ 1897106d6d11S[email protected] return level > LEVEL_2; 1898106d6d11S[email protected] } 1899106d6d11S[email protected] 190034d2123cS[email protected] /** 190134d2123cS[email protected] * @brief get current security level 190234d2123cS[email protected] */ 190334d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){ 190434d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 190534d2123cS[email protected] if (!connection) return LEVEL_0; 190634d2123cS[email protected] return gap_security_level_for_connection(connection); 190734d2123cS[email protected] } 190834d2123cS[email protected] 1909cb230b9dS[email protected] /** 1910cb230b9dS[email protected] * @brief request connection to device to 1911cb230b9dS[email protected] * @result GAP_AUTHENTICATION_RESULT 1912cb230b9dS[email protected] */ 191334d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){ 191434d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 191534d2123cS[email protected] if (!connection){ 1916a00031e2S[email protected] hci_emit_security_level(con_handle, LEVEL_0); 191734d2123cS[email protected] return; 191834d2123cS[email protected] } 191934d2123cS[email protected] gap_security_level_t current_level = gap_security_level(con_handle); 192034d2123cS[email protected] log_info("gap_request_security_level %u, current level %u", requested_level, current_level); 192134d2123cS[email protected] if (current_level >= requested_level){ 1922a00031e2S[email protected] hci_emit_security_level(con_handle, current_level); 192334d2123cS[email protected] return; 192434d2123cS[email protected] } 1925a00031e2S[email protected] 192634d2123cS[email protected] connection->requested_security_level = requested_level; 1927a00031e2S[email protected] 1928fb8ba0dbS[email protected] // would enabling ecnryption suffice (>= LEVEL_2)? 1929*3a9fb326S[email protected] if (hci_stack->remote_device_db){ 1930a00031e2S[email protected] link_key_type_t link_key_type; 1931a00031e2S[email protected] link_key_t link_key; 1932*3a9fb326S[email protected] if (hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)){ 1933a00031e2S[email protected] if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){ 1934a00031e2S[email protected] connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 1935a00031e2S[email protected] return; 1936a00031e2S[email protected] } 1937a00031e2S[email protected] } 1938a00031e2S[email protected] } 1939a00031e2S[email protected] 19401eb2563eS[email protected] // try to authenticate connection 19411eb2563eS[email protected] connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 1942e00caf9cS[email protected] } 1943ad83dc6aS[email protected] 1944ad83dc6aS[email protected] /** 1945ad83dc6aS[email protected] * @brief start dedicated bonding with device. disconnect after bonding 1946ad83dc6aS[email protected] * @param device 1947ad83dc6aS[email protected] * @param request MITM protection 1948ad83dc6aS[email protected] * @result GAP_DEDICATED_BONDING_COMPLETE 1949ad83dc6aS[email protected] */ 1950ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){ 1951ad83dc6aS[email protected] 1952ad83dc6aS[email protected] 1953ad83dc6aS[email protected] printf("gap_dedicated_bonding clled\n"); 1954ad83dc6aS[email protected] // create connection state machine 1955ad83dc6aS[email protected] hci_connection_t * connection = create_connection_for_addr(device); 1956ad83dc6aS[email protected] 1957ad83dc6aS[email protected] if (!connection){ 1958ad83dc6aS[email protected] return BTSTACK_MEMORY_ALLOC_FAILED; 1959ad83dc6aS[email protected] } 1960ad83dc6aS[email protected] 1961ad83dc6aS[email protected] printf("gap_dedicated_bonding 2\n"); 1962ad83dc6aS[email protected] 1963ad83dc6aS[email protected] // delete linkn key 1964ad83dc6aS[email protected] hci_drop_link_key_for_bd_addr( (bd_addr_t *) &device); 1965ad83dc6aS[email protected] 1966ad83dc6aS[email protected] // configure LEVEL_2/3, dedicated bonding 1967ad83dc6aS[email protected] connection->state = SEND_CREATE_CONNECTION; 1968ad83dc6aS[email protected] connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2; 1969ad83dc6aS[email protected] connection->bonding_flags = BONDING_DEDICATED; 1970ad83dc6aS[email protected] 1971ad83dc6aS[email protected] // wait for GAP Security Result and send GAP Dedicated Bonding complete 1972ad83dc6aS[email protected] 1973ad83dc6aS[email protected] // handle: connnection failure (connection complete != ok) 1974ad83dc6aS[email protected] // handle: authentication failure 1975ad83dc6aS[email protected] // handle: disconnect on done 1976ad83dc6aS[email protected] 1977ad83dc6aS[email protected] hci_run(); 1978ad83dc6aS[email protected] 1979ad83dc6aS[email protected] return 0; 1980ad83dc6aS[email protected] } 1981