11f504dbdSmatthias.ringwald /* 26b64433eSmatthias.ringwald * Copyright (C) 2009-2012 by Matthias Ringwald 31713bceaSmatthias.ringwald * 41713bceaSmatthias.ringwald * Redistribution and use in source and binary forms, with or without 51713bceaSmatthias.ringwald * modification, are permitted provided that the following conditions 61713bceaSmatthias.ringwald * are met: 71713bceaSmatthias.ringwald * 81713bceaSmatthias.ringwald * 1. Redistributions of source code must retain the above copyright 91713bceaSmatthias.ringwald * notice, this list of conditions and the following disclaimer. 101713bceaSmatthias.ringwald * 2. Redistributions in binary form must reproduce the above copyright 111713bceaSmatthias.ringwald * notice, this list of conditions and the following disclaimer in the 121713bceaSmatthias.ringwald * documentation and/or other materials provided with the distribution. 131713bceaSmatthias.ringwald * 3. Neither the name of the copyright holders nor the names of 141713bceaSmatthias.ringwald * contributors may be used to endorse or promote products derived 151713bceaSmatthias.ringwald * from this software without specific prior written permission. 166b64433eSmatthias.ringwald * 4. Any redistribution, use, or modification is done solely for 176b64433eSmatthias.ringwald * personal benefit and not for any commercial purpose or for 186b64433eSmatthias.ringwald * monetary gain. 191713bceaSmatthias.ringwald * 201713bceaSmatthias.ringwald * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS 211713bceaSmatthias.ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 221713bceaSmatthias.ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 231713bceaSmatthias.ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 241713bceaSmatthias.ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 251713bceaSmatthias.ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 261713bceaSmatthias.ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 271713bceaSmatthias.ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 281713bceaSmatthias.ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 291713bceaSmatthias.ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 301713bceaSmatthias.ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311713bceaSmatthias.ringwald * SUCH DAMAGE. 321713bceaSmatthias.ringwald * 336b64433eSmatthias.ringwald * Please inquire about commercial licensing options at [email protected] 346b64433eSmatthias.ringwald * 351713bceaSmatthias.ringwald */ 361713bceaSmatthias.ringwald 371713bceaSmatthias.ringwald /* 381f504dbdSmatthias.ringwald * hci.c 391f504dbdSmatthias.ringwald * 401f504dbdSmatthias.ringwald * Created by Matthias Ringwald on 4/29/09. 411f504dbdSmatthias.ringwald * 421f504dbdSmatthias.ringwald */ 431f504dbdSmatthias.ringwald 44bde315ceS[email protected] #include "btstack-config.h" 4528171530Smatthias.ringwald 467f2435e6Smatthias.ringwald #include "hci.h" 474c57c146S[email protected] #include "gap.h" 487f2435e6Smatthias.ringwald 4993b8dc03Smatthias.ringwald #include <stdarg.h> 5093b8dc03Smatthias.ringwald #include <string.h> 5156fe0872Smatthias.ringwald #include <stdio.h> 527f2435e6Smatthias.ringwald 53549e6ebeSmatthias.ringwald #ifndef EMBEDDED 54549e6ebeSmatthias.ringwald #include <unistd.h> // gethostbyname 5509ba8edeSmatthias.ringwald #include <btstack/version.h> 56549e6ebeSmatthias.ringwald #endif 57549e6ebeSmatthias.ringwald 58a3b02b71Smatthias.ringwald #include "btstack_memory.h" 597f2435e6Smatthias.ringwald #include "debug.h" 60d8905019Smatthias.ringwald #include "hci_dump.h" 6193b8dc03Smatthias.ringwald 62ae1fd9f3Smatthias.ringwald #include <btstack/hci_cmds.h> 631b0e3922Smatthias.ringwald 64169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000 65ee091cf1Smatthias.ringwald 66a45d6b9fS[email protected] #define HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP 11 67da5275c5S[email protected] 6828171530Smatthias.ringwald #ifdef USE_BLUETOOL 6928171530Smatthias.ringwald #include "bt_control_iphone.h" 7028171530Smatthias.ringwald #endif 7128171530Smatthias.ringwald 72758b46ceSmatthias.ringwald static void hci_update_scan_enable(void); 73a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection); 74758b46ceSmatthias.ringwald 7506b35ec0Smatthias.ringwald // the STACK is here 763a9fb326S[email protected] #ifndef HAVE_MALLOC 773a9fb326S[email protected] static hci_stack_t hci_stack_static; 783a9fb326S[email protected] #endif 793a9fb326S[email protected] static hci_stack_t * hci_stack = NULL; 8016833f0aSmatthias.ringwald 81*66fb9560S[email protected] // test helper 82*66fb9560S[email protected] static uint8_t disable_l2cap_timeouts = 0; 83*66fb9560S[email protected] 84*66fb9560S[email protected] 8597addcc5Smatthias.ringwald /** 86ee091cf1Smatthias.ringwald * get connection for a given handle 87ee091cf1Smatthias.ringwald * 88ee091cf1Smatthias.ringwald * @return connection OR NULL, if not found 89ee091cf1Smatthias.ringwald */ 905061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){ 91ee091cf1Smatthias.ringwald linked_item_t *it; 923a9fb326S[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){ 93ee091cf1Smatthias.ringwald if ( ((hci_connection_t *) it)->con_handle == con_handle){ 94ee091cf1Smatthias.ringwald return (hci_connection_t *) it; 95ee091cf1Smatthias.ringwald } 96ee091cf1Smatthias.ringwald } 97ee091cf1Smatthias.ringwald return NULL; 98ee091cf1Smatthias.ringwald } 99ee091cf1Smatthias.ringwald 10062bda3fbS[email protected] hci_connection_t * hci_connection_for_bd_addr(bd_addr_t * addr){ 10162bda3fbS[email protected] linked_item_t *it; 10262bda3fbS[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){ 10362bda3fbS[email protected] hci_connection_t * connection = (hci_connection_t *) it; 10462bda3fbS[email protected] if (memcmp(addr, connection->address, 6) == 0) { 10562bda3fbS[email protected] return connection; 10662bda3fbS[email protected] } 10762bda3fbS[email protected] } 10862bda3fbS[email protected] return NULL; 10962bda3fbS[email protected] } 11062bda3fbS[email protected] 1112b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){ 11228ca2b46S[email protected] hci_connection_t * connection = (hci_connection_t *) linked_item_get_user(&timer->item); 113c785ef68Smatthias.ringwald #ifdef HAVE_TIME 114ee091cf1Smatthias.ringwald struct timeval tv; 115ee091cf1Smatthias.ringwald gettimeofday(&tv, NULL); 116c21e6239Smatthias.ringwald if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) { 117ee091cf1Smatthias.ringwald // connections might be timed out 118ee091cf1Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 119ee091cf1Smatthias.ringwald } 1202b12a0b9Smatthias.ringwald #endif 121e5780900Smatthias.ringwald #ifdef HAVE_TICK 122c785ef68Smatthias.ringwald if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){ 123c785ef68Smatthias.ringwald // connections might be timed out 124c785ef68Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 125c785ef68Smatthias.ringwald } 126c785ef68Smatthias.ringwald #endif 127c785ef68Smatthias.ringwald run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS); 128c785ef68Smatthias.ringwald run_loop_add_timer(timer); 129c785ef68Smatthias.ringwald } 130ee091cf1Smatthias.ringwald 131ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){ 132c7492964Smatthias.ringwald #ifdef HAVE_TIME 133ee091cf1Smatthias.ringwald gettimeofday(&connection->timestamp, NULL); 134c7492964Smatthias.ringwald #endif 135e5780900Smatthias.ringwald #ifdef HAVE_TICK 136c785ef68Smatthias.ringwald connection->timestamp = embedded_get_ticks(); 137c785ef68Smatthias.ringwald #endif 138ee091cf1Smatthias.ringwald } 139ee091cf1Smatthias.ringwald 140ee091cf1Smatthias.ringwald /** 141c8e4258aSmatthias.ringwald * create connection for given address 142c8e4258aSmatthias.ringwald * 14317f1ba2aSmatthias.ringwald * @return connection OR NULL, if no memory left 144c8e4258aSmatthias.ringwald */ 145c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){ 14628ca2b46S[email protected] hci_connection_t * conn = (hci_connection_t *) btstack_memory_hci_connection_get(); 147c8e4258aSmatthias.ringwald if (!conn) return NULL; 148c8e4258aSmatthias.ringwald BD_ADDR_COPY(conn->address, addr); 149c8e4258aSmatthias.ringwald conn->con_handle = 0xffff; 1507d3b3569Smatthias.ringwald conn->authentication_flags = AUTH_FLAGS_NONE; 151afd4e962S[email protected] conn->bonding_flags = 0; 15234d2123cS[email protected] conn->requested_security_level = LEVEL_0; 153ee091cf1Smatthias.ringwald linked_item_set_user(&conn->timeout.item, conn); 154ee091cf1Smatthias.ringwald conn->timeout.process = hci_connection_timeout_handler; 155ee091cf1Smatthias.ringwald hci_connection_timestamp(conn); 156d55db49eSmatthias.ringwald conn->acl_recombination_length = 0; 1577856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 15856cf178bSmatthias.ringwald conn->num_acl_packets_sent = 0; 1593a9fb326S[email protected] linked_list_add(&hci_stack->connections, (linked_item_t *) conn); 160c8e4258aSmatthias.ringwald return conn; 161c8e4258aSmatthias.ringwald } 162c8e4258aSmatthias.ringwald 163c8e4258aSmatthias.ringwald /** 16406b35ec0Smatthias.ringwald * get connection for given address 16597addcc5Smatthias.ringwald * 16697addcc5Smatthias.ringwald * @return connection OR NULL, if not found 16797addcc5Smatthias.ringwald */ 168fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){ 16906b35ec0Smatthias.ringwald linked_item_t *it; 1703a9fb326S[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){ 17106b35ec0Smatthias.ringwald if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){ 17206b35ec0Smatthias.ringwald return (hci_connection_t *) it; 17306b35ec0Smatthias.ringwald } 17406b35ec0Smatthias.ringwald } 17506b35ec0Smatthias.ringwald return NULL; 17606b35ec0Smatthias.ringwald } 17706b35ec0Smatthias.ringwald 17828ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 17928ca2b46S[email protected] conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags); 18028ca2b46S[email protected] } 18128ca2b46S[email protected] 18228ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 18328ca2b46S[email protected] conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags); 18428ca2b46S[email protected] } 18528ca2b46S[email protected] 18628ca2b46S[email protected] 18743bfb1bdSmatthias.ringwald /** 18880ca58a0Smatthias.ringwald * add authentication flags and reset timer 1897fde4af9Smatthias.ringwald */ 1907fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){ 1917fde4af9Smatthias.ringwald bd_addr_t addr; 1927fde4af9Smatthias.ringwald bt_flip_addr(addr, *(bd_addr_t *) bd_addr); 1937fde4af9Smatthias.ringwald hci_connection_t * conn = connection_for_address(addr); 1947fde4af9Smatthias.ringwald if (conn) { 19528ca2b46S[email protected] connectionSetAuthenticationFlags(conn, flags); 19680ca58a0Smatthias.ringwald hci_connection_timestamp(conn); 1977fde4af9Smatthias.ringwald } 1987fde4af9Smatthias.ringwald } 1997fde4af9Smatthias.ringwald 20080ca58a0Smatthias.ringwald int hci_authentication_active_for_handle(hci_con_handle_t handle){ 2015061f3afS[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 20280ca58a0Smatthias.ringwald if (!conn) return 0; 2036724cd9eS[email protected] if (conn->authentication_flags & LEGACY_PAIRING_ACTIVE) return 1; 2046724cd9eS[email protected] if (conn->authentication_flags & SSP_PAIRING_ACTIVE) return 1; 2056724cd9eS[email protected] return 0; 20680ca58a0Smatthias.ringwald } 20780ca58a0Smatthias.ringwald 208c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){ 2093a9fb326S[email protected] if (hci_stack->remote_device_db) { 2103a9fb326S[email protected] hci_stack->remote_device_db->delete_link_key(addr); 211c12e46e7Smatthias.ringwald } 212c12e46e7Smatthias.ringwald } 213c12e46e7Smatthias.ringwald 2147fde4af9Smatthias.ringwald 2157fde4af9Smatthias.ringwald /** 21643bfb1bdSmatthias.ringwald * count connections 21743bfb1bdSmatthias.ringwald */ 21840d1c7a4Smatthias.ringwald static int nr_hci_connections(void){ 21956c253c9Smatthias.ringwald int count = 0; 22043bfb1bdSmatthias.ringwald linked_item_t *it; 2213a9fb326S[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next, count++); 22243bfb1bdSmatthias.ringwald return count; 22343bfb1bdSmatthias.ringwald } 224c8e4258aSmatthias.ringwald 22597addcc5Smatthias.ringwald /** 226ba681a6cSmatthias.ringwald * Dummy handler called by HCI 22716833f0aSmatthias.ringwald */ 2282718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 22916833f0aSmatthias.ringwald } 23016833f0aSmatthias.ringwald 231998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){ 2325061f3afS[email protected] hci_connection_t * connection = hci_connection_for_handle(handle); 233998906cdSmatthias.ringwald if (!connection) { 2347d67539fSmatthias.ringwald log_error("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle); 235998906cdSmatthias.ringwald return 0; 236998906cdSmatthias.ringwald } 237998906cdSmatthias.ringwald return connection->num_acl_packets_sent; 238998906cdSmatthias.ringwald } 239998906cdSmatthias.ringwald 240998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){ 2413a9fb326S[email protected] uint8_t free_slots = hci_stack->total_num_acl_packets; 242998906cdSmatthias.ringwald linked_item_t *it; 2433a9fb326S[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){ 244998906cdSmatthias.ringwald hci_connection_t * connection = (hci_connection_t *) it; 245998906cdSmatthias.ringwald if (free_slots < connection->num_acl_packets_sent) { 2467d67539fSmatthias.ringwald log_error("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n"); 247998906cdSmatthias.ringwald return 0; 248998906cdSmatthias.ringwald } 249998906cdSmatthias.ringwald free_slots -= connection->num_acl_packets_sent; 250998906cdSmatthias.ringwald } 251998906cdSmatthias.ringwald return free_slots; 252998906cdSmatthias.ringwald } 253998906cdSmatthias.ringwald 254c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){ 2552b12a0b9Smatthias.ringwald 2562b12a0b9Smatthias.ringwald // check for async hci transport implementations 2573a9fb326S[email protected] if (hci_stack->hci_transport->can_send_packet_now){ 2583a9fb326S[email protected] if (!hci_stack->hci_transport->can_send_packet_now(packet_type)){ 2592b12a0b9Smatthias.ringwald return 0; 2602b12a0b9Smatthias.ringwald } 2612b12a0b9Smatthias.ringwald } 2622b12a0b9Smatthias.ringwald 2632b12a0b9Smatthias.ringwald // check regular Bluetooth flow control 264c24735b1Smatthias.ringwald switch (packet_type) { 265c24735b1Smatthias.ringwald case HCI_ACL_DATA_PACKET: 266c24735b1Smatthias.ringwald return hci_number_free_acl_slots(); 267c24735b1Smatthias.ringwald case HCI_COMMAND_DATA_PACKET: 2683a9fb326S[email protected] return hci_stack->num_cmd_packets; 269c24735b1Smatthias.ringwald default: 270c24735b1Smatthias.ringwald return 0; 271c24735b1Smatthias.ringwald } 272c24735b1Smatthias.ringwald } 273c24735b1Smatthias.ringwald 274ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){ 2757856c818Smatthias.ringwald 2766218e6f1Smatthias.ringwald // check for free places on BT module 2775932f1b4Smatthias.ringwald if (!hci_number_free_acl_slots()) return BTSTACK_ACL_BUFFERS_FULL; 2786218e6f1Smatthias.ringwald 2797856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 2805061f3afS[email protected] hci_connection_t *connection = hci_connection_for_handle( con_handle); 28156cf178bSmatthias.ringwald if (!connection) return 0; 28256cf178bSmatthias.ringwald hci_connection_timestamp(connection); 28356cf178bSmatthias.ringwald 28456cf178bSmatthias.ringwald // count packet 28556cf178bSmatthias.ringwald connection->num_acl_packets_sent++; 2867b5fbe1fSmatthias.ringwald // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent); 2877856c818Smatthias.ringwald 28800d8e42eSmatthias.ringwald // send packet 2893a9fb326S[email protected] int err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size); 2906218e6f1Smatthias.ringwald 29100d8e42eSmatthias.ringwald return err; 292ee091cf1Smatthias.ringwald } 293ee091cf1Smatthias.ringwald 29416833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){ 2957856c818Smatthias.ringwald 296e76a89eeS[email protected] // log_info("acl_handler: size %u", size); 297e76a89eeS[email protected] 2987856c818Smatthias.ringwald // get info 2997856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 3005061f3afS[email protected] hci_connection_t *conn = hci_connection_for_handle(con_handle); 3017856c818Smatthias.ringwald uint8_t acl_flags = READ_ACL_FLAGS(packet); 3027856c818Smatthias.ringwald uint16_t acl_length = READ_ACL_LENGTH(packet); 3037856c818Smatthias.ringwald 3047856c818Smatthias.ringwald // ignore non-registered handle 3057856c818Smatthias.ringwald if (!conn){ 3067d67539fSmatthias.ringwald log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle); 3077856c818Smatthias.ringwald return; 3087856c818Smatthias.ringwald } 3097856c818Smatthias.ringwald 310e76a89eeS[email protected] // assert packet is complete 3119ecc3e17S[email protected] if (acl_length + 4 != size){ 312e76a89eeS[email protected] log_error("hci.c: acl_handler called with ACL packet of wrong size %u, expected %u => dropping packet", size, acl_length + 4); 313e76a89eeS[email protected] return; 314e76a89eeS[email protected] } 315e76a89eeS[email protected] 3167856c818Smatthias.ringwald // update idle timestamp 3177856c818Smatthias.ringwald hci_connection_timestamp(conn); 3187856c818Smatthias.ringwald 3197856c818Smatthias.ringwald // handle different packet types 3207856c818Smatthias.ringwald switch (acl_flags & 0x03) { 3217856c818Smatthias.ringwald 3227856c818Smatthias.ringwald case 0x01: // continuation fragment 3237856c818Smatthias.ringwald 3247856c818Smatthias.ringwald // sanity check 3257856c818Smatthias.ringwald if (conn->acl_recombination_pos == 0) { 3267d67539fSmatthias.ringwald log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle); 3277856c818Smatthias.ringwald return; 3287856c818Smatthias.ringwald } 3297856c818Smatthias.ringwald 3307856c818Smatthias.ringwald // append fragment payload (header already stored) 3317856c818Smatthias.ringwald memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length ); 3327856c818Smatthias.ringwald conn->acl_recombination_pos += acl_length; 3337856c818Smatthias.ringwald 3347d67539fSmatthias.ringwald // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length, 335decc01a8Smatthias.ringwald // conn->acl_recombination_pos, conn->acl_recombination_length); 3367856c818Smatthias.ringwald 3377856c818Smatthias.ringwald // forward complete L2CAP packet if complete. 3387856c818Smatthias.ringwald if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header 3397856c818Smatthias.ringwald 3403a9fb326S[email protected] hci_stack->packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos); 3417856c818Smatthias.ringwald // reset recombination buffer 3427856c818Smatthias.ringwald conn->acl_recombination_length = 0; 3437856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 3447856c818Smatthias.ringwald } 3457856c818Smatthias.ringwald break; 3467856c818Smatthias.ringwald 3477856c818Smatthias.ringwald case 0x02: { // first fragment 3487856c818Smatthias.ringwald 3497856c818Smatthias.ringwald // sanity check 3507856c818Smatthias.ringwald if (conn->acl_recombination_pos) { 3517d67539fSmatthias.ringwald log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle); 3527856c818Smatthias.ringwald return; 3537856c818Smatthias.ringwald } 3547856c818Smatthias.ringwald 3557856c818Smatthias.ringwald // peek into L2CAP packet! 3567856c818Smatthias.ringwald uint16_t l2cap_length = READ_L2CAP_LENGTH( packet ); 3577856c818Smatthias.ringwald 358e76a89eeS[email protected] // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length); 359decc01a8Smatthias.ringwald 3607856c818Smatthias.ringwald // compare fragment size to L2CAP packet size 3617856c818Smatthias.ringwald if (acl_length >= l2cap_length + 4){ 3627856c818Smatthias.ringwald 3637856c818Smatthias.ringwald // forward fragment as L2CAP packet 3643a9fb326S[email protected] hci_stack->packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4); 3657856c818Smatthias.ringwald 3667856c818Smatthias.ringwald } else { 3677856c818Smatthias.ringwald // store first fragment and tweak acl length for complete package 3687856c818Smatthias.ringwald memcpy(conn->acl_recombination_buffer, packet, acl_length + 4); 3697856c818Smatthias.ringwald conn->acl_recombination_pos = acl_length + 4; 3707856c818Smatthias.ringwald conn->acl_recombination_length = l2cap_length; 371decc01a8Smatthias.ringwald bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4); 3727856c818Smatthias.ringwald } 3737856c818Smatthias.ringwald break; 3747856c818Smatthias.ringwald 3757856c818Smatthias.ringwald } 3767856c818Smatthias.ringwald default: 3777d67539fSmatthias.ringwald log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03); 3787856c818Smatthias.ringwald return; 3797856c818Smatthias.ringwald } 38094ab26f8Smatthias.ringwald 38194ab26f8Smatthias.ringwald // execute main loop 38294ab26f8Smatthias.ringwald hci_run(); 38316833f0aSmatthias.ringwald } 38422909952Smatthias.ringwald 38567a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){ 386339b6768Smatthias.ringwald log_info("Connection closed: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 3873c4d4b90Smatthias.ringwald 3883c4d4b90Smatthias.ringwald // cancel all l2cap connections 3893c4d4b90Smatthias.ringwald hci_emit_disconnection_complete(conn->con_handle, 0x16); // terminated by local host 3903c4d4b90Smatthias.ringwald 391c7e0c5f6Smatthias.ringwald run_loop_remove_timer(&conn->timeout); 392c785ef68Smatthias.ringwald 3933a9fb326S[email protected] linked_list_remove(&hci_stack->connections, (linked_item_t *) conn); 394a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 3953c4d4b90Smatthias.ringwald 3963c4d4b90Smatthias.ringwald // now it's gone 397c7e0c5f6Smatthias.ringwald hci_emit_nr_connections_changed(); 398c7e0c5f6Smatthias.ringwald } 399c7e0c5f6Smatthias.ringwald 4000c042179S[email protected] static const uint16_t packet_type_sizes[] = { 4018f8108aaSmatthias.ringwald 0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE, 4028f8108aaSmatthias.ringwald HCI_ACL_DH1_SIZE, 0, 0, 0, 4038f8108aaSmatthias.ringwald HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE, 4048f8108aaSmatthias.ringwald HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE 4058f8108aaSmatthias.ringwald }; 40665389bfcS[email protected] static const uint8_t packet_type_feature_requirement_bit[] = { 40765389bfcS[email protected] 0, // 3 slot packets 40865389bfcS[email protected] 1, // 5 slot packets 40965389bfcS[email protected] 25, // EDR 2 mpbs 41065389bfcS[email protected] 26, // EDR 3 mbps 41165389bfcS[email protected] 39, // 3 slot EDR packts 41265389bfcS[email protected] 40, // 5 slot EDR packet 41365389bfcS[email protected] }; 41465389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = { 41565389bfcS[email protected] 0x0f00, // 3 slot packets 41665389bfcS[email protected] 0xf000, // 5 slot packets 41765389bfcS[email protected] 0x1102, // EDR 2 mpbs 41865389bfcS[email protected] 0x2204, // EDR 3 mbps 41965389bfcS[email protected] 0x0300, // 3 slot EDR packts 42065389bfcS[email protected] 0x3000, // 5 slot EDR packet 42165389bfcS[email protected] }; 4228f8108aaSmatthias.ringwald 42365389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){ 42465389bfcS[email protected] // enable packet types based on size 4258f8108aaSmatthias.ringwald uint16_t packet_types = 0; 4268f8108aaSmatthias.ringwald int i; 4278f8108aaSmatthias.ringwald for (i=0;i<16;i++){ 4288f8108aaSmatthias.ringwald if (packet_type_sizes[i] == 0) continue; 4298f8108aaSmatthias.ringwald if (packet_type_sizes[i] <= buffer_size){ 4308f8108aaSmatthias.ringwald packet_types |= 1 << i; 4318f8108aaSmatthias.ringwald } 4328f8108aaSmatthias.ringwald } 43365389bfcS[email protected] // disable packet types due to missing local supported features 43465389bfcS[email protected] for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){ 43565389bfcS[email protected] int bit_idx = packet_type_feature_requirement_bit[i]; 43665389bfcS[email protected] int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0; 43765389bfcS[email protected] if (feature_set) continue; 43865389bfcS[email protected] log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]); 43965389bfcS[email protected] packet_types &= ~packet_type_feature_packet_mask[i]; 44065389bfcS[email protected] } 4418f8108aaSmatthias.ringwald // flip bits for "may not be used" 4428f8108aaSmatthias.ringwald packet_types ^= 0x3306; 4438f8108aaSmatthias.ringwald return packet_types; 4448f8108aaSmatthias.ringwald } 4458f8108aaSmatthias.ringwald 4468f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){ 4473a9fb326S[email protected] return hci_stack->packet_types; 4488f8108aaSmatthias.ringwald } 4498f8108aaSmatthias.ringwald 4507dc17943Smatthias.ringwald uint8_t* hci_get_outgoing_acl_packet_buffer(void){ 4517dc17943Smatthias.ringwald // hci packet buffer is >= acl data packet length 4523a9fb326S[email protected] return hci_stack->hci_packet_buffer; 4537dc17943Smatthias.ringwald } 4547dc17943Smatthias.ringwald 455f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){ 4563a9fb326S[email protected] return hci_stack->acl_data_packet_length; 4577dc17943Smatthias.ringwald } 4587dc17943Smatthias.ringwald 459f5d8d141S[email protected] int hci_ssp_supported(void){ 460f5d8d141S[email protected] // No 51, byte 6, bit 3 4613a9fb326S[email protected] return (hci_stack->local_supported_features[6] & (1 << 3)) != 0; 462f5d8d141S[email protected] } 463f5d8d141S[email protected] 464f5d8d141S[email protected] int hci_classic_supported(void){ 465f5d8d141S[email protected] // No 37, byte 4, bit 5, = No BR/EDR Support 4663a9fb326S[email protected] return (hci_stack->local_supported_features[4] & (1 << 5)) == 0; 467f5d8d141S[email protected] } 468f5d8d141S[email protected] 469f5d8d141S[email protected] int hci_le_supported(void){ 470f5d8d141S[email protected] // No 37, byte 4, bit 6 = LE Supported (Controller) 471f5d8d141S[email protected] #ifdef HAVE_BLE 4723a9fb326S[email protected] return (hci_stack->local_supported_features[4] & (1 << 6)) != 0; 473f5d8d141S[email protected] #else 474f5d8d141S[email protected] return 0; 475f5d8d141S[email protected] #endif 476f5d8d141S[email protected] } 477f5d8d141S[email protected] 47869a97523S[email protected] // get addr type and address used in advertisement packets 47918904abdS[email protected] void hci_le_advertisement_address(uint8_t * addr_type, bd_addr_t * addr){ 4803a9fb326S[email protected] *addr_type = hci_stack->adv_addr_type; 4813a9fb326S[email protected] if (hci_stack->adv_addr_type){ 4823a9fb326S[email protected] memcpy(addr, hci_stack->adv_address, 6); 48369a97523S[email protected] } else { 4843a9fb326S[email protected] memcpy(addr, hci_stack->local_bd_addr, 6); 48569a97523S[email protected] } 48669a97523S[email protected] } 48769a97523S[email protected] 48874ec757aSmatthias.ringwald // avoid huge local variables 489223aafc1Smatthias.ringwald #ifndef EMBEDDED 49074ec757aSmatthias.ringwald static device_name_t device_name; 491223aafc1Smatthias.ringwald #endif 49216833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){ 493e76a89eeS[email protected] 494e76a89eeS[email protected] uint16_t event_length = packet[1]; 495e76a89eeS[email protected] 496e76a89eeS[email protected] // assert packet is complete 497e76a89eeS[email protected] if (size != event_length + 2){ 498e76a89eeS[email protected] log_error("hci.c: event_handler called with event packet of wrong size %u, expected %u => dropping packet", size, event_length + 2); 499e76a89eeS[email protected] return; 500e76a89eeS[email protected] } 501e76a89eeS[email protected] 5021281a47eSmatthias.ringwald bd_addr_t addr; 5032d00edd4Smatthias.ringwald uint8_t link_type; 504fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 5051f7b95a1Smatthias.ringwald hci_connection_t * conn; 50656cf178bSmatthias.ringwald int i; 50722909952Smatthias.ringwald 5085909f7f2Smatthias.ringwald // printf("HCI:EVENT:%02x\n", packet[0]); 5095909f7f2Smatthias.ringwald 5106772a24cSmatthias.ringwald switch (packet[0]) { 51122909952Smatthias.ringwald 5126772a24cSmatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 5137ec5eeaaSmatthias.ringwald // get num cmd packets 5143a9fb326S[email protected] // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack->num_cmd_packets, packet[2]); 5153a9fb326S[email protected] hci_stack->num_cmd_packets = packet[2]; 5167ec5eeaaSmatthias.ringwald 517e2edc0c3Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){ 518e2edc0c3Smatthias.ringwald // from offset 5 519e2edc0c3Smatthias.ringwald // status 5201d279b20Smatthias.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" 5213a9fb326S[email protected] hci_stack->acl_data_packet_length = READ_BT_16(packet, 6); 52256cf178bSmatthias.ringwald // ignore: SCO data packet len (8) 5233a9fb326S[email protected] hci_stack->total_num_acl_packets = packet[9]; 52456cf178bSmatthias.ringwald // ignore: total num SCO packets 5253a9fb326S[email protected] if (hci_stack->state == HCI_STATE_INITIALIZING){ 526a7a04bd9Smatthias.ringwald // determine usable ACL payload size 5273a9fb326S[email protected] if (HCI_ACL_PAYLOAD_SIZE < hci_stack->acl_data_packet_length){ 5283a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 5298f8108aaSmatthias.ringwald } 53065389bfcS[email protected] log_info("hci_read_buffer_size: used size %u, count %u\n", 5313a9fb326S[email protected] hci_stack->acl_data_packet_length, hci_stack->total_num_acl_packets); 532e2edc0c3Smatthias.ringwald } 53356cf178bSmatthias.ringwald } 53465a46ef3S[email protected] #ifdef HAVE_BLE 53565a46ef3S[email protected] if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)){ 5363a9fb326S[email protected] hci_stack->le_data_packet_length = READ_BT_16(packet, 6); 5373a9fb326S[email protected] hci_stack->total_num_le_packets = packet[8]; 5383a9fb326S[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); 53965a46ef3S[email protected] } 54065a46ef3S[email protected] #endif 541188981d2Smatthias.ringwald // Dump local address 542188981d2Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) { 5433a9fb326S[email protected] bt_flip_addr(hci_stack->local_bd_addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]); 544188981d2Smatthias.ringwald log_info("Local Address, Status: 0x%02x: Addr: %s\n", 5453a9fb326S[email protected] packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr)); 546188981d2Smatthias.ringwald } 547381fbed8Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 5483a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 549381fbed8Smatthias.ringwald } 55065389bfcS[email protected] // Note: HCI init checks 551559e517eS[email protected] if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){ 5523a9fb326S[email protected] memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8); 553559e517eS[email protected] log_info("Local Supported Features: 0x%02x%02x%02x%02x%02x%02x%02x%02x", 5543a9fb326S[email protected] hci_stack->local_supported_features[0], hci_stack->local_supported_features[1], 5553a9fb326S[email protected] hci_stack->local_supported_features[2], hci_stack->local_supported_features[3], 5563a9fb326S[email protected] hci_stack->local_supported_features[4], hci_stack->local_supported_features[5], 5573a9fb326S[email protected] hci_stack->local_supported_features[6], hci_stack->local_supported_features[7]); 55865389bfcS[email protected] 55965389bfcS[email protected] // determine usable ACL packet types based buffer size and supported features 5603a9fb326S[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]); 5613a9fb326S[email protected] log_info("packet types %04x", hci_stack->packet_types); 562f5d8d141S[email protected] 563f5d8d141S[email protected] // Classic/LE 564f5d8d141S[email protected] log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported()); 565559e517eS[email protected] } 56656cf178bSmatthias.ringwald break; 56756cf178bSmatthias.ringwald 5687ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 5697ec5eeaaSmatthias.ringwald // get num cmd packets 5703a9fb326S[email protected] // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack->num_cmd_packets, packet[3]); 5713a9fb326S[email protected] hci_stack->num_cmd_packets = packet[3]; 5727ec5eeaaSmatthias.ringwald break; 5737ec5eeaaSmatthias.ringwald 57456cf178bSmatthias.ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 57556cf178bSmatthias.ringwald for (i=0; i<packet[2];i++){ 57656cf178bSmatthias.ringwald handle = READ_BT_16(packet, 3 + 2*i); 57756cf178bSmatthias.ringwald uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i); 5785061f3afS[email protected] conn = hci_connection_for_handle(handle); 57956cf178bSmatthias.ringwald if (!conn){ 5807d67539fSmatthias.ringwald log_error("hci_number_completed_packet lists unused con handle %u\n", handle); 58156cf178bSmatthias.ringwald continue; 58256cf178bSmatthias.ringwald } 58356cf178bSmatthias.ringwald conn->num_acl_packets_sent -= num_packets; 5847b5fbe1fSmatthias.ringwald // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent); 58556cf178bSmatthias.ringwald } 5866772a24cSmatthias.ringwald break; 5876772a24cSmatthias.ringwald 5881f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 58937eaa4cfSmatthias.ringwald bt_flip_addr(addr, &packet[2]); 59037eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 5912d00edd4Smatthias.ringwald link_type = packet[11]; 592339b6768Smatthias.ringwald log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type); 59337eaa4cfSmatthias.ringwald if (link_type == 1) { // ACL 5941f7b95a1Smatthias.ringwald conn = connection_for_address(addr); 5951f7b95a1Smatthias.ringwald if (!conn) { 5961f7b95a1Smatthias.ringwald conn = create_connection_for_addr(addr); 5971f7b95a1Smatthias.ringwald } 598ce4c8fabSmatthias.ringwald if (!conn) { 599ce4c8fabSmatthias.ringwald // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 6003a9fb326S[email protected] hci_stack->decline_reason = 0x0d; 6013a9fb326S[email protected] BD_ADDR_COPY(hci_stack->decline_addr, addr); 602ce4c8fabSmatthias.ringwald break; 603ce4c8fabSmatthias.ringwald } 60432ab9390Smatthias.ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 60532ab9390Smatthias.ringwald hci_run(); 60637eaa4cfSmatthias.ringwald } else { 607ce4c8fabSmatthias.ringwald // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A) 6083a9fb326S[email protected] hci_stack->decline_reason = 0x0a; 6093a9fb326S[email protected] BD_ADDR_COPY(hci_stack->decline_addr, addr); 61037eaa4cfSmatthias.ringwald } 6111f7b95a1Smatthias.ringwald break; 6121f7b95a1Smatthias.ringwald 6136772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 614fe1ed1b8Smatthias.ringwald // Connection management 615fe1ed1b8Smatthias.ringwald bt_flip_addr(addr, &packet[5]); 616339b6768Smatthias.ringwald log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr)); 6171f7b95a1Smatthias.ringwald conn = connection_for_address(addr); 618fe1ed1b8Smatthias.ringwald if (conn) { 619b448a0e7Smatthias.ringwald if (!packet[2]){ 620c8e4258aSmatthias.ringwald conn->state = OPEN; 621fe1ed1b8Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 3); 622ad83dc6aS[email protected] conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES; 623ee091cf1Smatthias.ringwald 624c785ef68Smatthias.ringwald // restart timer 625c21e6239Smatthias.ringwald run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 626ee091cf1Smatthias.ringwald run_loop_add_timer(&conn->timeout); 627c785ef68Smatthias.ringwald 628339b6768Smatthias.ringwald log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 62943bfb1bdSmatthias.ringwald 63043bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 631b448a0e7Smatthias.ringwald } else { 632ad83dc6aS[email protected] // notify client if dedicated bonding 633ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 634ad83dc6aS[email protected] hci_emit_dedicated_bonding_result(conn, packet[2]); 635ad83dc6aS[email protected] } 636ad83dc6aS[email protected] 637b448a0e7Smatthias.ringwald // connection failed, remove entry 6383a9fb326S[email protected] linked_list_remove(&hci_stack->connections, (linked_item_t *) conn); 639a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 640c12e46e7Smatthias.ringwald 641c12e46e7Smatthias.ringwald // if authentication error, also delete link key 642c12e46e7Smatthias.ringwald if (packet[2] == 0x05) { 643c12e46e7Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 644c12e46e7Smatthias.ringwald } 645fe1ed1b8Smatthias.ringwald } 646fe1ed1b8Smatthias.ringwald } 6476772a24cSmatthias.ringwald break; 648fe1ed1b8Smatthias.ringwald 649afd4e962S[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 650afd4e962S[email protected] handle = READ_BT_16(packet, 3); 651afd4e962S[email protected] conn = hci_connection_for_handle(handle); 652afd4e962S[email protected] if (!conn) break; 653afd4e962S[email protected] if (!packet[2]){ 654afd4e962S[email protected] uint8_t * features = &packet[5]; 655afd4e962S[email protected] if (features[6] & (1 << 3)){ 656afd4e962S[email protected] conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP; 657afd4e962S[email protected] } 658afd4e962S[email protected] } 659afd4e962S[email protected] conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES; 660ad83dc6aS[email protected] log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x", conn->bonding_flags); 661ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 662ad83dc6aS[email protected] conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 663ad83dc6aS[email protected] } 664afd4e962S[email protected] break; 665afd4e962S[email protected] 6667fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 6677b5fbe1fSmatthias.ringwald log_info("HCI_EVENT_LINK_KEY_REQUEST\n"); 6687fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST); 66974d716b5S[email protected] // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST 6703a9fb326S[email protected] if (hci_stack->bondable && !hci_stack->remote_device_db) break; 67132ab9390Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST); 67264472d52Smatthias.ringwald hci_run(); 673d9a327f9Smatthias.ringwald // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set 67429d53098Smatthias.ringwald return; 6757fde4af9Smatthias.ringwald 6769ab95c90S[email protected] case HCI_EVENT_LINK_KEY_NOTIFICATION: { 67729d53098Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 6789ab95c90S[email protected] conn = connection_for_address(addr); 6799ab95c90S[email protected] if (!conn) break; 6809ab95c90S[email protected] conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION; 6819ab95c90S[email protected] link_key_type_t link_key_type = packet[24]; 6829ab95c90S[email protected] // Change Connection Encryption keeps link key type 6839ab95c90S[email protected] if (link_key_type != CHANGED_COMBINATION_KEY){ 6849ab95c90S[email protected] conn->link_key_type = link_key_type; 6859ab95c90S[email protected] } 6863a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 6873a9fb326S[email protected] hci_stack->remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8], conn->link_key_type); 68829d53098Smatthias.ringwald // still forward event to allow dismiss of pairing dialog 6897fde4af9Smatthias.ringwald break; 6909ab95c90S[email protected] } 6917fde4af9Smatthias.ringwald 6927fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 6936724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE); 6944c57c146S[email protected] // non-bondable mode: pin code negative reply will be sent 6953a9fb326S[email protected] if (!hci_stack->bondable){ 696899283eaS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST); 697f8fb5f6eS[email protected] hci_run(); 698f8fb5f6eS[email protected] return; 6994c57c146S[email protected] } 700d9a327f9Smatthias.ringwald // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key 7013a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 702d9a327f9Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 7033a9fb326S[email protected] hci_stack->remote_device_db->delete_link_key(&addr); 7047fde4af9Smatthias.ringwald break; 7057fde4af9Smatthias.ringwald 7061d6b20aeS[email protected] case HCI_EVENT_IO_CAPABILITY_REQUEST: 7071d6b20aeS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST); 708dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY); 709dbe1a790S[email protected] break; 710dbe1a790S[email protected] 711dbe1a790S[email protected] case HCI_EVENT_USER_CONFIRMATION_REQUEST: 7126724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 7133a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 714dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY); 715dbe1a790S[email protected] break; 716dbe1a790S[email protected] 717dbe1a790S[email protected] case HCI_EVENT_USER_PASSKEY_REQUEST: 7186724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 7193a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 720dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY); 7211d6b20aeS[email protected] break; 7221d6b20aeS[email protected] 723f0944df2S[email protected] case HCI_EVENT_ENCRYPTION_CHANGE: 724f0944df2S[email protected] handle = READ_BT_16(packet, 3); 725f0944df2S[email protected] conn = hci_connection_for_handle(handle); 726f0944df2S[email protected] if (!conn) break; 727ad83dc6aS[email protected] if (packet[2] == 0) { 728f0944df2S[email protected] if (packet[5]){ 729f0944df2S[email protected] conn->authentication_flags |= CONNECTION_ENCRYPTED; 730f0944df2S[email protected] } else { 731536f9994S[email protected] conn->authentication_flags &= ~CONNECTION_ENCRYPTED; 732f0944df2S[email protected] } 733ad83dc6aS[email protected] } 734a00031e2S[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 735f0944df2S[email protected] break; 736f0944df2S[email protected] 7371eb2563eS[email protected] case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT: 7381eb2563eS[email protected] handle = READ_BT_16(packet, 3); 7391eb2563eS[email protected] conn = hci_connection_for_handle(handle); 7401eb2563eS[email protected] if (!conn) break; 741ad83dc6aS[email protected] 742ad83dc6aS[email protected] // dedicated bonding: send result and disconnect 743ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 744ad83dc6aS[email protected] conn->bonding_flags &= ~BONDING_DEDICATED; 745ad83dc6aS[email protected] hci_emit_dedicated_bonding_result( conn, packet[2]); 746ad83dc6aS[email protected] conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE; 747ad83dc6aS[email protected] break; 748ad83dc6aS[email protected] } 749ad83dc6aS[email protected] 750b5cb6874S[email protected] if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){ 7511eb2563eS[email protected] // link key sufficient for requested security 7521eb2563eS[email protected] conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 753ad83dc6aS[email protected] break; 754ad83dc6aS[email protected] } 7551eb2563eS[email protected] // not enough 7561eb2563eS[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 7571eb2563eS[email protected] break; 75834d2123cS[email protected] 759223aafc1Smatthias.ringwald #ifndef EMBEDDED 76074ec757aSmatthias.ringwald case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 7613a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 76274ec757aSmatthias.ringwald if (packet[2]) break; // status not ok 76374ec757aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 7645a06394aSmatthias.ringwald // fix for invalid remote names - terminate on 0xff 7655a06394aSmatthias.ringwald for (i=0; i<248;i++){ 7665a06394aSmatthias.ringwald if (packet[9+i] == 0xff){ 7675a06394aSmatthias.ringwald packet[9+i] = 0; 7685a06394aSmatthias.ringwald break; 769cdc9101dSmatthias.ringwald } 7705a06394aSmatthias.ringwald } 771d2fe945cS[email protected] memset(&device_name, 0, sizeof(device_name_t)); 77274ec757aSmatthias.ringwald strncpy((char*) device_name, (char*) &packet[9], 248); 7733a9fb326S[email protected] hci_stack->remote_device_db->put_name(&addr, &device_name); 77474ec757aSmatthias.ringwald break; 77574ec757aSmatthias.ringwald 77674ec757aSmatthias.ringwald case HCI_EVENT_INQUIRY_RESULT: 77774ec757aSmatthias.ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 7783a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 77974ec757aSmatthias.ringwald // first send inq result packet 7803a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size); 78174ec757aSmatthias.ringwald // then send cached remote names 78274ec757aSmatthias.ringwald for (i=0; i<packet[2];i++){ 78374ec757aSmatthias.ringwald bt_flip_addr(addr, &packet[3+i*6]); 7843a9fb326S[email protected] if (hci_stack->remote_device_db->get_name(&addr, &device_name)){ 78574ec757aSmatthias.ringwald hci_emit_remote_name_cached(&addr, &device_name); 78674ec757aSmatthias.ringwald } 78774ec757aSmatthias.ringwald } 78874ec757aSmatthias.ringwald return; 789223aafc1Smatthias.ringwald #endif 79074ec757aSmatthias.ringwald 7916772a24cSmatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 792fe1ed1b8Smatthias.ringwald if (!packet[2]){ 793fe1ed1b8Smatthias.ringwald handle = READ_BT_16(packet, 3); 7945061f3afS[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 795fe1ed1b8Smatthias.ringwald if (conn) { 796c7e0c5f6Smatthias.ringwald hci_shutdown_connection(conn); 797fe1ed1b8Smatthias.ringwald } 798fe1ed1b8Smatthias.ringwald } 7996772a24cSmatthias.ringwald break; 8006772a24cSmatthias.ringwald 801c68bdf90Smatthias.ringwald case HCI_EVENT_HARDWARE_ERROR: 8023a9fb326S[email protected] if(hci_stack->control && hci_stack->control->hw_error){ 8033a9fb326S[email protected] (*hci_stack->control->hw_error)(); 804c68bdf90Smatthias.ringwald } 805c68bdf90Smatthias.ringwald break; 806c68bdf90Smatthias.ringwald 8075909f7f2Smatthias.ringwald #ifdef HAVE_BLE 8085909f7f2Smatthias.ringwald case HCI_EVENT_LE_META: 8095909f7f2Smatthias.ringwald switch (packet[2]) { 8105909f7f2Smatthias.ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 8115909f7f2Smatthias.ringwald // Connection management 8125909f7f2Smatthias.ringwald bt_flip_addr(addr, &packet[8]); 8135909f7f2Smatthias.ringwald log_info("LE Connection_complete (status=%u) %s\n", packet[3], bd_addr_to_str(addr)); 8145909f7f2Smatthias.ringwald // LE connections are auto-accepted, so just create a connection if there isn't one already 8155909f7f2Smatthias.ringwald conn = connection_for_address(addr); 8165909f7f2Smatthias.ringwald if (packet[3]){ 8175909f7f2Smatthias.ringwald if (conn){ 8185909f7f2Smatthias.ringwald // outgoing connection failed, remove entry 8193a9fb326S[email protected] linked_list_remove(&hci_stack->connections, (linked_item_t *) conn); 8205909f7f2Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 8215909f7f2Smatthias.ringwald 8225909f7f2Smatthias.ringwald } 8235909f7f2Smatthias.ringwald // if authentication error, also delete link key 8245909f7f2Smatthias.ringwald if (packet[3] == 0x05) { 8255909f7f2Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 8265909f7f2Smatthias.ringwald } 8275909f7f2Smatthias.ringwald break; 8285909f7f2Smatthias.ringwald } 8295909f7f2Smatthias.ringwald if (!conn){ 8305909f7f2Smatthias.ringwald conn = create_connection_for_addr(addr); 8315909f7f2Smatthias.ringwald } 8325909f7f2Smatthias.ringwald if (!conn){ 8335909f7f2Smatthias.ringwald // no memory 8345909f7f2Smatthias.ringwald break; 8355909f7f2Smatthias.ringwald } 8365909f7f2Smatthias.ringwald 8375909f7f2Smatthias.ringwald conn->state = OPEN; 8385909f7f2Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 4); 8395909f7f2Smatthias.ringwald 8405909f7f2Smatthias.ringwald // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock 8415909f7f2Smatthias.ringwald 8425909f7f2Smatthias.ringwald // restart timer 8435909f7f2Smatthias.ringwald // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 8445909f7f2Smatthias.ringwald // run_loop_add_timer(&conn->timeout); 8455909f7f2Smatthias.ringwald 8465909f7f2Smatthias.ringwald log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 8475909f7f2Smatthias.ringwald 8485909f7f2Smatthias.ringwald hci_emit_nr_connections_changed(); 8495909f7f2Smatthias.ringwald break; 8505909f7f2Smatthias.ringwald 85165a46ef3S[email protected] // printf("LE buffer size: %u, count %u\n", READ_BT_16(packet,6), packet[8]); 85265a46ef3S[email protected] 8535909f7f2Smatthias.ringwald default: 8545909f7f2Smatthias.ringwald break; 8555909f7f2Smatthias.ringwald } 8565909f7f2Smatthias.ringwald break; 8575909f7f2Smatthias.ringwald #endif 8585909f7f2Smatthias.ringwald 8596772a24cSmatthias.ringwald default: 8606772a24cSmatthias.ringwald break; 861fe1ed1b8Smatthias.ringwald } 862fe1ed1b8Smatthias.ringwald 8633429f56bSmatthias.ringwald // handle BT initialization 8643a9fb326S[email protected] if (hci_stack->state == HCI_STATE_INITIALIZING){ 8653a9fb326S[email protected] if (hci_stack->substate % 2){ 8663429f56bSmatthias.ringwald // odd: waiting for event 867f155fca3Smatthias.ringwald if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || packet[0] == HCI_EVENT_COMMAND_STATUS){ 868c9af4d3fS[email protected] // wait for explicit COMMAND COMPLETE on RESET 8693a9fb326S[email protected] if (hci_stack->substate > 1 || COMMAND_COMPLETE_EVENT(packet, hci_reset)) { 8703a9fb326S[email protected] hci_stack->substate++; 8713429f56bSmatthias.ringwald } 8723429f56bSmatthias.ringwald } 87322909952Smatthias.ringwald } 874c9af4d3fS[email protected] } 87522909952Smatthias.ringwald 87689db417bSmatthias.ringwald // help with BT sleep 8773a9fb326S[email protected] if (hci_stack->state == HCI_STATE_FALLING_ASLEEP 8783a9fb326S[email protected] && hci_stack->substate == 1 87989db417bSmatthias.ringwald && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 8803a9fb326S[email protected] hci_stack->substate++; 88189db417bSmatthias.ringwald } 88289db417bSmatthias.ringwald 8833a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size); 88494ab26f8Smatthias.ringwald 88594ab26f8Smatthias.ringwald // execute main loop 88694ab26f8Smatthias.ringwald hci_run(); 88716833f0aSmatthias.ringwald } 88816833f0aSmatthias.ringwald 8890a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 89010e830c9Smatthias.ringwald switch (packet_type) { 89110e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 89210e830c9Smatthias.ringwald event_handler(packet, size); 89310e830c9Smatthias.ringwald break; 89410e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 89510e830c9Smatthias.ringwald acl_handler(packet, size); 89610e830c9Smatthias.ringwald break; 89710e830c9Smatthias.ringwald default: 89810e830c9Smatthias.ringwald break; 89910e830c9Smatthias.ringwald } 90010e830c9Smatthias.ringwald } 90110e830c9Smatthias.ringwald 902fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 9032718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 9043a9fb326S[email protected] hci_stack->packet_handler = handler; 90516833f0aSmatthias.ringwald } 90616833f0aSmatthias.ringwald 9074f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){ 908475c8125Smatthias.ringwald 9093a9fb326S[email protected] #ifdef HAVE_MALLOC 9103a9fb326S[email protected] if (!hci_stack) { 9113a9fb326S[email protected] hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t)); 9123a9fb326S[email protected] } 9133a9fb326S[email protected] #else 9143a9fb326S[email protected] hci_stack = &hci_stack_static; 9153a9fb326S[email protected] #endif 916*66fb9560S[email protected] memset(hci_stack, 0, sizeof(hci_stack_t)); 9173a9fb326S[email protected] 918475c8125Smatthias.ringwald // reference to use transport layer implementation 9193a9fb326S[email protected] hci_stack->hci_transport = transport; 920475c8125Smatthias.ringwald 92111e23e5fSmatthias.ringwald // references to used control implementation 9223a9fb326S[email protected] hci_stack->control = control; 92311e23e5fSmatthias.ringwald 92411e23e5fSmatthias.ringwald // reference to used config 9253a9fb326S[email protected] hci_stack->config = config; 92611e23e5fSmatthias.ringwald 927fe1ed1b8Smatthias.ringwald // no connections yet 9283a9fb326S[email protected] hci_stack->connections = NULL; 9293a9fb326S[email protected] hci_stack->discoverable = 0; 9303a9fb326S[email protected] hci_stack->connectable = 0; 9313a9fb326S[email protected] hci_stack->bondable = 1; 932758b46ceSmatthias.ringwald 933b031bebbSmatthias.ringwald // no pending cmds 9343a9fb326S[email protected] hci_stack->decline_reason = 0; 9353a9fb326S[email protected] hci_stack->new_scan_enable_value = 0xff; 936b031bebbSmatthias.ringwald 93716833f0aSmatthias.ringwald // higher level handler 9383a9fb326S[email protected] hci_stack->packet_handler = dummy_handler; 93916833f0aSmatthias.ringwald 940404843c1Smatthias.ringwald // store and open remote device db 9413a9fb326S[email protected] hci_stack->remote_device_db = remote_device_db; 9423a9fb326S[email protected] if (hci_stack->remote_device_db) { 9433a9fb326S[email protected] hci_stack->remote_device_db->open(); 944404843c1Smatthias.ringwald } 94529d53098Smatthias.ringwald 9468fcba05dSmatthias.ringwald // max acl payload size defined in config.h 9473a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 9488fcba05dSmatthias.ringwald 94916833f0aSmatthias.ringwald // register packet handlers with transport 95010e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 951f5454fc6Smatthias.ringwald 9523a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 953e2386ba1S[email protected] 954e2386ba1S[email protected] // class of device 9553a9fb326S[email protected] hci_stack->class_of_device = 0x007a020c; // Smartphone 956a45d6b9fS[email protected] 95763048403S[email protected] // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept 9583a9fb326S[email protected] hci_stack->ssp_enable = 1; 9593a9fb326S[email protected] hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT; 9603a9fb326S[email protected] hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING; 9613a9fb326S[email protected] hci_stack->ssp_auto_accept = 1; 96269a97523S[email protected] 96369a97523S[email protected] // LE 9643a9fb326S[email protected] hci_stack->adv_addr_type = 0; 9653a9fb326S[email protected] memset(hci_stack->adv_address, 0, 6); 966475c8125Smatthias.ringwald } 967475c8125Smatthias.ringwald 968404843c1Smatthias.ringwald void hci_close(){ 969404843c1Smatthias.ringwald // close remote device db 9703a9fb326S[email protected] if (hci_stack->remote_device_db) { 9713a9fb326S[email protected] hci_stack->remote_device_db->close(); 972404843c1Smatthias.ringwald } 9733a9fb326S[email protected] while (hci_stack->connections) { 9743a9fb326S[email protected] hci_shutdown_connection((hci_connection_t *) hci_stack->connections); 975f5454fc6Smatthias.ringwald } 976f5454fc6Smatthias.ringwald hci_power_control(HCI_POWER_OFF); 9773a9fb326S[email protected] 9783a9fb326S[email protected] #ifdef HAVE_MALLOC 9793a9fb326S[email protected] free(hci_stack); 9803a9fb326S[email protected] #endif 9813a9fb326S[email protected] hci_stack = NULL; 982404843c1Smatthias.ringwald } 983404843c1Smatthias.ringwald 9849e61646fS[email protected] void hci_set_class_of_device(uint32_t class_of_device){ 9859e61646fS[email protected] hci_stack->class_of_device = class_of_device; 9869e61646fS[email protected] } 9879e61646fS[email protected] 988*66fb9560S[email protected] void hci_disable_l2cap_timeout_check(){ 989*66fb9560S[email protected] disable_l2cap_timeouts = 1; 990*66fb9560S[email protected] } 9918d213e1aSmatthias.ringwald // State-Module-Driver overview 9928d213e1aSmatthias.ringwald // state module low-level 9938d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 9948d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 9958d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 9968d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 997d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 998d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 999c7e0c5f6Smatthias.ringwald 100040d1c7a4Smatthias.ringwald static int hci_power_control_on(void){ 10017301ad89Smatthias.ringwald 1002038bc64cSmatthias.ringwald // power on 1003f9a30166Smatthias.ringwald int err = 0; 10043a9fb326S[email protected] if (hci_stack->control && hci_stack->control->on){ 10053a9fb326S[email protected] err = (*hci_stack->control->on)(hci_stack->config); 1006f9a30166Smatthias.ringwald } 1007038bc64cSmatthias.ringwald if (err){ 10087d67539fSmatthias.ringwald log_error( "POWER_ON failed\n"); 1009038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 1010038bc64cSmatthias.ringwald return err; 1011038bc64cSmatthias.ringwald } 1012038bc64cSmatthias.ringwald 1013038bc64cSmatthias.ringwald // open low-level device 10143a9fb326S[email protected] err = hci_stack->hci_transport->open(hci_stack->config); 1015038bc64cSmatthias.ringwald if (err){ 10167d67539fSmatthias.ringwald log_error( "HCI_INIT failed, turning Bluetooth off again\n"); 10173a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 10183a9fb326S[email protected] (*hci_stack->control->off)(hci_stack->config); 1019f9a30166Smatthias.ringwald } 1020038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 1021038bc64cSmatthias.ringwald return err; 1022038bc64cSmatthias.ringwald } 10238d213e1aSmatthias.ringwald return 0; 10248d213e1aSmatthias.ringwald } 1025038bc64cSmatthias.ringwald 102640d1c7a4Smatthias.ringwald static void hci_power_control_off(void){ 10278d213e1aSmatthias.ringwald 10287b5fbe1fSmatthias.ringwald log_info("hci_power_control_off\n"); 10299418f9c9Smatthias.ringwald 10308d213e1aSmatthias.ringwald // close low-level device 10313a9fb326S[email protected] hci_stack->hci_transport->close(hci_stack->config); 10328d213e1aSmatthias.ringwald 10337b5fbe1fSmatthias.ringwald log_info("hci_power_control_off - hci_transport closed\n"); 10349418f9c9Smatthias.ringwald 10358d213e1aSmatthias.ringwald // power off 10363a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 10373a9fb326S[email protected] (*hci_stack->control->off)(hci_stack->config); 10388d213e1aSmatthias.ringwald } 10399418f9c9Smatthias.ringwald 10407b5fbe1fSmatthias.ringwald log_info("hci_power_control_off - control closed\n"); 10419418f9c9Smatthias.ringwald 10423a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 104372ea5239Smatthias.ringwald } 104472ea5239Smatthias.ringwald 104540d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){ 104672ea5239Smatthias.ringwald 10477b5fbe1fSmatthias.ringwald log_info("hci_power_control_sleep\n"); 10483144bce4Smatthias.ringwald 1049b429b9b7Smatthias.ringwald #if 0 1050b429b9b7Smatthias.ringwald // don't close serial port during sleep 1051b429b9b7Smatthias.ringwald 105272ea5239Smatthias.ringwald // close low-level device 10533a9fb326S[email protected] hci_stack->hci_transport->close(hci_stack->config); 1054b429b9b7Smatthias.ringwald #endif 105572ea5239Smatthias.ringwald 105672ea5239Smatthias.ringwald // sleep mode 10573a9fb326S[email protected] if (hci_stack->control && hci_stack->control->sleep){ 10583a9fb326S[email protected] (*hci_stack->control->sleep)(hci_stack->config); 105972ea5239Smatthias.ringwald } 1060b429b9b7Smatthias.ringwald 10613a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 10628d213e1aSmatthias.ringwald } 10638d213e1aSmatthias.ringwald 106440d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){ 1065b429b9b7Smatthias.ringwald 10667b5fbe1fSmatthias.ringwald log_info("hci_power_control_wake\n"); 1067b429b9b7Smatthias.ringwald 1068b429b9b7Smatthias.ringwald // wake on 10693a9fb326S[email protected] if (hci_stack->control && hci_stack->control->wake){ 10703a9fb326S[email protected] (*hci_stack->control->wake)(hci_stack->config); 1071b429b9b7Smatthias.ringwald } 1072b429b9b7Smatthias.ringwald 1073b429b9b7Smatthias.ringwald #if 0 1074b429b9b7Smatthias.ringwald // open low-level device 10753a9fb326S[email protected] int err = hci_stack->hci_transport->open(hci_stack->config); 1076b429b9b7Smatthias.ringwald if (err){ 10777d67539fSmatthias.ringwald log_error( "HCI_INIT failed, turning Bluetooth off again\n"); 10783a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 10793a9fb326S[email protected] (*hci_stack->control->off)(hci_stack->config); 1080b429b9b7Smatthias.ringwald } 1081b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 1082b429b9b7Smatthias.ringwald return err; 1083b429b9b7Smatthias.ringwald } 1084b429b9b7Smatthias.ringwald #endif 1085b429b9b7Smatthias.ringwald 1086b429b9b7Smatthias.ringwald return 0; 1087b429b9b7Smatthias.ringwald } 1088b429b9b7Smatthias.ringwald 1089b429b9b7Smatthias.ringwald 10908d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 1091d661ed19Smatthias.ringwald 10923a9fb326S[email protected] log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack->state); 1093d661ed19Smatthias.ringwald 10948d213e1aSmatthias.ringwald int err = 0; 10953a9fb326S[email protected] switch (hci_stack->state){ 10968d213e1aSmatthias.ringwald 10978d213e1aSmatthias.ringwald case HCI_STATE_OFF: 10988d213e1aSmatthias.ringwald switch (power_mode){ 10998d213e1aSmatthias.ringwald case HCI_POWER_ON: 11008d213e1aSmatthias.ringwald err = hci_power_control_on(); 11018d213e1aSmatthias.ringwald if (err) return err; 11027301ad89Smatthias.ringwald // set up state machine 11033a9fb326S[email protected] hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent 11043a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 11053a9fb326S[email protected] hci_stack->substate = 0; 11068d213e1aSmatthias.ringwald break; 11078d213e1aSmatthias.ringwald case HCI_POWER_OFF: 11088d213e1aSmatthias.ringwald // do nothing 11098d213e1aSmatthias.ringwald break; 11108d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1111b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 11128d213e1aSmatthias.ringwald break; 11138d213e1aSmatthias.ringwald } 11148d213e1aSmatthias.ringwald break; 11157301ad89Smatthias.ringwald 11168d213e1aSmatthias.ringwald case HCI_STATE_INITIALIZING: 11178d213e1aSmatthias.ringwald switch (power_mode){ 11188d213e1aSmatthias.ringwald case HCI_POWER_ON: 11198d213e1aSmatthias.ringwald // do nothing 11208d213e1aSmatthias.ringwald break; 11218d213e1aSmatthias.ringwald case HCI_POWER_OFF: 11228d213e1aSmatthias.ringwald // no connections yet, just turn it off 11238d213e1aSmatthias.ringwald hci_power_control_off(); 11248d213e1aSmatthias.ringwald break; 11258d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1126b546ac54Smatthias.ringwald // no connections yet, just turn it off 112772ea5239Smatthias.ringwald hci_power_control_sleep(); 11288d213e1aSmatthias.ringwald break; 11298d213e1aSmatthias.ringwald } 11308d213e1aSmatthias.ringwald break; 11317301ad89Smatthias.ringwald 11328d213e1aSmatthias.ringwald case HCI_STATE_WORKING: 11338d213e1aSmatthias.ringwald switch (power_mode){ 11348d213e1aSmatthias.ringwald case HCI_POWER_ON: 11358d213e1aSmatthias.ringwald // do nothing 11368d213e1aSmatthias.ringwald break; 11378d213e1aSmatthias.ringwald case HCI_POWER_OFF: 1138c7e0c5f6Smatthias.ringwald // see hci_run 11393a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 11408d213e1aSmatthias.ringwald break; 11418d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1142b546ac54Smatthias.ringwald // see hci_run 11433a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 11443a9fb326S[email protected] hci_stack->substate = 0; 11458d213e1aSmatthias.ringwald break; 11468d213e1aSmatthias.ringwald } 11478d213e1aSmatthias.ringwald break; 11487301ad89Smatthias.ringwald 11498d213e1aSmatthias.ringwald case HCI_STATE_HALTING: 11508d213e1aSmatthias.ringwald switch (power_mode){ 11518d213e1aSmatthias.ringwald case HCI_POWER_ON: 11528d213e1aSmatthias.ringwald // set up state machine 11533a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 11543a9fb326S[email protected] hci_stack->substate = 0; 11558d213e1aSmatthias.ringwald break; 11568d213e1aSmatthias.ringwald case HCI_POWER_OFF: 11578d213e1aSmatthias.ringwald // do nothing 11588d213e1aSmatthias.ringwald break; 11598d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1160b546ac54Smatthias.ringwald // see hci_run 11613a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 11623a9fb326S[email protected] hci_stack->substate = 0; 11638d213e1aSmatthias.ringwald break; 11648d213e1aSmatthias.ringwald } 11658d213e1aSmatthias.ringwald break; 11668d213e1aSmatthias.ringwald 11678d213e1aSmatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 11688d213e1aSmatthias.ringwald switch (power_mode){ 11698d213e1aSmatthias.ringwald case HCI_POWER_ON: 117028171530Smatthias.ringwald 117128171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 117228171530Smatthias.ringwald // nothing to do, if H4 supports power management 117328171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 11743a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 11753a9fb326S[email protected] hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP; 117628171530Smatthias.ringwald break; 117728171530Smatthias.ringwald } 117828171530Smatthias.ringwald #endif 1179b546ac54Smatthias.ringwald // set up state machine 11803a9fb326S[email protected] hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent 11813a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 11823a9fb326S[email protected] hci_stack->substate = 0; 11838d213e1aSmatthias.ringwald break; 11848d213e1aSmatthias.ringwald case HCI_POWER_OFF: 1185b546ac54Smatthias.ringwald // see hci_run 11863a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 11878d213e1aSmatthias.ringwald break; 11888d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1189b546ac54Smatthias.ringwald // do nothing 11908d213e1aSmatthias.ringwald break; 11918d213e1aSmatthias.ringwald } 11928d213e1aSmatthias.ringwald break; 11938d213e1aSmatthias.ringwald 11948d213e1aSmatthias.ringwald case HCI_STATE_SLEEPING: 11958d213e1aSmatthias.ringwald switch (power_mode){ 11968d213e1aSmatthias.ringwald case HCI_POWER_ON: 119728171530Smatthias.ringwald 119828171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 119928171530Smatthias.ringwald // nothing to do, if H4 supports power management 120028171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 12013a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 12023a9fb326S[email protected] hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP; 1203758b46ceSmatthias.ringwald hci_update_scan_enable(); 120428171530Smatthias.ringwald break; 120528171530Smatthias.ringwald } 120628171530Smatthias.ringwald #endif 12073144bce4Smatthias.ringwald err = hci_power_control_wake(); 12083144bce4Smatthias.ringwald if (err) return err; 1209b546ac54Smatthias.ringwald // set up state machine 12103a9fb326S[email protected] hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent 12113a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 12123a9fb326S[email protected] hci_stack->substate = 0; 12138d213e1aSmatthias.ringwald break; 12148d213e1aSmatthias.ringwald case HCI_POWER_OFF: 12153a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 12168d213e1aSmatthias.ringwald break; 12178d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1218b546ac54Smatthias.ringwald // do nothing 12198d213e1aSmatthias.ringwald break; 12208d213e1aSmatthias.ringwald } 12218d213e1aSmatthias.ringwald break; 122211e23e5fSmatthias.ringwald } 122368d92d03Smatthias.ringwald 1224038bc64cSmatthias.ringwald // create internal event 1225ee8bf225Smatthias.ringwald hci_emit_state(); 1226ee8bf225Smatthias.ringwald 122768d92d03Smatthias.ringwald // trigger next/first action 122868d92d03Smatthias.ringwald hci_run(); 122968d92d03Smatthias.ringwald 1230475c8125Smatthias.ringwald return 0; 1231475c8125Smatthias.ringwald } 1232475c8125Smatthias.ringwald 1233758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){ 1234758b46ceSmatthias.ringwald // 2 = page scan, 1 = inq scan 12353a9fb326S[email protected] hci_stack->new_scan_enable_value = hci_stack->connectable << 1 | hci_stack->discoverable; 1236758b46ceSmatthias.ringwald hci_run(); 1237758b46ceSmatthias.ringwald } 1238758b46ceSmatthias.ringwald 1239381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){ 1240381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 1241381fbed8Smatthias.ringwald 12423a9fb326S[email protected] if (hci_stack->discoverable == enable){ 12433a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 1244381fbed8Smatthias.ringwald return; 1245381fbed8Smatthias.ringwald } 1246381fbed8Smatthias.ringwald 12473a9fb326S[email protected] hci_stack->discoverable = enable; 1248758b46ceSmatthias.ringwald hci_update_scan_enable(); 1249758b46ceSmatthias.ringwald } 1250b031bebbSmatthias.ringwald 1251758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){ 1252758b46ceSmatthias.ringwald if (enable) enable = 1; // normalize argument 1253758b46ceSmatthias.ringwald 1254758b46ceSmatthias.ringwald // don't emit event 12553a9fb326S[email protected] if (hci_stack->connectable == enable) return; 1256758b46ceSmatthias.ringwald 12573a9fb326S[email protected] hci_stack->connectable = enable; 1258758b46ceSmatthias.ringwald hci_update_scan_enable(); 1259381fbed8Smatthias.ringwald } 1260381fbed8Smatthias.ringwald 12615061f3afS[email protected] bd_addr_t * hci_local_bd_addr(void){ 12623a9fb326S[email protected] return &hci_stack->local_bd_addr; 12635061f3afS[email protected] } 12645061f3afS[email protected] 126506b35ec0Smatthias.ringwald void hci_run(){ 12668a485f27Smatthias.ringwald 126732ab9390Smatthias.ringwald hci_connection_t * connection; 126832ab9390Smatthias.ringwald linked_item_t * it; 126932ab9390Smatthias.ringwald 1270ce4c8fabSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 1271ce4c8fabSmatthias.ringwald 1272b031bebbSmatthias.ringwald // global/non-connection oriented commands 1273b031bebbSmatthias.ringwald 1274b031bebbSmatthias.ringwald // decline incoming connections 12753a9fb326S[email protected] if (hci_stack->decline_reason){ 12763a9fb326S[email protected] uint8_t reason = hci_stack->decline_reason; 12773a9fb326S[email protected] hci_stack->decline_reason = 0; 12783a9fb326S[email protected] hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason); 1279dbe1a790S[email protected] return; 1280ce4c8fabSmatthias.ringwald } 1281ce4c8fabSmatthias.ringwald 1282b031bebbSmatthias.ringwald // send scan enable 12833a9fb326S[email protected] if (hci_stack->state == HCI_STATE_WORKING && hci_stack->new_scan_enable_value != 0xff && hci_classic_supported()){ 12843a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value); 12853a9fb326S[email protected] hci_stack->new_scan_enable_value = 0xff; 1286dbe1a790S[email protected] return; 1287b031bebbSmatthias.ringwald } 1288b031bebbSmatthias.ringwald 128932ab9390Smatthias.ringwald // send pending HCI commands 12903a9fb326S[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){ 129132ab9390Smatthias.ringwald 1292b031bebbSmatthias.ringwald connection = (hci_connection_t *) it; 129332ab9390Smatthias.ringwald 1294ad83dc6aS[email protected] if (connection->state == SEND_CREATE_CONNECTION){ 1295ad83dc6aS[email protected] log_info("sending hci_create_connection\n"); 1296ad83dc6aS[email protected] hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 1297ad83dc6aS[email protected] return; 1298ad83dc6aS[email protected] } 1299ad83dc6aS[email protected] 130032ab9390Smatthias.ringwald if (connection->state == RECEIVED_CONNECTION_REQUEST){ 13017b5fbe1fSmatthias.ringwald log_info("sending hci_accept_connection_request\n"); 130232ab9390Smatthias.ringwald connection->state = ACCEPTED_CONNECTION_REQUEST; 130334d2123cS[email protected] hci_send_cmd(&hci_accept_connection_request, connection->address, 1); 1304dbe1a790S[email protected] return; 1305c7e0c5f6Smatthias.ringwald } 1306c7e0c5f6Smatthias.ringwald 130732ab9390Smatthias.ringwald if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){ 130834d2123cS[email protected] log_info("responding to link key request\n"); 130934d2123cS[email protected] connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST); 131032ab9390Smatthias.ringwald link_key_t link_key; 1311c77e8838S[email protected] link_key_type_t link_key_type; 13123a9fb326S[email protected] if ( hci_stack->remote_device_db 13133a9fb326S[email protected] && hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type) 131434d2123cS[email protected] && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){ 13159ab95c90S[email protected] connection->link_key_type = link_key_type; 131632ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key); 131732ab9390Smatthias.ringwald } else { 131832ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 131932ab9390Smatthias.ringwald } 1320dbe1a790S[email protected] return; 132132ab9390Smatthias.ringwald } 13221d6b20aeS[email protected] 1323899283eaS[email protected] if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){ 13244c57c146S[email protected] log_info("denying to pin request\n"); 1325899283eaS[email protected] connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST); 132634d2123cS[email protected] hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address); 13274c57c146S[email protected] return; 13284c57c146S[email protected] } 13294c57c146S[email protected] 1330dbe1a790S[email protected] if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){ 133134d2123cS[email protected] connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY); 13323a9fb326S[email protected] if (hci_stack->bondable && hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN){ 1333106d6d11S[email protected] // tweak authentication requirements 13343a9fb326S[email protected] uint8_t authreq = hci_stack->ssp_authentication_requirement; 1335106d6d11S[email protected] if (connection->bonding_flags & BONDING_DEDICATED){ 13369faad3abS[email protected] authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 13379faad3abS[email protected] } 13389faad3abS[email protected] if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){ 13399faad3abS[email protected] authreq |= 1; 1340106d6d11S[email protected] } 13413a9fb326S[email protected] hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, NULL, authreq); 1342f8fb5f6eS[email protected] } else { 1343f8fb5f6eS[email protected] hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED); 1344f8fb5f6eS[email protected] } 1345dbe1a790S[email protected] return; 134632ab9390Smatthias.ringwald } 134732ab9390Smatthias.ringwald 1348dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){ 1349dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY); 135034d2123cS[email protected] hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address); 1351dbe1a790S[email protected] return; 1352dbe1a790S[email protected] } 1353dbe1a790S[email protected] 1354dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){ 1355dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY); 135634d2123cS[email protected] hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000); 1357dbe1a790S[email protected] return; 1358dbe1a790S[email protected] } 1359afd4e962S[email protected] 1360afd4e962S[email protected] if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){ 1361afd4e962S[email protected] connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES; 136234d2123cS[email protected] hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle); 13632bd8b7e7S[email protected] return; 13642bd8b7e7S[email protected] } 13652bd8b7e7S[email protected] 13662bd8b7e7S[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){ 13672bd8b7e7S[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK; 136834d2123cS[email protected] hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005); // authentication failure 136934d2123cS[email protected] return; 137034d2123cS[email protected] } 1371ad83dc6aS[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){ 1372ad83dc6aS[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE; 1373ad83dc6aS[email protected] hci_send_cmd(&hci_disconnect, connection->con_handle, 0); // authentication done 1374ad83dc6aS[email protected] return; 1375ad83dc6aS[email protected] } 137634d2123cS[email protected] if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){ 137734d2123cS[email protected] connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST; 137834d2123cS[email protected] hci_send_cmd(&hci_authentication_requested, connection->con_handle); 13792bd8b7e7S[email protected] return; 1380afd4e962S[email protected] } 1381dce78009S[email protected] if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){ 1382dce78009S[email protected] connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST; 1383dce78009S[email protected] hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1); 1384dce78009S[email protected] return; 1385dce78009S[email protected] } 1386dbe1a790S[email protected] } 1387c7e0c5f6Smatthias.ringwald 13883a9fb326S[email protected] switch (hci_stack->state){ 13893429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 13903a9fb326S[email protected] // log_info("hci_init: substate %u\n", hci_stack->substate); 13913a9fb326S[email protected] if (hci_stack->substate % 2) { 13923429f56bSmatthias.ringwald // odd: waiting for command completion 139306b35ec0Smatthias.ringwald return; 13943429f56bSmatthias.ringwald } 13953a9fb326S[email protected] switch (hci_stack->substate >> 1){ 1396c7492964Smatthias.ringwald case 0: // RESET 139722909952Smatthias.ringwald hci_send_cmd(&hci_reset); 139824052c2aS[email protected] 13993a9fb326S[email protected] if (hci_stack->config == 0 || ((hci_uart_config_t *)hci_stack->config)->baudrate_main == 0){ 1400c7492964Smatthias.ringwald // skip baud change 14013a9fb326S[email protected] hci_stack->substate = 4; // >> 1 = 2 1402c7492964Smatthias.ringwald } 14033429f56bSmatthias.ringwald break; 1404c7492964Smatthias.ringwald case 1: // SEND BAUD CHANGE 14053a9fb326S[email protected] hci_stack->control->baudrate_cmd(hci_stack->config, ((hci_uart_config_t *)hci_stack->config)->baudrate_main, hci_stack->hci_packet_buffer); 14063a9fb326S[email protected] hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]); 1407c7492964Smatthias.ringwald break; 1408c7492964Smatthias.ringwald case 2: // LOCAL BAUD CHANGE 14093a9fb326S[email protected] hci_stack->hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack->config)->baudrate_main); 14103a9fb326S[email protected] hci_stack->substate += 2; 1411c7492964Smatthias.ringwald // break missing here for fall through 1412c7492964Smatthias.ringwald 1413c7492964Smatthias.ringwald case 3: 141424052c2aS[email protected] // Custom initialization 14153a9fb326S[email protected] if (hci_stack->control && hci_stack->control->next_cmd){ 14163a9fb326S[email protected] int valid_cmd = (*hci_stack->control->next_cmd)(hci_stack->config, hci_stack->hci_packet_buffer); 1417d62aca9fSmatthias.ringwald if (valid_cmd){ 14183a9fb326S[email protected] int size = 3 + hci_stack->hci_packet_buffer[2]; 14193a9fb326S[email protected] hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size); 14203a9fb326S[email protected] hci_stack->substate = 4; // more init commands 142190919203Smatthias.ringwald break; 142290919203Smatthias.ringwald } 1423d62aca9fSmatthias.ringwald log_info("hci_run: init script done\n\r"); 142490919203Smatthias.ringwald } 14252f6c30e1Smatthias.ringwald // otherwise continue 1426f432a6ddSmatthias.ringwald hci_send_cmd(&hci_read_bd_addr); 1427f432a6ddSmatthias.ringwald break; 1428c7492964Smatthias.ringwald case 4: 1429e2edc0c3Smatthias.ringwald hci_send_cmd(&hci_read_buffer_size); 1430e2edc0c3Smatthias.ringwald break; 1431c7492964Smatthias.ringwald case 5: 143265389bfcS[email protected] hci_send_cmd(&hci_read_local_supported_features); 14333429f56bSmatthias.ringwald break; 1434c7492964Smatthias.ringwald case 6: 1435d7e0e3a8S[email protected] if (hci_le_supported()){ 1436d7e0e3a8S[email protected] hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF); 1437d7e0e3a8S[email protected] } else { 1438d7e0e3a8S[email protected] // Kensington Bluetoot 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff... 1439d7e0e3a8S[email protected] hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF); 1440d7e0e3a8S[email protected] } 1441f5d8d141S[email protected] 1442f5d8d141S[email protected] // skip Classic init commands for LE only chipsets 144324052c2aS[email protected] if (!hci_classic_supported()){ 144424052c2aS[email protected] if (hci_le_supported()){ 14453a9fb326S[email protected] hci_stack->substate = 11 << 1; // skip all classic command 144624052c2aS[email protected] } else { 144724052c2aS[email protected] log_error("Neither BR/EDR nor LE supported"); 14483a9fb326S[email protected] hci_stack->substate = 13 << 1; // skip all 144924052c2aS[email protected] } 1450f5d8d141S[email protected] } 1451f5d8d141S[email protected] break; 1452f5d8d141S[email protected] case 7: 145324052c2aS[email protected] if (hci_ssp_supported()){ 14543a9fb326S[email protected] hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable); 1455f5d8d141S[email protected] break; 145624052c2aS[email protected] } 14573a9fb326S[email protected] hci_stack->substate += 2; 145824052c2aS[email protected] // break missing here for fall through 145924052c2aS[email protected] 1460f5d8d141S[email protected] case 8: 146165389bfcS[email protected] // ca. 15 sec 146265389bfcS[email protected] hci_send_cmd(&hci_write_page_timeout, 0x6000); 1463559e517eS[email protected] break; 1464f5d8d141S[email protected] case 9: 14653a9fb326S[email protected] hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device); 1466e2386ba1S[email protected] break; 1467f5d8d141S[email protected] case 10: 14683a9fb326S[email protected] if (hci_stack->local_name){ 14693a9fb326S[email protected] hci_send_cmd(&hci_write_local_name, hci_stack->local_name); 1470e2386ba1S[email protected] } else { 14718a485f27Smatthias.ringwald char hostname[30]; 1472e2386ba1S[email protected] #ifdef EMBEDDED 1473e2386ba1S[email protected] // BTstack-11:22:33:44:55:66 1474e2386ba1S[email protected] strcpy(hostname, "BTstack "); 14753a9fb326S[email protected] strcat(hostname, bd_addr_to_str(hci_stack->local_bd_addr)); 1476e2386ba1S[email protected] printf("---> Name %s\n", hostname); 1477e2386ba1S[email protected] #else 1478e2386ba1S[email protected] // hostname for POSIX systems 14798a485f27Smatthias.ringwald gethostname(hostname, 30); 14808a485f27Smatthias.ringwald hostname[29] = '\0'; 1481e2386ba1S[email protected] #endif 14828a485f27Smatthias.ringwald hci_send_cmd(&hci_write_local_name, hostname); 14838a485f27Smatthias.ringwald } 14843c4d4b90Smatthias.ringwald break; 1485e0dbca83S[email protected] case 11: 14863a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan 148724052c2aS[email protected] if (!hci_le_supported()){ 148824052c2aS[email protected] // SKIP LE init for Classic only configuration 14893a9fb326S[email protected] hci_stack->substate = 13 << 1; 149024052c2aS[email protected] } 1491a45d6b9fS[email protected] break; 149224052c2aS[email protected] 149343aa7007S[email protected] #ifdef HAVE_BLE 149424052c2aS[email protected] // LE INIT 1495a45d6b9fS[email protected] case 12: 149624052c2aS[email protected] hci_send_cmd(&hci_le_read_buffer_size); 149724052c2aS[email protected] break; 149824052c2aS[email protected] case 13: 149924052c2aS[email protected] // LE Supported Host = 1, Simultaneous Host = 0 150024052c2aS[email protected] hci_send_cmd(&hci_write_le_host_supported, 1, 0); 150124052c2aS[email protected] break; 150243aa7007S[email protected] #endif 150324052c2aS[email protected] 150424052c2aS[email protected] // DONE 150524052c2aS[email protected] case 14: 15063429f56bSmatthias.ringwald // done. 15073a9fb326S[email protected] hci_stack->state = HCI_STATE_WORKING; 1508b360b6adSmatthias.ringwald hci_emit_state(); 15093429f56bSmatthias.ringwald break; 15103429f56bSmatthias.ringwald default: 15113429f56bSmatthias.ringwald break; 1512475c8125Smatthias.ringwald } 15133a9fb326S[email protected] hci_stack->substate++; 15143429f56bSmatthias.ringwald break; 1515c7e0c5f6Smatthias.ringwald 1516c7e0c5f6Smatthias.ringwald case HCI_STATE_HALTING: 1517c7e0c5f6Smatthias.ringwald 15187b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING\n"); 1519c7e0c5f6Smatthias.ringwald // close all open connections 15203a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 1521c7e0c5f6Smatthias.ringwald if (connection){ 152232ab9390Smatthias.ringwald 1523c7e0c5f6Smatthias.ringwald // send disconnect 152432ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 152532ab9390Smatthias.ringwald 152679bbfa0bSmatthias.ringwald log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle); 15276ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 1528c7e0c5f6Smatthias.ringwald 1529c7e0c5f6Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 1530c7e0c5f6Smatthias.ringwald hci_shutdown_connection(connection); 1531c7e0c5f6Smatthias.ringwald return; 1532c7e0c5f6Smatthias.ringwald } 15337b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, calling off\n"); 1534c7e0c5f6Smatthias.ringwald 153572ea5239Smatthias.ringwald // switch mode 1536c7e0c5f6Smatthias.ringwald hci_power_control_off(); 15379418f9c9Smatthias.ringwald 15387b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, emitting state\n"); 153972ea5239Smatthias.ringwald hci_emit_state(); 15407b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, done\n"); 154172ea5239Smatthias.ringwald break; 1542c7e0c5f6Smatthias.ringwald 154372ea5239Smatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 15443a9fb326S[email protected] switch(hci_stack->substate) { 154589db417bSmatthias.ringwald case 0: 15467b5fbe1fSmatthias.ringwald log_info("HCI_STATE_FALLING_ASLEEP\n"); 154772ea5239Smatthias.ringwald // close all open connections 15483a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 154966da7044Smatthias.ringwald 155028171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 155166da7044Smatthias.ringwald // don't close connections, if H4 supports power management 155266da7044Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 155366da7044Smatthias.ringwald connection = NULL; 155466da7044Smatthias.ringwald } 155566da7044Smatthias.ringwald #endif 155672ea5239Smatthias.ringwald if (connection){ 155732ab9390Smatthias.ringwald 155872ea5239Smatthias.ringwald // send disconnect 155932ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 156032ab9390Smatthias.ringwald 156179bbfa0bSmatthias.ringwald log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle); 15626ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 156372ea5239Smatthias.ringwald 156472ea5239Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 156572ea5239Smatthias.ringwald hci_shutdown_connection(connection); 156672ea5239Smatthias.ringwald return; 156772ea5239Smatthias.ringwald } 156872ea5239Smatthias.ringwald 156992368cd3S[email protected] if (hci_classic_supported()){ 157089db417bSmatthias.ringwald // disable page and inquiry scan 157132ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 157232ab9390Smatthias.ringwald 157392368cd3S[email protected] log_info("HCI_STATE_HALTING, disabling inq scans\n"); 15743a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan 157589db417bSmatthias.ringwald 157689db417bSmatthias.ringwald // continue in next sub state 15773a9fb326S[email protected] hci_stack->substate++; 157889db417bSmatthias.ringwald break; 157992368cd3S[email protected] } 158092368cd3S[email protected] // fall through for ble-only chips 158192368cd3S[email protected] 158289db417bSmatthias.ringwald case 2: 15837b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, calling sleep\n"); 158428171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 158528171530Smatthias.ringwald // don't actually go to sleep, if H4 supports power management 158628171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 158728171530Smatthias.ringwald // SLEEP MODE reached 15883a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 158928171530Smatthias.ringwald hci_emit_state(); 159028171530Smatthias.ringwald break; 159128171530Smatthias.ringwald } 159228171530Smatthias.ringwald #endif 159372ea5239Smatthias.ringwald // switch mode 15943a9fb326S[email protected] hci_power_control_sleep(); // changes hci_stack->state to SLEEP 1595c7e0c5f6Smatthias.ringwald hci_emit_state(); 159628171530Smatthias.ringwald break; 159728171530Smatthias.ringwald 159889db417bSmatthias.ringwald default: 159989db417bSmatthias.ringwald break; 160089db417bSmatthias.ringwald } 1601c7e0c5f6Smatthias.ringwald break; 1602c7e0c5f6Smatthias.ringwald 16033429f56bSmatthias.ringwald default: 16043429f56bSmatthias.ringwald break; 16051f504dbdSmatthias.ringwald } 16063429f56bSmatthias.ringwald } 160716833f0aSmatthias.ringwald 160831452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 1609c8e4258aSmatthias.ringwald bd_addr_t addr; 1610c8e4258aSmatthias.ringwald hci_connection_t * conn; 1611c8e4258aSmatthias.ringwald // house-keeping 1612c8e4258aSmatthias.ringwald 1613c8e4258aSmatthias.ringwald // create_connection? 1614c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 1615c8e4258aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 1616339b6768Smatthias.ringwald log_info("Create_connection to %s\n", bd_addr_to_str(addr)); 1617c8e4258aSmatthias.ringwald 1618ad83dc6aS[email protected] conn = connection_for_address(addr); 1619ad83dc6aS[email protected] if (!conn){ 162017f1ba2aSmatthias.ringwald conn = create_connection_for_addr(addr); 162117f1ba2aSmatthias.ringwald if (!conn){ 162217f1ba2aSmatthias.ringwald // notify client that alloc failed 162317f1ba2aSmatthias.ringwald hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED); 162417f1ba2aSmatthias.ringwald return 0; // don't sent packet to controller 162517f1ba2aSmatthias.ringwald } 1626ad83dc6aS[email protected] conn->state = SEND_CREATE_CONNECTION; 1627ad83dc6aS[email protected] } 1628ad83dc6aS[email protected] log_info("conn state %u", conn->state); 1629ad83dc6aS[email protected] switch (conn->state){ 1630ad83dc6aS[email protected] // if connection active exists 1631ad83dc6aS[email protected] case OPEN: 163262bda3fbS[email protected] // and OPEN, emit connection complete command, don't send to controller 1633ad83dc6aS[email protected] hci_emit_connection_complete(conn, 0); 163462bda3fbS[email protected] return 0; 1635ad83dc6aS[email protected] case SEND_CREATE_CONNECTION: 1636ad83dc6aS[email protected] // connection created by hci, e.g. dedicated bonding 1637ad83dc6aS[email protected] break; 1638ad83dc6aS[email protected] default: 1639ad83dc6aS[email protected] // otherwise, just ignore as it is already in the open process 1640ad83dc6aS[email protected] return 0; 1641ad83dc6aS[email protected] } 1642c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 1643c8e4258aSmatthias.ringwald } 1644c8e4258aSmatthias.ringwald 16457fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_reply)){ 16467fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY); 16477fde4af9Smatthias.ringwald } 16487fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){ 16497fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST); 16507fde4af9Smatthias.ringwald } 16517fde4af9Smatthias.ringwald 16528ef73945Smatthias.ringwald if (IS_COMMAND(packet, hci_delete_stored_link_key)){ 16533a9fb326S[email protected] if (hci_stack->remote_device_db){ 16548ef73945Smatthias.ringwald bt_flip_addr(addr, &packet[3]); 16553a9fb326S[email protected] hci_stack->remote_device_db->delete_link_key(&addr); 16568ef73945Smatthias.ringwald } 16578ef73945Smatthias.ringwald } 1658c8e4258aSmatthias.ringwald 16596724cd9eS[email protected] if (IS_COMMAND(packet, hci_pin_code_request_negative_reply) 16606724cd9eS[email protected] || IS_COMMAND(packet, hci_pin_code_request_reply)){ 16616724cd9eS[email protected] bt_flip_addr(addr, &packet[3]); 16626724cd9eS[email protected] conn = connection_for_address(addr); 16636724cd9eS[email protected] if (conn){ 16646724cd9eS[email protected] connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE); 16656724cd9eS[email protected] } 16666724cd9eS[email protected] } 16676724cd9eS[email protected] 16686724cd9eS[email protected] if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply) 16696724cd9eS[email protected] || IS_COMMAND(packet, hci_user_confirmation_request_reply) 16706724cd9eS[email protected] || IS_COMMAND(packet, hci_user_passkey_request_negative_reply) 16716724cd9eS[email protected] || IS_COMMAND(packet, hci_user_passkey_request_reply)) { 16726724cd9eS[email protected] bt_flip_addr(addr, &packet[3]); 16736724cd9eS[email protected] conn = connection_for_address(addr); 16746724cd9eS[email protected] if (conn){ 16756724cd9eS[email protected] connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE); 16766724cd9eS[email protected] } 16776724cd9eS[email protected] } 16786724cd9eS[email protected] 167969a97523S[email protected] #ifdef HAVE_BLE 168069a97523S[email protected] if (IS_COMMAND(packet, hci_le_set_advertising_parameters)){ 16813a9fb326S[email protected] hci_stack->adv_addr_type = packet[8]; 168269a97523S[email protected] } 168369a97523S[email protected] if (IS_COMMAND(packet, hci_le_set_random_address)){ 16843a9fb326S[email protected] bt_flip_addr(hci_stack->adv_address, &packet[3]); 168569a97523S[email protected] } 168669a97523S[email protected] #endif 168769a97523S[email protected] 168869a97523S[email protected] 16893a9fb326S[email protected] hci_stack->num_cmd_packets--; 16903a9fb326S[email protected] return hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 169131452debSmatthias.ringwald } 16928adf0ddaSmatthias.ringwald 16932bd8b7e7S[email protected] // disconnect because of security block 16942bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){ 16952bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 16962bd8b7e7S[email protected] if (!connection) return; 16972bd8b7e7S[email protected] connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 16982bd8b7e7S[email protected] } 16992bd8b7e7S[email protected] 17002bd8b7e7S[email protected] 1701dbe1a790S[email protected] // Configure Secure Simple Pairing 1702dbe1a790S[email protected] 1703dbe1a790S[email protected] // enable will enable SSP during init 1704dbe1a790S[email protected] void hci_ssp_set_enable(int enable){ 17053a9fb326S[email protected] hci_stack->ssp_enable = enable; 1706dbe1a790S[email protected] } 1707dbe1a790S[email protected] 17082bd8b7e7S[email protected] int hci_local_ssp_activated(){ 17093a9fb326S[email protected] return hci_ssp_supported() && hci_stack->ssp_enable; 17102bd8b7e7S[email protected] } 17112bd8b7e7S[email protected] 1712dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement 1713dbe1a790S[email protected] void hci_ssp_set_io_capability(int io_capability){ 17143a9fb326S[email protected] hci_stack->ssp_io_capability = io_capability; 1715dbe1a790S[email protected] } 1716dbe1a790S[email protected] void hci_ssp_set_authentication_requirement(int authentication_requirement){ 17173a9fb326S[email protected] hci_stack->ssp_authentication_requirement = authentication_requirement; 1718dbe1a790S[email protected] } 1719dbe1a790S[email protected] 1720dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested 1721dbe1a790S[email protected] void hci_ssp_set_auto_accept(int auto_accept){ 17223a9fb326S[email protected] hci_stack->ssp_auto_accept = auto_accept; 1723dbe1a790S[email protected] } 1724dbe1a790S[email protected] 17251cd208adSmatthias.ringwald /** 17261cd208adSmatthias.ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 17271cd208adSmatthias.ringwald */ 1728fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){ 17291cd208adSmatthias.ringwald va_list argptr; 17301cd208adSmatthias.ringwald va_start(argptr, cmd); 17313a9fb326S[email protected] uint16_t size = hci_create_cmd_internal(hci_stack->hci_packet_buffer, cmd, argptr); 17321cd208adSmatthias.ringwald va_end(argptr); 17333a9fb326S[email protected] return hci_send_cmd_packet(hci_stack->hci_packet_buffer, size); 173493b8dc03Smatthias.ringwald } 1735c8e4258aSmatthias.ringwald 1736ee091cf1Smatthias.ringwald // Create various non-HCI events. 1737ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 1738ee091cf1Smatthias.ringwald 1739c8e4258aSmatthias.ringwald void hci_emit_state(){ 17403a9fb326S[email protected] log_info("BTSTACK_EVENT_STATE %u", hci_stack->state); 1741425d1371Smatthias.ringwald uint8_t event[3]; 174280d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 1743425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 17443a9fb326S[email protected] event[2] = hci_stack->state; 1745425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 17463a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1747c8e4258aSmatthias.ringwald } 1748c8e4258aSmatthias.ringwald 174917f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){ 1750425d1371Smatthias.ringwald uint8_t event[13]; 1751c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 1752425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 175317f1ba2aSmatthias.ringwald event[2] = status; 1754c8e4258aSmatthias.ringwald bt_store_16(event, 3, conn->con_handle); 1755c8e4258aSmatthias.ringwald bt_flip_addr(&event[5], conn->address); 1756c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 1757c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 1758425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 17593a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1760c8e4258aSmatthias.ringwald } 1761c8e4258aSmatthias.ringwald 17623c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){ 1763425d1371Smatthias.ringwald uint8_t event[6]; 17643c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 1765e518c4b8Smatthias.ringwald event[1] = sizeof(event) - 2; 17663c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 17673c4d4b90Smatthias.ringwald bt_store_16(event, 3, handle); 17683c4d4b90Smatthias.ringwald event[5] = reason; 1769425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 17703a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 17713c4d4b90Smatthias.ringwald } 17723c4d4b90Smatthias.ringwald 1773ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 1774*66fb9560S[email protected] if (disable_l2cap_timeouts) return; 1775e0abb8e7S[email protected] log_info("L2CAP_EVENT_TIMEOUT_CHECK"); 1776425d1371Smatthias.ringwald uint8_t event[4]; 177780d52d6bSmatthias.ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 1778425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1779ee091cf1Smatthias.ringwald bt_store_16(event, 2, conn->con_handle); 1780425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 17813a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1782ee091cf1Smatthias.ringwald } 178343bfb1bdSmatthias.ringwald 178443bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){ 1785e0abb8e7S[email protected] log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections()); 1786425d1371Smatthias.ringwald uint8_t event[3]; 178780d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 1788425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 178943bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 1790425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 17913a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 179243bfb1bdSmatthias.ringwald } 1793038bc64cSmatthias.ringwald 1794038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){ 1795e0abb8e7S[email protected] log_info("BTSTACK_EVENT_POWERON_FAILED"); 1796425d1371Smatthias.ringwald uint8_t event[2]; 179780d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 1798425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1799425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 18003a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1801038bc64cSmatthias.ringwald } 18021b0e3922Smatthias.ringwald 180309ba8edeSmatthias.ringwald #ifndef EMBEDDED 18041b0e3922Smatthias.ringwald void hci_emit_btstack_version() { 1805e0abb8e7S[email protected] log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR); 1806425d1371Smatthias.ringwald uint8_t event[6]; 18071b0e3922Smatthias.ringwald event[0] = BTSTACK_EVENT_VERSION; 1808425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1809425d1371Smatthias.ringwald event[2] = BTSTACK_MAJOR; 1810425d1371Smatthias.ringwald event[3] = BTSTACK_MINOR; 1811425d1371Smatthias.ringwald bt_store_16(event, 4, BTSTACK_REVISION); 1812425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 18133a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 18141b0e3922Smatthias.ringwald } 181509ba8edeSmatthias.ringwald #endif 18161b0e3922Smatthias.ringwald 18172ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){ 1818e0abb8e7S[email protected] log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled); 1819425d1371Smatthias.ringwald uint8_t event[3]; 18202ed6235cSmatthias.ringwald event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED; 1821425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 18222ed6235cSmatthias.ringwald event[2] = enabled; 1823425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 18243a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 18252ed6235cSmatthias.ringwald } 1826627c2f45Smatthias.ringwald 1827627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){ 1828e0abb8e7S[email protected] uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info 1829627c2f45Smatthias.ringwald event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED; 1830e0abb8e7S[email protected] event[1] = sizeof(event) - 2 - 1; 1831f653b6bdSmatthias.ringwald event[2] = 0; // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 18322f89d309Smatthias.ringwald bt_flip_addr(&event[3], *addr); 1833f653b6bdSmatthias.ringwald memcpy(&event[9], name, 248); 1834e0abb8e7S[email protected] 1835e0abb8e7S[email protected] event[9+248] = 0; // assert \0 for log_info 1836e0abb8e7S[email protected] log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]); 1837e0abb8e7S[email protected] 1838e0abb8e7S[email protected] hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1); 18393a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1); 1840627c2f45Smatthias.ringwald } 1841381fbed8Smatthias.ringwald 1842381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){ 1843e0abb8e7S[email protected] log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled); 1844425d1371Smatthias.ringwald uint8_t event[3]; 1845381fbed8Smatthias.ringwald event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED; 1846425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1847381fbed8Smatthias.ringwald event[2] = enabled; 1848425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 18493a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1850381fbed8Smatthias.ringwald } 1851458bf4e8S[email protected] 1852a00031e2S[email protected] void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){ 1853df3354fcS[email protected] log_info("hci_emit_security_level %u for handle %x", level, con_handle); 1854a00031e2S[email protected] uint8_t event[5]; 1855e00caf9cS[email protected] int pos = 0; 1856a00031e2S[email protected] event[pos++] = GAP_SECURITY_LEVEL; 1857e00caf9cS[email protected] event[pos++] = sizeof(event) - 2; 1858a00031e2S[email protected] bt_store_16(event, 2, con_handle); 1859e00caf9cS[email protected] pos += 2; 1860e00caf9cS[email protected] event[pos++] = level; 1861e00caf9cS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 18623a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1863e00caf9cS[email protected] } 1864e00caf9cS[email protected] 1865ad83dc6aS[email protected] void hci_emit_dedicated_bonding_result(hci_connection_t * connection, uint8_t status){ 1866ad83dc6aS[email protected] log_info("hci_emit_dedicated_bonding_result %u ", status); 1867ad83dc6aS[email protected] uint8_t event[9]; 1868ad83dc6aS[email protected] int pos = 0; 1869ad83dc6aS[email protected] event[pos++] = GAP_DEDICATED_BONDING_COMPLETED; 1870ad83dc6aS[email protected] event[pos++] = sizeof(event) - 2; 1871ad83dc6aS[email protected] event[pos++] = status; 1872ad83dc6aS[email protected] bt_flip_addr( * (bd_addr_t *) &event[pos], connection->address); 1873ad83dc6aS[email protected] pos += 6; 1874ad83dc6aS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 18753a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1876ad83dc6aS[email protected] } 1877ad83dc6aS[email protected] 18782bd8b7e7S[email protected] // query if remote side supports SSP 18792bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){ 18802bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 18812bd8b7e7S[email protected] if (!connection) return 0; 18822bd8b7e7S[email protected] return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0; 18832bd8b7e7S[email protected] } 18842bd8b7e7S[email protected] 1885df3354fcS[email protected] int hci_ssp_supported_on_both_sides(hci_con_handle_t handle){ 1886df3354fcS[email protected] return hci_local_ssp_activated() && hci_remote_ssp_supported(handle); 1887df3354fcS[email protected] } 1888df3354fcS[email protected] 1889458bf4e8S[email protected] // GAP API 1890458bf4e8S[email protected] /** 1891458bf4e8S[email protected] * @bbrief enable/disable bonding. default is enabled 1892458bf4e8S[email protected] * @praram enabled 1893458bf4e8S[email protected] */ 18944c57c146S[email protected] void gap_set_bondable_mode(int enable){ 18953a9fb326S[email protected] hci_stack->bondable = enable ? 1 : 0; 1896458bf4e8S[email protected] } 1897cb230b9dS[email protected] 1898cb230b9dS[email protected] /** 189934d2123cS[email protected] * @brief map link keys to security levels 1900cb230b9dS[email protected] */ 190134d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){ 190234d2123cS[email protected] switch (link_key_type){ 19033c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 19043c68dfa9S[email protected] return LEVEL_4; 19053c68dfa9S[email protected] case COMBINATION_KEY: 19063c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 19073c68dfa9S[email protected] return LEVEL_3; 19083c68dfa9S[email protected] default: 19093c68dfa9S[email protected] return LEVEL_2; 19103c68dfa9S[email protected] } 1911cb230b9dS[email protected] } 1912cb230b9dS[email protected] 1913a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){ 191434d2123cS[email protected] if (!connection) return LEVEL_0; 191534d2123cS[email protected] if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0; 191634d2123cS[email protected] return gap_security_level_for_link_key_type(connection->link_key_type); 191734d2123cS[email protected] } 191834d2123cS[email protected] 191934d2123cS[email protected] 1920106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){ 1921106d6d11S[email protected] return level > LEVEL_2; 1922106d6d11S[email protected] } 1923106d6d11S[email protected] 192434d2123cS[email protected] /** 192534d2123cS[email protected] * @brief get current security level 192634d2123cS[email protected] */ 192734d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){ 192834d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 192934d2123cS[email protected] if (!connection) return LEVEL_0; 193034d2123cS[email protected] return gap_security_level_for_connection(connection); 193134d2123cS[email protected] } 193234d2123cS[email protected] 1933cb230b9dS[email protected] /** 1934cb230b9dS[email protected] * @brief request connection to device to 1935cb230b9dS[email protected] * @result GAP_AUTHENTICATION_RESULT 1936cb230b9dS[email protected] */ 193734d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){ 193834d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 193934d2123cS[email protected] if (!connection){ 1940a00031e2S[email protected] hci_emit_security_level(con_handle, LEVEL_0); 194134d2123cS[email protected] return; 194234d2123cS[email protected] } 194334d2123cS[email protected] gap_security_level_t current_level = gap_security_level(con_handle); 194434d2123cS[email protected] log_info("gap_request_security_level %u, current level %u", requested_level, current_level); 194534d2123cS[email protected] if (current_level >= requested_level){ 1946a00031e2S[email protected] hci_emit_security_level(con_handle, current_level); 194734d2123cS[email protected] return; 194834d2123cS[email protected] } 1949a00031e2S[email protected] 195034d2123cS[email protected] connection->requested_security_level = requested_level; 1951a00031e2S[email protected] 1952fb8ba0dbS[email protected] // would enabling ecnryption suffice (>= LEVEL_2)? 19533a9fb326S[email protected] if (hci_stack->remote_device_db){ 1954a00031e2S[email protected] link_key_type_t link_key_type; 1955a00031e2S[email protected] link_key_t link_key; 19563a9fb326S[email protected] if (hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)){ 1957a00031e2S[email protected] if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){ 1958a00031e2S[email protected] connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 1959a00031e2S[email protected] return; 1960a00031e2S[email protected] } 1961a00031e2S[email protected] } 1962a00031e2S[email protected] } 1963a00031e2S[email protected] 19641eb2563eS[email protected] // try to authenticate connection 19651eb2563eS[email protected] connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 1966e00caf9cS[email protected] } 1967ad83dc6aS[email protected] 1968ad83dc6aS[email protected] /** 1969ad83dc6aS[email protected] * @brief start dedicated bonding with device. disconnect after bonding 1970ad83dc6aS[email protected] * @param device 1971ad83dc6aS[email protected] * @param request MITM protection 1972ad83dc6aS[email protected] * @result GAP_DEDICATED_BONDING_COMPLETE 1973ad83dc6aS[email protected] */ 1974ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){ 1975ad83dc6aS[email protected] 1976ad83dc6aS[email protected] 1977ad83dc6aS[email protected] printf("gap_dedicated_bonding clled\n"); 1978ad83dc6aS[email protected] // create connection state machine 1979ad83dc6aS[email protected] hci_connection_t * connection = create_connection_for_addr(device); 1980ad83dc6aS[email protected] 1981ad83dc6aS[email protected] if (!connection){ 1982ad83dc6aS[email protected] return BTSTACK_MEMORY_ALLOC_FAILED; 1983ad83dc6aS[email protected] } 1984ad83dc6aS[email protected] 1985ad83dc6aS[email protected] printf("gap_dedicated_bonding 2\n"); 1986ad83dc6aS[email protected] 1987ad83dc6aS[email protected] // delete linkn key 1988ad83dc6aS[email protected] hci_drop_link_key_for_bd_addr( (bd_addr_t *) &device); 1989ad83dc6aS[email protected] 1990ad83dc6aS[email protected] // configure LEVEL_2/3, dedicated bonding 1991ad83dc6aS[email protected] connection->state = SEND_CREATE_CONNECTION; 1992ad83dc6aS[email protected] connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2; 1993ad83dc6aS[email protected] connection->bonding_flags = BONDING_DEDICATED; 1994ad83dc6aS[email protected] 1995ad83dc6aS[email protected] // wait for GAP Security Result and send GAP Dedicated Bonding complete 1996ad83dc6aS[email protected] 1997ad83dc6aS[email protected] // handle: connnection failure (connection complete != ok) 1998ad83dc6aS[email protected] // handle: authentication failure 1999ad83dc6aS[email protected] // handle: disconnect on done 2000ad83dc6aS[email protected] 2001ad83dc6aS[email protected] hci_run(); 2002ad83dc6aS[email protected] 2003ad83dc6aS[email protected] return 0; 2004ad83dc6aS[email protected] } 2005