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" 741cfb383eSMatthias Ringwald #include "bluetooth_data_types.h" 754a3574a1SMatthias Ringwald #include "gap.h" 764a3574a1SMatthias Ringwald #include "hci.h" 774a3574a1SMatthias Ringwald #include "hci_cmd.h" 78d8905019Smatthias.ringwald #include "hci_dump.h" 791cfb383eSMatthias Ringwald #include "ad_parser.h" 8093b8dc03Smatthias.ringwald 812b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 822b838201SMatthias Ringwald #ifndef HCI_HOST_ACL_PACKET_NUM 832b838201SMatthias Ringwald #error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_ACL_PACKET_NUM" 842b838201SMatthias Ringwald #endif 852b838201SMatthias Ringwald #ifndef HCI_HOST_ACL_PACKET_LEN 862b838201SMatthias Ringwald #error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_ACL_PACKET_LEN" 872b838201SMatthias Ringwald #endif 882b838201SMatthias Ringwald #ifndef HCI_HOST_SCO_PACKET_NUM 892b838201SMatthias Ringwald #error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_SCO_PACKET_NUM" 902b838201SMatthias Ringwald #endif 912b838201SMatthias Ringwald #ifndef HCI_HOST_SCO_PACKET_LEN 922b838201SMatthias Ringwald #error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_SCO_PACKET_LEN" 932b838201SMatthias Ringwald #endif 942b838201SMatthias Ringwald #endif 951b0e3922Smatthias.ringwald 96169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000 97659d758cSMatthias Ringwald #define HCI_RESET_RESEND_TIMEOUT_MS 200 98ee091cf1Smatthias.ringwald 991cfb383eSMatthias Ringwald // Names are arbitrarily shortened to 32 bytes if not requested otherwise 1001cfb383eSMatthias Ringwald #ifndef GAP_INQUIRY_MAX_NAME_LEN 1011cfb383eSMatthias Ringwald #define GAP_INQUIRY_MAX_NAME_LEN 32 1021cfb383eSMatthias Ringwald #endif 1031cfb383eSMatthias Ringwald 104*f5875de5SMatthias Ringwald // GAP inquiry state: 0 = off, 0x01 - 0x30 = requested duration, 0xfe = active, 0xff = stop requested 105*f5875de5SMatthias Ringwald #define GAP_INQUIRY_DURATION_MIN 0x01 106*f5875de5SMatthias Ringwald #define GAP_INQUIRY_DURATION_MAX 0x30 107*f5875de5SMatthias Ringwald #define GAP_INQUIRY_STATE_ACTIVE 0x80 108*f5875de5SMatthias Ringwald #define GAP_INQUIRY_STATE_IDLE 0 109*f5875de5SMatthias Ringwald #define GAP_INQUIRY_STATE_W2_CANCEL 0x81 110*f5875de5SMatthias Ringwald #define GAP_INQUIRY_STATE_W4_CANCELLED 0x82 111*f5875de5SMatthias Ringwald 112b83d5eabSMatthias Ringwald // prototypes 11335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 114758b46ceSmatthias.ringwald static void hci_update_scan_enable(void); 11535454696SMatthias Ringwald static void hci_emit_discoverable_enabled(uint8_t enabled); 11635454696SMatthias Ringwald static int hci_local_ssp_activated(void); 11735454696SMatthias Ringwald static int hci_remote_ssp_supported(hci_con_handle_t con_handle); 11835454696SMatthias Ringwald static void hci_notify_if_sco_can_send_now(void); 11935454696SMatthias Ringwald static void hci_emit_connection_complete(bd_addr_t address, hci_con_handle_t con_handle, uint8_t status); 120a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection); 12135454696SMatthias Ringwald static void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level); 122ec820d77SMatthias Ringwald static void hci_connection_timeout_handler(btstack_timer_source_t *timer); 12396a45072S[email protected] static void hci_connection_timestamp(hci_connection_t *connection); 12452db98b2SMatthias Ringwald static void hci_emit_l2cap_check_timeout(hci_connection_t *conn); 1251cfb383eSMatthias Ringwald static void gap_inquiry_explode(uint8_t * packet); 12652db98b2SMatthias Ringwald #endif 1271cfb383eSMatthias Ringwald 1287586ee35S[email protected] static int hci_power_control_on(void); 1297586ee35S[email protected] static void hci_power_control_off(void); 1306da48142SSean Wilson static void hci_state_reset(void); 131fc64f94aSMatthias Ringwald static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason); 132b83d5eabSMatthias Ringwald static void hci_emit_nr_connections_changed(void); 133b83d5eabSMatthias Ringwald static void hci_emit_hci_open_failed(void); 134b83d5eabSMatthias Ringwald static void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status); 135b83d5eabSMatthias Ringwald static void hci_emit_event(uint8_t * event, uint16_t size, int dump); 136b83d5eabSMatthias Ringwald static void hci_emit_acl_packet(uint8_t * packet, uint16_t size); 13795d71764SMatthias Ringwald static void hci_run(void); 13895d71764SMatthias Ringwald static int hci_is_le_connection(hci_connection_t * connection); 13995d71764SMatthias Ringwald static int hci_number_free_acl_slots_for_connection_type( bd_addr_type_t address_type); 1405d509858SMatthias Ringwald 141a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 142e8c8828eSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 14339677e66SMatthias Ringwald // called from test/ble_client/advertising_data_parser.c 14439677e66SMatthias Ringwald void le_handle_advertisement_report(uint8_t *packet, int size); 14542ff5ba1SMatthias Ringwald static void hci_remove_from_whitelist(bd_addr_type_t address_type, bd_addr_t address); 1465d509858SMatthias Ringwald #endif 147d70217a2SMatthias Ringwald #endif 148758b46ceSmatthias.ringwald 14906b35ec0Smatthias.ringwald // the STACK is here 1503a9fb326S[email protected] #ifndef HAVE_MALLOC 1513a9fb326S[email protected] static hci_stack_t hci_stack_static; 1523a9fb326S[email protected] #endif 1533a9fb326S[email protected] static hci_stack_t * hci_stack = NULL; 15416833f0aSmatthias.ringwald 1551c9e5e9dSMatthias Ringwald #ifdef ENABLE_CLASSIC 15666fb9560S[email protected] // test helper 15766fb9560S[email protected] static uint8_t disable_l2cap_timeouts = 0; 1581c9e5e9dSMatthias Ringwald #endif 15966fb9560S[email protected] 16096a45072S[email protected] /** 16196a45072S[email protected] * create connection for given address 16296a45072S[email protected] * 16396a45072S[email protected] * @return connection OR NULL, if no memory left 16496a45072S[email protected] */ 16596a45072S[email protected] static hci_connection_t * create_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){ 1661a06f663S[email protected] log_info("create_connection_for_addr %s, type %x", bd_addr_to_str(addr), addr_type); 167bb69aaaeS[email protected] hci_connection_t * conn = btstack_memory_hci_connection_get(); 16896a45072S[email protected] if (!conn) return NULL; 169c91d150bS[email protected] memset(conn, 0, sizeof(hci_connection_t)); 170058e3d6bSMatthias Ringwald bd_addr_copy(conn->address, addr); 17196a45072S[email protected] conn->address_type = addr_type; 17296a45072S[email protected] conn->con_handle = 0xffff; 17396a45072S[email protected] conn->authentication_flags = AUTH_FLAGS_NONE; 17496a45072S[email protected] conn->bonding_flags = 0; 17596a45072S[email protected] conn->requested_security_level = LEVEL_0; 17652db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 17791a977e8SMatthias Ringwald btstack_run_loop_set_timer_handler(&conn->timeout, hci_connection_timeout_handler); 17891a977e8SMatthias Ringwald btstack_run_loop_set_timer_context(&conn->timeout, conn); 17996a45072S[email protected] hci_connection_timestamp(conn); 18052db98b2SMatthias Ringwald #endif 18196a45072S[email protected] conn->acl_recombination_length = 0; 18296a45072S[email protected] conn->acl_recombination_pos = 0; 18396a45072S[email protected] conn->num_acl_packets_sent = 0; 1841a06f663S[email protected] conn->num_sco_packets_sent = 0; 185da886c03S[email protected] conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 186665d90f2SMatthias Ringwald btstack_linked_list_add(&hci_stack->connections, (btstack_linked_item_t *) conn); 18796a45072S[email protected] return conn; 18896a45072S[email protected] } 18966fb9560S[email protected] 190da886c03S[email protected] 191da886c03S[email protected] /** 192da886c03S[email protected] * get le connection parameter range 193da886c03S[email protected] * 194da886c03S[email protected] * @return le connection parameter range struct 195da886c03S[email protected] */ 1964ced4e8cSMatthias Ringwald void gap_get_connection_parameter_range(le_connection_parameter_range_t * range){ 1974ced4e8cSMatthias Ringwald *range = hci_stack->le_connection_parameter_range; 198da886c03S[email protected] } 199da886c03S[email protected] 200da886c03S[email protected] /** 201da886c03S[email protected] * set le connection parameter range 202da886c03S[email protected] * 203da886c03S[email protected] */ 204da886c03S[email protected] 2054ced4e8cSMatthias Ringwald void gap_set_connection_parameter_range(le_connection_parameter_range_t *range){ 2064ced4e8cSMatthias Ringwald hci_stack->le_connection_parameter_range = *range; 207da886c03S[email protected] } 208da886c03S[email protected] 209da886c03S[email protected] /** 210da886c03S[email protected] * get hci connections iterator 211da886c03S[email protected] * 212da886c03S[email protected] * @return hci connections iterator 213da886c03S[email protected] */ 214da886c03S[email protected] 215665d90f2SMatthias Ringwald void hci_connections_get_iterator(btstack_linked_list_iterator_t *it){ 216665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(it, &hci_stack->connections); 217da886c03S[email protected] } 218da886c03S[email protected] 21997addcc5Smatthias.ringwald /** 220ee091cf1Smatthias.ringwald * get connection for a given handle 221ee091cf1Smatthias.ringwald * 222ee091cf1Smatthias.ringwald * @return connection OR NULL, if not found 223ee091cf1Smatthias.ringwald */ 2245061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){ 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 * item = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 2293ac2fe56S[email protected] if ( item->con_handle == con_handle ) { 230da886c03S[email protected] return item; 231ee091cf1Smatthias.ringwald } 232ee091cf1Smatthias.ringwald } 233ee091cf1Smatthias.ringwald return NULL; 234ee091cf1Smatthias.ringwald } 235ee091cf1Smatthias.ringwald 23696a45072S[email protected] /** 23796a45072S[email protected] * get connection for given address 23896a45072S[email protected] * 23996a45072S[email protected] * @return connection OR NULL, if not found 24096a45072S[email protected] */ 2412e77e513S[email protected] hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){ 242665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 243665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 244665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 245665d90f2SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 24696a45072S[email protected] if (connection->address_type != addr_type) continue; 24796a45072S[email protected] if (memcmp(addr, connection->address, 6) != 0) continue; 24862bda3fbS[email protected] return connection; 24962bda3fbS[email protected] } 25062bda3fbS[email protected] return NULL; 25162bda3fbS[email protected] } 25262bda3fbS[email protected] 25352db98b2SMatthias Ringwald 25452db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 25552db98b2SMatthias Ringwald 256ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 257ee752bb8SMatthias Ringwald static int hci_number_sco_connections(void){ 258ee752bb8SMatthias Ringwald int connections = 0; 259ee752bb8SMatthias Ringwald btstack_linked_list_iterator_t it; 260ee752bb8SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 261ee752bb8SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 262ee752bb8SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 263ee752bb8SMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_SCO) continue; 264ee752bb8SMatthias Ringwald connections++; 265ee752bb8SMatthias Ringwald } 266ee752bb8SMatthias Ringwald return connections; 267ee752bb8SMatthias Ringwald } 268ee752bb8SMatthias Ringwald #endif 269ee752bb8SMatthias Ringwald 270ec820d77SMatthias Ringwald static void hci_connection_timeout_handler(btstack_timer_source_t *timer){ 27191a977e8SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_run_loop_get_timer_context(timer); 272aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK 273528a4a3bSMatthias Ringwald if (btstack_run_loop_embedded_get_ticks() > connection->timestamp + btstack_run_loop_embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){ 274c785ef68Smatthias.ringwald // connections might be timed out 275c785ef68Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 276c785ef68Smatthias.ringwald } 277f316a845SMatthias Ringwald #else 278528a4a3bSMatthias Ringwald if (btstack_run_loop_get_time_ms() > connection->timestamp + HCI_CONNECTION_TIMEOUT_MS){ 2795f26aadcSMatthias Ringwald // connections might be timed out 2805f26aadcSMatthias Ringwald hci_emit_l2cap_check_timeout(connection); 2815f26aadcSMatthias Ringwald } 2825f26aadcSMatthias Ringwald #endif 283c785ef68Smatthias.ringwald } 284ee091cf1Smatthias.ringwald 285ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){ 286aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK 287528a4a3bSMatthias Ringwald connection->timestamp = btstack_run_loop_embedded_get_ticks(); 288f316a845SMatthias Ringwald #else 289528a4a3bSMatthias Ringwald connection->timestamp = btstack_run_loop_get_time_ms(); 2905f26aadcSMatthias Ringwald #endif 291ee091cf1Smatthias.ringwald } 292ee091cf1Smatthias.ringwald 29328ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 29428ca2b46S[email protected] conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags); 29528ca2b46S[email protected] } 29628ca2b46S[email protected] 29735454696SMatthias Ringwald 29828ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 29928ca2b46S[email protected] conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags); 30028ca2b46S[email protected] } 30128ca2b46S[email protected] 30243bfb1bdSmatthias.ringwald /** 30380ca58a0Smatthias.ringwald * add authentication flags and reset timer 30496a45072S[email protected] * @note: assumes classic connection 3052e77e513S[email protected] * @note: bd_addr is passed in as litle endian uint8_t * as it is called from parsing packets 3067fde4af9Smatthias.ringwald */ 3077fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){ 3087fde4af9Smatthias.ringwald bd_addr_t addr; 309724d70a2SMatthias Ringwald reverse_bd_addr(bd_addr, addr); 3102e77e513S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 3117fde4af9Smatthias.ringwald if (conn) { 31228ca2b46S[email protected] connectionSetAuthenticationFlags(conn, flags); 31380ca58a0Smatthias.ringwald hci_connection_timestamp(conn); 3147fde4af9Smatthias.ringwald } 3157fde4af9Smatthias.ringwald } 3167fde4af9Smatthias.ringwald 31780ca58a0Smatthias.ringwald int hci_authentication_active_for_handle(hci_con_handle_t handle){ 3185061f3afS[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 31980ca58a0Smatthias.ringwald if (!conn) return 0; 3206724cd9eS[email protected] if (conn->authentication_flags & LEGACY_PAIRING_ACTIVE) return 1; 3216724cd9eS[email protected] if (conn->authentication_flags & SSP_PAIRING_ACTIVE) return 1; 3226724cd9eS[email protected] return 0; 32380ca58a0Smatthias.ringwald } 32480ca58a0Smatthias.ringwald 32515a95bd5SMatthias Ringwald void gap_drop_link_key_for_bd_addr(bd_addr_t addr){ 32655597469SMatthias Ringwald if (!hci_stack->link_key_db) return; 3272bacf595SMatthias Ringwald log_info("gap_drop_link_key_for_bd_addr: %s", bd_addr_to_str(addr)); 328a98592bcSMatthias Ringwald hci_stack->link_key_db->delete_link_key(addr); 329c12e46e7Smatthias.ringwald } 33055597469SMatthias Ringwald 33155597469SMatthias Ringwald void gap_store_link_key_for_bd_addr(bd_addr_t addr, link_key_t link_key, link_key_type_t type){ 33255597469SMatthias Ringwald if (!hci_stack->link_key_db) return; 3332bacf595SMatthias Ringwald log_info("gap_store_link_key_for_bd_addr: %s, type %u", bd_addr_to_str(addr), type); 33455597469SMatthias Ringwald hci_stack->link_key_db->put_link_key(addr, link_key, type); 335c12e46e7Smatthias.ringwald } 33635454696SMatthias Ringwald #endif 337c12e46e7Smatthias.ringwald 33895d71764SMatthias Ringwald static int hci_is_le_connection(hci_connection_t * connection){ 3390bf6344aS[email protected] return connection->address_type == BD_ADDR_TYPE_LE_PUBLIC || 3400bf6344aS[email protected] connection->address_type == BD_ADDR_TYPE_LE_RANDOM; 3410bf6344aS[email protected] } 3420bf6344aS[email protected] 3437fde4af9Smatthias.ringwald /** 34443bfb1bdSmatthias.ringwald * count connections 34543bfb1bdSmatthias.ringwald */ 34640d1c7a4Smatthias.ringwald static int nr_hci_connections(void){ 34756c253c9Smatthias.ringwald int count = 0; 348665d90f2SMatthias Ringwald btstack_linked_item_t *it; 349665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next, count++); 35043bfb1bdSmatthias.ringwald return count; 35143bfb1bdSmatthias.ringwald } 352c8e4258aSmatthias.ringwald 35395d71764SMatthias Ringwald static int hci_number_free_acl_slots_for_connection_type(bd_addr_type_t address_type){ 354ee303eddS[email protected] 355f04a0c31SMatthias Ringwald unsigned int num_packets_sent_classic = 0; 356f04a0c31SMatthias Ringwald unsigned int num_packets_sent_le = 0; 357ee303eddS[email protected] 358665d90f2SMatthias Ringwald btstack_linked_item_t *it; 359665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 360998906cdSmatthias.ringwald hci_connection_t * connection = (hci_connection_t *) it; 361ee303eddS[email protected] if (connection->address_type == BD_ADDR_TYPE_CLASSIC){ 362ee303eddS[email protected] num_packets_sent_classic += connection->num_acl_packets_sent; 363ee303eddS[email protected] } else { 364ee303eddS[email protected] num_packets_sent_le += connection->num_acl_packets_sent; 365ee303eddS[email protected] } 366ee303eddS[email protected] } 367d999b54eSMatthias Ringwald log_debug("ACL classic buffers: %u used of %u", num_packets_sent_classic, hci_stack->acl_packets_total_num); 368ee303eddS[email protected] int free_slots_classic = hci_stack->acl_packets_total_num - num_packets_sent_classic; 369ee303eddS[email protected] int free_slots_le = 0; 370ee303eddS[email protected] 371ee303eddS[email protected] if (free_slots_classic < 0){ 3729da54300S[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); 373998906cdSmatthias.ringwald return 0; 374998906cdSmatthias.ringwald } 375ee303eddS[email protected] 376ee303eddS[email protected] if (hci_stack->le_acl_packets_total_num){ 377ee303eddS[email protected] // if we have LE slots, they are used 378ee303eddS[email protected] free_slots_le = hci_stack->le_acl_packets_total_num - num_packets_sent_le; 379ee303eddS[email protected] if (free_slots_le < 0){ 3809da54300S[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); 381ee303eddS[email protected] return 0; 382998906cdSmatthias.ringwald } 383ee303eddS[email protected] } else { 384ee303eddS[email protected] // otherwise, classic slots are used for LE, too 385ee303eddS[email protected] free_slots_classic -= num_packets_sent_le; 386ee303eddS[email protected] if (free_slots_classic < 0){ 3879da54300S[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); 388ee303eddS[email protected] return 0; 389ee303eddS[email protected] } 390ee303eddS[email protected] } 391ee303eddS[email protected] 392ee303eddS[email protected] switch (address_type){ 393ee303eddS[email protected] case BD_ADDR_TYPE_UNKNOWN: 3942125de09SMatthias Ringwald log_error("hci_number_free_acl_slots: unknown address type"); 395ee303eddS[email protected] return 0; 396ee303eddS[email protected] 397ee303eddS[email protected] case BD_ADDR_TYPE_CLASSIC: 398ee303eddS[email protected] return free_slots_classic; 399ee303eddS[email protected] 400ee303eddS[email protected] default: 401cb00d3aaS[email protected] if (hci_stack->le_acl_packets_total_num){ 402ee303eddS[email protected] return free_slots_le; 403ee303eddS[email protected] } 404cb00d3aaS[email protected] return free_slots_classic; 405cb00d3aaS[email protected] } 406998906cdSmatthias.ringwald } 407998906cdSmatthias.ringwald 4082125de09SMatthias Ringwald int hci_number_free_acl_slots_for_handle(hci_con_handle_t con_handle){ 4092125de09SMatthias Ringwald // get connection type 4102125de09SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 4112125de09SMatthias Ringwald if (!connection){ 4122125de09SMatthias Ringwald log_error("hci_number_free_acl_slots: handle 0x%04x not in connection list", con_handle); 4132125de09SMatthias Ringwald return 0; 4142125de09SMatthias Ringwald } 4152125de09SMatthias Ringwald return hci_number_free_acl_slots_for_connection_type(connection->address_type); 4162125de09SMatthias Ringwald } 4172125de09SMatthias Ringwald 41835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 419701e3307SMatthias Ringwald static int hci_number_free_sco_slots(void){ 420f04a0c31SMatthias Ringwald unsigned int num_sco_packets_sent = 0; 421665d90f2SMatthias Ringwald btstack_linked_item_t *it; 422665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 423e35edcc1S[email protected] hci_connection_t * connection = (hci_connection_t *) it; 424e35edcc1S[email protected] num_sco_packets_sent += connection->num_sco_packets_sent; 425e35edcc1S[email protected] } 426e35edcc1S[email protected] if (num_sco_packets_sent > hci_stack->sco_packets_total_num){ 427701e3307SMatthias Ringwald log_info("hci_number_free_sco_slots:packets (%u) > total packets (%u)", num_sco_packets_sent, hci_stack->sco_packets_total_num); 42844d0e3d5S[email protected] return 0; 42944d0e3d5S[email protected] } 430701e3307SMatthias Ringwald // log_info("hci_number_free_sco_slots u", handle, num_sco_packets_sent); 431e35edcc1S[email protected] return hci_stack->sco_packets_total_num - num_sco_packets_sent; 432e35edcc1S[email protected] } 43335454696SMatthias Ringwald #endif 43444d0e3d5S[email protected] 4352b838201SMatthias Ringwald // only used to send HCI Host Number Completed Packets 4362b838201SMatthias Ringwald static int hci_can_send_comand_packet_transport(void){ 437ac928cc2S[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 438ac928cc2S[email protected] 439ac928cc2S[email protected] // check for async hci transport implementations 440ac928cc2S[email protected] if (hci_stack->hci_transport->can_send_packet_now){ 441ac928cc2S[email protected] if (!hci_stack->hci_transport->can_send_packet_now(HCI_COMMAND_DATA_PACKET)){ 442ac928cc2S[email protected] return 0; 443ac928cc2S[email protected] } 444ac928cc2S[email protected] } 4452b838201SMatthias Ringwald return 1; 4462b838201SMatthias Ringwald } 447ac928cc2S[email protected] 4482b838201SMatthias Ringwald // new functions replacing hci_can_send_packet_now[_using_packet_buffer] 4492b838201SMatthias Ringwald int hci_can_send_command_packet_now(void){ 4502b838201SMatthias Ringwald if (hci_can_send_comand_packet_transport() == 0) return 0; 451ac928cc2S[email protected] return hci_stack->num_cmd_packets > 0; 452ac928cc2S[email protected] } 453ac928cc2S[email protected] 4549d04d3a7SMatthias Ringwald static int hci_transport_can_send_prepared_packet_now(uint8_t packet_type){ 455ac928cc2S[email protected] // check for async hci transport implementations 4569d04d3a7SMatthias Ringwald if (!hci_stack->hci_transport->can_send_packet_now) return 1; 4579d04d3a7SMatthias Ringwald return hci_stack->hci_transport->can_send_packet_now(packet_type); 458ac928cc2S[email protected] } 4599d04d3a7SMatthias Ringwald 4609d04d3a7SMatthias Ringwald static int hci_can_send_prepared_acl_packet_for_address_type(bd_addr_type_t address_type){ 4619d04d3a7SMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_ACL_DATA_PACKET)) return 0; 4629d04d3a7SMatthias Ringwald return hci_number_free_acl_slots_for_connection_type(address_type) > 0; 463ac928cc2S[email protected] } 4649d04d3a7SMatthias Ringwald 4659d04d3a7SMatthias Ringwald int hci_can_send_acl_le_packet_now(void){ 4669d04d3a7SMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return 0; 4679d04d3a7SMatthias Ringwald return hci_can_send_prepared_acl_packet_for_address_type(BD_ADDR_TYPE_LE_PUBLIC); 4689d04d3a7SMatthias Ringwald } 4699d04d3a7SMatthias Ringwald 4709d04d3a7SMatthias Ringwald int hci_can_send_prepared_acl_packet_now(hci_con_handle_t con_handle) { 4719d04d3a7SMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_ACL_DATA_PACKET)) return 0; 472e79abdd6S[email protected] return hci_number_free_acl_slots_for_handle(con_handle) > 0; 473ac928cc2S[email protected] } 474ac928cc2S[email protected] 475ac928cc2S[email protected] int hci_can_send_acl_packet_now(hci_con_handle_t con_handle){ 476ac928cc2S[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 477ac928cc2S[email protected] return hci_can_send_prepared_acl_packet_now(con_handle); 4786b4af23dS[email protected] } 4796b4af23dS[email protected] 48035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 48135454696SMatthias Ringwald int hci_can_send_acl_classic_packet_now(void){ 48235454696SMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return 0; 48335454696SMatthias Ringwald return hci_can_send_prepared_acl_packet_for_address_type(BD_ADDR_TYPE_CLASSIC); 48435454696SMatthias Ringwald } 48535454696SMatthias Ringwald 486701e3307SMatthias Ringwald int hci_can_send_prepared_sco_packet_now(void){ 487d057580eSMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_SCO_DATA_PACKET)) return 0; 488ed361f5fSMatthias Ringwald if (!hci_stack->synchronous_flow_control_enabled) return 1; 489701e3307SMatthias Ringwald return hci_number_free_sco_slots() > 0; 49044d0e3d5S[email protected] } 49144d0e3d5S[email protected] 492701e3307SMatthias Ringwald int hci_can_send_sco_packet_now(void){ 493d057580eSMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return 0; 494701e3307SMatthias Ringwald return hci_can_send_prepared_sco_packet_now(); 49544d0e3d5S[email protected] } 49644d0e3d5S[email protected] 497d057580eSMatthias Ringwald void hci_request_sco_can_send_now_event(void){ 498d057580eSMatthias Ringwald hci_stack->sco_waiting_for_can_send_now = 1; 499d057580eSMatthias Ringwald hci_notify_if_sco_can_send_now(); 500d057580eSMatthias Ringwald } 50135454696SMatthias Ringwald #endif 502d057580eSMatthias Ringwald 50395d71764SMatthias Ringwald // used for internal checks in l2cap.c 504c8b9416aS[email protected] int hci_is_packet_buffer_reserved(void){ 505c8b9416aS[email protected] return hci_stack->hci_packet_buffer_reserved; 506c8b9416aS[email protected] } 507c8b9416aS[email protected] 5086b4af23dS[email protected] // reserves outgoing packet buffer. @returns 1 if successful 5096b4af23dS[email protected] int hci_reserve_packet_buffer(void){ 5109d14b626S[email protected] if (hci_stack->hci_packet_buffer_reserved) { 5119d14b626S[email protected] log_error("hci_reserve_packet_buffer called but buffer already reserved"); 5129d14b626S[email protected] return 0; 5139d14b626S[email protected] } 5146b4af23dS[email protected] hci_stack->hci_packet_buffer_reserved = 1; 5156b4af23dS[email protected] return 1; 5166b4af23dS[email protected] } 5176b4af23dS[email protected] 51868a0fcf7S[email protected] void hci_release_packet_buffer(void){ 51968a0fcf7S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 52068a0fcf7S[email protected] } 52168a0fcf7S[email protected] 5226b4af23dS[email protected] // assumption: synchronous implementations don't provide can_send_packet_now as they don't keep the buffer after the call 5237f02f414SMatthias Ringwald static int hci_transport_synchronous(void){ 5246b4af23dS[email protected] return hci_stack->hci_transport->can_send_packet_now == NULL; 5256b4af23dS[email protected] } 5266b4af23dS[email protected] 527452cf3bbS[email protected] static int hci_send_acl_packet_fragments(hci_connection_t *connection){ 528452cf3bbS[email protected] 529452cf3bbS[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); 530452cf3bbS[email protected] 531452cf3bbS[email protected] // max ACL data packet length depends on connection type (LE vs. Classic) and available buffers 532452cf3bbS[email protected] uint16_t max_acl_data_packet_length = hci_stack->acl_data_packet_length; 533452cf3bbS[email protected] if (hci_is_le_connection(connection) && hci_stack->le_data_packets_length > 0){ 534452cf3bbS[email protected] max_acl_data_packet_length = hci_stack->le_data_packets_length; 535452cf3bbS[email protected] } 536452cf3bbS[email protected] 537452cf3bbS[email protected] // testing: reduce buffer to minimum 538452cf3bbS[email protected] // max_acl_data_packet_length = 52; 539452cf3bbS[email protected] 540d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments entered"); 541d999b54eSMatthias Ringwald 542452cf3bbS[email protected] int err; 543452cf3bbS[email protected] // multiple packets could be send on a synchronous HCI transport 544452cf3bbS[email protected] while (1){ 545452cf3bbS[email protected] 546d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop entered"); 547d999b54eSMatthias Ringwald 548452cf3bbS[email protected] // get current data 549452cf3bbS[email protected] const uint16_t acl_header_pos = hci_stack->acl_fragmentation_pos - 4; 550452cf3bbS[email protected] int current_acl_data_packet_length = hci_stack->acl_fragmentation_total_size - hci_stack->acl_fragmentation_pos; 551452cf3bbS[email protected] int more_fragments = 0; 552452cf3bbS[email protected] 553452cf3bbS[email protected] // if ACL packet is larger than Bluetooth packet buffer, only send max_acl_data_packet_length 554452cf3bbS[email protected] if (current_acl_data_packet_length > max_acl_data_packet_length){ 555452cf3bbS[email protected] more_fragments = 1; 556452cf3bbS[email protected] current_acl_data_packet_length = max_acl_data_packet_length; 557452cf3bbS[email protected] } 558452cf3bbS[email protected] 559452cf3bbS[email protected] // copy handle_and_flags if not first fragment and update packet boundary flags to be 01 (continuing fragmnent) 560452cf3bbS[email protected] if (acl_header_pos > 0){ 561f8fbdce0SMatthias Ringwald uint16_t handle_and_flags = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 562452cf3bbS[email protected] handle_and_flags = (handle_and_flags & 0xcfff) | (1 << 12); 563f8fbdce0SMatthias Ringwald little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos, handle_and_flags); 564452cf3bbS[email protected] } 565452cf3bbS[email protected] 566452cf3bbS[email protected] // update header len 567f8fbdce0SMatthias Ringwald little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos + 2, current_acl_data_packet_length); 568452cf3bbS[email protected] 569452cf3bbS[email protected] // count packet 570452cf3bbS[email protected] connection->num_acl_packets_sent++; 571f04a0c31SMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop before send (more fragments %d)", more_fragments); 572d999b54eSMatthias Ringwald 573d999b54eSMatthias Ringwald // update state for next fragment (if any) as "transport done" might be sent during send_packet already 574d999b54eSMatthias Ringwald if (more_fragments){ 575d999b54eSMatthias Ringwald // update start of next fragment to send 576d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_pos += current_acl_data_packet_length; 577d999b54eSMatthias Ringwald } else { 578d999b54eSMatthias Ringwald // done 579d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_pos = 0; 580d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_total_size = 0; 581d999b54eSMatthias Ringwald } 582452cf3bbS[email protected] 583452cf3bbS[email protected] // send packet 584452cf3bbS[email protected] uint8_t * packet = &hci_stack->hci_packet_buffer[acl_header_pos]; 585452cf3bbS[email protected] const int size = current_acl_data_packet_length + 4; 5865bb5bc3eS[email protected] hci_dump_packet(HCI_ACL_DATA_PACKET, 0, packet, size); 587452cf3bbS[email protected] err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size); 588452cf3bbS[email protected] 589f04a0c31SMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop after send (more fragments %d)", more_fragments); 590d999b54eSMatthias Ringwald 591452cf3bbS[email protected] // done yet? 592452cf3bbS[email protected] if (!more_fragments) break; 593452cf3bbS[email protected] 594452cf3bbS[email protected] // can send more? 595452cf3bbS[email protected] if (!hci_can_send_prepared_acl_packet_now(connection->con_handle)) return err; 596452cf3bbS[email protected] } 597452cf3bbS[email protected] 598d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop over"); 599452cf3bbS[email protected] 600d051460cS[email protected] // release buffer now for synchronous transport 601203bace6S[email protected] if (hci_transport_synchronous()){ 602452cf3bbS[email protected] hci_release_packet_buffer(); 60363fa3374SMatthias Ringwald // notify upper stack that it might be possible to send again 60463fa3374SMatthias Ringwald uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; 605d6b06661SMatthias Ringwald hci_emit_event(&event[0], sizeof(event), 0); // don't dump 606452cf3bbS[email protected] } 607452cf3bbS[email protected] 608452cf3bbS[email protected] return err; 609452cf3bbS[email protected] } 610452cf3bbS[email protected] 611826f7347S[email protected] // pre: caller has reserved the packet buffer 612826f7347S[email protected] int hci_send_acl_packet_buffer(int size){ 6137856c818Smatthias.ringwald 614452cf3bbS[email protected] // log_info("hci_send_acl_packet_buffer size %u", size); 615452cf3bbS[email protected] 616826f7347S[email protected] if (!hci_stack->hci_packet_buffer_reserved) { 617826f7347S[email protected] log_error("hci_send_acl_packet_buffer called without reserving packet buffer"); 618826f7347S[email protected] return 0; 619826f7347S[email protected] } 620826f7347S[email protected] 621d713a683S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 622d713a683S[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 623d713a683S[email protected] 624826f7347S[email protected] // check for free places on Bluetooth module 625d713a683S[email protected] if (!hci_can_send_prepared_acl_packet_now(con_handle)) { 626826f7347S[email protected] log_error("hci_send_acl_packet_buffer called but no free ACL buffers on controller"); 62797b61c7bS[email protected] hci_release_packet_buffer(); 62897b61c7bS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 62997b61c7bS[email protected] } 6306218e6f1Smatthias.ringwald 6315061f3afS[email protected] hci_connection_t *connection = hci_connection_for_handle( con_handle); 63297b61c7bS[email protected] if (!connection) { 6335fa0b7cfS[email protected] log_error("hci_send_acl_packet_buffer called but no connection for handle 0x%04x", con_handle); 63497b61c7bS[email protected] hci_release_packet_buffer(); 63597b61c7bS[email protected] return 0; 63697b61c7bS[email protected] } 63752db98b2SMatthias Ringwald 63852db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 63956cf178bSmatthias.ringwald hci_connection_timestamp(connection); 64052db98b2SMatthias Ringwald #endif 64156cf178bSmatthias.ringwald 642452cf3bbS[email protected] // hci_dump_packet( HCI_ACL_DATA_PACKET, 0, packet, size); 6437856c818Smatthias.ringwald 644452cf3bbS[email protected] // setup data 645452cf3bbS[email protected] hci_stack->acl_fragmentation_total_size = size; 646452cf3bbS[email protected] hci_stack->acl_fragmentation_pos = 4; // start of L2CAP packet 6476218e6f1Smatthias.ringwald 648452cf3bbS[email protected] return hci_send_acl_packet_fragments(connection); 649ee091cf1Smatthias.ringwald } 650ee091cf1Smatthias.ringwald 65135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 65244d0e3d5S[email protected] // pre: caller has reserved the packet buffer 65344d0e3d5S[email protected] int hci_send_sco_packet_buffer(int size){ 65444d0e3d5S[email protected] 65544d0e3d5S[email protected] // log_info("hci_send_acl_packet_buffer size %u", size); 65644d0e3d5S[email protected] 65744d0e3d5S[email protected] if (!hci_stack->hci_packet_buffer_reserved) { 65844d0e3d5S[email protected] log_error("hci_send_acl_packet_buffer called without reserving packet buffer"); 65944d0e3d5S[email protected] return 0; 66044d0e3d5S[email protected] } 66144d0e3d5S[email protected] 66244d0e3d5S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 6634b3e1e19SMatthias Ringwald 6644b3e1e19SMatthias Ringwald // skip checks in loopback mode 6654b3e1e19SMatthias Ringwald if (!hci_stack->loopback_mode){ 66644d0e3d5S[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); // same for ACL and SCO 66744d0e3d5S[email protected] 66844d0e3d5S[email protected] // check for free places on Bluetooth module 669701e3307SMatthias Ringwald if (!hci_can_send_prepared_sco_packet_now()) { 67044d0e3d5S[email protected] log_error("hci_send_sco_packet_buffer called but no free ACL buffers on controller"); 67144d0e3d5S[email protected] hci_release_packet_buffer(); 67244d0e3d5S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 67344d0e3d5S[email protected] } 67444d0e3d5S[email protected] 675e35edcc1S[email protected] // track send packet in connection struct 676e35edcc1S[email protected] hci_connection_t *connection = hci_connection_for_handle( con_handle); 677e35edcc1S[email protected] if (!connection) { 678e35edcc1S[email protected] log_error("hci_send_sco_packet_buffer called but no connection for handle 0x%04x", con_handle); 679e35edcc1S[email protected] hci_release_packet_buffer(); 680e35edcc1S[email protected] return 0; 681e35edcc1S[email protected] } 682e35edcc1S[email protected] connection->num_sco_packets_sent++; 6834b3e1e19SMatthias Ringwald } 68444d0e3d5S[email protected] 68544d0e3d5S[email protected] hci_dump_packet( HCI_SCO_DATA_PACKET, 0, packet, size); 686543e835cSMatthias Ringwald int err = hci_stack->hci_transport->send_packet(HCI_SCO_DATA_PACKET, packet, size); 687543e835cSMatthias Ringwald 688543e835cSMatthias Ringwald if (hci_transport_synchronous()){ 689543e835cSMatthias Ringwald hci_release_packet_buffer(); 69063fa3374SMatthias Ringwald // notify upper stack that it might be possible to send again 69163fa3374SMatthias Ringwald uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; 692d6b06661SMatthias Ringwald hci_emit_event(&event[0], sizeof(event), 0); // don't dump 693543e835cSMatthias Ringwald } 694543e835cSMatthias Ringwald 695543e835cSMatthias Ringwald return err; 69644d0e3d5S[email protected] } 69735454696SMatthias Ringwald #endif 69844d0e3d5S[email protected] 69916833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){ 7007856c818Smatthias.ringwald 701e76a89eeS[email protected] // log_info("acl_handler: size %u", size); 702e76a89eeS[email protected] 7037856c818Smatthias.ringwald // get info 7047856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 7055061f3afS[email protected] hci_connection_t *conn = hci_connection_for_handle(con_handle); 7067856c818Smatthias.ringwald uint8_t acl_flags = READ_ACL_FLAGS(packet); 7077856c818Smatthias.ringwald uint16_t acl_length = READ_ACL_LENGTH(packet); 7087856c818Smatthias.ringwald 7097856c818Smatthias.ringwald // ignore non-registered handle 7107856c818Smatthias.ringwald if (!conn){ 7119da54300S[email protected] log_error( "hci.c: acl_handler called with non-registered handle %u!" , con_handle); 7127856c818Smatthias.ringwald return; 7137856c818Smatthias.ringwald } 7147856c818Smatthias.ringwald 715e76a89eeS[email protected] // assert packet is complete 7169ecc3e17S[email protected] if (acl_length + 4 != size){ 717f04a0c31SMatthias Ringwald log_error("hci.c: acl_handler called with ACL packet of wrong size %d, expected %u => dropping packet", size, acl_length + 4); 718e76a89eeS[email protected] return; 719e76a89eeS[email protected] } 720e76a89eeS[email protected] 72152db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 7227856c818Smatthias.ringwald // update idle timestamp 7237856c818Smatthias.ringwald hci_connection_timestamp(conn); 72452db98b2SMatthias Ringwald #endif 7257856c818Smatthias.ringwald 7262b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 7272b838201SMatthias Ringwald hci_stack->host_completed_packets = 1; 7282b838201SMatthias Ringwald conn->num_packets_completed++; 7292b838201SMatthias Ringwald #endif 7302b838201SMatthias Ringwald 7317856c818Smatthias.ringwald // handle different packet types 7327856c818Smatthias.ringwald switch (acl_flags & 0x03) { 7337856c818Smatthias.ringwald 7347856c818Smatthias.ringwald case 0x01: // continuation fragment 7357856c818Smatthias.ringwald 7360ca847afS[email protected] // sanity checks 7377856c818Smatthias.ringwald if (conn->acl_recombination_pos == 0) { 7389da54300S[email protected] log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x", con_handle); 7397856c818Smatthias.ringwald return; 7407856c818Smatthias.ringwald } 7410ca847afS[email protected] if (conn->acl_recombination_pos + acl_length > 4 + HCI_ACL_BUFFER_SIZE){ 7420ca847afS[email protected] log_error( "ACL Cont Fragment to large: combined packet %u > buffer size %u for handle 0x%02x", 7430ca847afS[email protected] conn->acl_recombination_pos + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle); 7440ca847afS[email protected] conn->acl_recombination_pos = 0; 7450ca847afS[email protected] return; 7460ca847afS[email protected] } 7477856c818Smatthias.ringwald 7487856c818Smatthias.ringwald // append fragment payload (header already stored) 749ec6321eeS[email protected] memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + conn->acl_recombination_pos], &packet[4], acl_length ); 7507856c818Smatthias.ringwald conn->acl_recombination_pos += acl_length; 7517856c818Smatthias.ringwald 7529da54300S[email protected] // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u", acl_length, 753decc01a8Smatthias.ringwald // conn->acl_recombination_pos, conn->acl_recombination_length); 7547856c818Smatthias.ringwald 7557856c818Smatthias.ringwald // forward complete L2CAP packet if complete. 7567856c818Smatthias.ringwald if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header 757d6b06661SMatthias Ringwald hci_emit_acl_packet(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], conn->acl_recombination_pos); 7587856c818Smatthias.ringwald // reset recombination buffer 7597856c818Smatthias.ringwald conn->acl_recombination_length = 0; 7607856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 7617856c818Smatthias.ringwald } 7627856c818Smatthias.ringwald break; 7637856c818Smatthias.ringwald 7647856c818Smatthias.ringwald case 0x02: { // first fragment 7657856c818Smatthias.ringwald 76623a77e1aS[email protected] // sanity check 76723a77e1aS[email protected] if (conn->acl_recombination_pos) { 76823a77e1aS[email protected] log_error( "ACL First Fragment but data in buffer for handle 0x%02x, dropping stale fragments", con_handle); 76923a77e1aS[email protected] conn->acl_recombination_pos = 0; 77023a77e1aS[email protected] } 77123a77e1aS[email protected] 7727856c818Smatthias.ringwald // peek into L2CAP packet! 7737856c818Smatthias.ringwald uint16_t l2cap_length = READ_L2CAP_LENGTH( packet ); 7747856c818Smatthias.ringwald 7759da54300S[email protected] // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u", acl_length, l2cap_length); 776decc01a8Smatthias.ringwald 7777856c818Smatthias.ringwald // compare fragment size to L2CAP packet size 7787856c818Smatthias.ringwald if (acl_length >= l2cap_length + 4){ 7797856c818Smatthias.ringwald // forward fragment as L2CAP packet 780d6b06661SMatthias Ringwald hci_emit_acl_packet(packet, acl_length + 4); 7817856c818Smatthias.ringwald } else { 7820ca847afS[email protected] 7830ca847afS[email protected] if (acl_length > HCI_ACL_BUFFER_SIZE){ 7840ca847afS[email protected] log_error( "ACL First Fragment to large: fragment %u > buffer size %u for handle 0x%02x", 7850ca847afS[email protected] 4 + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle); 7860ca847afS[email protected] return; 7870ca847afS[email protected] } 7880ca847afS[email protected] 7897856c818Smatthias.ringwald // store first fragment and tweak acl length for complete package 790ec6321eeS[email protected] memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], packet, acl_length + 4); 7917856c818Smatthias.ringwald conn->acl_recombination_pos = acl_length + 4; 7927856c818Smatthias.ringwald conn->acl_recombination_length = l2cap_length; 793f8fbdce0SMatthias Ringwald little_endian_store_16(conn->acl_recombination_buffer, HCI_INCOMING_PRE_BUFFER_SIZE + 2, l2cap_length +4); 7947856c818Smatthias.ringwald } 7957856c818Smatthias.ringwald break; 7967856c818Smatthias.ringwald 7977856c818Smatthias.ringwald } 7987856c818Smatthias.ringwald default: 7999da54300S[email protected] log_error( "hci.c: acl_handler called with invalid packet boundary flags %u", acl_flags & 0x03); 8007856c818Smatthias.ringwald return; 8017856c818Smatthias.ringwald } 80294ab26f8Smatthias.ringwald 80394ab26f8Smatthias.ringwald // execute main loop 80494ab26f8Smatthias.ringwald hci_run(); 80516833f0aSmatthias.ringwald } 80622909952Smatthias.ringwald 80767a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){ 8089da54300S[email protected] log_info("Connection closed: handle 0x%x, %s", conn->con_handle, bd_addr_to_str(conn->address)); 8093c4d4b90Smatthias.ringwald 810b3264428SMatthias Ringwald #ifdef ENABLE_CLASSIC 811ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 812ee752bb8SMatthias Ringwald int addr_type = conn->address_type; 813ee752bb8SMatthias Ringwald #endif 814b3264428SMatthias Ringwald #endif 815ee752bb8SMatthias Ringwald 816528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&conn->timeout); 817c785ef68Smatthias.ringwald 818665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 819a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 8203c4d4b90Smatthias.ringwald 8213c4d4b90Smatthias.ringwald // now it's gone 822c7e0c5f6Smatthias.ringwald hci_emit_nr_connections_changed(); 823ee752bb8SMatthias Ringwald 824b3264428SMatthias Ringwald #ifdef ENABLE_CLASSIC 825034e9b53SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 826ee752bb8SMatthias Ringwald // update SCO 827ee752bb8SMatthias Ringwald if (addr_type == BD_ADDR_TYPE_SCO && hci_stack->hci_transport && hci_stack->hci_transport->set_sco_config){ 828ee752bb8SMatthias Ringwald hci_stack->hci_transport->set_sco_config(hci_stack->sco_voice_setting_active, hci_number_sco_connections()); 829ee752bb8SMatthias Ringwald } 830034e9b53SMatthias Ringwald #endif 831b3264428SMatthias Ringwald #endif 832c7e0c5f6Smatthias.ringwald } 833c7e0c5f6Smatthias.ringwald 83435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 83535454696SMatthias Ringwald 8360c042179S[email protected] static const uint16_t packet_type_sizes[] = { 8378f8108aaSmatthias.ringwald 0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE, 8388f8108aaSmatthias.ringwald HCI_ACL_DH1_SIZE, 0, 0, 0, 8398f8108aaSmatthias.ringwald HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE, 8408f8108aaSmatthias.ringwald HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE 8418f8108aaSmatthias.ringwald }; 84265389bfcS[email protected] static const uint8_t packet_type_feature_requirement_bit[] = { 84365389bfcS[email protected] 0, // 3 slot packets 84465389bfcS[email protected] 1, // 5 slot packets 84565389bfcS[email protected] 25, // EDR 2 mpbs 84665389bfcS[email protected] 26, // EDR 3 mbps 84765389bfcS[email protected] 39, // 3 slot EDR packts 84865389bfcS[email protected] 40, // 5 slot EDR packet 84965389bfcS[email protected] }; 85065389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = { 85165389bfcS[email protected] 0x0f00, // 3 slot packets 85265389bfcS[email protected] 0xf000, // 5 slot packets 85365389bfcS[email protected] 0x1102, // EDR 2 mpbs 85465389bfcS[email protected] 0x2204, // EDR 3 mbps 85565389bfcS[email protected] 0x0300, // 3 slot EDR packts 85665389bfcS[email protected] 0x3000, // 5 slot EDR packet 85765389bfcS[email protected] }; 8588f8108aaSmatthias.ringwald 85965389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){ 86065389bfcS[email protected] // enable packet types based on size 8618f8108aaSmatthias.ringwald uint16_t packet_types = 0; 862f16a69bbS[email protected] unsigned int i; 8638f8108aaSmatthias.ringwald for (i=0;i<16;i++){ 8648f8108aaSmatthias.ringwald if (packet_type_sizes[i] == 0) continue; 8658f8108aaSmatthias.ringwald if (packet_type_sizes[i] <= buffer_size){ 8668f8108aaSmatthias.ringwald packet_types |= 1 << i; 8678f8108aaSmatthias.ringwald } 8688f8108aaSmatthias.ringwald } 86965389bfcS[email protected] // disable packet types due to missing local supported features 87065389bfcS[email protected] for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){ 871f04a0c31SMatthias Ringwald unsigned int bit_idx = packet_type_feature_requirement_bit[i]; 87265389bfcS[email protected] int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0; 87365389bfcS[email protected] if (feature_set) continue; 87465389bfcS[email protected] log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]); 87565389bfcS[email protected] packet_types &= ~packet_type_feature_packet_mask[i]; 87665389bfcS[email protected] } 8778f8108aaSmatthias.ringwald // flip bits for "may not be used" 8788f8108aaSmatthias.ringwald packet_types ^= 0x3306; 8798f8108aaSmatthias.ringwald return packet_types; 8808f8108aaSmatthias.ringwald } 8818f8108aaSmatthias.ringwald 8828f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){ 8833a9fb326S[email protected] return hci_stack->packet_types; 8848f8108aaSmatthias.ringwald } 88535454696SMatthias Ringwald #endif 8868f8108aaSmatthias.ringwald 887facf93fdS[email protected] uint8_t* hci_get_outgoing_packet_buffer(void){ 8887dc17943Smatthias.ringwald // hci packet buffer is >= acl data packet length 8893a9fb326S[email protected] return hci_stack->hci_packet_buffer; 8907dc17943Smatthias.ringwald } 8917dc17943Smatthias.ringwald 892f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){ 8933a9fb326S[email protected] return hci_stack->acl_data_packet_length; 8947dc17943Smatthias.ringwald } 8957dc17943Smatthias.ringwald 89606b9e820SMatthias Ringwald #ifdef ENABLE_CLASSIC 8973e68d23dSMatthias Ringwald int hci_extended_sco_link_supported(void){ 8983e68d23dSMatthias Ringwald // No. 31, byte 3, bit 7 8993e68d23dSMatthias Ringwald return (hci_stack->local_supported_features[3] & (1 << 7)) != 0; 9003e68d23dSMatthias Ringwald } 90106b9e820SMatthias Ringwald #endif 9023e68d23dSMatthias Ringwald 9036ac9a97eS[email protected] int hci_non_flushable_packet_boundary_flag_supported(void){ 9046ac9a97eS[email protected] // No. 54, byte 6, bit 6 9056ac9a97eS[email protected] return (hci_stack->local_supported_features[6] & (1 << 6)) != 0; 9066ac9a97eS[email protected] } 9076ac9a97eS[email protected] 90815a95bd5SMatthias Ringwald static int gap_ssp_supported(void){ 9096ac9a97eS[email protected] // No. 51, byte 6, bit 3 9103a9fb326S[email protected] return (hci_stack->local_supported_features[6] & (1 << 3)) != 0; 911f5d8d141S[email protected] } 912f5d8d141S[email protected] 9137f02f414SMatthias Ringwald static int hci_classic_supported(void){ 91435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 9156ac9a97eS[email protected] // No. 37, byte 4, bit 5, = No BR/EDR Support 9163a9fb326S[email protected] return (hci_stack->local_supported_features[4] & (1 << 5)) == 0; 91735454696SMatthias Ringwald #else 91835454696SMatthias Ringwald return 0; 91935454696SMatthias Ringwald #endif 920f5d8d141S[email protected] } 921f5d8d141S[email protected] 9227f02f414SMatthias Ringwald static int hci_le_supported(void){ 923a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 9246ac9a97eS[email protected] // No. 37, byte 4, bit 6 = LE Supported (Controller) 9253a9fb326S[email protected] return (hci_stack->local_supported_features[4] & (1 << 6)) != 0; 926f5d8d141S[email protected] #else 927f5d8d141S[email protected] return 0; 928f5d8d141S[email protected] #endif 929f5d8d141S[email protected] } 930f5d8d141S[email protected] 931b95a5a35SMatthias Ringwald #ifdef ENABLE_BLE 932b95a5a35SMatthias Ringwald 933b95a5a35SMatthias Ringwald /** 934b95a5a35SMatthias Ringwald * @brief Get addr type and address used for LE in Advertisements, Scan Responses, 935b95a5a35SMatthias Ringwald */ 936b95a5a35SMatthias Ringwald void gap_le_get_own_address(uint8_t * addr_type, bd_addr_t addr){ 937b95a5a35SMatthias Ringwald *addr_type = hci_stack->le_own_addr_type; 938b95a5a35SMatthias Ringwald if (hci_stack->le_own_addr_type){ 939b95a5a35SMatthias Ringwald memcpy(addr, hci_stack->le_random_address, 6); 94069a97523S[email protected] } else { 9413a9fb326S[email protected] memcpy(addr, hci_stack->local_bd_addr, 6); 94269a97523S[email protected] } 94369a97523S[email protected] } 94469a97523S[email protected] 945e8c8828eSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 94639677e66SMatthias Ringwald void le_handle_advertisement_report(uint8_t *packet, int size){ 9479ec2630cSMatthias Ringwald 9489ec2630cSMatthias Ringwald UNUSED(size); 9499ec2630cSMatthias Ringwald 950d1dc057bS[email protected] int offset = 3; 951d1dc057bS[email protected] int num_reports = packet[offset]; 952d1dc057bS[email protected] offset += 1; 953d1dc057bS[email protected] 95457c9da5bS[email protected] int i; 9554f4e0224SMatthias Ringwald // log_info("HCI: handle adv report with num reports: %d", num_reports); 95603fbe9c6S[email protected] uint8_t event[12 + LE_ADVERTISING_DATA_SIZE]; // use upper bound to avoid var size automatic var 95757c9da5bS[email protected] for (i=0; i<num_reports;i++){ 958210c6774S[email protected] uint8_t data_length = packet[offset + 8]; 959a0aac9c4S[email protected] uint8_t event_size = 10 + data_length; 960d1dc057bS[email protected] int pos = 0; 961045013feSMatthias Ringwald event[pos++] = GAP_EVENT_ADVERTISING_REPORT; 96257c9da5bS[email protected] event[pos++] = event_size; 963210c6774S[email protected] memcpy(&event[pos], &packet[offset], 1+1+6); // event type + address type + address 964d1dc057bS[email protected] offset += 8; 965d1dc057bS[email protected] pos += 8; 966d1dc057bS[email protected] event[pos++] = packet[offset + 1 + data_length]; // rssi 967d1dc057bS[email protected] event[pos++] = packet[offset++]; //data_length; 968d1dc057bS[email protected] memcpy(&event[pos], &packet[offset], data_length); 96957c9da5bS[email protected] pos += data_length; 970d1dc057bS[email protected] offset += data_length + 1; // rssi 971d6b06661SMatthias Ringwald hci_emit_event(event, pos, 1); 97257c9da5bS[email protected] } 97357c9da5bS[email protected] } 974b2f949feS[email protected] #endif 975e8c8828eSMatthias Ringwald #endif 97657c9da5bS[email protected] 977cd77dd38SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 97806b9e820SMatthias Ringwald 97996b53536SMatthias Ringwald static uint32_t hci_transport_uart_get_main_baud_rate(void){ 98096b53536SMatthias Ringwald if (!hci_stack->config) return 0; 9819796ebeaSMatthias Ringwald uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 98296b53536SMatthias Ringwald // Limit baud rate for Broadcom chipsets to 3 mbps 98361f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION && baud_rate > 3000000){ 98496b53536SMatthias Ringwald baud_rate = 3000000; 98596b53536SMatthias Ringwald } 98696b53536SMatthias Ringwald return baud_rate; 98796b53536SMatthias Ringwald } 98896b53536SMatthias Ringwald 989ec820d77SMatthias Ringwald static void hci_initialization_timeout_handler(btstack_timer_source_t * ds){ 9909ec2630cSMatthias Ringwald UNUSED(ds); 9919ec2630cSMatthias Ringwald 9920305bdeaSMatthias Ringwald switch (hci_stack->substate){ 9930305bdeaSMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 9947b0d7667SMatthias Ringwald log_info("Resend HCI Reset"); 9950305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET; 9967b0d7667SMatthias Ringwald hci_stack->num_cmd_packets = 1; 9970305bdeaSMatthias Ringwald hci_run(); 9980305bdeaSMatthias Ringwald break; 9999f007422SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET: 10009f007422SMatthias Ringwald log_info("Resend HCI Reset - CSR Warm Boot with Link Reset"); 10019f007422SMatthias Ringwald if (hci_stack->hci_transport->reset_link){ 10029f007422SMatthias Ringwald hci_stack->hci_transport->reset_link(); 10039f007422SMatthias Ringwald } 1004202c8a4cSMatthias Ringwald // no break - explicit fallthrough to HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT 1005e47e68c7SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT: 1006e47e68c7SMatthias Ringwald log_info("Resend HCI Reset - CSR Warm Boot"); 1007e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT; 1008e47e68c7SMatthias Ringwald hci_stack->num_cmd_packets = 1; 1009e47e68c7SMatthias Ringwald hci_run(); 1010688c2635SMatthias Ringwald break; 10117224be7eSMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE: 10127224be7eSMatthias Ringwald if (hci_stack->hci_transport->set_baudrate){ 101396b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1014cd724cb7SMatthias Ringwald log_info("Local baud rate change to %"PRIu32"(timeout handler)", baud_rate); 10157dd9d0ecSMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 10167224be7eSMatthias Ringwald } 1017834bce8cSMatthias Ringwald // For CSR, HCI Reset is sent on new baud rate. Don't forget to reset link for H5/BCSP 101861f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO){ 1019834bce8cSMatthias Ringwald if (hci_stack->hci_transport->reset_link){ 1020834bce8cSMatthias Ringwald log_info("Link Reset"); 1021834bce8cSMatthias Ringwald hci_stack->hci_transport->reset_link(); 1022834bce8cSMatthias Ringwald } 1023772a36d3SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT; 1024772a36d3SMatthias Ringwald hci_run(); 1025772a36d3SMatthias Ringwald } 10264696bddbSMatthias Ringwald break; 1027559961d0SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_BCM_DELAY: 1028559961d0SMatthias Ringwald // otherwise continue 1029559961d0SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 1030559961d0SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 1031559961d0SMatthias Ringwald break; 10320305bdeaSMatthias Ringwald default: 10330305bdeaSMatthias Ringwald break; 10340305bdeaSMatthias Ringwald } 10350305bdeaSMatthias Ringwald } 103606b9e820SMatthias Ringwald #endif 10370305bdeaSMatthias Ringwald 103871de195eSMatthias Ringwald static void hci_initializing_next_state(void){ 103974b323a9SMatthias Ringwald hci_stack->substate = (hci_substate_t )( ((int) hci_stack->substate) + 1); 104074b323a9SMatthias Ringwald } 104174b323a9SMatthias Ringwald 104274b323a9SMatthias Ringwald // assumption: hci_can_send_command_packet_now() == true 104371de195eSMatthias Ringwald static void hci_initializing_run(void){ 1044148ca237SMatthias Ringwald log_debug("hci_initializing_run: substate %u, can send %u", hci_stack->substate, hci_can_send_command_packet_now()); 104574b323a9SMatthias Ringwald switch (hci_stack->substate){ 104674b323a9SMatthias Ringwald case HCI_INIT_SEND_RESET: 104774b323a9SMatthias Ringwald hci_state_reset(); 1048a0cf2f3fSMatthias Ringwald 104906b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 10500305bdeaSMatthias Ringwald // prepare reset if command complete not received in 100ms 1051659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1052528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1053528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1054a0cf2f3fSMatthias Ringwald #endif 10550305bdeaSMatthias Ringwald // send command 105674b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 10570305bdeaSMatthias Ringwald hci_send_cmd(&hci_reset); 105874b323a9SMatthias Ringwald break; 105976fcb19bSMatthias Ringwald case HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION: 106076fcb19bSMatthias Ringwald hci_send_cmd(&hci_read_local_version_information); 106176fcb19bSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION; 106276fcb19bSMatthias Ringwald break; 1063e90bae01SMatthias Ringwald case HCI_INIT_SEND_READ_LOCAL_NAME: 1064e90bae01SMatthias Ringwald hci_send_cmd(&hci_read_local_name); 1065e90bae01SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_NAME; 1066e90bae01SMatthias Ringwald break; 106706b9e820SMatthias Ringwald 106806b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 1069e47e68c7SMatthias Ringwald case HCI_INIT_SEND_RESET_CSR_WARM_BOOT: 1070e47e68c7SMatthias Ringwald hci_state_reset(); 1071e47e68c7SMatthias Ringwald // prepare reset if command complete not received in 100ms 1072659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1073528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1074528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1075e47e68c7SMatthias Ringwald // send command 1076e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT; 1077e47e68c7SMatthias Ringwald hci_send_cmd(&hci_reset); 1078e47e68c7SMatthias Ringwald break; 10798d29070eSMatthias Ringwald case HCI_INIT_SEND_RESET_ST_WARM_BOOT: 10808d29070eSMatthias Ringwald hci_state_reset(); 10818d29070eSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT; 10828d29070eSMatthias Ringwald hci_send_cmd(&hci_reset); 10838d29070eSMatthias Ringwald break; 1084fab26ab3SMatthias Ringwald case HCI_INIT_SEND_BAUD_CHANGE: { 108596b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 10863fb36a29SMatthias Ringwald hci_stack->chipset->set_baudrate_command(baud_rate, hci_stack->hci_packet_buffer); 1087f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 108874b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE; 10890305bdeaSMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]); 10904696bddbSMatthias Ringwald // STLC25000D: baudrate change happens within 0.5 s after command was send, 10914696bddbSMatthias Ringwald // use timer to update baud rate after 100 ms (knowing exactly, when command was sent is non-trivial) 109261f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS){ 1093659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1094528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 10954696bddbSMatthias Ringwald } 109674b323a9SMatthias Ringwald break; 1097fab26ab3SMatthias Ringwald } 1098fab26ab3SMatthias Ringwald case HCI_INIT_SEND_BAUD_CHANGE_BCM: { 109996b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 11003fb36a29SMatthias Ringwald hci_stack->chipset->set_baudrate_command(baud_rate, hci_stack->hci_packet_buffer); 1101f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 1102eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE_BCM; 1103eb3a5314SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]); 1104eb3a5314SMatthias Ringwald break; 1105fab26ab3SMatthias Ringwald } 110674b323a9SMatthias Ringwald case HCI_INIT_CUSTOM_INIT: 110774b323a9SMatthias Ringwald // Custom initialization 11083fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->next_command){ 11093fb36a29SMatthias Ringwald int valid_cmd = (*hci_stack->chipset->next_command)(hci_stack->hci_packet_buffer); 111074b323a9SMatthias Ringwald if (valid_cmd){ 111174b323a9SMatthias Ringwald int size = 3 + hci_stack->hci_packet_buffer[2]; 1112f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 111374b323a9SMatthias Ringwald hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, hci_stack->hci_packet_buffer, size); 1114e47e68c7SMatthias Ringwald switch (valid_cmd) { 1115e47e68c7SMatthias Ringwald case 1: 1116e47e68c7SMatthias Ringwald default: 1117e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT; 1118e47e68c7SMatthias Ringwald break; 1119e47e68c7SMatthias Ringwald case 2: // CSR Warm Boot: Wait a bit, then send HCI Reset until HCI Command Complete 1120e47e68c7SMatthias Ringwald log_info("CSR Warm Boot"); 1121659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1122528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1123528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 112461f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO 1125772a36d3SMatthias Ringwald && hci_stack->config 11263fb36a29SMatthias Ringwald && hci_stack->chipset 11273fb36a29SMatthias Ringwald // && hci_stack->chipset->set_baudrate_command -- there's no such command 1128772a36d3SMatthias Ringwald && hci_stack->hci_transport->set_baudrate 11292caefae9SMatthias Ringwald && hci_transport_uart_get_main_baud_rate()){ 1130772a36d3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE; 1131772a36d3SMatthias Ringwald } else { 11329f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET; 1133772a36d3SMatthias Ringwald } 1134e47e68c7SMatthias Ringwald break; 1135e47e68c7SMatthias Ringwald } 11360305bdeaSMatthias Ringwald hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size); 113774b323a9SMatthias Ringwald break; 113874b323a9SMatthias Ringwald } 1139148ca237SMatthias Ringwald log_info("Init script done"); 114092a0d36dSMatthias Ringwald 1141559961d0SMatthias Ringwald // Init script download on Broadcom chipsets causes: 114261f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION){ 1143559961d0SMatthias Ringwald // - baud rate to reset, restore UART baud rate if needed 114492a0d36dSMatthias Ringwald int need_baud_change = hci_stack->config 11453fb36a29SMatthias Ringwald && hci_stack->chipset 11463fb36a29SMatthias Ringwald && hci_stack->chipset->set_baudrate_command 114792a0d36dSMatthias Ringwald && hci_stack->hci_transport->set_baudrate 11489796ebeaSMatthias Ringwald && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 114992a0d36dSMatthias Ringwald if (need_baud_change) { 11509796ebeaSMatthias Ringwald uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_init; 1151cd724cb7SMatthias Ringwald log_info("Local baud rate change to %"PRIu32" after init script (bcm)", baud_rate); 115292a0d36dSMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 115392a0d36dSMatthias Ringwald } 1154559961d0SMatthias Ringwald 1155559961d0SMatthias Ringwald // - RTS will raise during update, but manual RTS/CTS in WICED port on RedBear Duo cannot handle this 1156559961d0SMatthias Ringwald // -> Work around: wait a few milliseconds here. 1157559961d0SMatthias Ringwald log_info("BCM delay after init script"); 1158559961d0SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_BCM_DELAY; 1159559961d0SMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, 10); 1160559961d0SMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1161559961d0SMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1162559961d0SMatthias Ringwald break; 116392a0d36dSMatthias Ringwald } 116474b323a9SMatthias Ringwald } 116574b323a9SMatthias Ringwald // otherwise continue 1166a828a756SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 1167a828a756SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 1168a828a756SMatthias Ringwald break; 116953860077SMatthias Ringwald case HCI_INIT_SET_BD_ADDR: 117053860077SMatthias Ringwald log_info("Set Public BD ADDR to %s", bd_addr_to_str(hci_stack->custom_bd_addr)); 11713fb36a29SMatthias Ringwald hci_stack->chipset->set_bd_addr_command(hci_stack->custom_bd_addr, hci_stack->hci_packet_buffer); 1172f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 117353860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_BD_ADDR; 117453860077SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]); 117553860077SMatthias Ringwald break; 117606b9e820SMatthias Ringwald #endif 117706b9e820SMatthias Ringwald 117806b9e820SMatthias Ringwald case HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS: 117906b9e820SMatthias Ringwald log_info("Resend hci_read_local_supported_commands after CSR Warm Boot double reset"); 118006b9e820SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 118106b9e820SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 118206b9e820SMatthias Ringwald break; 118353860077SMatthias Ringwald case HCI_INIT_READ_BD_ADDR: 118453860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_BD_ADDR; 118553860077SMatthias Ringwald hci_send_cmd(&hci_read_bd_addr); 118653860077SMatthias Ringwald break; 118774b323a9SMatthias Ringwald case HCI_INIT_READ_BUFFER_SIZE: 118874b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_BUFFER_SIZE; 11890305bdeaSMatthias Ringwald hci_send_cmd(&hci_read_buffer_size); 119074b323a9SMatthias Ringwald break; 119153860077SMatthias Ringwald case HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES: 119253860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_FEATURES; 11930305bdeaSMatthias Ringwald hci_send_cmd(&hci_read_local_supported_features); 119474b323a9SMatthias Ringwald break; 11952b838201SMatthias Ringwald 11962b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 11972b838201SMatthias Ringwald case HCI_INIT_SET_CONTROLLER_TO_HOST_FLOW_CONTROL: 11982b838201SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_CONTROLLER_TO_HOST_FLOW_CONTROL; 11992b838201SMatthias Ringwald hci_send_cmd(&hci_set_controller_to_host_flow_control, 3); // ACL + SCO Flow Control 12002b838201SMatthias Ringwald break; 12012b838201SMatthias Ringwald case HCI_INIT_HOST_BUFFER_SIZE: 12022b838201SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_HOST_BUFFER_SIZE; 12032b838201SMatthias Ringwald hci_send_cmd(&hci_host_buffer_size, HCI_HOST_ACL_PACKET_LEN, HCI_HOST_SCO_PACKET_LEN, 12042b838201SMatthias Ringwald HCI_HOST_ACL_PACKET_NUM, HCI_HOST_SCO_PACKET_NUM); 12052b838201SMatthias Ringwald break; 12062b838201SMatthias Ringwald #endif 12072b838201SMatthias Ringwald 120874b323a9SMatthias Ringwald case HCI_INIT_SET_EVENT_MASK: 12090305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_EVENT_MASK; 121074b323a9SMatthias Ringwald if (hci_le_supported()){ 121174b323a9SMatthias Ringwald hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF); 121274b323a9SMatthias Ringwald } else { 121374b323a9SMatthias Ringwald // Kensington Bluetooth 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff... 121474b323a9SMatthias Ringwald hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF); 121574b323a9SMatthias Ringwald } 121674b323a9SMatthias Ringwald break; 12172b838201SMatthias Ringwald 121835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 121974b323a9SMatthias Ringwald case HCI_INIT_WRITE_SIMPLE_PAIRING_MODE: 122074b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SIMPLE_PAIRING_MODE; 12210305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable); 122274b323a9SMatthias Ringwald break; 122374b323a9SMatthias Ringwald case HCI_INIT_WRITE_PAGE_TIMEOUT: 122474b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_PAGE_TIMEOUT; 12250305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_page_timeout, 0x6000); // ca. 15 sec 122674b323a9SMatthias Ringwald break; 122774b323a9SMatthias Ringwald case HCI_INIT_WRITE_CLASS_OF_DEVICE: 122874b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_CLASS_OF_DEVICE; 12290305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device); 123074b323a9SMatthias Ringwald break; 123174b323a9SMatthias Ringwald case HCI_INIT_WRITE_LOCAL_NAME: 12320305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_LOCAL_NAME; 123374b323a9SMatthias Ringwald if (hci_stack->local_name){ 123474b323a9SMatthias Ringwald hci_send_cmd(&hci_write_local_name, hci_stack->local_name); 123574b323a9SMatthias Ringwald } else { 12367224be7eSMatthias Ringwald char local_name[8+17+1]; 12377224be7eSMatthias Ringwald // BTstack 11:22:33:44:55:66 12387224be7eSMatthias Ringwald memcpy(local_name, "BTstack ", 8); 12397224be7eSMatthias Ringwald memcpy(&local_name[8], bd_addr_to_str(hci_stack->local_bd_addr), 17); // strlen(bd_addr_to_str(...)) = 17 12407224be7eSMatthias Ringwald local_name[8+17] = '\0'; 124193bcd4d0SMatthias Ringwald log_info("---> Name %s", local_name); 124293bcd4d0SMatthias Ringwald hci_send_cmd(&hci_write_local_name, local_name); 124374b323a9SMatthias Ringwald } 124474b323a9SMatthias Ringwald break; 1245ff00ed1cSMatthias Ringwald case HCI_INIT_WRITE_EIR_DATA: 12468a114470SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_EIR_DATA; 1247ff00ed1cSMatthias Ringwald hci_send_cmd(&hci_write_extended_inquiry_response, 0, hci_stack->eir_data); 1248ff00ed1cSMatthias Ringwald break; 1249f6858d14SMatthias Ringwald case HCI_INIT_WRITE_INQUIRY_MODE: 1250f6858d14SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_INQUIRY_MODE; 12518a114470SMatthias Ringwald hci_send_cmd(&hci_write_inquiry_mode, (int) hci_stack->inquiry_mode); 1252f6858d14SMatthias Ringwald break; 125374b323a9SMatthias Ringwald case HCI_INIT_WRITE_SCAN_ENABLE: 125474b323a9SMatthias Ringwald hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan 125574b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SCAN_ENABLE; 125674b323a9SMatthias Ringwald break; 1257483c5078SMatthias Ringwald // only sent if ENABLE_SCO_OVER_HCI is defined 1258729ed62eSMatthias Ringwald case HCI_INIT_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE: 1259729ed62eSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE; 1260729ed62eSMatthias Ringwald hci_send_cmd(&hci_write_synchronous_flow_control_enable, 1); // SCO tracking enabled 1261729ed62eSMatthias Ringwald break; 1262483c5078SMatthias Ringwald case HCI_INIT_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING: 1263483c5078SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING; 1264483c5078SMatthias Ringwald hci_send_cmd(&hci_write_default_erroneous_data_reporting, 1); 1265483c5078SMatthias Ringwald break; 1266a42798c3SMatthias Ringwald // only sent if ENABLE_SCO_OVER_HCI and manufacturer is Broadcom 1267a42798c3SMatthias Ringwald case HCI_INIT_BCM_WRITE_SCO_PCM_INT: 1268a42798c3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT; 1269a42798c3SMatthias Ringwald log_info("BCM: Route SCO data via HCI transport"); 1270a42798c3SMatthias Ringwald hci_send_cmd(&hci_bcm_write_sco_pcm_int, 1, 0, 0, 0, 0); 1271a42798c3SMatthias Ringwald break; 1272a42798c3SMatthias Ringwald 127335454696SMatthias Ringwald #endif 1274a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 127574b323a9SMatthias Ringwald // LE INIT 127674b323a9SMatthias Ringwald case HCI_INIT_LE_READ_BUFFER_SIZE: 127774b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_READ_BUFFER_SIZE; 12780305bdeaSMatthias Ringwald hci_send_cmd(&hci_le_read_buffer_size); 127974b323a9SMatthias Ringwald break; 128074b323a9SMatthias Ringwald case HCI_INIT_WRITE_LE_HOST_SUPPORTED: 128174b323a9SMatthias Ringwald // LE Supported Host = 1, Simultaneous Host = 0 128274b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_LE_HOST_SUPPORTED; 12830305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_le_host_supported, 1, 0); 128474b323a9SMatthias Ringwald break; 1285b95a5a35SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 12863b6d4121SMatthias Ringwald case HCI_INIT_READ_WHITE_LIST_SIZE: 12873b6d4121SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_WHITE_LIST_SIZE; 12883b6d4121SMatthias Ringwald hci_send_cmd(&hci_le_read_white_list_size); 12893b6d4121SMatthias Ringwald break; 129074b323a9SMatthias Ringwald case HCI_INIT_LE_SET_SCAN_PARAMETERS: 1291b95a5a35SMatthias Ringwald // LE Scan Parameters: active scanning, 300 ms interval, 30 ms window, own address type, accept all advs 129274b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_SET_SCAN_PARAMETERS; 1293b95a5a35SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_parameters, 1, 0x1e0, 0x30, hci_stack->le_own_addr_type, 0); 129474b323a9SMatthias Ringwald break; 129574b323a9SMatthias Ringwald #endif 1296b95a5a35SMatthias Ringwald #endif 129774b323a9SMatthias Ringwald default: 129874b323a9SMatthias Ringwald return; 129974b323a9SMatthias Ringwald } 130055975f88SMatthias Ringwald } 130155975f88SMatthias Ringwald 1302a650ba4dSMatthias Ringwald static void hci_init_done(void){ 1303a650ba4dSMatthias Ringwald // done. tell the app 1304a650ba4dSMatthias Ringwald log_info("hci_init_done -> HCI_STATE_WORKING"); 1305a650ba4dSMatthias Ringwald hci_stack->state = HCI_STATE_WORKING; 1306a650ba4dSMatthias Ringwald hci_emit_state(); 1307a650ba4dSMatthias Ringwald hci_run(); 1308a650ba4dSMatthias Ringwald } 1309a650ba4dSMatthias Ringwald 13106155b3d3S[email protected] static void hci_initializing_event_handler(uint8_t * packet, uint16_t size){ 13119ec2630cSMatthias Ringwald UNUSED(size); 13129ec2630cSMatthias Ringwald 1313a2481739S[email protected] uint8_t command_completed = 0; 1314c4633093SMatthias Ringwald 13150e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE){ 1316f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 13176155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 1318a2481739S[email protected] command_completed = 1; 1319148ca237SMatthias Ringwald log_debug("Command complete for expected opcode %04x at substate %u", opcode, hci_stack->substate); 13206155b3d3S[email protected] } else { 1321d58dd308SMatthias Ringwald log_info("Command complete for different opcode %04x, expected %04x, at substate %u", opcode, hci_stack->last_cmd_opcode, hci_stack->substate); 13226155b3d3S[email protected] } 13236155b3d3S[email protected] } 13240f97eae7SMatthias Ringwald 13250e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_STATUS){ 13266155b3d3S[email protected] uint8_t status = packet[2]; 1327f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,4); 13286155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 13296155b3d3S[email protected] if (status){ 1330a2481739S[email protected] command_completed = 1; 1331148ca237SMatthias Ringwald log_debug("Command status error 0x%02x for expected opcode %04x at substate %u", status, opcode, hci_stack->substate); 13326155b3d3S[email protected] } else { 13336155b3d3S[email protected] log_info("Command status OK for expected opcode %04x, waiting for command complete", opcode); 13346155b3d3S[email protected] } 13356155b3d3S[email protected] } else { 1336148ca237SMatthias Ringwald log_debug("Command status for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode); 13376155b3d3S[email protected] } 13386155b3d3S[email protected] } 13390f97eae7SMatthias Ringwald 134006b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 134106b9e820SMatthias Ringwald 1342e47e68c7SMatthias Ringwald // Vendor == CSR 13430e2df43fSMatthias Ringwald if (hci_stack->substate == HCI_INIT_W4_CUSTOM_INIT && hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC){ 1344e47e68c7SMatthias Ringwald // TODO: track actual command 1345e47e68c7SMatthias Ringwald command_completed = 1; 1346e47e68c7SMatthias Ringwald } 1347a2481739S[email protected] 13484e9daa6fSMatthias Ringwald // Vendor == Toshiba 13490e2df43fSMatthias Ringwald if (hci_stack->substate == HCI_INIT_W4_SEND_BAUD_CHANGE && hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC){ 13504e9daa6fSMatthias Ringwald // TODO: track actual command 13514e9daa6fSMatthias Ringwald command_completed = 1; 13524e9daa6fSMatthias Ringwald } 13534e9daa6fSMatthias Ringwald 13540f97eae7SMatthias Ringwald // Late response (> 100 ms) for HCI Reset e.g. on Toshiba TC35661: 13550f97eae7SMatthias Ringwald // Command complete for HCI Reset arrives after we've resent the HCI Reset command 13560f97eae7SMatthias Ringwald // 13570f97eae7SMatthias Ringwald // HCI Reset 13580f97eae7SMatthias Ringwald // Timeout 100 ms 13590f97eae7SMatthias Ringwald // HCI Reset 13600f97eae7SMatthias Ringwald // Command Complete Reset 13610f97eae7SMatthias Ringwald // HCI Read Local Version Information 13620f97eae7SMatthias Ringwald // Command Complete Reset - but we expected Command Complete Read Local Version Information 13630f97eae7SMatthias Ringwald // hang... 13640f97eae7SMatthias Ringwald // 13650f97eae7SMatthias Ringwald // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend 13660f97eae7SMatthias Ringwald if (!command_completed 13670e2df43fSMatthias Ringwald && hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE 13680f97eae7SMatthias Ringwald && hci_stack->substate == HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION){ 13690f97eae7SMatthias Ringwald 1370f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 13710f97eae7SMatthias Ringwald if (opcode == hci_reset.opcode){ 13720f97eae7SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION; 13730f97eae7SMatthias Ringwald return; 13740f97eae7SMatthias Ringwald } 13750f97eae7SMatthias Ringwald } 13760f97eae7SMatthias Ringwald 13779f007422SMatthias Ringwald // CSR & H5 13789f007422SMatthias Ringwald // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend 13799f007422SMatthias Ringwald if (!command_completed 13809f007422SMatthias Ringwald && hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE 13819f007422SMatthias Ringwald && hci_stack->substate == HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS){ 13829f007422SMatthias Ringwald 13839f007422SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 13849f007422SMatthias Ringwald if (opcode == hci_reset.opcode){ 13859f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS; 13869f007422SMatthias Ringwald return; 13879f007422SMatthias Ringwald } 13889f007422SMatthias Ringwald } 13899f007422SMatthias Ringwald 13909f007422SMatthias 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 13919f007422SMatthias Ringwald // fix: Correct substate and behave as command below 13929f007422SMatthias Ringwald if (command_completed){ 13939f007422SMatthias Ringwald switch (hci_stack->substate){ 13949f007422SMatthias Ringwald case HCI_INIT_SEND_RESET: 13959f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 13969f007422SMatthias Ringwald break; 13979f007422SMatthias Ringwald case HCI_INIT_SEND_RESET_CSR_WARM_BOOT: 13989f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT; 13999f007422SMatthias Ringwald break; 14009f007422SMatthias Ringwald default: 14019f007422SMatthias Ringwald break; 14029f007422SMatthias Ringwald } 14039f007422SMatthias Ringwald } 14040f97eae7SMatthias Ringwald 140506b9e820SMatthias Ringwald #endif 14060f97eae7SMatthias Ringwald 1407a2481739S[email protected] if (!command_completed) return; 1408a2481739S[email protected] 140906b9e820SMatthias Ringwald int need_baud_change = 0; 141006b9e820SMatthias Ringwald int need_addr_change = 0; 141106b9e820SMatthias Ringwald 141206b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 141306b9e820SMatthias Ringwald need_baud_change = hci_stack->config 14143fb36a29SMatthias Ringwald && hci_stack->chipset 14153fb36a29SMatthias Ringwald && hci_stack->chipset->set_baudrate_command 1416db8bc6ffSMatthias Ringwald && hci_stack->hci_transport->set_baudrate 14179796ebeaSMatthias Ringwald && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 1418db8bc6ffSMatthias Ringwald 141906b9e820SMatthias Ringwald need_addr_change = hci_stack->custom_bd_addr_set 14203fb36a29SMatthias Ringwald && hci_stack->chipset 14213fb36a29SMatthias Ringwald && hci_stack->chipset->set_bd_addr_command; 142206b9e820SMatthias Ringwald #endif 1423a80162e9SMatthias Ringwald 14245c363727SMatthias Ringwald switch(hci_stack->substate){ 142506b9e820SMatthias Ringwald 142606b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 14279f007422SMatthias Ringwald case HCI_INIT_SEND_RESET: 1428d58dd308SMatthias Ringwald // on CSR with BCSP/H5, resend triggers resend of HCI Reset and leads to substate == HCI_INIT_SEND_RESET 14299f007422SMatthias Ringwald // fix: just correct substate and behave as command below 14309f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 14319f007422SMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 14329f007422SMatthias Ringwald break; 143374b323a9SMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 1434528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 143576fcb19bSMatthias Ringwald break; 1436e90bae01SMatthias Ringwald case HCI_INIT_W4_SEND_READ_LOCAL_NAME: 1437e90bae01SMatthias Ringwald log_info("Received local name, need baud change %d", need_baud_change); 14382f48d920SMatthias Ringwald if (need_baud_change){ 14392f48d920SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE; 14402f48d920SMatthias Ringwald return; 14412f48d920SMatthias Ringwald } 144253860077SMatthias Ringwald // skip baud change 144374b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 144474b323a9SMatthias Ringwald return; 1445a80162e9SMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE: 14464696bddbSMatthias Ringwald // for STLC2500D, baud rate change already happened. 1447fab26ab3SMatthias Ringwald // for others, baud rate gets changed now 144861f37892SMatthias Ringwald if ((hci_stack->manufacturer != BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS) && need_baud_change){ 144996b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1450cd724cb7SMatthias Ringwald log_info("Local baud rate change to %"PRIu32"(w4_send_baud_change)", baud_rate); 1451fab26ab3SMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 14524696bddbSMatthias Ringwald } 145374b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 145474b323a9SMatthias Ringwald return; 1455a80162e9SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT: 1456528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 1457a80162e9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 1458a80162e9SMatthias Ringwald return; 145974b323a9SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT: 146074b323a9SMatthias Ringwald // repeat custom init 146174b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 146274b323a9SMatthias Ringwald return; 146306b9e820SMatthias Ringwald #else 146406b9e820SMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 146506b9e820SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS; 146606b9e820SMatthias Ringwald return ; 146706b9e820SMatthias Ringwald #endif 146806b9e820SMatthias Ringwald 1469a828a756SMatthias Ringwald case HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS: 147061f37892SMatthias Ringwald if (need_baud_change && hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION){ 1471eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE_BCM; 1472eb3a5314SMatthias Ringwald return; 1473eb3a5314SMatthias Ringwald } 147453860077SMatthias Ringwald if (need_addr_change){ 147553860077SMatthias Ringwald hci_stack->substate = HCI_INIT_SET_BD_ADDR; 147653860077SMatthias Ringwald return; 147753860077SMatthias Ringwald } 147853860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 147953860077SMatthias Ringwald return; 148006b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 14817224be7eSMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE_BCM: 14827224be7eSMatthias Ringwald if (need_baud_change){ 148396b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1484cd724cb7SMatthias Ringwald log_info("Local baud rate change to %"PRIu32"(w4_send_baud_change_bcm))", baud_rate); 1485fab26ab3SMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 14867224be7eSMatthias Ringwald } 1487eb3a5314SMatthias Ringwald if (need_addr_change){ 1488eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_SET_BD_ADDR; 1489eb3a5314SMatthias Ringwald return; 1490eb3a5314SMatthias Ringwald } 1491eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 1492eb3a5314SMatthias Ringwald return; 149353860077SMatthias Ringwald case HCI_INIT_W4_SET_BD_ADDR: 149453860077SMatthias Ringwald // for STLC2500D, bd addr change only gets active after sending reset command 149561f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS){ 149653860077SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_ST_WARM_BOOT; 149753860077SMatthias Ringwald return; 149853860077SMatthias Ringwald } 149953860077SMatthias Ringwald // skipping st warm boot 150053860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 150153860077SMatthias Ringwald return; 150253860077SMatthias Ringwald case HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT: 150353860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 150453860077SMatthias Ringwald return; 150506b9e820SMatthias Ringwald #endif 150653860077SMatthias Ringwald case HCI_INIT_W4_READ_BD_ADDR: 150753860077SMatthias Ringwald // only read buffer size if supported 150853860077SMatthias Ringwald if (hci_stack->local_supported_commands[0] & 0x01) { 150953860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BUFFER_SIZE; 151053860077SMatthias Ringwald return; 151153860077SMatthias Ringwald } 151253860077SMatthias Ringwald // skipping read buffer size 151353860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES; 1514a828a756SMatthias Ringwald return; 151574b323a9SMatthias Ringwald case HCI_INIT_W4_SET_EVENT_MASK: 15166155b3d3S[email protected] // skip Classic init commands for LE only chipsets 15176155b3d3S[email protected] if (!hci_classic_supported()){ 15182c68f164SMatthias Ringwald #ifdef ENABLE_BLE 15196155b3d3S[email protected] if (hci_le_supported()){ 152074b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_LE_READ_BUFFER_SIZE; // skip all classic command 152174b323a9SMatthias Ringwald return; 15222c68f164SMatthias Ringwald } 15232c68f164SMatthias Ringwald #endif 15246155b3d3S[email protected] log_error("Neither BR/EDR nor LE supported"); 1525a650ba4dSMatthias Ringwald hci_init_done(); 152674b323a9SMatthias Ringwald return; 15276155b3d3S[email protected] } 152815a95bd5SMatthias Ringwald if (!gap_ssp_supported()){ 15295c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_WRITE_PAGE_TIMEOUT; 153074b323a9SMatthias Ringwald return; 15316155b3d3S[email protected] } 15326155b3d3S[email protected] break; 1533903ea03aSMatthias Ringwald #ifdef ENABLE_BLE 1534a828a756SMatthias Ringwald case HCI_INIT_W4_LE_READ_BUFFER_SIZE: 1535a828a756SMatthias Ringwald // skip write le host if not supported (e.g. on LE only EM9301) 1536a828a756SMatthias Ringwald if (hci_stack->local_supported_commands[0] & 0x02) break; 1537903ea03aSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 1538903ea03aSMatthias Ringwald hci_stack->substate = HCI_INIT_READ_WHITE_LIST_SIZE; 1539903ea03aSMatthias Ringwald #else 1540903ea03aSMatthias Ringwald hci_init_done(); 1541903ea03aSMatthias Ringwald #endif 1542a828a756SMatthias Ringwald return; 1543903ea03aSMatthias Ringwald #endif 1544ff00ed1cSMatthias Ringwald case HCI_INIT_W4_WRITE_LOCAL_NAME: 1545ff00ed1cSMatthias Ringwald // skip write eir data if no eir data set 1546ff00ed1cSMatthias Ringwald if (hci_stack->eir_data) break; 1547ff00ed1cSMatthias Ringwald hci_stack->substate = HCI_INIT_WRITE_INQUIRY_MODE; 1548ff00ed1cSMatthias Ringwald return; 1549729ed62eSMatthias Ringwald 155071c4de7aSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 1551729ed62eSMatthias Ringwald case HCI_INIT_W4_WRITE_SCAN_ENABLE: 15523905afbfSMatthias Ringwald // skip write synchronous flow control if not supported 15533905afbfSMatthias Ringwald if (hci_stack->local_supported_commands[0] & 0x04) break; 15543905afbfSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE; 15553905afbfSMatthias Ringwald // explicit fall through to reduce repetitions 15563905afbfSMatthias Ringwald 1557729ed62eSMatthias Ringwald case HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE: 15583905afbfSMatthias Ringwald // skip write default erroneous data reporting if not supported 15593905afbfSMatthias Ringwald if (hci_stack->local_supported_commands[0] & 0x08) break; 15603905afbfSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING; 15613905afbfSMatthias Ringwald // explicit fall through to reduce repetitions 15623905afbfSMatthias Ringwald 1563f064e0bbSMatthias Ringwald case HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING: 1564a42798c3SMatthias Ringwald // skip bcm set sco pcm config on non-Broadcom chipsets 156561f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION) break; 1566a42798c3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT; 1567a42798c3SMatthias Ringwald // explicit fall through to reduce repetitions 1568a42798c3SMatthias Ringwald 1569a42798c3SMatthias Ringwald case HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT: 1570729ed62eSMatthias Ringwald if (!hci_le_supported()){ 1571729ed62eSMatthias Ringwald // SKIP LE init for Classic only configuration 1572a650ba4dSMatthias Ringwald hci_init_done(); 1573729ed62eSMatthias Ringwald return; 1574729ed62eSMatthias Ringwald } 1575729ed62eSMatthias Ringwald break; 1576f3b012f9SMatthias Ringwald 1577f3b012f9SMatthias Ringwald #else /* !ENABLE_SCO_OVER_HCI */ 1578f3b012f9SMatthias Ringwald 157974b323a9SMatthias Ringwald case HCI_INIT_W4_WRITE_SCAN_ENABLE: 1580f3b012f9SMatthias Ringwald #ifdef ENABLE_BLE 1581f3b012f9SMatthias Ringwald if (hci_le_supported()){ 1582f3b012f9SMatthias Ringwald hci_stack->substate = HCI_INIT_LE_READ_BUFFER_SIZE; 158374b323a9SMatthias Ringwald return; 15846155b3d3S[email protected] } 1585729ed62eSMatthias Ringwald #endif 1586f3b012f9SMatthias Ringwald // SKIP LE init for Classic only configuration 1587f3b012f9SMatthias Ringwald hci_init_done(); 1588f3b012f9SMatthias Ringwald return; 1589f3b012f9SMatthias Ringwald #endif /* ENABLE_SCO_OVER_HCI */ 1590f3b012f9SMatthias Ringwald 1591a650ba4dSMatthias Ringwald // Response to command before init done state -> init done 1592a650ba4dSMatthias Ringwald case (HCI_INIT_DONE-1): 1593a650ba4dSMatthias Ringwald hci_init_done(); 1594a650ba4dSMatthias Ringwald return; 1595a650ba4dSMatthias Ringwald 15966155b3d3S[email protected] default: 159774b323a9SMatthias Ringwald break; 15986155b3d3S[email protected] } 159955975f88SMatthias Ringwald hci_initializing_next_state(); 16006155b3d3S[email protected] } 16016155b3d3S[email protected] 160216833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){ 1603e76a89eeS[email protected] 1604e76a89eeS[email protected] uint16_t event_length = packet[1]; 1605e76a89eeS[email protected] 1606e76a89eeS[email protected] // assert packet is complete 1607e76a89eeS[email protected] if (size != event_length + 2){ 1608f04a0c31SMatthias Ringwald log_error("hci.c: event_handler called with event packet of wrong size %d, expected %u => dropping packet", size, event_length + 2); 1609e76a89eeS[email protected] return; 1610e76a89eeS[email protected] } 1611e76a89eeS[email protected] 16121281a47eSmatthias.ringwald bd_addr_t addr; 161396a45072S[email protected] bd_addr_type_t addr_type; 1614fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 16151f7b95a1Smatthias.ringwald hci_connection_t * conn; 161656cf178bSmatthias.ringwald int i; 16170014e02cSMatthias Ringwald #ifdef ENABLE_CLASSIC 16180014e02cSMatthias Ringwald uint8_t link_type; 16190014e02cSMatthias Ringwald #endif 162035454696SMatthias Ringwald 16210e2df43fSMatthias Ringwald // log_info("HCI:EVENT:%02x", hci_event_packet_get_type(packet)); 16225909f7f2Smatthias.ringwald 16230e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 162422909952Smatthias.ringwald 16256772a24cSmatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 16266bef4003SMatthias Ringwald // get num cmd packets - limit to 1 to reduce complexity 16276bef4003SMatthias Ringwald hci_stack->num_cmd_packets = packet[2] ? 1 : 0; 16287ec5eeaaSmatthias.ringwald 16299e263bd7SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){ 163006b9e820SMatthias Ringwald if (packet[5]) break; 16319e263bd7SMatthias Ringwald // terminate, name 248 chars 16329e263bd7SMatthias Ringwald packet[6+248] = 0; 16339e263bd7SMatthias Ringwald log_info("local name: %s", &packet[6]); 16349e263bd7SMatthias Ringwald } 1635073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_buffer_size)){ 16361d279b20Smatthias.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" 1637429122ccSMatthias Ringwald if (hci_stack->state == HCI_STATE_INITIALIZING){ 1638429122ccSMatthias Ringwald uint16_t acl_len = little_endian_read_16(packet, 6); 1639429122ccSMatthias Ringwald uint16_t sco_len = packet[8]; 1640429122ccSMatthias Ringwald 1641429122ccSMatthias Ringwald // determine usable ACL/SCO payload size 1642429122ccSMatthias Ringwald hci_stack->acl_data_packet_length = btstack_min(acl_len, HCI_ACL_PAYLOAD_SIZE); 1643429122ccSMatthias Ringwald hci_stack->sco_data_packet_length = btstack_min(sco_len, HCI_ACL_PAYLOAD_SIZE); 1644429122ccSMatthias Ringwald 1645f8fbdce0SMatthias Ringwald hci_stack->acl_packets_total_num = little_endian_read_16(packet, 9); 1646f8fbdce0SMatthias Ringwald hci_stack->sco_packets_total_num = little_endian_read_16(packet, 11); 1647a8b12447S[email protected] 1648429122ccSMatthias Ringwald log_info("hci_read_buffer_size: ACL size module %u -> used %u, count %u / SCO size %u, count %u", 1649429122ccSMatthias Ringwald acl_len, hci_stack->acl_data_packet_length, hci_stack->acl_packets_total_num, 16501a06f663S[email protected] hci_stack->sco_data_packet_length, hci_stack->sco_packets_total_num); 1651e2edc0c3Smatthias.ringwald } 165256cf178bSmatthias.ringwald } 1653a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 1654073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_le_read_buffer_size)){ 1655f8fbdce0SMatthias Ringwald hci_stack->le_data_packets_length = little_endian_read_16(packet, 6); 1656ee303eddS[email protected] hci_stack->le_acl_packets_total_num = packet[8]; 16576c26b087S[email protected] // determine usable ACL payload size 16586c26b087S[email protected] if (HCI_ACL_PAYLOAD_SIZE < hci_stack->le_data_packets_length){ 16596c26b087S[email protected] hci_stack->le_data_packets_length = HCI_ACL_PAYLOAD_SIZE; 16606c26b087S[email protected] } 16619da54300S[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); 166265a46ef3S[email protected] } 1663d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 1664073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_le_read_white_list_size)){ 1665e691bb38SMatthias Ringwald hci_stack->le_whitelist_capacity = packet[6]; 166615d0a15bSMatthias Ringwald log_info("hci_le_read_white_list_size: size %u", hci_stack->le_whitelist_capacity); 16673b6d4121SMatthias Ringwald } 166865a46ef3S[email protected] #endif 1669d70217a2SMatthias Ringwald #endif 1670073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_bd_addr)) { 1671724d70a2SMatthias Ringwald reverse_bd_addr(&packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1], 1672724d70a2SMatthias Ringwald hci_stack->local_bd_addr); 16739da54300S[email protected] log_info("Local Address, Status: 0x%02x: Addr: %s", 16743a9fb326S[email protected] packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr)); 167533373e40SMatthias Ringwald #ifdef ENABLE_CLASSIC 16761624665aSMatthias Ringwald if (hci_stack->link_key_db){ 16771624665aSMatthias Ringwald hci_stack->link_key_db->set_local_bd_addr(hci_stack->local_bd_addr); 16781624665aSMatthias Ringwald } 167933373e40SMatthias Ringwald #endif 1680188981d2Smatthias.ringwald } 168135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1682073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_scan_enable)){ 16833a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 1684381fbed8Smatthias.ringwald } 1685*f5875de5SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_inquiry_cancel)){ 1686*f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_W4_CANCELLED){ 1687*f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_IDLE; 1688*f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 1689*f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 1690*f5875de5SMatthias Ringwald } 1691*f5875de5SMatthias Ringwald } 169235454696SMatthias Ringwald #endif 169335454696SMatthias Ringwald 169465389bfcS[email protected] // Note: HCI init checks 1695073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_supported_features)){ 16963a9fb326S[email protected] memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8); 169765389bfcS[email protected] 169835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1699a5a23fc2S[email protected] // determine usable ACL packet types based on host buffer size and supported features 1700a5a23fc2S[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]); 17018b96126aSMatthias Ringwald log_info("Packet types %04x, eSCO %u", hci_stack->packet_types, hci_extended_sco_link_supported()); 170235454696SMatthias Ringwald #endif 1703f5d8d141S[email protected] // Classic/LE 1704f5d8d141S[email protected] log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported()); 1705559e517eS[email protected] } 1706073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_version_information)){ 1707f8fbdce0SMatthias Ringwald // hci_stack->hci_version = little_endian_read_16(packet, 4); 1708f8fbdce0SMatthias Ringwald // hci_stack->hci_revision = little_endian_read_16(packet, 6); 1709f8fbdce0SMatthias Ringwald // hci_stack->lmp_version = little_endian_read_16(packet, 8); 1710f8fbdce0SMatthias Ringwald hci_stack->manufacturer = little_endian_read_16(packet, 10); 1711f8fbdce0SMatthias Ringwald // hci_stack->lmp_subversion = little_endian_read_16(packet, 12); 17124696bddbSMatthias Ringwald log_info("Manufacturer: 0x%04x", hci_stack->manufacturer); 17134696bddbSMatthias Ringwald } 1714073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_supported_commands)){ 1715a828a756SMatthias Ringwald hci_stack->local_supported_commands[0] = 17163905afbfSMatthias Ringwald (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+14] & 0x80) >> 7 | // bit 0 = Octet 14, bit 7 17173905afbfSMatthias Ringwald (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+24] & 0x40) >> 5 | // bit 1 = Octet 24, bit 6 17183905afbfSMatthias Ringwald (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+10] & 0x10) >> 2 | // bit 2 = Octet 10, bit 4 17193905afbfSMatthias Ringwald (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+18] & 0x08); // bit 3 = Octet 18, bit 3 17203905afbfSMatthias Ringwald log_info("Local supported commands summary 0x%02x", hci_stack->local_supported_commands[0]); 1721a828a756SMatthias Ringwald } 1722e8c8828eSMatthias Ringwald #ifdef ENABLE_CLASSIC 1723073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_synchronous_flow_control_enable)){ 17245b9b590fSMatthias Ringwald if (packet[5] == 0){ 17255b9b590fSMatthias Ringwald hci_stack->synchronous_flow_control_enabled = 1; 17265b9b590fSMatthias Ringwald } 17275b9b590fSMatthias Ringwald } 1728e8c8828eSMatthias Ringwald #endif 172956cf178bSmatthias.ringwald break; 173056cf178bSmatthias.ringwald 17317ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 17326bef4003SMatthias Ringwald // get num cmd packets - limit to 1 to reduce complexity 17336bef4003SMatthias Ringwald hci_stack->num_cmd_packets = packet[3] ? 1 : 0; 17347ec5eeaaSmatthias.ringwald break; 17357ec5eeaaSmatthias.ringwald 17362e440c8aS[email protected] case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:{ 17372e440c8aS[email protected] int offset = 3; 173856cf178bSmatthias.ringwald for (i=0; i<packet[2];i++){ 1739f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, offset); 17402e440c8aS[email protected] offset += 2; 1741f8fbdce0SMatthias Ringwald uint16_t num_packets = little_endian_read_16(packet, offset); 17422e440c8aS[email protected] offset += 2; 17432e440c8aS[email protected] 17445061f3afS[email protected] conn = hci_connection_for_handle(handle); 174556cf178bSmatthias.ringwald if (!conn){ 17469da54300S[email protected] log_error("hci_number_completed_packet lists unused con handle %u", handle); 174756cf178bSmatthias.ringwald continue; 174856cf178bSmatthias.ringwald } 174923bed257S[email protected] 1750e35edcc1S[email protected] if (conn->address_type == BD_ADDR_TYPE_SCO){ 175135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1752e35edcc1S[email protected] if (conn->num_sco_packets_sent >= num_packets){ 1753e35edcc1S[email protected] conn->num_sco_packets_sent -= num_packets; 1754e35edcc1S[email protected] } else { 1755e35edcc1S[email protected] log_error("hci_number_completed_packets, more sco slots freed then sent."); 1756e35edcc1S[email protected] conn->num_sco_packets_sent = 0; 1757e35edcc1S[email protected] } 1758701e3307SMatthias Ringwald hci_notify_if_sco_can_send_now(); 175935454696SMatthias Ringwald #endif 1760e35edcc1S[email protected] } else { 176123bed257S[email protected] if (conn->num_acl_packets_sent >= num_packets){ 176256cf178bSmatthias.ringwald conn->num_acl_packets_sent -= num_packets; 176323bed257S[email protected] } else { 1764e35edcc1S[email protected] log_error("hci_number_completed_packets, more acl slots freed then sent."); 176523bed257S[email protected] conn->num_acl_packets_sent = 0; 176623bed257S[email protected] } 1767e35edcc1S[email protected] } 17689da54300S[email protected] // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u", num_packets, handle, conn->num_acl_packets_sent); 176956cf178bSmatthias.ringwald } 17706772a24cSmatthias.ringwald break; 17712e440c8aS[email protected] } 177235454696SMatthias Ringwald 177335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1774*f5875de5SMatthias Ringwald case HCI_EVENT_INQUIRY_COMPLETE: 1775*f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_ACTIVE){ 1776*f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_IDLE; 1777*f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 1778*f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 1779*f5875de5SMatthias Ringwald } 1780*f5875de5SMatthias Ringwald break; 17811f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 1782724d70a2SMatthias Ringwald reverse_bd_addr(&packet[2], addr); 178337eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 17842d00edd4Smatthias.ringwald link_type = packet[11]; 17859da54300S[email protected] log_info("Connection_incoming: %s, type %u", bd_addr_to_str(addr), link_type); 1786e35edcc1S[email protected] addr_type = link_type == 1 ? BD_ADDR_TYPE_CLASSIC : BD_ADDR_TYPE_SCO; 17872e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 17881f7b95a1Smatthias.ringwald if (!conn) { 17895293c072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 17901f7b95a1Smatthias.ringwald } 1791ce4c8fabSmatthias.ringwald if (!conn) { 1792ce4c8fabSmatthias.ringwald // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 17933a9fb326S[email protected] hci_stack->decline_reason = 0x0d; 1794058e3d6bSMatthias Ringwald bd_addr_copy(hci_stack->decline_addr, addr); 1795ce4c8fabSmatthias.ringwald break; 1796ce4c8fabSmatthias.ringwald } 17975cf766e8SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 179832ab9390Smatthias.ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 1799f3a16b9aSMatthias Ringwald // store info about eSCO 1800f3a16b9aSMatthias Ringwald if (link_type == 0x02){ 1801f3a16b9aSMatthias Ringwald conn->remote_supported_feature_eSCO = 1; 1802f3a16b9aSMatthias Ringwald } 180332ab9390Smatthias.ringwald hci_run(); 18041f7b95a1Smatthias.ringwald break; 18051f7b95a1Smatthias.ringwald 18066772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 1807fe1ed1b8Smatthias.ringwald // Connection management 1808724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], addr); 18099da54300S[email protected] log_info("Connection_complete (status=%u) %s", packet[2], bd_addr_to_str(addr)); 181096a45072S[email protected] addr_type = BD_ADDR_TYPE_CLASSIC; 18112e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 1812fe1ed1b8Smatthias.ringwald if (conn) { 1813b448a0e7Smatthias.ringwald if (!packet[2]){ 1814c8e4258aSmatthias.ringwald conn->state = OPEN; 1815f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 3); 1816ad83dc6aS[email protected] conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES; 1817ee091cf1Smatthias.ringwald 1818c785ef68Smatthias.ringwald // restart timer 1819528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 1820528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&conn->timeout); 1821c785ef68Smatthias.ringwald 18229da54300S[email protected] log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 182343bfb1bdSmatthias.ringwald 182443bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 1825b448a0e7Smatthias.ringwald } else { 18261bd5283dS[email protected] int notify_dedicated_bonding_failed = conn->bonding_flags & BONDING_DEDICATED; 18271bd5283dS[email protected] uint8_t status = packet[2]; 18281bd5283dS[email protected] bd_addr_t bd_address; 18291bd5283dS[email protected] memcpy(&bd_address, conn->address, 6); 1830ad83dc6aS[email protected] 1831b448a0e7Smatthias.ringwald // connection failed, remove entry 1832665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 1833a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 1834c12e46e7Smatthias.ringwald 18351bd5283dS[email protected] // notify client if dedicated bonding 18361bd5283dS[email protected] if (notify_dedicated_bonding_failed){ 18371bd5283dS[email protected] log_info("hci notify_dedicated_bonding_failed"); 18381bd5283dS[email protected] hci_emit_dedicated_bonding_result(bd_address, status); 18391bd5283dS[email protected] } 18401bd5283dS[email protected] 1841c12e46e7Smatthias.ringwald // if authentication error, also delete link key 1842c12e46e7Smatthias.ringwald if (packet[2] == 0x05) { 184315a95bd5SMatthias Ringwald gap_drop_link_key_for_bd_addr(addr); 1844c12e46e7Smatthias.ringwald } 1845fe1ed1b8Smatthias.ringwald } 1846fe1ed1b8Smatthias.ringwald } 18476772a24cSmatthias.ringwald break; 1848fe1ed1b8Smatthias.ringwald 184944d0e3d5S[email protected] case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE: 1850724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], addr); 185144d0e3d5S[email protected] log_info("Synchronous Connection Complete (status=%u) %s", packet[2], bd_addr_to_str(addr)); 18521a06f663S[email protected] if (packet[2]){ 185344d0e3d5S[email protected] // connection failed 185444d0e3d5S[email protected] break; 185544d0e3d5S[email protected] } 18562e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 1857e35edcc1S[email protected] if (!conn) { 1858e35edcc1S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 1859e35edcc1S[email protected] } 1860e35edcc1S[email protected] if (!conn) { 1861e35edcc1S[email protected] break; 1862e35edcc1S[email protected] } 18631a06f663S[email protected] conn->state = OPEN; 1864f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 3); 1865ee752bb8SMatthias Ringwald 1866ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 1867ee752bb8SMatthias Ringwald // update SCO 1868ee752bb8SMatthias Ringwald if (conn->address_type == BD_ADDR_TYPE_SCO && hci_stack->hci_transport && hci_stack->hci_transport->set_sco_config){ 1869ee752bb8SMatthias Ringwald hci_stack->hci_transport->set_sco_config(hci_stack->sco_voice_setting_active, hci_number_sco_connections()); 1870ee752bb8SMatthias Ringwald } 1871ee752bb8SMatthias Ringwald #endif 187244d0e3d5S[email protected] break; 187344d0e3d5S[email protected] 1874afd4e962S[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 1875f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1876afd4e962S[email protected] conn = hci_connection_for_handle(handle); 1877afd4e962S[email protected] if (!conn) break; 1878afd4e962S[email protected] if (!packet[2]){ 1879afd4e962S[email protected] uint8_t * features = &packet[5]; 1880afd4e962S[email protected] if (features[6] & (1 << 3)){ 1881afd4e962S[email protected] conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP; 1882afd4e962S[email protected] } 188398a2fd1cSMatthias Ringwald if (features[3] & (1<<7)){ 188498a2fd1cSMatthias Ringwald conn->remote_supported_feature_eSCO = 1; 188598a2fd1cSMatthias Ringwald } 1886afd4e962S[email protected] } 1887afd4e962S[email protected] conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES; 188898a2fd1cSMatthias Ringwald log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x, eSCO %u", conn->bonding_flags, conn->remote_supported_feature_eSCO); 1889ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 1890ad83dc6aS[email protected] conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 1891ad83dc6aS[email protected] } 1892afd4e962S[email protected] break; 1893afd4e962S[email protected] 18947fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 18959da54300S[email protected] log_info("HCI_EVENT_LINK_KEY_REQUEST"); 18967fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST); 189774d716b5S[email protected] // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST 1898a98592bcSMatthias Ringwald if (hci_stack->bondable && !hci_stack->link_key_db) break; 189932ab9390Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST); 190064472d52Smatthias.ringwald hci_run(); 1901d9a327f9Smatthias.ringwald // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set 190229d53098Smatthias.ringwald return; 19037fde4af9Smatthias.ringwald 19049ab95c90S[email protected] case HCI_EVENT_LINK_KEY_NOTIFICATION: { 1905724d70a2SMatthias Ringwald reverse_bd_addr(&packet[2], addr); 19062e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 19079ab95c90S[email protected] if (!conn) break; 19089ab95c90S[email protected] conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION; 19097bdc6798S[email protected] link_key_type_t link_key_type = (link_key_type_t)packet[24]; 19109ab95c90S[email protected] // Change Connection Encryption keeps link key type 19119ab95c90S[email protected] if (link_key_type != CHANGED_COMBINATION_KEY){ 19129ab95c90S[email protected] conn->link_key_type = link_key_type; 19139ab95c90S[email protected] } 191455597469SMatthias Ringwald gap_store_link_key_for_bd_addr(addr, &packet[8], conn->link_key_type); 191529d53098Smatthias.ringwald // still forward event to allow dismiss of pairing dialog 19167fde4af9Smatthias.ringwald break; 19179ab95c90S[email protected] } 19187fde4af9Smatthias.ringwald 19197fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 19206724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE); 19214c57c146S[email protected] // non-bondable mode: pin code negative reply will be sent 19223a9fb326S[email protected] if (!hci_stack->bondable){ 1923899283eaS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST); 1924f8fb5f6eS[email protected] hci_run(); 1925f8fb5f6eS[email protected] return; 19264c57c146S[email protected] } 1927d9a327f9Smatthias.ringwald // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key 1928a98592bcSMatthias Ringwald if (!hci_stack->link_key_db) break; 1929a6ef64baSMilanka Ringwald hci_event_pin_code_request_get_bd_addr(packet, addr); 1930a98592bcSMatthias Ringwald hci_stack->link_key_db->delete_link_key(addr); 19317fde4af9Smatthias.ringwald break; 19327fde4af9Smatthias.ringwald 19331d6b20aeS[email protected] case HCI_EVENT_IO_CAPABILITY_REQUEST: 19341d6b20aeS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST); 1935dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY); 1936dbe1a790S[email protected] break; 1937dbe1a790S[email protected] 1938dbe1a790S[email protected] case HCI_EVENT_USER_CONFIRMATION_REQUEST: 19396724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 19403a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 1941dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY); 1942dbe1a790S[email protected] break; 1943dbe1a790S[email protected] 1944dbe1a790S[email protected] case HCI_EVENT_USER_PASSKEY_REQUEST: 19456724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 19463a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 1947dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY); 19481d6b20aeS[email protected] break; 194935454696SMatthias Ringwald #endif 19501d6b20aeS[email protected] 1951f0944df2S[email protected] case HCI_EVENT_ENCRYPTION_CHANGE: 1952f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1953f0944df2S[email protected] conn = hci_connection_for_handle(handle); 1954f0944df2S[email protected] if (!conn) break; 1955ad83dc6aS[email protected] if (packet[2] == 0) { 1956f0944df2S[email protected] if (packet[5]){ 1957f0944df2S[email protected] conn->authentication_flags |= CONNECTION_ENCRYPTED; 1958f0944df2S[email protected] } else { 1959536f9994S[email protected] conn->authentication_flags &= ~CONNECTION_ENCRYPTED; 1960f0944df2S[email protected] } 1961ad83dc6aS[email protected] } 196235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1963a00031e2S[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 196435454696SMatthias Ringwald #endif 1965f0944df2S[email protected] break; 1966f0944df2S[email protected] 196735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 19681eb2563eS[email protected] case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT: 1969f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 19701eb2563eS[email protected] conn = hci_connection_for_handle(handle); 19711eb2563eS[email protected] if (!conn) break; 1972ad83dc6aS[email protected] 1973ad83dc6aS[email protected] // dedicated bonding: send result and disconnect 1974ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 1975ad83dc6aS[email protected] conn->bonding_flags &= ~BONDING_DEDICATED; 1976ad83dc6aS[email protected] conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE; 19771bd5283dS[email protected] conn->bonding_status = packet[2]; 1978ad83dc6aS[email protected] break; 1979ad83dc6aS[email protected] } 1980ad83dc6aS[email protected] 1981b5cb6874S[email protected] if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){ 19821eb2563eS[email protected] // link key sufficient for requested security 19831eb2563eS[email protected] conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 1984ad83dc6aS[email protected] break; 1985ad83dc6aS[email protected] } 19861eb2563eS[email protected] // not enough 19871eb2563eS[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 19881eb2563eS[email protected] break; 198935454696SMatthias Ringwald #endif 199034d2123cS[email protected] 199186805605S[email protected] // HCI_EVENT_DISCONNECTION_COMPLETE 1992ccda6e14S[email protected] // has been split, to first notify stack before shutting connection down 1993ccda6e14S[email protected] // see end of function, too. 1994a4f30ec0S[email protected] case HCI_EVENT_DISCONNECTION_COMPLETE: 1995a4f30ec0S[email protected] if (packet[2]) break; // status != 0 1996f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1997c6a37cfdSMatthias Ringwald // drop outgoing ACL fragments if it is for closed connection 1998c6a37cfdSMatthias Ringwald if (hci_stack->acl_fragmentation_total_size > 0) { 1999c6a37cfdSMatthias Ringwald if (handle == READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer)){ 2000c6a37cfdSMatthias Ringwald log_info("hci: drop fragmented ACL data for closed connection"); 2001c6a37cfdSMatthias Ringwald hci_stack->acl_fragmentation_total_size = 0; 2002c6a37cfdSMatthias Ringwald hci_stack->acl_fragmentation_pos = 0; 2003c6a37cfdSMatthias Ringwald } 2004c6a37cfdSMatthias Ringwald } 200535454696SMatthias Ringwald 20069a2e4658SMatthias Ringwald // re-enable advertisements for le connections if active 2007c6a37cfdSMatthias Ringwald conn = hci_connection_for_handle(handle); 2008c6a37cfdSMatthias Ringwald if (!conn) break; 200935454696SMatthias Ringwald #ifdef ENABLE_BLE 2010d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 20119a2e4658SMatthias Ringwald if (hci_is_le_connection(conn) && hci_stack->le_advertisements_enabled){ 20129a2e4658SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_ENABLE; 20139a2e4658SMatthias Ringwald } 201435454696SMatthias Ringwald #endif 2015d70217a2SMatthias Ringwald #endif 2016ccda6e14S[email protected] conn->state = RECEIVED_DISCONNECTION_COMPLETE; 2017ccda6e14S[email protected] break; 20186772a24cSmatthias.ringwald 2019c68bdf90Smatthias.ringwald case HCI_EVENT_HARDWARE_ERROR: 2020313e5f9cSMatthias Ringwald log_error("Hardware Error: 0x%02x", packet[2]); 2021d23838ecSMatthias Ringwald if (hci_stack->hardware_error_callback){ 2022c2e1fa60SMatthias Ringwald (*hci_stack->hardware_error_callback)(packet[2]); 20237586ee35S[email protected] } else { 20247586ee35S[email protected] // if no special requests, just reboot stack 20257586ee35S[email protected] hci_power_control_off(); 20267586ee35S[email protected] hci_power_control_on(); 2027c68bdf90Smatthias.ringwald } 2028c68bdf90Smatthias.ringwald break; 2029c68bdf90Smatthias.ringwald 20300e6f3837SMatthias Ringwald #ifdef ENABLE_CLASSIC 20315cf766e8SMatthias Ringwald case HCI_EVENT_ROLE_CHANGE: 20325cf766e8SMatthias Ringwald if (packet[2]) break; // status != 0 2033f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 20345cf766e8SMatthias Ringwald conn = hci_connection_for_handle(handle); 20355cf766e8SMatthias Ringwald if (!conn) break; // no conn 20365cf766e8SMatthias Ringwald conn->role = packet[9]; 20375cf766e8SMatthias Ringwald break; 20380e6f3837SMatthias Ringwald #endif 20395cf766e8SMatthias Ringwald 204063fa3374SMatthias Ringwald case HCI_EVENT_TRANSPORT_PACKET_SENT: 2041d051460cS[email protected] // release packet buffer only for asynchronous transport and if there are not further fragements 20424fa24b5fS[email protected] if (hci_transport_synchronous()) { 204363fa3374SMatthias Ringwald log_error("Synchronous HCI Transport shouldn't send HCI_EVENT_TRANSPORT_PACKET_SENT"); 20444fa24b5fS[email protected] return; // instead of break: to avoid re-entering hci_run() 20454fa24b5fS[email protected] } 2046d051460cS[email protected] if (hci_stack->acl_fragmentation_total_size) break; 2047d051460cS[email protected] hci_release_packet_buffer(); 2048701e3307SMatthias Ringwald 204963fa3374SMatthias Ringwald // L2CAP receives this event via the hci_emit_event below 205063fa3374SMatthias Ringwald 205135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 205263fa3374SMatthias Ringwald // For SCO, we do the can_send_now_check here 2053701e3307SMatthias Ringwald hci_notify_if_sco_can_send_now(); 205435454696SMatthias Ringwald #endif 20556b4af23dS[email protected] break; 20566b4af23dS[email protected] 205735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 205863fa3374SMatthias Ringwald case HCI_EVENT_SCO_CAN_SEND_NOW: 205963fa3374SMatthias Ringwald // For SCO, we do the can_send_now_check here 206063fa3374SMatthias Ringwald hci_notify_if_sco_can_send_now(); 206163fa3374SMatthias Ringwald return; 20621cfb383eSMatthias Ringwald 20631cfb383eSMatthias Ringwald // explode inquriy results for easier consumption 20641cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 20651cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 20661cfb383eSMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 20671cfb383eSMatthias Ringwald gap_inquiry_explode(packet); 20681cfb383eSMatthias Ringwald break; 206935454696SMatthias Ringwald #endif 207063fa3374SMatthias Ringwald 2071a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 20725909f7f2Smatthias.ringwald case HCI_EVENT_LE_META: 20735909f7f2Smatthias.ringwald switch (packet[2]){ 2074d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 207557c9da5bS[email protected] case HCI_SUBEVENT_LE_ADVERTISING_REPORT: 20764f4e0224SMatthias Ringwald // log_info("advertising report received"); 20777bdc6798S[email protected] if (hci_stack->le_scanning_state != LE_SCANNING) break; 207857c9da5bS[email protected] le_handle_advertisement_report(packet, size); 20797bdc6798S[email protected] break; 2080d70217a2SMatthias Ringwald #endif 20815909f7f2Smatthias.ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 20825909f7f2Smatthias.ringwald // Connection management 2083724d70a2SMatthias Ringwald reverse_bd_addr(&packet[8], addr); 20847bdc6798S[email protected] addr_type = (bd_addr_type_t)packet[7]; 20859da54300S[email protected] log_info("LE Connection_complete (status=%u) type %u, %s", packet[3], addr_type, bd_addr_to_str(addr)); 20862e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 2087d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 208842ff5ba1SMatthias Ringwald // if auto-connect, remove from whitelist in both roles 208942ff5ba1SMatthias Ringwald if (hci_stack->le_connecting_state == LE_CONNECTING_WHITELIST){ 209042ff5ba1SMatthias Ringwald hci_remove_from_whitelist(addr_type, addr); 209142ff5ba1SMatthias Ringwald } 209242ff5ba1SMatthias Ringwald // handle error: error is reported only to the initiator -> outgoing connection 20935909f7f2Smatthias.ringwald if (packet[3]){ 209442ff5ba1SMatthias Ringwald // outgoing connection establishment is done 209542ff5ba1SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 209642ff5ba1SMatthias Ringwald // remove entry 20975909f7f2Smatthias.ringwald if (conn){ 2098665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 20995909f7f2Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 21005909f7f2Smatthias.ringwald } 21015909f7f2Smatthias.ringwald break; 21025909f7f2Smatthias.ringwald } 2103d70217a2SMatthias Ringwald #endif 210442ff5ba1SMatthias Ringwald // on success, both hosts receive connection complete event 21055cf766e8SMatthias Ringwald if (packet[6] == HCI_ROLE_MASTER){ 2106d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 210742ff5ba1SMatthias Ringwald // if we're master, it was an outgoing connection and we're done with it 210842ff5ba1SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 2109d70217a2SMatthias Ringwald #endif 211042ff5ba1SMatthias Ringwald } else { 2111d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 211242ff5ba1SMatthias Ringwald // if we're slave, it was an incoming connection, advertisements have stopped 2113171293d3SMatthias Ringwald hci_stack->le_advertisements_active = 0; 21145106e6dcSMatthias Ringwald // try to re-enable them 21155106e6dcSMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_ENABLE; 2116d70217a2SMatthias Ringwald #endif 211742ff5ba1SMatthias Ringwald } 2118171293d3SMatthias Ringwald // LE connections are auto-accepted, so just create a connection if there isn't one already 211942ff5ba1SMatthias Ringwald if (!conn){ 212096a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 21215909f7f2Smatthias.ringwald } 212242ff5ba1SMatthias Ringwald // no memory, sorry. 21235909f7f2Smatthias.ringwald if (!conn){ 21245909f7f2Smatthias.ringwald break; 21255909f7f2Smatthias.ringwald } 21265909f7f2Smatthias.ringwald 21275909f7f2Smatthias.ringwald conn->state = OPEN; 21285cf766e8SMatthias Ringwald conn->role = packet[6]; 2129f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 4); 21305909f7f2Smatthias.ringwald 21315909f7f2Smatthias.ringwald // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock 21325909f7f2Smatthias.ringwald 21335909f7f2Smatthias.ringwald // restart timer 2134528a4a3bSMatthias Ringwald // btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 2135528a4a3bSMatthias Ringwald // btstack_run_loop_add_timer(&conn->timeout); 21365909f7f2Smatthias.ringwald 21379da54300S[email protected] log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 21385909f7f2Smatthias.ringwald 21395909f7f2Smatthias.ringwald hci_emit_nr_connections_changed(); 21405909f7f2Smatthias.ringwald break; 21415909f7f2Smatthias.ringwald 2142f8fbdce0SMatthias Ringwald // log_info("LE buffer size: %u, count %u", little_endian_read_16(packet,6), packet[8]); 214365a46ef3S[email protected] 21445909f7f2Smatthias.ringwald default: 21455909f7f2Smatthias.ringwald break; 21465909f7f2Smatthias.ringwald } 21475909f7f2Smatthias.ringwald break; 21485909f7f2Smatthias.ringwald #endif 21496772a24cSmatthias.ringwald default: 21506772a24cSmatthias.ringwald break; 2151fe1ed1b8Smatthias.ringwald } 2152fe1ed1b8Smatthias.ringwald 21533429f56bSmatthias.ringwald // handle BT initialization 21543a9fb326S[email protected] if (hci_stack->state == HCI_STATE_INITIALIZING){ 21556155b3d3S[email protected] hci_initializing_event_handler(packet, size); 2156c9af4d3fS[email protected] } 215722909952Smatthias.ringwald 215889db417bSmatthias.ringwald // help with BT sleep 21593a9fb326S[email protected] if (hci_stack->state == HCI_STATE_FALLING_ASLEEP 216074b323a9SMatthias Ringwald && hci_stack->substate == HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE 2161073bd0faSMatthias Ringwald && HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_scan_enable)){ 216255975f88SMatthias Ringwald hci_initializing_next_state(); 216389db417bSmatthias.ringwald } 216489db417bSmatthias.ringwald 216586805605S[email protected] // notify upper stack 2166d6b06661SMatthias Ringwald hci_emit_event(packet, size, 0); // don't dump, already happened in packet handler 216794ab26f8Smatthias.ringwald 216886805605S[email protected] // moved here to give upper stack a chance to close down everything with hci_connection_t intact 21690e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_DISCONNECTION_COMPLETE){ 217086805605S[email protected] if (!packet[2]){ 2171f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 217205ae8de3SMatthias Ringwald hci_connection_t * aConn = hci_connection_for_handle(handle); 217305ae8de3SMatthias Ringwald if (aConn) { 217405ae8de3SMatthias Ringwald uint8_t status = aConn->bonding_status; 217505ae8de3SMatthias Ringwald uint16_t flags = aConn->bonding_flags; 217686805605S[email protected] bd_addr_t bd_address; 217705ae8de3SMatthias Ringwald memcpy(&bd_address, aConn->address, 6); 217805ae8de3SMatthias Ringwald hci_shutdown_connection(aConn); 2179bb820044S[email protected] // connection struct is gone, don't access anymore 2180bb820044S[email protected] if (flags & BONDING_EMIT_COMPLETE_ON_DISCONNECT){ 218186805605S[email protected] hci_emit_dedicated_bonding_result(bd_address, status); 218286805605S[email protected] } 218386805605S[email protected] } 218486805605S[email protected] } 218586805605S[email protected] } 218686805605S[email protected] 218794ab26f8Smatthias.ringwald // execute main loop 218894ab26f8Smatthias.ringwald hci_run(); 218916833f0aSmatthias.ringwald } 219016833f0aSmatthias.ringwald 219135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2192c91d150bS[email protected] static void sco_handler(uint8_t * packet, uint16_t size){ 21938abbe8b5SMatthias Ringwald if (!hci_stack->sco_packet_handler) return; 21943d50b4baSMatthias Ringwald hci_stack->sco_packet_handler(HCI_SCO_DATA_PACKET, 0, packet, size); 21952b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 21962b838201SMatthias Ringwald hci_con_handle_t con_handle = READ_SCO_CONNECTION_HANDLE(packet); 21972b838201SMatthias Ringwald hci_connection_t *conn = hci_connection_for_handle(con_handle); 21982b838201SMatthias Ringwald if (conn){ 21992b838201SMatthias Ringwald conn->num_packets_completed++; 22002b838201SMatthias Ringwald hci_stack->host_completed_packets = 1; 22012b838201SMatthias Ringwald hci_run(); 22022b838201SMatthias Ringwald } 22032b838201SMatthias Ringwald #endif 2204c91d150bS[email protected] } 220535454696SMatthias Ringwald #endif 2206c91d150bS[email protected] 22070a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 22085bb5bc3eS[email protected] hci_dump_packet(packet_type, 1, packet, size); 220910e830c9Smatthias.ringwald switch (packet_type) { 221010e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 221110e830c9Smatthias.ringwald event_handler(packet, size); 221210e830c9Smatthias.ringwald break; 221310e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 221410e830c9Smatthias.ringwald acl_handler(packet, size); 221510e830c9Smatthias.ringwald break; 221635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2217c91d150bS[email protected] case HCI_SCO_DATA_PACKET: 2218c91d150bS[email protected] sco_handler(packet, size); 2219202c8a4cSMatthias Ringwald break; 222035454696SMatthias Ringwald #endif 222110e830c9Smatthias.ringwald default: 222210e830c9Smatthias.ringwald break; 222310e830c9Smatthias.ringwald } 222410e830c9Smatthias.ringwald } 222510e830c9Smatthias.ringwald 2226d6b06661SMatthias Ringwald /** 2227d6b06661SMatthias Ringwald * @brief Add event packet handler. 2228d6b06661SMatthias Ringwald */ 2229d6b06661SMatthias Ringwald void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){ 2230d6b06661SMatthias Ringwald btstack_linked_list_add_tail(&hci_stack->event_handlers, (btstack_linked_item_t*) callback_handler); 2231d6b06661SMatthias Ringwald } 2232d6b06661SMatthias Ringwald 2233d6b06661SMatthias Ringwald 2234fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 22353d50b4baSMatthias Ringwald void hci_register_acl_packet_handler(btstack_packet_handler_t handler){ 2236fb37a842SMatthias Ringwald hci_stack->acl_packet_handler = handler; 223716833f0aSmatthias.ringwald } 223816833f0aSmatthias.ringwald 223935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 22408abbe8b5SMatthias Ringwald /** 22418abbe8b5SMatthias Ringwald * @brief Registers a packet handler for SCO data. Used for HSP and HFP profiles. 22428abbe8b5SMatthias Ringwald */ 22433d50b4baSMatthias Ringwald void hci_register_sco_packet_handler(btstack_packet_handler_t handler){ 22448abbe8b5SMatthias Ringwald hci_stack->sco_packet_handler = handler; 22458abbe8b5SMatthias Ringwald } 224635454696SMatthias Ringwald #endif 22478abbe8b5SMatthias Ringwald 224871de195eSMatthias Ringwald static void hci_state_reset(void){ 2249595bdbfbS[email protected] // no connections yet 2250595bdbfbS[email protected] hci_stack->connections = NULL; 225174308b23Smatthias.ringwald 225274308b23Smatthias.ringwald // keep discoverable/connectable as this has been requested by the client(s) 225374308b23Smatthias.ringwald // hci_stack->discoverable = 0; 225474308b23Smatthias.ringwald // hci_stack->connectable = 0; 225574308b23Smatthias.ringwald // hci_stack->bondable = 1; 2256b95a5a35SMatthias Ringwald // hci_stack->own_addr_type = 0; 2257595bdbfbS[email protected] 225844935e40S[email protected] // buffer is free 225944935e40S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 226044935e40S[email protected] 2261595bdbfbS[email protected] // no pending cmds 2262595bdbfbS[email protected] hci_stack->decline_reason = 0; 2263595bdbfbS[email protected] hci_stack->new_scan_enable_value = 0xff; 2264595bdbfbS[email protected] 2265595bdbfbS[email protected] // LE 2266b95a5a35SMatthias Ringwald #ifdef ENABLE_BLE 2267b95a5a35SMatthias Ringwald memset(hci_stack->le_random_address, 0, 6); 2268b95a5a35SMatthias Ringwald hci_stack->le_random_address_set = 0; 2269d70217a2SMatthias Ringwald #endif 2270d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 2271595bdbfbS[email protected] hci_stack->le_scanning_state = LE_SCAN_IDLE; 2272e2602ea2Smatthias.ringwald hci_stack->le_scan_type = 0xff; 2273b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 2274e83201bcSMatthias Ringwald hci_stack->le_whitelist = 0; 2275e83201bcSMatthias Ringwald hci_stack->le_whitelist_capacity = 0; 2276d70217a2SMatthias Ringwald #endif 2277d70217a2SMatthias Ringwald 2278e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_interval_min = 6; 2279e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_interval_max = 3200; 2280e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_latency_min = 0; 2281e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_latency_max = 500; 2282e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_supervision_timeout_min = 10; 2283e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_supervision_timeout_max = 3200; 2284595bdbfbS[email protected] } 2285595bdbfbS[email protected] 228635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 22872d5e09d6SMatthias Ringwald /** 22882d5e09d6SMatthias Ringwald * @brief Configure Bluetooth hardware control. Has to be called before power on. 22892d5e09d6SMatthias Ringwald */ 22902d5e09d6SMatthias Ringwald void hci_set_link_key_db(btstack_link_key_db_t const * link_key_db){ 22912d5e09d6SMatthias Ringwald // store and open remote device db 22922d5e09d6SMatthias Ringwald hci_stack->link_key_db = link_key_db; 22932d5e09d6SMatthias Ringwald if (hci_stack->link_key_db) { 22942d5e09d6SMatthias Ringwald hci_stack->link_key_db->open(); 22952d5e09d6SMatthias Ringwald } 22962d5e09d6SMatthias Ringwald } 229735454696SMatthias Ringwald #endif 22982d5e09d6SMatthias Ringwald 22992d5e09d6SMatthias Ringwald void hci_init(const hci_transport_t *transport, const void *config){ 2300475c8125Smatthias.ringwald 23013a9fb326S[email protected] #ifdef HAVE_MALLOC 23023a9fb326S[email protected] if (!hci_stack) { 23033a9fb326S[email protected] hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t)); 23043a9fb326S[email protected] } 23053a9fb326S[email protected] #else 23063a9fb326S[email protected] hci_stack = &hci_stack_static; 23073a9fb326S[email protected] #endif 230866fb9560S[email protected] memset(hci_stack, 0, sizeof(hci_stack_t)); 23093a9fb326S[email protected] 2310475c8125Smatthias.ringwald // reference to use transport layer implementation 23113a9fb326S[email protected] hci_stack->hci_transport = transport; 2312475c8125Smatthias.ringwald 231311e23e5fSmatthias.ringwald // reference to used config 23143a9fb326S[email protected] hci_stack->config = config; 231511e23e5fSmatthias.ringwald 231626a9b6daSMatthias Ringwald // setup pointer for outgoing packet buffer 231726a9b6daSMatthias Ringwald hci_stack->hci_packet_buffer = &hci_stack->hci_packet_buffer_data[HCI_OUTGOING_PRE_BUFFER_SIZE]; 231826a9b6daSMatthias Ringwald 23198fcba05dSmatthias.ringwald // max acl payload size defined in config.h 23203a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 23218fcba05dSmatthias.ringwald 232216833f0aSmatthias.ringwald // register packet handlers with transport 232310e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 2324f5454fc6Smatthias.ringwald 23253a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 2326e2386ba1S[email protected] 2327e2386ba1S[email protected] // class of device 23283a9fb326S[email protected] hci_stack->class_of_device = 0x007a020c; // Smartphone 2329a45d6b9fS[email protected] 2330f20168b8Smatthias.ringwald // bondable by default 2331f20168b8Smatthias.ringwald hci_stack->bondable = 1; 2332f20168b8Smatthias.ringwald 233363048403S[email protected] // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept 23343a9fb326S[email protected] hci_stack->ssp_enable = 1; 23353a9fb326S[email protected] hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT; 23363a9fb326S[email protected] hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING; 23373a9fb326S[email protected] hci_stack->ssp_auto_accept = 1; 233869a97523S[email protected] 2339fac2e2feSMatthias Ringwald // voice setting - signed 16 bit pcm data with CVSD over the air 2340fac2e2feSMatthias Ringwald hci_stack->sco_voice_setting = 0x60; 2341d950d659SMatthias Ringwald 2342595bdbfbS[email protected] hci_state_reset(); 2343475c8125Smatthias.ringwald } 2344475c8125Smatthias.ringwald 23453fb36a29SMatthias Ringwald /** 23463fb36a29SMatthias Ringwald * @brief Configure Bluetooth chipset driver. Has to be called before power on, or right after receiving the local version information 23473fb36a29SMatthias Ringwald */ 23483fb36a29SMatthias Ringwald void hci_set_chipset(const btstack_chipset_t *chipset_driver){ 23493fb36a29SMatthias Ringwald hci_stack->chipset = chipset_driver; 23503fb36a29SMatthias Ringwald 23513fb36a29SMatthias Ringwald // reset chipset driver - init is also called on power_up 23523fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->init){ 23533fb36a29SMatthias Ringwald hci_stack->chipset->init(hci_stack->config); 23543fb36a29SMatthias Ringwald } 23553fb36a29SMatthias Ringwald } 23563fb36a29SMatthias Ringwald 2357fb55bd0aSMatthias Ringwald /** 2358d0b87befSMatthias Ringwald * @brief Configure Bluetooth hardware control. Has to be called after hci_init() but before power on. 2359fb55bd0aSMatthias Ringwald */ 2360fb55bd0aSMatthias Ringwald void hci_set_control(const btstack_control_t *hardware_control){ 2361fb55bd0aSMatthias Ringwald // references to used control implementation 2362fb55bd0aSMatthias Ringwald hci_stack->control = hardware_control; 2363d0b87befSMatthias Ringwald // init with transport config 2364d0b87befSMatthias Ringwald hardware_control->init(hci_stack->config); 2365fb55bd0aSMatthias Ringwald } 2366fb55bd0aSMatthias Ringwald 236771de195eSMatthias Ringwald void hci_close(void){ 2368404843c1Smatthias.ringwald // close remote device db 2369a98592bcSMatthias Ringwald if (hci_stack->link_key_db) { 2370a98592bcSMatthias Ringwald hci_stack->link_key_db->close(); 2371404843c1Smatthias.ringwald } 23727224be7eSMatthias Ringwald 23737224be7eSMatthias Ringwald btstack_linked_list_iterator_t lit; 23747224be7eSMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->connections); 23757224be7eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 23767224be7eSMatthias Ringwald // cancel all l2cap connections by emitting dicsconnection complete before shutdown (free) connection 23777224be7eSMatthias Ringwald hci_connection_t * connection = (hci_connection_t*) btstack_linked_list_iterator_next(&lit); 23787224be7eSMatthias Ringwald hci_emit_disconnection_complete(connection->con_handle, 0x16); // terminated by local host 23797224be7eSMatthias Ringwald hci_shutdown_connection(connection); 2380f5454fc6Smatthias.ringwald } 23817224be7eSMatthias Ringwald 2382f5454fc6Smatthias.ringwald hci_power_control(HCI_POWER_OFF); 23833a9fb326S[email protected] 23843a9fb326S[email protected] #ifdef HAVE_MALLOC 23853a9fb326S[email protected] free(hci_stack); 23863a9fb326S[email protected] #endif 23873a9fb326S[email protected] hci_stack = NULL; 2388404843c1Smatthias.ringwald } 2389404843c1Smatthias.ringwald 239035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 239160b9e82fSMatthias Ringwald void gap_set_class_of_device(uint32_t class_of_device){ 23929e61646fS[email protected] hci_stack->class_of_device = class_of_device; 23939e61646fS[email protected] } 239476f27cffSMatthias Ringwald 239576f27cffSMatthias Ringwald void hci_disable_l2cap_timeout_check(void){ 239676f27cffSMatthias Ringwald disable_l2cap_timeouts = 1; 239776f27cffSMatthias Ringwald } 239835454696SMatthias Ringwald #endif 23999e61646fS[email protected] 240076f27cffSMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 2401f456b2d0S[email protected] // Set Public BD ADDR - passed on to Bluetooth chipset if supported in bt_control_h 2402f456b2d0S[email protected] void hci_set_bd_addr(bd_addr_t addr){ 2403f456b2d0S[email protected] memcpy(hci_stack->custom_bd_addr, addr, 6); 2404f456b2d0S[email protected] hci_stack->custom_bd_addr_set = 1; 2405f456b2d0S[email protected] } 240676f27cffSMatthias Ringwald #endif 2407f456b2d0S[email protected] 24088d213e1aSmatthias.ringwald // State-Module-Driver overview 24098d213e1aSmatthias.ringwald // state module low-level 24108d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 24118d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 24128d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 24138d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 2414d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 2415d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 2416c7e0c5f6Smatthias.ringwald 241740d1c7a4Smatthias.ringwald static int hci_power_control_on(void){ 24187301ad89Smatthias.ringwald 2419038bc64cSmatthias.ringwald // power on 2420f9a30166Smatthias.ringwald int err = 0; 24213a9fb326S[email protected] if (hci_stack->control && hci_stack->control->on){ 2422d0b87befSMatthias Ringwald err = (*hci_stack->control->on)(); 2423f9a30166Smatthias.ringwald } 2424038bc64cSmatthias.ringwald if (err){ 24259da54300S[email protected] log_error( "POWER_ON failed"); 2426038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 2427038bc64cSmatthias.ringwald return err; 2428038bc64cSmatthias.ringwald } 2429038bc64cSmatthias.ringwald 243024b3c629SMatthias Ringwald // int chipset driver 24313fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->init){ 24323fb36a29SMatthias Ringwald hci_stack->chipset->init(hci_stack->config); 24333fb36a29SMatthias Ringwald } 24343fb36a29SMatthias Ringwald 243524b3c629SMatthias Ringwald // init transport 243624b3c629SMatthias Ringwald if (hci_stack->hci_transport->init){ 243724b3c629SMatthias Ringwald hci_stack->hci_transport->init(hci_stack->config); 243824b3c629SMatthias Ringwald } 243924b3c629SMatthias Ringwald 244024b3c629SMatthias Ringwald // open transport 244124b3c629SMatthias Ringwald err = hci_stack->hci_transport->open(); 2442038bc64cSmatthias.ringwald if (err){ 24439da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 24443a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 2445d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 2446f9a30166Smatthias.ringwald } 2447038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 2448038bc64cSmatthias.ringwald return err; 2449038bc64cSmatthias.ringwald } 24508d213e1aSmatthias.ringwald return 0; 24518d213e1aSmatthias.ringwald } 2452038bc64cSmatthias.ringwald 245340d1c7a4Smatthias.ringwald static void hci_power_control_off(void){ 24548d213e1aSmatthias.ringwald 24559da54300S[email protected] log_info("hci_power_control_off"); 24569418f9c9Smatthias.ringwald 24578d213e1aSmatthias.ringwald // close low-level device 245824b3c629SMatthias Ringwald hci_stack->hci_transport->close(); 24598d213e1aSmatthias.ringwald 24609da54300S[email protected] log_info("hci_power_control_off - hci_transport closed"); 24619418f9c9Smatthias.ringwald 24628d213e1aSmatthias.ringwald // power off 24633a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 2464d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 24658d213e1aSmatthias.ringwald } 24669418f9c9Smatthias.ringwald 24679da54300S[email protected] log_info("hci_power_control_off - control closed"); 24689418f9c9Smatthias.ringwald 24693a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 247072ea5239Smatthias.ringwald } 247172ea5239Smatthias.ringwald 247240d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){ 247372ea5239Smatthias.ringwald 24749da54300S[email protected] log_info("hci_power_control_sleep"); 24753144bce4Smatthias.ringwald 2476b429b9b7Smatthias.ringwald #if 0 2477b429b9b7Smatthias.ringwald // don't close serial port during sleep 2478b429b9b7Smatthias.ringwald 247972ea5239Smatthias.ringwald // close low-level device 24803a9fb326S[email protected] hci_stack->hci_transport->close(hci_stack->config); 2481b429b9b7Smatthias.ringwald #endif 248272ea5239Smatthias.ringwald 248372ea5239Smatthias.ringwald // sleep mode 24843a9fb326S[email protected] if (hci_stack->control && hci_stack->control->sleep){ 2485d0b87befSMatthias Ringwald (*hci_stack->control->sleep)(); 248672ea5239Smatthias.ringwald } 2487b429b9b7Smatthias.ringwald 24883a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 24898d213e1aSmatthias.ringwald } 24908d213e1aSmatthias.ringwald 249140d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){ 2492b429b9b7Smatthias.ringwald 24939da54300S[email protected] log_info("hci_power_control_wake"); 2494b429b9b7Smatthias.ringwald 2495b429b9b7Smatthias.ringwald // wake on 24963a9fb326S[email protected] if (hci_stack->control && hci_stack->control->wake){ 2497d0b87befSMatthias Ringwald (*hci_stack->control->wake)(); 2498b429b9b7Smatthias.ringwald } 2499b429b9b7Smatthias.ringwald 2500b429b9b7Smatthias.ringwald #if 0 2501b429b9b7Smatthias.ringwald // open low-level device 25023a9fb326S[email protected] int err = hci_stack->hci_transport->open(hci_stack->config); 2503b429b9b7Smatthias.ringwald if (err){ 25049da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 25053a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 2506d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 2507b429b9b7Smatthias.ringwald } 2508b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 2509b429b9b7Smatthias.ringwald return err; 2510b429b9b7Smatthias.ringwald } 2511b429b9b7Smatthias.ringwald #endif 2512b429b9b7Smatthias.ringwald 2513b429b9b7Smatthias.ringwald return 0; 2514b429b9b7Smatthias.ringwald } 2515b429b9b7Smatthias.ringwald 251644935e40S[email protected] static void hci_power_transition_to_initializing(void){ 251744935e40S[email protected] // set up state machine 251844935e40S[email protected] hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent 251944935e40S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 252044935e40S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 25215c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET; 252244935e40S[email protected] } 2523b429b9b7Smatthias.ringwald 25248d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 2525d661ed19Smatthias.ringwald 2526f04a0c31SMatthias Ringwald log_info("hci_power_control: %d, current mode %u", power_mode, hci_stack->state); 2527d661ed19Smatthias.ringwald 25288d213e1aSmatthias.ringwald int err = 0; 25293a9fb326S[email protected] switch (hci_stack->state){ 25308d213e1aSmatthias.ringwald 25318d213e1aSmatthias.ringwald case HCI_STATE_OFF: 25328d213e1aSmatthias.ringwald switch (power_mode){ 25338d213e1aSmatthias.ringwald case HCI_POWER_ON: 25348d213e1aSmatthias.ringwald err = hci_power_control_on(); 253597b61c7bS[email protected] if (err) { 2536f04a0c31SMatthias Ringwald log_error("hci_power_control_on() error %d", err); 253797b61c7bS[email protected] return err; 253897b61c7bS[email protected] } 253944935e40S[email protected] hci_power_transition_to_initializing(); 25408d213e1aSmatthias.ringwald break; 25418d213e1aSmatthias.ringwald case HCI_POWER_OFF: 25428d213e1aSmatthias.ringwald // do nothing 25438d213e1aSmatthias.ringwald break; 25448d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2545b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 25468d213e1aSmatthias.ringwald break; 25478d213e1aSmatthias.ringwald } 25488d213e1aSmatthias.ringwald break; 25497301ad89Smatthias.ringwald 25508d213e1aSmatthias.ringwald case HCI_STATE_INITIALIZING: 25518d213e1aSmatthias.ringwald switch (power_mode){ 25528d213e1aSmatthias.ringwald case HCI_POWER_ON: 25538d213e1aSmatthias.ringwald // do nothing 25548d213e1aSmatthias.ringwald break; 25558d213e1aSmatthias.ringwald case HCI_POWER_OFF: 25568d213e1aSmatthias.ringwald // no connections yet, just turn it off 25578d213e1aSmatthias.ringwald hci_power_control_off(); 25588d213e1aSmatthias.ringwald break; 25598d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2560b546ac54Smatthias.ringwald // no connections yet, just turn it off 256172ea5239Smatthias.ringwald hci_power_control_sleep(); 25628d213e1aSmatthias.ringwald break; 25638d213e1aSmatthias.ringwald } 25648d213e1aSmatthias.ringwald break; 25657301ad89Smatthias.ringwald 25668d213e1aSmatthias.ringwald case HCI_STATE_WORKING: 25678d213e1aSmatthias.ringwald switch (power_mode){ 25688d213e1aSmatthias.ringwald case HCI_POWER_ON: 25698d213e1aSmatthias.ringwald // do nothing 25708d213e1aSmatthias.ringwald break; 25718d213e1aSmatthias.ringwald case HCI_POWER_OFF: 2572c7e0c5f6Smatthias.ringwald // see hci_run 25733a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 25748d213e1aSmatthias.ringwald break; 25758d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2576b546ac54Smatthias.ringwald // see hci_run 25773a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 257874b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT; 25798d213e1aSmatthias.ringwald break; 25808d213e1aSmatthias.ringwald } 25818d213e1aSmatthias.ringwald break; 25827301ad89Smatthias.ringwald 25838d213e1aSmatthias.ringwald case HCI_STATE_HALTING: 25848d213e1aSmatthias.ringwald switch (power_mode){ 25858d213e1aSmatthias.ringwald case HCI_POWER_ON: 258644935e40S[email protected] hci_power_transition_to_initializing(); 25878d213e1aSmatthias.ringwald break; 25888d213e1aSmatthias.ringwald case HCI_POWER_OFF: 25898d213e1aSmatthias.ringwald // do nothing 25908d213e1aSmatthias.ringwald break; 25918d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2592b546ac54Smatthias.ringwald // see hci_run 25933a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 259474b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT; 25958d213e1aSmatthias.ringwald break; 25968d213e1aSmatthias.ringwald } 25978d213e1aSmatthias.ringwald break; 25988d213e1aSmatthias.ringwald 25998d213e1aSmatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 26008d213e1aSmatthias.ringwald switch (power_mode){ 26018d213e1aSmatthias.ringwald case HCI_POWER_ON: 260228171530Smatthias.ringwald 2603423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 260428171530Smatthias.ringwald // nothing to do, if H4 supports power management 2605d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 26063a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 260774b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_WRITE_SCAN_ENABLE; // init after sleep 260828171530Smatthias.ringwald break; 260928171530Smatthias.ringwald } 261028171530Smatthias.ringwald #endif 261144935e40S[email protected] hci_power_transition_to_initializing(); 26128d213e1aSmatthias.ringwald break; 26138d213e1aSmatthias.ringwald case HCI_POWER_OFF: 2614b546ac54Smatthias.ringwald // see hci_run 26153a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 26168d213e1aSmatthias.ringwald break; 26178d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2618b546ac54Smatthias.ringwald // do nothing 26198d213e1aSmatthias.ringwald break; 26208d213e1aSmatthias.ringwald } 26218d213e1aSmatthias.ringwald break; 26228d213e1aSmatthias.ringwald 26238d213e1aSmatthias.ringwald case HCI_STATE_SLEEPING: 26248d213e1aSmatthias.ringwald switch (power_mode){ 26258d213e1aSmatthias.ringwald case HCI_POWER_ON: 262628171530Smatthias.ringwald 2627423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 262828171530Smatthias.ringwald // nothing to do, if H4 supports power management 2629d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 26303a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 26315c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_AFTER_SLEEP; 2632758b46ceSmatthias.ringwald hci_update_scan_enable(); 263328171530Smatthias.ringwald break; 263428171530Smatthias.ringwald } 263528171530Smatthias.ringwald #endif 26363144bce4Smatthias.ringwald err = hci_power_control_wake(); 26373144bce4Smatthias.ringwald if (err) return err; 263844935e40S[email protected] hci_power_transition_to_initializing(); 26398d213e1aSmatthias.ringwald break; 26408d213e1aSmatthias.ringwald case HCI_POWER_OFF: 26413a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 26428d213e1aSmatthias.ringwald break; 26438d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2644b546ac54Smatthias.ringwald // do nothing 26458d213e1aSmatthias.ringwald break; 26468d213e1aSmatthias.ringwald } 26478d213e1aSmatthias.ringwald break; 264811e23e5fSmatthias.ringwald } 264968d92d03Smatthias.ringwald 2650038bc64cSmatthias.ringwald // create internal event 2651ee8bf225Smatthias.ringwald hci_emit_state(); 2652ee8bf225Smatthias.ringwald 265368d92d03Smatthias.ringwald // trigger next/first action 265468d92d03Smatthias.ringwald hci_run(); 265568d92d03Smatthias.ringwald 2656475c8125Smatthias.ringwald return 0; 2657475c8125Smatthias.ringwald } 2658475c8125Smatthias.ringwald 265935454696SMatthias Ringwald 266035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 266135454696SMatthias Ringwald 2662758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){ 2663758b46ceSmatthias.ringwald // 2 = page scan, 1 = inq scan 26643a9fb326S[email protected] hci_stack->new_scan_enable_value = hci_stack->connectable << 1 | hci_stack->discoverable; 2665758b46ceSmatthias.ringwald hci_run(); 2666758b46ceSmatthias.ringwald } 2667758b46ceSmatthias.ringwald 266815a95bd5SMatthias Ringwald void gap_discoverable_control(uint8_t enable){ 2669381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 2670381fbed8Smatthias.ringwald 26713a9fb326S[email protected] if (hci_stack->discoverable == enable){ 26723a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 2673381fbed8Smatthias.ringwald return; 2674381fbed8Smatthias.ringwald } 2675381fbed8Smatthias.ringwald 26763a9fb326S[email protected] hci_stack->discoverable = enable; 2677758b46ceSmatthias.ringwald hci_update_scan_enable(); 2678758b46ceSmatthias.ringwald } 2679b031bebbSmatthias.ringwald 268015a95bd5SMatthias Ringwald void gap_connectable_control(uint8_t enable){ 2681758b46ceSmatthias.ringwald if (enable) enable = 1; // normalize argument 2682758b46ceSmatthias.ringwald 2683758b46ceSmatthias.ringwald // don't emit event 26843a9fb326S[email protected] if (hci_stack->connectable == enable) return; 2685758b46ceSmatthias.ringwald 26863a9fb326S[email protected] hci_stack->connectable = enable; 2687758b46ceSmatthias.ringwald hci_update_scan_enable(); 2688381fbed8Smatthias.ringwald } 268935454696SMatthias Ringwald #endif 2690381fbed8Smatthias.ringwald 269115a95bd5SMatthias Ringwald void gap_local_bd_addr(bd_addr_t address_buffer){ 2692690bd0baS[email protected] memcpy(address_buffer, hci_stack->local_bd_addr, 6); 26935061f3afS[email protected] } 26945061f3afS[email protected] 26952b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 26962b838201SMatthias Ringwald static void hci_host_num_completed_packets(void){ 26972b838201SMatthias Ringwald 26982b838201SMatthias Ringwald // create packet manually as arrays are not supported and num_commands should not get reduced 26992b838201SMatthias Ringwald hci_reserve_packet_buffer(); 27002b838201SMatthias Ringwald uint8_t * packet = hci_get_outgoing_packet_buffer(); 27012b838201SMatthias Ringwald 27022b838201SMatthias Ringwald uint16_t size = 0; 27032b838201SMatthias Ringwald uint16_t num_handles = 0; 27042b838201SMatthias Ringwald packet[size++] = 0x35; 27052b838201SMatthias Ringwald packet[size++] = 0x0c; 27062b838201SMatthias Ringwald size++; // skip param len 27072b838201SMatthias Ringwald size++; // skip num handles 27082b838201SMatthias Ringwald 27092b838201SMatthias Ringwald // add { handle, packets } entries 27102b838201SMatthias Ringwald btstack_linked_item_t * it; 27112b838201SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 27122b838201SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 27132b838201SMatthias Ringwald if (connection->num_packets_completed){ 27142b838201SMatthias Ringwald little_endian_store_16(packet, size, connection->con_handle); 27152b838201SMatthias Ringwald size += 2; 27162b838201SMatthias Ringwald little_endian_store_16(packet, size, connection->num_packets_completed); 27172b838201SMatthias Ringwald size += 2; 27182b838201SMatthias Ringwald // 27192b838201SMatthias Ringwald num_handles++; 27202b838201SMatthias Ringwald connection->num_packets_completed = 0; 27212b838201SMatthias Ringwald } 27222b838201SMatthias Ringwald } 27232b838201SMatthias Ringwald 27242b838201SMatthias Ringwald packet[2] = size - 3; 27252b838201SMatthias Ringwald packet[3] = num_handles; 27262b838201SMatthias Ringwald 27272b838201SMatthias Ringwald hci_stack->host_completed_packets = 0; 27282b838201SMatthias Ringwald 27292b838201SMatthias Ringwald hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size); 27302b838201SMatthias Ringwald hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 27312b838201SMatthias Ringwald 27322b838201SMatthias Ringwald // release packet buffer for synchronous transport implementations 27332b838201SMatthias Ringwald if (hci_transport_synchronous()){ 27342b838201SMatthias Ringwald hci_stack->hci_packet_buffer_reserved = 0; 27352b838201SMatthias Ringwald } 27362b838201SMatthias Ringwald } 27372b838201SMatthias Ringwald #endif 27382b838201SMatthias Ringwald 273995d71764SMatthias Ringwald static void hci_run(void){ 27408a485f27Smatthias.ringwald 2741db8bc6ffSMatthias Ringwald // log_info("hci_run: entered"); 2742665d90f2SMatthias Ringwald btstack_linked_item_t * it; 274332ab9390Smatthias.ringwald 2744b5d8b22bS[email protected] // send continuation fragments first, as they block the prepared packet buffer 2745b5d8b22bS[email protected] if (hci_stack->acl_fragmentation_total_size > 0) { 2746b5d8b22bS[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer); 2747b5d8b22bS[email protected] hci_connection_t *connection = hci_connection_for_handle(con_handle); 2748b5d8b22bS[email protected] if (connection) { 274928a0332dSMatthias Ringwald if (hci_can_send_prepared_acl_packet_now(con_handle)){ 2750b5d8b22bS[email protected] hci_send_acl_packet_fragments(connection); 2751b5d8b22bS[email protected] return; 2752b5d8b22bS[email protected] } 275328a0332dSMatthias Ringwald } else { 2754b5d8b22bS[email protected] // connection gone -> discard further fragments 275528a0332dSMatthias Ringwald log_info("hci_run: fragmented ACL packet no connection -> discard fragment"); 2756b5d8b22bS[email protected] hci_stack->acl_fragmentation_total_size = 0; 2757b5d8b22bS[email protected] hci_stack->acl_fragmentation_pos = 0; 2758b5d8b22bS[email protected] } 2759b5d8b22bS[email protected] } 2760b5d8b22bS[email protected] 27612b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 27622b838201SMatthias Ringwald // send host num completed packets next as they don't require num_cmd_packets > 0 27632b838201SMatthias Ringwald if (!hci_can_send_comand_packet_transport()) return; 27642b838201SMatthias Ringwald if (hci_stack->host_completed_packets){ 27652b838201SMatthias Ringwald hci_host_num_completed_packets(); 27662b838201SMatthias Ringwald return; 27672b838201SMatthias Ringwald } 27682b838201SMatthias Ringwald #endif 27692b838201SMatthias Ringwald 2770d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 2771ce4c8fabSmatthias.ringwald 2772b031bebbSmatthias.ringwald // global/non-connection oriented commands 2773b031bebbSmatthias.ringwald 277435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2775b031bebbSmatthias.ringwald // decline incoming connections 27763a9fb326S[email protected] if (hci_stack->decline_reason){ 27773a9fb326S[email protected] uint8_t reason = hci_stack->decline_reason; 27783a9fb326S[email protected] hci_stack->decline_reason = 0; 27793a9fb326S[email protected] hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason); 2780dbe1a790S[email protected] return; 2781ce4c8fabSmatthias.ringwald } 2782b031bebbSmatthias.ringwald // send scan enable 27833a9fb326S[email protected] if (hci_stack->state == HCI_STATE_WORKING && hci_stack->new_scan_enable_value != 0xff && hci_classic_supported()){ 27843a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value); 27853a9fb326S[email protected] hci_stack->new_scan_enable_value = 0xff; 2786dbe1a790S[email protected] return; 2787b031bebbSmatthias.ringwald } 2788*f5875de5SMatthias Ringwald // start/stop inquiry 2789*f5875de5SMatthias Ringwald if (hci_stack->inquiry_state >= GAP_INQUIRY_DURATION_MIN && hci_stack->inquiry_state <= GAP_INQUIRY_DURATION_MAX){ 2790*f5875de5SMatthias Ringwald uint8_t duration = hci_stack->inquiry_state; 2791*f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_ACTIVE; 2792*f5875de5SMatthias Ringwald hci_send_cmd(&hci_inquiry, HCI_INQUIRY_LAP, duration, 0); 2793*f5875de5SMatthias Ringwald return; 2794*f5875de5SMatthias Ringwald } 2795*f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_W2_CANCEL){ 2796*f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W4_CANCELLED; 2797*f5875de5SMatthias Ringwald hci_send_cmd(&hci_inquiry_cancel); 2798*f5875de5SMatthias Ringwald return; 2799*f5875de5SMatthias Ringwald } 280035454696SMatthias Ringwald #endif 2801b031bebbSmatthias.ringwald 2802a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 2803b95a5a35SMatthias Ringwald // advertisements, active scanning, and creating connections requires randaom address to be set if using private address 2804b95a5a35SMatthias Ringwald if ((hci_stack->state == HCI_STATE_WORKING) 2805b95a5a35SMatthias Ringwald && (hci_stack->le_own_addr_type == BD_ADDR_TYPE_LE_PUBLIC || hci_stack->le_random_address_set)){ 2806d70217a2SMatthias Ringwald 2807d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 280845c102fdSMatthias Ringwald // handle le scan 28097bdc6798S[email protected] switch(hci_stack->le_scanning_state){ 28107bdc6798S[email protected] case LE_START_SCAN: 28117bdc6798S[email protected] hci_stack->le_scanning_state = LE_SCANNING; 28127bdc6798S[email protected] hci_send_cmd(&hci_le_set_scan_enable, 1, 0); 28137bdc6798S[email protected] return; 28147bdc6798S[email protected] 28157bdc6798S[email protected] case LE_STOP_SCAN: 28167bdc6798S[email protected] hci_stack->le_scanning_state = LE_SCAN_IDLE; 28177bdc6798S[email protected] hci_send_cmd(&hci_le_set_scan_enable, 0, 0); 28187bdc6798S[email protected] return; 28197bdc6798S[email protected] default: 28207bdc6798S[email protected] break; 28217bdc6798S[email protected] } 2822e2602ea2Smatthias.ringwald if (hci_stack->le_scan_type != 0xff){ 2823e2602ea2Smatthias.ringwald // defaults: active scanning, accept all advertisement packets 2824e2602ea2Smatthias.ringwald int scan_type = hci_stack->le_scan_type; 2825e2602ea2Smatthias.ringwald hci_stack->le_scan_type = 0xff; 2826b95a5a35SMatthias 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); 2827e2602ea2Smatthias.ringwald return; 2828e2602ea2Smatthias.ringwald } 2829d70217a2SMatthias Ringwald #endif 2830d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 283145c102fdSMatthias Ringwald // le advertisement control 28329a2e4658SMatthias Ringwald if (hci_stack->le_advertisements_todo){ 28339a2e4658SMatthias Ringwald log_info("hci_run: gap_le: adv todo: %x", hci_stack->le_advertisements_todo ); 28349a2e4658SMatthias Ringwald } 283545c102fdSMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_DISABLE){ 283645c102fdSMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_DISABLE; 283745c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 0); 283845c102fdSMatthias Ringwald return; 283945c102fdSMatthias Ringwald } 284045c102fdSMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_PARAMS){ 284145c102fdSMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_PARAMS; 284245c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_parameters, 284345c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_min, 284445c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_max, 284545c102fdSMatthias Ringwald hci_stack->le_advertisements_type, 2846b95a5a35SMatthias Ringwald hci_stack->le_own_addr_type, 284745c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address_type, 284845c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address, 284945c102fdSMatthias Ringwald hci_stack->le_advertisements_channel_map, 285045c102fdSMatthias Ringwald hci_stack->le_advertisements_filter_policy); 285145c102fdSMatthias Ringwald return; 285245c102fdSMatthias Ringwald } 2853501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_ADV_DATA){ 2854501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 2855b95a5a35SMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_data, hci_stack->le_advertisements_data_len, hci_stack->le_advertisements_data); 285645c102fdSMatthias Ringwald return; 285745c102fdSMatthias Ringwald } 2858501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA){ 2859501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 2860501f56b3SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_response_data, hci_stack->le_scan_response_data_len, 2861501f56b3SMatthias Ringwald hci_stack->le_scan_response_data); 2862501f56b3SMatthias Ringwald return; 2863501f56b3SMatthias Ringwald } 2864b95a5a35SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_ENABLE){ 286545c102fdSMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_ENABLE; 286645c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 1); 286745c102fdSMatthias Ringwald return; 286845c102fdSMatthias Ringwald } 2869d70217a2SMatthias Ringwald #endif 28709956955bSMatthias Ringwald 2871d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 28729956955bSMatthias Ringwald // 28739956955bSMatthias Ringwald // LE Whitelist Management 28749956955bSMatthias Ringwald // 28759956955bSMatthias Ringwald 28769956955bSMatthias Ringwald // check if whitelist needs modification 2877665d90f2SMatthias Ringwald btstack_linked_list_iterator_t lit; 28789956955bSMatthias Ringwald int modification_pending = 0; 2879665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 2880665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 2881665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 28829956955bSMatthias Ringwald if (entry->state & (LE_WHITELIST_REMOVE_FROM_CONTROLLER | LE_WHITELIST_ADD_TO_CONTROLLER)){ 28839956955bSMatthias Ringwald modification_pending = 1; 28849956955bSMatthias Ringwald break; 28859956955bSMatthias Ringwald } 28869956955bSMatthias Ringwald } 288791915b0bSMatthias Ringwald 28889956955bSMatthias Ringwald if (modification_pending){ 288991915b0bSMatthias Ringwald // stop connnecting if modification pending 28909956955bSMatthias Ringwald if (hci_stack->le_connecting_state != LE_CONNECTING_IDLE){ 28919956955bSMatthias Ringwald hci_send_cmd(&hci_le_create_connection_cancel); 28929956955bSMatthias Ringwald return; 28939956955bSMatthias Ringwald } 28949956955bSMatthias Ringwald 28959956955bSMatthias Ringwald // add/remove entries 2896665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 2897665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 2898665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 28999956955bSMatthias Ringwald if (entry->state & LE_WHITELIST_ADD_TO_CONTROLLER){ 29009956955bSMatthias Ringwald entry->state = LE_WHITELIST_ON_CONTROLLER; 29019956955bSMatthias Ringwald hci_send_cmd(&hci_le_add_device_to_white_list, entry->address_type, entry->address); 29029956955bSMatthias Ringwald return; 29039956955bSMatthias Ringwald 29049956955bSMatthias Ringwald } 29059956955bSMatthias Ringwald if (entry->state & LE_WHITELIST_REMOVE_FROM_CONTROLLER){ 29069ecbe201SMatthias Ringwald bd_addr_t address; 29079ecbe201SMatthias Ringwald bd_addr_type_t address_type = entry->address_type; 29089ecbe201SMatthias Ringwald memcpy(address, entry->address, 6); 2909665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry); 29109956955bSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 29119ecbe201SMatthias Ringwald hci_send_cmd(&hci_le_remove_device_from_white_list, address_type, address); 29129956955bSMatthias Ringwald return; 29139956955bSMatthias Ringwald } 29149956955bSMatthias Ringwald } 291591915b0bSMatthias Ringwald } 29169956955bSMatthias Ringwald 29179956955bSMatthias Ringwald // start connecting 291891915b0bSMatthias Ringwald if ( hci_stack->le_connecting_state == LE_CONNECTING_IDLE && 2919665d90f2SMatthias Ringwald !btstack_linked_list_empty(&hci_stack->le_whitelist)){ 29209956955bSMatthias Ringwald bd_addr_t null_addr; 29219956955bSMatthias Ringwald memset(null_addr, 0, 6); 29229956955bSMatthias Ringwald hci_send_cmd(&hci_le_create_connection, 29239956955bSMatthias Ringwald 0x0060, // scan interval: 60 ms 29249956955bSMatthias Ringwald 0x0030, // scan interval: 30 ms 29259956955bSMatthias Ringwald 1, // use whitelist 29269956955bSMatthias Ringwald 0, // peer address type 29279956955bSMatthias Ringwald null_addr, // peer bd addr 2928b95a5a35SMatthias Ringwald hci_stack->le_own_addr_type, // our addr type: 29299956955bSMatthias Ringwald 0x0008, // conn interval min 29309956955bSMatthias Ringwald 0x0018, // conn interval max 29319956955bSMatthias Ringwald 0, // conn latency 29329956955bSMatthias Ringwald 0x0048, // supervision timeout 29339956955bSMatthias Ringwald 0x0001, // min ce length 29349956955bSMatthias Ringwald 0x0001 // max ce length 29359956955bSMatthias Ringwald ); 29369956955bSMatthias Ringwald return; 29379956955bSMatthias Ringwald } 2938d70217a2SMatthias Ringwald #endif 29397bdc6798S[email protected] } 2940b2f949feS[email protected] #endif 29417bdc6798S[email protected] 294232ab9390Smatthias.ringwald // send pending HCI commands 2943665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 294405ae8de3SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 294532ab9390Smatthias.ringwald 29460bf6344aS[email protected] switch(connection->state){ 29470bf6344aS[email protected] case SEND_CREATE_CONNECTION: 29484f3229d8S[email protected] switch(connection->address_type){ 294935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 29504f3229d8S[email protected] case BD_ADDR_TYPE_CLASSIC: 29519da54300S[email protected] log_info("sending hci_create_connection"); 2952ad83dc6aS[email protected] hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 29534f3229d8S[email protected] break; 295435454696SMatthias Ringwald #endif 29554f3229d8S[email protected] default: 2956a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 2957d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 29589da54300S[email protected] log_info("sending hci_le_create_connection"); 29594f3229d8S[email protected] hci_send_cmd(&hci_le_create_connection, 2960b2571179Smatthias.ringwald 0x0060, // scan interval: 60 ms 2961b2571179Smatthias.ringwald 0x0030, // scan interval: 30 ms 29624f3229d8S[email protected] 0, // don't use whitelist 29634f3229d8S[email protected] connection->address_type, // peer address type 29644f3229d8S[email protected] connection->address, // peer bd addr 2965b95a5a35SMatthias Ringwald hci_stack->le_own_addr_type, // our addr type: 2966b2571179Smatthias.ringwald 0x0008, // conn interval min 2967b2571179Smatthias.ringwald 0x0018, // conn interval max 29684f3229d8S[email protected] 0, // conn latency 2969b2571179Smatthias.ringwald 0x0048, // supervision timeout 2970b2571179Smatthias.ringwald 0x0001, // min ce length 2971b2571179Smatthias.ringwald 0x0001 // max ce length 29724f3229d8S[email protected] ); 29734f3229d8S[email protected] 29744f3229d8S[email protected] connection->state = SENT_CREATE_CONNECTION; 2975b2f949feS[email protected] #endif 2976d70217a2SMatthias Ringwald #endif 29774f3229d8S[email protected] break; 29784f3229d8S[email protected] } 2979ad83dc6aS[email protected] return; 2980ad83dc6aS[email protected] 298135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 29820bf6344aS[email protected] case RECEIVED_CONNECTION_REQUEST: 298394e4aaa2SMatthias Ringwald log_info("sending hci_accept_connection_request, remote eSCO %u", connection->remote_supported_feature_eSCO); 298432ab9390Smatthias.ringwald connection->state = ACCEPTED_CONNECTION_REQUEST; 29855cf766e8SMatthias Ringwald connection->role = HCI_ROLE_SLAVE; 2986e35edcc1S[email protected] if (connection->address_type == BD_ADDR_TYPE_CLASSIC){ 298734d2123cS[email protected] hci_send_cmd(&hci_accept_connection_request, connection->address, 1); 2988e35edcc1S[email protected] } 2989dbe1a790S[email protected] return; 299035454696SMatthias Ringwald #endif 29910bf6344aS[email protected] 2992a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 2993d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 29940bf6344aS[email protected] case SEND_CANCEL_CONNECTION: 29950bf6344aS[email protected] connection->state = SENT_CANCEL_CONNECTION; 29960bf6344aS[email protected] hci_send_cmd(&hci_le_create_connection_cancel); 29970bf6344aS[email protected] return; 2998a6725849S[email protected] #endif 2999d70217a2SMatthias Ringwald #endif 30000bf6344aS[email protected] case SEND_DISCONNECT: 30010bf6344aS[email protected] connection->state = SENT_DISCONNECT; 30027851196eSmatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 30030bf6344aS[email protected] return; 30040bf6344aS[email protected] 30050bf6344aS[email protected] default: 30060bf6344aS[email protected] break; 3007c7e0c5f6Smatthias.ringwald } 3008c7e0c5f6Smatthias.ringwald 300935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 301032ab9390Smatthias.ringwald if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){ 30119da54300S[email protected] log_info("responding to link key request"); 301234d2123cS[email protected] connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST); 301332ab9390Smatthias.ringwald link_key_t link_key; 3014c77e8838S[email protected] link_key_type_t link_key_type; 3015a98592bcSMatthias Ringwald if ( hci_stack->link_key_db 3016a98592bcSMatthias Ringwald && hci_stack->link_key_db->get_link_key(connection->address, link_key, &link_key_type) 301734d2123cS[email protected] && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){ 30189ab95c90S[email protected] connection->link_key_type = link_key_type; 301932ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key); 302032ab9390Smatthias.ringwald } else { 302132ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 302232ab9390Smatthias.ringwald } 3023dbe1a790S[email protected] return; 302432ab9390Smatthias.ringwald } 30251d6b20aeS[email protected] 3026899283eaS[email protected] if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){ 30279da54300S[email protected] log_info("denying to pin request"); 3028899283eaS[email protected] connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST); 302934d2123cS[email protected] hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address); 30304c57c146S[email protected] return; 30314c57c146S[email protected] } 30324c57c146S[email protected] 3033dbe1a790S[email protected] if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){ 303434d2123cS[email protected] connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY); 303582d8f825S[email protected] log_info("IO Capability Request received, stack bondable %u, io cap %u", hci_stack->bondable, hci_stack->ssp_io_capability); 303682d8f825S[email protected] if (hci_stack->bondable && (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN)){ 3037106d6d11S[email protected] // tweak authentication requirements 30383a9fb326S[email protected] uint8_t authreq = hci_stack->ssp_authentication_requirement; 3039106d6d11S[email protected] if (connection->bonding_flags & BONDING_DEDICATED){ 30409faad3abS[email protected] authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 30419faad3abS[email protected] } 30429faad3abS[email protected] if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){ 30439faad3abS[email protected] authreq |= 1; 3044106d6d11S[email protected] } 30453a9fb326S[email protected] hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, NULL, authreq); 3046f8fb5f6eS[email protected] } else { 3047f8fb5f6eS[email protected] hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED); 3048f8fb5f6eS[email protected] } 3049dbe1a790S[email protected] return; 305032ab9390Smatthias.ringwald } 305132ab9390Smatthias.ringwald 3052dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){ 3053dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY); 305434d2123cS[email protected] hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address); 3055dbe1a790S[email protected] return; 3056dbe1a790S[email protected] } 3057dbe1a790S[email protected] 3058dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){ 3059dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY); 306034d2123cS[email protected] hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000); 3061dbe1a790S[email protected] return; 3062dbe1a790S[email protected] } 3063afd4e962S[email protected] 3064afd4e962S[email protected] if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){ 3065afd4e962S[email protected] connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES; 306634d2123cS[email protected] hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle); 30672bd8b7e7S[email protected] return; 30682bd8b7e7S[email protected] } 30692bd8b7e7S[email protected] 3070ad83dc6aS[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){ 3071ad83dc6aS[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE; 30721bd5283dS[email protected] connection->bonding_flags |= BONDING_EMIT_COMPLETE_ON_DISCONNECT; 307352d34f98S[email protected] hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // authentication done 3074ad83dc6aS[email protected] return; 3075ad83dc6aS[email protected] } 307676f27cffSMatthias Ringwald 307734d2123cS[email protected] if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){ 307834d2123cS[email protected] connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST; 307934d2123cS[email protected] hci_send_cmd(&hci_authentication_requested, connection->con_handle); 30802bd8b7e7S[email protected] return; 3081afd4e962S[email protected] } 308276f27cffSMatthias Ringwald 3083dce78009S[email protected] if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){ 3084dce78009S[email protected] connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST; 3085dce78009S[email protected] hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1); 3086dce78009S[email protected] return; 3087dce78009S[email protected] } 308876f27cffSMatthias Ringwald #endif 308976f27cffSMatthias Ringwald 309076f27cffSMatthias Ringwald if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){ 309176f27cffSMatthias Ringwald connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK; 309276f27cffSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005); // authentication failure 309376f27cffSMatthias Ringwald return; 309476f27cffSMatthias Ringwald } 3095da886c03S[email protected] 3096a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 3097da886c03S[email protected] if (connection->le_con_parameter_update_state == CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS){ 3098da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 3099da886c03S[email protected] 3100c37a3166S[email protected] uint16_t connection_interval_min = connection->le_conn_interval_min; 3101c37a3166S[email protected] connection->le_conn_interval_min = 0; 3102c37a3166S[email protected] hci_send_cmd(&hci_le_connection_update, connection->con_handle, connection_interval_min, 3103c37a3166S[email protected] connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout, 3104c37a3166S[email protected] 0x0000, 0xffff); 3105c37a3166S[email protected] } 3106c37a3166S[email protected] #endif 3107dbe1a790S[email protected] } 3108c7e0c5f6Smatthias.ringwald 310905ae8de3SMatthias Ringwald hci_connection_t * connection; 31103a9fb326S[email protected] switch (hci_stack->state){ 31113429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 311274b323a9SMatthias Ringwald hci_initializing_run(); 31133429f56bSmatthias.ringwald break; 3114c7e0c5f6Smatthias.ringwald 3115c7e0c5f6Smatthias.ringwald case HCI_STATE_HALTING: 3116c7e0c5f6Smatthias.ringwald 31179da54300S[email protected] log_info("HCI_STATE_HALTING"); 31189956955bSMatthias Ringwald 31199956955bSMatthias Ringwald // free whitelist entries 3120a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 3121d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 31229956955bSMatthias Ringwald { 3123665d90f2SMatthias Ringwald btstack_linked_list_iterator_t lit; 3124665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 3125665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 3126665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 3127665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry); 31289956955bSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 31299956955bSMatthias Ringwald } 31309956955bSMatthias Ringwald } 31319956955bSMatthias Ringwald #endif 3132d70217a2SMatthias Ringwald #endif 3133c7e0c5f6Smatthias.ringwald // close all open connections 31343a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 3135c7e0c5f6Smatthias.ringwald if (connection){ 3136711e6c80SMatthias Ringwald hci_con_handle_t con_handle = (uint16_t) connection->con_handle; 3137d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 313832ab9390Smatthias.ringwald 31398837e9efSMatthias Ringwald log_info("HCI_STATE_HALTING, connection %p, handle %u", connection, con_handle); 3140c7e0c5f6Smatthias.ringwald 31418837e9efSMatthias Ringwald // cancel all l2cap connections right away instead of waiting for disconnection complete event ... 31428837e9efSMatthias Ringwald hci_emit_disconnection_complete(con_handle, 0x16); // terminated by local host 31438837e9efSMatthias Ringwald 31448837e9efSMatthias Ringwald // ... which would be ignored anyway as we shutdown (free) the connection now 3145c7e0c5f6Smatthias.ringwald hci_shutdown_connection(connection); 31468837e9efSMatthias Ringwald 31478837e9efSMatthias Ringwald // finally, send the disconnect command 31488837e9efSMatthias Ringwald hci_send_cmd(&hci_disconnect, con_handle, 0x13); // remote closed connection 3149c7e0c5f6Smatthias.ringwald return; 3150c7e0c5f6Smatthias.ringwald } 31519da54300S[email protected] log_info("HCI_STATE_HALTING, calling off"); 3152c7e0c5f6Smatthias.ringwald 315372ea5239Smatthias.ringwald // switch mode 3154c7e0c5f6Smatthias.ringwald hci_power_control_off(); 31559418f9c9Smatthias.ringwald 31569da54300S[email protected] log_info("HCI_STATE_HALTING, emitting state"); 315772ea5239Smatthias.ringwald hci_emit_state(); 31589da54300S[email protected] log_info("HCI_STATE_HALTING, done"); 315972ea5239Smatthias.ringwald break; 3160c7e0c5f6Smatthias.ringwald 316172ea5239Smatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 31623a9fb326S[email protected] switch(hci_stack->substate) { 316374b323a9SMatthias Ringwald case HCI_FALLING_ASLEEP_DISCONNECT: 31649da54300S[email protected] log_info("HCI_STATE_FALLING_ASLEEP"); 316572ea5239Smatthias.ringwald // close all open connections 31663a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 316766da7044Smatthias.ringwald 3168423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 316966da7044Smatthias.ringwald // don't close connections, if H4 supports power management 3170d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 317166da7044Smatthias.ringwald connection = NULL; 317266da7044Smatthias.ringwald } 317366da7044Smatthias.ringwald #endif 317472ea5239Smatthias.ringwald if (connection){ 317532ab9390Smatthias.ringwald 317672ea5239Smatthias.ringwald // send disconnect 3177d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 317832ab9390Smatthias.ringwald 31799da54300S[email protected] log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u", connection, (uint16_t)connection->con_handle); 31806ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 318172ea5239Smatthias.ringwald 318272ea5239Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 318372ea5239Smatthias.ringwald hci_shutdown_connection(connection); 318472ea5239Smatthias.ringwald return; 318572ea5239Smatthias.ringwald } 318672ea5239Smatthias.ringwald 318792368cd3S[email protected] if (hci_classic_supported()){ 318889db417bSmatthias.ringwald // disable page and inquiry scan 3189d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 319032ab9390Smatthias.ringwald 31919da54300S[email protected] log_info("HCI_STATE_HALTING, disabling inq scans"); 31923a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan 319389db417bSmatthias.ringwald 319489db417bSmatthias.ringwald // continue in next sub state 319574b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE; 319689db417bSmatthias.ringwald break; 319792368cd3S[email protected] } 3198202c8a4cSMatthias Ringwald // no break - fall through for ble-only chips 319992368cd3S[email protected] 320074b323a9SMatthias Ringwald case HCI_FALLING_ASLEEP_COMPLETE: 32019da54300S[email protected] log_info("HCI_STATE_HALTING, calling sleep"); 3202423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 320328171530Smatthias.ringwald // don't actually go to sleep, if H4 supports power management 3204d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 320528171530Smatthias.ringwald // SLEEP MODE reached 32063a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 320728171530Smatthias.ringwald hci_emit_state(); 320828171530Smatthias.ringwald break; 320928171530Smatthias.ringwald } 321028171530Smatthias.ringwald #endif 321172ea5239Smatthias.ringwald // switch mode 32123a9fb326S[email protected] hci_power_control_sleep(); // changes hci_stack->state to SLEEP 3213c7e0c5f6Smatthias.ringwald hci_emit_state(); 321428171530Smatthias.ringwald break; 321528171530Smatthias.ringwald 321689db417bSmatthias.ringwald default: 321789db417bSmatthias.ringwald break; 321889db417bSmatthias.ringwald } 3219c7e0c5f6Smatthias.ringwald break; 3220c7e0c5f6Smatthias.ringwald 32213429f56bSmatthias.ringwald default: 32223429f56bSmatthias.ringwald break; 32231f504dbdSmatthias.ringwald } 32243429f56bSmatthias.ringwald } 322516833f0aSmatthias.ringwald 322631452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 322735454696SMatthias Ringwald // house-keeping 322835454696SMatthias Ringwald 322935454696SMatthias Ringwald if (IS_COMMAND(packet, hci_write_loopback_mode)){ 323035454696SMatthias Ringwald hci_stack->loopback_mode = packet[3]; 323135454696SMatthias Ringwald } 323235454696SMatthias Ringwald 323335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 3234c8e4258aSmatthias.ringwald bd_addr_t addr; 3235c8e4258aSmatthias.ringwald hci_connection_t * conn; 3236c8e4258aSmatthias.ringwald 3237c8e4258aSmatthias.ringwald // create_connection? 3238c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 3239724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 32409da54300S[email protected] log_info("Create_connection to %s", bd_addr_to_str(addr)); 3241c8e4258aSmatthias.ringwald 32422e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 3243ad83dc6aS[email protected] if (!conn){ 324496a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 324517f1ba2aSmatthias.ringwald if (!conn){ 324617f1ba2aSmatthias.ringwald // notify client that alloc failed 32472deddeceSMatthias Ringwald hci_emit_connection_complete(addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 324817f1ba2aSmatthias.ringwald return 0; // don't sent packet to controller 324917f1ba2aSmatthias.ringwald } 3250ad83dc6aS[email protected] conn->state = SEND_CREATE_CONNECTION; 3251ad83dc6aS[email protected] } 3252ad83dc6aS[email protected] log_info("conn state %u", conn->state); 3253ad83dc6aS[email protected] switch (conn->state){ 3254ad83dc6aS[email protected] // if connection active exists 3255ad83dc6aS[email protected] case OPEN: 325662bda3fbS[email protected] // and OPEN, emit connection complete command, don't send to controller 3257274dad91SMatthias Ringwald hci_emit_connection_complete(addr, conn->con_handle, 0); 325862bda3fbS[email protected] return 0; 3259ad83dc6aS[email protected] case SEND_CREATE_CONNECTION: 3260ad83dc6aS[email protected] // connection created by hci, e.g. dedicated bonding 3261ad83dc6aS[email protected] break; 3262ad83dc6aS[email protected] default: 3263ad83dc6aS[email protected] // otherwise, just ignore as it is already in the open process 3264ad83dc6aS[email protected] return 0; 3265ad83dc6aS[email protected] } 3266c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 3267c8e4258aSmatthias.ringwald } 326835454696SMatthias Ringwald 32697fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_reply)){ 32707fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY); 32717fde4af9Smatthias.ringwald } 32727fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){ 32737fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST); 32747fde4af9Smatthias.ringwald } 32757fde4af9Smatthias.ringwald 32768ef73945Smatthias.ringwald if (IS_COMMAND(packet, hci_delete_stored_link_key)){ 3277a98592bcSMatthias Ringwald if (hci_stack->link_key_db){ 3278724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 3279a98592bcSMatthias Ringwald hci_stack->link_key_db->delete_link_key(addr); 32808ef73945Smatthias.ringwald } 32818ef73945Smatthias.ringwald } 3282c8e4258aSmatthias.ringwald 32836724cd9eS[email protected] if (IS_COMMAND(packet, hci_pin_code_request_negative_reply) 32846724cd9eS[email protected] || IS_COMMAND(packet, hci_pin_code_request_reply)){ 3285724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 32862e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 32876724cd9eS[email protected] if (conn){ 32886724cd9eS[email protected] connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE); 32896724cd9eS[email protected] } 32906724cd9eS[email protected] } 32916724cd9eS[email protected] 32926724cd9eS[email protected] if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply) 32936724cd9eS[email protected] || IS_COMMAND(packet, hci_user_confirmation_request_reply) 32946724cd9eS[email protected] || IS_COMMAND(packet, hci_user_passkey_request_negative_reply) 32956724cd9eS[email protected] || IS_COMMAND(packet, hci_user_passkey_request_reply)) { 3296724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 32972e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 32986724cd9eS[email protected] if (conn){ 32996724cd9eS[email protected] connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE); 33006724cd9eS[email protected] } 33016724cd9eS[email protected] } 3302ee752bb8SMatthias Ringwald 3303ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 3304ee752bb8SMatthias Ringwald // setup_synchronous_connection? Voice setting at offset 22 3305ee752bb8SMatthias Ringwald if (IS_COMMAND(packet, hci_setup_synchronous_connection)){ 3306ee752bb8SMatthias Ringwald // TODO: compare to current setting if sco connection already active 3307ee752bb8SMatthias Ringwald hci_stack->sco_voice_setting_active = little_endian_read_16(packet, 15); 3308ee752bb8SMatthias Ringwald } 3309ee752bb8SMatthias Ringwald // accept_synchronus_connection? Voice setting at offset 18 3310ee752bb8SMatthias Ringwald if (IS_COMMAND(packet, hci_accept_synchronous_connection)){ 3311ee752bb8SMatthias Ringwald // TODO: compare to current setting if sco connection already active 3312ee752bb8SMatthias Ringwald hci_stack->sco_voice_setting_active = little_endian_read_16(packet, 19); 3313ee752bb8SMatthias Ringwald } 3314ee752bb8SMatthias Ringwald #endif 331535454696SMatthias Ringwald #endif 33164b3e1e19SMatthias Ringwald 3317a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 3318d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 331969a97523S[email protected] if (IS_COMMAND(packet, hci_le_set_random_address)){ 3320b95a5a35SMatthias Ringwald hci_stack->le_random_address_set = 1; 3321b95a5a35SMatthias Ringwald reverse_bd_addr(&packet[3], hci_stack->le_random_address); 3322d70217a2SMatthias Ringwald } 3323171293d3SMatthias Ringwald if (IS_COMMAND(packet, hci_le_set_advertise_enable)){ 3324171293d3SMatthias Ringwald hci_stack->le_advertisements_active = packet[3]; 3325171293d3SMatthias Ringwald } 3326d70217a2SMatthias Ringwald #endif 3327d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 3328b04dfa37SMatthias Ringwald if (IS_COMMAND(packet, hci_le_create_connection)){ 3329b04dfa37SMatthias Ringwald // white list used? 3330b04dfa37SMatthias Ringwald uint8_t initiator_filter_policy = packet[7]; 3331b04dfa37SMatthias Ringwald switch (initiator_filter_policy){ 3332b04dfa37SMatthias Ringwald case 0: 3333b04dfa37SMatthias Ringwald // whitelist not used 3334b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_DIRECT; 3335b04dfa37SMatthias Ringwald break; 3336b04dfa37SMatthias Ringwald case 1: 3337b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_WHITELIST; 3338b04dfa37SMatthias Ringwald break; 3339b04dfa37SMatthias Ringwald default: 3340b04dfa37SMatthias Ringwald log_error("Invalid initiator_filter_policy in LE Create Connection %u", initiator_filter_policy); 3341b04dfa37SMatthias Ringwald break; 3342b04dfa37SMatthias Ringwald } 3343b04dfa37SMatthias Ringwald } 3344b04dfa37SMatthias Ringwald if (IS_COMMAND(packet, hci_le_create_connection_cancel)){ 3345b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 3346b04dfa37SMatthias Ringwald } 334769a97523S[email protected] #endif 3348d70217a2SMatthias Ringwald #endif 334969a97523S[email protected] 33503a9fb326S[email protected] hci_stack->num_cmd_packets--; 33515bb5bc3eS[email protected] 33525bb5bc3eS[email protected] hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size); 33536b4af23dS[email protected] int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 33546b4af23dS[email protected] 3355d051460cS[email protected] // release packet buffer for synchronous transport implementations 3356c8b9416aS[email protected] if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){ 33576b4af23dS[email protected] hci_stack->hci_packet_buffer_reserved = 0; 33586b4af23dS[email protected] } 33596b4af23dS[email protected] 33606b4af23dS[email protected] return err; 336131452debSmatthias.ringwald } 33628adf0ddaSmatthias.ringwald 33632bd8b7e7S[email protected] // disconnect because of security block 33642bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){ 33652bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 33662bd8b7e7S[email protected] if (!connection) return; 33672bd8b7e7S[email protected] connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 33682bd8b7e7S[email protected] } 33692bd8b7e7S[email protected] 33702bd8b7e7S[email protected] 3371dbe1a790S[email protected] // Configure Secure Simple Pairing 3372dbe1a790S[email protected] 337335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 337435454696SMatthias Ringwald 3375dbe1a790S[email protected] // enable will enable SSP during init 337615a95bd5SMatthias Ringwald void gap_ssp_set_enable(int enable){ 33773a9fb326S[email protected] hci_stack->ssp_enable = enable; 3378dbe1a790S[email protected] } 3379dbe1a790S[email protected] 338095d71764SMatthias Ringwald static int hci_local_ssp_activated(void){ 338115a95bd5SMatthias Ringwald return gap_ssp_supported() && hci_stack->ssp_enable; 33822bd8b7e7S[email protected] } 33832bd8b7e7S[email protected] 3384dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement 338515a95bd5SMatthias Ringwald void gap_ssp_set_io_capability(int io_capability){ 33863a9fb326S[email protected] hci_stack->ssp_io_capability = io_capability; 3387dbe1a790S[email protected] } 338815a95bd5SMatthias Ringwald void gap_ssp_set_authentication_requirement(int authentication_requirement){ 33893a9fb326S[email protected] hci_stack->ssp_authentication_requirement = authentication_requirement; 3390dbe1a790S[email protected] } 3391dbe1a790S[email protected] 3392dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested 339315a95bd5SMatthias Ringwald void gap_ssp_set_auto_accept(int auto_accept){ 33943a9fb326S[email protected] hci_stack->ssp_auto_accept = auto_accept; 3395dbe1a790S[email protected] } 339635454696SMatthias Ringwald #endif 3397dbe1a790S[email protected] 339894be1a66SMatthias Ringwald // va_list part of hci_send_cmd 339994be1a66SMatthias Ringwald int hci_send_cmd_va_arg(const hci_cmd_t *cmd, va_list argptr){ 3400d94d3cafS[email protected] if (!hci_can_send_command_packet_now()){ 34019d14b626S[email protected] log_error("hci_send_cmd called but cannot send packet now"); 34029d14b626S[email protected] return 0; 34039d14b626S[email protected] } 34049d14b626S[email protected] 34055127cc62S[email protected] // for HCI INITIALIZATION 34069da54300S[email protected] // log_info("hci_send_cmd: opcode %04x", cmd->opcode); 34075127cc62S[email protected] hci_stack->last_cmd_opcode = cmd->opcode; 34085127cc62S[email protected] 34099d14b626S[email protected] hci_reserve_packet_buffer(); 34109d14b626S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 341194be1a66SMatthias Ringwald uint16_t size = hci_cmd_create_from_template(packet, cmd, argptr); 341294be1a66SMatthias Ringwald return hci_send_cmd_packet(packet, size); 341394be1a66SMatthias Ringwald } 34149d14b626S[email protected] 341594be1a66SMatthias Ringwald /** 341694be1a66SMatthias Ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 341794be1a66SMatthias Ringwald */ 341894be1a66SMatthias Ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){ 34191cd208adSmatthias.ringwald va_list argptr; 34201cd208adSmatthias.ringwald va_start(argptr, cmd); 342194be1a66SMatthias Ringwald int res = hci_send_cmd_va_arg(cmd, argptr); 34221cd208adSmatthias.ringwald va_end(argptr); 342394be1a66SMatthias Ringwald return res; 342493b8dc03Smatthias.ringwald } 3425c8e4258aSmatthias.ringwald 3426ee091cf1Smatthias.ringwald // Create various non-HCI events. 3427ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 3428ee091cf1Smatthias.ringwald 3429d6b06661SMatthias Ringwald static void hci_emit_event(uint8_t * event, uint16_t size, int dump){ 3430fb37a842SMatthias Ringwald // dump packet 3431d6b06661SMatthias Ringwald if (dump) { 3432300c1ba4SMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, size); 3433d6b06661SMatthias Ringwald } 34341ef6bb52SMatthias Ringwald 3435fb37a842SMatthias Ringwald // dispatch to all event handlers 34361ef6bb52SMatthias Ringwald btstack_linked_list_iterator_t it; 34371ef6bb52SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->event_handlers); 34381ef6bb52SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 34391ef6bb52SMatthias Ringwald btstack_packet_callback_registration_t * entry = (btstack_packet_callback_registration_t*) btstack_linked_list_iterator_next(&it); 3440d9a7306aSMatthias Ringwald entry->callback(HCI_EVENT_PACKET, 0, event, size); 34411ef6bb52SMatthias Ringwald } 3442d6b06661SMatthias Ringwald } 3443d6b06661SMatthias Ringwald 3444d6b06661SMatthias Ringwald static void hci_emit_acl_packet(uint8_t * packet, uint16_t size){ 3445fb37a842SMatthias Ringwald if (!hci_stack->acl_packet_handler) return; 34463d50b4baSMatthias Ringwald hci_stack->acl_packet_handler(HCI_ACL_DATA_PACKET, 0, packet, size); 3447d6b06661SMatthias Ringwald } 3448d6b06661SMatthias Ringwald 344935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 3450701e3307SMatthias Ringwald static void hci_notify_if_sco_can_send_now(void){ 34513bc639ceSMatthias Ringwald // notify SCO sender if waiting 3452701e3307SMatthias Ringwald if (!hci_stack->sco_waiting_for_can_send_now) return; 3453701e3307SMatthias Ringwald if (hci_can_send_sco_packet_now()){ 34543bc639ceSMatthias Ringwald hci_stack->sco_waiting_for_can_send_now = 0; 3455701e3307SMatthias Ringwald uint8_t event[2] = { HCI_EVENT_SCO_CAN_SEND_NOW, 0 }; 3456701e3307SMatthias Ringwald hci_dump_packet(HCI_EVENT_PACKET, 1, event, sizeof(event)); 34573d50b4baSMatthias Ringwald hci_stack->sco_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event)); 34583bc639ceSMatthias Ringwald } 34593bc639ceSMatthias Ringwald } 34601cfb383eSMatthias Ringwald 34611cfb383eSMatthias Ringwald // parsing end emitting has been merged to reduce code size 34621cfb383eSMatthias Ringwald static void gap_inquiry_explode(uint8_t * packet){ 34631cfb383eSMatthias Ringwald uint8_t event[15+GAP_INQUIRY_MAX_NAME_LEN]; 34641cfb383eSMatthias Ringwald 34651cfb383eSMatthias Ringwald uint8_t * eir_data; 34661cfb383eSMatthias Ringwald ad_context_t context; 34671cfb383eSMatthias Ringwald const uint8_t * name; 34681cfb383eSMatthias Ringwald uint8_t name_len; 34691cfb383eSMatthias Ringwald 34701cfb383eSMatthias Ringwald int event_type = hci_event_packet_get_type(packet); 34711cfb383eSMatthias Ringwald int num_reserved_fields = event_type == HCI_EVENT_INQUIRY_RESULT ? 2 : 1; // 2 for old event, 1 otherwise 34721cfb383eSMatthias Ringwald int num_responses = hci_event_inquiry_result_get_num_responses(packet); 34731cfb383eSMatthias Ringwald 34741cfb383eSMatthias Ringwald // event[1] is set at the end 34751cfb383eSMatthias Ringwald int i; 34761cfb383eSMatthias Ringwald for (i=0; i<num_responses;i++){ 34771cfb383eSMatthias Ringwald memset(event, 0, sizeof(event)); 34781cfb383eSMatthias Ringwald event[0] = GAP_EVENT_INQUIRY_RESULT; 34791cfb383eSMatthias Ringwald uint8_t event_size = 18; // if name is not set by EIR 34801cfb383eSMatthias Ringwald 34811cfb383eSMatthias Ringwald memcpy(&event[2], &packet[3 + i*6], 6); // bd_addr 34821cfb383eSMatthias Ringwald event[8] = packet[3 + num_responses*(6) + i*1]; // page_scan_repetition_mode 34831cfb383eSMatthias Ringwald memcpy(&event[9], &packet[3 + num_responses*(6+1+num_reserved_fields) + i*3], 3); // class of device 34841cfb383eSMatthias Ringwald memcpy(&event[12], &packet[3 + num_responses*(6+1+num_reserved_fields+3) + i*2], 2); // clock offset 34851cfb383eSMatthias Ringwald 34861cfb383eSMatthias Ringwald switch (event_type){ 34871cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 34881cfb383eSMatthias Ringwald // 14,15,16,17 = 0, size 18 34891cfb383eSMatthias Ringwald break; 34901cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 34911cfb383eSMatthias Ringwald event[14] = 1; 34921cfb383eSMatthias Ringwald event[15] = packet [3 + num_responses*(6+1+num_reserved_fields+3+2) + i*1]; // rssi 34931cfb383eSMatthias Ringwald // 16,17 = 0, size 18 34941cfb383eSMatthias Ringwald break; 34951cfb383eSMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 34961cfb383eSMatthias Ringwald event[14] = 1; 34971cfb383eSMatthias Ringwald event[15] = packet [3 + num_responses*(6+1+num_reserved_fields+3+2) + i*1]; // rssi 34981cfb383eSMatthias Ringwald // for EIR packets, there is only one reponse in it 34991cfb383eSMatthias Ringwald eir_data = &packet[3 + (6+1+num_reserved_fields+3+2+1)]; 35001cfb383eSMatthias Ringwald name = NULL; 35011cfb383eSMatthias Ringwald // EIR data is 240 bytes in EIR event 35021cfb383eSMatthias Ringwald for (ad_iterator_init(&context, 240, eir_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)){ 35031cfb383eSMatthias Ringwald uint8_t data_type = ad_iterator_get_data_type(&context); 35041cfb383eSMatthias Ringwald uint8_t data_size = ad_iterator_get_data_len(&context); 35051cfb383eSMatthias Ringwald const uint8_t * data = ad_iterator_get_data(&context); 35061cfb383eSMatthias Ringwald // Prefer Complete Local Name over Shortend Local Name 35071cfb383eSMatthias Ringwald switch (data_type){ 35081cfb383eSMatthias Ringwald case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME: 35091cfb383eSMatthias Ringwald if (name) continue; 35101cfb383eSMatthias Ringwald /* explicit fall-through */ 35111cfb383eSMatthias Ringwald case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME: 35121cfb383eSMatthias Ringwald name = data; 35131cfb383eSMatthias Ringwald name_len = data_size; 35141cfb383eSMatthias Ringwald break; 35151cfb383eSMatthias Ringwald default: 35161cfb383eSMatthias Ringwald break; 35171cfb383eSMatthias Ringwald } 35181cfb383eSMatthias Ringwald } 35191cfb383eSMatthias Ringwald if (name){ 35201cfb383eSMatthias Ringwald event[16] = 1; 35211cfb383eSMatthias Ringwald // truncate name if needed 35221cfb383eSMatthias Ringwald int len = btstack_min(name_len, GAP_INQUIRY_MAX_NAME_LEN); 35231cfb383eSMatthias Ringwald event[17] = len; 35241cfb383eSMatthias Ringwald memcpy(&event[18], name, len); 35251cfb383eSMatthias Ringwald event_size += len; 35261cfb383eSMatthias Ringwald } 35271cfb383eSMatthias Ringwald break; 35281cfb383eSMatthias Ringwald } 35291cfb383eSMatthias Ringwald event[1] = event_size - 2; 35301cfb383eSMatthias Ringwald hci_emit_event(event, event_size, 1); 35311cfb383eSMatthias Ringwald } 35321cfb383eSMatthias Ringwald } 353335454696SMatthias Ringwald #endif 35343bc639ceSMatthias Ringwald 353571de195eSMatthias Ringwald void hci_emit_state(void){ 35363a9fb326S[email protected] log_info("BTSTACK_EVENT_STATE %u", hci_stack->state); 3537425d1371Smatthias.ringwald uint8_t event[3]; 353880d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 3539425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 35403a9fb326S[email protected] event[2] = hci_stack->state; 3541d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3542c8e4258aSmatthias.ringwald } 3543c8e4258aSmatthias.ringwald 354435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 35452deddeceSMatthias Ringwald static void hci_emit_connection_complete(bd_addr_t address, hci_con_handle_t con_handle, uint8_t status){ 3546425d1371Smatthias.ringwald uint8_t event[13]; 3547c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 3548425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 354917f1ba2aSmatthias.ringwald event[2] = status; 35502deddeceSMatthias Ringwald little_endian_store_16(event, 3, con_handle); 35512deddeceSMatthias Ringwald reverse_bd_addr(address, &event[5]); 3552c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 3553c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 3554d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3555c8e4258aSmatthias.ringwald } 355652db98b2SMatthias Ringwald static void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 355752db98b2SMatthias Ringwald if (disable_l2cap_timeouts) return; 355852db98b2SMatthias Ringwald log_info("L2CAP_EVENT_TIMEOUT_CHECK"); 355952db98b2SMatthias Ringwald uint8_t event[4]; 356052db98b2SMatthias Ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 356152db98b2SMatthias Ringwald event[1] = sizeof(event) - 2; 356252db98b2SMatthias Ringwald little_endian_store_16(event, 2, conn->con_handle); 356352db98b2SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 356452db98b2SMatthias Ringwald } 356535454696SMatthias Ringwald #endif 3566c8e4258aSmatthias.ringwald 356735454696SMatthias Ringwald #ifdef ENABLE_BLE 3568d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 3569fc64f94aSMatthias 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){ 35704f3229d8S[email protected] uint8_t event[21]; 35714f3229d8S[email protected] event[0] = HCI_EVENT_LE_META; 35724f3229d8S[email protected] event[1] = sizeof(event) - 2; 35734f3229d8S[email protected] event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE; 35744f3229d8S[email protected] event[3] = status; 3575fc64f94aSMatthias Ringwald little_endian_store_16(event, 4, con_handle); 35764f3229d8S[email protected] event[6] = 0; // TODO: role 35776e2e9a6bS[email protected] event[7] = address_type; 3578724d70a2SMatthias Ringwald reverse_bd_addr(address, &event[8]); 3579f8fbdce0SMatthias Ringwald little_endian_store_16(event, 14, 0); // interval 3580f8fbdce0SMatthias Ringwald little_endian_store_16(event, 16, 0); // latency 3581f8fbdce0SMatthias Ringwald little_endian_store_16(event, 18, 0); // supervision timeout 35824f3229d8S[email protected] event[20] = 0; // master clock accuracy 3583d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 35844f3229d8S[email protected] } 358535454696SMatthias Ringwald #endif 3586d70217a2SMatthias Ringwald #endif 35874f3229d8S[email protected] 3588fc64f94aSMatthias Ringwald static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason){ 3589425d1371Smatthias.ringwald uint8_t event[6]; 35903c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 3591e518c4b8Smatthias.ringwald event[1] = sizeof(event) - 2; 35923c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 3593fc64f94aSMatthias Ringwald little_endian_store_16(event, 3, con_handle); 35943c4d4b90Smatthias.ringwald event[5] = reason; 3595d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 35963c4d4b90Smatthias.ringwald } 35973c4d4b90Smatthias.ringwald 3598b83d5eabSMatthias Ringwald static void hci_emit_nr_connections_changed(void){ 3599e0abb8e7S[email protected] log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections()); 3600425d1371Smatthias.ringwald uint8_t event[3]; 360180d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 3602425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 360343bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 3604d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 360543bfb1bdSmatthias.ringwald } 3606038bc64cSmatthias.ringwald 3607b83d5eabSMatthias Ringwald static void hci_emit_hci_open_failed(void){ 3608e0abb8e7S[email protected] log_info("BTSTACK_EVENT_POWERON_FAILED"); 3609425d1371Smatthias.ringwald uint8_t event[2]; 361080d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 3611425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 3612d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3613038bc64cSmatthias.ringwald } 36141b0e3922Smatthias.ringwald 361535454696SMatthias Ringwald static void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status){ 361635454696SMatthias Ringwald log_info("hci_emit_dedicated_bonding_result %u ", status); 361735454696SMatthias Ringwald uint8_t event[9]; 361835454696SMatthias Ringwald int pos = 0; 361935454696SMatthias Ringwald event[pos++] = GAP_EVENT_DEDICATED_BONDING_COMPLETED; 362035454696SMatthias Ringwald event[pos++] = sizeof(event) - 2; 362135454696SMatthias Ringwald event[pos++] = status; 362235454696SMatthias Ringwald reverse_bd_addr(address, &event[pos]); 3623d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3624381fbed8Smatthias.ringwald } 3625458bf4e8S[email protected] 362635454696SMatthias Ringwald 362735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 362835454696SMatthias Ringwald 3629b83d5eabSMatthias Ringwald static void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){ 3630df3354fcS[email protected] log_info("hci_emit_security_level %u for handle %x", level, con_handle); 3631a00031e2S[email protected] uint8_t event[5]; 3632e00caf9cS[email protected] int pos = 0; 36335611a760SMatthias Ringwald event[pos++] = GAP_EVENT_SECURITY_LEVEL; 3634e00caf9cS[email protected] event[pos++] = sizeof(event) - 2; 3635f8fbdce0SMatthias Ringwald little_endian_store_16(event, 2, con_handle); 3636e00caf9cS[email protected] pos += 2; 3637e00caf9cS[email protected] event[pos++] = level; 3638d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3639e00caf9cS[email protected] } 3640e00caf9cS[email protected] 364135454696SMatthias Ringwald static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){ 364235454696SMatthias Ringwald if (!connection) return LEVEL_0; 364335454696SMatthias Ringwald if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0; 364435454696SMatthias Ringwald return gap_security_level_for_link_key_type(connection->link_key_type); 364535454696SMatthias Ringwald } 364635454696SMatthias Ringwald 364735454696SMatthias Ringwald static void hci_emit_discoverable_enabled(uint8_t enabled){ 364835454696SMatthias Ringwald log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled); 364935454696SMatthias Ringwald uint8_t event[3]; 365035454696SMatthias Ringwald event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED; 365135454696SMatthias Ringwald event[1] = sizeof(event) - 2; 365235454696SMatthias Ringwald event[2] = enabled; 3653d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3654ad83dc6aS[email protected] } 3655ad83dc6aS[email protected] 365606b9e820SMatthias Ringwald #ifdef ENABLE_CLASSIC 365798a2fd1cSMatthias Ringwald // query if remote side supports eSCO 3658073bd0faSMatthias Ringwald int hci_remote_esco_supported(hci_con_handle_t con_handle){ 365998a2fd1cSMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 366098a2fd1cSMatthias Ringwald if (!connection) return 0; 366198a2fd1cSMatthias Ringwald return connection->remote_supported_feature_eSCO; 366298a2fd1cSMatthias Ringwald } 366398a2fd1cSMatthias Ringwald 36642bd8b7e7S[email protected] // query if remote side supports SSP 36652bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){ 36662bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 36672bd8b7e7S[email protected] if (!connection) return 0; 36682bd8b7e7S[email protected] return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0; 36692bd8b7e7S[email protected] } 36702bd8b7e7S[email protected] 367115a95bd5SMatthias Ringwald int gap_ssp_supported_on_both_sides(hci_con_handle_t handle){ 3672df3354fcS[email protected] return hci_local_ssp_activated() && hci_remote_ssp_supported(handle); 3673df3354fcS[email protected] } 367406b9e820SMatthias Ringwald #endif 3675df3354fcS[email protected] 3676458bf4e8S[email protected] // GAP API 3677458bf4e8S[email protected] /** 3678458bf4e8S[email protected] * @bbrief enable/disable bonding. default is enabled 3679458bf4e8S[email protected] * @praram enabled 3680458bf4e8S[email protected] */ 36814c57c146S[email protected] void gap_set_bondable_mode(int enable){ 36823a9fb326S[email protected] hci_stack->bondable = enable ? 1 : 0; 3683458bf4e8S[email protected] } 36844ef6443cSMatthias Ringwald /** 36854ef6443cSMatthias Ringwald * @brief Get bondable mode. 36864ef6443cSMatthias Ringwald * @return 1 if bondable 36874ef6443cSMatthias Ringwald */ 36884ef6443cSMatthias Ringwald int gap_get_bondable_mode(void){ 36894ef6443cSMatthias Ringwald return hci_stack->bondable; 36904ef6443cSMatthias Ringwald } 3691cb230b9dS[email protected] 3692cb230b9dS[email protected] /** 369334d2123cS[email protected] * @brief map link keys to security levels 3694cb230b9dS[email protected] */ 369534d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){ 369634d2123cS[email protected] switch (link_key_type){ 36973c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 36983c68dfa9S[email protected] return LEVEL_4; 36993c68dfa9S[email protected] case COMBINATION_KEY: 37003c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 37013c68dfa9S[email protected] return LEVEL_3; 37023c68dfa9S[email protected] default: 37033c68dfa9S[email protected] return LEVEL_2; 37043c68dfa9S[email protected] } 3705cb230b9dS[email protected] } 3706cb230b9dS[email protected] 3707106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){ 37085127cc62S[email protected] log_info("gap_mitm_protection_required_for_security_level %u", level); 3709106d6d11S[email protected] return level > LEVEL_2; 3710106d6d11S[email protected] } 3711106d6d11S[email protected] 371234d2123cS[email protected] /** 371334d2123cS[email protected] * @brief get current security level 371434d2123cS[email protected] */ 371534d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){ 371634d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 371734d2123cS[email protected] if (!connection) return LEVEL_0; 371834d2123cS[email protected] return gap_security_level_for_connection(connection); 371934d2123cS[email protected] } 372034d2123cS[email protected] 3721cb230b9dS[email protected] /** 3722cb230b9dS[email protected] * @brief request connection to device to 3723cb230b9dS[email protected] * @result GAP_AUTHENTICATION_RESULT 3724cb230b9dS[email protected] */ 372534d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){ 372634d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 372734d2123cS[email protected] if (!connection){ 3728a00031e2S[email protected] hci_emit_security_level(con_handle, LEVEL_0); 372934d2123cS[email protected] return; 373034d2123cS[email protected] } 373134d2123cS[email protected] gap_security_level_t current_level = gap_security_level(con_handle); 373234d2123cS[email protected] log_info("gap_request_security_level %u, current level %u", requested_level, current_level); 373334d2123cS[email protected] if (current_level >= requested_level){ 3734a00031e2S[email protected] hci_emit_security_level(con_handle, current_level); 373534d2123cS[email protected] return; 373634d2123cS[email protected] } 3737a00031e2S[email protected] 373834d2123cS[email protected] connection->requested_security_level = requested_level; 3739a00031e2S[email protected] 374025bf5872S[email protected] #if 0 374125bf5872S[email protected] // sending encryption request without a link key results in an error. 374225bf5872S[email protected] // TODO: figure out how to use it properly 374325bf5872S[email protected] 3744fb8ba0dbS[email protected] // would enabling ecnryption suffice (>= LEVEL_2)? 3745a98592bcSMatthias Ringwald if (hci_stack->link_key_db){ 3746a00031e2S[email protected] link_key_type_t link_key_type; 3747a00031e2S[email protected] link_key_t link_key; 3748a98592bcSMatthias Ringwald if (hci_stack->link_key_db->get_link_key( &connection->address, &link_key, &link_key_type)){ 3749a00031e2S[email protected] if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){ 3750a00031e2S[email protected] connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 3751a00031e2S[email protected] return; 3752a00031e2S[email protected] } 3753a00031e2S[email protected] } 3754a00031e2S[email protected] } 375525bf5872S[email protected] #endif 3756a00031e2S[email protected] 37571eb2563eS[email protected] // try to authenticate connection 37581eb2563eS[email protected] connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 3759e80b2cf9S[email protected] hci_run(); 3760e00caf9cS[email protected] } 3761ad83dc6aS[email protected] 3762ad83dc6aS[email protected] /** 3763ad83dc6aS[email protected] * @brief start dedicated bonding with device. disconnect after bonding 3764ad83dc6aS[email protected] * @param device 3765ad83dc6aS[email protected] * @param request MITM protection 3766ad83dc6aS[email protected] * @result GAP_DEDICATED_BONDING_COMPLETE 3767ad83dc6aS[email protected] */ 3768ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){ 3769ad83dc6aS[email protected] 3770ad83dc6aS[email protected] // create connection state machine 377196a45072S[email protected] hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_CLASSIC); 3772ad83dc6aS[email protected] 3773ad83dc6aS[email protected] if (!connection){ 3774ad83dc6aS[email protected] return BTSTACK_MEMORY_ALLOC_FAILED; 3775ad83dc6aS[email protected] } 3776ad83dc6aS[email protected] 3777ad83dc6aS[email protected] // delete linkn key 377815a95bd5SMatthias Ringwald gap_drop_link_key_for_bd_addr(device); 3779ad83dc6aS[email protected] 3780ad83dc6aS[email protected] // configure LEVEL_2/3, dedicated bonding 3781ad83dc6aS[email protected] connection->state = SEND_CREATE_CONNECTION; 3782ad83dc6aS[email protected] connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2; 3783f04a0c31SMatthias Ringwald log_info("gap_dedicated_bonding, mitm %d -> level %u", mitm_protection_required, connection->requested_security_level); 3784ad83dc6aS[email protected] connection->bonding_flags = BONDING_DEDICATED; 3785ad83dc6aS[email protected] 3786ad83dc6aS[email protected] // wait for GAP Security Result and send GAP Dedicated Bonding complete 3787ad83dc6aS[email protected] 3788ad83dc6aS[email protected] // handle: connnection failure (connection complete != ok) 3789ad83dc6aS[email protected] // handle: authentication failure 3790ad83dc6aS[email protected] // handle: disconnect on done 3791ad83dc6aS[email protected] 3792ad83dc6aS[email protected] hci_run(); 3793ad83dc6aS[email protected] 3794ad83dc6aS[email protected] return 0; 3795ad83dc6aS[email protected] } 379635454696SMatthias Ringwald #endif 37978e618f72S[email protected] 37988e618f72S[email protected] void gap_set_local_name(const char * local_name){ 37998e618f72S[email protected] hci_stack->local_name = local_name; 38008e618f72S[email protected] } 38018e618f72S[email protected] 380235454696SMatthias Ringwald 380335454696SMatthias Ringwald #ifdef ENABLE_BLE 380435454696SMatthias Ringwald 3805d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 3806d8e8f12aSMatthias Ringwald void gap_start_scan(void){ 3807d8e8f12aSMatthias Ringwald if (hci_stack->le_scanning_state == LE_SCANNING) return; 38087bdc6798S[email protected] hci_stack->le_scanning_state = LE_START_SCAN; 38097bdc6798S[email protected] hci_run(); 38107bdc6798S[email protected] } 38118e618f72S[email protected] 3812d8e8f12aSMatthias Ringwald void gap_stop_scan(void){ 3813d8e8f12aSMatthias Ringwald if ( hci_stack->le_scanning_state == LE_SCAN_IDLE) return; 38147bdc6798S[email protected] hci_stack->le_scanning_state = LE_STOP_SCAN; 38157bdc6798S[email protected] hci_run(); 38167bdc6798S[email protected] } 38174f3229d8S[email protected] 3818d8e8f12aSMatthias Ringwald void gap_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){ 3819ef11999fSmatthias.ringwald hci_stack->le_scan_type = scan_type; 3820ef11999fSmatthias.ringwald hci_stack->le_scan_interval = scan_interval; 3821ef11999fSmatthias.ringwald hci_stack->le_scan_window = scan_window; 3822ef11999fSmatthias.ringwald hci_run(); 3823ef11999fSmatthias.ringwald } 38244f3229d8S[email protected] 3825d8e8f12aSMatthias Ringwald uint8_t gap_connect(bd_addr_t addr, bd_addr_type_t addr_type){ 38264f3229d8S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 38274f3229d8S[email protected] if (!conn){ 3828d8e8f12aSMatthias Ringwald log_info("gap_connect: no connection exists yet, creating context"); 38292e77e513S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 38304f3229d8S[email protected] if (!conn){ 38314f3229d8S[email protected] // notify client that alloc failed 38326e2e9a6bS[email protected] hci_emit_le_connection_complete(addr_type, addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 3833d8e8f12aSMatthias Ringwald log_info("gap_connect: failed to alloc hci_connection_t"); 3834472a5742SMatthias Ringwald return GATT_CLIENT_NOT_CONNECTED; // don't sent packet to controller 38354f3229d8S[email protected] } 38364f3229d8S[email protected] conn->state = SEND_CREATE_CONNECTION; 3837d8e8f12aSMatthias Ringwald log_info("gap_connect: send create connection next"); 3838564fca32S[email protected] hci_run(); 3839616edd56SMatthias Ringwald return 0; 38404f3229d8S[email protected] } 38410bf6344aS[email protected] 38420bf6344aS[email protected] if (!hci_is_le_connection(conn) || 38430bf6344aS[email protected] conn->state == SEND_CREATE_CONNECTION || 38440bf6344aS[email protected] conn->state == SENT_CREATE_CONNECTION) { 38452e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_COMMAND_DISALLOWED); 3846d8e8f12aSMatthias Ringwald log_error("gap_connect: classic connection or connect is already being created"); 3847616edd56SMatthias Ringwald return GATT_CLIENT_IN_WRONG_STATE; 38480bf6344aS[email protected] } 38490bf6344aS[email protected] 3850d8e8f12aSMatthias Ringwald log_info("gap_connect: context exists with state %u", conn->state); 38512e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, conn->con_handle, 0); 38524f3229d8S[email protected] hci_run(); 3853616edd56SMatthias Ringwald return 0; 38544f3229d8S[email protected] } 38554f3229d8S[email protected] 38567851196eSmatthias.ringwald // @assumption: only a single outgoing LE Connection exists 3857d8e8f12aSMatthias Ringwald static hci_connection_t * gap_get_outgoing_connection(void){ 3858665d90f2SMatthias Ringwald btstack_linked_item_t *it; 3859665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 38600bf6344aS[email protected] hci_connection_t * conn = (hci_connection_t *) it; 38610bf6344aS[email protected] if (!hci_is_le_connection(conn)) continue; 38620bf6344aS[email protected] switch (conn->state){ 3863a6725849S[email protected] case SEND_CREATE_CONNECTION: 38647851196eSmatthias.ringwald case SENT_CREATE_CONNECTION: 38657851196eSmatthias.ringwald return conn; 38667851196eSmatthias.ringwald default: 38677851196eSmatthias.ringwald break; 38687851196eSmatthias.ringwald }; 38697851196eSmatthias.ringwald } 38707851196eSmatthias.ringwald return NULL; 38717851196eSmatthias.ringwald } 38727851196eSmatthias.ringwald 3873d8e8f12aSMatthias Ringwald uint8_t gap_connect_cancel(void){ 3874d8e8f12aSMatthias Ringwald hci_connection_t * conn = gap_get_outgoing_connection(); 3875616edd56SMatthias Ringwald if (!conn) return 0; 38767851196eSmatthias.ringwald switch (conn->state){ 38777851196eSmatthias.ringwald case SEND_CREATE_CONNECTION: 38787851196eSmatthias.ringwald // skip sending create connection and emit event instead 38792e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER); 3880665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 38817851196eSmatthias.ringwald btstack_memory_hci_connection_free( conn ); 38820bf6344aS[email protected] break; 3883a6725849S[email protected] case SENT_CREATE_CONNECTION: 38847851196eSmatthias.ringwald // request to send cancel connection 38850bf6344aS[email protected] conn->state = SEND_CANCEL_CONNECTION; 38860bf6344aS[email protected] hci_run(); 38870bf6344aS[email protected] break; 38880bf6344aS[email protected] default: 38890bf6344aS[email protected] break; 38900bf6344aS[email protected] } 3891616edd56SMatthias Ringwald return 0; 3892e31f89a7S[email protected] } 3893d70217a2SMatthias Ringwald #endif 38944f3229d8S[email protected] 3895c37a3166S[email protected] /** 3896c37a3166S[email protected] * @brief Updates the connection parameters for a given LE connection 3897c37a3166S[email protected] * @param handle 3898c37a3166S[email protected] * @param conn_interval_min (unit: 1.25ms) 3899c37a3166S[email protected] * @param conn_interval_max (unit: 1.25ms) 3900c37a3166S[email protected] * @param conn_latency 3901c37a3166S[email protected] * @param supervision_timeout (unit: 10ms) 3902c37a3166S[email protected] * @returns 0 if ok 3903c37a3166S[email protected] */ 3904c37a3166S[email protected] int gap_update_connection_parameters(hci_con_handle_t con_handle, uint16_t conn_interval_min, 3905c37a3166S[email protected] uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 3906c37a3166S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 3907c37a3166S[email protected] if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3908c37a3166S[email protected] connection->le_conn_interval_min = conn_interval_min; 3909c37a3166S[email protected] connection->le_conn_interval_max = conn_interval_max; 3910c37a3166S[email protected] connection->le_conn_latency = conn_latency; 3911c37a3166S[email protected] connection->le_supervision_timeout = supervision_timeout; 391284cf6d83SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS; 3913cfc59f1bSMatthias Ringwald hci_run(); 3914c37a3166S[email protected] return 0; 3915c37a3166S[email protected] } 3916c37a3166S[email protected] 391745c102fdSMatthias Ringwald /** 3918b68d7bc3SMatthias Ringwald * @brief Request an update of the connection parameter for a given LE connection 3919b68d7bc3SMatthias Ringwald * @param handle 3920b68d7bc3SMatthias Ringwald * @param conn_interval_min (unit: 1.25ms) 3921b68d7bc3SMatthias Ringwald * @param conn_interval_max (unit: 1.25ms) 3922b68d7bc3SMatthias Ringwald * @param conn_latency 3923b68d7bc3SMatthias Ringwald * @param supervision_timeout (unit: 10ms) 3924b68d7bc3SMatthias Ringwald * @returns 0 if ok 3925b68d7bc3SMatthias Ringwald */ 3926b68d7bc3SMatthias Ringwald int gap_request_connection_parameter_update(hci_con_handle_t con_handle, uint16_t conn_interval_min, 3927b68d7bc3SMatthias Ringwald uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 3928b68d7bc3SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 3929b68d7bc3SMatthias Ringwald if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3930b68d7bc3SMatthias Ringwald connection->le_conn_interval_min = conn_interval_min; 3931b68d7bc3SMatthias Ringwald connection->le_conn_interval_max = conn_interval_max; 3932b68d7bc3SMatthias Ringwald connection->le_conn_latency = conn_latency; 3933b68d7bc3SMatthias Ringwald connection->le_supervision_timeout = supervision_timeout; 3934b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_REQUEST; 3935b68d7bc3SMatthias Ringwald hci_run(); 3936b68d7bc3SMatthias Ringwald return 0; 3937b68d7bc3SMatthias Ringwald } 3938b68d7bc3SMatthias Ringwald 3939d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 3940d70217a2SMatthias Ringwald 3941501f56b3SMatthias Ringwald static void gap_advertisments_changed(void){ 3942501f56b3SMatthias Ringwald // disable advertisements before updating adv, scan data, or adv params 3943501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_active){ 3944501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_DISABLE | LE_ADVERTISEMENT_TASKS_ENABLE; 3945501f56b3SMatthias Ringwald } 3946501f56b3SMatthias Ringwald hci_run(); 3947501f56b3SMatthias Ringwald } 3948501f56b3SMatthias Ringwald 3949b68d7bc3SMatthias Ringwald /** 395045c102fdSMatthias Ringwald * @brief Set Advertisement Data 395145c102fdSMatthias Ringwald * @param advertising_data_length 395245c102fdSMatthias Ringwald * @param advertising_data (max 31 octets) 395345c102fdSMatthias Ringwald * @note data is not copied, pointer has to stay valid 395445c102fdSMatthias Ringwald */ 395545c102fdSMatthias Ringwald void gap_advertisements_set_data(uint8_t advertising_data_length, uint8_t * advertising_data){ 395645c102fdSMatthias Ringwald hci_stack->le_advertisements_data_len = advertising_data_length; 395745c102fdSMatthias Ringwald hci_stack->le_advertisements_data = advertising_data; 3958501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 3959501f56b3SMatthias Ringwald gap_advertisments_changed(); 396045c102fdSMatthias Ringwald } 3961501f56b3SMatthias Ringwald 3962501f56b3SMatthias Ringwald /** 3963501f56b3SMatthias Ringwald * @brief Set Scan Response Data 3964501f56b3SMatthias Ringwald * @param advertising_data_length 3965501f56b3SMatthias Ringwald * @param advertising_data (max 31 octets) 3966501f56b3SMatthias Ringwald * @note data is not copied, pointer has to stay valid 3967501f56b3SMatthias Ringwald */ 3968501f56b3SMatthias Ringwald void gap_scan_response_set_data(uint8_t scan_response_data_length, uint8_t * scan_response_data){ 3969501f56b3SMatthias Ringwald hci_stack->le_scan_response_data_len = scan_response_data_length; 3970501f56b3SMatthias Ringwald hci_stack->le_scan_response_data = scan_response_data; 3971501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 3972501f56b3SMatthias Ringwald gap_advertisments_changed(); 397345c102fdSMatthias Ringwald } 397445c102fdSMatthias Ringwald 397545c102fdSMatthias Ringwald /** 397645c102fdSMatthias Ringwald * @brief Set Advertisement Parameters 397745c102fdSMatthias Ringwald * @param adv_int_min 397845c102fdSMatthias Ringwald * @param adv_int_max 397945c102fdSMatthias Ringwald * @param adv_type 398045c102fdSMatthias Ringwald * @param direct_address_type 398145c102fdSMatthias Ringwald * @param direct_address 398245c102fdSMatthias Ringwald * @param channel_map 398345c102fdSMatthias Ringwald * @param filter_policy 398445c102fdSMatthias Ringwald * 398545c102fdSMatthias Ringwald * @note internal use. use gap_advertisements_set_params from gap_le.h instead. 398645c102fdSMatthias Ringwald */ 398745c102fdSMatthias Ringwald void hci_le_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type, 3988b95a5a35SMatthias Ringwald uint8_t direct_address_typ, bd_addr_t direct_address, 398945c102fdSMatthias Ringwald uint8_t channel_map, uint8_t filter_policy) { 399045c102fdSMatthias Ringwald 399145c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_min = adv_int_min; 399245c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_max = adv_int_max; 399345c102fdSMatthias Ringwald hci_stack->le_advertisements_type = adv_type; 399445c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address_type = direct_address_typ; 399545c102fdSMatthias Ringwald hci_stack->le_advertisements_channel_map = channel_map; 399645c102fdSMatthias Ringwald hci_stack->le_advertisements_filter_policy = filter_policy; 399745c102fdSMatthias Ringwald memcpy(hci_stack->le_advertisements_direct_address, direct_address, 6); 399845c102fdSMatthias Ringwald 399945c102fdSMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 4000501f56b3SMatthias Ringwald gap_advertisments_changed(); 400145c102fdSMatthias Ringwald } 400245c102fdSMatthias Ringwald 400345c102fdSMatthias Ringwald /** 400445c102fdSMatthias Ringwald * @brief Enable/Disable Advertisements 400545c102fdSMatthias Ringwald * @param enabled 400645c102fdSMatthias Ringwald */ 400745c102fdSMatthias Ringwald void gap_advertisements_enable(int enabled){ 400845c102fdSMatthias Ringwald hci_stack->le_advertisements_enabled = enabled; 400945c102fdSMatthias Ringwald if (enabled && !hci_stack->le_advertisements_active){ 401045c102fdSMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_ENABLE; 401145c102fdSMatthias Ringwald } 401245c102fdSMatthias Ringwald if (!enabled && hci_stack->le_advertisements_active){ 401345c102fdSMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_DISABLE; 401445c102fdSMatthias Ringwald } 4015cfc59f1bSMatthias Ringwald hci_run(); 401645c102fdSMatthias Ringwald } 401745c102fdSMatthias Ringwald 401835454696SMatthias Ringwald #endif 401906e5cf96SMatthias Ringwald 402006e5cf96SMatthias Ringwald void hci_le_set_own_address_type(uint8_t own_address_type){ 402106e5cf96SMatthias Ringwald log_info("hci_le_set_own_address_type: old %u, new %u", hci_stack->le_own_addr_type, own_address_type); 402206e5cf96SMatthias Ringwald if (own_address_type == hci_stack->le_own_addr_type) return; 402306e5cf96SMatthias Ringwald hci_stack->le_own_addr_type = own_address_type; 402406e5cf96SMatthias Ringwald 402564068776SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 402606e5cf96SMatthias Ringwald // update advertisement parameters, too 402706e5cf96SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 402806e5cf96SMatthias Ringwald gap_advertisments_changed(); 402964068776SMatthias Ringwald #endif 403064068776SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 403106e5cf96SMatthias Ringwald // note: we don't update scan parameters or modify ongoing connection attempts 403264068776SMatthias Ringwald #endif 403306e5cf96SMatthias Ringwald } 403406e5cf96SMatthias Ringwald 4035d70217a2SMatthias Ringwald #endif 403645c102fdSMatthias Ringwald 4037616edd56SMatthias Ringwald uint8_t gap_disconnect(hci_con_handle_t handle){ 40385917a5c5S[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 40395917a5c5S[email protected] if (!conn){ 40407851196eSmatthias.ringwald hci_emit_disconnection_complete(handle, 0); 4041616edd56SMatthias Ringwald return 0; 40425917a5c5S[email protected] } 40435917a5c5S[email protected] conn->state = SEND_DISCONNECT; 40445917a5c5S[email protected] hci_run(); 4045616edd56SMatthias Ringwald return 0; 40464f3229d8S[email protected] } 404704a6ef8cSmatthias.ringwald 4048a1bf5ae7SMatthias Ringwald /** 4049a1bf5ae7SMatthias Ringwald * @brief Get connection type 4050a1bf5ae7SMatthias Ringwald * @param con_handle 4051a1bf5ae7SMatthias Ringwald * @result connection_type 4052a1bf5ae7SMatthias Ringwald */ 4053a1bf5ae7SMatthias Ringwald gap_connection_type_t gap_get_connection_type(hci_con_handle_t connection_handle){ 4054a1bf5ae7SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(connection_handle); 4055a1bf5ae7SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 4056a1bf5ae7SMatthias Ringwald switch (conn->address_type){ 4057a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 4058a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 4059a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_LE; 4060a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_SCO: 4061a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_SCO; 4062a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_CLASSIC: 4063a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_ACL; 4064a1bf5ae7SMatthias Ringwald default: 4065a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_INVALID; 4066a1bf5ae7SMatthias Ringwald } 4067a1bf5ae7SMatthias Ringwald } 4068a1bf5ae7SMatthias Ringwald 4069a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 40704f551432SMatthias Ringwald 4071d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4072d23838ecSMatthias Ringwald /** 4073ac9c45e0SMatthias Ringwald * @brief Auto Connection Establishment - Start Connecting to device 4074ac9c45e0SMatthias Ringwald * @param address_typ 4075ac9c45e0SMatthias Ringwald * @param address 4076ac9c45e0SMatthias Ringwald * @returns 0 if ok 4077ac9c45e0SMatthias Ringwald */ 40784f551432SMatthias Ringwald int gap_auto_connection_start(bd_addr_type_t address_type, bd_addr_t address){ 4079e83201bcSMatthias Ringwald // check capacity 4080665d90f2SMatthias Ringwald int num_entries = btstack_linked_list_count(&hci_stack->le_whitelist); 408191915b0bSMatthias Ringwald if (num_entries >= hci_stack->le_whitelist_capacity) return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED; 4082e83201bcSMatthias Ringwald whitelist_entry_t * entry = btstack_memory_whitelist_entry_get(); 4083e83201bcSMatthias Ringwald if (!entry) return BTSTACK_MEMORY_ALLOC_FAILED; 4084e83201bcSMatthias Ringwald entry->address_type = address_type; 4085e83201bcSMatthias Ringwald memcpy(entry->address, address, 6); 4086e83201bcSMatthias Ringwald entry->state = LE_WHITELIST_ADD_TO_CONTROLLER; 4087665d90f2SMatthias Ringwald btstack_linked_list_add(&hci_stack->le_whitelist, (btstack_linked_item_t*) entry); 4088e83201bcSMatthias Ringwald hci_run(); 4089e83201bcSMatthias Ringwald return 0; 4090ac9c45e0SMatthias Ringwald } 4091ac9c45e0SMatthias Ringwald 409242ff5ba1SMatthias Ringwald static void hci_remove_from_whitelist(bd_addr_type_t address_type, bd_addr_t address){ 4093665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 4094665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 4095665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 4096665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it); 4097e83201bcSMatthias Ringwald if (entry->address_type != address_type) continue; 4098e83201bcSMatthias Ringwald if (memcmp(entry->address, address, 6) != 0) continue; 4099e83201bcSMatthias Ringwald if (entry->state & LE_WHITELIST_ON_CONTROLLER){ 4100e83201bcSMatthias Ringwald // remove from controller if already present 4101e83201bcSMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER; 4102e83201bcSMatthias Ringwald continue; 4103e83201bcSMatthias Ringwald } 4104e83201bcSMatthias Ringwald // direclty remove entry from whitelist 4105665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 4106e83201bcSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 4107e83201bcSMatthias Ringwald } 410842ff5ba1SMatthias Ringwald } 410942ff5ba1SMatthias Ringwald 411042ff5ba1SMatthias Ringwald /** 411142ff5ba1SMatthias Ringwald * @brief Auto Connection Establishment - Stop Connecting to device 411242ff5ba1SMatthias Ringwald * @param address_typ 411342ff5ba1SMatthias Ringwald * @param address 411442ff5ba1SMatthias Ringwald * @returns 0 if ok 411542ff5ba1SMatthias Ringwald */ 411642ff5ba1SMatthias Ringwald int gap_auto_connection_stop(bd_addr_type_t address_type, bd_addr_t address){ 411742ff5ba1SMatthias Ringwald hci_remove_from_whitelist(address_type, address); 4118e83201bcSMatthias Ringwald hci_run(); 4119e83201bcSMatthias Ringwald return 0; 4120ac9c45e0SMatthias Ringwald } 4121ac9c45e0SMatthias Ringwald 4122ac9c45e0SMatthias Ringwald /** 4123ac9c45e0SMatthias Ringwald * @brief Auto Connection Establishment - Stop everything 4124ac9c45e0SMatthias Ringwald * @note Convenience function to stop all active auto connection attempts 4125ac9c45e0SMatthias Ringwald */ 4126ac9c45e0SMatthias Ringwald void gap_auto_connection_stop_all(void){ 4127665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 4128665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 4129665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 4130665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it); 4131e83201bcSMatthias Ringwald if (entry->state & LE_WHITELIST_ON_CONTROLLER){ 4132e83201bcSMatthias Ringwald // remove from controller if already present 4133e83201bcSMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER; 4134e83201bcSMatthias Ringwald continue; 4135e83201bcSMatthias Ringwald } 413691915b0bSMatthias Ringwald // directly remove entry from whitelist 4137665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 4138e83201bcSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 4139e83201bcSMatthias Ringwald } 4140e83201bcSMatthias Ringwald hci_run(); 4141ac9c45e0SMatthias Ringwald } 4142d70217a2SMatthias Ringwald #endif 41434f551432SMatthias Ringwald #endif 41444f551432SMatthias Ringwald 414535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 4146ac9c45e0SMatthias Ringwald /** 4147ff00ed1cSMatthias Ringwald * @brief Set Extended Inquiry Response data 4148ff00ed1cSMatthias Ringwald * @param eir_data size 240 bytes, is not copied make sure memory is accessible during stack startup 4149ff00ed1cSMatthias Ringwald * @note has to be done before stack starts up 4150ff00ed1cSMatthias Ringwald */ 4151ff00ed1cSMatthias Ringwald void gap_set_extended_inquiry_response(const uint8_t * data){ 4152ff00ed1cSMatthias Ringwald hci_stack->eir_data = data; 4153ff00ed1cSMatthias Ringwald } 4154ff00ed1cSMatthias Ringwald 4155ff00ed1cSMatthias Ringwald /** 4156*f5875de5SMatthias Ringwald * @brief Start GAP Classic Inquiry 4157*f5875de5SMatthias Ringwald * @param duration in 1.28s units 4158*f5875de5SMatthias Ringwald * @return 0 if ok 4159*f5875de5SMatthias Ringwald * @events: GAP_EVENT_INQUIRY_RESULT, GAP_EVENT_INQUIRY_COMPLETE 4160*f5875de5SMatthias Ringwald */ 4161*f5875de5SMatthias Ringwald int gap_inquiry_start(uint8_t duration_in_1280ms_units){ 4162*f5875de5SMatthias Ringwald if (hci_stack->inquiry_state != GAP_INQUIRY_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 4163*f5875de5SMatthias Ringwald if (duration_in_1280ms_units < GAP_INQUIRY_DURATION_MIN || duration_in_1280ms_units > GAP_INQUIRY_DURATION_MAX){ 4164*f5875de5SMatthias Ringwald return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 4165*f5875de5SMatthias Ringwald } 4166*f5875de5SMatthias Ringwald hci_stack->inquiry_state = duration_in_1280ms_units; 4167*f5875de5SMatthias Ringwald hci_run(); 4168*f5875de5SMatthias Ringwald return 0; 4169*f5875de5SMatthias Ringwald } 4170*f5875de5SMatthias Ringwald 4171*f5875de5SMatthias Ringwald /** 4172*f5875de5SMatthias Ringwald * @brief Stop GAP Classic Inquiry 4173*f5875de5SMatthias Ringwald * @returns 0 if ok 4174*f5875de5SMatthias Ringwald */ 4175*f5875de5SMatthias Ringwald int gap_inquiry_stop(void){ 4176*f5875de5SMatthias Ringwald if (hci_stack->inquiry_state >= GAP_INQUIRY_DURATION_MIN || hci_stack->inquiry_state <= GAP_INQUIRY_DURATION_MAX) { 4177*f5875de5SMatthias Ringwald // emit inquiry complete event, before it even started 4178*f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 4179*f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 4180*f5875de5SMatthias Ringwald return 0; 4181*f5875de5SMatthias Ringwald } 4182*f5875de5SMatthias Ringwald if (hci_stack->inquiry_state != GAP_INQUIRY_STATE_ACTIVE) return ERROR_CODE_COMMAND_DISALLOWED; 4183*f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W2_CANCEL; 4184*f5875de5SMatthias Ringwald hci_run(); 4185*f5875de5SMatthias Ringwald return 0; 4186*f5875de5SMatthias Ringwald } 4187*f5875de5SMatthias Ringwald 4188*f5875de5SMatthias Ringwald /** 4189f6858d14SMatthias Ringwald * @brief Set inquiry mode: standard, with RSSI, with RSSI + Extended Inquiry Results. Has to be called before power on. 4190*f5875de5SMatthias Ringwald * @param inquiry_mode see bluetooth_defines.h 4191f6858d14SMatthias Ringwald */ 4192f6858d14SMatthias Ringwald void hci_set_inquiry_mode(inquiry_mode_t mode){ 4193f6858d14SMatthias Ringwald hci_stack->inquiry_mode = mode; 4194f6858d14SMatthias Ringwald } 4195f6858d14SMatthias Ringwald 4196f6858d14SMatthias Ringwald /** 4197d950d659SMatthias Ringwald * @brief Configure Voice Setting for use with SCO data in HSP/HFP 4198d950d659SMatthias Ringwald */ 4199d950d659SMatthias Ringwald void hci_set_sco_voice_setting(uint16_t voice_setting){ 4200d950d659SMatthias Ringwald hci_stack->sco_voice_setting = voice_setting; 4201d950d659SMatthias Ringwald } 4202d950d659SMatthias Ringwald 4203d950d659SMatthias Ringwald /** 4204d950d659SMatthias Ringwald * @brief Get SCO Voice Setting 4205d950d659SMatthias Ringwald * @return current voice setting 4206d950d659SMatthias Ringwald */ 42070cb5b971SMatthias Ringwald uint16_t hci_get_sco_voice_setting(void){ 4208d950d659SMatthias Ringwald return hci_stack->sco_voice_setting; 4209d950d659SMatthias Ringwald } 4210d950d659SMatthias Ringwald 4211b3aad8daSMatthias Ringwald /** @brief Get SCO packet length for current SCO Voice setting 4212b3aad8daSMatthias Ringwald * @note Using SCO packets of the exact length is required for USB transfer 4213b3aad8daSMatthias Ringwald * @return Length of SCO packets in bytes (not audio frames) 4214b3aad8daSMatthias Ringwald */ 4215b3aad8daSMatthias Ringwald int hci_get_sco_packet_length(void){ 4216b3aad8daSMatthias Ringwald // see Core Spec for H2 USB Transfer. 4217b3aad8daSMatthias Ringwald if (hci_stack->sco_voice_setting & 0x0020) return 51; 4218b3aad8daSMatthias Ringwald return 27; 4219b3aad8daSMatthias Ringwald } 422035454696SMatthias Ringwald #endif 4221b3aad8daSMatthias Ringwald 4222d950d659SMatthias Ringwald /** 4223d23838ecSMatthias Ringwald * @brief Set callback for Bluetooth Hardware Error 4224d23838ecSMatthias Ringwald */ 4225c2e1fa60SMatthias Ringwald void hci_set_hardware_error_callback(void (*fn)(uint8_t error)){ 4226d23838ecSMatthias Ringwald hci_stack->hardware_error_callback = fn; 4227d23838ecSMatthias Ringwald } 4228d23838ecSMatthias Ringwald 422971de195eSMatthias Ringwald void hci_disconnect_all(void){ 4230665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 4231665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 4232665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 4233665d90f2SMatthias Ringwald hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 423404a6ef8cSmatthias.ringwald if (con->state == SENT_DISCONNECT) continue; 423504a6ef8cSmatthias.ringwald con->state = SEND_DISCONNECT; 423604a6ef8cSmatthias.ringwald } 4237d31fba26S[email protected] hci_run(); 423804a6ef8cSmatthias.ringwald } 423933373e40SMatthias Ringwald 424033373e40SMatthias Ringwald uint16_t hci_get_manufacturer(void){ 424133373e40SMatthias Ringwald return hci_stack->manufacturer; 424233373e40SMatthias Ringwald } 4243