11f504dbdSmatthias.ringwald /* 2a0c35809S[email protected] * Copyright (C) 2014 BlueKitchen GmbH 31713bceaSmatthias.ringwald * 41713bceaSmatthias.ringwald * Redistribution and use in source and binary forms, with or without 51713bceaSmatthias.ringwald * modification, are permitted provided that the following conditions 61713bceaSmatthias.ringwald * are met: 71713bceaSmatthias.ringwald * 81713bceaSmatthias.ringwald * 1. Redistributions of source code must retain the above copyright 91713bceaSmatthias.ringwald * notice, this list of conditions and the following disclaimer. 101713bceaSmatthias.ringwald * 2. Redistributions in binary form must reproduce the above copyright 111713bceaSmatthias.ringwald * notice, this list of conditions and the following disclaimer in the 121713bceaSmatthias.ringwald * documentation and/or other materials provided with the distribution. 131713bceaSmatthias.ringwald * 3. Neither the name of the copyright holders nor the names of 141713bceaSmatthias.ringwald * contributors may be used to endorse or promote products derived 151713bceaSmatthias.ringwald * from this software without specific prior written permission. 166b64433eSmatthias.ringwald * 4. Any redistribution, use, or modification is done solely for 176b64433eSmatthias.ringwald * personal benefit and not for any commercial purpose or for 186b64433eSmatthias.ringwald * monetary gain. 191713bceaSmatthias.ringwald * 20a0c35809S[email protected] * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 211713bceaSmatthias.ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 221713bceaSmatthias.ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 231713bceaSmatthias.ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 241713bceaSmatthias.ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 251713bceaSmatthias.ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 261713bceaSmatthias.ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 271713bceaSmatthias.ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 281713bceaSmatthias.ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 291713bceaSmatthias.ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 301713bceaSmatthias.ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311713bceaSmatthias.ringwald * SUCH DAMAGE. 321713bceaSmatthias.ringwald * 33a0c35809S[email protected] * Please inquire about commercial licensing options at 34a0c35809S[email protected] * [email protected] 356b64433eSmatthias.ringwald * 361713bceaSmatthias.ringwald */ 371713bceaSmatthias.ringwald 38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "hci.c" 39ab2c6ae4SMatthias Ringwald 401713bceaSmatthias.ringwald /* 411f504dbdSmatthias.ringwald * hci.c 421f504dbdSmatthias.ringwald * 431f504dbdSmatthias.ringwald * Created by Matthias Ringwald on 4/29/09. 441f504dbdSmatthias.ringwald * 451f504dbdSmatthias.ringwald */ 461f504dbdSmatthias.ringwald 477907f069SMatthias Ringwald #include "btstack_config.h" 4828171530Smatthias.ringwald 497f2435e6Smatthias.ringwald 5006b9e820SMatthias Ringwald #ifdef ENABLE_CLASSIC 51aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK 528f2a52f4SMatthias Ringwald #include "btstack_run_loop_embedded.h" 53a484130cSMatthias Ringwald #endif 5406b9e820SMatthias Ringwald #endif 55a484130cSMatthias Ringwald 564a3574a1SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 57d0b87befSMatthias Ringwald #include "../port/ios/src/btstack_control_iphone.h" 584a3574a1SMatthias Ringwald #endif 594a3574a1SMatthias Ringwald 60a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 61472a5742SMatthias Ringwald #include "gap.h" 6245c102fdSMatthias Ringwald #endif 6345c102fdSMatthias Ringwald 6493b8dc03Smatthias.ringwald #include <stdarg.h> 6593b8dc03Smatthias.ringwald #include <string.h> 6656fe0872Smatthias.ringwald #include <stdio.h> 675838a2edSMatthias Ringwald #include <inttypes.h> 687f2435e6Smatthias.ringwald 6916ece135SMatthias Ringwald #include "btstack_debug.h" 700e2df43fSMatthias Ringwald #include "btstack_event.h" 714a3574a1SMatthias Ringwald #include "btstack_linked_list.h" 724a3574a1SMatthias Ringwald #include "btstack_memory.h" 7361f37892SMatthias Ringwald #include "bluetooth_company_id.h" 741cfb383eSMatthias Ringwald #include "bluetooth_data_types.h" 754a3574a1SMatthias Ringwald #include "gap.h" 764a3574a1SMatthias Ringwald #include "hci.h" 774a3574a1SMatthias Ringwald #include "hci_cmd.h" 78d8905019Smatthias.ringwald #include "hci_dump.h" 791cfb383eSMatthias Ringwald #include "ad_parser.h" 8093b8dc03Smatthias.ringwald 812b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 822b838201SMatthias Ringwald #ifndef HCI_HOST_ACL_PACKET_NUM 832b838201SMatthias Ringwald #error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_ACL_PACKET_NUM" 842b838201SMatthias Ringwald #endif 852b838201SMatthias Ringwald #ifndef HCI_HOST_ACL_PACKET_LEN 862b838201SMatthias Ringwald #error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_ACL_PACKET_LEN" 872b838201SMatthias Ringwald #endif 882b838201SMatthias Ringwald #ifndef HCI_HOST_SCO_PACKET_NUM 892b838201SMatthias Ringwald #error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_SCO_PACKET_NUM" 902b838201SMatthias Ringwald #endif 912b838201SMatthias Ringwald #ifndef HCI_HOST_SCO_PACKET_LEN 922b838201SMatthias Ringwald #error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_SCO_PACKET_LEN" 932b838201SMatthias Ringwald #endif 942b838201SMatthias Ringwald #endif 951b0e3922Smatthias.ringwald 96169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000 97659d758cSMatthias Ringwald #define HCI_RESET_RESEND_TIMEOUT_MS 200 98ee091cf1Smatthias.ringwald 991cfb383eSMatthias Ringwald // Names are arbitrarily shortened to 32 bytes if not requested otherwise 1001cfb383eSMatthias Ringwald #ifndef GAP_INQUIRY_MAX_NAME_LEN 1011cfb383eSMatthias Ringwald #define GAP_INQUIRY_MAX_NAME_LEN 32 1021cfb383eSMatthias Ringwald #endif 1031cfb383eSMatthias Ringwald 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); 1171cfb383eSMatthias Ringwald static void gap_inquiry_explode(uint8_t * packet); 11852db98b2SMatthias Ringwald #endif 1191cfb383eSMatthias 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; 1606fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 16071f7b95a1Smatthias.ringwald hci_connection_t * conn; 160856cf178bSmatthias.ringwald int i; 1609*0014e02cSMatthias Ringwald #ifdef ENABLE_CLASSIC 1610*0014e02cSMatthias Ringwald uint8_t link_type; 1611*0014e02cSMatthias Ringwald #endif 161235454696SMatthias Ringwald 16130e2df43fSMatthias Ringwald // log_info("HCI:EVENT:%02x", hci_event_packet_get_type(packet)); 16145909f7f2Smatthias.ringwald 16150e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 161622909952Smatthias.ringwald 16176772a24cSmatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 16186bef4003SMatthias Ringwald // get num cmd packets - limit to 1 to reduce complexity 16196bef4003SMatthias Ringwald hci_stack->num_cmd_packets = packet[2] ? 1 : 0; 16207ec5eeaaSmatthias.ringwald 16219e263bd7SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){ 162206b9e820SMatthias Ringwald if (packet[5]) break; 16239e263bd7SMatthias Ringwald // terminate, name 248 chars 16249e263bd7SMatthias Ringwald packet[6+248] = 0; 16259e263bd7SMatthias Ringwald log_info("local name: %s", &packet[6]); 16269e263bd7SMatthias Ringwald } 1627073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_buffer_size)){ 16281d279b20Smatthias.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" 1629429122ccSMatthias Ringwald if (hci_stack->state == HCI_STATE_INITIALIZING){ 1630429122ccSMatthias Ringwald uint16_t acl_len = little_endian_read_16(packet, 6); 1631429122ccSMatthias Ringwald uint16_t sco_len = packet[8]; 1632429122ccSMatthias Ringwald 1633429122ccSMatthias Ringwald // determine usable ACL/SCO payload size 1634429122ccSMatthias Ringwald hci_stack->acl_data_packet_length = btstack_min(acl_len, HCI_ACL_PAYLOAD_SIZE); 1635429122ccSMatthias Ringwald hci_stack->sco_data_packet_length = btstack_min(sco_len, HCI_ACL_PAYLOAD_SIZE); 1636429122ccSMatthias Ringwald 1637f8fbdce0SMatthias Ringwald hci_stack->acl_packets_total_num = little_endian_read_16(packet, 9); 1638f8fbdce0SMatthias Ringwald hci_stack->sco_packets_total_num = little_endian_read_16(packet, 11); 1639a8b12447S[email protected] 1640429122ccSMatthias Ringwald log_info("hci_read_buffer_size: ACL size module %u -> used %u, count %u / SCO size %u, count %u", 1641429122ccSMatthias Ringwald acl_len, hci_stack->acl_data_packet_length, hci_stack->acl_packets_total_num, 16421a06f663S[email protected] hci_stack->sco_data_packet_length, hci_stack->sco_packets_total_num); 1643e2edc0c3Smatthias.ringwald } 164456cf178bSmatthias.ringwald } 1645a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 1646073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_le_read_buffer_size)){ 1647f8fbdce0SMatthias Ringwald hci_stack->le_data_packets_length = little_endian_read_16(packet, 6); 1648ee303eddS[email protected] hci_stack->le_acl_packets_total_num = packet[8]; 16496c26b087S[email protected] // determine usable ACL payload size 16506c26b087S[email protected] if (HCI_ACL_PAYLOAD_SIZE < hci_stack->le_data_packets_length){ 16516c26b087S[email protected] hci_stack->le_data_packets_length = HCI_ACL_PAYLOAD_SIZE; 16526c26b087S[email protected] } 16539da54300S[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); 165465a46ef3S[email protected] } 1655d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 1656073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_le_read_white_list_size)){ 1657e691bb38SMatthias Ringwald hci_stack->le_whitelist_capacity = packet[6]; 165815d0a15bSMatthias Ringwald log_info("hci_le_read_white_list_size: size %u", hci_stack->le_whitelist_capacity); 16593b6d4121SMatthias Ringwald } 166065a46ef3S[email protected] #endif 1661d70217a2SMatthias Ringwald #endif 1662073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_bd_addr)) { 1663724d70a2SMatthias Ringwald reverse_bd_addr(&packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1], 1664724d70a2SMatthias Ringwald hci_stack->local_bd_addr); 16659da54300S[email protected] log_info("Local Address, Status: 0x%02x: Addr: %s", 16663a9fb326S[email protected] packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr)); 166733373e40SMatthias Ringwald #ifdef ENABLE_CLASSIC 16681624665aSMatthias Ringwald if (hci_stack->link_key_db){ 16691624665aSMatthias Ringwald hci_stack->link_key_db->set_local_bd_addr(hci_stack->local_bd_addr); 16701624665aSMatthias Ringwald } 167133373e40SMatthias Ringwald #endif 1672188981d2Smatthias.ringwald } 167335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1674073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_scan_enable)){ 16753a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 1676381fbed8Smatthias.ringwald } 167735454696SMatthias Ringwald #endif 167835454696SMatthias Ringwald 167965389bfcS[email protected] // Note: HCI init checks 1680073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_supported_features)){ 16813a9fb326S[email protected] memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8); 168265389bfcS[email protected] 168335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1684a5a23fc2S[email protected] // determine usable ACL packet types based on host buffer size and supported features 1685a5a23fc2S[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]); 16868b96126aSMatthias Ringwald log_info("Packet types %04x, eSCO %u", hci_stack->packet_types, hci_extended_sco_link_supported()); 168735454696SMatthias Ringwald #endif 1688f5d8d141S[email protected] // Classic/LE 1689f5d8d141S[email protected] log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported()); 1690559e517eS[email protected] } 1691073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_version_information)){ 1692f8fbdce0SMatthias Ringwald // hci_stack->hci_version = little_endian_read_16(packet, 4); 1693f8fbdce0SMatthias Ringwald // hci_stack->hci_revision = little_endian_read_16(packet, 6); 1694f8fbdce0SMatthias Ringwald // hci_stack->lmp_version = little_endian_read_16(packet, 8); 1695f8fbdce0SMatthias Ringwald hci_stack->manufacturer = little_endian_read_16(packet, 10); 1696f8fbdce0SMatthias Ringwald // hci_stack->lmp_subversion = little_endian_read_16(packet, 12); 16974696bddbSMatthias Ringwald log_info("Manufacturer: 0x%04x", hci_stack->manufacturer); 16984696bddbSMatthias Ringwald } 1699073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_supported_commands)){ 1700a828a756SMatthias Ringwald hci_stack->local_supported_commands[0] = 17013905afbfSMatthias Ringwald (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+14] & 0x80) >> 7 | // bit 0 = Octet 14, bit 7 17023905afbfSMatthias Ringwald (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+24] & 0x40) >> 5 | // bit 1 = Octet 24, bit 6 17033905afbfSMatthias Ringwald (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+10] & 0x10) >> 2 | // bit 2 = Octet 10, bit 4 17043905afbfSMatthias Ringwald (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+18] & 0x08); // bit 3 = Octet 18, bit 3 17053905afbfSMatthias Ringwald log_info("Local supported commands summary 0x%02x", hci_stack->local_supported_commands[0]); 1706a828a756SMatthias Ringwald } 1707e8c8828eSMatthias Ringwald #ifdef ENABLE_CLASSIC 1708073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_synchronous_flow_control_enable)){ 17095b9b590fSMatthias Ringwald if (packet[5] == 0){ 17105b9b590fSMatthias Ringwald hci_stack->synchronous_flow_control_enabled = 1; 17115b9b590fSMatthias Ringwald } 17125b9b590fSMatthias Ringwald } 1713e8c8828eSMatthias Ringwald #endif 171456cf178bSmatthias.ringwald break; 171556cf178bSmatthias.ringwald 17167ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 17176bef4003SMatthias Ringwald // get num cmd packets - limit to 1 to reduce complexity 17186bef4003SMatthias Ringwald hci_stack->num_cmd_packets = packet[3] ? 1 : 0; 17197ec5eeaaSmatthias.ringwald break; 17207ec5eeaaSmatthias.ringwald 17212e440c8aS[email protected] case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:{ 17222e440c8aS[email protected] int offset = 3; 172356cf178bSmatthias.ringwald for (i=0; i<packet[2];i++){ 1724f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, offset); 17252e440c8aS[email protected] offset += 2; 1726f8fbdce0SMatthias Ringwald uint16_t num_packets = little_endian_read_16(packet, offset); 17272e440c8aS[email protected] offset += 2; 17282e440c8aS[email protected] 17295061f3afS[email protected] conn = hci_connection_for_handle(handle); 173056cf178bSmatthias.ringwald if (!conn){ 17319da54300S[email protected] log_error("hci_number_completed_packet lists unused con handle %u", handle); 173256cf178bSmatthias.ringwald continue; 173356cf178bSmatthias.ringwald } 173423bed257S[email protected] 1735e35edcc1S[email protected] if (conn->address_type == BD_ADDR_TYPE_SCO){ 173635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1737e35edcc1S[email protected] if (conn->num_sco_packets_sent >= num_packets){ 1738e35edcc1S[email protected] conn->num_sco_packets_sent -= num_packets; 1739e35edcc1S[email protected] } else { 1740e35edcc1S[email protected] log_error("hci_number_completed_packets, more sco slots freed then sent."); 1741e35edcc1S[email protected] conn->num_sco_packets_sent = 0; 1742e35edcc1S[email protected] } 1743701e3307SMatthias Ringwald hci_notify_if_sco_can_send_now(); 174435454696SMatthias Ringwald #endif 1745e35edcc1S[email protected] } else { 174623bed257S[email protected] if (conn->num_acl_packets_sent >= num_packets){ 174756cf178bSmatthias.ringwald conn->num_acl_packets_sent -= num_packets; 174823bed257S[email protected] } else { 1749e35edcc1S[email protected] log_error("hci_number_completed_packets, more acl slots freed then sent."); 175023bed257S[email protected] conn->num_acl_packets_sent = 0; 175123bed257S[email protected] } 1752e35edcc1S[email protected] } 17539da54300S[email protected] // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u", num_packets, handle, conn->num_acl_packets_sent); 175456cf178bSmatthias.ringwald } 17556772a24cSmatthias.ringwald break; 17562e440c8aS[email protected] } 175735454696SMatthias Ringwald 175835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 17591f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 1760724d70a2SMatthias Ringwald reverse_bd_addr(&packet[2], addr); 176137eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 17622d00edd4Smatthias.ringwald link_type = packet[11]; 17639da54300S[email protected] log_info("Connection_incoming: %s, type %u", bd_addr_to_str(addr), link_type); 1764e35edcc1S[email protected] addr_type = link_type == 1 ? BD_ADDR_TYPE_CLASSIC : BD_ADDR_TYPE_SCO; 17652e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 17661f7b95a1Smatthias.ringwald if (!conn) { 17675293c072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 17681f7b95a1Smatthias.ringwald } 1769ce4c8fabSmatthias.ringwald if (!conn) { 1770ce4c8fabSmatthias.ringwald // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 17713a9fb326S[email protected] hci_stack->decline_reason = 0x0d; 1772058e3d6bSMatthias Ringwald bd_addr_copy(hci_stack->decline_addr, addr); 1773ce4c8fabSmatthias.ringwald break; 1774ce4c8fabSmatthias.ringwald } 17755cf766e8SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 177632ab9390Smatthias.ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 1777f3a16b9aSMatthias Ringwald // store info about eSCO 1778f3a16b9aSMatthias Ringwald if (link_type == 0x02){ 1779f3a16b9aSMatthias Ringwald conn->remote_supported_feature_eSCO = 1; 1780f3a16b9aSMatthias Ringwald } 178132ab9390Smatthias.ringwald hci_run(); 17821f7b95a1Smatthias.ringwald break; 17831f7b95a1Smatthias.ringwald 17846772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 1785fe1ed1b8Smatthias.ringwald // Connection management 1786724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], addr); 17879da54300S[email protected] log_info("Connection_complete (status=%u) %s", packet[2], bd_addr_to_str(addr)); 178896a45072S[email protected] addr_type = BD_ADDR_TYPE_CLASSIC; 17892e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 1790fe1ed1b8Smatthias.ringwald if (conn) { 1791b448a0e7Smatthias.ringwald if (!packet[2]){ 1792c8e4258aSmatthias.ringwald conn->state = OPEN; 1793f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 3); 1794ad83dc6aS[email protected] conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES; 1795ee091cf1Smatthias.ringwald 1796c785ef68Smatthias.ringwald // restart timer 1797528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 1798528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&conn->timeout); 1799c785ef68Smatthias.ringwald 18009da54300S[email protected] log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 180143bfb1bdSmatthias.ringwald 180243bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 1803b448a0e7Smatthias.ringwald } else { 18041bd5283dS[email protected] int notify_dedicated_bonding_failed = conn->bonding_flags & BONDING_DEDICATED; 18051bd5283dS[email protected] uint8_t status = packet[2]; 18061bd5283dS[email protected] bd_addr_t bd_address; 18071bd5283dS[email protected] memcpy(&bd_address, conn->address, 6); 1808ad83dc6aS[email protected] 1809b448a0e7Smatthias.ringwald // connection failed, remove entry 1810665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 1811a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 1812c12e46e7Smatthias.ringwald 18131bd5283dS[email protected] // notify client if dedicated bonding 18141bd5283dS[email protected] if (notify_dedicated_bonding_failed){ 18151bd5283dS[email protected] log_info("hci notify_dedicated_bonding_failed"); 18161bd5283dS[email protected] hci_emit_dedicated_bonding_result(bd_address, status); 18171bd5283dS[email protected] } 18181bd5283dS[email protected] 1819c12e46e7Smatthias.ringwald // if authentication error, also delete link key 1820c12e46e7Smatthias.ringwald if (packet[2] == 0x05) { 182115a95bd5SMatthias Ringwald gap_drop_link_key_for_bd_addr(addr); 1822c12e46e7Smatthias.ringwald } 1823fe1ed1b8Smatthias.ringwald } 1824fe1ed1b8Smatthias.ringwald } 18256772a24cSmatthias.ringwald break; 1826fe1ed1b8Smatthias.ringwald 182744d0e3d5S[email protected] case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE: 1828724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], addr); 182944d0e3d5S[email protected] log_info("Synchronous Connection Complete (status=%u) %s", packet[2], bd_addr_to_str(addr)); 18301a06f663S[email protected] if (packet[2]){ 183144d0e3d5S[email protected] // connection failed 183244d0e3d5S[email protected] break; 183344d0e3d5S[email protected] } 18342e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 1835e35edcc1S[email protected] if (!conn) { 1836e35edcc1S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 1837e35edcc1S[email protected] } 1838e35edcc1S[email protected] if (!conn) { 1839e35edcc1S[email protected] break; 1840e35edcc1S[email protected] } 18411a06f663S[email protected] conn->state = OPEN; 1842f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 3); 1843ee752bb8SMatthias Ringwald 1844ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 1845ee752bb8SMatthias Ringwald // update SCO 1846ee752bb8SMatthias Ringwald if (conn->address_type == BD_ADDR_TYPE_SCO && hci_stack->hci_transport && hci_stack->hci_transport->set_sco_config){ 1847ee752bb8SMatthias Ringwald hci_stack->hci_transport->set_sco_config(hci_stack->sco_voice_setting_active, hci_number_sco_connections()); 1848ee752bb8SMatthias Ringwald } 1849ee752bb8SMatthias Ringwald #endif 185044d0e3d5S[email protected] break; 185144d0e3d5S[email protected] 1852afd4e962S[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 1853f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1854afd4e962S[email protected] conn = hci_connection_for_handle(handle); 1855afd4e962S[email protected] if (!conn) break; 1856afd4e962S[email protected] if (!packet[2]){ 1857afd4e962S[email protected] uint8_t * features = &packet[5]; 1858afd4e962S[email protected] if (features[6] & (1 << 3)){ 1859afd4e962S[email protected] conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP; 1860afd4e962S[email protected] } 186198a2fd1cSMatthias Ringwald if (features[3] & (1<<7)){ 186298a2fd1cSMatthias Ringwald conn->remote_supported_feature_eSCO = 1; 186398a2fd1cSMatthias Ringwald } 1864afd4e962S[email protected] } 1865afd4e962S[email protected] conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES; 186698a2fd1cSMatthias Ringwald log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x, eSCO %u", conn->bonding_flags, conn->remote_supported_feature_eSCO); 1867ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 1868ad83dc6aS[email protected] conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 1869ad83dc6aS[email protected] } 1870afd4e962S[email protected] break; 1871afd4e962S[email protected] 18727fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 18739da54300S[email protected] log_info("HCI_EVENT_LINK_KEY_REQUEST"); 18747fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST); 187574d716b5S[email protected] // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST 1876a98592bcSMatthias Ringwald if (hci_stack->bondable && !hci_stack->link_key_db) break; 187732ab9390Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST); 187864472d52Smatthias.ringwald hci_run(); 1879d9a327f9Smatthias.ringwald // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set 188029d53098Smatthias.ringwald return; 18817fde4af9Smatthias.ringwald 18829ab95c90S[email protected] case HCI_EVENT_LINK_KEY_NOTIFICATION: { 1883724d70a2SMatthias Ringwald reverse_bd_addr(&packet[2], addr); 18842e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 18859ab95c90S[email protected] if (!conn) break; 18869ab95c90S[email protected] conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION; 18877bdc6798S[email protected] link_key_type_t link_key_type = (link_key_type_t)packet[24]; 18889ab95c90S[email protected] // Change Connection Encryption keeps link key type 18899ab95c90S[email protected] if (link_key_type != CHANGED_COMBINATION_KEY){ 18909ab95c90S[email protected] conn->link_key_type = link_key_type; 18919ab95c90S[email protected] } 189255597469SMatthias Ringwald gap_store_link_key_for_bd_addr(addr, &packet[8], conn->link_key_type); 189329d53098Smatthias.ringwald // still forward event to allow dismiss of pairing dialog 18947fde4af9Smatthias.ringwald break; 18959ab95c90S[email protected] } 18967fde4af9Smatthias.ringwald 18977fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 18986724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE); 18994c57c146S[email protected] // non-bondable mode: pin code negative reply will be sent 19003a9fb326S[email protected] if (!hci_stack->bondable){ 1901899283eaS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST); 1902f8fb5f6eS[email protected] hci_run(); 1903f8fb5f6eS[email protected] return; 19044c57c146S[email protected] } 1905d9a327f9Smatthias.ringwald // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key 1906a98592bcSMatthias Ringwald if (!hci_stack->link_key_db) break; 1907a6ef64baSMilanka Ringwald hci_event_pin_code_request_get_bd_addr(packet, addr); 1908a98592bcSMatthias Ringwald hci_stack->link_key_db->delete_link_key(addr); 19097fde4af9Smatthias.ringwald break; 19107fde4af9Smatthias.ringwald 19111d6b20aeS[email protected] case HCI_EVENT_IO_CAPABILITY_REQUEST: 19121d6b20aeS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST); 1913dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY); 1914dbe1a790S[email protected] break; 1915dbe1a790S[email protected] 1916dbe1a790S[email protected] case HCI_EVENT_USER_CONFIRMATION_REQUEST: 19176724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 19183a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 1919dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY); 1920dbe1a790S[email protected] break; 1921dbe1a790S[email protected] 1922dbe1a790S[email protected] case HCI_EVENT_USER_PASSKEY_REQUEST: 19236724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 19243a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 1925dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY); 19261d6b20aeS[email protected] break; 192735454696SMatthias Ringwald #endif 19281d6b20aeS[email protected] 1929f0944df2S[email protected] case HCI_EVENT_ENCRYPTION_CHANGE: 1930f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1931f0944df2S[email protected] conn = hci_connection_for_handle(handle); 1932f0944df2S[email protected] if (!conn) break; 1933ad83dc6aS[email protected] if (packet[2] == 0) { 1934f0944df2S[email protected] if (packet[5]){ 1935f0944df2S[email protected] conn->authentication_flags |= CONNECTION_ENCRYPTED; 1936f0944df2S[email protected] } else { 1937536f9994S[email protected] conn->authentication_flags &= ~CONNECTION_ENCRYPTED; 1938f0944df2S[email protected] } 1939ad83dc6aS[email protected] } 194035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1941a00031e2S[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 194235454696SMatthias Ringwald #endif 1943f0944df2S[email protected] break; 1944f0944df2S[email protected] 194535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 19461eb2563eS[email protected] case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT: 1947f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 19481eb2563eS[email protected] conn = hci_connection_for_handle(handle); 19491eb2563eS[email protected] if (!conn) break; 1950ad83dc6aS[email protected] 1951ad83dc6aS[email protected] // dedicated bonding: send result and disconnect 1952ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 1953ad83dc6aS[email protected] conn->bonding_flags &= ~BONDING_DEDICATED; 1954ad83dc6aS[email protected] conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE; 19551bd5283dS[email protected] conn->bonding_status = packet[2]; 1956ad83dc6aS[email protected] break; 1957ad83dc6aS[email protected] } 1958ad83dc6aS[email protected] 1959b5cb6874S[email protected] if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){ 19601eb2563eS[email protected] // link key sufficient for requested security 19611eb2563eS[email protected] conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 1962ad83dc6aS[email protected] break; 1963ad83dc6aS[email protected] } 19641eb2563eS[email protected] // not enough 19651eb2563eS[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 19661eb2563eS[email protected] break; 196735454696SMatthias Ringwald #endif 196834d2123cS[email protected] 196986805605S[email protected] // HCI_EVENT_DISCONNECTION_COMPLETE 1970ccda6e14S[email protected] // has been split, to first notify stack before shutting connection down 1971ccda6e14S[email protected] // see end of function, too. 1972a4f30ec0S[email protected] case HCI_EVENT_DISCONNECTION_COMPLETE: 1973a4f30ec0S[email protected] if (packet[2]) break; // status != 0 1974f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1975c6a37cfdSMatthias Ringwald // drop outgoing ACL fragments if it is for closed connection 1976c6a37cfdSMatthias Ringwald if (hci_stack->acl_fragmentation_total_size > 0) { 1977c6a37cfdSMatthias Ringwald if (handle == READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer)){ 1978c6a37cfdSMatthias Ringwald log_info("hci: drop fragmented ACL data for closed connection"); 1979c6a37cfdSMatthias Ringwald hci_stack->acl_fragmentation_total_size = 0; 1980c6a37cfdSMatthias Ringwald hci_stack->acl_fragmentation_pos = 0; 1981c6a37cfdSMatthias Ringwald } 1982c6a37cfdSMatthias Ringwald } 198335454696SMatthias Ringwald 19849a2e4658SMatthias Ringwald // re-enable advertisements for le connections if active 1985c6a37cfdSMatthias Ringwald conn = hci_connection_for_handle(handle); 1986c6a37cfdSMatthias Ringwald if (!conn) break; 198735454696SMatthias Ringwald #ifdef ENABLE_BLE 1988d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 19899a2e4658SMatthias Ringwald if (hci_is_le_connection(conn) && hci_stack->le_advertisements_enabled){ 19909a2e4658SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_ENABLE; 19919a2e4658SMatthias Ringwald } 199235454696SMatthias Ringwald #endif 1993d70217a2SMatthias Ringwald #endif 1994ccda6e14S[email protected] conn->state = RECEIVED_DISCONNECTION_COMPLETE; 1995ccda6e14S[email protected] break; 19966772a24cSmatthias.ringwald 1997c68bdf90Smatthias.ringwald case HCI_EVENT_HARDWARE_ERROR: 1998313e5f9cSMatthias Ringwald log_error("Hardware Error: 0x%02x", packet[2]); 1999d23838ecSMatthias Ringwald if (hci_stack->hardware_error_callback){ 2000c2e1fa60SMatthias Ringwald (*hci_stack->hardware_error_callback)(packet[2]); 20017586ee35S[email protected] } else { 20027586ee35S[email protected] // if no special requests, just reboot stack 20037586ee35S[email protected] hci_power_control_off(); 20047586ee35S[email protected] hci_power_control_on(); 2005c68bdf90Smatthias.ringwald } 2006c68bdf90Smatthias.ringwald break; 2007c68bdf90Smatthias.ringwald 20080e6f3837SMatthias Ringwald #ifdef ENABLE_CLASSIC 20095cf766e8SMatthias Ringwald case HCI_EVENT_ROLE_CHANGE: 20105cf766e8SMatthias Ringwald if (packet[2]) break; // status != 0 2011f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 20125cf766e8SMatthias Ringwald conn = hci_connection_for_handle(handle); 20135cf766e8SMatthias Ringwald if (!conn) break; // no conn 20145cf766e8SMatthias Ringwald conn->role = packet[9]; 20155cf766e8SMatthias Ringwald break; 20160e6f3837SMatthias Ringwald #endif 20175cf766e8SMatthias Ringwald 201863fa3374SMatthias Ringwald case HCI_EVENT_TRANSPORT_PACKET_SENT: 2019d051460cS[email protected] // release packet buffer only for asynchronous transport and if there are not further fragements 20204fa24b5fS[email protected] if (hci_transport_synchronous()) { 202163fa3374SMatthias Ringwald log_error("Synchronous HCI Transport shouldn't send HCI_EVENT_TRANSPORT_PACKET_SENT"); 20224fa24b5fS[email protected] return; // instead of break: to avoid re-entering hci_run() 20234fa24b5fS[email protected] } 2024d051460cS[email protected] if (hci_stack->acl_fragmentation_total_size) break; 2025d051460cS[email protected] hci_release_packet_buffer(); 2026701e3307SMatthias Ringwald 202763fa3374SMatthias Ringwald // L2CAP receives this event via the hci_emit_event below 202863fa3374SMatthias Ringwald 202935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 203063fa3374SMatthias Ringwald // For SCO, we do the can_send_now_check here 2031701e3307SMatthias Ringwald hci_notify_if_sco_can_send_now(); 203235454696SMatthias Ringwald #endif 20336b4af23dS[email protected] break; 20346b4af23dS[email protected] 203535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 203663fa3374SMatthias Ringwald case HCI_EVENT_SCO_CAN_SEND_NOW: 203763fa3374SMatthias Ringwald // For SCO, we do the can_send_now_check here 203863fa3374SMatthias Ringwald hci_notify_if_sco_can_send_now(); 203963fa3374SMatthias Ringwald return; 20401cfb383eSMatthias Ringwald 20411cfb383eSMatthias Ringwald // explode inquriy results for easier consumption 20421cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 20431cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 20441cfb383eSMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 20451cfb383eSMatthias Ringwald gap_inquiry_explode(packet); 20461cfb383eSMatthias Ringwald break; 204735454696SMatthias Ringwald #endif 204863fa3374SMatthias Ringwald 2049a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 20505909f7f2Smatthias.ringwald case HCI_EVENT_LE_META: 20515909f7f2Smatthias.ringwald switch (packet[2]){ 2052d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 205357c9da5bS[email protected] case HCI_SUBEVENT_LE_ADVERTISING_REPORT: 20544f4e0224SMatthias Ringwald // log_info("advertising report received"); 20557bdc6798S[email protected] if (hci_stack->le_scanning_state != LE_SCANNING) break; 205657c9da5bS[email protected] le_handle_advertisement_report(packet, size); 20577bdc6798S[email protected] break; 2058d70217a2SMatthias Ringwald #endif 20595909f7f2Smatthias.ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 20605909f7f2Smatthias.ringwald // Connection management 2061724d70a2SMatthias Ringwald reverse_bd_addr(&packet[8], addr); 20627bdc6798S[email protected] addr_type = (bd_addr_type_t)packet[7]; 20639da54300S[email protected] log_info("LE Connection_complete (status=%u) type %u, %s", packet[3], addr_type, bd_addr_to_str(addr)); 20642e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 2065d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 206642ff5ba1SMatthias Ringwald // if auto-connect, remove from whitelist in both roles 206742ff5ba1SMatthias Ringwald if (hci_stack->le_connecting_state == LE_CONNECTING_WHITELIST){ 206842ff5ba1SMatthias Ringwald hci_remove_from_whitelist(addr_type, addr); 206942ff5ba1SMatthias Ringwald } 207042ff5ba1SMatthias Ringwald // handle error: error is reported only to the initiator -> outgoing connection 20715909f7f2Smatthias.ringwald if (packet[3]){ 207242ff5ba1SMatthias Ringwald // outgoing connection establishment is done 207342ff5ba1SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 207442ff5ba1SMatthias Ringwald // remove entry 20755909f7f2Smatthias.ringwald if (conn){ 2076665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 20775909f7f2Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 20785909f7f2Smatthias.ringwald } 20795909f7f2Smatthias.ringwald break; 20805909f7f2Smatthias.ringwald } 2081d70217a2SMatthias Ringwald #endif 208242ff5ba1SMatthias Ringwald // on success, both hosts receive connection complete event 20835cf766e8SMatthias Ringwald if (packet[6] == HCI_ROLE_MASTER){ 2084d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 208542ff5ba1SMatthias Ringwald // if we're master, it was an outgoing connection and we're done with it 208642ff5ba1SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 2087d70217a2SMatthias Ringwald #endif 208842ff5ba1SMatthias Ringwald } else { 2089d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 209042ff5ba1SMatthias Ringwald // if we're slave, it was an incoming connection, advertisements have stopped 2091171293d3SMatthias Ringwald hci_stack->le_advertisements_active = 0; 20925106e6dcSMatthias Ringwald // try to re-enable them 20935106e6dcSMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_ENABLE; 2094d70217a2SMatthias Ringwald #endif 209542ff5ba1SMatthias Ringwald } 2096171293d3SMatthias Ringwald // LE connections are auto-accepted, so just create a connection if there isn't one already 209742ff5ba1SMatthias Ringwald if (!conn){ 209896a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 20995909f7f2Smatthias.ringwald } 210042ff5ba1SMatthias Ringwald // no memory, sorry. 21015909f7f2Smatthias.ringwald if (!conn){ 21025909f7f2Smatthias.ringwald break; 21035909f7f2Smatthias.ringwald } 21045909f7f2Smatthias.ringwald 21055909f7f2Smatthias.ringwald conn->state = OPEN; 21065cf766e8SMatthias Ringwald conn->role = packet[6]; 2107f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 4); 21085909f7f2Smatthias.ringwald 21095909f7f2Smatthias.ringwald // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock 21105909f7f2Smatthias.ringwald 21115909f7f2Smatthias.ringwald // restart timer 2112528a4a3bSMatthias Ringwald // btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 2113528a4a3bSMatthias Ringwald // btstack_run_loop_add_timer(&conn->timeout); 21145909f7f2Smatthias.ringwald 21159da54300S[email protected] log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 21165909f7f2Smatthias.ringwald 21175909f7f2Smatthias.ringwald hci_emit_nr_connections_changed(); 21185909f7f2Smatthias.ringwald break; 21195909f7f2Smatthias.ringwald 2120f8fbdce0SMatthias Ringwald // log_info("LE buffer size: %u, count %u", little_endian_read_16(packet,6), packet[8]); 212165a46ef3S[email protected] 21225909f7f2Smatthias.ringwald default: 21235909f7f2Smatthias.ringwald break; 21245909f7f2Smatthias.ringwald } 21255909f7f2Smatthias.ringwald break; 21265909f7f2Smatthias.ringwald #endif 21276772a24cSmatthias.ringwald default: 21286772a24cSmatthias.ringwald break; 2129fe1ed1b8Smatthias.ringwald } 2130fe1ed1b8Smatthias.ringwald 21313429f56bSmatthias.ringwald // handle BT initialization 21323a9fb326S[email protected] if (hci_stack->state == HCI_STATE_INITIALIZING){ 21336155b3d3S[email protected] hci_initializing_event_handler(packet, size); 2134c9af4d3fS[email protected] } 213522909952Smatthias.ringwald 213689db417bSmatthias.ringwald // help with BT sleep 21373a9fb326S[email protected] if (hci_stack->state == HCI_STATE_FALLING_ASLEEP 213874b323a9SMatthias Ringwald && hci_stack->substate == HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE 2139073bd0faSMatthias Ringwald && HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_scan_enable)){ 214055975f88SMatthias Ringwald hci_initializing_next_state(); 214189db417bSmatthias.ringwald } 214289db417bSmatthias.ringwald 214386805605S[email protected] // notify upper stack 2144d6b06661SMatthias Ringwald hci_emit_event(packet, size, 0); // don't dump, already happened in packet handler 214594ab26f8Smatthias.ringwald 214686805605S[email protected] // moved here to give upper stack a chance to close down everything with hci_connection_t intact 21470e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_DISCONNECTION_COMPLETE){ 214886805605S[email protected] if (!packet[2]){ 2149f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 215005ae8de3SMatthias Ringwald hci_connection_t * aConn = hci_connection_for_handle(handle); 215105ae8de3SMatthias Ringwald if (aConn) { 215205ae8de3SMatthias Ringwald uint8_t status = aConn->bonding_status; 215305ae8de3SMatthias Ringwald uint16_t flags = aConn->bonding_flags; 215486805605S[email protected] bd_addr_t bd_address; 215505ae8de3SMatthias Ringwald memcpy(&bd_address, aConn->address, 6); 215605ae8de3SMatthias Ringwald hci_shutdown_connection(aConn); 2157bb820044S[email protected] // connection struct is gone, don't access anymore 2158bb820044S[email protected] if (flags & BONDING_EMIT_COMPLETE_ON_DISCONNECT){ 215986805605S[email protected] hci_emit_dedicated_bonding_result(bd_address, status); 216086805605S[email protected] } 216186805605S[email protected] } 216286805605S[email protected] } 216386805605S[email protected] } 216486805605S[email protected] 216594ab26f8Smatthias.ringwald // execute main loop 216694ab26f8Smatthias.ringwald hci_run(); 216716833f0aSmatthias.ringwald } 216816833f0aSmatthias.ringwald 216935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2170c91d150bS[email protected] static void sco_handler(uint8_t * packet, uint16_t size){ 21718abbe8b5SMatthias Ringwald if (!hci_stack->sco_packet_handler) return; 21723d50b4baSMatthias Ringwald hci_stack->sco_packet_handler(HCI_SCO_DATA_PACKET, 0, packet, size); 21732b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 21742b838201SMatthias Ringwald hci_con_handle_t con_handle = READ_SCO_CONNECTION_HANDLE(packet); 21752b838201SMatthias Ringwald hci_connection_t *conn = hci_connection_for_handle(con_handle); 21762b838201SMatthias Ringwald if (conn){ 21772b838201SMatthias Ringwald conn->num_packets_completed++; 21782b838201SMatthias Ringwald hci_stack->host_completed_packets = 1; 21792b838201SMatthias Ringwald hci_run(); 21802b838201SMatthias Ringwald } 21812b838201SMatthias Ringwald #endif 2182c91d150bS[email protected] } 218335454696SMatthias Ringwald #endif 2184c91d150bS[email protected] 21850a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 21865bb5bc3eS[email protected] hci_dump_packet(packet_type, 1, packet, size); 218710e830c9Smatthias.ringwald switch (packet_type) { 218810e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 218910e830c9Smatthias.ringwald event_handler(packet, size); 219010e830c9Smatthias.ringwald break; 219110e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 219210e830c9Smatthias.ringwald acl_handler(packet, size); 219310e830c9Smatthias.ringwald break; 219435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2195c91d150bS[email protected] case HCI_SCO_DATA_PACKET: 2196c91d150bS[email protected] sco_handler(packet, size); 2197202c8a4cSMatthias Ringwald break; 219835454696SMatthias Ringwald #endif 219910e830c9Smatthias.ringwald default: 220010e830c9Smatthias.ringwald break; 220110e830c9Smatthias.ringwald } 220210e830c9Smatthias.ringwald } 220310e830c9Smatthias.ringwald 2204d6b06661SMatthias Ringwald /** 2205d6b06661SMatthias Ringwald * @brief Add event packet handler. 2206d6b06661SMatthias Ringwald */ 2207d6b06661SMatthias Ringwald void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){ 2208d6b06661SMatthias Ringwald btstack_linked_list_add_tail(&hci_stack->event_handlers, (btstack_linked_item_t*) callback_handler); 2209d6b06661SMatthias Ringwald } 2210d6b06661SMatthias Ringwald 2211d6b06661SMatthias Ringwald 2212fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 22133d50b4baSMatthias Ringwald void hci_register_acl_packet_handler(btstack_packet_handler_t handler){ 2214fb37a842SMatthias Ringwald hci_stack->acl_packet_handler = handler; 221516833f0aSmatthias.ringwald } 221616833f0aSmatthias.ringwald 221735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 22188abbe8b5SMatthias Ringwald /** 22198abbe8b5SMatthias Ringwald * @brief Registers a packet handler for SCO data. Used for HSP and HFP profiles. 22208abbe8b5SMatthias Ringwald */ 22213d50b4baSMatthias Ringwald void hci_register_sco_packet_handler(btstack_packet_handler_t handler){ 22228abbe8b5SMatthias Ringwald hci_stack->sco_packet_handler = handler; 22238abbe8b5SMatthias Ringwald } 222435454696SMatthias Ringwald #endif 22258abbe8b5SMatthias Ringwald 222671de195eSMatthias Ringwald static void hci_state_reset(void){ 2227595bdbfbS[email protected] // no connections yet 2228595bdbfbS[email protected] hci_stack->connections = NULL; 222974308b23Smatthias.ringwald 223074308b23Smatthias.ringwald // keep discoverable/connectable as this has been requested by the client(s) 223174308b23Smatthias.ringwald // hci_stack->discoverable = 0; 223274308b23Smatthias.ringwald // hci_stack->connectable = 0; 223374308b23Smatthias.ringwald // hci_stack->bondable = 1; 2234b95a5a35SMatthias Ringwald // hci_stack->own_addr_type = 0; 2235595bdbfbS[email protected] 223644935e40S[email protected] // buffer is free 223744935e40S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 223844935e40S[email protected] 2239595bdbfbS[email protected] // no pending cmds 2240595bdbfbS[email protected] hci_stack->decline_reason = 0; 2241595bdbfbS[email protected] hci_stack->new_scan_enable_value = 0xff; 2242595bdbfbS[email protected] 2243595bdbfbS[email protected] // LE 2244b95a5a35SMatthias Ringwald #ifdef ENABLE_BLE 2245b95a5a35SMatthias Ringwald memset(hci_stack->le_random_address, 0, 6); 2246b95a5a35SMatthias Ringwald hci_stack->le_random_address_set = 0; 2247d70217a2SMatthias Ringwald #endif 2248d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 2249595bdbfbS[email protected] hci_stack->le_scanning_state = LE_SCAN_IDLE; 2250e2602ea2Smatthias.ringwald hci_stack->le_scan_type = 0xff; 2251b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 2252e83201bcSMatthias Ringwald hci_stack->le_whitelist = 0; 2253e83201bcSMatthias Ringwald hci_stack->le_whitelist_capacity = 0; 2254d70217a2SMatthias Ringwald #endif 2255d70217a2SMatthias Ringwald 2256e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_interval_min = 6; 2257e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_interval_max = 3200; 2258e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_latency_min = 0; 2259e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_latency_max = 500; 2260e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_supervision_timeout_min = 10; 2261e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_supervision_timeout_max = 3200; 2262595bdbfbS[email protected] } 2263595bdbfbS[email protected] 226435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 22652d5e09d6SMatthias Ringwald /** 22662d5e09d6SMatthias Ringwald * @brief Configure Bluetooth hardware control. Has to be called before power on. 22672d5e09d6SMatthias Ringwald */ 22682d5e09d6SMatthias Ringwald void hci_set_link_key_db(btstack_link_key_db_t const * link_key_db){ 22692d5e09d6SMatthias Ringwald // store and open remote device db 22702d5e09d6SMatthias Ringwald hci_stack->link_key_db = link_key_db; 22712d5e09d6SMatthias Ringwald if (hci_stack->link_key_db) { 22722d5e09d6SMatthias Ringwald hci_stack->link_key_db->open(); 22732d5e09d6SMatthias Ringwald } 22742d5e09d6SMatthias Ringwald } 227535454696SMatthias Ringwald #endif 22762d5e09d6SMatthias Ringwald 22772d5e09d6SMatthias Ringwald void hci_init(const hci_transport_t *transport, const void *config){ 2278475c8125Smatthias.ringwald 22793a9fb326S[email protected] #ifdef HAVE_MALLOC 22803a9fb326S[email protected] if (!hci_stack) { 22813a9fb326S[email protected] hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t)); 22823a9fb326S[email protected] } 22833a9fb326S[email protected] #else 22843a9fb326S[email protected] hci_stack = &hci_stack_static; 22853a9fb326S[email protected] #endif 228666fb9560S[email protected] memset(hci_stack, 0, sizeof(hci_stack_t)); 22873a9fb326S[email protected] 2288475c8125Smatthias.ringwald // reference to use transport layer implementation 22893a9fb326S[email protected] hci_stack->hci_transport = transport; 2290475c8125Smatthias.ringwald 229111e23e5fSmatthias.ringwald // reference to used config 22923a9fb326S[email protected] hci_stack->config = config; 229311e23e5fSmatthias.ringwald 229426a9b6daSMatthias Ringwald // setup pointer for outgoing packet buffer 229526a9b6daSMatthias Ringwald hci_stack->hci_packet_buffer = &hci_stack->hci_packet_buffer_data[HCI_OUTGOING_PRE_BUFFER_SIZE]; 229626a9b6daSMatthias Ringwald 22978fcba05dSmatthias.ringwald // max acl payload size defined in config.h 22983a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 22998fcba05dSmatthias.ringwald 230016833f0aSmatthias.ringwald // register packet handlers with transport 230110e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 2302f5454fc6Smatthias.ringwald 23033a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 2304e2386ba1S[email protected] 2305e2386ba1S[email protected] // class of device 23063a9fb326S[email protected] hci_stack->class_of_device = 0x007a020c; // Smartphone 2307a45d6b9fS[email protected] 2308f20168b8Smatthias.ringwald // bondable by default 2309f20168b8Smatthias.ringwald hci_stack->bondable = 1; 2310f20168b8Smatthias.ringwald 231163048403S[email protected] // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept 23123a9fb326S[email protected] hci_stack->ssp_enable = 1; 23133a9fb326S[email protected] hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT; 23143a9fb326S[email protected] hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING; 23153a9fb326S[email protected] hci_stack->ssp_auto_accept = 1; 231669a97523S[email protected] 2317fac2e2feSMatthias Ringwald // voice setting - signed 16 bit pcm data with CVSD over the air 2318fac2e2feSMatthias Ringwald hci_stack->sco_voice_setting = 0x60; 2319d950d659SMatthias Ringwald 2320595bdbfbS[email protected] hci_state_reset(); 2321475c8125Smatthias.ringwald } 2322475c8125Smatthias.ringwald 23233fb36a29SMatthias Ringwald /** 23243fb36a29SMatthias Ringwald * @brief Configure Bluetooth chipset driver. Has to be called before power on, or right after receiving the local version information 23253fb36a29SMatthias Ringwald */ 23263fb36a29SMatthias Ringwald void hci_set_chipset(const btstack_chipset_t *chipset_driver){ 23273fb36a29SMatthias Ringwald hci_stack->chipset = chipset_driver; 23283fb36a29SMatthias Ringwald 23293fb36a29SMatthias Ringwald // reset chipset driver - init is also called on power_up 23303fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->init){ 23313fb36a29SMatthias Ringwald hci_stack->chipset->init(hci_stack->config); 23323fb36a29SMatthias Ringwald } 23333fb36a29SMatthias Ringwald } 23343fb36a29SMatthias Ringwald 2335fb55bd0aSMatthias Ringwald /** 2336d0b87befSMatthias Ringwald * @brief Configure Bluetooth hardware control. Has to be called after hci_init() but before power on. 2337fb55bd0aSMatthias Ringwald */ 2338fb55bd0aSMatthias Ringwald void hci_set_control(const btstack_control_t *hardware_control){ 2339fb55bd0aSMatthias Ringwald // references to used control implementation 2340fb55bd0aSMatthias Ringwald hci_stack->control = hardware_control; 2341d0b87befSMatthias Ringwald // init with transport config 2342d0b87befSMatthias Ringwald hardware_control->init(hci_stack->config); 2343fb55bd0aSMatthias Ringwald } 2344fb55bd0aSMatthias Ringwald 234571de195eSMatthias Ringwald void hci_close(void){ 2346404843c1Smatthias.ringwald // close remote device db 2347a98592bcSMatthias Ringwald if (hci_stack->link_key_db) { 2348a98592bcSMatthias Ringwald hci_stack->link_key_db->close(); 2349404843c1Smatthias.ringwald } 23507224be7eSMatthias Ringwald 23517224be7eSMatthias Ringwald btstack_linked_list_iterator_t lit; 23527224be7eSMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->connections); 23537224be7eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 23547224be7eSMatthias Ringwald // cancel all l2cap connections by emitting dicsconnection complete before shutdown (free) connection 23557224be7eSMatthias Ringwald hci_connection_t * connection = (hci_connection_t*) btstack_linked_list_iterator_next(&lit); 23567224be7eSMatthias Ringwald hci_emit_disconnection_complete(connection->con_handle, 0x16); // terminated by local host 23577224be7eSMatthias Ringwald hci_shutdown_connection(connection); 2358f5454fc6Smatthias.ringwald } 23597224be7eSMatthias Ringwald 2360f5454fc6Smatthias.ringwald hci_power_control(HCI_POWER_OFF); 23613a9fb326S[email protected] 23623a9fb326S[email protected] #ifdef HAVE_MALLOC 23633a9fb326S[email protected] free(hci_stack); 23643a9fb326S[email protected] #endif 23653a9fb326S[email protected] hci_stack = NULL; 2366404843c1Smatthias.ringwald } 2367404843c1Smatthias.ringwald 236835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 236960b9e82fSMatthias Ringwald void gap_set_class_of_device(uint32_t class_of_device){ 23709e61646fS[email protected] hci_stack->class_of_device = class_of_device; 23719e61646fS[email protected] } 237276f27cffSMatthias Ringwald 237376f27cffSMatthias Ringwald void hci_disable_l2cap_timeout_check(void){ 237476f27cffSMatthias Ringwald disable_l2cap_timeouts = 1; 237576f27cffSMatthias Ringwald } 237635454696SMatthias Ringwald #endif 23779e61646fS[email protected] 237876f27cffSMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 2379f456b2d0S[email protected] // Set Public BD ADDR - passed on to Bluetooth chipset if supported in bt_control_h 2380f456b2d0S[email protected] void hci_set_bd_addr(bd_addr_t addr){ 2381f456b2d0S[email protected] memcpy(hci_stack->custom_bd_addr, addr, 6); 2382f456b2d0S[email protected] hci_stack->custom_bd_addr_set = 1; 2383f456b2d0S[email protected] } 238476f27cffSMatthias Ringwald #endif 2385f456b2d0S[email protected] 23868d213e1aSmatthias.ringwald // State-Module-Driver overview 23878d213e1aSmatthias.ringwald // state module low-level 23888d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 23898d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 23908d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 23918d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 2392d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 2393d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 2394c7e0c5f6Smatthias.ringwald 239540d1c7a4Smatthias.ringwald static int hci_power_control_on(void){ 23967301ad89Smatthias.ringwald 2397038bc64cSmatthias.ringwald // power on 2398f9a30166Smatthias.ringwald int err = 0; 23993a9fb326S[email protected] if (hci_stack->control && hci_stack->control->on){ 2400d0b87befSMatthias Ringwald err = (*hci_stack->control->on)(); 2401f9a30166Smatthias.ringwald } 2402038bc64cSmatthias.ringwald if (err){ 24039da54300S[email protected] log_error( "POWER_ON failed"); 2404038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 2405038bc64cSmatthias.ringwald return err; 2406038bc64cSmatthias.ringwald } 2407038bc64cSmatthias.ringwald 240824b3c629SMatthias Ringwald // int chipset driver 24093fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->init){ 24103fb36a29SMatthias Ringwald hci_stack->chipset->init(hci_stack->config); 24113fb36a29SMatthias Ringwald } 24123fb36a29SMatthias Ringwald 241324b3c629SMatthias Ringwald // init transport 241424b3c629SMatthias Ringwald if (hci_stack->hci_transport->init){ 241524b3c629SMatthias Ringwald hci_stack->hci_transport->init(hci_stack->config); 241624b3c629SMatthias Ringwald } 241724b3c629SMatthias Ringwald 241824b3c629SMatthias Ringwald // open transport 241924b3c629SMatthias Ringwald err = hci_stack->hci_transport->open(); 2420038bc64cSmatthias.ringwald if (err){ 24219da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 24223a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 2423d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 2424f9a30166Smatthias.ringwald } 2425038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 2426038bc64cSmatthias.ringwald return err; 2427038bc64cSmatthias.ringwald } 24288d213e1aSmatthias.ringwald return 0; 24298d213e1aSmatthias.ringwald } 2430038bc64cSmatthias.ringwald 243140d1c7a4Smatthias.ringwald static void hci_power_control_off(void){ 24328d213e1aSmatthias.ringwald 24339da54300S[email protected] log_info("hci_power_control_off"); 24349418f9c9Smatthias.ringwald 24358d213e1aSmatthias.ringwald // close low-level device 243624b3c629SMatthias Ringwald hci_stack->hci_transport->close(); 24378d213e1aSmatthias.ringwald 24389da54300S[email protected] log_info("hci_power_control_off - hci_transport closed"); 24399418f9c9Smatthias.ringwald 24408d213e1aSmatthias.ringwald // power off 24413a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 2442d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 24438d213e1aSmatthias.ringwald } 24449418f9c9Smatthias.ringwald 24459da54300S[email protected] log_info("hci_power_control_off - control closed"); 24469418f9c9Smatthias.ringwald 24473a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 244872ea5239Smatthias.ringwald } 244972ea5239Smatthias.ringwald 245040d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){ 245172ea5239Smatthias.ringwald 24529da54300S[email protected] log_info("hci_power_control_sleep"); 24533144bce4Smatthias.ringwald 2454b429b9b7Smatthias.ringwald #if 0 2455b429b9b7Smatthias.ringwald // don't close serial port during sleep 2456b429b9b7Smatthias.ringwald 245772ea5239Smatthias.ringwald // close low-level device 24583a9fb326S[email protected] hci_stack->hci_transport->close(hci_stack->config); 2459b429b9b7Smatthias.ringwald #endif 246072ea5239Smatthias.ringwald 246172ea5239Smatthias.ringwald // sleep mode 24623a9fb326S[email protected] if (hci_stack->control && hci_stack->control->sleep){ 2463d0b87befSMatthias Ringwald (*hci_stack->control->sleep)(); 246472ea5239Smatthias.ringwald } 2465b429b9b7Smatthias.ringwald 24663a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 24678d213e1aSmatthias.ringwald } 24688d213e1aSmatthias.ringwald 246940d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){ 2470b429b9b7Smatthias.ringwald 24719da54300S[email protected] log_info("hci_power_control_wake"); 2472b429b9b7Smatthias.ringwald 2473b429b9b7Smatthias.ringwald // wake on 24743a9fb326S[email protected] if (hci_stack->control && hci_stack->control->wake){ 2475d0b87befSMatthias Ringwald (*hci_stack->control->wake)(); 2476b429b9b7Smatthias.ringwald } 2477b429b9b7Smatthias.ringwald 2478b429b9b7Smatthias.ringwald #if 0 2479b429b9b7Smatthias.ringwald // open low-level device 24803a9fb326S[email protected] int err = hci_stack->hci_transport->open(hci_stack->config); 2481b429b9b7Smatthias.ringwald if (err){ 24829da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 24833a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 2484d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 2485b429b9b7Smatthias.ringwald } 2486b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 2487b429b9b7Smatthias.ringwald return err; 2488b429b9b7Smatthias.ringwald } 2489b429b9b7Smatthias.ringwald #endif 2490b429b9b7Smatthias.ringwald 2491b429b9b7Smatthias.ringwald return 0; 2492b429b9b7Smatthias.ringwald } 2493b429b9b7Smatthias.ringwald 249444935e40S[email protected] static void hci_power_transition_to_initializing(void){ 249544935e40S[email protected] // set up state machine 249644935e40S[email protected] hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent 249744935e40S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 249844935e40S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 24995c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET; 250044935e40S[email protected] } 2501b429b9b7Smatthias.ringwald 25028d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 2503d661ed19Smatthias.ringwald 2504f04a0c31SMatthias Ringwald log_info("hci_power_control: %d, current mode %u", power_mode, hci_stack->state); 2505d661ed19Smatthias.ringwald 25068d213e1aSmatthias.ringwald int err = 0; 25073a9fb326S[email protected] switch (hci_stack->state){ 25088d213e1aSmatthias.ringwald 25098d213e1aSmatthias.ringwald case HCI_STATE_OFF: 25108d213e1aSmatthias.ringwald switch (power_mode){ 25118d213e1aSmatthias.ringwald case HCI_POWER_ON: 25128d213e1aSmatthias.ringwald err = hci_power_control_on(); 251397b61c7bS[email protected] if (err) { 2514f04a0c31SMatthias Ringwald log_error("hci_power_control_on() error %d", err); 251597b61c7bS[email protected] return err; 251697b61c7bS[email protected] } 251744935e40S[email protected] hci_power_transition_to_initializing(); 25188d213e1aSmatthias.ringwald break; 25198d213e1aSmatthias.ringwald case HCI_POWER_OFF: 25208d213e1aSmatthias.ringwald // do nothing 25218d213e1aSmatthias.ringwald break; 25228d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2523b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 25248d213e1aSmatthias.ringwald break; 25258d213e1aSmatthias.ringwald } 25268d213e1aSmatthias.ringwald break; 25277301ad89Smatthias.ringwald 25288d213e1aSmatthias.ringwald case HCI_STATE_INITIALIZING: 25298d213e1aSmatthias.ringwald switch (power_mode){ 25308d213e1aSmatthias.ringwald case HCI_POWER_ON: 25318d213e1aSmatthias.ringwald // do nothing 25328d213e1aSmatthias.ringwald break; 25338d213e1aSmatthias.ringwald case HCI_POWER_OFF: 25348d213e1aSmatthias.ringwald // no connections yet, just turn it off 25358d213e1aSmatthias.ringwald hci_power_control_off(); 25368d213e1aSmatthias.ringwald break; 25378d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2538b546ac54Smatthias.ringwald // no connections yet, just turn it off 253972ea5239Smatthias.ringwald hci_power_control_sleep(); 25408d213e1aSmatthias.ringwald break; 25418d213e1aSmatthias.ringwald } 25428d213e1aSmatthias.ringwald break; 25437301ad89Smatthias.ringwald 25448d213e1aSmatthias.ringwald case HCI_STATE_WORKING: 25458d213e1aSmatthias.ringwald switch (power_mode){ 25468d213e1aSmatthias.ringwald case HCI_POWER_ON: 25478d213e1aSmatthias.ringwald // do nothing 25488d213e1aSmatthias.ringwald break; 25498d213e1aSmatthias.ringwald case HCI_POWER_OFF: 2550c7e0c5f6Smatthias.ringwald // see hci_run 25513a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 25528d213e1aSmatthias.ringwald break; 25538d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2554b546ac54Smatthias.ringwald // see hci_run 25553a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 255674b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT; 25578d213e1aSmatthias.ringwald break; 25588d213e1aSmatthias.ringwald } 25598d213e1aSmatthias.ringwald break; 25607301ad89Smatthias.ringwald 25618d213e1aSmatthias.ringwald case HCI_STATE_HALTING: 25628d213e1aSmatthias.ringwald switch (power_mode){ 25638d213e1aSmatthias.ringwald case HCI_POWER_ON: 256444935e40S[email protected] hci_power_transition_to_initializing(); 25658d213e1aSmatthias.ringwald break; 25668d213e1aSmatthias.ringwald case HCI_POWER_OFF: 25678d213e1aSmatthias.ringwald // do nothing 25688d213e1aSmatthias.ringwald break; 25698d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2570b546ac54Smatthias.ringwald // see hci_run 25713a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 257274b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT; 25738d213e1aSmatthias.ringwald break; 25748d213e1aSmatthias.ringwald } 25758d213e1aSmatthias.ringwald break; 25768d213e1aSmatthias.ringwald 25778d213e1aSmatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 25788d213e1aSmatthias.ringwald switch (power_mode){ 25798d213e1aSmatthias.ringwald case HCI_POWER_ON: 258028171530Smatthias.ringwald 2581423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 258228171530Smatthias.ringwald // nothing to do, if H4 supports power management 2583d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 25843a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 258574b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_WRITE_SCAN_ENABLE; // init after sleep 258628171530Smatthias.ringwald break; 258728171530Smatthias.ringwald } 258828171530Smatthias.ringwald #endif 258944935e40S[email protected] hci_power_transition_to_initializing(); 25908d213e1aSmatthias.ringwald break; 25918d213e1aSmatthias.ringwald case HCI_POWER_OFF: 2592b546ac54Smatthias.ringwald // see hci_run 25933a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 25948d213e1aSmatthias.ringwald break; 25958d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2596b546ac54Smatthias.ringwald // do nothing 25978d213e1aSmatthias.ringwald break; 25988d213e1aSmatthias.ringwald } 25998d213e1aSmatthias.ringwald break; 26008d213e1aSmatthias.ringwald 26018d213e1aSmatthias.ringwald case HCI_STATE_SLEEPING: 26028d213e1aSmatthias.ringwald switch (power_mode){ 26038d213e1aSmatthias.ringwald case HCI_POWER_ON: 260428171530Smatthias.ringwald 2605423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 260628171530Smatthias.ringwald // nothing to do, if H4 supports power management 2607d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 26083a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 26095c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_AFTER_SLEEP; 2610758b46ceSmatthias.ringwald hci_update_scan_enable(); 261128171530Smatthias.ringwald break; 261228171530Smatthias.ringwald } 261328171530Smatthias.ringwald #endif 26143144bce4Smatthias.ringwald err = hci_power_control_wake(); 26153144bce4Smatthias.ringwald if (err) return err; 261644935e40S[email protected] hci_power_transition_to_initializing(); 26178d213e1aSmatthias.ringwald break; 26188d213e1aSmatthias.ringwald case HCI_POWER_OFF: 26193a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 26208d213e1aSmatthias.ringwald break; 26218d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2622b546ac54Smatthias.ringwald // do nothing 26238d213e1aSmatthias.ringwald break; 26248d213e1aSmatthias.ringwald } 26258d213e1aSmatthias.ringwald break; 262611e23e5fSmatthias.ringwald } 262768d92d03Smatthias.ringwald 2628038bc64cSmatthias.ringwald // create internal event 2629ee8bf225Smatthias.ringwald hci_emit_state(); 2630ee8bf225Smatthias.ringwald 263168d92d03Smatthias.ringwald // trigger next/first action 263268d92d03Smatthias.ringwald hci_run(); 263368d92d03Smatthias.ringwald 2634475c8125Smatthias.ringwald return 0; 2635475c8125Smatthias.ringwald } 2636475c8125Smatthias.ringwald 263735454696SMatthias Ringwald 263835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 263935454696SMatthias Ringwald 2640758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){ 2641758b46ceSmatthias.ringwald // 2 = page scan, 1 = inq scan 26423a9fb326S[email protected] hci_stack->new_scan_enable_value = hci_stack->connectable << 1 | hci_stack->discoverable; 2643758b46ceSmatthias.ringwald hci_run(); 2644758b46ceSmatthias.ringwald } 2645758b46ceSmatthias.ringwald 264615a95bd5SMatthias Ringwald void gap_discoverable_control(uint8_t enable){ 2647381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 2648381fbed8Smatthias.ringwald 26493a9fb326S[email protected] if (hci_stack->discoverable == enable){ 26503a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 2651381fbed8Smatthias.ringwald return; 2652381fbed8Smatthias.ringwald } 2653381fbed8Smatthias.ringwald 26543a9fb326S[email protected] hci_stack->discoverable = enable; 2655758b46ceSmatthias.ringwald hci_update_scan_enable(); 2656758b46ceSmatthias.ringwald } 2657b031bebbSmatthias.ringwald 265815a95bd5SMatthias Ringwald void gap_connectable_control(uint8_t enable){ 2659758b46ceSmatthias.ringwald if (enable) enable = 1; // normalize argument 2660758b46ceSmatthias.ringwald 2661758b46ceSmatthias.ringwald // don't emit event 26623a9fb326S[email protected] if (hci_stack->connectable == enable) return; 2663758b46ceSmatthias.ringwald 26643a9fb326S[email protected] hci_stack->connectable = enable; 2665758b46ceSmatthias.ringwald hci_update_scan_enable(); 2666381fbed8Smatthias.ringwald } 266735454696SMatthias Ringwald #endif 2668381fbed8Smatthias.ringwald 266915a95bd5SMatthias Ringwald void gap_local_bd_addr(bd_addr_t address_buffer){ 2670690bd0baS[email protected] memcpy(address_buffer, hci_stack->local_bd_addr, 6); 26715061f3afS[email protected] } 26725061f3afS[email protected] 26732b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 26742b838201SMatthias Ringwald static void hci_host_num_completed_packets(void){ 26752b838201SMatthias Ringwald 26762b838201SMatthias Ringwald // create packet manually as arrays are not supported and num_commands should not get reduced 26772b838201SMatthias Ringwald hci_reserve_packet_buffer(); 26782b838201SMatthias Ringwald uint8_t * packet = hci_get_outgoing_packet_buffer(); 26792b838201SMatthias Ringwald 26802b838201SMatthias Ringwald uint16_t size = 0; 26812b838201SMatthias Ringwald uint16_t num_handles = 0; 26822b838201SMatthias Ringwald packet[size++] = 0x35; 26832b838201SMatthias Ringwald packet[size++] = 0x0c; 26842b838201SMatthias Ringwald size++; // skip param len 26852b838201SMatthias Ringwald size++; // skip num handles 26862b838201SMatthias Ringwald 26872b838201SMatthias Ringwald // add { handle, packets } entries 26882b838201SMatthias Ringwald btstack_linked_item_t * it; 26892b838201SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 26902b838201SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 26912b838201SMatthias Ringwald if (connection->num_packets_completed){ 26922b838201SMatthias Ringwald little_endian_store_16(packet, size, connection->con_handle); 26932b838201SMatthias Ringwald size += 2; 26942b838201SMatthias Ringwald little_endian_store_16(packet, size, connection->num_packets_completed); 26952b838201SMatthias Ringwald size += 2; 26962b838201SMatthias Ringwald // 26972b838201SMatthias Ringwald num_handles++; 26982b838201SMatthias Ringwald connection->num_packets_completed = 0; 26992b838201SMatthias Ringwald } 27002b838201SMatthias Ringwald } 27012b838201SMatthias Ringwald 27022b838201SMatthias Ringwald packet[2] = size - 3; 27032b838201SMatthias Ringwald packet[3] = num_handles; 27042b838201SMatthias Ringwald 27052b838201SMatthias Ringwald hci_stack->host_completed_packets = 0; 27062b838201SMatthias Ringwald 27072b838201SMatthias Ringwald hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size); 27082b838201SMatthias Ringwald hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 27092b838201SMatthias Ringwald 27102b838201SMatthias Ringwald // release packet buffer for synchronous transport implementations 27112b838201SMatthias Ringwald if (hci_transport_synchronous()){ 27122b838201SMatthias Ringwald hci_stack->hci_packet_buffer_reserved = 0; 27132b838201SMatthias Ringwald } 27142b838201SMatthias Ringwald } 27152b838201SMatthias Ringwald #endif 27162b838201SMatthias Ringwald 271795d71764SMatthias Ringwald static void hci_run(void){ 27188a485f27Smatthias.ringwald 2719db8bc6ffSMatthias Ringwald // log_info("hci_run: entered"); 2720665d90f2SMatthias Ringwald btstack_linked_item_t * it; 272132ab9390Smatthias.ringwald 2722b5d8b22bS[email protected] // send continuation fragments first, as they block the prepared packet buffer 2723b5d8b22bS[email protected] if (hci_stack->acl_fragmentation_total_size > 0) { 2724b5d8b22bS[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer); 2725b5d8b22bS[email protected] hci_connection_t *connection = hci_connection_for_handle(con_handle); 2726b5d8b22bS[email protected] if (connection) { 272728a0332dSMatthias Ringwald if (hci_can_send_prepared_acl_packet_now(con_handle)){ 2728b5d8b22bS[email protected] hci_send_acl_packet_fragments(connection); 2729b5d8b22bS[email protected] return; 2730b5d8b22bS[email protected] } 273128a0332dSMatthias Ringwald } else { 2732b5d8b22bS[email protected] // connection gone -> discard further fragments 273328a0332dSMatthias Ringwald log_info("hci_run: fragmented ACL packet no connection -> discard fragment"); 2734b5d8b22bS[email protected] hci_stack->acl_fragmentation_total_size = 0; 2735b5d8b22bS[email protected] hci_stack->acl_fragmentation_pos = 0; 2736b5d8b22bS[email protected] } 2737b5d8b22bS[email protected] } 2738b5d8b22bS[email protected] 27392b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 27402b838201SMatthias Ringwald // send host num completed packets next as they don't require num_cmd_packets > 0 27412b838201SMatthias Ringwald if (!hci_can_send_comand_packet_transport()) return; 27422b838201SMatthias Ringwald if (hci_stack->host_completed_packets){ 27432b838201SMatthias Ringwald hci_host_num_completed_packets(); 27442b838201SMatthias Ringwald return; 27452b838201SMatthias Ringwald } 27462b838201SMatthias Ringwald #endif 27472b838201SMatthias Ringwald 2748d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 2749ce4c8fabSmatthias.ringwald 2750b031bebbSmatthias.ringwald // global/non-connection oriented commands 2751b031bebbSmatthias.ringwald 275235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2753b031bebbSmatthias.ringwald // decline incoming connections 27543a9fb326S[email protected] if (hci_stack->decline_reason){ 27553a9fb326S[email protected] uint8_t reason = hci_stack->decline_reason; 27563a9fb326S[email protected] hci_stack->decline_reason = 0; 27573a9fb326S[email protected] hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason); 2758dbe1a790S[email protected] return; 2759ce4c8fabSmatthias.ringwald } 2760ce4c8fabSmatthias.ringwald 2761b031bebbSmatthias.ringwald // send scan enable 27623a9fb326S[email protected] if (hci_stack->state == HCI_STATE_WORKING && hci_stack->new_scan_enable_value != 0xff && hci_classic_supported()){ 27633a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value); 27643a9fb326S[email protected] hci_stack->new_scan_enable_value = 0xff; 2765dbe1a790S[email protected] return; 2766b031bebbSmatthias.ringwald } 276735454696SMatthias Ringwald #endif 2768b031bebbSmatthias.ringwald 2769a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 2770b95a5a35SMatthias Ringwald // advertisements, active scanning, and creating connections requires randaom address to be set if using private address 2771b95a5a35SMatthias Ringwald if ((hci_stack->state == HCI_STATE_WORKING) 2772b95a5a35SMatthias Ringwald && (hci_stack->le_own_addr_type == BD_ADDR_TYPE_LE_PUBLIC || hci_stack->le_random_address_set)){ 2773d70217a2SMatthias Ringwald 2774d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 277545c102fdSMatthias Ringwald // handle le scan 27767bdc6798S[email protected] switch(hci_stack->le_scanning_state){ 27777bdc6798S[email protected] case LE_START_SCAN: 27787bdc6798S[email protected] hci_stack->le_scanning_state = LE_SCANNING; 27797bdc6798S[email protected] hci_send_cmd(&hci_le_set_scan_enable, 1, 0); 27807bdc6798S[email protected] return; 27817bdc6798S[email protected] 27827bdc6798S[email protected] case LE_STOP_SCAN: 27837bdc6798S[email protected] hci_stack->le_scanning_state = LE_SCAN_IDLE; 27847bdc6798S[email protected] hci_send_cmd(&hci_le_set_scan_enable, 0, 0); 27857bdc6798S[email protected] return; 27867bdc6798S[email protected] default: 27877bdc6798S[email protected] break; 27887bdc6798S[email protected] } 2789e2602ea2Smatthias.ringwald if (hci_stack->le_scan_type != 0xff){ 2790e2602ea2Smatthias.ringwald // defaults: active scanning, accept all advertisement packets 2791e2602ea2Smatthias.ringwald int scan_type = hci_stack->le_scan_type; 2792e2602ea2Smatthias.ringwald hci_stack->le_scan_type = 0xff; 2793b95a5a35SMatthias 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); 2794e2602ea2Smatthias.ringwald return; 2795e2602ea2Smatthias.ringwald } 2796d70217a2SMatthias Ringwald #endif 2797d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 279845c102fdSMatthias Ringwald // le advertisement control 27999a2e4658SMatthias Ringwald if (hci_stack->le_advertisements_todo){ 28009a2e4658SMatthias Ringwald log_info("hci_run: gap_le: adv todo: %x", hci_stack->le_advertisements_todo ); 28019a2e4658SMatthias Ringwald } 280245c102fdSMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_DISABLE){ 280345c102fdSMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_DISABLE; 280445c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 0); 280545c102fdSMatthias Ringwald return; 280645c102fdSMatthias Ringwald } 280745c102fdSMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_PARAMS){ 280845c102fdSMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_PARAMS; 280945c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_parameters, 281045c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_min, 281145c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_max, 281245c102fdSMatthias Ringwald hci_stack->le_advertisements_type, 2813b95a5a35SMatthias Ringwald hci_stack->le_own_addr_type, 281445c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address_type, 281545c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address, 281645c102fdSMatthias Ringwald hci_stack->le_advertisements_channel_map, 281745c102fdSMatthias Ringwald hci_stack->le_advertisements_filter_policy); 281845c102fdSMatthias Ringwald return; 281945c102fdSMatthias Ringwald } 2820501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_ADV_DATA){ 2821501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 2822b95a5a35SMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_data, hci_stack->le_advertisements_data_len, hci_stack->le_advertisements_data); 282345c102fdSMatthias Ringwald return; 282445c102fdSMatthias Ringwald } 2825501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA){ 2826501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 2827501f56b3SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_response_data, hci_stack->le_scan_response_data_len, 2828501f56b3SMatthias Ringwald hci_stack->le_scan_response_data); 2829501f56b3SMatthias Ringwald return; 2830501f56b3SMatthias Ringwald } 2831b95a5a35SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_ENABLE){ 283245c102fdSMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_ENABLE; 283345c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 1); 283445c102fdSMatthias Ringwald return; 283545c102fdSMatthias Ringwald } 2836d70217a2SMatthias Ringwald #endif 28379956955bSMatthias Ringwald 2838d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 28399956955bSMatthias Ringwald // 28409956955bSMatthias Ringwald // LE Whitelist Management 28419956955bSMatthias Ringwald // 28429956955bSMatthias Ringwald 28439956955bSMatthias Ringwald // check if whitelist needs modification 2844665d90f2SMatthias Ringwald btstack_linked_list_iterator_t lit; 28459956955bSMatthias Ringwald int modification_pending = 0; 2846665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 2847665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 2848665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 28499956955bSMatthias Ringwald if (entry->state & (LE_WHITELIST_REMOVE_FROM_CONTROLLER | LE_WHITELIST_ADD_TO_CONTROLLER)){ 28509956955bSMatthias Ringwald modification_pending = 1; 28519956955bSMatthias Ringwald break; 28529956955bSMatthias Ringwald } 28539956955bSMatthias Ringwald } 285491915b0bSMatthias Ringwald 28559956955bSMatthias Ringwald if (modification_pending){ 285691915b0bSMatthias Ringwald // stop connnecting if modification pending 28579956955bSMatthias Ringwald if (hci_stack->le_connecting_state != LE_CONNECTING_IDLE){ 28589956955bSMatthias Ringwald hci_send_cmd(&hci_le_create_connection_cancel); 28599956955bSMatthias Ringwald return; 28609956955bSMatthias Ringwald } 28619956955bSMatthias Ringwald 28629956955bSMatthias Ringwald // add/remove entries 2863665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 2864665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 2865665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 28669956955bSMatthias Ringwald if (entry->state & LE_WHITELIST_ADD_TO_CONTROLLER){ 28679956955bSMatthias Ringwald entry->state = LE_WHITELIST_ON_CONTROLLER; 28689956955bSMatthias Ringwald hci_send_cmd(&hci_le_add_device_to_white_list, entry->address_type, entry->address); 28699956955bSMatthias Ringwald return; 28709956955bSMatthias Ringwald 28719956955bSMatthias Ringwald } 28729956955bSMatthias Ringwald if (entry->state & LE_WHITELIST_REMOVE_FROM_CONTROLLER){ 28739ecbe201SMatthias Ringwald bd_addr_t address; 28749ecbe201SMatthias Ringwald bd_addr_type_t address_type = entry->address_type; 28759ecbe201SMatthias Ringwald memcpy(address, entry->address, 6); 2876665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry); 28779956955bSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 28789ecbe201SMatthias Ringwald hci_send_cmd(&hci_le_remove_device_from_white_list, address_type, address); 28799956955bSMatthias Ringwald return; 28809956955bSMatthias Ringwald } 28819956955bSMatthias Ringwald } 288291915b0bSMatthias Ringwald } 28839956955bSMatthias Ringwald 28849956955bSMatthias Ringwald // start connecting 288591915b0bSMatthias Ringwald if ( hci_stack->le_connecting_state == LE_CONNECTING_IDLE && 2886665d90f2SMatthias Ringwald !btstack_linked_list_empty(&hci_stack->le_whitelist)){ 28879956955bSMatthias Ringwald bd_addr_t null_addr; 28889956955bSMatthias Ringwald memset(null_addr, 0, 6); 28899956955bSMatthias Ringwald hci_send_cmd(&hci_le_create_connection, 28909956955bSMatthias Ringwald 0x0060, // scan interval: 60 ms 28919956955bSMatthias Ringwald 0x0030, // scan interval: 30 ms 28929956955bSMatthias Ringwald 1, // use whitelist 28939956955bSMatthias Ringwald 0, // peer address type 28949956955bSMatthias Ringwald null_addr, // peer bd addr 2895b95a5a35SMatthias Ringwald hci_stack->le_own_addr_type, // our addr type: 28969956955bSMatthias Ringwald 0x0008, // conn interval min 28979956955bSMatthias Ringwald 0x0018, // conn interval max 28989956955bSMatthias Ringwald 0, // conn latency 28999956955bSMatthias Ringwald 0x0048, // supervision timeout 29009956955bSMatthias Ringwald 0x0001, // min ce length 29019956955bSMatthias Ringwald 0x0001 // max ce length 29029956955bSMatthias Ringwald ); 29039956955bSMatthias Ringwald return; 29049956955bSMatthias Ringwald } 2905d70217a2SMatthias Ringwald #endif 29067bdc6798S[email protected] } 2907b2f949feS[email protected] #endif 29087bdc6798S[email protected] 290932ab9390Smatthias.ringwald // send pending HCI commands 2910665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 291105ae8de3SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 291232ab9390Smatthias.ringwald 29130bf6344aS[email protected] switch(connection->state){ 29140bf6344aS[email protected] case SEND_CREATE_CONNECTION: 29154f3229d8S[email protected] switch(connection->address_type){ 291635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 29174f3229d8S[email protected] case BD_ADDR_TYPE_CLASSIC: 29189da54300S[email protected] log_info("sending hci_create_connection"); 2919ad83dc6aS[email protected] hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 29204f3229d8S[email protected] break; 292135454696SMatthias Ringwald #endif 29224f3229d8S[email protected] default: 2923a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 2924d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 29259da54300S[email protected] log_info("sending hci_le_create_connection"); 29264f3229d8S[email protected] hci_send_cmd(&hci_le_create_connection, 2927b2571179Smatthias.ringwald 0x0060, // scan interval: 60 ms 2928b2571179Smatthias.ringwald 0x0030, // scan interval: 30 ms 29294f3229d8S[email protected] 0, // don't use whitelist 29304f3229d8S[email protected] connection->address_type, // peer address type 29314f3229d8S[email protected] connection->address, // peer bd addr 2932b95a5a35SMatthias Ringwald hci_stack->le_own_addr_type, // our addr type: 2933b2571179Smatthias.ringwald 0x0008, // conn interval min 2934b2571179Smatthias.ringwald 0x0018, // conn interval max 29354f3229d8S[email protected] 0, // conn latency 2936b2571179Smatthias.ringwald 0x0048, // supervision timeout 2937b2571179Smatthias.ringwald 0x0001, // min ce length 2938b2571179Smatthias.ringwald 0x0001 // max ce length 29394f3229d8S[email protected] ); 29404f3229d8S[email protected] 29414f3229d8S[email protected] connection->state = SENT_CREATE_CONNECTION; 2942b2f949feS[email protected] #endif 2943d70217a2SMatthias Ringwald #endif 29444f3229d8S[email protected] break; 29454f3229d8S[email protected] } 2946ad83dc6aS[email protected] return; 2947ad83dc6aS[email protected] 294835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 29490bf6344aS[email protected] case RECEIVED_CONNECTION_REQUEST: 295094e4aaa2SMatthias Ringwald log_info("sending hci_accept_connection_request, remote eSCO %u", connection->remote_supported_feature_eSCO); 295132ab9390Smatthias.ringwald connection->state = ACCEPTED_CONNECTION_REQUEST; 29525cf766e8SMatthias Ringwald connection->role = HCI_ROLE_SLAVE; 2953e35edcc1S[email protected] if (connection->address_type == BD_ADDR_TYPE_CLASSIC){ 295434d2123cS[email protected] hci_send_cmd(&hci_accept_connection_request, connection->address, 1); 2955e35edcc1S[email protected] } 2956dbe1a790S[email protected] return; 295735454696SMatthias Ringwald #endif 29580bf6344aS[email protected] 2959a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 2960d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 29610bf6344aS[email protected] case SEND_CANCEL_CONNECTION: 29620bf6344aS[email protected] connection->state = SENT_CANCEL_CONNECTION; 29630bf6344aS[email protected] hci_send_cmd(&hci_le_create_connection_cancel); 29640bf6344aS[email protected] return; 2965a6725849S[email protected] #endif 2966d70217a2SMatthias Ringwald #endif 29670bf6344aS[email protected] case SEND_DISCONNECT: 29680bf6344aS[email protected] connection->state = SENT_DISCONNECT; 29697851196eSmatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 29700bf6344aS[email protected] return; 29710bf6344aS[email protected] 29720bf6344aS[email protected] default: 29730bf6344aS[email protected] break; 2974c7e0c5f6Smatthias.ringwald } 2975c7e0c5f6Smatthias.ringwald 297635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 297732ab9390Smatthias.ringwald if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){ 29789da54300S[email protected] log_info("responding to link key request"); 297934d2123cS[email protected] connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST); 298032ab9390Smatthias.ringwald link_key_t link_key; 2981c77e8838S[email protected] link_key_type_t link_key_type; 2982a98592bcSMatthias Ringwald if ( hci_stack->link_key_db 2983a98592bcSMatthias Ringwald && hci_stack->link_key_db->get_link_key(connection->address, link_key, &link_key_type) 298434d2123cS[email protected] && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){ 29859ab95c90S[email protected] connection->link_key_type = link_key_type; 298632ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key); 298732ab9390Smatthias.ringwald } else { 298832ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 298932ab9390Smatthias.ringwald } 2990dbe1a790S[email protected] return; 299132ab9390Smatthias.ringwald } 29921d6b20aeS[email protected] 2993899283eaS[email protected] if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){ 29949da54300S[email protected] log_info("denying to pin request"); 2995899283eaS[email protected] connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST); 299634d2123cS[email protected] hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address); 29974c57c146S[email protected] return; 29984c57c146S[email protected] } 29994c57c146S[email protected] 3000dbe1a790S[email protected] if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){ 300134d2123cS[email protected] connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY); 300282d8f825S[email protected] log_info("IO Capability Request received, stack bondable %u, io cap %u", hci_stack->bondable, hci_stack->ssp_io_capability); 300382d8f825S[email protected] if (hci_stack->bondable && (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN)){ 3004106d6d11S[email protected] // tweak authentication requirements 30053a9fb326S[email protected] uint8_t authreq = hci_stack->ssp_authentication_requirement; 3006106d6d11S[email protected] if (connection->bonding_flags & BONDING_DEDICATED){ 30079faad3abS[email protected] authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 30089faad3abS[email protected] } 30099faad3abS[email protected] if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){ 30109faad3abS[email protected] authreq |= 1; 3011106d6d11S[email protected] } 30123a9fb326S[email protected] hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, NULL, authreq); 3013f8fb5f6eS[email protected] } else { 3014f8fb5f6eS[email protected] hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED); 3015f8fb5f6eS[email protected] } 3016dbe1a790S[email protected] return; 301732ab9390Smatthias.ringwald } 301832ab9390Smatthias.ringwald 3019dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){ 3020dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY); 302134d2123cS[email protected] hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address); 3022dbe1a790S[email protected] return; 3023dbe1a790S[email protected] } 3024dbe1a790S[email protected] 3025dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){ 3026dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY); 302734d2123cS[email protected] hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000); 3028dbe1a790S[email protected] return; 3029dbe1a790S[email protected] } 3030afd4e962S[email protected] 3031afd4e962S[email protected] if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){ 3032afd4e962S[email protected] connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES; 303334d2123cS[email protected] hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle); 30342bd8b7e7S[email protected] return; 30352bd8b7e7S[email protected] } 30362bd8b7e7S[email protected] 3037ad83dc6aS[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){ 3038ad83dc6aS[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE; 30391bd5283dS[email protected] connection->bonding_flags |= BONDING_EMIT_COMPLETE_ON_DISCONNECT; 304052d34f98S[email protected] hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // authentication done 3041ad83dc6aS[email protected] return; 3042ad83dc6aS[email protected] } 304376f27cffSMatthias Ringwald 304434d2123cS[email protected] if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){ 304534d2123cS[email protected] connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST; 304634d2123cS[email protected] hci_send_cmd(&hci_authentication_requested, connection->con_handle); 30472bd8b7e7S[email protected] return; 3048afd4e962S[email protected] } 304976f27cffSMatthias Ringwald 3050dce78009S[email protected] if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){ 3051dce78009S[email protected] connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST; 3052dce78009S[email protected] hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1); 3053dce78009S[email protected] return; 3054dce78009S[email protected] } 305576f27cffSMatthias Ringwald #endif 305676f27cffSMatthias Ringwald 305776f27cffSMatthias Ringwald if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){ 305876f27cffSMatthias Ringwald connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK; 305976f27cffSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005); // authentication failure 306076f27cffSMatthias Ringwald return; 306176f27cffSMatthias Ringwald } 3062da886c03S[email protected] 3063a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 3064da886c03S[email protected] if (connection->le_con_parameter_update_state == CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS){ 3065da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 3066da886c03S[email protected] 3067c37a3166S[email protected] uint16_t connection_interval_min = connection->le_conn_interval_min; 3068c37a3166S[email protected] connection->le_conn_interval_min = 0; 3069c37a3166S[email protected] hci_send_cmd(&hci_le_connection_update, connection->con_handle, connection_interval_min, 3070c37a3166S[email protected] connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout, 3071c37a3166S[email protected] 0x0000, 0xffff); 3072c37a3166S[email protected] } 3073c37a3166S[email protected] #endif 3074dbe1a790S[email protected] } 3075c7e0c5f6Smatthias.ringwald 307605ae8de3SMatthias Ringwald hci_connection_t * connection; 30773a9fb326S[email protected] switch (hci_stack->state){ 30783429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 307974b323a9SMatthias Ringwald hci_initializing_run(); 30803429f56bSmatthias.ringwald break; 3081c7e0c5f6Smatthias.ringwald 3082c7e0c5f6Smatthias.ringwald case HCI_STATE_HALTING: 3083c7e0c5f6Smatthias.ringwald 30849da54300S[email protected] log_info("HCI_STATE_HALTING"); 30859956955bSMatthias Ringwald 30869956955bSMatthias Ringwald // free whitelist entries 3087a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 3088d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 30899956955bSMatthias Ringwald { 3090665d90f2SMatthias Ringwald btstack_linked_list_iterator_t lit; 3091665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 3092665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 3093665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 3094665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry); 30959956955bSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 30969956955bSMatthias Ringwald } 30979956955bSMatthias Ringwald } 30989956955bSMatthias Ringwald #endif 3099d70217a2SMatthias Ringwald #endif 3100c7e0c5f6Smatthias.ringwald // close all open connections 31013a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 3102c7e0c5f6Smatthias.ringwald if (connection){ 3103711e6c80SMatthias Ringwald hci_con_handle_t con_handle = (uint16_t) connection->con_handle; 3104d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 310532ab9390Smatthias.ringwald 31068837e9efSMatthias Ringwald log_info("HCI_STATE_HALTING, connection %p, handle %u", connection, con_handle); 3107c7e0c5f6Smatthias.ringwald 31088837e9efSMatthias Ringwald // cancel all l2cap connections right away instead of waiting for disconnection complete event ... 31098837e9efSMatthias Ringwald hci_emit_disconnection_complete(con_handle, 0x16); // terminated by local host 31108837e9efSMatthias Ringwald 31118837e9efSMatthias Ringwald // ... which would be ignored anyway as we shutdown (free) the connection now 3112c7e0c5f6Smatthias.ringwald hci_shutdown_connection(connection); 31138837e9efSMatthias Ringwald 31148837e9efSMatthias Ringwald // finally, send the disconnect command 31158837e9efSMatthias Ringwald hci_send_cmd(&hci_disconnect, con_handle, 0x13); // remote closed connection 3116c7e0c5f6Smatthias.ringwald return; 3117c7e0c5f6Smatthias.ringwald } 31189da54300S[email protected] log_info("HCI_STATE_HALTING, calling off"); 3119c7e0c5f6Smatthias.ringwald 312072ea5239Smatthias.ringwald // switch mode 3121c7e0c5f6Smatthias.ringwald hci_power_control_off(); 31229418f9c9Smatthias.ringwald 31239da54300S[email protected] log_info("HCI_STATE_HALTING, emitting state"); 312472ea5239Smatthias.ringwald hci_emit_state(); 31259da54300S[email protected] log_info("HCI_STATE_HALTING, done"); 312672ea5239Smatthias.ringwald break; 3127c7e0c5f6Smatthias.ringwald 312872ea5239Smatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 31293a9fb326S[email protected] switch(hci_stack->substate) { 313074b323a9SMatthias Ringwald case HCI_FALLING_ASLEEP_DISCONNECT: 31319da54300S[email protected] log_info("HCI_STATE_FALLING_ASLEEP"); 313272ea5239Smatthias.ringwald // close all open connections 31333a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 313466da7044Smatthias.ringwald 3135423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 313666da7044Smatthias.ringwald // don't close connections, if H4 supports power management 3137d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 313866da7044Smatthias.ringwald connection = NULL; 313966da7044Smatthias.ringwald } 314066da7044Smatthias.ringwald #endif 314172ea5239Smatthias.ringwald if (connection){ 314232ab9390Smatthias.ringwald 314372ea5239Smatthias.ringwald // send disconnect 3144d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 314532ab9390Smatthias.ringwald 31469da54300S[email protected] log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u", connection, (uint16_t)connection->con_handle); 31476ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 314872ea5239Smatthias.ringwald 314972ea5239Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 315072ea5239Smatthias.ringwald hci_shutdown_connection(connection); 315172ea5239Smatthias.ringwald return; 315272ea5239Smatthias.ringwald } 315372ea5239Smatthias.ringwald 315492368cd3S[email protected] if (hci_classic_supported()){ 315589db417bSmatthias.ringwald // disable page and inquiry scan 3156d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 315732ab9390Smatthias.ringwald 31589da54300S[email protected] log_info("HCI_STATE_HALTING, disabling inq scans"); 31593a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan 316089db417bSmatthias.ringwald 316189db417bSmatthias.ringwald // continue in next sub state 316274b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE; 316389db417bSmatthias.ringwald break; 316492368cd3S[email protected] } 3165202c8a4cSMatthias Ringwald // no break - fall through for ble-only chips 316692368cd3S[email protected] 316774b323a9SMatthias Ringwald case HCI_FALLING_ASLEEP_COMPLETE: 31689da54300S[email protected] log_info("HCI_STATE_HALTING, calling sleep"); 3169423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 317028171530Smatthias.ringwald // don't actually go to sleep, if H4 supports power management 3171d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 317228171530Smatthias.ringwald // SLEEP MODE reached 31733a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 317428171530Smatthias.ringwald hci_emit_state(); 317528171530Smatthias.ringwald break; 317628171530Smatthias.ringwald } 317728171530Smatthias.ringwald #endif 317872ea5239Smatthias.ringwald // switch mode 31793a9fb326S[email protected] hci_power_control_sleep(); // changes hci_stack->state to SLEEP 3180c7e0c5f6Smatthias.ringwald hci_emit_state(); 318128171530Smatthias.ringwald break; 318228171530Smatthias.ringwald 318389db417bSmatthias.ringwald default: 318489db417bSmatthias.ringwald break; 318589db417bSmatthias.ringwald } 3186c7e0c5f6Smatthias.ringwald break; 3187c7e0c5f6Smatthias.ringwald 31883429f56bSmatthias.ringwald default: 31893429f56bSmatthias.ringwald break; 31901f504dbdSmatthias.ringwald } 31913429f56bSmatthias.ringwald } 319216833f0aSmatthias.ringwald 319331452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 319435454696SMatthias Ringwald // house-keeping 319535454696SMatthias Ringwald 319635454696SMatthias Ringwald if (IS_COMMAND(packet, hci_write_loopback_mode)){ 319735454696SMatthias Ringwald hci_stack->loopback_mode = packet[3]; 319835454696SMatthias Ringwald } 319935454696SMatthias Ringwald 320035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 3201c8e4258aSmatthias.ringwald bd_addr_t addr; 3202c8e4258aSmatthias.ringwald hci_connection_t * conn; 3203c8e4258aSmatthias.ringwald 3204c8e4258aSmatthias.ringwald // create_connection? 3205c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 3206724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 32079da54300S[email protected] log_info("Create_connection to %s", bd_addr_to_str(addr)); 3208c8e4258aSmatthias.ringwald 32092e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 3210ad83dc6aS[email protected] if (!conn){ 321196a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 321217f1ba2aSmatthias.ringwald if (!conn){ 321317f1ba2aSmatthias.ringwald // notify client that alloc failed 32142deddeceSMatthias Ringwald hci_emit_connection_complete(addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 321517f1ba2aSmatthias.ringwald return 0; // don't sent packet to controller 321617f1ba2aSmatthias.ringwald } 3217ad83dc6aS[email protected] conn->state = SEND_CREATE_CONNECTION; 3218ad83dc6aS[email protected] } 3219ad83dc6aS[email protected] log_info("conn state %u", conn->state); 3220ad83dc6aS[email protected] switch (conn->state){ 3221ad83dc6aS[email protected] // if connection active exists 3222ad83dc6aS[email protected] case OPEN: 322362bda3fbS[email protected] // and OPEN, emit connection complete command, don't send to controller 3224274dad91SMatthias Ringwald hci_emit_connection_complete(addr, conn->con_handle, 0); 322562bda3fbS[email protected] return 0; 3226ad83dc6aS[email protected] case SEND_CREATE_CONNECTION: 3227ad83dc6aS[email protected] // connection created by hci, e.g. dedicated bonding 3228ad83dc6aS[email protected] break; 3229ad83dc6aS[email protected] default: 3230ad83dc6aS[email protected] // otherwise, just ignore as it is already in the open process 3231ad83dc6aS[email protected] return 0; 3232ad83dc6aS[email protected] } 3233c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 3234c8e4258aSmatthias.ringwald } 323535454696SMatthias Ringwald 32367fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_reply)){ 32377fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY); 32387fde4af9Smatthias.ringwald } 32397fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){ 32407fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST); 32417fde4af9Smatthias.ringwald } 32427fde4af9Smatthias.ringwald 32438ef73945Smatthias.ringwald if (IS_COMMAND(packet, hci_delete_stored_link_key)){ 3244a98592bcSMatthias Ringwald if (hci_stack->link_key_db){ 3245724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 3246a98592bcSMatthias Ringwald hci_stack->link_key_db->delete_link_key(addr); 32478ef73945Smatthias.ringwald } 32488ef73945Smatthias.ringwald } 3249c8e4258aSmatthias.ringwald 32506724cd9eS[email protected] if (IS_COMMAND(packet, hci_pin_code_request_negative_reply) 32516724cd9eS[email protected] || IS_COMMAND(packet, hci_pin_code_request_reply)){ 3252724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 32532e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 32546724cd9eS[email protected] if (conn){ 32556724cd9eS[email protected] connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE); 32566724cd9eS[email protected] } 32576724cd9eS[email protected] } 32586724cd9eS[email protected] 32596724cd9eS[email protected] if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply) 32606724cd9eS[email protected] || IS_COMMAND(packet, hci_user_confirmation_request_reply) 32616724cd9eS[email protected] || IS_COMMAND(packet, hci_user_passkey_request_negative_reply) 32626724cd9eS[email protected] || IS_COMMAND(packet, hci_user_passkey_request_reply)) { 3263724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 32642e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 32656724cd9eS[email protected] if (conn){ 32666724cd9eS[email protected] connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE); 32676724cd9eS[email protected] } 32686724cd9eS[email protected] } 3269ee752bb8SMatthias Ringwald 3270ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 3271ee752bb8SMatthias Ringwald // setup_synchronous_connection? Voice setting at offset 22 3272ee752bb8SMatthias Ringwald if (IS_COMMAND(packet, hci_setup_synchronous_connection)){ 3273ee752bb8SMatthias Ringwald // TODO: compare to current setting if sco connection already active 3274ee752bb8SMatthias Ringwald hci_stack->sco_voice_setting_active = little_endian_read_16(packet, 15); 3275ee752bb8SMatthias Ringwald } 3276ee752bb8SMatthias Ringwald // accept_synchronus_connection? Voice setting at offset 18 3277ee752bb8SMatthias Ringwald if (IS_COMMAND(packet, hci_accept_synchronous_connection)){ 3278ee752bb8SMatthias Ringwald // TODO: compare to current setting if sco connection already active 3279ee752bb8SMatthias Ringwald hci_stack->sco_voice_setting_active = little_endian_read_16(packet, 19); 3280ee752bb8SMatthias Ringwald } 3281ee752bb8SMatthias Ringwald #endif 328235454696SMatthias Ringwald #endif 32834b3e1e19SMatthias Ringwald 3284a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 3285d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 328669a97523S[email protected] if (IS_COMMAND(packet, hci_le_set_random_address)){ 3287b95a5a35SMatthias Ringwald hci_stack->le_random_address_set = 1; 3288b95a5a35SMatthias Ringwald reverse_bd_addr(&packet[3], hci_stack->le_random_address); 3289d70217a2SMatthias Ringwald } 3290171293d3SMatthias Ringwald if (IS_COMMAND(packet, hci_le_set_advertise_enable)){ 3291171293d3SMatthias Ringwald hci_stack->le_advertisements_active = packet[3]; 3292171293d3SMatthias Ringwald } 3293d70217a2SMatthias Ringwald #endif 3294d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 3295b04dfa37SMatthias Ringwald if (IS_COMMAND(packet, hci_le_create_connection)){ 3296b04dfa37SMatthias Ringwald // white list used? 3297b04dfa37SMatthias Ringwald uint8_t initiator_filter_policy = packet[7]; 3298b04dfa37SMatthias Ringwald switch (initiator_filter_policy){ 3299b04dfa37SMatthias Ringwald case 0: 3300b04dfa37SMatthias Ringwald // whitelist not used 3301b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_DIRECT; 3302b04dfa37SMatthias Ringwald break; 3303b04dfa37SMatthias Ringwald case 1: 3304b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_WHITELIST; 3305b04dfa37SMatthias Ringwald break; 3306b04dfa37SMatthias Ringwald default: 3307b04dfa37SMatthias Ringwald log_error("Invalid initiator_filter_policy in LE Create Connection %u", initiator_filter_policy); 3308b04dfa37SMatthias Ringwald break; 3309b04dfa37SMatthias Ringwald } 3310b04dfa37SMatthias Ringwald } 3311b04dfa37SMatthias Ringwald if (IS_COMMAND(packet, hci_le_create_connection_cancel)){ 3312b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 3313b04dfa37SMatthias Ringwald } 331469a97523S[email protected] #endif 3315d70217a2SMatthias Ringwald #endif 331669a97523S[email protected] 33173a9fb326S[email protected] hci_stack->num_cmd_packets--; 33185bb5bc3eS[email protected] 33195bb5bc3eS[email protected] hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size); 33206b4af23dS[email protected] int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 33216b4af23dS[email protected] 3322d051460cS[email protected] // release packet buffer for synchronous transport implementations 3323c8b9416aS[email protected] if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){ 33246b4af23dS[email protected] hci_stack->hci_packet_buffer_reserved = 0; 33256b4af23dS[email protected] } 33266b4af23dS[email protected] 33276b4af23dS[email protected] return err; 332831452debSmatthias.ringwald } 33298adf0ddaSmatthias.ringwald 33302bd8b7e7S[email protected] // disconnect because of security block 33312bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){ 33322bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 33332bd8b7e7S[email protected] if (!connection) return; 33342bd8b7e7S[email protected] connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 33352bd8b7e7S[email protected] } 33362bd8b7e7S[email protected] 33372bd8b7e7S[email protected] 3338dbe1a790S[email protected] // Configure Secure Simple Pairing 3339dbe1a790S[email protected] 334035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 334135454696SMatthias Ringwald 3342dbe1a790S[email protected] // enable will enable SSP during init 334315a95bd5SMatthias Ringwald void gap_ssp_set_enable(int enable){ 33443a9fb326S[email protected] hci_stack->ssp_enable = enable; 3345dbe1a790S[email protected] } 3346dbe1a790S[email protected] 334795d71764SMatthias Ringwald static int hci_local_ssp_activated(void){ 334815a95bd5SMatthias Ringwald return gap_ssp_supported() && hci_stack->ssp_enable; 33492bd8b7e7S[email protected] } 33502bd8b7e7S[email protected] 3351dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement 335215a95bd5SMatthias Ringwald void gap_ssp_set_io_capability(int io_capability){ 33533a9fb326S[email protected] hci_stack->ssp_io_capability = io_capability; 3354dbe1a790S[email protected] } 335515a95bd5SMatthias Ringwald void gap_ssp_set_authentication_requirement(int authentication_requirement){ 33563a9fb326S[email protected] hci_stack->ssp_authentication_requirement = authentication_requirement; 3357dbe1a790S[email protected] } 3358dbe1a790S[email protected] 3359dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested 336015a95bd5SMatthias Ringwald void gap_ssp_set_auto_accept(int auto_accept){ 33613a9fb326S[email protected] hci_stack->ssp_auto_accept = auto_accept; 3362dbe1a790S[email protected] } 336335454696SMatthias Ringwald #endif 3364dbe1a790S[email protected] 336594be1a66SMatthias Ringwald // va_list part of hci_send_cmd 336694be1a66SMatthias Ringwald int hci_send_cmd_va_arg(const hci_cmd_t *cmd, va_list argptr){ 3367d94d3cafS[email protected] if (!hci_can_send_command_packet_now()){ 33689d14b626S[email protected] log_error("hci_send_cmd called but cannot send packet now"); 33699d14b626S[email protected] return 0; 33709d14b626S[email protected] } 33719d14b626S[email protected] 33725127cc62S[email protected] // for HCI INITIALIZATION 33739da54300S[email protected] // log_info("hci_send_cmd: opcode %04x", cmd->opcode); 33745127cc62S[email protected] hci_stack->last_cmd_opcode = cmd->opcode; 33755127cc62S[email protected] 33769d14b626S[email protected] hci_reserve_packet_buffer(); 33779d14b626S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 337894be1a66SMatthias Ringwald uint16_t size = hci_cmd_create_from_template(packet, cmd, argptr); 337994be1a66SMatthias Ringwald return hci_send_cmd_packet(packet, size); 338094be1a66SMatthias Ringwald } 33819d14b626S[email protected] 338294be1a66SMatthias Ringwald /** 338394be1a66SMatthias Ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 338494be1a66SMatthias Ringwald */ 338594be1a66SMatthias Ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){ 33861cd208adSmatthias.ringwald va_list argptr; 33871cd208adSmatthias.ringwald va_start(argptr, cmd); 338894be1a66SMatthias Ringwald int res = hci_send_cmd_va_arg(cmd, argptr); 33891cd208adSmatthias.ringwald va_end(argptr); 339094be1a66SMatthias Ringwald return res; 339193b8dc03Smatthias.ringwald } 3392c8e4258aSmatthias.ringwald 3393ee091cf1Smatthias.ringwald // Create various non-HCI events. 3394ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 3395ee091cf1Smatthias.ringwald 3396d6b06661SMatthias Ringwald static void hci_emit_event(uint8_t * event, uint16_t size, int dump){ 3397fb37a842SMatthias Ringwald // dump packet 3398d6b06661SMatthias Ringwald if (dump) { 3399300c1ba4SMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, size); 3400d6b06661SMatthias Ringwald } 34011ef6bb52SMatthias Ringwald 3402fb37a842SMatthias Ringwald // dispatch to all event handlers 34031ef6bb52SMatthias Ringwald btstack_linked_list_iterator_t it; 34041ef6bb52SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->event_handlers); 34051ef6bb52SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 34061ef6bb52SMatthias Ringwald btstack_packet_callback_registration_t * entry = (btstack_packet_callback_registration_t*) btstack_linked_list_iterator_next(&it); 3407d9a7306aSMatthias Ringwald entry->callback(HCI_EVENT_PACKET, 0, event, size); 34081ef6bb52SMatthias Ringwald } 3409d6b06661SMatthias Ringwald } 3410d6b06661SMatthias Ringwald 3411d6b06661SMatthias Ringwald static void hci_emit_acl_packet(uint8_t * packet, uint16_t size){ 3412fb37a842SMatthias Ringwald if (!hci_stack->acl_packet_handler) return; 34133d50b4baSMatthias Ringwald hci_stack->acl_packet_handler(HCI_ACL_DATA_PACKET, 0, packet, size); 3414d6b06661SMatthias Ringwald } 3415d6b06661SMatthias Ringwald 341635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 3417701e3307SMatthias Ringwald static void hci_notify_if_sco_can_send_now(void){ 34183bc639ceSMatthias Ringwald // notify SCO sender if waiting 3419701e3307SMatthias Ringwald if (!hci_stack->sco_waiting_for_can_send_now) return; 3420701e3307SMatthias Ringwald if (hci_can_send_sco_packet_now()){ 34213bc639ceSMatthias Ringwald hci_stack->sco_waiting_for_can_send_now = 0; 3422701e3307SMatthias Ringwald uint8_t event[2] = { HCI_EVENT_SCO_CAN_SEND_NOW, 0 }; 3423701e3307SMatthias Ringwald hci_dump_packet(HCI_EVENT_PACKET, 1, event, sizeof(event)); 34243d50b4baSMatthias Ringwald hci_stack->sco_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event)); 34253bc639ceSMatthias Ringwald } 34263bc639ceSMatthias Ringwald } 34271cfb383eSMatthias Ringwald 34281cfb383eSMatthias Ringwald // parsing end emitting has been merged to reduce code size 34291cfb383eSMatthias Ringwald static void gap_inquiry_explode(uint8_t * packet){ 34301cfb383eSMatthias Ringwald uint8_t event[15+GAP_INQUIRY_MAX_NAME_LEN]; 34311cfb383eSMatthias Ringwald 34321cfb383eSMatthias Ringwald uint8_t * eir_data; 34331cfb383eSMatthias Ringwald ad_context_t context; 34341cfb383eSMatthias Ringwald const uint8_t * name; 34351cfb383eSMatthias Ringwald uint8_t name_len; 34361cfb383eSMatthias Ringwald 34371cfb383eSMatthias Ringwald int event_type = hci_event_packet_get_type(packet); 34381cfb383eSMatthias Ringwald int num_reserved_fields = event_type == HCI_EVENT_INQUIRY_RESULT ? 2 : 1; // 2 for old event, 1 otherwise 34391cfb383eSMatthias Ringwald int num_responses = hci_event_inquiry_result_get_num_responses(packet); 34401cfb383eSMatthias Ringwald 34411cfb383eSMatthias Ringwald // event[1] is set at the end 34421cfb383eSMatthias Ringwald int i; 34431cfb383eSMatthias Ringwald for (i=0; i<num_responses;i++){ 34441cfb383eSMatthias Ringwald memset(event, 0, sizeof(event)); 34451cfb383eSMatthias Ringwald event[0] = GAP_EVENT_INQUIRY_RESULT; 34461cfb383eSMatthias Ringwald uint8_t event_size = 18; // if name is not set by EIR 34471cfb383eSMatthias Ringwald 34481cfb383eSMatthias Ringwald memcpy(&event[2], &packet[3 + i*6], 6); // bd_addr 34491cfb383eSMatthias Ringwald event[8] = packet[3 + num_responses*(6) + i*1]; // page_scan_repetition_mode 34501cfb383eSMatthias Ringwald memcpy(&event[9], &packet[3 + num_responses*(6+1+num_reserved_fields) + i*3], 3); // class of device 34511cfb383eSMatthias Ringwald memcpy(&event[12], &packet[3 + num_responses*(6+1+num_reserved_fields+3) + i*2], 2); // clock offset 34521cfb383eSMatthias Ringwald 34531cfb383eSMatthias Ringwald switch (event_type){ 34541cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 34551cfb383eSMatthias Ringwald // 14,15,16,17 = 0, size 18 34561cfb383eSMatthias Ringwald break; 34571cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 34581cfb383eSMatthias Ringwald event[14] = 1; 34591cfb383eSMatthias Ringwald event[15] = packet [3 + num_responses*(6+1+num_reserved_fields+3+2) + i*1]; // rssi 34601cfb383eSMatthias Ringwald // 16,17 = 0, size 18 34611cfb383eSMatthias Ringwald break; 34621cfb383eSMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 34631cfb383eSMatthias Ringwald event[14] = 1; 34641cfb383eSMatthias Ringwald event[15] = packet [3 + num_responses*(6+1+num_reserved_fields+3+2) + i*1]; // rssi 34651cfb383eSMatthias Ringwald // for EIR packets, there is only one reponse in it 34661cfb383eSMatthias Ringwald eir_data = &packet[3 + (6+1+num_reserved_fields+3+2+1)]; 34671cfb383eSMatthias Ringwald name = NULL; 34681cfb383eSMatthias Ringwald // EIR data is 240 bytes in EIR event 34691cfb383eSMatthias Ringwald for (ad_iterator_init(&context, 240, eir_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)){ 34701cfb383eSMatthias Ringwald uint8_t data_type = ad_iterator_get_data_type(&context); 34711cfb383eSMatthias Ringwald uint8_t data_size = ad_iterator_get_data_len(&context); 34721cfb383eSMatthias Ringwald const uint8_t * data = ad_iterator_get_data(&context); 34731cfb383eSMatthias Ringwald // Prefer Complete Local Name over Shortend Local Name 34741cfb383eSMatthias Ringwald switch (data_type){ 34751cfb383eSMatthias Ringwald case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME: 34761cfb383eSMatthias Ringwald if (name) continue; 34771cfb383eSMatthias Ringwald /* explicit fall-through */ 34781cfb383eSMatthias Ringwald case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME: 34791cfb383eSMatthias Ringwald name = data; 34801cfb383eSMatthias Ringwald name_len = data_size; 34811cfb383eSMatthias Ringwald break; 34821cfb383eSMatthias Ringwald default: 34831cfb383eSMatthias Ringwald break; 34841cfb383eSMatthias Ringwald } 34851cfb383eSMatthias Ringwald } 34861cfb383eSMatthias Ringwald if (name){ 34871cfb383eSMatthias Ringwald event[16] = 1; 34881cfb383eSMatthias Ringwald // truncate name if needed 34891cfb383eSMatthias Ringwald int len = btstack_min(name_len, GAP_INQUIRY_MAX_NAME_LEN); 34901cfb383eSMatthias Ringwald event[17] = len; 34911cfb383eSMatthias Ringwald memcpy(&event[18], name, len); 34921cfb383eSMatthias Ringwald event_size += len; 34931cfb383eSMatthias Ringwald } 34941cfb383eSMatthias Ringwald break; 34951cfb383eSMatthias Ringwald } 34961cfb383eSMatthias Ringwald event[1] = event_size - 2; 34971cfb383eSMatthias Ringwald hci_emit_event(event, event_size, 1); 34981cfb383eSMatthias Ringwald } 34991cfb383eSMatthias Ringwald } 350035454696SMatthias Ringwald #endif 35013bc639ceSMatthias Ringwald 350271de195eSMatthias Ringwald void hci_emit_state(void){ 35033a9fb326S[email protected] log_info("BTSTACK_EVENT_STATE %u", hci_stack->state); 3504425d1371Smatthias.ringwald uint8_t event[3]; 350580d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 3506425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 35073a9fb326S[email protected] event[2] = hci_stack->state; 3508d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3509c8e4258aSmatthias.ringwald } 3510c8e4258aSmatthias.ringwald 351135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 35122deddeceSMatthias Ringwald static void hci_emit_connection_complete(bd_addr_t address, hci_con_handle_t con_handle, uint8_t status){ 3513425d1371Smatthias.ringwald uint8_t event[13]; 3514c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 3515425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 351617f1ba2aSmatthias.ringwald event[2] = status; 35172deddeceSMatthias Ringwald little_endian_store_16(event, 3, con_handle); 35182deddeceSMatthias Ringwald reverse_bd_addr(address, &event[5]); 3519c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 3520c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 3521d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3522c8e4258aSmatthias.ringwald } 352352db98b2SMatthias Ringwald static void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 352452db98b2SMatthias Ringwald if (disable_l2cap_timeouts) return; 352552db98b2SMatthias Ringwald log_info("L2CAP_EVENT_TIMEOUT_CHECK"); 352652db98b2SMatthias Ringwald uint8_t event[4]; 352752db98b2SMatthias Ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 352852db98b2SMatthias Ringwald event[1] = sizeof(event) - 2; 352952db98b2SMatthias Ringwald little_endian_store_16(event, 2, conn->con_handle); 353052db98b2SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 353152db98b2SMatthias Ringwald } 353235454696SMatthias Ringwald #endif 3533c8e4258aSmatthias.ringwald 353435454696SMatthias Ringwald #ifdef ENABLE_BLE 3535d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 3536fc64f94aSMatthias 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){ 35374f3229d8S[email protected] uint8_t event[21]; 35384f3229d8S[email protected] event[0] = HCI_EVENT_LE_META; 35394f3229d8S[email protected] event[1] = sizeof(event) - 2; 35404f3229d8S[email protected] event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE; 35414f3229d8S[email protected] event[3] = status; 3542fc64f94aSMatthias Ringwald little_endian_store_16(event, 4, con_handle); 35434f3229d8S[email protected] event[6] = 0; // TODO: role 35446e2e9a6bS[email protected] event[7] = address_type; 3545724d70a2SMatthias Ringwald reverse_bd_addr(address, &event[8]); 3546f8fbdce0SMatthias Ringwald little_endian_store_16(event, 14, 0); // interval 3547f8fbdce0SMatthias Ringwald little_endian_store_16(event, 16, 0); // latency 3548f8fbdce0SMatthias Ringwald little_endian_store_16(event, 18, 0); // supervision timeout 35494f3229d8S[email protected] event[20] = 0; // master clock accuracy 3550d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 35514f3229d8S[email protected] } 355235454696SMatthias Ringwald #endif 3553d70217a2SMatthias Ringwald #endif 35544f3229d8S[email protected] 3555fc64f94aSMatthias Ringwald static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason){ 3556425d1371Smatthias.ringwald uint8_t event[6]; 35573c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 3558e518c4b8Smatthias.ringwald event[1] = sizeof(event) - 2; 35593c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 3560fc64f94aSMatthias Ringwald little_endian_store_16(event, 3, con_handle); 35613c4d4b90Smatthias.ringwald event[5] = reason; 3562d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 35633c4d4b90Smatthias.ringwald } 35643c4d4b90Smatthias.ringwald 3565b83d5eabSMatthias Ringwald static void hci_emit_nr_connections_changed(void){ 3566e0abb8e7S[email protected] log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections()); 3567425d1371Smatthias.ringwald uint8_t event[3]; 356880d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 3569425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 357043bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 3571d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 357243bfb1bdSmatthias.ringwald } 3573038bc64cSmatthias.ringwald 3574b83d5eabSMatthias Ringwald static void hci_emit_hci_open_failed(void){ 3575e0abb8e7S[email protected] log_info("BTSTACK_EVENT_POWERON_FAILED"); 3576425d1371Smatthias.ringwald uint8_t event[2]; 357780d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 3578425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 3579d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3580038bc64cSmatthias.ringwald } 35811b0e3922Smatthias.ringwald 358235454696SMatthias Ringwald static void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status){ 358335454696SMatthias Ringwald log_info("hci_emit_dedicated_bonding_result %u ", status); 358435454696SMatthias Ringwald uint8_t event[9]; 358535454696SMatthias Ringwald int pos = 0; 358635454696SMatthias Ringwald event[pos++] = GAP_EVENT_DEDICATED_BONDING_COMPLETED; 358735454696SMatthias Ringwald event[pos++] = sizeof(event) - 2; 358835454696SMatthias Ringwald event[pos++] = status; 358935454696SMatthias Ringwald reverse_bd_addr(address, &event[pos]); 3590d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3591381fbed8Smatthias.ringwald } 3592458bf4e8S[email protected] 359335454696SMatthias Ringwald 359435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 359535454696SMatthias Ringwald 3596b83d5eabSMatthias Ringwald static void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){ 3597df3354fcS[email protected] log_info("hci_emit_security_level %u for handle %x", level, con_handle); 3598a00031e2S[email protected] uint8_t event[5]; 3599e00caf9cS[email protected] int pos = 0; 36005611a760SMatthias Ringwald event[pos++] = GAP_EVENT_SECURITY_LEVEL; 3601e00caf9cS[email protected] event[pos++] = sizeof(event) - 2; 3602f8fbdce0SMatthias Ringwald little_endian_store_16(event, 2, con_handle); 3603e00caf9cS[email protected] pos += 2; 3604e00caf9cS[email protected] event[pos++] = level; 3605d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3606e00caf9cS[email protected] } 3607e00caf9cS[email protected] 360835454696SMatthias Ringwald static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){ 360935454696SMatthias Ringwald if (!connection) return LEVEL_0; 361035454696SMatthias Ringwald if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0; 361135454696SMatthias Ringwald return gap_security_level_for_link_key_type(connection->link_key_type); 361235454696SMatthias Ringwald } 361335454696SMatthias Ringwald 361435454696SMatthias Ringwald static void hci_emit_discoverable_enabled(uint8_t enabled){ 361535454696SMatthias Ringwald log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled); 361635454696SMatthias Ringwald uint8_t event[3]; 361735454696SMatthias Ringwald event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED; 361835454696SMatthias Ringwald event[1] = sizeof(event) - 2; 361935454696SMatthias Ringwald event[2] = enabled; 3620d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3621ad83dc6aS[email protected] } 3622ad83dc6aS[email protected] 362306b9e820SMatthias Ringwald #ifdef ENABLE_CLASSIC 362498a2fd1cSMatthias Ringwald // query if remote side supports eSCO 3625073bd0faSMatthias Ringwald int hci_remote_esco_supported(hci_con_handle_t con_handle){ 362698a2fd1cSMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 362798a2fd1cSMatthias Ringwald if (!connection) return 0; 362898a2fd1cSMatthias Ringwald return connection->remote_supported_feature_eSCO; 362998a2fd1cSMatthias Ringwald } 363098a2fd1cSMatthias Ringwald 36312bd8b7e7S[email protected] // query if remote side supports SSP 36322bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){ 36332bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 36342bd8b7e7S[email protected] if (!connection) return 0; 36352bd8b7e7S[email protected] return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0; 36362bd8b7e7S[email protected] } 36372bd8b7e7S[email protected] 363815a95bd5SMatthias Ringwald int gap_ssp_supported_on_both_sides(hci_con_handle_t handle){ 3639df3354fcS[email protected] return hci_local_ssp_activated() && hci_remote_ssp_supported(handle); 3640df3354fcS[email protected] } 364106b9e820SMatthias Ringwald #endif 3642df3354fcS[email protected] 3643458bf4e8S[email protected] // GAP API 3644458bf4e8S[email protected] /** 3645458bf4e8S[email protected] * @bbrief enable/disable bonding. default is enabled 3646458bf4e8S[email protected] * @praram enabled 3647458bf4e8S[email protected] */ 36484c57c146S[email protected] void gap_set_bondable_mode(int enable){ 36493a9fb326S[email protected] hci_stack->bondable = enable ? 1 : 0; 3650458bf4e8S[email protected] } 36514ef6443cSMatthias Ringwald /** 36524ef6443cSMatthias Ringwald * @brief Get bondable mode. 36534ef6443cSMatthias Ringwald * @return 1 if bondable 36544ef6443cSMatthias Ringwald */ 36554ef6443cSMatthias Ringwald int gap_get_bondable_mode(void){ 36564ef6443cSMatthias Ringwald return hci_stack->bondable; 36574ef6443cSMatthias Ringwald } 3658cb230b9dS[email protected] 3659cb230b9dS[email protected] /** 366034d2123cS[email protected] * @brief map link keys to security levels 3661cb230b9dS[email protected] */ 366234d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){ 366334d2123cS[email protected] switch (link_key_type){ 36643c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 36653c68dfa9S[email protected] return LEVEL_4; 36663c68dfa9S[email protected] case COMBINATION_KEY: 36673c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 36683c68dfa9S[email protected] return LEVEL_3; 36693c68dfa9S[email protected] default: 36703c68dfa9S[email protected] return LEVEL_2; 36713c68dfa9S[email protected] } 3672cb230b9dS[email protected] } 3673cb230b9dS[email protected] 3674106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){ 36755127cc62S[email protected] log_info("gap_mitm_protection_required_for_security_level %u", level); 3676106d6d11S[email protected] return level > LEVEL_2; 3677106d6d11S[email protected] } 3678106d6d11S[email protected] 367934d2123cS[email protected] /** 368034d2123cS[email protected] * @brief get current security level 368134d2123cS[email protected] */ 368234d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){ 368334d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 368434d2123cS[email protected] if (!connection) return LEVEL_0; 368534d2123cS[email protected] return gap_security_level_for_connection(connection); 368634d2123cS[email protected] } 368734d2123cS[email protected] 3688cb230b9dS[email protected] /** 3689cb230b9dS[email protected] * @brief request connection to device to 3690cb230b9dS[email protected] * @result GAP_AUTHENTICATION_RESULT 3691cb230b9dS[email protected] */ 369234d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){ 369334d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 369434d2123cS[email protected] if (!connection){ 3695a00031e2S[email protected] hci_emit_security_level(con_handle, LEVEL_0); 369634d2123cS[email protected] return; 369734d2123cS[email protected] } 369834d2123cS[email protected] gap_security_level_t current_level = gap_security_level(con_handle); 369934d2123cS[email protected] log_info("gap_request_security_level %u, current level %u", requested_level, current_level); 370034d2123cS[email protected] if (current_level >= requested_level){ 3701a00031e2S[email protected] hci_emit_security_level(con_handle, current_level); 370234d2123cS[email protected] return; 370334d2123cS[email protected] } 3704a00031e2S[email protected] 370534d2123cS[email protected] connection->requested_security_level = requested_level; 3706a00031e2S[email protected] 370725bf5872S[email protected] #if 0 370825bf5872S[email protected] // sending encryption request without a link key results in an error. 370925bf5872S[email protected] // TODO: figure out how to use it properly 371025bf5872S[email protected] 3711fb8ba0dbS[email protected] // would enabling ecnryption suffice (>= LEVEL_2)? 3712a98592bcSMatthias Ringwald if (hci_stack->link_key_db){ 3713a00031e2S[email protected] link_key_type_t link_key_type; 3714a00031e2S[email protected] link_key_t link_key; 3715a98592bcSMatthias Ringwald if (hci_stack->link_key_db->get_link_key( &connection->address, &link_key, &link_key_type)){ 3716a00031e2S[email protected] if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){ 3717a00031e2S[email protected] connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 3718a00031e2S[email protected] return; 3719a00031e2S[email protected] } 3720a00031e2S[email protected] } 3721a00031e2S[email protected] } 372225bf5872S[email protected] #endif 3723a00031e2S[email protected] 37241eb2563eS[email protected] // try to authenticate connection 37251eb2563eS[email protected] connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 3726e80b2cf9S[email protected] hci_run(); 3727e00caf9cS[email protected] } 3728ad83dc6aS[email protected] 3729ad83dc6aS[email protected] /** 3730ad83dc6aS[email protected] * @brief start dedicated bonding with device. disconnect after bonding 3731ad83dc6aS[email protected] * @param device 3732ad83dc6aS[email protected] * @param request MITM protection 3733ad83dc6aS[email protected] * @result GAP_DEDICATED_BONDING_COMPLETE 3734ad83dc6aS[email protected] */ 3735ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){ 3736ad83dc6aS[email protected] 3737ad83dc6aS[email protected] // create connection state machine 373896a45072S[email protected] hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_CLASSIC); 3739ad83dc6aS[email protected] 3740ad83dc6aS[email protected] if (!connection){ 3741ad83dc6aS[email protected] return BTSTACK_MEMORY_ALLOC_FAILED; 3742ad83dc6aS[email protected] } 3743ad83dc6aS[email protected] 3744ad83dc6aS[email protected] // delete linkn key 374515a95bd5SMatthias Ringwald gap_drop_link_key_for_bd_addr(device); 3746ad83dc6aS[email protected] 3747ad83dc6aS[email protected] // configure LEVEL_2/3, dedicated bonding 3748ad83dc6aS[email protected] connection->state = SEND_CREATE_CONNECTION; 3749ad83dc6aS[email protected] connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2; 3750f04a0c31SMatthias Ringwald log_info("gap_dedicated_bonding, mitm %d -> level %u", mitm_protection_required, connection->requested_security_level); 3751ad83dc6aS[email protected] connection->bonding_flags = BONDING_DEDICATED; 3752ad83dc6aS[email protected] 3753ad83dc6aS[email protected] // wait for GAP Security Result and send GAP Dedicated Bonding complete 3754ad83dc6aS[email protected] 3755ad83dc6aS[email protected] // handle: connnection failure (connection complete != ok) 3756ad83dc6aS[email protected] // handle: authentication failure 3757ad83dc6aS[email protected] // handle: disconnect on done 3758ad83dc6aS[email protected] 3759ad83dc6aS[email protected] hci_run(); 3760ad83dc6aS[email protected] 3761ad83dc6aS[email protected] return 0; 3762ad83dc6aS[email protected] } 376335454696SMatthias Ringwald #endif 37648e618f72S[email protected] 37658e618f72S[email protected] void gap_set_local_name(const char * local_name){ 37668e618f72S[email protected] hci_stack->local_name = local_name; 37678e618f72S[email protected] } 37688e618f72S[email protected] 376935454696SMatthias Ringwald 377035454696SMatthias Ringwald #ifdef ENABLE_BLE 377135454696SMatthias Ringwald 3772d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 3773d8e8f12aSMatthias Ringwald void gap_start_scan(void){ 3774d8e8f12aSMatthias Ringwald if (hci_stack->le_scanning_state == LE_SCANNING) return; 37757bdc6798S[email protected] hci_stack->le_scanning_state = LE_START_SCAN; 37767bdc6798S[email protected] hci_run(); 37777bdc6798S[email protected] } 37788e618f72S[email protected] 3779d8e8f12aSMatthias Ringwald void gap_stop_scan(void){ 3780d8e8f12aSMatthias Ringwald if ( hci_stack->le_scanning_state == LE_SCAN_IDLE) return; 37817bdc6798S[email protected] hci_stack->le_scanning_state = LE_STOP_SCAN; 37827bdc6798S[email protected] hci_run(); 37837bdc6798S[email protected] } 37844f3229d8S[email protected] 3785d8e8f12aSMatthias Ringwald void gap_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){ 3786ef11999fSmatthias.ringwald hci_stack->le_scan_type = scan_type; 3787ef11999fSmatthias.ringwald hci_stack->le_scan_interval = scan_interval; 3788ef11999fSmatthias.ringwald hci_stack->le_scan_window = scan_window; 3789ef11999fSmatthias.ringwald hci_run(); 3790ef11999fSmatthias.ringwald } 37914f3229d8S[email protected] 3792d8e8f12aSMatthias Ringwald uint8_t gap_connect(bd_addr_t addr, bd_addr_type_t addr_type){ 37934f3229d8S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 37944f3229d8S[email protected] if (!conn){ 3795d8e8f12aSMatthias Ringwald log_info("gap_connect: no connection exists yet, creating context"); 37962e77e513S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 37974f3229d8S[email protected] if (!conn){ 37984f3229d8S[email protected] // notify client that alloc failed 37996e2e9a6bS[email protected] hci_emit_le_connection_complete(addr_type, addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 3800d8e8f12aSMatthias Ringwald log_info("gap_connect: failed to alloc hci_connection_t"); 3801472a5742SMatthias Ringwald return GATT_CLIENT_NOT_CONNECTED; // don't sent packet to controller 38024f3229d8S[email protected] } 38034f3229d8S[email protected] conn->state = SEND_CREATE_CONNECTION; 3804d8e8f12aSMatthias Ringwald log_info("gap_connect: send create connection next"); 3805564fca32S[email protected] hci_run(); 3806616edd56SMatthias Ringwald return 0; 38074f3229d8S[email protected] } 38080bf6344aS[email protected] 38090bf6344aS[email protected] if (!hci_is_le_connection(conn) || 38100bf6344aS[email protected] conn->state == SEND_CREATE_CONNECTION || 38110bf6344aS[email protected] conn->state == SENT_CREATE_CONNECTION) { 38122e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_COMMAND_DISALLOWED); 3813d8e8f12aSMatthias Ringwald log_error("gap_connect: classic connection or connect is already being created"); 3814616edd56SMatthias Ringwald return GATT_CLIENT_IN_WRONG_STATE; 38150bf6344aS[email protected] } 38160bf6344aS[email protected] 3817d8e8f12aSMatthias Ringwald log_info("gap_connect: context exists with state %u", conn->state); 38182e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, conn->con_handle, 0); 38194f3229d8S[email protected] hci_run(); 3820616edd56SMatthias Ringwald return 0; 38214f3229d8S[email protected] } 38224f3229d8S[email protected] 38237851196eSmatthias.ringwald // @assumption: only a single outgoing LE Connection exists 3824d8e8f12aSMatthias Ringwald static hci_connection_t * gap_get_outgoing_connection(void){ 3825665d90f2SMatthias Ringwald btstack_linked_item_t *it; 3826665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 38270bf6344aS[email protected] hci_connection_t * conn = (hci_connection_t *) it; 38280bf6344aS[email protected] if (!hci_is_le_connection(conn)) continue; 38290bf6344aS[email protected] switch (conn->state){ 3830a6725849S[email protected] case SEND_CREATE_CONNECTION: 38317851196eSmatthias.ringwald case SENT_CREATE_CONNECTION: 38327851196eSmatthias.ringwald return conn; 38337851196eSmatthias.ringwald default: 38347851196eSmatthias.ringwald break; 38357851196eSmatthias.ringwald }; 38367851196eSmatthias.ringwald } 38377851196eSmatthias.ringwald return NULL; 38387851196eSmatthias.ringwald } 38397851196eSmatthias.ringwald 3840d8e8f12aSMatthias Ringwald uint8_t gap_connect_cancel(void){ 3841d8e8f12aSMatthias Ringwald hci_connection_t * conn = gap_get_outgoing_connection(); 3842616edd56SMatthias Ringwald if (!conn) return 0; 38437851196eSmatthias.ringwald switch (conn->state){ 38447851196eSmatthias.ringwald case SEND_CREATE_CONNECTION: 38457851196eSmatthias.ringwald // skip sending create connection and emit event instead 38462e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER); 3847665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 38487851196eSmatthias.ringwald btstack_memory_hci_connection_free( conn ); 38490bf6344aS[email protected] break; 3850a6725849S[email protected] case SENT_CREATE_CONNECTION: 38517851196eSmatthias.ringwald // request to send cancel connection 38520bf6344aS[email protected] conn->state = SEND_CANCEL_CONNECTION; 38530bf6344aS[email protected] hci_run(); 38540bf6344aS[email protected] break; 38550bf6344aS[email protected] default: 38560bf6344aS[email protected] break; 38570bf6344aS[email protected] } 3858616edd56SMatthias Ringwald return 0; 3859e31f89a7S[email protected] } 3860d70217a2SMatthias Ringwald #endif 38614f3229d8S[email protected] 3862c37a3166S[email protected] /** 3863c37a3166S[email protected] * @brief Updates the connection parameters for a given LE connection 3864c37a3166S[email protected] * @param handle 3865c37a3166S[email protected] * @param conn_interval_min (unit: 1.25ms) 3866c37a3166S[email protected] * @param conn_interval_max (unit: 1.25ms) 3867c37a3166S[email protected] * @param conn_latency 3868c37a3166S[email protected] * @param supervision_timeout (unit: 10ms) 3869c37a3166S[email protected] * @returns 0 if ok 3870c37a3166S[email protected] */ 3871c37a3166S[email protected] int gap_update_connection_parameters(hci_con_handle_t con_handle, uint16_t conn_interval_min, 3872c37a3166S[email protected] uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 3873c37a3166S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 3874c37a3166S[email protected] if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3875c37a3166S[email protected] connection->le_conn_interval_min = conn_interval_min; 3876c37a3166S[email protected] connection->le_conn_interval_max = conn_interval_max; 3877c37a3166S[email protected] connection->le_conn_latency = conn_latency; 3878c37a3166S[email protected] connection->le_supervision_timeout = supervision_timeout; 387984cf6d83SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS; 3880cfc59f1bSMatthias Ringwald hci_run(); 3881c37a3166S[email protected] return 0; 3882c37a3166S[email protected] } 3883c37a3166S[email protected] 388445c102fdSMatthias Ringwald /** 3885b68d7bc3SMatthias Ringwald * @brief Request an update of the connection parameter for a given LE connection 3886b68d7bc3SMatthias Ringwald * @param handle 3887b68d7bc3SMatthias Ringwald * @param conn_interval_min (unit: 1.25ms) 3888b68d7bc3SMatthias Ringwald * @param conn_interval_max (unit: 1.25ms) 3889b68d7bc3SMatthias Ringwald * @param conn_latency 3890b68d7bc3SMatthias Ringwald * @param supervision_timeout (unit: 10ms) 3891b68d7bc3SMatthias Ringwald * @returns 0 if ok 3892b68d7bc3SMatthias Ringwald */ 3893b68d7bc3SMatthias Ringwald int gap_request_connection_parameter_update(hci_con_handle_t con_handle, uint16_t conn_interval_min, 3894b68d7bc3SMatthias Ringwald uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 3895b68d7bc3SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 3896b68d7bc3SMatthias Ringwald if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3897b68d7bc3SMatthias Ringwald connection->le_conn_interval_min = conn_interval_min; 3898b68d7bc3SMatthias Ringwald connection->le_conn_interval_max = conn_interval_max; 3899b68d7bc3SMatthias Ringwald connection->le_conn_latency = conn_latency; 3900b68d7bc3SMatthias Ringwald connection->le_supervision_timeout = supervision_timeout; 3901b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_REQUEST; 3902b68d7bc3SMatthias Ringwald hci_run(); 3903b68d7bc3SMatthias Ringwald return 0; 3904b68d7bc3SMatthias Ringwald } 3905b68d7bc3SMatthias Ringwald 3906d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 3907d70217a2SMatthias Ringwald 3908501f56b3SMatthias Ringwald static void gap_advertisments_changed(void){ 3909501f56b3SMatthias Ringwald // disable advertisements before updating adv, scan data, or adv params 3910501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_active){ 3911501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_DISABLE | LE_ADVERTISEMENT_TASKS_ENABLE; 3912501f56b3SMatthias Ringwald } 3913501f56b3SMatthias Ringwald hci_run(); 3914501f56b3SMatthias Ringwald } 3915501f56b3SMatthias Ringwald 3916b68d7bc3SMatthias Ringwald /** 391745c102fdSMatthias Ringwald * @brief Set Advertisement Data 391845c102fdSMatthias Ringwald * @param advertising_data_length 391945c102fdSMatthias Ringwald * @param advertising_data (max 31 octets) 392045c102fdSMatthias Ringwald * @note data is not copied, pointer has to stay valid 392145c102fdSMatthias Ringwald */ 392245c102fdSMatthias Ringwald void gap_advertisements_set_data(uint8_t advertising_data_length, uint8_t * advertising_data){ 392345c102fdSMatthias Ringwald hci_stack->le_advertisements_data_len = advertising_data_length; 392445c102fdSMatthias Ringwald hci_stack->le_advertisements_data = advertising_data; 3925501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 3926501f56b3SMatthias Ringwald gap_advertisments_changed(); 392745c102fdSMatthias Ringwald } 3928501f56b3SMatthias Ringwald 3929501f56b3SMatthias Ringwald /** 3930501f56b3SMatthias Ringwald * @brief Set Scan Response Data 3931501f56b3SMatthias Ringwald * @param advertising_data_length 3932501f56b3SMatthias Ringwald * @param advertising_data (max 31 octets) 3933501f56b3SMatthias Ringwald * @note data is not copied, pointer has to stay valid 3934501f56b3SMatthias Ringwald */ 3935501f56b3SMatthias Ringwald void gap_scan_response_set_data(uint8_t scan_response_data_length, uint8_t * scan_response_data){ 3936501f56b3SMatthias Ringwald hci_stack->le_scan_response_data_len = scan_response_data_length; 3937501f56b3SMatthias Ringwald hci_stack->le_scan_response_data = scan_response_data; 3938501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 3939501f56b3SMatthias Ringwald gap_advertisments_changed(); 394045c102fdSMatthias Ringwald } 394145c102fdSMatthias Ringwald 394245c102fdSMatthias Ringwald /** 394345c102fdSMatthias Ringwald * @brief Set Advertisement Parameters 394445c102fdSMatthias Ringwald * @param adv_int_min 394545c102fdSMatthias Ringwald * @param adv_int_max 394645c102fdSMatthias Ringwald * @param adv_type 394745c102fdSMatthias Ringwald * @param direct_address_type 394845c102fdSMatthias Ringwald * @param direct_address 394945c102fdSMatthias Ringwald * @param channel_map 395045c102fdSMatthias Ringwald * @param filter_policy 395145c102fdSMatthias Ringwald * 395245c102fdSMatthias Ringwald * @note internal use. use gap_advertisements_set_params from gap_le.h instead. 395345c102fdSMatthias Ringwald */ 395445c102fdSMatthias Ringwald void hci_le_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type, 3955b95a5a35SMatthias Ringwald uint8_t direct_address_typ, bd_addr_t direct_address, 395645c102fdSMatthias Ringwald uint8_t channel_map, uint8_t filter_policy) { 395745c102fdSMatthias Ringwald 395845c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_min = adv_int_min; 395945c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_max = adv_int_max; 396045c102fdSMatthias Ringwald hci_stack->le_advertisements_type = adv_type; 396145c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address_type = direct_address_typ; 396245c102fdSMatthias Ringwald hci_stack->le_advertisements_channel_map = channel_map; 396345c102fdSMatthias Ringwald hci_stack->le_advertisements_filter_policy = filter_policy; 396445c102fdSMatthias Ringwald memcpy(hci_stack->le_advertisements_direct_address, direct_address, 6); 396545c102fdSMatthias Ringwald 396645c102fdSMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 3967501f56b3SMatthias Ringwald gap_advertisments_changed(); 396845c102fdSMatthias Ringwald } 396945c102fdSMatthias Ringwald 397045c102fdSMatthias Ringwald /** 397145c102fdSMatthias Ringwald * @brief Enable/Disable Advertisements 397245c102fdSMatthias Ringwald * @param enabled 397345c102fdSMatthias Ringwald */ 397445c102fdSMatthias Ringwald void gap_advertisements_enable(int enabled){ 397545c102fdSMatthias Ringwald hci_stack->le_advertisements_enabled = enabled; 397645c102fdSMatthias Ringwald if (enabled && !hci_stack->le_advertisements_active){ 397745c102fdSMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_ENABLE; 397845c102fdSMatthias Ringwald } 397945c102fdSMatthias Ringwald if (!enabled && hci_stack->le_advertisements_active){ 398045c102fdSMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_DISABLE; 398145c102fdSMatthias Ringwald } 3982cfc59f1bSMatthias Ringwald hci_run(); 398345c102fdSMatthias Ringwald } 398445c102fdSMatthias Ringwald 398535454696SMatthias Ringwald #endif 398606e5cf96SMatthias Ringwald 398706e5cf96SMatthias Ringwald void hci_le_set_own_address_type(uint8_t own_address_type){ 398806e5cf96SMatthias Ringwald log_info("hci_le_set_own_address_type: old %u, new %u", hci_stack->le_own_addr_type, own_address_type); 398906e5cf96SMatthias Ringwald if (own_address_type == hci_stack->le_own_addr_type) return; 399006e5cf96SMatthias Ringwald hci_stack->le_own_addr_type = own_address_type; 399106e5cf96SMatthias Ringwald 399264068776SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 399306e5cf96SMatthias Ringwald // update advertisement parameters, too 399406e5cf96SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 399506e5cf96SMatthias Ringwald gap_advertisments_changed(); 399664068776SMatthias Ringwald #endif 399764068776SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 399806e5cf96SMatthias Ringwald // note: we don't update scan parameters or modify ongoing connection attempts 399964068776SMatthias Ringwald #endif 400006e5cf96SMatthias Ringwald } 400106e5cf96SMatthias Ringwald 4002d70217a2SMatthias Ringwald #endif 400345c102fdSMatthias Ringwald 4004616edd56SMatthias Ringwald uint8_t gap_disconnect(hci_con_handle_t handle){ 40055917a5c5S[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 40065917a5c5S[email protected] if (!conn){ 40077851196eSmatthias.ringwald hci_emit_disconnection_complete(handle, 0); 4008616edd56SMatthias Ringwald return 0; 40095917a5c5S[email protected] } 40105917a5c5S[email protected] conn->state = SEND_DISCONNECT; 40115917a5c5S[email protected] hci_run(); 4012616edd56SMatthias Ringwald return 0; 40134f3229d8S[email protected] } 401404a6ef8cSmatthias.ringwald 4015a1bf5ae7SMatthias Ringwald /** 4016a1bf5ae7SMatthias Ringwald * @brief Get connection type 4017a1bf5ae7SMatthias Ringwald * @param con_handle 4018a1bf5ae7SMatthias Ringwald * @result connection_type 4019a1bf5ae7SMatthias Ringwald */ 4020a1bf5ae7SMatthias Ringwald gap_connection_type_t gap_get_connection_type(hci_con_handle_t connection_handle){ 4021a1bf5ae7SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(connection_handle); 4022a1bf5ae7SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 4023a1bf5ae7SMatthias Ringwald switch (conn->address_type){ 4024a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 4025a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 4026a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_LE; 4027a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_SCO: 4028a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_SCO; 4029a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_CLASSIC: 4030a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_ACL; 4031a1bf5ae7SMatthias Ringwald default: 4032a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_INVALID; 4033a1bf5ae7SMatthias Ringwald } 4034a1bf5ae7SMatthias Ringwald } 4035a1bf5ae7SMatthias Ringwald 4036a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 40374f551432SMatthias Ringwald 4038d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4039d23838ecSMatthias Ringwald /** 4040ac9c45e0SMatthias Ringwald * @brief Auto Connection Establishment - Start Connecting to device 4041ac9c45e0SMatthias Ringwald * @param address_typ 4042ac9c45e0SMatthias Ringwald * @param address 4043ac9c45e0SMatthias Ringwald * @returns 0 if ok 4044ac9c45e0SMatthias Ringwald */ 40454f551432SMatthias Ringwald int gap_auto_connection_start(bd_addr_type_t address_type, bd_addr_t address){ 4046e83201bcSMatthias Ringwald // check capacity 4047665d90f2SMatthias Ringwald int num_entries = btstack_linked_list_count(&hci_stack->le_whitelist); 404891915b0bSMatthias Ringwald if (num_entries >= hci_stack->le_whitelist_capacity) return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED; 4049e83201bcSMatthias Ringwald whitelist_entry_t * entry = btstack_memory_whitelist_entry_get(); 4050e83201bcSMatthias Ringwald if (!entry) return BTSTACK_MEMORY_ALLOC_FAILED; 4051e83201bcSMatthias Ringwald entry->address_type = address_type; 4052e83201bcSMatthias Ringwald memcpy(entry->address, address, 6); 4053e83201bcSMatthias Ringwald entry->state = LE_WHITELIST_ADD_TO_CONTROLLER; 4054665d90f2SMatthias Ringwald btstack_linked_list_add(&hci_stack->le_whitelist, (btstack_linked_item_t*) entry); 4055e83201bcSMatthias Ringwald hci_run(); 4056e83201bcSMatthias Ringwald return 0; 4057ac9c45e0SMatthias Ringwald } 4058ac9c45e0SMatthias Ringwald 405942ff5ba1SMatthias Ringwald static void hci_remove_from_whitelist(bd_addr_type_t address_type, bd_addr_t address){ 4060665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 4061665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 4062665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 4063665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it); 4064e83201bcSMatthias Ringwald if (entry->address_type != address_type) continue; 4065e83201bcSMatthias Ringwald if (memcmp(entry->address, address, 6) != 0) continue; 4066e83201bcSMatthias Ringwald if (entry->state & LE_WHITELIST_ON_CONTROLLER){ 4067e83201bcSMatthias Ringwald // remove from controller if already present 4068e83201bcSMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER; 4069e83201bcSMatthias Ringwald continue; 4070e83201bcSMatthias Ringwald } 4071e83201bcSMatthias Ringwald // direclty remove entry from whitelist 4072665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 4073e83201bcSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 4074e83201bcSMatthias Ringwald } 407542ff5ba1SMatthias Ringwald } 407642ff5ba1SMatthias Ringwald 407742ff5ba1SMatthias Ringwald /** 407842ff5ba1SMatthias Ringwald * @brief Auto Connection Establishment - Stop Connecting to device 407942ff5ba1SMatthias Ringwald * @param address_typ 408042ff5ba1SMatthias Ringwald * @param address 408142ff5ba1SMatthias Ringwald * @returns 0 if ok 408242ff5ba1SMatthias Ringwald */ 408342ff5ba1SMatthias Ringwald int gap_auto_connection_stop(bd_addr_type_t address_type, bd_addr_t address){ 408442ff5ba1SMatthias Ringwald hci_remove_from_whitelist(address_type, address); 4085e83201bcSMatthias Ringwald hci_run(); 4086e83201bcSMatthias Ringwald return 0; 4087ac9c45e0SMatthias Ringwald } 4088ac9c45e0SMatthias Ringwald 4089ac9c45e0SMatthias Ringwald /** 4090ac9c45e0SMatthias Ringwald * @brief Auto Connection Establishment - Stop everything 4091ac9c45e0SMatthias Ringwald * @note Convenience function to stop all active auto connection attempts 4092ac9c45e0SMatthias Ringwald */ 4093ac9c45e0SMatthias Ringwald void gap_auto_connection_stop_all(void){ 4094665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 4095665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 4096665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 4097665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it); 4098e83201bcSMatthias Ringwald if (entry->state & LE_WHITELIST_ON_CONTROLLER){ 4099e83201bcSMatthias Ringwald // remove from controller if already present 4100e83201bcSMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER; 4101e83201bcSMatthias Ringwald continue; 4102e83201bcSMatthias Ringwald } 410391915b0bSMatthias Ringwald // directly remove entry from whitelist 4104665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 4105e83201bcSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 4106e83201bcSMatthias Ringwald } 4107e83201bcSMatthias Ringwald hci_run(); 4108ac9c45e0SMatthias Ringwald } 4109d70217a2SMatthias Ringwald #endif 41104f551432SMatthias Ringwald #endif 41114f551432SMatthias Ringwald 411235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 4113ac9c45e0SMatthias Ringwald /** 4114ff00ed1cSMatthias Ringwald * @brief Set Extended Inquiry Response data 4115ff00ed1cSMatthias Ringwald * @param eir_data size 240 bytes, is not copied make sure memory is accessible during stack startup 4116ff00ed1cSMatthias Ringwald * @note has to be done before stack starts up 4117ff00ed1cSMatthias Ringwald */ 4118ff00ed1cSMatthias Ringwald void gap_set_extended_inquiry_response(const uint8_t * data){ 4119ff00ed1cSMatthias Ringwald hci_stack->eir_data = data; 4120ff00ed1cSMatthias Ringwald } 4121ff00ed1cSMatthias Ringwald 4122ff00ed1cSMatthias Ringwald /** 4123f6858d14SMatthias Ringwald * @brief Set inquiry mode: standard, with RSSI, with RSSI + Extended Inquiry Results. Has to be called before power on. 4124f6858d14SMatthias Ringwald * @param inquriy_mode see bluetooth_defines.h 4125f6858d14SMatthias Ringwald */ 4126f6858d14SMatthias Ringwald void hci_set_inquiry_mode(inquiry_mode_t mode){ 4127f6858d14SMatthias Ringwald hci_stack->inquiry_mode = mode; 4128f6858d14SMatthias Ringwald } 4129f6858d14SMatthias Ringwald 4130f6858d14SMatthias Ringwald /** 4131d950d659SMatthias Ringwald * @brief Configure Voice Setting for use with SCO data in HSP/HFP 4132d950d659SMatthias Ringwald */ 4133d950d659SMatthias Ringwald void hci_set_sco_voice_setting(uint16_t voice_setting){ 4134d950d659SMatthias Ringwald hci_stack->sco_voice_setting = voice_setting; 4135d950d659SMatthias Ringwald } 4136d950d659SMatthias Ringwald 4137d950d659SMatthias Ringwald /** 4138d950d659SMatthias Ringwald * @brief Get SCO Voice Setting 4139d950d659SMatthias Ringwald * @return current voice setting 4140d950d659SMatthias Ringwald */ 41410cb5b971SMatthias Ringwald uint16_t hci_get_sco_voice_setting(void){ 4142d950d659SMatthias Ringwald return hci_stack->sco_voice_setting; 4143d950d659SMatthias Ringwald } 4144d950d659SMatthias Ringwald 4145b3aad8daSMatthias Ringwald /** @brief Get SCO packet length for current SCO Voice setting 4146b3aad8daSMatthias Ringwald * @note Using SCO packets of the exact length is required for USB transfer 4147b3aad8daSMatthias Ringwald * @return Length of SCO packets in bytes (not audio frames) 4148b3aad8daSMatthias Ringwald */ 4149b3aad8daSMatthias Ringwald int hci_get_sco_packet_length(void){ 4150b3aad8daSMatthias Ringwald // see Core Spec for H2 USB Transfer. 4151b3aad8daSMatthias Ringwald if (hci_stack->sco_voice_setting & 0x0020) return 51; 4152b3aad8daSMatthias Ringwald return 27; 4153b3aad8daSMatthias Ringwald } 415435454696SMatthias Ringwald #endif 4155b3aad8daSMatthias Ringwald 4156d950d659SMatthias Ringwald /** 4157d23838ecSMatthias Ringwald * @brief Set callback for Bluetooth Hardware Error 4158d23838ecSMatthias Ringwald */ 4159c2e1fa60SMatthias Ringwald void hci_set_hardware_error_callback(void (*fn)(uint8_t error)){ 4160d23838ecSMatthias Ringwald hci_stack->hardware_error_callback = fn; 4161d23838ecSMatthias Ringwald } 4162d23838ecSMatthias Ringwald 416371de195eSMatthias Ringwald void hci_disconnect_all(void){ 4164665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 4165665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 4166665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 4167665d90f2SMatthias Ringwald hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 416804a6ef8cSmatthias.ringwald if (con->state == SENT_DISCONNECT) continue; 416904a6ef8cSmatthias.ringwald con->state = SEND_DISCONNECT; 417004a6ef8cSmatthias.ringwald } 4171d31fba26S[email protected] hci_run(); 417204a6ef8cSmatthias.ringwald } 417333373e40SMatthias Ringwald 417433373e40SMatthias Ringwald uint16_t hci_get_manufacturer(void){ 417533373e40SMatthias Ringwald return hci_stack->manufacturer; 417633373e40SMatthias Ringwald } 4177