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 104f5875de5SMatthias Ringwald // GAP inquiry state: 0 = off, 0x01 - 0x30 = requested duration, 0xfe = active, 0xff = stop requested 105f5875de5SMatthias Ringwald #define GAP_INQUIRY_DURATION_MIN 0x01 106f5875de5SMatthias Ringwald #define GAP_INQUIRY_DURATION_MAX 0x30 107f5875de5SMatthias Ringwald #define GAP_INQUIRY_STATE_ACTIVE 0x80 108f5875de5SMatthias Ringwald #define GAP_INQUIRY_STATE_IDLE 0 109f5875de5SMatthias Ringwald #define GAP_INQUIRY_STATE_W2_CANCEL 0x81 110f5875de5SMatthias Ringwald #define GAP_INQUIRY_STATE_W4_CANCELLED 0x82 111f5875de5SMatthias Ringwald 112*b7f1ee76SMatthias Ringwald // GAP Remote Name Request 113*b7f1ee76SMatthias Ringwald #define GAP_REMOTE_NAME_STATE_IDLE 0 114*b7f1ee76SMatthias Ringwald #define GAP_REMOTE_NAME_STATE_W2_SEND 1 115*b7f1ee76SMatthias Ringwald #define GAP_REMOTE_NAME_STATE_W4_COMPLETE 2 116*b7f1ee76SMatthias Ringwald 117b83d5eabSMatthias Ringwald // prototypes 11835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 119758b46ceSmatthias.ringwald static void hci_update_scan_enable(void); 12035454696SMatthias Ringwald static void hci_emit_discoverable_enabled(uint8_t enabled); 12135454696SMatthias Ringwald static int hci_local_ssp_activated(void); 12235454696SMatthias Ringwald static int hci_remote_ssp_supported(hci_con_handle_t con_handle); 12335454696SMatthias Ringwald static void hci_notify_if_sco_can_send_now(void); 12435454696SMatthias Ringwald static void hci_emit_connection_complete(bd_addr_t address, hci_con_handle_t con_handle, uint8_t status); 125a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection); 12635454696SMatthias Ringwald static void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level); 127ec820d77SMatthias Ringwald static void hci_connection_timeout_handler(btstack_timer_source_t *timer); 12896a45072S[email protected] static void hci_connection_timestamp(hci_connection_t *connection); 12952db98b2SMatthias Ringwald static void hci_emit_l2cap_check_timeout(hci_connection_t *conn); 1301cfb383eSMatthias Ringwald static void gap_inquiry_explode(uint8_t * packet); 13152db98b2SMatthias Ringwald #endif 1321cfb383eSMatthias Ringwald 1337586ee35S[email protected] static int hci_power_control_on(void); 1347586ee35S[email protected] static void hci_power_control_off(void); 1356da48142SSean Wilson static void hci_state_reset(void); 136fc64f94aSMatthias Ringwald static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason); 137b83d5eabSMatthias Ringwald static void hci_emit_nr_connections_changed(void); 138b83d5eabSMatthias Ringwald static void hci_emit_hci_open_failed(void); 139b83d5eabSMatthias Ringwald static void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status); 140b83d5eabSMatthias Ringwald static void hci_emit_event(uint8_t * event, uint16_t size, int dump); 141b83d5eabSMatthias Ringwald static void hci_emit_acl_packet(uint8_t * packet, uint16_t size); 14295d71764SMatthias Ringwald static void hci_run(void); 14395d71764SMatthias Ringwald static int hci_is_le_connection(hci_connection_t * connection); 14495d71764SMatthias Ringwald static int hci_number_free_acl_slots_for_connection_type( bd_addr_type_t address_type); 1455d509858SMatthias Ringwald 146a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 147e8c8828eSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 14839677e66SMatthias Ringwald // called from test/ble_client/advertising_data_parser.c 14939677e66SMatthias Ringwald void le_handle_advertisement_report(uint8_t *packet, int size); 15042ff5ba1SMatthias Ringwald static void hci_remove_from_whitelist(bd_addr_type_t address_type, bd_addr_t address); 1515d509858SMatthias Ringwald #endif 152d70217a2SMatthias Ringwald #endif 153758b46ceSmatthias.ringwald 15406b35ec0Smatthias.ringwald // the STACK is here 1553a9fb326S[email protected] #ifndef HAVE_MALLOC 1563a9fb326S[email protected] static hci_stack_t hci_stack_static; 1573a9fb326S[email protected] #endif 1583a9fb326S[email protected] static hci_stack_t * hci_stack = NULL; 15916833f0aSmatthias.ringwald 1601c9e5e9dSMatthias Ringwald #ifdef ENABLE_CLASSIC 16166fb9560S[email protected] // test helper 16266fb9560S[email protected] static uint8_t disable_l2cap_timeouts = 0; 1631c9e5e9dSMatthias Ringwald #endif 16466fb9560S[email protected] 16596a45072S[email protected] /** 16696a45072S[email protected] * create connection for given address 16796a45072S[email protected] * 16896a45072S[email protected] * @return connection OR NULL, if no memory left 16996a45072S[email protected] */ 17096a45072S[email protected] static hci_connection_t * create_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){ 1711a06f663S[email protected] log_info("create_connection_for_addr %s, type %x", bd_addr_to_str(addr), addr_type); 172bb69aaaeS[email protected] hci_connection_t * conn = btstack_memory_hci_connection_get(); 17396a45072S[email protected] if (!conn) return NULL; 174c91d150bS[email protected] memset(conn, 0, sizeof(hci_connection_t)); 175058e3d6bSMatthias Ringwald bd_addr_copy(conn->address, addr); 17696a45072S[email protected] conn->address_type = addr_type; 17796a45072S[email protected] conn->con_handle = 0xffff; 17896a45072S[email protected] conn->authentication_flags = AUTH_FLAGS_NONE; 17996a45072S[email protected] conn->bonding_flags = 0; 18096a45072S[email protected] conn->requested_security_level = LEVEL_0; 18152db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 18291a977e8SMatthias Ringwald btstack_run_loop_set_timer_handler(&conn->timeout, hci_connection_timeout_handler); 18391a977e8SMatthias Ringwald btstack_run_loop_set_timer_context(&conn->timeout, conn); 18496a45072S[email protected] hci_connection_timestamp(conn); 18552db98b2SMatthias Ringwald #endif 18696a45072S[email protected] conn->acl_recombination_length = 0; 18796a45072S[email protected] conn->acl_recombination_pos = 0; 18896a45072S[email protected] conn->num_acl_packets_sent = 0; 1891a06f663S[email protected] conn->num_sco_packets_sent = 0; 190da886c03S[email protected] conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 191665d90f2SMatthias Ringwald btstack_linked_list_add(&hci_stack->connections, (btstack_linked_item_t *) conn); 19296a45072S[email protected] return conn; 19396a45072S[email protected] } 19466fb9560S[email protected] 195da886c03S[email protected] 196da886c03S[email protected] /** 197da886c03S[email protected] * get le connection parameter range 198da886c03S[email protected] * 199da886c03S[email protected] * @return le connection parameter range struct 200da886c03S[email protected] */ 2014ced4e8cSMatthias Ringwald void gap_get_connection_parameter_range(le_connection_parameter_range_t * range){ 2024ced4e8cSMatthias Ringwald *range = hci_stack->le_connection_parameter_range; 203da886c03S[email protected] } 204da886c03S[email protected] 205da886c03S[email protected] /** 206da886c03S[email protected] * set le connection parameter range 207da886c03S[email protected] * 208da886c03S[email protected] */ 209da886c03S[email protected] 2104ced4e8cSMatthias Ringwald void gap_set_connection_parameter_range(le_connection_parameter_range_t *range){ 2114ced4e8cSMatthias Ringwald hci_stack->le_connection_parameter_range = *range; 212da886c03S[email protected] } 213da886c03S[email protected] 214da886c03S[email protected] /** 215da886c03S[email protected] * get hci connections iterator 216da886c03S[email protected] * 217da886c03S[email protected] * @return hci connections iterator 218da886c03S[email protected] */ 219da886c03S[email protected] 220665d90f2SMatthias Ringwald void hci_connections_get_iterator(btstack_linked_list_iterator_t *it){ 221665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(it, &hci_stack->connections); 222da886c03S[email protected] } 223da886c03S[email protected] 22497addcc5Smatthias.ringwald /** 225ee091cf1Smatthias.ringwald * get connection for a given handle 226ee091cf1Smatthias.ringwald * 227ee091cf1Smatthias.ringwald * @return connection OR NULL, if not found 228ee091cf1Smatthias.ringwald */ 2295061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){ 230665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 231665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 232665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 233665d90f2SMatthias Ringwald hci_connection_t * item = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 2343ac2fe56S[email protected] if ( item->con_handle == con_handle ) { 235da886c03S[email protected] return item; 236ee091cf1Smatthias.ringwald } 237ee091cf1Smatthias.ringwald } 238ee091cf1Smatthias.ringwald return NULL; 239ee091cf1Smatthias.ringwald } 240ee091cf1Smatthias.ringwald 24196a45072S[email protected] /** 24296a45072S[email protected] * get connection for given address 24396a45072S[email protected] * 24496a45072S[email protected] * @return connection OR NULL, if not found 24596a45072S[email protected] */ 2462e77e513S[email protected] hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){ 247665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 248665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 249665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 250665d90f2SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 25196a45072S[email protected] if (connection->address_type != addr_type) continue; 25296a45072S[email protected] if (memcmp(addr, connection->address, 6) != 0) continue; 25362bda3fbS[email protected] return connection; 25462bda3fbS[email protected] } 25562bda3fbS[email protected] return NULL; 25662bda3fbS[email protected] } 25762bda3fbS[email protected] 25852db98b2SMatthias Ringwald 25952db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 26052db98b2SMatthias Ringwald 261ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 262ee752bb8SMatthias Ringwald static int hci_number_sco_connections(void){ 263ee752bb8SMatthias Ringwald int connections = 0; 264ee752bb8SMatthias Ringwald btstack_linked_list_iterator_t it; 265ee752bb8SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 266ee752bb8SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 267ee752bb8SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 268ee752bb8SMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_SCO) continue; 269ee752bb8SMatthias Ringwald connections++; 270ee752bb8SMatthias Ringwald } 271ee752bb8SMatthias Ringwald return connections; 272ee752bb8SMatthias Ringwald } 273ee752bb8SMatthias Ringwald #endif 274ee752bb8SMatthias Ringwald 275ec820d77SMatthias Ringwald static void hci_connection_timeout_handler(btstack_timer_source_t *timer){ 27691a977e8SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_run_loop_get_timer_context(timer); 277aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK 278528a4a3bSMatthias Ringwald if (btstack_run_loop_embedded_get_ticks() > connection->timestamp + btstack_run_loop_embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){ 279c785ef68Smatthias.ringwald // connections might be timed out 280c785ef68Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 281c785ef68Smatthias.ringwald } 282f316a845SMatthias Ringwald #else 283528a4a3bSMatthias Ringwald if (btstack_run_loop_get_time_ms() > connection->timestamp + HCI_CONNECTION_TIMEOUT_MS){ 2845f26aadcSMatthias Ringwald // connections might be timed out 2855f26aadcSMatthias Ringwald hci_emit_l2cap_check_timeout(connection); 2865f26aadcSMatthias Ringwald } 2875f26aadcSMatthias Ringwald #endif 288c785ef68Smatthias.ringwald } 289ee091cf1Smatthias.ringwald 290ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){ 291aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK 292528a4a3bSMatthias Ringwald connection->timestamp = btstack_run_loop_embedded_get_ticks(); 293f316a845SMatthias Ringwald #else 294528a4a3bSMatthias Ringwald connection->timestamp = btstack_run_loop_get_time_ms(); 2955f26aadcSMatthias Ringwald #endif 296ee091cf1Smatthias.ringwald } 297ee091cf1Smatthias.ringwald 29828ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(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] 30235454696SMatthias Ringwald 30328ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 30428ca2b46S[email protected] conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags); 30528ca2b46S[email protected] } 30628ca2b46S[email protected] 30743bfb1bdSmatthias.ringwald /** 30880ca58a0Smatthias.ringwald * add authentication flags and reset timer 30996a45072S[email protected] * @note: assumes classic connection 3102e77e513S[email protected] * @note: bd_addr is passed in as litle endian uint8_t * as it is called from parsing packets 3117fde4af9Smatthias.ringwald */ 3127fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){ 3137fde4af9Smatthias.ringwald bd_addr_t addr; 314724d70a2SMatthias Ringwald reverse_bd_addr(bd_addr, addr); 3152e77e513S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 3167fde4af9Smatthias.ringwald if (conn) { 31728ca2b46S[email protected] connectionSetAuthenticationFlags(conn, flags); 31880ca58a0Smatthias.ringwald hci_connection_timestamp(conn); 3197fde4af9Smatthias.ringwald } 3207fde4af9Smatthias.ringwald } 3217fde4af9Smatthias.ringwald 32280ca58a0Smatthias.ringwald int hci_authentication_active_for_handle(hci_con_handle_t handle){ 3235061f3afS[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 32480ca58a0Smatthias.ringwald if (!conn) return 0; 3256724cd9eS[email protected] if (conn->authentication_flags & LEGACY_PAIRING_ACTIVE) return 1; 3266724cd9eS[email protected] if (conn->authentication_flags & SSP_PAIRING_ACTIVE) return 1; 3276724cd9eS[email protected] return 0; 32880ca58a0Smatthias.ringwald } 32980ca58a0Smatthias.ringwald 33015a95bd5SMatthias Ringwald void gap_drop_link_key_for_bd_addr(bd_addr_t addr){ 33155597469SMatthias Ringwald if (!hci_stack->link_key_db) return; 3322bacf595SMatthias Ringwald log_info("gap_drop_link_key_for_bd_addr: %s", bd_addr_to_str(addr)); 333a98592bcSMatthias Ringwald hci_stack->link_key_db->delete_link_key(addr); 334c12e46e7Smatthias.ringwald } 33555597469SMatthias Ringwald 33655597469SMatthias Ringwald void gap_store_link_key_for_bd_addr(bd_addr_t addr, link_key_t link_key, link_key_type_t type){ 33755597469SMatthias Ringwald if (!hci_stack->link_key_db) return; 3382bacf595SMatthias Ringwald log_info("gap_store_link_key_for_bd_addr: %s, type %u", bd_addr_to_str(addr), type); 33955597469SMatthias Ringwald hci_stack->link_key_db->put_link_key(addr, link_key, type); 340c12e46e7Smatthias.ringwald } 34135454696SMatthias Ringwald #endif 342c12e46e7Smatthias.ringwald 34395d71764SMatthias Ringwald static int hci_is_le_connection(hci_connection_t * connection){ 3440bf6344aS[email protected] return connection->address_type == BD_ADDR_TYPE_LE_PUBLIC || 3450bf6344aS[email protected] connection->address_type == BD_ADDR_TYPE_LE_RANDOM; 3460bf6344aS[email protected] } 3470bf6344aS[email protected] 3487fde4af9Smatthias.ringwald /** 34943bfb1bdSmatthias.ringwald * count connections 35043bfb1bdSmatthias.ringwald */ 35140d1c7a4Smatthias.ringwald static int nr_hci_connections(void){ 35256c253c9Smatthias.ringwald int count = 0; 353665d90f2SMatthias Ringwald btstack_linked_item_t *it; 354665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next, count++); 35543bfb1bdSmatthias.ringwald return count; 35643bfb1bdSmatthias.ringwald } 357c8e4258aSmatthias.ringwald 35895d71764SMatthias Ringwald static int hci_number_free_acl_slots_for_connection_type(bd_addr_type_t address_type){ 359ee303eddS[email protected] 360f04a0c31SMatthias Ringwald unsigned int num_packets_sent_classic = 0; 361f04a0c31SMatthias Ringwald unsigned int num_packets_sent_le = 0; 362ee303eddS[email protected] 363665d90f2SMatthias Ringwald btstack_linked_item_t *it; 364665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 365998906cdSmatthias.ringwald hci_connection_t * connection = (hci_connection_t *) it; 366ee303eddS[email protected] if (connection->address_type == BD_ADDR_TYPE_CLASSIC){ 367ee303eddS[email protected] num_packets_sent_classic += connection->num_acl_packets_sent; 368ee303eddS[email protected] } else { 369ee303eddS[email protected] num_packets_sent_le += connection->num_acl_packets_sent; 370ee303eddS[email protected] } 371ee303eddS[email protected] } 372d999b54eSMatthias Ringwald log_debug("ACL classic buffers: %u used of %u", num_packets_sent_classic, hci_stack->acl_packets_total_num); 373ee303eddS[email protected] int free_slots_classic = hci_stack->acl_packets_total_num - num_packets_sent_classic; 374ee303eddS[email protected] int free_slots_le = 0; 375ee303eddS[email protected] 376ee303eddS[email protected] if (free_slots_classic < 0){ 3779da54300S[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); 378998906cdSmatthias.ringwald return 0; 379998906cdSmatthias.ringwald } 380ee303eddS[email protected] 381ee303eddS[email protected] if (hci_stack->le_acl_packets_total_num){ 382ee303eddS[email protected] // if we have LE slots, they are used 383ee303eddS[email protected] free_slots_le = hci_stack->le_acl_packets_total_num - num_packets_sent_le; 384ee303eddS[email protected] if (free_slots_le < 0){ 3859da54300S[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); 386ee303eddS[email protected] return 0; 387998906cdSmatthias.ringwald } 388ee303eddS[email protected] } else { 389ee303eddS[email protected] // otherwise, classic slots are used for LE, too 390ee303eddS[email protected] free_slots_classic -= num_packets_sent_le; 391ee303eddS[email protected] if (free_slots_classic < 0){ 3929da54300S[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); 393ee303eddS[email protected] return 0; 394ee303eddS[email protected] } 395ee303eddS[email protected] } 396ee303eddS[email protected] 397ee303eddS[email protected] switch (address_type){ 398ee303eddS[email protected] case BD_ADDR_TYPE_UNKNOWN: 3992125de09SMatthias Ringwald log_error("hci_number_free_acl_slots: unknown address type"); 400ee303eddS[email protected] return 0; 401ee303eddS[email protected] 402ee303eddS[email protected] case BD_ADDR_TYPE_CLASSIC: 403ee303eddS[email protected] return free_slots_classic; 404ee303eddS[email protected] 405ee303eddS[email protected] default: 406cb00d3aaS[email protected] if (hci_stack->le_acl_packets_total_num){ 407ee303eddS[email protected] return free_slots_le; 408ee303eddS[email protected] } 409cb00d3aaS[email protected] return free_slots_classic; 410cb00d3aaS[email protected] } 411998906cdSmatthias.ringwald } 412998906cdSmatthias.ringwald 4132125de09SMatthias Ringwald int hci_number_free_acl_slots_for_handle(hci_con_handle_t con_handle){ 4142125de09SMatthias Ringwald // get connection type 4152125de09SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 4162125de09SMatthias Ringwald if (!connection){ 4172125de09SMatthias Ringwald log_error("hci_number_free_acl_slots: handle 0x%04x not in connection list", con_handle); 4182125de09SMatthias Ringwald return 0; 4192125de09SMatthias Ringwald } 4202125de09SMatthias Ringwald return hci_number_free_acl_slots_for_connection_type(connection->address_type); 4212125de09SMatthias Ringwald } 4222125de09SMatthias Ringwald 42335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 424701e3307SMatthias Ringwald static int hci_number_free_sco_slots(void){ 425f04a0c31SMatthias Ringwald unsigned int num_sco_packets_sent = 0; 426665d90f2SMatthias Ringwald btstack_linked_item_t *it; 427665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 428e35edcc1S[email protected] hci_connection_t * connection = (hci_connection_t *) it; 429e35edcc1S[email protected] num_sco_packets_sent += connection->num_sco_packets_sent; 430e35edcc1S[email protected] } 431e35edcc1S[email protected] if (num_sco_packets_sent > hci_stack->sco_packets_total_num){ 432701e3307SMatthias Ringwald log_info("hci_number_free_sco_slots:packets (%u) > total packets (%u)", num_sco_packets_sent, hci_stack->sco_packets_total_num); 43344d0e3d5S[email protected] return 0; 43444d0e3d5S[email protected] } 435701e3307SMatthias Ringwald // log_info("hci_number_free_sco_slots u", handle, num_sco_packets_sent); 436e35edcc1S[email protected] return hci_stack->sco_packets_total_num - num_sco_packets_sent; 437e35edcc1S[email protected] } 43835454696SMatthias Ringwald #endif 43944d0e3d5S[email protected] 4402b838201SMatthias Ringwald // only used to send HCI Host Number Completed Packets 4412b838201SMatthias Ringwald static int hci_can_send_comand_packet_transport(void){ 442ac928cc2S[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 443ac928cc2S[email protected] 444ac928cc2S[email protected] // check for async hci transport implementations 445ac928cc2S[email protected] if (hci_stack->hci_transport->can_send_packet_now){ 446ac928cc2S[email protected] if (!hci_stack->hci_transport->can_send_packet_now(HCI_COMMAND_DATA_PACKET)){ 447ac928cc2S[email protected] return 0; 448ac928cc2S[email protected] } 449ac928cc2S[email protected] } 4502b838201SMatthias Ringwald return 1; 4512b838201SMatthias Ringwald } 452ac928cc2S[email protected] 4532b838201SMatthias Ringwald // new functions replacing hci_can_send_packet_now[_using_packet_buffer] 4542b838201SMatthias Ringwald int hci_can_send_command_packet_now(void){ 4552b838201SMatthias Ringwald if (hci_can_send_comand_packet_transport() == 0) return 0; 456ac928cc2S[email protected] return hci_stack->num_cmd_packets > 0; 457ac928cc2S[email protected] } 458ac928cc2S[email protected] 4599d04d3a7SMatthias Ringwald static int hci_transport_can_send_prepared_packet_now(uint8_t packet_type){ 460ac928cc2S[email protected] // check for async hci transport implementations 4619d04d3a7SMatthias Ringwald if (!hci_stack->hci_transport->can_send_packet_now) return 1; 4629d04d3a7SMatthias Ringwald return hci_stack->hci_transport->can_send_packet_now(packet_type); 463ac928cc2S[email protected] } 4649d04d3a7SMatthias Ringwald 4659d04d3a7SMatthias Ringwald static int hci_can_send_prepared_acl_packet_for_address_type(bd_addr_type_t address_type){ 4669d04d3a7SMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_ACL_DATA_PACKET)) return 0; 4679d04d3a7SMatthias Ringwald return hci_number_free_acl_slots_for_connection_type(address_type) > 0; 468ac928cc2S[email protected] } 4699d04d3a7SMatthias Ringwald 4709d04d3a7SMatthias Ringwald int hci_can_send_acl_le_packet_now(void){ 4719d04d3a7SMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return 0; 4729d04d3a7SMatthias Ringwald return hci_can_send_prepared_acl_packet_for_address_type(BD_ADDR_TYPE_LE_PUBLIC); 4739d04d3a7SMatthias Ringwald } 4749d04d3a7SMatthias Ringwald 4759d04d3a7SMatthias Ringwald int hci_can_send_prepared_acl_packet_now(hci_con_handle_t con_handle) { 4769d04d3a7SMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_ACL_DATA_PACKET)) return 0; 477e79abdd6S[email protected] return hci_number_free_acl_slots_for_handle(con_handle) > 0; 478ac928cc2S[email protected] } 479ac928cc2S[email protected] 480ac928cc2S[email protected] int hci_can_send_acl_packet_now(hci_con_handle_t con_handle){ 481ac928cc2S[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 482ac928cc2S[email protected] return hci_can_send_prepared_acl_packet_now(con_handle); 4836b4af23dS[email protected] } 4846b4af23dS[email protected] 48535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 48635454696SMatthias Ringwald int hci_can_send_acl_classic_packet_now(void){ 48735454696SMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return 0; 48835454696SMatthias Ringwald return hci_can_send_prepared_acl_packet_for_address_type(BD_ADDR_TYPE_CLASSIC); 48935454696SMatthias Ringwald } 49035454696SMatthias Ringwald 491701e3307SMatthias Ringwald int hci_can_send_prepared_sco_packet_now(void){ 492d057580eSMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_SCO_DATA_PACKET)) return 0; 493ed361f5fSMatthias Ringwald if (!hci_stack->synchronous_flow_control_enabled) return 1; 494701e3307SMatthias Ringwald return hci_number_free_sco_slots() > 0; 49544d0e3d5S[email protected] } 49644d0e3d5S[email protected] 497701e3307SMatthias Ringwald int hci_can_send_sco_packet_now(void){ 498d057580eSMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return 0; 499701e3307SMatthias Ringwald return hci_can_send_prepared_sco_packet_now(); 50044d0e3d5S[email protected] } 50144d0e3d5S[email protected] 502d057580eSMatthias Ringwald void hci_request_sco_can_send_now_event(void){ 503d057580eSMatthias Ringwald hci_stack->sco_waiting_for_can_send_now = 1; 504d057580eSMatthias Ringwald hci_notify_if_sco_can_send_now(); 505d057580eSMatthias Ringwald } 50635454696SMatthias Ringwald #endif 507d057580eSMatthias Ringwald 50895d71764SMatthias Ringwald // used for internal checks in l2cap.c 509c8b9416aS[email protected] int hci_is_packet_buffer_reserved(void){ 510c8b9416aS[email protected] return hci_stack->hci_packet_buffer_reserved; 511c8b9416aS[email protected] } 512c8b9416aS[email protected] 5136b4af23dS[email protected] // reserves outgoing packet buffer. @returns 1 if successful 5146b4af23dS[email protected] int hci_reserve_packet_buffer(void){ 5159d14b626S[email protected] if (hci_stack->hci_packet_buffer_reserved) { 5169d14b626S[email protected] log_error("hci_reserve_packet_buffer called but buffer already reserved"); 5179d14b626S[email protected] return 0; 5189d14b626S[email protected] } 5196b4af23dS[email protected] hci_stack->hci_packet_buffer_reserved = 1; 5206b4af23dS[email protected] return 1; 5216b4af23dS[email protected] } 5226b4af23dS[email protected] 52368a0fcf7S[email protected] void hci_release_packet_buffer(void){ 52468a0fcf7S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 52568a0fcf7S[email protected] } 52668a0fcf7S[email protected] 5276b4af23dS[email protected] // assumption: synchronous implementations don't provide can_send_packet_now as they don't keep the buffer after the call 5287f02f414SMatthias Ringwald static int hci_transport_synchronous(void){ 5296b4af23dS[email protected] return hci_stack->hci_transport->can_send_packet_now == NULL; 5306b4af23dS[email protected] } 5316b4af23dS[email protected] 532452cf3bbS[email protected] static int hci_send_acl_packet_fragments(hci_connection_t *connection){ 533452cf3bbS[email protected] 534452cf3bbS[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); 535452cf3bbS[email protected] 536452cf3bbS[email protected] // max ACL data packet length depends on connection type (LE vs. Classic) and available buffers 537452cf3bbS[email protected] uint16_t max_acl_data_packet_length = hci_stack->acl_data_packet_length; 538452cf3bbS[email protected] if (hci_is_le_connection(connection) && hci_stack->le_data_packets_length > 0){ 539452cf3bbS[email protected] max_acl_data_packet_length = hci_stack->le_data_packets_length; 540452cf3bbS[email protected] } 541452cf3bbS[email protected] 542452cf3bbS[email protected] // testing: reduce buffer to minimum 543452cf3bbS[email protected] // max_acl_data_packet_length = 52; 544452cf3bbS[email protected] 545d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments entered"); 546d999b54eSMatthias Ringwald 547452cf3bbS[email protected] int err; 548452cf3bbS[email protected] // multiple packets could be send on a synchronous HCI transport 549452cf3bbS[email protected] while (1){ 550452cf3bbS[email protected] 551d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop entered"); 552d999b54eSMatthias Ringwald 553452cf3bbS[email protected] // get current data 554452cf3bbS[email protected] const uint16_t acl_header_pos = hci_stack->acl_fragmentation_pos - 4; 555452cf3bbS[email protected] int current_acl_data_packet_length = hci_stack->acl_fragmentation_total_size - hci_stack->acl_fragmentation_pos; 556452cf3bbS[email protected] int more_fragments = 0; 557452cf3bbS[email protected] 558452cf3bbS[email protected] // if ACL packet is larger than Bluetooth packet buffer, only send max_acl_data_packet_length 559452cf3bbS[email protected] if (current_acl_data_packet_length > max_acl_data_packet_length){ 560452cf3bbS[email protected] more_fragments = 1; 561452cf3bbS[email protected] current_acl_data_packet_length = max_acl_data_packet_length; 562452cf3bbS[email protected] } 563452cf3bbS[email protected] 564452cf3bbS[email protected] // copy handle_and_flags if not first fragment and update packet boundary flags to be 01 (continuing fragmnent) 565452cf3bbS[email protected] if (acl_header_pos > 0){ 566f8fbdce0SMatthias Ringwald uint16_t handle_and_flags = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 567452cf3bbS[email protected] handle_and_flags = (handle_and_flags & 0xcfff) | (1 << 12); 568f8fbdce0SMatthias Ringwald little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos, handle_and_flags); 569452cf3bbS[email protected] } 570452cf3bbS[email protected] 571452cf3bbS[email protected] // update header len 572f8fbdce0SMatthias Ringwald little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos + 2, current_acl_data_packet_length); 573452cf3bbS[email protected] 574452cf3bbS[email protected] // count packet 575452cf3bbS[email protected] connection->num_acl_packets_sent++; 576f04a0c31SMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop before send (more fragments %d)", more_fragments); 577d999b54eSMatthias Ringwald 578d999b54eSMatthias Ringwald // update state for next fragment (if any) as "transport done" might be sent during send_packet already 579d999b54eSMatthias Ringwald if (more_fragments){ 580d999b54eSMatthias Ringwald // update start of next fragment to send 581d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_pos += current_acl_data_packet_length; 582d999b54eSMatthias Ringwald } else { 583d999b54eSMatthias Ringwald // done 584d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_pos = 0; 585d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_total_size = 0; 586d999b54eSMatthias Ringwald } 587452cf3bbS[email protected] 588452cf3bbS[email protected] // send packet 589452cf3bbS[email protected] uint8_t * packet = &hci_stack->hci_packet_buffer[acl_header_pos]; 590452cf3bbS[email protected] const int size = current_acl_data_packet_length + 4; 5915bb5bc3eS[email protected] hci_dump_packet(HCI_ACL_DATA_PACKET, 0, packet, size); 592452cf3bbS[email protected] err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size); 593452cf3bbS[email protected] 594f04a0c31SMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop after send (more fragments %d)", more_fragments); 595d999b54eSMatthias Ringwald 596452cf3bbS[email protected] // done yet? 597452cf3bbS[email protected] if (!more_fragments) break; 598452cf3bbS[email protected] 599452cf3bbS[email protected] // can send more? 600452cf3bbS[email protected] if (!hci_can_send_prepared_acl_packet_now(connection->con_handle)) return err; 601452cf3bbS[email protected] } 602452cf3bbS[email protected] 603d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop over"); 604452cf3bbS[email protected] 605d051460cS[email protected] // release buffer now for synchronous transport 606203bace6S[email protected] if (hci_transport_synchronous()){ 607452cf3bbS[email protected] hci_release_packet_buffer(); 60863fa3374SMatthias Ringwald // notify upper stack that it might be possible to send again 60963fa3374SMatthias Ringwald uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; 610d6b06661SMatthias Ringwald hci_emit_event(&event[0], sizeof(event), 0); // don't dump 611452cf3bbS[email protected] } 612452cf3bbS[email protected] 613452cf3bbS[email protected] return err; 614452cf3bbS[email protected] } 615452cf3bbS[email protected] 616826f7347S[email protected] // pre: caller has reserved the packet buffer 617826f7347S[email protected] int hci_send_acl_packet_buffer(int size){ 6187856c818Smatthias.ringwald 619452cf3bbS[email protected] // log_info("hci_send_acl_packet_buffer size %u", size); 620452cf3bbS[email protected] 621826f7347S[email protected] if (!hci_stack->hci_packet_buffer_reserved) { 622826f7347S[email protected] log_error("hci_send_acl_packet_buffer called without reserving packet buffer"); 623826f7347S[email protected] return 0; 624826f7347S[email protected] } 625826f7347S[email protected] 626d713a683S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 627d713a683S[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 628d713a683S[email protected] 629826f7347S[email protected] // check for free places on Bluetooth module 630d713a683S[email protected] if (!hci_can_send_prepared_acl_packet_now(con_handle)) { 631826f7347S[email protected] log_error("hci_send_acl_packet_buffer called but no free ACL buffers on controller"); 63297b61c7bS[email protected] hci_release_packet_buffer(); 63397b61c7bS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 63497b61c7bS[email protected] } 6356218e6f1Smatthias.ringwald 6365061f3afS[email protected] hci_connection_t *connection = hci_connection_for_handle( con_handle); 63797b61c7bS[email protected] if (!connection) { 6385fa0b7cfS[email protected] log_error("hci_send_acl_packet_buffer called but no connection for handle 0x%04x", con_handle); 63997b61c7bS[email protected] hci_release_packet_buffer(); 64097b61c7bS[email protected] return 0; 64197b61c7bS[email protected] } 64252db98b2SMatthias Ringwald 64352db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 64456cf178bSmatthias.ringwald hci_connection_timestamp(connection); 64552db98b2SMatthias Ringwald #endif 64656cf178bSmatthias.ringwald 647452cf3bbS[email protected] // hci_dump_packet( HCI_ACL_DATA_PACKET, 0, packet, size); 6487856c818Smatthias.ringwald 649452cf3bbS[email protected] // setup data 650452cf3bbS[email protected] hci_stack->acl_fragmentation_total_size = size; 651452cf3bbS[email protected] hci_stack->acl_fragmentation_pos = 4; // start of L2CAP packet 6526218e6f1Smatthias.ringwald 653452cf3bbS[email protected] return hci_send_acl_packet_fragments(connection); 654ee091cf1Smatthias.ringwald } 655ee091cf1Smatthias.ringwald 65635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 65744d0e3d5S[email protected] // pre: caller has reserved the packet buffer 65844d0e3d5S[email protected] int hci_send_sco_packet_buffer(int size){ 65944d0e3d5S[email protected] 66044d0e3d5S[email protected] // log_info("hci_send_acl_packet_buffer size %u", size); 66144d0e3d5S[email protected] 66244d0e3d5S[email protected] if (!hci_stack->hci_packet_buffer_reserved) { 66344d0e3d5S[email protected] log_error("hci_send_acl_packet_buffer called without reserving packet buffer"); 66444d0e3d5S[email protected] return 0; 66544d0e3d5S[email protected] } 66644d0e3d5S[email protected] 66744d0e3d5S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 6684b3e1e19SMatthias Ringwald 6694b3e1e19SMatthias Ringwald // skip checks in loopback mode 6704b3e1e19SMatthias Ringwald if (!hci_stack->loopback_mode){ 67144d0e3d5S[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); // same for ACL and SCO 67244d0e3d5S[email protected] 67344d0e3d5S[email protected] // check for free places on Bluetooth module 674701e3307SMatthias Ringwald if (!hci_can_send_prepared_sco_packet_now()) { 67544d0e3d5S[email protected] log_error("hci_send_sco_packet_buffer called but no free ACL buffers on controller"); 67644d0e3d5S[email protected] hci_release_packet_buffer(); 67744d0e3d5S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 67844d0e3d5S[email protected] } 67944d0e3d5S[email protected] 680e35edcc1S[email protected] // track send packet in connection struct 681e35edcc1S[email protected] hci_connection_t *connection = hci_connection_for_handle( con_handle); 682e35edcc1S[email protected] if (!connection) { 683e35edcc1S[email protected] log_error("hci_send_sco_packet_buffer called but no connection for handle 0x%04x", con_handle); 684e35edcc1S[email protected] hci_release_packet_buffer(); 685e35edcc1S[email protected] return 0; 686e35edcc1S[email protected] } 687e35edcc1S[email protected] connection->num_sco_packets_sent++; 6884b3e1e19SMatthias Ringwald } 68944d0e3d5S[email protected] 69044d0e3d5S[email protected] hci_dump_packet( HCI_SCO_DATA_PACKET, 0, packet, size); 691543e835cSMatthias Ringwald int err = hci_stack->hci_transport->send_packet(HCI_SCO_DATA_PACKET, packet, size); 692543e835cSMatthias Ringwald 693543e835cSMatthias Ringwald if (hci_transport_synchronous()){ 694543e835cSMatthias Ringwald hci_release_packet_buffer(); 69563fa3374SMatthias Ringwald // notify upper stack that it might be possible to send again 69663fa3374SMatthias Ringwald uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; 697d6b06661SMatthias Ringwald hci_emit_event(&event[0], sizeof(event), 0); // don't dump 698543e835cSMatthias Ringwald } 699543e835cSMatthias Ringwald 700543e835cSMatthias Ringwald return err; 70144d0e3d5S[email protected] } 70235454696SMatthias Ringwald #endif 70344d0e3d5S[email protected] 70416833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){ 7057856c818Smatthias.ringwald 706e76a89eeS[email protected] // log_info("acl_handler: size %u", size); 707e76a89eeS[email protected] 7087856c818Smatthias.ringwald // get info 7097856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 7105061f3afS[email protected] hci_connection_t *conn = hci_connection_for_handle(con_handle); 7117856c818Smatthias.ringwald uint8_t acl_flags = READ_ACL_FLAGS(packet); 7127856c818Smatthias.ringwald uint16_t acl_length = READ_ACL_LENGTH(packet); 7137856c818Smatthias.ringwald 7147856c818Smatthias.ringwald // ignore non-registered handle 7157856c818Smatthias.ringwald if (!conn){ 7169da54300S[email protected] log_error( "hci.c: acl_handler called with non-registered handle %u!" , con_handle); 7177856c818Smatthias.ringwald return; 7187856c818Smatthias.ringwald } 7197856c818Smatthias.ringwald 720e76a89eeS[email protected] // assert packet is complete 7219ecc3e17S[email protected] if (acl_length + 4 != size){ 722f04a0c31SMatthias Ringwald log_error("hci.c: acl_handler called with ACL packet of wrong size %d, expected %u => dropping packet", size, acl_length + 4); 723e76a89eeS[email protected] return; 724e76a89eeS[email protected] } 725e76a89eeS[email protected] 72652db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 7277856c818Smatthias.ringwald // update idle timestamp 7287856c818Smatthias.ringwald hci_connection_timestamp(conn); 72952db98b2SMatthias Ringwald #endif 7307856c818Smatthias.ringwald 7312b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 7322b838201SMatthias Ringwald hci_stack->host_completed_packets = 1; 7332b838201SMatthias Ringwald conn->num_packets_completed++; 7342b838201SMatthias Ringwald #endif 7352b838201SMatthias Ringwald 7367856c818Smatthias.ringwald // handle different packet types 7377856c818Smatthias.ringwald switch (acl_flags & 0x03) { 7387856c818Smatthias.ringwald 7397856c818Smatthias.ringwald case 0x01: // continuation fragment 7407856c818Smatthias.ringwald 7410ca847afS[email protected] // sanity checks 7427856c818Smatthias.ringwald if (conn->acl_recombination_pos == 0) { 7439da54300S[email protected] log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x", con_handle); 7447856c818Smatthias.ringwald return; 7457856c818Smatthias.ringwald } 7460ca847afS[email protected] if (conn->acl_recombination_pos + acl_length > 4 + HCI_ACL_BUFFER_SIZE){ 7470ca847afS[email protected] log_error( "ACL Cont Fragment to large: combined packet %u > buffer size %u for handle 0x%02x", 7480ca847afS[email protected] conn->acl_recombination_pos + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle); 7490ca847afS[email protected] conn->acl_recombination_pos = 0; 7500ca847afS[email protected] return; 7510ca847afS[email protected] } 7527856c818Smatthias.ringwald 7537856c818Smatthias.ringwald // append fragment payload (header already stored) 754ec6321eeS[email protected] memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + conn->acl_recombination_pos], &packet[4], acl_length ); 7557856c818Smatthias.ringwald conn->acl_recombination_pos += acl_length; 7567856c818Smatthias.ringwald 7579da54300S[email protected] // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u", acl_length, 758decc01a8Smatthias.ringwald // conn->acl_recombination_pos, conn->acl_recombination_length); 7597856c818Smatthias.ringwald 7607856c818Smatthias.ringwald // forward complete L2CAP packet if complete. 7617856c818Smatthias.ringwald if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header 762d6b06661SMatthias Ringwald hci_emit_acl_packet(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], conn->acl_recombination_pos); 7637856c818Smatthias.ringwald // reset recombination buffer 7647856c818Smatthias.ringwald conn->acl_recombination_length = 0; 7657856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 7667856c818Smatthias.ringwald } 7677856c818Smatthias.ringwald break; 7687856c818Smatthias.ringwald 7697856c818Smatthias.ringwald case 0x02: { // first fragment 7707856c818Smatthias.ringwald 77123a77e1aS[email protected] // sanity check 77223a77e1aS[email protected] if (conn->acl_recombination_pos) { 77323a77e1aS[email protected] log_error( "ACL First Fragment but data in buffer for handle 0x%02x, dropping stale fragments", con_handle); 77423a77e1aS[email protected] conn->acl_recombination_pos = 0; 77523a77e1aS[email protected] } 77623a77e1aS[email protected] 7777856c818Smatthias.ringwald // peek into L2CAP packet! 7787856c818Smatthias.ringwald uint16_t l2cap_length = READ_L2CAP_LENGTH( packet ); 7797856c818Smatthias.ringwald 7809da54300S[email protected] // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u", acl_length, l2cap_length); 781decc01a8Smatthias.ringwald 7827856c818Smatthias.ringwald // compare fragment size to L2CAP packet size 7837856c818Smatthias.ringwald if (acl_length >= l2cap_length + 4){ 7847856c818Smatthias.ringwald // forward fragment as L2CAP packet 785d6b06661SMatthias Ringwald hci_emit_acl_packet(packet, acl_length + 4); 7867856c818Smatthias.ringwald } else { 7870ca847afS[email protected] 7880ca847afS[email protected] if (acl_length > HCI_ACL_BUFFER_SIZE){ 7890ca847afS[email protected] log_error( "ACL First Fragment to large: fragment %u > buffer size %u for handle 0x%02x", 7900ca847afS[email protected] 4 + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle); 7910ca847afS[email protected] return; 7920ca847afS[email protected] } 7930ca847afS[email protected] 7947856c818Smatthias.ringwald // store first fragment and tweak acl length for complete package 795ec6321eeS[email protected] memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], packet, acl_length + 4); 7967856c818Smatthias.ringwald conn->acl_recombination_pos = acl_length + 4; 7977856c818Smatthias.ringwald conn->acl_recombination_length = l2cap_length; 798f8fbdce0SMatthias Ringwald little_endian_store_16(conn->acl_recombination_buffer, HCI_INCOMING_PRE_BUFFER_SIZE + 2, l2cap_length +4); 7997856c818Smatthias.ringwald } 8007856c818Smatthias.ringwald break; 8017856c818Smatthias.ringwald 8027856c818Smatthias.ringwald } 8037856c818Smatthias.ringwald default: 8049da54300S[email protected] log_error( "hci.c: acl_handler called with invalid packet boundary flags %u", acl_flags & 0x03); 8057856c818Smatthias.ringwald return; 8067856c818Smatthias.ringwald } 80794ab26f8Smatthias.ringwald 80894ab26f8Smatthias.ringwald // execute main loop 80994ab26f8Smatthias.ringwald hci_run(); 81016833f0aSmatthias.ringwald } 81122909952Smatthias.ringwald 81267a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){ 8139da54300S[email protected] log_info("Connection closed: handle 0x%x, %s", conn->con_handle, bd_addr_to_str(conn->address)); 8143c4d4b90Smatthias.ringwald 815b3264428SMatthias Ringwald #ifdef ENABLE_CLASSIC 816ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 817ee752bb8SMatthias Ringwald int addr_type = conn->address_type; 818ee752bb8SMatthias Ringwald #endif 819b3264428SMatthias Ringwald #endif 820ee752bb8SMatthias Ringwald 821528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&conn->timeout); 822c785ef68Smatthias.ringwald 823665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 824a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 8253c4d4b90Smatthias.ringwald 8263c4d4b90Smatthias.ringwald // now it's gone 827c7e0c5f6Smatthias.ringwald hci_emit_nr_connections_changed(); 828ee752bb8SMatthias Ringwald 829b3264428SMatthias Ringwald #ifdef ENABLE_CLASSIC 830034e9b53SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 831ee752bb8SMatthias Ringwald // update SCO 832ee752bb8SMatthias Ringwald if (addr_type == BD_ADDR_TYPE_SCO && hci_stack->hci_transport && hci_stack->hci_transport->set_sco_config){ 833ee752bb8SMatthias Ringwald hci_stack->hci_transport->set_sco_config(hci_stack->sco_voice_setting_active, hci_number_sco_connections()); 834ee752bb8SMatthias Ringwald } 835034e9b53SMatthias Ringwald #endif 836b3264428SMatthias Ringwald #endif 837c7e0c5f6Smatthias.ringwald } 838c7e0c5f6Smatthias.ringwald 83935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 84035454696SMatthias Ringwald 8410c042179S[email protected] static const uint16_t packet_type_sizes[] = { 8428f8108aaSmatthias.ringwald 0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE, 8438f8108aaSmatthias.ringwald HCI_ACL_DH1_SIZE, 0, 0, 0, 8448f8108aaSmatthias.ringwald HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE, 8458f8108aaSmatthias.ringwald HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE 8468f8108aaSmatthias.ringwald }; 84765389bfcS[email protected] static const uint8_t packet_type_feature_requirement_bit[] = { 84865389bfcS[email protected] 0, // 3 slot packets 84965389bfcS[email protected] 1, // 5 slot packets 85065389bfcS[email protected] 25, // EDR 2 mpbs 85165389bfcS[email protected] 26, // EDR 3 mbps 85265389bfcS[email protected] 39, // 3 slot EDR packts 85365389bfcS[email protected] 40, // 5 slot EDR packet 85465389bfcS[email protected] }; 85565389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = { 85665389bfcS[email protected] 0x0f00, // 3 slot packets 85765389bfcS[email protected] 0xf000, // 5 slot packets 85865389bfcS[email protected] 0x1102, // EDR 2 mpbs 85965389bfcS[email protected] 0x2204, // EDR 3 mbps 86065389bfcS[email protected] 0x0300, // 3 slot EDR packts 86165389bfcS[email protected] 0x3000, // 5 slot EDR packet 86265389bfcS[email protected] }; 8638f8108aaSmatthias.ringwald 86465389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){ 86565389bfcS[email protected] // enable packet types based on size 8668f8108aaSmatthias.ringwald uint16_t packet_types = 0; 867f16a69bbS[email protected] unsigned int i; 8688f8108aaSmatthias.ringwald for (i=0;i<16;i++){ 8698f8108aaSmatthias.ringwald if (packet_type_sizes[i] == 0) continue; 8708f8108aaSmatthias.ringwald if (packet_type_sizes[i] <= buffer_size){ 8718f8108aaSmatthias.ringwald packet_types |= 1 << i; 8728f8108aaSmatthias.ringwald } 8738f8108aaSmatthias.ringwald } 87465389bfcS[email protected] // disable packet types due to missing local supported features 87565389bfcS[email protected] for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){ 876f04a0c31SMatthias Ringwald unsigned int bit_idx = packet_type_feature_requirement_bit[i]; 87765389bfcS[email protected] int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0; 87865389bfcS[email protected] if (feature_set) continue; 87965389bfcS[email protected] log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]); 88065389bfcS[email protected] packet_types &= ~packet_type_feature_packet_mask[i]; 88165389bfcS[email protected] } 8828f8108aaSmatthias.ringwald // flip bits for "may not be used" 8838f8108aaSmatthias.ringwald packet_types ^= 0x3306; 8848f8108aaSmatthias.ringwald return packet_types; 8858f8108aaSmatthias.ringwald } 8868f8108aaSmatthias.ringwald 8878f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){ 8883a9fb326S[email protected] return hci_stack->packet_types; 8898f8108aaSmatthias.ringwald } 89035454696SMatthias Ringwald #endif 8918f8108aaSmatthias.ringwald 892facf93fdS[email protected] uint8_t* hci_get_outgoing_packet_buffer(void){ 8937dc17943Smatthias.ringwald // hci packet buffer is >= acl data packet length 8943a9fb326S[email protected] return hci_stack->hci_packet_buffer; 8957dc17943Smatthias.ringwald } 8967dc17943Smatthias.ringwald 897f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){ 8983a9fb326S[email protected] return hci_stack->acl_data_packet_length; 8997dc17943Smatthias.ringwald } 9007dc17943Smatthias.ringwald 90106b9e820SMatthias Ringwald #ifdef ENABLE_CLASSIC 9023e68d23dSMatthias Ringwald int hci_extended_sco_link_supported(void){ 9033e68d23dSMatthias Ringwald // No. 31, byte 3, bit 7 9043e68d23dSMatthias Ringwald return (hci_stack->local_supported_features[3] & (1 << 7)) != 0; 9053e68d23dSMatthias Ringwald } 90606b9e820SMatthias Ringwald #endif 9073e68d23dSMatthias Ringwald 9086ac9a97eS[email protected] int hci_non_flushable_packet_boundary_flag_supported(void){ 9096ac9a97eS[email protected] // No. 54, byte 6, bit 6 9106ac9a97eS[email protected] return (hci_stack->local_supported_features[6] & (1 << 6)) != 0; 9116ac9a97eS[email protected] } 9126ac9a97eS[email protected] 91315a95bd5SMatthias Ringwald static int gap_ssp_supported(void){ 9146ac9a97eS[email protected] // No. 51, byte 6, bit 3 9153a9fb326S[email protected] return (hci_stack->local_supported_features[6] & (1 << 3)) != 0; 916f5d8d141S[email protected] } 917f5d8d141S[email protected] 9187f02f414SMatthias Ringwald static int hci_classic_supported(void){ 91935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 9206ac9a97eS[email protected] // No. 37, byte 4, bit 5, = No BR/EDR Support 9213a9fb326S[email protected] return (hci_stack->local_supported_features[4] & (1 << 5)) == 0; 92235454696SMatthias Ringwald #else 92335454696SMatthias Ringwald return 0; 92435454696SMatthias Ringwald #endif 925f5d8d141S[email protected] } 926f5d8d141S[email protected] 9277f02f414SMatthias Ringwald static int hci_le_supported(void){ 928a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 9296ac9a97eS[email protected] // No. 37, byte 4, bit 6 = LE Supported (Controller) 9303a9fb326S[email protected] return (hci_stack->local_supported_features[4] & (1 << 6)) != 0; 931f5d8d141S[email protected] #else 932f5d8d141S[email protected] return 0; 933f5d8d141S[email protected] #endif 934f5d8d141S[email protected] } 935f5d8d141S[email protected] 936b95a5a35SMatthias Ringwald #ifdef ENABLE_BLE 937b95a5a35SMatthias Ringwald 938b95a5a35SMatthias Ringwald /** 939b95a5a35SMatthias Ringwald * @brief Get addr type and address used for LE in Advertisements, Scan Responses, 940b95a5a35SMatthias Ringwald */ 941b95a5a35SMatthias Ringwald void gap_le_get_own_address(uint8_t * addr_type, bd_addr_t addr){ 942b95a5a35SMatthias Ringwald *addr_type = hci_stack->le_own_addr_type; 943b95a5a35SMatthias Ringwald if (hci_stack->le_own_addr_type){ 944b95a5a35SMatthias Ringwald memcpy(addr, hci_stack->le_random_address, 6); 94569a97523S[email protected] } else { 9463a9fb326S[email protected] memcpy(addr, hci_stack->local_bd_addr, 6); 94769a97523S[email protected] } 94869a97523S[email protected] } 94969a97523S[email protected] 950e8c8828eSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 95139677e66SMatthias Ringwald void le_handle_advertisement_report(uint8_t *packet, int size){ 9529ec2630cSMatthias Ringwald 9539ec2630cSMatthias Ringwald UNUSED(size); 9549ec2630cSMatthias Ringwald 955d1dc057bS[email protected] int offset = 3; 956d1dc057bS[email protected] int num_reports = packet[offset]; 957d1dc057bS[email protected] offset += 1; 958d1dc057bS[email protected] 95957c9da5bS[email protected] int i; 9604f4e0224SMatthias Ringwald // log_info("HCI: handle adv report with num reports: %d", num_reports); 96103fbe9c6S[email protected] uint8_t event[12 + LE_ADVERTISING_DATA_SIZE]; // use upper bound to avoid var size automatic var 96257c9da5bS[email protected] for (i=0; i<num_reports;i++){ 963210c6774S[email protected] uint8_t data_length = packet[offset + 8]; 964a0aac9c4S[email protected] uint8_t event_size = 10 + data_length; 965d1dc057bS[email protected] int pos = 0; 966045013feSMatthias Ringwald event[pos++] = GAP_EVENT_ADVERTISING_REPORT; 96757c9da5bS[email protected] event[pos++] = event_size; 968210c6774S[email protected] memcpy(&event[pos], &packet[offset], 1+1+6); // event type + address type + address 969d1dc057bS[email protected] offset += 8; 970d1dc057bS[email protected] pos += 8; 971d1dc057bS[email protected] event[pos++] = packet[offset + 1 + data_length]; // rssi 972d1dc057bS[email protected] event[pos++] = packet[offset++]; //data_length; 973d1dc057bS[email protected] memcpy(&event[pos], &packet[offset], data_length); 97457c9da5bS[email protected] pos += data_length; 975d1dc057bS[email protected] offset += data_length + 1; // rssi 976d6b06661SMatthias Ringwald hci_emit_event(event, pos, 1); 97757c9da5bS[email protected] } 97857c9da5bS[email protected] } 979b2f949feS[email protected] #endif 980e8c8828eSMatthias Ringwald #endif 98157c9da5bS[email protected] 982cd77dd38SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 98306b9e820SMatthias Ringwald 98496b53536SMatthias Ringwald static uint32_t hci_transport_uart_get_main_baud_rate(void){ 98596b53536SMatthias Ringwald if (!hci_stack->config) return 0; 9869796ebeaSMatthias Ringwald uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 98796b53536SMatthias Ringwald // Limit baud rate for Broadcom chipsets to 3 mbps 98861f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION && baud_rate > 3000000){ 98996b53536SMatthias Ringwald baud_rate = 3000000; 99096b53536SMatthias Ringwald } 99196b53536SMatthias Ringwald return baud_rate; 99296b53536SMatthias Ringwald } 99396b53536SMatthias Ringwald 994ec820d77SMatthias Ringwald static void hci_initialization_timeout_handler(btstack_timer_source_t * ds){ 9959ec2630cSMatthias Ringwald UNUSED(ds); 9969ec2630cSMatthias Ringwald 9970305bdeaSMatthias Ringwald switch (hci_stack->substate){ 9980305bdeaSMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 9997b0d7667SMatthias Ringwald log_info("Resend HCI Reset"); 10000305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET; 10017b0d7667SMatthias Ringwald hci_stack->num_cmd_packets = 1; 10020305bdeaSMatthias Ringwald hci_run(); 10030305bdeaSMatthias Ringwald break; 10049f007422SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET: 10059f007422SMatthias Ringwald log_info("Resend HCI Reset - CSR Warm Boot with Link Reset"); 10069f007422SMatthias Ringwald if (hci_stack->hci_transport->reset_link){ 10079f007422SMatthias Ringwald hci_stack->hci_transport->reset_link(); 10089f007422SMatthias Ringwald } 1009202c8a4cSMatthias Ringwald // no break - explicit fallthrough to HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT 1010e47e68c7SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT: 1011e47e68c7SMatthias Ringwald log_info("Resend HCI Reset - CSR Warm Boot"); 1012e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT; 1013e47e68c7SMatthias Ringwald hci_stack->num_cmd_packets = 1; 1014e47e68c7SMatthias Ringwald hci_run(); 1015688c2635SMatthias Ringwald break; 10167224be7eSMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE: 10177224be7eSMatthias Ringwald if (hci_stack->hci_transport->set_baudrate){ 101896b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1019cd724cb7SMatthias Ringwald log_info("Local baud rate change to %"PRIu32"(timeout handler)", baud_rate); 10207dd9d0ecSMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 10217224be7eSMatthias Ringwald } 1022834bce8cSMatthias Ringwald // For CSR, HCI Reset is sent on new baud rate. Don't forget to reset link for H5/BCSP 102361f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO){ 1024834bce8cSMatthias Ringwald if (hci_stack->hci_transport->reset_link){ 1025834bce8cSMatthias Ringwald log_info("Link Reset"); 1026834bce8cSMatthias Ringwald hci_stack->hci_transport->reset_link(); 1027834bce8cSMatthias Ringwald } 1028772a36d3SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT; 1029772a36d3SMatthias Ringwald hci_run(); 1030772a36d3SMatthias Ringwald } 10314696bddbSMatthias Ringwald break; 1032559961d0SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_BCM_DELAY: 1033559961d0SMatthias Ringwald // otherwise continue 1034559961d0SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 1035559961d0SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 1036559961d0SMatthias Ringwald break; 10370305bdeaSMatthias Ringwald default: 10380305bdeaSMatthias Ringwald break; 10390305bdeaSMatthias Ringwald } 10400305bdeaSMatthias Ringwald } 104106b9e820SMatthias Ringwald #endif 10420305bdeaSMatthias Ringwald 104371de195eSMatthias Ringwald static void hci_initializing_next_state(void){ 104474b323a9SMatthias Ringwald hci_stack->substate = (hci_substate_t )( ((int) hci_stack->substate) + 1); 104574b323a9SMatthias Ringwald } 104674b323a9SMatthias Ringwald 104774b323a9SMatthias Ringwald // assumption: hci_can_send_command_packet_now() == true 104871de195eSMatthias Ringwald static void hci_initializing_run(void){ 1049148ca237SMatthias Ringwald log_debug("hci_initializing_run: substate %u, can send %u", hci_stack->substate, hci_can_send_command_packet_now()); 105074b323a9SMatthias Ringwald switch (hci_stack->substate){ 105174b323a9SMatthias Ringwald case HCI_INIT_SEND_RESET: 105274b323a9SMatthias Ringwald hci_state_reset(); 1053a0cf2f3fSMatthias Ringwald 105406b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 10550305bdeaSMatthias Ringwald // prepare reset if command complete not received in 100ms 1056659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1057528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1058528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1059a0cf2f3fSMatthias Ringwald #endif 10600305bdeaSMatthias Ringwald // send command 106174b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 10620305bdeaSMatthias Ringwald hci_send_cmd(&hci_reset); 106374b323a9SMatthias Ringwald break; 106476fcb19bSMatthias Ringwald case HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION: 106576fcb19bSMatthias Ringwald hci_send_cmd(&hci_read_local_version_information); 106676fcb19bSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION; 106776fcb19bSMatthias Ringwald break; 1068e90bae01SMatthias Ringwald case HCI_INIT_SEND_READ_LOCAL_NAME: 1069e90bae01SMatthias Ringwald hci_send_cmd(&hci_read_local_name); 1070e90bae01SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_NAME; 1071e90bae01SMatthias Ringwald break; 107206b9e820SMatthias Ringwald 107306b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 1074e47e68c7SMatthias Ringwald case HCI_INIT_SEND_RESET_CSR_WARM_BOOT: 1075e47e68c7SMatthias Ringwald hci_state_reset(); 1076e47e68c7SMatthias Ringwald // prepare reset if command complete not received in 100ms 1077659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1078528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1079528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1080e47e68c7SMatthias Ringwald // send command 1081e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT; 1082e47e68c7SMatthias Ringwald hci_send_cmd(&hci_reset); 1083e47e68c7SMatthias Ringwald break; 10848d29070eSMatthias Ringwald case HCI_INIT_SEND_RESET_ST_WARM_BOOT: 10858d29070eSMatthias Ringwald hci_state_reset(); 10868d29070eSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT; 10878d29070eSMatthias Ringwald hci_send_cmd(&hci_reset); 10888d29070eSMatthias Ringwald break; 1089fab26ab3SMatthias Ringwald case HCI_INIT_SEND_BAUD_CHANGE: { 109096b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 10913fb36a29SMatthias Ringwald hci_stack->chipset->set_baudrate_command(baud_rate, hci_stack->hci_packet_buffer); 1092f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 109374b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE; 10940305bdeaSMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]); 10954696bddbSMatthias Ringwald // STLC25000D: baudrate change happens within 0.5 s after command was send, 10964696bddbSMatthias Ringwald // use timer to update baud rate after 100 ms (knowing exactly, when command was sent is non-trivial) 109761f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS){ 1098659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1099528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 11004696bddbSMatthias Ringwald } 110174b323a9SMatthias Ringwald break; 1102fab26ab3SMatthias Ringwald } 1103fab26ab3SMatthias Ringwald case HCI_INIT_SEND_BAUD_CHANGE_BCM: { 110496b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 11053fb36a29SMatthias Ringwald hci_stack->chipset->set_baudrate_command(baud_rate, hci_stack->hci_packet_buffer); 1106f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 1107eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE_BCM; 1108eb3a5314SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]); 1109eb3a5314SMatthias Ringwald break; 1110fab26ab3SMatthias Ringwald } 111174b323a9SMatthias Ringwald case HCI_INIT_CUSTOM_INIT: 111274b323a9SMatthias Ringwald // Custom initialization 11133fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->next_command){ 11143fb36a29SMatthias Ringwald int valid_cmd = (*hci_stack->chipset->next_command)(hci_stack->hci_packet_buffer); 111574b323a9SMatthias Ringwald if (valid_cmd){ 111674b323a9SMatthias Ringwald int size = 3 + hci_stack->hci_packet_buffer[2]; 1117f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 111874b323a9SMatthias Ringwald hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, hci_stack->hci_packet_buffer, size); 1119e47e68c7SMatthias Ringwald switch (valid_cmd) { 1120e47e68c7SMatthias Ringwald case 1: 1121e47e68c7SMatthias Ringwald default: 1122e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT; 1123e47e68c7SMatthias Ringwald break; 1124e47e68c7SMatthias Ringwald case 2: // CSR Warm Boot: Wait a bit, then send HCI Reset until HCI Command Complete 1125e47e68c7SMatthias Ringwald log_info("CSR Warm Boot"); 1126659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1127528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1128528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 112961f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO 1130772a36d3SMatthias Ringwald && hci_stack->config 11313fb36a29SMatthias Ringwald && hci_stack->chipset 11323fb36a29SMatthias Ringwald // && hci_stack->chipset->set_baudrate_command -- there's no such command 1133772a36d3SMatthias Ringwald && hci_stack->hci_transport->set_baudrate 11342caefae9SMatthias Ringwald && hci_transport_uart_get_main_baud_rate()){ 1135772a36d3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE; 1136772a36d3SMatthias Ringwald } else { 11379f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET; 1138772a36d3SMatthias Ringwald } 1139e47e68c7SMatthias Ringwald break; 1140e47e68c7SMatthias Ringwald } 11410305bdeaSMatthias Ringwald hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size); 114274b323a9SMatthias Ringwald break; 114374b323a9SMatthias Ringwald } 1144148ca237SMatthias Ringwald log_info("Init script done"); 114592a0d36dSMatthias Ringwald 1146559961d0SMatthias Ringwald // Init script download on Broadcom chipsets causes: 114761f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION){ 1148559961d0SMatthias Ringwald // - baud rate to reset, restore UART baud rate if needed 114992a0d36dSMatthias Ringwald int need_baud_change = hci_stack->config 11503fb36a29SMatthias Ringwald && hci_stack->chipset 11513fb36a29SMatthias Ringwald && hci_stack->chipset->set_baudrate_command 115292a0d36dSMatthias Ringwald && hci_stack->hci_transport->set_baudrate 11539796ebeaSMatthias Ringwald && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 115492a0d36dSMatthias Ringwald if (need_baud_change) { 11559796ebeaSMatthias Ringwald uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_init; 1156cd724cb7SMatthias Ringwald log_info("Local baud rate change to %"PRIu32" after init script (bcm)", baud_rate); 115792a0d36dSMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 115892a0d36dSMatthias Ringwald } 1159559961d0SMatthias Ringwald 1160559961d0SMatthias Ringwald // - RTS will raise during update, but manual RTS/CTS in WICED port on RedBear Duo cannot handle this 1161559961d0SMatthias Ringwald // -> Work around: wait a few milliseconds here. 1162559961d0SMatthias Ringwald log_info("BCM delay after init script"); 1163559961d0SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_BCM_DELAY; 1164559961d0SMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, 10); 1165559961d0SMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1166559961d0SMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1167559961d0SMatthias Ringwald break; 116892a0d36dSMatthias Ringwald } 116974b323a9SMatthias Ringwald } 117074b323a9SMatthias Ringwald // otherwise continue 1171a828a756SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 1172a828a756SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 1173a828a756SMatthias Ringwald break; 117453860077SMatthias Ringwald case HCI_INIT_SET_BD_ADDR: 117553860077SMatthias Ringwald log_info("Set Public BD ADDR to %s", bd_addr_to_str(hci_stack->custom_bd_addr)); 11763fb36a29SMatthias Ringwald hci_stack->chipset->set_bd_addr_command(hci_stack->custom_bd_addr, hci_stack->hci_packet_buffer); 1177f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 117853860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_BD_ADDR; 117953860077SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]); 118053860077SMatthias Ringwald break; 118106b9e820SMatthias Ringwald #endif 118206b9e820SMatthias Ringwald 118306b9e820SMatthias Ringwald case HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS: 118406b9e820SMatthias Ringwald log_info("Resend hci_read_local_supported_commands after CSR Warm Boot double reset"); 118506b9e820SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 118606b9e820SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 118706b9e820SMatthias Ringwald break; 118853860077SMatthias Ringwald case HCI_INIT_READ_BD_ADDR: 118953860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_BD_ADDR; 119053860077SMatthias Ringwald hci_send_cmd(&hci_read_bd_addr); 119153860077SMatthias Ringwald break; 119274b323a9SMatthias Ringwald case HCI_INIT_READ_BUFFER_SIZE: 119374b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_BUFFER_SIZE; 11940305bdeaSMatthias Ringwald hci_send_cmd(&hci_read_buffer_size); 119574b323a9SMatthias Ringwald break; 119653860077SMatthias Ringwald case HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES: 119753860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_FEATURES; 11980305bdeaSMatthias Ringwald hci_send_cmd(&hci_read_local_supported_features); 119974b323a9SMatthias Ringwald break; 12002b838201SMatthias Ringwald 12012b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 12022b838201SMatthias Ringwald case HCI_INIT_SET_CONTROLLER_TO_HOST_FLOW_CONTROL: 12032b838201SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_CONTROLLER_TO_HOST_FLOW_CONTROL; 12042b838201SMatthias Ringwald hci_send_cmd(&hci_set_controller_to_host_flow_control, 3); // ACL + SCO Flow Control 12052b838201SMatthias Ringwald break; 12062b838201SMatthias Ringwald case HCI_INIT_HOST_BUFFER_SIZE: 12072b838201SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_HOST_BUFFER_SIZE; 12082b838201SMatthias Ringwald hci_send_cmd(&hci_host_buffer_size, HCI_HOST_ACL_PACKET_LEN, HCI_HOST_SCO_PACKET_LEN, 12092b838201SMatthias Ringwald HCI_HOST_ACL_PACKET_NUM, HCI_HOST_SCO_PACKET_NUM); 12102b838201SMatthias Ringwald break; 12112b838201SMatthias Ringwald #endif 12122b838201SMatthias Ringwald 121374b323a9SMatthias Ringwald case HCI_INIT_SET_EVENT_MASK: 12140305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_EVENT_MASK; 121574b323a9SMatthias Ringwald if (hci_le_supported()){ 121674b323a9SMatthias Ringwald hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF); 121774b323a9SMatthias Ringwald } else { 121874b323a9SMatthias Ringwald // Kensington Bluetooth 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff... 121974b323a9SMatthias Ringwald hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF); 122074b323a9SMatthias Ringwald } 122174b323a9SMatthias Ringwald break; 12222b838201SMatthias Ringwald 122335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 122474b323a9SMatthias Ringwald case HCI_INIT_WRITE_SIMPLE_PAIRING_MODE: 122574b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SIMPLE_PAIRING_MODE; 12260305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable); 122774b323a9SMatthias Ringwald break; 122874b323a9SMatthias Ringwald case HCI_INIT_WRITE_PAGE_TIMEOUT: 122974b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_PAGE_TIMEOUT; 12300305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_page_timeout, 0x6000); // ca. 15 sec 123174b323a9SMatthias Ringwald break; 123274b323a9SMatthias Ringwald case HCI_INIT_WRITE_CLASS_OF_DEVICE: 123374b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_CLASS_OF_DEVICE; 12340305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device); 123574b323a9SMatthias Ringwald break; 123674b323a9SMatthias Ringwald case HCI_INIT_WRITE_LOCAL_NAME: 12370305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_LOCAL_NAME; 123874b323a9SMatthias Ringwald if (hci_stack->local_name){ 123974b323a9SMatthias Ringwald hci_send_cmd(&hci_write_local_name, hci_stack->local_name); 124074b323a9SMatthias Ringwald } else { 12417224be7eSMatthias Ringwald char local_name[8+17+1]; 12427224be7eSMatthias Ringwald // BTstack 11:22:33:44:55:66 12437224be7eSMatthias Ringwald memcpy(local_name, "BTstack ", 8); 12447224be7eSMatthias Ringwald memcpy(&local_name[8], bd_addr_to_str(hci_stack->local_bd_addr), 17); // strlen(bd_addr_to_str(...)) = 17 12457224be7eSMatthias Ringwald local_name[8+17] = '\0'; 124693bcd4d0SMatthias Ringwald log_info("---> Name %s", local_name); 124793bcd4d0SMatthias Ringwald hci_send_cmd(&hci_write_local_name, local_name); 124874b323a9SMatthias Ringwald } 124974b323a9SMatthias Ringwald break; 1250ff00ed1cSMatthias Ringwald case HCI_INIT_WRITE_EIR_DATA: 12518a114470SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_EIR_DATA; 1252ff00ed1cSMatthias Ringwald hci_send_cmd(&hci_write_extended_inquiry_response, 0, hci_stack->eir_data); 1253ff00ed1cSMatthias Ringwald break; 1254f6858d14SMatthias Ringwald case HCI_INIT_WRITE_INQUIRY_MODE: 1255f6858d14SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_INQUIRY_MODE; 12568a114470SMatthias Ringwald hci_send_cmd(&hci_write_inquiry_mode, (int) hci_stack->inquiry_mode); 1257f6858d14SMatthias Ringwald break; 125874b323a9SMatthias Ringwald case HCI_INIT_WRITE_SCAN_ENABLE: 125974b323a9SMatthias Ringwald hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan 126074b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SCAN_ENABLE; 126174b323a9SMatthias Ringwald break; 1262483c5078SMatthias Ringwald // only sent if ENABLE_SCO_OVER_HCI is defined 1263729ed62eSMatthias Ringwald case HCI_INIT_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE: 1264729ed62eSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE; 1265729ed62eSMatthias Ringwald hci_send_cmd(&hci_write_synchronous_flow_control_enable, 1); // SCO tracking enabled 1266729ed62eSMatthias Ringwald break; 1267483c5078SMatthias Ringwald case HCI_INIT_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING: 1268483c5078SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING; 1269483c5078SMatthias Ringwald hci_send_cmd(&hci_write_default_erroneous_data_reporting, 1); 1270483c5078SMatthias Ringwald break; 1271a42798c3SMatthias Ringwald // only sent if ENABLE_SCO_OVER_HCI and manufacturer is Broadcom 1272a42798c3SMatthias Ringwald case HCI_INIT_BCM_WRITE_SCO_PCM_INT: 1273a42798c3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT; 1274a42798c3SMatthias Ringwald log_info("BCM: Route SCO data via HCI transport"); 1275a42798c3SMatthias Ringwald hci_send_cmd(&hci_bcm_write_sco_pcm_int, 1, 0, 0, 0, 0); 1276a42798c3SMatthias Ringwald break; 1277a42798c3SMatthias Ringwald 127835454696SMatthias Ringwald #endif 1279a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 128074b323a9SMatthias Ringwald // LE INIT 128174b323a9SMatthias Ringwald case HCI_INIT_LE_READ_BUFFER_SIZE: 128274b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_READ_BUFFER_SIZE; 12830305bdeaSMatthias Ringwald hci_send_cmd(&hci_le_read_buffer_size); 128474b323a9SMatthias Ringwald break; 128574b323a9SMatthias Ringwald case HCI_INIT_WRITE_LE_HOST_SUPPORTED: 128674b323a9SMatthias Ringwald // LE Supported Host = 1, Simultaneous Host = 0 128774b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_LE_HOST_SUPPORTED; 12880305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_le_host_supported, 1, 0); 128974b323a9SMatthias Ringwald break; 1290b95a5a35SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 12913b6d4121SMatthias Ringwald case HCI_INIT_READ_WHITE_LIST_SIZE: 12923b6d4121SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_WHITE_LIST_SIZE; 12933b6d4121SMatthias Ringwald hci_send_cmd(&hci_le_read_white_list_size); 12943b6d4121SMatthias Ringwald break; 129574b323a9SMatthias Ringwald case HCI_INIT_LE_SET_SCAN_PARAMETERS: 1296b95a5a35SMatthias Ringwald // LE Scan Parameters: active scanning, 300 ms interval, 30 ms window, own address type, accept all advs 129774b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_SET_SCAN_PARAMETERS; 1298b95a5a35SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_parameters, 1, 0x1e0, 0x30, hci_stack->le_own_addr_type, 0); 129974b323a9SMatthias Ringwald break; 130074b323a9SMatthias Ringwald #endif 1301b95a5a35SMatthias Ringwald #endif 130274b323a9SMatthias Ringwald default: 130374b323a9SMatthias Ringwald return; 130474b323a9SMatthias Ringwald } 130555975f88SMatthias Ringwald } 130655975f88SMatthias Ringwald 1307a650ba4dSMatthias Ringwald static void hci_init_done(void){ 1308a650ba4dSMatthias Ringwald // done. tell the app 1309a650ba4dSMatthias Ringwald log_info("hci_init_done -> HCI_STATE_WORKING"); 1310a650ba4dSMatthias Ringwald hci_stack->state = HCI_STATE_WORKING; 1311a650ba4dSMatthias Ringwald hci_emit_state(); 1312a650ba4dSMatthias Ringwald hci_run(); 1313a650ba4dSMatthias Ringwald } 1314a650ba4dSMatthias Ringwald 13156155b3d3S[email protected] static void hci_initializing_event_handler(uint8_t * packet, uint16_t size){ 13169ec2630cSMatthias Ringwald UNUSED(size); 13179ec2630cSMatthias Ringwald 1318a2481739S[email protected] uint8_t command_completed = 0; 1319c4633093SMatthias Ringwald 13200e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE){ 1321f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 13226155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 1323a2481739S[email protected] command_completed = 1; 1324148ca237SMatthias Ringwald log_debug("Command complete for expected opcode %04x at substate %u", opcode, hci_stack->substate); 13256155b3d3S[email protected] } else { 1326d58dd308SMatthias Ringwald log_info("Command complete for different opcode %04x, expected %04x, at substate %u", opcode, hci_stack->last_cmd_opcode, hci_stack->substate); 13276155b3d3S[email protected] } 13286155b3d3S[email protected] } 13290f97eae7SMatthias Ringwald 13300e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_STATUS){ 13316155b3d3S[email protected] uint8_t status = packet[2]; 1332f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,4); 13336155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 13346155b3d3S[email protected] if (status){ 1335a2481739S[email protected] command_completed = 1; 1336148ca237SMatthias Ringwald log_debug("Command status error 0x%02x for expected opcode %04x at substate %u", status, opcode, hci_stack->substate); 13376155b3d3S[email protected] } else { 13386155b3d3S[email protected] log_info("Command status OK for expected opcode %04x, waiting for command complete", opcode); 13396155b3d3S[email protected] } 13406155b3d3S[email protected] } else { 1341148ca237SMatthias Ringwald log_debug("Command status for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode); 13426155b3d3S[email protected] } 13436155b3d3S[email protected] } 13440f97eae7SMatthias Ringwald 134506b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 134606b9e820SMatthias Ringwald 1347e47e68c7SMatthias Ringwald // Vendor == CSR 13480e2df43fSMatthias Ringwald if (hci_stack->substate == HCI_INIT_W4_CUSTOM_INIT && hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC){ 1349e47e68c7SMatthias Ringwald // TODO: track actual command 1350e47e68c7SMatthias Ringwald command_completed = 1; 1351e47e68c7SMatthias Ringwald } 1352a2481739S[email protected] 13534e9daa6fSMatthias Ringwald // Vendor == Toshiba 13540e2df43fSMatthias Ringwald if (hci_stack->substate == HCI_INIT_W4_SEND_BAUD_CHANGE && hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC){ 13554e9daa6fSMatthias Ringwald // TODO: track actual command 13564e9daa6fSMatthias Ringwald command_completed = 1; 13574e9daa6fSMatthias Ringwald } 13584e9daa6fSMatthias Ringwald 13590f97eae7SMatthias Ringwald // Late response (> 100 ms) for HCI Reset e.g. on Toshiba TC35661: 13600f97eae7SMatthias Ringwald // Command complete for HCI Reset arrives after we've resent the HCI Reset command 13610f97eae7SMatthias Ringwald // 13620f97eae7SMatthias Ringwald // HCI Reset 13630f97eae7SMatthias Ringwald // Timeout 100 ms 13640f97eae7SMatthias Ringwald // HCI Reset 13650f97eae7SMatthias Ringwald // Command Complete Reset 13660f97eae7SMatthias Ringwald // HCI Read Local Version Information 13670f97eae7SMatthias Ringwald // Command Complete Reset - but we expected Command Complete Read Local Version Information 13680f97eae7SMatthias Ringwald // hang... 13690f97eae7SMatthias Ringwald // 13700f97eae7SMatthias Ringwald // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend 13710f97eae7SMatthias Ringwald if (!command_completed 13720e2df43fSMatthias Ringwald && hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE 13730f97eae7SMatthias Ringwald && hci_stack->substate == HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION){ 13740f97eae7SMatthias Ringwald 1375f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 13760f97eae7SMatthias Ringwald if (opcode == hci_reset.opcode){ 13770f97eae7SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION; 13780f97eae7SMatthias Ringwald return; 13790f97eae7SMatthias Ringwald } 13800f97eae7SMatthias Ringwald } 13810f97eae7SMatthias Ringwald 13829f007422SMatthias Ringwald // CSR & H5 13839f007422SMatthias Ringwald // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend 13849f007422SMatthias Ringwald if (!command_completed 13859f007422SMatthias Ringwald && hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE 13869f007422SMatthias Ringwald && hci_stack->substate == HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS){ 13879f007422SMatthias Ringwald 13889f007422SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 13899f007422SMatthias Ringwald if (opcode == hci_reset.opcode){ 13909f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS; 13919f007422SMatthias Ringwald return; 13929f007422SMatthias Ringwald } 13939f007422SMatthias Ringwald } 13949f007422SMatthias Ringwald 13959f007422SMatthias 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 13969f007422SMatthias Ringwald // fix: Correct substate and behave as command below 13979f007422SMatthias Ringwald if (command_completed){ 13989f007422SMatthias Ringwald switch (hci_stack->substate){ 13999f007422SMatthias Ringwald case HCI_INIT_SEND_RESET: 14009f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 14019f007422SMatthias Ringwald break; 14029f007422SMatthias Ringwald case HCI_INIT_SEND_RESET_CSR_WARM_BOOT: 14039f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT; 14049f007422SMatthias Ringwald break; 14059f007422SMatthias Ringwald default: 14069f007422SMatthias Ringwald break; 14079f007422SMatthias Ringwald } 14089f007422SMatthias Ringwald } 14090f97eae7SMatthias Ringwald 141006b9e820SMatthias Ringwald #endif 14110f97eae7SMatthias Ringwald 1412a2481739S[email protected] if (!command_completed) return; 1413a2481739S[email protected] 141406b9e820SMatthias Ringwald int need_baud_change = 0; 141506b9e820SMatthias Ringwald int need_addr_change = 0; 141606b9e820SMatthias Ringwald 141706b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 141806b9e820SMatthias Ringwald need_baud_change = hci_stack->config 14193fb36a29SMatthias Ringwald && hci_stack->chipset 14203fb36a29SMatthias Ringwald && hci_stack->chipset->set_baudrate_command 1421db8bc6ffSMatthias Ringwald && hci_stack->hci_transport->set_baudrate 14229796ebeaSMatthias Ringwald && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 1423db8bc6ffSMatthias Ringwald 142406b9e820SMatthias Ringwald need_addr_change = hci_stack->custom_bd_addr_set 14253fb36a29SMatthias Ringwald && hci_stack->chipset 14263fb36a29SMatthias Ringwald && hci_stack->chipset->set_bd_addr_command; 142706b9e820SMatthias Ringwald #endif 1428a80162e9SMatthias Ringwald 14295c363727SMatthias Ringwald switch(hci_stack->substate){ 143006b9e820SMatthias Ringwald 143106b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 14329f007422SMatthias Ringwald case HCI_INIT_SEND_RESET: 1433d58dd308SMatthias Ringwald // on CSR with BCSP/H5, resend triggers resend of HCI Reset and leads to substate == HCI_INIT_SEND_RESET 14349f007422SMatthias Ringwald // fix: just correct substate and behave as command below 14359f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 14369f007422SMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 14379f007422SMatthias Ringwald break; 143874b323a9SMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 1439528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 144076fcb19bSMatthias Ringwald break; 1441e90bae01SMatthias Ringwald case HCI_INIT_W4_SEND_READ_LOCAL_NAME: 1442e90bae01SMatthias Ringwald log_info("Received local name, need baud change %d", need_baud_change); 14432f48d920SMatthias Ringwald if (need_baud_change){ 14442f48d920SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE; 14452f48d920SMatthias Ringwald return; 14462f48d920SMatthias Ringwald } 144753860077SMatthias Ringwald // skip baud change 144874b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 144974b323a9SMatthias Ringwald return; 1450a80162e9SMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE: 14514696bddbSMatthias Ringwald // for STLC2500D, baud rate change already happened. 1452fab26ab3SMatthias Ringwald // for others, baud rate gets changed now 145361f37892SMatthias Ringwald if ((hci_stack->manufacturer != BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS) && need_baud_change){ 145496b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1455cd724cb7SMatthias Ringwald log_info("Local baud rate change to %"PRIu32"(w4_send_baud_change)", baud_rate); 1456fab26ab3SMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 14574696bddbSMatthias Ringwald } 145874b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 145974b323a9SMatthias Ringwald return; 1460a80162e9SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT: 1461528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 1462a80162e9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 1463a80162e9SMatthias Ringwald return; 146474b323a9SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT: 146574b323a9SMatthias Ringwald // repeat custom init 146674b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 146774b323a9SMatthias Ringwald return; 146806b9e820SMatthias Ringwald #else 146906b9e820SMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 147006b9e820SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS; 147106b9e820SMatthias Ringwald return ; 147206b9e820SMatthias Ringwald #endif 147306b9e820SMatthias Ringwald 1474a828a756SMatthias Ringwald case HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS: 147561f37892SMatthias Ringwald if (need_baud_change && hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION){ 1476eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE_BCM; 1477eb3a5314SMatthias Ringwald return; 1478eb3a5314SMatthias Ringwald } 147953860077SMatthias Ringwald if (need_addr_change){ 148053860077SMatthias Ringwald hci_stack->substate = HCI_INIT_SET_BD_ADDR; 148153860077SMatthias Ringwald return; 148253860077SMatthias Ringwald } 148353860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 148453860077SMatthias Ringwald return; 148506b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 14867224be7eSMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE_BCM: 14877224be7eSMatthias Ringwald if (need_baud_change){ 148896b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1489cd724cb7SMatthias Ringwald log_info("Local baud rate change to %"PRIu32"(w4_send_baud_change_bcm))", baud_rate); 1490fab26ab3SMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 14917224be7eSMatthias Ringwald } 1492eb3a5314SMatthias Ringwald if (need_addr_change){ 1493eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_SET_BD_ADDR; 1494eb3a5314SMatthias Ringwald return; 1495eb3a5314SMatthias Ringwald } 1496eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 1497eb3a5314SMatthias Ringwald return; 149853860077SMatthias Ringwald case HCI_INIT_W4_SET_BD_ADDR: 149953860077SMatthias Ringwald // for STLC2500D, bd addr change only gets active after sending reset command 150061f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS){ 150153860077SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_ST_WARM_BOOT; 150253860077SMatthias Ringwald return; 150353860077SMatthias Ringwald } 150453860077SMatthias Ringwald // skipping st warm boot 150553860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 150653860077SMatthias Ringwald return; 150753860077SMatthias Ringwald case HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT: 150853860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 150953860077SMatthias Ringwald return; 151006b9e820SMatthias Ringwald #endif 151153860077SMatthias Ringwald case HCI_INIT_W4_READ_BD_ADDR: 151253860077SMatthias Ringwald // only read buffer size if supported 151353860077SMatthias Ringwald if (hci_stack->local_supported_commands[0] & 0x01) { 151453860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BUFFER_SIZE; 151553860077SMatthias Ringwald return; 151653860077SMatthias Ringwald } 151753860077SMatthias Ringwald // skipping read buffer size 151853860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES; 1519a828a756SMatthias Ringwald return; 152074b323a9SMatthias Ringwald case HCI_INIT_W4_SET_EVENT_MASK: 15216155b3d3S[email protected] // skip Classic init commands for LE only chipsets 15226155b3d3S[email protected] if (!hci_classic_supported()){ 15232c68f164SMatthias Ringwald #ifdef ENABLE_BLE 15246155b3d3S[email protected] if (hci_le_supported()){ 152574b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_LE_READ_BUFFER_SIZE; // skip all classic command 152674b323a9SMatthias Ringwald return; 15272c68f164SMatthias Ringwald } 15282c68f164SMatthias Ringwald #endif 15296155b3d3S[email protected] log_error("Neither BR/EDR nor LE supported"); 1530a650ba4dSMatthias Ringwald hci_init_done(); 153174b323a9SMatthias Ringwald return; 15326155b3d3S[email protected] } 153315a95bd5SMatthias Ringwald if (!gap_ssp_supported()){ 15345c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_WRITE_PAGE_TIMEOUT; 153574b323a9SMatthias Ringwald return; 15366155b3d3S[email protected] } 15376155b3d3S[email protected] break; 1538903ea03aSMatthias Ringwald #ifdef ENABLE_BLE 1539a828a756SMatthias Ringwald case HCI_INIT_W4_LE_READ_BUFFER_SIZE: 1540a828a756SMatthias Ringwald // skip write le host if not supported (e.g. on LE only EM9301) 1541a828a756SMatthias Ringwald if (hci_stack->local_supported_commands[0] & 0x02) break; 1542903ea03aSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 1543903ea03aSMatthias Ringwald hci_stack->substate = HCI_INIT_READ_WHITE_LIST_SIZE; 1544903ea03aSMatthias Ringwald #else 1545903ea03aSMatthias Ringwald hci_init_done(); 1546903ea03aSMatthias Ringwald #endif 1547a828a756SMatthias Ringwald return; 1548903ea03aSMatthias Ringwald #endif 1549ff00ed1cSMatthias Ringwald case HCI_INIT_W4_WRITE_LOCAL_NAME: 1550ff00ed1cSMatthias Ringwald // skip write eir data if no eir data set 1551ff00ed1cSMatthias Ringwald if (hci_stack->eir_data) break; 1552ff00ed1cSMatthias Ringwald hci_stack->substate = HCI_INIT_WRITE_INQUIRY_MODE; 1553ff00ed1cSMatthias Ringwald return; 1554729ed62eSMatthias Ringwald 155571c4de7aSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 1556729ed62eSMatthias Ringwald case HCI_INIT_W4_WRITE_SCAN_ENABLE: 15573905afbfSMatthias Ringwald // skip write synchronous flow control if not supported 15583905afbfSMatthias Ringwald if (hci_stack->local_supported_commands[0] & 0x04) break; 15593905afbfSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE; 15603905afbfSMatthias Ringwald // explicit fall through to reduce repetitions 15613905afbfSMatthias Ringwald 1562729ed62eSMatthias Ringwald case HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE: 15633905afbfSMatthias Ringwald // skip write default erroneous data reporting if not supported 15643905afbfSMatthias Ringwald if (hci_stack->local_supported_commands[0] & 0x08) break; 15653905afbfSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING; 15663905afbfSMatthias Ringwald // explicit fall through to reduce repetitions 15673905afbfSMatthias Ringwald 1568f064e0bbSMatthias Ringwald case HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING: 1569a42798c3SMatthias Ringwald // skip bcm set sco pcm config on non-Broadcom chipsets 157061f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION) break; 1571a42798c3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT; 1572a42798c3SMatthias Ringwald // explicit fall through to reduce repetitions 1573a42798c3SMatthias Ringwald 1574a42798c3SMatthias Ringwald case HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT: 1575729ed62eSMatthias Ringwald if (!hci_le_supported()){ 1576729ed62eSMatthias Ringwald // SKIP LE init for Classic only configuration 1577a650ba4dSMatthias Ringwald hci_init_done(); 1578729ed62eSMatthias Ringwald return; 1579729ed62eSMatthias Ringwald } 1580729ed62eSMatthias Ringwald break; 1581f3b012f9SMatthias Ringwald 1582f3b012f9SMatthias Ringwald #else /* !ENABLE_SCO_OVER_HCI */ 1583f3b012f9SMatthias Ringwald 158474b323a9SMatthias Ringwald case HCI_INIT_W4_WRITE_SCAN_ENABLE: 1585f3b012f9SMatthias Ringwald #ifdef ENABLE_BLE 1586f3b012f9SMatthias Ringwald if (hci_le_supported()){ 1587f3b012f9SMatthias Ringwald hci_stack->substate = HCI_INIT_LE_READ_BUFFER_SIZE; 158874b323a9SMatthias Ringwald return; 15896155b3d3S[email protected] } 1590729ed62eSMatthias Ringwald #endif 1591f3b012f9SMatthias Ringwald // SKIP LE init for Classic only configuration 1592f3b012f9SMatthias Ringwald hci_init_done(); 1593f3b012f9SMatthias Ringwald return; 1594f3b012f9SMatthias Ringwald #endif /* ENABLE_SCO_OVER_HCI */ 1595f3b012f9SMatthias Ringwald 1596a650ba4dSMatthias Ringwald // Response to command before init done state -> init done 1597a650ba4dSMatthias Ringwald case (HCI_INIT_DONE-1): 1598a650ba4dSMatthias Ringwald hci_init_done(); 1599a650ba4dSMatthias Ringwald return; 1600a650ba4dSMatthias Ringwald 16016155b3d3S[email protected] default: 160274b323a9SMatthias Ringwald break; 16036155b3d3S[email protected] } 160455975f88SMatthias Ringwald hci_initializing_next_state(); 16056155b3d3S[email protected] } 16066155b3d3S[email protected] 160716833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){ 1608e76a89eeS[email protected] 1609e76a89eeS[email protected] uint16_t event_length = packet[1]; 1610e76a89eeS[email protected] 1611e76a89eeS[email protected] // assert packet is complete 1612e76a89eeS[email protected] if (size != event_length + 2){ 1613f04a0c31SMatthias Ringwald log_error("hci.c: event_handler called with event packet of wrong size %d, expected %u => dropping packet", size, event_length + 2); 1614e76a89eeS[email protected] return; 1615e76a89eeS[email protected] } 1616e76a89eeS[email protected] 16171281a47eSmatthias.ringwald bd_addr_t addr; 161896a45072S[email protected] bd_addr_type_t addr_type; 1619fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 16201f7b95a1Smatthias.ringwald hci_connection_t * conn; 162156cf178bSmatthias.ringwald int i; 16220014e02cSMatthias Ringwald #ifdef ENABLE_CLASSIC 16230014e02cSMatthias Ringwald uint8_t link_type; 16240014e02cSMatthias Ringwald #endif 162535454696SMatthias Ringwald 16260e2df43fSMatthias Ringwald // log_info("HCI:EVENT:%02x", hci_event_packet_get_type(packet)); 16275909f7f2Smatthias.ringwald 16280e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 162922909952Smatthias.ringwald 16306772a24cSmatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 16316bef4003SMatthias Ringwald // get num cmd packets - limit to 1 to reduce complexity 16326bef4003SMatthias Ringwald hci_stack->num_cmd_packets = packet[2] ? 1 : 0; 16337ec5eeaaSmatthias.ringwald 16349e263bd7SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){ 163506b9e820SMatthias Ringwald if (packet[5]) break; 16369e263bd7SMatthias Ringwald // terminate, name 248 chars 16379e263bd7SMatthias Ringwald packet[6+248] = 0; 16389e263bd7SMatthias Ringwald log_info("local name: %s", &packet[6]); 16399e263bd7SMatthias Ringwald } 1640073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_buffer_size)){ 16411d279b20Smatthias.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" 1642429122ccSMatthias Ringwald if (hci_stack->state == HCI_STATE_INITIALIZING){ 1643429122ccSMatthias Ringwald uint16_t acl_len = little_endian_read_16(packet, 6); 1644429122ccSMatthias Ringwald uint16_t sco_len = packet[8]; 1645429122ccSMatthias Ringwald 1646429122ccSMatthias Ringwald // determine usable ACL/SCO payload size 1647429122ccSMatthias Ringwald hci_stack->acl_data_packet_length = btstack_min(acl_len, HCI_ACL_PAYLOAD_SIZE); 1648429122ccSMatthias Ringwald hci_stack->sco_data_packet_length = btstack_min(sco_len, HCI_ACL_PAYLOAD_SIZE); 1649429122ccSMatthias Ringwald 1650f8fbdce0SMatthias Ringwald hci_stack->acl_packets_total_num = little_endian_read_16(packet, 9); 1651f8fbdce0SMatthias Ringwald hci_stack->sco_packets_total_num = little_endian_read_16(packet, 11); 1652a8b12447S[email protected] 1653429122ccSMatthias Ringwald log_info("hci_read_buffer_size: ACL size module %u -> used %u, count %u / SCO size %u, count %u", 1654429122ccSMatthias Ringwald acl_len, hci_stack->acl_data_packet_length, hci_stack->acl_packets_total_num, 16551a06f663S[email protected] hci_stack->sco_data_packet_length, hci_stack->sco_packets_total_num); 1656e2edc0c3Smatthias.ringwald } 165756cf178bSmatthias.ringwald } 1658a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 1659073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_le_read_buffer_size)){ 1660f8fbdce0SMatthias Ringwald hci_stack->le_data_packets_length = little_endian_read_16(packet, 6); 1661ee303eddS[email protected] hci_stack->le_acl_packets_total_num = packet[8]; 16626c26b087S[email protected] // determine usable ACL payload size 16636c26b087S[email protected] if (HCI_ACL_PAYLOAD_SIZE < hci_stack->le_data_packets_length){ 16646c26b087S[email protected] hci_stack->le_data_packets_length = HCI_ACL_PAYLOAD_SIZE; 16656c26b087S[email protected] } 16669da54300S[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); 166765a46ef3S[email protected] } 1668d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 1669073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_le_read_white_list_size)){ 1670e691bb38SMatthias Ringwald hci_stack->le_whitelist_capacity = packet[6]; 167115d0a15bSMatthias Ringwald log_info("hci_le_read_white_list_size: size %u", hci_stack->le_whitelist_capacity); 16723b6d4121SMatthias Ringwald } 167365a46ef3S[email protected] #endif 1674d70217a2SMatthias Ringwald #endif 1675073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_bd_addr)) { 1676724d70a2SMatthias Ringwald reverse_bd_addr(&packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1], 1677724d70a2SMatthias Ringwald hci_stack->local_bd_addr); 16789da54300S[email protected] log_info("Local Address, Status: 0x%02x: Addr: %s", 16793a9fb326S[email protected] packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr)); 168033373e40SMatthias Ringwald #ifdef ENABLE_CLASSIC 16811624665aSMatthias Ringwald if (hci_stack->link_key_db){ 16821624665aSMatthias Ringwald hci_stack->link_key_db->set_local_bd_addr(hci_stack->local_bd_addr); 16831624665aSMatthias Ringwald } 168433373e40SMatthias Ringwald #endif 1685188981d2Smatthias.ringwald } 168635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1687073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_scan_enable)){ 16883a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 1689381fbed8Smatthias.ringwald } 1690f5875de5SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_inquiry_cancel)){ 1691f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_W4_CANCELLED){ 1692f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_IDLE; 1693f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 1694f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 1695f5875de5SMatthias Ringwald } 1696f5875de5SMatthias Ringwald } 169735454696SMatthias Ringwald #endif 169835454696SMatthias Ringwald 169965389bfcS[email protected] // Note: HCI init checks 1700073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_supported_features)){ 17013a9fb326S[email protected] memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8); 170265389bfcS[email protected] 170335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1704a5a23fc2S[email protected] // determine usable ACL packet types based on host buffer size and supported features 1705a5a23fc2S[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]); 17068b96126aSMatthias Ringwald log_info("Packet types %04x, eSCO %u", hci_stack->packet_types, hci_extended_sco_link_supported()); 170735454696SMatthias Ringwald #endif 1708f5d8d141S[email protected] // Classic/LE 1709f5d8d141S[email protected] log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported()); 1710559e517eS[email protected] } 1711073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_version_information)){ 1712f8fbdce0SMatthias Ringwald // hci_stack->hci_version = little_endian_read_16(packet, 4); 1713f8fbdce0SMatthias Ringwald // hci_stack->hci_revision = little_endian_read_16(packet, 6); 1714f8fbdce0SMatthias Ringwald // hci_stack->lmp_version = little_endian_read_16(packet, 8); 1715f8fbdce0SMatthias Ringwald hci_stack->manufacturer = little_endian_read_16(packet, 10); 1716f8fbdce0SMatthias Ringwald // hci_stack->lmp_subversion = little_endian_read_16(packet, 12); 17174696bddbSMatthias Ringwald log_info("Manufacturer: 0x%04x", hci_stack->manufacturer); 17184696bddbSMatthias Ringwald } 1719073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_supported_commands)){ 1720a828a756SMatthias Ringwald hci_stack->local_supported_commands[0] = 17213905afbfSMatthias Ringwald (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+14] & 0x80) >> 7 | // bit 0 = Octet 14, bit 7 17223905afbfSMatthias Ringwald (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+24] & 0x40) >> 5 | // bit 1 = Octet 24, bit 6 17233905afbfSMatthias Ringwald (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+10] & 0x10) >> 2 | // bit 2 = Octet 10, bit 4 17243905afbfSMatthias Ringwald (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+18] & 0x08); // bit 3 = Octet 18, bit 3 17253905afbfSMatthias Ringwald log_info("Local supported commands summary 0x%02x", hci_stack->local_supported_commands[0]); 1726a828a756SMatthias Ringwald } 1727e8c8828eSMatthias Ringwald #ifdef ENABLE_CLASSIC 1728073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_synchronous_flow_control_enable)){ 17295b9b590fSMatthias Ringwald if (packet[5] == 0){ 17305b9b590fSMatthias Ringwald hci_stack->synchronous_flow_control_enabled = 1; 17315b9b590fSMatthias Ringwald } 17325b9b590fSMatthias Ringwald } 1733e8c8828eSMatthias Ringwald #endif 173456cf178bSmatthias.ringwald break; 173556cf178bSmatthias.ringwald 17367ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 17376bef4003SMatthias Ringwald // get num cmd packets - limit to 1 to reduce complexity 17386bef4003SMatthias Ringwald hci_stack->num_cmd_packets = packet[3] ? 1 : 0; 17397ec5eeaaSmatthias.ringwald break; 17407ec5eeaaSmatthias.ringwald 17412e440c8aS[email protected] case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:{ 17422e440c8aS[email protected] int offset = 3; 174356cf178bSmatthias.ringwald for (i=0; i<packet[2];i++){ 1744f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, offset); 17452e440c8aS[email protected] offset += 2; 1746f8fbdce0SMatthias Ringwald uint16_t num_packets = little_endian_read_16(packet, offset); 17472e440c8aS[email protected] offset += 2; 17482e440c8aS[email protected] 17495061f3afS[email protected] conn = hci_connection_for_handle(handle); 175056cf178bSmatthias.ringwald if (!conn){ 17519da54300S[email protected] log_error("hci_number_completed_packet lists unused con handle %u", handle); 175256cf178bSmatthias.ringwald continue; 175356cf178bSmatthias.ringwald } 175423bed257S[email protected] 1755e35edcc1S[email protected] if (conn->address_type == BD_ADDR_TYPE_SCO){ 175635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1757e35edcc1S[email protected] if (conn->num_sco_packets_sent >= num_packets){ 1758e35edcc1S[email protected] conn->num_sco_packets_sent -= num_packets; 1759e35edcc1S[email protected] } else { 1760e35edcc1S[email protected] log_error("hci_number_completed_packets, more sco slots freed then sent."); 1761e35edcc1S[email protected] conn->num_sco_packets_sent = 0; 1762e35edcc1S[email protected] } 1763701e3307SMatthias Ringwald hci_notify_if_sco_can_send_now(); 176435454696SMatthias Ringwald #endif 1765e35edcc1S[email protected] } else { 176623bed257S[email protected] if (conn->num_acl_packets_sent >= num_packets){ 176756cf178bSmatthias.ringwald conn->num_acl_packets_sent -= num_packets; 176823bed257S[email protected] } else { 1769e35edcc1S[email protected] log_error("hci_number_completed_packets, more acl slots freed then sent."); 177023bed257S[email protected] conn->num_acl_packets_sent = 0; 177123bed257S[email protected] } 1772e35edcc1S[email protected] } 17739da54300S[email protected] // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u", num_packets, handle, conn->num_acl_packets_sent); 177456cf178bSmatthias.ringwald } 17756772a24cSmatthias.ringwald break; 17762e440c8aS[email protected] } 177735454696SMatthias Ringwald 177835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1779f5875de5SMatthias Ringwald case HCI_EVENT_INQUIRY_COMPLETE: 1780f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_ACTIVE){ 1781f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_IDLE; 1782f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 1783f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 1784f5875de5SMatthias Ringwald } 1785f5875de5SMatthias Ringwald break; 1786*b7f1ee76SMatthias Ringwald case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 1787*b7f1ee76SMatthias Ringwald if (hci_stack->remote_name_state == GAP_REMOTE_NAME_STATE_W4_COMPLETE){ 1788*b7f1ee76SMatthias Ringwald hci_stack->remote_name_state = GAP_REMOTE_NAME_STATE_IDLE; 1789*b7f1ee76SMatthias Ringwald } 1790*b7f1ee76SMatthias Ringwald break; 17911f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 1792724d70a2SMatthias Ringwald reverse_bd_addr(&packet[2], addr); 179337eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 17942d00edd4Smatthias.ringwald link_type = packet[11]; 17959da54300S[email protected] log_info("Connection_incoming: %s, type %u", bd_addr_to_str(addr), link_type); 1796e35edcc1S[email protected] addr_type = link_type == 1 ? BD_ADDR_TYPE_CLASSIC : BD_ADDR_TYPE_SCO; 17972e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 17981f7b95a1Smatthias.ringwald if (!conn) { 17995293c072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 18001f7b95a1Smatthias.ringwald } 1801ce4c8fabSmatthias.ringwald if (!conn) { 1802ce4c8fabSmatthias.ringwald // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 18033a9fb326S[email protected] hci_stack->decline_reason = 0x0d; 1804058e3d6bSMatthias Ringwald bd_addr_copy(hci_stack->decline_addr, addr); 1805ce4c8fabSmatthias.ringwald break; 1806ce4c8fabSmatthias.ringwald } 18075cf766e8SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 180832ab9390Smatthias.ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 1809f3a16b9aSMatthias Ringwald // store info about eSCO 1810f3a16b9aSMatthias Ringwald if (link_type == 0x02){ 1811f3a16b9aSMatthias Ringwald conn->remote_supported_feature_eSCO = 1; 1812f3a16b9aSMatthias Ringwald } 181332ab9390Smatthias.ringwald hci_run(); 18141f7b95a1Smatthias.ringwald break; 18151f7b95a1Smatthias.ringwald 18166772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 1817fe1ed1b8Smatthias.ringwald // Connection management 1818724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], addr); 18199da54300S[email protected] log_info("Connection_complete (status=%u) %s", packet[2], bd_addr_to_str(addr)); 182096a45072S[email protected] addr_type = BD_ADDR_TYPE_CLASSIC; 18212e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 1822fe1ed1b8Smatthias.ringwald if (conn) { 1823b448a0e7Smatthias.ringwald if (!packet[2]){ 1824c8e4258aSmatthias.ringwald conn->state = OPEN; 1825f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 3); 1826ad83dc6aS[email protected] conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES; 1827ee091cf1Smatthias.ringwald 1828c785ef68Smatthias.ringwald // restart timer 1829528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 1830528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&conn->timeout); 1831c785ef68Smatthias.ringwald 18329da54300S[email protected] log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 183343bfb1bdSmatthias.ringwald 183443bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 1835b448a0e7Smatthias.ringwald } else { 18361bd5283dS[email protected] int notify_dedicated_bonding_failed = conn->bonding_flags & BONDING_DEDICATED; 18371bd5283dS[email protected] uint8_t status = packet[2]; 18381bd5283dS[email protected] bd_addr_t bd_address; 18391bd5283dS[email protected] memcpy(&bd_address, conn->address, 6); 1840ad83dc6aS[email protected] 1841b448a0e7Smatthias.ringwald // connection failed, remove entry 1842665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 1843a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 1844c12e46e7Smatthias.ringwald 18451bd5283dS[email protected] // notify client if dedicated bonding 18461bd5283dS[email protected] if (notify_dedicated_bonding_failed){ 18471bd5283dS[email protected] log_info("hci notify_dedicated_bonding_failed"); 18481bd5283dS[email protected] hci_emit_dedicated_bonding_result(bd_address, status); 18491bd5283dS[email protected] } 18501bd5283dS[email protected] 1851c12e46e7Smatthias.ringwald // if authentication error, also delete link key 1852c12e46e7Smatthias.ringwald if (packet[2] == 0x05) { 185315a95bd5SMatthias Ringwald gap_drop_link_key_for_bd_addr(addr); 1854c12e46e7Smatthias.ringwald } 1855fe1ed1b8Smatthias.ringwald } 1856fe1ed1b8Smatthias.ringwald } 18576772a24cSmatthias.ringwald break; 1858fe1ed1b8Smatthias.ringwald 185944d0e3d5S[email protected] case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE: 1860724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], addr); 186144d0e3d5S[email protected] log_info("Synchronous Connection Complete (status=%u) %s", packet[2], bd_addr_to_str(addr)); 18621a06f663S[email protected] if (packet[2]){ 186344d0e3d5S[email protected] // connection failed 186444d0e3d5S[email protected] break; 186544d0e3d5S[email protected] } 18662e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 1867e35edcc1S[email protected] if (!conn) { 1868e35edcc1S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 1869e35edcc1S[email protected] } 1870e35edcc1S[email protected] if (!conn) { 1871e35edcc1S[email protected] break; 1872e35edcc1S[email protected] } 18731a06f663S[email protected] conn->state = OPEN; 1874f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 3); 1875ee752bb8SMatthias Ringwald 1876ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 1877ee752bb8SMatthias Ringwald // update SCO 1878ee752bb8SMatthias Ringwald if (conn->address_type == BD_ADDR_TYPE_SCO && hci_stack->hci_transport && hci_stack->hci_transport->set_sco_config){ 1879ee752bb8SMatthias Ringwald hci_stack->hci_transport->set_sco_config(hci_stack->sco_voice_setting_active, hci_number_sco_connections()); 1880ee752bb8SMatthias Ringwald } 1881ee752bb8SMatthias Ringwald #endif 188244d0e3d5S[email protected] break; 188344d0e3d5S[email protected] 1884afd4e962S[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 1885f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1886afd4e962S[email protected] conn = hci_connection_for_handle(handle); 1887afd4e962S[email protected] if (!conn) break; 1888afd4e962S[email protected] if (!packet[2]){ 1889afd4e962S[email protected] uint8_t * features = &packet[5]; 1890afd4e962S[email protected] if (features[6] & (1 << 3)){ 1891afd4e962S[email protected] conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP; 1892afd4e962S[email protected] } 189398a2fd1cSMatthias Ringwald if (features[3] & (1<<7)){ 189498a2fd1cSMatthias Ringwald conn->remote_supported_feature_eSCO = 1; 189598a2fd1cSMatthias Ringwald } 1896afd4e962S[email protected] } 1897afd4e962S[email protected] conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES; 189898a2fd1cSMatthias Ringwald log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x, eSCO %u", conn->bonding_flags, conn->remote_supported_feature_eSCO); 1899ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 1900ad83dc6aS[email protected] conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 1901ad83dc6aS[email protected] } 1902afd4e962S[email protected] break; 1903afd4e962S[email protected] 19047fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 19059da54300S[email protected] log_info("HCI_EVENT_LINK_KEY_REQUEST"); 19067fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST); 190774d716b5S[email protected] // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST 1908a98592bcSMatthias Ringwald if (hci_stack->bondable && !hci_stack->link_key_db) break; 190932ab9390Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST); 191064472d52Smatthias.ringwald hci_run(); 1911d9a327f9Smatthias.ringwald // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set 191229d53098Smatthias.ringwald return; 19137fde4af9Smatthias.ringwald 19149ab95c90S[email protected] case HCI_EVENT_LINK_KEY_NOTIFICATION: { 1915724d70a2SMatthias Ringwald reverse_bd_addr(&packet[2], addr); 19162e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 19179ab95c90S[email protected] if (!conn) break; 19189ab95c90S[email protected] conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION; 19197bdc6798S[email protected] link_key_type_t link_key_type = (link_key_type_t)packet[24]; 19209ab95c90S[email protected] // Change Connection Encryption keeps link key type 19219ab95c90S[email protected] if (link_key_type != CHANGED_COMBINATION_KEY){ 19229ab95c90S[email protected] conn->link_key_type = link_key_type; 19239ab95c90S[email protected] } 192455597469SMatthias Ringwald gap_store_link_key_for_bd_addr(addr, &packet[8], conn->link_key_type); 192529d53098Smatthias.ringwald // still forward event to allow dismiss of pairing dialog 19267fde4af9Smatthias.ringwald break; 19279ab95c90S[email protected] } 19287fde4af9Smatthias.ringwald 19297fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 19306724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE); 19314c57c146S[email protected] // non-bondable mode: pin code negative reply will be sent 19323a9fb326S[email protected] if (!hci_stack->bondable){ 1933899283eaS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST); 1934f8fb5f6eS[email protected] hci_run(); 1935f8fb5f6eS[email protected] return; 19364c57c146S[email protected] } 1937d9a327f9Smatthias.ringwald // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key 1938a98592bcSMatthias Ringwald if (!hci_stack->link_key_db) break; 1939a6ef64baSMilanka Ringwald hci_event_pin_code_request_get_bd_addr(packet, addr); 1940a98592bcSMatthias Ringwald hci_stack->link_key_db->delete_link_key(addr); 19417fde4af9Smatthias.ringwald break; 19427fde4af9Smatthias.ringwald 19431d6b20aeS[email protected] case HCI_EVENT_IO_CAPABILITY_REQUEST: 19441d6b20aeS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST); 1945dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY); 1946dbe1a790S[email protected] break; 1947dbe1a790S[email protected] 1948dbe1a790S[email protected] case HCI_EVENT_USER_CONFIRMATION_REQUEST: 19496724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 19503a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 1951dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY); 1952dbe1a790S[email protected] break; 1953dbe1a790S[email protected] 1954dbe1a790S[email protected] case HCI_EVENT_USER_PASSKEY_REQUEST: 19556724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 19563a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 1957dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY); 19581d6b20aeS[email protected] break; 195935454696SMatthias Ringwald #endif 19601d6b20aeS[email protected] 1961f0944df2S[email protected] case HCI_EVENT_ENCRYPTION_CHANGE: 1962f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1963f0944df2S[email protected] conn = hci_connection_for_handle(handle); 1964f0944df2S[email protected] if (!conn) break; 1965ad83dc6aS[email protected] if (packet[2] == 0) { 1966f0944df2S[email protected] if (packet[5]){ 1967f0944df2S[email protected] conn->authentication_flags |= CONNECTION_ENCRYPTED; 1968f0944df2S[email protected] } else { 1969536f9994S[email protected] conn->authentication_flags &= ~CONNECTION_ENCRYPTED; 1970f0944df2S[email protected] } 1971ad83dc6aS[email protected] } 197235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1973a00031e2S[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 197435454696SMatthias Ringwald #endif 1975f0944df2S[email protected] break; 1976f0944df2S[email protected] 197735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 19781eb2563eS[email protected] case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT: 1979f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 19801eb2563eS[email protected] conn = hci_connection_for_handle(handle); 19811eb2563eS[email protected] if (!conn) break; 1982ad83dc6aS[email protected] 1983ad83dc6aS[email protected] // dedicated bonding: send result and disconnect 1984ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 1985ad83dc6aS[email protected] conn->bonding_flags &= ~BONDING_DEDICATED; 1986ad83dc6aS[email protected] conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE; 19871bd5283dS[email protected] conn->bonding_status = packet[2]; 1988ad83dc6aS[email protected] break; 1989ad83dc6aS[email protected] } 1990ad83dc6aS[email protected] 1991b5cb6874S[email protected] if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){ 19921eb2563eS[email protected] // link key sufficient for requested security 19931eb2563eS[email protected] conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 1994ad83dc6aS[email protected] break; 1995ad83dc6aS[email protected] } 19961eb2563eS[email protected] // not enough 19971eb2563eS[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 19981eb2563eS[email protected] break; 199935454696SMatthias Ringwald #endif 200034d2123cS[email protected] 200186805605S[email protected] // HCI_EVENT_DISCONNECTION_COMPLETE 2002ccda6e14S[email protected] // has been split, to first notify stack before shutting connection down 2003ccda6e14S[email protected] // see end of function, too. 2004a4f30ec0S[email protected] case HCI_EVENT_DISCONNECTION_COMPLETE: 2005a4f30ec0S[email protected] if (packet[2]) break; // status != 0 2006f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 2007c6a37cfdSMatthias Ringwald // drop outgoing ACL fragments if it is for closed connection 2008c6a37cfdSMatthias Ringwald if (hci_stack->acl_fragmentation_total_size > 0) { 2009c6a37cfdSMatthias Ringwald if (handle == READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer)){ 2010c6a37cfdSMatthias Ringwald log_info("hci: drop fragmented ACL data for closed connection"); 2011c6a37cfdSMatthias Ringwald hci_stack->acl_fragmentation_total_size = 0; 2012c6a37cfdSMatthias Ringwald hci_stack->acl_fragmentation_pos = 0; 2013c6a37cfdSMatthias Ringwald } 2014c6a37cfdSMatthias Ringwald } 201535454696SMatthias Ringwald 20169a2e4658SMatthias Ringwald // re-enable advertisements for le connections if active 2017c6a37cfdSMatthias Ringwald conn = hci_connection_for_handle(handle); 2018c6a37cfdSMatthias Ringwald if (!conn) break; 201935454696SMatthias Ringwald #ifdef ENABLE_BLE 2020d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 20219a2e4658SMatthias Ringwald if (hci_is_le_connection(conn) && hci_stack->le_advertisements_enabled){ 20229a2e4658SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_ENABLE; 20239a2e4658SMatthias Ringwald } 202435454696SMatthias Ringwald #endif 2025d70217a2SMatthias Ringwald #endif 2026ccda6e14S[email protected] conn->state = RECEIVED_DISCONNECTION_COMPLETE; 2027ccda6e14S[email protected] break; 20286772a24cSmatthias.ringwald 2029c68bdf90Smatthias.ringwald case HCI_EVENT_HARDWARE_ERROR: 2030313e5f9cSMatthias Ringwald log_error("Hardware Error: 0x%02x", packet[2]); 2031d23838ecSMatthias Ringwald if (hci_stack->hardware_error_callback){ 2032c2e1fa60SMatthias Ringwald (*hci_stack->hardware_error_callback)(packet[2]); 20337586ee35S[email protected] } else { 20347586ee35S[email protected] // if no special requests, just reboot stack 20357586ee35S[email protected] hci_power_control_off(); 20367586ee35S[email protected] hci_power_control_on(); 2037c68bdf90Smatthias.ringwald } 2038c68bdf90Smatthias.ringwald break; 2039c68bdf90Smatthias.ringwald 20400e6f3837SMatthias Ringwald #ifdef ENABLE_CLASSIC 20415cf766e8SMatthias Ringwald case HCI_EVENT_ROLE_CHANGE: 20425cf766e8SMatthias Ringwald if (packet[2]) break; // status != 0 2043f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 20445cf766e8SMatthias Ringwald conn = hci_connection_for_handle(handle); 20455cf766e8SMatthias Ringwald if (!conn) break; // no conn 20465cf766e8SMatthias Ringwald conn->role = packet[9]; 20475cf766e8SMatthias Ringwald break; 20480e6f3837SMatthias Ringwald #endif 20495cf766e8SMatthias Ringwald 205063fa3374SMatthias Ringwald case HCI_EVENT_TRANSPORT_PACKET_SENT: 2051d051460cS[email protected] // release packet buffer only for asynchronous transport and if there are not further fragements 20524fa24b5fS[email protected] if (hci_transport_synchronous()) { 205363fa3374SMatthias Ringwald log_error("Synchronous HCI Transport shouldn't send HCI_EVENT_TRANSPORT_PACKET_SENT"); 20544fa24b5fS[email protected] return; // instead of break: to avoid re-entering hci_run() 20554fa24b5fS[email protected] } 2056d051460cS[email protected] if (hci_stack->acl_fragmentation_total_size) break; 2057d051460cS[email protected] hci_release_packet_buffer(); 2058701e3307SMatthias Ringwald 205963fa3374SMatthias Ringwald // L2CAP receives this event via the hci_emit_event below 206063fa3374SMatthias Ringwald 206135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 206263fa3374SMatthias Ringwald // For SCO, we do the can_send_now_check here 2063701e3307SMatthias Ringwald hci_notify_if_sco_can_send_now(); 206435454696SMatthias Ringwald #endif 20656b4af23dS[email protected] break; 20666b4af23dS[email protected] 206735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 206863fa3374SMatthias Ringwald case HCI_EVENT_SCO_CAN_SEND_NOW: 206963fa3374SMatthias Ringwald // For SCO, we do the can_send_now_check here 207063fa3374SMatthias Ringwald hci_notify_if_sco_can_send_now(); 207163fa3374SMatthias Ringwald return; 20721cfb383eSMatthias Ringwald 20731cfb383eSMatthias Ringwald // explode inquriy results for easier consumption 20741cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 20751cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 20761cfb383eSMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 20771cfb383eSMatthias Ringwald gap_inquiry_explode(packet); 20781cfb383eSMatthias Ringwald break; 207935454696SMatthias Ringwald #endif 208063fa3374SMatthias Ringwald 2081a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 20825909f7f2Smatthias.ringwald case HCI_EVENT_LE_META: 20835909f7f2Smatthias.ringwald switch (packet[2]){ 2084d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 208557c9da5bS[email protected] case HCI_SUBEVENT_LE_ADVERTISING_REPORT: 20864f4e0224SMatthias Ringwald // log_info("advertising report received"); 20877bdc6798S[email protected] if (hci_stack->le_scanning_state != LE_SCANNING) break; 208857c9da5bS[email protected] le_handle_advertisement_report(packet, size); 20897bdc6798S[email protected] break; 2090d70217a2SMatthias Ringwald #endif 20915909f7f2Smatthias.ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 20925909f7f2Smatthias.ringwald // Connection management 2093724d70a2SMatthias Ringwald reverse_bd_addr(&packet[8], addr); 20947bdc6798S[email protected] addr_type = (bd_addr_type_t)packet[7]; 20959da54300S[email protected] log_info("LE Connection_complete (status=%u) type %u, %s", packet[3], addr_type, bd_addr_to_str(addr)); 20962e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 2097d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 209842ff5ba1SMatthias Ringwald // if auto-connect, remove from whitelist in both roles 209942ff5ba1SMatthias Ringwald if (hci_stack->le_connecting_state == LE_CONNECTING_WHITELIST){ 210042ff5ba1SMatthias Ringwald hci_remove_from_whitelist(addr_type, addr); 210142ff5ba1SMatthias Ringwald } 210242ff5ba1SMatthias Ringwald // handle error: error is reported only to the initiator -> outgoing connection 21035909f7f2Smatthias.ringwald if (packet[3]){ 210442ff5ba1SMatthias Ringwald // outgoing connection establishment is done 210542ff5ba1SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 210642ff5ba1SMatthias Ringwald // remove entry 21075909f7f2Smatthias.ringwald if (conn){ 2108665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 21095909f7f2Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 21105909f7f2Smatthias.ringwald } 21115909f7f2Smatthias.ringwald break; 21125909f7f2Smatthias.ringwald } 2113d70217a2SMatthias Ringwald #endif 211442ff5ba1SMatthias Ringwald // on success, both hosts receive connection complete event 21155cf766e8SMatthias Ringwald if (packet[6] == HCI_ROLE_MASTER){ 2116d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 211742ff5ba1SMatthias Ringwald // if we're master, it was an outgoing connection and we're done with it 211842ff5ba1SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 2119d70217a2SMatthias Ringwald #endif 212042ff5ba1SMatthias Ringwald } else { 2121d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 212242ff5ba1SMatthias Ringwald // if we're slave, it was an incoming connection, advertisements have stopped 2123171293d3SMatthias Ringwald hci_stack->le_advertisements_active = 0; 21245106e6dcSMatthias Ringwald // try to re-enable them 21255106e6dcSMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_ENABLE; 2126d70217a2SMatthias Ringwald #endif 212742ff5ba1SMatthias Ringwald } 2128171293d3SMatthias Ringwald // LE connections are auto-accepted, so just create a connection if there isn't one already 212942ff5ba1SMatthias Ringwald if (!conn){ 213096a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 21315909f7f2Smatthias.ringwald } 213242ff5ba1SMatthias Ringwald // no memory, sorry. 21335909f7f2Smatthias.ringwald if (!conn){ 21345909f7f2Smatthias.ringwald break; 21355909f7f2Smatthias.ringwald } 21365909f7f2Smatthias.ringwald 21375909f7f2Smatthias.ringwald conn->state = OPEN; 21385cf766e8SMatthias Ringwald conn->role = packet[6]; 2139f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 4); 21405909f7f2Smatthias.ringwald 21415909f7f2Smatthias.ringwald // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock 21425909f7f2Smatthias.ringwald 21435909f7f2Smatthias.ringwald // restart timer 2144528a4a3bSMatthias Ringwald // btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 2145528a4a3bSMatthias Ringwald // btstack_run_loop_add_timer(&conn->timeout); 21465909f7f2Smatthias.ringwald 21479da54300S[email protected] log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 21485909f7f2Smatthias.ringwald 21495909f7f2Smatthias.ringwald hci_emit_nr_connections_changed(); 21505909f7f2Smatthias.ringwald break; 21515909f7f2Smatthias.ringwald 2152f8fbdce0SMatthias Ringwald // log_info("LE buffer size: %u, count %u", little_endian_read_16(packet,6), packet[8]); 215365a46ef3S[email protected] 21545909f7f2Smatthias.ringwald default: 21555909f7f2Smatthias.ringwald break; 21565909f7f2Smatthias.ringwald } 21575909f7f2Smatthias.ringwald break; 21585909f7f2Smatthias.ringwald #endif 21596772a24cSmatthias.ringwald default: 21606772a24cSmatthias.ringwald break; 2161fe1ed1b8Smatthias.ringwald } 2162fe1ed1b8Smatthias.ringwald 21633429f56bSmatthias.ringwald // handle BT initialization 21643a9fb326S[email protected] if (hci_stack->state == HCI_STATE_INITIALIZING){ 21656155b3d3S[email protected] hci_initializing_event_handler(packet, size); 2166c9af4d3fS[email protected] } 216722909952Smatthias.ringwald 216889db417bSmatthias.ringwald // help with BT sleep 21693a9fb326S[email protected] if (hci_stack->state == HCI_STATE_FALLING_ASLEEP 217074b323a9SMatthias Ringwald && hci_stack->substate == HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE 2171073bd0faSMatthias Ringwald && HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_scan_enable)){ 217255975f88SMatthias Ringwald hci_initializing_next_state(); 217389db417bSmatthias.ringwald } 217489db417bSmatthias.ringwald 217586805605S[email protected] // notify upper stack 2176d6b06661SMatthias Ringwald hci_emit_event(packet, size, 0); // don't dump, already happened in packet handler 217794ab26f8Smatthias.ringwald 217886805605S[email protected] // moved here to give upper stack a chance to close down everything with hci_connection_t intact 21790e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_DISCONNECTION_COMPLETE){ 218086805605S[email protected] if (!packet[2]){ 2181f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 218205ae8de3SMatthias Ringwald hci_connection_t * aConn = hci_connection_for_handle(handle); 218305ae8de3SMatthias Ringwald if (aConn) { 218405ae8de3SMatthias Ringwald uint8_t status = aConn->bonding_status; 218505ae8de3SMatthias Ringwald uint16_t flags = aConn->bonding_flags; 218686805605S[email protected] bd_addr_t bd_address; 218705ae8de3SMatthias Ringwald memcpy(&bd_address, aConn->address, 6); 218805ae8de3SMatthias Ringwald hci_shutdown_connection(aConn); 2189bb820044S[email protected] // connection struct is gone, don't access anymore 2190bb820044S[email protected] if (flags & BONDING_EMIT_COMPLETE_ON_DISCONNECT){ 219186805605S[email protected] hci_emit_dedicated_bonding_result(bd_address, status); 219286805605S[email protected] } 219386805605S[email protected] } 219486805605S[email protected] } 219586805605S[email protected] } 219686805605S[email protected] 219794ab26f8Smatthias.ringwald // execute main loop 219894ab26f8Smatthias.ringwald hci_run(); 219916833f0aSmatthias.ringwald } 220016833f0aSmatthias.ringwald 220135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2202c91d150bS[email protected] static void sco_handler(uint8_t * packet, uint16_t size){ 22038abbe8b5SMatthias Ringwald if (!hci_stack->sco_packet_handler) return; 22043d50b4baSMatthias Ringwald hci_stack->sco_packet_handler(HCI_SCO_DATA_PACKET, 0, packet, size); 22052b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 22062b838201SMatthias Ringwald hci_con_handle_t con_handle = READ_SCO_CONNECTION_HANDLE(packet); 22072b838201SMatthias Ringwald hci_connection_t *conn = hci_connection_for_handle(con_handle); 22082b838201SMatthias Ringwald if (conn){ 22092b838201SMatthias Ringwald conn->num_packets_completed++; 22102b838201SMatthias Ringwald hci_stack->host_completed_packets = 1; 22112b838201SMatthias Ringwald hci_run(); 22122b838201SMatthias Ringwald } 22132b838201SMatthias Ringwald #endif 2214c91d150bS[email protected] } 221535454696SMatthias Ringwald #endif 2216c91d150bS[email protected] 22170a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 22185bb5bc3eS[email protected] hci_dump_packet(packet_type, 1, packet, size); 221910e830c9Smatthias.ringwald switch (packet_type) { 222010e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 222110e830c9Smatthias.ringwald event_handler(packet, size); 222210e830c9Smatthias.ringwald break; 222310e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 222410e830c9Smatthias.ringwald acl_handler(packet, size); 222510e830c9Smatthias.ringwald break; 222635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2227c91d150bS[email protected] case HCI_SCO_DATA_PACKET: 2228c91d150bS[email protected] sco_handler(packet, size); 2229202c8a4cSMatthias Ringwald break; 223035454696SMatthias Ringwald #endif 223110e830c9Smatthias.ringwald default: 223210e830c9Smatthias.ringwald break; 223310e830c9Smatthias.ringwald } 223410e830c9Smatthias.ringwald } 223510e830c9Smatthias.ringwald 2236d6b06661SMatthias Ringwald /** 2237d6b06661SMatthias Ringwald * @brief Add event packet handler. 2238d6b06661SMatthias Ringwald */ 2239d6b06661SMatthias Ringwald void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){ 2240d6b06661SMatthias Ringwald btstack_linked_list_add_tail(&hci_stack->event_handlers, (btstack_linked_item_t*) callback_handler); 2241d6b06661SMatthias Ringwald } 2242d6b06661SMatthias Ringwald 2243d6b06661SMatthias Ringwald 2244fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 22453d50b4baSMatthias Ringwald void hci_register_acl_packet_handler(btstack_packet_handler_t handler){ 2246fb37a842SMatthias Ringwald hci_stack->acl_packet_handler = handler; 224716833f0aSmatthias.ringwald } 224816833f0aSmatthias.ringwald 224935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 22508abbe8b5SMatthias Ringwald /** 22518abbe8b5SMatthias Ringwald * @brief Registers a packet handler for SCO data. Used for HSP and HFP profiles. 22528abbe8b5SMatthias Ringwald */ 22533d50b4baSMatthias Ringwald void hci_register_sco_packet_handler(btstack_packet_handler_t handler){ 22548abbe8b5SMatthias Ringwald hci_stack->sco_packet_handler = handler; 22558abbe8b5SMatthias Ringwald } 225635454696SMatthias Ringwald #endif 22578abbe8b5SMatthias Ringwald 225871de195eSMatthias Ringwald static void hci_state_reset(void){ 2259595bdbfbS[email protected] // no connections yet 2260595bdbfbS[email protected] hci_stack->connections = NULL; 226174308b23Smatthias.ringwald 226274308b23Smatthias.ringwald // keep discoverable/connectable as this has been requested by the client(s) 226374308b23Smatthias.ringwald // hci_stack->discoverable = 0; 226474308b23Smatthias.ringwald // hci_stack->connectable = 0; 226574308b23Smatthias.ringwald // hci_stack->bondable = 1; 2266b95a5a35SMatthias Ringwald // hci_stack->own_addr_type = 0; 2267595bdbfbS[email protected] 226844935e40S[email protected] // buffer is free 226944935e40S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 227044935e40S[email protected] 2271595bdbfbS[email protected] // no pending cmds 2272595bdbfbS[email protected] hci_stack->decline_reason = 0; 2273595bdbfbS[email protected] hci_stack->new_scan_enable_value = 0xff; 2274595bdbfbS[email protected] 2275595bdbfbS[email protected] // LE 2276b95a5a35SMatthias Ringwald #ifdef ENABLE_BLE 2277b95a5a35SMatthias Ringwald memset(hci_stack->le_random_address, 0, 6); 2278b95a5a35SMatthias Ringwald hci_stack->le_random_address_set = 0; 2279d70217a2SMatthias Ringwald #endif 2280d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 2281595bdbfbS[email protected] hci_stack->le_scanning_state = LE_SCAN_IDLE; 2282e2602ea2Smatthias.ringwald hci_stack->le_scan_type = 0xff; 2283b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 2284e83201bcSMatthias Ringwald hci_stack->le_whitelist = 0; 2285e83201bcSMatthias Ringwald hci_stack->le_whitelist_capacity = 0; 2286d70217a2SMatthias Ringwald #endif 2287d70217a2SMatthias Ringwald 2288e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_interval_min = 6; 2289e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_interval_max = 3200; 2290e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_latency_min = 0; 2291e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_latency_max = 500; 2292e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_supervision_timeout_min = 10; 2293e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_supervision_timeout_max = 3200; 2294595bdbfbS[email protected] } 2295595bdbfbS[email protected] 229635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 22972d5e09d6SMatthias Ringwald /** 22982d5e09d6SMatthias Ringwald * @brief Configure Bluetooth hardware control. Has to be called before power on. 22992d5e09d6SMatthias Ringwald */ 23002d5e09d6SMatthias Ringwald void hci_set_link_key_db(btstack_link_key_db_t const * link_key_db){ 23012d5e09d6SMatthias Ringwald // store and open remote device db 23022d5e09d6SMatthias Ringwald hci_stack->link_key_db = link_key_db; 23032d5e09d6SMatthias Ringwald if (hci_stack->link_key_db) { 23042d5e09d6SMatthias Ringwald hci_stack->link_key_db->open(); 23052d5e09d6SMatthias Ringwald } 23062d5e09d6SMatthias Ringwald } 230735454696SMatthias Ringwald #endif 23082d5e09d6SMatthias Ringwald 23092d5e09d6SMatthias Ringwald void hci_init(const hci_transport_t *transport, const void *config){ 2310475c8125Smatthias.ringwald 23113a9fb326S[email protected] #ifdef HAVE_MALLOC 23123a9fb326S[email protected] if (!hci_stack) { 23133a9fb326S[email protected] hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t)); 23143a9fb326S[email protected] } 23153a9fb326S[email protected] #else 23163a9fb326S[email protected] hci_stack = &hci_stack_static; 23173a9fb326S[email protected] #endif 231866fb9560S[email protected] memset(hci_stack, 0, sizeof(hci_stack_t)); 23193a9fb326S[email protected] 2320475c8125Smatthias.ringwald // reference to use transport layer implementation 23213a9fb326S[email protected] hci_stack->hci_transport = transport; 2322475c8125Smatthias.ringwald 232311e23e5fSmatthias.ringwald // reference to used config 23243a9fb326S[email protected] hci_stack->config = config; 232511e23e5fSmatthias.ringwald 232626a9b6daSMatthias Ringwald // setup pointer for outgoing packet buffer 232726a9b6daSMatthias Ringwald hci_stack->hci_packet_buffer = &hci_stack->hci_packet_buffer_data[HCI_OUTGOING_PRE_BUFFER_SIZE]; 232826a9b6daSMatthias Ringwald 23298fcba05dSmatthias.ringwald // max acl payload size defined in config.h 23303a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 23318fcba05dSmatthias.ringwald 233216833f0aSmatthias.ringwald // register packet handlers with transport 233310e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 2334f5454fc6Smatthias.ringwald 23353a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 2336e2386ba1S[email protected] 2337e2386ba1S[email protected] // class of device 23383a9fb326S[email protected] hci_stack->class_of_device = 0x007a020c; // Smartphone 2339a45d6b9fS[email protected] 2340f20168b8Smatthias.ringwald // bondable by default 2341f20168b8Smatthias.ringwald hci_stack->bondable = 1; 2342f20168b8Smatthias.ringwald 234363048403S[email protected] // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept 23443a9fb326S[email protected] hci_stack->ssp_enable = 1; 23453a9fb326S[email protected] hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT; 23463a9fb326S[email protected] hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING; 23473a9fb326S[email protected] hci_stack->ssp_auto_accept = 1; 234869a97523S[email protected] 2349fac2e2feSMatthias Ringwald // voice setting - signed 16 bit pcm data with CVSD over the air 2350fac2e2feSMatthias Ringwald hci_stack->sco_voice_setting = 0x60; 2351d950d659SMatthias Ringwald 2352595bdbfbS[email protected] hci_state_reset(); 2353475c8125Smatthias.ringwald } 2354475c8125Smatthias.ringwald 23553fb36a29SMatthias Ringwald /** 23563fb36a29SMatthias Ringwald * @brief Configure Bluetooth chipset driver. Has to be called before power on, or right after receiving the local version information 23573fb36a29SMatthias Ringwald */ 23583fb36a29SMatthias Ringwald void hci_set_chipset(const btstack_chipset_t *chipset_driver){ 23593fb36a29SMatthias Ringwald hci_stack->chipset = chipset_driver; 23603fb36a29SMatthias Ringwald 23613fb36a29SMatthias Ringwald // reset chipset driver - init is also called on power_up 23623fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->init){ 23633fb36a29SMatthias Ringwald hci_stack->chipset->init(hci_stack->config); 23643fb36a29SMatthias Ringwald } 23653fb36a29SMatthias Ringwald } 23663fb36a29SMatthias Ringwald 2367fb55bd0aSMatthias Ringwald /** 2368d0b87befSMatthias Ringwald * @brief Configure Bluetooth hardware control. Has to be called after hci_init() but before power on. 2369fb55bd0aSMatthias Ringwald */ 2370fb55bd0aSMatthias Ringwald void hci_set_control(const btstack_control_t *hardware_control){ 2371fb55bd0aSMatthias Ringwald // references to used control implementation 2372fb55bd0aSMatthias Ringwald hci_stack->control = hardware_control; 2373d0b87befSMatthias Ringwald // init with transport config 2374d0b87befSMatthias Ringwald hardware_control->init(hci_stack->config); 2375fb55bd0aSMatthias Ringwald } 2376fb55bd0aSMatthias Ringwald 237771de195eSMatthias Ringwald void hci_close(void){ 2378404843c1Smatthias.ringwald // close remote device db 2379a98592bcSMatthias Ringwald if (hci_stack->link_key_db) { 2380a98592bcSMatthias Ringwald hci_stack->link_key_db->close(); 2381404843c1Smatthias.ringwald } 23827224be7eSMatthias Ringwald 23837224be7eSMatthias Ringwald btstack_linked_list_iterator_t lit; 23847224be7eSMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->connections); 23857224be7eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 23867224be7eSMatthias Ringwald // cancel all l2cap connections by emitting dicsconnection complete before shutdown (free) connection 23877224be7eSMatthias Ringwald hci_connection_t * connection = (hci_connection_t*) btstack_linked_list_iterator_next(&lit); 23887224be7eSMatthias Ringwald hci_emit_disconnection_complete(connection->con_handle, 0x16); // terminated by local host 23897224be7eSMatthias Ringwald hci_shutdown_connection(connection); 2390f5454fc6Smatthias.ringwald } 23917224be7eSMatthias Ringwald 2392f5454fc6Smatthias.ringwald hci_power_control(HCI_POWER_OFF); 23933a9fb326S[email protected] 23943a9fb326S[email protected] #ifdef HAVE_MALLOC 23953a9fb326S[email protected] free(hci_stack); 23963a9fb326S[email protected] #endif 23973a9fb326S[email protected] hci_stack = NULL; 2398404843c1Smatthias.ringwald } 2399404843c1Smatthias.ringwald 240035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 240160b9e82fSMatthias Ringwald void gap_set_class_of_device(uint32_t class_of_device){ 24029e61646fS[email protected] hci_stack->class_of_device = class_of_device; 24039e61646fS[email protected] } 240476f27cffSMatthias Ringwald 240576f27cffSMatthias Ringwald void hci_disable_l2cap_timeout_check(void){ 240676f27cffSMatthias Ringwald disable_l2cap_timeouts = 1; 240776f27cffSMatthias Ringwald } 240835454696SMatthias Ringwald #endif 24099e61646fS[email protected] 241076f27cffSMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 2411f456b2d0S[email protected] // Set Public BD ADDR - passed on to Bluetooth chipset if supported in bt_control_h 2412f456b2d0S[email protected] void hci_set_bd_addr(bd_addr_t addr){ 2413f456b2d0S[email protected] memcpy(hci_stack->custom_bd_addr, addr, 6); 2414f456b2d0S[email protected] hci_stack->custom_bd_addr_set = 1; 2415f456b2d0S[email protected] } 241676f27cffSMatthias Ringwald #endif 2417f456b2d0S[email protected] 24188d213e1aSmatthias.ringwald // State-Module-Driver overview 24198d213e1aSmatthias.ringwald // state module low-level 24208d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 24218d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 24228d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 24238d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 2424d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 2425d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 2426c7e0c5f6Smatthias.ringwald 242740d1c7a4Smatthias.ringwald static int hci_power_control_on(void){ 24287301ad89Smatthias.ringwald 2429038bc64cSmatthias.ringwald // power on 2430f9a30166Smatthias.ringwald int err = 0; 24313a9fb326S[email protected] if (hci_stack->control && hci_stack->control->on){ 2432d0b87befSMatthias Ringwald err = (*hci_stack->control->on)(); 2433f9a30166Smatthias.ringwald } 2434038bc64cSmatthias.ringwald if (err){ 24359da54300S[email protected] log_error( "POWER_ON failed"); 2436038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 2437038bc64cSmatthias.ringwald return err; 2438038bc64cSmatthias.ringwald } 2439038bc64cSmatthias.ringwald 244024b3c629SMatthias Ringwald // int chipset driver 24413fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->init){ 24423fb36a29SMatthias Ringwald hci_stack->chipset->init(hci_stack->config); 24433fb36a29SMatthias Ringwald } 24443fb36a29SMatthias Ringwald 244524b3c629SMatthias Ringwald // init transport 244624b3c629SMatthias Ringwald if (hci_stack->hci_transport->init){ 244724b3c629SMatthias Ringwald hci_stack->hci_transport->init(hci_stack->config); 244824b3c629SMatthias Ringwald } 244924b3c629SMatthias Ringwald 245024b3c629SMatthias Ringwald // open transport 245124b3c629SMatthias Ringwald err = hci_stack->hci_transport->open(); 2452038bc64cSmatthias.ringwald if (err){ 24539da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 24543a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 2455d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 2456f9a30166Smatthias.ringwald } 2457038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 2458038bc64cSmatthias.ringwald return err; 2459038bc64cSmatthias.ringwald } 24608d213e1aSmatthias.ringwald return 0; 24618d213e1aSmatthias.ringwald } 2462038bc64cSmatthias.ringwald 246340d1c7a4Smatthias.ringwald static void hci_power_control_off(void){ 24648d213e1aSmatthias.ringwald 24659da54300S[email protected] log_info("hci_power_control_off"); 24669418f9c9Smatthias.ringwald 24678d213e1aSmatthias.ringwald // close low-level device 246824b3c629SMatthias Ringwald hci_stack->hci_transport->close(); 24698d213e1aSmatthias.ringwald 24709da54300S[email protected] log_info("hci_power_control_off - hci_transport closed"); 24719418f9c9Smatthias.ringwald 24728d213e1aSmatthias.ringwald // power off 24733a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 2474d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 24758d213e1aSmatthias.ringwald } 24769418f9c9Smatthias.ringwald 24779da54300S[email protected] log_info("hci_power_control_off - control closed"); 24789418f9c9Smatthias.ringwald 24793a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 248072ea5239Smatthias.ringwald } 248172ea5239Smatthias.ringwald 248240d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){ 248372ea5239Smatthias.ringwald 24849da54300S[email protected] log_info("hci_power_control_sleep"); 24853144bce4Smatthias.ringwald 2486b429b9b7Smatthias.ringwald #if 0 2487b429b9b7Smatthias.ringwald // don't close serial port during sleep 2488b429b9b7Smatthias.ringwald 248972ea5239Smatthias.ringwald // close low-level device 24903a9fb326S[email protected] hci_stack->hci_transport->close(hci_stack->config); 2491b429b9b7Smatthias.ringwald #endif 249272ea5239Smatthias.ringwald 249372ea5239Smatthias.ringwald // sleep mode 24943a9fb326S[email protected] if (hci_stack->control && hci_stack->control->sleep){ 2495d0b87befSMatthias Ringwald (*hci_stack->control->sleep)(); 249672ea5239Smatthias.ringwald } 2497b429b9b7Smatthias.ringwald 24983a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 24998d213e1aSmatthias.ringwald } 25008d213e1aSmatthias.ringwald 250140d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){ 2502b429b9b7Smatthias.ringwald 25039da54300S[email protected] log_info("hci_power_control_wake"); 2504b429b9b7Smatthias.ringwald 2505b429b9b7Smatthias.ringwald // wake on 25063a9fb326S[email protected] if (hci_stack->control && hci_stack->control->wake){ 2507d0b87befSMatthias Ringwald (*hci_stack->control->wake)(); 2508b429b9b7Smatthias.ringwald } 2509b429b9b7Smatthias.ringwald 2510b429b9b7Smatthias.ringwald #if 0 2511b429b9b7Smatthias.ringwald // open low-level device 25123a9fb326S[email protected] int err = hci_stack->hci_transport->open(hci_stack->config); 2513b429b9b7Smatthias.ringwald if (err){ 25149da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 25153a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 2516d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 2517b429b9b7Smatthias.ringwald } 2518b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 2519b429b9b7Smatthias.ringwald return err; 2520b429b9b7Smatthias.ringwald } 2521b429b9b7Smatthias.ringwald #endif 2522b429b9b7Smatthias.ringwald 2523b429b9b7Smatthias.ringwald return 0; 2524b429b9b7Smatthias.ringwald } 2525b429b9b7Smatthias.ringwald 252644935e40S[email protected] static void hci_power_transition_to_initializing(void){ 252744935e40S[email protected] // set up state machine 252844935e40S[email protected] hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent 252944935e40S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 253044935e40S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 25315c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET; 253244935e40S[email protected] } 2533b429b9b7Smatthias.ringwald 25348d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 2535d661ed19Smatthias.ringwald 2536f04a0c31SMatthias Ringwald log_info("hci_power_control: %d, current mode %u", power_mode, hci_stack->state); 2537d661ed19Smatthias.ringwald 25388d213e1aSmatthias.ringwald int err = 0; 25393a9fb326S[email protected] switch (hci_stack->state){ 25408d213e1aSmatthias.ringwald 25418d213e1aSmatthias.ringwald case HCI_STATE_OFF: 25428d213e1aSmatthias.ringwald switch (power_mode){ 25438d213e1aSmatthias.ringwald case HCI_POWER_ON: 25448d213e1aSmatthias.ringwald err = hci_power_control_on(); 254597b61c7bS[email protected] if (err) { 2546f04a0c31SMatthias Ringwald log_error("hci_power_control_on() error %d", err); 254797b61c7bS[email protected] return err; 254897b61c7bS[email protected] } 254944935e40S[email protected] hci_power_transition_to_initializing(); 25508d213e1aSmatthias.ringwald break; 25518d213e1aSmatthias.ringwald case HCI_POWER_OFF: 25528d213e1aSmatthias.ringwald // do nothing 25538d213e1aSmatthias.ringwald break; 25548d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2555b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 25568d213e1aSmatthias.ringwald break; 25578d213e1aSmatthias.ringwald } 25588d213e1aSmatthias.ringwald break; 25597301ad89Smatthias.ringwald 25608d213e1aSmatthias.ringwald case HCI_STATE_INITIALIZING: 25618d213e1aSmatthias.ringwald switch (power_mode){ 25628d213e1aSmatthias.ringwald case HCI_POWER_ON: 25638d213e1aSmatthias.ringwald // do nothing 25648d213e1aSmatthias.ringwald break; 25658d213e1aSmatthias.ringwald case HCI_POWER_OFF: 25668d213e1aSmatthias.ringwald // no connections yet, just turn it off 25678d213e1aSmatthias.ringwald hci_power_control_off(); 25688d213e1aSmatthias.ringwald break; 25698d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2570b546ac54Smatthias.ringwald // no connections yet, just turn it off 257172ea5239Smatthias.ringwald hci_power_control_sleep(); 25728d213e1aSmatthias.ringwald break; 25738d213e1aSmatthias.ringwald } 25748d213e1aSmatthias.ringwald break; 25757301ad89Smatthias.ringwald 25768d213e1aSmatthias.ringwald case HCI_STATE_WORKING: 25778d213e1aSmatthias.ringwald switch (power_mode){ 25788d213e1aSmatthias.ringwald case HCI_POWER_ON: 25798d213e1aSmatthias.ringwald // do nothing 25808d213e1aSmatthias.ringwald break; 25818d213e1aSmatthias.ringwald case HCI_POWER_OFF: 2582c7e0c5f6Smatthias.ringwald // see hci_run 25833a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 25848d213e1aSmatthias.ringwald break; 25858d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2586b546ac54Smatthias.ringwald // see hci_run 25873a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 258874b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT; 25898d213e1aSmatthias.ringwald break; 25908d213e1aSmatthias.ringwald } 25918d213e1aSmatthias.ringwald break; 25927301ad89Smatthias.ringwald 25938d213e1aSmatthias.ringwald case HCI_STATE_HALTING: 25948d213e1aSmatthias.ringwald switch (power_mode){ 25958d213e1aSmatthias.ringwald case HCI_POWER_ON: 259644935e40S[email protected] hci_power_transition_to_initializing(); 25978d213e1aSmatthias.ringwald break; 25988d213e1aSmatthias.ringwald case HCI_POWER_OFF: 25998d213e1aSmatthias.ringwald // do nothing 26008d213e1aSmatthias.ringwald break; 26018d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2602b546ac54Smatthias.ringwald // see hci_run 26033a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 260474b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT; 26058d213e1aSmatthias.ringwald break; 26068d213e1aSmatthias.ringwald } 26078d213e1aSmatthias.ringwald break; 26088d213e1aSmatthias.ringwald 26098d213e1aSmatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 26108d213e1aSmatthias.ringwald switch (power_mode){ 26118d213e1aSmatthias.ringwald case HCI_POWER_ON: 261228171530Smatthias.ringwald 2613423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 261428171530Smatthias.ringwald // nothing to do, if H4 supports power management 2615d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 26163a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 261774b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_WRITE_SCAN_ENABLE; // init after sleep 261828171530Smatthias.ringwald break; 261928171530Smatthias.ringwald } 262028171530Smatthias.ringwald #endif 262144935e40S[email protected] hci_power_transition_to_initializing(); 26228d213e1aSmatthias.ringwald break; 26238d213e1aSmatthias.ringwald case HCI_POWER_OFF: 2624b546ac54Smatthias.ringwald // see hci_run 26253a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 26268d213e1aSmatthias.ringwald break; 26278d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2628b546ac54Smatthias.ringwald // do nothing 26298d213e1aSmatthias.ringwald break; 26308d213e1aSmatthias.ringwald } 26318d213e1aSmatthias.ringwald break; 26328d213e1aSmatthias.ringwald 26338d213e1aSmatthias.ringwald case HCI_STATE_SLEEPING: 26348d213e1aSmatthias.ringwald switch (power_mode){ 26358d213e1aSmatthias.ringwald case HCI_POWER_ON: 263628171530Smatthias.ringwald 2637423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 263828171530Smatthias.ringwald // nothing to do, if H4 supports power management 2639d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 26403a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 26415c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_AFTER_SLEEP; 2642758b46ceSmatthias.ringwald hci_update_scan_enable(); 264328171530Smatthias.ringwald break; 264428171530Smatthias.ringwald } 264528171530Smatthias.ringwald #endif 26463144bce4Smatthias.ringwald err = hci_power_control_wake(); 26473144bce4Smatthias.ringwald if (err) return err; 264844935e40S[email protected] hci_power_transition_to_initializing(); 26498d213e1aSmatthias.ringwald break; 26508d213e1aSmatthias.ringwald case HCI_POWER_OFF: 26513a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 26528d213e1aSmatthias.ringwald break; 26538d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2654b546ac54Smatthias.ringwald // do nothing 26558d213e1aSmatthias.ringwald break; 26568d213e1aSmatthias.ringwald } 26578d213e1aSmatthias.ringwald break; 265811e23e5fSmatthias.ringwald } 265968d92d03Smatthias.ringwald 2660038bc64cSmatthias.ringwald // create internal event 2661ee8bf225Smatthias.ringwald hci_emit_state(); 2662ee8bf225Smatthias.ringwald 266368d92d03Smatthias.ringwald // trigger next/first action 266468d92d03Smatthias.ringwald hci_run(); 266568d92d03Smatthias.ringwald 2666475c8125Smatthias.ringwald return 0; 2667475c8125Smatthias.ringwald } 2668475c8125Smatthias.ringwald 266935454696SMatthias Ringwald 267035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 267135454696SMatthias Ringwald 2672758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){ 2673758b46ceSmatthias.ringwald // 2 = page scan, 1 = inq scan 26743a9fb326S[email protected] hci_stack->new_scan_enable_value = hci_stack->connectable << 1 | hci_stack->discoverable; 2675758b46ceSmatthias.ringwald hci_run(); 2676758b46ceSmatthias.ringwald } 2677758b46ceSmatthias.ringwald 267815a95bd5SMatthias Ringwald void gap_discoverable_control(uint8_t enable){ 2679381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 2680381fbed8Smatthias.ringwald 26813a9fb326S[email protected] if (hci_stack->discoverable == enable){ 26823a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 2683381fbed8Smatthias.ringwald return; 2684381fbed8Smatthias.ringwald } 2685381fbed8Smatthias.ringwald 26863a9fb326S[email protected] hci_stack->discoverable = enable; 2687758b46ceSmatthias.ringwald hci_update_scan_enable(); 2688758b46ceSmatthias.ringwald } 2689b031bebbSmatthias.ringwald 269015a95bd5SMatthias Ringwald void gap_connectable_control(uint8_t enable){ 2691758b46ceSmatthias.ringwald if (enable) enable = 1; // normalize argument 2692758b46ceSmatthias.ringwald 2693758b46ceSmatthias.ringwald // don't emit event 26943a9fb326S[email protected] if (hci_stack->connectable == enable) return; 2695758b46ceSmatthias.ringwald 26963a9fb326S[email protected] hci_stack->connectable = enable; 2697758b46ceSmatthias.ringwald hci_update_scan_enable(); 2698381fbed8Smatthias.ringwald } 269935454696SMatthias Ringwald #endif 2700381fbed8Smatthias.ringwald 270115a95bd5SMatthias Ringwald void gap_local_bd_addr(bd_addr_t address_buffer){ 2702690bd0baS[email protected] memcpy(address_buffer, hci_stack->local_bd_addr, 6); 27035061f3afS[email protected] } 27045061f3afS[email protected] 27052b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 27062b838201SMatthias Ringwald static void hci_host_num_completed_packets(void){ 27072b838201SMatthias Ringwald 27082b838201SMatthias Ringwald // create packet manually as arrays are not supported and num_commands should not get reduced 27092b838201SMatthias Ringwald hci_reserve_packet_buffer(); 27102b838201SMatthias Ringwald uint8_t * packet = hci_get_outgoing_packet_buffer(); 27112b838201SMatthias Ringwald 27122b838201SMatthias Ringwald uint16_t size = 0; 27132b838201SMatthias Ringwald uint16_t num_handles = 0; 27142b838201SMatthias Ringwald packet[size++] = 0x35; 27152b838201SMatthias Ringwald packet[size++] = 0x0c; 27162b838201SMatthias Ringwald size++; // skip param len 27172b838201SMatthias Ringwald size++; // skip num handles 27182b838201SMatthias Ringwald 27192b838201SMatthias Ringwald // add { handle, packets } entries 27202b838201SMatthias Ringwald btstack_linked_item_t * it; 27212b838201SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 27222b838201SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 27232b838201SMatthias Ringwald if (connection->num_packets_completed){ 27242b838201SMatthias Ringwald little_endian_store_16(packet, size, connection->con_handle); 27252b838201SMatthias Ringwald size += 2; 27262b838201SMatthias Ringwald little_endian_store_16(packet, size, connection->num_packets_completed); 27272b838201SMatthias Ringwald size += 2; 27282b838201SMatthias Ringwald // 27292b838201SMatthias Ringwald num_handles++; 27302b838201SMatthias Ringwald connection->num_packets_completed = 0; 27312b838201SMatthias Ringwald } 27322b838201SMatthias Ringwald } 27332b838201SMatthias Ringwald 27342b838201SMatthias Ringwald packet[2] = size - 3; 27352b838201SMatthias Ringwald packet[3] = num_handles; 27362b838201SMatthias Ringwald 27372b838201SMatthias Ringwald hci_stack->host_completed_packets = 0; 27382b838201SMatthias Ringwald 27392b838201SMatthias Ringwald hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size); 27402b838201SMatthias Ringwald hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 27412b838201SMatthias Ringwald 27422b838201SMatthias Ringwald // release packet buffer for synchronous transport implementations 27432b838201SMatthias Ringwald if (hci_transport_synchronous()){ 27442b838201SMatthias Ringwald hci_stack->hci_packet_buffer_reserved = 0; 27452b838201SMatthias Ringwald } 27462b838201SMatthias Ringwald } 27472b838201SMatthias Ringwald #endif 27482b838201SMatthias Ringwald 274995d71764SMatthias Ringwald static void hci_run(void){ 27508a485f27Smatthias.ringwald 2751db8bc6ffSMatthias Ringwald // log_info("hci_run: entered"); 2752665d90f2SMatthias Ringwald btstack_linked_item_t * it; 275332ab9390Smatthias.ringwald 2754b5d8b22bS[email protected] // send continuation fragments first, as they block the prepared packet buffer 2755b5d8b22bS[email protected] if (hci_stack->acl_fragmentation_total_size > 0) { 2756b5d8b22bS[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer); 2757b5d8b22bS[email protected] hci_connection_t *connection = hci_connection_for_handle(con_handle); 2758b5d8b22bS[email protected] if (connection) { 275928a0332dSMatthias Ringwald if (hci_can_send_prepared_acl_packet_now(con_handle)){ 2760b5d8b22bS[email protected] hci_send_acl_packet_fragments(connection); 2761b5d8b22bS[email protected] return; 2762b5d8b22bS[email protected] } 276328a0332dSMatthias Ringwald } else { 2764b5d8b22bS[email protected] // connection gone -> discard further fragments 276528a0332dSMatthias Ringwald log_info("hci_run: fragmented ACL packet no connection -> discard fragment"); 2766b5d8b22bS[email protected] hci_stack->acl_fragmentation_total_size = 0; 2767b5d8b22bS[email protected] hci_stack->acl_fragmentation_pos = 0; 2768b5d8b22bS[email protected] } 2769b5d8b22bS[email protected] } 2770b5d8b22bS[email protected] 27712b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 27722b838201SMatthias Ringwald // send host num completed packets next as they don't require num_cmd_packets > 0 27732b838201SMatthias Ringwald if (!hci_can_send_comand_packet_transport()) return; 27742b838201SMatthias Ringwald if (hci_stack->host_completed_packets){ 27752b838201SMatthias Ringwald hci_host_num_completed_packets(); 27762b838201SMatthias Ringwald return; 27772b838201SMatthias Ringwald } 27782b838201SMatthias Ringwald #endif 27792b838201SMatthias Ringwald 2780d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 2781ce4c8fabSmatthias.ringwald 2782b031bebbSmatthias.ringwald // global/non-connection oriented commands 2783b031bebbSmatthias.ringwald 278435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2785b031bebbSmatthias.ringwald // decline incoming connections 27863a9fb326S[email protected] if (hci_stack->decline_reason){ 27873a9fb326S[email protected] uint8_t reason = hci_stack->decline_reason; 27883a9fb326S[email protected] hci_stack->decline_reason = 0; 27893a9fb326S[email protected] hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason); 2790dbe1a790S[email protected] return; 2791ce4c8fabSmatthias.ringwald } 2792b031bebbSmatthias.ringwald // send scan enable 27933a9fb326S[email protected] if (hci_stack->state == HCI_STATE_WORKING && hci_stack->new_scan_enable_value != 0xff && hci_classic_supported()){ 27943a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value); 27953a9fb326S[email protected] hci_stack->new_scan_enable_value = 0xff; 2796dbe1a790S[email protected] return; 2797b031bebbSmatthias.ringwald } 2798f5875de5SMatthias Ringwald // start/stop inquiry 2799f5875de5SMatthias Ringwald if (hci_stack->inquiry_state >= GAP_INQUIRY_DURATION_MIN && hci_stack->inquiry_state <= GAP_INQUIRY_DURATION_MAX){ 2800f5875de5SMatthias Ringwald uint8_t duration = hci_stack->inquiry_state; 2801f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_ACTIVE; 2802f5875de5SMatthias Ringwald hci_send_cmd(&hci_inquiry, HCI_INQUIRY_LAP, duration, 0); 2803f5875de5SMatthias Ringwald return; 2804f5875de5SMatthias Ringwald } 2805f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_W2_CANCEL){ 2806f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W4_CANCELLED; 2807f5875de5SMatthias Ringwald hci_send_cmd(&hci_inquiry_cancel); 2808f5875de5SMatthias Ringwald return; 2809f5875de5SMatthias Ringwald } 2810*b7f1ee76SMatthias Ringwald // remote name request 2811*b7f1ee76SMatthias Ringwald if (hci_stack->remote_name_state == GAP_REMOTE_NAME_STATE_W2_SEND){ 2812*b7f1ee76SMatthias Ringwald hci_stack->remote_name_state = GAP_REMOTE_NAME_STATE_W4_COMPLETE; 2813*b7f1ee76SMatthias Ringwald hci_send_cmd(&hci_remote_name_request, hci_stack->remote_name_addr, 2814*b7f1ee76SMatthias Ringwald hci_stack->remote_name_page_scan_repetition_mode, hci_stack->remote_name_clock_offset); 2815*b7f1ee76SMatthias Ringwald } 281635454696SMatthias Ringwald #endif 2817b031bebbSmatthias.ringwald 2818a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 2819b95a5a35SMatthias Ringwald // advertisements, active scanning, and creating connections requires randaom address to be set if using private address 2820b95a5a35SMatthias Ringwald if ((hci_stack->state == HCI_STATE_WORKING) 2821b95a5a35SMatthias Ringwald && (hci_stack->le_own_addr_type == BD_ADDR_TYPE_LE_PUBLIC || hci_stack->le_random_address_set)){ 2822d70217a2SMatthias Ringwald 2823d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 282445c102fdSMatthias Ringwald // handle le scan 28257bdc6798S[email protected] switch(hci_stack->le_scanning_state){ 28267bdc6798S[email protected] case LE_START_SCAN: 28277bdc6798S[email protected] hci_stack->le_scanning_state = LE_SCANNING; 28287bdc6798S[email protected] hci_send_cmd(&hci_le_set_scan_enable, 1, 0); 28297bdc6798S[email protected] return; 28307bdc6798S[email protected] 28317bdc6798S[email protected] case LE_STOP_SCAN: 28327bdc6798S[email protected] hci_stack->le_scanning_state = LE_SCAN_IDLE; 28337bdc6798S[email protected] hci_send_cmd(&hci_le_set_scan_enable, 0, 0); 28347bdc6798S[email protected] return; 28357bdc6798S[email protected] default: 28367bdc6798S[email protected] break; 28377bdc6798S[email protected] } 2838e2602ea2Smatthias.ringwald if (hci_stack->le_scan_type != 0xff){ 2839e2602ea2Smatthias.ringwald // defaults: active scanning, accept all advertisement packets 2840e2602ea2Smatthias.ringwald int scan_type = hci_stack->le_scan_type; 2841e2602ea2Smatthias.ringwald hci_stack->le_scan_type = 0xff; 2842b95a5a35SMatthias 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); 2843e2602ea2Smatthias.ringwald return; 2844e2602ea2Smatthias.ringwald } 2845d70217a2SMatthias Ringwald #endif 2846d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 284745c102fdSMatthias Ringwald // le advertisement control 28489a2e4658SMatthias Ringwald if (hci_stack->le_advertisements_todo){ 28499a2e4658SMatthias Ringwald log_info("hci_run: gap_le: adv todo: %x", hci_stack->le_advertisements_todo ); 28509a2e4658SMatthias Ringwald } 285145c102fdSMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_DISABLE){ 285245c102fdSMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_DISABLE; 285345c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 0); 285445c102fdSMatthias Ringwald return; 285545c102fdSMatthias Ringwald } 285645c102fdSMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_PARAMS){ 285745c102fdSMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_PARAMS; 285845c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_parameters, 285945c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_min, 286045c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_max, 286145c102fdSMatthias Ringwald hci_stack->le_advertisements_type, 2862b95a5a35SMatthias Ringwald hci_stack->le_own_addr_type, 286345c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address_type, 286445c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address, 286545c102fdSMatthias Ringwald hci_stack->le_advertisements_channel_map, 286645c102fdSMatthias Ringwald hci_stack->le_advertisements_filter_policy); 286745c102fdSMatthias Ringwald return; 286845c102fdSMatthias Ringwald } 2869501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_ADV_DATA){ 2870501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 2871b95a5a35SMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_data, hci_stack->le_advertisements_data_len, hci_stack->le_advertisements_data); 287245c102fdSMatthias Ringwald return; 287345c102fdSMatthias Ringwald } 2874501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA){ 2875501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 2876501f56b3SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_response_data, hci_stack->le_scan_response_data_len, 2877501f56b3SMatthias Ringwald hci_stack->le_scan_response_data); 2878501f56b3SMatthias Ringwald return; 2879501f56b3SMatthias Ringwald } 2880b95a5a35SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_ENABLE){ 288145c102fdSMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_ENABLE; 288245c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 1); 288345c102fdSMatthias Ringwald return; 288445c102fdSMatthias Ringwald } 2885d70217a2SMatthias Ringwald #endif 28869956955bSMatthias Ringwald 2887d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 28889956955bSMatthias Ringwald // 28899956955bSMatthias Ringwald // LE Whitelist Management 28909956955bSMatthias Ringwald // 28919956955bSMatthias Ringwald 28929956955bSMatthias Ringwald // check if whitelist needs modification 2893665d90f2SMatthias Ringwald btstack_linked_list_iterator_t lit; 28949956955bSMatthias Ringwald int modification_pending = 0; 2895665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 2896665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 2897665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 28989956955bSMatthias Ringwald if (entry->state & (LE_WHITELIST_REMOVE_FROM_CONTROLLER | LE_WHITELIST_ADD_TO_CONTROLLER)){ 28999956955bSMatthias Ringwald modification_pending = 1; 29009956955bSMatthias Ringwald break; 29019956955bSMatthias Ringwald } 29029956955bSMatthias Ringwald } 290391915b0bSMatthias Ringwald 29049956955bSMatthias Ringwald if (modification_pending){ 290591915b0bSMatthias Ringwald // stop connnecting if modification pending 29069956955bSMatthias Ringwald if (hci_stack->le_connecting_state != LE_CONNECTING_IDLE){ 29079956955bSMatthias Ringwald hci_send_cmd(&hci_le_create_connection_cancel); 29089956955bSMatthias Ringwald return; 29099956955bSMatthias Ringwald } 29109956955bSMatthias Ringwald 29119956955bSMatthias Ringwald // add/remove entries 2912665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 2913665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 2914665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 29159956955bSMatthias Ringwald if (entry->state & LE_WHITELIST_ADD_TO_CONTROLLER){ 29169956955bSMatthias Ringwald entry->state = LE_WHITELIST_ON_CONTROLLER; 29179956955bSMatthias Ringwald hci_send_cmd(&hci_le_add_device_to_white_list, entry->address_type, entry->address); 29189956955bSMatthias Ringwald return; 29199956955bSMatthias Ringwald 29209956955bSMatthias Ringwald } 29219956955bSMatthias Ringwald if (entry->state & LE_WHITELIST_REMOVE_FROM_CONTROLLER){ 29229ecbe201SMatthias Ringwald bd_addr_t address; 29239ecbe201SMatthias Ringwald bd_addr_type_t address_type = entry->address_type; 29249ecbe201SMatthias Ringwald memcpy(address, entry->address, 6); 2925665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry); 29269956955bSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 29279ecbe201SMatthias Ringwald hci_send_cmd(&hci_le_remove_device_from_white_list, address_type, address); 29289956955bSMatthias Ringwald return; 29299956955bSMatthias Ringwald } 29309956955bSMatthias Ringwald } 293191915b0bSMatthias Ringwald } 29329956955bSMatthias Ringwald 29339956955bSMatthias Ringwald // start connecting 293491915b0bSMatthias Ringwald if ( hci_stack->le_connecting_state == LE_CONNECTING_IDLE && 2935665d90f2SMatthias Ringwald !btstack_linked_list_empty(&hci_stack->le_whitelist)){ 29369956955bSMatthias Ringwald bd_addr_t null_addr; 29379956955bSMatthias Ringwald memset(null_addr, 0, 6); 29389956955bSMatthias Ringwald hci_send_cmd(&hci_le_create_connection, 29399956955bSMatthias Ringwald 0x0060, // scan interval: 60 ms 29409956955bSMatthias Ringwald 0x0030, // scan interval: 30 ms 29419956955bSMatthias Ringwald 1, // use whitelist 29429956955bSMatthias Ringwald 0, // peer address type 29439956955bSMatthias Ringwald null_addr, // peer bd addr 2944b95a5a35SMatthias Ringwald hci_stack->le_own_addr_type, // our addr type: 29459956955bSMatthias Ringwald 0x0008, // conn interval min 29469956955bSMatthias Ringwald 0x0018, // conn interval max 29479956955bSMatthias Ringwald 0, // conn latency 29489956955bSMatthias Ringwald 0x0048, // supervision timeout 29499956955bSMatthias Ringwald 0x0001, // min ce length 29509956955bSMatthias Ringwald 0x0001 // max ce length 29519956955bSMatthias Ringwald ); 29529956955bSMatthias Ringwald return; 29539956955bSMatthias Ringwald } 2954d70217a2SMatthias Ringwald #endif 29557bdc6798S[email protected] } 2956b2f949feS[email protected] #endif 29577bdc6798S[email protected] 295832ab9390Smatthias.ringwald // send pending HCI commands 2959665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 296005ae8de3SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 296132ab9390Smatthias.ringwald 29620bf6344aS[email protected] switch(connection->state){ 29630bf6344aS[email protected] case SEND_CREATE_CONNECTION: 29644f3229d8S[email protected] switch(connection->address_type){ 296535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 29664f3229d8S[email protected] case BD_ADDR_TYPE_CLASSIC: 29679da54300S[email protected] log_info("sending hci_create_connection"); 2968ad83dc6aS[email protected] hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 29694f3229d8S[email protected] break; 297035454696SMatthias Ringwald #endif 29714f3229d8S[email protected] default: 2972a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 2973d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 29749da54300S[email protected] log_info("sending hci_le_create_connection"); 29754f3229d8S[email protected] hci_send_cmd(&hci_le_create_connection, 2976b2571179Smatthias.ringwald 0x0060, // scan interval: 60 ms 2977b2571179Smatthias.ringwald 0x0030, // scan interval: 30 ms 29784f3229d8S[email protected] 0, // don't use whitelist 29794f3229d8S[email protected] connection->address_type, // peer address type 29804f3229d8S[email protected] connection->address, // peer bd addr 2981b95a5a35SMatthias Ringwald hci_stack->le_own_addr_type, // our addr type: 2982b2571179Smatthias.ringwald 0x0008, // conn interval min 2983b2571179Smatthias.ringwald 0x0018, // conn interval max 29844f3229d8S[email protected] 0, // conn latency 2985b2571179Smatthias.ringwald 0x0048, // supervision timeout 2986b2571179Smatthias.ringwald 0x0001, // min ce length 2987b2571179Smatthias.ringwald 0x0001 // max ce length 29884f3229d8S[email protected] ); 29894f3229d8S[email protected] 29904f3229d8S[email protected] connection->state = SENT_CREATE_CONNECTION; 2991b2f949feS[email protected] #endif 2992d70217a2SMatthias Ringwald #endif 29934f3229d8S[email protected] break; 29944f3229d8S[email protected] } 2995ad83dc6aS[email protected] return; 2996ad83dc6aS[email protected] 299735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 29980bf6344aS[email protected] case RECEIVED_CONNECTION_REQUEST: 299994e4aaa2SMatthias Ringwald log_info("sending hci_accept_connection_request, remote eSCO %u", connection->remote_supported_feature_eSCO); 300032ab9390Smatthias.ringwald connection->state = ACCEPTED_CONNECTION_REQUEST; 30015cf766e8SMatthias Ringwald connection->role = HCI_ROLE_SLAVE; 3002e35edcc1S[email protected] if (connection->address_type == BD_ADDR_TYPE_CLASSIC){ 300334d2123cS[email protected] hci_send_cmd(&hci_accept_connection_request, connection->address, 1); 3004e35edcc1S[email protected] } 3005dbe1a790S[email protected] return; 300635454696SMatthias Ringwald #endif 30070bf6344aS[email protected] 3008a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 3009d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 30100bf6344aS[email protected] case SEND_CANCEL_CONNECTION: 30110bf6344aS[email protected] connection->state = SENT_CANCEL_CONNECTION; 30120bf6344aS[email protected] hci_send_cmd(&hci_le_create_connection_cancel); 30130bf6344aS[email protected] return; 3014a6725849S[email protected] #endif 3015d70217a2SMatthias Ringwald #endif 30160bf6344aS[email protected] case SEND_DISCONNECT: 30170bf6344aS[email protected] connection->state = SENT_DISCONNECT; 30187851196eSmatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 30190bf6344aS[email protected] return; 30200bf6344aS[email protected] 30210bf6344aS[email protected] default: 30220bf6344aS[email protected] break; 3023c7e0c5f6Smatthias.ringwald } 3024c7e0c5f6Smatthias.ringwald 302535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 302632ab9390Smatthias.ringwald if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){ 30279da54300S[email protected] log_info("responding to link key request"); 302834d2123cS[email protected] connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST); 302932ab9390Smatthias.ringwald link_key_t link_key; 3030c77e8838S[email protected] link_key_type_t link_key_type; 3031a98592bcSMatthias Ringwald if ( hci_stack->link_key_db 3032a98592bcSMatthias Ringwald && hci_stack->link_key_db->get_link_key(connection->address, link_key, &link_key_type) 303334d2123cS[email protected] && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){ 30349ab95c90S[email protected] connection->link_key_type = link_key_type; 303532ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key); 303632ab9390Smatthias.ringwald } else { 303732ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 303832ab9390Smatthias.ringwald } 3039dbe1a790S[email protected] return; 304032ab9390Smatthias.ringwald } 30411d6b20aeS[email protected] 3042899283eaS[email protected] if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){ 30439da54300S[email protected] log_info("denying to pin request"); 3044899283eaS[email protected] connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST); 304534d2123cS[email protected] hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address); 30464c57c146S[email protected] return; 30474c57c146S[email protected] } 30484c57c146S[email protected] 3049dbe1a790S[email protected] if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){ 305034d2123cS[email protected] connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY); 305182d8f825S[email protected] log_info("IO Capability Request received, stack bondable %u, io cap %u", hci_stack->bondable, hci_stack->ssp_io_capability); 305282d8f825S[email protected] if (hci_stack->bondable && (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN)){ 3053106d6d11S[email protected] // tweak authentication requirements 30543a9fb326S[email protected] uint8_t authreq = hci_stack->ssp_authentication_requirement; 3055106d6d11S[email protected] if (connection->bonding_flags & BONDING_DEDICATED){ 30569faad3abS[email protected] authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 30579faad3abS[email protected] } 30589faad3abS[email protected] if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){ 30599faad3abS[email protected] authreq |= 1; 3060106d6d11S[email protected] } 30613a9fb326S[email protected] hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, NULL, authreq); 3062f8fb5f6eS[email protected] } else { 3063f8fb5f6eS[email protected] hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED); 3064f8fb5f6eS[email protected] } 3065dbe1a790S[email protected] return; 306632ab9390Smatthias.ringwald } 306732ab9390Smatthias.ringwald 3068dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){ 3069dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY); 307034d2123cS[email protected] hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address); 3071dbe1a790S[email protected] return; 3072dbe1a790S[email protected] } 3073dbe1a790S[email protected] 3074dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){ 3075dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY); 307634d2123cS[email protected] hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000); 3077dbe1a790S[email protected] return; 3078dbe1a790S[email protected] } 3079afd4e962S[email protected] 3080afd4e962S[email protected] if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){ 3081afd4e962S[email protected] connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES; 308234d2123cS[email protected] hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle); 30832bd8b7e7S[email protected] return; 30842bd8b7e7S[email protected] } 30852bd8b7e7S[email protected] 3086ad83dc6aS[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){ 3087ad83dc6aS[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE; 30881bd5283dS[email protected] connection->bonding_flags |= BONDING_EMIT_COMPLETE_ON_DISCONNECT; 308952d34f98S[email protected] hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // authentication done 3090ad83dc6aS[email protected] return; 3091ad83dc6aS[email protected] } 309276f27cffSMatthias Ringwald 309334d2123cS[email protected] if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){ 309434d2123cS[email protected] connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST; 309534d2123cS[email protected] hci_send_cmd(&hci_authentication_requested, connection->con_handle); 30962bd8b7e7S[email protected] return; 3097afd4e962S[email protected] } 309876f27cffSMatthias Ringwald 3099dce78009S[email protected] if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){ 3100dce78009S[email protected] connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST; 3101dce78009S[email protected] hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1); 3102dce78009S[email protected] return; 3103dce78009S[email protected] } 310476f27cffSMatthias Ringwald #endif 310576f27cffSMatthias Ringwald 310676f27cffSMatthias Ringwald if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){ 310776f27cffSMatthias Ringwald connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK; 310876f27cffSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005); // authentication failure 310976f27cffSMatthias Ringwald return; 311076f27cffSMatthias Ringwald } 3111da886c03S[email protected] 3112a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 3113da886c03S[email protected] if (connection->le_con_parameter_update_state == CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS){ 3114da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 3115da886c03S[email protected] 3116c37a3166S[email protected] uint16_t connection_interval_min = connection->le_conn_interval_min; 3117c37a3166S[email protected] connection->le_conn_interval_min = 0; 3118c37a3166S[email protected] hci_send_cmd(&hci_le_connection_update, connection->con_handle, connection_interval_min, 3119c37a3166S[email protected] connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout, 3120c37a3166S[email protected] 0x0000, 0xffff); 3121c37a3166S[email protected] } 3122c37a3166S[email protected] #endif 3123dbe1a790S[email protected] } 3124c7e0c5f6Smatthias.ringwald 312505ae8de3SMatthias Ringwald hci_connection_t * connection; 31263a9fb326S[email protected] switch (hci_stack->state){ 31273429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 312874b323a9SMatthias Ringwald hci_initializing_run(); 31293429f56bSmatthias.ringwald break; 3130c7e0c5f6Smatthias.ringwald 3131c7e0c5f6Smatthias.ringwald case HCI_STATE_HALTING: 3132c7e0c5f6Smatthias.ringwald 31339da54300S[email protected] log_info("HCI_STATE_HALTING"); 31349956955bSMatthias Ringwald 31359956955bSMatthias Ringwald // free whitelist entries 3136a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 3137d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 31389956955bSMatthias Ringwald { 3139665d90f2SMatthias Ringwald btstack_linked_list_iterator_t lit; 3140665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 3141665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 3142665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 3143665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry); 31449956955bSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 31459956955bSMatthias Ringwald } 31469956955bSMatthias Ringwald } 31479956955bSMatthias Ringwald #endif 3148d70217a2SMatthias Ringwald #endif 3149c7e0c5f6Smatthias.ringwald // close all open connections 31503a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 3151c7e0c5f6Smatthias.ringwald if (connection){ 3152711e6c80SMatthias Ringwald hci_con_handle_t con_handle = (uint16_t) connection->con_handle; 3153d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 315432ab9390Smatthias.ringwald 31558837e9efSMatthias Ringwald log_info("HCI_STATE_HALTING, connection %p, handle %u", connection, con_handle); 3156c7e0c5f6Smatthias.ringwald 31578837e9efSMatthias Ringwald // cancel all l2cap connections right away instead of waiting for disconnection complete event ... 31588837e9efSMatthias Ringwald hci_emit_disconnection_complete(con_handle, 0x16); // terminated by local host 31598837e9efSMatthias Ringwald 31608837e9efSMatthias Ringwald // ... which would be ignored anyway as we shutdown (free) the connection now 3161c7e0c5f6Smatthias.ringwald hci_shutdown_connection(connection); 31628837e9efSMatthias Ringwald 31638837e9efSMatthias Ringwald // finally, send the disconnect command 31648837e9efSMatthias Ringwald hci_send_cmd(&hci_disconnect, con_handle, 0x13); // remote closed connection 3165c7e0c5f6Smatthias.ringwald return; 3166c7e0c5f6Smatthias.ringwald } 31679da54300S[email protected] log_info("HCI_STATE_HALTING, calling off"); 3168c7e0c5f6Smatthias.ringwald 316972ea5239Smatthias.ringwald // switch mode 3170c7e0c5f6Smatthias.ringwald hci_power_control_off(); 31719418f9c9Smatthias.ringwald 31729da54300S[email protected] log_info("HCI_STATE_HALTING, emitting state"); 317372ea5239Smatthias.ringwald hci_emit_state(); 31749da54300S[email protected] log_info("HCI_STATE_HALTING, done"); 317572ea5239Smatthias.ringwald break; 3176c7e0c5f6Smatthias.ringwald 317772ea5239Smatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 31783a9fb326S[email protected] switch(hci_stack->substate) { 317974b323a9SMatthias Ringwald case HCI_FALLING_ASLEEP_DISCONNECT: 31809da54300S[email protected] log_info("HCI_STATE_FALLING_ASLEEP"); 318172ea5239Smatthias.ringwald // close all open connections 31823a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 318366da7044Smatthias.ringwald 3184423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 318566da7044Smatthias.ringwald // don't close connections, if H4 supports power management 3186d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 318766da7044Smatthias.ringwald connection = NULL; 318866da7044Smatthias.ringwald } 318966da7044Smatthias.ringwald #endif 319072ea5239Smatthias.ringwald if (connection){ 319132ab9390Smatthias.ringwald 319272ea5239Smatthias.ringwald // send disconnect 3193d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 319432ab9390Smatthias.ringwald 31959da54300S[email protected] log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u", connection, (uint16_t)connection->con_handle); 31966ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 319772ea5239Smatthias.ringwald 319872ea5239Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 319972ea5239Smatthias.ringwald hci_shutdown_connection(connection); 320072ea5239Smatthias.ringwald return; 320172ea5239Smatthias.ringwald } 320272ea5239Smatthias.ringwald 320392368cd3S[email protected] if (hci_classic_supported()){ 320489db417bSmatthias.ringwald // disable page and inquiry scan 3205d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 320632ab9390Smatthias.ringwald 32079da54300S[email protected] log_info("HCI_STATE_HALTING, disabling inq scans"); 32083a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan 320989db417bSmatthias.ringwald 321089db417bSmatthias.ringwald // continue in next sub state 321174b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE; 321289db417bSmatthias.ringwald break; 321392368cd3S[email protected] } 3214202c8a4cSMatthias Ringwald // no break - fall through for ble-only chips 321592368cd3S[email protected] 321674b323a9SMatthias Ringwald case HCI_FALLING_ASLEEP_COMPLETE: 32179da54300S[email protected] log_info("HCI_STATE_HALTING, calling sleep"); 3218423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 321928171530Smatthias.ringwald // don't actually go to sleep, if H4 supports power management 3220d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 322128171530Smatthias.ringwald // SLEEP MODE reached 32223a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 322328171530Smatthias.ringwald hci_emit_state(); 322428171530Smatthias.ringwald break; 322528171530Smatthias.ringwald } 322628171530Smatthias.ringwald #endif 322772ea5239Smatthias.ringwald // switch mode 32283a9fb326S[email protected] hci_power_control_sleep(); // changes hci_stack->state to SLEEP 3229c7e0c5f6Smatthias.ringwald hci_emit_state(); 323028171530Smatthias.ringwald break; 323128171530Smatthias.ringwald 323289db417bSmatthias.ringwald default: 323389db417bSmatthias.ringwald break; 323489db417bSmatthias.ringwald } 3235c7e0c5f6Smatthias.ringwald break; 3236c7e0c5f6Smatthias.ringwald 32373429f56bSmatthias.ringwald default: 32383429f56bSmatthias.ringwald break; 32391f504dbdSmatthias.ringwald } 32403429f56bSmatthias.ringwald } 324116833f0aSmatthias.ringwald 324231452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 324335454696SMatthias Ringwald // house-keeping 324435454696SMatthias Ringwald 324535454696SMatthias Ringwald if (IS_COMMAND(packet, hci_write_loopback_mode)){ 324635454696SMatthias Ringwald hci_stack->loopback_mode = packet[3]; 324735454696SMatthias Ringwald } 324835454696SMatthias Ringwald 324935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 3250c8e4258aSmatthias.ringwald bd_addr_t addr; 3251c8e4258aSmatthias.ringwald hci_connection_t * conn; 3252c8e4258aSmatthias.ringwald 3253c8e4258aSmatthias.ringwald // create_connection? 3254c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 3255724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 32569da54300S[email protected] log_info("Create_connection to %s", bd_addr_to_str(addr)); 3257c8e4258aSmatthias.ringwald 32582e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 3259ad83dc6aS[email protected] if (!conn){ 326096a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 326117f1ba2aSmatthias.ringwald if (!conn){ 326217f1ba2aSmatthias.ringwald // notify client that alloc failed 32632deddeceSMatthias Ringwald hci_emit_connection_complete(addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 326417f1ba2aSmatthias.ringwald return 0; // don't sent packet to controller 326517f1ba2aSmatthias.ringwald } 3266ad83dc6aS[email protected] conn->state = SEND_CREATE_CONNECTION; 3267ad83dc6aS[email protected] } 3268ad83dc6aS[email protected] log_info("conn state %u", conn->state); 3269ad83dc6aS[email protected] switch (conn->state){ 3270ad83dc6aS[email protected] // if connection active exists 3271ad83dc6aS[email protected] case OPEN: 327262bda3fbS[email protected] // and OPEN, emit connection complete command, don't send to controller 3273274dad91SMatthias Ringwald hci_emit_connection_complete(addr, conn->con_handle, 0); 327462bda3fbS[email protected] return 0; 3275ad83dc6aS[email protected] case SEND_CREATE_CONNECTION: 3276ad83dc6aS[email protected] // connection created by hci, e.g. dedicated bonding 3277ad83dc6aS[email protected] break; 3278ad83dc6aS[email protected] default: 3279ad83dc6aS[email protected] // otherwise, just ignore as it is already in the open process 3280ad83dc6aS[email protected] return 0; 3281ad83dc6aS[email protected] } 3282c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 3283c8e4258aSmatthias.ringwald } 328435454696SMatthias Ringwald 32857fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_reply)){ 32867fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY); 32877fde4af9Smatthias.ringwald } 32887fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){ 32897fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST); 32907fde4af9Smatthias.ringwald } 32917fde4af9Smatthias.ringwald 32928ef73945Smatthias.ringwald if (IS_COMMAND(packet, hci_delete_stored_link_key)){ 3293a98592bcSMatthias Ringwald if (hci_stack->link_key_db){ 3294724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 3295a98592bcSMatthias Ringwald hci_stack->link_key_db->delete_link_key(addr); 32968ef73945Smatthias.ringwald } 32978ef73945Smatthias.ringwald } 3298c8e4258aSmatthias.ringwald 32996724cd9eS[email protected] if (IS_COMMAND(packet, hci_pin_code_request_negative_reply) 33006724cd9eS[email protected] || IS_COMMAND(packet, hci_pin_code_request_reply)){ 3301724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 33022e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 33036724cd9eS[email protected] if (conn){ 33046724cd9eS[email protected] connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE); 33056724cd9eS[email protected] } 33066724cd9eS[email protected] } 33076724cd9eS[email protected] 33086724cd9eS[email protected] if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply) 33096724cd9eS[email protected] || IS_COMMAND(packet, hci_user_confirmation_request_reply) 33106724cd9eS[email protected] || IS_COMMAND(packet, hci_user_passkey_request_negative_reply) 33116724cd9eS[email protected] || IS_COMMAND(packet, hci_user_passkey_request_reply)) { 3312724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 33132e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 33146724cd9eS[email protected] if (conn){ 33156724cd9eS[email protected] connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE); 33166724cd9eS[email protected] } 33176724cd9eS[email protected] } 3318ee752bb8SMatthias Ringwald 3319ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 3320ee752bb8SMatthias Ringwald // setup_synchronous_connection? Voice setting at offset 22 3321ee752bb8SMatthias Ringwald if (IS_COMMAND(packet, hci_setup_synchronous_connection)){ 3322ee752bb8SMatthias Ringwald // TODO: compare to current setting if sco connection already active 3323ee752bb8SMatthias Ringwald hci_stack->sco_voice_setting_active = little_endian_read_16(packet, 15); 3324ee752bb8SMatthias Ringwald } 3325ee752bb8SMatthias Ringwald // accept_synchronus_connection? Voice setting at offset 18 3326ee752bb8SMatthias Ringwald if (IS_COMMAND(packet, hci_accept_synchronous_connection)){ 3327ee752bb8SMatthias Ringwald // TODO: compare to current setting if sco connection already active 3328ee752bb8SMatthias Ringwald hci_stack->sco_voice_setting_active = little_endian_read_16(packet, 19); 3329ee752bb8SMatthias Ringwald } 3330ee752bb8SMatthias Ringwald #endif 333135454696SMatthias Ringwald #endif 33324b3e1e19SMatthias Ringwald 3333a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 3334d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 333569a97523S[email protected] if (IS_COMMAND(packet, hci_le_set_random_address)){ 3336b95a5a35SMatthias Ringwald hci_stack->le_random_address_set = 1; 3337b95a5a35SMatthias Ringwald reverse_bd_addr(&packet[3], hci_stack->le_random_address); 3338d70217a2SMatthias Ringwald } 3339171293d3SMatthias Ringwald if (IS_COMMAND(packet, hci_le_set_advertise_enable)){ 3340171293d3SMatthias Ringwald hci_stack->le_advertisements_active = packet[3]; 3341171293d3SMatthias Ringwald } 3342d70217a2SMatthias Ringwald #endif 3343d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 3344b04dfa37SMatthias Ringwald if (IS_COMMAND(packet, hci_le_create_connection)){ 3345b04dfa37SMatthias Ringwald // white list used? 3346b04dfa37SMatthias Ringwald uint8_t initiator_filter_policy = packet[7]; 3347b04dfa37SMatthias Ringwald switch (initiator_filter_policy){ 3348b04dfa37SMatthias Ringwald case 0: 3349b04dfa37SMatthias Ringwald // whitelist not used 3350b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_DIRECT; 3351b04dfa37SMatthias Ringwald break; 3352b04dfa37SMatthias Ringwald case 1: 3353b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_WHITELIST; 3354b04dfa37SMatthias Ringwald break; 3355b04dfa37SMatthias Ringwald default: 3356b04dfa37SMatthias Ringwald log_error("Invalid initiator_filter_policy in LE Create Connection %u", initiator_filter_policy); 3357b04dfa37SMatthias Ringwald break; 3358b04dfa37SMatthias Ringwald } 3359b04dfa37SMatthias Ringwald } 3360b04dfa37SMatthias Ringwald if (IS_COMMAND(packet, hci_le_create_connection_cancel)){ 3361b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 3362b04dfa37SMatthias Ringwald } 336369a97523S[email protected] #endif 3364d70217a2SMatthias Ringwald #endif 336569a97523S[email protected] 33663a9fb326S[email protected] hci_stack->num_cmd_packets--; 33675bb5bc3eS[email protected] 33685bb5bc3eS[email protected] hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size); 33696b4af23dS[email protected] int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 33706b4af23dS[email protected] 3371d051460cS[email protected] // release packet buffer for synchronous transport implementations 3372c8b9416aS[email protected] if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){ 33736b4af23dS[email protected] hci_stack->hci_packet_buffer_reserved = 0; 33746b4af23dS[email protected] } 33756b4af23dS[email protected] 33766b4af23dS[email protected] return err; 337731452debSmatthias.ringwald } 33788adf0ddaSmatthias.ringwald 33792bd8b7e7S[email protected] // disconnect because of security block 33802bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){ 33812bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 33822bd8b7e7S[email protected] if (!connection) return; 33832bd8b7e7S[email protected] connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 33842bd8b7e7S[email protected] } 33852bd8b7e7S[email protected] 33862bd8b7e7S[email protected] 3387dbe1a790S[email protected] // Configure Secure Simple Pairing 3388dbe1a790S[email protected] 338935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 339035454696SMatthias Ringwald 3391dbe1a790S[email protected] // enable will enable SSP during init 339215a95bd5SMatthias Ringwald void gap_ssp_set_enable(int enable){ 33933a9fb326S[email protected] hci_stack->ssp_enable = enable; 3394dbe1a790S[email protected] } 3395dbe1a790S[email protected] 339695d71764SMatthias Ringwald static int hci_local_ssp_activated(void){ 339715a95bd5SMatthias Ringwald return gap_ssp_supported() && hci_stack->ssp_enable; 33982bd8b7e7S[email protected] } 33992bd8b7e7S[email protected] 3400dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement 340115a95bd5SMatthias Ringwald void gap_ssp_set_io_capability(int io_capability){ 34023a9fb326S[email protected] hci_stack->ssp_io_capability = io_capability; 3403dbe1a790S[email protected] } 340415a95bd5SMatthias Ringwald void gap_ssp_set_authentication_requirement(int authentication_requirement){ 34053a9fb326S[email protected] hci_stack->ssp_authentication_requirement = authentication_requirement; 3406dbe1a790S[email protected] } 3407dbe1a790S[email protected] 3408dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested 340915a95bd5SMatthias Ringwald void gap_ssp_set_auto_accept(int auto_accept){ 34103a9fb326S[email protected] hci_stack->ssp_auto_accept = auto_accept; 3411dbe1a790S[email protected] } 341235454696SMatthias Ringwald #endif 3413dbe1a790S[email protected] 341494be1a66SMatthias Ringwald // va_list part of hci_send_cmd 341594be1a66SMatthias Ringwald int hci_send_cmd_va_arg(const hci_cmd_t *cmd, va_list argptr){ 3416d94d3cafS[email protected] if (!hci_can_send_command_packet_now()){ 34179d14b626S[email protected] log_error("hci_send_cmd called but cannot send packet now"); 34189d14b626S[email protected] return 0; 34199d14b626S[email protected] } 34209d14b626S[email protected] 34215127cc62S[email protected] // for HCI INITIALIZATION 34229da54300S[email protected] // log_info("hci_send_cmd: opcode %04x", cmd->opcode); 34235127cc62S[email protected] hci_stack->last_cmd_opcode = cmd->opcode; 34245127cc62S[email protected] 34259d14b626S[email protected] hci_reserve_packet_buffer(); 34269d14b626S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 342794be1a66SMatthias Ringwald uint16_t size = hci_cmd_create_from_template(packet, cmd, argptr); 342894be1a66SMatthias Ringwald return hci_send_cmd_packet(packet, size); 342994be1a66SMatthias Ringwald } 34309d14b626S[email protected] 343194be1a66SMatthias Ringwald /** 343294be1a66SMatthias Ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 343394be1a66SMatthias Ringwald */ 343494be1a66SMatthias Ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){ 34351cd208adSmatthias.ringwald va_list argptr; 34361cd208adSmatthias.ringwald va_start(argptr, cmd); 343794be1a66SMatthias Ringwald int res = hci_send_cmd_va_arg(cmd, argptr); 34381cd208adSmatthias.ringwald va_end(argptr); 343994be1a66SMatthias Ringwald return res; 344093b8dc03Smatthias.ringwald } 3441c8e4258aSmatthias.ringwald 3442ee091cf1Smatthias.ringwald // Create various non-HCI events. 3443ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 3444ee091cf1Smatthias.ringwald 3445d6b06661SMatthias Ringwald static void hci_emit_event(uint8_t * event, uint16_t size, int dump){ 3446fb37a842SMatthias Ringwald // dump packet 3447d6b06661SMatthias Ringwald if (dump) { 3448300c1ba4SMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, size); 3449d6b06661SMatthias Ringwald } 34501ef6bb52SMatthias Ringwald 3451fb37a842SMatthias Ringwald // dispatch to all event handlers 34521ef6bb52SMatthias Ringwald btstack_linked_list_iterator_t it; 34531ef6bb52SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->event_handlers); 34541ef6bb52SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 34551ef6bb52SMatthias Ringwald btstack_packet_callback_registration_t * entry = (btstack_packet_callback_registration_t*) btstack_linked_list_iterator_next(&it); 3456d9a7306aSMatthias Ringwald entry->callback(HCI_EVENT_PACKET, 0, event, size); 34571ef6bb52SMatthias Ringwald } 3458d6b06661SMatthias Ringwald } 3459d6b06661SMatthias Ringwald 3460d6b06661SMatthias Ringwald static void hci_emit_acl_packet(uint8_t * packet, uint16_t size){ 3461fb37a842SMatthias Ringwald if (!hci_stack->acl_packet_handler) return; 34623d50b4baSMatthias Ringwald hci_stack->acl_packet_handler(HCI_ACL_DATA_PACKET, 0, packet, size); 3463d6b06661SMatthias Ringwald } 3464d6b06661SMatthias Ringwald 346535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 3466701e3307SMatthias Ringwald static void hci_notify_if_sco_can_send_now(void){ 34673bc639ceSMatthias Ringwald // notify SCO sender if waiting 3468701e3307SMatthias Ringwald if (!hci_stack->sco_waiting_for_can_send_now) return; 3469701e3307SMatthias Ringwald if (hci_can_send_sco_packet_now()){ 34703bc639ceSMatthias Ringwald hci_stack->sco_waiting_for_can_send_now = 0; 3471701e3307SMatthias Ringwald uint8_t event[2] = { HCI_EVENT_SCO_CAN_SEND_NOW, 0 }; 3472701e3307SMatthias Ringwald hci_dump_packet(HCI_EVENT_PACKET, 1, event, sizeof(event)); 34733d50b4baSMatthias Ringwald hci_stack->sco_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event)); 34743bc639ceSMatthias Ringwald } 34753bc639ceSMatthias Ringwald } 34761cfb383eSMatthias Ringwald 34771cfb383eSMatthias Ringwald // parsing end emitting has been merged to reduce code size 34781cfb383eSMatthias Ringwald static void gap_inquiry_explode(uint8_t * packet){ 34791cfb383eSMatthias Ringwald uint8_t event[15+GAP_INQUIRY_MAX_NAME_LEN]; 34801cfb383eSMatthias Ringwald 34811cfb383eSMatthias Ringwald uint8_t * eir_data; 34821cfb383eSMatthias Ringwald ad_context_t context; 34831cfb383eSMatthias Ringwald const uint8_t * name; 34841cfb383eSMatthias Ringwald uint8_t name_len; 34851cfb383eSMatthias Ringwald 34861cfb383eSMatthias Ringwald int event_type = hci_event_packet_get_type(packet); 34871cfb383eSMatthias Ringwald int num_reserved_fields = event_type == HCI_EVENT_INQUIRY_RESULT ? 2 : 1; // 2 for old event, 1 otherwise 34881cfb383eSMatthias Ringwald int num_responses = hci_event_inquiry_result_get_num_responses(packet); 34891cfb383eSMatthias Ringwald 34901cfb383eSMatthias Ringwald // event[1] is set at the end 34911cfb383eSMatthias Ringwald int i; 34921cfb383eSMatthias Ringwald for (i=0; i<num_responses;i++){ 34931cfb383eSMatthias Ringwald memset(event, 0, sizeof(event)); 34941cfb383eSMatthias Ringwald event[0] = GAP_EVENT_INQUIRY_RESULT; 34951cfb383eSMatthias Ringwald uint8_t event_size = 18; // if name is not set by EIR 34961cfb383eSMatthias Ringwald 34971cfb383eSMatthias Ringwald memcpy(&event[2], &packet[3 + i*6], 6); // bd_addr 34981cfb383eSMatthias Ringwald event[8] = packet[3 + num_responses*(6) + i*1]; // page_scan_repetition_mode 34991cfb383eSMatthias Ringwald memcpy(&event[9], &packet[3 + num_responses*(6+1+num_reserved_fields) + i*3], 3); // class of device 35001cfb383eSMatthias Ringwald memcpy(&event[12], &packet[3 + num_responses*(6+1+num_reserved_fields+3) + i*2], 2); // clock offset 35011cfb383eSMatthias Ringwald 35021cfb383eSMatthias Ringwald switch (event_type){ 35031cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 35041cfb383eSMatthias Ringwald // 14,15,16,17 = 0, size 18 35051cfb383eSMatthias Ringwald break; 35061cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 35071cfb383eSMatthias Ringwald event[14] = 1; 35081cfb383eSMatthias Ringwald event[15] = packet [3 + num_responses*(6+1+num_reserved_fields+3+2) + i*1]; // rssi 35091cfb383eSMatthias Ringwald // 16,17 = 0, size 18 35101cfb383eSMatthias Ringwald break; 35111cfb383eSMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 35121cfb383eSMatthias Ringwald event[14] = 1; 35131cfb383eSMatthias Ringwald event[15] = packet [3 + num_responses*(6+1+num_reserved_fields+3+2) + i*1]; // rssi 35141cfb383eSMatthias Ringwald // for EIR packets, there is only one reponse in it 35151cfb383eSMatthias Ringwald eir_data = &packet[3 + (6+1+num_reserved_fields+3+2+1)]; 35161cfb383eSMatthias Ringwald name = NULL; 35171cfb383eSMatthias Ringwald // EIR data is 240 bytes in EIR event 35181cfb383eSMatthias Ringwald for (ad_iterator_init(&context, 240, eir_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)){ 35191cfb383eSMatthias Ringwald uint8_t data_type = ad_iterator_get_data_type(&context); 35201cfb383eSMatthias Ringwald uint8_t data_size = ad_iterator_get_data_len(&context); 35211cfb383eSMatthias Ringwald const uint8_t * data = ad_iterator_get_data(&context); 35221cfb383eSMatthias Ringwald // Prefer Complete Local Name over Shortend Local Name 35231cfb383eSMatthias Ringwald switch (data_type){ 35241cfb383eSMatthias Ringwald case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME: 35251cfb383eSMatthias Ringwald if (name) continue; 35261cfb383eSMatthias Ringwald /* explicit fall-through */ 35271cfb383eSMatthias Ringwald case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME: 35281cfb383eSMatthias Ringwald name = data; 35291cfb383eSMatthias Ringwald name_len = data_size; 35301cfb383eSMatthias Ringwald break; 35311cfb383eSMatthias Ringwald default: 35321cfb383eSMatthias Ringwald break; 35331cfb383eSMatthias Ringwald } 35341cfb383eSMatthias Ringwald } 35351cfb383eSMatthias Ringwald if (name){ 35361cfb383eSMatthias Ringwald event[16] = 1; 35371cfb383eSMatthias Ringwald // truncate name if needed 35381cfb383eSMatthias Ringwald int len = btstack_min(name_len, GAP_INQUIRY_MAX_NAME_LEN); 35391cfb383eSMatthias Ringwald event[17] = len; 35401cfb383eSMatthias Ringwald memcpy(&event[18], name, len); 35411cfb383eSMatthias Ringwald event_size += len; 35421cfb383eSMatthias Ringwald } 35431cfb383eSMatthias Ringwald break; 35441cfb383eSMatthias Ringwald } 35451cfb383eSMatthias Ringwald event[1] = event_size - 2; 35461cfb383eSMatthias Ringwald hci_emit_event(event, event_size, 1); 35471cfb383eSMatthias Ringwald } 35481cfb383eSMatthias Ringwald } 354935454696SMatthias Ringwald #endif 35503bc639ceSMatthias Ringwald 355171de195eSMatthias Ringwald void hci_emit_state(void){ 35523a9fb326S[email protected] log_info("BTSTACK_EVENT_STATE %u", hci_stack->state); 3553425d1371Smatthias.ringwald uint8_t event[3]; 355480d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 3555425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 35563a9fb326S[email protected] event[2] = hci_stack->state; 3557d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3558c8e4258aSmatthias.ringwald } 3559c8e4258aSmatthias.ringwald 356035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 35612deddeceSMatthias Ringwald static void hci_emit_connection_complete(bd_addr_t address, hci_con_handle_t con_handle, uint8_t status){ 3562425d1371Smatthias.ringwald uint8_t event[13]; 3563c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 3564425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 356517f1ba2aSmatthias.ringwald event[2] = status; 35662deddeceSMatthias Ringwald little_endian_store_16(event, 3, con_handle); 35672deddeceSMatthias Ringwald reverse_bd_addr(address, &event[5]); 3568c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 3569c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 3570d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3571c8e4258aSmatthias.ringwald } 357252db98b2SMatthias Ringwald static void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 357352db98b2SMatthias Ringwald if (disable_l2cap_timeouts) return; 357452db98b2SMatthias Ringwald log_info("L2CAP_EVENT_TIMEOUT_CHECK"); 357552db98b2SMatthias Ringwald uint8_t event[4]; 357652db98b2SMatthias Ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 357752db98b2SMatthias Ringwald event[1] = sizeof(event) - 2; 357852db98b2SMatthias Ringwald little_endian_store_16(event, 2, conn->con_handle); 357952db98b2SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 358052db98b2SMatthias Ringwald } 358135454696SMatthias Ringwald #endif 3582c8e4258aSmatthias.ringwald 358335454696SMatthias Ringwald #ifdef ENABLE_BLE 3584d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 3585fc64f94aSMatthias 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){ 35864f3229d8S[email protected] uint8_t event[21]; 35874f3229d8S[email protected] event[0] = HCI_EVENT_LE_META; 35884f3229d8S[email protected] event[1] = sizeof(event) - 2; 35894f3229d8S[email protected] event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE; 35904f3229d8S[email protected] event[3] = status; 3591fc64f94aSMatthias Ringwald little_endian_store_16(event, 4, con_handle); 35924f3229d8S[email protected] event[6] = 0; // TODO: role 35936e2e9a6bS[email protected] event[7] = address_type; 3594724d70a2SMatthias Ringwald reverse_bd_addr(address, &event[8]); 3595f8fbdce0SMatthias Ringwald little_endian_store_16(event, 14, 0); // interval 3596f8fbdce0SMatthias Ringwald little_endian_store_16(event, 16, 0); // latency 3597f8fbdce0SMatthias Ringwald little_endian_store_16(event, 18, 0); // supervision timeout 35984f3229d8S[email protected] event[20] = 0; // master clock accuracy 3599d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 36004f3229d8S[email protected] } 360135454696SMatthias Ringwald #endif 3602d70217a2SMatthias Ringwald #endif 36034f3229d8S[email protected] 3604fc64f94aSMatthias Ringwald static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason){ 3605425d1371Smatthias.ringwald uint8_t event[6]; 36063c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 3607e518c4b8Smatthias.ringwald event[1] = sizeof(event) - 2; 36083c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 3609fc64f94aSMatthias Ringwald little_endian_store_16(event, 3, con_handle); 36103c4d4b90Smatthias.ringwald event[5] = reason; 3611d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 36123c4d4b90Smatthias.ringwald } 36133c4d4b90Smatthias.ringwald 3614b83d5eabSMatthias Ringwald static void hci_emit_nr_connections_changed(void){ 3615e0abb8e7S[email protected] log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections()); 3616425d1371Smatthias.ringwald uint8_t event[3]; 361780d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 3618425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 361943bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 3620d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 362143bfb1bdSmatthias.ringwald } 3622038bc64cSmatthias.ringwald 3623b83d5eabSMatthias Ringwald static void hci_emit_hci_open_failed(void){ 3624e0abb8e7S[email protected] log_info("BTSTACK_EVENT_POWERON_FAILED"); 3625425d1371Smatthias.ringwald uint8_t event[2]; 362680d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 3627425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 3628d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3629038bc64cSmatthias.ringwald } 36301b0e3922Smatthias.ringwald 363135454696SMatthias Ringwald static void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status){ 363235454696SMatthias Ringwald log_info("hci_emit_dedicated_bonding_result %u ", status); 363335454696SMatthias Ringwald uint8_t event[9]; 363435454696SMatthias Ringwald int pos = 0; 363535454696SMatthias Ringwald event[pos++] = GAP_EVENT_DEDICATED_BONDING_COMPLETED; 363635454696SMatthias Ringwald event[pos++] = sizeof(event) - 2; 363735454696SMatthias Ringwald event[pos++] = status; 363835454696SMatthias Ringwald reverse_bd_addr(address, &event[pos]); 3639d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3640381fbed8Smatthias.ringwald } 3641458bf4e8S[email protected] 364235454696SMatthias Ringwald 364335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 364435454696SMatthias Ringwald 3645b83d5eabSMatthias Ringwald static void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){ 3646df3354fcS[email protected] log_info("hci_emit_security_level %u for handle %x", level, con_handle); 3647a00031e2S[email protected] uint8_t event[5]; 3648e00caf9cS[email protected] int pos = 0; 36495611a760SMatthias Ringwald event[pos++] = GAP_EVENT_SECURITY_LEVEL; 3650e00caf9cS[email protected] event[pos++] = sizeof(event) - 2; 3651f8fbdce0SMatthias Ringwald little_endian_store_16(event, 2, con_handle); 3652e00caf9cS[email protected] pos += 2; 3653e00caf9cS[email protected] event[pos++] = level; 3654d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3655e00caf9cS[email protected] } 3656e00caf9cS[email protected] 365735454696SMatthias Ringwald static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){ 365835454696SMatthias Ringwald if (!connection) return LEVEL_0; 365935454696SMatthias Ringwald if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0; 366035454696SMatthias Ringwald return gap_security_level_for_link_key_type(connection->link_key_type); 366135454696SMatthias Ringwald } 366235454696SMatthias Ringwald 366335454696SMatthias Ringwald static void hci_emit_discoverable_enabled(uint8_t enabled){ 366435454696SMatthias Ringwald log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled); 366535454696SMatthias Ringwald uint8_t event[3]; 366635454696SMatthias Ringwald event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED; 366735454696SMatthias Ringwald event[1] = sizeof(event) - 2; 366835454696SMatthias Ringwald event[2] = enabled; 3669d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3670ad83dc6aS[email protected] } 3671ad83dc6aS[email protected] 367206b9e820SMatthias Ringwald #ifdef ENABLE_CLASSIC 367398a2fd1cSMatthias Ringwald // query if remote side supports eSCO 3674073bd0faSMatthias Ringwald int hci_remote_esco_supported(hci_con_handle_t con_handle){ 367598a2fd1cSMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 367698a2fd1cSMatthias Ringwald if (!connection) return 0; 367798a2fd1cSMatthias Ringwald return connection->remote_supported_feature_eSCO; 367898a2fd1cSMatthias Ringwald } 367998a2fd1cSMatthias Ringwald 36802bd8b7e7S[email protected] // query if remote side supports SSP 36812bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){ 36822bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 36832bd8b7e7S[email protected] if (!connection) return 0; 36842bd8b7e7S[email protected] return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0; 36852bd8b7e7S[email protected] } 36862bd8b7e7S[email protected] 368715a95bd5SMatthias Ringwald int gap_ssp_supported_on_both_sides(hci_con_handle_t handle){ 3688df3354fcS[email protected] return hci_local_ssp_activated() && hci_remote_ssp_supported(handle); 3689df3354fcS[email protected] } 369006b9e820SMatthias Ringwald #endif 3691df3354fcS[email protected] 3692458bf4e8S[email protected] // GAP API 3693458bf4e8S[email protected] /** 3694458bf4e8S[email protected] * @bbrief enable/disable bonding. default is enabled 3695458bf4e8S[email protected] * @praram enabled 3696458bf4e8S[email protected] */ 36974c57c146S[email protected] void gap_set_bondable_mode(int enable){ 36983a9fb326S[email protected] hci_stack->bondable = enable ? 1 : 0; 3699458bf4e8S[email protected] } 37004ef6443cSMatthias Ringwald /** 37014ef6443cSMatthias Ringwald * @brief Get bondable mode. 37024ef6443cSMatthias Ringwald * @return 1 if bondable 37034ef6443cSMatthias Ringwald */ 37044ef6443cSMatthias Ringwald int gap_get_bondable_mode(void){ 37054ef6443cSMatthias Ringwald return hci_stack->bondable; 37064ef6443cSMatthias Ringwald } 3707cb230b9dS[email protected] 3708cb230b9dS[email protected] /** 370934d2123cS[email protected] * @brief map link keys to security levels 3710cb230b9dS[email protected] */ 371134d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){ 371234d2123cS[email protected] switch (link_key_type){ 37133c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 37143c68dfa9S[email protected] return LEVEL_4; 37153c68dfa9S[email protected] case COMBINATION_KEY: 37163c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 37173c68dfa9S[email protected] return LEVEL_3; 37183c68dfa9S[email protected] default: 37193c68dfa9S[email protected] return LEVEL_2; 37203c68dfa9S[email protected] } 3721cb230b9dS[email protected] } 3722cb230b9dS[email protected] 3723106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){ 37245127cc62S[email protected] log_info("gap_mitm_protection_required_for_security_level %u", level); 3725106d6d11S[email protected] return level > LEVEL_2; 3726106d6d11S[email protected] } 3727106d6d11S[email protected] 372834d2123cS[email protected] /** 372934d2123cS[email protected] * @brief get current security level 373034d2123cS[email protected] */ 373134d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){ 373234d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 373334d2123cS[email protected] if (!connection) return LEVEL_0; 373434d2123cS[email protected] return gap_security_level_for_connection(connection); 373534d2123cS[email protected] } 373634d2123cS[email protected] 3737cb230b9dS[email protected] /** 3738cb230b9dS[email protected] * @brief request connection to device to 3739cb230b9dS[email protected] * @result GAP_AUTHENTICATION_RESULT 3740cb230b9dS[email protected] */ 374134d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){ 374234d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 374334d2123cS[email protected] if (!connection){ 3744a00031e2S[email protected] hci_emit_security_level(con_handle, LEVEL_0); 374534d2123cS[email protected] return; 374634d2123cS[email protected] } 374734d2123cS[email protected] gap_security_level_t current_level = gap_security_level(con_handle); 374834d2123cS[email protected] log_info("gap_request_security_level %u, current level %u", requested_level, current_level); 374934d2123cS[email protected] if (current_level >= requested_level){ 3750a00031e2S[email protected] hci_emit_security_level(con_handle, current_level); 375134d2123cS[email protected] return; 375234d2123cS[email protected] } 3753a00031e2S[email protected] 375434d2123cS[email protected] connection->requested_security_level = requested_level; 3755a00031e2S[email protected] 375625bf5872S[email protected] #if 0 375725bf5872S[email protected] // sending encryption request without a link key results in an error. 375825bf5872S[email protected] // TODO: figure out how to use it properly 375925bf5872S[email protected] 3760fb8ba0dbS[email protected] // would enabling ecnryption suffice (>= LEVEL_2)? 3761a98592bcSMatthias Ringwald if (hci_stack->link_key_db){ 3762a00031e2S[email protected] link_key_type_t link_key_type; 3763a00031e2S[email protected] link_key_t link_key; 3764a98592bcSMatthias Ringwald if (hci_stack->link_key_db->get_link_key( &connection->address, &link_key, &link_key_type)){ 3765a00031e2S[email protected] if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){ 3766a00031e2S[email protected] connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 3767a00031e2S[email protected] return; 3768a00031e2S[email protected] } 3769a00031e2S[email protected] } 3770a00031e2S[email protected] } 377125bf5872S[email protected] #endif 3772a00031e2S[email protected] 37731eb2563eS[email protected] // try to authenticate connection 37741eb2563eS[email protected] connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 3775e80b2cf9S[email protected] hci_run(); 3776e00caf9cS[email protected] } 3777ad83dc6aS[email protected] 3778ad83dc6aS[email protected] /** 3779ad83dc6aS[email protected] * @brief start dedicated bonding with device. disconnect after bonding 3780ad83dc6aS[email protected] * @param device 3781ad83dc6aS[email protected] * @param request MITM protection 3782ad83dc6aS[email protected] * @result GAP_DEDICATED_BONDING_COMPLETE 3783ad83dc6aS[email protected] */ 3784ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){ 3785ad83dc6aS[email protected] 3786ad83dc6aS[email protected] // create connection state machine 378796a45072S[email protected] hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_CLASSIC); 3788ad83dc6aS[email protected] 3789ad83dc6aS[email protected] if (!connection){ 3790ad83dc6aS[email protected] return BTSTACK_MEMORY_ALLOC_FAILED; 3791ad83dc6aS[email protected] } 3792ad83dc6aS[email protected] 3793ad83dc6aS[email protected] // delete linkn key 379415a95bd5SMatthias Ringwald gap_drop_link_key_for_bd_addr(device); 3795ad83dc6aS[email protected] 3796ad83dc6aS[email protected] // configure LEVEL_2/3, dedicated bonding 3797ad83dc6aS[email protected] connection->state = SEND_CREATE_CONNECTION; 3798ad83dc6aS[email protected] connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2; 3799f04a0c31SMatthias Ringwald log_info("gap_dedicated_bonding, mitm %d -> level %u", mitm_protection_required, connection->requested_security_level); 3800ad83dc6aS[email protected] connection->bonding_flags = BONDING_DEDICATED; 3801ad83dc6aS[email protected] 3802ad83dc6aS[email protected] // wait for GAP Security Result and send GAP Dedicated Bonding complete 3803ad83dc6aS[email protected] 3804ad83dc6aS[email protected] // handle: connnection failure (connection complete != ok) 3805ad83dc6aS[email protected] // handle: authentication failure 3806ad83dc6aS[email protected] // handle: disconnect on done 3807ad83dc6aS[email protected] 3808ad83dc6aS[email protected] hci_run(); 3809ad83dc6aS[email protected] 3810ad83dc6aS[email protected] return 0; 3811ad83dc6aS[email protected] } 381235454696SMatthias Ringwald #endif 38138e618f72S[email protected] 38148e618f72S[email protected] void gap_set_local_name(const char * local_name){ 38158e618f72S[email protected] hci_stack->local_name = local_name; 38168e618f72S[email protected] } 38178e618f72S[email protected] 381835454696SMatthias Ringwald 381935454696SMatthias Ringwald #ifdef ENABLE_BLE 382035454696SMatthias Ringwald 3821d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 3822d8e8f12aSMatthias Ringwald void gap_start_scan(void){ 3823d8e8f12aSMatthias Ringwald if (hci_stack->le_scanning_state == LE_SCANNING) return; 38247bdc6798S[email protected] hci_stack->le_scanning_state = LE_START_SCAN; 38257bdc6798S[email protected] hci_run(); 38267bdc6798S[email protected] } 38278e618f72S[email protected] 3828d8e8f12aSMatthias Ringwald void gap_stop_scan(void){ 3829d8e8f12aSMatthias Ringwald if ( hci_stack->le_scanning_state == LE_SCAN_IDLE) return; 38307bdc6798S[email protected] hci_stack->le_scanning_state = LE_STOP_SCAN; 38317bdc6798S[email protected] hci_run(); 38327bdc6798S[email protected] } 38334f3229d8S[email protected] 3834d8e8f12aSMatthias Ringwald void gap_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){ 3835ef11999fSmatthias.ringwald hci_stack->le_scan_type = scan_type; 3836ef11999fSmatthias.ringwald hci_stack->le_scan_interval = scan_interval; 3837ef11999fSmatthias.ringwald hci_stack->le_scan_window = scan_window; 3838ef11999fSmatthias.ringwald hci_run(); 3839ef11999fSmatthias.ringwald } 38404f3229d8S[email protected] 3841d8e8f12aSMatthias Ringwald uint8_t gap_connect(bd_addr_t addr, bd_addr_type_t addr_type){ 38424f3229d8S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 38434f3229d8S[email protected] if (!conn){ 3844d8e8f12aSMatthias Ringwald log_info("gap_connect: no connection exists yet, creating context"); 38452e77e513S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 38464f3229d8S[email protected] if (!conn){ 38474f3229d8S[email protected] // notify client that alloc failed 38486e2e9a6bS[email protected] hci_emit_le_connection_complete(addr_type, addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 3849d8e8f12aSMatthias Ringwald log_info("gap_connect: failed to alloc hci_connection_t"); 3850472a5742SMatthias Ringwald return GATT_CLIENT_NOT_CONNECTED; // don't sent packet to controller 38514f3229d8S[email protected] } 38524f3229d8S[email protected] conn->state = SEND_CREATE_CONNECTION; 3853d8e8f12aSMatthias Ringwald log_info("gap_connect: send create connection next"); 3854564fca32S[email protected] hci_run(); 3855616edd56SMatthias Ringwald return 0; 38564f3229d8S[email protected] } 38570bf6344aS[email protected] 38580bf6344aS[email protected] if (!hci_is_le_connection(conn) || 38590bf6344aS[email protected] conn->state == SEND_CREATE_CONNECTION || 38600bf6344aS[email protected] conn->state == SENT_CREATE_CONNECTION) { 38612e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_COMMAND_DISALLOWED); 3862d8e8f12aSMatthias Ringwald log_error("gap_connect: classic connection or connect is already being created"); 3863616edd56SMatthias Ringwald return GATT_CLIENT_IN_WRONG_STATE; 38640bf6344aS[email protected] } 38650bf6344aS[email protected] 3866d8e8f12aSMatthias Ringwald log_info("gap_connect: context exists with state %u", conn->state); 38672e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, conn->con_handle, 0); 38684f3229d8S[email protected] hci_run(); 3869616edd56SMatthias Ringwald return 0; 38704f3229d8S[email protected] } 38714f3229d8S[email protected] 38727851196eSmatthias.ringwald // @assumption: only a single outgoing LE Connection exists 3873d8e8f12aSMatthias Ringwald static hci_connection_t * gap_get_outgoing_connection(void){ 3874665d90f2SMatthias Ringwald btstack_linked_item_t *it; 3875665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 38760bf6344aS[email protected] hci_connection_t * conn = (hci_connection_t *) it; 38770bf6344aS[email protected] if (!hci_is_le_connection(conn)) continue; 38780bf6344aS[email protected] switch (conn->state){ 3879a6725849S[email protected] case SEND_CREATE_CONNECTION: 38807851196eSmatthias.ringwald case SENT_CREATE_CONNECTION: 38817851196eSmatthias.ringwald return conn; 38827851196eSmatthias.ringwald default: 38837851196eSmatthias.ringwald break; 38847851196eSmatthias.ringwald }; 38857851196eSmatthias.ringwald } 38867851196eSmatthias.ringwald return NULL; 38877851196eSmatthias.ringwald } 38887851196eSmatthias.ringwald 3889d8e8f12aSMatthias Ringwald uint8_t gap_connect_cancel(void){ 3890d8e8f12aSMatthias Ringwald hci_connection_t * conn = gap_get_outgoing_connection(); 3891616edd56SMatthias Ringwald if (!conn) return 0; 38927851196eSmatthias.ringwald switch (conn->state){ 38937851196eSmatthias.ringwald case SEND_CREATE_CONNECTION: 38947851196eSmatthias.ringwald // skip sending create connection and emit event instead 38952e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER); 3896665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 38977851196eSmatthias.ringwald btstack_memory_hci_connection_free( conn ); 38980bf6344aS[email protected] break; 3899a6725849S[email protected] case SENT_CREATE_CONNECTION: 39007851196eSmatthias.ringwald // request to send cancel connection 39010bf6344aS[email protected] conn->state = SEND_CANCEL_CONNECTION; 39020bf6344aS[email protected] hci_run(); 39030bf6344aS[email protected] break; 39040bf6344aS[email protected] default: 39050bf6344aS[email protected] break; 39060bf6344aS[email protected] } 3907616edd56SMatthias Ringwald return 0; 3908e31f89a7S[email protected] } 3909d70217a2SMatthias Ringwald #endif 39104f3229d8S[email protected] 3911c37a3166S[email protected] /** 3912c37a3166S[email protected] * @brief Updates the connection parameters for a given LE connection 3913c37a3166S[email protected] * @param handle 3914c37a3166S[email protected] * @param conn_interval_min (unit: 1.25ms) 3915c37a3166S[email protected] * @param conn_interval_max (unit: 1.25ms) 3916c37a3166S[email protected] * @param conn_latency 3917c37a3166S[email protected] * @param supervision_timeout (unit: 10ms) 3918c37a3166S[email protected] * @returns 0 if ok 3919c37a3166S[email protected] */ 3920c37a3166S[email protected] int gap_update_connection_parameters(hci_con_handle_t con_handle, uint16_t conn_interval_min, 3921c37a3166S[email protected] uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 3922c37a3166S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 3923c37a3166S[email protected] if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3924c37a3166S[email protected] connection->le_conn_interval_min = conn_interval_min; 3925c37a3166S[email protected] connection->le_conn_interval_max = conn_interval_max; 3926c37a3166S[email protected] connection->le_conn_latency = conn_latency; 3927c37a3166S[email protected] connection->le_supervision_timeout = supervision_timeout; 392884cf6d83SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS; 3929cfc59f1bSMatthias Ringwald hci_run(); 3930c37a3166S[email protected] return 0; 3931c37a3166S[email protected] } 3932c37a3166S[email protected] 393345c102fdSMatthias Ringwald /** 3934b68d7bc3SMatthias Ringwald * @brief Request an update of the connection parameter for a given LE connection 3935b68d7bc3SMatthias Ringwald * @param handle 3936b68d7bc3SMatthias Ringwald * @param conn_interval_min (unit: 1.25ms) 3937b68d7bc3SMatthias Ringwald * @param conn_interval_max (unit: 1.25ms) 3938b68d7bc3SMatthias Ringwald * @param conn_latency 3939b68d7bc3SMatthias Ringwald * @param supervision_timeout (unit: 10ms) 3940b68d7bc3SMatthias Ringwald * @returns 0 if ok 3941b68d7bc3SMatthias Ringwald */ 3942b68d7bc3SMatthias Ringwald int gap_request_connection_parameter_update(hci_con_handle_t con_handle, uint16_t conn_interval_min, 3943b68d7bc3SMatthias Ringwald uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 3944b68d7bc3SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 3945b68d7bc3SMatthias Ringwald if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3946b68d7bc3SMatthias Ringwald connection->le_conn_interval_min = conn_interval_min; 3947b68d7bc3SMatthias Ringwald connection->le_conn_interval_max = conn_interval_max; 3948b68d7bc3SMatthias Ringwald connection->le_conn_latency = conn_latency; 3949b68d7bc3SMatthias Ringwald connection->le_supervision_timeout = supervision_timeout; 3950b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_REQUEST; 3951b68d7bc3SMatthias Ringwald hci_run(); 3952b68d7bc3SMatthias Ringwald return 0; 3953b68d7bc3SMatthias Ringwald } 3954b68d7bc3SMatthias Ringwald 3955d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 3956d70217a2SMatthias Ringwald 3957501f56b3SMatthias Ringwald static void gap_advertisments_changed(void){ 3958501f56b3SMatthias Ringwald // disable advertisements before updating adv, scan data, or adv params 3959501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_active){ 3960501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_DISABLE | LE_ADVERTISEMENT_TASKS_ENABLE; 3961501f56b3SMatthias Ringwald } 3962501f56b3SMatthias Ringwald hci_run(); 3963501f56b3SMatthias Ringwald } 3964501f56b3SMatthias Ringwald 3965b68d7bc3SMatthias Ringwald /** 396645c102fdSMatthias Ringwald * @brief Set Advertisement Data 396745c102fdSMatthias Ringwald * @param advertising_data_length 396845c102fdSMatthias Ringwald * @param advertising_data (max 31 octets) 396945c102fdSMatthias Ringwald * @note data is not copied, pointer has to stay valid 397045c102fdSMatthias Ringwald */ 397145c102fdSMatthias Ringwald void gap_advertisements_set_data(uint8_t advertising_data_length, uint8_t * advertising_data){ 397245c102fdSMatthias Ringwald hci_stack->le_advertisements_data_len = advertising_data_length; 397345c102fdSMatthias Ringwald hci_stack->le_advertisements_data = advertising_data; 3974501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 3975501f56b3SMatthias Ringwald gap_advertisments_changed(); 397645c102fdSMatthias Ringwald } 3977501f56b3SMatthias Ringwald 3978501f56b3SMatthias Ringwald /** 3979501f56b3SMatthias Ringwald * @brief Set Scan Response Data 3980501f56b3SMatthias Ringwald * @param advertising_data_length 3981501f56b3SMatthias Ringwald * @param advertising_data (max 31 octets) 3982501f56b3SMatthias Ringwald * @note data is not copied, pointer has to stay valid 3983501f56b3SMatthias Ringwald */ 3984501f56b3SMatthias Ringwald void gap_scan_response_set_data(uint8_t scan_response_data_length, uint8_t * scan_response_data){ 3985501f56b3SMatthias Ringwald hci_stack->le_scan_response_data_len = scan_response_data_length; 3986501f56b3SMatthias Ringwald hci_stack->le_scan_response_data = scan_response_data; 3987501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 3988501f56b3SMatthias Ringwald gap_advertisments_changed(); 398945c102fdSMatthias Ringwald } 399045c102fdSMatthias Ringwald 399145c102fdSMatthias Ringwald /** 399245c102fdSMatthias Ringwald * @brief Set Advertisement Parameters 399345c102fdSMatthias Ringwald * @param adv_int_min 399445c102fdSMatthias Ringwald * @param adv_int_max 399545c102fdSMatthias Ringwald * @param adv_type 399645c102fdSMatthias Ringwald * @param direct_address_type 399745c102fdSMatthias Ringwald * @param direct_address 399845c102fdSMatthias Ringwald * @param channel_map 399945c102fdSMatthias Ringwald * @param filter_policy 400045c102fdSMatthias Ringwald * 400145c102fdSMatthias Ringwald * @note internal use. use gap_advertisements_set_params from gap_le.h instead. 400245c102fdSMatthias Ringwald */ 400345c102fdSMatthias Ringwald void hci_le_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type, 4004b95a5a35SMatthias Ringwald uint8_t direct_address_typ, bd_addr_t direct_address, 400545c102fdSMatthias Ringwald uint8_t channel_map, uint8_t filter_policy) { 400645c102fdSMatthias Ringwald 400745c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_min = adv_int_min; 400845c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_max = adv_int_max; 400945c102fdSMatthias Ringwald hci_stack->le_advertisements_type = adv_type; 401045c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address_type = direct_address_typ; 401145c102fdSMatthias Ringwald hci_stack->le_advertisements_channel_map = channel_map; 401245c102fdSMatthias Ringwald hci_stack->le_advertisements_filter_policy = filter_policy; 401345c102fdSMatthias Ringwald memcpy(hci_stack->le_advertisements_direct_address, direct_address, 6); 401445c102fdSMatthias Ringwald 401545c102fdSMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 4016501f56b3SMatthias Ringwald gap_advertisments_changed(); 401745c102fdSMatthias Ringwald } 401845c102fdSMatthias Ringwald 401945c102fdSMatthias Ringwald /** 402045c102fdSMatthias Ringwald * @brief Enable/Disable Advertisements 402145c102fdSMatthias Ringwald * @param enabled 402245c102fdSMatthias Ringwald */ 402345c102fdSMatthias Ringwald void gap_advertisements_enable(int enabled){ 402445c102fdSMatthias Ringwald hci_stack->le_advertisements_enabled = enabled; 402545c102fdSMatthias Ringwald if (enabled && !hci_stack->le_advertisements_active){ 402645c102fdSMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_ENABLE; 402745c102fdSMatthias Ringwald } 402845c102fdSMatthias Ringwald if (!enabled && hci_stack->le_advertisements_active){ 402945c102fdSMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_DISABLE; 403045c102fdSMatthias Ringwald } 4031cfc59f1bSMatthias Ringwald hci_run(); 403245c102fdSMatthias Ringwald } 403345c102fdSMatthias Ringwald 403435454696SMatthias Ringwald #endif 403506e5cf96SMatthias Ringwald 403606e5cf96SMatthias Ringwald void hci_le_set_own_address_type(uint8_t own_address_type){ 403706e5cf96SMatthias Ringwald log_info("hci_le_set_own_address_type: old %u, new %u", hci_stack->le_own_addr_type, own_address_type); 403806e5cf96SMatthias Ringwald if (own_address_type == hci_stack->le_own_addr_type) return; 403906e5cf96SMatthias Ringwald hci_stack->le_own_addr_type = own_address_type; 404006e5cf96SMatthias Ringwald 404164068776SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 404206e5cf96SMatthias Ringwald // update advertisement parameters, too 404306e5cf96SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 404406e5cf96SMatthias Ringwald gap_advertisments_changed(); 404564068776SMatthias Ringwald #endif 404664068776SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 404706e5cf96SMatthias Ringwald // note: we don't update scan parameters or modify ongoing connection attempts 404864068776SMatthias Ringwald #endif 404906e5cf96SMatthias Ringwald } 405006e5cf96SMatthias Ringwald 4051d70217a2SMatthias Ringwald #endif 405245c102fdSMatthias Ringwald 4053616edd56SMatthias Ringwald uint8_t gap_disconnect(hci_con_handle_t handle){ 40545917a5c5S[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 40555917a5c5S[email protected] if (!conn){ 40567851196eSmatthias.ringwald hci_emit_disconnection_complete(handle, 0); 4057616edd56SMatthias Ringwald return 0; 40585917a5c5S[email protected] } 40595917a5c5S[email protected] conn->state = SEND_DISCONNECT; 40605917a5c5S[email protected] hci_run(); 4061616edd56SMatthias Ringwald return 0; 40624f3229d8S[email protected] } 406304a6ef8cSmatthias.ringwald 4064a1bf5ae7SMatthias Ringwald /** 4065a1bf5ae7SMatthias Ringwald * @brief Get connection type 4066a1bf5ae7SMatthias Ringwald * @param con_handle 4067a1bf5ae7SMatthias Ringwald * @result connection_type 4068a1bf5ae7SMatthias Ringwald */ 4069a1bf5ae7SMatthias Ringwald gap_connection_type_t gap_get_connection_type(hci_con_handle_t connection_handle){ 4070a1bf5ae7SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(connection_handle); 4071a1bf5ae7SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 4072a1bf5ae7SMatthias Ringwald switch (conn->address_type){ 4073a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 4074a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 4075a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_LE; 4076a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_SCO: 4077a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_SCO; 4078a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_CLASSIC: 4079a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_ACL; 4080a1bf5ae7SMatthias Ringwald default: 4081a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_INVALID; 4082a1bf5ae7SMatthias Ringwald } 4083a1bf5ae7SMatthias Ringwald } 4084a1bf5ae7SMatthias Ringwald 4085a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 40864f551432SMatthias Ringwald 4087d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4088d23838ecSMatthias Ringwald /** 4089ac9c45e0SMatthias Ringwald * @brief Auto Connection Establishment - Start Connecting to device 4090ac9c45e0SMatthias Ringwald * @param address_typ 4091ac9c45e0SMatthias Ringwald * @param address 4092ac9c45e0SMatthias Ringwald * @returns 0 if ok 4093ac9c45e0SMatthias Ringwald */ 40944f551432SMatthias Ringwald int gap_auto_connection_start(bd_addr_type_t address_type, bd_addr_t address){ 4095e83201bcSMatthias Ringwald // check capacity 4096665d90f2SMatthias Ringwald int num_entries = btstack_linked_list_count(&hci_stack->le_whitelist); 409791915b0bSMatthias Ringwald if (num_entries >= hci_stack->le_whitelist_capacity) return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED; 4098e83201bcSMatthias Ringwald whitelist_entry_t * entry = btstack_memory_whitelist_entry_get(); 4099e83201bcSMatthias Ringwald if (!entry) return BTSTACK_MEMORY_ALLOC_FAILED; 4100e83201bcSMatthias Ringwald entry->address_type = address_type; 4101e83201bcSMatthias Ringwald memcpy(entry->address, address, 6); 4102e83201bcSMatthias Ringwald entry->state = LE_WHITELIST_ADD_TO_CONTROLLER; 4103665d90f2SMatthias Ringwald btstack_linked_list_add(&hci_stack->le_whitelist, (btstack_linked_item_t*) entry); 4104e83201bcSMatthias Ringwald hci_run(); 4105e83201bcSMatthias Ringwald return 0; 4106ac9c45e0SMatthias Ringwald } 4107ac9c45e0SMatthias Ringwald 410842ff5ba1SMatthias Ringwald static void hci_remove_from_whitelist(bd_addr_type_t address_type, bd_addr_t address){ 4109665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 4110665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 4111665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 4112665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it); 4113e83201bcSMatthias Ringwald if (entry->address_type != address_type) continue; 4114e83201bcSMatthias Ringwald if (memcmp(entry->address, address, 6) != 0) continue; 4115e83201bcSMatthias Ringwald if (entry->state & LE_WHITELIST_ON_CONTROLLER){ 4116e83201bcSMatthias Ringwald // remove from controller if already present 4117e83201bcSMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER; 4118e83201bcSMatthias Ringwald continue; 4119e83201bcSMatthias Ringwald } 4120e83201bcSMatthias Ringwald // direclty remove entry from whitelist 4121665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 4122e83201bcSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 4123e83201bcSMatthias Ringwald } 412442ff5ba1SMatthias Ringwald } 412542ff5ba1SMatthias Ringwald 412642ff5ba1SMatthias Ringwald /** 412742ff5ba1SMatthias Ringwald * @brief Auto Connection Establishment - Stop Connecting to device 412842ff5ba1SMatthias Ringwald * @param address_typ 412942ff5ba1SMatthias Ringwald * @param address 413042ff5ba1SMatthias Ringwald * @returns 0 if ok 413142ff5ba1SMatthias Ringwald */ 413242ff5ba1SMatthias Ringwald int gap_auto_connection_stop(bd_addr_type_t address_type, bd_addr_t address){ 413342ff5ba1SMatthias Ringwald hci_remove_from_whitelist(address_type, address); 4134e83201bcSMatthias Ringwald hci_run(); 4135e83201bcSMatthias Ringwald return 0; 4136ac9c45e0SMatthias Ringwald } 4137ac9c45e0SMatthias Ringwald 4138ac9c45e0SMatthias Ringwald /** 4139ac9c45e0SMatthias Ringwald * @brief Auto Connection Establishment - Stop everything 4140ac9c45e0SMatthias Ringwald * @note Convenience function to stop all active auto connection attempts 4141ac9c45e0SMatthias Ringwald */ 4142ac9c45e0SMatthias Ringwald void gap_auto_connection_stop_all(void){ 4143665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 4144665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 4145665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 4146665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it); 4147e83201bcSMatthias Ringwald if (entry->state & LE_WHITELIST_ON_CONTROLLER){ 4148e83201bcSMatthias Ringwald // remove from controller if already present 4149e83201bcSMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER; 4150e83201bcSMatthias Ringwald continue; 4151e83201bcSMatthias Ringwald } 415291915b0bSMatthias Ringwald // directly remove entry from whitelist 4153665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 4154e83201bcSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 4155e83201bcSMatthias Ringwald } 4156e83201bcSMatthias Ringwald hci_run(); 4157ac9c45e0SMatthias Ringwald } 4158d70217a2SMatthias Ringwald #endif 41594f551432SMatthias Ringwald #endif 41604f551432SMatthias Ringwald 416135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 4162ac9c45e0SMatthias Ringwald /** 4163ff00ed1cSMatthias Ringwald * @brief Set Extended Inquiry Response data 4164ff00ed1cSMatthias Ringwald * @param eir_data size 240 bytes, is not copied make sure memory is accessible during stack startup 4165ff00ed1cSMatthias Ringwald * @note has to be done before stack starts up 4166ff00ed1cSMatthias Ringwald */ 4167ff00ed1cSMatthias Ringwald void gap_set_extended_inquiry_response(const uint8_t * data){ 4168ff00ed1cSMatthias Ringwald hci_stack->eir_data = data; 4169ff00ed1cSMatthias Ringwald } 4170ff00ed1cSMatthias Ringwald 4171ff00ed1cSMatthias Ringwald /** 4172f5875de5SMatthias Ringwald * @brief Start GAP Classic Inquiry 4173f5875de5SMatthias Ringwald * @param duration in 1.28s units 4174f5875de5SMatthias Ringwald * @return 0 if ok 4175f5875de5SMatthias Ringwald * @events: GAP_EVENT_INQUIRY_RESULT, GAP_EVENT_INQUIRY_COMPLETE 4176f5875de5SMatthias Ringwald */ 4177f5875de5SMatthias Ringwald int gap_inquiry_start(uint8_t duration_in_1280ms_units){ 4178f5875de5SMatthias Ringwald if (hci_stack->inquiry_state != GAP_INQUIRY_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 4179f5875de5SMatthias Ringwald if (duration_in_1280ms_units < GAP_INQUIRY_DURATION_MIN || duration_in_1280ms_units > GAP_INQUIRY_DURATION_MAX){ 4180f5875de5SMatthias Ringwald return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 4181f5875de5SMatthias Ringwald } 4182f5875de5SMatthias Ringwald hci_stack->inquiry_state = duration_in_1280ms_units; 4183f5875de5SMatthias Ringwald hci_run(); 4184f5875de5SMatthias Ringwald return 0; 4185f5875de5SMatthias Ringwald } 4186f5875de5SMatthias Ringwald 4187f5875de5SMatthias Ringwald /** 4188f5875de5SMatthias Ringwald * @brief Stop GAP Classic Inquiry 4189f5875de5SMatthias Ringwald * @returns 0 if ok 4190f5875de5SMatthias Ringwald */ 4191f5875de5SMatthias Ringwald int gap_inquiry_stop(void){ 4192f5875de5SMatthias Ringwald if (hci_stack->inquiry_state >= GAP_INQUIRY_DURATION_MIN || hci_stack->inquiry_state <= GAP_INQUIRY_DURATION_MAX) { 4193f5875de5SMatthias Ringwald // emit inquiry complete event, before it even started 4194f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 4195f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 4196f5875de5SMatthias Ringwald return 0; 4197f5875de5SMatthias Ringwald } 4198f5875de5SMatthias Ringwald if (hci_stack->inquiry_state != GAP_INQUIRY_STATE_ACTIVE) return ERROR_CODE_COMMAND_DISALLOWED; 4199f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W2_CANCEL; 4200f5875de5SMatthias Ringwald hci_run(); 4201f5875de5SMatthias Ringwald return 0; 4202f5875de5SMatthias Ringwald } 4203f5875de5SMatthias Ringwald 4204*b7f1ee76SMatthias Ringwald 4205*b7f1ee76SMatthias Ringwald /** 4206*b7f1ee76SMatthias Ringwald * @brief Remote Name Request 4207*b7f1ee76SMatthias Ringwald * @param addr 4208*b7f1ee76SMatthias Ringwald * @param page_scan_repetition_mode 4209*b7f1ee76SMatthias Ringwald * @param clock_offset only used when bit 15 is set 4210*b7f1ee76SMatthias Ringwald * @events: HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 4211*b7f1ee76SMatthias Ringwald */ 4212*b7f1ee76SMatthias Ringwald int gap_remote_name_request(bd_addr_t addr, uint8_t page_scan_repetition_mode, uint16_t clock_offset){ 4213*b7f1ee76SMatthias Ringwald if (hci_stack->remote_name_state != GAP_REMOTE_NAME_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 4214*b7f1ee76SMatthias Ringwald memcpy(hci_stack->remote_name_addr, addr, 6); 4215*b7f1ee76SMatthias Ringwald hci_stack->remote_name_page_scan_repetition_mode = page_scan_repetition_mode; 4216*b7f1ee76SMatthias Ringwald hci_stack->remote_name_clock_offset = clock_offset; 4217*b7f1ee76SMatthias Ringwald hci_stack->remote_name_state = GAP_REMOTE_NAME_STATE_W2_SEND; 4218*b7f1ee76SMatthias Ringwald hci_run(); 4219*b7f1ee76SMatthias Ringwald return 0; 4220*b7f1ee76SMatthias Ringwald } 4221*b7f1ee76SMatthias Ringwald 4222f5875de5SMatthias Ringwald /** 4223f6858d14SMatthias Ringwald * @brief Set inquiry mode: standard, with RSSI, with RSSI + Extended Inquiry Results. Has to be called before power on. 4224f5875de5SMatthias Ringwald * @param inquiry_mode see bluetooth_defines.h 4225f6858d14SMatthias Ringwald */ 4226f6858d14SMatthias Ringwald void hci_set_inquiry_mode(inquiry_mode_t mode){ 4227f6858d14SMatthias Ringwald hci_stack->inquiry_mode = mode; 4228f6858d14SMatthias Ringwald } 4229f6858d14SMatthias Ringwald 4230f6858d14SMatthias Ringwald /** 4231d950d659SMatthias Ringwald * @brief Configure Voice Setting for use with SCO data in HSP/HFP 4232d950d659SMatthias Ringwald */ 4233d950d659SMatthias Ringwald void hci_set_sco_voice_setting(uint16_t voice_setting){ 4234d950d659SMatthias Ringwald hci_stack->sco_voice_setting = voice_setting; 4235d950d659SMatthias Ringwald } 4236d950d659SMatthias Ringwald 4237d950d659SMatthias Ringwald /** 4238d950d659SMatthias Ringwald * @brief Get SCO Voice Setting 4239d950d659SMatthias Ringwald * @return current voice setting 4240d950d659SMatthias Ringwald */ 42410cb5b971SMatthias Ringwald uint16_t hci_get_sco_voice_setting(void){ 4242d950d659SMatthias Ringwald return hci_stack->sco_voice_setting; 4243d950d659SMatthias Ringwald } 4244d950d659SMatthias Ringwald 4245b3aad8daSMatthias Ringwald /** @brief Get SCO packet length for current SCO Voice setting 4246b3aad8daSMatthias Ringwald * @note Using SCO packets of the exact length is required for USB transfer 4247b3aad8daSMatthias Ringwald * @return Length of SCO packets in bytes (not audio frames) 4248b3aad8daSMatthias Ringwald */ 4249b3aad8daSMatthias Ringwald int hci_get_sco_packet_length(void){ 4250b3aad8daSMatthias Ringwald // see Core Spec for H2 USB Transfer. 4251b3aad8daSMatthias Ringwald if (hci_stack->sco_voice_setting & 0x0020) return 51; 4252b3aad8daSMatthias Ringwald return 27; 4253b3aad8daSMatthias Ringwald } 425435454696SMatthias Ringwald #endif 4255b3aad8daSMatthias Ringwald 4256d950d659SMatthias Ringwald /** 4257d23838ecSMatthias Ringwald * @brief Set callback for Bluetooth Hardware Error 4258d23838ecSMatthias Ringwald */ 4259c2e1fa60SMatthias Ringwald void hci_set_hardware_error_callback(void (*fn)(uint8_t error)){ 4260d23838ecSMatthias Ringwald hci_stack->hardware_error_callback = fn; 4261d23838ecSMatthias Ringwald } 4262d23838ecSMatthias Ringwald 426371de195eSMatthias Ringwald void hci_disconnect_all(void){ 4264665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 4265665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 4266665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 4267665d90f2SMatthias Ringwald hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 426804a6ef8cSmatthias.ringwald if (con->state == SENT_DISCONNECT) continue; 426904a6ef8cSmatthias.ringwald con->state = SEND_DISCONNECT; 427004a6ef8cSmatthias.ringwald } 4271d31fba26S[email protected] hci_run(); 427204a6ef8cSmatthias.ringwald } 427333373e40SMatthias Ringwald 427433373e40SMatthias Ringwald uint16_t hci_get_manufacturer(void){ 427533373e40SMatthias Ringwald return hci_stack->manufacturer; 427633373e40SMatthias Ringwald } 4277