11f504dbdSmatthias.ringwald /* 2a0c35809S[email protected] * Copyright (C) 2014 BlueKitchen GmbH 31713bceaSmatthias.ringwald * 41713bceaSmatthias.ringwald * Redistribution and use in source and binary forms, with or without 51713bceaSmatthias.ringwald * modification, are permitted provided that the following conditions 61713bceaSmatthias.ringwald * are met: 71713bceaSmatthias.ringwald * 81713bceaSmatthias.ringwald * 1. Redistributions of source code must retain the above copyright 91713bceaSmatthias.ringwald * notice, this list of conditions and the following disclaimer. 101713bceaSmatthias.ringwald * 2. Redistributions in binary form must reproduce the above copyright 111713bceaSmatthias.ringwald * notice, this list of conditions and the following disclaimer in the 121713bceaSmatthias.ringwald * documentation and/or other materials provided with the distribution. 131713bceaSmatthias.ringwald * 3. Neither the name of the copyright holders nor the names of 141713bceaSmatthias.ringwald * contributors may be used to endorse or promote products derived 151713bceaSmatthias.ringwald * from this software without specific prior written permission. 166b64433eSmatthias.ringwald * 4. Any redistribution, use, or modification is done solely for 176b64433eSmatthias.ringwald * personal benefit and not for any commercial purpose or for 186b64433eSmatthias.ringwald * monetary gain. 191713bceaSmatthias.ringwald * 20a0c35809S[email protected] * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 211713bceaSmatthias.ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 221713bceaSmatthias.ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 231713bceaSmatthias.ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 241713bceaSmatthias.ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 251713bceaSmatthias.ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 261713bceaSmatthias.ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 271713bceaSmatthias.ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 281713bceaSmatthias.ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 291713bceaSmatthias.ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 301713bceaSmatthias.ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311713bceaSmatthias.ringwald * SUCH DAMAGE. 321713bceaSmatthias.ringwald * 33a0c35809S[email protected] * Please inquire about commercial licensing options at 34a0c35809S[email protected] * [email protected] 356b64433eSmatthias.ringwald * 361713bceaSmatthias.ringwald */ 371713bceaSmatthias.ringwald 38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "hci.c" 39ab2c6ae4SMatthias Ringwald 401713bceaSmatthias.ringwald /* 411f504dbdSmatthias.ringwald * hci.c 421f504dbdSmatthias.ringwald * 431f504dbdSmatthias.ringwald * Created by Matthias Ringwald on 4/29/09. 441f504dbdSmatthias.ringwald * 451f504dbdSmatthias.ringwald */ 461f504dbdSmatthias.ringwald 477907f069SMatthias Ringwald #include "btstack_config.h" 4828171530Smatthias.ringwald 497f2435e6Smatthias.ringwald 5006b9e820SMatthias Ringwald #ifdef ENABLE_CLASSIC 51aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK 528f2a52f4SMatthias Ringwald #include "btstack_run_loop_embedded.h" 53a484130cSMatthias Ringwald #endif 5406b9e820SMatthias Ringwald #endif 55a484130cSMatthias Ringwald 564a3574a1SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 57d0b87befSMatthias Ringwald #include "../port/ios/src/btstack_control_iphone.h" 584a3574a1SMatthias Ringwald #endif 594a3574a1SMatthias Ringwald 60a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 61472a5742SMatthias Ringwald #include "gap.h" 6245c102fdSMatthias Ringwald #endif 6345c102fdSMatthias Ringwald 6493b8dc03Smatthias.ringwald #include <stdarg.h> 6593b8dc03Smatthias.ringwald #include <string.h> 6656fe0872Smatthias.ringwald #include <stdio.h> 675838a2edSMatthias Ringwald #include <inttypes.h> 687f2435e6Smatthias.ringwald 6916ece135SMatthias Ringwald #include "btstack_debug.h" 700e2df43fSMatthias Ringwald #include "btstack_event.h" 714a3574a1SMatthias Ringwald #include "btstack_linked_list.h" 724a3574a1SMatthias Ringwald #include "btstack_memory.h" 7361f37892SMatthias Ringwald #include "bluetooth_company_id.h" 741cfb383eSMatthias Ringwald #include "bluetooth_data_types.h" 754a3574a1SMatthias Ringwald #include "gap.h" 764a3574a1SMatthias Ringwald #include "hci.h" 774a3574a1SMatthias Ringwald #include "hci_cmd.h" 78d8905019Smatthias.ringwald #include "hci_dump.h" 791cfb383eSMatthias Ringwald #include "ad_parser.h" 8093b8dc03Smatthias.ringwald 812b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 822b838201SMatthias Ringwald #ifndef HCI_HOST_ACL_PACKET_NUM 832b838201SMatthias Ringwald #error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_ACL_PACKET_NUM" 842b838201SMatthias Ringwald #endif 852b838201SMatthias Ringwald #ifndef HCI_HOST_ACL_PACKET_LEN 862b838201SMatthias Ringwald #error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_ACL_PACKET_LEN" 872b838201SMatthias Ringwald #endif 882b838201SMatthias Ringwald #ifndef HCI_HOST_SCO_PACKET_NUM 892b838201SMatthias Ringwald #error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_SCO_PACKET_NUM" 902b838201SMatthias Ringwald #endif 912b838201SMatthias Ringwald #ifndef HCI_HOST_SCO_PACKET_LEN 922b838201SMatthias Ringwald #error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_SCO_PACKET_LEN" 932b838201SMatthias Ringwald #endif 942b838201SMatthias Ringwald #endif 951b0e3922Smatthias.ringwald 96169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000 97659d758cSMatthias Ringwald #define HCI_RESET_RESEND_TIMEOUT_MS 200 98ee091cf1Smatthias.ringwald 991cfb383eSMatthias Ringwald // Names are arbitrarily shortened to 32 bytes if not requested otherwise 1001cfb383eSMatthias Ringwald #ifndef GAP_INQUIRY_MAX_NAME_LEN 1011cfb383eSMatthias Ringwald #define GAP_INQUIRY_MAX_NAME_LEN 32 1021cfb383eSMatthias Ringwald #endif 1031cfb383eSMatthias Ringwald 104f5875de5SMatthias Ringwald // GAP inquiry state: 0 = off, 0x01 - 0x30 = requested duration, 0xfe = active, 0xff = stop requested 105f5875de5SMatthias Ringwald #define GAP_INQUIRY_DURATION_MIN 0x01 106f5875de5SMatthias Ringwald #define GAP_INQUIRY_DURATION_MAX 0x30 107f5875de5SMatthias Ringwald #define GAP_INQUIRY_STATE_ACTIVE 0x80 108f5875de5SMatthias Ringwald #define GAP_INQUIRY_STATE_IDLE 0 109f5875de5SMatthias Ringwald #define GAP_INQUIRY_STATE_W2_CANCEL 0x81 110f5875de5SMatthias Ringwald #define GAP_INQUIRY_STATE_W4_CANCELLED 0x82 111f5875de5SMatthias Ringwald 112b7f1ee76SMatthias Ringwald // GAP Remote Name Request 113b7f1ee76SMatthias Ringwald #define GAP_REMOTE_NAME_STATE_IDLE 0 114b7f1ee76SMatthias Ringwald #define GAP_REMOTE_NAME_STATE_W2_SEND 1 115b7f1ee76SMatthias Ringwald #define GAP_REMOTE_NAME_STATE_W4_COMPLETE 2 116b7f1ee76SMatthias Ringwald 1170a51f88bSMatthias Ringwald // GAP Pairing 1180a51f88bSMatthias Ringwald #define GAP_PAIRING_STATE_IDLE 0 1190a51f88bSMatthias Ringwald #define GAP_PAIRING_STATE_SEND_PIN 1 1200a51f88bSMatthias Ringwald #define GAP_PAIRING_STATE_SEND_PIN_NEGATIVE 2 1210a51f88bSMatthias Ringwald #define GAP_PAIRING_STATE_SEND_PASSKEY 3 1220a51f88bSMatthias Ringwald #define GAP_PAIRING_STATE_SEND_PASSKEY_NEGATIVE 4 1230a51f88bSMatthias Ringwald #define GAP_PAIRING_STATE_SEND_CONFIRMATION 5 1240a51f88bSMatthias Ringwald #define GAP_PAIRING_STATE_SEND_CONFIRMATION_NEGATIVE 6 1250a51f88bSMatthias Ringwald 1260a51f88bSMatthias Ringwald 127b83d5eabSMatthias Ringwald // prototypes 12835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 129758b46ceSmatthias.ringwald static void hci_update_scan_enable(void); 13035454696SMatthias Ringwald static void hci_emit_discoverable_enabled(uint8_t enabled); 13135454696SMatthias Ringwald static int hci_local_ssp_activated(void); 13235454696SMatthias Ringwald static int hci_remote_ssp_supported(hci_con_handle_t con_handle); 13335454696SMatthias Ringwald static void hci_notify_if_sco_can_send_now(void); 13435454696SMatthias Ringwald static void hci_emit_connection_complete(bd_addr_t address, hci_con_handle_t con_handle, uint8_t status); 135a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection); 13635454696SMatthias Ringwald static void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level); 137ec820d77SMatthias Ringwald static void hci_connection_timeout_handler(btstack_timer_source_t *timer); 13896a45072S[email protected] static void hci_connection_timestamp(hci_connection_t *connection); 13952db98b2SMatthias Ringwald static void hci_emit_l2cap_check_timeout(hci_connection_t *conn); 1401cfb383eSMatthias Ringwald static void gap_inquiry_explode(uint8_t * packet); 14152db98b2SMatthias Ringwald #endif 1421cfb383eSMatthias Ringwald 1437586ee35S[email protected] static int hci_power_control_on(void); 1447586ee35S[email protected] static void hci_power_control_off(void); 1456da48142SSean Wilson static void hci_state_reset(void); 146fd43c0e0SMatthias Ringwald static void hci_emit_transport_packet_sent(void); 147fc64f94aSMatthias Ringwald static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason); 148b83d5eabSMatthias Ringwald static void hci_emit_nr_connections_changed(void); 149b83d5eabSMatthias Ringwald static void hci_emit_hci_open_failed(void); 150b83d5eabSMatthias Ringwald static void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status); 151b83d5eabSMatthias Ringwald static void hci_emit_event(uint8_t * event, uint16_t size, int dump); 152b83d5eabSMatthias Ringwald static void hci_emit_acl_packet(uint8_t * packet, uint16_t size); 15395d71764SMatthias Ringwald static void hci_run(void); 15495d71764SMatthias Ringwald static int hci_is_le_connection(hci_connection_t * connection); 15595d71764SMatthias Ringwald static int hci_number_free_acl_slots_for_connection_type( bd_addr_type_t address_type); 1565d509858SMatthias Ringwald 157a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 158e8c8828eSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 15939677e66SMatthias Ringwald // called from test/ble_client/advertising_data_parser.c 160384b59deSMatthias Ringwald void le_handle_advertisement_report(uint8_t *packet, uint16_t size); 16142ff5ba1SMatthias Ringwald static void hci_remove_from_whitelist(bd_addr_type_t address_type, bd_addr_t address); 1629c77c9dbSMatthias Ringwald static hci_connection_t * gap_get_outgoing_connection(void); 1635d509858SMatthias Ringwald #endif 164d70217a2SMatthias Ringwald #endif 165758b46ceSmatthias.ringwald 16606b35ec0Smatthias.ringwald // the STACK is here 1673a9fb326S[email protected] #ifndef HAVE_MALLOC 1683a9fb326S[email protected] static hci_stack_t hci_stack_static; 1693a9fb326S[email protected] #endif 1703a9fb326S[email protected] static hci_stack_t * hci_stack = NULL; 17116833f0aSmatthias.ringwald 1721c9e5e9dSMatthias Ringwald #ifdef ENABLE_CLASSIC 17363168530SMatthias Ringwald // default name 17463168530SMatthias Ringwald static const char * default_classic_name = "BTstack 00:00:00:00:00:00"; 17563168530SMatthias Ringwald 17666fb9560S[email protected] // test helper 17766fb9560S[email protected] static uint8_t disable_l2cap_timeouts = 0; 1781c9e5e9dSMatthias Ringwald #endif 17966fb9560S[email protected] 18096a45072S[email protected] /** 18196a45072S[email protected] * create connection for given address 18296a45072S[email protected] * 18396a45072S[email protected] * @return connection OR NULL, if no memory left 18496a45072S[email protected] */ 18596a45072S[email protected] static hci_connection_t * create_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){ 1861a06f663S[email protected] log_info("create_connection_for_addr %s, type %x", bd_addr_to_str(addr), addr_type); 187bb69aaaeS[email protected] hci_connection_t * conn = btstack_memory_hci_connection_get(); 18896a45072S[email protected] if (!conn) return NULL; 189058e3d6bSMatthias Ringwald bd_addr_copy(conn->address, addr); 19096a45072S[email protected] conn->address_type = addr_type; 19196a45072S[email protected] conn->con_handle = 0xffff; 19296a45072S[email protected] conn->authentication_flags = AUTH_FLAGS_NONE; 19396a45072S[email protected] conn->bonding_flags = 0; 19496a45072S[email protected] conn->requested_security_level = LEVEL_0; 19552db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 19691a977e8SMatthias Ringwald btstack_run_loop_set_timer_handler(&conn->timeout, hci_connection_timeout_handler); 19791a977e8SMatthias Ringwald btstack_run_loop_set_timer_context(&conn->timeout, conn); 19896a45072S[email protected] hci_connection_timestamp(conn); 19952db98b2SMatthias Ringwald #endif 20096a45072S[email protected] conn->acl_recombination_length = 0; 20196a45072S[email protected] conn->acl_recombination_pos = 0; 202ce41473eSMatthias Ringwald conn->num_packets_sent = 0; 203760b20efSMatthias Ringwald 204da886c03S[email protected] conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 205b90f6e0aSMatthias Ringwald #ifdef ENABLE_BLE 206b90f6e0aSMatthias Ringwald conn->le_phy_update_all_phys = 0xff; 207b90f6e0aSMatthias Ringwald #endif 208665d90f2SMatthias Ringwald btstack_linked_list_add(&hci_stack->connections, (btstack_linked_item_t *) conn); 20996a45072S[email protected] return conn; 21096a45072S[email protected] } 21166fb9560S[email protected] 212da886c03S[email protected] 213da886c03S[email protected] /** 214da886c03S[email protected] * get le connection parameter range 215da886c03S[email protected] * 216da886c03S[email protected] * @return le connection parameter range struct 217da886c03S[email protected] */ 2184ced4e8cSMatthias Ringwald void gap_get_connection_parameter_range(le_connection_parameter_range_t * range){ 2194ced4e8cSMatthias Ringwald *range = hci_stack->le_connection_parameter_range; 220da886c03S[email protected] } 221da886c03S[email protected] 222da886c03S[email protected] /** 223da886c03S[email protected] * set le connection parameter range 224da886c03S[email protected] * 225da886c03S[email protected] */ 226da886c03S[email protected] 2274ced4e8cSMatthias Ringwald void gap_set_connection_parameter_range(le_connection_parameter_range_t *range){ 2284ced4e8cSMatthias Ringwald hci_stack->le_connection_parameter_range = *range; 229da886c03S[email protected] } 230da886c03S[email protected] 231da886c03S[email protected] /** 23273cd8a2aSMatthias Ringwald * @brief Test if connection parameters are inside in existing rage 23373cd8a2aSMatthias Ringwald * @param conn_interval_min (unit: 1.25ms) 23473cd8a2aSMatthias Ringwald * @param conn_interval_max (unit: 1.25ms) 23573cd8a2aSMatthias Ringwald * @param conn_latency 23673cd8a2aSMatthias Ringwald * @param supervision_timeout (unit: 10ms) 23773cd8a2aSMatthias Ringwald * @returns 1 if included 23873cd8a2aSMatthias Ringwald */ 23973cd8a2aSMatthias Ringwald int gap_connection_parameter_range_included(le_connection_parameter_range_t * existing_range, uint16_t le_conn_interval_min, uint16_t le_conn_interval_max, uint16_t le_conn_latency, uint16_t le_supervision_timeout){ 24073cd8a2aSMatthias Ringwald if (le_conn_interval_min < existing_range->le_conn_interval_min) return 0; 24173cd8a2aSMatthias Ringwald if (le_conn_interval_max > existing_range->le_conn_interval_max) return 0; 24273cd8a2aSMatthias Ringwald 24373cd8a2aSMatthias Ringwald if (le_conn_latency < existing_range->le_conn_latency_min) return 0; 24473cd8a2aSMatthias Ringwald if (le_conn_latency > existing_range->le_conn_latency_max) return 0; 24573cd8a2aSMatthias Ringwald 24673cd8a2aSMatthias Ringwald if (le_supervision_timeout < existing_range->le_supervision_timeout_min) return 0; 24773cd8a2aSMatthias Ringwald if (le_supervision_timeout > existing_range->le_supervision_timeout_max) return 0; 24873cd8a2aSMatthias Ringwald 24973cd8a2aSMatthias Ringwald return 1; 25073cd8a2aSMatthias Ringwald } 25173cd8a2aSMatthias Ringwald 25273cd8a2aSMatthias Ringwald /** 2532b6ab3e6SMatthias Ringwald * @brief Set max number of connections in LE Peripheral role (if Bluetooth Controller supports it) 2542b6ab3e6SMatthias Ringwald * @note: default: 1 2552b6ab3e6SMatthias Ringwald * @param max_peripheral_connections 2562b6ab3e6SMatthias Ringwald */ 257d4e4907bSMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 2582b6ab3e6SMatthias Ringwald void gap_set_max_number_peripheral_connections(int max_peripheral_connections){ 2592b6ab3e6SMatthias Ringwald hci_stack->le_max_number_peripheral_connections = max_peripheral_connections; 2602b6ab3e6SMatthias Ringwald } 261d4e4907bSMatthias Ringwald #endif 2622b6ab3e6SMatthias Ringwald 2632b6ab3e6SMatthias Ringwald /** 264da886c03S[email protected] * get hci connections iterator 265da886c03S[email protected] * 266da886c03S[email protected] * @return hci connections iterator 267da886c03S[email protected] */ 268da886c03S[email protected] 269665d90f2SMatthias Ringwald void hci_connections_get_iterator(btstack_linked_list_iterator_t *it){ 270665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(it, &hci_stack->connections); 271da886c03S[email protected] } 272da886c03S[email protected] 27397addcc5Smatthias.ringwald /** 274ee091cf1Smatthias.ringwald * get connection for a given handle 275ee091cf1Smatthias.ringwald * 276ee091cf1Smatthias.ringwald * @return connection OR NULL, if not found 277ee091cf1Smatthias.ringwald */ 2785061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){ 279665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 280665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 281665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 282665d90f2SMatthias Ringwald hci_connection_t * item = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 2833ac2fe56S[email protected] if ( item->con_handle == con_handle ) { 284da886c03S[email protected] return item; 285ee091cf1Smatthias.ringwald } 286ee091cf1Smatthias.ringwald } 287ee091cf1Smatthias.ringwald return NULL; 288ee091cf1Smatthias.ringwald } 289ee091cf1Smatthias.ringwald 29096a45072S[email protected] /** 29196a45072S[email protected] * get connection for given address 29296a45072S[email protected] * 29396a45072S[email protected] * @return connection OR NULL, if not found 29496a45072S[email protected] */ 2952e77e513S[email protected] hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){ 296665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 297665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 298665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 299665d90f2SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 30096a45072S[email protected] if (connection->address_type != addr_type) continue; 30196a45072S[email protected] if (memcmp(addr, connection->address, 6) != 0) continue; 30262bda3fbS[email protected] return connection; 30362bda3fbS[email protected] } 30462bda3fbS[email protected] return NULL; 30562bda3fbS[email protected] } 30662bda3fbS[email protected] 30752db98b2SMatthias Ringwald 30852db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 30952db98b2SMatthias Ringwald 310ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 311ee752bb8SMatthias Ringwald static int hci_number_sco_connections(void){ 312ee752bb8SMatthias Ringwald int connections = 0; 313ee752bb8SMatthias Ringwald btstack_linked_list_iterator_t it; 314ee752bb8SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 315ee752bb8SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 316ee752bb8SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 317ee752bb8SMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_SCO) continue; 318ee752bb8SMatthias Ringwald connections++; 319ee752bb8SMatthias Ringwald } 320ee752bb8SMatthias Ringwald return connections; 321ee752bb8SMatthias Ringwald } 322ee752bb8SMatthias Ringwald #endif 323ee752bb8SMatthias Ringwald 324ec820d77SMatthias Ringwald static void hci_connection_timeout_handler(btstack_timer_source_t *timer){ 32591a977e8SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_run_loop_get_timer_context(timer); 326aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK 327528a4a3bSMatthias Ringwald if (btstack_run_loop_embedded_get_ticks() > connection->timestamp + btstack_run_loop_embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){ 328c785ef68Smatthias.ringwald // connections might be timed out 329c785ef68Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 330c785ef68Smatthias.ringwald } 331f316a845SMatthias Ringwald #else 332528a4a3bSMatthias Ringwald if (btstack_run_loop_get_time_ms() > connection->timestamp + HCI_CONNECTION_TIMEOUT_MS){ 3335f26aadcSMatthias Ringwald // connections might be timed out 3345f26aadcSMatthias Ringwald hci_emit_l2cap_check_timeout(connection); 3355f26aadcSMatthias Ringwald } 3365f26aadcSMatthias Ringwald #endif 337c785ef68Smatthias.ringwald } 338ee091cf1Smatthias.ringwald 339ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){ 340aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK 341528a4a3bSMatthias Ringwald connection->timestamp = btstack_run_loop_embedded_get_ticks(); 342f316a845SMatthias Ringwald #else 343528a4a3bSMatthias Ringwald connection->timestamp = btstack_run_loop_get_time_ms(); 3445f26aadcSMatthias Ringwald #endif 345ee091cf1Smatthias.ringwald } 346ee091cf1Smatthias.ringwald 34728ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 34828ca2b46S[email protected] conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags); 34928ca2b46S[email protected] } 35028ca2b46S[email protected] 35135454696SMatthias Ringwald 35228ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 35328ca2b46S[email protected] conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags); 35428ca2b46S[email protected] } 35528ca2b46S[email protected] 35643bfb1bdSmatthias.ringwald /** 35780ca58a0Smatthias.ringwald * add authentication flags and reset timer 35896a45072S[email protected] * @note: assumes classic connection 3592e77e513S[email protected] * @note: bd_addr is passed in as litle endian uint8_t * as it is called from parsing packets 3607fde4af9Smatthias.ringwald */ 3617fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){ 3627fde4af9Smatthias.ringwald bd_addr_t addr; 363724d70a2SMatthias Ringwald reverse_bd_addr(bd_addr, addr); 3642e77e513S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 3657fde4af9Smatthias.ringwald if (conn) { 36628ca2b46S[email protected] connectionSetAuthenticationFlags(conn, flags); 36780ca58a0Smatthias.ringwald hci_connection_timestamp(conn); 3687fde4af9Smatthias.ringwald } 3697fde4af9Smatthias.ringwald } 3707fde4af9Smatthias.ringwald 37180ca58a0Smatthias.ringwald int hci_authentication_active_for_handle(hci_con_handle_t handle){ 3725061f3afS[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 37380ca58a0Smatthias.ringwald if (!conn) return 0; 3746724cd9eS[email protected] if (conn->authentication_flags & LEGACY_PAIRING_ACTIVE) return 1; 3756724cd9eS[email protected] if (conn->authentication_flags & SSP_PAIRING_ACTIVE) return 1; 3766724cd9eS[email protected] return 0; 37780ca58a0Smatthias.ringwald } 37880ca58a0Smatthias.ringwald 37915a95bd5SMatthias Ringwald void gap_drop_link_key_for_bd_addr(bd_addr_t addr){ 38055597469SMatthias Ringwald if (!hci_stack->link_key_db) return; 3812bacf595SMatthias Ringwald log_info("gap_drop_link_key_for_bd_addr: %s", bd_addr_to_str(addr)); 382a98592bcSMatthias Ringwald hci_stack->link_key_db->delete_link_key(addr); 383c12e46e7Smatthias.ringwald } 38455597469SMatthias Ringwald 38555597469SMatthias Ringwald void gap_store_link_key_for_bd_addr(bd_addr_t addr, link_key_t link_key, link_key_type_t type){ 38655597469SMatthias Ringwald if (!hci_stack->link_key_db) return; 3872bacf595SMatthias Ringwald log_info("gap_store_link_key_for_bd_addr: %s, type %u", bd_addr_to_str(addr), type); 38855597469SMatthias Ringwald hci_stack->link_key_db->put_link_key(addr, link_key, type); 389c12e46e7Smatthias.ringwald } 3901b6fb31bSMatthias Ringwald 391ceecb9d9SMatthias Ringwald void gap_delete_all_link_keys(void){ 392ceecb9d9SMatthias Ringwald bd_addr_t addr; 393ceecb9d9SMatthias Ringwald link_key_t link_key; 394ceecb9d9SMatthias Ringwald link_key_type_t type; 395ceecb9d9SMatthias Ringwald btstack_link_key_iterator_t it; 396ceecb9d9SMatthias Ringwald int ok = gap_link_key_iterator_init(&it); 397ceecb9d9SMatthias Ringwald if (!ok) { 398ceecb9d9SMatthias Ringwald log_error("could not initialize iterator"); 399ceecb9d9SMatthias Ringwald return; 400ceecb9d9SMatthias Ringwald } 401ceecb9d9SMatthias Ringwald while (gap_link_key_iterator_get_next(&it, addr, link_key, &type)){ 402ceecb9d9SMatthias Ringwald gap_drop_link_key_for_bd_addr(addr); 403ceecb9d9SMatthias Ringwald } 404ceecb9d9SMatthias Ringwald gap_link_key_iterator_done(&it); 405ceecb9d9SMatthias Ringwald } 406ceecb9d9SMatthias Ringwald 4071b6fb31bSMatthias Ringwald int gap_link_key_iterator_init(btstack_link_key_iterator_t * it){ 4081b6fb31bSMatthias Ringwald if (!hci_stack->link_key_db) return 0; 4091b6fb31bSMatthias Ringwald if (!hci_stack->link_key_db->iterator_init) return 0; 4101b6fb31bSMatthias Ringwald return hci_stack->link_key_db->iterator_init(it); 4111b6fb31bSMatthias Ringwald } 4121b6fb31bSMatthias Ringwald int gap_link_key_iterator_get_next(btstack_link_key_iterator_t * it, bd_addr_t bd_addr, link_key_t link_key, link_key_type_t * type){ 4131b6fb31bSMatthias Ringwald if (!hci_stack->link_key_db) return 0; 4141b6fb31bSMatthias Ringwald return hci_stack->link_key_db->iterator_get_next(it, bd_addr, link_key, type); 4151b6fb31bSMatthias Ringwald } 4161b6fb31bSMatthias Ringwald void gap_link_key_iterator_done(btstack_link_key_iterator_t * it){ 4171b6fb31bSMatthias Ringwald if (!hci_stack->link_key_db) return; 4181b6fb31bSMatthias Ringwald hci_stack->link_key_db->iterator_done(it); 4191b6fb31bSMatthias Ringwald } 42035454696SMatthias Ringwald #endif 421c12e46e7Smatthias.ringwald 42295d71764SMatthias Ringwald static int hci_is_le_connection(hci_connection_t * connection){ 423ce41473eSMatthias Ringwald switch (connection->address_type){ 424ce41473eSMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 425ce41473eSMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 426ce41473eSMatthias Ringwald case BD_ADDR_TYPE_LE_PRIVAT_FALLBACK_PUBLIC: 427ce41473eSMatthias Ringwald case BD_ADDR_TYPE_LE_PRIVAT_FALLBACK_RANDOM: 428ce41473eSMatthias Ringwald return 1; 429ce41473eSMatthias Ringwald default: 430ce41473eSMatthias Ringwald return 0; 431ce41473eSMatthias Ringwald } 4320bf6344aS[email protected] } 4330bf6344aS[email protected] 4347fde4af9Smatthias.ringwald /** 43543bfb1bdSmatthias.ringwald * count connections 43643bfb1bdSmatthias.ringwald */ 43740d1c7a4Smatthias.ringwald static int nr_hci_connections(void){ 43856c253c9Smatthias.ringwald int count = 0; 439665d90f2SMatthias Ringwald btstack_linked_item_t *it; 440665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next, count++); 44143bfb1bdSmatthias.ringwald return count; 44243bfb1bdSmatthias.ringwald } 443c8e4258aSmatthias.ringwald 44495d71764SMatthias Ringwald static int hci_number_free_acl_slots_for_connection_type(bd_addr_type_t address_type){ 445ee303eddS[email protected] 446f04a0c31SMatthias Ringwald unsigned int num_packets_sent_classic = 0; 447f04a0c31SMatthias Ringwald unsigned int num_packets_sent_le = 0; 448ee303eddS[email protected] 449665d90f2SMatthias Ringwald btstack_linked_item_t *it; 450665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 451998906cdSmatthias.ringwald hci_connection_t * connection = (hci_connection_t *) it; 452ce41473eSMatthias Ringwald if (hci_is_le_connection(connection)){ 453ce41473eSMatthias Ringwald num_packets_sent_le += connection->num_packets_sent; 454ce41473eSMatthias Ringwald } 455ee303eddS[email protected] if (connection->address_type == BD_ADDR_TYPE_CLASSIC){ 456ce41473eSMatthias Ringwald num_packets_sent_classic += connection->num_packets_sent; 457ee303eddS[email protected] } 458ee303eddS[email protected] } 459d999b54eSMatthias Ringwald log_debug("ACL classic buffers: %u used of %u", num_packets_sent_classic, hci_stack->acl_packets_total_num); 460ee303eddS[email protected] int free_slots_classic = hci_stack->acl_packets_total_num - num_packets_sent_classic; 461ee303eddS[email protected] int free_slots_le = 0; 462ee303eddS[email protected] 463ee303eddS[email protected] if (free_slots_classic < 0){ 4649da54300S[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); 465998906cdSmatthias.ringwald return 0; 466998906cdSmatthias.ringwald } 467ee303eddS[email protected] 468ee303eddS[email protected] if (hci_stack->le_acl_packets_total_num){ 469ee303eddS[email protected] // if we have LE slots, they are used 470ee303eddS[email protected] free_slots_le = hci_stack->le_acl_packets_total_num - num_packets_sent_le; 471ee303eddS[email protected] if (free_slots_le < 0){ 4729da54300S[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); 473ee303eddS[email protected] return 0; 474998906cdSmatthias.ringwald } 475ee303eddS[email protected] } else { 476ee303eddS[email protected] // otherwise, classic slots are used for LE, too 477ee303eddS[email protected] free_slots_classic -= num_packets_sent_le; 478ee303eddS[email protected] if (free_slots_classic < 0){ 4799da54300S[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); 480ee303eddS[email protected] return 0; 481ee303eddS[email protected] } 482ee303eddS[email protected] } 483ee303eddS[email protected] 484ee303eddS[email protected] switch (address_type){ 485ee303eddS[email protected] case BD_ADDR_TYPE_UNKNOWN: 4862125de09SMatthias Ringwald log_error("hci_number_free_acl_slots: unknown address type"); 487ee303eddS[email protected] return 0; 488ee303eddS[email protected] 489ee303eddS[email protected] case BD_ADDR_TYPE_CLASSIC: 490ee303eddS[email protected] return free_slots_classic; 491ee303eddS[email protected] 492ee303eddS[email protected] default: 493cb00d3aaS[email protected] if (hci_stack->le_acl_packets_total_num){ 494ee303eddS[email protected] return free_slots_le; 495ee303eddS[email protected] } 496cb00d3aaS[email protected] return free_slots_classic; 497cb00d3aaS[email protected] } 498998906cdSmatthias.ringwald } 499998906cdSmatthias.ringwald 5002125de09SMatthias Ringwald int hci_number_free_acl_slots_for_handle(hci_con_handle_t con_handle){ 5012125de09SMatthias Ringwald // get connection type 5022125de09SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 5032125de09SMatthias Ringwald if (!connection){ 5042125de09SMatthias Ringwald log_error("hci_number_free_acl_slots: handle 0x%04x not in connection list", con_handle); 5052125de09SMatthias Ringwald return 0; 5062125de09SMatthias Ringwald } 5072125de09SMatthias Ringwald return hci_number_free_acl_slots_for_connection_type(connection->address_type); 5082125de09SMatthias Ringwald } 5092125de09SMatthias Ringwald 51035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 511701e3307SMatthias Ringwald static int hci_number_free_sco_slots(void){ 512f04a0c31SMatthias Ringwald unsigned int num_sco_packets_sent = 0; 513665d90f2SMatthias Ringwald btstack_linked_item_t *it; 514760b20efSMatthias Ringwald if (hci_stack->synchronous_flow_control_enabled){ 515760b20efSMatthias Ringwald // explicit flow control 516665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 517e35edcc1S[email protected] hci_connection_t * connection = (hci_connection_t *) it; 518ce41473eSMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_SCO) continue; 519ce41473eSMatthias Ringwald num_sco_packets_sent += connection->num_packets_sent; 520e35edcc1S[email protected] } 521e35edcc1S[email protected] if (num_sco_packets_sent > hci_stack->sco_packets_total_num){ 522701e3307SMatthias Ringwald log_info("hci_number_free_sco_slots:packets (%u) > total packets (%u)", num_sco_packets_sent, hci_stack->sco_packets_total_num); 52344d0e3d5S[email protected] return 0; 52444d0e3d5S[email protected] } 525e35edcc1S[email protected] return hci_stack->sco_packets_total_num - num_sco_packets_sent; 526*49205f5dSMatthias Ringwald } else { 527*49205f5dSMatthias Ringwald // implicit flow control -- TODO 528*49205f5dSMatthias Ringwald return 0; 529*49205f5dSMatthias Ringwald } 530*49205f5dSMatthias Ringwald 531e35edcc1S[email protected] } 53235454696SMatthias Ringwald #endif 53344d0e3d5S[email protected] 5342b838201SMatthias Ringwald // only used to send HCI Host Number Completed Packets 5352b838201SMatthias Ringwald static int hci_can_send_comand_packet_transport(void){ 536ac928cc2S[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 537ac928cc2S[email protected] 538ac928cc2S[email protected] // check for async hci transport implementations 539ac928cc2S[email protected] if (hci_stack->hci_transport->can_send_packet_now){ 540ac928cc2S[email protected] if (!hci_stack->hci_transport->can_send_packet_now(HCI_COMMAND_DATA_PACKET)){ 541ac928cc2S[email protected] return 0; 542ac928cc2S[email protected] } 543ac928cc2S[email protected] } 5442b838201SMatthias Ringwald return 1; 5452b838201SMatthias Ringwald } 546ac928cc2S[email protected] 5472b838201SMatthias Ringwald // new functions replacing hci_can_send_packet_now[_using_packet_buffer] 5482b838201SMatthias Ringwald int hci_can_send_command_packet_now(void){ 5492b838201SMatthias Ringwald if (hci_can_send_comand_packet_transport() == 0) return 0; 550ac928cc2S[email protected] return hci_stack->num_cmd_packets > 0; 551ac928cc2S[email protected] } 552ac928cc2S[email protected] 5539d04d3a7SMatthias Ringwald static int hci_transport_can_send_prepared_packet_now(uint8_t packet_type){ 554ac928cc2S[email protected] // check for async hci transport implementations 5559d04d3a7SMatthias Ringwald if (!hci_stack->hci_transport->can_send_packet_now) return 1; 5569d04d3a7SMatthias Ringwald return hci_stack->hci_transport->can_send_packet_now(packet_type); 557ac928cc2S[email protected] } 5589d04d3a7SMatthias Ringwald 5599d04d3a7SMatthias Ringwald static int hci_can_send_prepared_acl_packet_for_address_type(bd_addr_type_t address_type){ 5609d04d3a7SMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_ACL_DATA_PACKET)) return 0; 5619d04d3a7SMatthias Ringwald return hci_number_free_acl_slots_for_connection_type(address_type) > 0; 562ac928cc2S[email protected] } 5639d04d3a7SMatthias Ringwald 5649d04d3a7SMatthias Ringwald int hci_can_send_acl_le_packet_now(void){ 5659d04d3a7SMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return 0; 5669d04d3a7SMatthias Ringwald return hci_can_send_prepared_acl_packet_for_address_type(BD_ADDR_TYPE_LE_PUBLIC); 5679d04d3a7SMatthias Ringwald } 5689d04d3a7SMatthias Ringwald 5699d04d3a7SMatthias Ringwald int hci_can_send_prepared_acl_packet_now(hci_con_handle_t con_handle) { 5709d04d3a7SMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_ACL_DATA_PACKET)) return 0; 571e79abdd6S[email protected] return hci_number_free_acl_slots_for_handle(con_handle) > 0; 572ac928cc2S[email protected] } 573ac928cc2S[email protected] 574ac928cc2S[email protected] int hci_can_send_acl_packet_now(hci_con_handle_t con_handle){ 575ac928cc2S[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 576ac928cc2S[email protected] return hci_can_send_prepared_acl_packet_now(con_handle); 5776b4af23dS[email protected] } 5786b4af23dS[email protected] 57935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 58035454696SMatthias Ringwald int hci_can_send_acl_classic_packet_now(void){ 58135454696SMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return 0; 58235454696SMatthias Ringwald return hci_can_send_prepared_acl_packet_for_address_type(BD_ADDR_TYPE_CLASSIC); 58335454696SMatthias Ringwald } 58435454696SMatthias Ringwald 585701e3307SMatthias Ringwald int hci_can_send_prepared_sco_packet_now(void){ 586d057580eSMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_SCO_DATA_PACKET)) return 0; 587701e3307SMatthias Ringwald return hci_number_free_sco_slots() > 0; 58844d0e3d5S[email protected] } 58944d0e3d5S[email protected] 590701e3307SMatthias Ringwald int hci_can_send_sco_packet_now(void){ 591d057580eSMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return 0; 592701e3307SMatthias Ringwald return hci_can_send_prepared_sco_packet_now(); 59344d0e3d5S[email protected] } 59444d0e3d5S[email protected] 595d057580eSMatthias Ringwald void hci_request_sco_can_send_now_event(void){ 596d057580eSMatthias Ringwald hci_stack->sco_waiting_for_can_send_now = 1; 597d057580eSMatthias Ringwald hci_notify_if_sco_can_send_now(); 598d057580eSMatthias Ringwald } 59935454696SMatthias Ringwald #endif 600d057580eSMatthias Ringwald 60195d71764SMatthias Ringwald // used for internal checks in l2cap.c 602c8b9416aS[email protected] int hci_is_packet_buffer_reserved(void){ 603c8b9416aS[email protected] return hci_stack->hci_packet_buffer_reserved; 604c8b9416aS[email protected] } 605c8b9416aS[email protected] 6066b4af23dS[email protected] // reserves outgoing packet buffer. @returns 1 if successful 6076b4af23dS[email protected] int hci_reserve_packet_buffer(void){ 6089d14b626S[email protected] if (hci_stack->hci_packet_buffer_reserved) { 6099d14b626S[email protected] log_error("hci_reserve_packet_buffer called but buffer already reserved"); 6109d14b626S[email protected] return 0; 6119d14b626S[email protected] } 6126b4af23dS[email protected] hci_stack->hci_packet_buffer_reserved = 1; 6136b4af23dS[email protected] return 1; 6146b4af23dS[email protected] } 6156b4af23dS[email protected] 61668a0fcf7S[email protected] void hci_release_packet_buffer(void){ 61768a0fcf7S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 61868a0fcf7S[email protected] } 61968a0fcf7S[email protected] 6206b4af23dS[email protected] // assumption: synchronous implementations don't provide can_send_packet_now as they don't keep the buffer after the call 6217f02f414SMatthias Ringwald static int hci_transport_synchronous(void){ 6226b4af23dS[email protected] return hci_stack->hci_transport->can_send_packet_now == NULL; 6236b4af23dS[email protected] } 6246b4af23dS[email protected] 625452cf3bbS[email protected] static int hci_send_acl_packet_fragments(hci_connection_t *connection){ 626452cf3bbS[email protected] 627452cf3bbS[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); 628452cf3bbS[email protected] 629452cf3bbS[email protected] // max ACL data packet length depends on connection type (LE vs. Classic) and available buffers 630452cf3bbS[email protected] uint16_t max_acl_data_packet_length = hci_stack->acl_data_packet_length; 631452cf3bbS[email protected] if (hci_is_le_connection(connection) && hci_stack->le_data_packets_length > 0){ 632452cf3bbS[email protected] max_acl_data_packet_length = hci_stack->le_data_packets_length; 633452cf3bbS[email protected] } 634452cf3bbS[email protected] 635452cf3bbS[email protected] // testing: reduce buffer to minimum 636452cf3bbS[email protected] // max_acl_data_packet_length = 52; 637452cf3bbS[email protected] 638d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments entered"); 639d999b54eSMatthias Ringwald 640452cf3bbS[email protected] int err; 641452cf3bbS[email protected] // multiple packets could be send on a synchronous HCI transport 642452cf3bbS[email protected] while (1){ 643452cf3bbS[email protected] 644d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop entered"); 645d999b54eSMatthias Ringwald 646452cf3bbS[email protected] // get current data 647452cf3bbS[email protected] const uint16_t acl_header_pos = hci_stack->acl_fragmentation_pos - 4; 648452cf3bbS[email protected] int current_acl_data_packet_length = hci_stack->acl_fragmentation_total_size - hci_stack->acl_fragmentation_pos; 649452cf3bbS[email protected] int more_fragments = 0; 650452cf3bbS[email protected] 651452cf3bbS[email protected] // if ACL packet is larger than Bluetooth packet buffer, only send max_acl_data_packet_length 652452cf3bbS[email protected] if (current_acl_data_packet_length > max_acl_data_packet_length){ 653452cf3bbS[email protected] more_fragments = 1; 654452cf3bbS[email protected] current_acl_data_packet_length = max_acl_data_packet_length; 655452cf3bbS[email protected] } 656452cf3bbS[email protected] 657452cf3bbS[email protected] // copy handle_and_flags if not first fragment and update packet boundary flags to be 01 (continuing fragmnent) 658452cf3bbS[email protected] if (acl_header_pos > 0){ 659f8fbdce0SMatthias Ringwald uint16_t handle_and_flags = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 660452cf3bbS[email protected] handle_and_flags = (handle_and_flags & 0xcfff) | (1 << 12); 661f8fbdce0SMatthias Ringwald little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos, handle_and_flags); 662452cf3bbS[email protected] } 663452cf3bbS[email protected] 664452cf3bbS[email protected] // update header len 665f8fbdce0SMatthias Ringwald little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos + 2, current_acl_data_packet_length); 666452cf3bbS[email protected] 667452cf3bbS[email protected] // count packet 668ce41473eSMatthias Ringwald connection->num_packets_sent++; 669f04a0c31SMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop before send (more fragments %d)", more_fragments); 670d999b54eSMatthias Ringwald 671d999b54eSMatthias Ringwald // update state for next fragment (if any) as "transport done" might be sent during send_packet already 672d999b54eSMatthias Ringwald if (more_fragments){ 673d999b54eSMatthias Ringwald // update start of next fragment to send 674d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_pos += current_acl_data_packet_length; 675d999b54eSMatthias Ringwald } else { 676d999b54eSMatthias Ringwald // done 677d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_pos = 0; 678d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_total_size = 0; 679d999b54eSMatthias Ringwald } 680452cf3bbS[email protected] 681452cf3bbS[email protected] // send packet 682452cf3bbS[email protected] uint8_t * packet = &hci_stack->hci_packet_buffer[acl_header_pos]; 683452cf3bbS[email protected] const int size = current_acl_data_packet_length + 4; 6845bb5bc3eS[email protected] hci_dump_packet(HCI_ACL_DATA_PACKET, 0, packet, size); 68581d2bdb2SMatthias Ringwald hci_stack->acl_fragmentation_tx_active = 1; 686452cf3bbS[email protected] err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size); 687452cf3bbS[email protected] 688f04a0c31SMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop after send (more fragments %d)", more_fragments); 689d999b54eSMatthias Ringwald 690452cf3bbS[email protected] // done yet? 691452cf3bbS[email protected] if (!more_fragments) break; 692452cf3bbS[email protected] 693452cf3bbS[email protected] // can send more? 694452cf3bbS[email protected] if (!hci_can_send_prepared_acl_packet_now(connection->con_handle)) return err; 695452cf3bbS[email protected] } 696452cf3bbS[email protected] 697d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop over"); 698452cf3bbS[email protected] 699d051460cS[email protected] // release buffer now for synchronous transport 700203bace6S[email protected] if (hci_transport_synchronous()){ 70181d2bdb2SMatthias Ringwald hci_stack->acl_fragmentation_tx_active = 0; 702452cf3bbS[email protected] hci_release_packet_buffer(); 703fd43c0e0SMatthias Ringwald hci_emit_transport_packet_sent(); 704452cf3bbS[email protected] } 705452cf3bbS[email protected] 706452cf3bbS[email protected] return err; 707452cf3bbS[email protected] } 708452cf3bbS[email protected] 709826f7347S[email protected] // pre: caller has reserved the packet buffer 710826f7347S[email protected] int hci_send_acl_packet_buffer(int size){ 7117856c818Smatthias.ringwald 712452cf3bbS[email protected] // log_info("hci_send_acl_packet_buffer size %u", size); 713452cf3bbS[email protected] 714826f7347S[email protected] if (!hci_stack->hci_packet_buffer_reserved) { 715826f7347S[email protected] log_error("hci_send_acl_packet_buffer called without reserving packet buffer"); 716826f7347S[email protected] return 0; 717826f7347S[email protected] } 718826f7347S[email protected] 719d713a683S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 720d713a683S[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 721d713a683S[email protected] 722826f7347S[email protected] // check for free places on Bluetooth module 723d713a683S[email protected] if (!hci_can_send_prepared_acl_packet_now(con_handle)) { 724826f7347S[email protected] log_error("hci_send_acl_packet_buffer called but no free ACL buffers on controller"); 72597b61c7bS[email protected] hci_release_packet_buffer(); 726068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 72797b61c7bS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 72897b61c7bS[email protected] } 7296218e6f1Smatthias.ringwald 7305061f3afS[email protected] hci_connection_t *connection = hci_connection_for_handle( con_handle); 73197b61c7bS[email protected] if (!connection) { 7325fa0b7cfS[email protected] log_error("hci_send_acl_packet_buffer called but no connection for handle 0x%04x", con_handle); 73397b61c7bS[email protected] hci_release_packet_buffer(); 734068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 73597b61c7bS[email protected] return 0; 73697b61c7bS[email protected] } 73752db98b2SMatthias Ringwald 73852db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 73956cf178bSmatthias.ringwald hci_connection_timestamp(connection); 74052db98b2SMatthias Ringwald #endif 74156cf178bSmatthias.ringwald 742452cf3bbS[email protected] // hci_dump_packet( HCI_ACL_DATA_PACKET, 0, packet, size); 7437856c818Smatthias.ringwald 744452cf3bbS[email protected] // setup data 745452cf3bbS[email protected] hci_stack->acl_fragmentation_total_size = size; 746452cf3bbS[email protected] hci_stack->acl_fragmentation_pos = 4; // start of L2CAP packet 7476218e6f1Smatthias.ringwald 748452cf3bbS[email protected] return hci_send_acl_packet_fragments(connection); 749ee091cf1Smatthias.ringwald } 750ee091cf1Smatthias.ringwald 75135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 75244d0e3d5S[email protected] // pre: caller has reserved the packet buffer 75344d0e3d5S[email protected] int hci_send_sco_packet_buffer(int size){ 75444d0e3d5S[email protected] 75544d0e3d5S[email protected] // log_info("hci_send_acl_packet_buffer size %u", size); 75644d0e3d5S[email protected] 75744d0e3d5S[email protected] if (!hci_stack->hci_packet_buffer_reserved) { 75844d0e3d5S[email protected] log_error("hci_send_acl_packet_buffer called without reserving packet buffer"); 75944d0e3d5S[email protected] return 0; 76044d0e3d5S[email protected] } 76144d0e3d5S[email protected] 76244d0e3d5S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 7634b3e1e19SMatthias Ringwald 7644b3e1e19SMatthias Ringwald // skip checks in loopback mode 7654b3e1e19SMatthias Ringwald if (!hci_stack->loopback_mode){ 76644d0e3d5S[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); // same for ACL and SCO 76744d0e3d5S[email protected] 76844d0e3d5S[email protected] // check for free places on Bluetooth module 769701e3307SMatthias Ringwald if (!hci_can_send_prepared_sco_packet_now()) { 770cbf638a9SMatthias Ringwald log_error("hci_send_sco_packet_buffer called but no free SCO buffers on controller"); 77144d0e3d5S[email protected] hci_release_packet_buffer(); 772068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 77344d0e3d5S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 77444d0e3d5S[email protected] } 77544d0e3d5S[email protected] 776e35edcc1S[email protected] // track send packet in connection struct 777e35edcc1S[email protected] hci_connection_t *connection = hci_connection_for_handle( con_handle); 778e35edcc1S[email protected] if (!connection) { 779e35edcc1S[email protected] log_error("hci_send_sco_packet_buffer called but no connection for handle 0x%04x", con_handle); 780e35edcc1S[email protected] hci_release_packet_buffer(); 781068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 782e35edcc1S[email protected] return 0; 783e35edcc1S[email protected] } 784760b20efSMatthias Ringwald if (hci_stack->synchronous_flow_control_enabled){ 785ce41473eSMatthias Ringwald connection->num_packets_sent++; 786760b20efSMatthias Ringwald } 7874b3e1e19SMatthias Ringwald } 78844d0e3d5S[email protected] 78944d0e3d5S[email protected] hci_dump_packet( HCI_SCO_DATA_PACKET, 0, packet, size); 790543e835cSMatthias Ringwald int err = hci_stack->hci_transport->send_packet(HCI_SCO_DATA_PACKET, packet, size); 791543e835cSMatthias Ringwald 792543e835cSMatthias Ringwald if (hci_transport_synchronous()){ 793543e835cSMatthias Ringwald hci_release_packet_buffer(); 794fd43c0e0SMatthias Ringwald hci_emit_transport_packet_sent(); 795543e835cSMatthias Ringwald } 796543e835cSMatthias Ringwald 797543e835cSMatthias Ringwald return err; 79844d0e3d5S[email protected] } 79935454696SMatthias Ringwald #endif 80044d0e3d5S[email protected] 80116833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){ 8027856c818Smatthias.ringwald 803e76a89eeS[email protected] // log_info("acl_handler: size %u", size); 804e76a89eeS[email protected] 8057856c818Smatthias.ringwald // get info 8067856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 8075061f3afS[email protected] hci_connection_t *conn = hci_connection_for_handle(con_handle); 8087856c818Smatthias.ringwald uint8_t acl_flags = READ_ACL_FLAGS(packet); 8097856c818Smatthias.ringwald uint16_t acl_length = READ_ACL_LENGTH(packet); 8107856c818Smatthias.ringwald 8117856c818Smatthias.ringwald // ignore non-registered handle 8127856c818Smatthias.ringwald if (!conn){ 8139da54300S[email protected] log_error( "hci.c: acl_handler called with non-registered handle %u!" , con_handle); 8147856c818Smatthias.ringwald return; 8157856c818Smatthias.ringwald } 8167856c818Smatthias.ringwald 817e76a89eeS[email protected] // assert packet is complete 8189ecc3e17S[email protected] if (acl_length + 4 != size){ 819f04a0c31SMatthias Ringwald log_error("hci.c: acl_handler called with ACL packet of wrong size %d, expected %u => dropping packet", size, acl_length + 4); 820e76a89eeS[email protected] return; 821e76a89eeS[email protected] } 822e76a89eeS[email protected] 82352db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 8247856c818Smatthias.ringwald // update idle timestamp 8257856c818Smatthias.ringwald hci_connection_timestamp(conn); 82652db98b2SMatthias Ringwald #endif 8277856c818Smatthias.ringwald 8282b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 8292b838201SMatthias Ringwald hci_stack->host_completed_packets = 1; 8302b838201SMatthias Ringwald conn->num_packets_completed++; 8312b838201SMatthias Ringwald #endif 8322b838201SMatthias Ringwald 8337856c818Smatthias.ringwald // handle different packet types 8347856c818Smatthias.ringwald switch (acl_flags & 0x03) { 8357856c818Smatthias.ringwald 8367856c818Smatthias.ringwald case 0x01: // continuation fragment 8377856c818Smatthias.ringwald 8380ca847afS[email protected] // sanity checks 8397856c818Smatthias.ringwald if (conn->acl_recombination_pos == 0) { 8409da54300S[email protected] log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x", con_handle); 8417856c818Smatthias.ringwald return; 8427856c818Smatthias.ringwald } 8430ca847afS[email protected] if (conn->acl_recombination_pos + acl_length > 4 + HCI_ACL_BUFFER_SIZE){ 8440ca847afS[email protected] log_error( "ACL Cont Fragment to large: combined packet %u > buffer size %u for handle 0x%02x", 8450ca847afS[email protected] conn->acl_recombination_pos + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle); 8460ca847afS[email protected] conn->acl_recombination_pos = 0; 8470ca847afS[email protected] return; 8480ca847afS[email protected] } 8497856c818Smatthias.ringwald 8507856c818Smatthias.ringwald // append fragment payload (header already stored) 851ec6321eeS[email protected] memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + conn->acl_recombination_pos], &packet[4], acl_length ); 8527856c818Smatthias.ringwald conn->acl_recombination_pos += acl_length; 8537856c818Smatthias.ringwald 8549da54300S[email protected] // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u", acl_length, 855decc01a8Smatthias.ringwald // conn->acl_recombination_pos, conn->acl_recombination_length); 8567856c818Smatthias.ringwald 8577856c818Smatthias.ringwald // forward complete L2CAP packet if complete. 8587856c818Smatthias.ringwald if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header 859d6b06661SMatthias Ringwald hci_emit_acl_packet(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], conn->acl_recombination_pos); 8607856c818Smatthias.ringwald // reset recombination buffer 8617856c818Smatthias.ringwald conn->acl_recombination_length = 0; 8627856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 8637856c818Smatthias.ringwald } 8647856c818Smatthias.ringwald break; 8657856c818Smatthias.ringwald 8667856c818Smatthias.ringwald case 0x02: { // first fragment 8677856c818Smatthias.ringwald 86823a77e1aS[email protected] // sanity check 86923a77e1aS[email protected] if (conn->acl_recombination_pos) { 87023a77e1aS[email protected] log_error( "ACL First Fragment but data in buffer for handle 0x%02x, dropping stale fragments", con_handle); 87123a77e1aS[email protected] conn->acl_recombination_pos = 0; 87223a77e1aS[email protected] } 87323a77e1aS[email protected] 8747856c818Smatthias.ringwald // peek into L2CAP packet! 8757856c818Smatthias.ringwald uint16_t l2cap_length = READ_L2CAP_LENGTH( packet ); 8767856c818Smatthias.ringwald 8779da54300S[email protected] // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u", acl_length, l2cap_length); 878decc01a8Smatthias.ringwald 8797856c818Smatthias.ringwald // compare fragment size to L2CAP packet size 8807856c818Smatthias.ringwald if (acl_length >= l2cap_length + 4){ 8817856c818Smatthias.ringwald // forward fragment as L2CAP packet 882d6b06661SMatthias Ringwald hci_emit_acl_packet(packet, acl_length + 4); 8837856c818Smatthias.ringwald } else { 8840ca847afS[email protected] 8850ca847afS[email protected] if (acl_length > HCI_ACL_BUFFER_SIZE){ 8860ca847afS[email protected] log_error( "ACL First Fragment to large: fragment %u > buffer size %u for handle 0x%02x", 8870ca847afS[email protected] 4 + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle); 8880ca847afS[email protected] return; 8890ca847afS[email protected] } 8900ca847afS[email protected] 8917856c818Smatthias.ringwald // store first fragment and tweak acl length for complete package 892ec6321eeS[email protected] memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], packet, acl_length + 4); 8937856c818Smatthias.ringwald conn->acl_recombination_pos = acl_length + 4; 8947856c818Smatthias.ringwald conn->acl_recombination_length = l2cap_length; 895f8fbdce0SMatthias Ringwald little_endian_store_16(conn->acl_recombination_buffer, HCI_INCOMING_PRE_BUFFER_SIZE + 2, l2cap_length +4); 8967856c818Smatthias.ringwald } 8977856c818Smatthias.ringwald break; 8987856c818Smatthias.ringwald 8997856c818Smatthias.ringwald } 9007856c818Smatthias.ringwald default: 9019da54300S[email protected] log_error( "hci.c: acl_handler called with invalid packet boundary flags %u", acl_flags & 0x03); 9027856c818Smatthias.ringwald return; 9037856c818Smatthias.ringwald } 90494ab26f8Smatthias.ringwald 90594ab26f8Smatthias.ringwald // execute main loop 90694ab26f8Smatthias.ringwald hci_run(); 90716833f0aSmatthias.ringwald } 90822909952Smatthias.ringwald 90967a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){ 9109da54300S[email protected] log_info("Connection closed: handle 0x%x, %s", conn->con_handle, bd_addr_to_str(conn->address)); 9113c4d4b90Smatthias.ringwald 912b3264428SMatthias Ringwald #ifdef ENABLE_CLASSIC 913ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 914ee752bb8SMatthias Ringwald int addr_type = conn->address_type; 915ee752bb8SMatthias Ringwald #endif 916b3264428SMatthias Ringwald #endif 917ee752bb8SMatthias Ringwald 918528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&conn->timeout); 919c785ef68Smatthias.ringwald 920665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 921a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 9223c4d4b90Smatthias.ringwald 9233c4d4b90Smatthias.ringwald // now it's gone 924c7e0c5f6Smatthias.ringwald hci_emit_nr_connections_changed(); 925ee752bb8SMatthias Ringwald 926b3264428SMatthias Ringwald #ifdef ENABLE_CLASSIC 927034e9b53SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 928ee752bb8SMatthias Ringwald // update SCO 929ee752bb8SMatthias Ringwald if (addr_type == BD_ADDR_TYPE_SCO && hci_stack->hci_transport && hci_stack->hci_transport->set_sco_config){ 930ee752bb8SMatthias Ringwald hci_stack->hci_transport->set_sco_config(hci_stack->sco_voice_setting_active, hci_number_sco_connections()); 931ee752bb8SMatthias Ringwald } 932034e9b53SMatthias Ringwald #endif 933b3264428SMatthias Ringwald #endif 934c7e0c5f6Smatthias.ringwald } 935c7e0c5f6Smatthias.ringwald 93635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 93735454696SMatthias Ringwald 9380c042179S[email protected] static const uint16_t packet_type_sizes[] = { 9398f8108aaSmatthias.ringwald 0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE, 9408f8108aaSmatthias.ringwald HCI_ACL_DH1_SIZE, 0, 0, 0, 9418f8108aaSmatthias.ringwald HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE, 9428f8108aaSmatthias.ringwald HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE 9438f8108aaSmatthias.ringwald }; 94465389bfcS[email protected] static const uint8_t packet_type_feature_requirement_bit[] = { 94565389bfcS[email protected] 0, // 3 slot packets 94665389bfcS[email protected] 1, // 5 slot packets 94765389bfcS[email protected] 25, // EDR 2 mpbs 94865389bfcS[email protected] 26, // EDR 3 mbps 94965389bfcS[email protected] 39, // 3 slot EDR packts 95065389bfcS[email protected] 40, // 5 slot EDR packet 95165389bfcS[email protected] }; 95265389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = { 95365389bfcS[email protected] 0x0f00, // 3 slot packets 95465389bfcS[email protected] 0xf000, // 5 slot packets 95565389bfcS[email protected] 0x1102, // EDR 2 mpbs 95665389bfcS[email protected] 0x2204, // EDR 3 mbps 95765389bfcS[email protected] 0x0300, // 3 slot EDR packts 95865389bfcS[email protected] 0x3000, // 5 slot EDR packet 95965389bfcS[email protected] }; 9608f8108aaSmatthias.ringwald 96165389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){ 96265389bfcS[email protected] // enable packet types based on size 9638f8108aaSmatthias.ringwald uint16_t packet_types = 0; 964f16a69bbS[email protected] unsigned int i; 9658f8108aaSmatthias.ringwald for (i=0;i<16;i++){ 9668f8108aaSmatthias.ringwald if (packet_type_sizes[i] == 0) continue; 9678f8108aaSmatthias.ringwald if (packet_type_sizes[i] <= buffer_size){ 9688f8108aaSmatthias.ringwald packet_types |= 1 << i; 9698f8108aaSmatthias.ringwald } 9708f8108aaSmatthias.ringwald } 97165389bfcS[email protected] // disable packet types due to missing local supported features 97265389bfcS[email protected] for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){ 973f04a0c31SMatthias Ringwald unsigned int bit_idx = packet_type_feature_requirement_bit[i]; 97465389bfcS[email protected] int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0; 97565389bfcS[email protected] if (feature_set) continue; 97665389bfcS[email protected] log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]); 97765389bfcS[email protected] packet_types &= ~packet_type_feature_packet_mask[i]; 97865389bfcS[email protected] } 9798f8108aaSmatthias.ringwald // flip bits for "may not be used" 9808f8108aaSmatthias.ringwald packet_types ^= 0x3306; 9818f8108aaSmatthias.ringwald return packet_types; 9828f8108aaSmatthias.ringwald } 9838f8108aaSmatthias.ringwald 9848f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){ 9853a9fb326S[email protected] return hci_stack->packet_types; 9868f8108aaSmatthias.ringwald } 98735454696SMatthias Ringwald #endif 9888f8108aaSmatthias.ringwald 989facf93fdS[email protected] uint8_t* hci_get_outgoing_packet_buffer(void){ 9907dc17943Smatthias.ringwald // hci packet buffer is >= acl data packet length 9913a9fb326S[email protected] return hci_stack->hci_packet_buffer; 9927dc17943Smatthias.ringwald } 9937dc17943Smatthias.ringwald 994f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){ 9953a9fb326S[email protected] return hci_stack->acl_data_packet_length; 9967dc17943Smatthias.ringwald } 9977dc17943Smatthias.ringwald 99806b9e820SMatthias Ringwald #ifdef ENABLE_CLASSIC 9993e68d23dSMatthias Ringwald int hci_extended_sco_link_supported(void){ 10003e68d23dSMatthias Ringwald // No. 31, byte 3, bit 7 10013e68d23dSMatthias Ringwald return (hci_stack->local_supported_features[3] & (1 << 7)) != 0; 10023e68d23dSMatthias Ringwald } 100306b9e820SMatthias Ringwald #endif 10043e68d23dSMatthias Ringwald 10056ac9a97eS[email protected] int hci_non_flushable_packet_boundary_flag_supported(void){ 10066ac9a97eS[email protected] // No. 54, byte 6, bit 6 10076ac9a97eS[email protected] return (hci_stack->local_supported_features[6] & (1 << 6)) != 0; 10086ac9a97eS[email protected] } 10096ac9a97eS[email protected] 101015a95bd5SMatthias Ringwald static int gap_ssp_supported(void){ 10116ac9a97eS[email protected] // No. 51, byte 6, bit 3 10123a9fb326S[email protected] return (hci_stack->local_supported_features[6] & (1 << 3)) != 0; 1013f5d8d141S[email protected] } 1014f5d8d141S[email protected] 10157f02f414SMatthias Ringwald static int hci_classic_supported(void){ 101635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 10176ac9a97eS[email protected] // No. 37, byte 4, bit 5, = No BR/EDR Support 10183a9fb326S[email protected] return (hci_stack->local_supported_features[4] & (1 << 5)) == 0; 101935454696SMatthias Ringwald #else 102035454696SMatthias Ringwald return 0; 102135454696SMatthias Ringwald #endif 1022f5d8d141S[email protected] } 1023f5d8d141S[email protected] 10247f02f414SMatthias Ringwald static int hci_le_supported(void){ 1025a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 10266ac9a97eS[email protected] // No. 37, byte 4, bit 6 = LE Supported (Controller) 10273a9fb326S[email protected] return (hci_stack->local_supported_features[4] & (1 << 6)) != 0; 1028f5d8d141S[email protected] #else 1029f5d8d141S[email protected] return 0; 1030f5d8d141S[email protected] #endif 1031f5d8d141S[email protected] } 1032f5d8d141S[email protected] 1033b95a5a35SMatthias Ringwald #ifdef ENABLE_BLE 1034b95a5a35SMatthias Ringwald 1035b95a5a35SMatthias Ringwald /** 1036b95a5a35SMatthias Ringwald * @brief Get addr type and address used for LE in Advertisements, Scan Responses, 1037b95a5a35SMatthias Ringwald */ 1038b95a5a35SMatthias Ringwald void gap_le_get_own_address(uint8_t * addr_type, bd_addr_t addr){ 1039b95a5a35SMatthias Ringwald *addr_type = hci_stack->le_own_addr_type; 1040b95a5a35SMatthias Ringwald if (hci_stack->le_own_addr_type){ 1041b95a5a35SMatthias Ringwald memcpy(addr, hci_stack->le_random_address, 6); 104269a97523S[email protected] } else { 10433a9fb326S[email protected] memcpy(addr, hci_stack->local_bd_addr, 6); 104469a97523S[email protected] } 104569a97523S[email protected] } 104669a97523S[email protected] 1047e8c8828eSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 1048384b59deSMatthias Ringwald void le_handle_advertisement_report(uint8_t *packet, uint16_t size){ 10499ec2630cSMatthias Ringwald 1050d1dc057bS[email protected] int offset = 3; 1051d1dc057bS[email protected] int num_reports = packet[offset]; 1052d1dc057bS[email protected] offset += 1; 1053d1dc057bS[email protected] 105457c9da5bS[email protected] int i; 10554f4e0224SMatthias Ringwald // log_info("HCI: handle adv report with num reports: %d", num_reports); 105603fbe9c6S[email protected] uint8_t event[12 + LE_ADVERTISING_DATA_SIZE]; // use upper bound to avoid var size automatic var 1057384b59deSMatthias Ringwald for (i=0; i<num_reports && offset < size;i++){ 105833e6948bSMatthias Ringwald // sanity checks on data_length: 105933e6948bSMatthias Ringwald uint8_t data_length = packet[offset + 8]; 106033e6948bSMatthias Ringwald if (data_length > LE_ADVERTISING_DATA_SIZE) return; 106133e6948bSMatthias Ringwald if (offset + 9 + data_length + 1 > size) return; 106233e6948bSMatthias Ringwald // setup event 1063a0aac9c4S[email protected] uint8_t event_size = 10 + data_length; 1064d1dc057bS[email protected] int pos = 0; 1065045013feSMatthias Ringwald event[pos++] = GAP_EVENT_ADVERTISING_REPORT; 106657c9da5bS[email protected] event[pos++] = event_size; 1067210c6774S[email protected] memcpy(&event[pos], &packet[offset], 1+1+6); // event type + address type + address 1068d1dc057bS[email protected] offset += 8; 1069d1dc057bS[email protected] pos += 8; 1070d1dc057bS[email protected] event[pos++] = packet[offset + 1 + data_length]; // rssi 107133e6948bSMatthias Ringwald event[pos++] = data_length; 107233e6948bSMatthias Ringwald offset++; 1073d1dc057bS[email protected] memcpy(&event[pos], &packet[offset], data_length); 107457c9da5bS[email protected] pos += data_length; 1075d1dc057bS[email protected] offset += data_length + 1; // rssi 1076d6b06661SMatthias Ringwald hci_emit_event(event, pos, 1); 107757c9da5bS[email protected] } 107857c9da5bS[email protected] } 1079b2f949feS[email protected] #endif 1080e8c8828eSMatthias Ringwald #endif 108157c9da5bS[email protected] 10822b6ab3e6SMatthias Ringwald #ifdef ENABLE_BLE 10832b6ab3e6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 10842b6ab3e6SMatthias Ringwald static void hci_reenable_advertisements_if_needed(void){ 10852b6ab3e6SMatthias Ringwald if (!hci_stack->le_advertisements_active && hci_stack->le_advertisements_enabled){ 10862b6ab3e6SMatthias Ringwald // get number of active le slave connections 10872b6ab3e6SMatthias Ringwald int num_slave_connections = 0; 10882b6ab3e6SMatthias Ringwald btstack_linked_list_iterator_t it; 10892b6ab3e6SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 10902b6ab3e6SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 10912b6ab3e6SMatthias Ringwald hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 10922b6ab3e6SMatthias Ringwald log_info("state %u, role %u, le_con %u", con->state, con->role, hci_is_le_connection(con)); 10932b6ab3e6SMatthias Ringwald if (con->state != OPEN) continue; 10942b6ab3e6SMatthias Ringwald if (con->role != HCI_ROLE_SLAVE) continue; 10952b6ab3e6SMatthias Ringwald if (!hci_is_le_connection(con)) continue; 10962b6ab3e6SMatthias Ringwald num_slave_connections++; 10972b6ab3e6SMatthias Ringwald } 10982b6ab3e6SMatthias Ringwald log_info("Num LE Peripheral roles: %u of %u", num_slave_connections, hci_stack->le_max_number_peripheral_connections); 10992b6ab3e6SMatthias Ringwald if (num_slave_connections < hci_stack->le_max_number_peripheral_connections){ 11002b6ab3e6SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_ENABLE; 11012b6ab3e6SMatthias Ringwald } 11022b6ab3e6SMatthias Ringwald } 11032b6ab3e6SMatthias Ringwald } 11042b6ab3e6SMatthias Ringwald #endif 11052b6ab3e6SMatthias Ringwald #endif 11062b6ab3e6SMatthias Ringwald 1107cd77dd38SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 110806b9e820SMatthias Ringwald 110996b53536SMatthias Ringwald static uint32_t hci_transport_uart_get_main_baud_rate(void){ 111096b53536SMatthias Ringwald if (!hci_stack->config) return 0; 11119796ebeaSMatthias Ringwald uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 111296b53536SMatthias Ringwald // Limit baud rate for Broadcom chipsets to 3 mbps 111361f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION && baud_rate > 3000000){ 111496b53536SMatthias Ringwald baud_rate = 3000000; 111596b53536SMatthias Ringwald } 111696b53536SMatthias Ringwald return baud_rate; 111796b53536SMatthias Ringwald } 111896b53536SMatthias Ringwald 1119ec820d77SMatthias Ringwald static void hci_initialization_timeout_handler(btstack_timer_source_t * ds){ 11209ec2630cSMatthias Ringwald UNUSED(ds); 11219ec2630cSMatthias Ringwald 11220305bdeaSMatthias Ringwald switch (hci_stack->substate){ 11230305bdeaSMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 11247b0d7667SMatthias Ringwald log_info("Resend HCI Reset"); 11250305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET; 11267b0d7667SMatthias Ringwald hci_stack->num_cmd_packets = 1; 11270305bdeaSMatthias Ringwald hci_run(); 11280305bdeaSMatthias Ringwald break; 11299f007422SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET: 11309f007422SMatthias Ringwald log_info("Resend HCI Reset - CSR Warm Boot with Link Reset"); 11319f007422SMatthias Ringwald if (hci_stack->hci_transport->reset_link){ 11329f007422SMatthias Ringwald hci_stack->hci_transport->reset_link(); 11339f007422SMatthias Ringwald } 1134202c8a4cSMatthias Ringwald // no break - explicit fallthrough to HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT 1135e47e68c7SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT: 1136e47e68c7SMatthias Ringwald log_info("Resend HCI Reset - CSR Warm Boot"); 1137e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT; 1138e47e68c7SMatthias Ringwald hci_stack->num_cmd_packets = 1; 1139e47e68c7SMatthias Ringwald hci_run(); 1140688c2635SMatthias Ringwald break; 11417224be7eSMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE: 11427224be7eSMatthias Ringwald if (hci_stack->hci_transport->set_baudrate){ 114396b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1144cd724cb7SMatthias Ringwald log_info("Local baud rate change to %"PRIu32"(timeout handler)", baud_rate); 11457dd9d0ecSMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 11467224be7eSMatthias Ringwald } 1147834bce8cSMatthias Ringwald // For CSR, HCI Reset is sent on new baud rate. Don't forget to reset link for H5/BCSP 114861f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO){ 1149834bce8cSMatthias Ringwald if (hci_stack->hci_transport->reset_link){ 1150834bce8cSMatthias Ringwald log_info("Link Reset"); 1151834bce8cSMatthias Ringwald hci_stack->hci_transport->reset_link(); 1152834bce8cSMatthias Ringwald } 1153772a36d3SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT; 1154772a36d3SMatthias Ringwald hci_run(); 1155772a36d3SMatthias Ringwald } 11564696bddbSMatthias Ringwald break; 1157559961d0SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_BCM_DELAY: 1158559961d0SMatthias Ringwald // otherwise continue 1159559961d0SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 1160559961d0SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 1161559961d0SMatthias Ringwald break; 11620305bdeaSMatthias Ringwald default: 11630305bdeaSMatthias Ringwald break; 11640305bdeaSMatthias Ringwald } 11650305bdeaSMatthias Ringwald } 116606b9e820SMatthias Ringwald #endif 11670305bdeaSMatthias Ringwald 116871de195eSMatthias Ringwald static void hci_initializing_next_state(void){ 116974b323a9SMatthias Ringwald hci_stack->substate = (hci_substate_t )( ((int) hci_stack->substate) + 1); 117074b323a9SMatthias Ringwald } 117174b323a9SMatthias Ringwald 1172e9f343c8SMatthias Ringwald #if defined(ENABLE_CLASSIC) || defined(ENABLE_LE_PERIPHERAL) 11731c2a62f5SMatthias Ringwald static void hci_replace_bd_addr_placeholder(uint8_t * data, uint16_t size){ 11741c2a62f5SMatthias Ringwald const int bd_addr_string_len = 17; 11751c2a62f5SMatthias Ringwald int i = 0; 11761c2a62f5SMatthias Ringwald while (i < size - bd_addr_string_len){ 11771c2a62f5SMatthias Ringwald if (memcmp(&data[i], "00:00:00:00:00:00", bd_addr_string_len)) { 11781c2a62f5SMatthias Ringwald i++; 11791c2a62f5SMatthias Ringwald continue; 11801c2a62f5SMatthias Ringwald } 11811c2a62f5SMatthias Ringwald // set real address 11821c2a62f5SMatthias Ringwald memcpy(&data[i], bd_addr_to_str(hci_stack->local_bd_addr), bd_addr_string_len); 11831c2a62f5SMatthias Ringwald i += bd_addr_string_len; 11841c2a62f5SMatthias Ringwald } 11851c2a62f5SMatthias Ringwald } 11861c2a62f5SMatthias Ringwald #endif 11871c2a62f5SMatthias Ringwald 118874b323a9SMatthias Ringwald // assumption: hci_can_send_command_packet_now() == true 118971de195eSMatthias Ringwald static void hci_initializing_run(void){ 1190148ca237SMatthias Ringwald log_debug("hci_initializing_run: substate %u, can send %u", hci_stack->substate, hci_can_send_command_packet_now()); 119174b323a9SMatthias Ringwald switch (hci_stack->substate){ 119274b323a9SMatthias Ringwald case HCI_INIT_SEND_RESET: 119374b323a9SMatthias Ringwald hci_state_reset(); 1194a0cf2f3fSMatthias Ringwald 119506b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 11960305bdeaSMatthias Ringwald // prepare reset if command complete not received in 100ms 1197659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1198528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1199528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1200a0cf2f3fSMatthias Ringwald #endif 12010305bdeaSMatthias Ringwald // send command 120274b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 12030305bdeaSMatthias Ringwald hci_send_cmd(&hci_reset); 120474b323a9SMatthias Ringwald break; 120576fcb19bSMatthias Ringwald case HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION: 120676fcb19bSMatthias Ringwald hci_send_cmd(&hci_read_local_version_information); 120776fcb19bSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION; 120876fcb19bSMatthias Ringwald break; 1209e90bae01SMatthias Ringwald case HCI_INIT_SEND_READ_LOCAL_NAME: 1210e90bae01SMatthias Ringwald hci_send_cmd(&hci_read_local_name); 1211e90bae01SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_NAME; 1212e90bae01SMatthias Ringwald break; 121306b9e820SMatthias Ringwald 121406b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 1215e47e68c7SMatthias Ringwald case HCI_INIT_SEND_RESET_CSR_WARM_BOOT: 1216e47e68c7SMatthias Ringwald hci_state_reset(); 1217e47e68c7SMatthias Ringwald // prepare reset if command complete not received in 100ms 1218659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1219528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1220528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1221e47e68c7SMatthias Ringwald // send command 1222e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT; 1223e47e68c7SMatthias Ringwald hci_send_cmd(&hci_reset); 1224e47e68c7SMatthias Ringwald break; 12258d29070eSMatthias Ringwald case HCI_INIT_SEND_RESET_ST_WARM_BOOT: 12268d29070eSMatthias Ringwald hci_state_reset(); 12278d29070eSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT; 12288d29070eSMatthias Ringwald hci_send_cmd(&hci_reset); 12298d29070eSMatthias Ringwald break; 1230fab26ab3SMatthias Ringwald case HCI_INIT_SEND_BAUD_CHANGE: { 123196b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 12323fb36a29SMatthias Ringwald hci_stack->chipset->set_baudrate_command(baud_rate, hci_stack->hci_packet_buffer); 1233f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 123474b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE; 12350305bdeaSMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]); 12364696bddbSMatthias Ringwald // STLC25000D: baudrate change happens within 0.5 s after command was send, 12374696bddbSMatthias Ringwald // use timer to update baud rate after 100 ms (knowing exactly, when command was sent is non-trivial) 123861f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS){ 1239659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1240528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 12414696bddbSMatthias Ringwald } 124274b323a9SMatthias Ringwald break; 1243fab26ab3SMatthias Ringwald } 1244fab26ab3SMatthias Ringwald case HCI_INIT_SEND_BAUD_CHANGE_BCM: { 124596b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 12463fb36a29SMatthias Ringwald hci_stack->chipset->set_baudrate_command(baud_rate, hci_stack->hci_packet_buffer); 1247f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 1248eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE_BCM; 1249eb3a5314SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]); 1250eb3a5314SMatthias Ringwald break; 1251fab26ab3SMatthias Ringwald } 125274b323a9SMatthias Ringwald case HCI_INIT_CUSTOM_INIT: 125374b323a9SMatthias Ringwald // Custom initialization 12543fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->next_command){ 12553fb36a29SMatthias Ringwald int valid_cmd = (*hci_stack->chipset->next_command)(hci_stack->hci_packet_buffer); 125674b323a9SMatthias Ringwald if (valid_cmd){ 125774b323a9SMatthias Ringwald int size = 3 + hci_stack->hci_packet_buffer[2]; 1258f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 125974b323a9SMatthias Ringwald hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, hci_stack->hci_packet_buffer, size); 1260e47e68c7SMatthias Ringwald switch (valid_cmd) { 1261f41911edSMatthias Ringwald case BTSTACK_CHIPSET_VALID_COMMAND: 1262e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT; 1263e47e68c7SMatthias Ringwald break; 1264f41911edSMatthias Ringwald case BTSTACK_CHIPSET_WARMSTART_REQUIRED: 1265f41911edSMatthias Ringwald // CSR Warm Boot: Wait a bit, then send HCI Reset until HCI Command Complete 1266e47e68c7SMatthias Ringwald log_info("CSR Warm Boot"); 1267659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1268528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1269528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 127061f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO 1271772a36d3SMatthias Ringwald && hci_stack->config 12723fb36a29SMatthias Ringwald && hci_stack->chipset 12733fb36a29SMatthias Ringwald // && hci_stack->chipset->set_baudrate_command -- there's no such command 1274772a36d3SMatthias Ringwald && hci_stack->hci_transport->set_baudrate 12752caefae9SMatthias Ringwald && hci_transport_uart_get_main_baud_rate()){ 1276772a36d3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE; 1277772a36d3SMatthias Ringwald } else { 12789f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET; 1279772a36d3SMatthias Ringwald } 1280e47e68c7SMatthias Ringwald break; 1281f41911edSMatthias Ringwald default: 1282f41911edSMatthias Ringwald // should not get here 1283f41911edSMatthias Ringwald break; 1284e47e68c7SMatthias Ringwald } 12850305bdeaSMatthias Ringwald hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size); 128674b323a9SMatthias Ringwald break; 128774b323a9SMatthias Ringwald } 1288148ca237SMatthias Ringwald log_info("Init script done"); 128992a0d36dSMatthias Ringwald 1290559961d0SMatthias Ringwald // Init script download on Broadcom chipsets causes: 1291e021ff1eSMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION 1292e021ff1eSMatthias Ringwald || hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_EM_MICROELECTRONIC_MARIN_SA){ 1293e021ff1eSMatthias Ringwald 1294559961d0SMatthias Ringwald // - baud rate to reset, restore UART baud rate if needed 129592a0d36dSMatthias Ringwald int need_baud_change = hci_stack->config 12963fb36a29SMatthias Ringwald && hci_stack->chipset 12973fb36a29SMatthias Ringwald && hci_stack->chipset->set_baudrate_command 129892a0d36dSMatthias Ringwald && hci_stack->hci_transport->set_baudrate 12999796ebeaSMatthias Ringwald && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 130092a0d36dSMatthias Ringwald if (need_baud_change) { 13019796ebeaSMatthias Ringwald uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_init; 1302cd724cb7SMatthias Ringwald log_info("Local baud rate change to %"PRIu32" after init script (bcm)", baud_rate); 130392a0d36dSMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 130492a0d36dSMatthias Ringwald } 1305559961d0SMatthias Ringwald 1306559961d0SMatthias Ringwald // - RTS will raise during update, but manual RTS/CTS in WICED port on RedBear Duo cannot handle this 1307559961d0SMatthias Ringwald // -> Work around: wait a few milliseconds here. 1308559961d0SMatthias Ringwald log_info("BCM delay after init script"); 1309559961d0SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_BCM_DELAY; 1310559961d0SMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, 10); 1311559961d0SMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1312559961d0SMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1313559961d0SMatthias Ringwald break; 131492a0d36dSMatthias Ringwald } 131574b323a9SMatthias Ringwald } 131674b323a9SMatthias Ringwald // otherwise continue 1317a828a756SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 1318a828a756SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 1319a828a756SMatthias Ringwald break; 132053860077SMatthias Ringwald case HCI_INIT_SET_BD_ADDR: 132153860077SMatthias Ringwald log_info("Set Public BD ADDR to %s", bd_addr_to_str(hci_stack->custom_bd_addr)); 13223fb36a29SMatthias Ringwald hci_stack->chipset->set_bd_addr_command(hci_stack->custom_bd_addr, hci_stack->hci_packet_buffer); 1323f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 132453860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_BD_ADDR; 132553860077SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]); 132653860077SMatthias Ringwald break; 132706b9e820SMatthias Ringwald #endif 132806b9e820SMatthias Ringwald 132906b9e820SMatthias Ringwald case HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS: 133006b9e820SMatthias Ringwald log_info("Resend hci_read_local_supported_commands after CSR Warm Boot double reset"); 133106b9e820SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 133206b9e820SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 133306b9e820SMatthias Ringwald break; 133453860077SMatthias Ringwald case HCI_INIT_READ_BD_ADDR: 133553860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_BD_ADDR; 133653860077SMatthias Ringwald hci_send_cmd(&hci_read_bd_addr); 133753860077SMatthias Ringwald break; 133874b323a9SMatthias Ringwald case HCI_INIT_READ_BUFFER_SIZE: 133974b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_BUFFER_SIZE; 13400305bdeaSMatthias Ringwald hci_send_cmd(&hci_read_buffer_size); 134174b323a9SMatthias Ringwald break; 134253860077SMatthias Ringwald case HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES: 134353860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_FEATURES; 13440305bdeaSMatthias Ringwald hci_send_cmd(&hci_read_local_supported_features); 134574b323a9SMatthias Ringwald break; 13462b838201SMatthias Ringwald 13472b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 13482b838201SMatthias Ringwald case HCI_INIT_SET_CONTROLLER_TO_HOST_FLOW_CONTROL: 13492b838201SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_CONTROLLER_TO_HOST_FLOW_CONTROL; 13502b838201SMatthias Ringwald hci_send_cmd(&hci_set_controller_to_host_flow_control, 3); // ACL + SCO Flow Control 13512b838201SMatthias Ringwald break; 13522b838201SMatthias Ringwald case HCI_INIT_HOST_BUFFER_SIZE: 13532b838201SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_HOST_BUFFER_SIZE; 13542b838201SMatthias Ringwald hci_send_cmd(&hci_host_buffer_size, HCI_HOST_ACL_PACKET_LEN, HCI_HOST_SCO_PACKET_LEN, 13552b838201SMatthias Ringwald HCI_HOST_ACL_PACKET_NUM, HCI_HOST_SCO_PACKET_NUM); 13562b838201SMatthias Ringwald break; 13572b838201SMatthias Ringwald #endif 13582b838201SMatthias Ringwald 135974b323a9SMatthias Ringwald case HCI_INIT_SET_EVENT_MASK: 13600305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_EVENT_MASK; 136174b323a9SMatthias Ringwald if (hci_le_supported()){ 136274b323a9SMatthias Ringwald hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF); 136374b323a9SMatthias Ringwald } else { 136474b323a9SMatthias Ringwald // Kensington Bluetooth 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff... 136574b323a9SMatthias Ringwald hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF); 136674b323a9SMatthias Ringwald } 136774b323a9SMatthias Ringwald break; 13682b838201SMatthias Ringwald 136935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 137074b323a9SMatthias Ringwald case HCI_INIT_WRITE_SIMPLE_PAIRING_MODE: 137174b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SIMPLE_PAIRING_MODE; 13720305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable); 137374b323a9SMatthias Ringwald break; 137474b323a9SMatthias Ringwald case HCI_INIT_WRITE_PAGE_TIMEOUT: 137574b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_PAGE_TIMEOUT; 13760305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_page_timeout, 0x6000); // ca. 15 sec 137774b323a9SMatthias Ringwald break; 1378c33e56d3SMatthias Ringwald case HCI_INIT_WRITE_DEFAULT_LINK_POLICY_SETTING: 1379c33e56d3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_DEFAULT_LINK_POLICY_SETTING; 1380c33e56d3SMatthias Ringwald hci_send_cmd(&hci_write_default_link_policy_setting, hci_stack->default_link_policy_settings); 1381c33e56d3SMatthias Ringwald break; 138274b323a9SMatthias Ringwald case HCI_INIT_WRITE_CLASS_OF_DEVICE: 138374b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_CLASS_OF_DEVICE; 13840305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device); 138574b323a9SMatthias Ringwald break; 13861c2a62f5SMatthias Ringwald case HCI_INIT_WRITE_LOCAL_NAME: { 13870305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_LOCAL_NAME; 13881c2a62f5SMatthias Ringwald hci_reserve_packet_buffer(); 13891c2a62f5SMatthias Ringwald uint8_t * packet = hci_stack->hci_packet_buffer; 13901c2a62f5SMatthias Ringwald // construct HCI Command and send 13911c2a62f5SMatthias Ringwald uint16_t opcode = hci_write_local_name.opcode; 13921c2a62f5SMatthias Ringwald hci_stack->last_cmd_opcode = opcode; 13931c2a62f5SMatthias Ringwald packet[0] = opcode & 0xff; 13941c2a62f5SMatthias Ringwald packet[1] = opcode >> 8; 13951c2a62f5SMatthias Ringwald packet[2] = DEVICE_NAME_LEN; 13961c2a62f5SMatthias Ringwald memset(&packet[3], 0, DEVICE_NAME_LEN); 139763168530SMatthias Ringwald memcpy(&packet[3], hci_stack->local_name, strlen(hci_stack->local_name)); 13981c2a62f5SMatthias Ringwald // expand '00:00:00:00:00:00' in name with bd_addr 13991c2a62f5SMatthias Ringwald hci_replace_bd_addr_placeholder(&packet[3], DEVICE_NAME_LEN); 14001c2a62f5SMatthias Ringwald hci_send_cmd_packet(packet, HCI_CMD_HEADER_SIZE + DEVICE_NAME_LEN); 140174b323a9SMatthias Ringwald break; 14021c2a62f5SMatthias Ringwald } 140329af07f3SMatthias Ringwald case HCI_INIT_WRITE_EIR_DATA: { 14048a114470SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_EIR_DATA; 140529af07f3SMatthias Ringwald hci_reserve_packet_buffer(); 140629af07f3SMatthias Ringwald uint8_t * packet = hci_stack->hci_packet_buffer; 140729af07f3SMatthias Ringwald // construct HCI Command and send 140829af07f3SMatthias Ringwald uint16_t opcode = hci_write_extended_inquiry_response.opcode; 140929af07f3SMatthias Ringwald hci_stack->last_cmd_opcode = opcode; 141029af07f3SMatthias Ringwald packet[0] = opcode & 0xff; 141129af07f3SMatthias Ringwald packet[1] = opcode >> 8; 141229af07f3SMatthias Ringwald packet[2] = 1 + 240; 141329af07f3SMatthias Ringwald packet[3] = 0; // FEC not required 1414ad0d1108SMatthias Ringwald if (hci_stack->eir_data){ 141529af07f3SMatthias Ringwald memcpy(&packet[4], hci_stack->eir_data, 240); 1416ad0d1108SMatthias Ringwald } else { 1417ad0d1108SMatthias Ringwald memset(&packet[4], 0, 240); 1418ad0d1108SMatthias Ringwald int name_len = strlen(hci_stack->local_name); 1419ad0d1108SMatthias Ringwald packet[4] = name_len + 1; 1420ad0d1108SMatthias Ringwald packet[5] = BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME; 1421ad0d1108SMatthias Ringwald memcpy(&packet[6], hci_stack->local_name, name_len); 1422ad0d1108SMatthias Ringwald } 142329af07f3SMatthias Ringwald // expand '00:00:00:00:00:00' in name with bd_addr 142429af07f3SMatthias Ringwald hci_replace_bd_addr_placeholder(&packet[4], 240); 1425c439e296SMatthias Ringwald hci_send_cmd_packet(packet, HCI_CMD_HEADER_SIZE + 1 + 240); 1426ff00ed1cSMatthias Ringwald break; 142729af07f3SMatthias Ringwald } 1428f6858d14SMatthias Ringwald case HCI_INIT_WRITE_INQUIRY_MODE: 1429f6858d14SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_INQUIRY_MODE; 14308a114470SMatthias Ringwald hci_send_cmd(&hci_write_inquiry_mode, (int) hci_stack->inquiry_mode); 1431f6858d14SMatthias Ringwald break; 143274b323a9SMatthias Ringwald case HCI_INIT_WRITE_SCAN_ENABLE: 143374b323a9SMatthias Ringwald hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan 143474b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SCAN_ENABLE; 143574b323a9SMatthias Ringwald break; 1436483c5078SMatthias Ringwald // only sent if ENABLE_SCO_OVER_HCI is defined 1437729ed62eSMatthias Ringwald case HCI_INIT_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE: 1438729ed62eSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE; 1439729ed62eSMatthias Ringwald hci_send_cmd(&hci_write_synchronous_flow_control_enable, 1); // SCO tracking enabled 1440729ed62eSMatthias Ringwald break; 1441483c5078SMatthias Ringwald case HCI_INIT_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING: 1442483c5078SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING; 1443483c5078SMatthias Ringwald hci_send_cmd(&hci_write_default_erroneous_data_reporting, 1); 1444483c5078SMatthias Ringwald break; 1445a42798c3SMatthias Ringwald // only sent if ENABLE_SCO_OVER_HCI and manufacturer is Broadcom 1446a42798c3SMatthias Ringwald case HCI_INIT_BCM_WRITE_SCO_PCM_INT: 1447a42798c3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT; 1448a42798c3SMatthias Ringwald log_info("BCM: Route SCO data via HCI transport"); 1449a42798c3SMatthias Ringwald hci_send_cmd(&hci_bcm_write_sco_pcm_int, 1, 0, 0, 0, 0); 1450a42798c3SMatthias Ringwald break; 1451a42798c3SMatthias Ringwald 145235454696SMatthias Ringwald #endif 1453a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 145474b323a9SMatthias Ringwald // LE INIT 145574b323a9SMatthias Ringwald case HCI_INIT_LE_READ_BUFFER_SIZE: 145674b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_READ_BUFFER_SIZE; 14570305bdeaSMatthias Ringwald hci_send_cmd(&hci_le_read_buffer_size); 145874b323a9SMatthias Ringwald break; 1459daabb8b8SMatthias Ringwald case HCI_INIT_LE_SET_EVENT_MASK: 1460daabb8b8SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_SET_EVENT_MASK; 1461729710c0SMatthias Ringwald hci_send_cmd(&hci_le_set_event_mask, 0x809FF, 0x0); // bits 0-8, 11, 19 1462daabb8b8SMatthias Ringwald break; 146374b323a9SMatthias Ringwald case HCI_INIT_WRITE_LE_HOST_SUPPORTED: 146474b323a9SMatthias Ringwald // LE Supported Host = 1, Simultaneous Host = 0 146574b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_LE_HOST_SUPPORTED; 14660305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_le_host_supported, 1, 0); 146774b323a9SMatthias Ringwald break; 1468b435e062SMatthias Ringwald #endif 1469b435e062SMatthias Ringwald 1470b435e062SMatthias Ringwald #ifdef ENABLE_LE_DATA_LENGTH_EXTENSION 1471dcd678baSMatthias Ringwald case HCI_INIT_LE_READ_MAX_DATA_LENGTH: 1472dcd678baSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_READ_MAX_DATA_LENGTH; 1473dcd678baSMatthias Ringwald hci_send_cmd(&hci_le_read_maximum_data_length); 1474dcd678baSMatthias Ringwald break; 1475dcd678baSMatthias Ringwald case HCI_INIT_LE_WRITE_SUGGESTED_DATA_LENGTH: 1476dcd678baSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_WRITE_SUGGESTED_DATA_LENGTH; 1477b435e062SMatthias Ringwald hci_send_cmd(&hci_le_write_suggested_default_data_length, hci_stack->le_supported_max_tx_octets, hci_stack->le_supported_max_tx_time); 1478dcd678baSMatthias Ringwald break; 1479b435e062SMatthias Ringwald #endif 1480b435e062SMatthias Ringwald 1481b95a5a35SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 14823b6d4121SMatthias Ringwald case HCI_INIT_READ_WHITE_LIST_SIZE: 14833b6d4121SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_WHITE_LIST_SIZE; 14843b6d4121SMatthias Ringwald hci_send_cmd(&hci_le_read_white_list_size); 14853b6d4121SMatthias Ringwald break; 148674b323a9SMatthias Ringwald case HCI_INIT_LE_SET_SCAN_PARAMETERS: 1487b95a5a35SMatthias Ringwald // LE Scan Parameters: active scanning, 300 ms interval, 30 ms window, own address type, accept all advs 148874b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_SET_SCAN_PARAMETERS; 1489b95a5a35SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_parameters, 1, 0x1e0, 0x30, hci_stack->le_own_addr_type, 0); 149074b323a9SMatthias Ringwald break; 149174b323a9SMatthias Ringwald #endif 149274b323a9SMatthias Ringwald default: 149374b323a9SMatthias Ringwald return; 149474b323a9SMatthias Ringwald } 149555975f88SMatthias Ringwald } 149655975f88SMatthias Ringwald 1497a650ba4dSMatthias Ringwald static void hci_init_done(void){ 1498a650ba4dSMatthias Ringwald // done. tell the app 1499a650ba4dSMatthias Ringwald log_info("hci_init_done -> HCI_STATE_WORKING"); 1500a650ba4dSMatthias Ringwald hci_stack->state = HCI_STATE_WORKING; 1501a650ba4dSMatthias Ringwald hci_emit_state(); 1502a650ba4dSMatthias Ringwald hci_run(); 1503a650ba4dSMatthias Ringwald } 1504a650ba4dSMatthias Ringwald 15056155b3d3S[email protected] static void hci_initializing_event_handler(uint8_t * packet, uint16_t size){ 1506384b59deSMatthias Ringwald 1507384b59deSMatthias Ringwald UNUSED(size); // ok: less than 6 bytes are read from our buffer 15089ec2630cSMatthias Ringwald 1509a2481739S[email protected] uint8_t command_completed = 0; 1510c4633093SMatthias Ringwald 15110e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE){ 1512f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 15136155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 1514a2481739S[email protected] command_completed = 1; 1515148ca237SMatthias Ringwald log_debug("Command complete for expected opcode %04x at substate %u", opcode, hci_stack->substate); 15166155b3d3S[email protected] } else { 1517d58dd308SMatthias Ringwald log_info("Command complete for different opcode %04x, expected %04x, at substate %u", opcode, hci_stack->last_cmd_opcode, hci_stack->substate); 15186155b3d3S[email protected] } 15196155b3d3S[email protected] } 15200f97eae7SMatthias Ringwald 15210e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_STATUS){ 15226155b3d3S[email protected] uint8_t status = packet[2]; 1523f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,4); 15246155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 15256155b3d3S[email protected] if (status){ 1526a2481739S[email protected] command_completed = 1; 1527148ca237SMatthias Ringwald log_debug("Command status error 0x%02x for expected opcode %04x at substate %u", status, opcode, hci_stack->substate); 15286155b3d3S[email protected] } else { 15296155b3d3S[email protected] log_info("Command status OK for expected opcode %04x, waiting for command complete", opcode); 15306155b3d3S[email protected] } 15316155b3d3S[email protected] } else { 1532148ca237SMatthias Ringwald log_debug("Command status for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode); 15336155b3d3S[email protected] } 15346155b3d3S[email protected] } 15350f97eae7SMatthias Ringwald 153606b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 153706b9e820SMatthias Ringwald 1538e47e68c7SMatthias Ringwald // Vendor == CSR 15390e2df43fSMatthias Ringwald if (hci_stack->substate == HCI_INIT_W4_CUSTOM_INIT && hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC){ 1540e47e68c7SMatthias Ringwald // TODO: track actual command 1541e47e68c7SMatthias Ringwald command_completed = 1; 1542e47e68c7SMatthias Ringwald } 1543a2481739S[email protected] 15444e9daa6fSMatthias Ringwald // Vendor == Toshiba 15450e2df43fSMatthias Ringwald if (hci_stack->substate == HCI_INIT_W4_SEND_BAUD_CHANGE && hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC){ 15464e9daa6fSMatthias Ringwald // TODO: track actual command 15474e9daa6fSMatthias Ringwald command_completed = 1; 1548004902f1SMatthias Ringwald // Fix: no HCI Command Complete received, so num_cmd_packets not reset 1549004902f1SMatthias Ringwald hci_stack->num_cmd_packets = 1; 15504e9daa6fSMatthias Ringwald } 15514e9daa6fSMatthias Ringwald 15520f97eae7SMatthias Ringwald // Late response (> 100 ms) for HCI Reset e.g. on Toshiba TC35661: 15530f97eae7SMatthias Ringwald // Command complete for HCI Reset arrives after we've resent the HCI Reset command 15540f97eae7SMatthias Ringwald // 15550f97eae7SMatthias Ringwald // HCI Reset 15560f97eae7SMatthias Ringwald // Timeout 100 ms 15570f97eae7SMatthias Ringwald // HCI Reset 15580f97eae7SMatthias Ringwald // Command Complete Reset 15590f97eae7SMatthias Ringwald // HCI Read Local Version Information 15600f97eae7SMatthias Ringwald // Command Complete Reset - but we expected Command Complete Read Local Version Information 15610f97eae7SMatthias Ringwald // hang... 15620f97eae7SMatthias Ringwald // 15630f97eae7SMatthias Ringwald // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend 15640f97eae7SMatthias Ringwald if (!command_completed 15650e2df43fSMatthias Ringwald && hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE 15660f97eae7SMatthias Ringwald && hci_stack->substate == HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION){ 15670f97eae7SMatthias Ringwald 1568f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 15690f97eae7SMatthias Ringwald if (opcode == hci_reset.opcode){ 15700f97eae7SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION; 15710f97eae7SMatthias Ringwald return; 15720f97eae7SMatthias Ringwald } 15730f97eae7SMatthias Ringwald } 15740f97eae7SMatthias Ringwald 15759f007422SMatthias Ringwald // CSR & H5 15769f007422SMatthias Ringwald // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend 15779f007422SMatthias Ringwald if (!command_completed 15789f007422SMatthias Ringwald && hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE 15799f007422SMatthias Ringwald && hci_stack->substate == HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS){ 15809f007422SMatthias Ringwald 15819f007422SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 15829f007422SMatthias Ringwald if (opcode == hci_reset.opcode){ 15839f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS; 15849f007422SMatthias Ringwald return; 15859f007422SMatthias Ringwald } 15869f007422SMatthias Ringwald } 15879f007422SMatthias Ringwald 15889f007422SMatthias 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 15899f007422SMatthias Ringwald // fix: Correct substate and behave as command below 15909f007422SMatthias Ringwald if (command_completed){ 15919f007422SMatthias Ringwald switch (hci_stack->substate){ 15929f007422SMatthias Ringwald case HCI_INIT_SEND_RESET: 15939f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 15949f007422SMatthias Ringwald break; 15959f007422SMatthias Ringwald case HCI_INIT_SEND_RESET_CSR_WARM_BOOT: 15969f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT; 15979f007422SMatthias Ringwald break; 15989f007422SMatthias Ringwald default: 15999f007422SMatthias Ringwald break; 16009f007422SMatthias Ringwald } 16019f007422SMatthias Ringwald } 16020f97eae7SMatthias Ringwald 160306b9e820SMatthias Ringwald #endif 16040f97eae7SMatthias Ringwald 1605a2481739S[email protected] if (!command_completed) return; 1606a2481739S[email protected] 160706b9e820SMatthias Ringwald int need_baud_change = 0; 160806b9e820SMatthias Ringwald int need_addr_change = 0; 160906b9e820SMatthias Ringwald 161006b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 161106b9e820SMatthias Ringwald need_baud_change = hci_stack->config 16123fb36a29SMatthias Ringwald && hci_stack->chipset 16133fb36a29SMatthias Ringwald && hci_stack->chipset->set_baudrate_command 1614db8bc6ffSMatthias Ringwald && hci_stack->hci_transport->set_baudrate 16159796ebeaSMatthias Ringwald && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 1616db8bc6ffSMatthias Ringwald 161706b9e820SMatthias Ringwald need_addr_change = hci_stack->custom_bd_addr_set 16183fb36a29SMatthias Ringwald && hci_stack->chipset 16193fb36a29SMatthias Ringwald && hci_stack->chipset->set_bd_addr_command; 162006b9e820SMatthias Ringwald #endif 1621a80162e9SMatthias Ringwald 16225c363727SMatthias Ringwald switch(hci_stack->substate){ 162306b9e820SMatthias Ringwald 162406b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 16259f007422SMatthias Ringwald case HCI_INIT_SEND_RESET: 1626d58dd308SMatthias Ringwald // on CSR with BCSP/H5, resend triggers resend of HCI Reset and leads to substate == HCI_INIT_SEND_RESET 16279f007422SMatthias Ringwald // fix: just correct substate and behave as command below 16289f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 16299f007422SMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 16309f007422SMatthias Ringwald break; 163174b323a9SMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 1632528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 163376fcb19bSMatthias Ringwald break; 1634e90bae01SMatthias Ringwald case HCI_INIT_W4_SEND_READ_LOCAL_NAME: 1635e90bae01SMatthias Ringwald log_info("Received local name, need baud change %d", need_baud_change); 16362f48d920SMatthias Ringwald if (need_baud_change){ 16372f48d920SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE; 16382f48d920SMatthias Ringwald return; 16392f48d920SMatthias Ringwald } 164053860077SMatthias Ringwald // skip baud change 164174b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 164274b323a9SMatthias Ringwald return; 1643a80162e9SMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE: 16444696bddbSMatthias Ringwald // for STLC2500D, baud rate change already happened. 1645fab26ab3SMatthias Ringwald // for others, baud rate gets changed now 164661f37892SMatthias Ringwald if ((hci_stack->manufacturer != BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS) && need_baud_change){ 164796b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1648cd724cb7SMatthias Ringwald log_info("Local baud rate change to %"PRIu32"(w4_send_baud_change)", baud_rate); 1649fab26ab3SMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 16504696bddbSMatthias Ringwald } 165174b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 165274b323a9SMatthias Ringwald return; 1653a80162e9SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT: 1654528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 1655a80162e9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 1656a80162e9SMatthias Ringwald return; 165774b323a9SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT: 165874b323a9SMatthias Ringwald // repeat custom init 165974b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 166074b323a9SMatthias Ringwald return; 166106b9e820SMatthias Ringwald #else 166206b9e820SMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 166306b9e820SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS; 166406b9e820SMatthias Ringwald return ; 166506b9e820SMatthias Ringwald #endif 166606b9e820SMatthias Ringwald 1667a828a756SMatthias Ringwald case HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS: 1668efd3b327SMatthias Ringwald if (need_baud_change && 1669efd3b327SMatthias Ringwald ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION) || 1670efd3b327SMatthias Ringwald (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_EM_MICROELECTRONIC_MARIN_SA))) { 1671eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE_BCM; 1672eb3a5314SMatthias Ringwald return; 1673eb3a5314SMatthias Ringwald } 167453860077SMatthias Ringwald if (need_addr_change){ 167553860077SMatthias Ringwald hci_stack->substate = HCI_INIT_SET_BD_ADDR; 167653860077SMatthias Ringwald return; 167753860077SMatthias Ringwald } 167853860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 167953860077SMatthias Ringwald return; 168006b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 16817224be7eSMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE_BCM: 16827224be7eSMatthias Ringwald if (need_baud_change){ 168396b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1684cd724cb7SMatthias Ringwald log_info("Local baud rate change to %"PRIu32"(w4_send_baud_change_bcm))", baud_rate); 1685fab26ab3SMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 16867224be7eSMatthias Ringwald } 1687eb3a5314SMatthias Ringwald if (need_addr_change){ 1688eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_SET_BD_ADDR; 1689eb3a5314SMatthias Ringwald return; 1690eb3a5314SMatthias Ringwald } 1691eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 1692eb3a5314SMatthias Ringwald return; 169353860077SMatthias Ringwald case HCI_INIT_W4_SET_BD_ADDR: 16946ca9a99aSMatthias Ringwald // for STLC2500D + ATWILC3000, bd addr change only gets active after sending reset command 16956ca9a99aSMatthias Ringwald if ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS) 16966ca9a99aSMatthias Ringwald || (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ATMEL_CORPORATION)){ 169753860077SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_ST_WARM_BOOT; 169853860077SMatthias Ringwald return; 169953860077SMatthias Ringwald } 170053860077SMatthias Ringwald // skipping st warm boot 170153860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 170253860077SMatthias Ringwald return; 170353860077SMatthias Ringwald case HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT: 170453860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 170553860077SMatthias Ringwald return; 170606b9e820SMatthias Ringwald #endif 170753860077SMatthias Ringwald case HCI_INIT_W4_READ_BD_ADDR: 170853860077SMatthias Ringwald // only read buffer size if supported 170953860077SMatthias Ringwald if (hci_stack->local_supported_commands[0] & 0x01) { 171053860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BUFFER_SIZE; 171153860077SMatthias Ringwald return; 171253860077SMatthias Ringwald } 171353860077SMatthias Ringwald // skipping read buffer size 171453860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES; 1715a828a756SMatthias Ringwald return; 171674b323a9SMatthias Ringwald case HCI_INIT_W4_SET_EVENT_MASK: 17176155b3d3S[email protected] // skip Classic init commands for LE only chipsets 17186155b3d3S[email protected] if (!hci_classic_supported()){ 17192c68f164SMatthias Ringwald #ifdef ENABLE_BLE 17206155b3d3S[email protected] if (hci_le_supported()){ 172174b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_LE_READ_BUFFER_SIZE; // skip all classic command 172274b323a9SMatthias Ringwald return; 17232c68f164SMatthias Ringwald } 17242c68f164SMatthias Ringwald #endif 17256155b3d3S[email protected] log_error("Neither BR/EDR nor LE supported"); 1726a650ba4dSMatthias Ringwald hci_init_done(); 172774b323a9SMatthias Ringwald return; 17286155b3d3S[email protected] } 172915a95bd5SMatthias Ringwald if (!gap_ssp_supported()){ 17305c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_WRITE_PAGE_TIMEOUT; 173174b323a9SMatthias Ringwald return; 17326155b3d3S[email protected] } 17336155b3d3S[email protected] break; 1734903ea03aSMatthias Ringwald #ifdef ENABLE_BLE 1735a828a756SMatthias Ringwald case HCI_INIT_W4_LE_READ_BUFFER_SIZE: 1736a828a756SMatthias Ringwald // skip write le host if not supported (e.g. on LE only EM9301) 1737a828a756SMatthias Ringwald if (hci_stack->local_supported_commands[0] & 0x02) break; 1738daabb8b8SMatthias Ringwald hci_stack->substate = HCI_INIT_LE_SET_EVENT_MASK; 1739daabb8b8SMatthias Ringwald return; 1740daabb8b8SMatthias Ringwald 1741b435e062SMatthias Ringwald #ifdef ENABLE_LE_DATA_LENGTH_EXTENSION 1742b435e062SMatthias Ringwald case HCI_INIT_W4_WRITE_LE_HOST_SUPPORTED: 17436fab74dbSMatthias Ringwald log_info("Supported commands %x", hci_stack->local_supported_commands[0] & 0x30); 1744b435e062SMatthias Ringwald if ((hci_stack->local_supported_commands[0] & 0x30) == 0x30){ 174580d7d618SMatthias Ringwald hci_stack->substate = HCI_INIT_LE_SET_EVENT_MASK; 1746b435e062SMatthias Ringwald return; 1747b435e062SMatthias Ringwald } 1748b435e062SMatthias Ringwald // explicit fall through to reduce repetitions 1749b435e062SMatthias Ringwald 1750b435e062SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 1751b435e062SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_WHITE_LIST_SIZE; 1752903ea03aSMatthias Ringwald #else 1753903ea03aSMatthias Ringwald hci_init_done(); 1754903ea03aSMatthias Ringwald #endif 1755a828a756SMatthias Ringwald return; 175606337f4fSMatthias Ringwald #endif /* ENABLE_LE_DATA_LENGTH_EXTENSION */ 175706337f4fSMatthias Ringwald 175806337f4fSMatthias Ringwald #endif /* ENABLE_BLE */ 1759b435e062SMatthias Ringwald 176071c4de7aSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 1761729ed62eSMatthias Ringwald case HCI_INIT_W4_WRITE_SCAN_ENABLE: 17623905afbfSMatthias Ringwald // skip write synchronous flow control if not supported 17633905afbfSMatthias Ringwald if (hci_stack->local_supported_commands[0] & 0x04) break; 17643905afbfSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE; 17653905afbfSMatthias Ringwald // explicit fall through to reduce repetitions 17663905afbfSMatthias Ringwald 1767729ed62eSMatthias Ringwald case HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE: 17683905afbfSMatthias Ringwald // skip write default erroneous data reporting if not supported 17693905afbfSMatthias Ringwald if (hci_stack->local_supported_commands[0] & 0x08) break; 17703905afbfSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING; 17713905afbfSMatthias Ringwald // explicit fall through to reduce repetitions 17723905afbfSMatthias Ringwald 1773f064e0bbSMatthias Ringwald case HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING: 1774a42798c3SMatthias Ringwald // skip bcm set sco pcm config on non-Broadcom chipsets 177561f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION) break; 1776a42798c3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT; 1777a42798c3SMatthias Ringwald // explicit fall through to reduce repetitions 1778a42798c3SMatthias Ringwald 1779a42798c3SMatthias Ringwald case HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT: 1780729ed62eSMatthias Ringwald if (!hci_le_supported()){ 1781729ed62eSMatthias Ringwald // SKIP LE init for Classic only configuration 1782a650ba4dSMatthias Ringwald hci_init_done(); 1783729ed62eSMatthias Ringwald return; 1784729ed62eSMatthias Ringwald } 1785729ed62eSMatthias Ringwald break; 1786f3b012f9SMatthias Ringwald 1787f3b012f9SMatthias Ringwald #else /* !ENABLE_SCO_OVER_HCI */ 1788f3b012f9SMatthias Ringwald 178974b323a9SMatthias Ringwald case HCI_INIT_W4_WRITE_SCAN_ENABLE: 1790f3b012f9SMatthias Ringwald #ifdef ENABLE_BLE 1791f3b012f9SMatthias Ringwald if (hci_le_supported()){ 1792f3b012f9SMatthias Ringwald hci_stack->substate = HCI_INIT_LE_READ_BUFFER_SIZE; 179374b323a9SMatthias Ringwald return; 17946155b3d3S[email protected] } 1795729ed62eSMatthias Ringwald #endif 1796f3b012f9SMatthias Ringwald // SKIP LE init for Classic only configuration 1797f3b012f9SMatthias Ringwald hci_init_done(); 1798f3b012f9SMatthias Ringwald return; 1799f3b012f9SMatthias Ringwald #endif /* ENABLE_SCO_OVER_HCI */ 1800f3b012f9SMatthias Ringwald 1801a4e96e78SMatthias Ringwald // avoid compile error due to duplicate cases: HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT == HCI_INIT_DONE-1 1802a4e96e78SMatthias Ringwald #if defined(ENABLE_BLE) || defined(ENABLE_LE_DATA_LENGTH_EXTENSION) || defined(ENABLE_LE_CENTRAL) 1803a650ba4dSMatthias Ringwald // Response to command before init done state -> init done 1804a650ba4dSMatthias Ringwald case (HCI_INIT_DONE-1): 1805a650ba4dSMatthias Ringwald hci_init_done(); 1806a650ba4dSMatthias Ringwald return; 1807a4e96e78SMatthias Ringwald #endif 1808a650ba4dSMatthias Ringwald 18096155b3d3S[email protected] default: 181074b323a9SMatthias Ringwald break; 18116155b3d3S[email protected] } 181255975f88SMatthias Ringwald hci_initializing_next_state(); 18136155b3d3S[email protected] } 18146155b3d3S[email protected] 18150bbba85bSMatthias Ringwald static void hci_handle_connection_failed(hci_connection_t * conn, uint8_t status){ 1816229331c6SMatthias Ringwald log_info("Outgoing connection to %s failed", bd_addr_to_str(conn->address)); 18170bbba85bSMatthias Ringwald bd_addr_t bd_address; 18180bbba85bSMatthias Ringwald memcpy(&bd_address, conn->address, 6); 18190bbba85bSMatthias Ringwald 18206bc9fa5eSMatthias Ringwald #ifdef ENABLE_CLASSIC 18216bc9fa5eSMatthias Ringwald // cache needed data 18226bc9fa5eSMatthias Ringwald int notify_dedicated_bonding_failed = conn->bonding_flags & BONDING_DEDICATED; 18236bc9fa5eSMatthias Ringwald #endif 18246bc9fa5eSMatthias Ringwald 18250bbba85bSMatthias Ringwald // connection failed, remove entry 18260bbba85bSMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 18270bbba85bSMatthias Ringwald btstack_memory_hci_connection_free( conn ); 18280bbba85bSMatthias Ringwald 18296bc9fa5eSMatthias Ringwald #ifdef ENABLE_CLASSIC 18300bbba85bSMatthias Ringwald // notify client if dedicated bonding 18310bbba85bSMatthias Ringwald if (notify_dedicated_bonding_failed){ 18320bbba85bSMatthias Ringwald log_info("hci notify_dedicated_bonding_failed"); 18330bbba85bSMatthias Ringwald hci_emit_dedicated_bonding_result(bd_address, status); 18340bbba85bSMatthias Ringwald } 18350bbba85bSMatthias Ringwald 18360bbba85bSMatthias Ringwald // if authentication error, also delete link key 18370bbba85bSMatthias Ringwald if (status == ERROR_CODE_AUTHENTICATION_FAILURE) { 18380bbba85bSMatthias Ringwald gap_drop_link_key_for_bd_addr(bd_address); 18390bbba85bSMatthias Ringwald } 18406bc9fa5eSMatthias Ringwald #endif 18410bbba85bSMatthias Ringwald } 18420bbba85bSMatthias Ringwald 184316833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){ 1844e76a89eeS[email protected] 1845e76a89eeS[email protected] uint16_t event_length = packet[1]; 1846e76a89eeS[email protected] 1847e76a89eeS[email protected] // assert packet is complete 1848e76a89eeS[email protected] if (size != event_length + 2){ 18493dce6128SMatthias Ringwald log_error("event_handler called with packet of wrong size %d, expected %u => dropping packet", size, event_length + 2); 1850e76a89eeS[email protected] return; 1851e76a89eeS[email protected] } 1852e76a89eeS[email protected] 18531281a47eSmatthias.ringwald bd_addr_t addr; 185496a45072S[email protected] bd_addr_type_t addr_type; 1855fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 18561f7b95a1Smatthias.ringwald hci_connection_t * conn; 185756cf178bSmatthias.ringwald int i; 1858c57fa566SMatthias Ringwald int create_connection_cmd; 1859c57fa566SMatthias Ringwald 18600014e02cSMatthias Ringwald #ifdef ENABLE_CLASSIC 18610014e02cSMatthias Ringwald uint8_t link_type; 18620014e02cSMatthias Ringwald #endif 186335454696SMatthias Ringwald 18640e2df43fSMatthias Ringwald // log_info("HCI:EVENT:%02x", hci_event_packet_get_type(packet)); 18655909f7f2Smatthias.ringwald 18660e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 186722909952Smatthias.ringwald 18686772a24cSmatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 18696bef4003SMatthias Ringwald // get num cmd packets - limit to 1 to reduce complexity 18706bef4003SMatthias Ringwald hci_stack->num_cmd_packets = packet[2] ? 1 : 0; 18717ec5eeaaSmatthias.ringwald 18729e263bd7SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){ 187306b9e820SMatthias Ringwald if (packet[5]) break; 18749e263bd7SMatthias Ringwald // terminate, name 248 chars 18759e263bd7SMatthias Ringwald packet[6+248] = 0; 18769e263bd7SMatthias Ringwald log_info("local name: %s", &packet[6]); 18779e263bd7SMatthias Ringwald } 1878073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_buffer_size)){ 18791d279b20Smatthias.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" 1880429122ccSMatthias Ringwald if (hci_stack->state == HCI_STATE_INITIALIZING){ 1881429122ccSMatthias Ringwald uint16_t acl_len = little_endian_read_16(packet, 6); 1882429122ccSMatthias Ringwald uint16_t sco_len = packet[8]; 1883429122ccSMatthias Ringwald 1884429122ccSMatthias Ringwald // determine usable ACL/SCO payload size 1885429122ccSMatthias Ringwald hci_stack->acl_data_packet_length = btstack_min(acl_len, HCI_ACL_PAYLOAD_SIZE); 1886429122ccSMatthias Ringwald hci_stack->sco_data_packet_length = btstack_min(sco_len, HCI_ACL_PAYLOAD_SIZE); 1887429122ccSMatthias Ringwald 1888f8fbdce0SMatthias Ringwald hci_stack->acl_packets_total_num = little_endian_read_16(packet, 9); 1889f8fbdce0SMatthias Ringwald hci_stack->sco_packets_total_num = little_endian_read_16(packet, 11); 1890a8b12447S[email protected] 1891429122ccSMatthias Ringwald log_info("hci_read_buffer_size: ACL size module %u -> used %u, count %u / SCO size %u, count %u", 1892429122ccSMatthias Ringwald acl_len, hci_stack->acl_data_packet_length, hci_stack->acl_packets_total_num, 18931a06f663S[email protected] hci_stack->sco_data_packet_length, hci_stack->sco_packets_total_num); 1894e2edc0c3Smatthias.ringwald } 189556cf178bSmatthias.ringwald } 1896a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 1897073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_le_read_buffer_size)){ 1898f8fbdce0SMatthias Ringwald hci_stack->le_data_packets_length = little_endian_read_16(packet, 6); 1899ee303eddS[email protected] hci_stack->le_acl_packets_total_num = packet[8]; 19006c26b087S[email protected] // determine usable ACL payload size 19016c26b087S[email protected] if (HCI_ACL_PAYLOAD_SIZE < hci_stack->le_data_packets_length){ 19026c26b087S[email protected] hci_stack->le_data_packets_length = HCI_ACL_PAYLOAD_SIZE; 19036c26b087S[email protected] } 19049da54300S[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); 190565a46ef3S[email protected] } 1906b435e062SMatthias Ringwald #endif 1907b435e062SMatthias Ringwald #ifdef ENABLE_LE_DATA_LENGTH_EXTENSION 1908dcd678baSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_le_read_maximum_data_length)){ 1909dcd678baSMatthias Ringwald hci_stack->le_supported_max_tx_octets = little_endian_read_16(packet, 6); 1910dcd678baSMatthias Ringwald hci_stack->le_supported_max_tx_time = little_endian_read_16(packet, 8); 1911dcd678baSMatthias Ringwald log_info("hci_le_read_maximum_data_length: tx octets %u, tx time %u us", hci_stack->le_supported_max_tx_octets, hci_stack->le_supported_max_tx_time); 1912dcd678baSMatthias Ringwald } 1913b435e062SMatthias Ringwald #endif 1914d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 1915073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_le_read_white_list_size)){ 1916e691bb38SMatthias Ringwald hci_stack->le_whitelist_capacity = packet[6]; 191715d0a15bSMatthias Ringwald log_info("hci_le_read_white_list_size: size %u", hci_stack->le_whitelist_capacity); 19183b6d4121SMatthias Ringwald } 191965a46ef3S[email protected] #endif 1920073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_bd_addr)) { 1921724d70a2SMatthias Ringwald reverse_bd_addr(&packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1], 1922724d70a2SMatthias Ringwald hci_stack->local_bd_addr); 19239da54300S[email protected] log_info("Local Address, Status: 0x%02x: Addr: %s", 19243a9fb326S[email protected] packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr)); 192533373e40SMatthias Ringwald #ifdef ENABLE_CLASSIC 19261624665aSMatthias Ringwald if (hci_stack->link_key_db){ 19271624665aSMatthias Ringwald hci_stack->link_key_db->set_local_bd_addr(hci_stack->local_bd_addr); 19281624665aSMatthias Ringwald } 192933373e40SMatthias Ringwald #endif 1930188981d2Smatthias.ringwald } 193135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1932073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_scan_enable)){ 19333a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 1934381fbed8Smatthias.ringwald } 1935f5875de5SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_inquiry_cancel)){ 1936f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_W4_CANCELLED){ 1937f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_IDLE; 1938f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 1939f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 1940f5875de5SMatthias Ringwald } 1941f5875de5SMatthias Ringwald } 194235454696SMatthias Ringwald #endif 194335454696SMatthias Ringwald 194465389bfcS[email protected] // Note: HCI init checks 1945073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_supported_features)){ 19463a9fb326S[email protected] memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8); 194765389bfcS[email protected] 194835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1949a5a23fc2S[email protected] // determine usable ACL packet types based on host buffer size and supported features 1950a5a23fc2S[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]); 19518b96126aSMatthias Ringwald log_info("Packet types %04x, eSCO %u", hci_stack->packet_types, hci_extended_sco_link_supported()); 195235454696SMatthias Ringwald #endif 1953f5d8d141S[email protected] // Classic/LE 1954f5d8d141S[email protected] log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported()); 1955559e517eS[email protected] } 1956073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_version_information)){ 1957f8fbdce0SMatthias Ringwald // hci_stack->hci_version = little_endian_read_16(packet, 4); 1958f8fbdce0SMatthias Ringwald // hci_stack->hci_revision = little_endian_read_16(packet, 6); 1959f8fbdce0SMatthias Ringwald // hci_stack->lmp_version = little_endian_read_16(packet, 8); 1960f8fbdce0SMatthias Ringwald hci_stack->manufacturer = little_endian_read_16(packet, 10); 1961f8fbdce0SMatthias Ringwald // hci_stack->lmp_subversion = little_endian_read_16(packet, 12); 19624696bddbSMatthias Ringwald log_info("Manufacturer: 0x%04x", hci_stack->manufacturer); 19634696bddbSMatthias Ringwald } 1964073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_supported_commands)){ 1965a828a756SMatthias Ringwald hci_stack->local_supported_commands[0] = 1966cd542ed6SMatthias Ringwald (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+14] & 0x80) >> 7 | // bit 0 = Octet 14, bit 7 / Read Buffer Size 1967cd542ed6SMatthias Ringwald (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+24] & 0x40) >> 5 | // bit 1 = Octet 24, bit 6 / Write Le Host Supported 1968cd542ed6SMatthias Ringwald (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+10] & 0x10) >> 2 | // bit 2 = Octet 10, bit 4 / Write Synchronous Flow Control Enable 1969cd542ed6SMatthias Ringwald (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+18] & 0x08) | // bit 3 = Octet 18, bit 3 / Write Default Erroneous Data Reporting 1970cd542ed6SMatthias Ringwald (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+34] & 0x01) << 4 | // bit 4 = Octet 34, bit 0 / LE Write Suggested Default Data Length 1971cd542ed6SMatthias Ringwald (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+35] & 0x08) << 2 | // bit 5 = Octet 35, bit 3 / LE Read Maximum Data Length 1972cd542ed6SMatthias Ringwald (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+35] & 0x20) << 1; // bit 6 = Octet 35, bit 5 / LE Set Default PHY 19733905afbfSMatthias Ringwald log_info("Local supported commands summary 0x%02x", hci_stack->local_supported_commands[0]); 1974a828a756SMatthias Ringwald } 1975e8c8828eSMatthias Ringwald #ifdef ENABLE_CLASSIC 1976073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_synchronous_flow_control_enable)){ 19775b9b590fSMatthias Ringwald if (packet[5] == 0){ 19785b9b590fSMatthias Ringwald hci_stack->synchronous_flow_control_enabled = 1; 19795b9b590fSMatthias Ringwald } 19805b9b590fSMatthias Ringwald } 1981e8c8828eSMatthias Ringwald #endif 198256cf178bSmatthias.ringwald break; 198356cf178bSmatthias.ringwald 19847ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 19856bef4003SMatthias Ringwald // get num cmd packets - limit to 1 to reduce complexity 19866bef4003SMatthias Ringwald hci_stack->num_cmd_packets = packet[3] ? 1 : 0; 1987229331c6SMatthias Ringwald 1988229331c6SMatthias Ringwald // check command status to detected failed outgoing connections 1989c57fa566SMatthias Ringwald create_connection_cmd = 0; 1990c57fa566SMatthias Ringwald #ifdef ENABLE_CLASSIC 1991c57fa566SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_STATUS(packet, hci_create_connection)){ 1992c57fa566SMatthias Ringwald create_connection_cmd = 1; 1993c57fa566SMatthias Ringwald } 1994c57fa566SMatthias Ringwald #endif 1995c57fa566SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 1996c57fa566SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_STATUS(packet, hci_le_create_connection)){ 1997c57fa566SMatthias Ringwald create_connection_cmd = 1; 1998c57fa566SMatthias Ringwald } 1999c57fa566SMatthias Ringwald #endif 2000c57fa566SMatthias Ringwald if (create_connection_cmd) { 2001229331c6SMatthias Ringwald uint8_t status = hci_event_command_status_get_status(packet); 2002229331c6SMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(hci_stack->outgoing_addr, hci_stack->outgoing_addr_type); 2003229331c6SMatthias Ringwald log_info("command status (create connection), status %x, connection %p, addr %s, type %x", status, conn, bd_addr_to_str(hci_stack->outgoing_addr), hci_stack->outgoing_addr_type); 2004229331c6SMatthias Ringwald 2005229331c6SMatthias Ringwald // reset outgoing address info 2006229331c6SMatthias Ringwald memset(hci_stack->outgoing_addr, 0, 6); 2007229331c6SMatthias Ringwald hci_stack->outgoing_addr_type = BD_ADDR_TYPE_UNKNOWN; 2008229331c6SMatthias Ringwald 2009229331c6SMatthias Ringwald // error => outgoing connection failed 2010229331c6SMatthias Ringwald if ((conn != NULL) && (status != 0)){ 2011229331c6SMatthias Ringwald hci_handle_connection_failed(conn, status); 2012229331c6SMatthias Ringwald } 2013229331c6SMatthias Ringwald } 20147ec5eeaaSmatthias.ringwald break; 20157ec5eeaaSmatthias.ringwald 20162e440c8aS[email protected] case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:{ 20172e440c8aS[email protected] int offset = 3; 201856cf178bSmatthias.ringwald for (i=0; i<packet[2];i++){ 201980117cebSMatthias Ringwald handle = little_endian_read_16(packet, offset) & 0x0fff; 20202e440c8aS[email protected] offset += 2; 2021f8fbdce0SMatthias Ringwald uint16_t num_packets = little_endian_read_16(packet, offset); 20222e440c8aS[email protected] offset += 2; 20232e440c8aS[email protected] 20245061f3afS[email protected] conn = hci_connection_for_handle(handle); 202556cf178bSmatthias.ringwald if (!conn){ 20269da54300S[email protected] log_error("hci_number_completed_packet lists unused con handle %u", handle); 202756cf178bSmatthias.ringwald continue; 202856cf178bSmatthias.ringwald } 202923bed257S[email protected] 2030ce41473eSMatthias Ringwald if (conn->num_packets_sent >= num_packets){ 2031ce41473eSMatthias Ringwald conn->num_packets_sent -= num_packets; 2032e35edcc1S[email protected] } else { 2033ce41473eSMatthias Ringwald log_error("hci_number_completed_packets, more packet slots freed then sent."); 2034ce41473eSMatthias Ringwald conn->num_packets_sent = 0; 2035e35edcc1S[email protected] } 2036ce41473eSMatthias Ringwald // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u", num_packets, handle, conn->num_packets_sent); 2037760b20efSMatthias Ringwald 2038760b20efSMatthias Ringwald #ifdef ENABLE_CLASSIC 2039760b20efSMatthias Ringwald // For SCO, we do the can_send_now_check here 2040760b20efSMatthias Ringwald hci_notify_if_sco_can_send_now(); 2041760b20efSMatthias Ringwald #endif 204256cf178bSmatthias.ringwald } 20436772a24cSmatthias.ringwald break; 20442e440c8aS[email protected] } 204535454696SMatthias Ringwald 204635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2047f5875de5SMatthias Ringwald case HCI_EVENT_INQUIRY_COMPLETE: 2048f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_ACTIVE){ 2049f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_IDLE; 2050f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 2051f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 2052f5875de5SMatthias Ringwald } 2053f5875de5SMatthias Ringwald break; 2054b7f1ee76SMatthias Ringwald case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 2055b7f1ee76SMatthias Ringwald if (hci_stack->remote_name_state == GAP_REMOTE_NAME_STATE_W4_COMPLETE){ 2056b7f1ee76SMatthias Ringwald hci_stack->remote_name_state = GAP_REMOTE_NAME_STATE_IDLE; 2057b7f1ee76SMatthias Ringwald } 2058b7f1ee76SMatthias Ringwald break; 20591f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 2060724d70a2SMatthias Ringwald reverse_bd_addr(&packet[2], addr); 206137eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 20622d00edd4Smatthias.ringwald link_type = packet[11]; 20639da54300S[email protected] log_info("Connection_incoming: %s, type %u", bd_addr_to_str(addr), link_type); 2064e35edcc1S[email protected] addr_type = link_type == 1 ? BD_ADDR_TYPE_CLASSIC : BD_ADDR_TYPE_SCO; 20652e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 20661f7b95a1Smatthias.ringwald if (!conn) { 20675293c072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 20681f7b95a1Smatthias.ringwald } 2069ce4c8fabSmatthias.ringwald if (!conn) { 2070ce4c8fabSmatthias.ringwald // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 20713a9fb326S[email protected] hci_stack->decline_reason = 0x0d; 2072058e3d6bSMatthias Ringwald bd_addr_copy(hci_stack->decline_addr, addr); 2073ce4c8fabSmatthias.ringwald break; 2074ce4c8fabSmatthias.ringwald } 20755cf766e8SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 207632ab9390Smatthias.ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 2077f3a16b9aSMatthias Ringwald // store info about eSCO 2078f3a16b9aSMatthias Ringwald if (link_type == 0x02){ 2079f3a16b9aSMatthias Ringwald conn->remote_supported_feature_eSCO = 1; 2080f3a16b9aSMatthias Ringwald } 208132ab9390Smatthias.ringwald hci_run(); 20821f7b95a1Smatthias.ringwald break; 20831f7b95a1Smatthias.ringwald 20846772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 2085fe1ed1b8Smatthias.ringwald // Connection management 2086724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], addr); 20879da54300S[email protected] log_info("Connection_complete (status=%u) %s", packet[2], bd_addr_to_str(addr)); 208896a45072S[email protected] addr_type = BD_ADDR_TYPE_CLASSIC; 20892e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 2090fe1ed1b8Smatthias.ringwald if (conn) { 2091b448a0e7Smatthias.ringwald if (!packet[2]){ 2092c8e4258aSmatthias.ringwald conn->state = OPEN; 2093f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 3); 2094ad83dc6aS[email protected] conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES; 2095ee091cf1Smatthias.ringwald 2096c785ef68Smatthias.ringwald // restart timer 2097528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 2098528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&conn->timeout); 2099c785ef68Smatthias.ringwald 21009da54300S[email protected] log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 210143bfb1bdSmatthias.ringwald 210243bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 2103b448a0e7Smatthias.ringwald } else { 21040bbba85bSMatthias Ringwald // connection failed 21050bbba85bSMatthias Ringwald hci_handle_connection_failed(conn, packet[2]); 2106fe1ed1b8Smatthias.ringwald } 2107fe1ed1b8Smatthias.ringwald } 21086772a24cSmatthias.ringwald break; 2109fe1ed1b8Smatthias.ringwald 211044d0e3d5S[email protected] case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE: 2111724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], addr); 211244d0e3d5S[email protected] log_info("Synchronous Connection Complete (status=%u) %s", packet[2], bd_addr_to_str(addr)); 21131a06f663S[email protected] if (packet[2]){ 211444d0e3d5S[email protected] // connection failed 211544d0e3d5S[email protected] break; 211644d0e3d5S[email protected] } 21172e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 2118e35edcc1S[email protected] if (!conn) { 2119e35edcc1S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 2120e35edcc1S[email protected] } 2121e35edcc1S[email protected] if (!conn) { 2122e35edcc1S[email protected] break; 2123e35edcc1S[email protected] } 21241a06f663S[email protected] conn->state = OPEN; 2125f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 3); 2126ee752bb8SMatthias Ringwald 2127ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 2128ee752bb8SMatthias Ringwald // update SCO 2129ee752bb8SMatthias Ringwald if (conn->address_type == BD_ADDR_TYPE_SCO && hci_stack->hci_transport && hci_stack->hci_transport->set_sco_config){ 2130ee752bb8SMatthias Ringwald hci_stack->hci_transport->set_sco_config(hci_stack->sco_voice_setting_active, hci_number_sco_connections()); 2131ee752bb8SMatthias Ringwald } 2132ee752bb8SMatthias Ringwald #endif 213344d0e3d5S[email protected] break; 213444d0e3d5S[email protected] 2135afd4e962S[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 2136f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 2137afd4e962S[email protected] conn = hci_connection_for_handle(handle); 2138afd4e962S[email protected] if (!conn) break; 2139afd4e962S[email protected] if (!packet[2]){ 2140afd4e962S[email protected] uint8_t * features = &packet[5]; 2141afd4e962S[email protected] if (features[6] & (1 << 3)){ 2142afd4e962S[email protected] conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP; 2143afd4e962S[email protected] } 214498a2fd1cSMatthias Ringwald if (features[3] & (1<<7)){ 214598a2fd1cSMatthias Ringwald conn->remote_supported_feature_eSCO = 1; 214698a2fd1cSMatthias Ringwald } 2147afd4e962S[email protected] } 2148afd4e962S[email protected] conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES; 214998a2fd1cSMatthias Ringwald log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x, eSCO %u", conn->bonding_flags, conn->remote_supported_feature_eSCO); 2150ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 2151ad83dc6aS[email protected] conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 2152ad83dc6aS[email protected] } 2153afd4e962S[email protected] break; 2154afd4e962S[email protected] 21557fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 21569da54300S[email protected] log_info("HCI_EVENT_LINK_KEY_REQUEST"); 21577fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST); 215874d716b5S[email protected] // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST 2159a98592bcSMatthias Ringwald if (hci_stack->bondable && !hci_stack->link_key_db) break; 216032ab9390Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST); 216164472d52Smatthias.ringwald hci_run(); 2162d9a327f9Smatthias.ringwald // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set 216329d53098Smatthias.ringwald return; 21647fde4af9Smatthias.ringwald 21659ab95c90S[email protected] case HCI_EVENT_LINK_KEY_NOTIFICATION: { 2166724d70a2SMatthias Ringwald reverse_bd_addr(&packet[2], addr); 21672e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 21689ab95c90S[email protected] if (!conn) break; 21699ab95c90S[email protected] conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION; 21707bdc6798S[email protected] link_key_type_t link_key_type = (link_key_type_t)packet[24]; 21719ab95c90S[email protected] // Change Connection Encryption keeps link key type 21729ab95c90S[email protected] if (link_key_type != CHANGED_COMBINATION_KEY){ 21739ab95c90S[email protected] conn->link_key_type = link_key_type; 21749ab95c90S[email protected] } 217555597469SMatthias Ringwald gap_store_link_key_for_bd_addr(addr, &packet[8], conn->link_key_type); 217629d53098Smatthias.ringwald // still forward event to allow dismiss of pairing dialog 21777fde4af9Smatthias.ringwald break; 21789ab95c90S[email protected] } 21797fde4af9Smatthias.ringwald 21807fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 21816724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE); 21824c57c146S[email protected] // non-bondable mode: pin code negative reply will be sent 21833a9fb326S[email protected] if (!hci_stack->bondable){ 2184899283eaS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST); 2185f8fb5f6eS[email protected] hci_run(); 2186f8fb5f6eS[email protected] return; 21874c57c146S[email protected] } 2188d9a327f9Smatthias.ringwald // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key 2189a98592bcSMatthias Ringwald if (!hci_stack->link_key_db) break; 2190a6ef64baSMilanka Ringwald hci_event_pin_code_request_get_bd_addr(packet, addr); 2191a98592bcSMatthias Ringwald hci_stack->link_key_db->delete_link_key(addr); 21927fde4af9Smatthias.ringwald break; 21937fde4af9Smatthias.ringwald 21941d6b20aeS[email protected] case HCI_EVENT_IO_CAPABILITY_REQUEST: 21951d6b20aeS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST); 2196dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY); 2197dbe1a790S[email protected] break; 2198dbe1a790S[email protected] 2199dbe1a790S[email protected] case HCI_EVENT_USER_CONFIRMATION_REQUEST: 22006724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 22013a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 2202dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY); 2203dbe1a790S[email protected] break; 2204dbe1a790S[email protected] 2205dbe1a790S[email protected] case HCI_EVENT_USER_PASSKEY_REQUEST: 22066724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 22073a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 2208dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY); 22091d6b20aeS[email protected] break; 22103dce6128SMatthias Ringwald case HCI_EVENT_MODE_CHANGE: 22113dce6128SMatthias Ringwald handle = hci_event_mode_change_get_handle(packet); 22123dce6128SMatthias Ringwald conn = hci_connection_for_handle(handle); 22133dce6128SMatthias Ringwald if (!conn) break; 22143dce6128SMatthias Ringwald conn->connection_mode = hci_event_mode_change_get_mode(packet); 22153dce6128SMatthias Ringwald log_info("HCI_EVENT_MODE_CHANGE, handle 0x%04x, mode %u", handle, conn->connection_mode); 22163dce6128SMatthias Ringwald break; 221735454696SMatthias Ringwald #endif 22181d6b20aeS[email protected] 2219f0944df2S[email protected] case HCI_EVENT_ENCRYPTION_CHANGE: 2220f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 2221f0944df2S[email protected] conn = hci_connection_for_handle(handle); 2222f0944df2S[email protected] if (!conn) break; 2223ad83dc6aS[email protected] if (packet[2] == 0) { 2224f0944df2S[email protected] if (packet[5]){ 2225f0944df2S[email protected] conn->authentication_flags |= CONNECTION_ENCRYPTED; 2226f0944df2S[email protected] } else { 2227536f9994S[email protected] conn->authentication_flags &= ~CONNECTION_ENCRYPTED; 2228f0944df2S[email protected] } 2229ad83dc6aS[email protected] } 223035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2231a00031e2S[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 223235454696SMatthias Ringwald #endif 2233f0944df2S[email protected] break; 2234f0944df2S[email protected] 223535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 22361eb2563eS[email protected] case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT: 2237f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 22381eb2563eS[email protected] conn = hci_connection_for_handle(handle); 22391eb2563eS[email protected] if (!conn) break; 2240ad83dc6aS[email protected] 2241ad83dc6aS[email protected] // dedicated bonding: send result and disconnect 2242ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 2243ad83dc6aS[email protected] conn->bonding_flags &= ~BONDING_DEDICATED; 2244ad83dc6aS[email protected] conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE; 22451bd5283dS[email protected] conn->bonding_status = packet[2]; 2246ad83dc6aS[email protected] break; 2247ad83dc6aS[email protected] } 2248ad83dc6aS[email protected] 2249b5cb6874S[email protected] if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){ 22501eb2563eS[email protected] // link key sufficient for requested security 22511eb2563eS[email protected] conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 2252ad83dc6aS[email protected] break; 2253ad83dc6aS[email protected] } 22541eb2563eS[email protected] // not enough 22551eb2563eS[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 22561eb2563eS[email protected] break; 225735454696SMatthias Ringwald #endif 225834d2123cS[email protected] 225986805605S[email protected] // HCI_EVENT_DISCONNECTION_COMPLETE 2260ccda6e14S[email protected] // has been split, to first notify stack before shutting connection down 2261ccda6e14S[email protected] // see end of function, too. 2262a4f30ec0S[email protected] case HCI_EVENT_DISCONNECTION_COMPLETE: 2263a4f30ec0S[email protected] if (packet[2]) break; // status != 0 2264f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 226581d2bdb2SMatthias Ringwald // drop outgoing ACL fragments if it is for closed connection and release buffer if tx not active 2266c6a37cfdSMatthias Ringwald if (hci_stack->acl_fragmentation_total_size > 0) { 2267c6a37cfdSMatthias Ringwald if (handle == READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer)){ 226881d2bdb2SMatthias Ringwald int release_buffer = hci_stack->acl_fragmentation_tx_active == 0; 226981d2bdb2SMatthias Ringwald log_info("drop fragmented ACL data for closed connection, release buffer %u", release_buffer); 2270c6a37cfdSMatthias Ringwald hci_stack->acl_fragmentation_total_size = 0; 2271c6a37cfdSMatthias Ringwald hci_stack->acl_fragmentation_pos = 0; 227281d2bdb2SMatthias Ringwald if (release_buffer){ 227381d2bdb2SMatthias Ringwald hci_release_packet_buffer(); 227481d2bdb2SMatthias Ringwald } 2275c6a37cfdSMatthias Ringwald } 2276c6a37cfdSMatthias Ringwald } 227735454696SMatthias Ringwald 22789a2e4658SMatthias Ringwald // re-enable advertisements for le connections if active 2279c6a37cfdSMatthias Ringwald conn = hci_connection_for_handle(handle); 2280c6a37cfdSMatthias Ringwald if (!conn) break; 2281a70e17d4SMatthias Ringwald conn->state = RECEIVED_DISCONNECTION_COMPLETE; 228235454696SMatthias Ringwald #ifdef ENABLE_BLE 2283d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 22842b6ab3e6SMatthias Ringwald if (hci_is_le_connection(conn)){ 22852b6ab3e6SMatthias Ringwald hci_reenable_advertisements_if_needed(); 22869a2e4658SMatthias Ringwald } 228735454696SMatthias Ringwald #endif 2288d70217a2SMatthias Ringwald #endif 2289ccda6e14S[email protected] break; 22906772a24cSmatthias.ringwald 2291c68bdf90Smatthias.ringwald case HCI_EVENT_HARDWARE_ERROR: 2292313e5f9cSMatthias Ringwald log_error("Hardware Error: 0x%02x", packet[2]); 2293d23838ecSMatthias Ringwald if (hci_stack->hardware_error_callback){ 2294c2e1fa60SMatthias Ringwald (*hci_stack->hardware_error_callback)(packet[2]); 22957586ee35S[email protected] } else { 22967586ee35S[email protected] // if no special requests, just reboot stack 22977586ee35S[email protected] hci_power_control_off(); 22987586ee35S[email protected] hci_power_control_on(); 2299c68bdf90Smatthias.ringwald } 2300c68bdf90Smatthias.ringwald break; 2301c68bdf90Smatthias.ringwald 23020e6f3837SMatthias Ringwald #ifdef ENABLE_CLASSIC 23035cf766e8SMatthias Ringwald case HCI_EVENT_ROLE_CHANGE: 23045cf766e8SMatthias Ringwald if (packet[2]) break; // status != 0 2305c4c88f1bSJakob Krantz reverse_bd_addr(&packet[3], addr); 2306c4c88f1bSJakob Krantz addr_type = BD_ADDR_TYPE_CLASSIC; 2307c4c88f1bSJakob Krantz conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 2308c4c88f1bSJakob Krantz if (!conn) break; 23095cf766e8SMatthias Ringwald conn->role = packet[9]; 23105cf766e8SMatthias Ringwald break; 23110e6f3837SMatthias Ringwald #endif 23125cf766e8SMatthias Ringwald 231363fa3374SMatthias Ringwald case HCI_EVENT_TRANSPORT_PACKET_SENT: 2314d051460cS[email protected] // release packet buffer only for asynchronous transport and if there are not further fragements 23154fa24b5fS[email protected] if (hci_transport_synchronous()) { 231663fa3374SMatthias Ringwald log_error("Synchronous HCI Transport shouldn't send HCI_EVENT_TRANSPORT_PACKET_SENT"); 23174fa24b5fS[email protected] return; // instead of break: to avoid re-entering hci_run() 23184fa24b5fS[email protected] } 231981d2bdb2SMatthias Ringwald hci_stack->acl_fragmentation_tx_active = 0; 2320d051460cS[email protected] if (hci_stack->acl_fragmentation_total_size) break; 2321d051460cS[email protected] hci_release_packet_buffer(); 2322701e3307SMatthias Ringwald 232363fa3374SMatthias Ringwald // L2CAP receives this event via the hci_emit_event below 232463fa3374SMatthias Ringwald 232535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 232663fa3374SMatthias Ringwald // For SCO, we do the can_send_now_check here 2327701e3307SMatthias Ringwald hci_notify_if_sco_can_send_now(); 232835454696SMatthias Ringwald #endif 23296b4af23dS[email protected] break; 23306b4af23dS[email protected] 233135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 233263fa3374SMatthias Ringwald case HCI_EVENT_SCO_CAN_SEND_NOW: 233363fa3374SMatthias Ringwald // For SCO, we do the can_send_now_check here 233463fa3374SMatthias Ringwald hci_notify_if_sco_can_send_now(); 233563fa3374SMatthias Ringwald return; 23361cfb383eSMatthias Ringwald 23371cfb383eSMatthias Ringwald // explode inquriy results for easier consumption 23381cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 23391cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 23401cfb383eSMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 23411cfb383eSMatthias Ringwald gap_inquiry_explode(packet); 23421cfb383eSMatthias Ringwald break; 234335454696SMatthias Ringwald #endif 234463fa3374SMatthias Ringwald 2345a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 23465909f7f2Smatthias.ringwald case HCI_EVENT_LE_META: 23475909f7f2Smatthias.ringwald switch (packet[2]){ 2348d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 234957c9da5bS[email protected] case HCI_SUBEVENT_LE_ADVERTISING_REPORT: 23504f4e0224SMatthias Ringwald // log_info("advertising report received"); 23516fab74dbSMatthias Ringwald if (!hci_stack->le_scanning_enabled) break; 235257c9da5bS[email protected] le_handle_advertisement_report(packet, size); 23537bdc6798S[email protected] break; 2354d70217a2SMatthias Ringwald #endif 23555909f7f2Smatthias.ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 23565909f7f2Smatthias.ringwald // Connection management 2357724d70a2SMatthias Ringwald reverse_bd_addr(&packet[8], addr); 23587bdc6798S[email protected] addr_type = (bd_addr_type_t)packet[7]; 23599da54300S[email protected] log_info("LE Connection_complete (status=%u) type %u, %s", packet[3], addr_type, bd_addr_to_str(addr)); 23602e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 23619c77c9dbSMatthias Ringwald 2362d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 236342ff5ba1SMatthias Ringwald // if auto-connect, remove from whitelist in both roles 236442ff5ba1SMatthias Ringwald if (hci_stack->le_connecting_state == LE_CONNECTING_WHITELIST){ 236542ff5ba1SMatthias Ringwald hci_remove_from_whitelist(addr_type, addr); 236642ff5ba1SMatthias Ringwald } 236742ff5ba1SMatthias Ringwald // handle error: error is reported only to the initiator -> outgoing connection 23685909f7f2Smatthias.ringwald if (packet[3]){ 23699c77c9dbSMatthias Ringwald 23709c77c9dbSMatthias Ringwald // handle cancelled outgoing connection 23719c77c9dbSMatthias Ringwald // "If the cancellation was successful then, after the Command Complete event for the LE_Create_Connection_Cancel command, 23729c77c9dbSMatthias Ringwald // either an LE Connection Complete or an LE Enhanced Connection Complete event shall be generated. 23739c77c9dbSMatthias Ringwald // In either case, the event shall be sent with the error code Unknown Connection Identifier (0x02)." 23749c77c9dbSMatthias Ringwald if (packet[3] == ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER){ 23759c77c9dbSMatthias Ringwald conn = gap_get_outgoing_connection(); 23769c77c9dbSMatthias Ringwald } 23779c77c9dbSMatthias Ringwald 237842ff5ba1SMatthias Ringwald // outgoing connection establishment is done 237942ff5ba1SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 238042ff5ba1SMatthias Ringwald // remove entry 23815909f7f2Smatthias.ringwald if (conn){ 2382665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 23835909f7f2Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 23845909f7f2Smatthias.ringwald } 23855909f7f2Smatthias.ringwald break; 23865909f7f2Smatthias.ringwald } 2387d70217a2SMatthias Ringwald #endif 238842ff5ba1SMatthias Ringwald // on success, both hosts receive connection complete event 23895cf766e8SMatthias Ringwald if (packet[6] == HCI_ROLE_MASTER){ 2390d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 239142ff5ba1SMatthias Ringwald // if we're master, it was an outgoing connection and we're done with it 239242ff5ba1SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 2393d70217a2SMatthias Ringwald #endif 239442ff5ba1SMatthias Ringwald } else { 2395d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 239642ff5ba1SMatthias Ringwald // if we're slave, it was an incoming connection, advertisements have stopped 2397171293d3SMatthias Ringwald hci_stack->le_advertisements_active = 0; 2398d70217a2SMatthias Ringwald #endif 239942ff5ba1SMatthias Ringwald } 2400171293d3SMatthias Ringwald // LE connections are auto-accepted, so just create a connection if there isn't one already 240142ff5ba1SMatthias Ringwald if (!conn){ 240296a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 24035909f7f2Smatthias.ringwald } 240442ff5ba1SMatthias Ringwald // no memory, sorry. 24055909f7f2Smatthias.ringwald if (!conn){ 24065909f7f2Smatthias.ringwald break; 24075909f7f2Smatthias.ringwald } 24085909f7f2Smatthias.ringwald 24095909f7f2Smatthias.ringwald conn->state = OPEN; 24105cf766e8SMatthias Ringwald conn->role = packet[6]; 2411c9db5c21SMilanka Ringwald conn->con_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); 2412c9db5c21SMilanka Ringwald conn->le_connection_interval = hci_subevent_le_connection_complete_get_conn_interval(packet); 24135909f7f2Smatthias.ringwald 24142b6ab3e6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 24152b6ab3e6SMatthias Ringwald if (packet[6] == HCI_ROLE_SLAVE){ 24162b6ab3e6SMatthias Ringwald hci_reenable_advertisements_if_needed(); 24172b6ab3e6SMatthias Ringwald } 24182b6ab3e6SMatthias Ringwald #endif 24192b6ab3e6SMatthias Ringwald 24205909f7f2Smatthias.ringwald // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock 24215909f7f2Smatthias.ringwald 24225909f7f2Smatthias.ringwald // restart timer 2423528a4a3bSMatthias Ringwald // btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 2424528a4a3bSMatthias Ringwald // btstack_run_loop_add_timer(&conn->timeout); 24255909f7f2Smatthias.ringwald 24269da54300S[email protected] log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 24275909f7f2Smatthias.ringwald 24285909f7f2Smatthias.ringwald hci_emit_nr_connections_changed(); 24295909f7f2Smatthias.ringwald break; 24305909f7f2Smatthias.ringwald 2431f8fbdce0SMatthias Ringwald // log_info("LE buffer size: %u, count %u", little_endian_read_16(packet,6), packet[8]); 2432c9db5c21SMilanka Ringwald case HCI_SUBEVENT_LE_CONNECTION_UPDATE_COMPLETE: 2433c9db5c21SMilanka Ringwald handle = hci_subevent_le_connection_update_complete_get_connection_handle(packet); 2434c9db5c21SMilanka Ringwald conn = hci_connection_for_handle(handle); 2435c9db5c21SMilanka Ringwald if (!conn) break; 2436c9db5c21SMilanka Ringwald conn->le_connection_interval = hci_subevent_le_connection_update_complete_get_conn_interval(packet); 2437c9db5c21SMilanka Ringwald break; 243865a46ef3S[email protected] 243973cd8a2aSMatthias Ringwald case HCI_SUBEVENT_LE_REMOTE_CONNECTION_PARAMETER_REQUEST: 244073cd8a2aSMatthias Ringwald // connection 244173cd8a2aSMatthias Ringwald handle = hci_subevent_le_remote_connection_parameter_request_get_connection_handle(packet); 244273cd8a2aSMatthias Ringwald conn = hci_connection_for_handle(handle); 244373cd8a2aSMatthias Ringwald if (conn) { 244473cd8a2aSMatthias Ringwald // read arguments 244573cd8a2aSMatthias Ringwald uint16_t le_conn_interval_min = hci_subevent_le_remote_connection_parameter_request_get_interval_min(packet); 244673cd8a2aSMatthias Ringwald uint16_t le_conn_interval_max = hci_subevent_le_remote_connection_parameter_request_get_interval_max(packet); 244773cd8a2aSMatthias Ringwald uint16_t le_conn_latency = hci_subevent_le_remote_connection_parameter_request_get_latency(packet); 244873cd8a2aSMatthias Ringwald uint16_t le_supervision_timeout = hci_subevent_le_remote_connection_parameter_request_get_timeout(packet); 244973cd8a2aSMatthias Ringwald 245073cd8a2aSMatthias Ringwald // validate against current connection parameter range 245173cd8a2aSMatthias Ringwald le_connection_parameter_range_t existing_range; 245273cd8a2aSMatthias Ringwald gap_get_connection_parameter_range(&existing_range); 245373cd8a2aSMatthias Ringwald int update_parameter = gap_connection_parameter_range_included(&existing_range, le_conn_interval_min, le_conn_interval_max, le_conn_latency, le_supervision_timeout); 245473cd8a2aSMatthias Ringwald if (update_parameter){ 245573cd8a2aSMatthias Ringwald conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_REPLY; 245673cd8a2aSMatthias Ringwald conn->le_conn_interval_min = le_conn_interval_min; 245773cd8a2aSMatthias Ringwald conn->le_conn_interval_max = le_conn_interval_max; 245873cd8a2aSMatthias Ringwald conn->le_conn_latency = le_conn_latency; 245973cd8a2aSMatthias Ringwald conn->le_supervision_timeout = le_supervision_timeout; 246073cd8a2aSMatthias Ringwald } else { 246173cd8a2aSMatthias Ringwald conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY; 246273cd8a2aSMatthias Ringwald } 246373cd8a2aSMatthias Ringwald } 246473cd8a2aSMatthias Ringwald break; 24655909f7f2Smatthias.ringwald default: 24665909f7f2Smatthias.ringwald break; 24675909f7f2Smatthias.ringwald } 24685909f7f2Smatthias.ringwald break; 24695909f7f2Smatthias.ringwald #endif 24700b36101dSMatthias Ringwald case HCI_EVENT_VENDOR_SPECIFIC: 24710b36101dSMatthias Ringwald // Vendor specific commands often create vendor specific event instead of num completed packets 24720b36101dSMatthias Ringwald // To avoid getting stuck as num_cmds_packets is zero, reset it to 1 for controllers with this behaviour 24730b36101dSMatthias Ringwald switch (hci_stack->manufacturer){ 24740b36101dSMatthias Ringwald case BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO: 24750b36101dSMatthias Ringwald hci_stack->num_cmd_packets = 1; 24760b36101dSMatthias Ringwald break; 24770b36101dSMatthias Ringwald default: 24780b36101dSMatthias Ringwald break; 24790b36101dSMatthias Ringwald } 24800b36101dSMatthias Ringwald break; 24816772a24cSmatthias.ringwald default: 24826772a24cSmatthias.ringwald break; 2483fe1ed1b8Smatthias.ringwald } 2484fe1ed1b8Smatthias.ringwald 24853429f56bSmatthias.ringwald // handle BT initialization 24863a9fb326S[email protected] if (hci_stack->state == HCI_STATE_INITIALIZING){ 24876155b3d3S[email protected] hci_initializing_event_handler(packet, size); 2488c9af4d3fS[email protected] } 248922909952Smatthias.ringwald 249089db417bSmatthias.ringwald // help with BT sleep 24913a9fb326S[email protected] if (hci_stack->state == HCI_STATE_FALLING_ASLEEP 249274b323a9SMatthias Ringwald && hci_stack->substate == HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE 2493073bd0faSMatthias Ringwald && HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_scan_enable)){ 249455975f88SMatthias Ringwald hci_initializing_next_state(); 249589db417bSmatthias.ringwald } 249689db417bSmatthias.ringwald 249786805605S[email protected] // notify upper stack 2498d6b06661SMatthias Ringwald hci_emit_event(packet, size, 0); // don't dump, already happened in packet handler 249994ab26f8Smatthias.ringwald 250086805605S[email protected] // moved here to give upper stack a chance to close down everything with hci_connection_t intact 25010e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_DISCONNECTION_COMPLETE){ 250286805605S[email protected] if (!packet[2]){ 2503f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 250405ae8de3SMatthias Ringwald hci_connection_t * aConn = hci_connection_for_handle(handle); 250505ae8de3SMatthias Ringwald if (aConn) { 250605ae8de3SMatthias Ringwald uint8_t status = aConn->bonding_status; 250705ae8de3SMatthias Ringwald uint16_t flags = aConn->bonding_flags; 250886805605S[email protected] bd_addr_t bd_address; 250905ae8de3SMatthias Ringwald memcpy(&bd_address, aConn->address, 6); 251005ae8de3SMatthias Ringwald hci_shutdown_connection(aConn); 2511bb820044S[email protected] // connection struct is gone, don't access anymore 2512bb820044S[email protected] if (flags & BONDING_EMIT_COMPLETE_ON_DISCONNECT){ 251386805605S[email protected] hci_emit_dedicated_bonding_result(bd_address, status); 251486805605S[email protected] } 251586805605S[email protected] } 251686805605S[email protected] } 251786805605S[email protected] } 251886805605S[email protected] 251994ab26f8Smatthias.ringwald // execute main loop 252094ab26f8Smatthias.ringwald hci_run(); 252116833f0aSmatthias.ringwald } 252216833f0aSmatthias.ringwald 252335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2524c91d150bS[email protected] static void sco_handler(uint8_t * packet, uint16_t size){ 25250b3f95dfSMatthias Ringwald // lookup connection struct 25262b838201SMatthias Ringwald hci_con_handle_t con_handle = READ_SCO_CONNECTION_HANDLE(packet); 25272b838201SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 25280b3f95dfSMatthias Ringwald if (!conn) return; 25290b3f95dfSMatthias Ringwald 25300b3f95dfSMatthias Ringwald int notify_sco = 0; 25310b3f95dfSMatthias Ringwald 2532760b20efSMatthias Ringwald // CSR 8811 prefixes 60 byte SCO packet in transparent mode with 20 zero bytes -> skip first 20 payload bytes 2533760b20efSMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO){ 253450299413SMatthias Ringwald if (size == 83 && ((hci_stack->sco_voice_setting_active & 0x03) == 0x03)){ 2535760b20efSMatthias Ringwald packet[2] = 0x3c; 2536760b20efSMatthias Ringwald memmove(&packet[3], &packet[23], 63); 2537760b20efSMatthias Ringwald size = 63; 25380b3f95dfSMatthias Ringwald } 25390b3f95dfSMatthias Ringwald } 2540760b20efSMatthias Ringwald 2541*49205f5dSMatthias Ringwald // log_debug("sco flow %u, handle 0x%04x, packets sent %u, bytes send %u", hci_stack->synchronous_flow_control_enabled, (int) con_handle, conn->num_packets_sent, conn->num_sco_bytes_sent); 2542760b20efSMatthias Ringwald if (hci_stack->synchronous_flow_control_enabled == 0){ 2543*49205f5dSMatthias Ringwald // TODO: 2544760b20efSMatthias Ringwald } 2545*49205f5dSMatthias Ringwald 25460b3f95dfSMatthias Ringwald // deliver to app 25470b3f95dfSMatthias Ringwald if (hci_stack->sco_packet_handler) { 25480b3f95dfSMatthias Ringwald hci_stack->sco_packet_handler(HCI_SCO_DATA_PACKET, 0, packet, size); 25490b3f95dfSMatthias Ringwald } 25500b3f95dfSMatthias Ringwald 25510b3f95dfSMatthias Ringwald // notify app if it can send again 25520b3f95dfSMatthias Ringwald if (notify_sco){ 25530b3f95dfSMatthias Ringwald hci_notify_if_sco_can_send_now(); 25540b3f95dfSMatthias Ringwald } 25550b3f95dfSMatthias Ringwald 25560b3f95dfSMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 25572b838201SMatthias Ringwald conn->num_packets_completed++; 25582b838201SMatthias Ringwald hci_stack->host_completed_packets = 1; 25592b838201SMatthias Ringwald hci_run(); 25602b838201SMatthias Ringwald #endif 2561c91d150bS[email protected] } 256235454696SMatthias Ringwald #endif 2563c91d150bS[email protected] 25640a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 25655bb5bc3eS[email protected] hci_dump_packet(packet_type, 1, packet, size); 256610e830c9Smatthias.ringwald switch (packet_type) { 256710e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 256810e830c9Smatthias.ringwald event_handler(packet, size); 256910e830c9Smatthias.ringwald break; 257010e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 257110e830c9Smatthias.ringwald acl_handler(packet, size); 257210e830c9Smatthias.ringwald break; 257335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2574c91d150bS[email protected] case HCI_SCO_DATA_PACKET: 2575c91d150bS[email protected] sco_handler(packet, size); 2576202c8a4cSMatthias Ringwald break; 257735454696SMatthias Ringwald #endif 257810e830c9Smatthias.ringwald default: 257910e830c9Smatthias.ringwald break; 258010e830c9Smatthias.ringwald } 258110e830c9Smatthias.ringwald } 258210e830c9Smatthias.ringwald 2583d6b06661SMatthias Ringwald /** 2584d6b06661SMatthias Ringwald * @brief Add event packet handler. 2585d6b06661SMatthias Ringwald */ 2586d6b06661SMatthias Ringwald void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){ 2587d6b06661SMatthias Ringwald btstack_linked_list_add_tail(&hci_stack->event_handlers, (btstack_linked_item_t*) callback_handler); 2588d6b06661SMatthias Ringwald } 2589d6b06661SMatthias Ringwald 2590d6b06661SMatthias Ringwald 2591fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 25923d50b4baSMatthias Ringwald void hci_register_acl_packet_handler(btstack_packet_handler_t handler){ 2593fb37a842SMatthias Ringwald hci_stack->acl_packet_handler = handler; 259416833f0aSmatthias.ringwald } 259516833f0aSmatthias.ringwald 259635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 25978abbe8b5SMatthias Ringwald /** 25988abbe8b5SMatthias Ringwald * @brief Registers a packet handler for SCO data. Used for HSP and HFP profiles. 25998abbe8b5SMatthias Ringwald */ 26003d50b4baSMatthias Ringwald void hci_register_sco_packet_handler(btstack_packet_handler_t handler){ 26018abbe8b5SMatthias Ringwald hci_stack->sco_packet_handler = handler; 26028abbe8b5SMatthias Ringwald } 260335454696SMatthias Ringwald #endif 26048abbe8b5SMatthias Ringwald 260571de195eSMatthias Ringwald static void hci_state_reset(void){ 2606595bdbfbS[email protected] // no connections yet 2607595bdbfbS[email protected] hci_stack->connections = NULL; 260874308b23Smatthias.ringwald 260974308b23Smatthias.ringwald // keep discoverable/connectable as this has been requested by the client(s) 261074308b23Smatthias.ringwald // hci_stack->discoverable = 0; 261174308b23Smatthias.ringwald // hci_stack->connectable = 0; 261274308b23Smatthias.ringwald // hci_stack->bondable = 1; 2613b95a5a35SMatthias Ringwald // hci_stack->own_addr_type = 0; 2614595bdbfbS[email protected] 261544935e40S[email protected] // buffer is free 261644935e40S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 261744935e40S[email protected] 2618595bdbfbS[email protected] // no pending cmds 2619595bdbfbS[email protected] hci_stack->decline_reason = 0; 2620595bdbfbS[email protected] hci_stack->new_scan_enable_value = 0xff; 2621595bdbfbS[email protected] 2622595bdbfbS[email protected] // LE 2623b95a5a35SMatthias Ringwald #ifdef ENABLE_BLE 2624b95a5a35SMatthias Ringwald memset(hci_stack->le_random_address, 0, 6); 2625b95a5a35SMatthias Ringwald hci_stack->le_random_address_set = 0; 2626d70217a2SMatthias Ringwald #endif 2627d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 26286fab74dbSMatthias Ringwald hci_stack->le_scanning_active = 0; 2629e2602ea2Smatthias.ringwald hci_stack->le_scan_type = 0xff; 2630b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 2631e83201bcSMatthias Ringwald hci_stack->le_whitelist = 0; 2632e83201bcSMatthias Ringwald hci_stack->le_whitelist_capacity = 0; 2633d70217a2SMatthias Ringwald #endif 2634595bdbfbS[email protected] } 2635595bdbfbS[email protected] 263635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 26372d5e09d6SMatthias Ringwald /** 26382d5e09d6SMatthias Ringwald * @brief Configure Bluetooth hardware control. Has to be called before power on. 26392d5e09d6SMatthias Ringwald */ 26402d5e09d6SMatthias Ringwald void hci_set_link_key_db(btstack_link_key_db_t const * link_key_db){ 26412d5e09d6SMatthias Ringwald // store and open remote device db 26422d5e09d6SMatthias Ringwald hci_stack->link_key_db = link_key_db; 26432d5e09d6SMatthias Ringwald if (hci_stack->link_key_db) { 26442d5e09d6SMatthias Ringwald hci_stack->link_key_db->open(); 26452d5e09d6SMatthias Ringwald } 26462d5e09d6SMatthias Ringwald } 264735454696SMatthias Ringwald #endif 26482d5e09d6SMatthias Ringwald 26492d5e09d6SMatthias Ringwald void hci_init(const hci_transport_t *transport, const void *config){ 2650475c8125Smatthias.ringwald 26513a9fb326S[email protected] #ifdef HAVE_MALLOC 26523a9fb326S[email protected] if (!hci_stack) { 26533a9fb326S[email protected] hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t)); 26543a9fb326S[email protected] } 26553a9fb326S[email protected] #else 26563a9fb326S[email protected] hci_stack = &hci_stack_static; 26573a9fb326S[email protected] #endif 265866fb9560S[email protected] memset(hci_stack, 0, sizeof(hci_stack_t)); 26593a9fb326S[email protected] 2660475c8125Smatthias.ringwald // reference to use transport layer implementation 26613a9fb326S[email protected] hci_stack->hci_transport = transport; 2662475c8125Smatthias.ringwald 266311e23e5fSmatthias.ringwald // reference to used config 26643a9fb326S[email protected] hci_stack->config = config; 266511e23e5fSmatthias.ringwald 266626a9b6daSMatthias Ringwald // setup pointer for outgoing packet buffer 266726a9b6daSMatthias Ringwald hci_stack->hci_packet_buffer = &hci_stack->hci_packet_buffer_data[HCI_OUTGOING_PRE_BUFFER_SIZE]; 266826a9b6daSMatthias Ringwald 26698fcba05dSmatthias.ringwald // max acl payload size defined in config.h 26703a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 26718fcba05dSmatthias.ringwald 267216833f0aSmatthias.ringwald // register packet handlers with transport 267310e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 2674f5454fc6Smatthias.ringwald 26753a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 2676e2386ba1S[email protected] 2677e2386ba1S[email protected] // class of device 26783a9fb326S[email protected] hci_stack->class_of_device = 0x007a020c; // Smartphone 2679a45d6b9fS[email protected] 2680f20168b8Smatthias.ringwald // bondable by default 2681f20168b8Smatthias.ringwald hci_stack->bondable = 1; 2682f20168b8Smatthias.ringwald 2683e9f343c8SMatthias Ringwald #ifdef ENABLE_CLASSIC 268463168530SMatthias Ringwald // classic name 268563168530SMatthias Ringwald hci_stack->local_name = default_classic_name; 2686c4c88f1bSJakob Krantz 2687c4c88f1bSJakob Krantz // Master slave policy 2688c4c88f1bSJakob Krantz hci_stack->master_slave_policy = 1; 2689e9f343c8SMatthias Ringwald #endif 269063168530SMatthias Ringwald 269163048403S[email protected] // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept 26923a9fb326S[email protected] hci_stack->ssp_enable = 1; 26933a9fb326S[email protected] hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT; 26943a9fb326S[email protected] hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING; 26953a9fb326S[email protected] hci_stack->ssp_auto_accept = 1; 269669a97523S[email protected] 2697fac2e2feSMatthias Ringwald // voice setting - signed 16 bit pcm data with CVSD over the air 2698fac2e2feSMatthias Ringwald hci_stack->sco_voice_setting = 0x60; 2699d950d659SMatthias Ringwald 2700831711daSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 2701831711daSMatthias Ringwald // connection parameter to use for outgoing connections 2702cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval = 0x0060; // 60ms 2703cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window = 0x0030; // 30ms 2704831711daSMatthias Ringwald hci_stack->le_connection_interval_min = 0x0008; // 10 ms 2705831711daSMatthias Ringwald hci_stack->le_connection_interval_max = 0x0018; // 30 ms 2706831711daSMatthias Ringwald hci_stack->le_connection_latency = 4; // 4 2707831711daSMatthias Ringwald hci_stack->le_supervision_timeout = 0x0048; // 720 ms 2708831711daSMatthias Ringwald hci_stack->le_minimum_ce_length = 2; // 1.25 ms 2709831711daSMatthias Ringwald hci_stack->le_maximum_ce_length = 0x0030; // 30 ms 2710831711daSMatthias Ringwald #endif 2711831711daSMatthias Ringwald 27122b6ab3e6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 27132b6ab3e6SMatthias Ringwald hci_stack->le_max_number_peripheral_connections = 1; // only single connection as peripheral 27142b6ab3e6SMatthias Ringwald #endif 27152b6ab3e6SMatthias Ringwald 2716831711daSMatthias Ringwald // connection parameter range used to answer connection parameter update requests in l2cap 2717831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_interval_min = 6; 2718831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_interval_max = 3200; 2719831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_latency_min = 0; 2720831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_latency_max = 500; 2721831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_supervision_timeout_min = 10; 2722831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_supervision_timeout_max = 3200; 2723831711daSMatthias Ringwald 2724595bdbfbS[email protected] hci_state_reset(); 2725475c8125Smatthias.ringwald } 2726475c8125Smatthias.ringwald 27273fb36a29SMatthias Ringwald /** 27283fb36a29SMatthias Ringwald * @brief Configure Bluetooth chipset driver. Has to be called before power on, or right after receiving the local version information 27293fb36a29SMatthias Ringwald */ 27303fb36a29SMatthias Ringwald void hci_set_chipset(const btstack_chipset_t *chipset_driver){ 27313fb36a29SMatthias Ringwald hci_stack->chipset = chipset_driver; 27323fb36a29SMatthias Ringwald 27333fb36a29SMatthias Ringwald // reset chipset driver - init is also called on power_up 27343fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->init){ 27353fb36a29SMatthias Ringwald hci_stack->chipset->init(hci_stack->config); 27363fb36a29SMatthias Ringwald } 27373fb36a29SMatthias Ringwald } 27383fb36a29SMatthias Ringwald 2739fb55bd0aSMatthias Ringwald /** 2740d0b87befSMatthias Ringwald * @brief Configure Bluetooth hardware control. Has to be called after hci_init() but before power on. 2741fb55bd0aSMatthias Ringwald */ 2742fb55bd0aSMatthias Ringwald void hci_set_control(const btstack_control_t *hardware_control){ 2743fb55bd0aSMatthias Ringwald // references to used control implementation 2744fb55bd0aSMatthias Ringwald hci_stack->control = hardware_control; 2745d0b87befSMatthias Ringwald // init with transport config 2746d0b87befSMatthias Ringwald hardware_control->init(hci_stack->config); 2747fb55bd0aSMatthias Ringwald } 2748fb55bd0aSMatthias Ringwald 274971de195eSMatthias Ringwald void hci_close(void){ 2750404843c1Smatthias.ringwald // close remote device db 2751a98592bcSMatthias Ringwald if (hci_stack->link_key_db) { 2752a98592bcSMatthias Ringwald hci_stack->link_key_db->close(); 2753404843c1Smatthias.ringwald } 27547224be7eSMatthias Ringwald 27557224be7eSMatthias Ringwald btstack_linked_list_iterator_t lit; 27567224be7eSMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->connections); 27577224be7eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 27587224be7eSMatthias Ringwald // cancel all l2cap connections by emitting dicsconnection complete before shutdown (free) connection 27597224be7eSMatthias Ringwald hci_connection_t * connection = (hci_connection_t*) btstack_linked_list_iterator_next(&lit); 27607224be7eSMatthias Ringwald hci_emit_disconnection_complete(connection->con_handle, 0x16); // terminated by local host 27617224be7eSMatthias Ringwald hci_shutdown_connection(connection); 2762f5454fc6Smatthias.ringwald } 27637224be7eSMatthias Ringwald 2764f5454fc6Smatthias.ringwald hci_power_control(HCI_POWER_OFF); 27653a9fb326S[email protected] 27663a9fb326S[email protected] #ifdef HAVE_MALLOC 27673a9fb326S[email protected] free(hci_stack); 27683a9fb326S[email protected] #endif 27693a9fb326S[email protected] hci_stack = NULL; 2770404843c1Smatthias.ringwald } 2771404843c1Smatthias.ringwald 277235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 277360b9e82fSMatthias Ringwald void gap_set_class_of_device(uint32_t class_of_device){ 27749e61646fS[email protected] hci_stack->class_of_device = class_of_device; 27759e61646fS[email protected] } 277676f27cffSMatthias Ringwald 2777c33e56d3SMatthias Ringwald void gap_set_default_link_policy_settings(uint16_t default_link_policy_settings){ 2778c33e56d3SMatthias Ringwald hci_stack->default_link_policy_settings = default_link_policy_settings; 2779c33e56d3SMatthias Ringwald } 2780c33e56d3SMatthias Ringwald 278176f27cffSMatthias Ringwald void hci_disable_l2cap_timeout_check(void){ 278276f27cffSMatthias Ringwald disable_l2cap_timeouts = 1; 278376f27cffSMatthias Ringwald } 278435454696SMatthias Ringwald #endif 27859e61646fS[email protected] 278676f27cffSMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 2787f456b2d0S[email protected] // Set Public BD ADDR - passed on to Bluetooth chipset if supported in bt_control_h 2788f456b2d0S[email protected] void hci_set_bd_addr(bd_addr_t addr){ 2789f456b2d0S[email protected] memcpy(hci_stack->custom_bd_addr, addr, 6); 2790f456b2d0S[email protected] hci_stack->custom_bd_addr_set = 1; 2791f456b2d0S[email protected] } 279276f27cffSMatthias Ringwald #endif 2793f456b2d0S[email protected] 27948d213e1aSmatthias.ringwald // State-Module-Driver overview 27958d213e1aSmatthias.ringwald // state module low-level 27968d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 27978d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 27988d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 27998d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 2800d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 2801d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 2802c7e0c5f6Smatthias.ringwald 280340d1c7a4Smatthias.ringwald static int hci_power_control_on(void){ 28047301ad89Smatthias.ringwald 2805038bc64cSmatthias.ringwald // power on 2806f9a30166Smatthias.ringwald int err = 0; 28073a9fb326S[email protected] if (hci_stack->control && hci_stack->control->on){ 2808d0b87befSMatthias Ringwald err = (*hci_stack->control->on)(); 2809f9a30166Smatthias.ringwald } 2810038bc64cSmatthias.ringwald if (err){ 28119da54300S[email protected] log_error( "POWER_ON failed"); 2812038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 2813038bc64cSmatthias.ringwald return err; 2814038bc64cSmatthias.ringwald } 2815038bc64cSmatthias.ringwald 281624b3c629SMatthias Ringwald // int chipset driver 28173fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->init){ 28183fb36a29SMatthias Ringwald hci_stack->chipset->init(hci_stack->config); 28193fb36a29SMatthias Ringwald } 28203fb36a29SMatthias Ringwald 282124b3c629SMatthias Ringwald // init transport 282224b3c629SMatthias Ringwald if (hci_stack->hci_transport->init){ 282324b3c629SMatthias Ringwald hci_stack->hci_transport->init(hci_stack->config); 282424b3c629SMatthias Ringwald } 282524b3c629SMatthias Ringwald 282624b3c629SMatthias Ringwald // open transport 282724b3c629SMatthias Ringwald err = hci_stack->hci_transport->open(); 2828038bc64cSmatthias.ringwald if (err){ 28299da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 28303a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 2831d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 2832f9a30166Smatthias.ringwald } 2833038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 2834038bc64cSmatthias.ringwald return err; 2835038bc64cSmatthias.ringwald } 28368d213e1aSmatthias.ringwald return 0; 28378d213e1aSmatthias.ringwald } 2838038bc64cSmatthias.ringwald 283940d1c7a4Smatthias.ringwald static void hci_power_control_off(void){ 28408d213e1aSmatthias.ringwald 28419da54300S[email protected] log_info("hci_power_control_off"); 28429418f9c9Smatthias.ringwald 28438d213e1aSmatthias.ringwald // close low-level device 284424b3c629SMatthias Ringwald hci_stack->hci_transport->close(); 28458d213e1aSmatthias.ringwald 28469da54300S[email protected] log_info("hci_power_control_off - hci_transport closed"); 28479418f9c9Smatthias.ringwald 28488d213e1aSmatthias.ringwald // power off 28493a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 2850d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 28518d213e1aSmatthias.ringwald } 28529418f9c9Smatthias.ringwald 28539da54300S[email protected] log_info("hci_power_control_off - control closed"); 28549418f9c9Smatthias.ringwald 28553a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 285672ea5239Smatthias.ringwald } 285772ea5239Smatthias.ringwald 285840d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){ 285972ea5239Smatthias.ringwald 28609da54300S[email protected] log_info("hci_power_control_sleep"); 28613144bce4Smatthias.ringwald 2862b429b9b7Smatthias.ringwald #if 0 2863b429b9b7Smatthias.ringwald // don't close serial port during sleep 2864b429b9b7Smatthias.ringwald 286572ea5239Smatthias.ringwald // close low-level device 28663a9fb326S[email protected] hci_stack->hci_transport->close(hci_stack->config); 2867b429b9b7Smatthias.ringwald #endif 286872ea5239Smatthias.ringwald 286972ea5239Smatthias.ringwald // sleep mode 28703a9fb326S[email protected] if (hci_stack->control && hci_stack->control->sleep){ 2871d0b87befSMatthias Ringwald (*hci_stack->control->sleep)(); 287272ea5239Smatthias.ringwald } 2873b429b9b7Smatthias.ringwald 28743a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 28758d213e1aSmatthias.ringwald } 28768d213e1aSmatthias.ringwald 287740d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){ 2878b429b9b7Smatthias.ringwald 28799da54300S[email protected] log_info("hci_power_control_wake"); 2880b429b9b7Smatthias.ringwald 2881b429b9b7Smatthias.ringwald // wake on 28823a9fb326S[email protected] if (hci_stack->control && hci_stack->control->wake){ 2883d0b87befSMatthias Ringwald (*hci_stack->control->wake)(); 2884b429b9b7Smatthias.ringwald } 2885b429b9b7Smatthias.ringwald 2886b429b9b7Smatthias.ringwald #if 0 2887b429b9b7Smatthias.ringwald // open low-level device 28883a9fb326S[email protected] int err = hci_stack->hci_transport->open(hci_stack->config); 2889b429b9b7Smatthias.ringwald if (err){ 28909da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 28913a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 2892d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 2893b429b9b7Smatthias.ringwald } 2894b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 2895b429b9b7Smatthias.ringwald return err; 2896b429b9b7Smatthias.ringwald } 2897b429b9b7Smatthias.ringwald #endif 2898b429b9b7Smatthias.ringwald 2899b429b9b7Smatthias.ringwald return 0; 2900b429b9b7Smatthias.ringwald } 2901b429b9b7Smatthias.ringwald 290244935e40S[email protected] static void hci_power_transition_to_initializing(void){ 290344935e40S[email protected] // set up state machine 290444935e40S[email protected] hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent 290544935e40S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 290644935e40S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 29075c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET; 290844935e40S[email protected] } 2909b429b9b7Smatthias.ringwald 29108d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 2911d661ed19Smatthias.ringwald 2912f04a0c31SMatthias Ringwald log_info("hci_power_control: %d, current mode %u", power_mode, hci_stack->state); 2913d661ed19Smatthias.ringwald 29148d213e1aSmatthias.ringwald int err = 0; 29153a9fb326S[email protected] switch (hci_stack->state){ 29168d213e1aSmatthias.ringwald 29178d213e1aSmatthias.ringwald case HCI_STATE_OFF: 29188d213e1aSmatthias.ringwald switch (power_mode){ 29198d213e1aSmatthias.ringwald case HCI_POWER_ON: 29208d213e1aSmatthias.ringwald err = hci_power_control_on(); 292197b61c7bS[email protected] if (err) { 2922f04a0c31SMatthias Ringwald log_error("hci_power_control_on() error %d", err); 292397b61c7bS[email protected] return err; 292497b61c7bS[email protected] } 292544935e40S[email protected] hci_power_transition_to_initializing(); 29268d213e1aSmatthias.ringwald break; 29278d213e1aSmatthias.ringwald case HCI_POWER_OFF: 29288d213e1aSmatthias.ringwald // do nothing 29298d213e1aSmatthias.ringwald break; 29308d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2931b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 29328d213e1aSmatthias.ringwald break; 29338d213e1aSmatthias.ringwald } 29348d213e1aSmatthias.ringwald break; 29357301ad89Smatthias.ringwald 29368d213e1aSmatthias.ringwald case HCI_STATE_INITIALIZING: 29378d213e1aSmatthias.ringwald switch (power_mode){ 29388d213e1aSmatthias.ringwald case HCI_POWER_ON: 29398d213e1aSmatthias.ringwald // do nothing 29408d213e1aSmatthias.ringwald break; 29418d213e1aSmatthias.ringwald case HCI_POWER_OFF: 29428d213e1aSmatthias.ringwald // no connections yet, just turn it off 29438d213e1aSmatthias.ringwald hci_power_control_off(); 29448d213e1aSmatthias.ringwald break; 29458d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2946b546ac54Smatthias.ringwald // no connections yet, just turn it off 294772ea5239Smatthias.ringwald hci_power_control_sleep(); 29488d213e1aSmatthias.ringwald break; 29498d213e1aSmatthias.ringwald } 29508d213e1aSmatthias.ringwald break; 29517301ad89Smatthias.ringwald 29528d213e1aSmatthias.ringwald case HCI_STATE_WORKING: 29538d213e1aSmatthias.ringwald switch (power_mode){ 29548d213e1aSmatthias.ringwald case HCI_POWER_ON: 29558d213e1aSmatthias.ringwald // do nothing 29568d213e1aSmatthias.ringwald break; 29578d213e1aSmatthias.ringwald case HCI_POWER_OFF: 2958c7e0c5f6Smatthias.ringwald // see hci_run 29593a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 2960beceeddeSMatthias Ringwald hci_stack->substate = HCI_HALTING_DISCONNECT_ALL_NO_TIMER; 29618d213e1aSmatthias.ringwald break; 29628d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2963b546ac54Smatthias.ringwald // see hci_run 29643a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 296574b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT; 29668d213e1aSmatthias.ringwald break; 29678d213e1aSmatthias.ringwald } 29688d213e1aSmatthias.ringwald break; 29697301ad89Smatthias.ringwald 29708d213e1aSmatthias.ringwald case HCI_STATE_HALTING: 29718d213e1aSmatthias.ringwald switch (power_mode){ 29728d213e1aSmatthias.ringwald case HCI_POWER_ON: 297344935e40S[email protected] hci_power_transition_to_initializing(); 29748d213e1aSmatthias.ringwald break; 29758d213e1aSmatthias.ringwald case HCI_POWER_OFF: 29768d213e1aSmatthias.ringwald // do nothing 29778d213e1aSmatthias.ringwald break; 29788d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2979b546ac54Smatthias.ringwald // see hci_run 29803a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 298174b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT; 29828d213e1aSmatthias.ringwald break; 29838d213e1aSmatthias.ringwald } 29848d213e1aSmatthias.ringwald break; 29858d213e1aSmatthias.ringwald 29868d213e1aSmatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 29878d213e1aSmatthias.ringwald switch (power_mode){ 29888d213e1aSmatthias.ringwald case HCI_POWER_ON: 298928171530Smatthias.ringwald 2990423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 299128171530Smatthias.ringwald // nothing to do, if H4 supports power management 2992d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 29933a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 299474b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_WRITE_SCAN_ENABLE; // init after sleep 299528171530Smatthias.ringwald break; 299628171530Smatthias.ringwald } 299728171530Smatthias.ringwald #endif 299844935e40S[email protected] hci_power_transition_to_initializing(); 29998d213e1aSmatthias.ringwald break; 30008d213e1aSmatthias.ringwald case HCI_POWER_OFF: 3001b546ac54Smatthias.ringwald // see hci_run 30023a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 3003beceeddeSMatthias Ringwald hci_stack->substate = HCI_HALTING_DISCONNECT_ALL_NO_TIMER; 30048d213e1aSmatthias.ringwald break; 30058d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 3006b546ac54Smatthias.ringwald // do nothing 30078d213e1aSmatthias.ringwald break; 30088d213e1aSmatthias.ringwald } 30098d213e1aSmatthias.ringwald break; 30108d213e1aSmatthias.ringwald 30118d213e1aSmatthias.ringwald case HCI_STATE_SLEEPING: 30128d213e1aSmatthias.ringwald switch (power_mode){ 30138d213e1aSmatthias.ringwald case HCI_POWER_ON: 301428171530Smatthias.ringwald 3015423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 301628171530Smatthias.ringwald // nothing to do, if H4 supports power management 3017d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 30183a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 30195c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_AFTER_SLEEP; 3020758b46ceSmatthias.ringwald hci_update_scan_enable(); 302128171530Smatthias.ringwald break; 302228171530Smatthias.ringwald } 302328171530Smatthias.ringwald #endif 30243144bce4Smatthias.ringwald err = hci_power_control_wake(); 30253144bce4Smatthias.ringwald if (err) return err; 302644935e40S[email protected] hci_power_transition_to_initializing(); 30278d213e1aSmatthias.ringwald break; 30288d213e1aSmatthias.ringwald case HCI_POWER_OFF: 30293a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 3030beceeddeSMatthias Ringwald hci_stack->substate = HCI_HALTING_DISCONNECT_ALL_NO_TIMER; 30318d213e1aSmatthias.ringwald break; 30328d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 3033b546ac54Smatthias.ringwald // do nothing 30348d213e1aSmatthias.ringwald break; 30358d213e1aSmatthias.ringwald } 30368d213e1aSmatthias.ringwald break; 303711e23e5fSmatthias.ringwald } 303868d92d03Smatthias.ringwald 3039038bc64cSmatthias.ringwald // create internal event 3040ee8bf225Smatthias.ringwald hci_emit_state(); 3041ee8bf225Smatthias.ringwald 304268d92d03Smatthias.ringwald // trigger next/first action 304368d92d03Smatthias.ringwald hci_run(); 304468d92d03Smatthias.ringwald 3045475c8125Smatthias.ringwald return 0; 3046475c8125Smatthias.ringwald } 3047475c8125Smatthias.ringwald 304835454696SMatthias Ringwald 304935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 305035454696SMatthias Ringwald 3051758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){ 3052758b46ceSmatthias.ringwald // 2 = page scan, 1 = inq scan 30533a9fb326S[email protected] hci_stack->new_scan_enable_value = hci_stack->connectable << 1 | hci_stack->discoverable; 3054758b46ceSmatthias.ringwald hci_run(); 3055758b46ceSmatthias.ringwald } 3056758b46ceSmatthias.ringwald 305715a95bd5SMatthias Ringwald void gap_discoverable_control(uint8_t enable){ 3058381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 3059381fbed8Smatthias.ringwald 30603a9fb326S[email protected] if (hci_stack->discoverable == enable){ 30613a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 3062381fbed8Smatthias.ringwald return; 3063381fbed8Smatthias.ringwald } 3064381fbed8Smatthias.ringwald 30653a9fb326S[email protected] hci_stack->discoverable = enable; 3066758b46ceSmatthias.ringwald hci_update_scan_enable(); 3067758b46ceSmatthias.ringwald } 3068b031bebbSmatthias.ringwald 306915a95bd5SMatthias Ringwald void gap_connectable_control(uint8_t enable){ 3070758b46ceSmatthias.ringwald if (enable) enable = 1; // normalize argument 3071758b46ceSmatthias.ringwald 3072758b46ceSmatthias.ringwald // don't emit event 30733a9fb326S[email protected] if (hci_stack->connectable == enable) return; 3074758b46ceSmatthias.ringwald 30753a9fb326S[email protected] hci_stack->connectable = enable; 3076758b46ceSmatthias.ringwald hci_update_scan_enable(); 3077381fbed8Smatthias.ringwald } 307835454696SMatthias Ringwald #endif 3079381fbed8Smatthias.ringwald 308015a95bd5SMatthias Ringwald void gap_local_bd_addr(bd_addr_t address_buffer){ 3081690bd0baS[email protected] memcpy(address_buffer, hci_stack->local_bd_addr, 6); 30825061f3afS[email protected] } 30835061f3afS[email protected] 30842b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 30852b838201SMatthias Ringwald static void hci_host_num_completed_packets(void){ 30862b838201SMatthias Ringwald 30872b838201SMatthias Ringwald // create packet manually as arrays are not supported and num_commands should not get reduced 30882b838201SMatthias Ringwald hci_reserve_packet_buffer(); 30892b838201SMatthias Ringwald uint8_t * packet = hci_get_outgoing_packet_buffer(); 30902b838201SMatthias Ringwald 30912b838201SMatthias Ringwald uint16_t size = 0; 30922b838201SMatthias Ringwald uint16_t num_handles = 0; 30932b838201SMatthias Ringwald packet[size++] = 0x35; 30942b838201SMatthias Ringwald packet[size++] = 0x0c; 30952b838201SMatthias Ringwald size++; // skip param len 30962b838201SMatthias Ringwald size++; // skip num handles 30972b838201SMatthias Ringwald 30982b838201SMatthias Ringwald // add { handle, packets } entries 30992b838201SMatthias Ringwald btstack_linked_item_t * it; 31002b838201SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 31012b838201SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 31022b838201SMatthias Ringwald if (connection->num_packets_completed){ 31032b838201SMatthias Ringwald little_endian_store_16(packet, size, connection->con_handle); 31042b838201SMatthias Ringwald size += 2; 31052b838201SMatthias Ringwald little_endian_store_16(packet, size, connection->num_packets_completed); 31062b838201SMatthias Ringwald size += 2; 31072b838201SMatthias Ringwald // 31082b838201SMatthias Ringwald num_handles++; 31092b838201SMatthias Ringwald connection->num_packets_completed = 0; 31102b838201SMatthias Ringwald } 31112b838201SMatthias Ringwald } 31122b838201SMatthias Ringwald 31132b838201SMatthias Ringwald packet[2] = size - 3; 31142b838201SMatthias Ringwald packet[3] = num_handles; 31152b838201SMatthias Ringwald 31162b838201SMatthias Ringwald hci_stack->host_completed_packets = 0; 31172b838201SMatthias Ringwald 31182b838201SMatthias Ringwald hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size); 31192b838201SMatthias Ringwald hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 31202b838201SMatthias Ringwald 31212b838201SMatthias Ringwald // release packet buffer for synchronous transport implementations 31222b838201SMatthias Ringwald if (hci_transport_synchronous()){ 3123e2d22487SMatthias Ringwald hci_release_packet_buffer(); 3124068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 31252b838201SMatthias Ringwald } 31262b838201SMatthias Ringwald } 31272b838201SMatthias Ringwald #endif 31282b838201SMatthias Ringwald 312926fe9592SMatthias Ringwald static void hci_halting_timeout_handler(btstack_timer_source_t * ds){ 313026fe9592SMatthias Ringwald UNUSED(ds); 313126fe9592SMatthias Ringwald hci_stack->substate = HCI_HALTING_CLOSE; 3132beceeddeSMatthias Ringwald // allow packet handlers to defer final shutdown 3133beceeddeSMatthias Ringwald hci_emit_state(); 313426fe9592SMatthias Ringwald hci_run(); 313526fe9592SMatthias Ringwald } 313626fe9592SMatthias Ringwald 313795d71764SMatthias Ringwald static void hci_run(void){ 31388a485f27Smatthias.ringwald 3139db8bc6ffSMatthias Ringwald // log_info("hci_run: entered"); 3140665d90f2SMatthias Ringwald btstack_linked_item_t * it; 314132ab9390Smatthias.ringwald 3142b5d8b22bS[email protected] // send continuation fragments first, as they block the prepared packet buffer 3143b5d8b22bS[email protected] if (hci_stack->acl_fragmentation_total_size > 0) { 3144b5d8b22bS[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer); 3145b5d8b22bS[email protected] hci_connection_t *connection = hci_connection_for_handle(con_handle); 3146b5d8b22bS[email protected] if (connection) { 314728a0332dSMatthias Ringwald if (hci_can_send_prepared_acl_packet_now(con_handle)){ 3148b5d8b22bS[email protected] hci_send_acl_packet_fragments(connection); 3149b5d8b22bS[email protected] return; 3150b5d8b22bS[email protected] } 315128a0332dSMatthias Ringwald } else { 3152b5d8b22bS[email protected] // connection gone -> discard further fragments 315328a0332dSMatthias Ringwald log_info("hci_run: fragmented ACL packet no connection -> discard fragment"); 3154b5d8b22bS[email protected] hci_stack->acl_fragmentation_total_size = 0; 3155b5d8b22bS[email protected] hci_stack->acl_fragmentation_pos = 0; 3156b5d8b22bS[email protected] } 3157b5d8b22bS[email protected] } 3158b5d8b22bS[email protected] 31592b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 31602b838201SMatthias Ringwald // send host num completed packets next as they don't require num_cmd_packets > 0 31612b838201SMatthias Ringwald if (!hci_can_send_comand_packet_transport()) return; 31622b838201SMatthias Ringwald if (hci_stack->host_completed_packets){ 31632b838201SMatthias Ringwald hci_host_num_completed_packets(); 31642b838201SMatthias Ringwald return; 31652b838201SMatthias Ringwald } 31662b838201SMatthias Ringwald #endif 31672b838201SMatthias Ringwald 3168d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 3169ce4c8fabSmatthias.ringwald 3170b031bebbSmatthias.ringwald // global/non-connection oriented commands 3171b031bebbSmatthias.ringwald 317235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 3173b031bebbSmatthias.ringwald // decline incoming connections 31743a9fb326S[email protected] if (hci_stack->decline_reason){ 31753a9fb326S[email protected] uint8_t reason = hci_stack->decline_reason; 31763a9fb326S[email protected] hci_stack->decline_reason = 0; 31773a9fb326S[email protected] hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason); 3178dbe1a790S[email protected] return; 3179ce4c8fabSmatthias.ringwald } 3180b031bebbSmatthias.ringwald // send scan enable 31813a9fb326S[email protected] if (hci_stack->state == HCI_STATE_WORKING && hci_stack->new_scan_enable_value != 0xff && hci_classic_supported()){ 31823a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value); 31833a9fb326S[email protected] hci_stack->new_scan_enable_value = 0xff; 3184dbe1a790S[email protected] return; 3185b031bebbSmatthias.ringwald } 3186f5875de5SMatthias Ringwald // start/stop inquiry 3187f5875de5SMatthias Ringwald if (hci_stack->inquiry_state >= GAP_INQUIRY_DURATION_MIN && hci_stack->inquiry_state <= GAP_INQUIRY_DURATION_MAX){ 3188f5875de5SMatthias Ringwald uint8_t duration = hci_stack->inquiry_state; 3189f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_ACTIVE; 31908aee7be2SMilanka Ringwald hci_send_cmd(&hci_inquiry, GAP_IAC_GENERAL_INQUIRY, duration, 0); 3191f5875de5SMatthias Ringwald return; 3192f5875de5SMatthias Ringwald } 3193f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_W2_CANCEL){ 3194f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W4_CANCELLED; 3195f5875de5SMatthias Ringwald hci_send_cmd(&hci_inquiry_cancel); 3196f5875de5SMatthias Ringwald return; 3197f5875de5SMatthias Ringwald } 3198b7f1ee76SMatthias Ringwald // remote name request 3199b7f1ee76SMatthias Ringwald if (hci_stack->remote_name_state == GAP_REMOTE_NAME_STATE_W2_SEND){ 3200b7f1ee76SMatthias Ringwald hci_stack->remote_name_state = GAP_REMOTE_NAME_STATE_W4_COMPLETE; 3201b7f1ee76SMatthias Ringwald hci_send_cmd(&hci_remote_name_request, hci_stack->remote_name_addr, 3202ee8a36c8SMatthias Ringwald hci_stack->remote_name_page_scan_repetition_mode, 0, hci_stack->remote_name_clock_offset); 32030e55bd69SMatthias Ringwald return; 3204b7f1ee76SMatthias Ringwald } 32050a51f88bSMatthias Ringwald // pairing 32060a51f88bSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE){ 32070a51f88bSMatthias Ringwald uint8_t state = hci_stack->gap_pairing_state; 32080a51f88bSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_IDLE; 32090a51f88bSMatthias Ringwald switch (state){ 32100a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PIN: 3211d504181aSMatthias Ringwald hci_send_cmd(&hci_pin_code_request_reply, hci_stack->gap_pairing_addr, strlen(hci_stack->gap_pairing_input.gap_pairing_pin), hci_stack->gap_pairing_input.gap_pairing_pin); 32120a51f88bSMatthias Ringwald break; 32130a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PIN_NEGATIVE: 32140a51f88bSMatthias Ringwald hci_send_cmd(&hci_pin_code_request_negative_reply, hci_stack->gap_pairing_addr); 32150a51f88bSMatthias Ringwald break; 32160a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PASSKEY: 3217d504181aSMatthias Ringwald hci_send_cmd(&hci_user_passkey_request_reply, hci_stack->gap_pairing_addr, hci_stack->gap_pairing_input.gap_pairing_passkey); 32180a51f88bSMatthias Ringwald break; 32190a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PASSKEY_NEGATIVE: 32200a51f88bSMatthias Ringwald hci_send_cmd(&hci_user_passkey_request_negative_reply, hci_stack->gap_pairing_addr); 32210a51f88bSMatthias Ringwald break; 32220a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_CONFIRMATION: 32230a51f88bSMatthias Ringwald hci_send_cmd(&hci_user_confirmation_request_reply, hci_stack->gap_pairing_addr); 32240a51f88bSMatthias Ringwald break; 32250a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_CONFIRMATION_NEGATIVE: 32260a51f88bSMatthias Ringwald hci_send_cmd(&hci_user_confirmation_request_negative_reply, hci_stack->gap_pairing_addr); 32270a51f88bSMatthias Ringwald break; 32280a51f88bSMatthias Ringwald default: 32290a51f88bSMatthias Ringwald break; 32300a51f88bSMatthias Ringwald } 32310a51f88bSMatthias Ringwald return; 32320a51f88bSMatthias Ringwald } 323335454696SMatthias Ringwald #endif 3234b031bebbSmatthias.ringwald 3235a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 3236b95a5a35SMatthias Ringwald // advertisements, active scanning, and creating connections requires randaom address to be set if using private address 3237b95a5a35SMatthias Ringwald if ((hci_stack->state == HCI_STATE_WORKING) 3238b95a5a35SMatthias Ringwald && (hci_stack->le_own_addr_type == BD_ADDR_TYPE_LE_PUBLIC || hci_stack->le_random_address_set)){ 3239d70217a2SMatthias Ringwald 3240d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 324145c102fdSMatthias Ringwald // handle le scan 32426fab74dbSMatthias Ringwald if ((hci_stack->le_scanning_enabled != hci_stack->le_scanning_active)){ 32436fab74dbSMatthias Ringwald hci_stack->le_scanning_active = hci_stack->le_scanning_enabled; 32446fab74dbSMatthias Ringwald hci_send_cmd(&hci_le_set_scan_enable, hci_stack->le_scanning_enabled, 0); 32457bdc6798S[email protected] return; 32467bdc6798S[email protected] } 3247e2602ea2Smatthias.ringwald if (hci_stack->le_scan_type != 0xff){ 3248e2602ea2Smatthias.ringwald // defaults: active scanning, accept all advertisement packets 3249e2602ea2Smatthias.ringwald int scan_type = hci_stack->le_scan_type; 3250e2602ea2Smatthias.ringwald hci_stack->le_scan_type = 0xff; 3251b95a5a35SMatthias 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); 3252e2602ea2Smatthias.ringwald return; 3253e2602ea2Smatthias.ringwald } 3254d70217a2SMatthias Ringwald #endif 3255d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 325645c102fdSMatthias Ringwald // le advertisement control 32579a2e4658SMatthias Ringwald if (hci_stack->le_advertisements_todo){ 32589a2e4658SMatthias Ringwald log_info("hci_run: gap_le: adv todo: %x", hci_stack->le_advertisements_todo ); 32599a2e4658SMatthias Ringwald } 326045c102fdSMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_DISABLE){ 326145c102fdSMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_DISABLE; 326245c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 0); 326345c102fdSMatthias Ringwald return; 326445c102fdSMatthias Ringwald } 326545c102fdSMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_PARAMS){ 326645c102fdSMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_PARAMS; 326745c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_parameters, 326845c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_min, 326945c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_max, 327045c102fdSMatthias Ringwald hci_stack->le_advertisements_type, 3271b95a5a35SMatthias Ringwald hci_stack->le_own_addr_type, 327245c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address_type, 327345c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address, 327445c102fdSMatthias Ringwald hci_stack->le_advertisements_channel_map, 327545c102fdSMatthias Ringwald hci_stack->le_advertisements_filter_policy); 327645c102fdSMatthias Ringwald return; 327745c102fdSMatthias Ringwald } 3278501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_ADV_DATA){ 3279501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 32807a85e4f5SMatthias Ringwald uint8_t adv_data_clean[31]; 32817a85e4f5SMatthias Ringwald memset(adv_data_clean, 0, sizeof(adv_data_clean)); 32827a85e4f5SMatthias Ringwald memcpy(adv_data_clean, hci_stack->le_advertisements_data, hci_stack->le_advertisements_data_len); 3283f868b059SMatthias Ringwald hci_replace_bd_addr_placeholder(adv_data_clean, hci_stack->le_advertisements_data_len); 32847a85e4f5SMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_data, hci_stack->le_advertisements_data_len, adv_data_clean); 328545c102fdSMatthias Ringwald return; 328645c102fdSMatthias Ringwald } 3287501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA){ 3288501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 3289f868b059SMatthias Ringwald uint8_t scan_data_clean[31]; 3290f868b059SMatthias Ringwald memset(scan_data_clean, 0, sizeof(scan_data_clean)); 32918cdc9940SMatthias Ringwald memcpy(scan_data_clean, hci_stack->le_scan_response_data, hci_stack->le_scan_response_data_len); 3292f868b059SMatthias Ringwald hci_replace_bd_addr_placeholder(scan_data_clean, hci_stack->le_scan_response_data_len); 3293f868b059SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_response_data, hci_stack->le_scan_response_data_len, hci_stack->le_scan_response_data); 3294501f56b3SMatthias Ringwald return; 3295501f56b3SMatthias Ringwald } 3296b95a5a35SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_ENABLE){ 329745c102fdSMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_ENABLE; 329845c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 1); 329945c102fdSMatthias Ringwald return; 330045c102fdSMatthias Ringwald } 3301d70217a2SMatthias Ringwald #endif 33029956955bSMatthias Ringwald 3303d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 33049956955bSMatthias Ringwald // 33059956955bSMatthias Ringwald // LE Whitelist Management 33069956955bSMatthias Ringwald // 33079956955bSMatthias Ringwald 33089956955bSMatthias Ringwald // check if whitelist needs modification 3309665d90f2SMatthias Ringwald btstack_linked_list_iterator_t lit; 33109956955bSMatthias Ringwald int modification_pending = 0; 3311665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 3312665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 3313665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 33149956955bSMatthias Ringwald if (entry->state & (LE_WHITELIST_REMOVE_FROM_CONTROLLER | LE_WHITELIST_ADD_TO_CONTROLLER)){ 33159956955bSMatthias Ringwald modification_pending = 1; 33169956955bSMatthias Ringwald break; 33179956955bSMatthias Ringwald } 33189956955bSMatthias Ringwald } 331991915b0bSMatthias Ringwald 33209956955bSMatthias Ringwald if (modification_pending){ 332191915b0bSMatthias Ringwald // stop connnecting if modification pending 33229956955bSMatthias Ringwald if (hci_stack->le_connecting_state != LE_CONNECTING_IDLE){ 33239956955bSMatthias Ringwald hci_send_cmd(&hci_le_create_connection_cancel); 33249956955bSMatthias Ringwald return; 33259956955bSMatthias Ringwald } 33269956955bSMatthias Ringwald 33279956955bSMatthias Ringwald // add/remove entries 3328665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 3329665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 3330665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 33319956955bSMatthias Ringwald if (entry->state & LE_WHITELIST_ADD_TO_CONTROLLER){ 33329956955bSMatthias Ringwald entry->state = LE_WHITELIST_ON_CONTROLLER; 33339956955bSMatthias Ringwald hci_send_cmd(&hci_le_add_device_to_white_list, entry->address_type, entry->address); 33349956955bSMatthias Ringwald return; 33359956955bSMatthias Ringwald 33369956955bSMatthias Ringwald } 33379956955bSMatthias Ringwald if (entry->state & LE_WHITELIST_REMOVE_FROM_CONTROLLER){ 33389ecbe201SMatthias Ringwald bd_addr_t address; 33399ecbe201SMatthias Ringwald bd_addr_type_t address_type = entry->address_type; 33409ecbe201SMatthias Ringwald memcpy(address, entry->address, 6); 3341665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry); 33429956955bSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 33439ecbe201SMatthias Ringwald hci_send_cmd(&hci_le_remove_device_from_white_list, address_type, address); 33449956955bSMatthias Ringwald return; 33459956955bSMatthias Ringwald } 33469956955bSMatthias Ringwald } 334791915b0bSMatthias Ringwald } 33489956955bSMatthias Ringwald 33499956955bSMatthias Ringwald // start connecting 335091915b0bSMatthias Ringwald if ( hci_stack->le_connecting_state == LE_CONNECTING_IDLE && 3351665d90f2SMatthias Ringwald !btstack_linked_list_empty(&hci_stack->le_whitelist)){ 33529956955bSMatthias Ringwald bd_addr_t null_addr; 33539956955bSMatthias Ringwald memset(null_addr, 0, 6); 33549956955bSMatthias Ringwald hci_send_cmd(&hci_le_create_connection, 3355cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval, // scan interval: 60 ms 3356cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window, // scan interval: 30 ms 33579956955bSMatthias Ringwald 1, // use whitelist 33589956955bSMatthias Ringwald 0, // peer address type 33599956955bSMatthias Ringwald null_addr, // peer bd addr 3360b95a5a35SMatthias Ringwald hci_stack->le_own_addr_type, // our addr type: 336173044eb2SMatthias Ringwald hci_stack->le_connection_interval_min, // conn interval min 336273044eb2SMatthias Ringwald hci_stack->le_connection_interval_max, // conn interval max 336373044eb2SMatthias Ringwald hci_stack->le_connection_latency, // conn latency 336473044eb2SMatthias Ringwald hci_stack->le_supervision_timeout, // conn latency 336573044eb2SMatthias Ringwald hci_stack->le_minimum_ce_length, // min ce length 336673044eb2SMatthias Ringwald hci_stack->le_maximum_ce_length // max ce length 33679956955bSMatthias Ringwald ); 33689956955bSMatthias Ringwald return; 33699956955bSMatthias Ringwald } 3370d70217a2SMatthias Ringwald #endif 33717bdc6798S[email protected] } 3372b2f949feS[email protected] #endif 33737bdc6798S[email protected] 337432ab9390Smatthias.ringwald // send pending HCI commands 3375665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 337605ae8de3SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 337732ab9390Smatthias.ringwald 33780bf6344aS[email protected] switch(connection->state){ 33790bf6344aS[email protected] case SEND_CREATE_CONNECTION: 33804f3229d8S[email protected] switch(connection->address_type){ 338135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 33824f3229d8S[email protected] case BD_ADDR_TYPE_CLASSIC: 33839da54300S[email protected] log_info("sending hci_create_connection"); 3384ad83dc6aS[email protected] hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 33854f3229d8S[email protected] break; 338635454696SMatthias Ringwald #endif 33874f3229d8S[email protected] default: 3388a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 3389d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 3390229331c6SMatthias Ringwald // track outgoing connection 3391229331c6SMatthias Ringwald hci_stack->outgoing_addr_type = connection->address_type; 3392229331c6SMatthias Ringwald memcpy(hci_stack->outgoing_addr, connection->address, 6); 33939da54300S[email protected] log_info("sending hci_le_create_connection"); 33944f3229d8S[email protected] hci_send_cmd(&hci_le_create_connection, 3395cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval, // conn scan interval 3396cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window, // conn scan windows 33974f3229d8S[email protected] 0, // don't use whitelist 33984f3229d8S[email protected] connection->address_type, // peer address type 33994f3229d8S[email protected] connection->address, // peer bd addr 3400b95a5a35SMatthias Ringwald hci_stack->le_own_addr_type, // our addr type: 340173044eb2SMatthias Ringwald hci_stack->le_connection_interval_min, // conn interval min 340273044eb2SMatthias Ringwald hci_stack->le_connection_interval_max, // conn interval max 340373044eb2SMatthias Ringwald hci_stack->le_connection_latency, // conn latency 340473044eb2SMatthias Ringwald hci_stack->le_supervision_timeout, // conn latency 340573044eb2SMatthias Ringwald hci_stack->le_minimum_ce_length, // min ce length 340673044eb2SMatthias Ringwald hci_stack->le_maximum_ce_length // max ce length 34074f3229d8S[email protected] ); 34084f3229d8S[email protected] connection->state = SENT_CREATE_CONNECTION; 3409b2f949feS[email protected] #endif 3410d70217a2SMatthias Ringwald #endif 34114f3229d8S[email protected] break; 34124f3229d8S[email protected] } 3413ad83dc6aS[email protected] return; 3414ad83dc6aS[email protected] 341535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 34160bf6344aS[email protected] case RECEIVED_CONNECTION_REQUEST: 34175cf766e8SMatthias Ringwald connection->role = HCI_ROLE_SLAVE; 3418e35edcc1S[email protected] if (connection->address_type == BD_ADDR_TYPE_CLASSIC){ 3419895f6685SMilanka Ringwald log_info("sending hci_accept_connection_request, remote eSCO %u", connection->remote_supported_feature_eSCO); 3420895f6685SMilanka Ringwald connection->state = ACCEPTED_CONNECTION_REQUEST; 3421c4c88f1bSJakob Krantz hci_send_cmd(&hci_accept_connection_request, connection->address, hci_stack->master_slave_policy); 3422e35edcc1S[email protected] } 3423dbe1a790S[email protected] return; 342435454696SMatthias Ringwald #endif 34250bf6344aS[email protected] 3426a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 3427d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 34280bf6344aS[email protected] case SEND_CANCEL_CONNECTION: 34290bf6344aS[email protected] connection->state = SENT_CANCEL_CONNECTION; 34300bf6344aS[email protected] hci_send_cmd(&hci_le_create_connection_cancel); 34310bf6344aS[email protected] return; 3432a6725849S[email protected] #endif 3433d70217a2SMatthias Ringwald #endif 34340bf6344aS[email protected] case SEND_DISCONNECT: 34350bf6344aS[email protected] connection->state = SENT_DISCONNECT; 34367851196eSmatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 34370bf6344aS[email protected] return; 34380bf6344aS[email protected] 34390bf6344aS[email protected] default: 34400bf6344aS[email protected] break; 3441c7e0c5f6Smatthias.ringwald } 3442c7e0c5f6Smatthias.ringwald 3443cabf004eSMatthias Ringwald // no further commands if connection is about to get shut down 3444cabf004eSMatthias Ringwald if (connection->state == SENT_DISCONNECT) continue; 3445cabf004eSMatthias Ringwald 344635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 344732ab9390Smatthias.ringwald if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){ 34489da54300S[email protected] log_info("responding to link key request"); 344934d2123cS[email protected] connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST); 345032ab9390Smatthias.ringwald link_key_t link_key; 3451c77e8838S[email protected] link_key_type_t link_key_type; 3452a98592bcSMatthias Ringwald if ( hci_stack->link_key_db 3453a98592bcSMatthias Ringwald && hci_stack->link_key_db->get_link_key(connection->address, link_key, &link_key_type) 345434d2123cS[email protected] && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){ 34559ab95c90S[email protected] connection->link_key_type = link_key_type; 345632ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key); 345732ab9390Smatthias.ringwald } else { 345832ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 345932ab9390Smatthias.ringwald } 3460dbe1a790S[email protected] return; 346132ab9390Smatthias.ringwald } 34621d6b20aeS[email protected] 3463899283eaS[email protected] if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){ 34649da54300S[email protected] log_info("denying to pin request"); 3465899283eaS[email protected] connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST); 346634d2123cS[email protected] hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address); 34674c57c146S[email protected] return; 34684c57c146S[email protected] } 34694c57c146S[email protected] 3470dbe1a790S[email protected] if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){ 347134d2123cS[email protected] connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY); 347282d8f825S[email protected] log_info("IO Capability Request received, stack bondable %u, io cap %u", hci_stack->bondable, hci_stack->ssp_io_capability); 347382d8f825S[email protected] if (hci_stack->bondable && (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN)){ 3474106d6d11S[email protected] // tweak authentication requirements 34753a9fb326S[email protected] uint8_t authreq = hci_stack->ssp_authentication_requirement; 3476106d6d11S[email protected] if (connection->bonding_flags & BONDING_DEDICATED){ 34779faad3abS[email protected] authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 34789faad3abS[email protected] } 34799faad3abS[email protected] if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){ 34809faad3abS[email protected] authreq |= 1; 3481106d6d11S[email protected] } 34823a9fb326S[email protected] hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, NULL, authreq); 3483f8fb5f6eS[email protected] } else { 3484f8fb5f6eS[email protected] hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED); 3485f8fb5f6eS[email protected] } 3486dbe1a790S[email protected] return; 348732ab9390Smatthias.ringwald } 348832ab9390Smatthias.ringwald 3489dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){ 3490dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY); 349134d2123cS[email protected] hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address); 3492dbe1a790S[email protected] return; 3493dbe1a790S[email protected] } 3494dbe1a790S[email protected] 3495dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){ 3496dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY); 349734d2123cS[email protected] hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000); 3498dbe1a790S[email protected] return; 3499dbe1a790S[email protected] } 3500afd4e962S[email protected] 3501afd4e962S[email protected] if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){ 3502afd4e962S[email protected] connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES; 350334d2123cS[email protected] hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle); 35042bd8b7e7S[email protected] return; 35052bd8b7e7S[email protected] } 35062bd8b7e7S[email protected] 3507ad83dc6aS[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){ 3508ad83dc6aS[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE; 35091bd5283dS[email protected] connection->bonding_flags |= BONDING_EMIT_COMPLETE_ON_DISCONNECT; 351052d34f98S[email protected] hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // authentication done 3511ad83dc6aS[email protected] return; 3512ad83dc6aS[email protected] } 351376f27cffSMatthias Ringwald 351434d2123cS[email protected] if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){ 351534d2123cS[email protected] connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST; 351634d2123cS[email protected] hci_send_cmd(&hci_authentication_requested, connection->con_handle); 35172bd8b7e7S[email protected] return; 3518afd4e962S[email protected] } 351976f27cffSMatthias Ringwald 3520dce78009S[email protected] if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){ 3521dce78009S[email protected] connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST; 3522dce78009S[email protected] hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1); 3523dce78009S[email protected] return; 3524dce78009S[email protected] } 352576f27cffSMatthias Ringwald #endif 352676f27cffSMatthias Ringwald 352776f27cffSMatthias Ringwald if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){ 352876f27cffSMatthias Ringwald connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK; 352976f27cffSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005); // authentication failure 353076f27cffSMatthias Ringwald return; 353176f27cffSMatthias Ringwald } 3532da886c03S[email protected] 35336cdc2862SMatthias Ringwald #ifdef ENABLE_CLASSIC 3534f8ee3071SMatthias Ringwald uint16_t sniff_min_interval; 3535f8ee3071SMatthias Ringwald switch (connection->sniff_min_interval){ 3536f8ee3071SMatthias Ringwald case 0: 3537f8ee3071SMatthias Ringwald break; 3538f8ee3071SMatthias Ringwald case 0xffff: 3539f8ee3071SMatthias Ringwald connection->sniff_min_interval = 0; 3540f8ee3071SMatthias Ringwald hci_send_cmd(&hci_exit_sniff_mode, connection->con_handle); 3541f8ee3071SMatthias Ringwald return; 3542f8ee3071SMatthias Ringwald default: 3543f8ee3071SMatthias Ringwald sniff_min_interval = connection->sniff_min_interval; 3544f8ee3071SMatthias Ringwald connection->sniff_min_interval = 0; 3545f8ee3071SMatthias Ringwald hci_send_cmd(&hci_sniff_mode, connection->con_handle, connection->sniff_max_interval, sniff_min_interval, connection->sniff_attempt, connection->sniff_timeout); 3546e705510cSMatthias Ringwald return; 3547f8ee3071SMatthias Ringwald } 35486cdc2862SMatthias Ringwald #endif 3549f8ee3071SMatthias Ringwald 3550a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 355173cd8a2aSMatthias Ringwald switch (connection->le_con_parameter_update_state){ 355273cd8a2aSMatthias Ringwald // response to L2CAP CON PARAMETER UPDATE REQUEST 355373cd8a2aSMatthias Ringwald case CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS: 3554da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 355573cd8a2aSMatthias Ringwald hci_send_cmd(&hci_le_connection_update, connection->con_handle, connection->le_conn_interval_min, 3556c37a3166S[email protected] connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout, 3557c37a3166S[email protected] 0x0000, 0xffff); 3558e705510cSMatthias Ringwald return; 355973cd8a2aSMatthias Ringwald case CON_PARAMETER_UPDATE_REPLY: 356073cd8a2aSMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 356173cd8a2aSMatthias Ringwald hci_send_cmd(&hci_le_remote_connection_parameter_request_reply, connection->con_handle, connection->le_conn_interval_min, 356273cd8a2aSMatthias Ringwald connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout, 356373cd8a2aSMatthias Ringwald 0x0000, 0xffff); 3564e705510cSMatthias Ringwald return; 356573cd8a2aSMatthias Ringwald case CON_PARAMETER_UPDATE_NEGATIVE_REPLY: 356673cd8a2aSMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 356773cd8a2aSMatthias Ringwald hci_send_cmd(&hci_le_remote_connection_parameter_request_negative_reply, ERROR_CODE_UNSUPPORTED_LMP_PARAMETER_VALUE_UNSUPPORTED_LL_PARAMETER_VALUE); 3568e705510cSMatthias Ringwald return; 356973cd8a2aSMatthias Ringwald default: 357073cd8a2aSMatthias Ringwald break; 3571c37a3166S[email protected] } 3572b90f6e0aSMatthias Ringwald if (connection->le_phy_update_all_phys != 0xff){ 3573b90f6e0aSMatthias Ringwald uint8_t all_phys = connection->le_phy_update_all_phys; 3574b90f6e0aSMatthias Ringwald connection->le_phy_update_all_phys = 0xff; 3575b90f6e0aSMatthias Ringwald hci_send_cmd(&hci_le_set_phy, connection->con_handle, all_phys, connection->le_phy_update_tx_phys, connection->le_phy_update_rx_phys, connection->le_phy_update_phy_options); 3576b90f6e0aSMatthias Ringwald return; 3577b90f6e0aSMatthias Ringwald } 3578c37a3166S[email protected] #endif 3579dbe1a790S[email protected] } 3580c7e0c5f6Smatthias.ringwald 358105ae8de3SMatthias Ringwald hci_connection_t * connection; 35823a9fb326S[email protected] switch (hci_stack->state){ 35833429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 358474b323a9SMatthias Ringwald hci_initializing_run(); 35853429f56bSmatthias.ringwald break; 3586c7e0c5f6Smatthias.ringwald 3587c7e0c5f6Smatthias.ringwald case HCI_STATE_HALTING: 3588c7e0c5f6Smatthias.ringwald 358926fe9592SMatthias Ringwald log_info("HCI_STATE_HALTING, substate %x\n", hci_stack->substate); 359026fe9592SMatthias Ringwald switch (hci_stack->substate){ 3591beceeddeSMatthias Ringwald case HCI_HALTING_DISCONNECT_ALL_NO_TIMER: 3592beceeddeSMatthias Ringwald case HCI_HALTING_DISCONNECT_ALL_TIMER: 3593beceeddeSMatthias Ringwald 3594a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 3595d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 3596beceeddeSMatthias Ringwald // free whitelist entries 35979956955bSMatthias Ringwald { 3598665d90f2SMatthias Ringwald btstack_linked_list_iterator_t lit; 3599665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 3600665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 3601665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 3602665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry); 36039956955bSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 36049956955bSMatthias Ringwald } 36059956955bSMatthias Ringwald } 36069956955bSMatthias Ringwald #endif 3607d70217a2SMatthias Ringwald #endif 3608c7e0c5f6Smatthias.ringwald // close all open connections 36093a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 3610c7e0c5f6Smatthias.ringwald if (connection){ 3611711e6c80SMatthias Ringwald hci_con_handle_t con_handle = (uint16_t) connection->con_handle; 3612d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 361332ab9390Smatthias.ringwald 36148fca890eSMatthias Ringwald // check state 36158fca890eSMatthias Ringwald if (connection->state == SENT_DISCONNECT) return; 36168fca890eSMatthias Ringwald connection->state = SENT_DISCONNECT; 36178fca890eSMatthias Ringwald 36188837e9efSMatthias Ringwald log_info("HCI_STATE_HALTING, connection %p, handle %u", connection, con_handle); 3619c7e0c5f6Smatthias.ringwald 36208837e9efSMatthias Ringwald // cancel all l2cap connections right away instead of waiting for disconnection complete event ... 36218837e9efSMatthias Ringwald hci_emit_disconnection_complete(con_handle, 0x16); // terminated by local host 36228837e9efSMatthias Ringwald 36238837e9efSMatthias Ringwald // ... which would be ignored anyway as we shutdown (free) the connection now 3624c7e0c5f6Smatthias.ringwald hci_shutdown_connection(connection); 36258837e9efSMatthias Ringwald 36268837e9efSMatthias Ringwald // finally, send the disconnect command 36278837e9efSMatthias Ringwald hci_send_cmd(&hci_disconnect, con_handle, 0x13); // remote closed connection 3628c7e0c5f6Smatthias.ringwald return; 3629c7e0c5f6Smatthias.ringwald } 363026fe9592SMatthias Ringwald 3631beceeddeSMatthias Ringwald if (hci_stack->substate == HCI_HALTING_DISCONNECT_ALL_TIMER){ 3632beceeddeSMatthias Ringwald // no connections left, wait a bit to assert that btstack_cyrpto isn't waiting for an HCI event 3633beceeddeSMatthias Ringwald log_info("HCI_STATE_HALTING: wait 50 ms"); 363426fe9592SMatthias Ringwald hci_stack->substate = HCI_HALTING_W4_TIMER; 3635beceeddeSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, 50); 363626fe9592SMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_halting_timeout_handler); 363726fe9592SMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 363826fe9592SMatthias Ringwald break; 3639beceeddeSMatthias Ringwald } 364026fe9592SMatthias Ringwald 3641beceeddeSMatthias Ringwald /* explicit fall-through */ 364226fe9592SMatthias Ringwald 364326fe9592SMatthias Ringwald case HCI_HALTING_CLOSE: 36449da54300S[email protected] log_info("HCI_STATE_HALTING, calling off"); 3645c7e0c5f6Smatthias.ringwald 364672ea5239Smatthias.ringwald // switch mode 3647c7e0c5f6Smatthias.ringwald hci_power_control_off(); 36489418f9c9Smatthias.ringwald 36499da54300S[email protected] log_info("HCI_STATE_HALTING, emitting state"); 365072ea5239Smatthias.ringwald hci_emit_state(); 36519da54300S[email protected] log_info("HCI_STATE_HALTING, done"); 365272ea5239Smatthias.ringwald break; 3653beceeddeSMatthias Ringwald 3654beceeddeSMatthias Ringwald case HCI_HALTING_W4_TIMER: 3655beceeddeSMatthias Ringwald // keep waiting 3656beceeddeSMatthias Ringwald 3657beceeddeSMatthias Ringwald break; 365826fe9592SMatthias Ringwald default: 365926fe9592SMatthias Ringwald break; 366026fe9592SMatthias Ringwald } 366126fe9592SMatthias Ringwald 366226fe9592SMatthias Ringwald break; 3663c7e0c5f6Smatthias.ringwald 366472ea5239Smatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 36653a9fb326S[email protected] switch(hci_stack->substate) { 366674b323a9SMatthias Ringwald case HCI_FALLING_ASLEEP_DISCONNECT: 36679da54300S[email protected] log_info("HCI_STATE_FALLING_ASLEEP"); 366872ea5239Smatthias.ringwald // close all open connections 36693a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 367066da7044Smatthias.ringwald 3671423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 367266da7044Smatthias.ringwald // don't close connections, if H4 supports power management 3673d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 367466da7044Smatthias.ringwald connection = NULL; 367566da7044Smatthias.ringwald } 367666da7044Smatthias.ringwald #endif 367772ea5239Smatthias.ringwald if (connection){ 367832ab9390Smatthias.ringwald 367972ea5239Smatthias.ringwald // send disconnect 3680d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 368132ab9390Smatthias.ringwald 36829da54300S[email protected] log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u", connection, (uint16_t)connection->con_handle); 36836ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 368472ea5239Smatthias.ringwald 368572ea5239Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 368672ea5239Smatthias.ringwald hci_shutdown_connection(connection); 368772ea5239Smatthias.ringwald return; 368872ea5239Smatthias.ringwald } 368972ea5239Smatthias.ringwald 369092368cd3S[email protected] if (hci_classic_supported()){ 369189db417bSmatthias.ringwald // disable page and inquiry scan 3692d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 369332ab9390Smatthias.ringwald 36949da54300S[email protected] log_info("HCI_STATE_HALTING, disabling inq scans"); 36953a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan 369689db417bSmatthias.ringwald 369789db417bSmatthias.ringwald // continue in next sub state 369874b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE; 369989db417bSmatthias.ringwald break; 370092368cd3S[email protected] } 3701202c8a4cSMatthias Ringwald // no break - fall through for ble-only chips 370292368cd3S[email protected] 370374b323a9SMatthias Ringwald case HCI_FALLING_ASLEEP_COMPLETE: 37049da54300S[email protected] log_info("HCI_STATE_HALTING, calling sleep"); 3705423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 370628171530Smatthias.ringwald // don't actually go to sleep, if H4 supports power management 3707d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 370828171530Smatthias.ringwald // SLEEP MODE reached 37093a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 371028171530Smatthias.ringwald hci_emit_state(); 371128171530Smatthias.ringwald break; 371228171530Smatthias.ringwald } 371328171530Smatthias.ringwald #endif 371472ea5239Smatthias.ringwald // switch mode 37153a9fb326S[email protected] hci_power_control_sleep(); // changes hci_stack->state to SLEEP 3716c7e0c5f6Smatthias.ringwald hci_emit_state(); 371728171530Smatthias.ringwald break; 371828171530Smatthias.ringwald 371989db417bSmatthias.ringwald default: 372089db417bSmatthias.ringwald break; 372189db417bSmatthias.ringwald } 3722c7e0c5f6Smatthias.ringwald break; 3723c7e0c5f6Smatthias.ringwald 37243429f56bSmatthias.ringwald default: 37253429f56bSmatthias.ringwald break; 37261f504dbdSmatthias.ringwald } 37273429f56bSmatthias.ringwald } 372816833f0aSmatthias.ringwald 372931452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 373035454696SMatthias Ringwald // house-keeping 373135454696SMatthias Ringwald 373235454696SMatthias Ringwald if (IS_COMMAND(packet, hci_write_loopback_mode)){ 373335454696SMatthias Ringwald hci_stack->loopback_mode = packet[3]; 373435454696SMatthias Ringwald } 373535454696SMatthias Ringwald 373635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 3737c8e4258aSmatthias.ringwald bd_addr_t addr; 3738c8e4258aSmatthias.ringwald hci_connection_t * conn; 3739c8e4258aSmatthias.ringwald 3740c8e4258aSmatthias.ringwald // create_connection? 3741c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 3742724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 37439da54300S[email protected] log_info("Create_connection to %s", bd_addr_to_str(addr)); 3744c8e4258aSmatthias.ringwald 37452e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 3746ad83dc6aS[email protected] if (!conn){ 374796a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 374817f1ba2aSmatthias.ringwald if (!conn){ 374917f1ba2aSmatthias.ringwald // notify client that alloc failed 37502deddeceSMatthias Ringwald hci_emit_connection_complete(addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 3751f5e5741dSMatthias Ringwald return -1; // packet not sent to controller 375217f1ba2aSmatthias.ringwald } 3753ad83dc6aS[email protected] conn->state = SEND_CREATE_CONNECTION; 3754ad83dc6aS[email protected] } 3755ad83dc6aS[email protected] log_info("conn state %u", conn->state); 3756ad83dc6aS[email protected] switch (conn->state){ 3757ad83dc6aS[email protected] // if connection active exists 3758ad83dc6aS[email protected] case OPEN: 3759f5e5741dSMatthias Ringwald // and OPEN, emit connection complete command 3760274dad91SMatthias Ringwald hci_emit_connection_complete(addr, conn->con_handle, 0); 3761f5e5741dSMatthias Ringwald return -1; // packet not sent to controller 3762ad83dc6aS[email protected] case SEND_CREATE_CONNECTION: 3763532f91a5SMatthias Ringwald // connection created by hci, e.g. dedicated bonding, but not executed yet, let's do it now 3764532f91a5SMatthias Ringwald break; 3765ad83dc6aS[email protected] default: 3766ad83dc6aS[email protected] // otherwise, just ignore as it is already in the open process 3767f5e5741dSMatthias Ringwald return -1; // packet not sent to controller 3768ad83dc6aS[email protected] } 3769c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 3770229331c6SMatthias Ringwald 3771229331c6SMatthias Ringwald // track outgoing connection 3772229331c6SMatthias Ringwald hci_stack->outgoing_addr_type = BD_ADDR_TYPE_CLASSIC; 3773229331c6SMatthias Ringwald memcpy(hci_stack->outgoing_addr, addr, 6); 3774c8e4258aSmatthias.ringwald } 377535454696SMatthias Ringwald 37767fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_reply)){ 37777fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY); 37787fde4af9Smatthias.ringwald } 37797fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){ 37807fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST); 37817fde4af9Smatthias.ringwald } 37827fde4af9Smatthias.ringwald 37838ef73945Smatthias.ringwald if (IS_COMMAND(packet, hci_delete_stored_link_key)){ 3784a98592bcSMatthias Ringwald if (hci_stack->link_key_db){ 3785724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 3786a98592bcSMatthias Ringwald hci_stack->link_key_db->delete_link_key(addr); 37878ef73945Smatthias.ringwald } 37888ef73945Smatthias.ringwald } 3789c8e4258aSmatthias.ringwald 37906724cd9eS[email protected] if (IS_COMMAND(packet, hci_pin_code_request_negative_reply) 37916724cd9eS[email protected] || IS_COMMAND(packet, hci_pin_code_request_reply)){ 3792724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 37932e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 37946724cd9eS[email protected] if (conn){ 37956724cd9eS[email protected] connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE); 37966724cd9eS[email protected] } 37976724cd9eS[email protected] } 37986724cd9eS[email protected] 37996724cd9eS[email protected] if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply) 38006724cd9eS[email protected] || IS_COMMAND(packet, hci_user_confirmation_request_reply) 38016724cd9eS[email protected] || IS_COMMAND(packet, hci_user_passkey_request_negative_reply) 38026724cd9eS[email protected] || IS_COMMAND(packet, hci_user_passkey_request_reply)) { 3803724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 38042e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 38056724cd9eS[email protected] if (conn){ 38066724cd9eS[email protected] connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE); 38076724cd9eS[email protected] } 38086724cd9eS[email protected] } 3809ee752bb8SMatthias Ringwald 3810ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 3811ee752bb8SMatthias Ringwald // setup_synchronous_connection? Voice setting at offset 22 3812ee752bb8SMatthias Ringwald if (IS_COMMAND(packet, hci_setup_synchronous_connection)){ 3813ee752bb8SMatthias Ringwald // TODO: compare to current setting if sco connection already active 3814ee752bb8SMatthias Ringwald hci_stack->sco_voice_setting_active = little_endian_read_16(packet, 15); 3815ee752bb8SMatthias Ringwald } 3816ee752bb8SMatthias Ringwald // accept_synchronus_connection? Voice setting at offset 18 3817ee752bb8SMatthias Ringwald if (IS_COMMAND(packet, hci_accept_synchronous_connection)){ 3818ee752bb8SMatthias Ringwald // TODO: compare to current setting if sco connection already active 3819ee752bb8SMatthias Ringwald hci_stack->sco_voice_setting_active = little_endian_read_16(packet, 19); 3820ee752bb8SMatthias Ringwald } 3821ee752bb8SMatthias Ringwald #endif 382235454696SMatthias Ringwald #endif 38234b3e1e19SMatthias Ringwald 3824a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 3825d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 382669a97523S[email protected] if (IS_COMMAND(packet, hci_le_set_random_address)){ 3827b95a5a35SMatthias Ringwald hci_stack->le_random_address_set = 1; 3828b95a5a35SMatthias Ringwald reverse_bd_addr(&packet[3], hci_stack->le_random_address); 3829d70217a2SMatthias Ringwald } 3830171293d3SMatthias Ringwald if (IS_COMMAND(packet, hci_le_set_advertise_enable)){ 3831171293d3SMatthias Ringwald hci_stack->le_advertisements_active = packet[3]; 3832171293d3SMatthias Ringwald } 3833d70217a2SMatthias Ringwald #endif 3834d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 3835b04dfa37SMatthias Ringwald if (IS_COMMAND(packet, hci_le_create_connection)){ 3836b04dfa37SMatthias Ringwald // white list used? 3837b04dfa37SMatthias Ringwald uint8_t initiator_filter_policy = packet[7]; 3838b04dfa37SMatthias Ringwald switch (initiator_filter_policy){ 3839b04dfa37SMatthias Ringwald case 0: 3840b04dfa37SMatthias Ringwald // whitelist not used 3841b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_DIRECT; 3842b04dfa37SMatthias Ringwald break; 3843b04dfa37SMatthias Ringwald case 1: 3844b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_WHITELIST; 3845b04dfa37SMatthias Ringwald break; 3846b04dfa37SMatthias Ringwald default: 3847b04dfa37SMatthias Ringwald log_error("Invalid initiator_filter_policy in LE Create Connection %u", initiator_filter_policy); 3848b04dfa37SMatthias Ringwald break; 3849b04dfa37SMatthias Ringwald } 3850b04dfa37SMatthias Ringwald } 3851b04dfa37SMatthias Ringwald if (IS_COMMAND(packet, hci_le_create_connection_cancel)){ 3852b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 3853b04dfa37SMatthias Ringwald } 385469a97523S[email protected] #endif 3855d70217a2SMatthias Ringwald #endif 385669a97523S[email protected] 38573a9fb326S[email protected] hci_stack->num_cmd_packets--; 38585bb5bc3eS[email protected] 38595bb5bc3eS[email protected] hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size); 3860bfea0222SMatthias Ringwald return hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 386131452debSmatthias.ringwald } 38628adf0ddaSmatthias.ringwald 38632bd8b7e7S[email protected] // disconnect because of security block 38642bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){ 38652bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 38662bd8b7e7S[email protected] if (!connection) return; 38672bd8b7e7S[email protected] connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 38682bd8b7e7S[email protected] } 38692bd8b7e7S[email protected] 38702bd8b7e7S[email protected] 3871dbe1a790S[email protected] // Configure Secure Simple Pairing 3872dbe1a790S[email protected] 387335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 387435454696SMatthias Ringwald 3875dbe1a790S[email protected] // enable will enable SSP during init 387615a95bd5SMatthias Ringwald void gap_ssp_set_enable(int enable){ 38773a9fb326S[email protected] hci_stack->ssp_enable = enable; 3878dbe1a790S[email protected] } 3879dbe1a790S[email protected] 388095d71764SMatthias Ringwald static int hci_local_ssp_activated(void){ 388115a95bd5SMatthias Ringwald return gap_ssp_supported() && hci_stack->ssp_enable; 38822bd8b7e7S[email protected] } 38832bd8b7e7S[email protected] 3884dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement 388515a95bd5SMatthias Ringwald void gap_ssp_set_io_capability(int io_capability){ 38863a9fb326S[email protected] hci_stack->ssp_io_capability = io_capability; 3887dbe1a790S[email protected] } 388815a95bd5SMatthias Ringwald void gap_ssp_set_authentication_requirement(int authentication_requirement){ 38893a9fb326S[email protected] hci_stack->ssp_authentication_requirement = authentication_requirement; 3890dbe1a790S[email protected] } 3891dbe1a790S[email protected] 3892dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested 389315a95bd5SMatthias Ringwald void gap_ssp_set_auto_accept(int auto_accept){ 38943a9fb326S[email protected] hci_stack->ssp_auto_accept = auto_accept; 3895dbe1a790S[email protected] } 389635454696SMatthias Ringwald #endif 3897dbe1a790S[email protected] 389894be1a66SMatthias Ringwald // va_list part of hci_send_cmd 389994be1a66SMatthias Ringwald int hci_send_cmd_va_arg(const hci_cmd_t *cmd, va_list argptr){ 3900d94d3cafS[email protected] if (!hci_can_send_command_packet_now()){ 39019d14b626S[email protected] log_error("hci_send_cmd called but cannot send packet now"); 39029d14b626S[email protected] return 0; 39039d14b626S[email protected] } 39049d14b626S[email protected] 39055127cc62S[email protected] // for HCI INITIALIZATION 39069da54300S[email protected] // log_info("hci_send_cmd: opcode %04x", cmd->opcode); 39075127cc62S[email protected] hci_stack->last_cmd_opcode = cmd->opcode; 39085127cc62S[email protected] 39099d14b626S[email protected] hci_reserve_packet_buffer(); 39109d14b626S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 391194be1a66SMatthias Ringwald uint16_t size = hci_cmd_create_from_template(packet, cmd, argptr); 3912bfea0222SMatthias Ringwald int err = hci_send_cmd_packet(packet, size); 3913bfea0222SMatthias Ringwald 3914bfea0222SMatthias Ringwald // release packet buffer for synchronous transport implementations 3915bfea0222SMatthias Ringwald if (hci_transport_synchronous()){ 3916e2d22487SMatthias Ringwald hci_release_packet_buffer(); 3917068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 3918bfea0222SMatthias Ringwald } 3919bfea0222SMatthias Ringwald 3920bfea0222SMatthias Ringwald return err; 392194be1a66SMatthias Ringwald } 39229d14b626S[email protected] 392394be1a66SMatthias Ringwald /** 392494be1a66SMatthias Ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 392594be1a66SMatthias Ringwald */ 392694be1a66SMatthias Ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){ 39271cd208adSmatthias.ringwald va_list argptr; 39281cd208adSmatthias.ringwald va_start(argptr, cmd); 392994be1a66SMatthias Ringwald int res = hci_send_cmd_va_arg(cmd, argptr); 39301cd208adSmatthias.ringwald va_end(argptr); 393194be1a66SMatthias Ringwald return res; 393293b8dc03Smatthias.ringwald } 3933c8e4258aSmatthias.ringwald 3934ee091cf1Smatthias.ringwald // Create various non-HCI events. 3935ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 3936ee091cf1Smatthias.ringwald 3937d6b06661SMatthias Ringwald static void hci_emit_event(uint8_t * event, uint16_t size, int dump){ 3938fb37a842SMatthias Ringwald // dump packet 3939d6b06661SMatthias Ringwald if (dump) { 3940300c1ba4SMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, size); 3941d6b06661SMatthias Ringwald } 39421ef6bb52SMatthias Ringwald 3943fb37a842SMatthias Ringwald // dispatch to all event handlers 39441ef6bb52SMatthias Ringwald btstack_linked_list_iterator_t it; 39451ef6bb52SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->event_handlers); 39461ef6bb52SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 39471ef6bb52SMatthias Ringwald btstack_packet_callback_registration_t * entry = (btstack_packet_callback_registration_t*) btstack_linked_list_iterator_next(&it); 3948d9a7306aSMatthias Ringwald entry->callback(HCI_EVENT_PACKET, 0, event, size); 39491ef6bb52SMatthias Ringwald } 3950d6b06661SMatthias Ringwald } 3951d6b06661SMatthias Ringwald 3952d6b06661SMatthias Ringwald static void hci_emit_acl_packet(uint8_t * packet, uint16_t size){ 3953fb37a842SMatthias Ringwald if (!hci_stack->acl_packet_handler) return; 39543d50b4baSMatthias Ringwald hci_stack->acl_packet_handler(HCI_ACL_DATA_PACKET, 0, packet, size); 3955d6b06661SMatthias Ringwald } 3956d6b06661SMatthias Ringwald 395735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 3958701e3307SMatthias Ringwald static void hci_notify_if_sco_can_send_now(void){ 39593bc639ceSMatthias Ringwald // notify SCO sender if waiting 3960701e3307SMatthias Ringwald if (!hci_stack->sco_waiting_for_can_send_now) return; 3961701e3307SMatthias Ringwald if (hci_can_send_sco_packet_now()){ 39623bc639ceSMatthias Ringwald hci_stack->sco_waiting_for_can_send_now = 0; 3963701e3307SMatthias Ringwald uint8_t event[2] = { HCI_EVENT_SCO_CAN_SEND_NOW, 0 }; 3964701e3307SMatthias Ringwald hci_dump_packet(HCI_EVENT_PACKET, 1, event, sizeof(event)); 39653d50b4baSMatthias Ringwald hci_stack->sco_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event)); 39663bc639ceSMatthias Ringwald } 39673bc639ceSMatthias Ringwald } 39681cfb383eSMatthias Ringwald 39691cfb383eSMatthias Ringwald // parsing end emitting has been merged to reduce code size 39701cfb383eSMatthias Ringwald static void gap_inquiry_explode(uint8_t * packet){ 3971e4f5dd75SMatthias Ringwald uint8_t event[19+GAP_INQUIRY_MAX_NAME_LEN]; 39721cfb383eSMatthias Ringwald 39731cfb383eSMatthias Ringwald uint8_t * eir_data; 39741cfb383eSMatthias Ringwald ad_context_t context; 39751cfb383eSMatthias Ringwald const uint8_t * name; 39761cfb383eSMatthias Ringwald uint8_t name_len; 39771cfb383eSMatthias Ringwald 39781cfb383eSMatthias Ringwald int event_type = hci_event_packet_get_type(packet); 39791cfb383eSMatthias Ringwald int num_reserved_fields = event_type == HCI_EVENT_INQUIRY_RESULT ? 2 : 1; // 2 for old event, 1 otherwise 39801cfb383eSMatthias Ringwald int num_responses = hci_event_inquiry_result_get_num_responses(packet); 39811cfb383eSMatthias Ringwald 39821cfb383eSMatthias Ringwald // event[1] is set at the end 39831cfb383eSMatthias Ringwald int i; 39841cfb383eSMatthias Ringwald for (i=0; i<num_responses;i++){ 39851cfb383eSMatthias Ringwald memset(event, 0, sizeof(event)); 39861cfb383eSMatthias Ringwald event[0] = GAP_EVENT_INQUIRY_RESULT; 39871cfb383eSMatthias Ringwald uint8_t event_size = 18; // if name is not set by EIR 39881cfb383eSMatthias Ringwald 39891cfb383eSMatthias Ringwald memcpy(&event[2], &packet[3 + i*6], 6); // bd_addr 39901cfb383eSMatthias Ringwald event[8] = packet[3 + num_responses*(6) + i*1]; // page_scan_repetition_mode 39911cfb383eSMatthias Ringwald memcpy(&event[9], &packet[3 + num_responses*(6+1+num_reserved_fields) + i*3], 3); // class of device 39921cfb383eSMatthias Ringwald memcpy(&event[12], &packet[3 + num_responses*(6+1+num_reserved_fields+3) + i*2], 2); // clock offset 39931cfb383eSMatthias Ringwald 39941cfb383eSMatthias Ringwald switch (event_type){ 39951cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 39961cfb383eSMatthias Ringwald // 14,15,16,17 = 0, size 18 39971cfb383eSMatthias Ringwald break; 39981cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 39991cfb383eSMatthias Ringwald event[14] = 1; 40001cfb383eSMatthias Ringwald event[15] = packet [3 + num_responses*(6+1+num_reserved_fields+3+2) + i*1]; // rssi 40011cfb383eSMatthias Ringwald // 16,17 = 0, size 18 40021cfb383eSMatthias Ringwald break; 40031cfb383eSMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 40041cfb383eSMatthias Ringwald event[14] = 1; 40051cfb383eSMatthias Ringwald event[15] = packet [3 + num_responses*(6+1+num_reserved_fields+3+2) + i*1]; // rssi 40061cfb383eSMatthias Ringwald // for EIR packets, there is only one reponse in it 40071cfb383eSMatthias Ringwald eir_data = &packet[3 + (6+1+num_reserved_fields+3+2+1)]; 40081cfb383eSMatthias Ringwald name = NULL; 40091cfb383eSMatthias Ringwald // EIR data is 240 bytes in EIR event 40101cfb383eSMatthias Ringwald for (ad_iterator_init(&context, 240, eir_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)){ 40111cfb383eSMatthias Ringwald uint8_t data_type = ad_iterator_get_data_type(&context); 40121cfb383eSMatthias Ringwald uint8_t data_size = ad_iterator_get_data_len(&context); 40131cfb383eSMatthias Ringwald const uint8_t * data = ad_iterator_get_data(&context); 40141cfb383eSMatthias Ringwald // Prefer Complete Local Name over Shortend Local Name 40151cfb383eSMatthias Ringwald switch (data_type){ 40161cfb383eSMatthias Ringwald case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME: 40171cfb383eSMatthias Ringwald if (name) continue; 40181cfb383eSMatthias Ringwald /* explicit fall-through */ 40191cfb383eSMatthias Ringwald case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME: 40201cfb383eSMatthias Ringwald name = data; 40211cfb383eSMatthias Ringwald name_len = data_size; 40221cfb383eSMatthias Ringwald break; 40231cfb383eSMatthias Ringwald default: 40241cfb383eSMatthias Ringwald break; 40251cfb383eSMatthias Ringwald } 40261cfb383eSMatthias Ringwald } 40271cfb383eSMatthias Ringwald if (name){ 40281cfb383eSMatthias Ringwald event[16] = 1; 40291cfb383eSMatthias Ringwald // truncate name if needed 40301cfb383eSMatthias Ringwald int len = btstack_min(name_len, GAP_INQUIRY_MAX_NAME_LEN); 40311cfb383eSMatthias Ringwald event[17] = len; 40321cfb383eSMatthias Ringwald memcpy(&event[18], name, len); 40331cfb383eSMatthias Ringwald event_size += len; 40341cfb383eSMatthias Ringwald } 40351cfb383eSMatthias Ringwald break; 40361cfb383eSMatthias Ringwald } 40371cfb383eSMatthias Ringwald event[1] = event_size - 2; 40381cfb383eSMatthias Ringwald hci_emit_event(event, event_size, 1); 40391cfb383eSMatthias Ringwald } 40401cfb383eSMatthias Ringwald } 404135454696SMatthias Ringwald #endif 40423bc639ceSMatthias Ringwald 404371de195eSMatthias Ringwald void hci_emit_state(void){ 40443a9fb326S[email protected] log_info("BTSTACK_EVENT_STATE %u", hci_stack->state); 4045425d1371Smatthias.ringwald uint8_t event[3]; 404680d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 4047425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 40483a9fb326S[email protected] event[2] = hci_stack->state; 4049d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 4050c8e4258aSmatthias.ringwald } 4051c8e4258aSmatthias.ringwald 405235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 40532deddeceSMatthias Ringwald static void hci_emit_connection_complete(bd_addr_t address, hci_con_handle_t con_handle, uint8_t status){ 4054425d1371Smatthias.ringwald uint8_t event[13]; 4055c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 4056425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 405717f1ba2aSmatthias.ringwald event[2] = status; 40582deddeceSMatthias Ringwald little_endian_store_16(event, 3, con_handle); 40592deddeceSMatthias Ringwald reverse_bd_addr(address, &event[5]); 4060c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 4061c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 4062d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 4063c8e4258aSmatthias.ringwald } 406452db98b2SMatthias Ringwald static void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 406552db98b2SMatthias Ringwald if (disable_l2cap_timeouts) return; 406652db98b2SMatthias Ringwald log_info("L2CAP_EVENT_TIMEOUT_CHECK"); 406752db98b2SMatthias Ringwald uint8_t event[4]; 406852db98b2SMatthias Ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 406952db98b2SMatthias Ringwald event[1] = sizeof(event) - 2; 407052db98b2SMatthias Ringwald little_endian_store_16(event, 2, conn->con_handle); 407152db98b2SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 407252db98b2SMatthias Ringwald } 407335454696SMatthias Ringwald #endif 4074c8e4258aSmatthias.ringwald 407535454696SMatthias Ringwald #ifdef ENABLE_BLE 4076d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4077fc64f94aSMatthias 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){ 40784f3229d8S[email protected] uint8_t event[21]; 40794f3229d8S[email protected] event[0] = HCI_EVENT_LE_META; 40804f3229d8S[email protected] event[1] = sizeof(event) - 2; 40814f3229d8S[email protected] event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE; 40824f3229d8S[email protected] event[3] = status; 4083fc64f94aSMatthias Ringwald little_endian_store_16(event, 4, con_handle); 40844f3229d8S[email protected] event[6] = 0; // TODO: role 40856e2e9a6bS[email protected] event[7] = address_type; 4086724d70a2SMatthias Ringwald reverse_bd_addr(address, &event[8]); 4087f8fbdce0SMatthias Ringwald little_endian_store_16(event, 14, 0); // interval 4088f8fbdce0SMatthias Ringwald little_endian_store_16(event, 16, 0); // latency 4089f8fbdce0SMatthias Ringwald little_endian_store_16(event, 18, 0); // supervision timeout 40904f3229d8S[email protected] event[20] = 0; // master clock accuracy 4091d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 40924f3229d8S[email protected] } 409335454696SMatthias Ringwald #endif 4094d70217a2SMatthias Ringwald #endif 40954f3229d8S[email protected] 4096fd43c0e0SMatthias Ringwald static void hci_emit_transport_packet_sent(void){ 4097fd43c0e0SMatthias Ringwald // notify upper stack that it might be possible to send again 4098fd43c0e0SMatthias Ringwald uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; 4099fd43c0e0SMatthias Ringwald hci_emit_event(&event[0], sizeof(event), 0); // don't dump 4100fd43c0e0SMatthias Ringwald } 4101fd43c0e0SMatthias Ringwald 4102fc64f94aSMatthias Ringwald static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason){ 4103425d1371Smatthias.ringwald uint8_t event[6]; 41043c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 4105e518c4b8Smatthias.ringwald event[1] = sizeof(event) - 2; 41063c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 4107fc64f94aSMatthias Ringwald little_endian_store_16(event, 3, con_handle); 41083c4d4b90Smatthias.ringwald event[5] = reason; 4109d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 41103c4d4b90Smatthias.ringwald } 41113c4d4b90Smatthias.ringwald 4112b83d5eabSMatthias Ringwald static void hci_emit_nr_connections_changed(void){ 4113e0abb8e7S[email protected] log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections()); 4114425d1371Smatthias.ringwald uint8_t event[3]; 411580d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 4116425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 411743bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 4118d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 411943bfb1bdSmatthias.ringwald } 4120038bc64cSmatthias.ringwald 4121b83d5eabSMatthias Ringwald static void hci_emit_hci_open_failed(void){ 4122e0abb8e7S[email protected] log_info("BTSTACK_EVENT_POWERON_FAILED"); 4123425d1371Smatthias.ringwald uint8_t event[2]; 412480d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 4125425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 4126d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 4127038bc64cSmatthias.ringwald } 41281b0e3922Smatthias.ringwald 412935454696SMatthias Ringwald static void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status){ 413035454696SMatthias Ringwald log_info("hci_emit_dedicated_bonding_result %u ", status); 413135454696SMatthias Ringwald uint8_t event[9]; 413235454696SMatthias Ringwald int pos = 0; 413335454696SMatthias Ringwald event[pos++] = GAP_EVENT_DEDICATED_BONDING_COMPLETED; 413435454696SMatthias Ringwald event[pos++] = sizeof(event) - 2; 413535454696SMatthias Ringwald event[pos++] = status; 413635454696SMatthias Ringwald reverse_bd_addr(address, &event[pos]); 4137d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 4138381fbed8Smatthias.ringwald } 4139458bf4e8S[email protected] 414035454696SMatthias Ringwald 414135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 414235454696SMatthias Ringwald 4143b83d5eabSMatthias Ringwald static void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){ 4144df3354fcS[email protected] log_info("hci_emit_security_level %u for handle %x", level, con_handle); 4145a00031e2S[email protected] uint8_t event[5]; 4146e00caf9cS[email protected] int pos = 0; 41475611a760SMatthias Ringwald event[pos++] = GAP_EVENT_SECURITY_LEVEL; 4148e00caf9cS[email protected] event[pos++] = sizeof(event) - 2; 4149f8fbdce0SMatthias Ringwald little_endian_store_16(event, 2, con_handle); 4150e00caf9cS[email protected] pos += 2; 4151e00caf9cS[email protected] event[pos++] = level; 4152d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 4153e00caf9cS[email protected] } 4154e00caf9cS[email protected] 415535454696SMatthias Ringwald static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){ 415635454696SMatthias Ringwald if (!connection) return LEVEL_0; 415735454696SMatthias Ringwald if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0; 415835454696SMatthias Ringwald return gap_security_level_for_link_key_type(connection->link_key_type); 415935454696SMatthias Ringwald } 416035454696SMatthias Ringwald 416135454696SMatthias Ringwald static void hci_emit_discoverable_enabled(uint8_t enabled){ 416235454696SMatthias Ringwald log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled); 416335454696SMatthias Ringwald uint8_t event[3]; 416435454696SMatthias Ringwald event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED; 416535454696SMatthias Ringwald event[1] = sizeof(event) - 2; 416635454696SMatthias Ringwald event[2] = enabled; 4167d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 4168ad83dc6aS[email protected] } 4169ad83dc6aS[email protected] 417006b9e820SMatthias Ringwald #ifdef ENABLE_CLASSIC 417198a2fd1cSMatthias Ringwald // query if remote side supports eSCO 4172073bd0faSMatthias Ringwald int hci_remote_esco_supported(hci_con_handle_t con_handle){ 417398a2fd1cSMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 417498a2fd1cSMatthias Ringwald if (!connection) return 0; 417598a2fd1cSMatthias Ringwald return connection->remote_supported_feature_eSCO; 417698a2fd1cSMatthias Ringwald } 417798a2fd1cSMatthias Ringwald 41782bd8b7e7S[email protected] // query if remote side supports SSP 41792bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){ 41802bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 41812bd8b7e7S[email protected] if (!connection) return 0; 41822bd8b7e7S[email protected] return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0; 41832bd8b7e7S[email protected] } 41842bd8b7e7S[email protected] 418515a95bd5SMatthias Ringwald int gap_ssp_supported_on_both_sides(hci_con_handle_t handle){ 4186df3354fcS[email protected] return hci_local_ssp_activated() && hci_remote_ssp_supported(handle); 4187df3354fcS[email protected] } 418806b9e820SMatthias Ringwald #endif 4189df3354fcS[email protected] 4190458bf4e8S[email protected] // GAP API 4191458bf4e8S[email protected] /** 4192458bf4e8S[email protected] * @bbrief enable/disable bonding. default is enabled 4193458bf4e8S[email protected] * @praram enabled 4194458bf4e8S[email protected] */ 41954c57c146S[email protected] void gap_set_bondable_mode(int enable){ 41963a9fb326S[email protected] hci_stack->bondable = enable ? 1 : 0; 4197458bf4e8S[email protected] } 41984ef6443cSMatthias Ringwald /** 41994ef6443cSMatthias Ringwald * @brief Get bondable mode. 42004ef6443cSMatthias Ringwald * @return 1 if bondable 42014ef6443cSMatthias Ringwald */ 42024ef6443cSMatthias Ringwald int gap_get_bondable_mode(void){ 42034ef6443cSMatthias Ringwald return hci_stack->bondable; 42044ef6443cSMatthias Ringwald } 4205cb230b9dS[email protected] 4206cb230b9dS[email protected] /** 420734d2123cS[email protected] * @brief map link keys to security levels 4208cb230b9dS[email protected] */ 420934d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){ 421034d2123cS[email protected] switch (link_key_type){ 42113c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 42123c68dfa9S[email protected] return LEVEL_4; 42133c68dfa9S[email protected] case COMBINATION_KEY: 42143c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 42153c68dfa9S[email protected] return LEVEL_3; 42163c68dfa9S[email protected] default: 42173c68dfa9S[email protected] return LEVEL_2; 42183c68dfa9S[email protected] } 4219cb230b9dS[email protected] } 4220cb230b9dS[email protected] 4221106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){ 42225127cc62S[email protected] log_info("gap_mitm_protection_required_for_security_level %u", level); 4223106d6d11S[email protected] return level > LEVEL_2; 4224106d6d11S[email protected] } 4225106d6d11S[email protected] 422634d2123cS[email protected] /** 422734d2123cS[email protected] * @brief get current security level 422834d2123cS[email protected] */ 422934d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){ 423034d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 423134d2123cS[email protected] if (!connection) return LEVEL_0; 423234d2123cS[email protected] return gap_security_level_for_connection(connection); 423334d2123cS[email protected] } 423434d2123cS[email protected] 4235cb230b9dS[email protected] /** 4236cb230b9dS[email protected] * @brief request connection to device to 4237cb230b9dS[email protected] * @result GAP_AUTHENTICATION_RESULT 4238cb230b9dS[email protected] */ 423934d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){ 424034d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 424134d2123cS[email protected] if (!connection){ 4242a00031e2S[email protected] hci_emit_security_level(con_handle, LEVEL_0); 424334d2123cS[email protected] return; 424434d2123cS[email protected] } 424534d2123cS[email protected] gap_security_level_t current_level = gap_security_level(con_handle); 424683d08d7cSMatthias Ringwald log_info("gap_request_security_level requested level %u, planned level %u, current level %u", 424783d08d7cSMatthias Ringwald requested_level, connection->requested_security_level, current_level); 424883d08d7cSMatthias Ringwald 424983d08d7cSMatthias Ringwald // assumption: earlier requested security higher than current level => security request is active 425083d08d7cSMatthias Ringwald if (current_level < connection->requested_security_level){ 425183d08d7cSMatthias Ringwald if (connection->requested_security_level < requested_level){ 425283d08d7cSMatthias Ringwald // increase requested level as new level is higher 425383d08d7cSMatthias Ringwald 425483d08d7cSMatthias Ringwald // TODO: handle re-authentication when done 425583d08d7cSMatthias Ringwald 425683d08d7cSMatthias Ringwald connection->requested_security_level = requested_level; 425783d08d7cSMatthias Ringwald } 425883d08d7cSMatthias Ringwald return; 425983d08d7cSMatthias Ringwald } 426083d08d7cSMatthias Ringwald 426183d08d7cSMatthias Ringwald // no request active, notify if security sufficient 426283d08d7cSMatthias Ringwald if (requested_level <= current_level){ 4263a00031e2S[email protected] hci_emit_security_level(con_handle, current_level); 426434d2123cS[email protected] return; 426534d2123cS[email protected] } 4266a00031e2S[email protected] 426783d08d7cSMatthias Ringwald // start pairing to increase security level 426834d2123cS[email protected] connection->requested_security_level = requested_level; 4269a00031e2S[email protected] 427025bf5872S[email protected] #if 0 427125bf5872S[email protected] // sending encryption request without a link key results in an error. 427225bf5872S[email protected] // TODO: figure out how to use it properly 427325bf5872S[email protected] 4274fb8ba0dbS[email protected] // would enabling ecnryption suffice (>= LEVEL_2)? 4275a98592bcSMatthias Ringwald if (hci_stack->link_key_db){ 4276a00031e2S[email protected] link_key_type_t link_key_type; 4277a00031e2S[email protected] link_key_t link_key; 4278a98592bcSMatthias Ringwald if (hci_stack->link_key_db->get_link_key( &connection->address, &link_key, &link_key_type)){ 4279a00031e2S[email protected] if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){ 4280a00031e2S[email protected] connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 4281a00031e2S[email protected] return; 4282a00031e2S[email protected] } 4283a00031e2S[email protected] } 4284a00031e2S[email protected] } 428525bf5872S[email protected] #endif 4286a00031e2S[email protected] 428783d08d7cSMatthias Ringwald // start to authenticate connection 42881eb2563eS[email protected] connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 4289e80b2cf9S[email protected] hci_run(); 4290e00caf9cS[email protected] } 4291ad83dc6aS[email protected] 4292ad83dc6aS[email protected] /** 4293ad83dc6aS[email protected] * @brief start dedicated bonding with device. disconnect after bonding 4294ad83dc6aS[email protected] * @param device 4295ad83dc6aS[email protected] * @param request MITM protection 4296ad83dc6aS[email protected] * @result GAP_DEDICATED_BONDING_COMPLETE 4297ad83dc6aS[email protected] */ 4298ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){ 4299ad83dc6aS[email protected] 4300ad83dc6aS[email protected] // create connection state machine 430196a45072S[email protected] hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_CLASSIC); 4302ad83dc6aS[email protected] 4303ad83dc6aS[email protected] if (!connection){ 4304ad83dc6aS[email protected] return BTSTACK_MEMORY_ALLOC_FAILED; 4305ad83dc6aS[email protected] } 4306ad83dc6aS[email protected] 4307ad83dc6aS[email protected] // delete linkn key 430815a95bd5SMatthias Ringwald gap_drop_link_key_for_bd_addr(device); 4309ad83dc6aS[email protected] 4310ad83dc6aS[email protected] // configure LEVEL_2/3, dedicated bonding 4311ad83dc6aS[email protected] connection->state = SEND_CREATE_CONNECTION; 4312ad83dc6aS[email protected] connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2; 4313f04a0c31SMatthias Ringwald log_info("gap_dedicated_bonding, mitm %d -> level %u", mitm_protection_required, connection->requested_security_level); 4314ad83dc6aS[email protected] connection->bonding_flags = BONDING_DEDICATED; 4315ad83dc6aS[email protected] 4316ad83dc6aS[email protected] // wait for GAP Security Result and send GAP Dedicated Bonding complete 4317ad83dc6aS[email protected] 4318ad83dc6aS[email protected] // handle: connnection failure (connection complete != ok) 4319ad83dc6aS[email protected] // handle: authentication failure 4320ad83dc6aS[email protected] // handle: disconnect on done 4321ad83dc6aS[email protected] 4322ad83dc6aS[email protected] hci_run(); 4323ad83dc6aS[email protected] 4324ad83dc6aS[email protected] return 0; 4325ad83dc6aS[email protected] } 432635454696SMatthias Ringwald #endif 43278e618f72S[email protected] 43288e618f72S[email protected] void gap_set_local_name(const char * local_name){ 43298e618f72S[email protected] hci_stack->local_name = local_name; 43308e618f72S[email protected] } 43318e618f72S[email protected] 433235454696SMatthias Ringwald 433335454696SMatthias Ringwald #ifdef ENABLE_BLE 433435454696SMatthias Ringwald 4335d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4336d8e8f12aSMatthias Ringwald void gap_start_scan(void){ 43376fab74dbSMatthias Ringwald hci_stack->le_scanning_enabled = 1; 43387bdc6798S[email protected] hci_run(); 43397bdc6798S[email protected] } 43408e618f72S[email protected] 4341d8e8f12aSMatthias Ringwald void gap_stop_scan(void){ 43426fab74dbSMatthias Ringwald hci_stack->le_scanning_enabled = 0; 43437bdc6798S[email protected] hci_run(); 43447bdc6798S[email protected] } 43454f3229d8S[email protected] 4346d8e8f12aSMatthias Ringwald void gap_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){ 4347ef11999fSmatthias.ringwald hci_stack->le_scan_type = scan_type; 4348ef11999fSmatthias.ringwald hci_stack->le_scan_interval = scan_interval; 4349ef11999fSmatthias.ringwald hci_stack->le_scan_window = scan_window; 4350ef11999fSmatthias.ringwald hci_run(); 4351ef11999fSmatthias.ringwald } 43524f3229d8S[email protected] 4353d8e8f12aSMatthias Ringwald uint8_t gap_connect(bd_addr_t addr, bd_addr_type_t addr_type){ 43544f3229d8S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 43554f3229d8S[email protected] if (!conn){ 4356d8e8f12aSMatthias Ringwald log_info("gap_connect: no connection exists yet, creating context"); 43572e77e513S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 43584f3229d8S[email protected] if (!conn){ 43594f3229d8S[email protected] // notify client that alloc failed 43606e2e9a6bS[email protected] hci_emit_le_connection_complete(addr_type, addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 4361d8e8f12aSMatthias Ringwald log_info("gap_connect: failed to alloc hci_connection_t"); 4362472a5742SMatthias Ringwald return GATT_CLIENT_NOT_CONNECTED; // don't sent packet to controller 43634f3229d8S[email protected] } 43644f3229d8S[email protected] conn->state = SEND_CREATE_CONNECTION; 4365d8e8f12aSMatthias Ringwald log_info("gap_connect: send create connection next"); 4366564fca32S[email protected] hci_run(); 4367616edd56SMatthias Ringwald return 0; 43684f3229d8S[email protected] } 43690bf6344aS[email protected] 43700bf6344aS[email protected] if (!hci_is_le_connection(conn) || 43710bf6344aS[email protected] conn->state == SEND_CREATE_CONNECTION || 43720bf6344aS[email protected] conn->state == SENT_CREATE_CONNECTION) { 43732e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_COMMAND_DISALLOWED); 4374d8e8f12aSMatthias Ringwald log_error("gap_connect: classic connection or connect is already being created"); 4375616edd56SMatthias Ringwald return GATT_CLIENT_IN_WRONG_STATE; 43760bf6344aS[email protected] } 43770bf6344aS[email protected] 4378d8e8f12aSMatthias Ringwald log_info("gap_connect: context exists with state %u", conn->state); 43792e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, conn->con_handle, 0); 43804f3229d8S[email protected] hci_run(); 4381616edd56SMatthias Ringwald return 0; 43824f3229d8S[email protected] } 43834f3229d8S[email protected] 43847851196eSmatthias.ringwald // @assumption: only a single outgoing LE Connection exists 4385d8e8f12aSMatthias Ringwald static hci_connection_t * gap_get_outgoing_connection(void){ 4386665d90f2SMatthias Ringwald btstack_linked_item_t *it; 4387665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 43880bf6344aS[email protected] hci_connection_t * conn = (hci_connection_t *) it; 43890bf6344aS[email protected] if (!hci_is_le_connection(conn)) continue; 43900bf6344aS[email protected] switch (conn->state){ 4391a6725849S[email protected] case SEND_CREATE_CONNECTION: 43927851196eSmatthias.ringwald case SENT_CREATE_CONNECTION: 43939c77c9dbSMatthias Ringwald case SENT_CANCEL_CONNECTION: 43947851196eSmatthias.ringwald return conn; 43957851196eSmatthias.ringwald default: 43967851196eSmatthias.ringwald break; 43977851196eSmatthias.ringwald }; 43987851196eSmatthias.ringwald } 43997851196eSmatthias.ringwald return NULL; 44007851196eSmatthias.ringwald } 44017851196eSmatthias.ringwald 4402d8e8f12aSMatthias Ringwald uint8_t gap_connect_cancel(void){ 4403d8e8f12aSMatthias Ringwald hci_connection_t * conn = gap_get_outgoing_connection(); 4404616edd56SMatthias Ringwald if (!conn) return 0; 44057851196eSmatthias.ringwald switch (conn->state){ 44067851196eSmatthias.ringwald case SEND_CREATE_CONNECTION: 44077851196eSmatthias.ringwald // skip sending create connection and emit event instead 44082e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER); 4409665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 44107851196eSmatthias.ringwald btstack_memory_hci_connection_free( conn ); 44110bf6344aS[email protected] break; 4412a6725849S[email protected] case SENT_CREATE_CONNECTION: 44137851196eSmatthias.ringwald // request to send cancel connection 44140bf6344aS[email protected] conn->state = SEND_CANCEL_CONNECTION; 44150bf6344aS[email protected] hci_run(); 44160bf6344aS[email protected] break; 44170bf6344aS[email protected] default: 44180bf6344aS[email protected] break; 44190bf6344aS[email protected] } 4420616edd56SMatthias Ringwald return 0; 4421e31f89a7S[email protected] } 4422d70217a2SMatthias Ringwald #endif 44234f3229d8S[email protected] 442413e645d2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4425c37a3166S[email protected] /** 44266012052bSMatthias Ringwald * @brief Set connection parameters for outgoing connections 4427cbe54ab2SJakob Krantz * @param conn_scan_interval (unit: 0.625 msec), default: 60 ms 4428cbe54ab2SJakob Krantz * @param conn_scan_window (unit: 0.625 msec), default: 30 ms 44296012052bSMatthias Ringwald * @param conn_interval_min (unit: 1.25ms), default: 10 ms 44306012052bSMatthias Ringwald * @param conn_interval_max (unit: 1.25ms), default: 30 ms 44316012052bSMatthias Ringwald * @param conn_latency, default: 4 44326012052bSMatthias Ringwald * @param supervision_timeout (unit: 10ms), default: 720 ms 44336012052bSMatthias Ringwald * @param min_ce_length (unit: 0.625ms), default: 10 ms 44346012052bSMatthias Ringwald * @param max_ce_length (unit: 0.625ms), default: 30 ms 44356012052bSMatthias Ringwald */ 44366012052bSMatthias Ringwald 4437cbe54ab2SJakob Krantz void gap_set_connection_parameters(uint16_t conn_scan_interval, uint16_t conn_scan_window, 4438cbe54ab2SJakob Krantz uint16_t conn_interval_min, uint16_t conn_interval_max, uint16_t conn_latency, 4439cbe54ab2SJakob Krantz uint16_t supervision_timeout, uint16_t min_ce_length, uint16_t max_ce_length){ 4440cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval = conn_scan_interval; 4441cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window = conn_scan_window; 44426012052bSMatthias Ringwald hci_stack->le_connection_interval_min = conn_interval_min; 44436012052bSMatthias Ringwald hci_stack->le_connection_interval_max = conn_interval_max; 44446012052bSMatthias Ringwald hci_stack->le_connection_latency = conn_latency; 44456012052bSMatthias Ringwald hci_stack->le_supervision_timeout = supervision_timeout; 44466012052bSMatthias Ringwald hci_stack->le_minimum_ce_length = min_ce_length; 44476012052bSMatthias Ringwald hci_stack->le_maximum_ce_length = max_ce_length; 44486012052bSMatthias Ringwald } 444913e645d2SMatthias Ringwald #endif 44506012052bSMatthias Ringwald 44516012052bSMatthias Ringwald /** 4452c37a3166S[email protected] * @brief Updates the connection parameters for a given LE connection 4453c37a3166S[email protected] * @param handle 4454c37a3166S[email protected] * @param conn_interval_min (unit: 1.25ms) 4455c37a3166S[email protected] * @param conn_interval_max (unit: 1.25ms) 4456c37a3166S[email protected] * @param conn_latency 4457c37a3166S[email protected] * @param supervision_timeout (unit: 10ms) 4458c37a3166S[email protected] * @returns 0 if ok 4459c37a3166S[email protected] */ 4460c37a3166S[email protected] int gap_update_connection_parameters(hci_con_handle_t con_handle, uint16_t conn_interval_min, 4461c37a3166S[email protected] uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 4462c37a3166S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 4463c37a3166S[email protected] if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 4464c37a3166S[email protected] connection->le_conn_interval_min = conn_interval_min; 4465c37a3166S[email protected] connection->le_conn_interval_max = conn_interval_max; 4466c37a3166S[email protected] connection->le_conn_latency = conn_latency; 4467c37a3166S[email protected] connection->le_supervision_timeout = supervision_timeout; 446884cf6d83SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS; 4469cfc59f1bSMatthias Ringwald hci_run(); 4470c37a3166S[email protected] return 0; 4471c37a3166S[email protected] } 4472c37a3166S[email protected] 447345c102fdSMatthias Ringwald /** 4474b68d7bc3SMatthias Ringwald * @brief Request an update of the connection parameter for a given LE connection 4475b68d7bc3SMatthias Ringwald * @param handle 4476b68d7bc3SMatthias Ringwald * @param conn_interval_min (unit: 1.25ms) 4477b68d7bc3SMatthias Ringwald * @param conn_interval_max (unit: 1.25ms) 4478b68d7bc3SMatthias Ringwald * @param conn_latency 4479b68d7bc3SMatthias Ringwald * @param supervision_timeout (unit: 10ms) 4480b68d7bc3SMatthias Ringwald * @returns 0 if ok 4481b68d7bc3SMatthias Ringwald */ 4482b68d7bc3SMatthias Ringwald int gap_request_connection_parameter_update(hci_con_handle_t con_handle, uint16_t conn_interval_min, 4483b68d7bc3SMatthias Ringwald uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 4484b68d7bc3SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 4485b68d7bc3SMatthias Ringwald if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 4486b68d7bc3SMatthias Ringwald connection->le_conn_interval_min = conn_interval_min; 4487b68d7bc3SMatthias Ringwald connection->le_conn_interval_max = conn_interval_max; 4488b68d7bc3SMatthias Ringwald connection->le_conn_latency = conn_latency; 4489b68d7bc3SMatthias Ringwald connection->le_supervision_timeout = supervision_timeout; 4490b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_REQUEST; 4491b68d7bc3SMatthias Ringwald hci_run(); 4492b68d7bc3SMatthias Ringwald return 0; 4493b68d7bc3SMatthias Ringwald } 4494b68d7bc3SMatthias Ringwald 4495d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 4496d70217a2SMatthias Ringwald 4497501f56b3SMatthias Ringwald static void gap_advertisments_changed(void){ 4498501f56b3SMatthias Ringwald // disable advertisements before updating adv, scan data, or adv params 4499501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_active){ 4500501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_DISABLE | LE_ADVERTISEMENT_TASKS_ENABLE; 4501501f56b3SMatthias Ringwald } 4502501f56b3SMatthias Ringwald hci_run(); 4503501f56b3SMatthias Ringwald } 4504501f56b3SMatthias Ringwald 4505b68d7bc3SMatthias Ringwald /** 450645c102fdSMatthias Ringwald * @brief Set Advertisement Data 450745c102fdSMatthias Ringwald * @param advertising_data_length 450845c102fdSMatthias Ringwald * @param advertising_data (max 31 octets) 450945c102fdSMatthias Ringwald * @note data is not copied, pointer has to stay valid 451045c102fdSMatthias Ringwald */ 451145c102fdSMatthias Ringwald void gap_advertisements_set_data(uint8_t advertising_data_length, uint8_t * advertising_data){ 451245c102fdSMatthias Ringwald hci_stack->le_advertisements_data_len = advertising_data_length; 451345c102fdSMatthias Ringwald hci_stack->le_advertisements_data = advertising_data; 4514501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 4515501f56b3SMatthias Ringwald gap_advertisments_changed(); 451645c102fdSMatthias Ringwald } 4517501f56b3SMatthias Ringwald 4518501f56b3SMatthias Ringwald /** 4519501f56b3SMatthias Ringwald * @brief Set Scan Response Data 4520501f56b3SMatthias Ringwald * @param advertising_data_length 4521501f56b3SMatthias Ringwald * @param advertising_data (max 31 octets) 4522501f56b3SMatthias Ringwald * @note data is not copied, pointer has to stay valid 4523501f56b3SMatthias Ringwald */ 4524501f56b3SMatthias Ringwald void gap_scan_response_set_data(uint8_t scan_response_data_length, uint8_t * scan_response_data){ 4525501f56b3SMatthias Ringwald hci_stack->le_scan_response_data_len = scan_response_data_length; 4526501f56b3SMatthias Ringwald hci_stack->le_scan_response_data = scan_response_data; 4527501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 4528501f56b3SMatthias Ringwald gap_advertisments_changed(); 452945c102fdSMatthias Ringwald } 453045c102fdSMatthias Ringwald 453145c102fdSMatthias Ringwald /** 453245c102fdSMatthias Ringwald * @brief Set Advertisement Parameters 453345c102fdSMatthias Ringwald * @param adv_int_min 453445c102fdSMatthias Ringwald * @param adv_int_max 453545c102fdSMatthias Ringwald * @param adv_type 453645c102fdSMatthias Ringwald * @param direct_address_type 453745c102fdSMatthias Ringwald * @param direct_address 453845c102fdSMatthias Ringwald * @param channel_map 453945c102fdSMatthias Ringwald * @param filter_policy 454045c102fdSMatthias Ringwald * 454145c102fdSMatthias Ringwald * @note internal use. use gap_advertisements_set_params from gap_le.h instead. 454245c102fdSMatthias Ringwald */ 454345c102fdSMatthias Ringwald void hci_le_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type, 4544b95a5a35SMatthias Ringwald uint8_t direct_address_typ, bd_addr_t direct_address, 454545c102fdSMatthias Ringwald uint8_t channel_map, uint8_t filter_policy) { 454645c102fdSMatthias Ringwald 454745c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_min = adv_int_min; 454845c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_max = adv_int_max; 454945c102fdSMatthias Ringwald hci_stack->le_advertisements_type = adv_type; 455045c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address_type = direct_address_typ; 455145c102fdSMatthias Ringwald hci_stack->le_advertisements_channel_map = channel_map; 455245c102fdSMatthias Ringwald hci_stack->le_advertisements_filter_policy = filter_policy; 455345c102fdSMatthias Ringwald memcpy(hci_stack->le_advertisements_direct_address, direct_address, 6); 455445c102fdSMatthias Ringwald 455545c102fdSMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 4556501f56b3SMatthias Ringwald gap_advertisments_changed(); 455745c102fdSMatthias Ringwald } 455845c102fdSMatthias Ringwald 455945c102fdSMatthias Ringwald /** 456045c102fdSMatthias Ringwald * @brief Enable/Disable Advertisements 456145c102fdSMatthias Ringwald * @param enabled 456245c102fdSMatthias Ringwald */ 456345c102fdSMatthias Ringwald void gap_advertisements_enable(int enabled){ 456445c102fdSMatthias Ringwald hci_stack->le_advertisements_enabled = enabled; 456545c102fdSMatthias Ringwald if (enabled && !hci_stack->le_advertisements_active){ 456645c102fdSMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_ENABLE; 456745c102fdSMatthias Ringwald } 456845c102fdSMatthias Ringwald if (!enabled && hci_stack->le_advertisements_active){ 456945c102fdSMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_DISABLE; 457045c102fdSMatthias Ringwald } 4571cfc59f1bSMatthias Ringwald hci_run(); 457245c102fdSMatthias Ringwald } 457345c102fdSMatthias Ringwald 457435454696SMatthias Ringwald #endif 457506e5cf96SMatthias Ringwald 457606e5cf96SMatthias Ringwald void hci_le_set_own_address_type(uint8_t own_address_type){ 457706e5cf96SMatthias Ringwald log_info("hci_le_set_own_address_type: old %u, new %u", hci_stack->le_own_addr_type, own_address_type); 457806e5cf96SMatthias Ringwald if (own_address_type == hci_stack->le_own_addr_type) return; 457906e5cf96SMatthias Ringwald hci_stack->le_own_addr_type = own_address_type; 458006e5cf96SMatthias Ringwald 458164068776SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 458206e5cf96SMatthias Ringwald // update advertisement parameters, too 458306e5cf96SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 458406e5cf96SMatthias Ringwald gap_advertisments_changed(); 458564068776SMatthias Ringwald #endif 458664068776SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 458706e5cf96SMatthias Ringwald // note: we don't update scan parameters or modify ongoing connection attempts 458864068776SMatthias Ringwald #endif 458906e5cf96SMatthias Ringwald } 459006e5cf96SMatthias Ringwald 4591d70217a2SMatthias Ringwald #endif 459245c102fdSMatthias Ringwald 4593616edd56SMatthias Ringwald uint8_t gap_disconnect(hci_con_handle_t handle){ 45945917a5c5S[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 45955917a5c5S[email protected] if (!conn){ 45967851196eSmatthias.ringwald hci_emit_disconnection_complete(handle, 0); 4597616edd56SMatthias Ringwald return 0; 45985917a5c5S[email protected] } 45997fd7aa6fSMatthias Ringwald // ignore if already disconnected 46007fd7aa6fSMatthias Ringwald if (conn->state == RECEIVED_DISCONNECTION_COMPLETE){ 46017fd7aa6fSMatthias Ringwald return 0; 46027fd7aa6fSMatthias Ringwald } 46035917a5c5S[email protected] conn->state = SEND_DISCONNECT; 46045917a5c5S[email protected] hci_run(); 4605616edd56SMatthias Ringwald return 0; 46064f3229d8S[email protected] } 460704a6ef8cSmatthias.ringwald 4608a1bf5ae7SMatthias Ringwald /** 4609a1bf5ae7SMatthias Ringwald * @brief Get connection type 4610a1bf5ae7SMatthias Ringwald * @param con_handle 4611a1bf5ae7SMatthias Ringwald * @result connection_type 4612a1bf5ae7SMatthias Ringwald */ 4613a1bf5ae7SMatthias Ringwald gap_connection_type_t gap_get_connection_type(hci_con_handle_t connection_handle){ 4614a1bf5ae7SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(connection_handle); 4615a1bf5ae7SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 4616a1bf5ae7SMatthias Ringwald switch (conn->address_type){ 4617a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 4618a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 4619a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_LE; 4620a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_SCO: 4621a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_SCO; 4622a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_CLASSIC: 4623a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_ACL; 4624a1bf5ae7SMatthias Ringwald default: 4625a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_INVALID; 4626a1bf5ae7SMatthias Ringwald } 4627a1bf5ae7SMatthias Ringwald } 4628a1bf5ae7SMatthias Ringwald 4629a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 46304f551432SMatthias Ringwald 4631b90f6e0aSMatthias Ringwald uint8_t gap_le_set_phy(hci_con_handle_t connection_handle, uint8_t all_phys, uint8_t tx_phys, uint8_t rx_phys, uint8_t phy_options){ 4632b90f6e0aSMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(connection_handle); 4633b90f6e0aSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 4634b90f6e0aSMatthias Ringwald 4635b90f6e0aSMatthias Ringwald conn->le_phy_update_all_phys = all_phys; 4636b90f6e0aSMatthias Ringwald conn->le_phy_update_tx_phys = tx_phys; 4637b90f6e0aSMatthias Ringwald conn->le_phy_update_rx_phys = rx_phys; 4638b90f6e0aSMatthias Ringwald conn->le_phy_update_phy_options = phy_options; 4639b90f6e0aSMatthias Ringwald 4640b90f6e0aSMatthias Ringwald hci_run(); 4641b90f6e0aSMatthias Ringwald 4642b90f6e0aSMatthias Ringwald return 0; 4643b90f6e0aSMatthias Ringwald } 4644b90f6e0aSMatthias Ringwald 4645d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4646d23838ecSMatthias Ringwald /** 4647ac9c45e0SMatthias Ringwald * @brief Auto Connection Establishment - Start Connecting to device 4648ac9c45e0SMatthias Ringwald * @param address_typ 4649ac9c45e0SMatthias Ringwald * @param address 4650ac9c45e0SMatthias Ringwald * @returns 0 if ok 4651ac9c45e0SMatthias Ringwald */ 46524f551432SMatthias Ringwald int gap_auto_connection_start(bd_addr_type_t address_type, bd_addr_t address){ 4653e83201bcSMatthias Ringwald // check capacity 4654665d90f2SMatthias Ringwald int num_entries = btstack_linked_list_count(&hci_stack->le_whitelist); 465591915b0bSMatthias Ringwald if (num_entries >= hci_stack->le_whitelist_capacity) return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED; 4656e83201bcSMatthias Ringwald whitelist_entry_t * entry = btstack_memory_whitelist_entry_get(); 4657e83201bcSMatthias Ringwald if (!entry) return BTSTACK_MEMORY_ALLOC_FAILED; 4658e83201bcSMatthias Ringwald entry->address_type = address_type; 4659e83201bcSMatthias Ringwald memcpy(entry->address, address, 6); 4660e83201bcSMatthias Ringwald entry->state = LE_WHITELIST_ADD_TO_CONTROLLER; 4661665d90f2SMatthias Ringwald btstack_linked_list_add(&hci_stack->le_whitelist, (btstack_linked_item_t*) entry); 4662e83201bcSMatthias Ringwald hci_run(); 4663e83201bcSMatthias Ringwald return 0; 4664ac9c45e0SMatthias Ringwald } 4665ac9c45e0SMatthias Ringwald 466642ff5ba1SMatthias Ringwald static void hci_remove_from_whitelist(bd_addr_type_t address_type, bd_addr_t address){ 4667665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 4668665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 4669665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 4670665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it); 4671e83201bcSMatthias Ringwald if (entry->address_type != address_type) continue; 4672e83201bcSMatthias Ringwald if (memcmp(entry->address, address, 6) != 0) continue; 4673e83201bcSMatthias Ringwald if (entry->state & LE_WHITELIST_ON_CONTROLLER){ 4674e83201bcSMatthias Ringwald // remove from controller if already present 4675e83201bcSMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER; 4676e83201bcSMatthias Ringwald continue; 4677e83201bcSMatthias Ringwald } 4678e83201bcSMatthias Ringwald // direclty remove entry from whitelist 4679665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 4680e83201bcSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 4681e83201bcSMatthias Ringwald } 468242ff5ba1SMatthias Ringwald } 468342ff5ba1SMatthias Ringwald 468442ff5ba1SMatthias Ringwald /** 468542ff5ba1SMatthias Ringwald * @brief Auto Connection Establishment - Stop Connecting to device 468642ff5ba1SMatthias Ringwald * @param address_typ 468742ff5ba1SMatthias Ringwald * @param address 468842ff5ba1SMatthias Ringwald * @returns 0 if ok 468942ff5ba1SMatthias Ringwald */ 469042ff5ba1SMatthias Ringwald int gap_auto_connection_stop(bd_addr_type_t address_type, bd_addr_t address){ 469142ff5ba1SMatthias Ringwald hci_remove_from_whitelist(address_type, address); 4692e83201bcSMatthias Ringwald hci_run(); 4693e83201bcSMatthias Ringwald return 0; 4694ac9c45e0SMatthias Ringwald } 4695ac9c45e0SMatthias Ringwald 4696ac9c45e0SMatthias Ringwald /** 4697ac9c45e0SMatthias Ringwald * @brief Auto Connection Establishment - Stop everything 4698ac9c45e0SMatthias Ringwald * @note Convenience function to stop all active auto connection attempts 4699ac9c45e0SMatthias Ringwald */ 4700ac9c45e0SMatthias Ringwald void gap_auto_connection_stop_all(void){ 4701665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 4702665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 4703665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 4704665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it); 4705e83201bcSMatthias Ringwald if (entry->state & LE_WHITELIST_ON_CONTROLLER){ 4706e83201bcSMatthias Ringwald // remove from controller if already present 4707e83201bcSMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER; 4708e83201bcSMatthias Ringwald continue; 4709e83201bcSMatthias Ringwald } 471091915b0bSMatthias Ringwald // directly remove entry from whitelist 4711665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 4712e83201bcSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 4713e83201bcSMatthias Ringwald } 4714e83201bcSMatthias Ringwald hci_run(); 4715ac9c45e0SMatthias Ringwald } 4716c9db5c21SMilanka Ringwald 4717c9db5c21SMilanka Ringwald uint16_t gap_le_connection_interval(hci_con_handle_t connection_handle){ 4718c9db5c21SMilanka Ringwald hci_connection_t * conn = hci_connection_for_handle(connection_handle); 4719c9db5c21SMilanka Ringwald if (!conn) return 0; 4720c9db5c21SMilanka Ringwald return conn->le_connection_interval; 4721c9db5c21SMilanka Ringwald } 4722d70217a2SMatthias Ringwald #endif 47234f551432SMatthias Ringwald #endif 47244f551432SMatthias Ringwald 472535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 4726ac9c45e0SMatthias Ringwald /** 4727ff00ed1cSMatthias Ringwald * @brief Set Extended Inquiry Response data 4728ff00ed1cSMatthias Ringwald * @param eir_data size 240 bytes, is not copied make sure memory is accessible during stack startup 4729ff00ed1cSMatthias Ringwald * @note has to be done before stack starts up 4730ff00ed1cSMatthias Ringwald */ 4731ff00ed1cSMatthias Ringwald void gap_set_extended_inquiry_response(const uint8_t * data){ 4732ff00ed1cSMatthias Ringwald hci_stack->eir_data = data; 4733ff00ed1cSMatthias Ringwald } 4734ff00ed1cSMatthias Ringwald 4735ff00ed1cSMatthias Ringwald /** 4736f5875de5SMatthias Ringwald * @brief Start GAP Classic Inquiry 4737f5875de5SMatthias Ringwald * @param duration in 1.28s units 4738f5875de5SMatthias Ringwald * @return 0 if ok 4739f5875de5SMatthias Ringwald * @events: GAP_EVENT_INQUIRY_RESULT, GAP_EVENT_INQUIRY_COMPLETE 4740f5875de5SMatthias Ringwald */ 4741f5875de5SMatthias Ringwald int gap_inquiry_start(uint8_t duration_in_1280ms_units){ 474299449554SMatthias Ringwald if (hci_stack->state != HCI_STATE_WORKING) return ERROR_CODE_COMMAND_DISALLOWED; 4743f5875de5SMatthias Ringwald if (hci_stack->inquiry_state != GAP_INQUIRY_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 4744f5875de5SMatthias Ringwald if (duration_in_1280ms_units < GAP_INQUIRY_DURATION_MIN || duration_in_1280ms_units > GAP_INQUIRY_DURATION_MAX){ 4745f5875de5SMatthias Ringwald return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 4746f5875de5SMatthias Ringwald } 4747f5875de5SMatthias Ringwald hci_stack->inquiry_state = duration_in_1280ms_units; 4748f5875de5SMatthias Ringwald hci_run(); 4749f5875de5SMatthias Ringwald return 0; 4750f5875de5SMatthias Ringwald } 4751f5875de5SMatthias Ringwald 4752f5875de5SMatthias Ringwald /** 4753f5875de5SMatthias Ringwald * @brief Stop GAP Classic Inquiry 4754f5875de5SMatthias Ringwald * @returns 0 if ok 4755f5875de5SMatthias Ringwald */ 4756f5875de5SMatthias Ringwald int gap_inquiry_stop(void){ 47572c123282SMatthias Ringwald if (hci_stack->inquiry_state >= GAP_INQUIRY_DURATION_MIN && hci_stack->inquiry_state <= GAP_INQUIRY_DURATION_MAX) { 4758f5875de5SMatthias Ringwald // emit inquiry complete event, before it even started 4759f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 4760f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 4761f5875de5SMatthias Ringwald return 0; 4762f5875de5SMatthias Ringwald } 4763f5875de5SMatthias Ringwald if (hci_stack->inquiry_state != GAP_INQUIRY_STATE_ACTIVE) return ERROR_CODE_COMMAND_DISALLOWED; 4764f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W2_CANCEL; 4765f5875de5SMatthias Ringwald hci_run(); 4766f5875de5SMatthias Ringwald return 0; 4767f5875de5SMatthias Ringwald } 4768f5875de5SMatthias Ringwald 4769b7f1ee76SMatthias Ringwald 4770b7f1ee76SMatthias Ringwald /** 4771b7f1ee76SMatthias Ringwald * @brief Remote Name Request 4772b7f1ee76SMatthias Ringwald * @param addr 4773b7f1ee76SMatthias Ringwald * @param page_scan_repetition_mode 4774b7f1ee76SMatthias Ringwald * @param clock_offset only used when bit 15 is set 4775b7f1ee76SMatthias Ringwald * @events: HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 4776b7f1ee76SMatthias Ringwald */ 4777b7f1ee76SMatthias Ringwald int gap_remote_name_request(bd_addr_t addr, uint8_t page_scan_repetition_mode, uint16_t clock_offset){ 4778b7f1ee76SMatthias Ringwald if (hci_stack->remote_name_state != GAP_REMOTE_NAME_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 4779b7f1ee76SMatthias Ringwald memcpy(hci_stack->remote_name_addr, addr, 6); 4780b7f1ee76SMatthias Ringwald hci_stack->remote_name_page_scan_repetition_mode = page_scan_repetition_mode; 4781b7f1ee76SMatthias Ringwald hci_stack->remote_name_clock_offset = clock_offset; 4782b7f1ee76SMatthias Ringwald hci_stack->remote_name_state = GAP_REMOTE_NAME_STATE_W2_SEND; 4783b7f1ee76SMatthias Ringwald hci_run(); 4784b7f1ee76SMatthias Ringwald return 0; 4785b7f1ee76SMatthias Ringwald } 4786b7f1ee76SMatthias Ringwald 47870a51f88bSMatthias Ringwald static int gap_pairing_set_state_and_run(bd_addr_t addr, uint8_t state){ 47880a51f88bSMatthias Ringwald hci_stack->gap_pairing_state = state; 47890a51f88bSMatthias Ringwald memcpy(hci_stack->gap_pairing_addr, addr, 6); 47900a51f88bSMatthias Ringwald hci_run(); 47910a51f88bSMatthias Ringwald return 0; 47920a51f88bSMatthias Ringwald } 47930a51f88bSMatthias Ringwald 47940a51f88bSMatthias Ringwald /** 47950a51f88bSMatthias Ringwald * @brief Legacy Pairing Pin Code Response 47960a51f88bSMatthias Ringwald * @param addr 47970a51f88bSMatthias Ringwald * @param pin 47980a51f88bSMatthias Ringwald * @return 0 if ok 47990a51f88bSMatthias Ringwald */ 48000a51f88bSMatthias Ringwald int gap_pin_code_response(bd_addr_t addr, const char * pin){ 4801cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 4802d504181aSMatthias Ringwald hci_stack->gap_pairing_input.gap_pairing_pin = pin; 48030a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PIN); 48040a51f88bSMatthias Ringwald } 48050a51f88bSMatthias Ringwald 48060a51f88bSMatthias Ringwald /** 48070a51f88bSMatthias Ringwald * @brief Abort Legacy Pairing 48080a51f88bSMatthias Ringwald * @param addr 48090a51f88bSMatthias Ringwald * @param pin 48100a51f88bSMatthias Ringwald * @return 0 if ok 48110a51f88bSMatthias Ringwald */ 48120a51f88bSMatthias Ringwald int gap_pin_code_negative(bd_addr_t addr){ 4813cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 48140a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PIN_NEGATIVE); 48150a51f88bSMatthias Ringwald } 48160a51f88bSMatthias Ringwald 48170a51f88bSMatthias Ringwald /** 48180a51f88bSMatthias Ringwald * @brief SSP Passkey Response 48190a51f88bSMatthias Ringwald * @param addr 48200a51f88bSMatthias Ringwald * @param passkey 48210a51f88bSMatthias Ringwald * @return 0 if ok 48220a51f88bSMatthias Ringwald */ 48230a51f88bSMatthias Ringwald int gap_ssp_passkey_response(bd_addr_t addr, uint32_t passkey){ 4824cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 4825d504181aSMatthias Ringwald hci_stack->gap_pairing_input.gap_pairing_passkey = passkey; 48260a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PASSKEY); 48270a51f88bSMatthias Ringwald } 48280a51f88bSMatthias Ringwald 48290a51f88bSMatthias Ringwald /** 48300a51f88bSMatthias Ringwald * @brief Abort SSP Passkey Entry/Pairing 48310a51f88bSMatthias Ringwald * @param addr 48320a51f88bSMatthias Ringwald * @param pin 48330a51f88bSMatthias Ringwald * @return 0 if ok 48340a51f88bSMatthias Ringwald */ 48350a51f88bSMatthias Ringwald int gap_ssp_passkey_negative(bd_addr_t addr){ 4836cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 48370a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PASSKEY_NEGATIVE); 48380a51f88bSMatthias Ringwald } 48390a51f88bSMatthias Ringwald 48400a51f88bSMatthias Ringwald /** 48410a51f88bSMatthias Ringwald * @brief Accept SSP Numeric Comparison 48420a51f88bSMatthias Ringwald * @param addr 48430a51f88bSMatthias Ringwald * @param passkey 48440a51f88bSMatthias Ringwald * @return 0 if ok 48450a51f88bSMatthias Ringwald */ 48460a51f88bSMatthias Ringwald int gap_ssp_confirmation_response(bd_addr_t addr){ 4847cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 48480a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_CONFIRMATION); 48490a51f88bSMatthias Ringwald } 48500a51f88bSMatthias Ringwald 48510a51f88bSMatthias Ringwald /** 48520a51f88bSMatthias Ringwald * @brief Abort SSP Numeric Comparison/Pairing 48530a51f88bSMatthias Ringwald * @param addr 48540a51f88bSMatthias Ringwald * @param pin 48550a51f88bSMatthias Ringwald * @return 0 if ok 48560a51f88bSMatthias Ringwald */ 48570a51f88bSMatthias Ringwald int gap_ssp_confirmation_negative(bd_addr_t addr){ 4858cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 48590a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_CONFIRMATION_NEGATIVE); 48600a51f88bSMatthias Ringwald } 48610a51f88bSMatthias Ringwald 4862f5875de5SMatthias Ringwald /** 4863f6858d14SMatthias Ringwald * @brief Set inquiry mode: standard, with RSSI, with RSSI + Extended Inquiry Results. Has to be called before power on. 4864f5875de5SMatthias Ringwald * @param inquiry_mode see bluetooth_defines.h 4865f6858d14SMatthias Ringwald */ 4866f6858d14SMatthias Ringwald void hci_set_inquiry_mode(inquiry_mode_t mode){ 4867f6858d14SMatthias Ringwald hci_stack->inquiry_mode = mode; 4868f6858d14SMatthias Ringwald } 4869f6858d14SMatthias Ringwald 4870f6858d14SMatthias Ringwald /** 4871d950d659SMatthias Ringwald * @brief Configure Voice Setting for use with SCO data in HSP/HFP 4872d950d659SMatthias Ringwald */ 4873d950d659SMatthias Ringwald void hci_set_sco_voice_setting(uint16_t voice_setting){ 4874d950d659SMatthias Ringwald hci_stack->sco_voice_setting = voice_setting; 4875d950d659SMatthias Ringwald } 4876d950d659SMatthias Ringwald 4877d950d659SMatthias Ringwald /** 4878d950d659SMatthias Ringwald * @brief Get SCO Voice Setting 4879d950d659SMatthias Ringwald * @return current voice setting 4880d950d659SMatthias Ringwald */ 48810cb5b971SMatthias Ringwald uint16_t hci_get_sco_voice_setting(void){ 4882d950d659SMatthias Ringwald return hci_stack->sco_voice_setting; 4883d950d659SMatthias Ringwald } 4884d950d659SMatthias Ringwald 48853139d098SMatthias Ringwald #ifdef ENABLE_CLASSIC 48863139d098SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 4887400ff5abSMatthias Ringwald static int hci_have_usb_transport(void){ 4888400ff5abSMatthias Ringwald if (!hci_stack->hci_transport) return 0; 4889400ff5abSMatthias Ringwald const char * transport_name = hci_stack->hci_transport->name; 4890400ff5abSMatthias Ringwald if (!transport_name) return 0; 4891400ff5abSMatthias Ringwald return (transport_name[0] == 'H') && (transport_name[1] == '2'); 4892400ff5abSMatthias Ringwald } 48933139d098SMatthias Ringwald #endif 48943139d098SMatthias Ringwald #endif 4895400ff5abSMatthias Ringwald 4896b3aad8daSMatthias Ringwald /** @brief Get SCO packet length for current SCO Voice setting 4897b3aad8daSMatthias Ringwald * @note Using SCO packets of the exact length is required for USB transfer 4898b3aad8daSMatthias Ringwald * @return Length of SCO packets in bytes (not audio frames) 4899b3aad8daSMatthias Ringwald */ 4900b3aad8daSMatthias Ringwald int hci_get_sco_packet_length(void){ 4901a3069afeSMatthias Ringwald int sco_packet_length = 0; 4902a3069afeSMatthias Ringwald 4903a3069afeSMatthias Ringwald #ifdef ENABLE_CLASSIC 4904a3069afeSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 4905cf119f3bSMatthias Ringwald 4906cf119f3bSMatthias Ringwald // CVSD requires twice as much bytes 49073e56f673SMatthias Ringwald int multiplier = hci_stack->sco_voice_setting_active & 0x0020 ? 2 : 1; 4908cf119f3bSMatthias Ringwald 4909400ff5abSMatthias Ringwald if (hci_have_usb_transport()){ 4910400ff5abSMatthias Ringwald // see Core Spec for H2 USB Transfer. 4911cf119f3bSMatthias Ringwald // 3 byte SCO header + 24 bytes per connection 491252e46257SMatthias Ringwald int num_sco_connections = btstack_max(1, hci_number_sco_connections()); 491352e46257SMatthias Ringwald sco_packet_length = 3 + 24 * num_sco_connections * multiplier; 4914400ff5abSMatthias Ringwald } else { 4915400ff5abSMatthias Ringwald // 3 byte SCO header + SCO packet size over the air (60 bytes) 4916400ff5abSMatthias Ringwald sco_packet_length = 3 + 60 * multiplier; 4917d966a453SMatthias Ringwald // assert that it still fits inside an SCO buffer 4918d966a453SMatthias Ringwald if (sco_packet_length > hci_stack->sco_data_packet_length){ 4919d966a453SMatthias Ringwald sco_packet_length = 3 + 60; 4920d966a453SMatthias Ringwald } 4921400ff5abSMatthias Ringwald } 4922a3069afeSMatthias Ringwald #endif 4923a3069afeSMatthias Ringwald #endif 4924a3069afeSMatthias Ringwald return sco_packet_length; 4925b3aad8daSMatthias Ringwald } 4926b3aad8daSMatthias Ringwald 4927c4c88f1bSJakob Krantz /** 4928c4c88f1bSJakob Krantz * @brief Sets the master/slave policy 4929c4c88f1bSJakob Krantz * @param policy (0: attempt to become master, 1: let connecting device decide) 4930c4c88f1bSJakob Krantz */ 4931c4c88f1bSJakob Krantz void hci_set_master_slave_policy(uint8_t policy){ 4932c4c88f1bSJakob Krantz hci_stack->master_slave_policy = policy; 4933c4c88f1bSJakob Krantz } 4934c4c88f1bSJakob Krantz 4935c4c88f1bSJakob Krantz #endif 4936ec111c8bSMatthias Ringwald 4937ec111c8bSMatthias Ringwald HCI_STATE hci_get_state(void){ 4938ec111c8bSMatthias Ringwald return hci_stack->state; 4939ec111c8bSMatthias Ringwald } 4940ec111c8bSMatthias Ringwald 4941ec111c8bSMatthias Ringwald 4942d950d659SMatthias Ringwald /** 4943d23838ecSMatthias Ringwald * @brief Set callback for Bluetooth Hardware Error 4944d23838ecSMatthias Ringwald */ 4945c2e1fa60SMatthias Ringwald void hci_set_hardware_error_callback(void (*fn)(uint8_t error)){ 4946d23838ecSMatthias Ringwald hci_stack->hardware_error_callback = fn; 4947d23838ecSMatthias Ringwald } 4948d23838ecSMatthias Ringwald 494971de195eSMatthias Ringwald void hci_disconnect_all(void){ 4950665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 4951665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 4952665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 4953665d90f2SMatthias Ringwald hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 495404a6ef8cSmatthias.ringwald if (con->state == SENT_DISCONNECT) continue; 495504a6ef8cSmatthias.ringwald con->state = SEND_DISCONNECT; 495604a6ef8cSmatthias.ringwald } 4957d31fba26S[email protected] hci_run(); 495804a6ef8cSmatthias.ringwald } 495933373e40SMatthias Ringwald 496033373e40SMatthias Ringwald uint16_t hci_get_manufacturer(void){ 496133373e40SMatthias Ringwald return hci_stack->manufacturer; 496233373e40SMatthias Ringwald } 49639c6e867eSMatthias Ringwald 49643e329ddfSandryblack #ifdef ENABLE_BLE 49653e329ddfSandryblack 49669c6e867eSMatthias Ringwald static sm_connection_t * sm_get_connection_for_handle(hci_con_handle_t con_handle){ 49679c6e867eSMatthias Ringwald hci_connection_t * hci_con = hci_connection_for_handle(con_handle); 49689c6e867eSMatthias Ringwald if (!hci_con) return NULL; 49699c6e867eSMatthias Ringwald return &hci_con->sm_connection; 49709c6e867eSMatthias Ringwald } 49719c6e867eSMatthias Ringwald 49729c6e867eSMatthias Ringwald // extracted from sm.c to allow enabling of l2cap le data channels without adding sm.c to the build 49739c6e867eSMatthias Ringwald // without sm.c default values from create_connection_for_bd_addr_and_type() resulg in non-encrypted, not-authenticated 49749c6e867eSMatthias Ringwald 49759c6e867eSMatthias Ringwald int gap_encryption_key_size(hci_con_handle_t con_handle){ 49769c6e867eSMatthias Ringwald sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle); 49779c6e867eSMatthias Ringwald if (!sm_conn) return 0; // wrong connection 49789c6e867eSMatthias Ringwald if (!sm_conn->sm_connection_encrypted) return 0; 49799c6e867eSMatthias Ringwald return sm_conn->sm_actual_encryption_key_size; 49809c6e867eSMatthias Ringwald } 49819c6e867eSMatthias Ringwald 49829c6e867eSMatthias Ringwald int gap_authenticated(hci_con_handle_t con_handle){ 49839c6e867eSMatthias Ringwald sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle); 49849c6e867eSMatthias Ringwald if (!sm_conn) return 0; // wrong connection 49859c6e867eSMatthias Ringwald if (!sm_conn->sm_connection_encrypted) return 0; // unencrypted connection cannot be authenticated 49869c6e867eSMatthias Ringwald return sm_conn->sm_connection_authenticated; 49879c6e867eSMatthias Ringwald } 49889c6e867eSMatthias Ringwald 4989f1dfbe18SMatthias Ringwald int gap_secure_connection(hci_con_handle_t con_handle){ 4990f1dfbe18SMatthias Ringwald sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle); 4991f1dfbe18SMatthias Ringwald if (!sm_conn) return 0; // wrong connection 4992f1dfbe18SMatthias Ringwald if (!sm_conn->sm_connection_encrypted) return 0; // unencrypted connection cannot be authenticated 4993f1dfbe18SMatthias Ringwald return sm_conn->sm_connection_sc; 4994f1dfbe18SMatthias Ringwald } 4995f1dfbe18SMatthias Ringwald 49969c6e867eSMatthias Ringwald authorization_state_t gap_authorization_state(hci_con_handle_t con_handle){ 49979c6e867eSMatthias Ringwald sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle); 49989c6e867eSMatthias Ringwald if (!sm_conn) return AUTHORIZATION_UNKNOWN; // wrong connection 49999c6e867eSMatthias Ringwald if (!sm_conn->sm_connection_encrypted) return AUTHORIZATION_UNKNOWN; // unencrypted connection cannot be authorized 50009c6e867eSMatthias Ringwald if (!sm_conn->sm_connection_authenticated) return AUTHORIZATION_UNKNOWN; // unauthenticatd connection cannot be authorized 50019c6e867eSMatthias Ringwald return sm_conn->sm_connection_authorization_state; 50029c6e867eSMatthias Ringwald } 50039c6e867eSMatthias Ringwald #endif 5004f8ee3071SMatthias Ringwald 5005f8ee3071SMatthias Ringwald #ifdef ENABLE_CLASSIC 5006f8ee3071SMatthias Ringwald uint8_t gap_sniff_mode_enter(hci_con_handle_t con_handle, uint16_t sniff_min_interval, uint16_t sniff_max_interval, uint16_t sniff_attempt, uint16_t sniff_timeout){ 5007f8ee3071SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 5008f8ee3071SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 5009f8ee3071SMatthias Ringwald conn->sniff_min_interval = sniff_min_interval; 5010f8ee3071SMatthias Ringwald conn->sniff_max_interval = sniff_max_interval; 5011f8ee3071SMatthias Ringwald conn->sniff_attempt = sniff_attempt; 5012f8ee3071SMatthias Ringwald conn->sniff_timeout = sniff_timeout; 5013f8ee3071SMatthias Ringwald hci_run(); 5014f8ee3071SMatthias Ringwald return 0; 5015f8ee3071SMatthias Ringwald } 5016f8ee3071SMatthias Ringwald 5017f8ee3071SMatthias Ringwald /** 5018f8ee3071SMatthias Ringwald * @brief Exit Sniff mode 5019f8ee3071SMatthias Ringwald * @param con_handle 5020f8ee3071SMatthias Ringwald @ @return 0 if ok 5021f8ee3071SMatthias Ringwald */ 5022f8ee3071SMatthias Ringwald uint8_t gap_sniff_mode_exit(hci_con_handle_t con_handle){ 5023f8ee3071SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 5024f8ee3071SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 5025f8ee3071SMatthias Ringwald conn->sniff_min_interval = 0xffff; 5026f8ee3071SMatthias Ringwald hci_run(); 5027f8ee3071SMatthias Ringwald return 0; 5028f8ee3071SMatthias Ringwald } 5029f8ee3071SMatthias Ringwald #endif 5030beceeddeSMatthias Ringwald 5031beceeddeSMatthias Ringwald void hci_halting_defer(void){ 5032beceeddeSMatthias Ringwald if (hci_stack->state != HCI_STATE_HALTING) return; 5033beceeddeSMatthias Ringwald switch (hci_stack->substate){ 5034beceeddeSMatthias Ringwald case HCI_HALTING_DISCONNECT_ALL_NO_TIMER: 5035beceeddeSMatthias Ringwald case HCI_HALTING_CLOSE: 5036beceeddeSMatthias Ringwald hci_stack->substate = HCI_HALTING_DISCONNECT_ALL_TIMER; 5037beceeddeSMatthias Ringwald break; 5038beceeddeSMatthias Ringwald default: 5039beceeddeSMatthias Ringwald break; 5040beceeddeSMatthias Ringwald } 5041beceeddeSMatthias Ringwald } 5042