11f504dbdSmatthias.ringwald /* 2a0c35809S[email protected] * Copyright (C) 2014 BlueKitchen GmbH 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 * 20a0c35809S[email protected] * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH 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 * 33a0c35809S[email protected] * Please inquire about commercial licensing options at 34a0c35809S[email protected] * [email protected] 356b64433eSmatthias.ringwald * 361713bceaSmatthias.ringwald */ 371713bceaSmatthias.ringwald 38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "hci.c" 39ab2c6ae4SMatthias Ringwald 401713bceaSmatthias.ringwald /* 411f504dbdSmatthias.ringwald * hci.c 421f504dbdSmatthias.ringwald * 431f504dbdSmatthias.ringwald * Created by Matthias Ringwald on 4/29/09. 441f504dbdSmatthias.ringwald * 451f504dbdSmatthias.ringwald */ 461f504dbdSmatthias.ringwald 477907f069SMatthias Ringwald #include "btstack_config.h" 4828171530Smatthias.ringwald 497f2435e6Smatthias.ringwald 5006b9e820SMatthias Ringwald #ifdef ENABLE_CLASSIC 51aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK 528f2a52f4SMatthias Ringwald #include "btstack_run_loop_embedded.h" 53a484130cSMatthias Ringwald #endif 5406b9e820SMatthias Ringwald #endif 55a484130cSMatthias Ringwald 564a3574a1SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 57d0b87befSMatthias Ringwald #include "../port/ios/src/btstack_control_iphone.h" 584a3574a1SMatthias Ringwald #endif 594a3574a1SMatthias Ringwald 60a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 61472a5742SMatthias Ringwald #include "gap.h" 6245c102fdSMatthias Ringwald #endif 6345c102fdSMatthias Ringwald 6493b8dc03Smatthias.ringwald #include <stdarg.h> 6593b8dc03Smatthias.ringwald #include <string.h> 6656fe0872Smatthias.ringwald #include <stdio.h> 675838a2edSMatthias Ringwald #include <inttypes.h> 687f2435e6Smatthias.ringwald 6916ece135SMatthias Ringwald #include "btstack_debug.h" 700e2df43fSMatthias Ringwald #include "btstack_event.h" 714a3574a1SMatthias Ringwald #include "btstack_linked_list.h" 724a3574a1SMatthias Ringwald #include "btstack_memory.h" 7361f37892SMatthias Ringwald #include "bluetooth_company_id.h" 744a3574a1SMatthias Ringwald #include "gap.h" 754a3574a1SMatthias Ringwald #include "hci.h" 764a3574a1SMatthias Ringwald #include "hci_cmd.h" 77d8905019Smatthias.ringwald #include "hci_dump.h" 7893b8dc03Smatthias.ringwald 79*2b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 80*2b838201SMatthias Ringwald #ifndef HCI_HOST_ACL_PACKET_NUM 81*2b838201SMatthias Ringwald #error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_ACL_PACKET_NUM" 82*2b838201SMatthias Ringwald #endif 83*2b838201SMatthias Ringwald #ifndef HCI_HOST_ACL_PACKET_LEN 84*2b838201SMatthias Ringwald #error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_ACL_PACKET_LEN" 85*2b838201SMatthias Ringwald #endif 86*2b838201SMatthias Ringwald #ifndef HCI_HOST_SCO_PACKET_NUM 87*2b838201SMatthias Ringwald #error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_SCO_PACKET_NUM" 88*2b838201SMatthias Ringwald #endif 89*2b838201SMatthias Ringwald #ifndef HCI_HOST_SCO_PACKET_LEN 90*2b838201SMatthias Ringwald #error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_SCO_PACKET_LEN" 91*2b838201SMatthias Ringwald #endif 92*2b838201SMatthias Ringwald #endif 931b0e3922Smatthias.ringwald 94169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000 95659d758cSMatthias Ringwald #define HCI_RESET_RESEND_TIMEOUT_MS 200 96ee091cf1Smatthias.ringwald 97b83d5eabSMatthias Ringwald // prototypes 9835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 99758b46ceSmatthias.ringwald static void hci_update_scan_enable(void); 10035454696SMatthias Ringwald static void hci_emit_discoverable_enabled(uint8_t enabled); 10135454696SMatthias Ringwald static int hci_local_ssp_activated(void); 10235454696SMatthias Ringwald static int hci_remote_ssp_supported(hci_con_handle_t con_handle); 10335454696SMatthias Ringwald static void hci_notify_if_sco_can_send_now(void); 10435454696SMatthias Ringwald static void hci_emit_connection_complete(bd_addr_t address, hci_con_handle_t con_handle, uint8_t status); 105a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection); 10635454696SMatthias Ringwald static void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level); 107ec820d77SMatthias Ringwald static void hci_connection_timeout_handler(btstack_timer_source_t *timer); 10896a45072S[email protected] static void hci_connection_timestamp(hci_connection_t *connection); 10952db98b2SMatthias Ringwald static void hci_emit_l2cap_check_timeout(hci_connection_t *conn); 11052db98b2SMatthias Ringwald #endif 1117586ee35S[email protected] static int hci_power_control_on(void); 1127586ee35S[email protected] static void hci_power_control_off(void); 1136da48142SSean Wilson static void hci_state_reset(void); 114fc64f94aSMatthias Ringwald static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason); 115b83d5eabSMatthias Ringwald static void hci_emit_nr_connections_changed(void); 116b83d5eabSMatthias Ringwald static void hci_emit_hci_open_failed(void); 117b83d5eabSMatthias Ringwald static void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status); 118b83d5eabSMatthias Ringwald static void hci_emit_event(uint8_t * event, uint16_t size, int dump); 119b83d5eabSMatthias Ringwald static void hci_emit_acl_packet(uint8_t * packet, uint16_t size); 12095d71764SMatthias Ringwald static void hci_run(void); 12195d71764SMatthias Ringwald static int hci_is_le_connection(hci_connection_t * connection); 12295d71764SMatthias Ringwald static int hci_number_free_acl_slots_for_connection_type( bd_addr_type_t address_type); 1235d509858SMatthias Ringwald 124a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 125e8c8828eSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 12639677e66SMatthias Ringwald // called from test/ble_client/advertising_data_parser.c 12739677e66SMatthias Ringwald void le_handle_advertisement_report(uint8_t *packet, int size); 12842ff5ba1SMatthias Ringwald static void hci_remove_from_whitelist(bd_addr_type_t address_type, bd_addr_t address); 1295d509858SMatthias Ringwald #endif 130d70217a2SMatthias Ringwald #endif 131758b46ceSmatthias.ringwald 13206b35ec0Smatthias.ringwald // the STACK is here 1333a9fb326S[email protected] #ifndef HAVE_MALLOC 1343a9fb326S[email protected] static hci_stack_t hci_stack_static; 1353a9fb326S[email protected] #endif 1363a9fb326S[email protected] static hci_stack_t * hci_stack = NULL; 13716833f0aSmatthias.ringwald 1381c9e5e9dSMatthias Ringwald #ifdef ENABLE_CLASSIC 13966fb9560S[email protected] // test helper 14066fb9560S[email protected] static uint8_t disable_l2cap_timeouts = 0; 1411c9e5e9dSMatthias Ringwald #endif 14266fb9560S[email protected] 14396a45072S[email protected] /** 14496a45072S[email protected] * create connection for given address 14596a45072S[email protected] * 14696a45072S[email protected] * @return connection OR NULL, if no memory left 14796a45072S[email protected] */ 14896a45072S[email protected] static hci_connection_t * create_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){ 1491a06f663S[email protected] log_info("create_connection_for_addr %s, type %x", bd_addr_to_str(addr), addr_type); 150bb69aaaeS[email protected] hci_connection_t * conn = btstack_memory_hci_connection_get(); 15196a45072S[email protected] if (!conn) return NULL; 152c91d150bS[email protected] memset(conn, 0, sizeof(hci_connection_t)); 153058e3d6bSMatthias Ringwald bd_addr_copy(conn->address, addr); 15496a45072S[email protected] conn->address_type = addr_type; 15596a45072S[email protected] conn->con_handle = 0xffff; 15696a45072S[email protected] conn->authentication_flags = AUTH_FLAGS_NONE; 15796a45072S[email protected] conn->bonding_flags = 0; 15896a45072S[email protected] conn->requested_security_level = LEVEL_0; 15952db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 16091a977e8SMatthias Ringwald btstack_run_loop_set_timer_handler(&conn->timeout, hci_connection_timeout_handler); 16191a977e8SMatthias Ringwald btstack_run_loop_set_timer_context(&conn->timeout, conn); 16296a45072S[email protected] hci_connection_timestamp(conn); 16352db98b2SMatthias Ringwald #endif 16496a45072S[email protected] conn->acl_recombination_length = 0; 16596a45072S[email protected] conn->acl_recombination_pos = 0; 16696a45072S[email protected] conn->num_acl_packets_sent = 0; 1671a06f663S[email protected] conn->num_sco_packets_sent = 0; 168da886c03S[email protected] conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 169665d90f2SMatthias Ringwald btstack_linked_list_add(&hci_stack->connections, (btstack_linked_item_t *) conn); 17096a45072S[email protected] return conn; 17196a45072S[email protected] } 17266fb9560S[email protected] 173da886c03S[email protected] 174da886c03S[email protected] /** 175da886c03S[email protected] * get le connection parameter range 176da886c03S[email protected] * 177da886c03S[email protected] * @return le connection parameter range struct 178da886c03S[email protected] */ 1794ced4e8cSMatthias Ringwald void gap_get_connection_parameter_range(le_connection_parameter_range_t * range){ 1804ced4e8cSMatthias Ringwald *range = hci_stack->le_connection_parameter_range; 181da886c03S[email protected] } 182da886c03S[email protected] 183da886c03S[email protected] /** 184da886c03S[email protected] * set le connection parameter range 185da886c03S[email protected] * 186da886c03S[email protected] */ 187da886c03S[email protected] 1884ced4e8cSMatthias Ringwald void gap_set_connection_parameter_range(le_connection_parameter_range_t *range){ 1894ced4e8cSMatthias Ringwald hci_stack->le_connection_parameter_range = *range; 190da886c03S[email protected] } 191da886c03S[email protected] 192da886c03S[email protected] /** 193da886c03S[email protected] * get hci connections iterator 194da886c03S[email protected] * 195da886c03S[email protected] * @return hci connections iterator 196da886c03S[email protected] */ 197da886c03S[email protected] 198665d90f2SMatthias Ringwald void hci_connections_get_iterator(btstack_linked_list_iterator_t *it){ 199665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(it, &hci_stack->connections); 200da886c03S[email protected] } 201da886c03S[email protected] 20297addcc5Smatthias.ringwald /** 203ee091cf1Smatthias.ringwald * get connection for a given handle 204ee091cf1Smatthias.ringwald * 205ee091cf1Smatthias.ringwald * @return connection OR NULL, if not found 206ee091cf1Smatthias.ringwald */ 2075061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){ 208665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 209665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 210665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 211665d90f2SMatthias Ringwald hci_connection_t * item = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 2123ac2fe56S[email protected] if ( item->con_handle == con_handle ) { 213da886c03S[email protected] return item; 214ee091cf1Smatthias.ringwald } 215ee091cf1Smatthias.ringwald } 216ee091cf1Smatthias.ringwald return NULL; 217ee091cf1Smatthias.ringwald } 218ee091cf1Smatthias.ringwald 21996a45072S[email protected] /** 22096a45072S[email protected] * get connection for given address 22196a45072S[email protected] * 22296a45072S[email protected] * @return connection OR NULL, if not found 22396a45072S[email protected] */ 2242e77e513S[email protected] hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){ 225665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 226665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 227665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 228665d90f2SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 22996a45072S[email protected] if (connection->address_type != addr_type) continue; 23096a45072S[email protected] if (memcmp(addr, connection->address, 6) != 0) continue; 23162bda3fbS[email protected] return connection; 23262bda3fbS[email protected] } 23362bda3fbS[email protected] return NULL; 23462bda3fbS[email protected] } 23562bda3fbS[email protected] 23652db98b2SMatthias Ringwald 23752db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 23852db98b2SMatthias Ringwald 239ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 240ee752bb8SMatthias Ringwald static int hci_number_sco_connections(void){ 241ee752bb8SMatthias Ringwald int connections = 0; 242ee752bb8SMatthias Ringwald btstack_linked_list_iterator_t it; 243ee752bb8SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 244ee752bb8SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 245ee752bb8SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 246ee752bb8SMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_SCO) continue; 247ee752bb8SMatthias Ringwald connections++; 248ee752bb8SMatthias Ringwald } 249ee752bb8SMatthias Ringwald return connections; 250ee752bb8SMatthias Ringwald } 251ee752bb8SMatthias Ringwald #endif 252ee752bb8SMatthias Ringwald 253ec820d77SMatthias Ringwald static void hci_connection_timeout_handler(btstack_timer_source_t *timer){ 25491a977e8SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_run_loop_get_timer_context(timer); 255aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK 256528a4a3bSMatthias Ringwald if (btstack_run_loop_embedded_get_ticks() > connection->timestamp + btstack_run_loop_embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){ 257c785ef68Smatthias.ringwald // connections might be timed out 258c785ef68Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 259c785ef68Smatthias.ringwald } 260f316a845SMatthias Ringwald #else 261528a4a3bSMatthias Ringwald if (btstack_run_loop_get_time_ms() > connection->timestamp + HCI_CONNECTION_TIMEOUT_MS){ 2625f26aadcSMatthias Ringwald // connections might be timed out 2635f26aadcSMatthias Ringwald hci_emit_l2cap_check_timeout(connection); 2645f26aadcSMatthias Ringwald } 2655f26aadcSMatthias Ringwald #endif 266c785ef68Smatthias.ringwald } 267ee091cf1Smatthias.ringwald 268ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){ 269aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK 270528a4a3bSMatthias Ringwald connection->timestamp = btstack_run_loop_embedded_get_ticks(); 271f316a845SMatthias Ringwald #else 272528a4a3bSMatthias Ringwald connection->timestamp = btstack_run_loop_get_time_ms(); 2735f26aadcSMatthias Ringwald #endif 274ee091cf1Smatthias.ringwald } 275ee091cf1Smatthias.ringwald 27628ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 27728ca2b46S[email protected] conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags); 27828ca2b46S[email protected] } 27928ca2b46S[email protected] 28035454696SMatthias Ringwald 28128ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 28228ca2b46S[email protected] conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags); 28328ca2b46S[email protected] } 28428ca2b46S[email protected] 28543bfb1bdSmatthias.ringwald /** 28680ca58a0Smatthias.ringwald * add authentication flags and reset timer 28796a45072S[email protected] * @note: assumes classic connection 2882e77e513S[email protected] * @note: bd_addr is passed in as litle endian uint8_t * as it is called from parsing packets 2897fde4af9Smatthias.ringwald */ 2907fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){ 2917fde4af9Smatthias.ringwald bd_addr_t addr; 292724d70a2SMatthias Ringwald reverse_bd_addr(bd_addr, addr); 2932e77e513S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 2947fde4af9Smatthias.ringwald if (conn) { 29528ca2b46S[email protected] connectionSetAuthenticationFlags(conn, flags); 29680ca58a0Smatthias.ringwald hci_connection_timestamp(conn); 2977fde4af9Smatthias.ringwald } 2987fde4af9Smatthias.ringwald } 2997fde4af9Smatthias.ringwald 30080ca58a0Smatthias.ringwald int hci_authentication_active_for_handle(hci_con_handle_t handle){ 3015061f3afS[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 30280ca58a0Smatthias.ringwald if (!conn) return 0; 3036724cd9eS[email protected] if (conn->authentication_flags & LEGACY_PAIRING_ACTIVE) return 1; 3046724cd9eS[email protected] if (conn->authentication_flags & SSP_PAIRING_ACTIVE) return 1; 3056724cd9eS[email protected] return 0; 30680ca58a0Smatthias.ringwald } 30780ca58a0Smatthias.ringwald 30815a95bd5SMatthias Ringwald void gap_drop_link_key_for_bd_addr(bd_addr_t addr){ 30955597469SMatthias Ringwald if (!hci_stack->link_key_db) return; 3102bacf595SMatthias Ringwald log_info("gap_drop_link_key_for_bd_addr: %s", bd_addr_to_str(addr)); 311a98592bcSMatthias Ringwald hci_stack->link_key_db->delete_link_key(addr); 312c12e46e7Smatthias.ringwald } 31355597469SMatthias Ringwald 31455597469SMatthias Ringwald void gap_store_link_key_for_bd_addr(bd_addr_t addr, link_key_t link_key, link_key_type_t type){ 31555597469SMatthias Ringwald if (!hci_stack->link_key_db) return; 3162bacf595SMatthias Ringwald log_info("gap_store_link_key_for_bd_addr: %s, type %u", bd_addr_to_str(addr), type); 31755597469SMatthias Ringwald hci_stack->link_key_db->put_link_key(addr, link_key, type); 318c12e46e7Smatthias.ringwald } 31935454696SMatthias Ringwald #endif 320c12e46e7Smatthias.ringwald 32195d71764SMatthias Ringwald static int hci_is_le_connection(hci_connection_t * connection){ 3220bf6344aS[email protected] return connection->address_type == BD_ADDR_TYPE_LE_PUBLIC || 3230bf6344aS[email protected] connection->address_type == BD_ADDR_TYPE_LE_RANDOM; 3240bf6344aS[email protected] } 3250bf6344aS[email protected] 3267fde4af9Smatthias.ringwald /** 32743bfb1bdSmatthias.ringwald * count connections 32843bfb1bdSmatthias.ringwald */ 32940d1c7a4Smatthias.ringwald static int nr_hci_connections(void){ 33056c253c9Smatthias.ringwald int count = 0; 331665d90f2SMatthias Ringwald btstack_linked_item_t *it; 332665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next, count++); 33343bfb1bdSmatthias.ringwald return count; 33443bfb1bdSmatthias.ringwald } 335c8e4258aSmatthias.ringwald 33695d71764SMatthias Ringwald static int hci_number_free_acl_slots_for_connection_type(bd_addr_type_t address_type){ 337ee303eddS[email protected] 338f04a0c31SMatthias Ringwald unsigned int num_packets_sent_classic = 0; 339f04a0c31SMatthias Ringwald unsigned int num_packets_sent_le = 0; 340ee303eddS[email protected] 341665d90f2SMatthias Ringwald btstack_linked_item_t *it; 342665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 343998906cdSmatthias.ringwald hci_connection_t * connection = (hci_connection_t *) it; 344ee303eddS[email protected] if (connection->address_type == BD_ADDR_TYPE_CLASSIC){ 345ee303eddS[email protected] num_packets_sent_classic += connection->num_acl_packets_sent; 346ee303eddS[email protected] } else { 347ee303eddS[email protected] num_packets_sent_le += connection->num_acl_packets_sent; 348ee303eddS[email protected] } 349ee303eddS[email protected] } 350d999b54eSMatthias Ringwald log_debug("ACL classic buffers: %u used of %u", num_packets_sent_classic, hci_stack->acl_packets_total_num); 351ee303eddS[email protected] int free_slots_classic = hci_stack->acl_packets_total_num - num_packets_sent_classic; 352ee303eddS[email protected] int free_slots_le = 0; 353ee303eddS[email protected] 354ee303eddS[email protected] if (free_slots_classic < 0){ 3559da54300S[email protected] log_error("hci_number_free_acl_slots: outgoing classic packets (%u) > total classic packets (%u)", num_packets_sent_classic, hci_stack->acl_packets_total_num); 356998906cdSmatthias.ringwald return 0; 357998906cdSmatthias.ringwald } 358ee303eddS[email protected] 359ee303eddS[email protected] if (hci_stack->le_acl_packets_total_num){ 360ee303eddS[email protected] // if we have LE slots, they are used 361ee303eddS[email protected] free_slots_le = hci_stack->le_acl_packets_total_num - num_packets_sent_le; 362ee303eddS[email protected] if (free_slots_le < 0){ 3639da54300S[email protected] log_error("hci_number_free_acl_slots: outgoing le packets (%u) > total le packets (%u)", num_packets_sent_le, hci_stack->le_acl_packets_total_num); 364ee303eddS[email protected] return 0; 365998906cdSmatthias.ringwald } 366ee303eddS[email protected] } else { 367ee303eddS[email protected] // otherwise, classic slots are used for LE, too 368ee303eddS[email protected] free_slots_classic -= num_packets_sent_le; 369ee303eddS[email protected] if (free_slots_classic < 0){ 3709da54300S[email protected] log_error("hci_number_free_acl_slots: outgoing classic + le packets (%u + %u) > total packets (%u)", num_packets_sent_classic, num_packets_sent_le, hci_stack->acl_packets_total_num); 371ee303eddS[email protected] return 0; 372ee303eddS[email protected] } 373ee303eddS[email protected] } 374ee303eddS[email protected] 375ee303eddS[email protected] switch (address_type){ 376ee303eddS[email protected] case BD_ADDR_TYPE_UNKNOWN: 3772125de09SMatthias Ringwald log_error("hci_number_free_acl_slots: unknown address type"); 378ee303eddS[email protected] return 0; 379ee303eddS[email protected] 380ee303eddS[email protected] case BD_ADDR_TYPE_CLASSIC: 381ee303eddS[email protected] return free_slots_classic; 382ee303eddS[email protected] 383ee303eddS[email protected] default: 384cb00d3aaS[email protected] if (hci_stack->le_acl_packets_total_num){ 385ee303eddS[email protected] return free_slots_le; 386ee303eddS[email protected] } 387cb00d3aaS[email protected] return free_slots_classic; 388cb00d3aaS[email protected] } 389998906cdSmatthias.ringwald } 390998906cdSmatthias.ringwald 3912125de09SMatthias Ringwald int hci_number_free_acl_slots_for_handle(hci_con_handle_t con_handle){ 3922125de09SMatthias Ringwald // get connection type 3932125de09SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 3942125de09SMatthias Ringwald if (!connection){ 3952125de09SMatthias Ringwald log_error("hci_number_free_acl_slots: handle 0x%04x not in connection list", con_handle); 3962125de09SMatthias Ringwald return 0; 3972125de09SMatthias Ringwald } 3982125de09SMatthias Ringwald return hci_number_free_acl_slots_for_connection_type(connection->address_type); 3992125de09SMatthias Ringwald } 4002125de09SMatthias Ringwald 40135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 402701e3307SMatthias Ringwald static int hci_number_free_sco_slots(void){ 403f04a0c31SMatthias Ringwald unsigned int num_sco_packets_sent = 0; 404665d90f2SMatthias Ringwald btstack_linked_item_t *it; 405665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 406e35edcc1S[email protected] hci_connection_t * connection = (hci_connection_t *) it; 407e35edcc1S[email protected] num_sco_packets_sent += connection->num_sco_packets_sent; 408e35edcc1S[email protected] } 409e35edcc1S[email protected] if (num_sco_packets_sent > hci_stack->sco_packets_total_num){ 410701e3307SMatthias Ringwald log_info("hci_number_free_sco_slots:packets (%u) > total packets (%u)", num_sco_packets_sent, hci_stack->sco_packets_total_num); 41144d0e3d5S[email protected] return 0; 41244d0e3d5S[email protected] } 413701e3307SMatthias Ringwald // log_info("hci_number_free_sco_slots u", handle, num_sco_packets_sent); 414e35edcc1S[email protected] return hci_stack->sco_packets_total_num - num_sco_packets_sent; 415e35edcc1S[email protected] } 41635454696SMatthias Ringwald #endif 41744d0e3d5S[email protected] 418*2b838201SMatthias Ringwald // only used to send HCI Host Number Completed Packets 419*2b838201SMatthias Ringwald static int hci_can_send_comand_packet_transport(void){ 420ac928cc2S[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 421ac928cc2S[email protected] 422ac928cc2S[email protected] // check for async hci transport implementations 423ac928cc2S[email protected] if (hci_stack->hci_transport->can_send_packet_now){ 424ac928cc2S[email protected] if (!hci_stack->hci_transport->can_send_packet_now(HCI_COMMAND_DATA_PACKET)){ 425ac928cc2S[email protected] return 0; 426ac928cc2S[email protected] } 427ac928cc2S[email protected] } 428*2b838201SMatthias Ringwald return 1; 429*2b838201SMatthias Ringwald } 430ac928cc2S[email protected] 431*2b838201SMatthias Ringwald // new functions replacing hci_can_send_packet_now[_using_packet_buffer] 432*2b838201SMatthias Ringwald int hci_can_send_command_packet_now(void){ 433*2b838201SMatthias Ringwald if (hci_can_send_comand_packet_transport() == 0) return 0; 434ac928cc2S[email protected] return hci_stack->num_cmd_packets > 0; 435ac928cc2S[email protected] } 436ac928cc2S[email protected] 4379d04d3a7SMatthias Ringwald static int hci_transport_can_send_prepared_packet_now(uint8_t packet_type){ 438ac928cc2S[email protected] // check for async hci transport implementations 4399d04d3a7SMatthias Ringwald if (!hci_stack->hci_transport->can_send_packet_now) return 1; 4409d04d3a7SMatthias Ringwald return hci_stack->hci_transport->can_send_packet_now(packet_type); 441ac928cc2S[email protected] } 4429d04d3a7SMatthias Ringwald 4439d04d3a7SMatthias Ringwald static int hci_can_send_prepared_acl_packet_for_address_type(bd_addr_type_t address_type){ 4449d04d3a7SMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_ACL_DATA_PACKET)) return 0; 4459d04d3a7SMatthias Ringwald return hci_number_free_acl_slots_for_connection_type(address_type) > 0; 446ac928cc2S[email protected] } 4479d04d3a7SMatthias Ringwald 4489d04d3a7SMatthias Ringwald int hci_can_send_acl_le_packet_now(void){ 4499d04d3a7SMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return 0; 4509d04d3a7SMatthias Ringwald return hci_can_send_prepared_acl_packet_for_address_type(BD_ADDR_TYPE_LE_PUBLIC); 4519d04d3a7SMatthias Ringwald } 4529d04d3a7SMatthias Ringwald 4539d04d3a7SMatthias Ringwald int hci_can_send_prepared_acl_packet_now(hci_con_handle_t con_handle) { 4549d04d3a7SMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_ACL_DATA_PACKET)) return 0; 455e79abdd6S[email protected] return hci_number_free_acl_slots_for_handle(con_handle) > 0; 456ac928cc2S[email protected] } 457ac928cc2S[email protected] 458ac928cc2S[email protected] int hci_can_send_acl_packet_now(hci_con_handle_t con_handle){ 459ac928cc2S[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 460ac928cc2S[email protected] return hci_can_send_prepared_acl_packet_now(con_handle); 4616b4af23dS[email protected] } 4626b4af23dS[email protected] 46335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 46435454696SMatthias Ringwald int hci_can_send_acl_classic_packet_now(void){ 46535454696SMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return 0; 46635454696SMatthias Ringwald return hci_can_send_prepared_acl_packet_for_address_type(BD_ADDR_TYPE_CLASSIC); 46735454696SMatthias Ringwald } 46835454696SMatthias Ringwald 469701e3307SMatthias Ringwald int hci_can_send_prepared_sco_packet_now(void){ 470d057580eSMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_SCO_DATA_PACKET)) return 0; 471ed361f5fSMatthias Ringwald if (!hci_stack->synchronous_flow_control_enabled) return 1; 472701e3307SMatthias Ringwald return hci_number_free_sco_slots() > 0; 47344d0e3d5S[email protected] } 47444d0e3d5S[email protected] 475701e3307SMatthias Ringwald int hci_can_send_sco_packet_now(void){ 476d057580eSMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return 0; 477701e3307SMatthias Ringwald return hci_can_send_prepared_sco_packet_now(); 47844d0e3d5S[email protected] } 47944d0e3d5S[email protected] 480d057580eSMatthias Ringwald void hci_request_sco_can_send_now_event(void){ 481d057580eSMatthias Ringwald hci_stack->sco_waiting_for_can_send_now = 1; 482d057580eSMatthias Ringwald hci_notify_if_sco_can_send_now(); 483d057580eSMatthias Ringwald } 48435454696SMatthias Ringwald #endif 485d057580eSMatthias Ringwald 48695d71764SMatthias Ringwald // used for internal checks in l2cap.c 487c8b9416aS[email protected] int hci_is_packet_buffer_reserved(void){ 488c8b9416aS[email protected] return hci_stack->hci_packet_buffer_reserved; 489c8b9416aS[email protected] } 490c8b9416aS[email protected] 4916b4af23dS[email protected] // reserves outgoing packet buffer. @returns 1 if successful 4926b4af23dS[email protected] int hci_reserve_packet_buffer(void){ 4939d14b626S[email protected] if (hci_stack->hci_packet_buffer_reserved) { 4949d14b626S[email protected] log_error("hci_reserve_packet_buffer called but buffer already reserved"); 4959d14b626S[email protected] return 0; 4969d14b626S[email protected] } 4976b4af23dS[email protected] hci_stack->hci_packet_buffer_reserved = 1; 4986b4af23dS[email protected] return 1; 4996b4af23dS[email protected] } 5006b4af23dS[email protected] 50168a0fcf7S[email protected] void hci_release_packet_buffer(void){ 50268a0fcf7S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 50368a0fcf7S[email protected] } 50468a0fcf7S[email protected] 5056b4af23dS[email protected] // assumption: synchronous implementations don't provide can_send_packet_now as they don't keep the buffer after the call 5067f02f414SMatthias Ringwald static int hci_transport_synchronous(void){ 5076b4af23dS[email protected] return hci_stack->hci_transport->can_send_packet_now == NULL; 5086b4af23dS[email protected] } 5096b4af23dS[email protected] 510452cf3bbS[email protected] static int hci_send_acl_packet_fragments(hci_connection_t *connection){ 511452cf3bbS[email protected] 512452cf3bbS[email protected] // log_info("hci_send_acl_packet_fragments %u/%u (con 0x%04x)", hci_stack->acl_fragmentation_pos, hci_stack->acl_fragmentation_total_size, connection->con_handle); 513452cf3bbS[email protected] 514452cf3bbS[email protected] // max ACL data packet length depends on connection type (LE vs. Classic) and available buffers 515452cf3bbS[email protected] uint16_t max_acl_data_packet_length = hci_stack->acl_data_packet_length; 516452cf3bbS[email protected] if (hci_is_le_connection(connection) && hci_stack->le_data_packets_length > 0){ 517452cf3bbS[email protected] max_acl_data_packet_length = hci_stack->le_data_packets_length; 518452cf3bbS[email protected] } 519452cf3bbS[email protected] 520452cf3bbS[email protected] // testing: reduce buffer to minimum 521452cf3bbS[email protected] // max_acl_data_packet_length = 52; 522452cf3bbS[email protected] 523d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments entered"); 524d999b54eSMatthias Ringwald 525452cf3bbS[email protected] int err; 526452cf3bbS[email protected] // multiple packets could be send on a synchronous HCI transport 527452cf3bbS[email protected] while (1){ 528452cf3bbS[email protected] 529d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop entered"); 530d999b54eSMatthias Ringwald 531452cf3bbS[email protected] // get current data 532452cf3bbS[email protected] const uint16_t acl_header_pos = hci_stack->acl_fragmentation_pos - 4; 533452cf3bbS[email protected] int current_acl_data_packet_length = hci_stack->acl_fragmentation_total_size - hci_stack->acl_fragmentation_pos; 534452cf3bbS[email protected] int more_fragments = 0; 535452cf3bbS[email protected] 536452cf3bbS[email protected] // if ACL packet is larger than Bluetooth packet buffer, only send max_acl_data_packet_length 537452cf3bbS[email protected] if (current_acl_data_packet_length > max_acl_data_packet_length){ 538452cf3bbS[email protected] more_fragments = 1; 539452cf3bbS[email protected] current_acl_data_packet_length = max_acl_data_packet_length; 540452cf3bbS[email protected] } 541452cf3bbS[email protected] 542452cf3bbS[email protected] // copy handle_and_flags if not first fragment and update packet boundary flags to be 01 (continuing fragmnent) 543452cf3bbS[email protected] if (acl_header_pos > 0){ 544f8fbdce0SMatthias Ringwald uint16_t handle_and_flags = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 545452cf3bbS[email protected] handle_and_flags = (handle_and_flags & 0xcfff) | (1 << 12); 546f8fbdce0SMatthias Ringwald little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos, handle_and_flags); 547452cf3bbS[email protected] } 548452cf3bbS[email protected] 549452cf3bbS[email protected] // update header len 550f8fbdce0SMatthias Ringwald little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos + 2, current_acl_data_packet_length); 551452cf3bbS[email protected] 552452cf3bbS[email protected] // count packet 553452cf3bbS[email protected] connection->num_acl_packets_sent++; 554f04a0c31SMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop before send (more fragments %d)", more_fragments); 555d999b54eSMatthias Ringwald 556d999b54eSMatthias Ringwald // update state for next fragment (if any) as "transport done" might be sent during send_packet already 557d999b54eSMatthias Ringwald if (more_fragments){ 558d999b54eSMatthias Ringwald // update start of next fragment to send 559d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_pos += current_acl_data_packet_length; 560d999b54eSMatthias Ringwald } else { 561d999b54eSMatthias Ringwald // done 562d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_pos = 0; 563d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_total_size = 0; 564d999b54eSMatthias Ringwald } 565452cf3bbS[email protected] 566452cf3bbS[email protected] // send packet 567452cf3bbS[email protected] uint8_t * packet = &hci_stack->hci_packet_buffer[acl_header_pos]; 568452cf3bbS[email protected] const int size = current_acl_data_packet_length + 4; 5695bb5bc3eS[email protected] hci_dump_packet(HCI_ACL_DATA_PACKET, 0, packet, size); 570452cf3bbS[email protected] err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size); 571452cf3bbS[email protected] 572f04a0c31SMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop after send (more fragments %d)", more_fragments); 573d999b54eSMatthias Ringwald 574452cf3bbS[email protected] // done yet? 575452cf3bbS[email protected] if (!more_fragments) break; 576452cf3bbS[email protected] 577452cf3bbS[email protected] // can send more? 578452cf3bbS[email protected] if (!hci_can_send_prepared_acl_packet_now(connection->con_handle)) return err; 579452cf3bbS[email protected] } 580452cf3bbS[email protected] 581d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop over"); 582452cf3bbS[email protected] 583d051460cS[email protected] // release buffer now for synchronous transport 584203bace6S[email protected] if (hci_transport_synchronous()){ 585452cf3bbS[email protected] hci_release_packet_buffer(); 58663fa3374SMatthias Ringwald // notify upper stack that it might be possible to send again 58763fa3374SMatthias Ringwald uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; 588d6b06661SMatthias Ringwald hci_emit_event(&event[0], sizeof(event), 0); // don't dump 589452cf3bbS[email protected] } 590452cf3bbS[email protected] 591452cf3bbS[email protected] return err; 592452cf3bbS[email protected] } 593452cf3bbS[email protected] 594826f7347S[email protected] // pre: caller has reserved the packet buffer 595826f7347S[email protected] int hci_send_acl_packet_buffer(int size){ 5967856c818Smatthias.ringwald 597452cf3bbS[email protected] // log_info("hci_send_acl_packet_buffer size %u", size); 598452cf3bbS[email protected] 599826f7347S[email protected] if (!hci_stack->hci_packet_buffer_reserved) { 600826f7347S[email protected] log_error("hci_send_acl_packet_buffer called without reserving packet buffer"); 601826f7347S[email protected] return 0; 602826f7347S[email protected] } 603826f7347S[email protected] 604d713a683S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 605d713a683S[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 606d713a683S[email protected] 607826f7347S[email protected] // check for free places on Bluetooth module 608d713a683S[email protected] if (!hci_can_send_prepared_acl_packet_now(con_handle)) { 609826f7347S[email protected] log_error("hci_send_acl_packet_buffer called but no free ACL buffers on controller"); 61097b61c7bS[email protected] hci_release_packet_buffer(); 61197b61c7bS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 61297b61c7bS[email protected] } 6136218e6f1Smatthias.ringwald 6145061f3afS[email protected] hci_connection_t *connection = hci_connection_for_handle( con_handle); 61597b61c7bS[email protected] if (!connection) { 6165fa0b7cfS[email protected] log_error("hci_send_acl_packet_buffer called but no connection for handle 0x%04x", con_handle); 61797b61c7bS[email protected] hci_release_packet_buffer(); 61897b61c7bS[email protected] return 0; 61997b61c7bS[email protected] } 62052db98b2SMatthias Ringwald 62152db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 62256cf178bSmatthias.ringwald hci_connection_timestamp(connection); 62352db98b2SMatthias Ringwald #endif 62456cf178bSmatthias.ringwald 625452cf3bbS[email protected] // hci_dump_packet( HCI_ACL_DATA_PACKET, 0, packet, size); 6267856c818Smatthias.ringwald 627452cf3bbS[email protected] // setup data 628452cf3bbS[email protected] hci_stack->acl_fragmentation_total_size = size; 629452cf3bbS[email protected] hci_stack->acl_fragmentation_pos = 4; // start of L2CAP packet 6306218e6f1Smatthias.ringwald 631452cf3bbS[email protected] return hci_send_acl_packet_fragments(connection); 632ee091cf1Smatthias.ringwald } 633ee091cf1Smatthias.ringwald 63435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 63544d0e3d5S[email protected] // pre: caller has reserved the packet buffer 63644d0e3d5S[email protected] int hci_send_sco_packet_buffer(int size){ 63744d0e3d5S[email protected] 63844d0e3d5S[email protected] // log_info("hci_send_acl_packet_buffer size %u", size); 63944d0e3d5S[email protected] 64044d0e3d5S[email protected] if (!hci_stack->hci_packet_buffer_reserved) { 64144d0e3d5S[email protected] log_error("hci_send_acl_packet_buffer called without reserving packet buffer"); 64244d0e3d5S[email protected] return 0; 64344d0e3d5S[email protected] } 64444d0e3d5S[email protected] 64544d0e3d5S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 6464b3e1e19SMatthias Ringwald 6474b3e1e19SMatthias Ringwald // skip checks in loopback mode 6484b3e1e19SMatthias Ringwald if (!hci_stack->loopback_mode){ 64944d0e3d5S[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); // same for ACL and SCO 65044d0e3d5S[email protected] 65144d0e3d5S[email protected] // check for free places on Bluetooth module 652701e3307SMatthias Ringwald if (!hci_can_send_prepared_sco_packet_now()) { 65344d0e3d5S[email protected] log_error("hci_send_sco_packet_buffer called but no free ACL buffers on controller"); 65444d0e3d5S[email protected] hci_release_packet_buffer(); 65544d0e3d5S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 65644d0e3d5S[email protected] } 65744d0e3d5S[email protected] 658e35edcc1S[email protected] // track send packet in connection struct 659e35edcc1S[email protected] hci_connection_t *connection = hci_connection_for_handle( con_handle); 660e35edcc1S[email protected] if (!connection) { 661e35edcc1S[email protected] log_error("hci_send_sco_packet_buffer called but no connection for handle 0x%04x", con_handle); 662e35edcc1S[email protected] hci_release_packet_buffer(); 663e35edcc1S[email protected] return 0; 664e35edcc1S[email protected] } 665e35edcc1S[email protected] connection->num_sco_packets_sent++; 6664b3e1e19SMatthias Ringwald } 66744d0e3d5S[email protected] 66844d0e3d5S[email protected] hci_dump_packet( HCI_SCO_DATA_PACKET, 0, packet, size); 669543e835cSMatthias Ringwald int err = hci_stack->hci_transport->send_packet(HCI_SCO_DATA_PACKET, packet, size); 670543e835cSMatthias Ringwald 671543e835cSMatthias Ringwald if (hci_transport_synchronous()){ 672543e835cSMatthias Ringwald hci_release_packet_buffer(); 67363fa3374SMatthias Ringwald // notify upper stack that it might be possible to send again 67463fa3374SMatthias Ringwald uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; 675d6b06661SMatthias Ringwald hci_emit_event(&event[0], sizeof(event), 0); // don't dump 676543e835cSMatthias Ringwald } 677543e835cSMatthias Ringwald 678543e835cSMatthias Ringwald return err; 67944d0e3d5S[email protected] } 68035454696SMatthias Ringwald #endif 68144d0e3d5S[email protected] 68216833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){ 6837856c818Smatthias.ringwald 684e76a89eeS[email protected] // log_info("acl_handler: size %u", size); 685e76a89eeS[email protected] 6867856c818Smatthias.ringwald // get info 6877856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 6885061f3afS[email protected] hci_connection_t *conn = hci_connection_for_handle(con_handle); 6897856c818Smatthias.ringwald uint8_t acl_flags = READ_ACL_FLAGS(packet); 6907856c818Smatthias.ringwald uint16_t acl_length = READ_ACL_LENGTH(packet); 6917856c818Smatthias.ringwald 6927856c818Smatthias.ringwald // ignore non-registered handle 6937856c818Smatthias.ringwald if (!conn){ 6949da54300S[email protected] log_error( "hci.c: acl_handler called with non-registered handle %u!" , con_handle); 6957856c818Smatthias.ringwald return; 6967856c818Smatthias.ringwald } 6977856c818Smatthias.ringwald 698e76a89eeS[email protected] // assert packet is complete 6999ecc3e17S[email protected] if (acl_length + 4 != size){ 700f04a0c31SMatthias Ringwald log_error("hci.c: acl_handler called with ACL packet of wrong size %d, expected %u => dropping packet", size, acl_length + 4); 701e76a89eeS[email protected] return; 702e76a89eeS[email protected] } 703e76a89eeS[email protected] 70452db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 7057856c818Smatthias.ringwald // update idle timestamp 7067856c818Smatthias.ringwald hci_connection_timestamp(conn); 70752db98b2SMatthias Ringwald #endif 7087856c818Smatthias.ringwald 709*2b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 710*2b838201SMatthias Ringwald hci_stack->host_completed_packets = 1; 711*2b838201SMatthias Ringwald conn->num_packets_completed++; 712*2b838201SMatthias Ringwald #endif 713*2b838201SMatthias Ringwald 7147856c818Smatthias.ringwald // handle different packet types 7157856c818Smatthias.ringwald switch (acl_flags & 0x03) { 7167856c818Smatthias.ringwald 7177856c818Smatthias.ringwald case 0x01: // continuation fragment 7187856c818Smatthias.ringwald 7190ca847afS[email protected] // sanity checks 7207856c818Smatthias.ringwald if (conn->acl_recombination_pos == 0) { 7219da54300S[email protected] log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x", con_handle); 7227856c818Smatthias.ringwald return; 7237856c818Smatthias.ringwald } 7240ca847afS[email protected] if (conn->acl_recombination_pos + acl_length > 4 + HCI_ACL_BUFFER_SIZE){ 7250ca847afS[email protected] log_error( "ACL Cont Fragment to large: combined packet %u > buffer size %u for handle 0x%02x", 7260ca847afS[email protected] conn->acl_recombination_pos + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle); 7270ca847afS[email protected] conn->acl_recombination_pos = 0; 7280ca847afS[email protected] return; 7290ca847afS[email protected] } 7307856c818Smatthias.ringwald 7317856c818Smatthias.ringwald // append fragment payload (header already stored) 732ec6321eeS[email protected] memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + conn->acl_recombination_pos], &packet[4], acl_length ); 7337856c818Smatthias.ringwald conn->acl_recombination_pos += acl_length; 7347856c818Smatthias.ringwald 7359da54300S[email protected] // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u", acl_length, 736decc01a8Smatthias.ringwald // conn->acl_recombination_pos, conn->acl_recombination_length); 7377856c818Smatthias.ringwald 7387856c818Smatthias.ringwald // forward complete L2CAP packet if complete. 7397856c818Smatthias.ringwald if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header 740d6b06661SMatthias Ringwald hci_emit_acl_packet(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], conn->acl_recombination_pos); 7417856c818Smatthias.ringwald // reset recombination buffer 7427856c818Smatthias.ringwald conn->acl_recombination_length = 0; 7437856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 7447856c818Smatthias.ringwald } 7457856c818Smatthias.ringwald break; 7467856c818Smatthias.ringwald 7477856c818Smatthias.ringwald case 0x02: { // first fragment 7487856c818Smatthias.ringwald 74923a77e1aS[email protected] // sanity check 75023a77e1aS[email protected] if (conn->acl_recombination_pos) { 75123a77e1aS[email protected] log_error( "ACL First Fragment but data in buffer for handle 0x%02x, dropping stale fragments", con_handle); 75223a77e1aS[email protected] conn->acl_recombination_pos = 0; 75323a77e1aS[email protected] } 75423a77e1aS[email protected] 7557856c818Smatthias.ringwald // peek into L2CAP packet! 7567856c818Smatthias.ringwald uint16_t l2cap_length = READ_L2CAP_LENGTH( packet ); 7577856c818Smatthias.ringwald 7589da54300S[email protected] // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u", acl_length, l2cap_length); 759decc01a8Smatthias.ringwald 7607856c818Smatthias.ringwald // compare fragment size to L2CAP packet size 7617856c818Smatthias.ringwald if (acl_length >= l2cap_length + 4){ 7627856c818Smatthias.ringwald // forward fragment as L2CAP packet 763d6b06661SMatthias Ringwald hci_emit_acl_packet(packet, acl_length + 4); 7647856c818Smatthias.ringwald } else { 7650ca847afS[email protected] 7660ca847afS[email protected] if (acl_length > HCI_ACL_BUFFER_SIZE){ 7670ca847afS[email protected] log_error( "ACL First Fragment to large: fragment %u > buffer size %u for handle 0x%02x", 7680ca847afS[email protected] 4 + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle); 7690ca847afS[email protected] return; 7700ca847afS[email protected] } 7710ca847afS[email protected] 7727856c818Smatthias.ringwald // store first fragment and tweak acl length for complete package 773ec6321eeS[email protected] memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], packet, acl_length + 4); 7747856c818Smatthias.ringwald conn->acl_recombination_pos = acl_length + 4; 7757856c818Smatthias.ringwald conn->acl_recombination_length = l2cap_length; 776f8fbdce0SMatthias Ringwald little_endian_store_16(conn->acl_recombination_buffer, HCI_INCOMING_PRE_BUFFER_SIZE + 2, l2cap_length +4); 7777856c818Smatthias.ringwald } 7787856c818Smatthias.ringwald break; 7797856c818Smatthias.ringwald 7807856c818Smatthias.ringwald } 7817856c818Smatthias.ringwald default: 7829da54300S[email protected] log_error( "hci.c: acl_handler called with invalid packet boundary flags %u", acl_flags & 0x03); 7837856c818Smatthias.ringwald return; 7847856c818Smatthias.ringwald } 78594ab26f8Smatthias.ringwald 78694ab26f8Smatthias.ringwald // execute main loop 78794ab26f8Smatthias.ringwald hci_run(); 78816833f0aSmatthias.ringwald } 78922909952Smatthias.ringwald 79067a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){ 7919da54300S[email protected] log_info("Connection closed: handle 0x%x, %s", conn->con_handle, bd_addr_to_str(conn->address)); 7923c4d4b90Smatthias.ringwald 793ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 794ee752bb8SMatthias Ringwald int addr_type = conn->address_type; 795ee752bb8SMatthias Ringwald #endif 796ee752bb8SMatthias Ringwald 797528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&conn->timeout); 798c785ef68Smatthias.ringwald 799665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 800a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 8013c4d4b90Smatthias.ringwald 8023c4d4b90Smatthias.ringwald // now it's gone 803c7e0c5f6Smatthias.ringwald hci_emit_nr_connections_changed(); 804ee752bb8SMatthias Ringwald 805034e9b53SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 806ee752bb8SMatthias Ringwald // update SCO 807ee752bb8SMatthias Ringwald if (addr_type == BD_ADDR_TYPE_SCO && hci_stack->hci_transport && hci_stack->hci_transport->set_sco_config){ 808ee752bb8SMatthias Ringwald hci_stack->hci_transport->set_sco_config(hci_stack->sco_voice_setting_active, hci_number_sco_connections()); 809ee752bb8SMatthias Ringwald } 810034e9b53SMatthias Ringwald #endif 811c7e0c5f6Smatthias.ringwald } 812c7e0c5f6Smatthias.ringwald 81335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 81435454696SMatthias Ringwald 8150c042179S[email protected] static const uint16_t packet_type_sizes[] = { 8168f8108aaSmatthias.ringwald 0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE, 8178f8108aaSmatthias.ringwald HCI_ACL_DH1_SIZE, 0, 0, 0, 8188f8108aaSmatthias.ringwald HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE, 8198f8108aaSmatthias.ringwald HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE 8208f8108aaSmatthias.ringwald }; 82165389bfcS[email protected] static const uint8_t packet_type_feature_requirement_bit[] = { 82265389bfcS[email protected] 0, // 3 slot packets 82365389bfcS[email protected] 1, // 5 slot packets 82465389bfcS[email protected] 25, // EDR 2 mpbs 82565389bfcS[email protected] 26, // EDR 3 mbps 82665389bfcS[email protected] 39, // 3 slot EDR packts 82765389bfcS[email protected] 40, // 5 slot EDR packet 82865389bfcS[email protected] }; 82965389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = { 83065389bfcS[email protected] 0x0f00, // 3 slot packets 83165389bfcS[email protected] 0xf000, // 5 slot packets 83265389bfcS[email protected] 0x1102, // EDR 2 mpbs 83365389bfcS[email protected] 0x2204, // EDR 3 mbps 83465389bfcS[email protected] 0x0300, // 3 slot EDR packts 83565389bfcS[email protected] 0x3000, // 5 slot EDR packet 83665389bfcS[email protected] }; 8378f8108aaSmatthias.ringwald 83865389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){ 83965389bfcS[email protected] // enable packet types based on size 8408f8108aaSmatthias.ringwald uint16_t packet_types = 0; 841f16a69bbS[email protected] unsigned int i; 8428f8108aaSmatthias.ringwald for (i=0;i<16;i++){ 8438f8108aaSmatthias.ringwald if (packet_type_sizes[i] == 0) continue; 8448f8108aaSmatthias.ringwald if (packet_type_sizes[i] <= buffer_size){ 8458f8108aaSmatthias.ringwald packet_types |= 1 << i; 8468f8108aaSmatthias.ringwald } 8478f8108aaSmatthias.ringwald } 84865389bfcS[email protected] // disable packet types due to missing local supported features 84965389bfcS[email protected] for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){ 850f04a0c31SMatthias Ringwald unsigned int bit_idx = packet_type_feature_requirement_bit[i]; 85165389bfcS[email protected] int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0; 85265389bfcS[email protected] if (feature_set) continue; 85365389bfcS[email protected] log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]); 85465389bfcS[email protected] packet_types &= ~packet_type_feature_packet_mask[i]; 85565389bfcS[email protected] } 8568f8108aaSmatthias.ringwald // flip bits for "may not be used" 8578f8108aaSmatthias.ringwald packet_types ^= 0x3306; 8588f8108aaSmatthias.ringwald return packet_types; 8598f8108aaSmatthias.ringwald } 8608f8108aaSmatthias.ringwald 8618f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){ 8623a9fb326S[email protected] return hci_stack->packet_types; 8638f8108aaSmatthias.ringwald } 86435454696SMatthias Ringwald #endif 8658f8108aaSmatthias.ringwald 866facf93fdS[email protected] uint8_t* hci_get_outgoing_packet_buffer(void){ 8677dc17943Smatthias.ringwald // hci packet buffer is >= acl data packet length 8683a9fb326S[email protected] return hci_stack->hci_packet_buffer; 8697dc17943Smatthias.ringwald } 8707dc17943Smatthias.ringwald 871f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){ 8723a9fb326S[email protected] return hci_stack->acl_data_packet_length; 8737dc17943Smatthias.ringwald } 8747dc17943Smatthias.ringwald 87506b9e820SMatthias Ringwald #ifdef ENABLE_CLASSIC 8763e68d23dSMatthias Ringwald int hci_extended_sco_link_supported(void){ 8773e68d23dSMatthias Ringwald // No. 31, byte 3, bit 7 8783e68d23dSMatthias Ringwald return (hci_stack->local_supported_features[3] & (1 << 7)) != 0; 8793e68d23dSMatthias Ringwald } 88006b9e820SMatthias Ringwald #endif 8813e68d23dSMatthias Ringwald 8826ac9a97eS[email protected] int hci_non_flushable_packet_boundary_flag_supported(void){ 8836ac9a97eS[email protected] // No. 54, byte 6, bit 6 8846ac9a97eS[email protected] return (hci_stack->local_supported_features[6] & (1 << 6)) != 0; 8856ac9a97eS[email protected] } 8866ac9a97eS[email protected] 88715a95bd5SMatthias Ringwald static int gap_ssp_supported(void){ 8886ac9a97eS[email protected] // No. 51, byte 6, bit 3 8893a9fb326S[email protected] return (hci_stack->local_supported_features[6] & (1 << 3)) != 0; 890f5d8d141S[email protected] } 891f5d8d141S[email protected] 8927f02f414SMatthias Ringwald static int hci_classic_supported(void){ 89335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 8946ac9a97eS[email protected] // No. 37, byte 4, bit 5, = No BR/EDR Support 8953a9fb326S[email protected] return (hci_stack->local_supported_features[4] & (1 << 5)) == 0; 89635454696SMatthias Ringwald #else 89735454696SMatthias Ringwald return 0; 89835454696SMatthias Ringwald #endif 899f5d8d141S[email protected] } 900f5d8d141S[email protected] 9017f02f414SMatthias Ringwald static int hci_le_supported(void){ 902a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 9036ac9a97eS[email protected] // No. 37, byte 4, bit 6 = LE Supported (Controller) 9043a9fb326S[email protected] return (hci_stack->local_supported_features[4] & (1 << 6)) != 0; 905f5d8d141S[email protected] #else 906f5d8d141S[email protected] return 0; 907f5d8d141S[email protected] #endif 908f5d8d141S[email protected] } 909f5d8d141S[email protected] 910b95a5a35SMatthias Ringwald #ifdef ENABLE_BLE 911b95a5a35SMatthias Ringwald 912b95a5a35SMatthias Ringwald /** 913b95a5a35SMatthias Ringwald * @brief Get addr type and address used for LE in Advertisements, Scan Responses, 914b95a5a35SMatthias Ringwald */ 915b95a5a35SMatthias Ringwald void gap_le_get_own_address(uint8_t * addr_type, bd_addr_t addr){ 916b95a5a35SMatthias Ringwald *addr_type = hci_stack->le_own_addr_type; 917b95a5a35SMatthias Ringwald if (hci_stack->le_own_addr_type){ 918b95a5a35SMatthias Ringwald memcpy(addr, hci_stack->le_random_address, 6); 91969a97523S[email protected] } else { 9203a9fb326S[email protected] memcpy(addr, hci_stack->local_bd_addr, 6); 92169a97523S[email protected] } 92269a97523S[email protected] } 92369a97523S[email protected] 924e8c8828eSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 92539677e66SMatthias Ringwald void le_handle_advertisement_report(uint8_t *packet, int size){ 9269ec2630cSMatthias Ringwald 9279ec2630cSMatthias Ringwald UNUSED(size); 9289ec2630cSMatthias Ringwald 929d1dc057bS[email protected] int offset = 3; 930d1dc057bS[email protected] int num_reports = packet[offset]; 931d1dc057bS[email protected] offset += 1; 932d1dc057bS[email protected] 93357c9da5bS[email protected] int i; 9344f4e0224SMatthias Ringwald // log_info("HCI: handle adv report with num reports: %d", num_reports); 93503fbe9c6S[email protected] uint8_t event[12 + LE_ADVERTISING_DATA_SIZE]; // use upper bound to avoid var size automatic var 93657c9da5bS[email protected] for (i=0; i<num_reports;i++){ 937210c6774S[email protected] uint8_t data_length = packet[offset + 8]; 938a0aac9c4S[email protected] uint8_t event_size = 10 + data_length; 939d1dc057bS[email protected] int pos = 0; 940045013feSMatthias Ringwald event[pos++] = GAP_EVENT_ADVERTISING_REPORT; 94157c9da5bS[email protected] event[pos++] = event_size; 942210c6774S[email protected] memcpy(&event[pos], &packet[offset], 1+1+6); // event type + address type + address 943d1dc057bS[email protected] offset += 8; 944d1dc057bS[email protected] pos += 8; 945d1dc057bS[email protected] event[pos++] = packet[offset + 1 + data_length]; // rssi 946d1dc057bS[email protected] event[pos++] = packet[offset++]; //data_length; 947d1dc057bS[email protected] memcpy(&event[pos], &packet[offset], data_length); 94857c9da5bS[email protected] pos += data_length; 949d1dc057bS[email protected] offset += data_length + 1; // rssi 950d6b06661SMatthias Ringwald hci_emit_event(event, pos, 1); 95157c9da5bS[email protected] } 95257c9da5bS[email protected] } 953b2f949feS[email protected] #endif 954e8c8828eSMatthias Ringwald #endif 95557c9da5bS[email protected] 956cd77dd38SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 95706b9e820SMatthias Ringwald 95896b53536SMatthias Ringwald static uint32_t hci_transport_uart_get_main_baud_rate(void){ 95996b53536SMatthias Ringwald if (!hci_stack->config) return 0; 9609796ebeaSMatthias Ringwald uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 96196b53536SMatthias Ringwald // Limit baud rate for Broadcom chipsets to 3 mbps 96261f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION && baud_rate > 3000000){ 96396b53536SMatthias Ringwald baud_rate = 3000000; 96496b53536SMatthias Ringwald } 96596b53536SMatthias Ringwald return baud_rate; 96696b53536SMatthias Ringwald } 96796b53536SMatthias Ringwald 968ec820d77SMatthias Ringwald static void hci_initialization_timeout_handler(btstack_timer_source_t * ds){ 9699ec2630cSMatthias Ringwald UNUSED(ds); 9709ec2630cSMatthias Ringwald 9710305bdeaSMatthias Ringwald switch (hci_stack->substate){ 9720305bdeaSMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 9737b0d7667SMatthias Ringwald log_info("Resend HCI Reset"); 9740305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET; 9757b0d7667SMatthias Ringwald hci_stack->num_cmd_packets = 1; 9760305bdeaSMatthias Ringwald hci_run(); 9770305bdeaSMatthias Ringwald break; 9789f007422SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET: 9799f007422SMatthias Ringwald log_info("Resend HCI Reset - CSR Warm Boot with Link Reset"); 9809f007422SMatthias Ringwald if (hci_stack->hci_transport->reset_link){ 9819f007422SMatthias Ringwald hci_stack->hci_transport->reset_link(); 9829f007422SMatthias Ringwald } 983202c8a4cSMatthias Ringwald // no break - explicit fallthrough to HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT 984e47e68c7SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT: 985e47e68c7SMatthias Ringwald log_info("Resend HCI Reset - CSR Warm Boot"); 986e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT; 987e47e68c7SMatthias Ringwald hci_stack->num_cmd_packets = 1; 988e47e68c7SMatthias Ringwald hci_run(); 989688c2635SMatthias Ringwald break; 9907224be7eSMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE: 9917224be7eSMatthias Ringwald if (hci_stack->hci_transport->set_baudrate){ 99296b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 993cd724cb7SMatthias Ringwald log_info("Local baud rate change to %"PRIu32"(timeout handler)", baud_rate); 9947dd9d0ecSMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 9957224be7eSMatthias Ringwald } 996772a36d3SMatthias Ringwald // For CSR, HCI Reset is sent on new baud rate 99761f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO){ 998772a36d3SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT; 999772a36d3SMatthias Ringwald hci_run(); 1000772a36d3SMatthias Ringwald } 10014696bddbSMatthias Ringwald break; 1002559961d0SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_BCM_DELAY: 1003559961d0SMatthias Ringwald // otherwise continue 1004559961d0SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 1005559961d0SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 1006559961d0SMatthias Ringwald break; 10070305bdeaSMatthias Ringwald default: 10080305bdeaSMatthias Ringwald break; 10090305bdeaSMatthias Ringwald } 10100305bdeaSMatthias Ringwald } 101106b9e820SMatthias Ringwald #endif 10120305bdeaSMatthias Ringwald 101371de195eSMatthias Ringwald static void hci_initializing_next_state(void){ 101474b323a9SMatthias Ringwald hci_stack->substate = (hci_substate_t )( ((int) hci_stack->substate) + 1); 101574b323a9SMatthias Ringwald } 101674b323a9SMatthias Ringwald 101774b323a9SMatthias Ringwald // assumption: hci_can_send_command_packet_now() == true 101871de195eSMatthias Ringwald static void hci_initializing_run(void){ 1019148ca237SMatthias Ringwald log_debug("hci_initializing_run: substate %u, can send %u", hci_stack->substate, hci_can_send_command_packet_now()); 102074b323a9SMatthias Ringwald switch (hci_stack->substate){ 102174b323a9SMatthias Ringwald case HCI_INIT_SEND_RESET: 102274b323a9SMatthias Ringwald hci_state_reset(); 1023a0cf2f3fSMatthias Ringwald 102406b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 10250305bdeaSMatthias Ringwald // prepare reset if command complete not received in 100ms 1026659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1027528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1028528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1029a0cf2f3fSMatthias Ringwald #endif 10300305bdeaSMatthias Ringwald // send command 103174b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 10320305bdeaSMatthias Ringwald hci_send_cmd(&hci_reset); 103374b323a9SMatthias Ringwald break; 103476fcb19bSMatthias Ringwald case HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION: 103576fcb19bSMatthias Ringwald hci_send_cmd(&hci_read_local_version_information); 103676fcb19bSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION; 103776fcb19bSMatthias Ringwald break; 1038e90bae01SMatthias Ringwald case HCI_INIT_SEND_READ_LOCAL_NAME: 1039e90bae01SMatthias Ringwald hci_send_cmd(&hci_read_local_name); 1040e90bae01SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_NAME; 1041e90bae01SMatthias Ringwald break; 104206b9e820SMatthias Ringwald 104306b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 1044e47e68c7SMatthias Ringwald case HCI_INIT_SEND_RESET_CSR_WARM_BOOT: 1045e47e68c7SMatthias Ringwald hci_state_reset(); 1046e47e68c7SMatthias Ringwald // prepare reset if command complete not received in 100ms 1047659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1048528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1049528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1050e47e68c7SMatthias Ringwald // send command 1051e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT; 1052e47e68c7SMatthias Ringwald hci_send_cmd(&hci_reset); 1053e47e68c7SMatthias Ringwald break; 10548d29070eSMatthias Ringwald case HCI_INIT_SEND_RESET_ST_WARM_BOOT: 10558d29070eSMatthias Ringwald hci_state_reset(); 10568d29070eSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT; 10578d29070eSMatthias Ringwald hci_send_cmd(&hci_reset); 10588d29070eSMatthias Ringwald break; 1059fab26ab3SMatthias Ringwald case HCI_INIT_SEND_BAUD_CHANGE: { 106096b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 10613fb36a29SMatthias Ringwald hci_stack->chipset->set_baudrate_command(baud_rate, hci_stack->hci_packet_buffer); 1062f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 106374b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE; 10640305bdeaSMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]); 10654696bddbSMatthias Ringwald // STLC25000D: baudrate change happens within 0.5 s after command was send, 10664696bddbSMatthias Ringwald // use timer to update baud rate after 100 ms (knowing exactly, when command was sent is non-trivial) 106761f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS){ 1068659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1069528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 10704696bddbSMatthias Ringwald } 107174b323a9SMatthias Ringwald break; 1072fab26ab3SMatthias Ringwald } 1073fab26ab3SMatthias Ringwald case HCI_INIT_SEND_BAUD_CHANGE_BCM: { 107496b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 10753fb36a29SMatthias Ringwald hci_stack->chipset->set_baudrate_command(baud_rate, hci_stack->hci_packet_buffer); 1076f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 1077eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE_BCM; 1078eb3a5314SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]); 1079eb3a5314SMatthias Ringwald break; 1080fab26ab3SMatthias Ringwald } 108174b323a9SMatthias Ringwald case HCI_INIT_CUSTOM_INIT: 108274b323a9SMatthias Ringwald // Custom initialization 10833fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->next_command){ 10843fb36a29SMatthias Ringwald int valid_cmd = (*hci_stack->chipset->next_command)(hci_stack->hci_packet_buffer); 108574b323a9SMatthias Ringwald if (valid_cmd){ 108674b323a9SMatthias Ringwald int size = 3 + hci_stack->hci_packet_buffer[2]; 1087f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 108874b323a9SMatthias Ringwald hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, hci_stack->hci_packet_buffer, size); 1089e47e68c7SMatthias Ringwald switch (valid_cmd) { 1090e47e68c7SMatthias Ringwald case 1: 1091e47e68c7SMatthias Ringwald default: 1092e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT; 1093e47e68c7SMatthias Ringwald break; 1094e47e68c7SMatthias Ringwald case 2: // CSR Warm Boot: Wait a bit, then send HCI Reset until HCI Command Complete 1095e47e68c7SMatthias Ringwald log_info("CSR Warm Boot"); 1096659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1097528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1098528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 109961f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO 1100772a36d3SMatthias Ringwald && hci_stack->config 11013fb36a29SMatthias Ringwald && hci_stack->chipset 11023fb36a29SMatthias Ringwald // && hci_stack->chipset->set_baudrate_command -- there's no such command 1103772a36d3SMatthias Ringwald && hci_stack->hci_transport->set_baudrate 11042caefae9SMatthias Ringwald && hci_transport_uart_get_main_baud_rate()){ 1105772a36d3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE; 1106772a36d3SMatthias Ringwald } else { 11079f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET; 1108772a36d3SMatthias Ringwald } 1109e47e68c7SMatthias Ringwald break; 1110e47e68c7SMatthias Ringwald } 11110305bdeaSMatthias Ringwald hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size); 111274b323a9SMatthias Ringwald break; 111374b323a9SMatthias Ringwald } 1114148ca237SMatthias Ringwald log_info("Init script done"); 111592a0d36dSMatthias Ringwald 1116559961d0SMatthias Ringwald // Init script download on Broadcom chipsets causes: 111761f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION){ 1118559961d0SMatthias Ringwald // - baud rate to reset, restore UART baud rate if needed 111992a0d36dSMatthias Ringwald int need_baud_change = hci_stack->config 11203fb36a29SMatthias Ringwald && hci_stack->chipset 11213fb36a29SMatthias Ringwald && hci_stack->chipset->set_baudrate_command 112292a0d36dSMatthias Ringwald && hci_stack->hci_transport->set_baudrate 11239796ebeaSMatthias Ringwald && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 112492a0d36dSMatthias Ringwald if (need_baud_change) { 11259796ebeaSMatthias Ringwald uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_init; 1126cd724cb7SMatthias Ringwald log_info("Local baud rate change to %"PRIu32" after init script (bcm)", baud_rate); 112792a0d36dSMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 112892a0d36dSMatthias Ringwald } 1129559961d0SMatthias Ringwald 1130559961d0SMatthias Ringwald // - RTS will raise during update, but manual RTS/CTS in WICED port on RedBear Duo cannot handle this 1131559961d0SMatthias Ringwald // -> Work around: wait a few milliseconds here. 1132559961d0SMatthias Ringwald log_info("BCM delay after init script"); 1133559961d0SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_BCM_DELAY; 1134559961d0SMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, 10); 1135559961d0SMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1136559961d0SMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1137559961d0SMatthias Ringwald break; 113892a0d36dSMatthias Ringwald } 113974b323a9SMatthias Ringwald } 114074b323a9SMatthias Ringwald // otherwise continue 1141a828a756SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 1142a828a756SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 1143a828a756SMatthias Ringwald break; 114453860077SMatthias Ringwald case HCI_INIT_SET_BD_ADDR: 114553860077SMatthias Ringwald log_info("Set Public BD ADDR to %s", bd_addr_to_str(hci_stack->custom_bd_addr)); 11463fb36a29SMatthias Ringwald hci_stack->chipset->set_bd_addr_command(hci_stack->custom_bd_addr, hci_stack->hci_packet_buffer); 1147f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 114853860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_BD_ADDR; 114953860077SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]); 115053860077SMatthias Ringwald break; 115106b9e820SMatthias Ringwald #endif 115206b9e820SMatthias Ringwald 115306b9e820SMatthias Ringwald case HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS: 115406b9e820SMatthias Ringwald log_info("Resend hci_read_local_supported_commands after CSR Warm Boot double reset"); 115506b9e820SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 115606b9e820SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 115706b9e820SMatthias Ringwald break; 115853860077SMatthias Ringwald case HCI_INIT_READ_BD_ADDR: 115953860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_BD_ADDR; 116053860077SMatthias Ringwald hci_send_cmd(&hci_read_bd_addr); 116153860077SMatthias Ringwald break; 116274b323a9SMatthias Ringwald case HCI_INIT_READ_BUFFER_SIZE: 116374b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_BUFFER_SIZE; 11640305bdeaSMatthias Ringwald hci_send_cmd(&hci_read_buffer_size); 116574b323a9SMatthias Ringwald break; 116653860077SMatthias Ringwald case HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES: 116753860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_FEATURES; 11680305bdeaSMatthias Ringwald hci_send_cmd(&hci_read_local_supported_features); 116974b323a9SMatthias Ringwald break; 1170*2b838201SMatthias Ringwald 1171*2b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 1172*2b838201SMatthias Ringwald case HCI_INIT_SET_CONTROLLER_TO_HOST_FLOW_CONTROL: 1173*2b838201SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_CONTROLLER_TO_HOST_FLOW_CONTROL; 1174*2b838201SMatthias Ringwald hci_send_cmd(&hci_set_controller_to_host_flow_control, 3); // ACL + SCO Flow Control 1175*2b838201SMatthias Ringwald break; 1176*2b838201SMatthias Ringwald case HCI_INIT_HOST_BUFFER_SIZE: 1177*2b838201SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_HOST_BUFFER_SIZE; 1178*2b838201SMatthias Ringwald hci_send_cmd(&hci_host_buffer_size, HCI_HOST_ACL_PACKET_LEN, HCI_HOST_SCO_PACKET_LEN, 1179*2b838201SMatthias Ringwald HCI_HOST_ACL_PACKET_NUM, HCI_HOST_SCO_PACKET_NUM); 1180*2b838201SMatthias Ringwald break; 1181*2b838201SMatthias Ringwald #endif 1182*2b838201SMatthias Ringwald 118374b323a9SMatthias Ringwald case HCI_INIT_SET_EVENT_MASK: 11840305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_EVENT_MASK; 118574b323a9SMatthias Ringwald if (hci_le_supported()){ 118674b323a9SMatthias Ringwald hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF); 118774b323a9SMatthias Ringwald } else { 118874b323a9SMatthias Ringwald // Kensington Bluetooth 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff... 118974b323a9SMatthias Ringwald hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF); 119074b323a9SMatthias Ringwald } 119174b323a9SMatthias Ringwald break; 1192*2b838201SMatthias Ringwald 119335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 119474b323a9SMatthias Ringwald case HCI_INIT_WRITE_SIMPLE_PAIRING_MODE: 119574b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SIMPLE_PAIRING_MODE; 11960305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable); 119774b323a9SMatthias Ringwald break; 119874b323a9SMatthias Ringwald case HCI_INIT_WRITE_PAGE_TIMEOUT: 119974b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_PAGE_TIMEOUT; 12000305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_page_timeout, 0x6000); // ca. 15 sec 120174b323a9SMatthias Ringwald break; 120274b323a9SMatthias Ringwald case HCI_INIT_WRITE_CLASS_OF_DEVICE: 120374b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_CLASS_OF_DEVICE; 12040305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device); 120574b323a9SMatthias Ringwald break; 120674b323a9SMatthias Ringwald case HCI_INIT_WRITE_LOCAL_NAME: 12070305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_LOCAL_NAME; 120874b323a9SMatthias Ringwald if (hci_stack->local_name){ 120974b323a9SMatthias Ringwald hci_send_cmd(&hci_write_local_name, hci_stack->local_name); 121074b323a9SMatthias Ringwald } else { 12117224be7eSMatthias Ringwald char local_name[8+17+1]; 12127224be7eSMatthias Ringwald // BTstack 11:22:33:44:55:66 12137224be7eSMatthias Ringwald memcpy(local_name, "BTstack ", 8); 12147224be7eSMatthias Ringwald memcpy(&local_name[8], bd_addr_to_str(hci_stack->local_bd_addr), 17); // strlen(bd_addr_to_str(...)) = 17 12157224be7eSMatthias Ringwald local_name[8+17] = '\0'; 121693bcd4d0SMatthias Ringwald log_info("---> Name %s", local_name); 121793bcd4d0SMatthias Ringwald hci_send_cmd(&hci_write_local_name, local_name); 121874b323a9SMatthias Ringwald } 121974b323a9SMatthias Ringwald break; 1220ff00ed1cSMatthias Ringwald case HCI_INIT_WRITE_EIR_DATA: 12218a114470SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_EIR_DATA; 1222ff00ed1cSMatthias Ringwald hci_send_cmd(&hci_write_extended_inquiry_response, 0, hci_stack->eir_data); 1223ff00ed1cSMatthias Ringwald break; 1224f6858d14SMatthias Ringwald case HCI_INIT_WRITE_INQUIRY_MODE: 1225f6858d14SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_INQUIRY_MODE; 12268a114470SMatthias Ringwald hci_send_cmd(&hci_write_inquiry_mode, (int) hci_stack->inquiry_mode); 1227f6858d14SMatthias Ringwald break; 122874b323a9SMatthias Ringwald case HCI_INIT_WRITE_SCAN_ENABLE: 122974b323a9SMatthias Ringwald hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan 123074b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SCAN_ENABLE; 123174b323a9SMatthias Ringwald break; 1232483c5078SMatthias Ringwald // only sent if ENABLE_SCO_OVER_HCI is defined 1233729ed62eSMatthias Ringwald case HCI_INIT_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE: 1234729ed62eSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE; 1235729ed62eSMatthias Ringwald hci_send_cmd(&hci_write_synchronous_flow_control_enable, 1); // SCO tracking enabled 1236729ed62eSMatthias Ringwald break; 1237483c5078SMatthias Ringwald case HCI_INIT_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING: 1238483c5078SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING; 1239483c5078SMatthias Ringwald hci_send_cmd(&hci_write_default_erroneous_data_reporting, 1); 1240483c5078SMatthias Ringwald break; 1241a42798c3SMatthias Ringwald // only sent if ENABLE_SCO_OVER_HCI and manufacturer is Broadcom 1242a42798c3SMatthias Ringwald case HCI_INIT_BCM_WRITE_SCO_PCM_INT: 1243a42798c3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT; 1244a42798c3SMatthias Ringwald log_info("BCM: Route SCO data via HCI transport"); 1245a42798c3SMatthias Ringwald hci_send_cmd(&hci_bcm_write_sco_pcm_int, 1, 0, 0, 0, 0); 1246a42798c3SMatthias Ringwald break; 1247a42798c3SMatthias Ringwald 124835454696SMatthias Ringwald #endif 1249a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 125074b323a9SMatthias Ringwald // LE INIT 125174b323a9SMatthias Ringwald case HCI_INIT_LE_READ_BUFFER_SIZE: 125274b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_READ_BUFFER_SIZE; 12530305bdeaSMatthias Ringwald hci_send_cmd(&hci_le_read_buffer_size); 125474b323a9SMatthias Ringwald break; 125574b323a9SMatthias Ringwald case HCI_INIT_WRITE_LE_HOST_SUPPORTED: 125674b323a9SMatthias Ringwald // LE Supported Host = 1, Simultaneous Host = 0 125774b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_LE_HOST_SUPPORTED; 12580305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_le_host_supported, 1, 0); 125974b323a9SMatthias Ringwald break; 1260b95a5a35SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 12613b6d4121SMatthias Ringwald case HCI_INIT_READ_WHITE_LIST_SIZE: 12623b6d4121SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_WHITE_LIST_SIZE; 12633b6d4121SMatthias Ringwald hci_send_cmd(&hci_le_read_white_list_size); 12643b6d4121SMatthias Ringwald break; 126574b323a9SMatthias Ringwald case HCI_INIT_LE_SET_SCAN_PARAMETERS: 1266b95a5a35SMatthias Ringwald // LE Scan Parameters: active scanning, 300 ms interval, 30 ms window, own address type, accept all advs 126774b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_SET_SCAN_PARAMETERS; 1268b95a5a35SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_parameters, 1, 0x1e0, 0x30, hci_stack->le_own_addr_type, 0); 126974b323a9SMatthias Ringwald break; 127074b323a9SMatthias Ringwald #endif 1271b95a5a35SMatthias Ringwald #endif 127274b323a9SMatthias Ringwald default: 127374b323a9SMatthias Ringwald return; 127474b323a9SMatthias Ringwald } 127555975f88SMatthias Ringwald } 127655975f88SMatthias Ringwald 1277a650ba4dSMatthias Ringwald static void hci_init_done(void){ 1278a650ba4dSMatthias Ringwald // done. tell the app 1279a650ba4dSMatthias Ringwald log_info("hci_init_done -> HCI_STATE_WORKING"); 1280a650ba4dSMatthias Ringwald hci_stack->state = HCI_STATE_WORKING; 1281a650ba4dSMatthias Ringwald hci_emit_state(); 1282a650ba4dSMatthias Ringwald hci_run(); 1283a650ba4dSMatthias Ringwald } 1284a650ba4dSMatthias Ringwald 12856155b3d3S[email protected] static void hci_initializing_event_handler(uint8_t * packet, uint16_t size){ 12869ec2630cSMatthias Ringwald UNUSED(size); 12879ec2630cSMatthias Ringwald 1288a2481739S[email protected] uint8_t command_completed = 0; 1289c4633093SMatthias Ringwald 12900e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE){ 1291f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 12926155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 1293a2481739S[email protected] command_completed = 1; 1294148ca237SMatthias Ringwald log_debug("Command complete for expected opcode %04x at substate %u", opcode, hci_stack->substate); 12956155b3d3S[email protected] } else { 1296d58dd308SMatthias Ringwald log_info("Command complete for different opcode %04x, expected %04x, at substate %u", opcode, hci_stack->last_cmd_opcode, hci_stack->substate); 12976155b3d3S[email protected] } 12986155b3d3S[email protected] } 12990f97eae7SMatthias Ringwald 13000e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_STATUS){ 13016155b3d3S[email protected] uint8_t status = packet[2]; 1302f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,4); 13036155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 13046155b3d3S[email protected] if (status){ 1305a2481739S[email protected] command_completed = 1; 1306148ca237SMatthias Ringwald log_debug("Command status error 0x%02x for expected opcode %04x at substate %u", status, opcode, hci_stack->substate); 13076155b3d3S[email protected] } else { 13086155b3d3S[email protected] log_info("Command status OK for expected opcode %04x, waiting for command complete", opcode); 13096155b3d3S[email protected] } 13106155b3d3S[email protected] } else { 1311148ca237SMatthias Ringwald log_debug("Command status for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode); 13126155b3d3S[email protected] } 13136155b3d3S[email protected] } 13140f97eae7SMatthias Ringwald 131506b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 131606b9e820SMatthias Ringwald 1317e47e68c7SMatthias Ringwald // Vendor == CSR 13180e2df43fSMatthias Ringwald if (hci_stack->substate == HCI_INIT_W4_CUSTOM_INIT && hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC){ 1319e47e68c7SMatthias Ringwald // TODO: track actual command 1320e47e68c7SMatthias Ringwald command_completed = 1; 1321e47e68c7SMatthias Ringwald } 1322a2481739S[email protected] 13234e9daa6fSMatthias Ringwald // Vendor == Toshiba 13240e2df43fSMatthias Ringwald if (hci_stack->substate == HCI_INIT_W4_SEND_BAUD_CHANGE && hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC){ 13254e9daa6fSMatthias Ringwald // TODO: track actual command 13264e9daa6fSMatthias Ringwald command_completed = 1; 13274e9daa6fSMatthias Ringwald } 13284e9daa6fSMatthias Ringwald 13290f97eae7SMatthias Ringwald // Late response (> 100 ms) for HCI Reset e.g. on Toshiba TC35661: 13300f97eae7SMatthias Ringwald // Command complete for HCI Reset arrives after we've resent the HCI Reset command 13310f97eae7SMatthias Ringwald // 13320f97eae7SMatthias Ringwald // HCI Reset 13330f97eae7SMatthias Ringwald // Timeout 100 ms 13340f97eae7SMatthias Ringwald // HCI Reset 13350f97eae7SMatthias Ringwald // Command Complete Reset 13360f97eae7SMatthias Ringwald // HCI Read Local Version Information 13370f97eae7SMatthias Ringwald // Command Complete Reset - but we expected Command Complete Read Local Version Information 13380f97eae7SMatthias Ringwald // hang... 13390f97eae7SMatthias Ringwald // 13400f97eae7SMatthias Ringwald // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend 13410f97eae7SMatthias Ringwald if (!command_completed 13420e2df43fSMatthias Ringwald && hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE 13430f97eae7SMatthias Ringwald && hci_stack->substate == HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION){ 13440f97eae7SMatthias Ringwald 1345f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 13460f97eae7SMatthias Ringwald if (opcode == hci_reset.opcode){ 13470f97eae7SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION; 13480f97eae7SMatthias Ringwald return; 13490f97eae7SMatthias Ringwald } 13500f97eae7SMatthias Ringwald } 13510f97eae7SMatthias Ringwald 13529f007422SMatthias Ringwald // CSR & H5 13539f007422SMatthias Ringwald // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend 13549f007422SMatthias Ringwald if (!command_completed 13559f007422SMatthias Ringwald && hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE 13569f007422SMatthias Ringwald && hci_stack->substate == HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS){ 13579f007422SMatthias Ringwald 13589f007422SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 13599f007422SMatthias Ringwald if (opcode == hci_reset.opcode){ 13609f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS; 13619f007422SMatthias Ringwald return; 13629f007422SMatthias Ringwald } 13639f007422SMatthias Ringwald } 13649f007422SMatthias Ringwald 13659f007422SMatthias Ringwald // on CSR with BCSP/H5, the reset resend timeout leads to substate == HCI_INIT_SEND_RESET or HCI_INIT_SEND_RESET_CSR_WARM_BOOT 13669f007422SMatthias Ringwald // fix: Correct substate and behave as command below 13679f007422SMatthias Ringwald if (command_completed){ 13689f007422SMatthias Ringwald switch (hci_stack->substate){ 13699f007422SMatthias Ringwald case HCI_INIT_SEND_RESET: 13709f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 13719f007422SMatthias Ringwald break; 13729f007422SMatthias Ringwald case HCI_INIT_SEND_RESET_CSR_WARM_BOOT: 13739f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT; 13749f007422SMatthias Ringwald break; 13759f007422SMatthias Ringwald default: 13769f007422SMatthias Ringwald break; 13779f007422SMatthias Ringwald } 13789f007422SMatthias Ringwald } 13790f97eae7SMatthias Ringwald 138006b9e820SMatthias Ringwald #endif 13810f97eae7SMatthias Ringwald 1382a2481739S[email protected] if (!command_completed) return; 1383a2481739S[email protected] 138406b9e820SMatthias Ringwald int need_baud_change = 0; 138506b9e820SMatthias Ringwald int need_addr_change = 0; 138606b9e820SMatthias Ringwald 138706b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 138806b9e820SMatthias Ringwald need_baud_change = hci_stack->config 13893fb36a29SMatthias Ringwald && hci_stack->chipset 13903fb36a29SMatthias Ringwald && hci_stack->chipset->set_baudrate_command 1391db8bc6ffSMatthias Ringwald && hci_stack->hci_transport->set_baudrate 13929796ebeaSMatthias Ringwald && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 1393db8bc6ffSMatthias Ringwald 139406b9e820SMatthias Ringwald need_addr_change = hci_stack->custom_bd_addr_set 13953fb36a29SMatthias Ringwald && hci_stack->chipset 13963fb36a29SMatthias Ringwald && hci_stack->chipset->set_bd_addr_command; 139706b9e820SMatthias Ringwald #endif 1398a80162e9SMatthias Ringwald 13995c363727SMatthias Ringwald switch(hci_stack->substate){ 140006b9e820SMatthias Ringwald 140106b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 14029f007422SMatthias Ringwald case HCI_INIT_SEND_RESET: 1403d58dd308SMatthias Ringwald // on CSR with BCSP/H5, resend triggers resend of HCI Reset and leads to substate == HCI_INIT_SEND_RESET 14049f007422SMatthias Ringwald // fix: just correct substate and behave as command below 14059f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 14069f007422SMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 14079f007422SMatthias Ringwald break; 140874b323a9SMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 1409528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 141076fcb19bSMatthias Ringwald break; 1411e90bae01SMatthias Ringwald case HCI_INIT_W4_SEND_READ_LOCAL_NAME: 1412e90bae01SMatthias Ringwald log_info("Received local name, need baud change %d", need_baud_change); 14132f48d920SMatthias Ringwald if (need_baud_change){ 14142f48d920SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE; 14152f48d920SMatthias Ringwald return; 14162f48d920SMatthias Ringwald } 141753860077SMatthias Ringwald // skip baud change 141874b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 141974b323a9SMatthias Ringwald return; 1420a80162e9SMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE: 14214696bddbSMatthias Ringwald // for STLC2500D, baud rate change already happened. 1422fab26ab3SMatthias Ringwald // for others, baud rate gets changed now 142361f37892SMatthias Ringwald if ((hci_stack->manufacturer != BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS) && need_baud_change){ 142496b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1425cd724cb7SMatthias Ringwald log_info("Local baud rate change to %"PRIu32"(w4_send_baud_change)", baud_rate); 1426fab26ab3SMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 14274696bddbSMatthias Ringwald } 142874b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 142974b323a9SMatthias Ringwald return; 1430a80162e9SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT: 1431528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 1432a80162e9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 1433a80162e9SMatthias Ringwald return; 143474b323a9SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT: 143574b323a9SMatthias Ringwald // repeat custom init 143674b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 143774b323a9SMatthias Ringwald return; 143806b9e820SMatthias Ringwald #else 143906b9e820SMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 144006b9e820SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS; 144106b9e820SMatthias Ringwald return ; 144206b9e820SMatthias Ringwald #endif 144306b9e820SMatthias Ringwald 1444a828a756SMatthias Ringwald case HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS: 144561f37892SMatthias Ringwald if (need_baud_change && hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION){ 1446eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE_BCM; 1447eb3a5314SMatthias Ringwald return; 1448eb3a5314SMatthias Ringwald } 144953860077SMatthias Ringwald if (need_addr_change){ 145053860077SMatthias Ringwald hci_stack->substate = HCI_INIT_SET_BD_ADDR; 145153860077SMatthias Ringwald return; 145253860077SMatthias Ringwald } 145353860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 145453860077SMatthias Ringwald return; 145506b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 14567224be7eSMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE_BCM: 14577224be7eSMatthias Ringwald if (need_baud_change){ 145896b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1459cd724cb7SMatthias Ringwald log_info("Local baud rate change to %"PRIu32"(w4_send_baud_change_bcm))", baud_rate); 1460fab26ab3SMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 14617224be7eSMatthias Ringwald } 1462eb3a5314SMatthias Ringwald if (need_addr_change){ 1463eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_SET_BD_ADDR; 1464eb3a5314SMatthias Ringwald return; 1465eb3a5314SMatthias Ringwald } 1466eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 1467eb3a5314SMatthias Ringwald return; 146853860077SMatthias Ringwald case HCI_INIT_W4_SET_BD_ADDR: 146953860077SMatthias Ringwald // for STLC2500D, bd addr change only gets active after sending reset command 147061f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS){ 147153860077SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_ST_WARM_BOOT; 147253860077SMatthias Ringwald return; 147353860077SMatthias Ringwald } 147453860077SMatthias Ringwald // skipping st warm boot 147553860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 147653860077SMatthias Ringwald return; 147753860077SMatthias Ringwald case HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT: 147853860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 147953860077SMatthias Ringwald return; 148006b9e820SMatthias Ringwald #endif 148153860077SMatthias Ringwald case HCI_INIT_W4_READ_BD_ADDR: 148253860077SMatthias Ringwald // only read buffer size if supported 148353860077SMatthias Ringwald if (hci_stack->local_supported_commands[0] & 0x01) { 148453860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BUFFER_SIZE; 148553860077SMatthias Ringwald return; 148653860077SMatthias Ringwald } 148753860077SMatthias Ringwald // skipping read buffer size 148853860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES; 1489a828a756SMatthias Ringwald return; 149074b323a9SMatthias Ringwald case HCI_INIT_W4_SET_EVENT_MASK: 14916155b3d3S[email protected] // skip Classic init commands for LE only chipsets 14926155b3d3S[email protected] if (!hci_classic_supported()){ 14932c68f164SMatthias Ringwald #ifdef ENABLE_BLE 14946155b3d3S[email protected] if (hci_le_supported()){ 149574b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_LE_READ_BUFFER_SIZE; // skip all classic command 149674b323a9SMatthias Ringwald return; 14972c68f164SMatthias Ringwald } 14982c68f164SMatthias Ringwald #endif 14996155b3d3S[email protected] log_error("Neither BR/EDR nor LE supported"); 1500a650ba4dSMatthias Ringwald hci_init_done(); 150174b323a9SMatthias Ringwald return; 15026155b3d3S[email protected] } 150315a95bd5SMatthias Ringwald if (!gap_ssp_supported()){ 15045c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_WRITE_PAGE_TIMEOUT; 150574b323a9SMatthias Ringwald return; 15066155b3d3S[email protected] } 15076155b3d3S[email protected] break; 1508903ea03aSMatthias Ringwald #ifdef ENABLE_BLE 1509a828a756SMatthias Ringwald case HCI_INIT_W4_LE_READ_BUFFER_SIZE: 1510a828a756SMatthias Ringwald // skip write le host if not supported (e.g. on LE only EM9301) 1511a828a756SMatthias Ringwald if (hci_stack->local_supported_commands[0] & 0x02) break; 1512903ea03aSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 1513903ea03aSMatthias Ringwald hci_stack->substate = HCI_INIT_READ_WHITE_LIST_SIZE; 1514903ea03aSMatthias Ringwald #else 1515903ea03aSMatthias Ringwald hci_init_done(); 1516903ea03aSMatthias Ringwald #endif 1517a828a756SMatthias Ringwald return; 1518903ea03aSMatthias Ringwald #endif 1519ff00ed1cSMatthias Ringwald case HCI_INIT_W4_WRITE_LOCAL_NAME: 1520ff00ed1cSMatthias Ringwald // skip write eir data if no eir data set 1521ff00ed1cSMatthias Ringwald if (hci_stack->eir_data) break; 1522ff00ed1cSMatthias Ringwald hci_stack->substate = HCI_INIT_WRITE_INQUIRY_MODE; 1523ff00ed1cSMatthias Ringwald return; 1524729ed62eSMatthias Ringwald 152571c4de7aSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 1526729ed62eSMatthias Ringwald case HCI_INIT_W4_WRITE_SCAN_ENABLE: 15273905afbfSMatthias Ringwald // skip write synchronous flow control if not supported 15283905afbfSMatthias Ringwald if (hci_stack->local_supported_commands[0] & 0x04) break; 15293905afbfSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE; 15303905afbfSMatthias Ringwald // explicit fall through to reduce repetitions 15313905afbfSMatthias Ringwald 1532729ed62eSMatthias Ringwald case HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE: 15333905afbfSMatthias Ringwald // skip write default erroneous data reporting if not supported 15343905afbfSMatthias Ringwald if (hci_stack->local_supported_commands[0] & 0x08) break; 15353905afbfSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING; 15363905afbfSMatthias Ringwald // explicit fall through to reduce repetitions 15373905afbfSMatthias Ringwald 1538f064e0bbSMatthias Ringwald case HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING: 1539a42798c3SMatthias Ringwald // skip bcm set sco pcm config on non-Broadcom chipsets 154061f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION) break; 1541a42798c3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT; 1542a42798c3SMatthias Ringwald // explicit fall through to reduce repetitions 1543a42798c3SMatthias Ringwald 1544a42798c3SMatthias Ringwald case HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT: 1545729ed62eSMatthias Ringwald if (!hci_le_supported()){ 1546729ed62eSMatthias Ringwald // SKIP LE init for Classic only configuration 1547a650ba4dSMatthias Ringwald hci_init_done(); 1548729ed62eSMatthias Ringwald return; 1549729ed62eSMatthias Ringwald } 1550729ed62eSMatthias Ringwald break; 1551f3b012f9SMatthias Ringwald 1552f3b012f9SMatthias Ringwald #else /* !ENABLE_SCO_OVER_HCI */ 1553f3b012f9SMatthias Ringwald 155474b323a9SMatthias Ringwald case HCI_INIT_W4_WRITE_SCAN_ENABLE: 1555f3b012f9SMatthias Ringwald #ifdef ENABLE_BLE 1556f3b012f9SMatthias Ringwald if (hci_le_supported()){ 1557f3b012f9SMatthias Ringwald hci_stack->substate = HCI_INIT_LE_READ_BUFFER_SIZE; 155874b323a9SMatthias Ringwald return; 15596155b3d3S[email protected] } 1560729ed62eSMatthias Ringwald #endif 1561f3b012f9SMatthias Ringwald // SKIP LE init for Classic only configuration 1562f3b012f9SMatthias Ringwald hci_init_done(); 1563f3b012f9SMatthias Ringwald return; 1564f3b012f9SMatthias Ringwald #endif /* ENABLE_SCO_OVER_HCI */ 1565f3b012f9SMatthias Ringwald 1566a650ba4dSMatthias Ringwald // Response to command before init done state -> init done 1567a650ba4dSMatthias Ringwald case (HCI_INIT_DONE-1): 1568a650ba4dSMatthias Ringwald hci_init_done(); 1569a650ba4dSMatthias Ringwald return; 1570a650ba4dSMatthias Ringwald 15716155b3d3S[email protected] default: 157274b323a9SMatthias Ringwald break; 15736155b3d3S[email protected] } 157455975f88SMatthias Ringwald hci_initializing_next_state(); 15756155b3d3S[email protected] } 15766155b3d3S[email protected] 157716833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){ 1578e76a89eeS[email protected] 1579e76a89eeS[email protected] uint16_t event_length = packet[1]; 1580e76a89eeS[email protected] 1581e76a89eeS[email protected] // assert packet is complete 1582e76a89eeS[email protected] if (size != event_length + 2){ 1583f04a0c31SMatthias Ringwald log_error("hci.c: event_handler called with event packet of wrong size %d, expected %u => dropping packet", size, event_length + 2); 1584e76a89eeS[email protected] return; 1585e76a89eeS[email protected] } 1586e76a89eeS[email protected] 15871281a47eSmatthias.ringwald bd_addr_t addr; 158896a45072S[email protected] bd_addr_type_t addr_type; 15892d00edd4Smatthias.ringwald uint8_t link_type; 1590fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 15911f7b95a1Smatthias.ringwald hci_connection_t * conn; 159256cf178bSmatthias.ringwald int i; 159322909952Smatthias.ringwald 159435454696SMatthias Ringwald // warnings 159535454696SMatthias Ringwald (void) link_type; 159635454696SMatthias Ringwald 15970e2df43fSMatthias Ringwald // log_info("HCI:EVENT:%02x", hci_event_packet_get_type(packet)); 15985909f7f2Smatthias.ringwald 15990e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 160022909952Smatthias.ringwald 16016772a24cSmatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 16026bef4003SMatthias Ringwald // get num cmd packets - limit to 1 to reduce complexity 16036bef4003SMatthias Ringwald hci_stack->num_cmd_packets = packet[2] ? 1 : 0; 16047ec5eeaaSmatthias.ringwald 16059e263bd7SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){ 160606b9e820SMatthias Ringwald if (packet[5]) break; 16079e263bd7SMatthias Ringwald // terminate, name 248 chars 16089e263bd7SMatthias Ringwald packet[6+248] = 0; 16099e263bd7SMatthias Ringwald log_info("local name: %s", &packet[6]); 16109e263bd7SMatthias Ringwald } 1611073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_buffer_size)){ 16121d279b20Smatthias.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" 1613429122ccSMatthias Ringwald if (hci_stack->state == HCI_STATE_INITIALIZING){ 1614429122ccSMatthias Ringwald uint16_t acl_len = little_endian_read_16(packet, 6); 1615429122ccSMatthias Ringwald uint16_t sco_len = packet[8]; 1616429122ccSMatthias Ringwald 1617429122ccSMatthias Ringwald // determine usable ACL/SCO payload size 1618429122ccSMatthias Ringwald hci_stack->acl_data_packet_length = btstack_min(acl_len, HCI_ACL_PAYLOAD_SIZE); 1619429122ccSMatthias Ringwald hci_stack->sco_data_packet_length = btstack_min(sco_len, HCI_ACL_PAYLOAD_SIZE); 1620429122ccSMatthias Ringwald 1621f8fbdce0SMatthias Ringwald hci_stack->acl_packets_total_num = little_endian_read_16(packet, 9); 1622f8fbdce0SMatthias Ringwald hci_stack->sco_packets_total_num = little_endian_read_16(packet, 11); 1623a8b12447S[email protected] 1624429122ccSMatthias Ringwald log_info("hci_read_buffer_size: ACL size module %u -> used %u, count %u / SCO size %u, count %u", 1625429122ccSMatthias Ringwald acl_len, hci_stack->acl_data_packet_length, hci_stack->acl_packets_total_num, 16261a06f663S[email protected] hci_stack->sco_data_packet_length, hci_stack->sco_packets_total_num); 1627e2edc0c3Smatthias.ringwald } 162856cf178bSmatthias.ringwald } 1629a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 1630073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_le_read_buffer_size)){ 1631f8fbdce0SMatthias Ringwald hci_stack->le_data_packets_length = little_endian_read_16(packet, 6); 1632ee303eddS[email protected] hci_stack->le_acl_packets_total_num = packet[8]; 16336c26b087S[email protected] // determine usable ACL payload size 16346c26b087S[email protected] if (HCI_ACL_PAYLOAD_SIZE < hci_stack->le_data_packets_length){ 16356c26b087S[email protected] hci_stack->le_data_packets_length = HCI_ACL_PAYLOAD_SIZE; 16366c26b087S[email protected] } 16379da54300S[email protected] log_info("hci_le_read_buffer_size: size %u, count %u", hci_stack->le_data_packets_length, hci_stack->le_acl_packets_total_num); 163865a46ef3S[email protected] } 1639d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 1640073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_le_read_white_list_size)){ 1641e691bb38SMatthias Ringwald hci_stack->le_whitelist_capacity = packet[6]; 164215d0a15bSMatthias Ringwald log_info("hci_le_read_white_list_size: size %u", hci_stack->le_whitelist_capacity); 16433b6d4121SMatthias Ringwald } 164465a46ef3S[email protected] #endif 1645d70217a2SMatthias Ringwald #endif 1646073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_bd_addr)) { 1647724d70a2SMatthias Ringwald reverse_bd_addr(&packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1], 1648724d70a2SMatthias Ringwald hci_stack->local_bd_addr); 16499da54300S[email protected] log_info("Local Address, Status: 0x%02x: Addr: %s", 16503a9fb326S[email protected] packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr)); 165133373e40SMatthias Ringwald #ifdef ENABLE_CLASSIC 16521624665aSMatthias Ringwald if (hci_stack->link_key_db){ 16531624665aSMatthias Ringwald hci_stack->link_key_db->set_local_bd_addr(hci_stack->local_bd_addr); 16541624665aSMatthias Ringwald } 165533373e40SMatthias Ringwald #endif 1656188981d2Smatthias.ringwald } 165735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1658073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_scan_enable)){ 16593a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 1660381fbed8Smatthias.ringwald } 166135454696SMatthias Ringwald #endif 166235454696SMatthias Ringwald 166365389bfcS[email protected] // Note: HCI init checks 1664073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_supported_features)){ 16653a9fb326S[email protected] memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8); 166665389bfcS[email protected] 166735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1668a5a23fc2S[email protected] // determine usable ACL packet types based on host buffer size and supported features 1669a5a23fc2S[email protected] hci_stack->packet_types = hci_acl_packet_types_for_buffer_size_and_local_features(HCI_ACL_PAYLOAD_SIZE, &hci_stack->local_supported_features[0]); 16708b96126aSMatthias Ringwald log_info("Packet types %04x, eSCO %u", hci_stack->packet_types, hci_extended_sco_link_supported()); 167135454696SMatthias Ringwald #endif 1672f5d8d141S[email protected] // Classic/LE 1673f5d8d141S[email protected] log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported()); 1674559e517eS[email protected] } 1675073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_version_information)){ 1676f8fbdce0SMatthias Ringwald // hci_stack->hci_version = little_endian_read_16(packet, 4); 1677f8fbdce0SMatthias Ringwald // hci_stack->hci_revision = little_endian_read_16(packet, 6); 1678f8fbdce0SMatthias Ringwald // hci_stack->lmp_version = little_endian_read_16(packet, 8); 1679f8fbdce0SMatthias Ringwald hci_stack->manufacturer = little_endian_read_16(packet, 10); 1680f8fbdce0SMatthias Ringwald // hci_stack->lmp_subversion = little_endian_read_16(packet, 12); 16814696bddbSMatthias Ringwald log_info("Manufacturer: 0x%04x", hci_stack->manufacturer); 16824696bddbSMatthias Ringwald } 1683073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_supported_commands)){ 1684a828a756SMatthias Ringwald hci_stack->local_supported_commands[0] = 16853905afbfSMatthias Ringwald (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+14] & 0x80) >> 7 | // bit 0 = Octet 14, bit 7 16863905afbfSMatthias Ringwald (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+24] & 0x40) >> 5 | // bit 1 = Octet 24, bit 6 16873905afbfSMatthias Ringwald (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+10] & 0x10) >> 2 | // bit 2 = Octet 10, bit 4 16883905afbfSMatthias Ringwald (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+18] & 0x08); // bit 3 = Octet 18, bit 3 16893905afbfSMatthias Ringwald log_info("Local supported commands summary 0x%02x", hci_stack->local_supported_commands[0]); 1690a828a756SMatthias Ringwald } 1691e8c8828eSMatthias Ringwald #ifdef ENABLE_CLASSIC 1692073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_synchronous_flow_control_enable)){ 16935b9b590fSMatthias Ringwald if (packet[5] == 0){ 16945b9b590fSMatthias Ringwald hci_stack->synchronous_flow_control_enabled = 1; 16955b9b590fSMatthias Ringwald } 16965b9b590fSMatthias Ringwald } 1697e8c8828eSMatthias Ringwald #endif 169856cf178bSmatthias.ringwald break; 169956cf178bSmatthias.ringwald 17007ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 17016bef4003SMatthias Ringwald // get num cmd packets - limit to 1 to reduce complexity 17026bef4003SMatthias Ringwald hci_stack->num_cmd_packets = packet[3] ? 1 : 0; 17037ec5eeaaSmatthias.ringwald break; 17047ec5eeaaSmatthias.ringwald 17052e440c8aS[email protected] case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:{ 17062e440c8aS[email protected] int offset = 3; 170756cf178bSmatthias.ringwald for (i=0; i<packet[2];i++){ 1708f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, offset); 17092e440c8aS[email protected] offset += 2; 1710f8fbdce0SMatthias Ringwald uint16_t num_packets = little_endian_read_16(packet, offset); 17112e440c8aS[email protected] offset += 2; 17122e440c8aS[email protected] 17135061f3afS[email protected] conn = hci_connection_for_handle(handle); 171456cf178bSmatthias.ringwald if (!conn){ 17159da54300S[email protected] log_error("hci_number_completed_packet lists unused con handle %u", handle); 171656cf178bSmatthias.ringwald continue; 171756cf178bSmatthias.ringwald } 171823bed257S[email protected] 1719e35edcc1S[email protected] if (conn->address_type == BD_ADDR_TYPE_SCO){ 172035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1721e35edcc1S[email protected] if (conn->num_sco_packets_sent >= num_packets){ 1722e35edcc1S[email protected] conn->num_sco_packets_sent -= num_packets; 1723e35edcc1S[email protected] } else { 1724e35edcc1S[email protected] log_error("hci_number_completed_packets, more sco slots freed then sent."); 1725e35edcc1S[email protected] conn->num_sco_packets_sent = 0; 1726e35edcc1S[email protected] } 1727701e3307SMatthias Ringwald hci_notify_if_sco_can_send_now(); 172835454696SMatthias Ringwald #endif 1729e35edcc1S[email protected] } else { 173023bed257S[email protected] if (conn->num_acl_packets_sent >= num_packets){ 173156cf178bSmatthias.ringwald conn->num_acl_packets_sent -= num_packets; 173223bed257S[email protected] } else { 1733e35edcc1S[email protected] log_error("hci_number_completed_packets, more acl slots freed then sent."); 173423bed257S[email protected] conn->num_acl_packets_sent = 0; 173523bed257S[email protected] } 1736e35edcc1S[email protected] } 17379da54300S[email protected] // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u", num_packets, handle, conn->num_acl_packets_sent); 173856cf178bSmatthias.ringwald } 17396772a24cSmatthias.ringwald break; 17402e440c8aS[email protected] } 174135454696SMatthias Ringwald 174235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 17431f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 1744724d70a2SMatthias Ringwald reverse_bd_addr(&packet[2], addr); 174537eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 17462d00edd4Smatthias.ringwald link_type = packet[11]; 17479da54300S[email protected] log_info("Connection_incoming: %s, type %u", bd_addr_to_str(addr), link_type); 1748e35edcc1S[email protected] addr_type = link_type == 1 ? BD_ADDR_TYPE_CLASSIC : BD_ADDR_TYPE_SCO; 17492e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 17501f7b95a1Smatthias.ringwald if (!conn) { 17515293c072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 17521f7b95a1Smatthias.ringwald } 1753ce4c8fabSmatthias.ringwald if (!conn) { 1754ce4c8fabSmatthias.ringwald // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 17553a9fb326S[email protected] hci_stack->decline_reason = 0x0d; 1756058e3d6bSMatthias Ringwald bd_addr_copy(hci_stack->decline_addr, addr); 1757ce4c8fabSmatthias.ringwald break; 1758ce4c8fabSmatthias.ringwald } 17595cf766e8SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 176032ab9390Smatthias.ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 1761f3a16b9aSMatthias Ringwald // store info about eSCO 1762f3a16b9aSMatthias Ringwald if (link_type == 0x02){ 1763f3a16b9aSMatthias Ringwald conn->remote_supported_feature_eSCO = 1; 1764f3a16b9aSMatthias Ringwald } 176532ab9390Smatthias.ringwald hci_run(); 17661f7b95a1Smatthias.ringwald break; 17671f7b95a1Smatthias.ringwald 17686772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 1769fe1ed1b8Smatthias.ringwald // Connection management 1770724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], addr); 17719da54300S[email protected] log_info("Connection_complete (status=%u) %s", packet[2], bd_addr_to_str(addr)); 177296a45072S[email protected] addr_type = BD_ADDR_TYPE_CLASSIC; 17732e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 1774fe1ed1b8Smatthias.ringwald if (conn) { 1775b448a0e7Smatthias.ringwald if (!packet[2]){ 1776c8e4258aSmatthias.ringwald conn->state = OPEN; 1777f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 3); 1778ad83dc6aS[email protected] conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES; 1779ee091cf1Smatthias.ringwald 1780c785ef68Smatthias.ringwald // restart timer 1781528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 1782528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&conn->timeout); 1783c785ef68Smatthias.ringwald 17849da54300S[email protected] log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 178543bfb1bdSmatthias.ringwald 178643bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 1787b448a0e7Smatthias.ringwald } else { 17881bd5283dS[email protected] int notify_dedicated_bonding_failed = conn->bonding_flags & BONDING_DEDICATED; 17891bd5283dS[email protected] uint8_t status = packet[2]; 17901bd5283dS[email protected] bd_addr_t bd_address; 17911bd5283dS[email protected] memcpy(&bd_address, conn->address, 6); 1792ad83dc6aS[email protected] 1793b448a0e7Smatthias.ringwald // connection failed, remove entry 1794665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 1795a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 1796c12e46e7Smatthias.ringwald 17971bd5283dS[email protected] // notify client if dedicated bonding 17981bd5283dS[email protected] if (notify_dedicated_bonding_failed){ 17991bd5283dS[email protected] log_info("hci notify_dedicated_bonding_failed"); 18001bd5283dS[email protected] hci_emit_dedicated_bonding_result(bd_address, status); 18011bd5283dS[email protected] } 18021bd5283dS[email protected] 1803c12e46e7Smatthias.ringwald // if authentication error, also delete link key 1804c12e46e7Smatthias.ringwald if (packet[2] == 0x05) { 180515a95bd5SMatthias Ringwald gap_drop_link_key_for_bd_addr(addr); 1806c12e46e7Smatthias.ringwald } 1807fe1ed1b8Smatthias.ringwald } 1808fe1ed1b8Smatthias.ringwald } 18096772a24cSmatthias.ringwald break; 1810fe1ed1b8Smatthias.ringwald 181144d0e3d5S[email protected] case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE: 1812724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], addr); 181344d0e3d5S[email protected] log_info("Synchronous Connection Complete (status=%u) %s", packet[2], bd_addr_to_str(addr)); 18141a06f663S[email protected] if (packet[2]){ 181544d0e3d5S[email protected] // connection failed 181644d0e3d5S[email protected] break; 181744d0e3d5S[email protected] } 18182e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 1819e35edcc1S[email protected] if (!conn) { 1820e35edcc1S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 1821e35edcc1S[email protected] } 1822e35edcc1S[email protected] if (!conn) { 1823e35edcc1S[email protected] break; 1824e35edcc1S[email protected] } 18251a06f663S[email protected] conn->state = OPEN; 1826f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 3); 1827ee752bb8SMatthias Ringwald 1828ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 1829ee752bb8SMatthias Ringwald // update SCO 1830ee752bb8SMatthias Ringwald if (conn->address_type == BD_ADDR_TYPE_SCO && hci_stack->hci_transport && hci_stack->hci_transport->set_sco_config){ 1831ee752bb8SMatthias Ringwald hci_stack->hci_transport->set_sco_config(hci_stack->sco_voice_setting_active, hci_number_sco_connections()); 1832ee752bb8SMatthias Ringwald } 1833ee752bb8SMatthias Ringwald #endif 183444d0e3d5S[email protected] break; 183544d0e3d5S[email protected] 1836afd4e962S[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 1837f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1838afd4e962S[email protected] conn = hci_connection_for_handle(handle); 1839afd4e962S[email protected] if (!conn) break; 1840afd4e962S[email protected] if (!packet[2]){ 1841afd4e962S[email protected] uint8_t * features = &packet[5]; 1842afd4e962S[email protected] if (features[6] & (1 << 3)){ 1843afd4e962S[email protected] conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP; 1844afd4e962S[email protected] } 184598a2fd1cSMatthias Ringwald if (features[3] & (1<<7)){ 184698a2fd1cSMatthias Ringwald conn->remote_supported_feature_eSCO = 1; 184798a2fd1cSMatthias Ringwald } 1848afd4e962S[email protected] } 1849afd4e962S[email protected] conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES; 185098a2fd1cSMatthias Ringwald log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x, eSCO %u", conn->bonding_flags, conn->remote_supported_feature_eSCO); 1851ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 1852ad83dc6aS[email protected] conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 1853ad83dc6aS[email protected] } 1854afd4e962S[email protected] break; 1855afd4e962S[email protected] 18567fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 18579da54300S[email protected] log_info("HCI_EVENT_LINK_KEY_REQUEST"); 18587fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST); 185974d716b5S[email protected] // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST 1860a98592bcSMatthias Ringwald if (hci_stack->bondable && !hci_stack->link_key_db) break; 186132ab9390Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST); 186264472d52Smatthias.ringwald hci_run(); 1863d9a327f9Smatthias.ringwald // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set 186429d53098Smatthias.ringwald return; 18657fde4af9Smatthias.ringwald 18669ab95c90S[email protected] case HCI_EVENT_LINK_KEY_NOTIFICATION: { 1867724d70a2SMatthias Ringwald reverse_bd_addr(&packet[2], addr); 18682e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 18699ab95c90S[email protected] if (!conn) break; 18709ab95c90S[email protected] conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION; 18717bdc6798S[email protected] link_key_type_t link_key_type = (link_key_type_t)packet[24]; 18729ab95c90S[email protected] // Change Connection Encryption keeps link key type 18739ab95c90S[email protected] if (link_key_type != CHANGED_COMBINATION_KEY){ 18749ab95c90S[email protected] conn->link_key_type = link_key_type; 18759ab95c90S[email protected] } 187655597469SMatthias Ringwald gap_store_link_key_for_bd_addr(addr, &packet[8], conn->link_key_type); 187729d53098Smatthias.ringwald // still forward event to allow dismiss of pairing dialog 18787fde4af9Smatthias.ringwald break; 18799ab95c90S[email protected] } 18807fde4af9Smatthias.ringwald 18817fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 18826724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE); 18834c57c146S[email protected] // non-bondable mode: pin code negative reply will be sent 18843a9fb326S[email protected] if (!hci_stack->bondable){ 1885899283eaS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST); 1886f8fb5f6eS[email protected] hci_run(); 1887f8fb5f6eS[email protected] return; 18884c57c146S[email protected] } 1889d9a327f9Smatthias.ringwald // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key 1890a98592bcSMatthias Ringwald if (!hci_stack->link_key_db) break; 1891a6ef64baSMilanka Ringwald hci_event_pin_code_request_get_bd_addr(packet, addr); 1892a98592bcSMatthias Ringwald hci_stack->link_key_db->delete_link_key(addr); 18937fde4af9Smatthias.ringwald break; 18947fde4af9Smatthias.ringwald 18951d6b20aeS[email protected] case HCI_EVENT_IO_CAPABILITY_REQUEST: 18961d6b20aeS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST); 1897dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY); 1898dbe1a790S[email protected] break; 1899dbe1a790S[email protected] 1900dbe1a790S[email protected] case HCI_EVENT_USER_CONFIRMATION_REQUEST: 19016724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 19023a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 1903dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY); 1904dbe1a790S[email protected] break; 1905dbe1a790S[email protected] 1906dbe1a790S[email protected] case HCI_EVENT_USER_PASSKEY_REQUEST: 19076724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 19083a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 1909dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY); 19101d6b20aeS[email protected] break; 191135454696SMatthias Ringwald #endif 19121d6b20aeS[email protected] 1913f0944df2S[email protected] case HCI_EVENT_ENCRYPTION_CHANGE: 1914f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1915f0944df2S[email protected] conn = hci_connection_for_handle(handle); 1916f0944df2S[email protected] if (!conn) break; 1917ad83dc6aS[email protected] if (packet[2] == 0) { 1918f0944df2S[email protected] if (packet[5]){ 1919f0944df2S[email protected] conn->authentication_flags |= CONNECTION_ENCRYPTED; 1920f0944df2S[email protected] } else { 1921536f9994S[email protected] conn->authentication_flags &= ~CONNECTION_ENCRYPTED; 1922f0944df2S[email protected] } 1923ad83dc6aS[email protected] } 192435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1925a00031e2S[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 192635454696SMatthias Ringwald #endif 1927f0944df2S[email protected] break; 1928f0944df2S[email protected] 192935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 19301eb2563eS[email protected] case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT: 1931f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 19321eb2563eS[email protected] conn = hci_connection_for_handle(handle); 19331eb2563eS[email protected] if (!conn) break; 1934ad83dc6aS[email protected] 1935ad83dc6aS[email protected] // dedicated bonding: send result and disconnect 1936ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 1937ad83dc6aS[email protected] conn->bonding_flags &= ~BONDING_DEDICATED; 1938ad83dc6aS[email protected] conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE; 19391bd5283dS[email protected] conn->bonding_status = packet[2]; 1940ad83dc6aS[email protected] break; 1941ad83dc6aS[email protected] } 1942ad83dc6aS[email protected] 1943b5cb6874S[email protected] if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){ 19441eb2563eS[email protected] // link key sufficient for requested security 19451eb2563eS[email protected] conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 1946ad83dc6aS[email protected] break; 1947ad83dc6aS[email protected] } 19481eb2563eS[email protected] // not enough 19491eb2563eS[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 19501eb2563eS[email protected] break; 195135454696SMatthias Ringwald #endif 195234d2123cS[email protected] 195386805605S[email protected] // HCI_EVENT_DISCONNECTION_COMPLETE 1954ccda6e14S[email protected] // has been split, to first notify stack before shutting connection down 1955ccda6e14S[email protected] // see end of function, too. 1956a4f30ec0S[email protected] case HCI_EVENT_DISCONNECTION_COMPLETE: 1957a4f30ec0S[email protected] if (packet[2]) break; // status != 0 1958f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1959c6a37cfdSMatthias Ringwald // drop outgoing ACL fragments if it is for closed connection 1960c6a37cfdSMatthias Ringwald if (hci_stack->acl_fragmentation_total_size > 0) { 1961c6a37cfdSMatthias Ringwald if (handle == READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer)){ 1962c6a37cfdSMatthias Ringwald log_info("hci: drop fragmented ACL data for closed connection"); 1963c6a37cfdSMatthias Ringwald hci_stack->acl_fragmentation_total_size = 0; 1964c6a37cfdSMatthias Ringwald hci_stack->acl_fragmentation_pos = 0; 1965c6a37cfdSMatthias Ringwald } 1966c6a37cfdSMatthias Ringwald } 196735454696SMatthias Ringwald 19689a2e4658SMatthias Ringwald // re-enable advertisements for le connections if active 1969c6a37cfdSMatthias Ringwald conn = hci_connection_for_handle(handle); 1970c6a37cfdSMatthias Ringwald if (!conn) break; 197135454696SMatthias Ringwald #ifdef ENABLE_BLE 1972d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 19739a2e4658SMatthias Ringwald if (hci_is_le_connection(conn) && hci_stack->le_advertisements_enabled){ 19749a2e4658SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_ENABLE; 19759a2e4658SMatthias Ringwald } 197635454696SMatthias Ringwald #endif 1977d70217a2SMatthias Ringwald #endif 1978ccda6e14S[email protected] conn->state = RECEIVED_DISCONNECTION_COMPLETE; 1979ccda6e14S[email protected] break; 19806772a24cSmatthias.ringwald 1981c68bdf90Smatthias.ringwald case HCI_EVENT_HARDWARE_ERROR: 1982313e5f9cSMatthias Ringwald log_error("Hardware Error: 0x%02x", packet[2]); 1983d23838ecSMatthias Ringwald if (hci_stack->hardware_error_callback){ 1984c2e1fa60SMatthias Ringwald (*hci_stack->hardware_error_callback)(packet[2]); 19857586ee35S[email protected] } else { 19867586ee35S[email protected] // if no special requests, just reboot stack 19877586ee35S[email protected] hci_power_control_off(); 19887586ee35S[email protected] hci_power_control_on(); 1989c68bdf90Smatthias.ringwald } 1990c68bdf90Smatthias.ringwald break; 1991c68bdf90Smatthias.ringwald 19920e6f3837SMatthias Ringwald #ifdef ENABLE_CLASSIC 19935cf766e8SMatthias Ringwald case HCI_EVENT_ROLE_CHANGE: 19945cf766e8SMatthias Ringwald if (packet[2]) break; // status != 0 1995f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 19965cf766e8SMatthias Ringwald conn = hci_connection_for_handle(handle); 19975cf766e8SMatthias Ringwald if (!conn) break; // no conn 19985cf766e8SMatthias Ringwald conn->role = packet[9]; 19995cf766e8SMatthias Ringwald break; 20000e6f3837SMatthias Ringwald #endif 20015cf766e8SMatthias Ringwald 200263fa3374SMatthias Ringwald case HCI_EVENT_TRANSPORT_PACKET_SENT: 2003d051460cS[email protected] // release packet buffer only for asynchronous transport and if there are not further fragements 20044fa24b5fS[email protected] if (hci_transport_synchronous()) { 200563fa3374SMatthias Ringwald log_error("Synchronous HCI Transport shouldn't send HCI_EVENT_TRANSPORT_PACKET_SENT"); 20064fa24b5fS[email protected] return; // instead of break: to avoid re-entering hci_run() 20074fa24b5fS[email protected] } 2008d051460cS[email protected] if (hci_stack->acl_fragmentation_total_size) break; 2009d051460cS[email protected] hci_release_packet_buffer(); 2010701e3307SMatthias Ringwald 201163fa3374SMatthias Ringwald // L2CAP receives this event via the hci_emit_event below 201263fa3374SMatthias Ringwald 201335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 201463fa3374SMatthias Ringwald // For SCO, we do the can_send_now_check here 2015701e3307SMatthias Ringwald hci_notify_if_sco_can_send_now(); 201635454696SMatthias Ringwald #endif 20176b4af23dS[email protected] break; 20186b4af23dS[email protected] 201935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 202063fa3374SMatthias Ringwald case HCI_EVENT_SCO_CAN_SEND_NOW: 202163fa3374SMatthias Ringwald // For SCO, we do the can_send_now_check here 202263fa3374SMatthias Ringwald hci_notify_if_sco_can_send_now(); 202363fa3374SMatthias Ringwald return; 202435454696SMatthias Ringwald #endif 202563fa3374SMatthias Ringwald 2026a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 20275909f7f2Smatthias.ringwald case HCI_EVENT_LE_META: 20285909f7f2Smatthias.ringwald switch (packet[2]){ 2029d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 203057c9da5bS[email protected] case HCI_SUBEVENT_LE_ADVERTISING_REPORT: 20314f4e0224SMatthias Ringwald // log_info("advertising report received"); 20327bdc6798S[email protected] if (hci_stack->le_scanning_state != LE_SCANNING) break; 203357c9da5bS[email protected] le_handle_advertisement_report(packet, size); 20347bdc6798S[email protected] break; 2035d70217a2SMatthias Ringwald #endif 20365909f7f2Smatthias.ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 20375909f7f2Smatthias.ringwald // Connection management 2038724d70a2SMatthias Ringwald reverse_bd_addr(&packet[8], addr); 20397bdc6798S[email protected] addr_type = (bd_addr_type_t)packet[7]; 20409da54300S[email protected] log_info("LE Connection_complete (status=%u) type %u, %s", packet[3], addr_type, bd_addr_to_str(addr)); 20412e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 2042d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 204342ff5ba1SMatthias Ringwald // if auto-connect, remove from whitelist in both roles 204442ff5ba1SMatthias Ringwald if (hci_stack->le_connecting_state == LE_CONNECTING_WHITELIST){ 204542ff5ba1SMatthias Ringwald hci_remove_from_whitelist(addr_type, addr); 204642ff5ba1SMatthias Ringwald } 204742ff5ba1SMatthias Ringwald // handle error: error is reported only to the initiator -> outgoing connection 20485909f7f2Smatthias.ringwald if (packet[3]){ 204942ff5ba1SMatthias Ringwald // outgoing connection establishment is done 205042ff5ba1SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 205142ff5ba1SMatthias Ringwald // remove entry 20525909f7f2Smatthias.ringwald if (conn){ 2053665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 20545909f7f2Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 20555909f7f2Smatthias.ringwald } 20565909f7f2Smatthias.ringwald break; 20575909f7f2Smatthias.ringwald } 2058d70217a2SMatthias Ringwald #endif 205942ff5ba1SMatthias Ringwald // on success, both hosts receive connection complete event 20605cf766e8SMatthias Ringwald if (packet[6] == HCI_ROLE_MASTER){ 2061d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 206242ff5ba1SMatthias Ringwald // if we're master, it was an outgoing connection and we're done with it 206342ff5ba1SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 2064d70217a2SMatthias Ringwald #endif 206542ff5ba1SMatthias Ringwald } else { 2066d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 206742ff5ba1SMatthias Ringwald // if we're slave, it was an incoming connection, advertisements have stopped 2068171293d3SMatthias Ringwald hci_stack->le_advertisements_active = 0; 20695106e6dcSMatthias Ringwald // try to re-enable them 20705106e6dcSMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_ENABLE; 2071d70217a2SMatthias Ringwald #endif 207242ff5ba1SMatthias Ringwald } 2073171293d3SMatthias Ringwald // LE connections are auto-accepted, so just create a connection if there isn't one already 207442ff5ba1SMatthias Ringwald if (!conn){ 207596a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 20765909f7f2Smatthias.ringwald } 207742ff5ba1SMatthias Ringwald // no memory, sorry. 20785909f7f2Smatthias.ringwald if (!conn){ 20795909f7f2Smatthias.ringwald break; 20805909f7f2Smatthias.ringwald } 20815909f7f2Smatthias.ringwald 20825909f7f2Smatthias.ringwald conn->state = OPEN; 20835cf766e8SMatthias Ringwald conn->role = packet[6]; 2084f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 4); 20855909f7f2Smatthias.ringwald 20865909f7f2Smatthias.ringwald // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock 20875909f7f2Smatthias.ringwald 20885909f7f2Smatthias.ringwald // restart timer 2089528a4a3bSMatthias Ringwald // btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 2090528a4a3bSMatthias Ringwald // btstack_run_loop_add_timer(&conn->timeout); 20915909f7f2Smatthias.ringwald 20929da54300S[email protected] log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 20935909f7f2Smatthias.ringwald 20945909f7f2Smatthias.ringwald hci_emit_nr_connections_changed(); 20955909f7f2Smatthias.ringwald break; 20965909f7f2Smatthias.ringwald 2097f8fbdce0SMatthias Ringwald // log_info("LE buffer size: %u, count %u", little_endian_read_16(packet,6), packet[8]); 209865a46ef3S[email protected] 20995909f7f2Smatthias.ringwald default: 21005909f7f2Smatthias.ringwald break; 21015909f7f2Smatthias.ringwald } 21025909f7f2Smatthias.ringwald break; 21035909f7f2Smatthias.ringwald #endif 21046772a24cSmatthias.ringwald default: 21056772a24cSmatthias.ringwald break; 2106fe1ed1b8Smatthias.ringwald } 2107fe1ed1b8Smatthias.ringwald 21083429f56bSmatthias.ringwald // handle BT initialization 21093a9fb326S[email protected] if (hci_stack->state == HCI_STATE_INITIALIZING){ 21106155b3d3S[email protected] hci_initializing_event_handler(packet, size); 2111c9af4d3fS[email protected] } 211222909952Smatthias.ringwald 211389db417bSmatthias.ringwald // help with BT sleep 21143a9fb326S[email protected] if (hci_stack->state == HCI_STATE_FALLING_ASLEEP 211574b323a9SMatthias Ringwald && hci_stack->substate == HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE 2116073bd0faSMatthias Ringwald && HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_scan_enable)){ 211755975f88SMatthias Ringwald hci_initializing_next_state(); 211889db417bSmatthias.ringwald } 211989db417bSmatthias.ringwald 212086805605S[email protected] // notify upper stack 2121d6b06661SMatthias Ringwald hci_emit_event(packet, size, 0); // don't dump, already happened in packet handler 212294ab26f8Smatthias.ringwald 212386805605S[email protected] // moved here to give upper stack a chance to close down everything with hci_connection_t intact 21240e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_DISCONNECTION_COMPLETE){ 212586805605S[email protected] if (!packet[2]){ 2126f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 212705ae8de3SMatthias Ringwald hci_connection_t * aConn = hci_connection_for_handle(handle); 212805ae8de3SMatthias Ringwald if (aConn) { 212905ae8de3SMatthias Ringwald uint8_t status = aConn->bonding_status; 213005ae8de3SMatthias Ringwald uint16_t flags = aConn->bonding_flags; 213186805605S[email protected] bd_addr_t bd_address; 213205ae8de3SMatthias Ringwald memcpy(&bd_address, aConn->address, 6); 213305ae8de3SMatthias Ringwald hci_shutdown_connection(aConn); 2134bb820044S[email protected] // connection struct is gone, don't access anymore 2135bb820044S[email protected] if (flags & BONDING_EMIT_COMPLETE_ON_DISCONNECT){ 213686805605S[email protected] hci_emit_dedicated_bonding_result(bd_address, status); 213786805605S[email protected] } 213886805605S[email protected] } 213986805605S[email protected] } 214086805605S[email protected] } 214186805605S[email protected] 214294ab26f8Smatthias.ringwald // execute main loop 214394ab26f8Smatthias.ringwald hci_run(); 214416833f0aSmatthias.ringwald } 214516833f0aSmatthias.ringwald 214635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2147c91d150bS[email protected] static void sco_handler(uint8_t * packet, uint16_t size){ 21488abbe8b5SMatthias Ringwald if (!hci_stack->sco_packet_handler) return; 21493d50b4baSMatthias Ringwald hci_stack->sco_packet_handler(HCI_SCO_DATA_PACKET, 0, packet, size); 2150*2b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 2151*2b838201SMatthias Ringwald hci_con_handle_t con_handle = READ_SCO_CONNECTION_HANDLE(packet); 2152*2b838201SMatthias Ringwald hci_connection_t *conn = hci_connection_for_handle(con_handle); 2153*2b838201SMatthias Ringwald if (conn){ 2154*2b838201SMatthias Ringwald conn->num_packets_completed++; 2155*2b838201SMatthias Ringwald hci_stack->host_completed_packets = 1; 2156*2b838201SMatthias Ringwald hci_run(); 2157*2b838201SMatthias Ringwald } 2158*2b838201SMatthias Ringwald #endif 2159c91d150bS[email protected] } 216035454696SMatthias Ringwald #endif 2161c91d150bS[email protected] 21620a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 21635bb5bc3eS[email protected] hci_dump_packet(packet_type, 1, packet, size); 216410e830c9Smatthias.ringwald switch (packet_type) { 216510e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 216610e830c9Smatthias.ringwald event_handler(packet, size); 216710e830c9Smatthias.ringwald break; 216810e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 216910e830c9Smatthias.ringwald acl_handler(packet, size); 217010e830c9Smatthias.ringwald break; 217135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2172c91d150bS[email protected] case HCI_SCO_DATA_PACKET: 2173c91d150bS[email protected] sco_handler(packet, size); 2174202c8a4cSMatthias Ringwald break; 217535454696SMatthias Ringwald #endif 217610e830c9Smatthias.ringwald default: 217710e830c9Smatthias.ringwald break; 217810e830c9Smatthias.ringwald } 217910e830c9Smatthias.ringwald } 218010e830c9Smatthias.ringwald 2181d6b06661SMatthias Ringwald /** 2182d6b06661SMatthias Ringwald * @brief Add event packet handler. 2183d6b06661SMatthias Ringwald */ 2184d6b06661SMatthias Ringwald void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){ 2185d6b06661SMatthias Ringwald btstack_linked_list_add_tail(&hci_stack->event_handlers, (btstack_linked_item_t*) callback_handler); 2186d6b06661SMatthias Ringwald } 2187d6b06661SMatthias Ringwald 2188d6b06661SMatthias Ringwald 2189fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 21903d50b4baSMatthias Ringwald void hci_register_acl_packet_handler(btstack_packet_handler_t handler){ 2191fb37a842SMatthias Ringwald hci_stack->acl_packet_handler = handler; 219216833f0aSmatthias.ringwald } 219316833f0aSmatthias.ringwald 219435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 21958abbe8b5SMatthias Ringwald /** 21968abbe8b5SMatthias Ringwald * @brief Registers a packet handler for SCO data. Used for HSP and HFP profiles. 21978abbe8b5SMatthias Ringwald */ 21983d50b4baSMatthias Ringwald void hci_register_sco_packet_handler(btstack_packet_handler_t handler){ 21998abbe8b5SMatthias Ringwald hci_stack->sco_packet_handler = handler; 22008abbe8b5SMatthias Ringwald } 220135454696SMatthias Ringwald #endif 22028abbe8b5SMatthias Ringwald 220371de195eSMatthias Ringwald static void hci_state_reset(void){ 2204595bdbfbS[email protected] // no connections yet 2205595bdbfbS[email protected] hci_stack->connections = NULL; 220674308b23Smatthias.ringwald 220774308b23Smatthias.ringwald // keep discoverable/connectable as this has been requested by the client(s) 220874308b23Smatthias.ringwald // hci_stack->discoverable = 0; 220974308b23Smatthias.ringwald // hci_stack->connectable = 0; 221074308b23Smatthias.ringwald // hci_stack->bondable = 1; 2211b95a5a35SMatthias Ringwald // hci_stack->own_addr_type = 0; 2212595bdbfbS[email protected] 221344935e40S[email protected] // buffer is free 221444935e40S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 221544935e40S[email protected] 2216595bdbfbS[email protected] // no pending cmds 2217595bdbfbS[email protected] hci_stack->decline_reason = 0; 2218595bdbfbS[email protected] hci_stack->new_scan_enable_value = 0xff; 2219595bdbfbS[email protected] 2220595bdbfbS[email protected] // LE 2221b95a5a35SMatthias Ringwald #ifdef ENABLE_BLE 2222b95a5a35SMatthias Ringwald memset(hci_stack->le_random_address, 0, 6); 2223b95a5a35SMatthias Ringwald hci_stack->le_random_address_set = 0; 2224d70217a2SMatthias Ringwald #endif 2225d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 2226595bdbfbS[email protected] hci_stack->le_scanning_state = LE_SCAN_IDLE; 2227e2602ea2Smatthias.ringwald hci_stack->le_scan_type = 0xff; 2228b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 2229e83201bcSMatthias Ringwald hci_stack->le_whitelist = 0; 2230e83201bcSMatthias Ringwald hci_stack->le_whitelist_capacity = 0; 2231d70217a2SMatthias Ringwald #endif 2232d70217a2SMatthias Ringwald 2233e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_interval_min = 6; 2234e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_interval_max = 3200; 2235e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_latency_min = 0; 2236e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_latency_max = 500; 2237e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_supervision_timeout_min = 10; 2238e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_supervision_timeout_max = 3200; 2239595bdbfbS[email protected] } 2240595bdbfbS[email protected] 224135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 22422d5e09d6SMatthias Ringwald /** 22432d5e09d6SMatthias Ringwald * @brief Configure Bluetooth hardware control. Has to be called before power on. 22442d5e09d6SMatthias Ringwald */ 22452d5e09d6SMatthias Ringwald void hci_set_link_key_db(btstack_link_key_db_t const * link_key_db){ 22462d5e09d6SMatthias Ringwald // store and open remote device db 22472d5e09d6SMatthias Ringwald hci_stack->link_key_db = link_key_db; 22482d5e09d6SMatthias Ringwald if (hci_stack->link_key_db) { 22492d5e09d6SMatthias Ringwald hci_stack->link_key_db->open(); 22502d5e09d6SMatthias Ringwald } 22512d5e09d6SMatthias Ringwald } 225235454696SMatthias Ringwald #endif 22532d5e09d6SMatthias Ringwald 22542d5e09d6SMatthias Ringwald void hci_init(const hci_transport_t *transport, const void *config){ 2255475c8125Smatthias.ringwald 22563a9fb326S[email protected] #ifdef HAVE_MALLOC 22573a9fb326S[email protected] if (!hci_stack) { 22583a9fb326S[email protected] hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t)); 22593a9fb326S[email protected] } 22603a9fb326S[email protected] #else 22613a9fb326S[email protected] hci_stack = &hci_stack_static; 22623a9fb326S[email protected] #endif 226366fb9560S[email protected] memset(hci_stack, 0, sizeof(hci_stack_t)); 22643a9fb326S[email protected] 2265475c8125Smatthias.ringwald // reference to use transport layer implementation 22663a9fb326S[email protected] hci_stack->hci_transport = transport; 2267475c8125Smatthias.ringwald 226811e23e5fSmatthias.ringwald // reference to used config 22693a9fb326S[email protected] hci_stack->config = config; 227011e23e5fSmatthias.ringwald 227126a9b6daSMatthias Ringwald // setup pointer for outgoing packet buffer 227226a9b6daSMatthias Ringwald hci_stack->hci_packet_buffer = &hci_stack->hci_packet_buffer_data[HCI_OUTGOING_PRE_BUFFER_SIZE]; 227326a9b6daSMatthias Ringwald 22748fcba05dSmatthias.ringwald // max acl payload size defined in config.h 22753a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 22768fcba05dSmatthias.ringwald 227716833f0aSmatthias.ringwald // register packet handlers with transport 227810e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 2279f5454fc6Smatthias.ringwald 22803a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 2281e2386ba1S[email protected] 2282e2386ba1S[email protected] // class of device 22833a9fb326S[email protected] hci_stack->class_of_device = 0x007a020c; // Smartphone 2284a45d6b9fS[email protected] 2285f20168b8Smatthias.ringwald // bondable by default 2286f20168b8Smatthias.ringwald hci_stack->bondable = 1; 2287f20168b8Smatthias.ringwald 228863048403S[email protected] // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept 22893a9fb326S[email protected] hci_stack->ssp_enable = 1; 22903a9fb326S[email protected] hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT; 22913a9fb326S[email protected] hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING; 22923a9fb326S[email protected] hci_stack->ssp_auto_accept = 1; 229369a97523S[email protected] 2294fac2e2feSMatthias Ringwald // voice setting - signed 16 bit pcm data with CVSD over the air 2295fac2e2feSMatthias Ringwald hci_stack->sco_voice_setting = 0x60; 2296d950d659SMatthias Ringwald 2297595bdbfbS[email protected] hci_state_reset(); 2298475c8125Smatthias.ringwald } 2299475c8125Smatthias.ringwald 23003fb36a29SMatthias Ringwald /** 23013fb36a29SMatthias Ringwald * @brief Configure Bluetooth chipset driver. Has to be called before power on, or right after receiving the local version information 23023fb36a29SMatthias Ringwald */ 23033fb36a29SMatthias Ringwald void hci_set_chipset(const btstack_chipset_t *chipset_driver){ 23043fb36a29SMatthias Ringwald hci_stack->chipset = chipset_driver; 23053fb36a29SMatthias Ringwald 23063fb36a29SMatthias Ringwald // reset chipset driver - init is also called on power_up 23073fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->init){ 23083fb36a29SMatthias Ringwald hci_stack->chipset->init(hci_stack->config); 23093fb36a29SMatthias Ringwald } 23103fb36a29SMatthias Ringwald } 23113fb36a29SMatthias Ringwald 2312fb55bd0aSMatthias Ringwald /** 2313d0b87befSMatthias Ringwald * @brief Configure Bluetooth hardware control. Has to be called after hci_init() but before power on. 2314fb55bd0aSMatthias Ringwald */ 2315fb55bd0aSMatthias Ringwald void hci_set_control(const btstack_control_t *hardware_control){ 2316fb55bd0aSMatthias Ringwald // references to used control implementation 2317fb55bd0aSMatthias Ringwald hci_stack->control = hardware_control; 2318d0b87befSMatthias Ringwald // init with transport config 2319d0b87befSMatthias Ringwald hardware_control->init(hci_stack->config); 2320fb55bd0aSMatthias Ringwald } 2321fb55bd0aSMatthias Ringwald 232271de195eSMatthias Ringwald void hci_close(void){ 2323404843c1Smatthias.ringwald // close remote device db 2324a98592bcSMatthias Ringwald if (hci_stack->link_key_db) { 2325a98592bcSMatthias Ringwald hci_stack->link_key_db->close(); 2326404843c1Smatthias.ringwald } 23277224be7eSMatthias Ringwald 23287224be7eSMatthias Ringwald btstack_linked_list_iterator_t lit; 23297224be7eSMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->connections); 23307224be7eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 23317224be7eSMatthias Ringwald // cancel all l2cap connections by emitting dicsconnection complete before shutdown (free) connection 23327224be7eSMatthias Ringwald hci_connection_t * connection = (hci_connection_t*) btstack_linked_list_iterator_next(&lit); 23337224be7eSMatthias Ringwald hci_emit_disconnection_complete(connection->con_handle, 0x16); // terminated by local host 23347224be7eSMatthias Ringwald hci_shutdown_connection(connection); 2335f5454fc6Smatthias.ringwald } 23367224be7eSMatthias Ringwald 2337f5454fc6Smatthias.ringwald hci_power_control(HCI_POWER_OFF); 23383a9fb326S[email protected] 23393a9fb326S[email protected] #ifdef HAVE_MALLOC 23403a9fb326S[email protected] free(hci_stack); 23413a9fb326S[email protected] #endif 23423a9fb326S[email protected] hci_stack = NULL; 2343404843c1Smatthias.ringwald } 2344404843c1Smatthias.ringwald 234535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 234660b9e82fSMatthias Ringwald void gap_set_class_of_device(uint32_t class_of_device){ 23479e61646fS[email protected] hci_stack->class_of_device = class_of_device; 23489e61646fS[email protected] } 234976f27cffSMatthias Ringwald 235076f27cffSMatthias Ringwald void hci_disable_l2cap_timeout_check(void){ 235176f27cffSMatthias Ringwald disable_l2cap_timeouts = 1; 235276f27cffSMatthias Ringwald } 235335454696SMatthias Ringwald #endif 23549e61646fS[email protected] 235576f27cffSMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 2356f456b2d0S[email protected] // Set Public BD ADDR - passed on to Bluetooth chipset if supported in bt_control_h 2357f456b2d0S[email protected] void hci_set_bd_addr(bd_addr_t addr){ 2358f456b2d0S[email protected] memcpy(hci_stack->custom_bd_addr, addr, 6); 2359f456b2d0S[email protected] hci_stack->custom_bd_addr_set = 1; 2360f456b2d0S[email protected] } 236176f27cffSMatthias Ringwald #endif 2362f456b2d0S[email protected] 23638d213e1aSmatthias.ringwald // State-Module-Driver overview 23648d213e1aSmatthias.ringwald // state module low-level 23658d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 23668d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 23678d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 23688d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 2369d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 2370d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 2371c7e0c5f6Smatthias.ringwald 237240d1c7a4Smatthias.ringwald static int hci_power_control_on(void){ 23737301ad89Smatthias.ringwald 2374038bc64cSmatthias.ringwald // power on 2375f9a30166Smatthias.ringwald int err = 0; 23763a9fb326S[email protected] if (hci_stack->control && hci_stack->control->on){ 2377d0b87befSMatthias Ringwald err = (*hci_stack->control->on)(); 2378f9a30166Smatthias.ringwald } 2379038bc64cSmatthias.ringwald if (err){ 23809da54300S[email protected] log_error( "POWER_ON failed"); 2381038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 2382038bc64cSmatthias.ringwald return err; 2383038bc64cSmatthias.ringwald } 2384038bc64cSmatthias.ringwald 238524b3c629SMatthias Ringwald // int chipset driver 23863fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->init){ 23873fb36a29SMatthias Ringwald hci_stack->chipset->init(hci_stack->config); 23883fb36a29SMatthias Ringwald } 23893fb36a29SMatthias Ringwald 239024b3c629SMatthias Ringwald // init transport 239124b3c629SMatthias Ringwald if (hci_stack->hci_transport->init){ 239224b3c629SMatthias Ringwald hci_stack->hci_transport->init(hci_stack->config); 239324b3c629SMatthias Ringwald } 239424b3c629SMatthias Ringwald 239524b3c629SMatthias Ringwald // open transport 239624b3c629SMatthias Ringwald err = hci_stack->hci_transport->open(); 2397038bc64cSmatthias.ringwald if (err){ 23989da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 23993a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 2400d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 2401f9a30166Smatthias.ringwald } 2402038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 2403038bc64cSmatthias.ringwald return err; 2404038bc64cSmatthias.ringwald } 24058d213e1aSmatthias.ringwald return 0; 24068d213e1aSmatthias.ringwald } 2407038bc64cSmatthias.ringwald 240840d1c7a4Smatthias.ringwald static void hci_power_control_off(void){ 24098d213e1aSmatthias.ringwald 24109da54300S[email protected] log_info("hci_power_control_off"); 24119418f9c9Smatthias.ringwald 24128d213e1aSmatthias.ringwald // close low-level device 241324b3c629SMatthias Ringwald hci_stack->hci_transport->close(); 24148d213e1aSmatthias.ringwald 24159da54300S[email protected] log_info("hci_power_control_off - hci_transport closed"); 24169418f9c9Smatthias.ringwald 24178d213e1aSmatthias.ringwald // power off 24183a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 2419d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 24208d213e1aSmatthias.ringwald } 24219418f9c9Smatthias.ringwald 24229da54300S[email protected] log_info("hci_power_control_off - control closed"); 24239418f9c9Smatthias.ringwald 24243a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 242572ea5239Smatthias.ringwald } 242672ea5239Smatthias.ringwald 242740d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){ 242872ea5239Smatthias.ringwald 24299da54300S[email protected] log_info("hci_power_control_sleep"); 24303144bce4Smatthias.ringwald 2431b429b9b7Smatthias.ringwald #if 0 2432b429b9b7Smatthias.ringwald // don't close serial port during sleep 2433b429b9b7Smatthias.ringwald 243472ea5239Smatthias.ringwald // close low-level device 24353a9fb326S[email protected] hci_stack->hci_transport->close(hci_stack->config); 2436b429b9b7Smatthias.ringwald #endif 243772ea5239Smatthias.ringwald 243872ea5239Smatthias.ringwald // sleep mode 24393a9fb326S[email protected] if (hci_stack->control && hci_stack->control->sleep){ 2440d0b87befSMatthias Ringwald (*hci_stack->control->sleep)(); 244172ea5239Smatthias.ringwald } 2442b429b9b7Smatthias.ringwald 24433a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 24448d213e1aSmatthias.ringwald } 24458d213e1aSmatthias.ringwald 244640d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){ 2447b429b9b7Smatthias.ringwald 24489da54300S[email protected] log_info("hci_power_control_wake"); 2449b429b9b7Smatthias.ringwald 2450b429b9b7Smatthias.ringwald // wake on 24513a9fb326S[email protected] if (hci_stack->control && hci_stack->control->wake){ 2452d0b87befSMatthias Ringwald (*hci_stack->control->wake)(); 2453b429b9b7Smatthias.ringwald } 2454b429b9b7Smatthias.ringwald 2455b429b9b7Smatthias.ringwald #if 0 2456b429b9b7Smatthias.ringwald // open low-level device 24573a9fb326S[email protected] int err = hci_stack->hci_transport->open(hci_stack->config); 2458b429b9b7Smatthias.ringwald if (err){ 24599da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 24603a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 2461d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 2462b429b9b7Smatthias.ringwald } 2463b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 2464b429b9b7Smatthias.ringwald return err; 2465b429b9b7Smatthias.ringwald } 2466b429b9b7Smatthias.ringwald #endif 2467b429b9b7Smatthias.ringwald 2468b429b9b7Smatthias.ringwald return 0; 2469b429b9b7Smatthias.ringwald } 2470b429b9b7Smatthias.ringwald 247144935e40S[email protected] static void hci_power_transition_to_initializing(void){ 247244935e40S[email protected] // set up state machine 247344935e40S[email protected] hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent 247444935e40S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 247544935e40S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 24765c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET; 247744935e40S[email protected] } 2478b429b9b7Smatthias.ringwald 24798d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 2480d661ed19Smatthias.ringwald 2481f04a0c31SMatthias Ringwald log_info("hci_power_control: %d, current mode %u", power_mode, hci_stack->state); 2482d661ed19Smatthias.ringwald 24838d213e1aSmatthias.ringwald int err = 0; 24843a9fb326S[email protected] switch (hci_stack->state){ 24858d213e1aSmatthias.ringwald 24868d213e1aSmatthias.ringwald case HCI_STATE_OFF: 24878d213e1aSmatthias.ringwald switch (power_mode){ 24888d213e1aSmatthias.ringwald case HCI_POWER_ON: 24898d213e1aSmatthias.ringwald err = hci_power_control_on(); 249097b61c7bS[email protected] if (err) { 2491f04a0c31SMatthias Ringwald log_error("hci_power_control_on() error %d", err); 249297b61c7bS[email protected] return err; 249397b61c7bS[email protected] } 249444935e40S[email protected] hci_power_transition_to_initializing(); 24958d213e1aSmatthias.ringwald break; 24968d213e1aSmatthias.ringwald case HCI_POWER_OFF: 24978d213e1aSmatthias.ringwald // do nothing 24988d213e1aSmatthias.ringwald break; 24998d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2500b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 25018d213e1aSmatthias.ringwald break; 25028d213e1aSmatthias.ringwald } 25038d213e1aSmatthias.ringwald break; 25047301ad89Smatthias.ringwald 25058d213e1aSmatthias.ringwald case HCI_STATE_INITIALIZING: 25068d213e1aSmatthias.ringwald switch (power_mode){ 25078d213e1aSmatthias.ringwald case HCI_POWER_ON: 25088d213e1aSmatthias.ringwald // do nothing 25098d213e1aSmatthias.ringwald break; 25108d213e1aSmatthias.ringwald case HCI_POWER_OFF: 25118d213e1aSmatthias.ringwald // no connections yet, just turn it off 25128d213e1aSmatthias.ringwald hci_power_control_off(); 25138d213e1aSmatthias.ringwald break; 25148d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2515b546ac54Smatthias.ringwald // no connections yet, just turn it off 251672ea5239Smatthias.ringwald hci_power_control_sleep(); 25178d213e1aSmatthias.ringwald break; 25188d213e1aSmatthias.ringwald } 25198d213e1aSmatthias.ringwald break; 25207301ad89Smatthias.ringwald 25218d213e1aSmatthias.ringwald case HCI_STATE_WORKING: 25228d213e1aSmatthias.ringwald switch (power_mode){ 25238d213e1aSmatthias.ringwald case HCI_POWER_ON: 25248d213e1aSmatthias.ringwald // do nothing 25258d213e1aSmatthias.ringwald break; 25268d213e1aSmatthias.ringwald case HCI_POWER_OFF: 2527c7e0c5f6Smatthias.ringwald // see hci_run 25283a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 25298d213e1aSmatthias.ringwald break; 25308d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2531b546ac54Smatthias.ringwald // see hci_run 25323a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 253374b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT; 25348d213e1aSmatthias.ringwald break; 25358d213e1aSmatthias.ringwald } 25368d213e1aSmatthias.ringwald break; 25377301ad89Smatthias.ringwald 25388d213e1aSmatthias.ringwald case HCI_STATE_HALTING: 25398d213e1aSmatthias.ringwald switch (power_mode){ 25408d213e1aSmatthias.ringwald case HCI_POWER_ON: 254144935e40S[email protected] hci_power_transition_to_initializing(); 25428d213e1aSmatthias.ringwald break; 25438d213e1aSmatthias.ringwald case HCI_POWER_OFF: 25448d213e1aSmatthias.ringwald // do nothing 25458d213e1aSmatthias.ringwald break; 25468d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2547b546ac54Smatthias.ringwald // see hci_run 25483a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 254974b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT; 25508d213e1aSmatthias.ringwald break; 25518d213e1aSmatthias.ringwald } 25528d213e1aSmatthias.ringwald break; 25538d213e1aSmatthias.ringwald 25548d213e1aSmatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 25558d213e1aSmatthias.ringwald switch (power_mode){ 25568d213e1aSmatthias.ringwald case HCI_POWER_ON: 255728171530Smatthias.ringwald 2558423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 255928171530Smatthias.ringwald // nothing to do, if H4 supports power management 2560d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 25613a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 256274b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_WRITE_SCAN_ENABLE; // init after sleep 256328171530Smatthias.ringwald break; 256428171530Smatthias.ringwald } 256528171530Smatthias.ringwald #endif 256644935e40S[email protected] hci_power_transition_to_initializing(); 25678d213e1aSmatthias.ringwald break; 25688d213e1aSmatthias.ringwald case HCI_POWER_OFF: 2569b546ac54Smatthias.ringwald // see hci_run 25703a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 25718d213e1aSmatthias.ringwald break; 25728d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2573b546ac54Smatthias.ringwald // do nothing 25748d213e1aSmatthias.ringwald break; 25758d213e1aSmatthias.ringwald } 25768d213e1aSmatthias.ringwald break; 25778d213e1aSmatthias.ringwald 25788d213e1aSmatthias.ringwald case HCI_STATE_SLEEPING: 25798d213e1aSmatthias.ringwald switch (power_mode){ 25808d213e1aSmatthias.ringwald case HCI_POWER_ON: 258128171530Smatthias.ringwald 2582423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 258328171530Smatthias.ringwald // nothing to do, if H4 supports power management 2584d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 25853a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 25865c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_AFTER_SLEEP; 2587758b46ceSmatthias.ringwald hci_update_scan_enable(); 258828171530Smatthias.ringwald break; 258928171530Smatthias.ringwald } 259028171530Smatthias.ringwald #endif 25913144bce4Smatthias.ringwald err = hci_power_control_wake(); 25923144bce4Smatthias.ringwald if (err) return err; 259344935e40S[email protected] hci_power_transition_to_initializing(); 25948d213e1aSmatthias.ringwald break; 25958d213e1aSmatthias.ringwald case HCI_POWER_OFF: 25963a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 25978d213e1aSmatthias.ringwald break; 25988d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2599b546ac54Smatthias.ringwald // do nothing 26008d213e1aSmatthias.ringwald break; 26018d213e1aSmatthias.ringwald } 26028d213e1aSmatthias.ringwald break; 260311e23e5fSmatthias.ringwald } 260468d92d03Smatthias.ringwald 2605038bc64cSmatthias.ringwald // create internal event 2606ee8bf225Smatthias.ringwald hci_emit_state(); 2607ee8bf225Smatthias.ringwald 260868d92d03Smatthias.ringwald // trigger next/first action 260968d92d03Smatthias.ringwald hci_run(); 261068d92d03Smatthias.ringwald 2611475c8125Smatthias.ringwald return 0; 2612475c8125Smatthias.ringwald } 2613475c8125Smatthias.ringwald 261435454696SMatthias Ringwald 261535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 261635454696SMatthias Ringwald 2617758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){ 2618758b46ceSmatthias.ringwald // 2 = page scan, 1 = inq scan 26193a9fb326S[email protected] hci_stack->new_scan_enable_value = hci_stack->connectable << 1 | hci_stack->discoverable; 2620758b46ceSmatthias.ringwald hci_run(); 2621758b46ceSmatthias.ringwald } 2622758b46ceSmatthias.ringwald 262315a95bd5SMatthias Ringwald void gap_discoverable_control(uint8_t enable){ 2624381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 2625381fbed8Smatthias.ringwald 26263a9fb326S[email protected] if (hci_stack->discoverable == enable){ 26273a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 2628381fbed8Smatthias.ringwald return; 2629381fbed8Smatthias.ringwald } 2630381fbed8Smatthias.ringwald 26313a9fb326S[email protected] hci_stack->discoverable = enable; 2632758b46ceSmatthias.ringwald hci_update_scan_enable(); 2633758b46ceSmatthias.ringwald } 2634b031bebbSmatthias.ringwald 263515a95bd5SMatthias Ringwald void gap_connectable_control(uint8_t enable){ 2636758b46ceSmatthias.ringwald if (enable) enable = 1; // normalize argument 2637758b46ceSmatthias.ringwald 2638758b46ceSmatthias.ringwald // don't emit event 26393a9fb326S[email protected] if (hci_stack->connectable == enable) return; 2640758b46ceSmatthias.ringwald 26413a9fb326S[email protected] hci_stack->connectable = enable; 2642758b46ceSmatthias.ringwald hci_update_scan_enable(); 2643381fbed8Smatthias.ringwald } 264435454696SMatthias Ringwald #endif 2645381fbed8Smatthias.ringwald 264615a95bd5SMatthias Ringwald void gap_local_bd_addr(bd_addr_t address_buffer){ 2647690bd0baS[email protected] memcpy(address_buffer, hci_stack->local_bd_addr, 6); 26485061f3afS[email protected] } 26495061f3afS[email protected] 2650*2b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 2651*2b838201SMatthias Ringwald static void hci_host_num_completed_packets(void){ 2652*2b838201SMatthias Ringwald 2653*2b838201SMatthias Ringwald // create packet manually as arrays are not supported and num_commands should not get reduced 2654*2b838201SMatthias Ringwald hci_reserve_packet_buffer(); 2655*2b838201SMatthias Ringwald uint8_t * packet = hci_get_outgoing_packet_buffer(); 2656*2b838201SMatthias Ringwald 2657*2b838201SMatthias Ringwald uint16_t size = 0; 2658*2b838201SMatthias Ringwald uint16_t num_handles = 0; 2659*2b838201SMatthias Ringwald packet[size++] = 0x35; 2660*2b838201SMatthias Ringwald packet[size++] = 0x0c; 2661*2b838201SMatthias Ringwald size++; // skip param len 2662*2b838201SMatthias Ringwald size++; // skip num handles 2663*2b838201SMatthias Ringwald 2664*2b838201SMatthias Ringwald // add { handle, packets } entries 2665*2b838201SMatthias Ringwald btstack_linked_item_t * it; 2666*2b838201SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 2667*2b838201SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 2668*2b838201SMatthias Ringwald if (connection->num_packets_completed){ 2669*2b838201SMatthias Ringwald little_endian_store_16(packet, size, connection->con_handle); 2670*2b838201SMatthias Ringwald size += 2; 2671*2b838201SMatthias Ringwald little_endian_store_16(packet, size, connection->num_packets_completed); 2672*2b838201SMatthias Ringwald size += 2; 2673*2b838201SMatthias Ringwald // 2674*2b838201SMatthias Ringwald num_handles++; 2675*2b838201SMatthias Ringwald connection->num_packets_completed = 0; 2676*2b838201SMatthias Ringwald } 2677*2b838201SMatthias Ringwald } 2678*2b838201SMatthias Ringwald 2679*2b838201SMatthias Ringwald packet[2] = size - 3; 2680*2b838201SMatthias Ringwald packet[3] = num_handles; 2681*2b838201SMatthias Ringwald 2682*2b838201SMatthias Ringwald hci_stack->host_completed_packets = 0; 2683*2b838201SMatthias Ringwald 2684*2b838201SMatthias Ringwald hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size); 2685*2b838201SMatthias Ringwald hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 2686*2b838201SMatthias Ringwald 2687*2b838201SMatthias Ringwald // release packet buffer for synchronous transport implementations 2688*2b838201SMatthias Ringwald if (hci_transport_synchronous()){ 2689*2b838201SMatthias Ringwald hci_stack->hci_packet_buffer_reserved = 0; 2690*2b838201SMatthias Ringwald } 2691*2b838201SMatthias Ringwald } 2692*2b838201SMatthias Ringwald #endif 2693*2b838201SMatthias Ringwald 269495d71764SMatthias Ringwald static void hci_run(void){ 26958a485f27Smatthias.ringwald 2696db8bc6ffSMatthias Ringwald // log_info("hci_run: entered"); 2697665d90f2SMatthias Ringwald btstack_linked_item_t * it; 269832ab9390Smatthias.ringwald 2699b5d8b22bS[email protected] // send continuation fragments first, as they block the prepared packet buffer 2700b5d8b22bS[email protected] if (hci_stack->acl_fragmentation_total_size > 0) { 2701b5d8b22bS[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer); 2702b5d8b22bS[email protected] hci_connection_t *connection = hci_connection_for_handle(con_handle); 2703b5d8b22bS[email protected] if (connection) { 270428a0332dSMatthias Ringwald if (hci_can_send_prepared_acl_packet_now(con_handle)){ 2705b5d8b22bS[email protected] hci_send_acl_packet_fragments(connection); 2706b5d8b22bS[email protected] return; 2707b5d8b22bS[email protected] } 270828a0332dSMatthias Ringwald } else { 2709b5d8b22bS[email protected] // connection gone -> discard further fragments 271028a0332dSMatthias Ringwald log_info("hci_run: fragmented ACL packet no connection -> discard fragment"); 2711b5d8b22bS[email protected] hci_stack->acl_fragmentation_total_size = 0; 2712b5d8b22bS[email protected] hci_stack->acl_fragmentation_pos = 0; 2713b5d8b22bS[email protected] } 2714b5d8b22bS[email protected] } 2715b5d8b22bS[email protected] 2716*2b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 2717*2b838201SMatthias Ringwald // send host num completed packets next as they don't require num_cmd_packets > 0 2718*2b838201SMatthias Ringwald if (!hci_can_send_comand_packet_transport()) return; 2719*2b838201SMatthias Ringwald if (hci_stack->host_completed_packets){ 2720*2b838201SMatthias Ringwald hci_host_num_completed_packets(); 2721*2b838201SMatthias Ringwald return; 2722*2b838201SMatthias Ringwald } 2723*2b838201SMatthias Ringwald #endif 2724*2b838201SMatthias Ringwald 2725d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 2726ce4c8fabSmatthias.ringwald 2727b031bebbSmatthias.ringwald // global/non-connection oriented commands 2728b031bebbSmatthias.ringwald 272935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2730b031bebbSmatthias.ringwald // decline incoming connections 27313a9fb326S[email protected] if (hci_stack->decline_reason){ 27323a9fb326S[email protected] uint8_t reason = hci_stack->decline_reason; 27333a9fb326S[email protected] hci_stack->decline_reason = 0; 27343a9fb326S[email protected] hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason); 2735dbe1a790S[email protected] return; 2736ce4c8fabSmatthias.ringwald } 2737ce4c8fabSmatthias.ringwald 2738b031bebbSmatthias.ringwald // send scan enable 27393a9fb326S[email protected] if (hci_stack->state == HCI_STATE_WORKING && hci_stack->new_scan_enable_value != 0xff && hci_classic_supported()){ 27403a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value); 27413a9fb326S[email protected] hci_stack->new_scan_enable_value = 0xff; 2742dbe1a790S[email protected] return; 2743b031bebbSmatthias.ringwald } 274435454696SMatthias Ringwald #endif 2745b031bebbSmatthias.ringwald 2746a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 2747b95a5a35SMatthias Ringwald // advertisements, active scanning, and creating connections requires randaom address to be set if using private address 2748b95a5a35SMatthias Ringwald if ((hci_stack->state == HCI_STATE_WORKING) 2749b95a5a35SMatthias Ringwald && (hci_stack->le_own_addr_type == BD_ADDR_TYPE_LE_PUBLIC || hci_stack->le_random_address_set)){ 2750d70217a2SMatthias Ringwald 2751d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 275245c102fdSMatthias Ringwald // handle le scan 27537bdc6798S[email protected] switch(hci_stack->le_scanning_state){ 27547bdc6798S[email protected] case LE_START_SCAN: 27557bdc6798S[email protected] hci_stack->le_scanning_state = LE_SCANNING; 27567bdc6798S[email protected] hci_send_cmd(&hci_le_set_scan_enable, 1, 0); 27577bdc6798S[email protected] return; 27587bdc6798S[email protected] 27597bdc6798S[email protected] case LE_STOP_SCAN: 27607bdc6798S[email protected] hci_stack->le_scanning_state = LE_SCAN_IDLE; 27617bdc6798S[email protected] hci_send_cmd(&hci_le_set_scan_enable, 0, 0); 27627bdc6798S[email protected] return; 27637bdc6798S[email protected] default: 27647bdc6798S[email protected] break; 27657bdc6798S[email protected] } 2766e2602ea2Smatthias.ringwald if (hci_stack->le_scan_type != 0xff){ 2767e2602ea2Smatthias.ringwald // defaults: active scanning, accept all advertisement packets 2768e2602ea2Smatthias.ringwald int scan_type = hci_stack->le_scan_type; 2769e2602ea2Smatthias.ringwald hci_stack->le_scan_type = 0xff; 2770b95a5a35SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_parameters, scan_type, hci_stack->le_scan_interval, hci_stack->le_scan_window, hci_stack->le_own_addr_type, 0); 2771e2602ea2Smatthias.ringwald return; 2772e2602ea2Smatthias.ringwald } 2773d70217a2SMatthias Ringwald #endif 2774d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 277545c102fdSMatthias Ringwald // le advertisement control 27769a2e4658SMatthias Ringwald if (hci_stack->le_advertisements_todo){ 27779a2e4658SMatthias Ringwald log_info("hci_run: gap_le: adv todo: %x", hci_stack->le_advertisements_todo ); 27789a2e4658SMatthias Ringwald } 277945c102fdSMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_DISABLE){ 278045c102fdSMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_DISABLE; 278145c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 0); 278245c102fdSMatthias Ringwald return; 278345c102fdSMatthias Ringwald } 278445c102fdSMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_PARAMS){ 278545c102fdSMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_PARAMS; 278645c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_parameters, 278745c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_min, 278845c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_max, 278945c102fdSMatthias Ringwald hci_stack->le_advertisements_type, 2790b95a5a35SMatthias Ringwald hci_stack->le_own_addr_type, 279145c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address_type, 279245c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address, 279345c102fdSMatthias Ringwald hci_stack->le_advertisements_channel_map, 279445c102fdSMatthias Ringwald hci_stack->le_advertisements_filter_policy); 279545c102fdSMatthias Ringwald return; 279645c102fdSMatthias Ringwald } 2797501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_ADV_DATA){ 2798501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 2799b95a5a35SMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_data, hci_stack->le_advertisements_data_len, hci_stack->le_advertisements_data); 280045c102fdSMatthias Ringwald return; 280145c102fdSMatthias Ringwald } 2802501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA){ 2803501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 2804501f56b3SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_response_data, hci_stack->le_scan_response_data_len, 2805501f56b3SMatthias Ringwald hci_stack->le_scan_response_data); 2806501f56b3SMatthias Ringwald return; 2807501f56b3SMatthias Ringwald } 2808b95a5a35SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_ENABLE){ 280945c102fdSMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_ENABLE; 281045c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 1); 281145c102fdSMatthias Ringwald return; 281245c102fdSMatthias Ringwald } 2813d70217a2SMatthias Ringwald #endif 28149956955bSMatthias Ringwald 2815d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 28169956955bSMatthias Ringwald // 28179956955bSMatthias Ringwald // LE Whitelist Management 28189956955bSMatthias Ringwald // 28199956955bSMatthias Ringwald 28209956955bSMatthias Ringwald // check if whitelist needs modification 2821665d90f2SMatthias Ringwald btstack_linked_list_iterator_t lit; 28229956955bSMatthias Ringwald int modification_pending = 0; 2823665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 2824665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 2825665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 28269956955bSMatthias Ringwald if (entry->state & (LE_WHITELIST_REMOVE_FROM_CONTROLLER | LE_WHITELIST_ADD_TO_CONTROLLER)){ 28279956955bSMatthias Ringwald modification_pending = 1; 28289956955bSMatthias Ringwald break; 28299956955bSMatthias Ringwald } 28309956955bSMatthias Ringwald } 283191915b0bSMatthias Ringwald 28329956955bSMatthias Ringwald if (modification_pending){ 283391915b0bSMatthias Ringwald // stop connnecting if modification pending 28349956955bSMatthias Ringwald if (hci_stack->le_connecting_state != LE_CONNECTING_IDLE){ 28359956955bSMatthias Ringwald hci_send_cmd(&hci_le_create_connection_cancel); 28369956955bSMatthias Ringwald return; 28379956955bSMatthias Ringwald } 28389956955bSMatthias Ringwald 28399956955bSMatthias Ringwald // add/remove entries 2840665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 2841665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 2842665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 28439956955bSMatthias Ringwald if (entry->state & LE_WHITELIST_ADD_TO_CONTROLLER){ 28449956955bSMatthias Ringwald entry->state = LE_WHITELIST_ON_CONTROLLER; 28459956955bSMatthias Ringwald hci_send_cmd(&hci_le_add_device_to_white_list, entry->address_type, entry->address); 28469956955bSMatthias Ringwald return; 28479956955bSMatthias Ringwald 28489956955bSMatthias Ringwald } 28499956955bSMatthias Ringwald if (entry->state & LE_WHITELIST_REMOVE_FROM_CONTROLLER){ 28509ecbe201SMatthias Ringwald bd_addr_t address; 28519ecbe201SMatthias Ringwald bd_addr_type_t address_type = entry->address_type; 28529ecbe201SMatthias Ringwald memcpy(address, entry->address, 6); 2853665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry); 28549956955bSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 28559ecbe201SMatthias Ringwald hci_send_cmd(&hci_le_remove_device_from_white_list, address_type, address); 28569956955bSMatthias Ringwald return; 28579956955bSMatthias Ringwald } 28589956955bSMatthias Ringwald } 285991915b0bSMatthias Ringwald } 28609956955bSMatthias Ringwald 28619956955bSMatthias Ringwald // start connecting 286291915b0bSMatthias Ringwald if ( hci_stack->le_connecting_state == LE_CONNECTING_IDLE && 2863665d90f2SMatthias Ringwald !btstack_linked_list_empty(&hci_stack->le_whitelist)){ 28649956955bSMatthias Ringwald bd_addr_t null_addr; 28659956955bSMatthias Ringwald memset(null_addr, 0, 6); 28669956955bSMatthias Ringwald hci_send_cmd(&hci_le_create_connection, 28679956955bSMatthias Ringwald 0x0060, // scan interval: 60 ms 28689956955bSMatthias Ringwald 0x0030, // scan interval: 30 ms 28699956955bSMatthias Ringwald 1, // use whitelist 28709956955bSMatthias Ringwald 0, // peer address type 28719956955bSMatthias Ringwald null_addr, // peer bd addr 2872b95a5a35SMatthias Ringwald hci_stack->le_own_addr_type, // our addr type: 28739956955bSMatthias Ringwald 0x0008, // conn interval min 28749956955bSMatthias Ringwald 0x0018, // conn interval max 28759956955bSMatthias Ringwald 0, // conn latency 28769956955bSMatthias Ringwald 0x0048, // supervision timeout 28779956955bSMatthias Ringwald 0x0001, // min ce length 28789956955bSMatthias Ringwald 0x0001 // max ce length 28799956955bSMatthias Ringwald ); 28809956955bSMatthias Ringwald return; 28819956955bSMatthias Ringwald } 2882d70217a2SMatthias Ringwald #endif 28837bdc6798S[email protected] } 2884b2f949feS[email protected] #endif 28857bdc6798S[email protected] 288632ab9390Smatthias.ringwald // send pending HCI commands 2887665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 288805ae8de3SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 288932ab9390Smatthias.ringwald 28900bf6344aS[email protected] switch(connection->state){ 28910bf6344aS[email protected] case SEND_CREATE_CONNECTION: 28924f3229d8S[email protected] switch(connection->address_type){ 289335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 28944f3229d8S[email protected] case BD_ADDR_TYPE_CLASSIC: 28959da54300S[email protected] log_info("sending hci_create_connection"); 2896ad83dc6aS[email protected] hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 28974f3229d8S[email protected] break; 289835454696SMatthias Ringwald #endif 28994f3229d8S[email protected] default: 2900a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 2901d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 29029da54300S[email protected] log_info("sending hci_le_create_connection"); 29034f3229d8S[email protected] hci_send_cmd(&hci_le_create_connection, 2904b2571179Smatthias.ringwald 0x0060, // scan interval: 60 ms 2905b2571179Smatthias.ringwald 0x0030, // scan interval: 30 ms 29064f3229d8S[email protected] 0, // don't use whitelist 29074f3229d8S[email protected] connection->address_type, // peer address type 29084f3229d8S[email protected] connection->address, // peer bd addr 2909b95a5a35SMatthias Ringwald hci_stack->le_own_addr_type, // our addr type: 2910b2571179Smatthias.ringwald 0x0008, // conn interval min 2911b2571179Smatthias.ringwald 0x0018, // conn interval max 29124f3229d8S[email protected] 0, // conn latency 2913b2571179Smatthias.ringwald 0x0048, // supervision timeout 2914b2571179Smatthias.ringwald 0x0001, // min ce length 2915b2571179Smatthias.ringwald 0x0001 // max ce length 29164f3229d8S[email protected] ); 29174f3229d8S[email protected] 29184f3229d8S[email protected] connection->state = SENT_CREATE_CONNECTION; 2919b2f949feS[email protected] #endif 2920d70217a2SMatthias Ringwald #endif 29214f3229d8S[email protected] break; 29224f3229d8S[email protected] } 2923ad83dc6aS[email protected] return; 2924ad83dc6aS[email protected] 292535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 29260bf6344aS[email protected] case RECEIVED_CONNECTION_REQUEST: 292794e4aaa2SMatthias Ringwald log_info("sending hci_accept_connection_request, remote eSCO %u", connection->remote_supported_feature_eSCO); 292832ab9390Smatthias.ringwald connection->state = ACCEPTED_CONNECTION_REQUEST; 29295cf766e8SMatthias Ringwald connection->role = HCI_ROLE_SLAVE; 2930e35edcc1S[email protected] if (connection->address_type == BD_ADDR_TYPE_CLASSIC){ 293134d2123cS[email protected] hci_send_cmd(&hci_accept_connection_request, connection->address, 1); 2932e35edcc1S[email protected] } 2933dbe1a790S[email protected] return; 293435454696SMatthias Ringwald #endif 29350bf6344aS[email protected] 2936a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 2937d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 29380bf6344aS[email protected] case SEND_CANCEL_CONNECTION: 29390bf6344aS[email protected] connection->state = SENT_CANCEL_CONNECTION; 29400bf6344aS[email protected] hci_send_cmd(&hci_le_create_connection_cancel); 29410bf6344aS[email protected] return; 2942a6725849S[email protected] #endif 2943d70217a2SMatthias Ringwald #endif 29440bf6344aS[email protected] case SEND_DISCONNECT: 29450bf6344aS[email protected] connection->state = SENT_DISCONNECT; 29467851196eSmatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 29470bf6344aS[email protected] return; 29480bf6344aS[email protected] 29490bf6344aS[email protected] default: 29500bf6344aS[email protected] break; 2951c7e0c5f6Smatthias.ringwald } 2952c7e0c5f6Smatthias.ringwald 295335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 295432ab9390Smatthias.ringwald if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){ 29559da54300S[email protected] log_info("responding to link key request"); 295634d2123cS[email protected] connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST); 295732ab9390Smatthias.ringwald link_key_t link_key; 2958c77e8838S[email protected] link_key_type_t link_key_type; 2959a98592bcSMatthias Ringwald if ( hci_stack->link_key_db 2960a98592bcSMatthias Ringwald && hci_stack->link_key_db->get_link_key(connection->address, link_key, &link_key_type) 296134d2123cS[email protected] && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){ 29629ab95c90S[email protected] connection->link_key_type = link_key_type; 296332ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key); 296432ab9390Smatthias.ringwald } else { 296532ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 296632ab9390Smatthias.ringwald } 2967dbe1a790S[email protected] return; 296832ab9390Smatthias.ringwald } 29691d6b20aeS[email protected] 2970899283eaS[email protected] if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){ 29719da54300S[email protected] log_info("denying to pin request"); 2972899283eaS[email protected] connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST); 297334d2123cS[email protected] hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address); 29744c57c146S[email protected] return; 29754c57c146S[email protected] } 29764c57c146S[email protected] 2977dbe1a790S[email protected] if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){ 297834d2123cS[email protected] connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY); 297982d8f825S[email protected] log_info("IO Capability Request received, stack bondable %u, io cap %u", hci_stack->bondable, hci_stack->ssp_io_capability); 298082d8f825S[email protected] if (hci_stack->bondable && (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN)){ 2981106d6d11S[email protected] // tweak authentication requirements 29823a9fb326S[email protected] uint8_t authreq = hci_stack->ssp_authentication_requirement; 2983106d6d11S[email protected] if (connection->bonding_flags & BONDING_DEDICATED){ 29849faad3abS[email protected] authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 29859faad3abS[email protected] } 29869faad3abS[email protected] if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){ 29879faad3abS[email protected] authreq |= 1; 2988106d6d11S[email protected] } 29893a9fb326S[email protected] hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, NULL, authreq); 2990f8fb5f6eS[email protected] } else { 2991f8fb5f6eS[email protected] hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED); 2992f8fb5f6eS[email protected] } 2993dbe1a790S[email protected] return; 299432ab9390Smatthias.ringwald } 299532ab9390Smatthias.ringwald 2996dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){ 2997dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY); 299834d2123cS[email protected] hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address); 2999dbe1a790S[email protected] return; 3000dbe1a790S[email protected] } 3001dbe1a790S[email protected] 3002dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){ 3003dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY); 300434d2123cS[email protected] hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000); 3005dbe1a790S[email protected] return; 3006dbe1a790S[email protected] } 3007afd4e962S[email protected] 3008afd4e962S[email protected] if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){ 3009afd4e962S[email protected] connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES; 301034d2123cS[email protected] hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle); 30112bd8b7e7S[email protected] return; 30122bd8b7e7S[email protected] } 30132bd8b7e7S[email protected] 3014ad83dc6aS[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){ 3015ad83dc6aS[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE; 30161bd5283dS[email protected] connection->bonding_flags |= BONDING_EMIT_COMPLETE_ON_DISCONNECT; 301752d34f98S[email protected] hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // authentication done 3018ad83dc6aS[email protected] return; 3019ad83dc6aS[email protected] } 302076f27cffSMatthias Ringwald 302134d2123cS[email protected] if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){ 302234d2123cS[email protected] connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST; 302334d2123cS[email protected] hci_send_cmd(&hci_authentication_requested, connection->con_handle); 30242bd8b7e7S[email protected] return; 3025afd4e962S[email protected] } 302676f27cffSMatthias Ringwald 3027dce78009S[email protected] if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){ 3028dce78009S[email protected] connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST; 3029dce78009S[email protected] hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1); 3030dce78009S[email protected] return; 3031dce78009S[email protected] } 303276f27cffSMatthias Ringwald #endif 303376f27cffSMatthias Ringwald 303476f27cffSMatthias Ringwald if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){ 303576f27cffSMatthias Ringwald connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK; 303676f27cffSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005); // authentication failure 303776f27cffSMatthias Ringwald return; 303876f27cffSMatthias Ringwald } 3039da886c03S[email protected] 3040a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 3041da886c03S[email protected] if (connection->le_con_parameter_update_state == CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS){ 3042da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 3043da886c03S[email protected] 3044c37a3166S[email protected] uint16_t connection_interval_min = connection->le_conn_interval_min; 3045c37a3166S[email protected] connection->le_conn_interval_min = 0; 3046c37a3166S[email protected] hci_send_cmd(&hci_le_connection_update, connection->con_handle, connection_interval_min, 3047c37a3166S[email protected] connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout, 3048c37a3166S[email protected] 0x0000, 0xffff); 3049c37a3166S[email protected] } 3050c37a3166S[email protected] #endif 3051dbe1a790S[email protected] } 3052c7e0c5f6Smatthias.ringwald 305305ae8de3SMatthias Ringwald hci_connection_t * connection; 30543a9fb326S[email protected] switch (hci_stack->state){ 30553429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 305674b323a9SMatthias Ringwald hci_initializing_run(); 30573429f56bSmatthias.ringwald break; 3058c7e0c5f6Smatthias.ringwald 3059c7e0c5f6Smatthias.ringwald case HCI_STATE_HALTING: 3060c7e0c5f6Smatthias.ringwald 30619da54300S[email protected] log_info("HCI_STATE_HALTING"); 30629956955bSMatthias Ringwald 30639956955bSMatthias Ringwald // free whitelist entries 3064a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 3065d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 30669956955bSMatthias Ringwald { 3067665d90f2SMatthias Ringwald btstack_linked_list_iterator_t lit; 3068665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 3069665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 3070665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 3071665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry); 30729956955bSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 30739956955bSMatthias Ringwald } 30749956955bSMatthias Ringwald } 30759956955bSMatthias Ringwald #endif 3076d70217a2SMatthias Ringwald #endif 3077c7e0c5f6Smatthias.ringwald // close all open connections 30783a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 3079c7e0c5f6Smatthias.ringwald if (connection){ 3080711e6c80SMatthias Ringwald hci_con_handle_t con_handle = (uint16_t) connection->con_handle; 3081d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 308232ab9390Smatthias.ringwald 30838837e9efSMatthias Ringwald log_info("HCI_STATE_HALTING, connection %p, handle %u", connection, con_handle); 3084c7e0c5f6Smatthias.ringwald 30858837e9efSMatthias Ringwald // cancel all l2cap connections right away instead of waiting for disconnection complete event ... 30868837e9efSMatthias Ringwald hci_emit_disconnection_complete(con_handle, 0x16); // terminated by local host 30878837e9efSMatthias Ringwald 30888837e9efSMatthias Ringwald // ... which would be ignored anyway as we shutdown (free) the connection now 3089c7e0c5f6Smatthias.ringwald hci_shutdown_connection(connection); 30908837e9efSMatthias Ringwald 30918837e9efSMatthias Ringwald // finally, send the disconnect command 30928837e9efSMatthias Ringwald hci_send_cmd(&hci_disconnect, con_handle, 0x13); // remote closed connection 3093c7e0c5f6Smatthias.ringwald return; 3094c7e0c5f6Smatthias.ringwald } 30959da54300S[email protected] log_info("HCI_STATE_HALTING, calling off"); 3096c7e0c5f6Smatthias.ringwald 309772ea5239Smatthias.ringwald // switch mode 3098c7e0c5f6Smatthias.ringwald hci_power_control_off(); 30999418f9c9Smatthias.ringwald 31009da54300S[email protected] log_info("HCI_STATE_HALTING, emitting state"); 310172ea5239Smatthias.ringwald hci_emit_state(); 31029da54300S[email protected] log_info("HCI_STATE_HALTING, done"); 310372ea5239Smatthias.ringwald break; 3104c7e0c5f6Smatthias.ringwald 310572ea5239Smatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 31063a9fb326S[email protected] switch(hci_stack->substate) { 310774b323a9SMatthias Ringwald case HCI_FALLING_ASLEEP_DISCONNECT: 31089da54300S[email protected] log_info("HCI_STATE_FALLING_ASLEEP"); 310972ea5239Smatthias.ringwald // close all open connections 31103a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 311166da7044Smatthias.ringwald 3112423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 311366da7044Smatthias.ringwald // don't close connections, if H4 supports power management 3114d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 311566da7044Smatthias.ringwald connection = NULL; 311666da7044Smatthias.ringwald } 311766da7044Smatthias.ringwald #endif 311872ea5239Smatthias.ringwald if (connection){ 311932ab9390Smatthias.ringwald 312072ea5239Smatthias.ringwald // send disconnect 3121d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 312232ab9390Smatthias.ringwald 31239da54300S[email protected] log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u", connection, (uint16_t)connection->con_handle); 31246ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 312572ea5239Smatthias.ringwald 312672ea5239Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 312772ea5239Smatthias.ringwald hci_shutdown_connection(connection); 312872ea5239Smatthias.ringwald return; 312972ea5239Smatthias.ringwald } 313072ea5239Smatthias.ringwald 313192368cd3S[email protected] if (hci_classic_supported()){ 313289db417bSmatthias.ringwald // disable page and inquiry scan 3133d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 313432ab9390Smatthias.ringwald 31359da54300S[email protected] log_info("HCI_STATE_HALTING, disabling inq scans"); 31363a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan 313789db417bSmatthias.ringwald 313889db417bSmatthias.ringwald // continue in next sub state 313974b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE; 314089db417bSmatthias.ringwald break; 314192368cd3S[email protected] } 3142202c8a4cSMatthias Ringwald // no break - fall through for ble-only chips 314392368cd3S[email protected] 314474b323a9SMatthias Ringwald case HCI_FALLING_ASLEEP_COMPLETE: 31459da54300S[email protected] log_info("HCI_STATE_HALTING, calling sleep"); 3146423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 314728171530Smatthias.ringwald // don't actually go to sleep, if H4 supports power management 3148d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 314928171530Smatthias.ringwald // SLEEP MODE reached 31503a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 315128171530Smatthias.ringwald hci_emit_state(); 315228171530Smatthias.ringwald break; 315328171530Smatthias.ringwald } 315428171530Smatthias.ringwald #endif 315572ea5239Smatthias.ringwald // switch mode 31563a9fb326S[email protected] hci_power_control_sleep(); // changes hci_stack->state to SLEEP 3157c7e0c5f6Smatthias.ringwald hci_emit_state(); 315828171530Smatthias.ringwald break; 315928171530Smatthias.ringwald 316089db417bSmatthias.ringwald default: 316189db417bSmatthias.ringwald break; 316289db417bSmatthias.ringwald } 3163c7e0c5f6Smatthias.ringwald break; 3164c7e0c5f6Smatthias.ringwald 31653429f56bSmatthias.ringwald default: 31663429f56bSmatthias.ringwald break; 31671f504dbdSmatthias.ringwald } 31683429f56bSmatthias.ringwald } 316916833f0aSmatthias.ringwald 317031452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 317135454696SMatthias Ringwald // house-keeping 317235454696SMatthias Ringwald 317335454696SMatthias Ringwald if (IS_COMMAND(packet, hci_write_loopback_mode)){ 317435454696SMatthias Ringwald hci_stack->loopback_mode = packet[3]; 317535454696SMatthias Ringwald } 317635454696SMatthias Ringwald 317735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 3178c8e4258aSmatthias.ringwald bd_addr_t addr; 3179c8e4258aSmatthias.ringwald hci_connection_t * conn; 3180c8e4258aSmatthias.ringwald 3181c8e4258aSmatthias.ringwald // create_connection? 3182c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 3183724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 31849da54300S[email protected] log_info("Create_connection to %s", bd_addr_to_str(addr)); 3185c8e4258aSmatthias.ringwald 31862e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 3187ad83dc6aS[email protected] if (!conn){ 318896a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 318917f1ba2aSmatthias.ringwald if (!conn){ 319017f1ba2aSmatthias.ringwald // notify client that alloc failed 31912deddeceSMatthias Ringwald hci_emit_connection_complete(addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 319217f1ba2aSmatthias.ringwald return 0; // don't sent packet to controller 319317f1ba2aSmatthias.ringwald } 3194ad83dc6aS[email protected] conn->state = SEND_CREATE_CONNECTION; 3195ad83dc6aS[email protected] } 3196ad83dc6aS[email protected] log_info("conn state %u", conn->state); 3197ad83dc6aS[email protected] switch (conn->state){ 3198ad83dc6aS[email protected] // if connection active exists 3199ad83dc6aS[email protected] case OPEN: 320062bda3fbS[email protected] // and OPEN, emit connection complete command, don't send to controller 3201274dad91SMatthias Ringwald hci_emit_connection_complete(addr, conn->con_handle, 0); 320262bda3fbS[email protected] return 0; 3203ad83dc6aS[email protected] case SEND_CREATE_CONNECTION: 3204ad83dc6aS[email protected] // connection created by hci, e.g. dedicated bonding 3205ad83dc6aS[email protected] break; 3206ad83dc6aS[email protected] default: 3207ad83dc6aS[email protected] // otherwise, just ignore as it is already in the open process 3208ad83dc6aS[email protected] return 0; 3209ad83dc6aS[email protected] } 3210c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 3211c8e4258aSmatthias.ringwald } 321235454696SMatthias Ringwald 32137fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_reply)){ 32147fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY); 32157fde4af9Smatthias.ringwald } 32167fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){ 32177fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST); 32187fde4af9Smatthias.ringwald } 32197fde4af9Smatthias.ringwald 32208ef73945Smatthias.ringwald if (IS_COMMAND(packet, hci_delete_stored_link_key)){ 3221a98592bcSMatthias Ringwald if (hci_stack->link_key_db){ 3222724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 3223a98592bcSMatthias Ringwald hci_stack->link_key_db->delete_link_key(addr); 32248ef73945Smatthias.ringwald } 32258ef73945Smatthias.ringwald } 3226c8e4258aSmatthias.ringwald 32276724cd9eS[email protected] if (IS_COMMAND(packet, hci_pin_code_request_negative_reply) 32286724cd9eS[email protected] || IS_COMMAND(packet, hci_pin_code_request_reply)){ 3229724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 32302e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 32316724cd9eS[email protected] if (conn){ 32326724cd9eS[email protected] connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE); 32336724cd9eS[email protected] } 32346724cd9eS[email protected] } 32356724cd9eS[email protected] 32366724cd9eS[email protected] if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply) 32376724cd9eS[email protected] || IS_COMMAND(packet, hci_user_confirmation_request_reply) 32386724cd9eS[email protected] || IS_COMMAND(packet, hci_user_passkey_request_negative_reply) 32396724cd9eS[email protected] || IS_COMMAND(packet, hci_user_passkey_request_reply)) { 3240724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 32412e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 32426724cd9eS[email protected] if (conn){ 32436724cd9eS[email protected] connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE); 32446724cd9eS[email protected] } 32456724cd9eS[email protected] } 3246ee752bb8SMatthias Ringwald 3247ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 3248ee752bb8SMatthias Ringwald // setup_synchronous_connection? Voice setting at offset 22 3249ee752bb8SMatthias Ringwald if (IS_COMMAND(packet, hci_setup_synchronous_connection)){ 3250ee752bb8SMatthias Ringwald // TODO: compare to current setting if sco connection already active 3251ee752bb8SMatthias Ringwald hci_stack->sco_voice_setting_active = little_endian_read_16(packet, 15); 3252ee752bb8SMatthias Ringwald } 3253ee752bb8SMatthias Ringwald // accept_synchronus_connection? Voice setting at offset 18 3254ee752bb8SMatthias Ringwald if (IS_COMMAND(packet, hci_accept_synchronous_connection)){ 3255ee752bb8SMatthias Ringwald // TODO: compare to current setting if sco connection already active 3256ee752bb8SMatthias Ringwald hci_stack->sco_voice_setting_active = little_endian_read_16(packet, 19); 3257ee752bb8SMatthias Ringwald } 3258ee752bb8SMatthias Ringwald #endif 325935454696SMatthias Ringwald #endif 32604b3e1e19SMatthias Ringwald 3261a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 3262d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 326369a97523S[email protected] if (IS_COMMAND(packet, hci_le_set_random_address)){ 3264b95a5a35SMatthias Ringwald hci_stack->le_random_address_set = 1; 3265b95a5a35SMatthias Ringwald reverse_bd_addr(&packet[3], hci_stack->le_random_address); 3266d70217a2SMatthias Ringwald } 3267171293d3SMatthias Ringwald if (IS_COMMAND(packet, hci_le_set_advertise_enable)){ 3268171293d3SMatthias Ringwald hci_stack->le_advertisements_active = packet[3]; 3269171293d3SMatthias Ringwald } 3270d70217a2SMatthias Ringwald #endif 3271d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 3272b04dfa37SMatthias Ringwald if (IS_COMMAND(packet, hci_le_create_connection)){ 3273b04dfa37SMatthias Ringwald // white list used? 3274b04dfa37SMatthias Ringwald uint8_t initiator_filter_policy = packet[7]; 3275b04dfa37SMatthias Ringwald switch (initiator_filter_policy){ 3276b04dfa37SMatthias Ringwald case 0: 3277b04dfa37SMatthias Ringwald // whitelist not used 3278b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_DIRECT; 3279b04dfa37SMatthias Ringwald break; 3280b04dfa37SMatthias Ringwald case 1: 3281b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_WHITELIST; 3282b04dfa37SMatthias Ringwald break; 3283b04dfa37SMatthias Ringwald default: 3284b04dfa37SMatthias Ringwald log_error("Invalid initiator_filter_policy in LE Create Connection %u", initiator_filter_policy); 3285b04dfa37SMatthias Ringwald break; 3286b04dfa37SMatthias Ringwald } 3287b04dfa37SMatthias Ringwald } 3288b04dfa37SMatthias Ringwald if (IS_COMMAND(packet, hci_le_create_connection_cancel)){ 3289b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 3290b04dfa37SMatthias Ringwald } 329169a97523S[email protected] #endif 3292d70217a2SMatthias Ringwald #endif 329369a97523S[email protected] 32943a9fb326S[email protected] hci_stack->num_cmd_packets--; 32955bb5bc3eS[email protected] 32965bb5bc3eS[email protected] hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size); 32976b4af23dS[email protected] int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 32986b4af23dS[email protected] 3299d051460cS[email protected] // release packet buffer for synchronous transport implementations 3300c8b9416aS[email protected] if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){ 33016b4af23dS[email protected] hci_stack->hci_packet_buffer_reserved = 0; 33026b4af23dS[email protected] } 33036b4af23dS[email protected] 33046b4af23dS[email protected] return err; 330531452debSmatthias.ringwald } 33068adf0ddaSmatthias.ringwald 33072bd8b7e7S[email protected] // disconnect because of security block 33082bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){ 33092bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 33102bd8b7e7S[email protected] if (!connection) return; 33112bd8b7e7S[email protected] connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 33122bd8b7e7S[email protected] } 33132bd8b7e7S[email protected] 33142bd8b7e7S[email protected] 3315dbe1a790S[email protected] // Configure Secure Simple Pairing 3316dbe1a790S[email protected] 331735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 331835454696SMatthias Ringwald 3319dbe1a790S[email protected] // enable will enable SSP during init 332015a95bd5SMatthias Ringwald void gap_ssp_set_enable(int enable){ 33213a9fb326S[email protected] hci_stack->ssp_enable = enable; 3322dbe1a790S[email protected] } 3323dbe1a790S[email protected] 332495d71764SMatthias Ringwald static int hci_local_ssp_activated(void){ 332515a95bd5SMatthias Ringwald return gap_ssp_supported() && hci_stack->ssp_enable; 33262bd8b7e7S[email protected] } 33272bd8b7e7S[email protected] 3328dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement 332915a95bd5SMatthias Ringwald void gap_ssp_set_io_capability(int io_capability){ 33303a9fb326S[email protected] hci_stack->ssp_io_capability = io_capability; 3331dbe1a790S[email protected] } 333215a95bd5SMatthias Ringwald void gap_ssp_set_authentication_requirement(int authentication_requirement){ 33333a9fb326S[email protected] hci_stack->ssp_authentication_requirement = authentication_requirement; 3334dbe1a790S[email protected] } 3335dbe1a790S[email protected] 3336dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested 333715a95bd5SMatthias Ringwald void gap_ssp_set_auto_accept(int auto_accept){ 33383a9fb326S[email protected] hci_stack->ssp_auto_accept = auto_accept; 3339dbe1a790S[email protected] } 334035454696SMatthias Ringwald #endif 3341dbe1a790S[email protected] 334294be1a66SMatthias Ringwald // va_list part of hci_send_cmd 334394be1a66SMatthias Ringwald int hci_send_cmd_va_arg(const hci_cmd_t *cmd, va_list argptr){ 3344d94d3cafS[email protected] if (!hci_can_send_command_packet_now()){ 33459d14b626S[email protected] log_error("hci_send_cmd called but cannot send packet now"); 33469d14b626S[email protected] return 0; 33479d14b626S[email protected] } 33489d14b626S[email protected] 33495127cc62S[email protected] // for HCI INITIALIZATION 33509da54300S[email protected] // log_info("hci_send_cmd: opcode %04x", cmd->opcode); 33515127cc62S[email protected] hci_stack->last_cmd_opcode = cmd->opcode; 33525127cc62S[email protected] 33539d14b626S[email protected] hci_reserve_packet_buffer(); 33549d14b626S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 335594be1a66SMatthias Ringwald uint16_t size = hci_cmd_create_from_template(packet, cmd, argptr); 335694be1a66SMatthias Ringwald return hci_send_cmd_packet(packet, size); 335794be1a66SMatthias Ringwald } 33589d14b626S[email protected] 335994be1a66SMatthias Ringwald /** 336094be1a66SMatthias Ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 336194be1a66SMatthias Ringwald */ 336294be1a66SMatthias Ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){ 33631cd208adSmatthias.ringwald va_list argptr; 33641cd208adSmatthias.ringwald va_start(argptr, cmd); 336594be1a66SMatthias Ringwald int res = hci_send_cmd_va_arg(cmd, argptr); 33661cd208adSmatthias.ringwald va_end(argptr); 336794be1a66SMatthias Ringwald return res; 336893b8dc03Smatthias.ringwald } 3369c8e4258aSmatthias.ringwald 3370ee091cf1Smatthias.ringwald // Create various non-HCI events. 3371ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 3372ee091cf1Smatthias.ringwald 3373d6b06661SMatthias Ringwald static void hci_emit_event(uint8_t * event, uint16_t size, int dump){ 3374fb37a842SMatthias Ringwald // dump packet 3375d6b06661SMatthias Ringwald if (dump) { 3376300c1ba4SMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, size); 3377d6b06661SMatthias Ringwald } 33781ef6bb52SMatthias Ringwald 3379fb37a842SMatthias Ringwald // dispatch to all event handlers 33801ef6bb52SMatthias Ringwald btstack_linked_list_iterator_t it; 33811ef6bb52SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->event_handlers); 33821ef6bb52SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 33831ef6bb52SMatthias Ringwald btstack_packet_callback_registration_t * entry = (btstack_packet_callback_registration_t*) btstack_linked_list_iterator_next(&it); 3384d9a7306aSMatthias Ringwald entry->callback(HCI_EVENT_PACKET, 0, event, size); 33851ef6bb52SMatthias Ringwald } 3386d6b06661SMatthias Ringwald } 3387d6b06661SMatthias Ringwald 3388d6b06661SMatthias Ringwald static void hci_emit_acl_packet(uint8_t * packet, uint16_t size){ 3389fb37a842SMatthias Ringwald if (!hci_stack->acl_packet_handler) return; 33903d50b4baSMatthias Ringwald hci_stack->acl_packet_handler(HCI_ACL_DATA_PACKET, 0, packet, size); 3391d6b06661SMatthias Ringwald } 3392d6b06661SMatthias Ringwald 339335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 3394701e3307SMatthias Ringwald static void hci_notify_if_sco_can_send_now(void){ 33953bc639ceSMatthias Ringwald // notify SCO sender if waiting 3396701e3307SMatthias Ringwald if (!hci_stack->sco_waiting_for_can_send_now) return; 3397701e3307SMatthias Ringwald if (hci_can_send_sco_packet_now()){ 33983bc639ceSMatthias Ringwald hci_stack->sco_waiting_for_can_send_now = 0; 3399701e3307SMatthias Ringwald uint8_t event[2] = { HCI_EVENT_SCO_CAN_SEND_NOW, 0 }; 3400701e3307SMatthias Ringwald hci_dump_packet(HCI_EVENT_PACKET, 1, event, sizeof(event)); 34013d50b4baSMatthias Ringwald hci_stack->sco_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event)); 34023bc639ceSMatthias Ringwald } 34033bc639ceSMatthias Ringwald } 340435454696SMatthias Ringwald #endif 34053bc639ceSMatthias Ringwald 340671de195eSMatthias Ringwald void hci_emit_state(void){ 34073a9fb326S[email protected] log_info("BTSTACK_EVENT_STATE %u", hci_stack->state); 3408425d1371Smatthias.ringwald uint8_t event[3]; 340980d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 3410425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 34113a9fb326S[email protected] event[2] = hci_stack->state; 3412d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3413c8e4258aSmatthias.ringwald } 3414c8e4258aSmatthias.ringwald 341535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 34162deddeceSMatthias Ringwald static void hci_emit_connection_complete(bd_addr_t address, hci_con_handle_t con_handle, uint8_t status){ 3417425d1371Smatthias.ringwald uint8_t event[13]; 3418c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 3419425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 342017f1ba2aSmatthias.ringwald event[2] = status; 34212deddeceSMatthias Ringwald little_endian_store_16(event, 3, con_handle); 34222deddeceSMatthias Ringwald reverse_bd_addr(address, &event[5]); 3423c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 3424c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 3425d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3426c8e4258aSmatthias.ringwald } 342752db98b2SMatthias Ringwald static void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 342852db98b2SMatthias Ringwald if (disable_l2cap_timeouts) return; 342952db98b2SMatthias Ringwald log_info("L2CAP_EVENT_TIMEOUT_CHECK"); 343052db98b2SMatthias Ringwald uint8_t event[4]; 343152db98b2SMatthias Ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 343252db98b2SMatthias Ringwald event[1] = sizeof(event) - 2; 343352db98b2SMatthias Ringwald little_endian_store_16(event, 2, conn->con_handle); 343452db98b2SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 343552db98b2SMatthias Ringwald } 343635454696SMatthias Ringwald #endif 3437c8e4258aSmatthias.ringwald 343835454696SMatthias Ringwald #ifdef ENABLE_BLE 3439d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 3440fc64f94aSMatthias Ringwald static void hci_emit_le_connection_complete(uint8_t address_type, bd_addr_t address, hci_con_handle_t con_handle, uint8_t status){ 34414f3229d8S[email protected] uint8_t event[21]; 34424f3229d8S[email protected] event[0] = HCI_EVENT_LE_META; 34434f3229d8S[email protected] event[1] = sizeof(event) - 2; 34444f3229d8S[email protected] event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE; 34454f3229d8S[email protected] event[3] = status; 3446fc64f94aSMatthias Ringwald little_endian_store_16(event, 4, con_handle); 34474f3229d8S[email protected] event[6] = 0; // TODO: role 34486e2e9a6bS[email protected] event[7] = address_type; 3449724d70a2SMatthias Ringwald reverse_bd_addr(address, &event[8]); 3450f8fbdce0SMatthias Ringwald little_endian_store_16(event, 14, 0); // interval 3451f8fbdce0SMatthias Ringwald little_endian_store_16(event, 16, 0); // latency 3452f8fbdce0SMatthias Ringwald little_endian_store_16(event, 18, 0); // supervision timeout 34534f3229d8S[email protected] event[20] = 0; // master clock accuracy 3454d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 34554f3229d8S[email protected] } 345635454696SMatthias Ringwald #endif 3457d70217a2SMatthias Ringwald #endif 34584f3229d8S[email protected] 3459fc64f94aSMatthias Ringwald static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason){ 3460425d1371Smatthias.ringwald uint8_t event[6]; 34613c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 3462e518c4b8Smatthias.ringwald event[1] = sizeof(event) - 2; 34633c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 3464fc64f94aSMatthias Ringwald little_endian_store_16(event, 3, con_handle); 34653c4d4b90Smatthias.ringwald event[5] = reason; 3466d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 34673c4d4b90Smatthias.ringwald } 34683c4d4b90Smatthias.ringwald 3469b83d5eabSMatthias Ringwald static void hci_emit_nr_connections_changed(void){ 3470e0abb8e7S[email protected] log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections()); 3471425d1371Smatthias.ringwald uint8_t event[3]; 347280d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 3473425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 347443bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 3475d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 347643bfb1bdSmatthias.ringwald } 3477038bc64cSmatthias.ringwald 3478b83d5eabSMatthias Ringwald static void hci_emit_hci_open_failed(void){ 3479e0abb8e7S[email protected] log_info("BTSTACK_EVENT_POWERON_FAILED"); 3480425d1371Smatthias.ringwald uint8_t event[2]; 348180d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 3482425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 3483d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3484038bc64cSmatthias.ringwald } 34851b0e3922Smatthias.ringwald 348635454696SMatthias Ringwald static void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status){ 348735454696SMatthias Ringwald log_info("hci_emit_dedicated_bonding_result %u ", status); 348835454696SMatthias Ringwald uint8_t event[9]; 348935454696SMatthias Ringwald int pos = 0; 349035454696SMatthias Ringwald event[pos++] = GAP_EVENT_DEDICATED_BONDING_COMPLETED; 349135454696SMatthias Ringwald event[pos++] = sizeof(event) - 2; 349235454696SMatthias Ringwald event[pos++] = status; 349335454696SMatthias Ringwald reverse_bd_addr(address, &event[pos]); 3494d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3495381fbed8Smatthias.ringwald } 3496458bf4e8S[email protected] 349735454696SMatthias Ringwald 349835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 349935454696SMatthias Ringwald 3500b83d5eabSMatthias Ringwald static void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){ 3501df3354fcS[email protected] log_info("hci_emit_security_level %u for handle %x", level, con_handle); 3502a00031e2S[email protected] uint8_t event[5]; 3503e00caf9cS[email protected] int pos = 0; 35045611a760SMatthias Ringwald event[pos++] = GAP_EVENT_SECURITY_LEVEL; 3505e00caf9cS[email protected] event[pos++] = sizeof(event) - 2; 3506f8fbdce0SMatthias Ringwald little_endian_store_16(event, 2, con_handle); 3507e00caf9cS[email protected] pos += 2; 3508e00caf9cS[email protected] event[pos++] = level; 3509d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3510e00caf9cS[email protected] } 3511e00caf9cS[email protected] 351235454696SMatthias Ringwald static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){ 351335454696SMatthias Ringwald if (!connection) return LEVEL_0; 351435454696SMatthias Ringwald if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0; 351535454696SMatthias Ringwald return gap_security_level_for_link_key_type(connection->link_key_type); 351635454696SMatthias Ringwald } 351735454696SMatthias Ringwald 351835454696SMatthias Ringwald static void hci_emit_discoverable_enabled(uint8_t enabled){ 351935454696SMatthias Ringwald log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled); 352035454696SMatthias Ringwald uint8_t event[3]; 352135454696SMatthias Ringwald event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED; 352235454696SMatthias Ringwald event[1] = sizeof(event) - 2; 352335454696SMatthias Ringwald event[2] = enabled; 3524d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3525ad83dc6aS[email protected] } 3526ad83dc6aS[email protected] 352706b9e820SMatthias Ringwald #ifdef ENABLE_CLASSIC 352898a2fd1cSMatthias Ringwald // query if remote side supports eSCO 3529073bd0faSMatthias Ringwald int hci_remote_esco_supported(hci_con_handle_t con_handle){ 353098a2fd1cSMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 353198a2fd1cSMatthias Ringwald if (!connection) return 0; 353298a2fd1cSMatthias Ringwald return connection->remote_supported_feature_eSCO; 353398a2fd1cSMatthias Ringwald } 353498a2fd1cSMatthias Ringwald 35352bd8b7e7S[email protected] // query if remote side supports SSP 35362bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){ 35372bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 35382bd8b7e7S[email protected] if (!connection) return 0; 35392bd8b7e7S[email protected] return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0; 35402bd8b7e7S[email protected] } 35412bd8b7e7S[email protected] 354215a95bd5SMatthias Ringwald int gap_ssp_supported_on_both_sides(hci_con_handle_t handle){ 3543df3354fcS[email protected] return hci_local_ssp_activated() && hci_remote_ssp_supported(handle); 3544df3354fcS[email protected] } 354506b9e820SMatthias Ringwald #endif 3546df3354fcS[email protected] 3547458bf4e8S[email protected] // GAP API 3548458bf4e8S[email protected] /** 3549458bf4e8S[email protected] * @bbrief enable/disable bonding. default is enabled 3550458bf4e8S[email protected] * @praram enabled 3551458bf4e8S[email protected] */ 35524c57c146S[email protected] void gap_set_bondable_mode(int enable){ 35533a9fb326S[email protected] hci_stack->bondable = enable ? 1 : 0; 3554458bf4e8S[email protected] } 35554ef6443cSMatthias Ringwald /** 35564ef6443cSMatthias Ringwald * @brief Get bondable mode. 35574ef6443cSMatthias Ringwald * @return 1 if bondable 35584ef6443cSMatthias Ringwald */ 35594ef6443cSMatthias Ringwald int gap_get_bondable_mode(void){ 35604ef6443cSMatthias Ringwald return hci_stack->bondable; 35614ef6443cSMatthias Ringwald } 3562cb230b9dS[email protected] 3563cb230b9dS[email protected] /** 356434d2123cS[email protected] * @brief map link keys to security levels 3565cb230b9dS[email protected] */ 356634d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){ 356734d2123cS[email protected] switch (link_key_type){ 35683c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 35693c68dfa9S[email protected] return LEVEL_4; 35703c68dfa9S[email protected] case COMBINATION_KEY: 35713c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 35723c68dfa9S[email protected] return LEVEL_3; 35733c68dfa9S[email protected] default: 35743c68dfa9S[email protected] return LEVEL_2; 35753c68dfa9S[email protected] } 3576cb230b9dS[email protected] } 3577cb230b9dS[email protected] 3578106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){ 35795127cc62S[email protected] log_info("gap_mitm_protection_required_for_security_level %u", level); 3580106d6d11S[email protected] return level > LEVEL_2; 3581106d6d11S[email protected] } 3582106d6d11S[email protected] 358334d2123cS[email protected] /** 358434d2123cS[email protected] * @brief get current security level 358534d2123cS[email protected] */ 358634d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){ 358734d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 358834d2123cS[email protected] if (!connection) return LEVEL_0; 358934d2123cS[email protected] return gap_security_level_for_connection(connection); 359034d2123cS[email protected] } 359134d2123cS[email protected] 3592cb230b9dS[email protected] /** 3593cb230b9dS[email protected] * @brief request connection to device to 3594cb230b9dS[email protected] * @result GAP_AUTHENTICATION_RESULT 3595cb230b9dS[email protected] */ 359634d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){ 359734d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 359834d2123cS[email protected] if (!connection){ 3599a00031e2S[email protected] hci_emit_security_level(con_handle, LEVEL_0); 360034d2123cS[email protected] return; 360134d2123cS[email protected] } 360234d2123cS[email protected] gap_security_level_t current_level = gap_security_level(con_handle); 360334d2123cS[email protected] log_info("gap_request_security_level %u, current level %u", requested_level, current_level); 360434d2123cS[email protected] if (current_level >= requested_level){ 3605a00031e2S[email protected] hci_emit_security_level(con_handle, current_level); 360634d2123cS[email protected] return; 360734d2123cS[email protected] } 3608a00031e2S[email protected] 360934d2123cS[email protected] connection->requested_security_level = requested_level; 3610a00031e2S[email protected] 361125bf5872S[email protected] #if 0 361225bf5872S[email protected] // sending encryption request without a link key results in an error. 361325bf5872S[email protected] // TODO: figure out how to use it properly 361425bf5872S[email protected] 3615fb8ba0dbS[email protected] // would enabling ecnryption suffice (>= LEVEL_2)? 3616a98592bcSMatthias Ringwald if (hci_stack->link_key_db){ 3617a00031e2S[email protected] link_key_type_t link_key_type; 3618a00031e2S[email protected] link_key_t link_key; 3619a98592bcSMatthias Ringwald if (hci_stack->link_key_db->get_link_key( &connection->address, &link_key, &link_key_type)){ 3620a00031e2S[email protected] if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){ 3621a00031e2S[email protected] connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 3622a00031e2S[email protected] return; 3623a00031e2S[email protected] } 3624a00031e2S[email protected] } 3625a00031e2S[email protected] } 362625bf5872S[email protected] #endif 3627a00031e2S[email protected] 36281eb2563eS[email protected] // try to authenticate connection 36291eb2563eS[email protected] connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 3630e80b2cf9S[email protected] hci_run(); 3631e00caf9cS[email protected] } 3632ad83dc6aS[email protected] 3633ad83dc6aS[email protected] /** 3634ad83dc6aS[email protected] * @brief start dedicated bonding with device. disconnect after bonding 3635ad83dc6aS[email protected] * @param device 3636ad83dc6aS[email protected] * @param request MITM protection 3637ad83dc6aS[email protected] * @result GAP_DEDICATED_BONDING_COMPLETE 3638ad83dc6aS[email protected] */ 3639ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){ 3640ad83dc6aS[email protected] 3641ad83dc6aS[email protected] // create connection state machine 364296a45072S[email protected] hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_CLASSIC); 3643ad83dc6aS[email protected] 3644ad83dc6aS[email protected] if (!connection){ 3645ad83dc6aS[email protected] return BTSTACK_MEMORY_ALLOC_FAILED; 3646ad83dc6aS[email protected] } 3647ad83dc6aS[email protected] 3648ad83dc6aS[email protected] // delete linkn key 364915a95bd5SMatthias Ringwald gap_drop_link_key_for_bd_addr(device); 3650ad83dc6aS[email protected] 3651ad83dc6aS[email protected] // configure LEVEL_2/3, dedicated bonding 3652ad83dc6aS[email protected] connection->state = SEND_CREATE_CONNECTION; 3653ad83dc6aS[email protected] connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2; 3654f04a0c31SMatthias Ringwald log_info("gap_dedicated_bonding, mitm %d -> level %u", mitm_protection_required, connection->requested_security_level); 3655ad83dc6aS[email protected] connection->bonding_flags = BONDING_DEDICATED; 3656ad83dc6aS[email protected] 3657ad83dc6aS[email protected] // wait for GAP Security Result and send GAP Dedicated Bonding complete 3658ad83dc6aS[email protected] 3659ad83dc6aS[email protected] // handle: connnection failure (connection complete != ok) 3660ad83dc6aS[email protected] // handle: authentication failure 3661ad83dc6aS[email protected] // handle: disconnect on done 3662ad83dc6aS[email protected] 3663ad83dc6aS[email protected] hci_run(); 3664ad83dc6aS[email protected] 3665ad83dc6aS[email protected] return 0; 3666ad83dc6aS[email protected] } 366735454696SMatthias Ringwald #endif 36688e618f72S[email protected] 36698e618f72S[email protected] void gap_set_local_name(const char * local_name){ 36708e618f72S[email protected] hci_stack->local_name = local_name; 36718e618f72S[email protected] } 36728e618f72S[email protected] 367335454696SMatthias Ringwald 367435454696SMatthias Ringwald #ifdef ENABLE_BLE 367535454696SMatthias Ringwald 3676d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 3677d8e8f12aSMatthias Ringwald void gap_start_scan(void){ 3678d8e8f12aSMatthias Ringwald if (hci_stack->le_scanning_state == LE_SCANNING) return; 36797bdc6798S[email protected] hci_stack->le_scanning_state = LE_START_SCAN; 36807bdc6798S[email protected] hci_run(); 36817bdc6798S[email protected] } 36828e618f72S[email protected] 3683d8e8f12aSMatthias Ringwald void gap_stop_scan(void){ 3684d8e8f12aSMatthias Ringwald if ( hci_stack->le_scanning_state == LE_SCAN_IDLE) return; 36857bdc6798S[email protected] hci_stack->le_scanning_state = LE_STOP_SCAN; 36867bdc6798S[email protected] hci_run(); 36877bdc6798S[email protected] } 36884f3229d8S[email protected] 3689d8e8f12aSMatthias Ringwald void gap_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){ 3690ef11999fSmatthias.ringwald hci_stack->le_scan_type = scan_type; 3691ef11999fSmatthias.ringwald hci_stack->le_scan_interval = scan_interval; 3692ef11999fSmatthias.ringwald hci_stack->le_scan_window = scan_window; 3693ef11999fSmatthias.ringwald hci_run(); 3694ef11999fSmatthias.ringwald } 36954f3229d8S[email protected] 3696d8e8f12aSMatthias Ringwald uint8_t gap_connect(bd_addr_t addr, bd_addr_type_t addr_type){ 36974f3229d8S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 36984f3229d8S[email protected] if (!conn){ 3699d8e8f12aSMatthias Ringwald log_info("gap_connect: no connection exists yet, creating context"); 37002e77e513S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 37014f3229d8S[email protected] if (!conn){ 37024f3229d8S[email protected] // notify client that alloc failed 37036e2e9a6bS[email protected] hci_emit_le_connection_complete(addr_type, addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 3704d8e8f12aSMatthias Ringwald log_info("gap_connect: failed to alloc hci_connection_t"); 3705472a5742SMatthias Ringwald return GATT_CLIENT_NOT_CONNECTED; // don't sent packet to controller 37064f3229d8S[email protected] } 37074f3229d8S[email protected] conn->state = SEND_CREATE_CONNECTION; 3708d8e8f12aSMatthias Ringwald log_info("gap_connect: send create connection next"); 3709564fca32S[email protected] hci_run(); 3710616edd56SMatthias Ringwald return 0; 37114f3229d8S[email protected] } 37120bf6344aS[email protected] 37130bf6344aS[email protected] if (!hci_is_le_connection(conn) || 37140bf6344aS[email protected] conn->state == SEND_CREATE_CONNECTION || 37150bf6344aS[email protected] conn->state == SENT_CREATE_CONNECTION) { 37162e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_COMMAND_DISALLOWED); 3717d8e8f12aSMatthias Ringwald log_error("gap_connect: classic connection or connect is already being created"); 3718616edd56SMatthias Ringwald return GATT_CLIENT_IN_WRONG_STATE; 37190bf6344aS[email protected] } 37200bf6344aS[email protected] 3721d8e8f12aSMatthias Ringwald log_info("gap_connect: context exists with state %u", conn->state); 37222e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, conn->con_handle, 0); 37234f3229d8S[email protected] hci_run(); 3724616edd56SMatthias Ringwald return 0; 37254f3229d8S[email protected] } 37264f3229d8S[email protected] 37277851196eSmatthias.ringwald // @assumption: only a single outgoing LE Connection exists 3728d8e8f12aSMatthias Ringwald static hci_connection_t * gap_get_outgoing_connection(void){ 3729665d90f2SMatthias Ringwald btstack_linked_item_t *it; 3730665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 37310bf6344aS[email protected] hci_connection_t * conn = (hci_connection_t *) it; 37320bf6344aS[email protected] if (!hci_is_le_connection(conn)) continue; 37330bf6344aS[email protected] switch (conn->state){ 3734a6725849S[email protected] case SEND_CREATE_CONNECTION: 37357851196eSmatthias.ringwald case SENT_CREATE_CONNECTION: 37367851196eSmatthias.ringwald return conn; 37377851196eSmatthias.ringwald default: 37387851196eSmatthias.ringwald break; 37397851196eSmatthias.ringwald }; 37407851196eSmatthias.ringwald } 37417851196eSmatthias.ringwald return NULL; 37427851196eSmatthias.ringwald } 37437851196eSmatthias.ringwald 3744d8e8f12aSMatthias Ringwald uint8_t gap_connect_cancel(void){ 3745d8e8f12aSMatthias Ringwald hci_connection_t * conn = gap_get_outgoing_connection(); 3746616edd56SMatthias Ringwald if (!conn) return 0; 37477851196eSmatthias.ringwald switch (conn->state){ 37487851196eSmatthias.ringwald case SEND_CREATE_CONNECTION: 37497851196eSmatthias.ringwald // skip sending create connection and emit event instead 37502e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER); 3751665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 37527851196eSmatthias.ringwald btstack_memory_hci_connection_free( conn ); 37530bf6344aS[email protected] break; 3754a6725849S[email protected] case SENT_CREATE_CONNECTION: 37557851196eSmatthias.ringwald // request to send cancel connection 37560bf6344aS[email protected] conn->state = SEND_CANCEL_CONNECTION; 37570bf6344aS[email protected] hci_run(); 37580bf6344aS[email protected] break; 37590bf6344aS[email protected] default: 37600bf6344aS[email protected] break; 37610bf6344aS[email protected] } 3762616edd56SMatthias Ringwald return 0; 3763e31f89a7S[email protected] } 3764d70217a2SMatthias Ringwald #endif 37654f3229d8S[email protected] 3766c37a3166S[email protected] /** 3767c37a3166S[email protected] * @brief Updates the connection parameters for a given LE connection 3768c37a3166S[email protected] * @param handle 3769c37a3166S[email protected] * @param conn_interval_min (unit: 1.25ms) 3770c37a3166S[email protected] * @param conn_interval_max (unit: 1.25ms) 3771c37a3166S[email protected] * @param conn_latency 3772c37a3166S[email protected] * @param supervision_timeout (unit: 10ms) 3773c37a3166S[email protected] * @returns 0 if ok 3774c37a3166S[email protected] */ 3775c37a3166S[email protected] int gap_update_connection_parameters(hci_con_handle_t con_handle, uint16_t conn_interval_min, 3776c37a3166S[email protected] uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 3777c37a3166S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 3778c37a3166S[email protected] if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3779c37a3166S[email protected] connection->le_conn_interval_min = conn_interval_min; 3780c37a3166S[email protected] connection->le_conn_interval_max = conn_interval_max; 3781c37a3166S[email protected] connection->le_conn_latency = conn_latency; 3782c37a3166S[email protected] connection->le_supervision_timeout = supervision_timeout; 378384cf6d83SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS; 3784cfc59f1bSMatthias Ringwald hci_run(); 3785c37a3166S[email protected] return 0; 3786c37a3166S[email protected] } 3787c37a3166S[email protected] 378845c102fdSMatthias Ringwald /** 3789b68d7bc3SMatthias Ringwald * @brief Request an update of the connection parameter for a given LE connection 3790b68d7bc3SMatthias Ringwald * @param handle 3791b68d7bc3SMatthias Ringwald * @param conn_interval_min (unit: 1.25ms) 3792b68d7bc3SMatthias Ringwald * @param conn_interval_max (unit: 1.25ms) 3793b68d7bc3SMatthias Ringwald * @param conn_latency 3794b68d7bc3SMatthias Ringwald * @param supervision_timeout (unit: 10ms) 3795b68d7bc3SMatthias Ringwald * @returns 0 if ok 3796b68d7bc3SMatthias Ringwald */ 3797b68d7bc3SMatthias Ringwald int gap_request_connection_parameter_update(hci_con_handle_t con_handle, uint16_t conn_interval_min, 3798b68d7bc3SMatthias Ringwald uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 3799b68d7bc3SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 3800b68d7bc3SMatthias Ringwald if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3801b68d7bc3SMatthias Ringwald connection->le_conn_interval_min = conn_interval_min; 3802b68d7bc3SMatthias Ringwald connection->le_conn_interval_max = conn_interval_max; 3803b68d7bc3SMatthias Ringwald connection->le_conn_latency = conn_latency; 3804b68d7bc3SMatthias Ringwald connection->le_supervision_timeout = supervision_timeout; 3805b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_REQUEST; 3806b68d7bc3SMatthias Ringwald hci_run(); 3807b68d7bc3SMatthias Ringwald return 0; 3808b68d7bc3SMatthias Ringwald } 3809b68d7bc3SMatthias Ringwald 3810d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 3811d70217a2SMatthias Ringwald 3812501f56b3SMatthias Ringwald static void gap_advertisments_changed(void){ 3813501f56b3SMatthias Ringwald // disable advertisements before updating adv, scan data, or adv params 3814501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_active){ 3815501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_DISABLE | LE_ADVERTISEMENT_TASKS_ENABLE; 3816501f56b3SMatthias Ringwald } 3817501f56b3SMatthias Ringwald hci_run(); 3818501f56b3SMatthias Ringwald } 3819501f56b3SMatthias Ringwald 3820b68d7bc3SMatthias Ringwald /** 382145c102fdSMatthias Ringwald * @brief Set Advertisement Data 382245c102fdSMatthias Ringwald * @param advertising_data_length 382345c102fdSMatthias Ringwald * @param advertising_data (max 31 octets) 382445c102fdSMatthias Ringwald * @note data is not copied, pointer has to stay valid 382545c102fdSMatthias Ringwald */ 382645c102fdSMatthias Ringwald void gap_advertisements_set_data(uint8_t advertising_data_length, uint8_t * advertising_data){ 382745c102fdSMatthias Ringwald hci_stack->le_advertisements_data_len = advertising_data_length; 382845c102fdSMatthias Ringwald hci_stack->le_advertisements_data = advertising_data; 3829501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 3830501f56b3SMatthias Ringwald gap_advertisments_changed(); 383145c102fdSMatthias Ringwald } 3832501f56b3SMatthias Ringwald 3833501f56b3SMatthias Ringwald /** 3834501f56b3SMatthias Ringwald * @brief Set Scan Response Data 3835501f56b3SMatthias Ringwald * @param advertising_data_length 3836501f56b3SMatthias Ringwald * @param advertising_data (max 31 octets) 3837501f56b3SMatthias Ringwald * @note data is not copied, pointer has to stay valid 3838501f56b3SMatthias Ringwald */ 3839501f56b3SMatthias Ringwald void gap_scan_response_set_data(uint8_t scan_response_data_length, uint8_t * scan_response_data){ 3840501f56b3SMatthias Ringwald hci_stack->le_scan_response_data_len = scan_response_data_length; 3841501f56b3SMatthias Ringwald hci_stack->le_scan_response_data = scan_response_data; 3842501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 3843501f56b3SMatthias Ringwald gap_advertisments_changed(); 384445c102fdSMatthias Ringwald } 384545c102fdSMatthias Ringwald 384645c102fdSMatthias Ringwald /** 384745c102fdSMatthias Ringwald * @brief Set Advertisement Parameters 384845c102fdSMatthias Ringwald * @param adv_int_min 384945c102fdSMatthias Ringwald * @param adv_int_max 385045c102fdSMatthias Ringwald * @param adv_type 385145c102fdSMatthias Ringwald * @param direct_address_type 385245c102fdSMatthias Ringwald * @param direct_address 385345c102fdSMatthias Ringwald * @param channel_map 385445c102fdSMatthias Ringwald * @param filter_policy 385545c102fdSMatthias Ringwald * 385645c102fdSMatthias Ringwald * @note internal use. use gap_advertisements_set_params from gap_le.h instead. 385745c102fdSMatthias Ringwald */ 385845c102fdSMatthias Ringwald void hci_le_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type, 3859b95a5a35SMatthias Ringwald uint8_t direct_address_typ, bd_addr_t direct_address, 386045c102fdSMatthias Ringwald uint8_t channel_map, uint8_t filter_policy) { 386145c102fdSMatthias Ringwald 386245c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_min = adv_int_min; 386345c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_max = adv_int_max; 386445c102fdSMatthias Ringwald hci_stack->le_advertisements_type = adv_type; 386545c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address_type = direct_address_typ; 386645c102fdSMatthias Ringwald hci_stack->le_advertisements_channel_map = channel_map; 386745c102fdSMatthias Ringwald hci_stack->le_advertisements_filter_policy = filter_policy; 386845c102fdSMatthias Ringwald memcpy(hci_stack->le_advertisements_direct_address, direct_address, 6); 386945c102fdSMatthias Ringwald 387045c102fdSMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 3871501f56b3SMatthias Ringwald gap_advertisments_changed(); 387245c102fdSMatthias Ringwald } 387345c102fdSMatthias Ringwald 387445c102fdSMatthias Ringwald /** 387545c102fdSMatthias Ringwald * @brief Enable/Disable Advertisements 387645c102fdSMatthias Ringwald * @param enabled 387745c102fdSMatthias Ringwald */ 387845c102fdSMatthias Ringwald void gap_advertisements_enable(int enabled){ 387945c102fdSMatthias Ringwald hci_stack->le_advertisements_enabled = enabled; 388045c102fdSMatthias Ringwald if (enabled && !hci_stack->le_advertisements_active){ 388145c102fdSMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_ENABLE; 388245c102fdSMatthias Ringwald } 388345c102fdSMatthias Ringwald if (!enabled && hci_stack->le_advertisements_active){ 388445c102fdSMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_DISABLE; 388545c102fdSMatthias Ringwald } 3886cfc59f1bSMatthias Ringwald hci_run(); 388745c102fdSMatthias Ringwald } 388845c102fdSMatthias Ringwald 388935454696SMatthias Ringwald #endif 389006e5cf96SMatthias Ringwald 389106e5cf96SMatthias Ringwald void hci_le_set_own_address_type(uint8_t own_address_type){ 389206e5cf96SMatthias Ringwald log_info("hci_le_set_own_address_type: old %u, new %u", hci_stack->le_own_addr_type, own_address_type); 389306e5cf96SMatthias Ringwald if (own_address_type == hci_stack->le_own_addr_type) return; 389406e5cf96SMatthias Ringwald hci_stack->le_own_addr_type = own_address_type; 389506e5cf96SMatthias Ringwald 389664068776SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 389706e5cf96SMatthias Ringwald // update advertisement parameters, too 389806e5cf96SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 389906e5cf96SMatthias Ringwald gap_advertisments_changed(); 390064068776SMatthias Ringwald #endif 390164068776SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 390206e5cf96SMatthias Ringwald // note: we don't update scan parameters or modify ongoing connection attempts 390364068776SMatthias Ringwald #endif 390406e5cf96SMatthias Ringwald } 390506e5cf96SMatthias Ringwald 3906d70217a2SMatthias Ringwald #endif 390745c102fdSMatthias Ringwald 3908616edd56SMatthias Ringwald uint8_t gap_disconnect(hci_con_handle_t handle){ 39095917a5c5S[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 39105917a5c5S[email protected] if (!conn){ 39117851196eSmatthias.ringwald hci_emit_disconnection_complete(handle, 0); 3912616edd56SMatthias Ringwald return 0; 39135917a5c5S[email protected] } 39145917a5c5S[email protected] conn->state = SEND_DISCONNECT; 39155917a5c5S[email protected] hci_run(); 3916616edd56SMatthias Ringwald return 0; 39174f3229d8S[email protected] } 391804a6ef8cSmatthias.ringwald 3919a1bf5ae7SMatthias Ringwald /** 3920a1bf5ae7SMatthias Ringwald * @brief Get connection type 3921a1bf5ae7SMatthias Ringwald * @param con_handle 3922a1bf5ae7SMatthias Ringwald * @result connection_type 3923a1bf5ae7SMatthias Ringwald */ 3924a1bf5ae7SMatthias Ringwald gap_connection_type_t gap_get_connection_type(hci_con_handle_t connection_handle){ 3925a1bf5ae7SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(connection_handle); 3926a1bf5ae7SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 3927a1bf5ae7SMatthias Ringwald switch (conn->address_type){ 3928a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 3929a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 3930a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_LE; 3931a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_SCO: 3932a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_SCO; 3933a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_CLASSIC: 3934a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_ACL; 3935a1bf5ae7SMatthias Ringwald default: 3936a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_INVALID; 3937a1bf5ae7SMatthias Ringwald } 3938a1bf5ae7SMatthias Ringwald } 3939a1bf5ae7SMatthias Ringwald 3940a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 39414f551432SMatthias Ringwald 3942d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 3943d23838ecSMatthias Ringwald /** 3944ac9c45e0SMatthias Ringwald * @brief Auto Connection Establishment - Start Connecting to device 3945ac9c45e0SMatthias Ringwald * @param address_typ 3946ac9c45e0SMatthias Ringwald * @param address 3947ac9c45e0SMatthias Ringwald * @returns 0 if ok 3948ac9c45e0SMatthias Ringwald */ 39494f551432SMatthias Ringwald int gap_auto_connection_start(bd_addr_type_t address_type, bd_addr_t address){ 3950e83201bcSMatthias Ringwald // check capacity 3951665d90f2SMatthias Ringwald int num_entries = btstack_linked_list_count(&hci_stack->le_whitelist); 395291915b0bSMatthias Ringwald if (num_entries >= hci_stack->le_whitelist_capacity) return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED; 3953e83201bcSMatthias Ringwald whitelist_entry_t * entry = btstack_memory_whitelist_entry_get(); 3954e83201bcSMatthias Ringwald if (!entry) return BTSTACK_MEMORY_ALLOC_FAILED; 3955e83201bcSMatthias Ringwald entry->address_type = address_type; 3956e83201bcSMatthias Ringwald memcpy(entry->address, address, 6); 3957e83201bcSMatthias Ringwald entry->state = LE_WHITELIST_ADD_TO_CONTROLLER; 3958665d90f2SMatthias Ringwald btstack_linked_list_add(&hci_stack->le_whitelist, (btstack_linked_item_t*) entry); 3959e83201bcSMatthias Ringwald hci_run(); 3960e83201bcSMatthias Ringwald return 0; 3961ac9c45e0SMatthias Ringwald } 3962ac9c45e0SMatthias Ringwald 396342ff5ba1SMatthias Ringwald static void hci_remove_from_whitelist(bd_addr_type_t address_type, bd_addr_t address){ 3964665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 3965665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 3966665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 3967665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it); 3968e83201bcSMatthias Ringwald if (entry->address_type != address_type) continue; 3969e83201bcSMatthias Ringwald if (memcmp(entry->address, address, 6) != 0) continue; 3970e83201bcSMatthias Ringwald if (entry->state & LE_WHITELIST_ON_CONTROLLER){ 3971e83201bcSMatthias Ringwald // remove from controller if already present 3972e83201bcSMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER; 3973e83201bcSMatthias Ringwald continue; 3974e83201bcSMatthias Ringwald } 3975e83201bcSMatthias Ringwald // direclty remove entry from whitelist 3976665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 3977e83201bcSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 3978e83201bcSMatthias Ringwald } 397942ff5ba1SMatthias Ringwald } 398042ff5ba1SMatthias Ringwald 398142ff5ba1SMatthias Ringwald /** 398242ff5ba1SMatthias Ringwald * @brief Auto Connection Establishment - Stop Connecting to device 398342ff5ba1SMatthias Ringwald * @param address_typ 398442ff5ba1SMatthias Ringwald * @param address 398542ff5ba1SMatthias Ringwald * @returns 0 if ok 398642ff5ba1SMatthias Ringwald */ 398742ff5ba1SMatthias Ringwald int gap_auto_connection_stop(bd_addr_type_t address_type, bd_addr_t address){ 398842ff5ba1SMatthias Ringwald hci_remove_from_whitelist(address_type, address); 3989e83201bcSMatthias Ringwald hci_run(); 3990e83201bcSMatthias Ringwald return 0; 3991ac9c45e0SMatthias Ringwald } 3992ac9c45e0SMatthias Ringwald 3993ac9c45e0SMatthias Ringwald /** 3994ac9c45e0SMatthias Ringwald * @brief Auto Connection Establishment - Stop everything 3995ac9c45e0SMatthias Ringwald * @note Convenience function to stop all active auto connection attempts 3996ac9c45e0SMatthias Ringwald */ 3997ac9c45e0SMatthias Ringwald void gap_auto_connection_stop_all(void){ 3998665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 3999665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 4000665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 4001665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it); 4002e83201bcSMatthias Ringwald if (entry->state & LE_WHITELIST_ON_CONTROLLER){ 4003e83201bcSMatthias Ringwald // remove from controller if already present 4004e83201bcSMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER; 4005e83201bcSMatthias Ringwald continue; 4006e83201bcSMatthias Ringwald } 400791915b0bSMatthias Ringwald // directly remove entry from whitelist 4008665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 4009e83201bcSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 4010e83201bcSMatthias Ringwald } 4011e83201bcSMatthias Ringwald hci_run(); 4012ac9c45e0SMatthias Ringwald } 4013d70217a2SMatthias Ringwald #endif 40144f551432SMatthias Ringwald #endif 40154f551432SMatthias Ringwald 401635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 4017ac9c45e0SMatthias Ringwald /** 4018ff00ed1cSMatthias Ringwald * @brief Set Extended Inquiry Response data 4019ff00ed1cSMatthias Ringwald * @param eir_data size 240 bytes, is not copied make sure memory is accessible during stack startup 4020ff00ed1cSMatthias Ringwald * @note has to be done before stack starts up 4021ff00ed1cSMatthias Ringwald */ 4022ff00ed1cSMatthias Ringwald void gap_set_extended_inquiry_response(const uint8_t * data){ 4023ff00ed1cSMatthias Ringwald hci_stack->eir_data = data; 4024ff00ed1cSMatthias Ringwald } 4025ff00ed1cSMatthias Ringwald 4026ff00ed1cSMatthias Ringwald /** 4027f6858d14SMatthias Ringwald * @brief Set inquiry mode: standard, with RSSI, with RSSI + Extended Inquiry Results. Has to be called before power on. 4028f6858d14SMatthias Ringwald * @param inquriy_mode see bluetooth_defines.h 4029f6858d14SMatthias Ringwald */ 4030f6858d14SMatthias Ringwald void hci_set_inquiry_mode(inquiry_mode_t mode){ 4031f6858d14SMatthias Ringwald hci_stack->inquiry_mode = mode; 4032f6858d14SMatthias Ringwald } 4033f6858d14SMatthias Ringwald 4034f6858d14SMatthias Ringwald /** 4035d950d659SMatthias Ringwald * @brief Configure Voice Setting for use with SCO data in HSP/HFP 4036d950d659SMatthias Ringwald */ 4037d950d659SMatthias Ringwald void hci_set_sco_voice_setting(uint16_t voice_setting){ 4038d950d659SMatthias Ringwald hci_stack->sco_voice_setting = voice_setting; 4039d950d659SMatthias Ringwald } 4040d950d659SMatthias Ringwald 4041d950d659SMatthias Ringwald /** 4042d950d659SMatthias Ringwald * @brief Get SCO Voice Setting 4043d950d659SMatthias Ringwald * @return current voice setting 4044d950d659SMatthias Ringwald */ 40450cb5b971SMatthias Ringwald uint16_t hci_get_sco_voice_setting(void){ 4046d950d659SMatthias Ringwald return hci_stack->sco_voice_setting; 4047d950d659SMatthias Ringwald } 4048d950d659SMatthias Ringwald 4049b3aad8daSMatthias Ringwald /** @brief Get SCO packet length for current SCO Voice setting 4050b3aad8daSMatthias Ringwald * @note Using SCO packets of the exact length is required for USB transfer 4051b3aad8daSMatthias Ringwald * @return Length of SCO packets in bytes (not audio frames) 4052b3aad8daSMatthias Ringwald */ 4053b3aad8daSMatthias Ringwald int hci_get_sco_packet_length(void){ 4054b3aad8daSMatthias Ringwald // see Core Spec for H2 USB Transfer. 4055b3aad8daSMatthias Ringwald if (hci_stack->sco_voice_setting & 0x0020) return 51; 4056b3aad8daSMatthias Ringwald return 27; 4057b3aad8daSMatthias Ringwald } 405835454696SMatthias Ringwald #endif 4059b3aad8daSMatthias Ringwald 4060d950d659SMatthias Ringwald /** 4061d23838ecSMatthias Ringwald * @brief Set callback for Bluetooth Hardware Error 4062d23838ecSMatthias Ringwald */ 4063c2e1fa60SMatthias Ringwald void hci_set_hardware_error_callback(void (*fn)(uint8_t error)){ 4064d23838ecSMatthias Ringwald hci_stack->hardware_error_callback = fn; 4065d23838ecSMatthias Ringwald } 4066d23838ecSMatthias Ringwald 406771de195eSMatthias Ringwald void hci_disconnect_all(void){ 4068665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 4069665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 4070665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 4071665d90f2SMatthias Ringwald hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 407204a6ef8cSmatthias.ringwald if (con->state == SENT_DISCONNECT) continue; 407304a6ef8cSmatthias.ringwald con->state = SEND_DISCONNECT; 407404a6ef8cSmatthias.ringwald } 4075d31fba26S[email protected] hci_run(); 407604a6ef8cSmatthias.ringwald } 407733373e40SMatthias Ringwald 407833373e40SMatthias Ringwald uint16_t hci_get_manufacturer(void){ 407933373e40SMatthias Ringwald return hci_stack->manufacturer; 408033373e40SMatthias Ringwald } 4081