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" 74*1cfb383eSMatthias 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" 79*1cfb383eSMatthias 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 99*1cfb383eSMatthias Ringwald // Names are arbitrarily shortened to 32 bytes if not requested otherwise 100*1cfb383eSMatthias Ringwald #ifndef GAP_INQUIRY_MAX_NAME_LEN 101*1cfb383eSMatthias Ringwald #define GAP_INQUIRY_MAX_NAME_LEN 32 102*1cfb383eSMatthias Ringwald #endif 103*1cfb383eSMatthias Ringwald 104b83d5eabSMatthias Ringwald // prototypes 10535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 106758b46ceSmatthias.ringwald static void hci_update_scan_enable(void); 10735454696SMatthias Ringwald static void hci_emit_discoverable_enabled(uint8_t enabled); 10835454696SMatthias Ringwald static int hci_local_ssp_activated(void); 10935454696SMatthias Ringwald static int hci_remote_ssp_supported(hci_con_handle_t con_handle); 11035454696SMatthias Ringwald static void hci_notify_if_sco_can_send_now(void); 11135454696SMatthias Ringwald static void hci_emit_connection_complete(bd_addr_t address, hci_con_handle_t con_handle, uint8_t status); 112a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection); 11335454696SMatthias Ringwald static void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level); 114ec820d77SMatthias Ringwald static void hci_connection_timeout_handler(btstack_timer_source_t *timer); 11596a45072S[email protected] static void hci_connection_timestamp(hci_connection_t *connection); 11652db98b2SMatthias Ringwald static void hci_emit_l2cap_check_timeout(hci_connection_t *conn); 117*1cfb383eSMatthias Ringwald static void gap_inquiry_explode(uint8_t * packet); 11852db98b2SMatthias Ringwald #endif 119*1cfb383eSMatthias Ringwald 1207586ee35S[email protected] static int hci_power_control_on(void); 1217586ee35S[email protected] static void hci_power_control_off(void); 1226da48142SSean Wilson static void hci_state_reset(void); 123fc64f94aSMatthias Ringwald static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason); 124b83d5eabSMatthias Ringwald static void hci_emit_nr_connections_changed(void); 125b83d5eabSMatthias Ringwald static void hci_emit_hci_open_failed(void); 126b83d5eabSMatthias Ringwald static void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status); 127b83d5eabSMatthias Ringwald static void hci_emit_event(uint8_t * event, uint16_t size, int dump); 128b83d5eabSMatthias Ringwald static void hci_emit_acl_packet(uint8_t * packet, uint16_t size); 12995d71764SMatthias Ringwald static void hci_run(void); 13095d71764SMatthias Ringwald static int hci_is_le_connection(hci_connection_t * connection); 13195d71764SMatthias Ringwald static int hci_number_free_acl_slots_for_connection_type( bd_addr_type_t address_type); 1325d509858SMatthias Ringwald 133a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 134e8c8828eSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 13539677e66SMatthias Ringwald // called from test/ble_client/advertising_data_parser.c 13639677e66SMatthias Ringwald void le_handle_advertisement_report(uint8_t *packet, int size); 13742ff5ba1SMatthias Ringwald static void hci_remove_from_whitelist(bd_addr_type_t address_type, bd_addr_t address); 1385d509858SMatthias Ringwald #endif 139d70217a2SMatthias Ringwald #endif 140758b46ceSmatthias.ringwald 14106b35ec0Smatthias.ringwald // the STACK is here 1423a9fb326S[email protected] #ifndef HAVE_MALLOC 1433a9fb326S[email protected] static hci_stack_t hci_stack_static; 1443a9fb326S[email protected] #endif 1453a9fb326S[email protected] static hci_stack_t * hci_stack = NULL; 14616833f0aSmatthias.ringwald 1471c9e5e9dSMatthias Ringwald #ifdef ENABLE_CLASSIC 14866fb9560S[email protected] // test helper 14966fb9560S[email protected] static uint8_t disable_l2cap_timeouts = 0; 1501c9e5e9dSMatthias Ringwald #endif 15166fb9560S[email protected] 15296a45072S[email protected] /** 15396a45072S[email protected] * create connection for given address 15496a45072S[email protected] * 15596a45072S[email protected] * @return connection OR NULL, if no memory left 15696a45072S[email protected] */ 15796a45072S[email protected] static hci_connection_t * create_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){ 1581a06f663S[email protected] log_info("create_connection_for_addr %s, type %x", bd_addr_to_str(addr), addr_type); 159bb69aaaeS[email protected] hci_connection_t * conn = btstack_memory_hci_connection_get(); 16096a45072S[email protected] if (!conn) return NULL; 161c91d150bS[email protected] memset(conn, 0, sizeof(hci_connection_t)); 162058e3d6bSMatthias Ringwald bd_addr_copy(conn->address, addr); 16396a45072S[email protected] conn->address_type = addr_type; 16496a45072S[email protected] conn->con_handle = 0xffff; 16596a45072S[email protected] conn->authentication_flags = AUTH_FLAGS_NONE; 16696a45072S[email protected] conn->bonding_flags = 0; 16796a45072S[email protected] conn->requested_security_level = LEVEL_0; 16852db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 16991a977e8SMatthias Ringwald btstack_run_loop_set_timer_handler(&conn->timeout, hci_connection_timeout_handler); 17091a977e8SMatthias Ringwald btstack_run_loop_set_timer_context(&conn->timeout, conn); 17196a45072S[email protected] hci_connection_timestamp(conn); 17252db98b2SMatthias Ringwald #endif 17396a45072S[email protected] conn->acl_recombination_length = 0; 17496a45072S[email protected] conn->acl_recombination_pos = 0; 17596a45072S[email protected] conn->num_acl_packets_sent = 0; 1761a06f663S[email protected] conn->num_sco_packets_sent = 0; 177da886c03S[email protected] conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 178665d90f2SMatthias Ringwald btstack_linked_list_add(&hci_stack->connections, (btstack_linked_item_t *) conn); 17996a45072S[email protected] return conn; 18096a45072S[email protected] } 18166fb9560S[email protected] 182da886c03S[email protected] 183da886c03S[email protected] /** 184da886c03S[email protected] * get le connection parameter range 185da886c03S[email protected] * 186da886c03S[email protected] * @return le connection parameter range struct 187da886c03S[email protected] */ 1884ced4e8cSMatthias Ringwald void gap_get_connection_parameter_range(le_connection_parameter_range_t * range){ 1894ced4e8cSMatthias Ringwald *range = hci_stack->le_connection_parameter_range; 190da886c03S[email protected] } 191da886c03S[email protected] 192da886c03S[email protected] /** 193da886c03S[email protected] * set le connection parameter range 194da886c03S[email protected] * 195da886c03S[email protected] */ 196da886c03S[email protected] 1974ced4e8cSMatthias Ringwald void gap_set_connection_parameter_range(le_connection_parameter_range_t *range){ 1984ced4e8cSMatthias Ringwald hci_stack->le_connection_parameter_range = *range; 199da886c03S[email protected] } 200da886c03S[email protected] 201da886c03S[email protected] /** 202da886c03S[email protected] * get hci connections iterator 203da886c03S[email protected] * 204da886c03S[email protected] * @return hci connections iterator 205da886c03S[email protected] */ 206da886c03S[email protected] 207665d90f2SMatthias Ringwald void hci_connections_get_iterator(btstack_linked_list_iterator_t *it){ 208665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(it, &hci_stack->connections); 209da886c03S[email protected] } 210da886c03S[email protected] 21197addcc5Smatthias.ringwald /** 212ee091cf1Smatthias.ringwald * get connection for a given handle 213ee091cf1Smatthias.ringwald * 214ee091cf1Smatthias.ringwald * @return connection OR NULL, if not found 215ee091cf1Smatthias.ringwald */ 2165061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){ 217665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 218665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 219665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 220665d90f2SMatthias Ringwald hci_connection_t * item = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 2213ac2fe56S[email protected] if ( item->con_handle == con_handle ) { 222da886c03S[email protected] return item; 223ee091cf1Smatthias.ringwald } 224ee091cf1Smatthias.ringwald } 225ee091cf1Smatthias.ringwald return NULL; 226ee091cf1Smatthias.ringwald } 227ee091cf1Smatthias.ringwald 22896a45072S[email protected] /** 22996a45072S[email protected] * get connection for given address 23096a45072S[email protected] * 23196a45072S[email protected] * @return connection OR NULL, if not found 23296a45072S[email protected] */ 2332e77e513S[email protected] hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){ 234665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 235665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 236665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 237665d90f2SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 23896a45072S[email protected] if (connection->address_type != addr_type) continue; 23996a45072S[email protected] if (memcmp(addr, connection->address, 6) != 0) continue; 24062bda3fbS[email protected] return connection; 24162bda3fbS[email protected] } 24262bda3fbS[email protected] return NULL; 24362bda3fbS[email protected] } 24462bda3fbS[email protected] 24552db98b2SMatthias Ringwald 24652db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 24752db98b2SMatthias Ringwald 248ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 249ee752bb8SMatthias Ringwald static int hci_number_sco_connections(void){ 250ee752bb8SMatthias Ringwald int connections = 0; 251ee752bb8SMatthias Ringwald btstack_linked_list_iterator_t it; 252ee752bb8SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 253ee752bb8SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 254ee752bb8SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 255ee752bb8SMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_SCO) continue; 256ee752bb8SMatthias Ringwald connections++; 257ee752bb8SMatthias Ringwald } 258ee752bb8SMatthias Ringwald return connections; 259ee752bb8SMatthias Ringwald } 260ee752bb8SMatthias Ringwald #endif 261ee752bb8SMatthias Ringwald 262ec820d77SMatthias Ringwald static void hci_connection_timeout_handler(btstack_timer_source_t *timer){ 26391a977e8SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_run_loop_get_timer_context(timer); 264aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK 265528a4a3bSMatthias Ringwald if (btstack_run_loop_embedded_get_ticks() > connection->timestamp + btstack_run_loop_embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){ 266c785ef68Smatthias.ringwald // connections might be timed out 267c785ef68Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 268c785ef68Smatthias.ringwald } 269f316a845SMatthias Ringwald #else 270528a4a3bSMatthias Ringwald if (btstack_run_loop_get_time_ms() > connection->timestamp + HCI_CONNECTION_TIMEOUT_MS){ 2715f26aadcSMatthias Ringwald // connections might be timed out 2725f26aadcSMatthias Ringwald hci_emit_l2cap_check_timeout(connection); 2735f26aadcSMatthias Ringwald } 2745f26aadcSMatthias Ringwald #endif 275c785ef68Smatthias.ringwald } 276ee091cf1Smatthias.ringwald 277ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){ 278aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK 279528a4a3bSMatthias Ringwald connection->timestamp = btstack_run_loop_embedded_get_ticks(); 280f316a845SMatthias Ringwald #else 281528a4a3bSMatthias Ringwald connection->timestamp = btstack_run_loop_get_time_ms(); 2825f26aadcSMatthias Ringwald #endif 283ee091cf1Smatthias.ringwald } 284ee091cf1Smatthias.ringwald 28528ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 28628ca2b46S[email protected] conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags); 28728ca2b46S[email protected] } 28828ca2b46S[email protected] 28935454696SMatthias Ringwald 29028ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 29128ca2b46S[email protected] conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags); 29228ca2b46S[email protected] } 29328ca2b46S[email protected] 29443bfb1bdSmatthias.ringwald /** 29580ca58a0Smatthias.ringwald * add authentication flags and reset timer 29696a45072S[email protected] * @note: assumes classic connection 2972e77e513S[email protected] * @note: bd_addr is passed in as litle endian uint8_t * as it is called from parsing packets 2987fde4af9Smatthias.ringwald */ 2997fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){ 3007fde4af9Smatthias.ringwald bd_addr_t addr; 301724d70a2SMatthias Ringwald reverse_bd_addr(bd_addr, addr); 3022e77e513S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 3037fde4af9Smatthias.ringwald if (conn) { 30428ca2b46S[email protected] connectionSetAuthenticationFlags(conn, flags); 30580ca58a0Smatthias.ringwald hci_connection_timestamp(conn); 3067fde4af9Smatthias.ringwald } 3077fde4af9Smatthias.ringwald } 3087fde4af9Smatthias.ringwald 30980ca58a0Smatthias.ringwald int hci_authentication_active_for_handle(hci_con_handle_t handle){ 3105061f3afS[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 31180ca58a0Smatthias.ringwald if (!conn) return 0; 3126724cd9eS[email protected] if (conn->authentication_flags & LEGACY_PAIRING_ACTIVE) return 1; 3136724cd9eS[email protected] if (conn->authentication_flags & SSP_PAIRING_ACTIVE) return 1; 3146724cd9eS[email protected] return 0; 31580ca58a0Smatthias.ringwald } 31680ca58a0Smatthias.ringwald 31715a95bd5SMatthias Ringwald void gap_drop_link_key_for_bd_addr(bd_addr_t addr){ 31855597469SMatthias Ringwald if (!hci_stack->link_key_db) return; 3192bacf595SMatthias Ringwald log_info("gap_drop_link_key_for_bd_addr: %s", bd_addr_to_str(addr)); 320a98592bcSMatthias Ringwald hci_stack->link_key_db->delete_link_key(addr); 321c12e46e7Smatthias.ringwald } 32255597469SMatthias Ringwald 32355597469SMatthias Ringwald void gap_store_link_key_for_bd_addr(bd_addr_t addr, link_key_t link_key, link_key_type_t type){ 32455597469SMatthias Ringwald if (!hci_stack->link_key_db) return; 3252bacf595SMatthias Ringwald log_info("gap_store_link_key_for_bd_addr: %s, type %u", bd_addr_to_str(addr), type); 32655597469SMatthias Ringwald hci_stack->link_key_db->put_link_key(addr, link_key, type); 327c12e46e7Smatthias.ringwald } 32835454696SMatthias Ringwald #endif 329c12e46e7Smatthias.ringwald 33095d71764SMatthias Ringwald static int hci_is_le_connection(hci_connection_t * connection){ 3310bf6344aS[email protected] return connection->address_type == BD_ADDR_TYPE_LE_PUBLIC || 3320bf6344aS[email protected] connection->address_type == BD_ADDR_TYPE_LE_RANDOM; 3330bf6344aS[email protected] } 3340bf6344aS[email protected] 3357fde4af9Smatthias.ringwald /** 33643bfb1bdSmatthias.ringwald * count connections 33743bfb1bdSmatthias.ringwald */ 33840d1c7a4Smatthias.ringwald static int nr_hci_connections(void){ 33956c253c9Smatthias.ringwald int count = 0; 340665d90f2SMatthias Ringwald btstack_linked_item_t *it; 341665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next, count++); 34243bfb1bdSmatthias.ringwald return count; 34343bfb1bdSmatthias.ringwald } 344c8e4258aSmatthias.ringwald 34595d71764SMatthias Ringwald static int hci_number_free_acl_slots_for_connection_type(bd_addr_type_t address_type){ 346ee303eddS[email protected] 347f04a0c31SMatthias Ringwald unsigned int num_packets_sent_classic = 0; 348f04a0c31SMatthias Ringwald unsigned int num_packets_sent_le = 0; 349ee303eddS[email protected] 350665d90f2SMatthias Ringwald btstack_linked_item_t *it; 351665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 352998906cdSmatthias.ringwald hci_connection_t * connection = (hci_connection_t *) it; 353ee303eddS[email protected] if (connection->address_type == BD_ADDR_TYPE_CLASSIC){ 354ee303eddS[email protected] num_packets_sent_classic += connection->num_acl_packets_sent; 355ee303eddS[email protected] } else { 356ee303eddS[email protected] num_packets_sent_le += connection->num_acl_packets_sent; 357ee303eddS[email protected] } 358ee303eddS[email protected] } 359d999b54eSMatthias Ringwald log_debug("ACL classic buffers: %u used of %u", num_packets_sent_classic, hci_stack->acl_packets_total_num); 360ee303eddS[email protected] int free_slots_classic = hci_stack->acl_packets_total_num - num_packets_sent_classic; 361ee303eddS[email protected] int free_slots_le = 0; 362ee303eddS[email protected] 363ee303eddS[email protected] if (free_slots_classic < 0){ 3649da54300S[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); 365998906cdSmatthias.ringwald return 0; 366998906cdSmatthias.ringwald } 367ee303eddS[email protected] 368ee303eddS[email protected] if (hci_stack->le_acl_packets_total_num){ 369ee303eddS[email protected] // if we have LE slots, they are used 370ee303eddS[email protected] free_slots_le = hci_stack->le_acl_packets_total_num - num_packets_sent_le; 371ee303eddS[email protected] if (free_slots_le < 0){ 3729da54300S[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); 373ee303eddS[email protected] return 0; 374998906cdSmatthias.ringwald } 375ee303eddS[email protected] } else { 376ee303eddS[email protected] // otherwise, classic slots are used for LE, too 377ee303eddS[email protected] free_slots_classic -= num_packets_sent_le; 378ee303eddS[email protected] if (free_slots_classic < 0){ 3799da54300S[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); 380ee303eddS[email protected] return 0; 381ee303eddS[email protected] } 382ee303eddS[email protected] } 383ee303eddS[email protected] 384ee303eddS[email protected] switch (address_type){ 385ee303eddS[email protected] case BD_ADDR_TYPE_UNKNOWN: 3862125de09SMatthias Ringwald log_error("hci_number_free_acl_slots: unknown address type"); 387ee303eddS[email protected] return 0; 388ee303eddS[email protected] 389ee303eddS[email protected] case BD_ADDR_TYPE_CLASSIC: 390ee303eddS[email protected] return free_slots_classic; 391ee303eddS[email protected] 392ee303eddS[email protected] default: 393cb00d3aaS[email protected] if (hci_stack->le_acl_packets_total_num){ 394ee303eddS[email protected] return free_slots_le; 395ee303eddS[email protected] } 396cb00d3aaS[email protected] return free_slots_classic; 397cb00d3aaS[email protected] } 398998906cdSmatthias.ringwald } 399998906cdSmatthias.ringwald 4002125de09SMatthias Ringwald int hci_number_free_acl_slots_for_handle(hci_con_handle_t con_handle){ 4012125de09SMatthias Ringwald // get connection type 4022125de09SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 4032125de09SMatthias Ringwald if (!connection){ 4042125de09SMatthias Ringwald log_error("hci_number_free_acl_slots: handle 0x%04x not in connection list", con_handle); 4052125de09SMatthias Ringwald return 0; 4062125de09SMatthias Ringwald } 4072125de09SMatthias Ringwald return hci_number_free_acl_slots_for_connection_type(connection->address_type); 4082125de09SMatthias Ringwald } 4092125de09SMatthias Ringwald 41035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 411701e3307SMatthias Ringwald static int hci_number_free_sco_slots(void){ 412f04a0c31SMatthias Ringwald unsigned int num_sco_packets_sent = 0; 413665d90f2SMatthias Ringwald btstack_linked_item_t *it; 414665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 415e35edcc1S[email protected] hci_connection_t * connection = (hci_connection_t *) it; 416e35edcc1S[email protected] num_sco_packets_sent += connection->num_sco_packets_sent; 417e35edcc1S[email protected] } 418e35edcc1S[email protected] if (num_sco_packets_sent > hci_stack->sco_packets_total_num){ 419701e3307SMatthias Ringwald log_info("hci_number_free_sco_slots:packets (%u) > total packets (%u)", num_sco_packets_sent, hci_stack->sco_packets_total_num); 42044d0e3d5S[email protected] return 0; 42144d0e3d5S[email protected] } 422701e3307SMatthias Ringwald // log_info("hci_number_free_sco_slots u", handle, num_sco_packets_sent); 423e35edcc1S[email protected] return hci_stack->sco_packets_total_num - num_sco_packets_sent; 424e35edcc1S[email protected] } 42535454696SMatthias Ringwald #endif 42644d0e3d5S[email protected] 4272b838201SMatthias Ringwald // only used to send HCI Host Number Completed Packets 4282b838201SMatthias Ringwald static int hci_can_send_comand_packet_transport(void){ 429ac928cc2S[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 430ac928cc2S[email protected] 431ac928cc2S[email protected] // check for async hci transport implementations 432ac928cc2S[email protected] if (hci_stack->hci_transport->can_send_packet_now){ 433ac928cc2S[email protected] if (!hci_stack->hci_transport->can_send_packet_now(HCI_COMMAND_DATA_PACKET)){ 434ac928cc2S[email protected] return 0; 435ac928cc2S[email protected] } 436ac928cc2S[email protected] } 4372b838201SMatthias Ringwald return 1; 4382b838201SMatthias Ringwald } 439ac928cc2S[email protected] 4402b838201SMatthias Ringwald // new functions replacing hci_can_send_packet_now[_using_packet_buffer] 4412b838201SMatthias Ringwald int hci_can_send_command_packet_now(void){ 4422b838201SMatthias Ringwald if (hci_can_send_comand_packet_transport() == 0) return 0; 443ac928cc2S[email protected] return hci_stack->num_cmd_packets > 0; 444ac928cc2S[email protected] } 445ac928cc2S[email protected] 4469d04d3a7SMatthias Ringwald static int hci_transport_can_send_prepared_packet_now(uint8_t packet_type){ 447ac928cc2S[email protected] // check for async hci transport implementations 4489d04d3a7SMatthias Ringwald if (!hci_stack->hci_transport->can_send_packet_now) return 1; 4499d04d3a7SMatthias Ringwald return hci_stack->hci_transport->can_send_packet_now(packet_type); 450ac928cc2S[email protected] } 4519d04d3a7SMatthias Ringwald 4529d04d3a7SMatthias Ringwald static int hci_can_send_prepared_acl_packet_for_address_type(bd_addr_type_t address_type){ 4539d04d3a7SMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_ACL_DATA_PACKET)) return 0; 4549d04d3a7SMatthias Ringwald return hci_number_free_acl_slots_for_connection_type(address_type) > 0; 455ac928cc2S[email protected] } 4569d04d3a7SMatthias Ringwald 4579d04d3a7SMatthias Ringwald int hci_can_send_acl_le_packet_now(void){ 4589d04d3a7SMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return 0; 4599d04d3a7SMatthias Ringwald return hci_can_send_prepared_acl_packet_for_address_type(BD_ADDR_TYPE_LE_PUBLIC); 4609d04d3a7SMatthias Ringwald } 4619d04d3a7SMatthias Ringwald 4629d04d3a7SMatthias Ringwald int hci_can_send_prepared_acl_packet_now(hci_con_handle_t con_handle) { 4639d04d3a7SMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_ACL_DATA_PACKET)) return 0; 464e79abdd6S[email protected] return hci_number_free_acl_slots_for_handle(con_handle) > 0; 465ac928cc2S[email protected] } 466ac928cc2S[email protected] 467ac928cc2S[email protected] int hci_can_send_acl_packet_now(hci_con_handle_t con_handle){ 468ac928cc2S[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 469ac928cc2S[email protected] return hci_can_send_prepared_acl_packet_now(con_handle); 4706b4af23dS[email protected] } 4716b4af23dS[email protected] 47235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 47335454696SMatthias Ringwald int hci_can_send_acl_classic_packet_now(void){ 47435454696SMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return 0; 47535454696SMatthias Ringwald return hci_can_send_prepared_acl_packet_for_address_type(BD_ADDR_TYPE_CLASSIC); 47635454696SMatthias Ringwald } 47735454696SMatthias Ringwald 478701e3307SMatthias Ringwald int hci_can_send_prepared_sco_packet_now(void){ 479d057580eSMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_SCO_DATA_PACKET)) return 0; 480ed361f5fSMatthias Ringwald if (!hci_stack->synchronous_flow_control_enabled) return 1; 481701e3307SMatthias Ringwald return hci_number_free_sco_slots() > 0; 48244d0e3d5S[email protected] } 48344d0e3d5S[email protected] 484701e3307SMatthias Ringwald int hci_can_send_sco_packet_now(void){ 485d057580eSMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return 0; 486701e3307SMatthias Ringwald return hci_can_send_prepared_sco_packet_now(); 48744d0e3d5S[email protected] } 48844d0e3d5S[email protected] 489d057580eSMatthias Ringwald void hci_request_sco_can_send_now_event(void){ 490d057580eSMatthias Ringwald hci_stack->sco_waiting_for_can_send_now = 1; 491d057580eSMatthias Ringwald hci_notify_if_sco_can_send_now(); 492d057580eSMatthias Ringwald } 49335454696SMatthias Ringwald #endif 494d057580eSMatthias Ringwald 49595d71764SMatthias Ringwald // used for internal checks in l2cap.c 496c8b9416aS[email protected] int hci_is_packet_buffer_reserved(void){ 497c8b9416aS[email protected] return hci_stack->hci_packet_buffer_reserved; 498c8b9416aS[email protected] } 499c8b9416aS[email protected] 5006b4af23dS[email protected] // reserves outgoing packet buffer. @returns 1 if successful 5016b4af23dS[email protected] int hci_reserve_packet_buffer(void){ 5029d14b626S[email protected] if (hci_stack->hci_packet_buffer_reserved) { 5039d14b626S[email protected] log_error("hci_reserve_packet_buffer called but buffer already reserved"); 5049d14b626S[email protected] return 0; 5059d14b626S[email protected] } 5066b4af23dS[email protected] hci_stack->hci_packet_buffer_reserved = 1; 5076b4af23dS[email protected] return 1; 5086b4af23dS[email protected] } 5096b4af23dS[email protected] 51068a0fcf7S[email protected] void hci_release_packet_buffer(void){ 51168a0fcf7S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 51268a0fcf7S[email protected] } 51368a0fcf7S[email protected] 5146b4af23dS[email protected] // assumption: synchronous implementations don't provide can_send_packet_now as they don't keep the buffer after the call 5157f02f414SMatthias Ringwald static int hci_transport_synchronous(void){ 5166b4af23dS[email protected] return hci_stack->hci_transport->can_send_packet_now == NULL; 5176b4af23dS[email protected] } 5186b4af23dS[email protected] 519452cf3bbS[email protected] static int hci_send_acl_packet_fragments(hci_connection_t *connection){ 520452cf3bbS[email protected] 521452cf3bbS[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); 522452cf3bbS[email protected] 523452cf3bbS[email protected] // max ACL data packet length depends on connection type (LE vs. Classic) and available buffers 524452cf3bbS[email protected] uint16_t max_acl_data_packet_length = hci_stack->acl_data_packet_length; 525452cf3bbS[email protected] if (hci_is_le_connection(connection) && hci_stack->le_data_packets_length > 0){ 526452cf3bbS[email protected] max_acl_data_packet_length = hci_stack->le_data_packets_length; 527452cf3bbS[email protected] } 528452cf3bbS[email protected] 529452cf3bbS[email protected] // testing: reduce buffer to minimum 530452cf3bbS[email protected] // max_acl_data_packet_length = 52; 531452cf3bbS[email protected] 532d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments entered"); 533d999b54eSMatthias Ringwald 534452cf3bbS[email protected] int err; 535452cf3bbS[email protected] // multiple packets could be send on a synchronous HCI transport 536452cf3bbS[email protected] while (1){ 537452cf3bbS[email protected] 538d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop entered"); 539d999b54eSMatthias Ringwald 540452cf3bbS[email protected] // get current data 541452cf3bbS[email protected] const uint16_t acl_header_pos = hci_stack->acl_fragmentation_pos - 4; 542452cf3bbS[email protected] int current_acl_data_packet_length = hci_stack->acl_fragmentation_total_size - hci_stack->acl_fragmentation_pos; 543452cf3bbS[email protected] int more_fragments = 0; 544452cf3bbS[email protected] 545452cf3bbS[email protected] // if ACL packet is larger than Bluetooth packet buffer, only send max_acl_data_packet_length 546452cf3bbS[email protected] if (current_acl_data_packet_length > max_acl_data_packet_length){ 547452cf3bbS[email protected] more_fragments = 1; 548452cf3bbS[email protected] current_acl_data_packet_length = max_acl_data_packet_length; 549452cf3bbS[email protected] } 550452cf3bbS[email protected] 551452cf3bbS[email protected] // copy handle_and_flags if not first fragment and update packet boundary flags to be 01 (continuing fragmnent) 552452cf3bbS[email protected] if (acl_header_pos > 0){ 553f8fbdce0SMatthias Ringwald uint16_t handle_and_flags = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 554452cf3bbS[email protected] handle_and_flags = (handle_and_flags & 0xcfff) | (1 << 12); 555f8fbdce0SMatthias Ringwald little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos, handle_and_flags); 556452cf3bbS[email protected] } 557452cf3bbS[email protected] 558452cf3bbS[email protected] // update header len 559f8fbdce0SMatthias Ringwald little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos + 2, current_acl_data_packet_length); 560452cf3bbS[email protected] 561452cf3bbS[email protected] // count packet 562452cf3bbS[email protected] connection->num_acl_packets_sent++; 563f04a0c31SMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop before send (more fragments %d)", more_fragments); 564d999b54eSMatthias Ringwald 565d999b54eSMatthias Ringwald // update state for next fragment (if any) as "transport done" might be sent during send_packet already 566d999b54eSMatthias Ringwald if (more_fragments){ 567d999b54eSMatthias Ringwald // update start of next fragment to send 568d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_pos += current_acl_data_packet_length; 569d999b54eSMatthias Ringwald } else { 570d999b54eSMatthias Ringwald // done 571d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_pos = 0; 572d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_total_size = 0; 573d999b54eSMatthias Ringwald } 574452cf3bbS[email protected] 575452cf3bbS[email protected] // send packet 576452cf3bbS[email protected] uint8_t * packet = &hci_stack->hci_packet_buffer[acl_header_pos]; 577452cf3bbS[email protected] const int size = current_acl_data_packet_length + 4; 5785bb5bc3eS[email protected] hci_dump_packet(HCI_ACL_DATA_PACKET, 0, packet, size); 579452cf3bbS[email protected] err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size); 580452cf3bbS[email protected] 581f04a0c31SMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop after send (more fragments %d)", more_fragments); 582d999b54eSMatthias Ringwald 583452cf3bbS[email protected] // done yet? 584452cf3bbS[email protected] if (!more_fragments) break; 585452cf3bbS[email protected] 586452cf3bbS[email protected] // can send more? 587452cf3bbS[email protected] if (!hci_can_send_prepared_acl_packet_now(connection->con_handle)) return err; 588452cf3bbS[email protected] } 589452cf3bbS[email protected] 590d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop over"); 591452cf3bbS[email protected] 592d051460cS[email protected] // release buffer now for synchronous transport 593203bace6S[email protected] if (hci_transport_synchronous()){ 594452cf3bbS[email protected] hci_release_packet_buffer(); 59563fa3374SMatthias Ringwald // notify upper stack that it might be possible to send again 59663fa3374SMatthias Ringwald uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; 597d6b06661SMatthias Ringwald hci_emit_event(&event[0], sizeof(event), 0); // don't dump 598452cf3bbS[email protected] } 599452cf3bbS[email protected] 600452cf3bbS[email protected] return err; 601452cf3bbS[email protected] } 602452cf3bbS[email protected] 603826f7347S[email protected] // pre: caller has reserved the packet buffer 604826f7347S[email protected] int hci_send_acl_packet_buffer(int size){ 6057856c818Smatthias.ringwald 606452cf3bbS[email protected] // log_info("hci_send_acl_packet_buffer size %u", size); 607452cf3bbS[email protected] 608826f7347S[email protected] if (!hci_stack->hci_packet_buffer_reserved) { 609826f7347S[email protected] log_error("hci_send_acl_packet_buffer called without reserving packet buffer"); 610826f7347S[email protected] return 0; 611826f7347S[email protected] } 612826f7347S[email protected] 613d713a683S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 614d713a683S[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 615d713a683S[email protected] 616826f7347S[email protected] // check for free places on Bluetooth module 617d713a683S[email protected] if (!hci_can_send_prepared_acl_packet_now(con_handle)) { 618826f7347S[email protected] log_error("hci_send_acl_packet_buffer called but no free ACL buffers on controller"); 61997b61c7bS[email protected] hci_release_packet_buffer(); 62097b61c7bS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 62197b61c7bS[email protected] } 6226218e6f1Smatthias.ringwald 6235061f3afS[email protected] hci_connection_t *connection = hci_connection_for_handle( con_handle); 62497b61c7bS[email protected] if (!connection) { 6255fa0b7cfS[email protected] log_error("hci_send_acl_packet_buffer called but no connection for handle 0x%04x", con_handle); 62697b61c7bS[email protected] hci_release_packet_buffer(); 62797b61c7bS[email protected] return 0; 62897b61c7bS[email protected] } 62952db98b2SMatthias Ringwald 63052db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 63156cf178bSmatthias.ringwald hci_connection_timestamp(connection); 63252db98b2SMatthias Ringwald #endif 63356cf178bSmatthias.ringwald 634452cf3bbS[email protected] // hci_dump_packet( HCI_ACL_DATA_PACKET, 0, packet, size); 6357856c818Smatthias.ringwald 636452cf3bbS[email protected] // setup data 637452cf3bbS[email protected] hci_stack->acl_fragmentation_total_size = size; 638452cf3bbS[email protected] hci_stack->acl_fragmentation_pos = 4; // start of L2CAP packet 6396218e6f1Smatthias.ringwald 640452cf3bbS[email protected] return hci_send_acl_packet_fragments(connection); 641ee091cf1Smatthias.ringwald } 642ee091cf1Smatthias.ringwald 64335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 64444d0e3d5S[email protected] // pre: caller has reserved the packet buffer 64544d0e3d5S[email protected] int hci_send_sco_packet_buffer(int size){ 64644d0e3d5S[email protected] 64744d0e3d5S[email protected] // log_info("hci_send_acl_packet_buffer size %u", size); 64844d0e3d5S[email protected] 64944d0e3d5S[email protected] if (!hci_stack->hci_packet_buffer_reserved) { 65044d0e3d5S[email protected] log_error("hci_send_acl_packet_buffer called without reserving packet buffer"); 65144d0e3d5S[email protected] return 0; 65244d0e3d5S[email protected] } 65344d0e3d5S[email protected] 65444d0e3d5S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 6554b3e1e19SMatthias Ringwald 6564b3e1e19SMatthias Ringwald // skip checks in loopback mode 6574b3e1e19SMatthias Ringwald if (!hci_stack->loopback_mode){ 65844d0e3d5S[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); // same for ACL and SCO 65944d0e3d5S[email protected] 66044d0e3d5S[email protected] // check for free places on Bluetooth module 661701e3307SMatthias Ringwald if (!hci_can_send_prepared_sco_packet_now()) { 66244d0e3d5S[email protected] log_error("hci_send_sco_packet_buffer called but no free ACL buffers on controller"); 66344d0e3d5S[email protected] hci_release_packet_buffer(); 66444d0e3d5S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 66544d0e3d5S[email protected] } 66644d0e3d5S[email protected] 667e35edcc1S[email protected] // track send packet in connection struct 668e35edcc1S[email protected] hci_connection_t *connection = hci_connection_for_handle( con_handle); 669e35edcc1S[email protected] if (!connection) { 670e35edcc1S[email protected] log_error("hci_send_sco_packet_buffer called but no connection for handle 0x%04x", con_handle); 671e35edcc1S[email protected] hci_release_packet_buffer(); 672e35edcc1S[email protected] return 0; 673e35edcc1S[email protected] } 674e35edcc1S[email protected] connection->num_sco_packets_sent++; 6754b3e1e19SMatthias Ringwald } 67644d0e3d5S[email protected] 67744d0e3d5S[email protected] hci_dump_packet( HCI_SCO_DATA_PACKET, 0, packet, size); 678543e835cSMatthias Ringwald int err = hci_stack->hci_transport->send_packet(HCI_SCO_DATA_PACKET, packet, size); 679543e835cSMatthias Ringwald 680543e835cSMatthias Ringwald if (hci_transport_synchronous()){ 681543e835cSMatthias Ringwald hci_release_packet_buffer(); 68263fa3374SMatthias Ringwald // notify upper stack that it might be possible to send again 68363fa3374SMatthias Ringwald uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; 684d6b06661SMatthias Ringwald hci_emit_event(&event[0], sizeof(event), 0); // don't dump 685543e835cSMatthias Ringwald } 686543e835cSMatthias Ringwald 687543e835cSMatthias Ringwald return err; 68844d0e3d5S[email protected] } 68935454696SMatthias Ringwald #endif 69044d0e3d5S[email protected] 69116833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){ 6927856c818Smatthias.ringwald 693e76a89eeS[email protected] // log_info("acl_handler: size %u", size); 694e76a89eeS[email protected] 6957856c818Smatthias.ringwald // get info 6967856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 6975061f3afS[email protected] hci_connection_t *conn = hci_connection_for_handle(con_handle); 6987856c818Smatthias.ringwald uint8_t acl_flags = READ_ACL_FLAGS(packet); 6997856c818Smatthias.ringwald uint16_t acl_length = READ_ACL_LENGTH(packet); 7007856c818Smatthias.ringwald 7017856c818Smatthias.ringwald // ignore non-registered handle 7027856c818Smatthias.ringwald if (!conn){ 7039da54300S[email protected] log_error( "hci.c: acl_handler called with non-registered handle %u!" , con_handle); 7047856c818Smatthias.ringwald return; 7057856c818Smatthias.ringwald } 7067856c818Smatthias.ringwald 707e76a89eeS[email protected] // assert packet is complete 7089ecc3e17S[email protected] if (acl_length + 4 != size){ 709f04a0c31SMatthias Ringwald log_error("hci.c: acl_handler called with ACL packet of wrong size %d, expected %u => dropping packet", size, acl_length + 4); 710e76a89eeS[email protected] return; 711e76a89eeS[email protected] } 712e76a89eeS[email protected] 71352db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 7147856c818Smatthias.ringwald // update idle timestamp 7157856c818Smatthias.ringwald hci_connection_timestamp(conn); 71652db98b2SMatthias Ringwald #endif 7177856c818Smatthias.ringwald 7182b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 7192b838201SMatthias Ringwald hci_stack->host_completed_packets = 1; 7202b838201SMatthias Ringwald conn->num_packets_completed++; 7212b838201SMatthias Ringwald #endif 7222b838201SMatthias Ringwald 7237856c818Smatthias.ringwald // handle different packet types 7247856c818Smatthias.ringwald switch (acl_flags & 0x03) { 7257856c818Smatthias.ringwald 7267856c818Smatthias.ringwald case 0x01: // continuation fragment 7277856c818Smatthias.ringwald 7280ca847afS[email protected] // sanity checks 7297856c818Smatthias.ringwald if (conn->acl_recombination_pos == 0) { 7309da54300S[email protected] log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x", con_handle); 7317856c818Smatthias.ringwald return; 7327856c818Smatthias.ringwald } 7330ca847afS[email protected] if (conn->acl_recombination_pos + acl_length > 4 + HCI_ACL_BUFFER_SIZE){ 7340ca847afS[email protected] log_error( "ACL Cont Fragment to large: combined packet %u > buffer size %u for handle 0x%02x", 7350ca847afS[email protected] conn->acl_recombination_pos + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle); 7360ca847afS[email protected] conn->acl_recombination_pos = 0; 7370ca847afS[email protected] return; 7380ca847afS[email protected] } 7397856c818Smatthias.ringwald 7407856c818Smatthias.ringwald // append fragment payload (header already stored) 741ec6321eeS[email protected] memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + conn->acl_recombination_pos], &packet[4], acl_length ); 7427856c818Smatthias.ringwald conn->acl_recombination_pos += acl_length; 7437856c818Smatthias.ringwald 7449da54300S[email protected] // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u", acl_length, 745decc01a8Smatthias.ringwald // conn->acl_recombination_pos, conn->acl_recombination_length); 7467856c818Smatthias.ringwald 7477856c818Smatthias.ringwald // forward complete L2CAP packet if complete. 7487856c818Smatthias.ringwald if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header 749d6b06661SMatthias Ringwald hci_emit_acl_packet(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], conn->acl_recombination_pos); 7507856c818Smatthias.ringwald // reset recombination buffer 7517856c818Smatthias.ringwald conn->acl_recombination_length = 0; 7527856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 7537856c818Smatthias.ringwald } 7547856c818Smatthias.ringwald break; 7557856c818Smatthias.ringwald 7567856c818Smatthias.ringwald case 0x02: { // first fragment 7577856c818Smatthias.ringwald 75823a77e1aS[email protected] // sanity check 75923a77e1aS[email protected] if (conn->acl_recombination_pos) { 76023a77e1aS[email protected] log_error( "ACL First Fragment but data in buffer for handle 0x%02x, dropping stale fragments", con_handle); 76123a77e1aS[email protected] conn->acl_recombination_pos = 0; 76223a77e1aS[email protected] } 76323a77e1aS[email protected] 7647856c818Smatthias.ringwald // peek into L2CAP packet! 7657856c818Smatthias.ringwald uint16_t l2cap_length = READ_L2CAP_LENGTH( packet ); 7667856c818Smatthias.ringwald 7679da54300S[email protected] // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u", acl_length, l2cap_length); 768decc01a8Smatthias.ringwald 7697856c818Smatthias.ringwald // compare fragment size to L2CAP packet size 7707856c818Smatthias.ringwald if (acl_length >= l2cap_length + 4){ 7717856c818Smatthias.ringwald // forward fragment as L2CAP packet 772d6b06661SMatthias Ringwald hci_emit_acl_packet(packet, acl_length + 4); 7737856c818Smatthias.ringwald } else { 7740ca847afS[email protected] 7750ca847afS[email protected] if (acl_length > HCI_ACL_BUFFER_SIZE){ 7760ca847afS[email protected] log_error( "ACL First Fragment to large: fragment %u > buffer size %u for handle 0x%02x", 7770ca847afS[email protected] 4 + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle); 7780ca847afS[email protected] return; 7790ca847afS[email protected] } 7800ca847afS[email protected] 7817856c818Smatthias.ringwald // store first fragment and tweak acl length for complete package 782ec6321eeS[email protected] memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], packet, acl_length + 4); 7837856c818Smatthias.ringwald conn->acl_recombination_pos = acl_length + 4; 7847856c818Smatthias.ringwald conn->acl_recombination_length = l2cap_length; 785f8fbdce0SMatthias Ringwald little_endian_store_16(conn->acl_recombination_buffer, HCI_INCOMING_PRE_BUFFER_SIZE + 2, l2cap_length +4); 7867856c818Smatthias.ringwald } 7877856c818Smatthias.ringwald break; 7887856c818Smatthias.ringwald 7897856c818Smatthias.ringwald } 7907856c818Smatthias.ringwald default: 7919da54300S[email protected] log_error( "hci.c: acl_handler called with invalid packet boundary flags %u", acl_flags & 0x03); 7927856c818Smatthias.ringwald return; 7937856c818Smatthias.ringwald } 79494ab26f8Smatthias.ringwald 79594ab26f8Smatthias.ringwald // execute main loop 79694ab26f8Smatthias.ringwald hci_run(); 79716833f0aSmatthias.ringwald } 79822909952Smatthias.ringwald 79967a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){ 8009da54300S[email protected] log_info("Connection closed: handle 0x%x, %s", conn->con_handle, bd_addr_to_str(conn->address)); 8013c4d4b90Smatthias.ringwald 802b3264428SMatthias Ringwald #ifdef ENABLE_CLASSIC 803ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 804ee752bb8SMatthias Ringwald int addr_type = conn->address_type; 805ee752bb8SMatthias Ringwald #endif 806b3264428SMatthias Ringwald #endif 807ee752bb8SMatthias Ringwald 808528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&conn->timeout); 809c785ef68Smatthias.ringwald 810665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 811a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 8123c4d4b90Smatthias.ringwald 8133c4d4b90Smatthias.ringwald // now it's gone 814c7e0c5f6Smatthias.ringwald hci_emit_nr_connections_changed(); 815ee752bb8SMatthias Ringwald 816b3264428SMatthias Ringwald #ifdef ENABLE_CLASSIC 817034e9b53SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 818ee752bb8SMatthias Ringwald // update SCO 819ee752bb8SMatthias Ringwald if (addr_type == BD_ADDR_TYPE_SCO && hci_stack->hci_transport && hci_stack->hci_transport->set_sco_config){ 820ee752bb8SMatthias Ringwald hci_stack->hci_transport->set_sco_config(hci_stack->sco_voice_setting_active, hci_number_sco_connections()); 821ee752bb8SMatthias Ringwald } 822034e9b53SMatthias Ringwald #endif 823b3264428SMatthias Ringwald #endif 824c7e0c5f6Smatthias.ringwald } 825c7e0c5f6Smatthias.ringwald 82635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 82735454696SMatthias Ringwald 8280c042179S[email protected] static const uint16_t packet_type_sizes[] = { 8298f8108aaSmatthias.ringwald 0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE, 8308f8108aaSmatthias.ringwald HCI_ACL_DH1_SIZE, 0, 0, 0, 8318f8108aaSmatthias.ringwald HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE, 8328f8108aaSmatthias.ringwald HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE 8338f8108aaSmatthias.ringwald }; 83465389bfcS[email protected] static const uint8_t packet_type_feature_requirement_bit[] = { 83565389bfcS[email protected] 0, // 3 slot packets 83665389bfcS[email protected] 1, // 5 slot packets 83765389bfcS[email protected] 25, // EDR 2 mpbs 83865389bfcS[email protected] 26, // EDR 3 mbps 83965389bfcS[email protected] 39, // 3 slot EDR packts 84065389bfcS[email protected] 40, // 5 slot EDR packet 84165389bfcS[email protected] }; 84265389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = { 84365389bfcS[email protected] 0x0f00, // 3 slot packets 84465389bfcS[email protected] 0xf000, // 5 slot packets 84565389bfcS[email protected] 0x1102, // EDR 2 mpbs 84665389bfcS[email protected] 0x2204, // EDR 3 mbps 84765389bfcS[email protected] 0x0300, // 3 slot EDR packts 84865389bfcS[email protected] 0x3000, // 5 slot EDR packet 84965389bfcS[email protected] }; 8508f8108aaSmatthias.ringwald 85165389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){ 85265389bfcS[email protected] // enable packet types based on size 8538f8108aaSmatthias.ringwald uint16_t packet_types = 0; 854f16a69bbS[email protected] unsigned int i; 8558f8108aaSmatthias.ringwald for (i=0;i<16;i++){ 8568f8108aaSmatthias.ringwald if (packet_type_sizes[i] == 0) continue; 8578f8108aaSmatthias.ringwald if (packet_type_sizes[i] <= buffer_size){ 8588f8108aaSmatthias.ringwald packet_types |= 1 << i; 8598f8108aaSmatthias.ringwald } 8608f8108aaSmatthias.ringwald } 86165389bfcS[email protected] // disable packet types due to missing local supported features 86265389bfcS[email protected] for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){ 863f04a0c31SMatthias Ringwald unsigned int bit_idx = packet_type_feature_requirement_bit[i]; 86465389bfcS[email protected] int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0; 86565389bfcS[email protected] if (feature_set) continue; 86665389bfcS[email protected] log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]); 86765389bfcS[email protected] packet_types &= ~packet_type_feature_packet_mask[i]; 86865389bfcS[email protected] } 8698f8108aaSmatthias.ringwald // flip bits for "may not be used" 8708f8108aaSmatthias.ringwald packet_types ^= 0x3306; 8718f8108aaSmatthias.ringwald return packet_types; 8728f8108aaSmatthias.ringwald } 8738f8108aaSmatthias.ringwald 8748f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){ 8753a9fb326S[email protected] return hci_stack->packet_types; 8768f8108aaSmatthias.ringwald } 87735454696SMatthias Ringwald #endif 8788f8108aaSmatthias.ringwald 879facf93fdS[email protected] uint8_t* hci_get_outgoing_packet_buffer(void){ 8807dc17943Smatthias.ringwald // hci packet buffer is >= acl data packet length 8813a9fb326S[email protected] return hci_stack->hci_packet_buffer; 8827dc17943Smatthias.ringwald } 8837dc17943Smatthias.ringwald 884f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){ 8853a9fb326S[email protected] return hci_stack->acl_data_packet_length; 8867dc17943Smatthias.ringwald } 8877dc17943Smatthias.ringwald 88806b9e820SMatthias Ringwald #ifdef ENABLE_CLASSIC 8893e68d23dSMatthias Ringwald int hci_extended_sco_link_supported(void){ 8903e68d23dSMatthias Ringwald // No. 31, byte 3, bit 7 8913e68d23dSMatthias Ringwald return (hci_stack->local_supported_features[3] & (1 << 7)) != 0; 8923e68d23dSMatthias Ringwald } 89306b9e820SMatthias Ringwald #endif 8943e68d23dSMatthias Ringwald 8956ac9a97eS[email protected] int hci_non_flushable_packet_boundary_flag_supported(void){ 8966ac9a97eS[email protected] // No. 54, byte 6, bit 6 8976ac9a97eS[email protected] return (hci_stack->local_supported_features[6] & (1 << 6)) != 0; 8986ac9a97eS[email protected] } 8996ac9a97eS[email protected] 90015a95bd5SMatthias Ringwald static int gap_ssp_supported(void){ 9016ac9a97eS[email protected] // No. 51, byte 6, bit 3 9023a9fb326S[email protected] return (hci_stack->local_supported_features[6] & (1 << 3)) != 0; 903f5d8d141S[email protected] } 904f5d8d141S[email protected] 9057f02f414SMatthias Ringwald static int hci_classic_supported(void){ 90635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 9076ac9a97eS[email protected] // No. 37, byte 4, bit 5, = No BR/EDR Support 9083a9fb326S[email protected] return (hci_stack->local_supported_features[4] & (1 << 5)) == 0; 90935454696SMatthias Ringwald #else 91035454696SMatthias Ringwald return 0; 91135454696SMatthias Ringwald #endif 912f5d8d141S[email protected] } 913f5d8d141S[email protected] 9147f02f414SMatthias Ringwald static int hci_le_supported(void){ 915a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 9166ac9a97eS[email protected] // No. 37, byte 4, bit 6 = LE Supported (Controller) 9173a9fb326S[email protected] return (hci_stack->local_supported_features[4] & (1 << 6)) != 0; 918f5d8d141S[email protected] #else 919f5d8d141S[email protected] return 0; 920f5d8d141S[email protected] #endif 921f5d8d141S[email protected] } 922f5d8d141S[email protected] 923b95a5a35SMatthias Ringwald #ifdef ENABLE_BLE 924b95a5a35SMatthias Ringwald 925b95a5a35SMatthias Ringwald /** 926b95a5a35SMatthias Ringwald * @brief Get addr type and address used for LE in Advertisements, Scan Responses, 927b95a5a35SMatthias Ringwald */ 928b95a5a35SMatthias Ringwald void gap_le_get_own_address(uint8_t * addr_type, bd_addr_t addr){ 929b95a5a35SMatthias Ringwald *addr_type = hci_stack->le_own_addr_type; 930b95a5a35SMatthias Ringwald if (hci_stack->le_own_addr_type){ 931b95a5a35SMatthias Ringwald memcpy(addr, hci_stack->le_random_address, 6); 93269a97523S[email protected] } else { 9333a9fb326S[email protected] memcpy(addr, hci_stack->local_bd_addr, 6); 93469a97523S[email protected] } 93569a97523S[email protected] } 93669a97523S[email protected] 937e8c8828eSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 93839677e66SMatthias Ringwald void le_handle_advertisement_report(uint8_t *packet, int size){ 9399ec2630cSMatthias Ringwald 9409ec2630cSMatthias Ringwald UNUSED(size); 9419ec2630cSMatthias Ringwald 942d1dc057bS[email protected] int offset = 3; 943d1dc057bS[email protected] int num_reports = packet[offset]; 944d1dc057bS[email protected] offset += 1; 945d1dc057bS[email protected] 94657c9da5bS[email protected] int i; 9474f4e0224SMatthias Ringwald // log_info("HCI: handle adv report with num reports: %d", num_reports); 94803fbe9c6S[email protected] uint8_t event[12 + LE_ADVERTISING_DATA_SIZE]; // use upper bound to avoid var size automatic var 94957c9da5bS[email protected] for (i=0; i<num_reports;i++){ 950210c6774S[email protected] uint8_t data_length = packet[offset + 8]; 951a0aac9c4S[email protected] uint8_t event_size = 10 + data_length; 952d1dc057bS[email protected] int pos = 0; 953045013feSMatthias Ringwald event[pos++] = GAP_EVENT_ADVERTISING_REPORT; 95457c9da5bS[email protected] event[pos++] = event_size; 955210c6774S[email protected] memcpy(&event[pos], &packet[offset], 1+1+6); // event type + address type + address 956d1dc057bS[email protected] offset += 8; 957d1dc057bS[email protected] pos += 8; 958d1dc057bS[email protected] event[pos++] = packet[offset + 1 + data_length]; // rssi 959d1dc057bS[email protected] event[pos++] = packet[offset++]; //data_length; 960d1dc057bS[email protected] memcpy(&event[pos], &packet[offset], data_length); 96157c9da5bS[email protected] pos += data_length; 962d1dc057bS[email protected] offset += data_length + 1; // rssi 963d6b06661SMatthias Ringwald hci_emit_event(event, pos, 1); 96457c9da5bS[email protected] } 96557c9da5bS[email protected] } 966b2f949feS[email protected] #endif 967e8c8828eSMatthias Ringwald #endif 96857c9da5bS[email protected] 969cd77dd38SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 97006b9e820SMatthias Ringwald 97196b53536SMatthias Ringwald static uint32_t hci_transport_uart_get_main_baud_rate(void){ 97296b53536SMatthias Ringwald if (!hci_stack->config) return 0; 9739796ebeaSMatthias Ringwald uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 97496b53536SMatthias Ringwald // Limit baud rate for Broadcom chipsets to 3 mbps 97561f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION && baud_rate > 3000000){ 97696b53536SMatthias Ringwald baud_rate = 3000000; 97796b53536SMatthias Ringwald } 97896b53536SMatthias Ringwald return baud_rate; 97996b53536SMatthias Ringwald } 98096b53536SMatthias Ringwald 981ec820d77SMatthias Ringwald static void hci_initialization_timeout_handler(btstack_timer_source_t * ds){ 9829ec2630cSMatthias Ringwald UNUSED(ds); 9839ec2630cSMatthias Ringwald 9840305bdeaSMatthias Ringwald switch (hci_stack->substate){ 9850305bdeaSMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 9867b0d7667SMatthias Ringwald log_info("Resend HCI Reset"); 9870305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET; 9887b0d7667SMatthias Ringwald hci_stack->num_cmd_packets = 1; 9890305bdeaSMatthias Ringwald hci_run(); 9900305bdeaSMatthias Ringwald break; 9919f007422SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET: 9929f007422SMatthias Ringwald log_info("Resend HCI Reset - CSR Warm Boot with Link Reset"); 9939f007422SMatthias Ringwald if (hci_stack->hci_transport->reset_link){ 9949f007422SMatthias Ringwald hci_stack->hci_transport->reset_link(); 9959f007422SMatthias Ringwald } 996202c8a4cSMatthias Ringwald // no break - explicit fallthrough to HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT 997e47e68c7SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT: 998e47e68c7SMatthias Ringwald log_info("Resend HCI Reset - CSR Warm Boot"); 999e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT; 1000e47e68c7SMatthias Ringwald hci_stack->num_cmd_packets = 1; 1001e47e68c7SMatthias Ringwald hci_run(); 1002688c2635SMatthias Ringwald break; 10037224be7eSMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE: 10047224be7eSMatthias Ringwald if (hci_stack->hci_transport->set_baudrate){ 100596b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1006cd724cb7SMatthias Ringwald log_info("Local baud rate change to %"PRIu32"(timeout handler)", baud_rate); 10077dd9d0ecSMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 10087224be7eSMatthias Ringwald } 1009834bce8cSMatthias Ringwald // For CSR, HCI Reset is sent on new baud rate. Don't forget to reset link for H5/BCSP 101061f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO){ 1011834bce8cSMatthias Ringwald if (hci_stack->hci_transport->reset_link){ 1012834bce8cSMatthias Ringwald log_info("Link Reset"); 1013834bce8cSMatthias Ringwald hci_stack->hci_transport->reset_link(); 1014834bce8cSMatthias Ringwald } 1015772a36d3SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT; 1016772a36d3SMatthias Ringwald hci_run(); 1017772a36d3SMatthias Ringwald } 10184696bddbSMatthias Ringwald break; 1019559961d0SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_BCM_DELAY: 1020559961d0SMatthias Ringwald // otherwise continue 1021559961d0SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 1022559961d0SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 1023559961d0SMatthias Ringwald break; 10240305bdeaSMatthias Ringwald default: 10250305bdeaSMatthias Ringwald break; 10260305bdeaSMatthias Ringwald } 10270305bdeaSMatthias Ringwald } 102806b9e820SMatthias Ringwald #endif 10290305bdeaSMatthias Ringwald 103071de195eSMatthias Ringwald static void hci_initializing_next_state(void){ 103174b323a9SMatthias Ringwald hci_stack->substate = (hci_substate_t )( ((int) hci_stack->substate) + 1); 103274b323a9SMatthias Ringwald } 103374b323a9SMatthias Ringwald 103474b323a9SMatthias Ringwald // assumption: hci_can_send_command_packet_now() == true 103571de195eSMatthias Ringwald static void hci_initializing_run(void){ 1036148ca237SMatthias Ringwald log_debug("hci_initializing_run: substate %u, can send %u", hci_stack->substate, hci_can_send_command_packet_now()); 103774b323a9SMatthias Ringwald switch (hci_stack->substate){ 103874b323a9SMatthias Ringwald case HCI_INIT_SEND_RESET: 103974b323a9SMatthias Ringwald hci_state_reset(); 1040a0cf2f3fSMatthias Ringwald 104106b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 10420305bdeaSMatthias Ringwald // prepare reset if command complete not received in 100ms 1043659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1044528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1045528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1046a0cf2f3fSMatthias Ringwald #endif 10470305bdeaSMatthias Ringwald // send command 104874b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 10490305bdeaSMatthias Ringwald hci_send_cmd(&hci_reset); 105074b323a9SMatthias Ringwald break; 105176fcb19bSMatthias Ringwald case HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION: 105276fcb19bSMatthias Ringwald hci_send_cmd(&hci_read_local_version_information); 105376fcb19bSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION; 105476fcb19bSMatthias Ringwald break; 1055e90bae01SMatthias Ringwald case HCI_INIT_SEND_READ_LOCAL_NAME: 1056e90bae01SMatthias Ringwald hci_send_cmd(&hci_read_local_name); 1057e90bae01SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_NAME; 1058e90bae01SMatthias Ringwald break; 105906b9e820SMatthias Ringwald 106006b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 1061e47e68c7SMatthias Ringwald case HCI_INIT_SEND_RESET_CSR_WARM_BOOT: 1062e47e68c7SMatthias Ringwald hci_state_reset(); 1063e47e68c7SMatthias Ringwald // prepare reset if command complete not received in 100ms 1064659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1065528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1066528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1067e47e68c7SMatthias Ringwald // send command 1068e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT; 1069e47e68c7SMatthias Ringwald hci_send_cmd(&hci_reset); 1070e47e68c7SMatthias Ringwald break; 10718d29070eSMatthias Ringwald case HCI_INIT_SEND_RESET_ST_WARM_BOOT: 10728d29070eSMatthias Ringwald hci_state_reset(); 10738d29070eSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT; 10748d29070eSMatthias Ringwald hci_send_cmd(&hci_reset); 10758d29070eSMatthias Ringwald break; 1076fab26ab3SMatthias Ringwald case HCI_INIT_SEND_BAUD_CHANGE: { 107796b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 10783fb36a29SMatthias Ringwald hci_stack->chipset->set_baudrate_command(baud_rate, hci_stack->hci_packet_buffer); 1079f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 108074b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE; 10810305bdeaSMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]); 10824696bddbSMatthias Ringwald // STLC25000D: baudrate change happens within 0.5 s after command was send, 10834696bddbSMatthias Ringwald // use timer to update baud rate after 100 ms (knowing exactly, when command was sent is non-trivial) 108461f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS){ 1085659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1086528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 10874696bddbSMatthias Ringwald } 108874b323a9SMatthias Ringwald break; 1089fab26ab3SMatthias Ringwald } 1090fab26ab3SMatthias Ringwald case HCI_INIT_SEND_BAUD_CHANGE_BCM: { 109196b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 10923fb36a29SMatthias Ringwald hci_stack->chipset->set_baudrate_command(baud_rate, hci_stack->hci_packet_buffer); 1093f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 1094eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE_BCM; 1095eb3a5314SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]); 1096eb3a5314SMatthias Ringwald break; 1097fab26ab3SMatthias Ringwald } 109874b323a9SMatthias Ringwald case HCI_INIT_CUSTOM_INIT: 109974b323a9SMatthias Ringwald // Custom initialization 11003fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->next_command){ 11013fb36a29SMatthias Ringwald int valid_cmd = (*hci_stack->chipset->next_command)(hci_stack->hci_packet_buffer); 110274b323a9SMatthias Ringwald if (valid_cmd){ 110374b323a9SMatthias Ringwald int size = 3 + hci_stack->hci_packet_buffer[2]; 1104f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 110574b323a9SMatthias Ringwald hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, hci_stack->hci_packet_buffer, size); 1106e47e68c7SMatthias Ringwald switch (valid_cmd) { 1107e47e68c7SMatthias Ringwald case 1: 1108e47e68c7SMatthias Ringwald default: 1109e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT; 1110e47e68c7SMatthias Ringwald break; 1111e47e68c7SMatthias Ringwald case 2: // CSR Warm Boot: Wait a bit, then send HCI Reset until HCI Command Complete 1112e47e68c7SMatthias Ringwald log_info("CSR Warm Boot"); 1113659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1114528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1115528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 111661f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO 1117772a36d3SMatthias Ringwald && hci_stack->config 11183fb36a29SMatthias Ringwald && hci_stack->chipset 11193fb36a29SMatthias Ringwald // && hci_stack->chipset->set_baudrate_command -- there's no such command 1120772a36d3SMatthias Ringwald && hci_stack->hci_transport->set_baudrate 11212caefae9SMatthias Ringwald && hci_transport_uart_get_main_baud_rate()){ 1122772a36d3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE; 1123772a36d3SMatthias Ringwald } else { 11249f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET; 1125772a36d3SMatthias Ringwald } 1126e47e68c7SMatthias Ringwald break; 1127e47e68c7SMatthias Ringwald } 11280305bdeaSMatthias Ringwald hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size); 112974b323a9SMatthias Ringwald break; 113074b323a9SMatthias Ringwald } 1131148ca237SMatthias Ringwald log_info("Init script done"); 113292a0d36dSMatthias Ringwald 1133559961d0SMatthias Ringwald // Init script download on Broadcom chipsets causes: 113461f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION){ 1135559961d0SMatthias Ringwald // - baud rate to reset, restore UART baud rate if needed 113692a0d36dSMatthias Ringwald int need_baud_change = hci_stack->config 11373fb36a29SMatthias Ringwald && hci_stack->chipset 11383fb36a29SMatthias Ringwald && hci_stack->chipset->set_baudrate_command 113992a0d36dSMatthias Ringwald && hci_stack->hci_transport->set_baudrate 11409796ebeaSMatthias Ringwald && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 114192a0d36dSMatthias Ringwald if (need_baud_change) { 11429796ebeaSMatthias Ringwald uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_init; 1143cd724cb7SMatthias Ringwald log_info("Local baud rate change to %"PRIu32" after init script (bcm)", baud_rate); 114492a0d36dSMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 114592a0d36dSMatthias Ringwald } 1146559961d0SMatthias Ringwald 1147559961d0SMatthias Ringwald // - RTS will raise during update, but manual RTS/CTS in WICED port on RedBear Duo cannot handle this 1148559961d0SMatthias Ringwald // -> Work around: wait a few milliseconds here. 1149559961d0SMatthias Ringwald log_info("BCM delay after init script"); 1150559961d0SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_BCM_DELAY; 1151559961d0SMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, 10); 1152559961d0SMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1153559961d0SMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1154559961d0SMatthias Ringwald break; 115592a0d36dSMatthias Ringwald } 115674b323a9SMatthias Ringwald } 115774b323a9SMatthias Ringwald // otherwise continue 1158a828a756SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 1159a828a756SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 1160a828a756SMatthias Ringwald break; 116153860077SMatthias Ringwald case HCI_INIT_SET_BD_ADDR: 116253860077SMatthias Ringwald log_info("Set Public BD ADDR to %s", bd_addr_to_str(hci_stack->custom_bd_addr)); 11633fb36a29SMatthias Ringwald hci_stack->chipset->set_bd_addr_command(hci_stack->custom_bd_addr, hci_stack->hci_packet_buffer); 1164f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 116553860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_BD_ADDR; 116653860077SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]); 116753860077SMatthias Ringwald break; 116806b9e820SMatthias Ringwald #endif 116906b9e820SMatthias Ringwald 117006b9e820SMatthias Ringwald case HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS: 117106b9e820SMatthias Ringwald log_info("Resend hci_read_local_supported_commands after CSR Warm Boot double reset"); 117206b9e820SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 117306b9e820SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 117406b9e820SMatthias Ringwald break; 117553860077SMatthias Ringwald case HCI_INIT_READ_BD_ADDR: 117653860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_BD_ADDR; 117753860077SMatthias Ringwald hci_send_cmd(&hci_read_bd_addr); 117853860077SMatthias Ringwald break; 117974b323a9SMatthias Ringwald case HCI_INIT_READ_BUFFER_SIZE: 118074b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_BUFFER_SIZE; 11810305bdeaSMatthias Ringwald hci_send_cmd(&hci_read_buffer_size); 118274b323a9SMatthias Ringwald break; 118353860077SMatthias Ringwald case HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES: 118453860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_FEATURES; 11850305bdeaSMatthias Ringwald hci_send_cmd(&hci_read_local_supported_features); 118674b323a9SMatthias Ringwald break; 11872b838201SMatthias Ringwald 11882b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 11892b838201SMatthias Ringwald case HCI_INIT_SET_CONTROLLER_TO_HOST_FLOW_CONTROL: 11902b838201SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_CONTROLLER_TO_HOST_FLOW_CONTROL; 11912b838201SMatthias Ringwald hci_send_cmd(&hci_set_controller_to_host_flow_control, 3); // ACL + SCO Flow Control 11922b838201SMatthias Ringwald break; 11932b838201SMatthias Ringwald case HCI_INIT_HOST_BUFFER_SIZE: 11942b838201SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_HOST_BUFFER_SIZE; 11952b838201SMatthias Ringwald hci_send_cmd(&hci_host_buffer_size, HCI_HOST_ACL_PACKET_LEN, HCI_HOST_SCO_PACKET_LEN, 11962b838201SMatthias Ringwald HCI_HOST_ACL_PACKET_NUM, HCI_HOST_SCO_PACKET_NUM); 11972b838201SMatthias Ringwald break; 11982b838201SMatthias Ringwald #endif 11992b838201SMatthias Ringwald 120074b323a9SMatthias Ringwald case HCI_INIT_SET_EVENT_MASK: 12010305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_EVENT_MASK; 120274b323a9SMatthias Ringwald if (hci_le_supported()){ 120374b323a9SMatthias Ringwald hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF); 120474b323a9SMatthias Ringwald } else { 120574b323a9SMatthias Ringwald // Kensington Bluetooth 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff... 120674b323a9SMatthias Ringwald hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF); 120774b323a9SMatthias Ringwald } 120874b323a9SMatthias Ringwald break; 12092b838201SMatthias Ringwald 121035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 121174b323a9SMatthias Ringwald case HCI_INIT_WRITE_SIMPLE_PAIRING_MODE: 121274b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SIMPLE_PAIRING_MODE; 12130305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable); 121474b323a9SMatthias Ringwald break; 121574b323a9SMatthias Ringwald case HCI_INIT_WRITE_PAGE_TIMEOUT: 121674b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_PAGE_TIMEOUT; 12170305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_page_timeout, 0x6000); // ca. 15 sec 121874b323a9SMatthias Ringwald break; 121974b323a9SMatthias Ringwald case HCI_INIT_WRITE_CLASS_OF_DEVICE: 122074b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_CLASS_OF_DEVICE; 12210305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device); 122274b323a9SMatthias Ringwald break; 122374b323a9SMatthias Ringwald case HCI_INIT_WRITE_LOCAL_NAME: 12240305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_LOCAL_NAME; 122574b323a9SMatthias Ringwald if (hci_stack->local_name){ 122674b323a9SMatthias Ringwald hci_send_cmd(&hci_write_local_name, hci_stack->local_name); 122774b323a9SMatthias Ringwald } else { 12287224be7eSMatthias Ringwald char local_name[8+17+1]; 12297224be7eSMatthias Ringwald // BTstack 11:22:33:44:55:66 12307224be7eSMatthias Ringwald memcpy(local_name, "BTstack ", 8); 12317224be7eSMatthias Ringwald memcpy(&local_name[8], bd_addr_to_str(hci_stack->local_bd_addr), 17); // strlen(bd_addr_to_str(...)) = 17 12327224be7eSMatthias Ringwald local_name[8+17] = '\0'; 123393bcd4d0SMatthias Ringwald log_info("---> Name %s", local_name); 123493bcd4d0SMatthias Ringwald hci_send_cmd(&hci_write_local_name, local_name); 123574b323a9SMatthias Ringwald } 123674b323a9SMatthias Ringwald break; 1237ff00ed1cSMatthias Ringwald case HCI_INIT_WRITE_EIR_DATA: 12388a114470SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_EIR_DATA; 1239ff00ed1cSMatthias Ringwald hci_send_cmd(&hci_write_extended_inquiry_response, 0, hci_stack->eir_data); 1240ff00ed1cSMatthias Ringwald break; 1241f6858d14SMatthias Ringwald case HCI_INIT_WRITE_INQUIRY_MODE: 1242f6858d14SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_INQUIRY_MODE; 12438a114470SMatthias Ringwald hci_send_cmd(&hci_write_inquiry_mode, (int) hci_stack->inquiry_mode); 1244f6858d14SMatthias Ringwald break; 124574b323a9SMatthias Ringwald case HCI_INIT_WRITE_SCAN_ENABLE: 124674b323a9SMatthias Ringwald hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan 124774b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SCAN_ENABLE; 124874b323a9SMatthias Ringwald break; 1249483c5078SMatthias Ringwald // only sent if ENABLE_SCO_OVER_HCI is defined 1250729ed62eSMatthias Ringwald case HCI_INIT_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE: 1251729ed62eSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE; 1252729ed62eSMatthias Ringwald hci_send_cmd(&hci_write_synchronous_flow_control_enable, 1); // SCO tracking enabled 1253729ed62eSMatthias Ringwald break; 1254483c5078SMatthias Ringwald case HCI_INIT_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING: 1255483c5078SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING; 1256483c5078SMatthias Ringwald hci_send_cmd(&hci_write_default_erroneous_data_reporting, 1); 1257483c5078SMatthias Ringwald break; 1258a42798c3SMatthias Ringwald // only sent if ENABLE_SCO_OVER_HCI and manufacturer is Broadcom 1259a42798c3SMatthias Ringwald case HCI_INIT_BCM_WRITE_SCO_PCM_INT: 1260a42798c3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT; 1261a42798c3SMatthias Ringwald log_info("BCM: Route SCO data via HCI transport"); 1262a42798c3SMatthias Ringwald hci_send_cmd(&hci_bcm_write_sco_pcm_int, 1, 0, 0, 0, 0); 1263a42798c3SMatthias Ringwald break; 1264a42798c3SMatthias Ringwald 126535454696SMatthias Ringwald #endif 1266a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 126774b323a9SMatthias Ringwald // LE INIT 126874b323a9SMatthias Ringwald case HCI_INIT_LE_READ_BUFFER_SIZE: 126974b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_READ_BUFFER_SIZE; 12700305bdeaSMatthias Ringwald hci_send_cmd(&hci_le_read_buffer_size); 127174b323a9SMatthias Ringwald break; 127274b323a9SMatthias Ringwald case HCI_INIT_WRITE_LE_HOST_SUPPORTED: 127374b323a9SMatthias Ringwald // LE Supported Host = 1, Simultaneous Host = 0 127474b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_LE_HOST_SUPPORTED; 12750305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_le_host_supported, 1, 0); 127674b323a9SMatthias Ringwald break; 1277b95a5a35SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 12783b6d4121SMatthias Ringwald case HCI_INIT_READ_WHITE_LIST_SIZE: 12793b6d4121SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_WHITE_LIST_SIZE; 12803b6d4121SMatthias Ringwald hci_send_cmd(&hci_le_read_white_list_size); 12813b6d4121SMatthias Ringwald break; 128274b323a9SMatthias Ringwald case HCI_INIT_LE_SET_SCAN_PARAMETERS: 1283b95a5a35SMatthias Ringwald // LE Scan Parameters: active scanning, 300 ms interval, 30 ms window, own address type, accept all advs 128474b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_SET_SCAN_PARAMETERS; 1285b95a5a35SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_parameters, 1, 0x1e0, 0x30, hci_stack->le_own_addr_type, 0); 128674b323a9SMatthias Ringwald break; 128774b323a9SMatthias Ringwald #endif 1288b95a5a35SMatthias Ringwald #endif 128974b323a9SMatthias Ringwald default: 129074b323a9SMatthias Ringwald return; 129174b323a9SMatthias Ringwald } 129255975f88SMatthias Ringwald } 129355975f88SMatthias Ringwald 1294a650ba4dSMatthias Ringwald static void hci_init_done(void){ 1295a650ba4dSMatthias Ringwald // done. tell the app 1296a650ba4dSMatthias Ringwald log_info("hci_init_done -> HCI_STATE_WORKING"); 1297a650ba4dSMatthias Ringwald hci_stack->state = HCI_STATE_WORKING; 1298a650ba4dSMatthias Ringwald hci_emit_state(); 1299a650ba4dSMatthias Ringwald hci_run(); 1300a650ba4dSMatthias Ringwald } 1301a650ba4dSMatthias Ringwald 13026155b3d3S[email protected] static void hci_initializing_event_handler(uint8_t * packet, uint16_t size){ 13039ec2630cSMatthias Ringwald UNUSED(size); 13049ec2630cSMatthias Ringwald 1305a2481739S[email protected] uint8_t command_completed = 0; 1306c4633093SMatthias Ringwald 13070e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE){ 1308f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 13096155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 1310a2481739S[email protected] command_completed = 1; 1311148ca237SMatthias Ringwald log_debug("Command complete for expected opcode %04x at substate %u", opcode, hci_stack->substate); 13126155b3d3S[email protected] } else { 1313d58dd308SMatthias Ringwald log_info("Command complete for different opcode %04x, expected %04x, at substate %u", opcode, hci_stack->last_cmd_opcode, hci_stack->substate); 13146155b3d3S[email protected] } 13156155b3d3S[email protected] } 13160f97eae7SMatthias Ringwald 13170e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_STATUS){ 13186155b3d3S[email protected] uint8_t status = packet[2]; 1319f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,4); 13206155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 13216155b3d3S[email protected] if (status){ 1322a2481739S[email protected] command_completed = 1; 1323148ca237SMatthias Ringwald log_debug("Command status error 0x%02x for expected opcode %04x at substate %u", status, opcode, hci_stack->substate); 13246155b3d3S[email protected] } else { 13256155b3d3S[email protected] log_info("Command status OK for expected opcode %04x, waiting for command complete", opcode); 13266155b3d3S[email protected] } 13276155b3d3S[email protected] } else { 1328148ca237SMatthias Ringwald log_debug("Command status for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode); 13296155b3d3S[email protected] } 13306155b3d3S[email protected] } 13310f97eae7SMatthias Ringwald 133206b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 133306b9e820SMatthias Ringwald 1334e47e68c7SMatthias Ringwald // Vendor == CSR 13350e2df43fSMatthias Ringwald if (hci_stack->substate == HCI_INIT_W4_CUSTOM_INIT && hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC){ 1336e47e68c7SMatthias Ringwald // TODO: track actual command 1337e47e68c7SMatthias Ringwald command_completed = 1; 1338e47e68c7SMatthias Ringwald } 1339a2481739S[email protected] 13404e9daa6fSMatthias Ringwald // Vendor == Toshiba 13410e2df43fSMatthias Ringwald if (hci_stack->substate == HCI_INIT_W4_SEND_BAUD_CHANGE && hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC){ 13424e9daa6fSMatthias Ringwald // TODO: track actual command 13434e9daa6fSMatthias Ringwald command_completed = 1; 13444e9daa6fSMatthias Ringwald } 13454e9daa6fSMatthias Ringwald 13460f97eae7SMatthias Ringwald // Late response (> 100 ms) for HCI Reset e.g. on Toshiba TC35661: 13470f97eae7SMatthias Ringwald // Command complete for HCI Reset arrives after we've resent the HCI Reset command 13480f97eae7SMatthias Ringwald // 13490f97eae7SMatthias Ringwald // HCI Reset 13500f97eae7SMatthias Ringwald // Timeout 100 ms 13510f97eae7SMatthias Ringwald // HCI Reset 13520f97eae7SMatthias Ringwald // Command Complete Reset 13530f97eae7SMatthias Ringwald // HCI Read Local Version Information 13540f97eae7SMatthias Ringwald // Command Complete Reset - but we expected Command Complete Read Local Version Information 13550f97eae7SMatthias Ringwald // hang... 13560f97eae7SMatthias Ringwald // 13570f97eae7SMatthias Ringwald // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend 13580f97eae7SMatthias Ringwald if (!command_completed 13590e2df43fSMatthias Ringwald && hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE 13600f97eae7SMatthias Ringwald && hci_stack->substate == HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION){ 13610f97eae7SMatthias Ringwald 1362f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 13630f97eae7SMatthias Ringwald if (opcode == hci_reset.opcode){ 13640f97eae7SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION; 13650f97eae7SMatthias Ringwald return; 13660f97eae7SMatthias Ringwald } 13670f97eae7SMatthias Ringwald } 13680f97eae7SMatthias Ringwald 13699f007422SMatthias Ringwald // CSR & H5 13709f007422SMatthias Ringwald // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend 13719f007422SMatthias Ringwald if (!command_completed 13729f007422SMatthias Ringwald && hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE 13739f007422SMatthias Ringwald && hci_stack->substate == HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS){ 13749f007422SMatthias Ringwald 13759f007422SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 13769f007422SMatthias Ringwald if (opcode == hci_reset.opcode){ 13779f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS; 13789f007422SMatthias Ringwald return; 13799f007422SMatthias Ringwald } 13809f007422SMatthias Ringwald } 13819f007422SMatthias Ringwald 13829f007422SMatthias 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 13839f007422SMatthias Ringwald // fix: Correct substate and behave as command below 13849f007422SMatthias Ringwald if (command_completed){ 13859f007422SMatthias Ringwald switch (hci_stack->substate){ 13869f007422SMatthias Ringwald case HCI_INIT_SEND_RESET: 13879f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 13889f007422SMatthias Ringwald break; 13899f007422SMatthias Ringwald case HCI_INIT_SEND_RESET_CSR_WARM_BOOT: 13909f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT; 13919f007422SMatthias Ringwald break; 13929f007422SMatthias Ringwald default: 13939f007422SMatthias Ringwald break; 13949f007422SMatthias Ringwald } 13959f007422SMatthias Ringwald } 13960f97eae7SMatthias Ringwald 139706b9e820SMatthias Ringwald #endif 13980f97eae7SMatthias Ringwald 1399a2481739S[email protected] if (!command_completed) return; 1400a2481739S[email protected] 140106b9e820SMatthias Ringwald int need_baud_change = 0; 140206b9e820SMatthias Ringwald int need_addr_change = 0; 140306b9e820SMatthias Ringwald 140406b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 140506b9e820SMatthias Ringwald need_baud_change = hci_stack->config 14063fb36a29SMatthias Ringwald && hci_stack->chipset 14073fb36a29SMatthias Ringwald && hci_stack->chipset->set_baudrate_command 1408db8bc6ffSMatthias Ringwald && hci_stack->hci_transport->set_baudrate 14099796ebeaSMatthias Ringwald && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 1410db8bc6ffSMatthias Ringwald 141106b9e820SMatthias Ringwald need_addr_change = hci_stack->custom_bd_addr_set 14123fb36a29SMatthias Ringwald && hci_stack->chipset 14133fb36a29SMatthias Ringwald && hci_stack->chipset->set_bd_addr_command; 141406b9e820SMatthias Ringwald #endif 1415a80162e9SMatthias Ringwald 14165c363727SMatthias Ringwald switch(hci_stack->substate){ 141706b9e820SMatthias Ringwald 141806b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 14199f007422SMatthias Ringwald case HCI_INIT_SEND_RESET: 1420d58dd308SMatthias Ringwald // on CSR with BCSP/H5, resend triggers resend of HCI Reset and leads to substate == HCI_INIT_SEND_RESET 14219f007422SMatthias Ringwald // fix: just correct substate and behave as command below 14229f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 14239f007422SMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 14249f007422SMatthias Ringwald break; 142574b323a9SMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 1426528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 142776fcb19bSMatthias Ringwald break; 1428e90bae01SMatthias Ringwald case HCI_INIT_W4_SEND_READ_LOCAL_NAME: 1429e90bae01SMatthias Ringwald log_info("Received local name, need baud change %d", need_baud_change); 14302f48d920SMatthias Ringwald if (need_baud_change){ 14312f48d920SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE; 14322f48d920SMatthias Ringwald return; 14332f48d920SMatthias Ringwald } 143453860077SMatthias Ringwald // skip baud change 143574b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 143674b323a9SMatthias Ringwald return; 1437a80162e9SMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE: 14384696bddbSMatthias Ringwald // for STLC2500D, baud rate change already happened. 1439fab26ab3SMatthias Ringwald // for others, baud rate gets changed now 144061f37892SMatthias Ringwald if ((hci_stack->manufacturer != BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS) && need_baud_change){ 144196b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1442cd724cb7SMatthias Ringwald log_info("Local baud rate change to %"PRIu32"(w4_send_baud_change)", baud_rate); 1443fab26ab3SMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 14444696bddbSMatthias Ringwald } 144574b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 144674b323a9SMatthias Ringwald return; 1447a80162e9SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT: 1448528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 1449a80162e9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 1450a80162e9SMatthias Ringwald return; 145174b323a9SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT: 145274b323a9SMatthias Ringwald // repeat custom init 145374b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 145474b323a9SMatthias Ringwald return; 145506b9e820SMatthias Ringwald #else 145606b9e820SMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 145706b9e820SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS; 145806b9e820SMatthias Ringwald return ; 145906b9e820SMatthias Ringwald #endif 146006b9e820SMatthias Ringwald 1461a828a756SMatthias Ringwald case HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS: 146261f37892SMatthias Ringwald if (need_baud_change && hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION){ 1463eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE_BCM; 1464eb3a5314SMatthias Ringwald return; 1465eb3a5314SMatthias Ringwald } 146653860077SMatthias Ringwald if (need_addr_change){ 146753860077SMatthias Ringwald hci_stack->substate = HCI_INIT_SET_BD_ADDR; 146853860077SMatthias Ringwald return; 146953860077SMatthias Ringwald } 147053860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 147153860077SMatthias Ringwald return; 147206b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 14737224be7eSMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE_BCM: 14747224be7eSMatthias Ringwald if (need_baud_change){ 147596b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1476cd724cb7SMatthias Ringwald log_info("Local baud rate change to %"PRIu32"(w4_send_baud_change_bcm))", baud_rate); 1477fab26ab3SMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 14787224be7eSMatthias Ringwald } 1479eb3a5314SMatthias Ringwald if (need_addr_change){ 1480eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_SET_BD_ADDR; 1481eb3a5314SMatthias Ringwald return; 1482eb3a5314SMatthias Ringwald } 1483eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 1484eb3a5314SMatthias Ringwald return; 148553860077SMatthias Ringwald case HCI_INIT_W4_SET_BD_ADDR: 148653860077SMatthias Ringwald // for STLC2500D, bd addr change only gets active after sending reset command 148761f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS){ 148853860077SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_ST_WARM_BOOT; 148953860077SMatthias Ringwald return; 149053860077SMatthias Ringwald } 149153860077SMatthias Ringwald // skipping st warm boot 149253860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 149353860077SMatthias Ringwald return; 149453860077SMatthias Ringwald case HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT: 149553860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 149653860077SMatthias Ringwald return; 149706b9e820SMatthias Ringwald #endif 149853860077SMatthias Ringwald case HCI_INIT_W4_READ_BD_ADDR: 149953860077SMatthias Ringwald // only read buffer size if supported 150053860077SMatthias Ringwald if (hci_stack->local_supported_commands[0] & 0x01) { 150153860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BUFFER_SIZE; 150253860077SMatthias Ringwald return; 150353860077SMatthias Ringwald } 150453860077SMatthias Ringwald // skipping read buffer size 150553860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES; 1506a828a756SMatthias Ringwald return; 150774b323a9SMatthias Ringwald case HCI_INIT_W4_SET_EVENT_MASK: 15086155b3d3S[email protected] // skip Classic init commands for LE only chipsets 15096155b3d3S[email protected] if (!hci_classic_supported()){ 15102c68f164SMatthias Ringwald #ifdef ENABLE_BLE 15116155b3d3S[email protected] if (hci_le_supported()){ 151274b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_LE_READ_BUFFER_SIZE; // skip all classic command 151374b323a9SMatthias Ringwald return; 15142c68f164SMatthias Ringwald } 15152c68f164SMatthias Ringwald #endif 15166155b3d3S[email protected] log_error("Neither BR/EDR nor LE supported"); 1517a650ba4dSMatthias Ringwald hci_init_done(); 151874b323a9SMatthias Ringwald return; 15196155b3d3S[email protected] } 152015a95bd5SMatthias Ringwald if (!gap_ssp_supported()){ 15215c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_WRITE_PAGE_TIMEOUT; 152274b323a9SMatthias Ringwald return; 15236155b3d3S[email protected] } 15246155b3d3S[email protected] break; 1525903ea03aSMatthias Ringwald #ifdef ENABLE_BLE 1526a828a756SMatthias Ringwald case HCI_INIT_W4_LE_READ_BUFFER_SIZE: 1527a828a756SMatthias Ringwald // skip write le host if not supported (e.g. on LE only EM9301) 1528a828a756SMatthias Ringwald if (hci_stack->local_supported_commands[0] & 0x02) break; 1529903ea03aSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 1530903ea03aSMatthias Ringwald hci_stack->substate = HCI_INIT_READ_WHITE_LIST_SIZE; 1531903ea03aSMatthias Ringwald #else 1532903ea03aSMatthias Ringwald hci_init_done(); 1533903ea03aSMatthias Ringwald #endif 1534a828a756SMatthias Ringwald return; 1535903ea03aSMatthias Ringwald #endif 1536ff00ed1cSMatthias Ringwald case HCI_INIT_W4_WRITE_LOCAL_NAME: 1537ff00ed1cSMatthias Ringwald // skip write eir data if no eir data set 1538ff00ed1cSMatthias Ringwald if (hci_stack->eir_data) break; 1539ff00ed1cSMatthias Ringwald hci_stack->substate = HCI_INIT_WRITE_INQUIRY_MODE; 1540ff00ed1cSMatthias Ringwald return; 1541729ed62eSMatthias Ringwald 154271c4de7aSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 1543729ed62eSMatthias Ringwald case HCI_INIT_W4_WRITE_SCAN_ENABLE: 15443905afbfSMatthias Ringwald // skip write synchronous flow control if not supported 15453905afbfSMatthias Ringwald if (hci_stack->local_supported_commands[0] & 0x04) break; 15463905afbfSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE; 15473905afbfSMatthias Ringwald // explicit fall through to reduce repetitions 15483905afbfSMatthias Ringwald 1549729ed62eSMatthias Ringwald case HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE: 15503905afbfSMatthias Ringwald // skip write default erroneous data reporting if not supported 15513905afbfSMatthias Ringwald if (hci_stack->local_supported_commands[0] & 0x08) break; 15523905afbfSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING; 15533905afbfSMatthias Ringwald // explicit fall through to reduce repetitions 15543905afbfSMatthias Ringwald 1555f064e0bbSMatthias Ringwald case HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING: 1556a42798c3SMatthias Ringwald // skip bcm set sco pcm config on non-Broadcom chipsets 155761f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION) break; 1558a42798c3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT; 1559a42798c3SMatthias Ringwald // explicit fall through to reduce repetitions 1560a42798c3SMatthias Ringwald 1561a42798c3SMatthias Ringwald case HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT: 1562729ed62eSMatthias Ringwald if (!hci_le_supported()){ 1563729ed62eSMatthias Ringwald // SKIP LE init for Classic only configuration 1564a650ba4dSMatthias Ringwald hci_init_done(); 1565729ed62eSMatthias Ringwald return; 1566729ed62eSMatthias Ringwald } 1567729ed62eSMatthias Ringwald break; 1568f3b012f9SMatthias Ringwald 1569f3b012f9SMatthias Ringwald #else /* !ENABLE_SCO_OVER_HCI */ 1570f3b012f9SMatthias Ringwald 157174b323a9SMatthias Ringwald case HCI_INIT_W4_WRITE_SCAN_ENABLE: 1572f3b012f9SMatthias Ringwald #ifdef ENABLE_BLE 1573f3b012f9SMatthias Ringwald if (hci_le_supported()){ 1574f3b012f9SMatthias Ringwald hci_stack->substate = HCI_INIT_LE_READ_BUFFER_SIZE; 157574b323a9SMatthias Ringwald return; 15766155b3d3S[email protected] } 1577729ed62eSMatthias Ringwald #endif 1578f3b012f9SMatthias Ringwald // SKIP LE init for Classic only configuration 1579f3b012f9SMatthias Ringwald hci_init_done(); 1580f3b012f9SMatthias Ringwald return; 1581f3b012f9SMatthias Ringwald #endif /* ENABLE_SCO_OVER_HCI */ 1582f3b012f9SMatthias Ringwald 1583a650ba4dSMatthias Ringwald // Response to command before init done state -> init done 1584a650ba4dSMatthias Ringwald case (HCI_INIT_DONE-1): 1585a650ba4dSMatthias Ringwald hci_init_done(); 1586a650ba4dSMatthias Ringwald return; 1587a650ba4dSMatthias Ringwald 15886155b3d3S[email protected] default: 158974b323a9SMatthias Ringwald break; 15906155b3d3S[email protected] } 159155975f88SMatthias Ringwald hci_initializing_next_state(); 15926155b3d3S[email protected] } 15936155b3d3S[email protected] 159416833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){ 1595e76a89eeS[email protected] 1596e76a89eeS[email protected] uint16_t event_length = packet[1]; 1597e76a89eeS[email protected] 1598e76a89eeS[email protected] // assert packet is complete 1599e76a89eeS[email protected] if (size != event_length + 2){ 1600f04a0c31SMatthias Ringwald log_error("hci.c: event_handler called with event packet of wrong size %d, expected %u => dropping packet", size, event_length + 2); 1601e76a89eeS[email protected] return; 1602e76a89eeS[email protected] } 1603e76a89eeS[email protected] 16041281a47eSmatthias.ringwald bd_addr_t addr; 160596a45072S[email protected] bd_addr_type_t addr_type; 16062d00edd4Smatthias.ringwald uint8_t link_type; 1607fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 16081f7b95a1Smatthias.ringwald hci_connection_t * conn; 160956cf178bSmatthias.ringwald int i; 161022909952Smatthias.ringwald 161135454696SMatthias Ringwald // warnings 161235454696SMatthias Ringwald (void) link_type; 161335454696SMatthias Ringwald 16140e2df43fSMatthias Ringwald // log_info("HCI:EVENT:%02x", hci_event_packet_get_type(packet)); 16155909f7f2Smatthias.ringwald 16160e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 161722909952Smatthias.ringwald 16186772a24cSmatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 16196bef4003SMatthias Ringwald // get num cmd packets - limit to 1 to reduce complexity 16206bef4003SMatthias Ringwald hci_stack->num_cmd_packets = packet[2] ? 1 : 0; 16217ec5eeaaSmatthias.ringwald 16229e263bd7SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){ 162306b9e820SMatthias Ringwald if (packet[5]) break; 16249e263bd7SMatthias Ringwald // terminate, name 248 chars 16259e263bd7SMatthias Ringwald packet[6+248] = 0; 16269e263bd7SMatthias Ringwald log_info("local name: %s", &packet[6]); 16279e263bd7SMatthias Ringwald } 1628073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_buffer_size)){ 16291d279b20Smatthias.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" 1630429122ccSMatthias Ringwald if (hci_stack->state == HCI_STATE_INITIALIZING){ 1631429122ccSMatthias Ringwald uint16_t acl_len = little_endian_read_16(packet, 6); 1632429122ccSMatthias Ringwald uint16_t sco_len = packet[8]; 1633429122ccSMatthias Ringwald 1634429122ccSMatthias Ringwald // determine usable ACL/SCO payload size 1635429122ccSMatthias Ringwald hci_stack->acl_data_packet_length = btstack_min(acl_len, HCI_ACL_PAYLOAD_SIZE); 1636429122ccSMatthias Ringwald hci_stack->sco_data_packet_length = btstack_min(sco_len, HCI_ACL_PAYLOAD_SIZE); 1637429122ccSMatthias Ringwald 1638f8fbdce0SMatthias Ringwald hci_stack->acl_packets_total_num = little_endian_read_16(packet, 9); 1639f8fbdce0SMatthias Ringwald hci_stack->sco_packets_total_num = little_endian_read_16(packet, 11); 1640a8b12447S[email protected] 1641429122ccSMatthias Ringwald log_info("hci_read_buffer_size: ACL size module %u -> used %u, count %u / SCO size %u, count %u", 1642429122ccSMatthias Ringwald acl_len, hci_stack->acl_data_packet_length, hci_stack->acl_packets_total_num, 16431a06f663S[email protected] hci_stack->sco_data_packet_length, hci_stack->sco_packets_total_num); 1644e2edc0c3Smatthias.ringwald } 164556cf178bSmatthias.ringwald } 1646a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 1647073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_le_read_buffer_size)){ 1648f8fbdce0SMatthias Ringwald hci_stack->le_data_packets_length = little_endian_read_16(packet, 6); 1649ee303eddS[email protected] hci_stack->le_acl_packets_total_num = packet[8]; 16506c26b087S[email protected] // determine usable ACL payload size 16516c26b087S[email protected] if (HCI_ACL_PAYLOAD_SIZE < hci_stack->le_data_packets_length){ 16526c26b087S[email protected] hci_stack->le_data_packets_length = HCI_ACL_PAYLOAD_SIZE; 16536c26b087S[email protected] } 16549da54300S[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); 165565a46ef3S[email protected] } 1656d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 1657073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_le_read_white_list_size)){ 1658e691bb38SMatthias Ringwald hci_stack->le_whitelist_capacity = packet[6]; 165915d0a15bSMatthias Ringwald log_info("hci_le_read_white_list_size: size %u", hci_stack->le_whitelist_capacity); 16603b6d4121SMatthias Ringwald } 166165a46ef3S[email protected] #endif 1662d70217a2SMatthias Ringwald #endif 1663073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_bd_addr)) { 1664724d70a2SMatthias Ringwald reverse_bd_addr(&packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1], 1665724d70a2SMatthias Ringwald hci_stack->local_bd_addr); 16669da54300S[email protected] log_info("Local Address, Status: 0x%02x: Addr: %s", 16673a9fb326S[email protected] packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr)); 166833373e40SMatthias Ringwald #ifdef ENABLE_CLASSIC 16691624665aSMatthias Ringwald if (hci_stack->link_key_db){ 16701624665aSMatthias Ringwald hci_stack->link_key_db->set_local_bd_addr(hci_stack->local_bd_addr); 16711624665aSMatthias Ringwald } 167233373e40SMatthias Ringwald #endif 1673188981d2Smatthias.ringwald } 167435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1675073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_scan_enable)){ 16763a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 1677381fbed8Smatthias.ringwald } 167835454696SMatthias Ringwald #endif 167935454696SMatthias Ringwald 168065389bfcS[email protected] // Note: HCI init checks 1681073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_supported_features)){ 16823a9fb326S[email protected] memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8); 168365389bfcS[email protected] 168435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1685a5a23fc2S[email protected] // determine usable ACL packet types based on host buffer size and supported features 1686a5a23fc2S[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]); 16878b96126aSMatthias Ringwald log_info("Packet types %04x, eSCO %u", hci_stack->packet_types, hci_extended_sco_link_supported()); 168835454696SMatthias Ringwald #endif 1689f5d8d141S[email protected] // Classic/LE 1690f5d8d141S[email protected] log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported()); 1691559e517eS[email protected] } 1692073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_version_information)){ 1693f8fbdce0SMatthias Ringwald // hci_stack->hci_version = little_endian_read_16(packet, 4); 1694f8fbdce0SMatthias Ringwald // hci_stack->hci_revision = little_endian_read_16(packet, 6); 1695f8fbdce0SMatthias Ringwald // hci_stack->lmp_version = little_endian_read_16(packet, 8); 1696f8fbdce0SMatthias Ringwald hci_stack->manufacturer = little_endian_read_16(packet, 10); 1697f8fbdce0SMatthias Ringwald // hci_stack->lmp_subversion = little_endian_read_16(packet, 12); 16984696bddbSMatthias Ringwald log_info("Manufacturer: 0x%04x", hci_stack->manufacturer); 16994696bddbSMatthias Ringwald } 1700073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_supported_commands)){ 1701a828a756SMatthias Ringwald hci_stack->local_supported_commands[0] = 17023905afbfSMatthias Ringwald (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+14] & 0x80) >> 7 | // bit 0 = Octet 14, bit 7 17033905afbfSMatthias Ringwald (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+24] & 0x40) >> 5 | // bit 1 = Octet 24, bit 6 17043905afbfSMatthias Ringwald (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+10] & 0x10) >> 2 | // bit 2 = Octet 10, bit 4 17053905afbfSMatthias Ringwald (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+18] & 0x08); // bit 3 = Octet 18, bit 3 17063905afbfSMatthias Ringwald log_info("Local supported commands summary 0x%02x", hci_stack->local_supported_commands[0]); 1707a828a756SMatthias Ringwald } 1708e8c8828eSMatthias Ringwald #ifdef ENABLE_CLASSIC 1709073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_synchronous_flow_control_enable)){ 17105b9b590fSMatthias Ringwald if (packet[5] == 0){ 17115b9b590fSMatthias Ringwald hci_stack->synchronous_flow_control_enabled = 1; 17125b9b590fSMatthias Ringwald } 17135b9b590fSMatthias Ringwald } 1714e8c8828eSMatthias Ringwald #endif 171556cf178bSmatthias.ringwald break; 171656cf178bSmatthias.ringwald 17177ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 17186bef4003SMatthias Ringwald // get num cmd packets - limit to 1 to reduce complexity 17196bef4003SMatthias Ringwald hci_stack->num_cmd_packets = packet[3] ? 1 : 0; 17207ec5eeaaSmatthias.ringwald break; 17217ec5eeaaSmatthias.ringwald 17222e440c8aS[email protected] case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:{ 17232e440c8aS[email protected] int offset = 3; 172456cf178bSmatthias.ringwald for (i=0; i<packet[2];i++){ 1725f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, offset); 17262e440c8aS[email protected] offset += 2; 1727f8fbdce0SMatthias Ringwald uint16_t num_packets = little_endian_read_16(packet, offset); 17282e440c8aS[email protected] offset += 2; 17292e440c8aS[email protected] 17305061f3afS[email protected] conn = hci_connection_for_handle(handle); 173156cf178bSmatthias.ringwald if (!conn){ 17329da54300S[email protected] log_error("hci_number_completed_packet lists unused con handle %u", handle); 173356cf178bSmatthias.ringwald continue; 173456cf178bSmatthias.ringwald } 173523bed257S[email protected] 1736e35edcc1S[email protected] if (conn->address_type == BD_ADDR_TYPE_SCO){ 173735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1738e35edcc1S[email protected] if (conn->num_sco_packets_sent >= num_packets){ 1739e35edcc1S[email protected] conn->num_sco_packets_sent -= num_packets; 1740e35edcc1S[email protected] } else { 1741e35edcc1S[email protected] log_error("hci_number_completed_packets, more sco slots freed then sent."); 1742e35edcc1S[email protected] conn->num_sco_packets_sent = 0; 1743e35edcc1S[email protected] } 1744701e3307SMatthias Ringwald hci_notify_if_sco_can_send_now(); 174535454696SMatthias Ringwald #endif 1746e35edcc1S[email protected] } else { 174723bed257S[email protected] if (conn->num_acl_packets_sent >= num_packets){ 174856cf178bSmatthias.ringwald conn->num_acl_packets_sent -= num_packets; 174923bed257S[email protected] } else { 1750e35edcc1S[email protected] log_error("hci_number_completed_packets, more acl slots freed then sent."); 175123bed257S[email protected] conn->num_acl_packets_sent = 0; 175223bed257S[email protected] } 1753e35edcc1S[email protected] } 17549da54300S[email protected] // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u", num_packets, handle, conn->num_acl_packets_sent); 175556cf178bSmatthias.ringwald } 17566772a24cSmatthias.ringwald break; 17572e440c8aS[email protected] } 175835454696SMatthias Ringwald 175935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 17601f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 1761724d70a2SMatthias Ringwald reverse_bd_addr(&packet[2], addr); 176237eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 17632d00edd4Smatthias.ringwald link_type = packet[11]; 17649da54300S[email protected] log_info("Connection_incoming: %s, type %u", bd_addr_to_str(addr), link_type); 1765e35edcc1S[email protected] addr_type = link_type == 1 ? BD_ADDR_TYPE_CLASSIC : BD_ADDR_TYPE_SCO; 17662e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 17671f7b95a1Smatthias.ringwald if (!conn) { 17685293c072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 17691f7b95a1Smatthias.ringwald } 1770ce4c8fabSmatthias.ringwald if (!conn) { 1771ce4c8fabSmatthias.ringwald // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 17723a9fb326S[email protected] hci_stack->decline_reason = 0x0d; 1773058e3d6bSMatthias Ringwald bd_addr_copy(hci_stack->decline_addr, addr); 1774ce4c8fabSmatthias.ringwald break; 1775ce4c8fabSmatthias.ringwald } 17765cf766e8SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 177732ab9390Smatthias.ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 1778f3a16b9aSMatthias Ringwald // store info about eSCO 1779f3a16b9aSMatthias Ringwald if (link_type == 0x02){ 1780f3a16b9aSMatthias Ringwald conn->remote_supported_feature_eSCO = 1; 1781f3a16b9aSMatthias Ringwald } 178232ab9390Smatthias.ringwald hci_run(); 17831f7b95a1Smatthias.ringwald break; 17841f7b95a1Smatthias.ringwald 17856772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 1786fe1ed1b8Smatthias.ringwald // Connection management 1787724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], addr); 17889da54300S[email protected] log_info("Connection_complete (status=%u) %s", packet[2], bd_addr_to_str(addr)); 178996a45072S[email protected] addr_type = BD_ADDR_TYPE_CLASSIC; 17902e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 1791fe1ed1b8Smatthias.ringwald if (conn) { 1792b448a0e7Smatthias.ringwald if (!packet[2]){ 1793c8e4258aSmatthias.ringwald conn->state = OPEN; 1794f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 3); 1795ad83dc6aS[email protected] conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES; 1796ee091cf1Smatthias.ringwald 1797c785ef68Smatthias.ringwald // restart timer 1798528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 1799528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&conn->timeout); 1800c785ef68Smatthias.ringwald 18019da54300S[email protected] log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 180243bfb1bdSmatthias.ringwald 180343bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 1804b448a0e7Smatthias.ringwald } else { 18051bd5283dS[email protected] int notify_dedicated_bonding_failed = conn->bonding_flags & BONDING_DEDICATED; 18061bd5283dS[email protected] uint8_t status = packet[2]; 18071bd5283dS[email protected] bd_addr_t bd_address; 18081bd5283dS[email protected] memcpy(&bd_address, conn->address, 6); 1809ad83dc6aS[email protected] 1810b448a0e7Smatthias.ringwald // connection failed, remove entry 1811665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 1812a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 1813c12e46e7Smatthias.ringwald 18141bd5283dS[email protected] // notify client if dedicated bonding 18151bd5283dS[email protected] if (notify_dedicated_bonding_failed){ 18161bd5283dS[email protected] log_info("hci notify_dedicated_bonding_failed"); 18171bd5283dS[email protected] hci_emit_dedicated_bonding_result(bd_address, status); 18181bd5283dS[email protected] } 18191bd5283dS[email protected] 1820c12e46e7Smatthias.ringwald // if authentication error, also delete link key 1821c12e46e7Smatthias.ringwald if (packet[2] == 0x05) { 182215a95bd5SMatthias Ringwald gap_drop_link_key_for_bd_addr(addr); 1823c12e46e7Smatthias.ringwald } 1824fe1ed1b8Smatthias.ringwald } 1825fe1ed1b8Smatthias.ringwald } 18266772a24cSmatthias.ringwald break; 1827fe1ed1b8Smatthias.ringwald 182844d0e3d5S[email protected] case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE: 1829724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], addr); 183044d0e3d5S[email protected] log_info("Synchronous Connection Complete (status=%u) %s", packet[2], bd_addr_to_str(addr)); 18311a06f663S[email protected] if (packet[2]){ 183244d0e3d5S[email protected] // connection failed 183344d0e3d5S[email protected] break; 183444d0e3d5S[email protected] } 18352e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 1836e35edcc1S[email protected] if (!conn) { 1837e35edcc1S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 1838e35edcc1S[email protected] } 1839e35edcc1S[email protected] if (!conn) { 1840e35edcc1S[email protected] break; 1841e35edcc1S[email protected] } 18421a06f663S[email protected] conn->state = OPEN; 1843f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 3); 1844ee752bb8SMatthias Ringwald 1845ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 1846ee752bb8SMatthias Ringwald // update SCO 1847ee752bb8SMatthias Ringwald if (conn->address_type == BD_ADDR_TYPE_SCO && hci_stack->hci_transport && hci_stack->hci_transport->set_sco_config){ 1848ee752bb8SMatthias Ringwald hci_stack->hci_transport->set_sco_config(hci_stack->sco_voice_setting_active, hci_number_sco_connections()); 1849ee752bb8SMatthias Ringwald } 1850ee752bb8SMatthias Ringwald #endif 185144d0e3d5S[email protected] break; 185244d0e3d5S[email protected] 1853afd4e962S[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 1854f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1855afd4e962S[email protected] conn = hci_connection_for_handle(handle); 1856afd4e962S[email protected] if (!conn) break; 1857afd4e962S[email protected] if (!packet[2]){ 1858afd4e962S[email protected] uint8_t * features = &packet[5]; 1859afd4e962S[email protected] if (features[6] & (1 << 3)){ 1860afd4e962S[email protected] conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP; 1861afd4e962S[email protected] } 186298a2fd1cSMatthias Ringwald if (features[3] & (1<<7)){ 186398a2fd1cSMatthias Ringwald conn->remote_supported_feature_eSCO = 1; 186498a2fd1cSMatthias Ringwald } 1865afd4e962S[email protected] } 1866afd4e962S[email protected] conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES; 186798a2fd1cSMatthias Ringwald log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x, eSCO %u", conn->bonding_flags, conn->remote_supported_feature_eSCO); 1868ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 1869ad83dc6aS[email protected] conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 1870ad83dc6aS[email protected] } 1871afd4e962S[email protected] break; 1872afd4e962S[email protected] 18737fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 18749da54300S[email protected] log_info("HCI_EVENT_LINK_KEY_REQUEST"); 18757fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST); 187674d716b5S[email protected] // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST 1877a98592bcSMatthias Ringwald if (hci_stack->bondable && !hci_stack->link_key_db) break; 187832ab9390Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST); 187964472d52Smatthias.ringwald hci_run(); 1880d9a327f9Smatthias.ringwald // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set 188129d53098Smatthias.ringwald return; 18827fde4af9Smatthias.ringwald 18839ab95c90S[email protected] case HCI_EVENT_LINK_KEY_NOTIFICATION: { 1884724d70a2SMatthias Ringwald reverse_bd_addr(&packet[2], addr); 18852e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 18869ab95c90S[email protected] if (!conn) break; 18879ab95c90S[email protected] conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION; 18887bdc6798S[email protected] link_key_type_t link_key_type = (link_key_type_t)packet[24]; 18899ab95c90S[email protected] // Change Connection Encryption keeps link key type 18909ab95c90S[email protected] if (link_key_type != CHANGED_COMBINATION_KEY){ 18919ab95c90S[email protected] conn->link_key_type = link_key_type; 18929ab95c90S[email protected] } 189355597469SMatthias Ringwald gap_store_link_key_for_bd_addr(addr, &packet[8], conn->link_key_type); 189429d53098Smatthias.ringwald // still forward event to allow dismiss of pairing dialog 18957fde4af9Smatthias.ringwald break; 18969ab95c90S[email protected] } 18977fde4af9Smatthias.ringwald 18987fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 18996724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE); 19004c57c146S[email protected] // non-bondable mode: pin code negative reply will be sent 19013a9fb326S[email protected] if (!hci_stack->bondable){ 1902899283eaS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST); 1903f8fb5f6eS[email protected] hci_run(); 1904f8fb5f6eS[email protected] return; 19054c57c146S[email protected] } 1906d9a327f9Smatthias.ringwald // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key 1907a98592bcSMatthias Ringwald if (!hci_stack->link_key_db) break; 1908a6ef64baSMilanka Ringwald hci_event_pin_code_request_get_bd_addr(packet, addr); 1909a98592bcSMatthias Ringwald hci_stack->link_key_db->delete_link_key(addr); 19107fde4af9Smatthias.ringwald break; 19117fde4af9Smatthias.ringwald 19121d6b20aeS[email protected] case HCI_EVENT_IO_CAPABILITY_REQUEST: 19131d6b20aeS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST); 1914dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY); 1915dbe1a790S[email protected] break; 1916dbe1a790S[email protected] 1917dbe1a790S[email protected] case HCI_EVENT_USER_CONFIRMATION_REQUEST: 19186724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 19193a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 1920dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY); 1921dbe1a790S[email protected] break; 1922dbe1a790S[email protected] 1923dbe1a790S[email protected] case HCI_EVENT_USER_PASSKEY_REQUEST: 19246724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 19253a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 1926dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY); 19271d6b20aeS[email protected] break; 192835454696SMatthias Ringwald #endif 19291d6b20aeS[email protected] 1930f0944df2S[email protected] case HCI_EVENT_ENCRYPTION_CHANGE: 1931f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1932f0944df2S[email protected] conn = hci_connection_for_handle(handle); 1933f0944df2S[email protected] if (!conn) break; 1934ad83dc6aS[email protected] if (packet[2] == 0) { 1935f0944df2S[email protected] if (packet[5]){ 1936f0944df2S[email protected] conn->authentication_flags |= CONNECTION_ENCRYPTED; 1937f0944df2S[email protected] } else { 1938536f9994S[email protected] conn->authentication_flags &= ~CONNECTION_ENCRYPTED; 1939f0944df2S[email protected] } 1940ad83dc6aS[email protected] } 194135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1942a00031e2S[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 194335454696SMatthias Ringwald #endif 1944f0944df2S[email protected] break; 1945f0944df2S[email protected] 194635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 19471eb2563eS[email protected] case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT: 1948f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 19491eb2563eS[email protected] conn = hci_connection_for_handle(handle); 19501eb2563eS[email protected] if (!conn) break; 1951ad83dc6aS[email protected] 1952ad83dc6aS[email protected] // dedicated bonding: send result and disconnect 1953ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 1954ad83dc6aS[email protected] conn->bonding_flags &= ~BONDING_DEDICATED; 1955ad83dc6aS[email protected] conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE; 19561bd5283dS[email protected] conn->bonding_status = packet[2]; 1957ad83dc6aS[email protected] break; 1958ad83dc6aS[email protected] } 1959ad83dc6aS[email protected] 1960b5cb6874S[email protected] if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){ 19611eb2563eS[email protected] // link key sufficient for requested security 19621eb2563eS[email protected] conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 1963ad83dc6aS[email protected] break; 1964ad83dc6aS[email protected] } 19651eb2563eS[email protected] // not enough 19661eb2563eS[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 19671eb2563eS[email protected] break; 196835454696SMatthias Ringwald #endif 196934d2123cS[email protected] 197086805605S[email protected] // HCI_EVENT_DISCONNECTION_COMPLETE 1971ccda6e14S[email protected] // has been split, to first notify stack before shutting connection down 1972ccda6e14S[email protected] // see end of function, too. 1973a4f30ec0S[email protected] case HCI_EVENT_DISCONNECTION_COMPLETE: 1974a4f30ec0S[email protected] if (packet[2]) break; // status != 0 1975f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1976c6a37cfdSMatthias Ringwald // drop outgoing ACL fragments if it is for closed connection 1977c6a37cfdSMatthias Ringwald if (hci_stack->acl_fragmentation_total_size > 0) { 1978c6a37cfdSMatthias Ringwald if (handle == READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer)){ 1979c6a37cfdSMatthias Ringwald log_info("hci: drop fragmented ACL data for closed connection"); 1980c6a37cfdSMatthias Ringwald hci_stack->acl_fragmentation_total_size = 0; 1981c6a37cfdSMatthias Ringwald hci_stack->acl_fragmentation_pos = 0; 1982c6a37cfdSMatthias Ringwald } 1983c6a37cfdSMatthias Ringwald } 198435454696SMatthias Ringwald 19859a2e4658SMatthias Ringwald // re-enable advertisements for le connections if active 1986c6a37cfdSMatthias Ringwald conn = hci_connection_for_handle(handle); 1987c6a37cfdSMatthias Ringwald if (!conn) break; 198835454696SMatthias Ringwald #ifdef ENABLE_BLE 1989d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 19909a2e4658SMatthias Ringwald if (hci_is_le_connection(conn) && hci_stack->le_advertisements_enabled){ 19919a2e4658SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_ENABLE; 19929a2e4658SMatthias Ringwald } 199335454696SMatthias Ringwald #endif 1994d70217a2SMatthias Ringwald #endif 1995ccda6e14S[email protected] conn->state = RECEIVED_DISCONNECTION_COMPLETE; 1996ccda6e14S[email protected] break; 19976772a24cSmatthias.ringwald 1998c68bdf90Smatthias.ringwald case HCI_EVENT_HARDWARE_ERROR: 1999313e5f9cSMatthias Ringwald log_error("Hardware Error: 0x%02x", packet[2]); 2000d23838ecSMatthias Ringwald if (hci_stack->hardware_error_callback){ 2001c2e1fa60SMatthias Ringwald (*hci_stack->hardware_error_callback)(packet[2]); 20027586ee35S[email protected] } else { 20037586ee35S[email protected] // if no special requests, just reboot stack 20047586ee35S[email protected] hci_power_control_off(); 20057586ee35S[email protected] hci_power_control_on(); 2006c68bdf90Smatthias.ringwald } 2007c68bdf90Smatthias.ringwald break; 2008c68bdf90Smatthias.ringwald 20090e6f3837SMatthias Ringwald #ifdef ENABLE_CLASSIC 20105cf766e8SMatthias Ringwald case HCI_EVENT_ROLE_CHANGE: 20115cf766e8SMatthias Ringwald if (packet[2]) break; // status != 0 2012f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 20135cf766e8SMatthias Ringwald conn = hci_connection_for_handle(handle); 20145cf766e8SMatthias Ringwald if (!conn) break; // no conn 20155cf766e8SMatthias Ringwald conn->role = packet[9]; 20165cf766e8SMatthias Ringwald break; 20170e6f3837SMatthias Ringwald #endif 20185cf766e8SMatthias Ringwald 201963fa3374SMatthias Ringwald case HCI_EVENT_TRANSPORT_PACKET_SENT: 2020d051460cS[email protected] // release packet buffer only for asynchronous transport and if there are not further fragements 20214fa24b5fS[email protected] if (hci_transport_synchronous()) { 202263fa3374SMatthias Ringwald log_error("Synchronous HCI Transport shouldn't send HCI_EVENT_TRANSPORT_PACKET_SENT"); 20234fa24b5fS[email protected] return; // instead of break: to avoid re-entering hci_run() 20244fa24b5fS[email protected] } 2025d051460cS[email protected] if (hci_stack->acl_fragmentation_total_size) break; 2026d051460cS[email protected] hci_release_packet_buffer(); 2027701e3307SMatthias Ringwald 202863fa3374SMatthias Ringwald // L2CAP receives this event via the hci_emit_event below 202963fa3374SMatthias Ringwald 203035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 203163fa3374SMatthias Ringwald // For SCO, we do the can_send_now_check here 2032701e3307SMatthias Ringwald hci_notify_if_sco_can_send_now(); 203335454696SMatthias Ringwald #endif 20346b4af23dS[email protected] break; 20356b4af23dS[email protected] 203635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 203763fa3374SMatthias Ringwald case HCI_EVENT_SCO_CAN_SEND_NOW: 203863fa3374SMatthias Ringwald // For SCO, we do the can_send_now_check here 203963fa3374SMatthias Ringwald hci_notify_if_sco_can_send_now(); 204063fa3374SMatthias Ringwald return; 2041*1cfb383eSMatthias Ringwald 2042*1cfb383eSMatthias Ringwald // explode inquriy results for easier consumption 2043*1cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 2044*1cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 2045*1cfb383eSMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 2046*1cfb383eSMatthias Ringwald gap_inquiry_explode(packet); 2047*1cfb383eSMatthias Ringwald break; 204835454696SMatthias Ringwald #endif 204963fa3374SMatthias Ringwald 2050a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 20515909f7f2Smatthias.ringwald case HCI_EVENT_LE_META: 20525909f7f2Smatthias.ringwald switch (packet[2]){ 2053d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 205457c9da5bS[email protected] case HCI_SUBEVENT_LE_ADVERTISING_REPORT: 20554f4e0224SMatthias Ringwald // log_info("advertising report received"); 20567bdc6798S[email protected] if (hci_stack->le_scanning_state != LE_SCANNING) break; 205757c9da5bS[email protected] le_handle_advertisement_report(packet, size); 20587bdc6798S[email protected] break; 2059d70217a2SMatthias Ringwald #endif 20605909f7f2Smatthias.ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 20615909f7f2Smatthias.ringwald // Connection management 2062724d70a2SMatthias Ringwald reverse_bd_addr(&packet[8], addr); 20637bdc6798S[email protected] addr_type = (bd_addr_type_t)packet[7]; 20649da54300S[email protected] log_info("LE Connection_complete (status=%u) type %u, %s", packet[3], addr_type, bd_addr_to_str(addr)); 20652e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 2066d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 206742ff5ba1SMatthias Ringwald // if auto-connect, remove from whitelist in both roles 206842ff5ba1SMatthias Ringwald if (hci_stack->le_connecting_state == LE_CONNECTING_WHITELIST){ 206942ff5ba1SMatthias Ringwald hci_remove_from_whitelist(addr_type, addr); 207042ff5ba1SMatthias Ringwald } 207142ff5ba1SMatthias Ringwald // handle error: error is reported only to the initiator -> outgoing connection 20725909f7f2Smatthias.ringwald if (packet[3]){ 207342ff5ba1SMatthias Ringwald // outgoing connection establishment is done 207442ff5ba1SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 207542ff5ba1SMatthias Ringwald // remove entry 20765909f7f2Smatthias.ringwald if (conn){ 2077665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 20785909f7f2Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 20795909f7f2Smatthias.ringwald } 20805909f7f2Smatthias.ringwald break; 20815909f7f2Smatthias.ringwald } 2082d70217a2SMatthias Ringwald #endif 208342ff5ba1SMatthias Ringwald // on success, both hosts receive connection complete event 20845cf766e8SMatthias Ringwald if (packet[6] == HCI_ROLE_MASTER){ 2085d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 208642ff5ba1SMatthias Ringwald // if we're master, it was an outgoing connection and we're done with it 208742ff5ba1SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 2088d70217a2SMatthias Ringwald #endif 208942ff5ba1SMatthias Ringwald } else { 2090d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 209142ff5ba1SMatthias Ringwald // if we're slave, it was an incoming connection, advertisements have stopped 2092171293d3SMatthias Ringwald hci_stack->le_advertisements_active = 0; 20935106e6dcSMatthias Ringwald // try to re-enable them 20945106e6dcSMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_ENABLE; 2095d70217a2SMatthias Ringwald #endif 209642ff5ba1SMatthias Ringwald } 2097171293d3SMatthias Ringwald // LE connections are auto-accepted, so just create a connection if there isn't one already 209842ff5ba1SMatthias Ringwald if (!conn){ 209996a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 21005909f7f2Smatthias.ringwald } 210142ff5ba1SMatthias Ringwald // no memory, sorry. 21025909f7f2Smatthias.ringwald if (!conn){ 21035909f7f2Smatthias.ringwald break; 21045909f7f2Smatthias.ringwald } 21055909f7f2Smatthias.ringwald 21065909f7f2Smatthias.ringwald conn->state = OPEN; 21075cf766e8SMatthias Ringwald conn->role = packet[6]; 2108f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 4); 21095909f7f2Smatthias.ringwald 21105909f7f2Smatthias.ringwald // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock 21115909f7f2Smatthias.ringwald 21125909f7f2Smatthias.ringwald // restart timer 2113528a4a3bSMatthias Ringwald // btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 2114528a4a3bSMatthias Ringwald // btstack_run_loop_add_timer(&conn->timeout); 21155909f7f2Smatthias.ringwald 21169da54300S[email protected] log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 21175909f7f2Smatthias.ringwald 21185909f7f2Smatthias.ringwald hci_emit_nr_connections_changed(); 21195909f7f2Smatthias.ringwald break; 21205909f7f2Smatthias.ringwald 2121f8fbdce0SMatthias Ringwald // log_info("LE buffer size: %u, count %u", little_endian_read_16(packet,6), packet[8]); 212265a46ef3S[email protected] 21235909f7f2Smatthias.ringwald default: 21245909f7f2Smatthias.ringwald break; 21255909f7f2Smatthias.ringwald } 21265909f7f2Smatthias.ringwald break; 21275909f7f2Smatthias.ringwald #endif 21286772a24cSmatthias.ringwald default: 21296772a24cSmatthias.ringwald break; 2130fe1ed1b8Smatthias.ringwald } 2131fe1ed1b8Smatthias.ringwald 21323429f56bSmatthias.ringwald // handle BT initialization 21333a9fb326S[email protected] if (hci_stack->state == HCI_STATE_INITIALIZING){ 21346155b3d3S[email protected] hci_initializing_event_handler(packet, size); 2135c9af4d3fS[email protected] } 213622909952Smatthias.ringwald 213789db417bSmatthias.ringwald // help with BT sleep 21383a9fb326S[email protected] if (hci_stack->state == HCI_STATE_FALLING_ASLEEP 213974b323a9SMatthias Ringwald && hci_stack->substate == HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE 2140073bd0faSMatthias Ringwald && HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_scan_enable)){ 214155975f88SMatthias Ringwald hci_initializing_next_state(); 214289db417bSmatthias.ringwald } 214389db417bSmatthias.ringwald 214486805605S[email protected] // notify upper stack 2145d6b06661SMatthias Ringwald hci_emit_event(packet, size, 0); // don't dump, already happened in packet handler 214694ab26f8Smatthias.ringwald 214786805605S[email protected] // moved here to give upper stack a chance to close down everything with hci_connection_t intact 21480e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_DISCONNECTION_COMPLETE){ 214986805605S[email protected] if (!packet[2]){ 2150f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 215105ae8de3SMatthias Ringwald hci_connection_t * aConn = hci_connection_for_handle(handle); 215205ae8de3SMatthias Ringwald if (aConn) { 215305ae8de3SMatthias Ringwald uint8_t status = aConn->bonding_status; 215405ae8de3SMatthias Ringwald uint16_t flags = aConn->bonding_flags; 215586805605S[email protected] bd_addr_t bd_address; 215605ae8de3SMatthias Ringwald memcpy(&bd_address, aConn->address, 6); 215705ae8de3SMatthias Ringwald hci_shutdown_connection(aConn); 2158bb820044S[email protected] // connection struct is gone, don't access anymore 2159bb820044S[email protected] if (flags & BONDING_EMIT_COMPLETE_ON_DISCONNECT){ 216086805605S[email protected] hci_emit_dedicated_bonding_result(bd_address, status); 216186805605S[email protected] } 216286805605S[email protected] } 216386805605S[email protected] } 216486805605S[email protected] } 216586805605S[email protected] 216694ab26f8Smatthias.ringwald // execute main loop 216794ab26f8Smatthias.ringwald hci_run(); 216816833f0aSmatthias.ringwald } 216916833f0aSmatthias.ringwald 217035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2171c91d150bS[email protected] static void sco_handler(uint8_t * packet, uint16_t size){ 21728abbe8b5SMatthias Ringwald if (!hci_stack->sco_packet_handler) return; 21733d50b4baSMatthias Ringwald hci_stack->sco_packet_handler(HCI_SCO_DATA_PACKET, 0, packet, size); 21742b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 21752b838201SMatthias Ringwald hci_con_handle_t con_handle = READ_SCO_CONNECTION_HANDLE(packet); 21762b838201SMatthias Ringwald hci_connection_t *conn = hci_connection_for_handle(con_handle); 21772b838201SMatthias Ringwald if (conn){ 21782b838201SMatthias Ringwald conn->num_packets_completed++; 21792b838201SMatthias Ringwald hci_stack->host_completed_packets = 1; 21802b838201SMatthias Ringwald hci_run(); 21812b838201SMatthias Ringwald } 21822b838201SMatthias Ringwald #endif 2183c91d150bS[email protected] } 218435454696SMatthias Ringwald #endif 2185c91d150bS[email protected] 21860a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 21875bb5bc3eS[email protected] hci_dump_packet(packet_type, 1, packet, size); 218810e830c9Smatthias.ringwald switch (packet_type) { 218910e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 219010e830c9Smatthias.ringwald event_handler(packet, size); 219110e830c9Smatthias.ringwald break; 219210e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 219310e830c9Smatthias.ringwald acl_handler(packet, size); 219410e830c9Smatthias.ringwald break; 219535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2196c91d150bS[email protected] case HCI_SCO_DATA_PACKET: 2197c91d150bS[email protected] sco_handler(packet, size); 2198202c8a4cSMatthias Ringwald break; 219935454696SMatthias Ringwald #endif 220010e830c9Smatthias.ringwald default: 220110e830c9Smatthias.ringwald break; 220210e830c9Smatthias.ringwald } 220310e830c9Smatthias.ringwald } 220410e830c9Smatthias.ringwald 2205d6b06661SMatthias Ringwald /** 2206d6b06661SMatthias Ringwald * @brief Add event packet handler. 2207d6b06661SMatthias Ringwald */ 2208d6b06661SMatthias Ringwald void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){ 2209d6b06661SMatthias Ringwald btstack_linked_list_add_tail(&hci_stack->event_handlers, (btstack_linked_item_t*) callback_handler); 2210d6b06661SMatthias Ringwald } 2211d6b06661SMatthias Ringwald 2212d6b06661SMatthias Ringwald 2213fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 22143d50b4baSMatthias Ringwald void hci_register_acl_packet_handler(btstack_packet_handler_t handler){ 2215fb37a842SMatthias Ringwald hci_stack->acl_packet_handler = handler; 221616833f0aSmatthias.ringwald } 221716833f0aSmatthias.ringwald 221835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 22198abbe8b5SMatthias Ringwald /** 22208abbe8b5SMatthias Ringwald * @brief Registers a packet handler for SCO data. Used for HSP and HFP profiles. 22218abbe8b5SMatthias Ringwald */ 22223d50b4baSMatthias Ringwald void hci_register_sco_packet_handler(btstack_packet_handler_t handler){ 22238abbe8b5SMatthias Ringwald hci_stack->sco_packet_handler = handler; 22248abbe8b5SMatthias Ringwald } 222535454696SMatthias Ringwald #endif 22268abbe8b5SMatthias Ringwald 222771de195eSMatthias Ringwald static void hci_state_reset(void){ 2228595bdbfbS[email protected] // no connections yet 2229595bdbfbS[email protected] hci_stack->connections = NULL; 223074308b23Smatthias.ringwald 223174308b23Smatthias.ringwald // keep discoverable/connectable as this has been requested by the client(s) 223274308b23Smatthias.ringwald // hci_stack->discoverable = 0; 223374308b23Smatthias.ringwald // hci_stack->connectable = 0; 223474308b23Smatthias.ringwald // hci_stack->bondable = 1; 2235b95a5a35SMatthias Ringwald // hci_stack->own_addr_type = 0; 2236595bdbfbS[email protected] 223744935e40S[email protected] // buffer is free 223844935e40S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 223944935e40S[email protected] 2240595bdbfbS[email protected] // no pending cmds 2241595bdbfbS[email protected] hci_stack->decline_reason = 0; 2242595bdbfbS[email protected] hci_stack->new_scan_enable_value = 0xff; 2243595bdbfbS[email protected] 2244595bdbfbS[email protected] // LE 2245b95a5a35SMatthias Ringwald #ifdef ENABLE_BLE 2246b95a5a35SMatthias Ringwald memset(hci_stack->le_random_address, 0, 6); 2247b95a5a35SMatthias Ringwald hci_stack->le_random_address_set = 0; 2248d70217a2SMatthias Ringwald #endif 2249d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 2250595bdbfbS[email protected] hci_stack->le_scanning_state = LE_SCAN_IDLE; 2251e2602ea2Smatthias.ringwald hci_stack->le_scan_type = 0xff; 2252b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 2253e83201bcSMatthias Ringwald hci_stack->le_whitelist = 0; 2254e83201bcSMatthias Ringwald hci_stack->le_whitelist_capacity = 0; 2255d70217a2SMatthias Ringwald #endif 2256d70217a2SMatthias Ringwald 2257e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_interval_min = 6; 2258e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_interval_max = 3200; 2259e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_latency_min = 0; 2260e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_latency_max = 500; 2261e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_supervision_timeout_min = 10; 2262e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_supervision_timeout_max = 3200; 2263595bdbfbS[email protected] } 2264595bdbfbS[email protected] 226535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 22662d5e09d6SMatthias Ringwald /** 22672d5e09d6SMatthias Ringwald * @brief Configure Bluetooth hardware control. Has to be called before power on. 22682d5e09d6SMatthias Ringwald */ 22692d5e09d6SMatthias Ringwald void hci_set_link_key_db(btstack_link_key_db_t const * link_key_db){ 22702d5e09d6SMatthias Ringwald // store and open remote device db 22712d5e09d6SMatthias Ringwald hci_stack->link_key_db = link_key_db; 22722d5e09d6SMatthias Ringwald if (hci_stack->link_key_db) { 22732d5e09d6SMatthias Ringwald hci_stack->link_key_db->open(); 22742d5e09d6SMatthias Ringwald } 22752d5e09d6SMatthias Ringwald } 227635454696SMatthias Ringwald #endif 22772d5e09d6SMatthias Ringwald 22782d5e09d6SMatthias Ringwald void hci_init(const hci_transport_t *transport, const void *config){ 2279475c8125Smatthias.ringwald 22803a9fb326S[email protected] #ifdef HAVE_MALLOC 22813a9fb326S[email protected] if (!hci_stack) { 22823a9fb326S[email protected] hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t)); 22833a9fb326S[email protected] } 22843a9fb326S[email protected] #else 22853a9fb326S[email protected] hci_stack = &hci_stack_static; 22863a9fb326S[email protected] #endif 228766fb9560S[email protected] memset(hci_stack, 0, sizeof(hci_stack_t)); 22883a9fb326S[email protected] 2289475c8125Smatthias.ringwald // reference to use transport layer implementation 22903a9fb326S[email protected] hci_stack->hci_transport = transport; 2291475c8125Smatthias.ringwald 229211e23e5fSmatthias.ringwald // reference to used config 22933a9fb326S[email protected] hci_stack->config = config; 229411e23e5fSmatthias.ringwald 229526a9b6daSMatthias Ringwald // setup pointer for outgoing packet buffer 229626a9b6daSMatthias Ringwald hci_stack->hci_packet_buffer = &hci_stack->hci_packet_buffer_data[HCI_OUTGOING_PRE_BUFFER_SIZE]; 229726a9b6daSMatthias Ringwald 22988fcba05dSmatthias.ringwald // max acl payload size defined in config.h 22993a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 23008fcba05dSmatthias.ringwald 230116833f0aSmatthias.ringwald // register packet handlers with transport 230210e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 2303f5454fc6Smatthias.ringwald 23043a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 2305e2386ba1S[email protected] 2306e2386ba1S[email protected] // class of device 23073a9fb326S[email protected] hci_stack->class_of_device = 0x007a020c; // Smartphone 2308a45d6b9fS[email protected] 2309f20168b8Smatthias.ringwald // bondable by default 2310f20168b8Smatthias.ringwald hci_stack->bondable = 1; 2311f20168b8Smatthias.ringwald 231263048403S[email protected] // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept 23133a9fb326S[email protected] hci_stack->ssp_enable = 1; 23143a9fb326S[email protected] hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT; 23153a9fb326S[email protected] hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING; 23163a9fb326S[email protected] hci_stack->ssp_auto_accept = 1; 231769a97523S[email protected] 2318fac2e2feSMatthias Ringwald // voice setting - signed 16 bit pcm data with CVSD over the air 2319fac2e2feSMatthias Ringwald hci_stack->sco_voice_setting = 0x60; 2320d950d659SMatthias Ringwald 2321595bdbfbS[email protected] hci_state_reset(); 2322475c8125Smatthias.ringwald } 2323475c8125Smatthias.ringwald 23243fb36a29SMatthias Ringwald /** 23253fb36a29SMatthias Ringwald * @brief Configure Bluetooth chipset driver. Has to be called before power on, or right after receiving the local version information 23263fb36a29SMatthias Ringwald */ 23273fb36a29SMatthias Ringwald void hci_set_chipset(const btstack_chipset_t *chipset_driver){ 23283fb36a29SMatthias Ringwald hci_stack->chipset = chipset_driver; 23293fb36a29SMatthias Ringwald 23303fb36a29SMatthias Ringwald // reset chipset driver - init is also called on power_up 23313fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->init){ 23323fb36a29SMatthias Ringwald hci_stack->chipset->init(hci_stack->config); 23333fb36a29SMatthias Ringwald } 23343fb36a29SMatthias Ringwald } 23353fb36a29SMatthias Ringwald 2336fb55bd0aSMatthias Ringwald /** 2337d0b87befSMatthias Ringwald * @brief Configure Bluetooth hardware control. Has to be called after hci_init() but before power on. 2338fb55bd0aSMatthias Ringwald */ 2339fb55bd0aSMatthias Ringwald void hci_set_control(const btstack_control_t *hardware_control){ 2340fb55bd0aSMatthias Ringwald // references to used control implementation 2341fb55bd0aSMatthias Ringwald hci_stack->control = hardware_control; 2342d0b87befSMatthias Ringwald // init with transport config 2343d0b87befSMatthias Ringwald hardware_control->init(hci_stack->config); 2344fb55bd0aSMatthias Ringwald } 2345fb55bd0aSMatthias Ringwald 234671de195eSMatthias Ringwald void hci_close(void){ 2347404843c1Smatthias.ringwald // close remote device db 2348a98592bcSMatthias Ringwald if (hci_stack->link_key_db) { 2349a98592bcSMatthias Ringwald hci_stack->link_key_db->close(); 2350404843c1Smatthias.ringwald } 23517224be7eSMatthias Ringwald 23527224be7eSMatthias Ringwald btstack_linked_list_iterator_t lit; 23537224be7eSMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->connections); 23547224be7eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 23557224be7eSMatthias Ringwald // cancel all l2cap connections by emitting dicsconnection complete before shutdown (free) connection 23567224be7eSMatthias Ringwald hci_connection_t * connection = (hci_connection_t*) btstack_linked_list_iterator_next(&lit); 23577224be7eSMatthias Ringwald hci_emit_disconnection_complete(connection->con_handle, 0x16); // terminated by local host 23587224be7eSMatthias Ringwald hci_shutdown_connection(connection); 2359f5454fc6Smatthias.ringwald } 23607224be7eSMatthias Ringwald 2361f5454fc6Smatthias.ringwald hci_power_control(HCI_POWER_OFF); 23623a9fb326S[email protected] 23633a9fb326S[email protected] #ifdef HAVE_MALLOC 23643a9fb326S[email protected] free(hci_stack); 23653a9fb326S[email protected] #endif 23663a9fb326S[email protected] hci_stack = NULL; 2367404843c1Smatthias.ringwald } 2368404843c1Smatthias.ringwald 236935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 237060b9e82fSMatthias Ringwald void gap_set_class_of_device(uint32_t class_of_device){ 23719e61646fS[email protected] hci_stack->class_of_device = class_of_device; 23729e61646fS[email protected] } 237376f27cffSMatthias Ringwald 237476f27cffSMatthias Ringwald void hci_disable_l2cap_timeout_check(void){ 237576f27cffSMatthias Ringwald disable_l2cap_timeouts = 1; 237676f27cffSMatthias Ringwald } 237735454696SMatthias Ringwald #endif 23789e61646fS[email protected] 237976f27cffSMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 2380f456b2d0S[email protected] // Set Public BD ADDR - passed on to Bluetooth chipset if supported in bt_control_h 2381f456b2d0S[email protected] void hci_set_bd_addr(bd_addr_t addr){ 2382f456b2d0S[email protected] memcpy(hci_stack->custom_bd_addr, addr, 6); 2383f456b2d0S[email protected] hci_stack->custom_bd_addr_set = 1; 2384f456b2d0S[email protected] } 238576f27cffSMatthias Ringwald #endif 2386f456b2d0S[email protected] 23878d213e1aSmatthias.ringwald // State-Module-Driver overview 23888d213e1aSmatthias.ringwald // state module low-level 23898d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 23908d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 23918d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 23928d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 2393d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 2394d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 2395c7e0c5f6Smatthias.ringwald 239640d1c7a4Smatthias.ringwald static int hci_power_control_on(void){ 23977301ad89Smatthias.ringwald 2398038bc64cSmatthias.ringwald // power on 2399f9a30166Smatthias.ringwald int err = 0; 24003a9fb326S[email protected] if (hci_stack->control && hci_stack->control->on){ 2401d0b87befSMatthias Ringwald err = (*hci_stack->control->on)(); 2402f9a30166Smatthias.ringwald } 2403038bc64cSmatthias.ringwald if (err){ 24049da54300S[email protected] log_error( "POWER_ON failed"); 2405038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 2406038bc64cSmatthias.ringwald return err; 2407038bc64cSmatthias.ringwald } 2408038bc64cSmatthias.ringwald 240924b3c629SMatthias Ringwald // int chipset driver 24103fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->init){ 24113fb36a29SMatthias Ringwald hci_stack->chipset->init(hci_stack->config); 24123fb36a29SMatthias Ringwald } 24133fb36a29SMatthias Ringwald 241424b3c629SMatthias Ringwald // init transport 241524b3c629SMatthias Ringwald if (hci_stack->hci_transport->init){ 241624b3c629SMatthias Ringwald hci_stack->hci_transport->init(hci_stack->config); 241724b3c629SMatthias Ringwald } 241824b3c629SMatthias Ringwald 241924b3c629SMatthias Ringwald // open transport 242024b3c629SMatthias Ringwald err = hci_stack->hci_transport->open(); 2421038bc64cSmatthias.ringwald if (err){ 24229da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 24233a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 2424d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 2425f9a30166Smatthias.ringwald } 2426038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 2427038bc64cSmatthias.ringwald return err; 2428038bc64cSmatthias.ringwald } 24298d213e1aSmatthias.ringwald return 0; 24308d213e1aSmatthias.ringwald } 2431038bc64cSmatthias.ringwald 243240d1c7a4Smatthias.ringwald static void hci_power_control_off(void){ 24338d213e1aSmatthias.ringwald 24349da54300S[email protected] log_info("hci_power_control_off"); 24359418f9c9Smatthias.ringwald 24368d213e1aSmatthias.ringwald // close low-level device 243724b3c629SMatthias Ringwald hci_stack->hci_transport->close(); 24388d213e1aSmatthias.ringwald 24399da54300S[email protected] log_info("hci_power_control_off - hci_transport closed"); 24409418f9c9Smatthias.ringwald 24418d213e1aSmatthias.ringwald // power off 24423a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 2443d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 24448d213e1aSmatthias.ringwald } 24459418f9c9Smatthias.ringwald 24469da54300S[email protected] log_info("hci_power_control_off - control closed"); 24479418f9c9Smatthias.ringwald 24483a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 244972ea5239Smatthias.ringwald } 245072ea5239Smatthias.ringwald 245140d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){ 245272ea5239Smatthias.ringwald 24539da54300S[email protected] log_info("hci_power_control_sleep"); 24543144bce4Smatthias.ringwald 2455b429b9b7Smatthias.ringwald #if 0 2456b429b9b7Smatthias.ringwald // don't close serial port during sleep 2457b429b9b7Smatthias.ringwald 245872ea5239Smatthias.ringwald // close low-level device 24593a9fb326S[email protected] hci_stack->hci_transport->close(hci_stack->config); 2460b429b9b7Smatthias.ringwald #endif 246172ea5239Smatthias.ringwald 246272ea5239Smatthias.ringwald // sleep mode 24633a9fb326S[email protected] if (hci_stack->control && hci_stack->control->sleep){ 2464d0b87befSMatthias Ringwald (*hci_stack->control->sleep)(); 246572ea5239Smatthias.ringwald } 2466b429b9b7Smatthias.ringwald 24673a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 24688d213e1aSmatthias.ringwald } 24698d213e1aSmatthias.ringwald 247040d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){ 2471b429b9b7Smatthias.ringwald 24729da54300S[email protected] log_info("hci_power_control_wake"); 2473b429b9b7Smatthias.ringwald 2474b429b9b7Smatthias.ringwald // wake on 24753a9fb326S[email protected] if (hci_stack->control && hci_stack->control->wake){ 2476d0b87befSMatthias Ringwald (*hci_stack->control->wake)(); 2477b429b9b7Smatthias.ringwald } 2478b429b9b7Smatthias.ringwald 2479b429b9b7Smatthias.ringwald #if 0 2480b429b9b7Smatthias.ringwald // open low-level device 24813a9fb326S[email protected] int err = hci_stack->hci_transport->open(hci_stack->config); 2482b429b9b7Smatthias.ringwald if (err){ 24839da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 24843a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 2485d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 2486b429b9b7Smatthias.ringwald } 2487b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 2488b429b9b7Smatthias.ringwald return err; 2489b429b9b7Smatthias.ringwald } 2490b429b9b7Smatthias.ringwald #endif 2491b429b9b7Smatthias.ringwald 2492b429b9b7Smatthias.ringwald return 0; 2493b429b9b7Smatthias.ringwald } 2494b429b9b7Smatthias.ringwald 249544935e40S[email protected] static void hci_power_transition_to_initializing(void){ 249644935e40S[email protected] // set up state machine 249744935e40S[email protected] hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent 249844935e40S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 249944935e40S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 25005c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET; 250144935e40S[email protected] } 2502b429b9b7Smatthias.ringwald 25038d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 2504d661ed19Smatthias.ringwald 2505f04a0c31SMatthias Ringwald log_info("hci_power_control: %d, current mode %u", power_mode, hci_stack->state); 2506d661ed19Smatthias.ringwald 25078d213e1aSmatthias.ringwald int err = 0; 25083a9fb326S[email protected] switch (hci_stack->state){ 25098d213e1aSmatthias.ringwald 25108d213e1aSmatthias.ringwald case HCI_STATE_OFF: 25118d213e1aSmatthias.ringwald switch (power_mode){ 25128d213e1aSmatthias.ringwald case HCI_POWER_ON: 25138d213e1aSmatthias.ringwald err = hci_power_control_on(); 251497b61c7bS[email protected] if (err) { 2515f04a0c31SMatthias Ringwald log_error("hci_power_control_on() error %d", err); 251697b61c7bS[email protected] return err; 251797b61c7bS[email protected] } 251844935e40S[email protected] hci_power_transition_to_initializing(); 25198d213e1aSmatthias.ringwald break; 25208d213e1aSmatthias.ringwald case HCI_POWER_OFF: 25218d213e1aSmatthias.ringwald // do nothing 25228d213e1aSmatthias.ringwald break; 25238d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2524b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 25258d213e1aSmatthias.ringwald break; 25268d213e1aSmatthias.ringwald } 25278d213e1aSmatthias.ringwald break; 25287301ad89Smatthias.ringwald 25298d213e1aSmatthias.ringwald case HCI_STATE_INITIALIZING: 25308d213e1aSmatthias.ringwald switch (power_mode){ 25318d213e1aSmatthias.ringwald case HCI_POWER_ON: 25328d213e1aSmatthias.ringwald // do nothing 25338d213e1aSmatthias.ringwald break; 25348d213e1aSmatthias.ringwald case HCI_POWER_OFF: 25358d213e1aSmatthias.ringwald // no connections yet, just turn it off 25368d213e1aSmatthias.ringwald hci_power_control_off(); 25378d213e1aSmatthias.ringwald break; 25388d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2539b546ac54Smatthias.ringwald // no connections yet, just turn it off 254072ea5239Smatthias.ringwald hci_power_control_sleep(); 25418d213e1aSmatthias.ringwald break; 25428d213e1aSmatthias.ringwald } 25438d213e1aSmatthias.ringwald break; 25447301ad89Smatthias.ringwald 25458d213e1aSmatthias.ringwald case HCI_STATE_WORKING: 25468d213e1aSmatthias.ringwald switch (power_mode){ 25478d213e1aSmatthias.ringwald case HCI_POWER_ON: 25488d213e1aSmatthias.ringwald // do nothing 25498d213e1aSmatthias.ringwald break; 25508d213e1aSmatthias.ringwald case HCI_POWER_OFF: 2551c7e0c5f6Smatthias.ringwald // see hci_run 25523a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 25538d213e1aSmatthias.ringwald break; 25548d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2555b546ac54Smatthias.ringwald // see hci_run 25563a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 255774b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT; 25588d213e1aSmatthias.ringwald break; 25598d213e1aSmatthias.ringwald } 25608d213e1aSmatthias.ringwald break; 25617301ad89Smatthias.ringwald 25628d213e1aSmatthias.ringwald case HCI_STATE_HALTING: 25638d213e1aSmatthias.ringwald switch (power_mode){ 25648d213e1aSmatthias.ringwald case HCI_POWER_ON: 256544935e40S[email protected] hci_power_transition_to_initializing(); 25668d213e1aSmatthias.ringwald break; 25678d213e1aSmatthias.ringwald case HCI_POWER_OFF: 25688d213e1aSmatthias.ringwald // do nothing 25698d213e1aSmatthias.ringwald break; 25708d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2571b546ac54Smatthias.ringwald // see hci_run 25723a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 257374b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT; 25748d213e1aSmatthias.ringwald break; 25758d213e1aSmatthias.ringwald } 25768d213e1aSmatthias.ringwald break; 25778d213e1aSmatthias.ringwald 25788d213e1aSmatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 25798d213e1aSmatthias.ringwald switch (power_mode){ 25808d213e1aSmatthias.ringwald case HCI_POWER_ON: 258128171530Smatthias.ringwald 2582423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 258328171530Smatthias.ringwald // nothing to do, if H4 supports power management 2584d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 25853a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 258674b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_WRITE_SCAN_ENABLE; // init after sleep 258728171530Smatthias.ringwald break; 258828171530Smatthias.ringwald } 258928171530Smatthias.ringwald #endif 259044935e40S[email protected] hci_power_transition_to_initializing(); 25918d213e1aSmatthias.ringwald break; 25928d213e1aSmatthias.ringwald case HCI_POWER_OFF: 2593b546ac54Smatthias.ringwald // see hci_run 25943a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 25958d213e1aSmatthias.ringwald break; 25968d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2597b546ac54Smatthias.ringwald // do nothing 25988d213e1aSmatthias.ringwald break; 25998d213e1aSmatthias.ringwald } 26008d213e1aSmatthias.ringwald break; 26018d213e1aSmatthias.ringwald 26028d213e1aSmatthias.ringwald case HCI_STATE_SLEEPING: 26038d213e1aSmatthias.ringwald switch (power_mode){ 26048d213e1aSmatthias.ringwald case HCI_POWER_ON: 260528171530Smatthias.ringwald 2606423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 260728171530Smatthias.ringwald // nothing to do, if H4 supports power management 2608d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 26093a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 26105c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_AFTER_SLEEP; 2611758b46ceSmatthias.ringwald hci_update_scan_enable(); 261228171530Smatthias.ringwald break; 261328171530Smatthias.ringwald } 261428171530Smatthias.ringwald #endif 26153144bce4Smatthias.ringwald err = hci_power_control_wake(); 26163144bce4Smatthias.ringwald if (err) return err; 261744935e40S[email protected] hci_power_transition_to_initializing(); 26188d213e1aSmatthias.ringwald break; 26198d213e1aSmatthias.ringwald case HCI_POWER_OFF: 26203a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 26218d213e1aSmatthias.ringwald break; 26228d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2623b546ac54Smatthias.ringwald // do nothing 26248d213e1aSmatthias.ringwald break; 26258d213e1aSmatthias.ringwald } 26268d213e1aSmatthias.ringwald break; 262711e23e5fSmatthias.ringwald } 262868d92d03Smatthias.ringwald 2629038bc64cSmatthias.ringwald // create internal event 2630ee8bf225Smatthias.ringwald hci_emit_state(); 2631ee8bf225Smatthias.ringwald 263268d92d03Smatthias.ringwald // trigger next/first action 263368d92d03Smatthias.ringwald hci_run(); 263468d92d03Smatthias.ringwald 2635475c8125Smatthias.ringwald return 0; 2636475c8125Smatthias.ringwald } 2637475c8125Smatthias.ringwald 263835454696SMatthias Ringwald 263935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 264035454696SMatthias Ringwald 2641758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){ 2642758b46ceSmatthias.ringwald // 2 = page scan, 1 = inq scan 26433a9fb326S[email protected] hci_stack->new_scan_enable_value = hci_stack->connectable << 1 | hci_stack->discoverable; 2644758b46ceSmatthias.ringwald hci_run(); 2645758b46ceSmatthias.ringwald } 2646758b46ceSmatthias.ringwald 264715a95bd5SMatthias Ringwald void gap_discoverable_control(uint8_t enable){ 2648381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 2649381fbed8Smatthias.ringwald 26503a9fb326S[email protected] if (hci_stack->discoverable == enable){ 26513a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 2652381fbed8Smatthias.ringwald return; 2653381fbed8Smatthias.ringwald } 2654381fbed8Smatthias.ringwald 26553a9fb326S[email protected] hci_stack->discoverable = enable; 2656758b46ceSmatthias.ringwald hci_update_scan_enable(); 2657758b46ceSmatthias.ringwald } 2658b031bebbSmatthias.ringwald 265915a95bd5SMatthias Ringwald void gap_connectable_control(uint8_t enable){ 2660758b46ceSmatthias.ringwald if (enable) enable = 1; // normalize argument 2661758b46ceSmatthias.ringwald 2662758b46ceSmatthias.ringwald // don't emit event 26633a9fb326S[email protected] if (hci_stack->connectable == enable) return; 2664758b46ceSmatthias.ringwald 26653a9fb326S[email protected] hci_stack->connectable = enable; 2666758b46ceSmatthias.ringwald hci_update_scan_enable(); 2667381fbed8Smatthias.ringwald } 266835454696SMatthias Ringwald #endif 2669381fbed8Smatthias.ringwald 267015a95bd5SMatthias Ringwald void gap_local_bd_addr(bd_addr_t address_buffer){ 2671690bd0baS[email protected] memcpy(address_buffer, hci_stack->local_bd_addr, 6); 26725061f3afS[email protected] } 26735061f3afS[email protected] 26742b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 26752b838201SMatthias Ringwald static void hci_host_num_completed_packets(void){ 26762b838201SMatthias Ringwald 26772b838201SMatthias Ringwald // create packet manually as arrays are not supported and num_commands should not get reduced 26782b838201SMatthias Ringwald hci_reserve_packet_buffer(); 26792b838201SMatthias Ringwald uint8_t * packet = hci_get_outgoing_packet_buffer(); 26802b838201SMatthias Ringwald 26812b838201SMatthias Ringwald uint16_t size = 0; 26822b838201SMatthias Ringwald uint16_t num_handles = 0; 26832b838201SMatthias Ringwald packet[size++] = 0x35; 26842b838201SMatthias Ringwald packet[size++] = 0x0c; 26852b838201SMatthias Ringwald size++; // skip param len 26862b838201SMatthias Ringwald size++; // skip num handles 26872b838201SMatthias Ringwald 26882b838201SMatthias Ringwald // add { handle, packets } entries 26892b838201SMatthias Ringwald btstack_linked_item_t * it; 26902b838201SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 26912b838201SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 26922b838201SMatthias Ringwald if (connection->num_packets_completed){ 26932b838201SMatthias Ringwald little_endian_store_16(packet, size, connection->con_handle); 26942b838201SMatthias Ringwald size += 2; 26952b838201SMatthias Ringwald little_endian_store_16(packet, size, connection->num_packets_completed); 26962b838201SMatthias Ringwald size += 2; 26972b838201SMatthias Ringwald // 26982b838201SMatthias Ringwald num_handles++; 26992b838201SMatthias Ringwald connection->num_packets_completed = 0; 27002b838201SMatthias Ringwald } 27012b838201SMatthias Ringwald } 27022b838201SMatthias Ringwald 27032b838201SMatthias Ringwald packet[2] = size - 3; 27042b838201SMatthias Ringwald packet[3] = num_handles; 27052b838201SMatthias Ringwald 27062b838201SMatthias Ringwald hci_stack->host_completed_packets = 0; 27072b838201SMatthias Ringwald 27082b838201SMatthias Ringwald hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size); 27092b838201SMatthias Ringwald hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 27102b838201SMatthias Ringwald 27112b838201SMatthias Ringwald // release packet buffer for synchronous transport implementations 27122b838201SMatthias Ringwald if (hci_transport_synchronous()){ 27132b838201SMatthias Ringwald hci_stack->hci_packet_buffer_reserved = 0; 27142b838201SMatthias Ringwald } 27152b838201SMatthias Ringwald } 27162b838201SMatthias Ringwald #endif 27172b838201SMatthias Ringwald 271895d71764SMatthias Ringwald static void hci_run(void){ 27198a485f27Smatthias.ringwald 2720db8bc6ffSMatthias Ringwald // log_info("hci_run: entered"); 2721665d90f2SMatthias Ringwald btstack_linked_item_t * it; 272232ab9390Smatthias.ringwald 2723b5d8b22bS[email protected] // send continuation fragments first, as they block the prepared packet buffer 2724b5d8b22bS[email protected] if (hci_stack->acl_fragmentation_total_size > 0) { 2725b5d8b22bS[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer); 2726b5d8b22bS[email protected] hci_connection_t *connection = hci_connection_for_handle(con_handle); 2727b5d8b22bS[email protected] if (connection) { 272828a0332dSMatthias Ringwald if (hci_can_send_prepared_acl_packet_now(con_handle)){ 2729b5d8b22bS[email protected] hci_send_acl_packet_fragments(connection); 2730b5d8b22bS[email protected] return; 2731b5d8b22bS[email protected] } 273228a0332dSMatthias Ringwald } else { 2733b5d8b22bS[email protected] // connection gone -> discard further fragments 273428a0332dSMatthias Ringwald log_info("hci_run: fragmented ACL packet no connection -> discard fragment"); 2735b5d8b22bS[email protected] hci_stack->acl_fragmentation_total_size = 0; 2736b5d8b22bS[email protected] hci_stack->acl_fragmentation_pos = 0; 2737b5d8b22bS[email protected] } 2738b5d8b22bS[email protected] } 2739b5d8b22bS[email protected] 27402b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 27412b838201SMatthias Ringwald // send host num completed packets next as they don't require num_cmd_packets > 0 27422b838201SMatthias Ringwald if (!hci_can_send_comand_packet_transport()) return; 27432b838201SMatthias Ringwald if (hci_stack->host_completed_packets){ 27442b838201SMatthias Ringwald hci_host_num_completed_packets(); 27452b838201SMatthias Ringwald return; 27462b838201SMatthias Ringwald } 27472b838201SMatthias Ringwald #endif 27482b838201SMatthias Ringwald 2749d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 2750ce4c8fabSmatthias.ringwald 2751b031bebbSmatthias.ringwald // global/non-connection oriented commands 2752b031bebbSmatthias.ringwald 275335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2754b031bebbSmatthias.ringwald // decline incoming connections 27553a9fb326S[email protected] if (hci_stack->decline_reason){ 27563a9fb326S[email protected] uint8_t reason = hci_stack->decline_reason; 27573a9fb326S[email protected] hci_stack->decline_reason = 0; 27583a9fb326S[email protected] hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason); 2759dbe1a790S[email protected] return; 2760ce4c8fabSmatthias.ringwald } 2761ce4c8fabSmatthias.ringwald 2762b031bebbSmatthias.ringwald // send scan enable 27633a9fb326S[email protected] if (hci_stack->state == HCI_STATE_WORKING && hci_stack->new_scan_enable_value != 0xff && hci_classic_supported()){ 27643a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value); 27653a9fb326S[email protected] hci_stack->new_scan_enable_value = 0xff; 2766dbe1a790S[email protected] return; 2767b031bebbSmatthias.ringwald } 276835454696SMatthias Ringwald #endif 2769b031bebbSmatthias.ringwald 2770a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 2771b95a5a35SMatthias Ringwald // advertisements, active scanning, and creating connections requires randaom address to be set if using private address 2772b95a5a35SMatthias Ringwald if ((hci_stack->state == HCI_STATE_WORKING) 2773b95a5a35SMatthias Ringwald && (hci_stack->le_own_addr_type == BD_ADDR_TYPE_LE_PUBLIC || hci_stack->le_random_address_set)){ 2774d70217a2SMatthias Ringwald 2775d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 277645c102fdSMatthias Ringwald // handle le scan 27777bdc6798S[email protected] switch(hci_stack->le_scanning_state){ 27787bdc6798S[email protected] case LE_START_SCAN: 27797bdc6798S[email protected] hci_stack->le_scanning_state = LE_SCANNING; 27807bdc6798S[email protected] hci_send_cmd(&hci_le_set_scan_enable, 1, 0); 27817bdc6798S[email protected] return; 27827bdc6798S[email protected] 27837bdc6798S[email protected] case LE_STOP_SCAN: 27847bdc6798S[email protected] hci_stack->le_scanning_state = LE_SCAN_IDLE; 27857bdc6798S[email protected] hci_send_cmd(&hci_le_set_scan_enable, 0, 0); 27867bdc6798S[email protected] return; 27877bdc6798S[email protected] default: 27887bdc6798S[email protected] break; 27897bdc6798S[email protected] } 2790e2602ea2Smatthias.ringwald if (hci_stack->le_scan_type != 0xff){ 2791e2602ea2Smatthias.ringwald // defaults: active scanning, accept all advertisement packets 2792e2602ea2Smatthias.ringwald int scan_type = hci_stack->le_scan_type; 2793e2602ea2Smatthias.ringwald hci_stack->le_scan_type = 0xff; 2794b95a5a35SMatthias 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); 2795e2602ea2Smatthias.ringwald return; 2796e2602ea2Smatthias.ringwald } 2797d70217a2SMatthias Ringwald #endif 2798d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 279945c102fdSMatthias Ringwald // le advertisement control 28009a2e4658SMatthias Ringwald if (hci_stack->le_advertisements_todo){ 28019a2e4658SMatthias Ringwald log_info("hci_run: gap_le: adv todo: %x", hci_stack->le_advertisements_todo ); 28029a2e4658SMatthias Ringwald } 280345c102fdSMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_DISABLE){ 280445c102fdSMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_DISABLE; 280545c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 0); 280645c102fdSMatthias Ringwald return; 280745c102fdSMatthias Ringwald } 280845c102fdSMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_PARAMS){ 280945c102fdSMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_PARAMS; 281045c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_parameters, 281145c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_min, 281245c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_max, 281345c102fdSMatthias Ringwald hci_stack->le_advertisements_type, 2814b95a5a35SMatthias Ringwald hci_stack->le_own_addr_type, 281545c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address_type, 281645c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address, 281745c102fdSMatthias Ringwald hci_stack->le_advertisements_channel_map, 281845c102fdSMatthias Ringwald hci_stack->le_advertisements_filter_policy); 281945c102fdSMatthias Ringwald return; 282045c102fdSMatthias Ringwald } 2821501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_ADV_DATA){ 2822501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 2823b95a5a35SMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_data, hci_stack->le_advertisements_data_len, hci_stack->le_advertisements_data); 282445c102fdSMatthias Ringwald return; 282545c102fdSMatthias Ringwald } 2826501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA){ 2827501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 2828501f56b3SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_response_data, hci_stack->le_scan_response_data_len, 2829501f56b3SMatthias Ringwald hci_stack->le_scan_response_data); 2830501f56b3SMatthias Ringwald return; 2831501f56b3SMatthias Ringwald } 2832b95a5a35SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_ENABLE){ 283345c102fdSMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_ENABLE; 283445c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 1); 283545c102fdSMatthias Ringwald return; 283645c102fdSMatthias Ringwald } 2837d70217a2SMatthias Ringwald #endif 28389956955bSMatthias Ringwald 2839d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 28409956955bSMatthias Ringwald // 28419956955bSMatthias Ringwald // LE Whitelist Management 28429956955bSMatthias Ringwald // 28439956955bSMatthias Ringwald 28449956955bSMatthias Ringwald // check if whitelist needs modification 2845665d90f2SMatthias Ringwald btstack_linked_list_iterator_t lit; 28469956955bSMatthias Ringwald int modification_pending = 0; 2847665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 2848665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 2849665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 28509956955bSMatthias Ringwald if (entry->state & (LE_WHITELIST_REMOVE_FROM_CONTROLLER | LE_WHITELIST_ADD_TO_CONTROLLER)){ 28519956955bSMatthias Ringwald modification_pending = 1; 28529956955bSMatthias Ringwald break; 28539956955bSMatthias Ringwald } 28549956955bSMatthias Ringwald } 285591915b0bSMatthias Ringwald 28569956955bSMatthias Ringwald if (modification_pending){ 285791915b0bSMatthias Ringwald // stop connnecting if modification pending 28589956955bSMatthias Ringwald if (hci_stack->le_connecting_state != LE_CONNECTING_IDLE){ 28599956955bSMatthias Ringwald hci_send_cmd(&hci_le_create_connection_cancel); 28609956955bSMatthias Ringwald return; 28619956955bSMatthias Ringwald } 28629956955bSMatthias Ringwald 28639956955bSMatthias Ringwald // add/remove entries 2864665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 2865665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 2866665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 28679956955bSMatthias Ringwald if (entry->state & LE_WHITELIST_ADD_TO_CONTROLLER){ 28689956955bSMatthias Ringwald entry->state = LE_WHITELIST_ON_CONTROLLER; 28699956955bSMatthias Ringwald hci_send_cmd(&hci_le_add_device_to_white_list, entry->address_type, entry->address); 28709956955bSMatthias Ringwald return; 28719956955bSMatthias Ringwald 28729956955bSMatthias Ringwald } 28739956955bSMatthias Ringwald if (entry->state & LE_WHITELIST_REMOVE_FROM_CONTROLLER){ 28749ecbe201SMatthias Ringwald bd_addr_t address; 28759ecbe201SMatthias Ringwald bd_addr_type_t address_type = entry->address_type; 28769ecbe201SMatthias Ringwald memcpy(address, entry->address, 6); 2877665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry); 28789956955bSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 28799ecbe201SMatthias Ringwald hci_send_cmd(&hci_le_remove_device_from_white_list, address_type, address); 28809956955bSMatthias Ringwald return; 28819956955bSMatthias Ringwald } 28829956955bSMatthias Ringwald } 288391915b0bSMatthias Ringwald } 28849956955bSMatthias Ringwald 28859956955bSMatthias Ringwald // start connecting 288691915b0bSMatthias Ringwald if ( hci_stack->le_connecting_state == LE_CONNECTING_IDLE && 2887665d90f2SMatthias Ringwald !btstack_linked_list_empty(&hci_stack->le_whitelist)){ 28889956955bSMatthias Ringwald bd_addr_t null_addr; 28899956955bSMatthias Ringwald memset(null_addr, 0, 6); 28909956955bSMatthias Ringwald hci_send_cmd(&hci_le_create_connection, 28919956955bSMatthias Ringwald 0x0060, // scan interval: 60 ms 28929956955bSMatthias Ringwald 0x0030, // scan interval: 30 ms 28939956955bSMatthias Ringwald 1, // use whitelist 28949956955bSMatthias Ringwald 0, // peer address type 28959956955bSMatthias Ringwald null_addr, // peer bd addr 2896b95a5a35SMatthias Ringwald hci_stack->le_own_addr_type, // our addr type: 28979956955bSMatthias Ringwald 0x0008, // conn interval min 28989956955bSMatthias Ringwald 0x0018, // conn interval max 28999956955bSMatthias Ringwald 0, // conn latency 29009956955bSMatthias Ringwald 0x0048, // supervision timeout 29019956955bSMatthias Ringwald 0x0001, // min ce length 29029956955bSMatthias Ringwald 0x0001 // max ce length 29039956955bSMatthias Ringwald ); 29049956955bSMatthias Ringwald return; 29059956955bSMatthias Ringwald } 2906d70217a2SMatthias Ringwald #endif 29077bdc6798S[email protected] } 2908b2f949feS[email protected] #endif 29097bdc6798S[email protected] 291032ab9390Smatthias.ringwald // send pending HCI commands 2911665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 291205ae8de3SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 291332ab9390Smatthias.ringwald 29140bf6344aS[email protected] switch(connection->state){ 29150bf6344aS[email protected] case SEND_CREATE_CONNECTION: 29164f3229d8S[email protected] switch(connection->address_type){ 291735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 29184f3229d8S[email protected] case BD_ADDR_TYPE_CLASSIC: 29199da54300S[email protected] log_info("sending hci_create_connection"); 2920ad83dc6aS[email protected] hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 29214f3229d8S[email protected] break; 292235454696SMatthias Ringwald #endif 29234f3229d8S[email protected] default: 2924a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 2925d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 29269da54300S[email protected] log_info("sending hci_le_create_connection"); 29274f3229d8S[email protected] hci_send_cmd(&hci_le_create_connection, 2928b2571179Smatthias.ringwald 0x0060, // scan interval: 60 ms 2929b2571179Smatthias.ringwald 0x0030, // scan interval: 30 ms 29304f3229d8S[email protected] 0, // don't use whitelist 29314f3229d8S[email protected] connection->address_type, // peer address type 29324f3229d8S[email protected] connection->address, // peer bd addr 2933b95a5a35SMatthias Ringwald hci_stack->le_own_addr_type, // our addr type: 2934b2571179Smatthias.ringwald 0x0008, // conn interval min 2935b2571179Smatthias.ringwald 0x0018, // conn interval max 29364f3229d8S[email protected] 0, // conn latency 2937b2571179Smatthias.ringwald 0x0048, // supervision timeout 2938b2571179Smatthias.ringwald 0x0001, // min ce length 2939b2571179Smatthias.ringwald 0x0001 // max ce length 29404f3229d8S[email protected] ); 29414f3229d8S[email protected] 29424f3229d8S[email protected] connection->state = SENT_CREATE_CONNECTION; 2943b2f949feS[email protected] #endif 2944d70217a2SMatthias Ringwald #endif 29454f3229d8S[email protected] break; 29464f3229d8S[email protected] } 2947ad83dc6aS[email protected] return; 2948ad83dc6aS[email protected] 294935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 29500bf6344aS[email protected] case RECEIVED_CONNECTION_REQUEST: 295194e4aaa2SMatthias Ringwald log_info("sending hci_accept_connection_request, remote eSCO %u", connection->remote_supported_feature_eSCO); 295232ab9390Smatthias.ringwald connection->state = ACCEPTED_CONNECTION_REQUEST; 29535cf766e8SMatthias Ringwald connection->role = HCI_ROLE_SLAVE; 2954e35edcc1S[email protected] if (connection->address_type == BD_ADDR_TYPE_CLASSIC){ 295534d2123cS[email protected] hci_send_cmd(&hci_accept_connection_request, connection->address, 1); 2956e35edcc1S[email protected] } 2957dbe1a790S[email protected] return; 295835454696SMatthias Ringwald #endif 29590bf6344aS[email protected] 2960a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 2961d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 29620bf6344aS[email protected] case SEND_CANCEL_CONNECTION: 29630bf6344aS[email protected] connection->state = SENT_CANCEL_CONNECTION; 29640bf6344aS[email protected] hci_send_cmd(&hci_le_create_connection_cancel); 29650bf6344aS[email protected] return; 2966a6725849S[email protected] #endif 2967d70217a2SMatthias Ringwald #endif 29680bf6344aS[email protected] case SEND_DISCONNECT: 29690bf6344aS[email protected] connection->state = SENT_DISCONNECT; 29707851196eSmatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 29710bf6344aS[email protected] return; 29720bf6344aS[email protected] 29730bf6344aS[email protected] default: 29740bf6344aS[email protected] break; 2975c7e0c5f6Smatthias.ringwald } 2976c7e0c5f6Smatthias.ringwald 297735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 297832ab9390Smatthias.ringwald if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){ 29799da54300S[email protected] log_info("responding to link key request"); 298034d2123cS[email protected] connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST); 298132ab9390Smatthias.ringwald link_key_t link_key; 2982c77e8838S[email protected] link_key_type_t link_key_type; 2983a98592bcSMatthias Ringwald if ( hci_stack->link_key_db 2984a98592bcSMatthias Ringwald && hci_stack->link_key_db->get_link_key(connection->address, link_key, &link_key_type) 298534d2123cS[email protected] && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){ 29869ab95c90S[email protected] connection->link_key_type = link_key_type; 298732ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key); 298832ab9390Smatthias.ringwald } else { 298932ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 299032ab9390Smatthias.ringwald } 2991dbe1a790S[email protected] return; 299232ab9390Smatthias.ringwald } 29931d6b20aeS[email protected] 2994899283eaS[email protected] if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){ 29959da54300S[email protected] log_info("denying to pin request"); 2996899283eaS[email protected] connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST); 299734d2123cS[email protected] hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address); 29984c57c146S[email protected] return; 29994c57c146S[email protected] } 30004c57c146S[email protected] 3001dbe1a790S[email protected] if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){ 300234d2123cS[email protected] connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY); 300382d8f825S[email protected] log_info("IO Capability Request received, stack bondable %u, io cap %u", hci_stack->bondable, hci_stack->ssp_io_capability); 300482d8f825S[email protected] if (hci_stack->bondable && (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN)){ 3005106d6d11S[email protected] // tweak authentication requirements 30063a9fb326S[email protected] uint8_t authreq = hci_stack->ssp_authentication_requirement; 3007106d6d11S[email protected] if (connection->bonding_flags & BONDING_DEDICATED){ 30089faad3abS[email protected] authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 30099faad3abS[email protected] } 30109faad3abS[email protected] if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){ 30119faad3abS[email protected] authreq |= 1; 3012106d6d11S[email protected] } 30133a9fb326S[email protected] hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, NULL, authreq); 3014f8fb5f6eS[email protected] } else { 3015f8fb5f6eS[email protected] hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED); 3016f8fb5f6eS[email protected] } 3017dbe1a790S[email protected] return; 301832ab9390Smatthias.ringwald } 301932ab9390Smatthias.ringwald 3020dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){ 3021dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY); 302234d2123cS[email protected] hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address); 3023dbe1a790S[email protected] return; 3024dbe1a790S[email protected] } 3025dbe1a790S[email protected] 3026dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){ 3027dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY); 302834d2123cS[email protected] hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000); 3029dbe1a790S[email protected] return; 3030dbe1a790S[email protected] } 3031afd4e962S[email protected] 3032afd4e962S[email protected] if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){ 3033afd4e962S[email protected] connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES; 303434d2123cS[email protected] hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle); 30352bd8b7e7S[email protected] return; 30362bd8b7e7S[email protected] } 30372bd8b7e7S[email protected] 3038ad83dc6aS[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){ 3039ad83dc6aS[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE; 30401bd5283dS[email protected] connection->bonding_flags |= BONDING_EMIT_COMPLETE_ON_DISCONNECT; 304152d34f98S[email protected] hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // authentication done 3042ad83dc6aS[email protected] return; 3043ad83dc6aS[email protected] } 304476f27cffSMatthias Ringwald 304534d2123cS[email protected] if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){ 304634d2123cS[email protected] connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST; 304734d2123cS[email protected] hci_send_cmd(&hci_authentication_requested, connection->con_handle); 30482bd8b7e7S[email protected] return; 3049afd4e962S[email protected] } 305076f27cffSMatthias Ringwald 3051dce78009S[email protected] if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){ 3052dce78009S[email protected] connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST; 3053dce78009S[email protected] hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1); 3054dce78009S[email protected] return; 3055dce78009S[email protected] } 305676f27cffSMatthias Ringwald #endif 305776f27cffSMatthias Ringwald 305876f27cffSMatthias Ringwald if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){ 305976f27cffSMatthias Ringwald connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK; 306076f27cffSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005); // authentication failure 306176f27cffSMatthias Ringwald return; 306276f27cffSMatthias Ringwald } 3063da886c03S[email protected] 3064a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 3065da886c03S[email protected] if (connection->le_con_parameter_update_state == CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS){ 3066da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 3067da886c03S[email protected] 3068c37a3166S[email protected] uint16_t connection_interval_min = connection->le_conn_interval_min; 3069c37a3166S[email protected] connection->le_conn_interval_min = 0; 3070c37a3166S[email protected] hci_send_cmd(&hci_le_connection_update, connection->con_handle, connection_interval_min, 3071c37a3166S[email protected] connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout, 3072c37a3166S[email protected] 0x0000, 0xffff); 3073c37a3166S[email protected] } 3074c37a3166S[email protected] #endif 3075dbe1a790S[email protected] } 3076c7e0c5f6Smatthias.ringwald 307705ae8de3SMatthias Ringwald hci_connection_t * connection; 30783a9fb326S[email protected] switch (hci_stack->state){ 30793429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 308074b323a9SMatthias Ringwald hci_initializing_run(); 30813429f56bSmatthias.ringwald break; 3082c7e0c5f6Smatthias.ringwald 3083c7e0c5f6Smatthias.ringwald case HCI_STATE_HALTING: 3084c7e0c5f6Smatthias.ringwald 30859da54300S[email protected] log_info("HCI_STATE_HALTING"); 30869956955bSMatthias Ringwald 30879956955bSMatthias Ringwald // free whitelist entries 3088a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 3089d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 30909956955bSMatthias Ringwald { 3091665d90f2SMatthias Ringwald btstack_linked_list_iterator_t lit; 3092665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 3093665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 3094665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 3095665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry); 30969956955bSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 30979956955bSMatthias Ringwald } 30989956955bSMatthias Ringwald } 30999956955bSMatthias Ringwald #endif 3100d70217a2SMatthias Ringwald #endif 3101c7e0c5f6Smatthias.ringwald // close all open connections 31023a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 3103c7e0c5f6Smatthias.ringwald if (connection){ 3104711e6c80SMatthias Ringwald hci_con_handle_t con_handle = (uint16_t) connection->con_handle; 3105d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 310632ab9390Smatthias.ringwald 31078837e9efSMatthias Ringwald log_info("HCI_STATE_HALTING, connection %p, handle %u", connection, con_handle); 3108c7e0c5f6Smatthias.ringwald 31098837e9efSMatthias Ringwald // cancel all l2cap connections right away instead of waiting for disconnection complete event ... 31108837e9efSMatthias Ringwald hci_emit_disconnection_complete(con_handle, 0x16); // terminated by local host 31118837e9efSMatthias Ringwald 31128837e9efSMatthias Ringwald // ... which would be ignored anyway as we shutdown (free) the connection now 3113c7e0c5f6Smatthias.ringwald hci_shutdown_connection(connection); 31148837e9efSMatthias Ringwald 31158837e9efSMatthias Ringwald // finally, send the disconnect command 31168837e9efSMatthias Ringwald hci_send_cmd(&hci_disconnect, con_handle, 0x13); // remote closed connection 3117c7e0c5f6Smatthias.ringwald return; 3118c7e0c5f6Smatthias.ringwald } 31199da54300S[email protected] log_info("HCI_STATE_HALTING, calling off"); 3120c7e0c5f6Smatthias.ringwald 312172ea5239Smatthias.ringwald // switch mode 3122c7e0c5f6Smatthias.ringwald hci_power_control_off(); 31239418f9c9Smatthias.ringwald 31249da54300S[email protected] log_info("HCI_STATE_HALTING, emitting state"); 312572ea5239Smatthias.ringwald hci_emit_state(); 31269da54300S[email protected] log_info("HCI_STATE_HALTING, done"); 312772ea5239Smatthias.ringwald break; 3128c7e0c5f6Smatthias.ringwald 312972ea5239Smatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 31303a9fb326S[email protected] switch(hci_stack->substate) { 313174b323a9SMatthias Ringwald case HCI_FALLING_ASLEEP_DISCONNECT: 31329da54300S[email protected] log_info("HCI_STATE_FALLING_ASLEEP"); 313372ea5239Smatthias.ringwald // close all open connections 31343a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 313566da7044Smatthias.ringwald 3136423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 313766da7044Smatthias.ringwald // don't close connections, if H4 supports power management 3138d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 313966da7044Smatthias.ringwald connection = NULL; 314066da7044Smatthias.ringwald } 314166da7044Smatthias.ringwald #endif 314272ea5239Smatthias.ringwald if (connection){ 314332ab9390Smatthias.ringwald 314472ea5239Smatthias.ringwald // send disconnect 3145d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 314632ab9390Smatthias.ringwald 31479da54300S[email protected] log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u", connection, (uint16_t)connection->con_handle); 31486ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 314972ea5239Smatthias.ringwald 315072ea5239Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 315172ea5239Smatthias.ringwald hci_shutdown_connection(connection); 315272ea5239Smatthias.ringwald return; 315372ea5239Smatthias.ringwald } 315472ea5239Smatthias.ringwald 315592368cd3S[email protected] if (hci_classic_supported()){ 315689db417bSmatthias.ringwald // disable page and inquiry scan 3157d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 315832ab9390Smatthias.ringwald 31599da54300S[email protected] log_info("HCI_STATE_HALTING, disabling inq scans"); 31603a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan 316189db417bSmatthias.ringwald 316289db417bSmatthias.ringwald // continue in next sub state 316374b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE; 316489db417bSmatthias.ringwald break; 316592368cd3S[email protected] } 3166202c8a4cSMatthias Ringwald // no break - fall through for ble-only chips 316792368cd3S[email protected] 316874b323a9SMatthias Ringwald case HCI_FALLING_ASLEEP_COMPLETE: 31699da54300S[email protected] log_info("HCI_STATE_HALTING, calling sleep"); 3170423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 317128171530Smatthias.ringwald // don't actually go to sleep, if H4 supports power management 3172d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 317328171530Smatthias.ringwald // SLEEP MODE reached 31743a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 317528171530Smatthias.ringwald hci_emit_state(); 317628171530Smatthias.ringwald break; 317728171530Smatthias.ringwald } 317828171530Smatthias.ringwald #endif 317972ea5239Smatthias.ringwald // switch mode 31803a9fb326S[email protected] hci_power_control_sleep(); // changes hci_stack->state to SLEEP 3181c7e0c5f6Smatthias.ringwald hci_emit_state(); 318228171530Smatthias.ringwald break; 318328171530Smatthias.ringwald 318489db417bSmatthias.ringwald default: 318589db417bSmatthias.ringwald break; 318689db417bSmatthias.ringwald } 3187c7e0c5f6Smatthias.ringwald break; 3188c7e0c5f6Smatthias.ringwald 31893429f56bSmatthias.ringwald default: 31903429f56bSmatthias.ringwald break; 31911f504dbdSmatthias.ringwald } 31923429f56bSmatthias.ringwald } 319316833f0aSmatthias.ringwald 319431452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 319535454696SMatthias Ringwald // house-keeping 319635454696SMatthias Ringwald 319735454696SMatthias Ringwald if (IS_COMMAND(packet, hci_write_loopback_mode)){ 319835454696SMatthias Ringwald hci_stack->loopback_mode = packet[3]; 319935454696SMatthias Ringwald } 320035454696SMatthias Ringwald 320135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 3202c8e4258aSmatthias.ringwald bd_addr_t addr; 3203c8e4258aSmatthias.ringwald hci_connection_t * conn; 3204c8e4258aSmatthias.ringwald 3205c8e4258aSmatthias.ringwald // create_connection? 3206c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 3207724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 32089da54300S[email protected] log_info("Create_connection to %s", bd_addr_to_str(addr)); 3209c8e4258aSmatthias.ringwald 32102e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 3211ad83dc6aS[email protected] if (!conn){ 321296a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 321317f1ba2aSmatthias.ringwald if (!conn){ 321417f1ba2aSmatthias.ringwald // notify client that alloc failed 32152deddeceSMatthias Ringwald hci_emit_connection_complete(addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 321617f1ba2aSmatthias.ringwald return 0; // don't sent packet to controller 321717f1ba2aSmatthias.ringwald } 3218ad83dc6aS[email protected] conn->state = SEND_CREATE_CONNECTION; 3219ad83dc6aS[email protected] } 3220ad83dc6aS[email protected] log_info("conn state %u", conn->state); 3221ad83dc6aS[email protected] switch (conn->state){ 3222ad83dc6aS[email protected] // if connection active exists 3223ad83dc6aS[email protected] case OPEN: 322462bda3fbS[email protected] // and OPEN, emit connection complete command, don't send to controller 3225274dad91SMatthias Ringwald hci_emit_connection_complete(addr, conn->con_handle, 0); 322662bda3fbS[email protected] return 0; 3227ad83dc6aS[email protected] case SEND_CREATE_CONNECTION: 3228ad83dc6aS[email protected] // connection created by hci, e.g. dedicated bonding 3229ad83dc6aS[email protected] break; 3230ad83dc6aS[email protected] default: 3231ad83dc6aS[email protected] // otherwise, just ignore as it is already in the open process 3232ad83dc6aS[email protected] return 0; 3233ad83dc6aS[email protected] } 3234c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 3235c8e4258aSmatthias.ringwald } 323635454696SMatthias Ringwald 32377fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_reply)){ 32387fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY); 32397fde4af9Smatthias.ringwald } 32407fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){ 32417fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST); 32427fde4af9Smatthias.ringwald } 32437fde4af9Smatthias.ringwald 32448ef73945Smatthias.ringwald if (IS_COMMAND(packet, hci_delete_stored_link_key)){ 3245a98592bcSMatthias Ringwald if (hci_stack->link_key_db){ 3246724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 3247a98592bcSMatthias Ringwald hci_stack->link_key_db->delete_link_key(addr); 32488ef73945Smatthias.ringwald } 32498ef73945Smatthias.ringwald } 3250c8e4258aSmatthias.ringwald 32516724cd9eS[email protected] if (IS_COMMAND(packet, hci_pin_code_request_negative_reply) 32526724cd9eS[email protected] || IS_COMMAND(packet, hci_pin_code_request_reply)){ 3253724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 32542e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 32556724cd9eS[email protected] if (conn){ 32566724cd9eS[email protected] connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE); 32576724cd9eS[email protected] } 32586724cd9eS[email protected] } 32596724cd9eS[email protected] 32606724cd9eS[email protected] if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply) 32616724cd9eS[email protected] || IS_COMMAND(packet, hci_user_confirmation_request_reply) 32626724cd9eS[email protected] || IS_COMMAND(packet, hci_user_passkey_request_negative_reply) 32636724cd9eS[email protected] || IS_COMMAND(packet, hci_user_passkey_request_reply)) { 3264724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 32652e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 32666724cd9eS[email protected] if (conn){ 32676724cd9eS[email protected] connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE); 32686724cd9eS[email protected] } 32696724cd9eS[email protected] } 3270ee752bb8SMatthias Ringwald 3271ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 3272ee752bb8SMatthias Ringwald // setup_synchronous_connection? Voice setting at offset 22 3273ee752bb8SMatthias Ringwald if (IS_COMMAND(packet, hci_setup_synchronous_connection)){ 3274ee752bb8SMatthias Ringwald // TODO: compare to current setting if sco connection already active 3275ee752bb8SMatthias Ringwald hci_stack->sco_voice_setting_active = little_endian_read_16(packet, 15); 3276ee752bb8SMatthias Ringwald } 3277ee752bb8SMatthias Ringwald // accept_synchronus_connection? Voice setting at offset 18 3278ee752bb8SMatthias Ringwald if (IS_COMMAND(packet, hci_accept_synchronous_connection)){ 3279ee752bb8SMatthias Ringwald // TODO: compare to current setting if sco connection already active 3280ee752bb8SMatthias Ringwald hci_stack->sco_voice_setting_active = little_endian_read_16(packet, 19); 3281ee752bb8SMatthias Ringwald } 3282ee752bb8SMatthias Ringwald #endif 328335454696SMatthias Ringwald #endif 32844b3e1e19SMatthias Ringwald 3285a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 3286d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 328769a97523S[email protected] if (IS_COMMAND(packet, hci_le_set_random_address)){ 3288b95a5a35SMatthias Ringwald hci_stack->le_random_address_set = 1; 3289b95a5a35SMatthias Ringwald reverse_bd_addr(&packet[3], hci_stack->le_random_address); 3290d70217a2SMatthias Ringwald } 3291171293d3SMatthias Ringwald if (IS_COMMAND(packet, hci_le_set_advertise_enable)){ 3292171293d3SMatthias Ringwald hci_stack->le_advertisements_active = packet[3]; 3293171293d3SMatthias Ringwald } 3294d70217a2SMatthias Ringwald #endif 3295d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 3296b04dfa37SMatthias Ringwald if (IS_COMMAND(packet, hci_le_create_connection)){ 3297b04dfa37SMatthias Ringwald // white list used? 3298b04dfa37SMatthias Ringwald uint8_t initiator_filter_policy = packet[7]; 3299b04dfa37SMatthias Ringwald switch (initiator_filter_policy){ 3300b04dfa37SMatthias Ringwald case 0: 3301b04dfa37SMatthias Ringwald // whitelist not used 3302b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_DIRECT; 3303b04dfa37SMatthias Ringwald break; 3304b04dfa37SMatthias Ringwald case 1: 3305b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_WHITELIST; 3306b04dfa37SMatthias Ringwald break; 3307b04dfa37SMatthias Ringwald default: 3308b04dfa37SMatthias Ringwald log_error("Invalid initiator_filter_policy in LE Create Connection %u", initiator_filter_policy); 3309b04dfa37SMatthias Ringwald break; 3310b04dfa37SMatthias Ringwald } 3311b04dfa37SMatthias Ringwald } 3312b04dfa37SMatthias Ringwald if (IS_COMMAND(packet, hci_le_create_connection_cancel)){ 3313b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 3314b04dfa37SMatthias Ringwald } 331569a97523S[email protected] #endif 3316d70217a2SMatthias Ringwald #endif 331769a97523S[email protected] 33183a9fb326S[email protected] hci_stack->num_cmd_packets--; 33195bb5bc3eS[email protected] 33205bb5bc3eS[email protected] hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size); 33216b4af23dS[email protected] int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 33226b4af23dS[email protected] 3323d051460cS[email protected] // release packet buffer for synchronous transport implementations 3324c8b9416aS[email protected] if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){ 33256b4af23dS[email protected] hci_stack->hci_packet_buffer_reserved = 0; 33266b4af23dS[email protected] } 33276b4af23dS[email protected] 33286b4af23dS[email protected] return err; 332931452debSmatthias.ringwald } 33308adf0ddaSmatthias.ringwald 33312bd8b7e7S[email protected] // disconnect because of security block 33322bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){ 33332bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 33342bd8b7e7S[email protected] if (!connection) return; 33352bd8b7e7S[email protected] connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 33362bd8b7e7S[email protected] } 33372bd8b7e7S[email protected] 33382bd8b7e7S[email protected] 3339dbe1a790S[email protected] // Configure Secure Simple Pairing 3340dbe1a790S[email protected] 334135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 334235454696SMatthias Ringwald 3343dbe1a790S[email protected] // enable will enable SSP during init 334415a95bd5SMatthias Ringwald void gap_ssp_set_enable(int enable){ 33453a9fb326S[email protected] hci_stack->ssp_enable = enable; 3346dbe1a790S[email protected] } 3347dbe1a790S[email protected] 334895d71764SMatthias Ringwald static int hci_local_ssp_activated(void){ 334915a95bd5SMatthias Ringwald return gap_ssp_supported() && hci_stack->ssp_enable; 33502bd8b7e7S[email protected] } 33512bd8b7e7S[email protected] 3352dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement 335315a95bd5SMatthias Ringwald void gap_ssp_set_io_capability(int io_capability){ 33543a9fb326S[email protected] hci_stack->ssp_io_capability = io_capability; 3355dbe1a790S[email protected] } 335615a95bd5SMatthias Ringwald void gap_ssp_set_authentication_requirement(int authentication_requirement){ 33573a9fb326S[email protected] hci_stack->ssp_authentication_requirement = authentication_requirement; 3358dbe1a790S[email protected] } 3359dbe1a790S[email protected] 3360dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested 336115a95bd5SMatthias Ringwald void gap_ssp_set_auto_accept(int auto_accept){ 33623a9fb326S[email protected] hci_stack->ssp_auto_accept = auto_accept; 3363dbe1a790S[email protected] } 336435454696SMatthias Ringwald #endif 3365dbe1a790S[email protected] 336694be1a66SMatthias Ringwald // va_list part of hci_send_cmd 336794be1a66SMatthias Ringwald int hci_send_cmd_va_arg(const hci_cmd_t *cmd, va_list argptr){ 3368d94d3cafS[email protected] if (!hci_can_send_command_packet_now()){ 33699d14b626S[email protected] log_error("hci_send_cmd called but cannot send packet now"); 33709d14b626S[email protected] return 0; 33719d14b626S[email protected] } 33729d14b626S[email protected] 33735127cc62S[email protected] // for HCI INITIALIZATION 33749da54300S[email protected] // log_info("hci_send_cmd: opcode %04x", cmd->opcode); 33755127cc62S[email protected] hci_stack->last_cmd_opcode = cmd->opcode; 33765127cc62S[email protected] 33779d14b626S[email protected] hci_reserve_packet_buffer(); 33789d14b626S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 337994be1a66SMatthias Ringwald uint16_t size = hci_cmd_create_from_template(packet, cmd, argptr); 338094be1a66SMatthias Ringwald return hci_send_cmd_packet(packet, size); 338194be1a66SMatthias Ringwald } 33829d14b626S[email protected] 338394be1a66SMatthias Ringwald /** 338494be1a66SMatthias Ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 338594be1a66SMatthias Ringwald */ 338694be1a66SMatthias Ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){ 33871cd208adSmatthias.ringwald va_list argptr; 33881cd208adSmatthias.ringwald va_start(argptr, cmd); 338994be1a66SMatthias Ringwald int res = hci_send_cmd_va_arg(cmd, argptr); 33901cd208adSmatthias.ringwald va_end(argptr); 339194be1a66SMatthias Ringwald return res; 339293b8dc03Smatthias.ringwald } 3393c8e4258aSmatthias.ringwald 3394ee091cf1Smatthias.ringwald // Create various non-HCI events. 3395ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 3396ee091cf1Smatthias.ringwald 3397d6b06661SMatthias Ringwald static void hci_emit_event(uint8_t * event, uint16_t size, int dump){ 3398fb37a842SMatthias Ringwald // dump packet 3399d6b06661SMatthias Ringwald if (dump) { 3400300c1ba4SMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, size); 3401d6b06661SMatthias Ringwald } 34021ef6bb52SMatthias Ringwald 3403fb37a842SMatthias Ringwald // dispatch to all event handlers 34041ef6bb52SMatthias Ringwald btstack_linked_list_iterator_t it; 34051ef6bb52SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->event_handlers); 34061ef6bb52SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 34071ef6bb52SMatthias Ringwald btstack_packet_callback_registration_t * entry = (btstack_packet_callback_registration_t*) btstack_linked_list_iterator_next(&it); 3408d9a7306aSMatthias Ringwald entry->callback(HCI_EVENT_PACKET, 0, event, size); 34091ef6bb52SMatthias Ringwald } 3410d6b06661SMatthias Ringwald } 3411d6b06661SMatthias Ringwald 3412d6b06661SMatthias Ringwald static void hci_emit_acl_packet(uint8_t * packet, uint16_t size){ 3413fb37a842SMatthias Ringwald if (!hci_stack->acl_packet_handler) return; 34143d50b4baSMatthias Ringwald hci_stack->acl_packet_handler(HCI_ACL_DATA_PACKET, 0, packet, size); 3415d6b06661SMatthias Ringwald } 3416d6b06661SMatthias Ringwald 341735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 3418701e3307SMatthias Ringwald static void hci_notify_if_sco_can_send_now(void){ 34193bc639ceSMatthias Ringwald // notify SCO sender if waiting 3420701e3307SMatthias Ringwald if (!hci_stack->sco_waiting_for_can_send_now) return; 3421701e3307SMatthias Ringwald if (hci_can_send_sco_packet_now()){ 34223bc639ceSMatthias Ringwald hci_stack->sco_waiting_for_can_send_now = 0; 3423701e3307SMatthias Ringwald uint8_t event[2] = { HCI_EVENT_SCO_CAN_SEND_NOW, 0 }; 3424701e3307SMatthias Ringwald hci_dump_packet(HCI_EVENT_PACKET, 1, event, sizeof(event)); 34253d50b4baSMatthias Ringwald hci_stack->sco_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event)); 34263bc639ceSMatthias Ringwald } 34273bc639ceSMatthias Ringwald } 3428*1cfb383eSMatthias Ringwald 3429*1cfb383eSMatthias Ringwald // parsing end emitting has been merged to reduce code size 3430*1cfb383eSMatthias Ringwald static void gap_inquiry_explode(uint8_t * packet){ 3431*1cfb383eSMatthias Ringwald uint8_t event[15+GAP_INQUIRY_MAX_NAME_LEN]; 3432*1cfb383eSMatthias Ringwald 3433*1cfb383eSMatthias Ringwald uint8_t * eir_data; 3434*1cfb383eSMatthias Ringwald ad_context_t context; 3435*1cfb383eSMatthias Ringwald const uint8_t * name; 3436*1cfb383eSMatthias Ringwald uint8_t name_len; 3437*1cfb383eSMatthias Ringwald 3438*1cfb383eSMatthias Ringwald int event_type = hci_event_packet_get_type(packet); 3439*1cfb383eSMatthias Ringwald int num_reserved_fields = event_type == HCI_EVENT_INQUIRY_RESULT ? 2 : 1; // 2 for old event, 1 otherwise 3440*1cfb383eSMatthias Ringwald int num_responses = hci_event_inquiry_result_get_num_responses(packet); 3441*1cfb383eSMatthias Ringwald 3442*1cfb383eSMatthias Ringwald // event[1] is set at the end 3443*1cfb383eSMatthias Ringwald int i; 3444*1cfb383eSMatthias Ringwald for (i=0; i<num_responses;i++){ 3445*1cfb383eSMatthias Ringwald memset(event, 0, sizeof(event)); 3446*1cfb383eSMatthias Ringwald event[0] = GAP_EVENT_INQUIRY_RESULT; 3447*1cfb383eSMatthias Ringwald uint8_t event_size = 18; // if name is not set by EIR 3448*1cfb383eSMatthias Ringwald 3449*1cfb383eSMatthias Ringwald memcpy(&event[2], &packet[3 + i*6], 6); // bd_addr 3450*1cfb383eSMatthias Ringwald event[8] = packet[3 + num_responses*(6) + i*1]; // page_scan_repetition_mode 3451*1cfb383eSMatthias Ringwald memcpy(&event[9], &packet[3 + num_responses*(6+1+num_reserved_fields) + i*3], 3); // class of device 3452*1cfb383eSMatthias Ringwald memcpy(&event[12], &packet[3 + num_responses*(6+1+num_reserved_fields+3) + i*2], 2); // clock offset 3453*1cfb383eSMatthias Ringwald 3454*1cfb383eSMatthias Ringwald switch (event_type){ 3455*1cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 3456*1cfb383eSMatthias Ringwald // 14,15,16,17 = 0, size 18 3457*1cfb383eSMatthias Ringwald break; 3458*1cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 3459*1cfb383eSMatthias Ringwald event[14] = 1; 3460*1cfb383eSMatthias Ringwald event[15] = packet [3 + num_responses*(6+1+num_reserved_fields+3+2) + i*1]; // rssi 3461*1cfb383eSMatthias Ringwald // 16,17 = 0, size 18 3462*1cfb383eSMatthias Ringwald break; 3463*1cfb383eSMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 3464*1cfb383eSMatthias Ringwald event[14] = 1; 3465*1cfb383eSMatthias Ringwald event[15] = packet [3 + num_responses*(6+1+num_reserved_fields+3+2) + i*1]; // rssi 3466*1cfb383eSMatthias Ringwald // for EIR packets, there is only one reponse in it 3467*1cfb383eSMatthias Ringwald eir_data = &packet[3 + (6+1+num_reserved_fields+3+2+1)]; 3468*1cfb383eSMatthias Ringwald name = NULL; 3469*1cfb383eSMatthias Ringwald // EIR data is 240 bytes in EIR event 3470*1cfb383eSMatthias Ringwald for (ad_iterator_init(&context, 240, eir_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)){ 3471*1cfb383eSMatthias Ringwald uint8_t data_type = ad_iterator_get_data_type(&context); 3472*1cfb383eSMatthias Ringwald uint8_t data_size = ad_iterator_get_data_len(&context); 3473*1cfb383eSMatthias Ringwald const uint8_t * data = ad_iterator_get_data(&context); 3474*1cfb383eSMatthias Ringwald // Prefer Complete Local Name over Shortend Local Name 3475*1cfb383eSMatthias Ringwald switch (data_type){ 3476*1cfb383eSMatthias Ringwald case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME: 3477*1cfb383eSMatthias Ringwald if (name) continue; 3478*1cfb383eSMatthias Ringwald /* explicit fall-through */ 3479*1cfb383eSMatthias Ringwald case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME: 3480*1cfb383eSMatthias Ringwald name = data; 3481*1cfb383eSMatthias Ringwald name_len = data_size; 3482*1cfb383eSMatthias Ringwald break; 3483*1cfb383eSMatthias Ringwald default: 3484*1cfb383eSMatthias Ringwald break; 3485*1cfb383eSMatthias Ringwald } 3486*1cfb383eSMatthias Ringwald } 3487*1cfb383eSMatthias Ringwald if (name){ 3488*1cfb383eSMatthias Ringwald event[16] = 1; 3489*1cfb383eSMatthias Ringwald // truncate name if needed 3490*1cfb383eSMatthias Ringwald int len = btstack_min(name_len, GAP_INQUIRY_MAX_NAME_LEN); 3491*1cfb383eSMatthias Ringwald event[17] = len; 3492*1cfb383eSMatthias Ringwald memcpy(&event[18], name, len); 3493*1cfb383eSMatthias Ringwald event_size += len; 3494*1cfb383eSMatthias Ringwald } 3495*1cfb383eSMatthias Ringwald break; 3496*1cfb383eSMatthias Ringwald } 3497*1cfb383eSMatthias Ringwald event[1] = event_size - 2; 3498*1cfb383eSMatthias Ringwald hci_emit_event(event, event_size, 1); 3499*1cfb383eSMatthias Ringwald } 3500*1cfb383eSMatthias Ringwald } 350135454696SMatthias Ringwald #endif 35023bc639ceSMatthias Ringwald 350371de195eSMatthias Ringwald void hci_emit_state(void){ 35043a9fb326S[email protected] log_info("BTSTACK_EVENT_STATE %u", hci_stack->state); 3505425d1371Smatthias.ringwald uint8_t event[3]; 350680d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 3507425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 35083a9fb326S[email protected] event[2] = hci_stack->state; 3509d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3510c8e4258aSmatthias.ringwald } 3511c8e4258aSmatthias.ringwald 351235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 35132deddeceSMatthias Ringwald static void hci_emit_connection_complete(bd_addr_t address, hci_con_handle_t con_handle, uint8_t status){ 3514425d1371Smatthias.ringwald uint8_t event[13]; 3515c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 3516425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 351717f1ba2aSmatthias.ringwald event[2] = status; 35182deddeceSMatthias Ringwald little_endian_store_16(event, 3, con_handle); 35192deddeceSMatthias Ringwald reverse_bd_addr(address, &event[5]); 3520c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 3521c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 3522d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3523c8e4258aSmatthias.ringwald } 352452db98b2SMatthias Ringwald static void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 352552db98b2SMatthias Ringwald if (disable_l2cap_timeouts) return; 352652db98b2SMatthias Ringwald log_info("L2CAP_EVENT_TIMEOUT_CHECK"); 352752db98b2SMatthias Ringwald uint8_t event[4]; 352852db98b2SMatthias Ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 352952db98b2SMatthias Ringwald event[1] = sizeof(event) - 2; 353052db98b2SMatthias Ringwald little_endian_store_16(event, 2, conn->con_handle); 353152db98b2SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 353252db98b2SMatthias Ringwald } 353335454696SMatthias Ringwald #endif 3534c8e4258aSmatthias.ringwald 353535454696SMatthias Ringwald #ifdef ENABLE_BLE 3536d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 3537fc64f94aSMatthias 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){ 35384f3229d8S[email protected] uint8_t event[21]; 35394f3229d8S[email protected] event[0] = HCI_EVENT_LE_META; 35404f3229d8S[email protected] event[1] = sizeof(event) - 2; 35414f3229d8S[email protected] event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE; 35424f3229d8S[email protected] event[3] = status; 3543fc64f94aSMatthias Ringwald little_endian_store_16(event, 4, con_handle); 35444f3229d8S[email protected] event[6] = 0; // TODO: role 35456e2e9a6bS[email protected] event[7] = address_type; 3546724d70a2SMatthias Ringwald reverse_bd_addr(address, &event[8]); 3547f8fbdce0SMatthias Ringwald little_endian_store_16(event, 14, 0); // interval 3548f8fbdce0SMatthias Ringwald little_endian_store_16(event, 16, 0); // latency 3549f8fbdce0SMatthias Ringwald little_endian_store_16(event, 18, 0); // supervision timeout 35504f3229d8S[email protected] event[20] = 0; // master clock accuracy 3551d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 35524f3229d8S[email protected] } 355335454696SMatthias Ringwald #endif 3554d70217a2SMatthias Ringwald #endif 35554f3229d8S[email protected] 3556fc64f94aSMatthias Ringwald static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason){ 3557425d1371Smatthias.ringwald uint8_t event[6]; 35583c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 3559e518c4b8Smatthias.ringwald event[1] = sizeof(event) - 2; 35603c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 3561fc64f94aSMatthias Ringwald little_endian_store_16(event, 3, con_handle); 35623c4d4b90Smatthias.ringwald event[5] = reason; 3563d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 35643c4d4b90Smatthias.ringwald } 35653c4d4b90Smatthias.ringwald 3566b83d5eabSMatthias Ringwald static void hci_emit_nr_connections_changed(void){ 3567e0abb8e7S[email protected] log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections()); 3568425d1371Smatthias.ringwald uint8_t event[3]; 356980d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 3570425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 357143bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 3572d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 357343bfb1bdSmatthias.ringwald } 3574038bc64cSmatthias.ringwald 3575b83d5eabSMatthias Ringwald static void hci_emit_hci_open_failed(void){ 3576e0abb8e7S[email protected] log_info("BTSTACK_EVENT_POWERON_FAILED"); 3577425d1371Smatthias.ringwald uint8_t event[2]; 357880d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 3579425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 3580d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3581038bc64cSmatthias.ringwald } 35821b0e3922Smatthias.ringwald 358335454696SMatthias Ringwald static void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status){ 358435454696SMatthias Ringwald log_info("hci_emit_dedicated_bonding_result %u ", status); 358535454696SMatthias Ringwald uint8_t event[9]; 358635454696SMatthias Ringwald int pos = 0; 358735454696SMatthias Ringwald event[pos++] = GAP_EVENT_DEDICATED_BONDING_COMPLETED; 358835454696SMatthias Ringwald event[pos++] = sizeof(event) - 2; 358935454696SMatthias Ringwald event[pos++] = status; 359035454696SMatthias Ringwald reverse_bd_addr(address, &event[pos]); 3591d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3592381fbed8Smatthias.ringwald } 3593458bf4e8S[email protected] 359435454696SMatthias Ringwald 359535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 359635454696SMatthias Ringwald 3597b83d5eabSMatthias Ringwald static void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){ 3598df3354fcS[email protected] log_info("hci_emit_security_level %u for handle %x", level, con_handle); 3599a00031e2S[email protected] uint8_t event[5]; 3600e00caf9cS[email protected] int pos = 0; 36015611a760SMatthias Ringwald event[pos++] = GAP_EVENT_SECURITY_LEVEL; 3602e00caf9cS[email protected] event[pos++] = sizeof(event) - 2; 3603f8fbdce0SMatthias Ringwald little_endian_store_16(event, 2, con_handle); 3604e00caf9cS[email protected] pos += 2; 3605e00caf9cS[email protected] event[pos++] = level; 3606d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3607e00caf9cS[email protected] } 3608e00caf9cS[email protected] 360935454696SMatthias Ringwald static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){ 361035454696SMatthias Ringwald if (!connection) return LEVEL_0; 361135454696SMatthias Ringwald if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0; 361235454696SMatthias Ringwald return gap_security_level_for_link_key_type(connection->link_key_type); 361335454696SMatthias Ringwald } 361435454696SMatthias Ringwald 361535454696SMatthias Ringwald static void hci_emit_discoverable_enabled(uint8_t enabled){ 361635454696SMatthias Ringwald log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled); 361735454696SMatthias Ringwald uint8_t event[3]; 361835454696SMatthias Ringwald event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED; 361935454696SMatthias Ringwald event[1] = sizeof(event) - 2; 362035454696SMatthias Ringwald event[2] = enabled; 3621d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3622ad83dc6aS[email protected] } 3623ad83dc6aS[email protected] 362406b9e820SMatthias Ringwald #ifdef ENABLE_CLASSIC 362598a2fd1cSMatthias Ringwald // query if remote side supports eSCO 3626073bd0faSMatthias Ringwald int hci_remote_esco_supported(hci_con_handle_t con_handle){ 362798a2fd1cSMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 362898a2fd1cSMatthias Ringwald if (!connection) return 0; 362998a2fd1cSMatthias Ringwald return connection->remote_supported_feature_eSCO; 363098a2fd1cSMatthias Ringwald } 363198a2fd1cSMatthias Ringwald 36322bd8b7e7S[email protected] // query if remote side supports SSP 36332bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){ 36342bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 36352bd8b7e7S[email protected] if (!connection) return 0; 36362bd8b7e7S[email protected] return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0; 36372bd8b7e7S[email protected] } 36382bd8b7e7S[email protected] 363915a95bd5SMatthias Ringwald int gap_ssp_supported_on_both_sides(hci_con_handle_t handle){ 3640df3354fcS[email protected] return hci_local_ssp_activated() && hci_remote_ssp_supported(handle); 3641df3354fcS[email protected] } 364206b9e820SMatthias Ringwald #endif 3643df3354fcS[email protected] 3644458bf4e8S[email protected] // GAP API 3645458bf4e8S[email protected] /** 3646458bf4e8S[email protected] * @bbrief enable/disable bonding. default is enabled 3647458bf4e8S[email protected] * @praram enabled 3648458bf4e8S[email protected] */ 36494c57c146S[email protected] void gap_set_bondable_mode(int enable){ 36503a9fb326S[email protected] hci_stack->bondable = enable ? 1 : 0; 3651458bf4e8S[email protected] } 36524ef6443cSMatthias Ringwald /** 36534ef6443cSMatthias Ringwald * @brief Get bondable mode. 36544ef6443cSMatthias Ringwald * @return 1 if bondable 36554ef6443cSMatthias Ringwald */ 36564ef6443cSMatthias Ringwald int gap_get_bondable_mode(void){ 36574ef6443cSMatthias Ringwald return hci_stack->bondable; 36584ef6443cSMatthias Ringwald } 3659cb230b9dS[email protected] 3660cb230b9dS[email protected] /** 366134d2123cS[email protected] * @brief map link keys to security levels 3662cb230b9dS[email protected] */ 366334d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){ 366434d2123cS[email protected] switch (link_key_type){ 36653c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 36663c68dfa9S[email protected] return LEVEL_4; 36673c68dfa9S[email protected] case COMBINATION_KEY: 36683c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 36693c68dfa9S[email protected] return LEVEL_3; 36703c68dfa9S[email protected] default: 36713c68dfa9S[email protected] return LEVEL_2; 36723c68dfa9S[email protected] } 3673cb230b9dS[email protected] } 3674cb230b9dS[email protected] 3675106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){ 36765127cc62S[email protected] log_info("gap_mitm_protection_required_for_security_level %u", level); 3677106d6d11S[email protected] return level > LEVEL_2; 3678106d6d11S[email protected] } 3679106d6d11S[email protected] 368034d2123cS[email protected] /** 368134d2123cS[email protected] * @brief get current security level 368234d2123cS[email protected] */ 368334d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){ 368434d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 368534d2123cS[email protected] if (!connection) return LEVEL_0; 368634d2123cS[email protected] return gap_security_level_for_connection(connection); 368734d2123cS[email protected] } 368834d2123cS[email protected] 3689cb230b9dS[email protected] /** 3690cb230b9dS[email protected] * @brief request connection to device to 3691cb230b9dS[email protected] * @result GAP_AUTHENTICATION_RESULT 3692cb230b9dS[email protected] */ 369334d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){ 369434d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 369534d2123cS[email protected] if (!connection){ 3696a00031e2S[email protected] hci_emit_security_level(con_handle, LEVEL_0); 369734d2123cS[email protected] return; 369834d2123cS[email protected] } 369934d2123cS[email protected] gap_security_level_t current_level = gap_security_level(con_handle); 370034d2123cS[email protected] log_info("gap_request_security_level %u, current level %u", requested_level, current_level); 370134d2123cS[email protected] if (current_level >= requested_level){ 3702a00031e2S[email protected] hci_emit_security_level(con_handle, current_level); 370334d2123cS[email protected] return; 370434d2123cS[email protected] } 3705a00031e2S[email protected] 370634d2123cS[email protected] connection->requested_security_level = requested_level; 3707a00031e2S[email protected] 370825bf5872S[email protected] #if 0 370925bf5872S[email protected] // sending encryption request without a link key results in an error. 371025bf5872S[email protected] // TODO: figure out how to use it properly 371125bf5872S[email protected] 3712fb8ba0dbS[email protected] // would enabling ecnryption suffice (>= LEVEL_2)? 3713a98592bcSMatthias Ringwald if (hci_stack->link_key_db){ 3714a00031e2S[email protected] link_key_type_t link_key_type; 3715a00031e2S[email protected] link_key_t link_key; 3716a98592bcSMatthias Ringwald if (hci_stack->link_key_db->get_link_key( &connection->address, &link_key, &link_key_type)){ 3717a00031e2S[email protected] if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){ 3718a00031e2S[email protected] connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 3719a00031e2S[email protected] return; 3720a00031e2S[email protected] } 3721a00031e2S[email protected] } 3722a00031e2S[email protected] } 372325bf5872S[email protected] #endif 3724a00031e2S[email protected] 37251eb2563eS[email protected] // try to authenticate connection 37261eb2563eS[email protected] connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 3727e80b2cf9S[email protected] hci_run(); 3728e00caf9cS[email protected] } 3729ad83dc6aS[email protected] 3730ad83dc6aS[email protected] /** 3731ad83dc6aS[email protected] * @brief start dedicated bonding with device. disconnect after bonding 3732ad83dc6aS[email protected] * @param device 3733ad83dc6aS[email protected] * @param request MITM protection 3734ad83dc6aS[email protected] * @result GAP_DEDICATED_BONDING_COMPLETE 3735ad83dc6aS[email protected] */ 3736ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){ 3737ad83dc6aS[email protected] 3738ad83dc6aS[email protected] // create connection state machine 373996a45072S[email protected] hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_CLASSIC); 3740ad83dc6aS[email protected] 3741ad83dc6aS[email protected] if (!connection){ 3742ad83dc6aS[email protected] return BTSTACK_MEMORY_ALLOC_FAILED; 3743ad83dc6aS[email protected] } 3744ad83dc6aS[email protected] 3745ad83dc6aS[email protected] // delete linkn key 374615a95bd5SMatthias Ringwald gap_drop_link_key_for_bd_addr(device); 3747ad83dc6aS[email protected] 3748ad83dc6aS[email protected] // configure LEVEL_2/3, dedicated bonding 3749ad83dc6aS[email protected] connection->state = SEND_CREATE_CONNECTION; 3750ad83dc6aS[email protected] connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2; 3751f04a0c31SMatthias Ringwald log_info("gap_dedicated_bonding, mitm %d -> level %u", mitm_protection_required, connection->requested_security_level); 3752ad83dc6aS[email protected] connection->bonding_flags = BONDING_DEDICATED; 3753ad83dc6aS[email protected] 3754ad83dc6aS[email protected] // wait for GAP Security Result and send GAP Dedicated Bonding complete 3755ad83dc6aS[email protected] 3756ad83dc6aS[email protected] // handle: connnection failure (connection complete != ok) 3757ad83dc6aS[email protected] // handle: authentication failure 3758ad83dc6aS[email protected] // handle: disconnect on done 3759ad83dc6aS[email protected] 3760ad83dc6aS[email protected] hci_run(); 3761ad83dc6aS[email protected] 3762ad83dc6aS[email protected] return 0; 3763ad83dc6aS[email protected] } 376435454696SMatthias Ringwald #endif 37658e618f72S[email protected] 37668e618f72S[email protected] void gap_set_local_name(const char * local_name){ 37678e618f72S[email protected] hci_stack->local_name = local_name; 37688e618f72S[email protected] } 37698e618f72S[email protected] 377035454696SMatthias Ringwald 377135454696SMatthias Ringwald #ifdef ENABLE_BLE 377235454696SMatthias Ringwald 3773d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 3774d8e8f12aSMatthias Ringwald void gap_start_scan(void){ 3775d8e8f12aSMatthias Ringwald if (hci_stack->le_scanning_state == LE_SCANNING) return; 37767bdc6798S[email protected] hci_stack->le_scanning_state = LE_START_SCAN; 37777bdc6798S[email protected] hci_run(); 37787bdc6798S[email protected] } 37798e618f72S[email protected] 3780d8e8f12aSMatthias Ringwald void gap_stop_scan(void){ 3781d8e8f12aSMatthias Ringwald if ( hci_stack->le_scanning_state == LE_SCAN_IDLE) return; 37827bdc6798S[email protected] hci_stack->le_scanning_state = LE_STOP_SCAN; 37837bdc6798S[email protected] hci_run(); 37847bdc6798S[email protected] } 37854f3229d8S[email protected] 3786d8e8f12aSMatthias Ringwald void gap_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){ 3787ef11999fSmatthias.ringwald hci_stack->le_scan_type = scan_type; 3788ef11999fSmatthias.ringwald hci_stack->le_scan_interval = scan_interval; 3789ef11999fSmatthias.ringwald hci_stack->le_scan_window = scan_window; 3790ef11999fSmatthias.ringwald hci_run(); 3791ef11999fSmatthias.ringwald } 37924f3229d8S[email protected] 3793d8e8f12aSMatthias Ringwald uint8_t gap_connect(bd_addr_t addr, bd_addr_type_t addr_type){ 37944f3229d8S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 37954f3229d8S[email protected] if (!conn){ 3796d8e8f12aSMatthias Ringwald log_info("gap_connect: no connection exists yet, creating context"); 37972e77e513S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 37984f3229d8S[email protected] if (!conn){ 37994f3229d8S[email protected] // notify client that alloc failed 38006e2e9a6bS[email protected] hci_emit_le_connection_complete(addr_type, addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 3801d8e8f12aSMatthias Ringwald log_info("gap_connect: failed to alloc hci_connection_t"); 3802472a5742SMatthias Ringwald return GATT_CLIENT_NOT_CONNECTED; // don't sent packet to controller 38034f3229d8S[email protected] } 38044f3229d8S[email protected] conn->state = SEND_CREATE_CONNECTION; 3805d8e8f12aSMatthias Ringwald log_info("gap_connect: send create connection next"); 3806564fca32S[email protected] hci_run(); 3807616edd56SMatthias Ringwald return 0; 38084f3229d8S[email protected] } 38090bf6344aS[email protected] 38100bf6344aS[email protected] if (!hci_is_le_connection(conn) || 38110bf6344aS[email protected] conn->state == SEND_CREATE_CONNECTION || 38120bf6344aS[email protected] conn->state == SENT_CREATE_CONNECTION) { 38132e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_COMMAND_DISALLOWED); 3814d8e8f12aSMatthias Ringwald log_error("gap_connect: classic connection or connect is already being created"); 3815616edd56SMatthias Ringwald return GATT_CLIENT_IN_WRONG_STATE; 38160bf6344aS[email protected] } 38170bf6344aS[email protected] 3818d8e8f12aSMatthias Ringwald log_info("gap_connect: context exists with state %u", conn->state); 38192e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, conn->con_handle, 0); 38204f3229d8S[email protected] hci_run(); 3821616edd56SMatthias Ringwald return 0; 38224f3229d8S[email protected] } 38234f3229d8S[email protected] 38247851196eSmatthias.ringwald // @assumption: only a single outgoing LE Connection exists 3825d8e8f12aSMatthias Ringwald static hci_connection_t * gap_get_outgoing_connection(void){ 3826665d90f2SMatthias Ringwald btstack_linked_item_t *it; 3827665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 38280bf6344aS[email protected] hci_connection_t * conn = (hci_connection_t *) it; 38290bf6344aS[email protected] if (!hci_is_le_connection(conn)) continue; 38300bf6344aS[email protected] switch (conn->state){ 3831a6725849S[email protected] case SEND_CREATE_CONNECTION: 38327851196eSmatthias.ringwald case SENT_CREATE_CONNECTION: 38337851196eSmatthias.ringwald return conn; 38347851196eSmatthias.ringwald default: 38357851196eSmatthias.ringwald break; 38367851196eSmatthias.ringwald }; 38377851196eSmatthias.ringwald } 38387851196eSmatthias.ringwald return NULL; 38397851196eSmatthias.ringwald } 38407851196eSmatthias.ringwald 3841d8e8f12aSMatthias Ringwald uint8_t gap_connect_cancel(void){ 3842d8e8f12aSMatthias Ringwald hci_connection_t * conn = gap_get_outgoing_connection(); 3843616edd56SMatthias Ringwald if (!conn) return 0; 38447851196eSmatthias.ringwald switch (conn->state){ 38457851196eSmatthias.ringwald case SEND_CREATE_CONNECTION: 38467851196eSmatthias.ringwald // skip sending create connection and emit event instead 38472e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER); 3848665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 38497851196eSmatthias.ringwald btstack_memory_hci_connection_free( conn ); 38500bf6344aS[email protected] break; 3851a6725849S[email protected] case SENT_CREATE_CONNECTION: 38527851196eSmatthias.ringwald // request to send cancel connection 38530bf6344aS[email protected] conn->state = SEND_CANCEL_CONNECTION; 38540bf6344aS[email protected] hci_run(); 38550bf6344aS[email protected] break; 38560bf6344aS[email protected] default: 38570bf6344aS[email protected] break; 38580bf6344aS[email protected] } 3859616edd56SMatthias Ringwald return 0; 3860e31f89a7S[email protected] } 3861d70217a2SMatthias Ringwald #endif 38624f3229d8S[email protected] 3863c37a3166S[email protected] /** 3864c37a3166S[email protected] * @brief Updates the connection parameters for a given LE connection 3865c37a3166S[email protected] * @param handle 3866c37a3166S[email protected] * @param conn_interval_min (unit: 1.25ms) 3867c37a3166S[email protected] * @param conn_interval_max (unit: 1.25ms) 3868c37a3166S[email protected] * @param conn_latency 3869c37a3166S[email protected] * @param supervision_timeout (unit: 10ms) 3870c37a3166S[email protected] * @returns 0 if ok 3871c37a3166S[email protected] */ 3872c37a3166S[email protected] int gap_update_connection_parameters(hci_con_handle_t con_handle, uint16_t conn_interval_min, 3873c37a3166S[email protected] uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 3874c37a3166S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 3875c37a3166S[email protected] if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3876c37a3166S[email protected] connection->le_conn_interval_min = conn_interval_min; 3877c37a3166S[email protected] connection->le_conn_interval_max = conn_interval_max; 3878c37a3166S[email protected] connection->le_conn_latency = conn_latency; 3879c37a3166S[email protected] connection->le_supervision_timeout = supervision_timeout; 388084cf6d83SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS; 3881cfc59f1bSMatthias Ringwald hci_run(); 3882c37a3166S[email protected] return 0; 3883c37a3166S[email protected] } 3884c37a3166S[email protected] 388545c102fdSMatthias Ringwald /** 3886b68d7bc3SMatthias Ringwald * @brief Request an update of the connection parameter for a given LE connection 3887b68d7bc3SMatthias Ringwald * @param handle 3888b68d7bc3SMatthias Ringwald * @param conn_interval_min (unit: 1.25ms) 3889b68d7bc3SMatthias Ringwald * @param conn_interval_max (unit: 1.25ms) 3890b68d7bc3SMatthias Ringwald * @param conn_latency 3891b68d7bc3SMatthias Ringwald * @param supervision_timeout (unit: 10ms) 3892b68d7bc3SMatthias Ringwald * @returns 0 if ok 3893b68d7bc3SMatthias Ringwald */ 3894b68d7bc3SMatthias Ringwald int gap_request_connection_parameter_update(hci_con_handle_t con_handle, uint16_t conn_interval_min, 3895b68d7bc3SMatthias Ringwald uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 3896b68d7bc3SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 3897b68d7bc3SMatthias Ringwald if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3898b68d7bc3SMatthias Ringwald connection->le_conn_interval_min = conn_interval_min; 3899b68d7bc3SMatthias Ringwald connection->le_conn_interval_max = conn_interval_max; 3900b68d7bc3SMatthias Ringwald connection->le_conn_latency = conn_latency; 3901b68d7bc3SMatthias Ringwald connection->le_supervision_timeout = supervision_timeout; 3902b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_REQUEST; 3903b68d7bc3SMatthias Ringwald hci_run(); 3904b68d7bc3SMatthias Ringwald return 0; 3905b68d7bc3SMatthias Ringwald } 3906b68d7bc3SMatthias Ringwald 3907d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 3908d70217a2SMatthias Ringwald 3909501f56b3SMatthias Ringwald static void gap_advertisments_changed(void){ 3910501f56b3SMatthias Ringwald // disable advertisements before updating adv, scan data, or adv params 3911501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_active){ 3912501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_DISABLE | LE_ADVERTISEMENT_TASKS_ENABLE; 3913501f56b3SMatthias Ringwald } 3914501f56b3SMatthias Ringwald hci_run(); 3915501f56b3SMatthias Ringwald } 3916501f56b3SMatthias Ringwald 3917b68d7bc3SMatthias Ringwald /** 391845c102fdSMatthias Ringwald * @brief Set Advertisement Data 391945c102fdSMatthias Ringwald * @param advertising_data_length 392045c102fdSMatthias Ringwald * @param advertising_data (max 31 octets) 392145c102fdSMatthias Ringwald * @note data is not copied, pointer has to stay valid 392245c102fdSMatthias Ringwald */ 392345c102fdSMatthias Ringwald void gap_advertisements_set_data(uint8_t advertising_data_length, uint8_t * advertising_data){ 392445c102fdSMatthias Ringwald hci_stack->le_advertisements_data_len = advertising_data_length; 392545c102fdSMatthias Ringwald hci_stack->le_advertisements_data = advertising_data; 3926501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 3927501f56b3SMatthias Ringwald gap_advertisments_changed(); 392845c102fdSMatthias Ringwald } 3929501f56b3SMatthias Ringwald 3930501f56b3SMatthias Ringwald /** 3931501f56b3SMatthias Ringwald * @brief Set Scan Response Data 3932501f56b3SMatthias Ringwald * @param advertising_data_length 3933501f56b3SMatthias Ringwald * @param advertising_data (max 31 octets) 3934501f56b3SMatthias Ringwald * @note data is not copied, pointer has to stay valid 3935501f56b3SMatthias Ringwald */ 3936501f56b3SMatthias Ringwald void gap_scan_response_set_data(uint8_t scan_response_data_length, uint8_t * scan_response_data){ 3937501f56b3SMatthias Ringwald hci_stack->le_scan_response_data_len = scan_response_data_length; 3938501f56b3SMatthias Ringwald hci_stack->le_scan_response_data = scan_response_data; 3939501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 3940501f56b3SMatthias Ringwald gap_advertisments_changed(); 394145c102fdSMatthias Ringwald } 394245c102fdSMatthias Ringwald 394345c102fdSMatthias Ringwald /** 394445c102fdSMatthias Ringwald * @brief Set Advertisement Parameters 394545c102fdSMatthias Ringwald * @param adv_int_min 394645c102fdSMatthias Ringwald * @param adv_int_max 394745c102fdSMatthias Ringwald * @param adv_type 394845c102fdSMatthias Ringwald * @param direct_address_type 394945c102fdSMatthias Ringwald * @param direct_address 395045c102fdSMatthias Ringwald * @param channel_map 395145c102fdSMatthias Ringwald * @param filter_policy 395245c102fdSMatthias Ringwald * 395345c102fdSMatthias Ringwald * @note internal use. use gap_advertisements_set_params from gap_le.h instead. 395445c102fdSMatthias Ringwald */ 395545c102fdSMatthias Ringwald void hci_le_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type, 3956b95a5a35SMatthias Ringwald uint8_t direct_address_typ, bd_addr_t direct_address, 395745c102fdSMatthias Ringwald uint8_t channel_map, uint8_t filter_policy) { 395845c102fdSMatthias Ringwald 395945c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_min = adv_int_min; 396045c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_max = adv_int_max; 396145c102fdSMatthias Ringwald hci_stack->le_advertisements_type = adv_type; 396245c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address_type = direct_address_typ; 396345c102fdSMatthias Ringwald hci_stack->le_advertisements_channel_map = channel_map; 396445c102fdSMatthias Ringwald hci_stack->le_advertisements_filter_policy = filter_policy; 396545c102fdSMatthias Ringwald memcpy(hci_stack->le_advertisements_direct_address, direct_address, 6); 396645c102fdSMatthias Ringwald 396745c102fdSMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 3968501f56b3SMatthias Ringwald gap_advertisments_changed(); 396945c102fdSMatthias Ringwald } 397045c102fdSMatthias Ringwald 397145c102fdSMatthias Ringwald /** 397245c102fdSMatthias Ringwald * @brief Enable/Disable Advertisements 397345c102fdSMatthias Ringwald * @param enabled 397445c102fdSMatthias Ringwald */ 397545c102fdSMatthias Ringwald void gap_advertisements_enable(int enabled){ 397645c102fdSMatthias Ringwald hci_stack->le_advertisements_enabled = enabled; 397745c102fdSMatthias Ringwald if (enabled && !hci_stack->le_advertisements_active){ 397845c102fdSMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_ENABLE; 397945c102fdSMatthias Ringwald } 398045c102fdSMatthias Ringwald if (!enabled && hci_stack->le_advertisements_active){ 398145c102fdSMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_DISABLE; 398245c102fdSMatthias Ringwald } 3983cfc59f1bSMatthias Ringwald hci_run(); 398445c102fdSMatthias Ringwald } 398545c102fdSMatthias Ringwald 398635454696SMatthias Ringwald #endif 398706e5cf96SMatthias Ringwald 398806e5cf96SMatthias Ringwald void hci_le_set_own_address_type(uint8_t own_address_type){ 398906e5cf96SMatthias Ringwald log_info("hci_le_set_own_address_type: old %u, new %u", hci_stack->le_own_addr_type, own_address_type); 399006e5cf96SMatthias Ringwald if (own_address_type == hci_stack->le_own_addr_type) return; 399106e5cf96SMatthias Ringwald hci_stack->le_own_addr_type = own_address_type; 399206e5cf96SMatthias Ringwald 399364068776SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 399406e5cf96SMatthias Ringwald // update advertisement parameters, too 399506e5cf96SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 399606e5cf96SMatthias Ringwald gap_advertisments_changed(); 399764068776SMatthias Ringwald #endif 399864068776SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 399906e5cf96SMatthias Ringwald // note: we don't update scan parameters or modify ongoing connection attempts 400064068776SMatthias Ringwald #endif 400106e5cf96SMatthias Ringwald } 400206e5cf96SMatthias Ringwald 4003d70217a2SMatthias Ringwald #endif 400445c102fdSMatthias Ringwald 4005616edd56SMatthias Ringwald uint8_t gap_disconnect(hci_con_handle_t handle){ 40065917a5c5S[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 40075917a5c5S[email protected] if (!conn){ 40087851196eSmatthias.ringwald hci_emit_disconnection_complete(handle, 0); 4009616edd56SMatthias Ringwald return 0; 40105917a5c5S[email protected] } 40115917a5c5S[email protected] conn->state = SEND_DISCONNECT; 40125917a5c5S[email protected] hci_run(); 4013616edd56SMatthias Ringwald return 0; 40144f3229d8S[email protected] } 401504a6ef8cSmatthias.ringwald 4016a1bf5ae7SMatthias Ringwald /** 4017a1bf5ae7SMatthias Ringwald * @brief Get connection type 4018a1bf5ae7SMatthias Ringwald * @param con_handle 4019a1bf5ae7SMatthias Ringwald * @result connection_type 4020a1bf5ae7SMatthias Ringwald */ 4021a1bf5ae7SMatthias Ringwald gap_connection_type_t gap_get_connection_type(hci_con_handle_t connection_handle){ 4022a1bf5ae7SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(connection_handle); 4023a1bf5ae7SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 4024a1bf5ae7SMatthias Ringwald switch (conn->address_type){ 4025a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 4026a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 4027a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_LE; 4028a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_SCO: 4029a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_SCO; 4030a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_CLASSIC: 4031a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_ACL; 4032a1bf5ae7SMatthias Ringwald default: 4033a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_INVALID; 4034a1bf5ae7SMatthias Ringwald } 4035a1bf5ae7SMatthias Ringwald } 4036a1bf5ae7SMatthias Ringwald 4037a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 40384f551432SMatthias Ringwald 4039d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4040d23838ecSMatthias Ringwald /** 4041ac9c45e0SMatthias Ringwald * @brief Auto Connection Establishment - Start Connecting to device 4042ac9c45e0SMatthias Ringwald * @param address_typ 4043ac9c45e0SMatthias Ringwald * @param address 4044ac9c45e0SMatthias Ringwald * @returns 0 if ok 4045ac9c45e0SMatthias Ringwald */ 40464f551432SMatthias Ringwald int gap_auto_connection_start(bd_addr_type_t address_type, bd_addr_t address){ 4047e83201bcSMatthias Ringwald // check capacity 4048665d90f2SMatthias Ringwald int num_entries = btstack_linked_list_count(&hci_stack->le_whitelist); 404991915b0bSMatthias Ringwald if (num_entries >= hci_stack->le_whitelist_capacity) return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED; 4050e83201bcSMatthias Ringwald whitelist_entry_t * entry = btstack_memory_whitelist_entry_get(); 4051e83201bcSMatthias Ringwald if (!entry) return BTSTACK_MEMORY_ALLOC_FAILED; 4052e83201bcSMatthias Ringwald entry->address_type = address_type; 4053e83201bcSMatthias Ringwald memcpy(entry->address, address, 6); 4054e83201bcSMatthias Ringwald entry->state = LE_WHITELIST_ADD_TO_CONTROLLER; 4055665d90f2SMatthias Ringwald btstack_linked_list_add(&hci_stack->le_whitelist, (btstack_linked_item_t*) entry); 4056e83201bcSMatthias Ringwald hci_run(); 4057e83201bcSMatthias Ringwald return 0; 4058ac9c45e0SMatthias Ringwald } 4059ac9c45e0SMatthias Ringwald 406042ff5ba1SMatthias Ringwald static void hci_remove_from_whitelist(bd_addr_type_t address_type, bd_addr_t address){ 4061665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 4062665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 4063665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 4064665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it); 4065e83201bcSMatthias Ringwald if (entry->address_type != address_type) continue; 4066e83201bcSMatthias Ringwald if (memcmp(entry->address, address, 6) != 0) continue; 4067e83201bcSMatthias Ringwald if (entry->state & LE_WHITELIST_ON_CONTROLLER){ 4068e83201bcSMatthias Ringwald // remove from controller if already present 4069e83201bcSMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER; 4070e83201bcSMatthias Ringwald continue; 4071e83201bcSMatthias Ringwald } 4072e83201bcSMatthias Ringwald // direclty remove entry from whitelist 4073665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 4074e83201bcSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 4075e83201bcSMatthias Ringwald } 407642ff5ba1SMatthias Ringwald } 407742ff5ba1SMatthias Ringwald 407842ff5ba1SMatthias Ringwald /** 407942ff5ba1SMatthias Ringwald * @brief Auto Connection Establishment - Stop Connecting to device 408042ff5ba1SMatthias Ringwald * @param address_typ 408142ff5ba1SMatthias Ringwald * @param address 408242ff5ba1SMatthias Ringwald * @returns 0 if ok 408342ff5ba1SMatthias Ringwald */ 408442ff5ba1SMatthias Ringwald int gap_auto_connection_stop(bd_addr_type_t address_type, bd_addr_t address){ 408542ff5ba1SMatthias Ringwald hci_remove_from_whitelist(address_type, address); 4086e83201bcSMatthias Ringwald hci_run(); 4087e83201bcSMatthias Ringwald return 0; 4088ac9c45e0SMatthias Ringwald } 4089ac9c45e0SMatthias Ringwald 4090ac9c45e0SMatthias Ringwald /** 4091ac9c45e0SMatthias Ringwald * @brief Auto Connection Establishment - Stop everything 4092ac9c45e0SMatthias Ringwald * @note Convenience function to stop all active auto connection attempts 4093ac9c45e0SMatthias Ringwald */ 4094ac9c45e0SMatthias Ringwald void gap_auto_connection_stop_all(void){ 4095665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 4096665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 4097665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 4098665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it); 4099e83201bcSMatthias Ringwald if (entry->state & LE_WHITELIST_ON_CONTROLLER){ 4100e83201bcSMatthias Ringwald // remove from controller if already present 4101e83201bcSMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER; 4102e83201bcSMatthias Ringwald continue; 4103e83201bcSMatthias Ringwald } 410491915b0bSMatthias Ringwald // directly remove entry from whitelist 4105665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 4106e83201bcSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 4107e83201bcSMatthias Ringwald } 4108e83201bcSMatthias Ringwald hci_run(); 4109ac9c45e0SMatthias Ringwald } 4110d70217a2SMatthias Ringwald #endif 41114f551432SMatthias Ringwald #endif 41124f551432SMatthias Ringwald 411335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 4114ac9c45e0SMatthias Ringwald /** 4115ff00ed1cSMatthias Ringwald * @brief Set Extended Inquiry Response data 4116ff00ed1cSMatthias Ringwald * @param eir_data size 240 bytes, is not copied make sure memory is accessible during stack startup 4117ff00ed1cSMatthias Ringwald * @note has to be done before stack starts up 4118ff00ed1cSMatthias Ringwald */ 4119ff00ed1cSMatthias Ringwald void gap_set_extended_inquiry_response(const uint8_t * data){ 4120ff00ed1cSMatthias Ringwald hci_stack->eir_data = data; 4121ff00ed1cSMatthias Ringwald } 4122ff00ed1cSMatthias Ringwald 4123ff00ed1cSMatthias Ringwald /** 4124f6858d14SMatthias Ringwald * @brief Set inquiry mode: standard, with RSSI, with RSSI + Extended Inquiry Results. Has to be called before power on. 4125f6858d14SMatthias Ringwald * @param inquriy_mode see bluetooth_defines.h 4126f6858d14SMatthias Ringwald */ 4127f6858d14SMatthias Ringwald void hci_set_inquiry_mode(inquiry_mode_t mode){ 4128f6858d14SMatthias Ringwald hci_stack->inquiry_mode = mode; 4129f6858d14SMatthias Ringwald } 4130f6858d14SMatthias Ringwald 4131f6858d14SMatthias Ringwald /** 4132d950d659SMatthias Ringwald * @brief Configure Voice Setting for use with SCO data in HSP/HFP 4133d950d659SMatthias Ringwald */ 4134d950d659SMatthias Ringwald void hci_set_sco_voice_setting(uint16_t voice_setting){ 4135d950d659SMatthias Ringwald hci_stack->sco_voice_setting = voice_setting; 4136d950d659SMatthias Ringwald } 4137d950d659SMatthias Ringwald 4138d950d659SMatthias Ringwald /** 4139d950d659SMatthias Ringwald * @brief Get SCO Voice Setting 4140d950d659SMatthias Ringwald * @return current voice setting 4141d950d659SMatthias Ringwald */ 41420cb5b971SMatthias Ringwald uint16_t hci_get_sco_voice_setting(void){ 4143d950d659SMatthias Ringwald return hci_stack->sco_voice_setting; 4144d950d659SMatthias Ringwald } 4145d950d659SMatthias Ringwald 4146b3aad8daSMatthias Ringwald /** @brief Get SCO packet length for current SCO Voice setting 4147b3aad8daSMatthias Ringwald * @note Using SCO packets of the exact length is required for USB transfer 4148b3aad8daSMatthias Ringwald * @return Length of SCO packets in bytes (not audio frames) 4149b3aad8daSMatthias Ringwald */ 4150b3aad8daSMatthias Ringwald int hci_get_sco_packet_length(void){ 4151b3aad8daSMatthias Ringwald // see Core Spec for H2 USB Transfer. 4152b3aad8daSMatthias Ringwald if (hci_stack->sco_voice_setting & 0x0020) return 51; 4153b3aad8daSMatthias Ringwald return 27; 4154b3aad8daSMatthias Ringwald } 415535454696SMatthias Ringwald #endif 4156b3aad8daSMatthias Ringwald 4157d950d659SMatthias Ringwald /** 4158d23838ecSMatthias Ringwald * @brief Set callback for Bluetooth Hardware Error 4159d23838ecSMatthias Ringwald */ 4160c2e1fa60SMatthias Ringwald void hci_set_hardware_error_callback(void (*fn)(uint8_t error)){ 4161d23838ecSMatthias Ringwald hci_stack->hardware_error_callback = fn; 4162d23838ecSMatthias Ringwald } 4163d23838ecSMatthias Ringwald 416471de195eSMatthias Ringwald void hci_disconnect_all(void){ 4165665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 4166665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 4167665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 4168665d90f2SMatthias Ringwald hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 416904a6ef8cSmatthias.ringwald if (con->state == SENT_DISCONNECT) continue; 417004a6ef8cSmatthias.ringwald con->state = SEND_DISCONNECT; 417104a6ef8cSmatthias.ringwald } 4172d31fba26S[email protected] hci_run(); 417304a6ef8cSmatthias.ringwald } 417433373e40SMatthias Ringwald 417533373e40SMatthias Ringwald uint16_t hci_get_manufacturer(void){ 417633373e40SMatthias Ringwald return hci_stack->manufacturer; 417733373e40SMatthias Ringwald } 4178