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 381713bceaSmatthias.ringwald /* 391f504dbdSmatthias.ringwald * hci.c 401f504dbdSmatthias.ringwald * 411f504dbdSmatthias.ringwald * Created by Matthias Ringwald on 4/29/09. 421f504dbdSmatthias.ringwald * 431f504dbdSmatthias.ringwald */ 441f504dbdSmatthias.ringwald 457907f069SMatthias Ringwald #include "btstack_config.h" 4628171530Smatthias.ringwald 477f2435e6Smatthias.ringwald 48aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK 498f2a52f4SMatthias Ringwald #include "btstack_run_loop_embedded.h" 50a484130cSMatthias Ringwald #endif 51a484130cSMatthias Ringwald 524a3574a1SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 53d0b87befSMatthias Ringwald #include "../port/ios/src/btstack_control_iphone.h" 544a3574a1SMatthias Ringwald #endif 554a3574a1SMatthias Ringwald 56a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 57472a5742SMatthias Ringwald #include "gap.h" 5845c102fdSMatthias Ringwald #endif 5945c102fdSMatthias Ringwald 6093b8dc03Smatthias.ringwald #include <stdarg.h> 6193b8dc03Smatthias.ringwald #include <string.h> 6256fe0872Smatthias.ringwald #include <stdio.h> 635838a2edSMatthias Ringwald #include <inttypes.h> 647f2435e6Smatthias.ringwald 6516ece135SMatthias Ringwald #include "btstack_debug.h" 660e2df43fSMatthias Ringwald #include "btstack_event.h" 674a3574a1SMatthias Ringwald #include "btstack_linked_list.h" 684a3574a1SMatthias Ringwald #include "btstack_memory.h" 694a3574a1SMatthias Ringwald #include "gap.h" 704a3574a1SMatthias Ringwald #include "hci.h" 714a3574a1SMatthias Ringwald #include "hci_cmd.h" 72d8905019Smatthias.ringwald #include "hci_dump.h" 7393b8dc03Smatthias.ringwald 741b0e3922Smatthias.ringwald 75169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000 76659d758cSMatthias Ringwald #define HCI_RESET_RESEND_TIMEOUT_MS 200 77ee091cf1Smatthias.ringwald 78b83d5eabSMatthias Ringwald // prototypes 79*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 80758b46ceSmatthias.ringwald static void hci_update_scan_enable(void); 81*35454696SMatthias Ringwald static void hci_emit_discoverable_enabled(uint8_t enabled); 82*35454696SMatthias Ringwald static int hci_local_ssp_activated(void); 83*35454696SMatthias Ringwald static int hci_remote_ssp_supported(hci_con_handle_t con_handle); 84*35454696SMatthias Ringwald static void hci_notify_if_sco_can_send_now(void); 85*35454696SMatthias Ringwald static void hci_emit_connection_complete(bd_addr_t address, hci_con_handle_t con_handle, uint8_t status); 86a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection); 87*35454696SMatthias Ringwald static void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level); 88*35454696SMatthias Ringwald #endif 89ec820d77SMatthias Ringwald static void hci_connection_timeout_handler(btstack_timer_source_t *timer); 9096a45072S[email protected] static void hci_connection_timestamp(hci_connection_t *connection); 917586ee35S[email protected] static int hci_power_control_on(void); 927586ee35S[email protected] static void hci_power_control_off(void); 936da48142SSean Wilson static void hci_state_reset(void); 94b83d5eabSMatthias Ringwald static void hci_emit_l2cap_check_timeout(hci_connection_t *conn); 95fc64f94aSMatthias Ringwald static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason); 96b83d5eabSMatthias Ringwald static void hci_emit_nr_connections_changed(void); 97b83d5eabSMatthias Ringwald static void hci_emit_hci_open_failed(void); 98b83d5eabSMatthias Ringwald static void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status); 99b83d5eabSMatthias Ringwald static void hci_emit_event(uint8_t * event, uint16_t size, int dump); 100b83d5eabSMatthias Ringwald static void hci_emit_acl_packet(uint8_t * packet, uint16_t size); 10195d71764SMatthias Ringwald static void hci_run(void); 10295d71764SMatthias Ringwald static int hci_is_le_connection(hci_connection_t * connection); 10395d71764SMatthias Ringwald static int hci_number_free_acl_slots_for_connection_type( bd_addr_type_t address_type); 1045d509858SMatthias Ringwald 105a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 10639677e66SMatthias Ringwald // called from test/ble_client/advertising_data_parser.c 10739677e66SMatthias Ringwald void le_handle_advertisement_report(uint8_t *packet, int size); 10842ff5ba1SMatthias Ringwald static void hci_remove_from_whitelist(bd_addr_type_t address_type, bd_addr_t address); 1095d509858SMatthias Ringwald #endif 110758b46ceSmatthias.ringwald 11106b35ec0Smatthias.ringwald // the STACK is here 1123a9fb326S[email protected] #ifndef HAVE_MALLOC 1133a9fb326S[email protected] static hci_stack_t hci_stack_static; 1143a9fb326S[email protected] #endif 1153a9fb326S[email protected] static hci_stack_t * hci_stack = NULL; 11616833f0aSmatthias.ringwald 11766fb9560S[email protected] // test helper 11866fb9560S[email protected] static uint8_t disable_l2cap_timeouts = 0; 11966fb9560S[email protected] 12096a45072S[email protected] /** 12196a45072S[email protected] * create connection for given address 12296a45072S[email protected] * 12396a45072S[email protected] * @return connection OR NULL, if no memory left 12496a45072S[email protected] */ 12596a45072S[email protected] static hci_connection_t * create_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){ 1261a06f663S[email protected] log_info("create_connection_for_addr %s, type %x", bd_addr_to_str(addr), addr_type); 127bb69aaaeS[email protected] hci_connection_t * conn = btstack_memory_hci_connection_get(); 12896a45072S[email protected] if (!conn) return NULL; 129c91d150bS[email protected] memset(conn, 0, sizeof(hci_connection_t)); 130058e3d6bSMatthias Ringwald bd_addr_copy(conn->address, addr); 13196a45072S[email protected] conn->address_type = addr_type; 13296a45072S[email protected] conn->con_handle = 0xffff; 13396a45072S[email protected] conn->authentication_flags = AUTH_FLAGS_NONE; 13496a45072S[email protected] conn->bonding_flags = 0; 13596a45072S[email protected] conn->requested_security_level = LEVEL_0; 13691a977e8SMatthias Ringwald btstack_run_loop_set_timer_handler(&conn->timeout, hci_connection_timeout_handler); 13791a977e8SMatthias Ringwald btstack_run_loop_set_timer_context(&conn->timeout, conn); 13896a45072S[email protected] hci_connection_timestamp(conn); 13996a45072S[email protected] conn->acl_recombination_length = 0; 14096a45072S[email protected] conn->acl_recombination_pos = 0; 14196a45072S[email protected] conn->num_acl_packets_sent = 0; 1421a06f663S[email protected] conn->num_sco_packets_sent = 0; 143da886c03S[email protected] conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 144665d90f2SMatthias Ringwald btstack_linked_list_add(&hci_stack->connections, (btstack_linked_item_t *) conn); 14596a45072S[email protected] return conn; 14696a45072S[email protected] } 14766fb9560S[email protected] 148da886c03S[email protected] 149da886c03S[email protected] /** 150da886c03S[email protected] * get le connection parameter range 151da886c03S[email protected] * 152da886c03S[email protected] * @return le connection parameter range struct 153da886c03S[email protected] */ 1544ced4e8cSMatthias Ringwald void gap_get_connection_parameter_range(le_connection_parameter_range_t * range){ 1554ced4e8cSMatthias Ringwald *range = hci_stack->le_connection_parameter_range; 156da886c03S[email protected] } 157da886c03S[email protected] 158da886c03S[email protected] /** 159da886c03S[email protected] * set le connection parameter range 160da886c03S[email protected] * 161da886c03S[email protected] */ 162da886c03S[email protected] 1634ced4e8cSMatthias Ringwald void gap_set_connection_parameter_range(le_connection_parameter_range_t *range){ 1644ced4e8cSMatthias Ringwald hci_stack->le_connection_parameter_range = *range; 165da886c03S[email protected] } 166da886c03S[email protected] 167da886c03S[email protected] /** 168da886c03S[email protected] * get hci connections iterator 169da886c03S[email protected] * 170da886c03S[email protected] * @return hci connections iterator 171da886c03S[email protected] */ 172da886c03S[email protected] 173665d90f2SMatthias Ringwald void hci_connections_get_iterator(btstack_linked_list_iterator_t *it){ 174665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(it, &hci_stack->connections); 175da886c03S[email protected] } 176da886c03S[email protected] 17797addcc5Smatthias.ringwald /** 178ee091cf1Smatthias.ringwald * get connection for a given handle 179ee091cf1Smatthias.ringwald * 180ee091cf1Smatthias.ringwald * @return connection OR NULL, if not found 181ee091cf1Smatthias.ringwald */ 1825061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){ 183665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 184665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 185665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 186665d90f2SMatthias Ringwald hci_connection_t * item = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 1873ac2fe56S[email protected] if ( item->con_handle == con_handle ) { 188da886c03S[email protected] return item; 189ee091cf1Smatthias.ringwald } 190ee091cf1Smatthias.ringwald } 191ee091cf1Smatthias.ringwald return NULL; 192ee091cf1Smatthias.ringwald } 193ee091cf1Smatthias.ringwald 19496a45072S[email protected] /** 19596a45072S[email protected] * get connection for given address 19696a45072S[email protected] * 19796a45072S[email protected] * @return connection OR NULL, if not found 19896a45072S[email protected] */ 1992e77e513S[email protected] hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){ 200665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 201665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 202665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 203665d90f2SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 20496a45072S[email protected] if (connection->address_type != addr_type) continue; 20596a45072S[email protected] if (memcmp(addr, connection->address, 6) != 0) continue; 20662bda3fbS[email protected] return connection; 20762bda3fbS[email protected] } 20862bda3fbS[email protected] return NULL; 20962bda3fbS[email protected] } 21062bda3fbS[email protected] 211ec820d77SMatthias Ringwald static void hci_connection_timeout_handler(btstack_timer_source_t *timer){ 21291a977e8SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_run_loop_get_timer_context(timer); 213aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK 214528a4a3bSMatthias Ringwald if (btstack_run_loop_embedded_get_ticks() > connection->timestamp + btstack_run_loop_embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){ 215c785ef68Smatthias.ringwald // connections might be timed out 216c785ef68Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 217c785ef68Smatthias.ringwald } 218f316a845SMatthias Ringwald #else 219528a4a3bSMatthias Ringwald if (btstack_run_loop_get_time_ms() > connection->timestamp + HCI_CONNECTION_TIMEOUT_MS){ 2205f26aadcSMatthias Ringwald // connections might be timed out 2215f26aadcSMatthias Ringwald hci_emit_l2cap_check_timeout(connection); 2225f26aadcSMatthias Ringwald } 2235f26aadcSMatthias Ringwald #endif 224c785ef68Smatthias.ringwald } 225ee091cf1Smatthias.ringwald 226ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){ 227aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK 228528a4a3bSMatthias Ringwald connection->timestamp = btstack_run_loop_embedded_get_ticks(); 229f316a845SMatthias Ringwald #else 230528a4a3bSMatthias Ringwald connection->timestamp = btstack_run_loop_get_time_ms(); 2315f26aadcSMatthias Ringwald #endif 232ee091cf1Smatthias.ringwald } 233ee091cf1Smatthias.ringwald 23406b35ec0Smatthias.ringwald 235*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 236*35454696SMatthias Ringwald 23728ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 23828ca2b46S[email protected] conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags); 23928ca2b46S[email protected] } 24028ca2b46S[email protected] 241*35454696SMatthias Ringwald 24228ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 24328ca2b46S[email protected] conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags); 24428ca2b46S[email protected] } 24528ca2b46S[email protected] 24643bfb1bdSmatthias.ringwald /** 24780ca58a0Smatthias.ringwald * add authentication flags and reset timer 24896a45072S[email protected] * @note: assumes classic connection 2492e77e513S[email protected] * @note: bd_addr is passed in as litle endian uint8_t * as it is called from parsing packets 2507fde4af9Smatthias.ringwald */ 2517fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){ 2527fde4af9Smatthias.ringwald bd_addr_t addr; 253724d70a2SMatthias Ringwald reverse_bd_addr(bd_addr, addr); 2542e77e513S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 2557fde4af9Smatthias.ringwald if (conn) { 25628ca2b46S[email protected] connectionSetAuthenticationFlags(conn, flags); 25780ca58a0Smatthias.ringwald hci_connection_timestamp(conn); 2587fde4af9Smatthias.ringwald } 2597fde4af9Smatthias.ringwald } 2607fde4af9Smatthias.ringwald 26180ca58a0Smatthias.ringwald int hci_authentication_active_for_handle(hci_con_handle_t handle){ 2625061f3afS[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 26380ca58a0Smatthias.ringwald if (!conn) return 0; 2646724cd9eS[email protected] if (conn->authentication_flags & LEGACY_PAIRING_ACTIVE) return 1; 2656724cd9eS[email protected] if (conn->authentication_flags & SSP_PAIRING_ACTIVE) return 1; 2666724cd9eS[email protected] return 0; 26780ca58a0Smatthias.ringwald } 26880ca58a0Smatthias.ringwald 26915a95bd5SMatthias Ringwald void gap_drop_link_key_for_bd_addr(bd_addr_t addr){ 27055597469SMatthias Ringwald if (!hci_stack->link_key_db) return; 2712bacf595SMatthias Ringwald log_info("gap_drop_link_key_for_bd_addr: %s", bd_addr_to_str(addr)); 272a98592bcSMatthias Ringwald hci_stack->link_key_db->delete_link_key(addr); 273c12e46e7Smatthias.ringwald } 27455597469SMatthias Ringwald 27555597469SMatthias Ringwald void gap_store_link_key_for_bd_addr(bd_addr_t addr, link_key_t link_key, link_key_type_t type){ 27655597469SMatthias Ringwald if (!hci_stack->link_key_db) return; 2772bacf595SMatthias Ringwald log_info("gap_store_link_key_for_bd_addr: %s, type %u", bd_addr_to_str(addr), type); 27855597469SMatthias Ringwald hci_stack->link_key_db->put_link_key(addr, link_key, type); 279c12e46e7Smatthias.ringwald } 280*35454696SMatthias Ringwald #endif 281c12e46e7Smatthias.ringwald 28295d71764SMatthias Ringwald static int hci_is_le_connection(hci_connection_t * connection){ 2830bf6344aS[email protected] return connection->address_type == BD_ADDR_TYPE_LE_PUBLIC || 2840bf6344aS[email protected] connection->address_type == BD_ADDR_TYPE_LE_RANDOM; 2850bf6344aS[email protected] } 2860bf6344aS[email protected] 2877fde4af9Smatthias.ringwald /** 28843bfb1bdSmatthias.ringwald * count connections 28943bfb1bdSmatthias.ringwald */ 29040d1c7a4Smatthias.ringwald static int nr_hci_connections(void){ 29156c253c9Smatthias.ringwald int count = 0; 292665d90f2SMatthias Ringwald btstack_linked_item_t *it; 293665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next, count++); 29443bfb1bdSmatthias.ringwald return count; 29543bfb1bdSmatthias.ringwald } 296c8e4258aSmatthias.ringwald 29795d71764SMatthias Ringwald static int hci_number_free_acl_slots_for_connection_type(bd_addr_type_t address_type){ 298ee303eddS[email protected] 299f04a0c31SMatthias Ringwald unsigned int num_packets_sent_classic = 0; 300f04a0c31SMatthias Ringwald unsigned int num_packets_sent_le = 0; 301ee303eddS[email protected] 302665d90f2SMatthias Ringwald btstack_linked_item_t *it; 303665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 304998906cdSmatthias.ringwald hci_connection_t * connection = (hci_connection_t *) it; 305ee303eddS[email protected] if (connection->address_type == BD_ADDR_TYPE_CLASSIC){ 306ee303eddS[email protected] num_packets_sent_classic += connection->num_acl_packets_sent; 307ee303eddS[email protected] } else { 308ee303eddS[email protected] num_packets_sent_le += connection->num_acl_packets_sent; 309ee303eddS[email protected] } 310ee303eddS[email protected] } 311d999b54eSMatthias Ringwald log_debug("ACL classic buffers: %u used of %u", num_packets_sent_classic, hci_stack->acl_packets_total_num); 312ee303eddS[email protected] int free_slots_classic = hci_stack->acl_packets_total_num - num_packets_sent_classic; 313ee303eddS[email protected] int free_slots_le = 0; 314ee303eddS[email protected] 315ee303eddS[email protected] if (free_slots_classic < 0){ 3169da54300S[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); 317998906cdSmatthias.ringwald return 0; 318998906cdSmatthias.ringwald } 319ee303eddS[email protected] 320ee303eddS[email protected] if (hci_stack->le_acl_packets_total_num){ 321ee303eddS[email protected] // if we have LE slots, they are used 322ee303eddS[email protected] free_slots_le = hci_stack->le_acl_packets_total_num - num_packets_sent_le; 323ee303eddS[email protected] if (free_slots_le < 0){ 3249da54300S[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); 325ee303eddS[email protected] return 0; 326998906cdSmatthias.ringwald } 327ee303eddS[email protected] } else { 328ee303eddS[email protected] // otherwise, classic slots are used for LE, too 329ee303eddS[email protected] free_slots_classic -= num_packets_sent_le; 330ee303eddS[email protected] if (free_slots_classic < 0){ 3319da54300S[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); 332ee303eddS[email protected] return 0; 333ee303eddS[email protected] } 334ee303eddS[email protected] } 335ee303eddS[email protected] 336ee303eddS[email protected] switch (address_type){ 337ee303eddS[email protected] case BD_ADDR_TYPE_UNKNOWN: 3382125de09SMatthias Ringwald log_error("hci_number_free_acl_slots: unknown address type"); 339ee303eddS[email protected] return 0; 340ee303eddS[email protected] 341ee303eddS[email protected] case BD_ADDR_TYPE_CLASSIC: 342ee303eddS[email protected] return free_slots_classic; 343ee303eddS[email protected] 344ee303eddS[email protected] default: 345cb00d3aaS[email protected] if (hci_stack->le_acl_packets_total_num){ 346ee303eddS[email protected] return free_slots_le; 347ee303eddS[email protected] } 348cb00d3aaS[email protected] return free_slots_classic; 349cb00d3aaS[email protected] } 350998906cdSmatthias.ringwald } 351998906cdSmatthias.ringwald 3522125de09SMatthias Ringwald int hci_number_free_acl_slots_for_handle(hci_con_handle_t con_handle){ 3532125de09SMatthias Ringwald // get connection type 3542125de09SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 3552125de09SMatthias Ringwald if (!connection){ 3562125de09SMatthias Ringwald log_error("hci_number_free_acl_slots: handle 0x%04x not in connection list", con_handle); 3572125de09SMatthias Ringwald return 0; 3582125de09SMatthias Ringwald } 3592125de09SMatthias Ringwald return hci_number_free_acl_slots_for_connection_type(connection->address_type); 3602125de09SMatthias Ringwald } 3612125de09SMatthias Ringwald 362*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 363701e3307SMatthias Ringwald static int hci_number_free_sco_slots(void){ 364f04a0c31SMatthias Ringwald unsigned int num_sco_packets_sent = 0; 365665d90f2SMatthias Ringwald btstack_linked_item_t *it; 366665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 367e35edcc1S[email protected] hci_connection_t * connection = (hci_connection_t *) it; 368e35edcc1S[email protected] num_sco_packets_sent += connection->num_sco_packets_sent; 369e35edcc1S[email protected] } 370e35edcc1S[email protected] if (num_sco_packets_sent > hci_stack->sco_packets_total_num){ 371701e3307SMatthias Ringwald log_info("hci_number_free_sco_slots:packets (%u) > total packets (%u)", num_sco_packets_sent, hci_stack->sco_packets_total_num); 37244d0e3d5S[email protected] return 0; 37344d0e3d5S[email protected] } 374701e3307SMatthias Ringwald // log_info("hci_number_free_sco_slots u", handle, num_sco_packets_sent); 375e35edcc1S[email protected] return hci_stack->sco_packets_total_num - num_sco_packets_sent; 376e35edcc1S[email protected] } 377*35454696SMatthias Ringwald #endif 37844d0e3d5S[email protected] 379ac928cc2S[email protected] // new functions replacing hci_can_send_packet_now[_using_packet_buffer] 380ac928cc2S[email protected] int hci_can_send_command_packet_now(void){ 381ac928cc2S[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 382ac928cc2S[email protected] 383ac928cc2S[email protected] // check for async hci transport implementations 384ac928cc2S[email protected] if (hci_stack->hci_transport->can_send_packet_now){ 385ac928cc2S[email protected] if (!hci_stack->hci_transport->can_send_packet_now(HCI_COMMAND_DATA_PACKET)){ 386ac928cc2S[email protected] return 0; 387ac928cc2S[email protected] } 388ac928cc2S[email protected] } 389ac928cc2S[email protected] 390ac928cc2S[email protected] return hci_stack->num_cmd_packets > 0; 391ac928cc2S[email protected] } 392ac928cc2S[email protected] 3939d04d3a7SMatthias Ringwald static int hci_transport_can_send_prepared_packet_now(uint8_t packet_type){ 394ac928cc2S[email protected] // check for async hci transport implementations 3959d04d3a7SMatthias Ringwald if (!hci_stack->hci_transport->can_send_packet_now) return 1; 3969d04d3a7SMatthias Ringwald return hci_stack->hci_transport->can_send_packet_now(packet_type); 397ac928cc2S[email protected] } 3989d04d3a7SMatthias Ringwald 3999d04d3a7SMatthias Ringwald static int hci_can_send_prepared_acl_packet_for_address_type(bd_addr_type_t address_type){ 4009d04d3a7SMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_ACL_DATA_PACKET)) return 0; 4019d04d3a7SMatthias Ringwald return hci_number_free_acl_slots_for_connection_type(address_type) > 0; 402ac928cc2S[email protected] } 4039d04d3a7SMatthias Ringwald 4049d04d3a7SMatthias Ringwald int hci_can_send_acl_le_packet_now(void){ 4059d04d3a7SMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return 0; 4069d04d3a7SMatthias Ringwald return hci_can_send_prepared_acl_packet_for_address_type(BD_ADDR_TYPE_LE_PUBLIC); 4079d04d3a7SMatthias Ringwald } 4089d04d3a7SMatthias Ringwald 4099d04d3a7SMatthias Ringwald int hci_can_send_prepared_acl_packet_now(hci_con_handle_t con_handle) { 4109d04d3a7SMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_ACL_DATA_PACKET)) return 0; 411e79abdd6S[email protected] return hci_number_free_acl_slots_for_handle(con_handle) > 0; 412ac928cc2S[email protected] } 413ac928cc2S[email protected] 414ac928cc2S[email protected] int hci_can_send_acl_packet_now(hci_con_handle_t con_handle){ 415ac928cc2S[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 416ac928cc2S[email protected] return hci_can_send_prepared_acl_packet_now(con_handle); 4176b4af23dS[email protected] } 4186b4af23dS[email protected] 419*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 420*35454696SMatthias Ringwald int hci_can_send_acl_classic_packet_now(void){ 421*35454696SMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return 0; 422*35454696SMatthias Ringwald return hci_can_send_prepared_acl_packet_for_address_type(BD_ADDR_TYPE_CLASSIC); 423*35454696SMatthias Ringwald } 424*35454696SMatthias Ringwald 425701e3307SMatthias Ringwald int hci_can_send_prepared_sco_packet_now(void){ 426d057580eSMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_SCO_DATA_PACKET)) return 0; 427ed361f5fSMatthias Ringwald if (!hci_stack->synchronous_flow_control_enabled) return 1; 428701e3307SMatthias Ringwald return hci_number_free_sco_slots() > 0; 42944d0e3d5S[email protected] } 43044d0e3d5S[email protected] 431701e3307SMatthias Ringwald int hci_can_send_sco_packet_now(void){ 432d057580eSMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return 0; 433701e3307SMatthias Ringwald return hci_can_send_prepared_sco_packet_now(); 43444d0e3d5S[email protected] } 43544d0e3d5S[email protected] 436d057580eSMatthias Ringwald void hci_request_sco_can_send_now_event(void){ 437d057580eSMatthias Ringwald hci_stack->sco_waiting_for_can_send_now = 1; 438d057580eSMatthias Ringwald hci_notify_if_sco_can_send_now(); 439d057580eSMatthias Ringwald } 440*35454696SMatthias Ringwald #endif 441d057580eSMatthias Ringwald 44295d71764SMatthias Ringwald // used for internal checks in l2cap.c 443c8b9416aS[email protected] int hci_is_packet_buffer_reserved(void){ 444c8b9416aS[email protected] return hci_stack->hci_packet_buffer_reserved; 445c8b9416aS[email protected] } 446c8b9416aS[email protected] 4476b4af23dS[email protected] // reserves outgoing packet buffer. @returns 1 if successful 4486b4af23dS[email protected] int hci_reserve_packet_buffer(void){ 4499d14b626S[email protected] if (hci_stack->hci_packet_buffer_reserved) { 4509d14b626S[email protected] log_error("hci_reserve_packet_buffer called but buffer already reserved"); 4519d14b626S[email protected] return 0; 4529d14b626S[email protected] } 4536b4af23dS[email protected] hci_stack->hci_packet_buffer_reserved = 1; 4546b4af23dS[email protected] return 1; 4556b4af23dS[email protected] } 4566b4af23dS[email protected] 45768a0fcf7S[email protected] void hci_release_packet_buffer(void){ 45868a0fcf7S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 45968a0fcf7S[email protected] } 46068a0fcf7S[email protected] 4616b4af23dS[email protected] // assumption: synchronous implementations don't provide can_send_packet_now as they don't keep the buffer after the call 4627f02f414SMatthias Ringwald static int hci_transport_synchronous(void){ 4636b4af23dS[email protected] return hci_stack->hci_transport->can_send_packet_now == NULL; 4646b4af23dS[email protected] } 4656b4af23dS[email protected] 466452cf3bbS[email protected] static int hci_send_acl_packet_fragments(hci_connection_t *connection){ 467452cf3bbS[email protected] 468452cf3bbS[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); 469452cf3bbS[email protected] 470452cf3bbS[email protected] // max ACL data packet length depends on connection type (LE vs. Classic) and available buffers 471452cf3bbS[email protected] uint16_t max_acl_data_packet_length = hci_stack->acl_data_packet_length; 472452cf3bbS[email protected] if (hci_is_le_connection(connection) && hci_stack->le_data_packets_length > 0){ 473452cf3bbS[email protected] max_acl_data_packet_length = hci_stack->le_data_packets_length; 474452cf3bbS[email protected] } 475452cf3bbS[email protected] 476452cf3bbS[email protected] // testing: reduce buffer to minimum 477452cf3bbS[email protected] // max_acl_data_packet_length = 52; 478452cf3bbS[email protected] 479d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments entered"); 480d999b54eSMatthias Ringwald 481452cf3bbS[email protected] int err; 482452cf3bbS[email protected] // multiple packets could be send on a synchronous HCI transport 483452cf3bbS[email protected] while (1){ 484452cf3bbS[email protected] 485d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop entered"); 486d999b54eSMatthias Ringwald 487452cf3bbS[email protected] // get current data 488452cf3bbS[email protected] const uint16_t acl_header_pos = hci_stack->acl_fragmentation_pos - 4; 489452cf3bbS[email protected] int current_acl_data_packet_length = hci_stack->acl_fragmentation_total_size - hci_stack->acl_fragmentation_pos; 490452cf3bbS[email protected] int more_fragments = 0; 491452cf3bbS[email protected] 492452cf3bbS[email protected] // if ACL packet is larger than Bluetooth packet buffer, only send max_acl_data_packet_length 493452cf3bbS[email protected] if (current_acl_data_packet_length > max_acl_data_packet_length){ 494452cf3bbS[email protected] more_fragments = 1; 495452cf3bbS[email protected] current_acl_data_packet_length = max_acl_data_packet_length; 496452cf3bbS[email protected] } 497452cf3bbS[email protected] 498452cf3bbS[email protected] // copy handle_and_flags if not first fragment and update packet boundary flags to be 01 (continuing fragmnent) 499452cf3bbS[email protected] if (acl_header_pos > 0){ 500f8fbdce0SMatthias Ringwald uint16_t handle_and_flags = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 501452cf3bbS[email protected] handle_and_flags = (handle_and_flags & 0xcfff) | (1 << 12); 502f8fbdce0SMatthias Ringwald little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos, handle_and_flags); 503452cf3bbS[email protected] } 504452cf3bbS[email protected] 505452cf3bbS[email protected] // update header len 506f8fbdce0SMatthias Ringwald little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos + 2, current_acl_data_packet_length); 507452cf3bbS[email protected] 508452cf3bbS[email protected] // count packet 509452cf3bbS[email protected] connection->num_acl_packets_sent++; 510f04a0c31SMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop before send (more fragments %d)", more_fragments); 511d999b54eSMatthias Ringwald 512d999b54eSMatthias Ringwald // update state for next fragment (if any) as "transport done" might be sent during send_packet already 513d999b54eSMatthias Ringwald if (more_fragments){ 514d999b54eSMatthias Ringwald // update start of next fragment to send 515d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_pos += current_acl_data_packet_length; 516d999b54eSMatthias Ringwald } else { 517d999b54eSMatthias Ringwald // done 518d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_pos = 0; 519d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_total_size = 0; 520d999b54eSMatthias Ringwald } 521452cf3bbS[email protected] 522452cf3bbS[email protected] // send packet 523452cf3bbS[email protected] uint8_t * packet = &hci_stack->hci_packet_buffer[acl_header_pos]; 524452cf3bbS[email protected] const int size = current_acl_data_packet_length + 4; 5255bb5bc3eS[email protected] hci_dump_packet(HCI_ACL_DATA_PACKET, 0, packet, size); 526452cf3bbS[email protected] err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size); 527452cf3bbS[email protected] 528f04a0c31SMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop after send (more fragments %d)", more_fragments); 529d999b54eSMatthias Ringwald 530452cf3bbS[email protected] // done yet? 531452cf3bbS[email protected] if (!more_fragments) break; 532452cf3bbS[email protected] 533452cf3bbS[email protected] // can send more? 534452cf3bbS[email protected] if (!hci_can_send_prepared_acl_packet_now(connection->con_handle)) return err; 535452cf3bbS[email protected] } 536452cf3bbS[email protected] 537d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop over"); 538452cf3bbS[email protected] 539d051460cS[email protected] // release buffer now for synchronous transport 540203bace6S[email protected] if (hci_transport_synchronous()){ 541452cf3bbS[email protected] hci_release_packet_buffer(); 54263fa3374SMatthias Ringwald // notify upper stack that it might be possible to send again 54363fa3374SMatthias Ringwald uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; 544d6b06661SMatthias Ringwald hci_emit_event(&event[0], sizeof(event), 0); // don't dump 545452cf3bbS[email protected] } 546452cf3bbS[email protected] 547452cf3bbS[email protected] return err; 548452cf3bbS[email protected] } 549452cf3bbS[email protected] 550826f7347S[email protected] // pre: caller has reserved the packet buffer 551826f7347S[email protected] int hci_send_acl_packet_buffer(int size){ 5527856c818Smatthias.ringwald 553452cf3bbS[email protected] // log_info("hci_send_acl_packet_buffer size %u", size); 554452cf3bbS[email protected] 555826f7347S[email protected] if (!hci_stack->hci_packet_buffer_reserved) { 556826f7347S[email protected] log_error("hci_send_acl_packet_buffer called without reserving packet buffer"); 557826f7347S[email protected] return 0; 558826f7347S[email protected] } 559826f7347S[email protected] 560d713a683S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 561d713a683S[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 562d713a683S[email protected] 563826f7347S[email protected] // check for free places on Bluetooth module 564d713a683S[email protected] if (!hci_can_send_prepared_acl_packet_now(con_handle)) { 565826f7347S[email protected] log_error("hci_send_acl_packet_buffer called but no free ACL buffers on controller"); 56697b61c7bS[email protected] hci_release_packet_buffer(); 56797b61c7bS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 56897b61c7bS[email protected] } 5696218e6f1Smatthias.ringwald 5705061f3afS[email protected] hci_connection_t *connection = hci_connection_for_handle( con_handle); 57197b61c7bS[email protected] if (!connection) { 5725fa0b7cfS[email protected] log_error("hci_send_acl_packet_buffer called but no connection for handle 0x%04x", con_handle); 57397b61c7bS[email protected] hci_release_packet_buffer(); 57497b61c7bS[email protected] return 0; 57597b61c7bS[email protected] } 57656cf178bSmatthias.ringwald hci_connection_timestamp(connection); 57756cf178bSmatthias.ringwald 578452cf3bbS[email protected] // hci_dump_packet( HCI_ACL_DATA_PACKET, 0, packet, size); 5797856c818Smatthias.ringwald 580452cf3bbS[email protected] // setup data 581452cf3bbS[email protected] hci_stack->acl_fragmentation_total_size = size; 582452cf3bbS[email protected] hci_stack->acl_fragmentation_pos = 4; // start of L2CAP packet 5836218e6f1Smatthias.ringwald 584452cf3bbS[email protected] return hci_send_acl_packet_fragments(connection); 585ee091cf1Smatthias.ringwald } 586ee091cf1Smatthias.ringwald 587*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 58844d0e3d5S[email protected] // pre: caller has reserved the packet buffer 58944d0e3d5S[email protected] int hci_send_sco_packet_buffer(int size){ 59044d0e3d5S[email protected] 59144d0e3d5S[email protected] // log_info("hci_send_acl_packet_buffer size %u", size); 59244d0e3d5S[email protected] 59344d0e3d5S[email protected] if (!hci_stack->hci_packet_buffer_reserved) { 59444d0e3d5S[email protected] log_error("hci_send_acl_packet_buffer called without reserving packet buffer"); 59544d0e3d5S[email protected] return 0; 59644d0e3d5S[email protected] } 59744d0e3d5S[email protected] 59844d0e3d5S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 5994b3e1e19SMatthias Ringwald 6004b3e1e19SMatthias Ringwald // skip checks in loopback mode 6014b3e1e19SMatthias Ringwald if (!hci_stack->loopback_mode){ 60244d0e3d5S[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); // same for ACL and SCO 60344d0e3d5S[email protected] 60444d0e3d5S[email protected] // check for free places on Bluetooth module 605701e3307SMatthias Ringwald if (!hci_can_send_prepared_sco_packet_now()) { 60644d0e3d5S[email protected] log_error("hci_send_sco_packet_buffer called but no free ACL buffers on controller"); 60744d0e3d5S[email protected] hci_release_packet_buffer(); 60844d0e3d5S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 60944d0e3d5S[email protected] } 61044d0e3d5S[email protected] 611e35edcc1S[email protected] // track send packet in connection struct 612e35edcc1S[email protected] hci_connection_t *connection = hci_connection_for_handle( con_handle); 613e35edcc1S[email protected] if (!connection) { 614e35edcc1S[email protected] log_error("hci_send_sco_packet_buffer called but no connection for handle 0x%04x", con_handle); 615e35edcc1S[email protected] hci_release_packet_buffer(); 616e35edcc1S[email protected] return 0; 617e35edcc1S[email protected] } 618e35edcc1S[email protected] connection->num_sco_packets_sent++; 6194b3e1e19SMatthias Ringwald } 62044d0e3d5S[email protected] 62144d0e3d5S[email protected] hci_dump_packet( HCI_SCO_DATA_PACKET, 0, packet, size); 622543e835cSMatthias Ringwald int err = hci_stack->hci_transport->send_packet(HCI_SCO_DATA_PACKET, packet, size); 623543e835cSMatthias Ringwald 624543e835cSMatthias Ringwald if (hci_transport_synchronous()){ 625543e835cSMatthias Ringwald hci_release_packet_buffer(); 62663fa3374SMatthias Ringwald // notify upper stack that it might be possible to send again 62763fa3374SMatthias Ringwald uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; 628d6b06661SMatthias Ringwald hci_emit_event(&event[0], sizeof(event), 0); // don't dump 629543e835cSMatthias Ringwald } 630543e835cSMatthias Ringwald 631543e835cSMatthias Ringwald return err; 63244d0e3d5S[email protected] } 633*35454696SMatthias Ringwald #endif 63444d0e3d5S[email protected] 63516833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){ 6367856c818Smatthias.ringwald 637e76a89eeS[email protected] // log_info("acl_handler: size %u", size); 638e76a89eeS[email protected] 6397856c818Smatthias.ringwald // get info 6407856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 6415061f3afS[email protected] hci_connection_t *conn = hci_connection_for_handle(con_handle); 6427856c818Smatthias.ringwald uint8_t acl_flags = READ_ACL_FLAGS(packet); 6437856c818Smatthias.ringwald uint16_t acl_length = READ_ACL_LENGTH(packet); 6447856c818Smatthias.ringwald 6457856c818Smatthias.ringwald // ignore non-registered handle 6467856c818Smatthias.ringwald if (!conn){ 6479da54300S[email protected] log_error( "hci.c: acl_handler called with non-registered handle %u!" , con_handle); 6487856c818Smatthias.ringwald return; 6497856c818Smatthias.ringwald } 6507856c818Smatthias.ringwald 651e76a89eeS[email protected] // assert packet is complete 6529ecc3e17S[email protected] if (acl_length + 4 != size){ 653f04a0c31SMatthias Ringwald log_error("hci.c: acl_handler called with ACL packet of wrong size %d, expected %u => dropping packet", size, acl_length + 4); 654e76a89eeS[email protected] return; 655e76a89eeS[email protected] } 656e76a89eeS[email protected] 6577856c818Smatthias.ringwald // update idle timestamp 6587856c818Smatthias.ringwald hci_connection_timestamp(conn); 6597856c818Smatthias.ringwald 6607856c818Smatthias.ringwald // handle different packet types 6617856c818Smatthias.ringwald switch (acl_flags & 0x03) { 6627856c818Smatthias.ringwald 6637856c818Smatthias.ringwald case 0x01: // continuation fragment 6647856c818Smatthias.ringwald 6650ca847afS[email protected] // sanity checks 6667856c818Smatthias.ringwald if (conn->acl_recombination_pos == 0) { 6679da54300S[email protected] log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x", con_handle); 6687856c818Smatthias.ringwald return; 6697856c818Smatthias.ringwald } 6700ca847afS[email protected] if (conn->acl_recombination_pos + acl_length > 4 + HCI_ACL_BUFFER_SIZE){ 6710ca847afS[email protected] log_error( "ACL Cont Fragment to large: combined packet %u > buffer size %u for handle 0x%02x", 6720ca847afS[email protected] conn->acl_recombination_pos + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle); 6730ca847afS[email protected] conn->acl_recombination_pos = 0; 6740ca847afS[email protected] return; 6750ca847afS[email protected] } 6767856c818Smatthias.ringwald 6777856c818Smatthias.ringwald // append fragment payload (header already stored) 678ec6321eeS[email protected] memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + conn->acl_recombination_pos], &packet[4], acl_length ); 6797856c818Smatthias.ringwald conn->acl_recombination_pos += acl_length; 6807856c818Smatthias.ringwald 6819da54300S[email protected] // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u", acl_length, 682decc01a8Smatthias.ringwald // conn->acl_recombination_pos, conn->acl_recombination_length); 6837856c818Smatthias.ringwald 6847856c818Smatthias.ringwald // forward complete L2CAP packet if complete. 6857856c818Smatthias.ringwald if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header 686d6b06661SMatthias Ringwald hci_emit_acl_packet(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], conn->acl_recombination_pos); 6877856c818Smatthias.ringwald // reset recombination buffer 6887856c818Smatthias.ringwald conn->acl_recombination_length = 0; 6897856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 6907856c818Smatthias.ringwald } 6917856c818Smatthias.ringwald break; 6927856c818Smatthias.ringwald 6937856c818Smatthias.ringwald case 0x02: { // first fragment 6947856c818Smatthias.ringwald 69523a77e1aS[email protected] // sanity check 69623a77e1aS[email protected] if (conn->acl_recombination_pos) { 69723a77e1aS[email protected] log_error( "ACL First Fragment but data in buffer for handle 0x%02x, dropping stale fragments", con_handle); 69823a77e1aS[email protected] conn->acl_recombination_pos = 0; 69923a77e1aS[email protected] } 70023a77e1aS[email protected] 7017856c818Smatthias.ringwald // peek into L2CAP packet! 7027856c818Smatthias.ringwald uint16_t l2cap_length = READ_L2CAP_LENGTH( packet ); 7037856c818Smatthias.ringwald 7049da54300S[email protected] // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u", acl_length, l2cap_length); 705decc01a8Smatthias.ringwald 7067856c818Smatthias.ringwald // compare fragment size to L2CAP packet size 7077856c818Smatthias.ringwald if (acl_length >= l2cap_length + 4){ 7087856c818Smatthias.ringwald // forward fragment as L2CAP packet 709d6b06661SMatthias Ringwald hci_emit_acl_packet(packet, acl_length + 4); 7107856c818Smatthias.ringwald } else { 7110ca847afS[email protected] 7120ca847afS[email protected] if (acl_length > HCI_ACL_BUFFER_SIZE){ 7130ca847afS[email protected] log_error( "ACL First Fragment to large: fragment %u > buffer size %u for handle 0x%02x", 7140ca847afS[email protected] 4 + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle); 7150ca847afS[email protected] return; 7160ca847afS[email protected] } 7170ca847afS[email protected] 7187856c818Smatthias.ringwald // store first fragment and tweak acl length for complete package 719ec6321eeS[email protected] memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], packet, acl_length + 4); 7207856c818Smatthias.ringwald conn->acl_recombination_pos = acl_length + 4; 7217856c818Smatthias.ringwald conn->acl_recombination_length = l2cap_length; 722f8fbdce0SMatthias Ringwald little_endian_store_16(conn->acl_recombination_buffer, HCI_INCOMING_PRE_BUFFER_SIZE + 2, l2cap_length +4); 7237856c818Smatthias.ringwald } 7247856c818Smatthias.ringwald break; 7257856c818Smatthias.ringwald 7267856c818Smatthias.ringwald } 7277856c818Smatthias.ringwald default: 7289da54300S[email protected] log_error( "hci.c: acl_handler called with invalid packet boundary flags %u", acl_flags & 0x03); 7297856c818Smatthias.ringwald return; 7307856c818Smatthias.ringwald } 73194ab26f8Smatthias.ringwald 73294ab26f8Smatthias.ringwald // execute main loop 73394ab26f8Smatthias.ringwald hci_run(); 73416833f0aSmatthias.ringwald } 73522909952Smatthias.ringwald 73667a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){ 7379da54300S[email protected] log_info("Connection closed: handle 0x%x, %s", conn->con_handle, bd_addr_to_str(conn->address)); 7383c4d4b90Smatthias.ringwald 739528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&conn->timeout); 740c785ef68Smatthias.ringwald 741665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 742a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 7433c4d4b90Smatthias.ringwald 7443c4d4b90Smatthias.ringwald // now it's gone 745c7e0c5f6Smatthias.ringwald hci_emit_nr_connections_changed(); 746c7e0c5f6Smatthias.ringwald } 747c7e0c5f6Smatthias.ringwald 748*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 749*35454696SMatthias Ringwald 7500c042179S[email protected] static const uint16_t packet_type_sizes[] = { 7518f8108aaSmatthias.ringwald 0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE, 7528f8108aaSmatthias.ringwald HCI_ACL_DH1_SIZE, 0, 0, 0, 7538f8108aaSmatthias.ringwald HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE, 7548f8108aaSmatthias.ringwald HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE 7558f8108aaSmatthias.ringwald }; 75665389bfcS[email protected] static const uint8_t packet_type_feature_requirement_bit[] = { 75765389bfcS[email protected] 0, // 3 slot packets 75865389bfcS[email protected] 1, // 5 slot packets 75965389bfcS[email protected] 25, // EDR 2 mpbs 76065389bfcS[email protected] 26, // EDR 3 mbps 76165389bfcS[email protected] 39, // 3 slot EDR packts 76265389bfcS[email protected] 40, // 5 slot EDR packet 76365389bfcS[email protected] }; 76465389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = { 76565389bfcS[email protected] 0x0f00, // 3 slot packets 76665389bfcS[email protected] 0xf000, // 5 slot packets 76765389bfcS[email protected] 0x1102, // EDR 2 mpbs 76865389bfcS[email protected] 0x2204, // EDR 3 mbps 76965389bfcS[email protected] 0x0300, // 3 slot EDR packts 77065389bfcS[email protected] 0x3000, // 5 slot EDR packet 77165389bfcS[email protected] }; 7728f8108aaSmatthias.ringwald 77365389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){ 77465389bfcS[email protected] // enable packet types based on size 7758f8108aaSmatthias.ringwald uint16_t packet_types = 0; 776f16a69bbS[email protected] unsigned int i; 7778f8108aaSmatthias.ringwald for (i=0;i<16;i++){ 7788f8108aaSmatthias.ringwald if (packet_type_sizes[i] == 0) continue; 7798f8108aaSmatthias.ringwald if (packet_type_sizes[i] <= buffer_size){ 7808f8108aaSmatthias.ringwald packet_types |= 1 << i; 7818f8108aaSmatthias.ringwald } 7828f8108aaSmatthias.ringwald } 78365389bfcS[email protected] // disable packet types due to missing local supported features 78465389bfcS[email protected] for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){ 785f04a0c31SMatthias Ringwald unsigned int bit_idx = packet_type_feature_requirement_bit[i]; 78665389bfcS[email protected] int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0; 78765389bfcS[email protected] if (feature_set) continue; 78865389bfcS[email protected] log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]); 78965389bfcS[email protected] packet_types &= ~packet_type_feature_packet_mask[i]; 79065389bfcS[email protected] } 7918f8108aaSmatthias.ringwald // flip bits for "may not be used" 7928f8108aaSmatthias.ringwald packet_types ^= 0x3306; 7938f8108aaSmatthias.ringwald return packet_types; 7948f8108aaSmatthias.ringwald } 7958f8108aaSmatthias.ringwald 7968f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){ 7973a9fb326S[email protected] return hci_stack->packet_types; 7988f8108aaSmatthias.ringwald } 799*35454696SMatthias Ringwald #endif 8008f8108aaSmatthias.ringwald 801facf93fdS[email protected] uint8_t* hci_get_outgoing_packet_buffer(void){ 8027dc17943Smatthias.ringwald // hci packet buffer is >= acl data packet length 8033a9fb326S[email protected] return hci_stack->hci_packet_buffer; 8047dc17943Smatthias.ringwald } 8057dc17943Smatthias.ringwald 806f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){ 8073a9fb326S[email protected] return hci_stack->acl_data_packet_length; 8087dc17943Smatthias.ringwald } 8097dc17943Smatthias.ringwald 8103e68d23dSMatthias Ringwald int hci_extended_sco_link_supported(void){ 8113e68d23dSMatthias Ringwald // No. 31, byte 3, bit 7 8123e68d23dSMatthias Ringwald return (hci_stack->local_supported_features[3] & (1 << 7)) != 0; 8133e68d23dSMatthias Ringwald } 8143e68d23dSMatthias Ringwald 8156ac9a97eS[email protected] int hci_non_flushable_packet_boundary_flag_supported(void){ 8166ac9a97eS[email protected] // No. 54, byte 6, bit 6 8176ac9a97eS[email protected] return (hci_stack->local_supported_features[6] & (1 << 6)) != 0; 8186ac9a97eS[email protected] } 8196ac9a97eS[email protected] 82015a95bd5SMatthias Ringwald static int gap_ssp_supported(void){ 8216ac9a97eS[email protected] // No. 51, byte 6, bit 3 8223a9fb326S[email protected] return (hci_stack->local_supported_features[6] & (1 << 3)) != 0; 823f5d8d141S[email protected] } 824f5d8d141S[email protected] 8257f02f414SMatthias Ringwald static int hci_classic_supported(void){ 826*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 8276ac9a97eS[email protected] // No. 37, byte 4, bit 5, = No BR/EDR Support 8283a9fb326S[email protected] return (hci_stack->local_supported_features[4] & (1 << 5)) == 0; 829*35454696SMatthias Ringwald #else 830*35454696SMatthias Ringwald return 0; 831*35454696SMatthias Ringwald #endif 832f5d8d141S[email protected] } 833f5d8d141S[email protected] 8347f02f414SMatthias Ringwald static int hci_le_supported(void){ 835a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 8366ac9a97eS[email protected] // No. 37, byte 4, bit 6 = LE Supported (Controller) 8373a9fb326S[email protected] return (hci_stack->local_supported_features[4] & (1 << 6)) != 0; 838f5d8d141S[email protected] #else 839f5d8d141S[email protected] return 0; 840f5d8d141S[email protected] #endif 841f5d8d141S[email protected] } 842f5d8d141S[email protected] 84369a97523S[email protected] // get addr type and address used in advertisement packets 84415a95bd5SMatthias Ringwald void gap_advertisements_get_address(uint8_t * addr_type, bd_addr_t addr){ 8453a9fb326S[email protected] *addr_type = hci_stack->adv_addr_type; 8463a9fb326S[email protected] if (hci_stack->adv_addr_type){ 8473a9fb326S[email protected] memcpy(addr, hci_stack->adv_address, 6); 84869a97523S[email protected] } else { 8493a9fb326S[email protected] memcpy(addr, hci_stack->local_bd_addr, 6); 85069a97523S[email protected] } 85169a97523S[email protected] } 85269a97523S[email protected] 853a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 85439677e66SMatthias Ringwald void le_handle_advertisement_report(uint8_t *packet, int size){ 855d1dc057bS[email protected] int offset = 3; 856d1dc057bS[email protected] int num_reports = packet[offset]; 857d1dc057bS[email protected] offset += 1; 858d1dc057bS[email protected] 85957c9da5bS[email protected] int i; 8604f4e0224SMatthias Ringwald // log_info("HCI: handle adv report with num reports: %d", num_reports); 86103fbe9c6S[email protected] uint8_t event[12 + LE_ADVERTISING_DATA_SIZE]; // use upper bound to avoid var size automatic var 86257c9da5bS[email protected] for (i=0; i<num_reports;i++){ 863210c6774S[email protected] uint8_t data_length = packet[offset + 8]; 864a0aac9c4S[email protected] uint8_t event_size = 10 + data_length; 865d1dc057bS[email protected] int pos = 0; 866045013feSMatthias Ringwald event[pos++] = GAP_EVENT_ADVERTISING_REPORT; 86757c9da5bS[email protected] event[pos++] = event_size; 868210c6774S[email protected] memcpy(&event[pos], &packet[offset], 1+1+6); // event type + address type + address 869d1dc057bS[email protected] offset += 8; 870d1dc057bS[email protected] pos += 8; 871d1dc057bS[email protected] event[pos++] = packet[offset + 1 + data_length]; // rssi 872d1dc057bS[email protected] event[pos++] = packet[offset++]; //data_length; 873d1dc057bS[email protected] memcpy(&event[pos], &packet[offset], data_length); 87457c9da5bS[email protected] pos += data_length; 875d1dc057bS[email protected] offset += data_length + 1; // rssi 876d6b06661SMatthias Ringwald hci_emit_event(event, pos, 1); 87757c9da5bS[email protected] } 87857c9da5bS[email protected] } 879b2f949feS[email protected] #endif 88057c9da5bS[email protected] 88196b53536SMatthias Ringwald static uint32_t hci_transport_uart_get_main_baud_rate(void){ 88296b53536SMatthias Ringwald if (!hci_stack->config) return 0; 8839796ebeaSMatthias Ringwald uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 88496b53536SMatthias Ringwald // Limit baud rate for Broadcom chipsets to 3 mbps 885c6d1dbedSMatthias Ringwald if (hci_stack->manufacturer == COMPANY_ID_BROADCOM_CORPORATION && baud_rate > 3000000){ 88696b53536SMatthias Ringwald baud_rate = 3000000; 88796b53536SMatthias Ringwald } 88896b53536SMatthias Ringwald return baud_rate; 88996b53536SMatthias Ringwald } 89096b53536SMatthias Ringwald 891ec820d77SMatthias Ringwald static void hci_initialization_timeout_handler(btstack_timer_source_t * ds){ 8920305bdeaSMatthias Ringwald switch (hci_stack->substate){ 8930305bdeaSMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 8947b0d7667SMatthias Ringwald log_info("Resend HCI Reset"); 8950305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET; 8967b0d7667SMatthias Ringwald hci_stack->num_cmd_packets = 1; 8970305bdeaSMatthias Ringwald hci_run(); 8980305bdeaSMatthias Ringwald break; 8999f007422SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET: 9009f007422SMatthias Ringwald log_info("Resend HCI Reset - CSR Warm Boot with Link Reset"); 9019f007422SMatthias Ringwald if (hci_stack->hci_transport->reset_link){ 9029f007422SMatthias Ringwald hci_stack->hci_transport->reset_link(); 9039f007422SMatthias Ringwald } 904202c8a4cSMatthias Ringwald // no break - explicit fallthrough to HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT 905e47e68c7SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT: 906e47e68c7SMatthias Ringwald log_info("Resend HCI Reset - CSR Warm Boot"); 907e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT; 908e47e68c7SMatthias Ringwald hci_stack->num_cmd_packets = 1; 909e47e68c7SMatthias Ringwald hci_run(); 910688c2635SMatthias Ringwald break; 9117224be7eSMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE: 9127224be7eSMatthias Ringwald if (hci_stack->hci_transport->set_baudrate){ 91396b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 914cd724cb7SMatthias Ringwald log_info("Local baud rate change to %"PRIu32"(timeout handler)", baud_rate); 9157dd9d0ecSMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 9167224be7eSMatthias Ringwald } 917772a36d3SMatthias Ringwald // For CSR, HCI Reset is sent on new baud rate 9182caefae9SMatthias Ringwald if (hci_stack->manufacturer == COMPANY_ID_CAMBRIDGE_SILICON_RADIO){ 919772a36d3SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT; 920772a36d3SMatthias Ringwald hci_run(); 921772a36d3SMatthias Ringwald } 9224696bddbSMatthias Ringwald break; 9230305bdeaSMatthias Ringwald default: 9240305bdeaSMatthias Ringwald break; 9250305bdeaSMatthias Ringwald } 9260305bdeaSMatthias Ringwald } 9270305bdeaSMatthias Ringwald 92871de195eSMatthias Ringwald static void hci_initializing_next_state(void){ 92974b323a9SMatthias Ringwald hci_stack->substate = (hci_substate_t )( ((int) hci_stack->substate) + 1); 93074b323a9SMatthias Ringwald } 93174b323a9SMatthias Ringwald 93274b323a9SMatthias Ringwald // assumption: hci_can_send_command_packet_now() == true 93371de195eSMatthias Ringwald static void hci_initializing_run(void){ 934148ca237SMatthias Ringwald log_debug("hci_initializing_run: substate %u, can send %u", hci_stack->substate, hci_can_send_command_packet_now()); 93574b323a9SMatthias Ringwald switch (hci_stack->substate){ 93674b323a9SMatthias Ringwald case HCI_INIT_SEND_RESET: 93774b323a9SMatthias Ringwald hci_state_reset(); 938a0cf2f3fSMatthias Ringwald 939423a3e49SMatthias Ringwald #ifndef HAVE_PLATFORM_IPHONE_OS 9400305bdeaSMatthias Ringwald // prepare reset if command complete not received in 100ms 941659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 942528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 943528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 944a0cf2f3fSMatthias Ringwald #endif 9450305bdeaSMatthias Ringwald // send command 94674b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 9470305bdeaSMatthias Ringwald hci_send_cmd(&hci_reset); 94874b323a9SMatthias Ringwald break; 94976fcb19bSMatthias Ringwald case HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION: 95076fcb19bSMatthias Ringwald hci_send_cmd(&hci_read_local_version_information); 95176fcb19bSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION; 95276fcb19bSMatthias Ringwald break; 953e47e68c7SMatthias Ringwald case HCI_INIT_SEND_RESET_CSR_WARM_BOOT: 954e47e68c7SMatthias Ringwald hci_state_reset(); 955e47e68c7SMatthias Ringwald // prepare reset if command complete not received in 100ms 956659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 957528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 958528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 959e47e68c7SMatthias Ringwald // send command 960e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT; 961e47e68c7SMatthias Ringwald hci_send_cmd(&hci_reset); 962e47e68c7SMatthias Ringwald break; 9638d29070eSMatthias Ringwald case HCI_INIT_SEND_RESET_ST_WARM_BOOT: 9648d29070eSMatthias Ringwald hci_state_reset(); 9658d29070eSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT; 9668d29070eSMatthias Ringwald hci_send_cmd(&hci_reset); 9678d29070eSMatthias Ringwald break; 968fab26ab3SMatthias Ringwald case HCI_INIT_SEND_BAUD_CHANGE: { 96996b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 9703fb36a29SMatthias Ringwald hci_stack->chipset->set_baudrate_command(baud_rate, hci_stack->hci_packet_buffer); 971f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 97274b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE; 9730305bdeaSMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]); 9744696bddbSMatthias Ringwald // STLC25000D: baudrate change happens within 0.5 s after command was send, 9754696bddbSMatthias Ringwald // use timer to update baud rate after 100 ms (knowing exactly, when command was sent is non-trivial) 976c6d1dbedSMatthias Ringwald if (hci_stack->manufacturer == COMPANY_ID_ST_MICROELECTRONICS){ 977659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 978528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 9794696bddbSMatthias Ringwald } 98074b323a9SMatthias Ringwald break; 981fab26ab3SMatthias Ringwald } 982fab26ab3SMatthias Ringwald case HCI_INIT_SEND_BAUD_CHANGE_BCM: { 98396b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 9843fb36a29SMatthias Ringwald hci_stack->chipset->set_baudrate_command(baud_rate, hci_stack->hci_packet_buffer); 985f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 986eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE_BCM; 987eb3a5314SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]); 988eb3a5314SMatthias Ringwald break; 989fab26ab3SMatthias Ringwald } 99074b323a9SMatthias Ringwald case HCI_INIT_CUSTOM_INIT: 99174b323a9SMatthias Ringwald // Custom initialization 9923fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->next_command){ 9933fb36a29SMatthias Ringwald int valid_cmd = (*hci_stack->chipset->next_command)(hci_stack->hci_packet_buffer); 99474b323a9SMatthias Ringwald if (valid_cmd){ 99574b323a9SMatthias Ringwald int size = 3 + hci_stack->hci_packet_buffer[2]; 996f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 99774b323a9SMatthias Ringwald hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, hci_stack->hci_packet_buffer, size); 998e47e68c7SMatthias Ringwald switch (valid_cmd) { 999e47e68c7SMatthias Ringwald case 1: 1000e47e68c7SMatthias Ringwald default: 1001e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT; 1002e47e68c7SMatthias Ringwald break; 1003e47e68c7SMatthias Ringwald case 2: // CSR Warm Boot: Wait a bit, then send HCI Reset until HCI Command Complete 1004e47e68c7SMatthias Ringwald log_info("CSR Warm Boot"); 1005659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1006528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1007528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 10082caefae9SMatthias Ringwald if (hci_stack->manufacturer == COMPANY_ID_CAMBRIDGE_SILICON_RADIO 1009772a36d3SMatthias Ringwald && hci_stack->config 10103fb36a29SMatthias Ringwald && hci_stack->chipset 10113fb36a29SMatthias Ringwald // && hci_stack->chipset->set_baudrate_command -- there's no such command 1012772a36d3SMatthias Ringwald && hci_stack->hci_transport->set_baudrate 10132caefae9SMatthias Ringwald && hci_transport_uart_get_main_baud_rate()){ 1014772a36d3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE; 1015772a36d3SMatthias Ringwald } else { 10169f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET; 1017772a36d3SMatthias Ringwald } 1018e47e68c7SMatthias Ringwald break; 1019e47e68c7SMatthias Ringwald } 10200305bdeaSMatthias Ringwald hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size); 102174b323a9SMatthias Ringwald break; 102274b323a9SMatthias Ringwald } 1023148ca237SMatthias Ringwald log_info("Init script done"); 102492a0d36dSMatthias Ringwald 102592a0d36dSMatthias Ringwald // Init script download causes baud rate to reset on Broadcom chipsets, restore UART baud rate if needed 1026c6d1dbedSMatthias Ringwald if (hci_stack->manufacturer == COMPANY_ID_BROADCOM_CORPORATION){ 102792a0d36dSMatthias Ringwald int need_baud_change = hci_stack->config 10283fb36a29SMatthias Ringwald && hci_stack->chipset 10293fb36a29SMatthias Ringwald && hci_stack->chipset->set_baudrate_command 103092a0d36dSMatthias Ringwald && hci_stack->hci_transport->set_baudrate 10319796ebeaSMatthias Ringwald && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 103292a0d36dSMatthias Ringwald if (need_baud_change) { 10339796ebeaSMatthias Ringwald uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_init; 1034cd724cb7SMatthias Ringwald log_info("Local baud rate change to %"PRIu32" after init script (bcm)", baud_rate); 103592a0d36dSMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 103692a0d36dSMatthias Ringwald } 103792a0d36dSMatthias Ringwald } 103874b323a9SMatthias Ringwald } 103974b323a9SMatthias Ringwald // otherwise continue 1040a828a756SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 1041a828a756SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 1042a828a756SMatthias Ringwald break; 1043d58dd308SMatthias Ringwald case HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS: 1044d58dd308SMatthias Ringwald log_info("Resend hci_read_local_supported_commands after CSR Warm Boot double reset"); 1045d58dd308SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 1046d58dd308SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 1047d58dd308SMatthias Ringwald break; 104853860077SMatthias Ringwald case HCI_INIT_SET_BD_ADDR: 104953860077SMatthias Ringwald log_info("Set Public BD ADDR to %s", bd_addr_to_str(hci_stack->custom_bd_addr)); 10503fb36a29SMatthias Ringwald hci_stack->chipset->set_bd_addr_command(hci_stack->custom_bd_addr, hci_stack->hci_packet_buffer); 1051f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 105253860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_BD_ADDR; 105353860077SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]); 105453860077SMatthias Ringwald break; 105553860077SMatthias Ringwald case HCI_INIT_READ_BD_ADDR: 105653860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_BD_ADDR; 105753860077SMatthias Ringwald hci_send_cmd(&hci_read_bd_addr); 105853860077SMatthias Ringwald break; 105974b323a9SMatthias Ringwald case HCI_INIT_READ_BUFFER_SIZE: 106074b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_BUFFER_SIZE; 10610305bdeaSMatthias Ringwald hci_send_cmd(&hci_read_buffer_size); 106274b323a9SMatthias Ringwald break; 106353860077SMatthias Ringwald case HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES: 106453860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_FEATURES; 10650305bdeaSMatthias Ringwald hci_send_cmd(&hci_read_local_supported_features); 106674b323a9SMatthias Ringwald break; 106774b323a9SMatthias Ringwald case HCI_INIT_SET_EVENT_MASK: 10680305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_EVENT_MASK; 106974b323a9SMatthias Ringwald if (hci_le_supported()){ 107074b323a9SMatthias Ringwald hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF); 107174b323a9SMatthias Ringwald } else { 107274b323a9SMatthias Ringwald // Kensington Bluetooth 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff... 107374b323a9SMatthias Ringwald hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF); 107474b323a9SMatthias Ringwald } 107574b323a9SMatthias Ringwald break; 1076*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 107774b323a9SMatthias Ringwald case HCI_INIT_WRITE_SIMPLE_PAIRING_MODE: 107874b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SIMPLE_PAIRING_MODE; 10790305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable); 108074b323a9SMatthias Ringwald break; 108174b323a9SMatthias Ringwald case HCI_INIT_WRITE_PAGE_TIMEOUT: 108274b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_PAGE_TIMEOUT; 10830305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_page_timeout, 0x6000); // ca. 15 sec 108474b323a9SMatthias Ringwald break; 108574b323a9SMatthias Ringwald case HCI_INIT_WRITE_CLASS_OF_DEVICE: 108674b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_CLASS_OF_DEVICE; 10870305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device); 108874b323a9SMatthias Ringwald break; 108974b323a9SMatthias Ringwald case HCI_INIT_WRITE_LOCAL_NAME: 10900305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_LOCAL_NAME; 109174b323a9SMatthias Ringwald if (hci_stack->local_name){ 109274b323a9SMatthias Ringwald hci_send_cmd(&hci_write_local_name, hci_stack->local_name); 109374b323a9SMatthias Ringwald } else { 10947224be7eSMatthias Ringwald char local_name[8+17+1]; 10957224be7eSMatthias Ringwald // BTstack 11:22:33:44:55:66 10967224be7eSMatthias Ringwald memcpy(local_name, "BTstack ", 8); 10977224be7eSMatthias Ringwald memcpy(&local_name[8], bd_addr_to_str(hci_stack->local_bd_addr), 17); // strlen(bd_addr_to_str(...)) = 17 10987224be7eSMatthias Ringwald local_name[8+17] = '\0'; 109993bcd4d0SMatthias Ringwald log_info("---> Name %s", local_name); 110093bcd4d0SMatthias Ringwald hci_send_cmd(&hci_write_local_name, local_name); 110174b323a9SMatthias Ringwald } 110274b323a9SMatthias Ringwald break; 1103ff00ed1cSMatthias Ringwald case HCI_INIT_WRITE_EIR_DATA: 11048a114470SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_EIR_DATA; 1105ff00ed1cSMatthias Ringwald hci_send_cmd(&hci_write_extended_inquiry_response, 0, hci_stack->eir_data); 1106ff00ed1cSMatthias Ringwald break; 1107f6858d14SMatthias Ringwald case HCI_INIT_WRITE_INQUIRY_MODE: 1108f6858d14SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_INQUIRY_MODE; 11098a114470SMatthias Ringwald hci_send_cmd(&hci_write_inquiry_mode, (int) hci_stack->inquiry_mode); 1110f6858d14SMatthias Ringwald break; 111174b323a9SMatthias Ringwald case HCI_INIT_WRITE_SCAN_ENABLE: 111274b323a9SMatthias Ringwald hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan 111374b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SCAN_ENABLE; 111474b323a9SMatthias Ringwald break; 1115483c5078SMatthias Ringwald // only sent if ENABLE_SCO_OVER_HCI is defined 1116729ed62eSMatthias Ringwald case HCI_INIT_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE: 1117729ed62eSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE; 1118729ed62eSMatthias Ringwald hci_send_cmd(&hci_write_synchronous_flow_control_enable, 1); // SCO tracking enabled 1119729ed62eSMatthias Ringwald break; 1120483c5078SMatthias Ringwald case HCI_INIT_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING: 1121483c5078SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING; 1122483c5078SMatthias Ringwald hci_send_cmd(&hci_write_default_erroneous_data_reporting, 1); 1123483c5078SMatthias Ringwald break; 1124*35454696SMatthias Ringwald #endif 1125a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 112674b323a9SMatthias Ringwald // LE INIT 112774b323a9SMatthias Ringwald case HCI_INIT_LE_READ_BUFFER_SIZE: 112874b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_READ_BUFFER_SIZE; 11290305bdeaSMatthias Ringwald hci_send_cmd(&hci_le_read_buffer_size); 113074b323a9SMatthias Ringwald break; 113174b323a9SMatthias Ringwald case HCI_INIT_WRITE_LE_HOST_SUPPORTED: 113274b323a9SMatthias Ringwald // LE Supported Host = 1, Simultaneous Host = 0 113374b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_LE_HOST_SUPPORTED; 11340305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_le_host_supported, 1, 0); 113574b323a9SMatthias Ringwald break; 11363b6d4121SMatthias Ringwald case HCI_INIT_READ_WHITE_LIST_SIZE: 11373b6d4121SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_WHITE_LIST_SIZE; 11383b6d4121SMatthias Ringwald hci_send_cmd(&hci_le_read_white_list_size); 11393b6d4121SMatthias Ringwald break; 114074b323a9SMatthias Ringwald case HCI_INIT_LE_SET_SCAN_PARAMETERS: 114174b323a9SMatthias Ringwald // LE Scan Parameters: active scanning, 300 ms interval, 30 ms window, public address, accept all advs 114274b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_SET_SCAN_PARAMETERS; 11430305bdeaSMatthias Ringwald hci_send_cmd(&hci_le_set_scan_parameters, 1, 0x1e0, 0x30, 0, 0); 114474b323a9SMatthias Ringwald break; 114574b323a9SMatthias Ringwald #endif 114674b323a9SMatthias Ringwald default: 114774b323a9SMatthias Ringwald return; 114874b323a9SMatthias Ringwald } 114955975f88SMatthias Ringwald } 115055975f88SMatthias Ringwald 1151a650ba4dSMatthias Ringwald static void hci_init_done(void){ 1152a650ba4dSMatthias Ringwald // done. tell the app 1153a650ba4dSMatthias Ringwald log_info("hci_init_done -> HCI_STATE_WORKING"); 1154a650ba4dSMatthias Ringwald hci_stack->state = HCI_STATE_WORKING; 1155a650ba4dSMatthias Ringwald hci_emit_state(); 1156a650ba4dSMatthias Ringwald hci_run(); 1157a650ba4dSMatthias Ringwald } 1158a650ba4dSMatthias Ringwald 11596155b3d3S[email protected] static void hci_initializing_event_handler(uint8_t * packet, uint16_t size){ 1160a2481739S[email protected] uint8_t command_completed = 0; 1161c4633093SMatthias Ringwald 11620e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE){ 1163f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 11646155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 1165a2481739S[email protected] command_completed = 1; 1166148ca237SMatthias Ringwald log_debug("Command complete for expected opcode %04x at substate %u", opcode, hci_stack->substate); 11676155b3d3S[email protected] } else { 1168d58dd308SMatthias Ringwald log_info("Command complete for different opcode %04x, expected %04x, at substate %u", opcode, hci_stack->last_cmd_opcode, hci_stack->substate); 11696155b3d3S[email protected] } 11706155b3d3S[email protected] } 11710f97eae7SMatthias Ringwald 11720e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_STATUS){ 11736155b3d3S[email protected] uint8_t status = packet[2]; 1174f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,4); 11756155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 11766155b3d3S[email protected] if (status){ 1177a2481739S[email protected] command_completed = 1; 1178148ca237SMatthias Ringwald log_debug("Command status error 0x%02x for expected opcode %04x at substate %u", status, opcode, hci_stack->substate); 11796155b3d3S[email protected] } else { 11806155b3d3S[email protected] log_info("Command status OK for expected opcode %04x, waiting for command complete", opcode); 11816155b3d3S[email protected] } 11826155b3d3S[email protected] } else { 1183148ca237SMatthias Ringwald log_debug("Command status for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode); 11846155b3d3S[email protected] } 11856155b3d3S[email protected] } 11860f97eae7SMatthias Ringwald 1187e47e68c7SMatthias Ringwald // Vendor == CSR 11880e2df43fSMatthias Ringwald if (hci_stack->substate == HCI_INIT_W4_CUSTOM_INIT && hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC){ 1189e47e68c7SMatthias Ringwald // TODO: track actual command 1190e47e68c7SMatthias Ringwald command_completed = 1; 1191e47e68c7SMatthias Ringwald } 1192a2481739S[email protected] 11934e9daa6fSMatthias Ringwald // Vendor == Toshiba 11940e2df43fSMatthias Ringwald if (hci_stack->substate == HCI_INIT_W4_SEND_BAUD_CHANGE && hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC){ 11954e9daa6fSMatthias Ringwald // TODO: track actual command 11964e9daa6fSMatthias Ringwald command_completed = 1; 11974e9daa6fSMatthias Ringwald } 11984e9daa6fSMatthias Ringwald 11990f97eae7SMatthias Ringwald // Late response (> 100 ms) for HCI Reset e.g. on Toshiba TC35661: 12000f97eae7SMatthias Ringwald // Command complete for HCI Reset arrives after we've resent the HCI Reset command 12010f97eae7SMatthias Ringwald // 12020f97eae7SMatthias Ringwald // HCI Reset 12030f97eae7SMatthias Ringwald // Timeout 100 ms 12040f97eae7SMatthias Ringwald // HCI Reset 12050f97eae7SMatthias Ringwald // Command Complete Reset 12060f97eae7SMatthias Ringwald // HCI Read Local Version Information 12070f97eae7SMatthias Ringwald // Command Complete Reset - but we expected Command Complete Read Local Version Information 12080f97eae7SMatthias Ringwald // hang... 12090f97eae7SMatthias Ringwald // 12100f97eae7SMatthias Ringwald // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend 12110f97eae7SMatthias Ringwald if (!command_completed 12120e2df43fSMatthias Ringwald && hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE 12130f97eae7SMatthias Ringwald && hci_stack->substate == HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION){ 12140f97eae7SMatthias Ringwald 1215f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 12160f97eae7SMatthias Ringwald if (opcode == hci_reset.opcode){ 12170f97eae7SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION; 12180f97eae7SMatthias Ringwald return; 12190f97eae7SMatthias Ringwald } 12200f97eae7SMatthias Ringwald } 12210f97eae7SMatthias Ringwald 12229f007422SMatthias Ringwald // CSR & H5 12239f007422SMatthias Ringwald // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend 12249f007422SMatthias Ringwald if (!command_completed 12259f007422SMatthias Ringwald && hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE 12269f007422SMatthias Ringwald && hci_stack->substate == HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS){ 12279f007422SMatthias Ringwald 12289f007422SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 12299f007422SMatthias Ringwald if (opcode == hci_reset.opcode){ 12309f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS; 12319f007422SMatthias Ringwald return; 12329f007422SMatthias Ringwald } 12339f007422SMatthias Ringwald } 12349f007422SMatthias Ringwald 12359f007422SMatthias 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 12369f007422SMatthias Ringwald // fix: Correct substate and behave as command below 12379f007422SMatthias Ringwald if (command_completed){ 12389f007422SMatthias Ringwald switch (hci_stack->substate){ 12399f007422SMatthias Ringwald case HCI_INIT_SEND_RESET: 12409f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 12419f007422SMatthias Ringwald break; 12429f007422SMatthias Ringwald case HCI_INIT_SEND_RESET_CSR_WARM_BOOT: 12439f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT; 12449f007422SMatthias Ringwald break; 12459f007422SMatthias Ringwald default: 12469f007422SMatthias Ringwald break; 12479f007422SMatthias Ringwald } 12489f007422SMatthias Ringwald } 12490f97eae7SMatthias Ringwald 12500f97eae7SMatthias Ringwald 1251a2481739S[email protected] if (!command_completed) return; 1252a2481739S[email protected] 1253db8bc6ffSMatthias Ringwald int need_baud_change = hci_stack->config 12543fb36a29SMatthias Ringwald && hci_stack->chipset 12553fb36a29SMatthias Ringwald && hci_stack->chipset->set_baudrate_command 1256db8bc6ffSMatthias Ringwald && hci_stack->hci_transport->set_baudrate 12579796ebeaSMatthias Ringwald && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 1258db8bc6ffSMatthias Ringwald 1259db8bc6ffSMatthias Ringwald int need_addr_change = hci_stack->custom_bd_addr_set 12603fb36a29SMatthias Ringwald && hci_stack->chipset 12613fb36a29SMatthias Ringwald && hci_stack->chipset->set_bd_addr_command; 1262a80162e9SMatthias Ringwald 12635c363727SMatthias Ringwald switch(hci_stack->substate){ 12649f007422SMatthias Ringwald case HCI_INIT_SEND_RESET: 1265d58dd308SMatthias Ringwald // on CSR with BCSP/H5, resend triggers resend of HCI Reset and leads to substate == HCI_INIT_SEND_RESET 12669f007422SMatthias Ringwald // fix: just correct substate and behave as command below 12679f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 12689f007422SMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 12699f007422SMatthias Ringwald break; 127074b323a9SMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 1271528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 127276fcb19bSMatthias Ringwald break; 127376fcb19bSMatthias Ringwald case HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION: 1274f04a0c31SMatthias Ringwald log_info("Received local version info, need baud change %d", need_baud_change); 12752f48d920SMatthias Ringwald if (need_baud_change){ 12762f48d920SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE; 12772f48d920SMatthias Ringwald return; 12782f48d920SMatthias Ringwald } 127953860077SMatthias Ringwald // skip baud change 128074b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 128174b323a9SMatthias Ringwald return; 1282a80162e9SMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE: 12834696bddbSMatthias Ringwald // for STLC2500D, baud rate change already happened. 1284fab26ab3SMatthias Ringwald // for others, baud rate gets changed now 12857224be7eSMatthias Ringwald if ((hci_stack->manufacturer != COMPANY_ID_ST_MICROELECTRONICS) && need_baud_change){ 128696b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1287cd724cb7SMatthias Ringwald log_info("Local baud rate change to %"PRIu32"(w4_send_baud_change)", baud_rate); 1288fab26ab3SMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 12894696bddbSMatthias Ringwald } 129074b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 129174b323a9SMatthias Ringwald return; 1292a80162e9SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT: 1293528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 1294a80162e9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 1295a80162e9SMatthias Ringwald return; 129674b323a9SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT: 129774b323a9SMatthias Ringwald // repeat custom init 129874b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 129974b323a9SMatthias Ringwald return; 1300a828a756SMatthias Ringwald case HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS: 1301c6d1dbedSMatthias Ringwald if (need_baud_change && hci_stack->manufacturer == COMPANY_ID_BROADCOM_CORPORATION){ 1302eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE_BCM; 1303eb3a5314SMatthias Ringwald return; 1304eb3a5314SMatthias Ringwald } 130553860077SMatthias Ringwald if (need_addr_change){ 130653860077SMatthias Ringwald hci_stack->substate = HCI_INIT_SET_BD_ADDR; 130753860077SMatthias Ringwald return; 130853860077SMatthias Ringwald } 130953860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 131053860077SMatthias Ringwald return; 13117224be7eSMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE_BCM: 13127224be7eSMatthias Ringwald if (need_baud_change){ 131396b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1314cd724cb7SMatthias Ringwald log_info("Local baud rate change to %"PRIu32"(w4_send_baud_change_bcm))", baud_rate); 1315fab26ab3SMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 13167224be7eSMatthias Ringwald } 1317eb3a5314SMatthias Ringwald if (need_addr_change){ 1318eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_SET_BD_ADDR; 1319eb3a5314SMatthias Ringwald return; 1320eb3a5314SMatthias Ringwald } 1321eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 1322eb3a5314SMatthias Ringwald return; 132353860077SMatthias Ringwald case HCI_INIT_W4_SET_BD_ADDR: 132453860077SMatthias Ringwald // for STLC2500D, bd addr change only gets active after sending reset command 1325c6d1dbedSMatthias Ringwald if (hci_stack->manufacturer == COMPANY_ID_ST_MICROELECTRONICS){ 132653860077SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_ST_WARM_BOOT; 132753860077SMatthias Ringwald return; 132853860077SMatthias Ringwald } 132953860077SMatthias Ringwald // skipping st warm boot 133053860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 133153860077SMatthias Ringwald return; 133253860077SMatthias Ringwald case HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT: 133353860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 133453860077SMatthias Ringwald return; 133553860077SMatthias Ringwald case HCI_INIT_W4_READ_BD_ADDR: 133653860077SMatthias Ringwald // only read buffer size if supported 133753860077SMatthias Ringwald if (hci_stack->local_supported_commands[0] & 0x01) { 133853860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BUFFER_SIZE; 133953860077SMatthias Ringwald return; 134053860077SMatthias Ringwald } 134153860077SMatthias Ringwald // skipping read buffer size 134253860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES; 1343a828a756SMatthias Ringwald return; 134474b323a9SMatthias Ringwald case HCI_INIT_W4_SET_EVENT_MASK: 13456155b3d3S[email protected] // skip Classic init commands for LE only chipsets 13466155b3d3S[email protected] if (!hci_classic_supported()){ 13476155b3d3S[email protected] if (hci_le_supported()){ 134874b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_LE_READ_BUFFER_SIZE; // skip all classic command 134974b323a9SMatthias Ringwald return; 13506155b3d3S[email protected] } else { 13516155b3d3S[email protected] log_error("Neither BR/EDR nor LE supported"); 1352a650ba4dSMatthias Ringwald hci_init_done(); 135374b323a9SMatthias Ringwald return; 13546155b3d3S[email protected] } 13556155b3d3S[email protected] } 135615a95bd5SMatthias Ringwald if (!gap_ssp_supported()){ 13575c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_WRITE_PAGE_TIMEOUT; 135874b323a9SMatthias Ringwald return; 13596155b3d3S[email protected] } 13606155b3d3S[email protected] break; 1361a828a756SMatthias Ringwald case HCI_INIT_W4_LE_READ_BUFFER_SIZE: 1362a828a756SMatthias Ringwald // skip write le host if not supported (e.g. on LE only EM9301) 1363a828a756SMatthias Ringwald if (hci_stack->local_supported_commands[0] & 0x02) break; 1364a828a756SMatthias Ringwald hci_stack->substate = HCI_INIT_LE_SET_SCAN_PARAMETERS; 1365a828a756SMatthias Ringwald return; 1366ff00ed1cSMatthias Ringwald case HCI_INIT_W4_WRITE_LOCAL_NAME: 1367ff00ed1cSMatthias Ringwald // skip write eir data if no eir data set 1368ff00ed1cSMatthias Ringwald if (hci_stack->eir_data) break; 1369ff00ed1cSMatthias Ringwald hci_stack->substate = HCI_INIT_WRITE_INQUIRY_MODE; 1370ff00ed1cSMatthias Ringwald return; 1371729ed62eSMatthias Ringwald 137271c4de7aSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 1373729ed62eSMatthias Ringwald case HCI_INIT_W4_WRITE_SCAN_ENABLE: 13743905afbfSMatthias Ringwald // skip write synchronous flow control if not supported 13753905afbfSMatthias Ringwald if (hci_stack->local_supported_commands[0] & 0x04) break; 13763905afbfSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE; 13773905afbfSMatthias Ringwald // explicit fall through to reduce repetitions 13783905afbfSMatthias Ringwald 1379729ed62eSMatthias Ringwald case HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE: 13803905afbfSMatthias Ringwald // skip write default erroneous data reporting if not supported 13813905afbfSMatthias Ringwald if (hci_stack->local_supported_commands[0] & 0x08) break; 13823905afbfSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING; 13833905afbfSMatthias Ringwald // explicit fall through to reduce repetitions 13843905afbfSMatthias Ringwald 1385f064e0bbSMatthias Ringwald case HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING: 1386729ed62eSMatthias Ringwald if (!hci_le_supported()){ 1387729ed62eSMatthias Ringwald // SKIP LE init for Classic only configuration 1388a650ba4dSMatthias Ringwald hci_init_done(); 1389729ed62eSMatthias Ringwald return; 1390729ed62eSMatthias Ringwald } 1391729ed62eSMatthias Ringwald break; 1392729ed62eSMatthias Ringwald #else 139374b323a9SMatthias Ringwald case HCI_INIT_W4_WRITE_SCAN_ENABLE: 13946155b3d3S[email protected] if (!hci_le_supported()){ 13956155b3d3S[email protected] // SKIP LE init for Classic only configuration 1396a650ba4dSMatthias Ringwald hci_init_done(); 139774b323a9SMatthias Ringwald return; 13986155b3d3S[email protected] } 1399729ed62eSMatthias Ringwald #endif 1400729ed62eSMatthias Ringwald break; 1401a650ba4dSMatthias Ringwald // Response to command before init done state -> init done 1402a650ba4dSMatthias Ringwald case (HCI_INIT_DONE-1): 1403a650ba4dSMatthias Ringwald hci_init_done(); 1404a650ba4dSMatthias Ringwald return; 1405a650ba4dSMatthias Ringwald 14066155b3d3S[email protected] default: 140774b323a9SMatthias Ringwald break; 14086155b3d3S[email protected] } 140955975f88SMatthias Ringwald hci_initializing_next_state(); 14106155b3d3S[email protected] } 14116155b3d3S[email protected] 141216833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){ 1413e76a89eeS[email protected] 1414e76a89eeS[email protected] uint16_t event_length = packet[1]; 1415e76a89eeS[email protected] 1416e76a89eeS[email protected] // assert packet is complete 1417e76a89eeS[email protected] if (size != event_length + 2){ 1418f04a0c31SMatthias Ringwald log_error("hci.c: event_handler called with event packet of wrong size %d, expected %u => dropping packet", size, event_length + 2); 1419e76a89eeS[email protected] return; 1420e76a89eeS[email protected] } 1421e76a89eeS[email protected] 14221281a47eSmatthias.ringwald bd_addr_t addr; 142396a45072S[email protected] bd_addr_type_t addr_type; 14242d00edd4Smatthias.ringwald uint8_t link_type; 1425fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 14261f7b95a1Smatthias.ringwald hci_connection_t * conn; 142756cf178bSmatthias.ringwald int i; 142822909952Smatthias.ringwald 1429*35454696SMatthias Ringwald // warnings 1430*35454696SMatthias Ringwald (void) link_type; 1431*35454696SMatthias Ringwald 14320e2df43fSMatthias Ringwald // log_info("HCI:EVENT:%02x", hci_event_packet_get_type(packet)); 14335909f7f2Smatthias.ringwald 14340e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 143522909952Smatthias.ringwald 14366772a24cSmatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 14377ec5eeaaSmatthias.ringwald // get num cmd packets 14389da54300S[email protected] // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u", hci_stack->num_cmd_packets, packet[2]); 14393a9fb326S[email protected] hci_stack->num_cmd_packets = packet[2]; 14407ec5eeaaSmatthias.ringwald 1441073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_buffer_size)){ 1442e2edc0c3Smatthias.ringwald // from offset 5 1443e2edc0c3Smatthias.ringwald // status 14441d279b20Smatthias.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" 1445f8fbdce0SMatthias Ringwald hci_stack->acl_data_packet_length = little_endian_read_16(packet, 6); 1446a8b12447S[email protected] hci_stack->sco_data_packet_length = packet[8]; 1447f8fbdce0SMatthias Ringwald hci_stack->acl_packets_total_num = little_endian_read_16(packet, 9); 1448f8fbdce0SMatthias Ringwald hci_stack->sco_packets_total_num = little_endian_read_16(packet, 11); 1449a8b12447S[email protected] 14503a9fb326S[email protected] if (hci_stack->state == HCI_STATE_INITIALIZING){ 1451a7a04bd9Smatthias.ringwald // determine usable ACL payload size 14523a9fb326S[email protected] if (HCI_ACL_PAYLOAD_SIZE < hci_stack->acl_data_packet_length){ 14533a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 14548f8108aaSmatthias.ringwald } 14551a06f663S[email protected] log_info("hci_read_buffer_size: acl used size %u, count %u / sco size %u, count %u", 14561a06f663S[email protected] hci_stack->acl_data_packet_length, hci_stack->acl_packets_total_num, 14571a06f663S[email protected] hci_stack->sco_data_packet_length, hci_stack->sco_packets_total_num); 1458e2edc0c3Smatthias.ringwald } 145956cf178bSmatthias.ringwald } 1460a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 1461073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_le_read_buffer_size)){ 1462f8fbdce0SMatthias Ringwald hci_stack->le_data_packets_length = little_endian_read_16(packet, 6); 1463ee303eddS[email protected] hci_stack->le_acl_packets_total_num = packet[8]; 14646c26b087S[email protected] // determine usable ACL payload size 14656c26b087S[email protected] if (HCI_ACL_PAYLOAD_SIZE < hci_stack->le_data_packets_length){ 14666c26b087S[email protected] hci_stack->le_data_packets_length = HCI_ACL_PAYLOAD_SIZE; 14676c26b087S[email protected] } 14689da54300S[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); 146965a46ef3S[email protected] } 1470073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_le_read_white_list_size)){ 1471f8fbdce0SMatthias Ringwald hci_stack->le_whitelist_capacity = little_endian_read_16(packet, 6); 147215d0a15bSMatthias Ringwald log_info("hci_le_read_white_list_size: size %u", hci_stack->le_whitelist_capacity); 14733b6d4121SMatthias Ringwald } 147465a46ef3S[email protected] #endif 1475188981d2Smatthias.ringwald // Dump local address 1476073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_bd_addr)) { 1477724d70a2SMatthias Ringwald reverse_bd_addr(&packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1], 1478724d70a2SMatthias Ringwald hci_stack->local_bd_addr); 14799da54300S[email protected] log_info("Local Address, Status: 0x%02x: Addr: %s", 14803a9fb326S[email protected] packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr)); 14811624665aSMatthias Ringwald if (hci_stack->link_key_db){ 14821624665aSMatthias Ringwald hci_stack->link_key_db->set_local_bd_addr(hci_stack->local_bd_addr); 14831624665aSMatthias Ringwald } 1484188981d2Smatthias.ringwald } 1485*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1486073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_scan_enable)){ 14873a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 1488381fbed8Smatthias.ringwald } 1489*35454696SMatthias Ringwald #endif 1490*35454696SMatthias Ringwald 149165389bfcS[email protected] // Note: HCI init checks 1492073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_supported_features)){ 14933a9fb326S[email protected] memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8); 149465389bfcS[email protected] 1495*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1496a5a23fc2S[email protected] // determine usable ACL packet types based on host buffer size and supported features 1497a5a23fc2S[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]); 14988b96126aSMatthias Ringwald log_info("Packet types %04x, eSCO %u", hci_stack->packet_types, hci_extended_sco_link_supported()); 1499*35454696SMatthias Ringwald #endif 1500f5d8d141S[email protected] // Classic/LE 1501f5d8d141S[email protected] log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported()); 1502559e517eS[email protected] } 1503073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_version_information)){ 1504f8fbdce0SMatthias Ringwald // hci_stack->hci_version = little_endian_read_16(packet, 4); 1505f8fbdce0SMatthias Ringwald // hci_stack->hci_revision = little_endian_read_16(packet, 6); 1506f8fbdce0SMatthias Ringwald // hci_stack->lmp_version = little_endian_read_16(packet, 8); 1507f8fbdce0SMatthias Ringwald hci_stack->manufacturer = little_endian_read_16(packet, 10); 1508f8fbdce0SMatthias Ringwald // hci_stack->lmp_subversion = little_endian_read_16(packet, 12); 15094696bddbSMatthias Ringwald log_info("Manufacturer: 0x%04x", hci_stack->manufacturer); 1510a6ddbcb9SMatthias Ringwald // notify app 1511a6ddbcb9SMatthias Ringwald if (hci_stack->local_version_information_callback){ 1512a6ddbcb9SMatthias Ringwald hci_stack->local_version_information_callback(packet); 1513a6ddbcb9SMatthias Ringwald } 15144696bddbSMatthias Ringwald } 1515073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_supported_commands)){ 1516a828a756SMatthias Ringwald hci_stack->local_supported_commands[0] = 15173905afbfSMatthias Ringwald (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+14] & 0x80) >> 7 | // bit 0 = Octet 14, bit 7 15183905afbfSMatthias Ringwald (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+24] & 0x40) >> 5 | // bit 1 = Octet 24, bit 6 15193905afbfSMatthias Ringwald (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+10] & 0x10) >> 2 | // bit 2 = Octet 10, bit 4 15203905afbfSMatthias Ringwald (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1+18] & 0x08); // bit 3 = Octet 18, bit 3 15213905afbfSMatthias Ringwald log_info("Local supported commands summary 0x%02x", hci_stack->local_supported_commands[0]); 1522a828a756SMatthias Ringwald } 1523073bd0faSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_synchronous_flow_control_enable)){ 15245b9b590fSMatthias Ringwald if (packet[5] == 0){ 15255b9b590fSMatthias Ringwald hci_stack->synchronous_flow_control_enabled = 1; 15265b9b590fSMatthias Ringwald } 15275b9b590fSMatthias Ringwald } 152856cf178bSmatthias.ringwald break; 152956cf178bSmatthias.ringwald 15307ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 15317ec5eeaaSmatthias.ringwald // get num cmd packets 15329da54300S[email protected] // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u", hci_stack->num_cmd_packets, packet[3]); 15333a9fb326S[email protected] hci_stack->num_cmd_packets = packet[3]; 15347ec5eeaaSmatthias.ringwald break; 15357ec5eeaaSmatthias.ringwald 15362e440c8aS[email protected] case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:{ 15372e440c8aS[email protected] int offset = 3; 153856cf178bSmatthias.ringwald for (i=0; i<packet[2];i++){ 1539f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, offset); 15402e440c8aS[email protected] offset += 2; 1541f8fbdce0SMatthias Ringwald uint16_t num_packets = little_endian_read_16(packet, offset); 15422e440c8aS[email protected] offset += 2; 15432e440c8aS[email protected] 15445061f3afS[email protected] conn = hci_connection_for_handle(handle); 154556cf178bSmatthias.ringwald if (!conn){ 15469da54300S[email protected] log_error("hci_number_completed_packet lists unused con handle %u", handle); 154756cf178bSmatthias.ringwald continue; 154856cf178bSmatthias.ringwald } 154923bed257S[email protected] 1550e35edcc1S[email protected] if (conn->address_type == BD_ADDR_TYPE_SCO){ 1551*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1552e35edcc1S[email protected] if (conn->num_sco_packets_sent >= num_packets){ 1553e35edcc1S[email protected] conn->num_sco_packets_sent -= num_packets; 1554e35edcc1S[email protected] } else { 1555e35edcc1S[email protected] log_error("hci_number_completed_packets, more sco slots freed then sent."); 1556e35edcc1S[email protected] conn->num_sco_packets_sent = 0; 1557e35edcc1S[email protected] } 1558701e3307SMatthias Ringwald hci_notify_if_sco_can_send_now(); 1559*35454696SMatthias Ringwald #endif 1560e35edcc1S[email protected] } else { 156123bed257S[email protected] if (conn->num_acl_packets_sent >= num_packets){ 156256cf178bSmatthias.ringwald conn->num_acl_packets_sent -= num_packets; 156323bed257S[email protected] } else { 1564e35edcc1S[email protected] log_error("hci_number_completed_packets, more acl slots freed then sent."); 156523bed257S[email protected] conn->num_acl_packets_sent = 0; 156623bed257S[email protected] } 1567e35edcc1S[email protected] } 15689da54300S[email protected] // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u", num_packets, handle, conn->num_acl_packets_sent); 156956cf178bSmatthias.ringwald } 15706772a24cSmatthias.ringwald break; 15712e440c8aS[email protected] } 1572*35454696SMatthias Ringwald 1573*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 15741f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 1575724d70a2SMatthias Ringwald reverse_bd_addr(&packet[2], addr); 157637eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 15772d00edd4Smatthias.ringwald link_type = packet[11]; 15789da54300S[email protected] log_info("Connection_incoming: %s, type %u", bd_addr_to_str(addr), link_type); 1579e35edcc1S[email protected] addr_type = link_type == 1 ? BD_ADDR_TYPE_CLASSIC : BD_ADDR_TYPE_SCO; 15802e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 15811f7b95a1Smatthias.ringwald if (!conn) { 15825293c072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 15831f7b95a1Smatthias.ringwald } 1584ce4c8fabSmatthias.ringwald if (!conn) { 1585ce4c8fabSmatthias.ringwald // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 15863a9fb326S[email protected] hci_stack->decline_reason = 0x0d; 1587058e3d6bSMatthias Ringwald bd_addr_copy(hci_stack->decline_addr, addr); 1588ce4c8fabSmatthias.ringwald break; 1589ce4c8fabSmatthias.ringwald } 15905cf766e8SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 159132ab9390Smatthias.ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 1592f3a16b9aSMatthias Ringwald // store info about eSCO 1593f3a16b9aSMatthias Ringwald if (link_type == 0x02){ 1594f3a16b9aSMatthias Ringwald conn->remote_supported_feature_eSCO = 1; 1595f3a16b9aSMatthias Ringwald } 159632ab9390Smatthias.ringwald hci_run(); 15971f7b95a1Smatthias.ringwald break; 15981f7b95a1Smatthias.ringwald 15996772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 1600fe1ed1b8Smatthias.ringwald // Connection management 1601724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], addr); 16029da54300S[email protected] log_info("Connection_complete (status=%u) %s", packet[2], bd_addr_to_str(addr)); 160396a45072S[email protected] addr_type = BD_ADDR_TYPE_CLASSIC; 16042e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 1605fe1ed1b8Smatthias.ringwald if (conn) { 1606b448a0e7Smatthias.ringwald if (!packet[2]){ 1607c8e4258aSmatthias.ringwald conn->state = OPEN; 1608f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 3); 1609ad83dc6aS[email protected] conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES; 1610ee091cf1Smatthias.ringwald 1611c785ef68Smatthias.ringwald // restart timer 1612528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 1613528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&conn->timeout); 1614c785ef68Smatthias.ringwald 16159da54300S[email protected] log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 161643bfb1bdSmatthias.ringwald 161743bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 1618b448a0e7Smatthias.ringwald } else { 16191bd5283dS[email protected] int notify_dedicated_bonding_failed = conn->bonding_flags & BONDING_DEDICATED; 16201bd5283dS[email protected] uint8_t status = packet[2]; 16211bd5283dS[email protected] bd_addr_t bd_address; 16221bd5283dS[email protected] memcpy(&bd_address, conn->address, 6); 1623ad83dc6aS[email protected] 1624b448a0e7Smatthias.ringwald // connection failed, remove entry 1625665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 1626a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 1627c12e46e7Smatthias.ringwald 16281bd5283dS[email protected] // notify client if dedicated bonding 16291bd5283dS[email protected] if (notify_dedicated_bonding_failed){ 16301bd5283dS[email protected] log_info("hci notify_dedicated_bonding_failed"); 16311bd5283dS[email protected] hci_emit_dedicated_bonding_result(bd_address, status); 16321bd5283dS[email protected] } 16331bd5283dS[email protected] 1634c12e46e7Smatthias.ringwald // if authentication error, also delete link key 1635c12e46e7Smatthias.ringwald if (packet[2] == 0x05) { 163615a95bd5SMatthias Ringwald gap_drop_link_key_for_bd_addr(addr); 1637c12e46e7Smatthias.ringwald } 1638fe1ed1b8Smatthias.ringwald } 1639fe1ed1b8Smatthias.ringwald } 16406772a24cSmatthias.ringwald break; 1641fe1ed1b8Smatthias.ringwald 164244d0e3d5S[email protected] case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE: 1643724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], addr); 164444d0e3d5S[email protected] log_info("Synchronous Connection Complete (status=%u) %s", packet[2], bd_addr_to_str(addr)); 16451a06f663S[email protected] if (packet[2]){ 164644d0e3d5S[email protected] // connection failed 164744d0e3d5S[email protected] break; 164844d0e3d5S[email protected] } 16492e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 1650e35edcc1S[email protected] if (!conn) { 1651e35edcc1S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 1652e35edcc1S[email protected] } 1653e35edcc1S[email protected] if (!conn) { 1654e35edcc1S[email protected] break; 1655e35edcc1S[email protected] } 16561a06f663S[email protected] conn->state = OPEN; 1657f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 3); 165844d0e3d5S[email protected] break; 165944d0e3d5S[email protected] 1660afd4e962S[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 1661f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1662afd4e962S[email protected] conn = hci_connection_for_handle(handle); 1663afd4e962S[email protected] if (!conn) break; 1664afd4e962S[email protected] if (!packet[2]){ 1665afd4e962S[email protected] uint8_t * features = &packet[5]; 1666afd4e962S[email protected] if (features[6] & (1 << 3)){ 1667afd4e962S[email protected] conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP; 1668afd4e962S[email protected] } 166998a2fd1cSMatthias Ringwald if (features[3] & (1<<7)){ 167098a2fd1cSMatthias Ringwald conn->remote_supported_feature_eSCO = 1; 167198a2fd1cSMatthias Ringwald } 1672afd4e962S[email protected] } 1673afd4e962S[email protected] conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES; 167498a2fd1cSMatthias Ringwald log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x, eSCO %u", conn->bonding_flags, conn->remote_supported_feature_eSCO); 1675ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 1676ad83dc6aS[email protected] conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 1677ad83dc6aS[email protected] } 1678afd4e962S[email protected] break; 1679afd4e962S[email protected] 16807fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 16819da54300S[email protected] log_info("HCI_EVENT_LINK_KEY_REQUEST"); 16827fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST); 168374d716b5S[email protected] // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST 1684a98592bcSMatthias Ringwald if (hci_stack->bondable && !hci_stack->link_key_db) break; 168532ab9390Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST); 168664472d52Smatthias.ringwald hci_run(); 1687d9a327f9Smatthias.ringwald // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set 168829d53098Smatthias.ringwald return; 16897fde4af9Smatthias.ringwald 16909ab95c90S[email protected] case HCI_EVENT_LINK_KEY_NOTIFICATION: { 1691724d70a2SMatthias Ringwald reverse_bd_addr(&packet[2], addr); 16922e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 16939ab95c90S[email protected] if (!conn) break; 16949ab95c90S[email protected] conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION; 16957bdc6798S[email protected] link_key_type_t link_key_type = (link_key_type_t)packet[24]; 16969ab95c90S[email protected] // Change Connection Encryption keeps link key type 16979ab95c90S[email protected] if (link_key_type != CHANGED_COMBINATION_KEY){ 16989ab95c90S[email protected] conn->link_key_type = link_key_type; 16999ab95c90S[email protected] } 170055597469SMatthias Ringwald gap_store_link_key_for_bd_addr(addr, &packet[8], conn->link_key_type); 170129d53098Smatthias.ringwald // still forward event to allow dismiss of pairing dialog 17027fde4af9Smatthias.ringwald break; 17039ab95c90S[email protected] } 17047fde4af9Smatthias.ringwald 17057fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 17066724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE); 17074c57c146S[email protected] // non-bondable mode: pin code negative reply will be sent 17083a9fb326S[email protected] if (!hci_stack->bondable){ 1709899283eaS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST); 1710f8fb5f6eS[email protected] hci_run(); 1711f8fb5f6eS[email protected] return; 17124c57c146S[email protected] } 1713d9a327f9Smatthias.ringwald // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key 1714a98592bcSMatthias Ringwald if (!hci_stack->link_key_db) break; 1715a6ef64baSMilanka Ringwald hci_event_pin_code_request_get_bd_addr(packet, addr); 1716a98592bcSMatthias Ringwald hci_stack->link_key_db->delete_link_key(addr); 17177fde4af9Smatthias.ringwald break; 17187fde4af9Smatthias.ringwald 17191d6b20aeS[email protected] case HCI_EVENT_IO_CAPABILITY_REQUEST: 17201d6b20aeS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST); 1721dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY); 1722dbe1a790S[email protected] break; 1723dbe1a790S[email protected] 1724dbe1a790S[email protected] case HCI_EVENT_USER_CONFIRMATION_REQUEST: 17256724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 17263a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 1727dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY); 1728dbe1a790S[email protected] break; 1729dbe1a790S[email protected] 1730dbe1a790S[email protected] case HCI_EVENT_USER_PASSKEY_REQUEST: 17316724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 17323a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 1733dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY); 17341d6b20aeS[email protected] break; 1735*35454696SMatthias Ringwald #endif 17361d6b20aeS[email protected] 1737f0944df2S[email protected] case HCI_EVENT_ENCRYPTION_CHANGE: 1738f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1739f0944df2S[email protected] conn = hci_connection_for_handle(handle); 1740f0944df2S[email protected] if (!conn) break; 1741ad83dc6aS[email protected] if (packet[2] == 0) { 1742f0944df2S[email protected] if (packet[5]){ 1743f0944df2S[email protected] conn->authentication_flags |= CONNECTION_ENCRYPTED; 1744f0944df2S[email protected] } else { 1745536f9994S[email protected] conn->authentication_flags &= ~CONNECTION_ENCRYPTED; 1746f0944df2S[email protected] } 1747ad83dc6aS[email protected] } 1748*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1749a00031e2S[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 1750*35454696SMatthias Ringwald #endif 1751f0944df2S[email protected] break; 1752f0944df2S[email protected] 1753*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 17541eb2563eS[email protected] case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT: 1755f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 17561eb2563eS[email protected] conn = hci_connection_for_handle(handle); 17571eb2563eS[email protected] if (!conn) break; 1758ad83dc6aS[email protected] 1759ad83dc6aS[email protected] // dedicated bonding: send result and disconnect 1760ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 1761ad83dc6aS[email protected] conn->bonding_flags &= ~BONDING_DEDICATED; 1762ad83dc6aS[email protected] conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE; 17631bd5283dS[email protected] conn->bonding_status = packet[2]; 1764ad83dc6aS[email protected] break; 1765ad83dc6aS[email protected] } 1766ad83dc6aS[email protected] 1767b5cb6874S[email protected] if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){ 17681eb2563eS[email protected] // link key sufficient for requested security 17691eb2563eS[email protected] conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 1770ad83dc6aS[email protected] break; 1771ad83dc6aS[email protected] } 17721eb2563eS[email protected] // not enough 17731eb2563eS[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 17741eb2563eS[email protected] break; 1775*35454696SMatthias Ringwald #endif 177634d2123cS[email protected] 177786805605S[email protected] // HCI_EVENT_DISCONNECTION_COMPLETE 1778ccda6e14S[email protected] // has been split, to first notify stack before shutting connection down 1779ccda6e14S[email protected] // see end of function, too. 1780a4f30ec0S[email protected] case HCI_EVENT_DISCONNECTION_COMPLETE: 1781a4f30ec0S[email protected] if (packet[2]) break; // status != 0 1782f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 1783c6a37cfdSMatthias Ringwald // drop outgoing ACL fragments if it is for closed connection 1784c6a37cfdSMatthias Ringwald if (hci_stack->acl_fragmentation_total_size > 0) { 1785c6a37cfdSMatthias Ringwald if (handle == READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer)){ 1786c6a37cfdSMatthias Ringwald log_info("hci: drop fragmented ACL data for closed connection"); 1787c6a37cfdSMatthias Ringwald hci_stack->acl_fragmentation_total_size = 0; 1788c6a37cfdSMatthias Ringwald hci_stack->acl_fragmentation_pos = 0; 1789c6a37cfdSMatthias Ringwald } 1790c6a37cfdSMatthias Ringwald } 1791*35454696SMatthias Ringwald 17929a2e4658SMatthias Ringwald // re-enable advertisements for le connections if active 1793c6a37cfdSMatthias Ringwald conn = hci_connection_for_handle(handle); 1794c6a37cfdSMatthias Ringwald if (!conn) break; 1795*35454696SMatthias Ringwald #ifdef ENABLE_BLE 17969a2e4658SMatthias Ringwald if (hci_is_le_connection(conn) && hci_stack->le_advertisements_enabled){ 17979a2e4658SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_ENABLE; 17989a2e4658SMatthias Ringwald } 1799*35454696SMatthias Ringwald #endif 1800ccda6e14S[email protected] conn->state = RECEIVED_DISCONNECTION_COMPLETE; 1801ccda6e14S[email protected] break; 18026772a24cSmatthias.ringwald 1803c68bdf90Smatthias.ringwald case HCI_EVENT_HARDWARE_ERROR: 1804313e5f9cSMatthias Ringwald log_error("Hardware Error: 0x%02x", packet[2]); 1805d23838ecSMatthias Ringwald if (hci_stack->hardware_error_callback){ 1806c2e1fa60SMatthias Ringwald (*hci_stack->hardware_error_callback)(packet[2]); 18077586ee35S[email protected] } else { 18087586ee35S[email protected] // if no special requests, just reboot stack 18097586ee35S[email protected] hci_power_control_off(); 18107586ee35S[email protected] hci_power_control_on(); 1811c68bdf90Smatthias.ringwald } 1812c68bdf90Smatthias.ringwald break; 1813c68bdf90Smatthias.ringwald 18145cf766e8SMatthias Ringwald case HCI_EVENT_ROLE_CHANGE: 18155cf766e8SMatthias Ringwald if (packet[2]) break; // status != 0 1816f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 18175cf766e8SMatthias Ringwald conn = hci_connection_for_handle(handle); 18185cf766e8SMatthias Ringwald if (!conn) break; // no conn 18195cf766e8SMatthias Ringwald conn->role = packet[9]; 18205cf766e8SMatthias Ringwald break; 18215cf766e8SMatthias Ringwald 182263fa3374SMatthias Ringwald case HCI_EVENT_TRANSPORT_PACKET_SENT: 1823d051460cS[email protected] // release packet buffer only for asynchronous transport and if there are not further fragements 18244fa24b5fS[email protected] if (hci_transport_synchronous()) { 182563fa3374SMatthias Ringwald log_error("Synchronous HCI Transport shouldn't send HCI_EVENT_TRANSPORT_PACKET_SENT"); 18264fa24b5fS[email protected] return; // instead of break: to avoid re-entering hci_run() 18274fa24b5fS[email protected] } 1828d051460cS[email protected] if (hci_stack->acl_fragmentation_total_size) break; 1829d051460cS[email protected] hci_release_packet_buffer(); 1830701e3307SMatthias Ringwald 183163fa3374SMatthias Ringwald // L2CAP receives this event via the hci_emit_event below 183263fa3374SMatthias Ringwald 1833*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 183463fa3374SMatthias Ringwald // For SCO, we do the can_send_now_check here 1835701e3307SMatthias Ringwald hci_notify_if_sco_can_send_now(); 1836*35454696SMatthias Ringwald #endif 18376b4af23dS[email protected] break; 18386b4af23dS[email protected] 1839*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 184063fa3374SMatthias Ringwald case HCI_EVENT_SCO_CAN_SEND_NOW: 184163fa3374SMatthias Ringwald // For SCO, we do the can_send_now_check here 184263fa3374SMatthias Ringwald hci_notify_if_sco_can_send_now(); 184363fa3374SMatthias Ringwald return; 1844*35454696SMatthias Ringwald #endif 184563fa3374SMatthias Ringwald 1846a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 18475909f7f2Smatthias.ringwald case HCI_EVENT_LE_META: 18485909f7f2Smatthias.ringwald switch (packet[2]){ 184957c9da5bS[email protected] case HCI_SUBEVENT_LE_ADVERTISING_REPORT: 18504f4e0224SMatthias Ringwald // log_info("advertising report received"); 18517bdc6798S[email protected] if (hci_stack->le_scanning_state != LE_SCANNING) break; 185257c9da5bS[email protected] le_handle_advertisement_report(packet, size); 18537bdc6798S[email protected] break; 18545909f7f2Smatthias.ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 18555909f7f2Smatthias.ringwald // Connection management 1856724d70a2SMatthias Ringwald reverse_bd_addr(&packet[8], addr); 18577bdc6798S[email protected] addr_type = (bd_addr_type_t)packet[7]; 18589da54300S[email protected] log_info("LE Connection_complete (status=%u) type %u, %s", packet[3], addr_type, bd_addr_to_str(addr)); 18592e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 186042ff5ba1SMatthias Ringwald // if auto-connect, remove from whitelist in both roles 186142ff5ba1SMatthias Ringwald if (hci_stack->le_connecting_state == LE_CONNECTING_WHITELIST){ 186242ff5ba1SMatthias Ringwald hci_remove_from_whitelist(addr_type, addr); 186342ff5ba1SMatthias Ringwald } 186442ff5ba1SMatthias Ringwald // handle error: error is reported only to the initiator -> outgoing connection 18655909f7f2Smatthias.ringwald if (packet[3]){ 186642ff5ba1SMatthias Ringwald // outgoing connection establishment is done 186742ff5ba1SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 186842ff5ba1SMatthias Ringwald // remove entry 18695909f7f2Smatthias.ringwald if (conn){ 1870665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 18715909f7f2Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 18725909f7f2Smatthias.ringwald } 18735909f7f2Smatthias.ringwald break; 18745909f7f2Smatthias.ringwald } 187542ff5ba1SMatthias Ringwald // on success, both hosts receive connection complete event 18765cf766e8SMatthias Ringwald if (packet[6] == HCI_ROLE_MASTER){ 187742ff5ba1SMatthias Ringwald // if we're master, it was an outgoing connection and we're done with it 187842ff5ba1SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 187942ff5ba1SMatthias Ringwald } else { 188042ff5ba1SMatthias Ringwald // if we're slave, it was an incoming connection, advertisements have stopped 1881171293d3SMatthias Ringwald hci_stack->le_advertisements_active = 0; 188242ff5ba1SMatthias Ringwald } 1883171293d3SMatthias Ringwald // LE connections are auto-accepted, so just create a connection if there isn't one already 188442ff5ba1SMatthias Ringwald if (!conn){ 188596a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 18865909f7f2Smatthias.ringwald } 188742ff5ba1SMatthias Ringwald // no memory, sorry. 18885909f7f2Smatthias.ringwald if (!conn){ 18895909f7f2Smatthias.ringwald break; 18905909f7f2Smatthias.ringwald } 18915909f7f2Smatthias.ringwald 18925909f7f2Smatthias.ringwald conn->state = OPEN; 18935cf766e8SMatthias Ringwald conn->role = packet[6]; 1894f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 4); 18955909f7f2Smatthias.ringwald 18965909f7f2Smatthias.ringwald // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock 18975909f7f2Smatthias.ringwald 18985909f7f2Smatthias.ringwald // restart timer 1899528a4a3bSMatthias Ringwald // btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 1900528a4a3bSMatthias Ringwald // btstack_run_loop_add_timer(&conn->timeout); 19015909f7f2Smatthias.ringwald 19029da54300S[email protected] log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 19035909f7f2Smatthias.ringwald 19045909f7f2Smatthias.ringwald hci_emit_nr_connections_changed(); 19055909f7f2Smatthias.ringwald break; 19065909f7f2Smatthias.ringwald 1907f8fbdce0SMatthias Ringwald // log_info("LE buffer size: %u, count %u", little_endian_read_16(packet,6), packet[8]); 190865a46ef3S[email protected] 19095909f7f2Smatthias.ringwald default: 19105909f7f2Smatthias.ringwald break; 19115909f7f2Smatthias.ringwald } 19125909f7f2Smatthias.ringwald break; 19135909f7f2Smatthias.ringwald #endif 19146772a24cSmatthias.ringwald default: 19156772a24cSmatthias.ringwald break; 1916fe1ed1b8Smatthias.ringwald } 1917fe1ed1b8Smatthias.ringwald 19183429f56bSmatthias.ringwald // handle BT initialization 19193a9fb326S[email protected] if (hci_stack->state == HCI_STATE_INITIALIZING){ 19206155b3d3S[email protected] hci_initializing_event_handler(packet, size); 1921c9af4d3fS[email protected] } 192222909952Smatthias.ringwald 192389db417bSmatthias.ringwald // help with BT sleep 19243a9fb326S[email protected] if (hci_stack->state == HCI_STATE_FALLING_ASLEEP 192574b323a9SMatthias Ringwald && hci_stack->substate == HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE 1926073bd0faSMatthias Ringwald && HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_scan_enable)){ 192755975f88SMatthias Ringwald hci_initializing_next_state(); 192889db417bSmatthias.ringwald } 192989db417bSmatthias.ringwald 193086805605S[email protected] // notify upper stack 1931d6b06661SMatthias Ringwald hci_emit_event(packet, size, 0); // don't dump, already happened in packet handler 193294ab26f8Smatthias.ringwald 193386805605S[email protected] // moved here to give upper stack a chance to close down everything with hci_connection_t intact 19340e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_DISCONNECTION_COMPLETE){ 193586805605S[email protected] if (!packet[2]){ 1936f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 193705ae8de3SMatthias Ringwald hci_connection_t * aConn = hci_connection_for_handle(handle); 193805ae8de3SMatthias Ringwald if (aConn) { 193905ae8de3SMatthias Ringwald uint8_t status = aConn->bonding_status; 194005ae8de3SMatthias Ringwald uint16_t flags = aConn->bonding_flags; 194186805605S[email protected] bd_addr_t bd_address; 194205ae8de3SMatthias Ringwald memcpy(&bd_address, aConn->address, 6); 194305ae8de3SMatthias Ringwald hci_shutdown_connection(aConn); 1944bb820044S[email protected] // connection struct is gone, don't access anymore 1945bb820044S[email protected] if (flags & BONDING_EMIT_COMPLETE_ON_DISCONNECT){ 194686805605S[email protected] hci_emit_dedicated_bonding_result(bd_address, status); 194786805605S[email protected] } 194886805605S[email protected] } 194986805605S[email protected] } 195086805605S[email protected] } 195186805605S[email protected] 195294ab26f8Smatthias.ringwald // execute main loop 195394ab26f8Smatthias.ringwald hci_run(); 195416833f0aSmatthias.ringwald } 195516833f0aSmatthias.ringwald 1956*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1957c91d150bS[email protected] static void sco_handler(uint8_t * packet, uint16_t size){ 19588abbe8b5SMatthias Ringwald if (!hci_stack->sco_packet_handler) return; 19593d50b4baSMatthias Ringwald hci_stack->sco_packet_handler(HCI_SCO_DATA_PACKET, 0, packet, size); 1960c91d150bS[email protected] } 1961*35454696SMatthias Ringwald #endif 1962c91d150bS[email protected] 19630a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 19645bb5bc3eS[email protected] hci_dump_packet(packet_type, 1, packet, size); 196510e830c9Smatthias.ringwald switch (packet_type) { 196610e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 196710e830c9Smatthias.ringwald event_handler(packet, size); 196810e830c9Smatthias.ringwald break; 196910e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 197010e830c9Smatthias.ringwald acl_handler(packet, size); 197110e830c9Smatthias.ringwald break; 1972*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1973c91d150bS[email protected] case HCI_SCO_DATA_PACKET: 1974c91d150bS[email protected] sco_handler(packet, size); 1975202c8a4cSMatthias Ringwald break; 1976*35454696SMatthias Ringwald #endif 197710e830c9Smatthias.ringwald default: 197810e830c9Smatthias.ringwald break; 197910e830c9Smatthias.ringwald } 198010e830c9Smatthias.ringwald } 198110e830c9Smatthias.ringwald 1982d6b06661SMatthias Ringwald /** 1983d6b06661SMatthias Ringwald * @brief Add event packet handler. 1984d6b06661SMatthias Ringwald */ 1985d6b06661SMatthias Ringwald void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){ 1986d6b06661SMatthias Ringwald btstack_linked_list_add_tail(&hci_stack->event_handlers, (btstack_linked_item_t*) callback_handler); 1987d6b06661SMatthias Ringwald } 1988d6b06661SMatthias Ringwald 1989d6b06661SMatthias Ringwald 1990fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 19913d50b4baSMatthias Ringwald void hci_register_acl_packet_handler(btstack_packet_handler_t handler){ 1992fb37a842SMatthias Ringwald hci_stack->acl_packet_handler = handler; 199316833f0aSmatthias.ringwald } 199416833f0aSmatthias.ringwald 1995*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 19968abbe8b5SMatthias Ringwald /** 19978abbe8b5SMatthias Ringwald * @brief Registers a packet handler for SCO data. Used for HSP and HFP profiles. 19988abbe8b5SMatthias Ringwald */ 19993d50b4baSMatthias Ringwald void hci_register_sco_packet_handler(btstack_packet_handler_t handler){ 20008abbe8b5SMatthias Ringwald hci_stack->sco_packet_handler = handler; 20018abbe8b5SMatthias Ringwald } 2002*35454696SMatthias Ringwald #endif 20038abbe8b5SMatthias Ringwald 200471de195eSMatthias Ringwald static void hci_state_reset(void){ 2005595bdbfbS[email protected] // no connections yet 2006595bdbfbS[email protected] hci_stack->connections = NULL; 200774308b23Smatthias.ringwald 200874308b23Smatthias.ringwald // keep discoverable/connectable as this has been requested by the client(s) 200974308b23Smatthias.ringwald // hci_stack->discoverable = 0; 201074308b23Smatthias.ringwald // hci_stack->connectable = 0; 201174308b23Smatthias.ringwald // hci_stack->bondable = 1; 2012595bdbfbS[email protected] 201344935e40S[email protected] // buffer is free 201444935e40S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 201544935e40S[email protected] 2016595bdbfbS[email protected] // no pending cmds 2017595bdbfbS[email protected] hci_stack->decline_reason = 0; 2018595bdbfbS[email protected] hci_stack->new_scan_enable_value = 0xff; 2019595bdbfbS[email protected] 2020595bdbfbS[email protected] // LE 2021595bdbfbS[email protected] hci_stack->adv_addr_type = 0; 2022595bdbfbS[email protected] memset(hci_stack->adv_address, 0, 6); 2023595bdbfbS[email protected] hci_stack->le_scanning_state = LE_SCAN_IDLE; 2024e2602ea2Smatthias.ringwald hci_stack->le_scan_type = 0xff; 2025b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 2026e83201bcSMatthias Ringwald hci_stack->le_whitelist = 0; 2027e83201bcSMatthias Ringwald hci_stack->le_whitelist_capacity = 0; 2028e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_interval_min = 6; 2029e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_interval_max = 3200; 2030e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_latency_min = 0; 2031e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_latency_max = 500; 2032e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_supervision_timeout_min = 10; 2033e21192abSMatthias Ringwald hci_stack->le_connection_parameter_range.le_supervision_timeout_max = 3200; 2034595bdbfbS[email protected] } 2035595bdbfbS[email protected] 2036*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 20372d5e09d6SMatthias Ringwald /** 20382d5e09d6SMatthias Ringwald * @brief Configure Bluetooth hardware control. Has to be called before power on. 20392d5e09d6SMatthias Ringwald */ 20402d5e09d6SMatthias Ringwald void hci_set_link_key_db(btstack_link_key_db_t const * link_key_db){ 20412d5e09d6SMatthias Ringwald // store and open remote device db 20422d5e09d6SMatthias Ringwald hci_stack->link_key_db = link_key_db; 20432d5e09d6SMatthias Ringwald if (hci_stack->link_key_db) { 20442d5e09d6SMatthias Ringwald hci_stack->link_key_db->open(); 20452d5e09d6SMatthias Ringwald } 20462d5e09d6SMatthias Ringwald } 2047*35454696SMatthias Ringwald #endif 20482d5e09d6SMatthias Ringwald 20492d5e09d6SMatthias Ringwald void hci_init(const hci_transport_t *transport, const void *config){ 2050475c8125Smatthias.ringwald 20513a9fb326S[email protected] #ifdef HAVE_MALLOC 20523a9fb326S[email protected] if (!hci_stack) { 20533a9fb326S[email protected] hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t)); 20543a9fb326S[email protected] } 20553a9fb326S[email protected] #else 20563a9fb326S[email protected] hci_stack = &hci_stack_static; 20573a9fb326S[email protected] #endif 205866fb9560S[email protected] memset(hci_stack, 0, sizeof(hci_stack_t)); 20593a9fb326S[email protected] 2060475c8125Smatthias.ringwald // reference to use transport layer implementation 20613a9fb326S[email protected] hci_stack->hci_transport = transport; 2062475c8125Smatthias.ringwald 206311e23e5fSmatthias.ringwald // reference to used config 20643a9fb326S[email protected] hci_stack->config = config; 206511e23e5fSmatthias.ringwald 206626a9b6daSMatthias Ringwald // setup pointer for outgoing packet buffer 206726a9b6daSMatthias Ringwald hci_stack->hci_packet_buffer = &hci_stack->hci_packet_buffer_data[HCI_OUTGOING_PRE_BUFFER_SIZE]; 206826a9b6daSMatthias Ringwald 20698fcba05dSmatthias.ringwald // max acl payload size defined in config.h 20703a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 20718fcba05dSmatthias.ringwald 207216833f0aSmatthias.ringwald // register packet handlers with transport 207310e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 2074f5454fc6Smatthias.ringwald 20753a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 2076e2386ba1S[email protected] 2077e2386ba1S[email protected] // class of device 20783a9fb326S[email protected] hci_stack->class_of_device = 0x007a020c; // Smartphone 2079a45d6b9fS[email protected] 2080f20168b8Smatthias.ringwald // bondable by default 2081f20168b8Smatthias.ringwald hci_stack->bondable = 1; 2082f20168b8Smatthias.ringwald 208363048403S[email protected] // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept 20843a9fb326S[email protected] hci_stack->ssp_enable = 1; 20853a9fb326S[email protected] hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT; 20863a9fb326S[email protected] hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING; 20873a9fb326S[email protected] hci_stack->ssp_auto_accept = 1; 208869a97523S[email protected] 2089d950d659SMatthias Ringwald // voice setting - signed 8 bit pcm data with CVSD over the air 2090d950d659SMatthias Ringwald hci_stack->sco_voice_setting = 0x40; 2091d950d659SMatthias Ringwald 2092595bdbfbS[email protected] hci_state_reset(); 2093475c8125Smatthias.ringwald } 2094475c8125Smatthias.ringwald 20953fb36a29SMatthias Ringwald /** 20963fb36a29SMatthias Ringwald * @brief Configure Bluetooth chipset driver. Has to be called before power on, or right after receiving the local version information 20973fb36a29SMatthias Ringwald */ 20983fb36a29SMatthias Ringwald void hci_set_chipset(const btstack_chipset_t *chipset_driver){ 20993fb36a29SMatthias Ringwald hci_stack->chipset = chipset_driver; 21003fb36a29SMatthias Ringwald 21013fb36a29SMatthias Ringwald // reset chipset driver - init is also called on power_up 21023fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->init){ 21033fb36a29SMatthias Ringwald hci_stack->chipset->init(hci_stack->config); 21043fb36a29SMatthias Ringwald } 21053fb36a29SMatthias Ringwald } 21063fb36a29SMatthias Ringwald 2107fb55bd0aSMatthias Ringwald /** 2108d0b87befSMatthias Ringwald * @brief Configure Bluetooth hardware control. Has to be called after hci_init() but before power on. 2109fb55bd0aSMatthias Ringwald */ 2110fb55bd0aSMatthias Ringwald void hci_set_control(const btstack_control_t *hardware_control){ 2111fb55bd0aSMatthias Ringwald // references to used control implementation 2112fb55bd0aSMatthias Ringwald hci_stack->control = hardware_control; 2113d0b87befSMatthias Ringwald // init with transport config 2114d0b87befSMatthias Ringwald hardware_control->init(hci_stack->config); 2115fb55bd0aSMatthias Ringwald } 2116fb55bd0aSMatthias Ringwald 211771de195eSMatthias Ringwald void hci_close(void){ 2118404843c1Smatthias.ringwald // close remote device db 2119a98592bcSMatthias Ringwald if (hci_stack->link_key_db) { 2120a98592bcSMatthias Ringwald hci_stack->link_key_db->close(); 2121404843c1Smatthias.ringwald } 21227224be7eSMatthias Ringwald 21237224be7eSMatthias Ringwald btstack_linked_list_iterator_t lit; 21247224be7eSMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->connections); 21257224be7eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 21267224be7eSMatthias Ringwald // cancel all l2cap connections by emitting dicsconnection complete before shutdown (free) connection 21277224be7eSMatthias Ringwald hci_connection_t * connection = (hci_connection_t*) btstack_linked_list_iterator_next(&lit); 21287224be7eSMatthias Ringwald hci_emit_disconnection_complete(connection->con_handle, 0x16); // terminated by local host 21297224be7eSMatthias Ringwald hci_shutdown_connection(connection); 2130f5454fc6Smatthias.ringwald } 21317224be7eSMatthias Ringwald 2132f5454fc6Smatthias.ringwald hci_power_control(HCI_POWER_OFF); 21333a9fb326S[email protected] 21343a9fb326S[email protected] #ifdef HAVE_MALLOC 21353a9fb326S[email protected] free(hci_stack); 21363a9fb326S[email protected] #endif 21373a9fb326S[email protected] hci_stack = NULL; 2138404843c1Smatthias.ringwald } 2139404843c1Smatthias.ringwald 2140*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 214160b9e82fSMatthias Ringwald void gap_set_class_of_device(uint32_t class_of_device){ 21429e61646fS[email protected] hci_stack->class_of_device = class_of_device; 21439e61646fS[email protected] } 2144*35454696SMatthias Ringwald #endif 21459e61646fS[email protected] 2146f456b2d0S[email protected] // Set Public BD ADDR - passed on to Bluetooth chipset if supported in bt_control_h 2147f456b2d0S[email protected] void hci_set_bd_addr(bd_addr_t addr){ 2148f456b2d0S[email protected] memcpy(hci_stack->custom_bd_addr, addr, 6); 2149f456b2d0S[email protected] hci_stack->custom_bd_addr_set = 1; 2150f456b2d0S[email protected] } 2151f456b2d0S[email protected] 215271de195eSMatthias Ringwald void hci_disable_l2cap_timeout_check(void){ 215366fb9560S[email protected] disable_l2cap_timeouts = 1; 215466fb9560S[email protected] } 21558d213e1aSmatthias.ringwald // State-Module-Driver overview 21568d213e1aSmatthias.ringwald // state module low-level 21578d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 21588d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 21598d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 21608d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 2161d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 2162d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 2163c7e0c5f6Smatthias.ringwald 216440d1c7a4Smatthias.ringwald static int hci_power_control_on(void){ 21657301ad89Smatthias.ringwald 2166038bc64cSmatthias.ringwald // power on 2167f9a30166Smatthias.ringwald int err = 0; 21683a9fb326S[email protected] if (hci_stack->control && hci_stack->control->on){ 2169d0b87befSMatthias Ringwald err = (*hci_stack->control->on)(); 2170f9a30166Smatthias.ringwald } 2171038bc64cSmatthias.ringwald if (err){ 21729da54300S[email protected] log_error( "POWER_ON failed"); 2173038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 2174038bc64cSmatthias.ringwald return err; 2175038bc64cSmatthias.ringwald } 2176038bc64cSmatthias.ringwald 217724b3c629SMatthias Ringwald // int chipset driver 21783fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->init){ 21793fb36a29SMatthias Ringwald hci_stack->chipset->init(hci_stack->config); 21803fb36a29SMatthias Ringwald } 21813fb36a29SMatthias Ringwald 218224b3c629SMatthias Ringwald // init transport 218324b3c629SMatthias Ringwald if (hci_stack->hci_transport->init){ 218424b3c629SMatthias Ringwald hci_stack->hci_transport->init(hci_stack->config); 218524b3c629SMatthias Ringwald } 218624b3c629SMatthias Ringwald 218724b3c629SMatthias Ringwald // open transport 218824b3c629SMatthias Ringwald err = hci_stack->hci_transport->open(); 2189038bc64cSmatthias.ringwald if (err){ 21909da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 21913a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 2192d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 2193f9a30166Smatthias.ringwald } 2194038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 2195038bc64cSmatthias.ringwald return err; 2196038bc64cSmatthias.ringwald } 21978d213e1aSmatthias.ringwald return 0; 21988d213e1aSmatthias.ringwald } 2199038bc64cSmatthias.ringwald 220040d1c7a4Smatthias.ringwald static void hci_power_control_off(void){ 22018d213e1aSmatthias.ringwald 22029da54300S[email protected] log_info("hci_power_control_off"); 22039418f9c9Smatthias.ringwald 22048d213e1aSmatthias.ringwald // close low-level device 220524b3c629SMatthias Ringwald hci_stack->hci_transport->close(); 22068d213e1aSmatthias.ringwald 22079da54300S[email protected] log_info("hci_power_control_off - hci_transport closed"); 22089418f9c9Smatthias.ringwald 22098d213e1aSmatthias.ringwald // power off 22103a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 2211d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 22128d213e1aSmatthias.ringwald } 22139418f9c9Smatthias.ringwald 22149da54300S[email protected] log_info("hci_power_control_off - control closed"); 22159418f9c9Smatthias.ringwald 22163a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 221772ea5239Smatthias.ringwald } 221872ea5239Smatthias.ringwald 221940d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){ 222072ea5239Smatthias.ringwald 22219da54300S[email protected] log_info("hci_power_control_sleep"); 22223144bce4Smatthias.ringwald 2223b429b9b7Smatthias.ringwald #if 0 2224b429b9b7Smatthias.ringwald // don't close serial port during sleep 2225b429b9b7Smatthias.ringwald 222672ea5239Smatthias.ringwald // close low-level device 22273a9fb326S[email protected] hci_stack->hci_transport->close(hci_stack->config); 2228b429b9b7Smatthias.ringwald #endif 222972ea5239Smatthias.ringwald 223072ea5239Smatthias.ringwald // sleep mode 22313a9fb326S[email protected] if (hci_stack->control && hci_stack->control->sleep){ 2232d0b87befSMatthias Ringwald (*hci_stack->control->sleep)(); 223372ea5239Smatthias.ringwald } 2234b429b9b7Smatthias.ringwald 22353a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 22368d213e1aSmatthias.ringwald } 22378d213e1aSmatthias.ringwald 223840d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){ 2239b429b9b7Smatthias.ringwald 22409da54300S[email protected] log_info("hci_power_control_wake"); 2241b429b9b7Smatthias.ringwald 2242b429b9b7Smatthias.ringwald // wake on 22433a9fb326S[email protected] if (hci_stack->control && hci_stack->control->wake){ 2244d0b87befSMatthias Ringwald (*hci_stack->control->wake)(); 2245b429b9b7Smatthias.ringwald } 2246b429b9b7Smatthias.ringwald 2247b429b9b7Smatthias.ringwald #if 0 2248b429b9b7Smatthias.ringwald // open low-level device 22493a9fb326S[email protected] int err = hci_stack->hci_transport->open(hci_stack->config); 2250b429b9b7Smatthias.ringwald if (err){ 22519da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 22523a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 2253d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 2254b429b9b7Smatthias.ringwald } 2255b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 2256b429b9b7Smatthias.ringwald return err; 2257b429b9b7Smatthias.ringwald } 2258b429b9b7Smatthias.ringwald #endif 2259b429b9b7Smatthias.ringwald 2260b429b9b7Smatthias.ringwald return 0; 2261b429b9b7Smatthias.ringwald } 2262b429b9b7Smatthias.ringwald 226344935e40S[email protected] static void hci_power_transition_to_initializing(void){ 226444935e40S[email protected] // set up state machine 226544935e40S[email protected] hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent 226644935e40S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 226744935e40S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 22685c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET; 226944935e40S[email protected] } 2270b429b9b7Smatthias.ringwald 22718d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 2272d661ed19Smatthias.ringwald 2273f04a0c31SMatthias Ringwald log_info("hci_power_control: %d, current mode %u", power_mode, hci_stack->state); 2274d661ed19Smatthias.ringwald 22758d213e1aSmatthias.ringwald int err = 0; 22763a9fb326S[email protected] switch (hci_stack->state){ 22778d213e1aSmatthias.ringwald 22788d213e1aSmatthias.ringwald case HCI_STATE_OFF: 22798d213e1aSmatthias.ringwald switch (power_mode){ 22808d213e1aSmatthias.ringwald case HCI_POWER_ON: 22818d213e1aSmatthias.ringwald err = hci_power_control_on(); 228297b61c7bS[email protected] if (err) { 2283f04a0c31SMatthias Ringwald log_error("hci_power_control_on() error %d", err); 228497b61c7bS[email protected] return err; 228597b61c7bS[email protected] } 228644935e40S[email protected] hci_power_transition_to_initializing(); 22878d213e1aSmatthias.ringwald break; 22888d213e1aSmatthias.ringwald case HCI_POWER_OFF: 22898d213e1aSmatthias.ringwald // do nothing 22908d213e1aSmatthias.ringwald break; 22918d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2292b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 22938d213e1aSmatthias.ringwald break; 22948d213e1aSmatthias.ringwald } 22958d213e1aSmatthias.ringwald break; 22967301ad89Smatthias.ringwald 22978d213e1aSmatthias.ringwald case HCI_STATE_INITIALIZING: 22988d213e1aSmatthias.ringwald switch (power_mode){ 22998d213e1aSmatthias.ringwald case HCI_POWER_ON: 23008d213e1aSmatthias.ringwald // do nothing 23018d213e1aSmatthias.ringwald break; 23028d213e1aSmatthias.ringwald case HCI_POWER_OFF: 23038d213e1aSmatthias.ringwald // no connections yet, just turn it off 23048d213e1aSmatthias.ringwald hci_power_control_off(); 23058d213e1aSmatthias.ringwald break; 23068d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2307b546ac54Smatthias.ringwald // no connections yet, just turn it off 230872ea5239Smatthias.ringwald hci_power_control_sleep(); 23098d213e1aSmatthias.ringwald break; 23108d213e1aSmatthias.ringwald } 23118d213e1aSmatthias.ringwald break; 23127301ad89Smatthias.ringwald 23138d213e1aSmatthias.ringwald case HCI_STATE_WORKING: 23148d213e1aSmatthias.ringwald switch (power_mode){ 23158d213e1aSmatthias.ringwald case HCI_POWER_ON: 23168d213e1aSmatthias.ringwald // do nothing 23178d213e1aSmatthias.ringwald break; 23188d213e1aSmatthias.ringwald case HCI_POWER_OFF: 2319c7e0c5f6Smatthias.ringwald // see hci_run 23203a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 23218d213e1aSmatthias.ringwald break; 23228d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2323b546ac54Smatthias.ringwald // see hci_run 23243a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 232574b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT; 23268d213e1aSmatthias.ringwald break; 23278d213e1aSmatthias.ringwald } 23288d213e1aSmatthias.ringwald break; 23297301ad89Smatthias.ringwald 23308d213e1aSmatthias.ringwald case HCI_STATE_HALTING: 23318d213e1aSmatthias.ringwald switch (power_mode){ 23328d213e1aSmatthias.ringwald case HCI_POWER_ON: 233344935e40S[email protected] hci_power_transition_to_initializing(); 23348d213e1aSmatthias.ringwald break; 23358d213e1aSmatthias.ringwald case HCI_POWER_OFF: 23368d213e1aSmatthias.ringwald // do nothing 23378d213e1aSmatthias.ringwald break; 23388d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2339b546ac54Smatthias.ringwald // see hci_run 23403a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 234174b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT; 23428d213e1aSmatthias.ringwald break; 23438d213e1aSmatthias.ringwald } 23448d213e1aSmatthias.ringwald break; 23458d213e1aSmatthias.ringwald 23468d213e1aSmatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 23478d213e1aSmatthias.ringwald switch (power_mode){ 23488d213e1aSmatthias.ringwald case HCI_POWER_ON: 234928171530Smatthias.ringwald 2350423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 235128171530Smatthias.ringwald // nothing to do, if H4 supports power management 2352d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 23533a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 235474b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_WRITE_SCAN_ENABLE; // init after sleep 235528171530Smatthias.ringwald break; 235628171530Smatthias.ringwald } 235728171530Smatthias.ringwald #endif 235844935e40S[email protected] hci_power_transition_to_initializing(); 23598d213e1aSmatthias.ringwald break; 23608d213e1aSmatthias.ringwald case HCI_POWER_OFF: 2361b546ac54Smatthias.ringwald // see hci_run 23623a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 23638d213e1aSmatthias.ringwald break; 23648d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2365b546ac54Smatthias.ringwald // do nothing 23668d213e1aSmatthias.ringwald break; 23678d213e1aSmatthias.ringwald } 23688d213e1aSmatthias.ringwald break; 23698d213e1aSmatthias.ringwald 23708d213e1aSmatthias.ringwald case HCI_STATE_SLEEPING: 23718d213e1aSmatthias.ringwald switch (power_mode){ 23728d213e1aSmatthias.ringwald case HCI_POWER_ON: 237328171530Smatthias.ringwald 2374423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 237528171530Smatthias.ringwald // nothing to do, if H4 supports power management 2376d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 23773a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 23785c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_AFTER_SLEEP; 2379758b46ceSmatthias.ringwald hci_update_scan_enable(); 238028171530Smatthias.ringwald break; 238128171530Smatthias.ringwald } 238228171530Smatthias.ringwald #endif 23833144bce4Smatthias.ringwald err = hci_power_control_wake(); 23843144bce4Smatthias.ringwald if (err) return err; 238544935e40S[email protected] hci_power_transition_to_initializing(); 23868d213e1aSmatthias.ringwald break; 23878d213e1aSmatthias.ringwald case HCI_POWER_OFF: 23883a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 23898d213e1aSmatthias.ringwald break; 23908d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 2391b546ac54Smatthias.ringwald // do nothing 23928d213e1aSmatthias.ringwald break; 23938d213e1aSmatthias.ringwald } 23948d213e1aSmatthias.ringwald break; 239511e23e5fSmatthias.ringwald } 239668d92d03Smatthias.ringwald 2397038bc64cSmatthias.ringwald // create internal event 2398ee8bf225Smatthias.ringwald hci_emit_state(); 2399ee8bf225Smatthias.ringwald 240068d92d03Smatthias.ringwald // trigger next/first action 240168d92d03Smatthias.ringwald hci_run(); 240268d92d03Smatthias.ringwald 2403475c8125Smatthias.ringwald return 0; 2404475c8125Smatthias.ringwald } 2405475c8125Smatthias.ringwald 2406*35454696SMatthias Ringwald 2407*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2408*35454696SMatthias Ringwald 2409758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){ 2410758b46ceSmatthias.ringwald // 2 = page scan, 1 = inq scan 24113a9fb326S[email protected] hci_stack->new_scan_enable_value = hci_stack->connectable << 1 | hci_stack->discoverable; 2412758b46ceSmatthias.ringwald hci_run(); 2413758b46ceSmatthias.ringwald } 2414758b46ceSmatthias.ringwald 241515a95bd5SMatthias Ringwald void gap_discoverable_control(uint8_t enable){ 2416381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 2417381fbed8Smatthias.ringwald 24183a9fb326S[email protected] if (hci_stack->discoverable == enable){ 24193a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 2420381fbed8Smatthias.ringwald return; 2421381fbed8Smatthias.ringwald } 2422381fbed8Smatthias.ringwald 24233a9fb326S[email protected] hci_stack->discoverable = enable; 2424758b46ceSmatthias.ringwald hci_update_scan_enable(); 2425758b46ceSmatthias.ringwald } 2426b031bebbSmatthias.ringwald 242715a95bd5SMatthias Ringwald void gap_connectable_control(uint8_t enable){ 2428758b46ceSmatthias.ringwald if (enable) enable = 1; // normalize argument 2429758b46ceSmatthias.ringwald 2430758b46ceSmatthias.ringwald // don't emit event 24313a9fb326S[email protected] if (hci_stack->connectable == enable) return; 2432758b46ceSmatthias.ringwald 24333a9fb326S[email protected] hci_stack->connectable = enable; 2434758b46ceSmatthias.ringwald hci_update_scan_enable(); 2435381fbed8Smatthias.ringwald } 2436*35454696SMatthias Ringwald #endif 2437381fbed8Smatthias.ringwald 243815a95bd5SMatthias Ringwald void gap_local_bd_addr(bd_addr_t address_buffer){ 2439690bd0baS[email protected] memcpy(address_buffer, hci_stack->local_bd_addr, 6); 24405061f3afS[email protected] } 24415061f3afS[email protected] 244295d71764SMatthias Ringwald static void hci_run(void){ 24438a485f27Smatthias.ringwald 2444db8bc6ffSMatthias Ringwald // log_info("hci_run: entered"); 2445665d90f2SMatthias Ringwald btstack_linked_item_t * it; 244632ab9390Smatthias.ringwald 2447b5d8b22bS[email protected] // send continuation fragments first, as they block the prepared packet buffer 2448b5d8b22bS[email protected] if (hci_stack->acl_fragmentation_total_size > 0) { 2449b5d8b22bS[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer); 2450b5d8b22bS[email protected] hci_connection_t *connection = hci_connection_for_handle(con_handle); 2451b5d8b22bS[email protected] if (connection) { 245228a0332dSMatthias Ringwald if (hci_can_send_prepared_acl_packet_now(con_handle)){ 2453b5d8b22bS[email protected] hci_send_acl_packet_fragments(connection); 2454b5d8b22bS[email protected] return; 2455b5d8b22bS[email protected] } 245628a0332dSMatthias Ringwald } else { 2457b5d8b22bS[email protected] // connection gone -> discard further fragments 245828a0332dSMatthias Ringwald log_info("hci_run: fragmented ACL packet no connection -> discard fragment"); 2459b5d8b22bS[email protected] hci_stack->acl_fragmentation_total_size = 0; 2460b5d8b22bS[email protected] hci_stack->acl_fragmentation_pos = 0; 2461b5d8b22bS[email protected] } 2462b5d8b22bS[email protected] } 2463b5d8b22bS[email protected] 2464d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 2465ce4c8fabSmatthias.ringwald 2466b031bebbSmatthias.ringwald // global/non-connection oriented commands 2467b031bebbSmatthias.ringwald 2468*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2469b031bebbSmatthias.ringwald // decline incoming connections 24703a9fb326S[email protected] if (hci_stack->decline_reason){ 24713a9fb326S[email protected] uint8_t reason = hci_stack->decline_reason; 24723a9fb326S[email protected] hci_stack->decline_reason = 0; 24733a9fb326S[email protected] hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason); 2474dbe1a790S[email protected] return; 2475ce4c8fabSmatthias.ringwald } 2476ce4c8fabSmatthias.ringwald 2477b031bebbSmatthias.ringwald // send scan enable 24783a9fb326S[email protected] if (hci_stack->state == HCI_STATE_WORKING && hci_stack->new_scan_enable_value != 0xff && hci_classic_supported()){ 24793a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value); 24803a9fb326S[email protected] hci_stack->new_scan_enable_value = 0xff; 2481dbe1a790S[email protected] return; 2482b031bebbSmatthias.ringwald } 2483*35454696SMatthias Ringwald #endif 2484b031bebbSmatthias.ringwald 2485a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 24867bdc6798S[email protected] if (hci_stack->state == HCI_STATE_WORKING){ 248745c102fdSMatthias Ringwald // handle le scan 24887bdc6798S[email protected] switch(hci_stack->le_scanning_state){ 24897bdc6798S[email protected] case LE_START_SCAN: 24907bdc6798S[email protected] hci_stack->le_scanning_state = LE_SCANNING; 24917bdc6798S[email protected] hci_send_cmd(&hci_le_set_scan_enable, 1, 0); 24927bdc6798S[email protected] return; 24937bdc6798S[email protected] 24947bdc6798S[email protected] case LE_STOP_SCAN: 24957bdc6798S[email protected] hci_stack->le_scanning_state = LE_SCAN_IDLE; 24967bdc6798S[email protected] hci_send_cmd(&hci_le_set_scan_enable, 0, 0); 24977bdc6798S[email protected] return; 24987bdc6798S[email protected] default: 24997bdc6798S[email protected] break; 25007bdc6798S[email protected] } 2501e2602ea2Smatthias.ringwald if (hci_stack->le_scan_type != 0xff){ 2502e2602ea2Smatthias.ringwald // defaults: active scanning, accept all advertisement packets 2503e2602ea2Smatthias.ringwald int scan_type = hci_stack->le_scan_type; 2504e2602ea2Smatthias.ringwald hci_stack->le_scan_type = 0xff; 2505e2602ea2Smatthias.ringwald hci_send_cmd(&hci_le_set_scan_parameters, scan_type, hci_stack->le_scan_interval, hci_stack->le_scan_window, hci_stack->adv_addr_type, 0); 2506e2602ea2Smatthias.ringwald return; 2507e2602ea2Smatthias.ringwald } 250845c102fdSMatthias Ringwald // le advertisement control 25099a2e4658SMatthias Ringwald if (hci_stack->le_advertisements_todo){ 25109a2e4658SMatthias Ringwald log_info("hci_run: gap_le: adv todo: %x", hci_stack->le_advertisements_todo ); 25119a2e4658SMatthias Ringwald } 251245c102fdSMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_DISABLE){ 251345c102fdSMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_DISABLE; 251445c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 0); 251545c102fdSMatthias Ringwald return; 251645c102fdSMatthias Ringwald } 251745c102fdSMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_PARAMS){ 251845c102fdSMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_PARAMS; 251945c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_parameters, 252045c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_min, 252145c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_max, 252245c102fdSMatthias Ringwald hci_stack->le_advertisements_type, 252345c102fdSMatthias Ringwald hci_stack->le_advertisements_own_address_type, 252445c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address_type, 252545c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address, 252645c102fdSMatthias Ringwald hci_stack->le_advertisements_channel_map, 252745c102fdSMatthias Ringwald hci_stack->le_advertisements_filter_policy); 252845c102fdSMatthias Ringwald return; 252945c102fdSMatthias Ringwald } 2530501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_ADV_DATA){ 2531501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 253245c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_data, hci_stack->le_advertisements_data_len, 253345c102fdSMatthias Ringwald hci_stack->le_advertisements_data); 253445c102fdSMatthias Ringwald return; 253545c102fdSMatthias Ringwald } 2536501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA){ 2537501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 2538501f56b3SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_response_data, hci_stack->le_scan_response_data_len, 2539501f56b3SMatthias Ringwald hci_stack->le_scan_response_data); 2540501f56b3SMatthias Ringwald return; 2541501f56b3SMatthias Ringwald } 254245c102fdSMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_ENABLE){ 254345c102fdSMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_ENABLE; 254445c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 1); 254545c102fdSMatthias Ringwald return; 254645c102fdSMatthias Ringwald } 25479956955bSMatthias Ringwald 25489956955bSMatthias Ringwald // 25499956955bSMatthias Ringwald // LE Whitelist Management 25509956955bSMatthias Ringwald // 25519956955bSMatthias Ringwald 25529956955bSMatthias Ringwald // check if whitelist needs modification 2553665d90f2SMatthias Ringwald btstack_linked_list_iterator_t lit; 25549956955bSMatthias Ringwald int modification_pending = 0; 2555665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 2556665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 2557665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 25589956955bSMatthias Ringwald if (entry->state & (LE_WHITELIST_REMOVE_FROM_CONTROLLER | LE_WHITELIST_ADD_TO_CONTROLLER)){ 25599956955bSMatthias Ringwald modification_pending = 1; 25609956955bSMatthias Ringwald break; 25619956955bSMatthias Ringwald } 25629956955bSMatthias Ringwald } 256391915b0bSMatthias Ringwald 25649956955bSMatthias Ringwald if (modification_pending){ 256591915b0bSMatthias Ringwald // stop connnecting if modification pending 25669956955bSMatthias Ringwald if (hci_stack->le_connecting_state != LE_CONNECTING_IDLE){ 25679956955bSMatthias Ringwald hci_send_cmd(&hci_le_create_connection_cancel); 25689956955bSMatthias Ringwald return; 25699956955bSMatthias Ringwald } 25709956955bSMatthias Ringwald 25719956955bSMatthias Ringwald // add/remove entries 2572665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 2573665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 2574665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 25759956955bSMatthias Ringwald if (entry->state & LE_WHITELIST_ADD_TO_CONTROLLER){ 25769956955bSMatthias Ringwald entry->state = LE_WHITELIST_ON_CONTROLLER; 25779956955bSMatthias Ringwald hci_send_cmd(&hci_le_add_device_to_white_list, entry->address_type, entry->address); 25789956955bSMatthias Ringwald return; 25799956955bSMatthias Ringwald 25809956955bSMatthias Ringwald } 25819956955bSMatthias Ringwald if (entry->state & LE_WHITELIST_REMOVE_FROM_CONTROLLER){ 25829ecbe201SMatthias Ringwald bd_addr_t address; 25839ecbe201SMatthias Ringwald bd_addr_type_t address_type = entry->address_type; 25849ecbe201SMatthias Ringwald memcpy(address, entry->address, 6); 2585665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry); 25869956955bSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 25879ecbe201SMatthias Ringwald hci_send_cmd(&hci_le_remove_device_from_white_list, address_type, address); 25889956955bSMatthias Ringwald return; 25899956955bSMatthias Ringwald } 25909956955bSMatthias Ringwald } 259191915b0bSMatthias Ringwald } 25929956955bSMatthias Ringwald 25939956955bSMatthias Ringwald // start connecting 259491915b0bSMatthias Ringwald if ( hci_stack->le_connecting_state == LE_CONNECTING_IDLE && 2595665d90f2SMatthias Ringwald !btstack_linked_list_empty(&hci_stack->le_whitelist)){ 25969956955bSMatthias Ringwald bd_addr_t null_addr; 25979956955bSMatthias Ringwald memset(null_addr, 0, 6); 25989956955bSMatthias Ringwald hci_send_cmd(&hci_le_create_connection, 25999956955bSMatthias Ringwald 0x0060, // scan interval: 60 ms 26009956955bSMatthias Ringwald 0x0030, // scan interval: 30 ms 26019956955bSMatthias Ringwald 1, // use whitelist 26029956955bSMatthias Ringwald 0, // peer address type 26039956955bSMatthias Ringwald null_addr, // peer bd addr 26049956955bSMatthias Ringwald hci_stack->adv_addr_type, // our addr type: 26059956955bSMatthias Ringwald 0x0008, // conn interval min 26069956955bSMatthias Ringwald 0x0018, // conn interval max 26079956955bSMatthias Ringwald 0, // conn latency 26089956955bSMatthias Ringwald 0x0048, // supervision timeout 26099956955bSMatthias Ringwald 0x0001, // min ce length 26109956955bSMatthias Ringwald 0x0001 // max ce length 26119956955bSMatthias Ringwald ); 26129956955bSMatthias Ringwald return; 26139956955bSMatthias Ringwald } 26147bdc6798S[email protected] } 2615b2f949feS[email protected] #endif 26167bdc6798S[email protected] 261732ab9390Smatthias.ringwald // send pending HCI commands 2618665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 261905ae8de3SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 262032ab9390Smatthias.ringwald 26210bf6344aS[email protected] switch(connection->state){ 26220bf6344aS[email protected] case SEND_CREATE_CONNECTION: 26234f3229d8S[email protected] switch(connection->address_type){ 2624*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 26254f3229d8S[email protected] case BD_ADDR_TYPE_CLASSIC: 26269da54300S[email protected] log_info("sending hci_create_connection"); 2627ad83dc6aS[email protected] hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 26284f3229d8S[email protected] break; 2629*35454696SMatthias Ringwald #endif 26304f3229d8S[email protected] default: 2631a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 26329da54300S[email protected] log_info("sending hci_le_create_connection"); 26334f3229d8S[email protected] hci_send_cmd(&hci_le_create_connection, 2634b2571179Smatthias.ringwald 0x0060, // scan interval: 60 ms 2635b2571179Smatthias.ringwald 0x0030, // scan interval: 30 ms 26364f3229d8S[email protected] 0, // don't use whitelist 26374f3229d8S[email protected] connection->address_type, // peer address type 26384f3229d8S[email protected] connection->address, // peer bd addr 2639b2571179Smatthias.ringwald hci_stack->adv_addr_type, // our addr type: 2640b2571179Smatthias.ringwald 0x0008, // conn interval min 2641b2571179Smatthias.ringwald 0x0018, // conn interval max 26424f3229d8S[email protected] 0, // conn latency 2643b2571179Smatthias.ringwald 0x0048, // supervision timeout 2644b2571179Smatthias.ringwald 0x0001, // min ce length 2645b2571179Smatthias.ringwald 0x0001 // max ce length 26464f3229d8S[email protected] ); 26474f3229d8S[email protected] 26484f3229d8S[email protected] connection->state = SENT_CREATE_CONNECTION; 2649b2f949feS[email protected] #endif 26504f3229d8S[email protected] break; 26514f3229d8S[email protected] } 2652ad83dc6aS[email protected] return; 2653ad83dc6aS[email protected] 2654*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 26550bf6344aS[email protected] case RECEIVED_CONNECTION_REQUEST: 265694e4aaa2SMatthias Ringwald log_info("sending hci_accept_connection_request, remote eSCO %u", connection->remote_supported_feature_eSCO); 265732ab9390Smatthias.ringwald connection->state = ACCEPTED_CONNECTION_REQUEST; 26585cf766e8SMatthias Ringwald connection->role = HCI_ROLE_SLAVE; 2659e35edcc1S[email protected] if (connection->address_type == BD_ADDR_TYPE_CLASSIC){ 266034d2123cS[email protected] hci_send_cmd(&hci_accept_connection_request, connection->address, 1); 2661e35edcc1S[email protected] } 2662dbe1a790S[email protected] return; 2663*35454696SMatthias Ringwald #endif 26640bf6344aS[email protected] 2665a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 26660bf6344aS[email protected] case SEND_CANCEL_CONNECTION: 26670bf6344aS[email protected] connection->state = SENT_CANCEL_CONNECTION; 26680bf6344aS[email protected] hci_send_cmd(&hci_le_create_connection_cancel); 26690bf6344aS[email protected] return; 2670a6725849S[email protected] #endif 26710bf6344aS[email protected] case SEND_DISCONNECT: 26720bf6344aS[email protected] connection->state = SENT_DISCONNECT; 26737851196eSmatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 26740bf6344aS[email protected] return; 26750bf6344aS[email protected] 26760bf6344aS[email protected] default: 26770bf6344aS[email protected] break; 2678c7e0c5f6Smatthias.ringwald } 2679c7e0c5f6Smatthias.ringwald 2680*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 268132ab9390Smatthias.ringwald if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){ 26829da54300S[email protected] log_info("responding to link key request"); 268334d2123cS[email protected] connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST); 268432ab9390Smatthias.ringwald link_key_t link_key; 2685c77e8838S[email protected] link_key_type_t link_key_type; 2686a98592bcSMatthias Ringwald if ( hci_stack->link_key_db 2687a98592bcSMatthias Ringwald && hci_stack->link_key_db->get_link_key(connection->address, link_key, &link_key_type) 268834d2123cS[email protected] && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){ 26899ab95c90S[email protected] connection->link_key_type = link_key_type; 269032ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key); 269132ab9390Smatthias.ringwald } else { 269232ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 269332ab9390Smatthias.ringwald } 2694dbe1a790S[email protected] return; 269532ab9390Smatthias.ringwald } 26961d6b20aeS[email protected] 2697899283eaS[email protected] if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){ 26989da54300S[email protected] log_info("denying to pin request"); 2699899283eaS[email protected] connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST); 270034d2123cS[email protected] hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address); 27014c57c146S[email protected] return; 27024c57c146S[email protected] } 27034c57c146S[email protected] 2704dbe1a790S[email protected] if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){ 270534d2123cS[email protected] connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY); 270682d8f825S[email protected] log_info("IO Capability Request received, stack bondable %u, io cap %u", hci_stack->bondable, hci_stack->ssp_io_capability); 270782d8f825S[email protected] if (hci_stack->bondable && (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN)){ 2708106d6d11S[email protected] // tweak authentication requirements 27093a9fb326S[email protected] uint8_t authreq = hci_stack->ssp_authentication_requirement; 2710106d6d11S[email protected] if (connection->bonding_flags & BONDING_DEDICATED){ 27119faad3abS[email protected] authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 27129faad3abS[email protected] } 27139faad3abS[email protected] if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){ 27149faad3abS[email protected] authreq |= 1; 2715106d6d11S[email protected] } 27163a9fb326S[email protected] hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, NULL, authreq); 2717f8fb5f6eS[email protected] } else { 2718f8fb5f6eS[email protected] hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED); 2719f8fb5f6eS[email protected] } 2720dbe1a790S[email protected] return; 272132ab9390Smatthias.ringwald } 272232ab9390Smatthias.ringwald 2723dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){ 2724dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY); 272534d2123cS[email protected] hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address); 2726dbe1a790S[email protected] return; 2727dbe1a790S[email protected] } 2728dbe1a790S[email protected] 2729dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){ 2730dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY); 273134d2123cS[email protected] hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000); 2732dbe1a790S[email protected] return; 2733dbe1a790S[email protected] } 2734*35454696SMatthias Ringwald #endif 2735afd4e962S[email protected] 2736afd4e962S[email protected] if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){ 2737afd4e962S[email protected] connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES; 273834d2123cS[email protected] hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle); 27392bd8b7e7S[email protected] return; 27402bd8b7e7S[email protected] } 27412bd8b7e7S[email protected] 27422bd8b7e7S[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){ 27432bd8b7e7S[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK; 274434d2123cS[email protected] hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005); // authentication failure 274534d2123cS[email protected] return; 274634d2123cS[email protected] } 2747ad83dc6aS[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){ 2748ad83dc6aS[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE; 27491bd5283dS[email protected] connection->bonding_flags |= BONDING_EMIT_COMPLETE_ON_DISCONNECT; 275052d34f98S[email protected] hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // authentication done 2751ad83dc6aS[email protected] return; 2752ad83dc6aS[email protected] } 275334d2123cS[email protected] if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){ 275434d2123cS[email protected] connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST; 275534d2123cS[email protected] hci_send_cmd(&hci_authentication_requested, connection->con_handle); 27562bd8b7e7S[email protected] return; 2757afd4e962S[email protected] } 2758dce78009S[email protected] if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){ 2759dce78009S[email protected] connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST; 2760dce78009S[email protected] hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1); 2761dce78009S[email protected] return; 2762dce78009S[email protected] } 2763da886c03S[email protected] 2764a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 2765da886c03S[email protected] if (connection->le_con_parameter_update_state == CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS){ 2766da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 2767da886c03S[email protected] 2768c37a3166S[email protected] uint16_t connection_interval_min = connection->le_conn_interval_min; 2769c37a3166S[email protected] connection->le_conn_interval_min = 0; 2770c37a3166S[email protected] hci_send_cmd(&hci_le_connection_update, connection->con_handle, connection_interval_min, 2771c37a3166S[email protected] connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout, 2772c37a3166S[email protected] 0x0000, 0xffff); 2773c37a3166S[email protected] } 2774c37a3166S[email protected] #endif 2775dbe1a790S[email protected] } 2776c7e0c5f6Smatthias.ringwald 277705ae8de3SMatthias Ringwald hci_connection_t * connection; 27783a9fb326S[email protected] switch (hci_stack->state){ 27793429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 278074b323a9SMatthias Ringwald hci_initializing_run(); 27813429f56bSmatthias.ringwald break; 2782c7e0c5f6Smatthias.ringwald 2783c7e0c5f6Smatthias.ringwald case HCI_STATE_HALTING: 2784c7e0c5f6Smatthias.ringwald 27859da54300S[email protected] log_info("HCI_STATE_HALTING"); 27869956955bSMatthias Ringwald 27879956955bSMatthias Ringwald // free whitelist entries 2788a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 27899956955bSMatthias Ringwald { 2790665d90f2SMatthias Ringwald btstack_linked_list_iterator_t lit; 2791665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 2792665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 2793665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 2794665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry); 27959956955bSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 27969956955bSMatthias Ringwald } 27979956955bSMatthias Ringwald } 27989956955bSMatthias Ringwald #endif 2799c7e0c5f6Smatthias.ringwald // close all open connections 28003a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 2801c7e0c5f6Smatthias.ringwald if (connection){ 2802711e6c80SMatthias Ringwald hci_con_handle_t con_handle = (uint16_t) connection->con_handle; 2803d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 280432ab9390Smatthias.ringwald 28058837e9efSMatthias Ringwald log_info("HCI_STATE_HALTING, connection %p, handle %u", connection, con_handle); 2806c7e0c5f6Smatthias.ringwald 28078837e9efSMatthias Ringwald // cancel all l2cap connections right away instead of waiting for disconnection complete event ... 28088837e9efSMatthias Ringwald hci_emit_disconnection_complete(con_handle, 0x16); // terminated by local host 28098837e9efSMatthias Ringwald 28108837e9efSMatthias Ringwald // ... which would be ignored anyway as we shutdown (free) the connection now 2811c7e0c5f6Smatthias.ringwald hci_shutdown_connection(connection); 28128837e9efSMatthias Ringwald 28138837e9efSMatthias Ringwald // finally, send the disconnect command 28148837e9efSMatthias Ringwald hci_send_cmd(&hci_disconnect, con_handle, 0x13); // remote closed connection 2815c7e0c5f6Smatthias.ringwald return; 2816c7e0c5f6Smatthias.ringwald } 28179da54300S[email protected] log_info("HCI_STATE_HALTING, calling off"); 2818c7e0c5f6Smatthias.ringwald 281972ea5239Smatthias.ringwald // switch mode 2820c7e0c5f6Smatthias.ringwald hci_power_control_off(); 28219418f9c9Smatthias.ringwald 28229da54300S[email protected] log_info("HCI_STATE_HALTING, emitting state"); 282372ea5239Smatthias.ringwald hci_emit_state(); 28249da54300S[email protected] log_info("HCI_STATE_HALTING, done"); 282572ea5239Smatthias.ringwald break; 2826c7e0c5f6Smatthias.ringwald 282772ea5239Smatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 28283a9fb326S[email protected] switch(hci_stack->substate) { 282974b323a9SMatthias Ringwald case HCI_FALLING_ASLEEP_DISCONNECT: 28309da54300S[email protected] log_info("HCI_STATE_FALLING_ASLEEP"); 283172ea5239Smatthias.ringwald // close all open connections 28323a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 283366da7044Smatthias.ringwald 2834423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 283566da7044Smatthias.ringwald // don't close connections, if H4 supports power management 2836d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 283766da7044Smatthias.ringwald connection = NULL; 283866da7044Smatthias.ringwald } 283966da7044Smatthias.ringwald #endif 284072ea5239Smatthias.ringwald if (connection){ 284132ab9390Smatthias.ringwald 284272ea5239Smatthias.ringwald // send disconnect 2843d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 284432ab9390Smatthias.ringwald 28459da54300S[email protected] log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u", connection, (uint16_t)connection->con_handle); 28466ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 284772ea5239Smatthias.ringwald 284872ea5239Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 284972ea5239Smatthias.ringwald hci_shutdown_connection(connection); 285072ea5239Smatthias.ringwald return; 285172ea5239Smatthias.ringwald } 285272ea5239Smatthias.ringwald 285392368cd3S[email protected] if (hci_classic_supported()){ 285489db417bSmatthias.ringwald // disable page and inquiry scan 2855d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 285632ab9390Smatthias.ringwald 28579da54300S[email protected] log_info("HCI_STATE_HALTING, disabling inq scans"); 28583a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan 285989db417bSmatthias.ringwald 286089db417bSmatthias.ringwald // continue in next sub state 286174b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE; 286289db417bSmatthias.ringwald break; 286392368cd3S[email protected] } 2864202c8a4cSMatthias Ringwald // no break - fall through for ble-only chips 286592368cd3S[email protected] 286674b323a9SMatthias Ringwald case HCI_FALLING_ASLEEP_COMPLETE: 28679da54300S[email protected] log_info("HCI_STATE_HALTING, calling sleep"); 2868423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 286928171530Smatthias.ringwald // don't actually go to sleep, if H4 supports power management 2870d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 287128171530Smatthias.ringwald // SLEEP MODE reached 28723a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 287328171530Smatthias.ringwald hci_emit_state(); 287428171530Smatthias.ringwald break; 287528171530Smatthias.ringwald } 287628171530Smatthias.ringwald #endif 287772ea5239Smatthias.ringwald // switch mode 28783a9fb326S[email protected] hci_power_control_sleep(); // changes hci_stack->state to SLEEP 2879c7e0c5f6Smatthias.ringwald hci_emit_state(); 288028171530Smatthias.ringwald break; 288128171530Smatthias.ringwald 288289db417bSmatthias.ringwald default: 288389db417bSmatthias.ringwald break; 288489db417bSmatthias.ringwald } 2885c7e0c5f6Smatthias.ringwald break; 2886c7e0c5f6Smatthias.ringwald 28873429f56bSmatthias.ringwald default: 28883429f56bSmatthias.ringwald break; 28891f504dbdSmatthias.ringwald } 28903429f56bSmatthias.ringwald } 289116833f0aSmatthias.ringwald 289231452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 2893*35454696SMatthias Ringwald // house-keeping 2894*35454696SMatthias Ringwald 2895*35454696SMatthias Ringwald if (IS_COMMAND(packet, hci_write_loopback_mode)){ 2896*35454696SMatthias Ringwald hci_stack->loopback_mode = packet[3]; 2897*35454696SMatthias Ringwald } 2898*35454696SMatthias Ringwald 2899*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2900c8e4258aSmatthias.ringwald bd_addr_t addr; 2901c8e4258aSmatthias.ringwald hci_connection_t * conn; 2902c8e4258aSmatthias.ringwald 2903c8e4258aSmatthias.ringwald // create_connection? 2904c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 2905724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 29069da54300S[email protected] log_info("Create_connection to %s", bd_addr_to_str(addr)); 2907c8e4258aSmatthias.ringwald 29082e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 2909ad83dc6aS[email protected] if (!conn){ 291096a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 291117f1ba2aSmatthias.ringwald if (!conn){ 291217f1ba2aSmatthias.ringwald // notify client that alloc failed 29132deddeceSMatthias Ringwald hci_emit_connection_complete(addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 291417f1ba2aSmatthias.ringwald return 0; // don't sent packet to controller 291517f1ba2aSmatthias.ringwald } 2916ad83dc6aS[email protected] conn->state = SEND_CREATE_CONNECTION; 2917ad83dc6aS[email protected] } 2918ad83dc6aS[email protected] log_info("conn state %u", conn->state); 2919ad83dc6aS[email protected] switch (conn->state){ 2920ad83dc6aS[email protected] // if connection active exists 2921ad83dc6aS[email protected] case OPEN: 292262bda3fbS[email protected] // and OPEN, emit connection complete command, don't send to controller 2923274dad91SMatthias Ringwald hci_emit_connection_complete(addr, conn->con_handle, 0); 292462bda3fbS[email protected] return 0; 2925ad83dc6aS[email protected] case SEND_CREATE_CONNECTION: 2926ad83dc6aS[email protected] // connection created by hci, e.g. dedicated bonding 2927ad83dc6aS[email protected] break; 2928ad83dc6aS[email protected] default: 2929ad83dc6aS[email protected] // otherwise, just ignore as it is already in the open process 2930ad83dc6aS[email protected] return 0; 2931ad83dc6aS[email protected] } 2932c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 2933c8e4258aSmatthias.ringwald } 2934*35454696SMatthias Ringwald 29357fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_reply)){ 29367fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY); 29377fde4af9Smatthias.ringwald } 29387fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){ 29397fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST); 29407fde4af9Smatthias.ringwald } 29417fde4af9Smatthias.ringwald 29428ef73945Smatthias.ringwald if (IS_COMMAND(packet, hci_delete_stored_link_key)){ 2943a98592bcSMatthias Ringwald if (hci_stack->link_key_db){ 2944724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 2945a98592bcSMatthias Ringwald hci_stack->link_key_db->delete_link_key(addr); 29468ef73945Smatthias.ringwald } 29478ef73945Smatthias.ringwald } 2948c8e4258aSmatthias.ringwald 29496724cd9eS[email protected] if (IS_COMMAND(packet, hci_pin_code_request_negative_reply) 29506724cd9eS[email protected] || IS_COMMAND(packet, hci_pin_code_request_reply)){ 2951724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 29522e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 29536724cd9eS[email protected] if (conn){ 29546724cd9eS[email protected] connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE); 29556724cd9eS[email protected] } 29566724cd9eS[email protected] } 29576724cd9eS[email protected] 29586724cd9eS[email protected] if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply) 29596724cd9eS[email protected] || IS_COMMAND(packet, hci_user_confirmation_request_reply) 29606724cd9eS[email protected] || IS_COMMAND(packet, hci_user_passkey_request_negative_reply) 29616724cd9eS[email protected] || IS_COMMAND(packet, hci_user_passkey_request_reply)) { 2962724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 29632e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 29646724cd9eS[email protected] if (conn){ 29656724cd9eS[email protected] connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE); 29666724cd9eS[email protected] } 29676724cd9eS[email protected] } 2968*35454696SMatthias Ringwald #endif 29694b3e1e19SMatthias Ringwald 2970a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 297169a97523S[email protected] if (IS_COMMAND(packet, hci_le_set_advertising_parameters)){ 29723a9fb326S[email protected] hci_stack->adv_addr_type = packet[8]; 297369a97523S[email protected] } 297469a97523S[email protected] if (IS_COMMAND(packet, hci_le_set_random_address)){ 2975724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], hci_stack->adv_address); 297669a97523S[email protected] } 2977171293d3SMatthias Ringwald if (IS_COMMAND(packet, hci_le_set_advertise_enable)){ 2978171293d3SMatthias Ringwald hci_stack->le_advertisements_active = packet[3]; 2979171293d3SMatthias Ringwald } 2980b04dfa37SMatthias Ringwald if (IS_COMMAND(packet, hci_le_create_connection)){ 2981b04dfa37SMatthias Ringwald // white list used? 2982b04dfa37SMatthias Ringwald uint8_t initiator_filter_policy = packet[7]; 2983b04dfa37SMatthias Ringwald switch (initiator_filter_policy){ 2984b04dfa37SMatthias Ringwald case 0: 2985b04dfa37SMatthias Ringwald // whitelist not used 2986b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_DIRECT; 2987b04dfa37SMatthias Ringwald break; 2988b04dfa37SMatthias Ringwald case 1: 2989b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_WHITELIST; 2990b04dfa37SMatthias Ringwald break; 2991b04dfa37SMatthias Ringwald default: 2992b04dfa37SMatthias Ringwald log_error("Invalid initiator_filter_policy in LE Create Connection %u", initiator_filter_policy); 2993b04dfa37SMatthias Ringwald break; 2994b04dfa37SMatthias Ringwald } 2995b04dfa37SMatthias Ringwald } 2996b04dfa37SMatthias Ringwald if (IS_COMMAND(packet, hci_le_create_connection_cancel)){ 2997b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 2998b04dfa37SMatthias Ringwald } 299969a97523S[email protected] #endif 300069a97523S[email protected] 30013a9fb326S[email protected] hci_stack->num_cmd_packets--; 30025bb5bc3eS[email protected] 30035bb5bc3eS[email protected] hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size); 30046b4af23dS[email protected] int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 30056b4af23dS[email protected] 3006d051460cS[email protected] // release packet buffer for synchronous transport implementations 3007c8b9416aS[email protected] if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){ 30086b4af23dS[email protected] hci_stack->hci_packet_buffer_reserved = 0; 30096b4af23dS[email protected] } 30106b4af23dS[email protected] 30116b4af23dS[email protected] return err; 301231452debSmatthias.ringwald } 30138adf0ddaSmatthias.ringwald 30142bd8b7e7S[email protected] // disconnect because of security block 30152bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){ 30162bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 30172bd8b7e7S[email protected] if (!connection) return; 30182bd8b7e7S[email protected] connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 30192bd8b7e7S[email protected] } 30202bd8b7e7S[email protected] 30212bd8b7e7S[email protected] 3022dbe1a790S[email protected] // Configure Secure Simple Pairing 3023dbe1a790S[email protected] 3024*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 3025*35454696SMatthias Ringwald 3026dbe1a790S[email protected] // enable will enable SSP during init 302715a95bd5SMatthias Ringwald void gap_ssp_set_enable(int enable){ 30283a9fb326S[email protected] hci_stack->ssp_enable = enable; 3029dbe1a790S[email protected] } 3030dbe1a790S[email protected] 303195d71764SMatthias Ringwald static int hci_local_ssp_activated(void){ 303215a95bd5SMatthias Ringwald return gap_ssp_supported() && hci_stack->ssp_enable; 30332bd8b7e7S[email protected] } 30342bd8b7e7S[email protected] 3035dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement 303615a95bd5SMatthias Ringwald void gap_ssp_set_io_capability(int io_capability){ 30373a9fb326S[email protected] hci_stack->ssp_io_capability = io_capability; 3038dbe1a790S[email protected] } 303915a95bd5SMatthias Ringwald void gap_ssp_set_authentication_requirement(int authentication_requirement){ 30403a9fb326S[email protected] hci_stack->ssp_authentication_requirement = authentication_requirement; 3041dbe1a790S[email protected] } 3042dbe1a790S[email protected] 3043dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested 304415a95bd5SMatthias Ringwald void gap_ssp_set_auto_accept(int auto_accept){ 30453a9fb326S[email protected] hci_stack->ssp_auto_accept = auto_accept; 3046dbe1a790S[email protected] } 3047*35454696SMatthias Ringwald #endif 3048dbe1a790S[email protected] 30491cd208adSmatthias.ringwald /** 30501cd208adSmatthias.ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 30511cd208adSmatthias.ringwald */ 3052fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){ 30535127cc62S[email protected] 3054d94d3cafS[email protected] if (!hci_can_send_command_packet_now()){ 30559d14b626S[email protected] log_error("hci_send_cmd called but cannot send packet now"); 30569d14b626S[email protected] return 0; 30579d14b626S[email protected] } 30589d14b626S[email protected] 30595127cc62S[email protected] // for HCI INITIALIZATION 30609da54300S[email protected] // log_info("hci_send_cmd: opcode %04x", cmd->opcode); 30615127cc62S[email protected] hci_stack->last_cmd_opcode = cmd->opcode; 30625127cc62S[email protected] 30639d14b626S[email protected] hci_reserve_packet_buffer(); 30649d14b626S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 30659d14b626S[email protected] 30661cd208adSmatthias.ringwald va_list argptr; 30671cd208adSmatthias.ringwald va_start(argptr, cmd); 3068bcc59d6cSMatthias Ringwald uint16_t size = hci_cmd_create_from_template(packet, cmd, argptr); 30691cd208adSmatthias.ringwald va_end(argptr); 30709d14b626S[email protected] 30719d14b626S[email protected] return hci_send_cmd_packet(packet, size); 307293b8dc03Smatthias.ringwald } 3073c8e4258aSmatthias.ringwald 3074ee091cf1Smatthias.ringwald // Create various non-HCI events. 3075ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 3076ee091cf1Smatthias.ringwald 3077d6b06661SMatthias Ringwald static void hci_emit_event(uint8_t * event, uint16_t size, int dump){ 3078fb37a842SMatthias Ringwald // dump packet 3079d6b06661SMatthias Ringwald if (dump) { 3080300c1ba4SMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, size); 3081d6b06661SMatthias Ringwald } 30821ef6bb52SMatthias Ringwald 3083fb37a842SMatthias Ringwald // dispatch to all event handlers 30841ef6bb52SMatthias Ringwald btstack_linked_list_iterator_t it; 30851ef6bb52SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->event_handlers); 30861ef6bb52SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 30871ef6bb52SMatthias Ringwald btstack_packet_callback_registration_t * entry = (btstack_packet_callback_registration_t*) btstack_linked_list_iterator_next(&it); 3088d9a7306aSMatthias Ringwald entry->callback(HCI_EVENT_PACKET, 0, event, size); 30891ef6bb52SMatthias Ringwald } 3090d6b06661SMatthias Ringwald } 3091d6b06661SMatthias Ringwald 3092d6b06661SMatthias Ringwald static void hci_emit_acl_packet(uint8_t * packet, uint16_t size){ 3093fb37a842SMatthias Ringwald if (!hci_stack->acl_packet_handler) return; 30943d50b4baSMatthias Ringwald hci_stack->acl_packet_handler(HCI_ACL_DATA_PACKET, 0, packet, size); 3095d6b06661SMatthias Ringwald } 3096d6b06661SMatthias Ringwald 3097*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 3098701e3307SMatthias Ringwald static void hci_notify_if_sco_can_send_now(void){ 30993bc639ceSMatthias Ringwald // notify SCO sender if waiting 3100701e3307SMatthias Ringwald if (!hci_stack->sco_waiting_for_can_send_now) return; 3101701e3307SMatthias Ringwald if (hci_can_send_sco_packet_now()){ 31023bc639ceSMatthias Ringwald hci_stack->sco_waiting_for_can_send_now = 0; 3103701e3307SMatthias Ringwald uint8_t event[2] = { HCI_EVENT_SCO_CAN_SEND_NOW, 0 }; 3104701e3307SMatthias Ringwald hci_dump_packet(HCI_EVENT_PACKET, 1, event, sizeof(event)); 31053d50b4baSMatthias Ringwald hci_stack->sco_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event)); 31063bc639ceSMatthias Ringwald } 31073bc639ceSMatthias Ringwald } 3108*35454696SMatthias Ringwald #endif 31093bc639ceSMatthias Ringwald 311071de195eSMatthias Ringwald void hci_emit_state(void){ 31113a9fb326S[email protected] log_info("BTSTACK_EVENT_STATE %u", hci_stack->state); 3112425d1371Smatthias.ringwald uint8_t event[3]; 311380d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 3114425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 31153a9fb326S[email protected] event[2] = hci_stack->state; 3116d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3117c8e4258aSmatthias.ringwald } 3118c8e4258aSmatthias.ringwald 3119*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 31202deddeceSMatthias Ringwald static void hci_emit_connection_complete(bd_addr_t address, hci_con_handle_t con_handle, uint8_t status){ 3121425d1371Smatthias.ringwald uint8_t event[13]; 3122c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 3123425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 312417f1ba2aSmatthias.ringwald event[2] = status; 31252deddeceSMatthias Ringwald little_endian_store_16(event, 3, con_handle); 31262deddeceSMatthias Ringwald reverse_bd_addr(address, &event[5]); 3127c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 3128c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 3129d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3130c8e4258aSmatthias.ringwald } 3131*35454696SMatthias Ringwald #endif 3132c8e4258aSmatthias.ringwald 3133*35454696SMatthias Ringwald #ifdef ENABLE_BLE 3134fc64f94aSMatthias 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){ 31354f3229d8S[email protected] uint8_t event[21]; 31364f3229d8S[email protected] event[0] = HCI_EVENT_LE_META; 31374f3229d8S[email protected] event[1] = sizeof(event) - 2; 31384f3229d8S[email protected] event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE; 31394f3229d8S[email protected] event[3] = status; 3140fc64f94aSMatthias Ringwald little_endian_store_16(event, 4, con_handle); 31414f3229d8S[email protected] event[6] = 0; // TODO: role 31426e2e9a6bS[email protected] event[7] = address_type; 3143724d70a2SMatthias Ringwald reverse_bd_addr(address, &event[8]); 3144f8fbdce0SMatthias Ringwald little_endian_store_16(event, 14, 0); // interval 3145f8fbdce0SMatthias Ringwald little_endian_store_16(event, 16, 0); // latency 3146f8fbdce0SMatthias Ringwald little_endian_store_16(event, 18, 0); // supervision timeout 31474f3229d8S[email protected] event[20] = 0; // master clock accuracy 3148d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 31494f3229d8S[email protected] } 3150*35454696SMatthias Ringwald #endif 31514f3229d8S[email protected] 3152fc64f94aSMatthias Ringwald static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason){ 3153425d1371Smatthias.ringwald uint8_t event[6]; 31543c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 3155e518c4b8Smatthias.ringwald event[1] = sizeof(event) - 2; 31563c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 3157fc64f94aSMatthias Ringwald little_endian_store_16(event, 3, con_handle); 31583c4d4b90Smatthias.ringwald event[5] = reason; 3159d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 31603c4d4b90Smatthias.ringwald } 31613c4d4b90Smatthias.ringwald 3162b83d5eabSMatthias Ringwald static void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 316366fb9560S[email protected] if (disable_l2cap_timeouts) return; 3164e0abb8e7S[email protected] log_info("L2CAP_EVENT_TIMEOUT_CHECK"); 3165425d1371Smatthias.ringwald uint8_t event[4]; 316680d52d6bSmatthias.ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 3167425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 3168f8fbdce0SMatthias Ringwald little_endian_store_16(event, 2, conn->con_handle); 3169d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3170ee091cf1Smatthias.ringwald } 317143bfb1bdSmatthias.ringwald 3172b83d5eabSMatthias Ringwald static void hci_emit_nr_connections_changed(void){ 3173e0abb8e7S[email protected] log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections()); 3174425d1371Smatthias.ringwald uint8_t event[3]; 317580d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 3176425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 317743bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 3178d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 317943bfb1bdSmatthias.ringwald } 3180038bc64cSmatthias.ringwald 3181b83d5eabSMatthias Ringwald static void hci_emit_hci_open_failed(void){ 3182e0abb8e7S[email protected] log_info("BTSTACK_EVENT_POWERON_FAILED"); 3183425d1371Smatthias.ringwald uint8_t event[2]; 318480d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 3185425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 3186d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3187038bc64cSmatthias.ringwald } 31881b0e3922Smatthias.ringwald 3189*35454696SMatthias Ringwald static void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status){ 3190*35454696SMatthias Ringwald log_info("hci_emit_dedicated_bonding_result %u ", status); 3191*35454696SMatthias Ringwald uint8_t event[9]; 3192*35454696SMatthias Ringwald int pos = 0; 3193*35454696SMatthias Ringwald event[pos++] = GAP_EVENT_DEDICATED_BONDING_COMPLETED; 3194*35454696SMatthias Ringwald event[pos++] = sizeof(event) - 2; 3195*35454696SMatthias Ringwald event[pos++] = status; 3196*35454696SMatthias Ringwald reverse_bd_addr(address, &event[pos]); 3197d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3198381fbed8Smatthias.ringwald } 3199458bf4e8S[email protected] 3200*35454696SMatthias Ringwald 3201*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 3202*35454696SMatthias Ringwald 3203b83d5eabSMatthias Ringwald static void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){ 3204df3354fcS[email protected] log_info("hci_emit_security_level %u for handle %x", level, con_handle); 3205a00031e2S[email protected] uint8_t event[5]; 3206e00caf9cS[email protected] int pos = 0; 32075611a760SMatthias Ringwald event[pos++] = GAP_EVENT_SECURITY_LEVEL; 3208e00caf9cS[email protected] event[pos++] = sizeof(event) - 2; 3209f8fbdce0SMatthias Ringwald little_endian_store_16(event, 2, con_handle); 3210e00caf9cS[email protected] pos += 2; 3211e00caf9cS[email protected] event[pos++] = level; 3212d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3213e00caf9cS[email protected] } 3214e00caf9cS[email protected] 3215*35454696SMatthias Ringwald static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){ 3216*35454696SMatthias Ringwald if (!connection) return LEVEL_0; 3217*35454696SMatthias Ringwald if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0; 3218*35454696SMatthias Ringwald return gap_security_level_for_link_key_type(connection->link_key_type); 3219*35454696SMatthias Ringwald } 3220*35454696SMatthias Ringwald 3221*35454696SMatthias Ringwald static void hci_emit_discoverable_enabled(uint8_t enabled){ 3222*35454696SMatthias Ringwald log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled); 3223*35454696SMatthias Ringwald uint8_t event[3]; 3224*35454696SMatthias Ringwald event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED; 3225*35454696SMatthias Ringwald event[1] = sizeof(event) - 2; 3226*35454696SMatthias Ringwald event[2] = enabled; 3227d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3228ad83dc6aS[email protected] } 3229ad83dc6aS[email protected] 323098a2fd1cSMatthias Ringwald // query if remote side supports eSCO 3231073bd0faSMatthias Ringwald int hci_remote_esco_supported(hci_con_handle_t con_handle){ 323298a2fd1cSMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 323398a2fd1cSMatthias Ringwald if (!connection) return 0; 323498a2fd1cSMatthias Ringwald return connection->remote_supported_feature_eSCO; 323598a2fd1cSMatthias Ringwald } 323698a2fd1cSMatthias Ringwald 32372bd8b7e7S[email protected] // query if remote side supports SSP 32382bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){ 32392bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 32402bd8b7e7S[email protected] if (!connection) return 0; 32412bd8b7e7S[email protected] return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0; 32422bd8b7e7S[email protected] } 32432bd8b7e7S[email protected] 324415a95bd5SMatthias Ringwald int gap_ssp_supported_on_both_sides(hci_con_handle_t handle){ 3245df3354fcS[email protected] return hci_local_ssp_activated() && hci_remote_ssp_supported(handle); 3246df3354fcS[email protected] } 3247df3354fcS[email protected] 3248458bf4e8S[email protected] // GAP API 3249458bf4e8S[email protected] /** 3250458bf4e8S[email protected] * @bbrief enable/disable bonding. default is enabled 3251458bf4e8S[email protected] * @praram enabled 3252458bf4e8S[email protected] */ 32534c57c146S[email protected] void gap_set_bondable_mode(int enable){ 32543a9fb326S[email protected] hci_stack->bondable = enable ? 1 : 0; 3255458bf4e8S[email protected] } 32564ef6443cSMatthias Ringwald /** 32574ef6443cSMatthias Ringwald * @brief Get bondable mode. 32584ef6443cSMatthias Ringwald * @return 1 if bondable 32594ef6443cSMatthias Ringwald */ 32604ef6443cSMatthias Ringwald int gap_get_bondable_mode(void){ 32614ef6443cSMatthias Ringwald return hci_stack->bondable; 32624ef6443cSMatthias Ringwald } 3263cb230b9dS[email protected] 3264cb230b9dS[email protected] /** 326534d2123cS[email protected] * @brief map link keys to security levels 3266cb230b9dS[email protected] */ 326734d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){ 326834d2123cS[email protected] switch (link_key_type){ 32693c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 32703c68dfa9S[email protected] return LEVEL_4; 32713c68dfa9S[email protected] case COMBINATION_KEY: 32723c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 32733c68dfa9S[email protected] return LEVEL_3; 32743c68dfa9S[email protected] default: 32753c68dfa9S[email protected] return LEVEL_2; 32763c68dfa9S[email protected] } 3277cb230b9dS[email protected] } 3278cb230b9dS[email protected] 3279106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){ 32805127cc62S[email protected] log_info("gap_mitm_protection_required_for_security_level %u", level); 3281106d6d11S[email protected] return level > LEVEL_2; 3282106d6d11S[email protected] } 3283106d6d11S[email protected] 328434d2123cS[email protected] /** 328534d2123cS[email protected] * @brief get current security level 328634d2123cS[email protected] */ 328734d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){ 328834d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 328934d2123cS[email protected] if (!connection) return LEVEL_0; 329034d2123cS[email protected] return gap_security_level_for_connection(connection); 329134d2123cS[email protected] } 329234d2123cS[email protected] 3293cb230b9dS[email protected] /** 3294cb230b9dS[email protected] * @brief request connection to device to 3295cb230b9dS[email protected] * @result GAP_AUTHENTICATION_RESULT 3296cb230b9dS[email protected] */ 329734d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){ 329834d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 329934d2123cS[email protected] if (!connection){ 3300a00031e2S[email protected] hci_emit_security_level(con_handle, LEVEL_0); 330134d2123cS[email protected] return; 330234d2123cS[email protected] } 330334d2123cS[email protected] gap_security_level_t current_level = gap_security_level(con_handle); 330434d2123cS[email protected] log_info("gap_request_security_level %u, current level %u", requested_level, current_level); 330534d2123cS[email protected] if (current_level >= requested_level){ 3306a00031e2S[email protected] hci_emit_security_level(con_handle, current_level); 330734d2123cS[email protected] return; 330834d2123cS[email protected] } 3309a00031e2S[email protected] 331034d2123cS[email protected] connection->requested_security_level = requested_level; 3311a00031e2S[email protected] 331225bf5872S[email protected] #if 0 331325bf5872S[email protected] // sending encryption request without a link key results in an error. 331425bf5872S[email protected] // TODO: figure out how to use it properly 331525bf5872S[email protected] 3316fb8ba0dbS[email protected] // would enabling ecnryption suffice (>= LEVEL_2)? 3317a98592bcSMatthias Ringwald if (hci_stack->link_key_db){ 3318a00031e2S[email protected] link_key_type_t link_key_type; 3319a00031e2S[email protected] link_key_t link_key; 3320a98592bcSMatthias Ringwald if (hci_stack->link_key_db->get_link_key( &connection->address, &link_key, &link_key_type)){ 3321a00031e2S[email protected] if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){ 3322a00031e2S[email protected] connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 3323a00031e2S[email protected] return; 3324a00031e2S[email protected] } 3325a00031e2S[email protected] } 3326a00031e2S[email protected] } 332725bf5872S[email protected] #endif 3328a00031e2S[email protected] 33291eb2563eS[email protected] // try to authenticate connection 33301eb2563eS[email protected] connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 3331e80b2cf9S[email protected] hci_run(); 3332e00caf9cS[email protected] } 3333ad83dc6aS[email protected] 3334ad83dc6aS[email protected] /** 3335ad83dc6aS[email protected] * @brief start dedicated bonding with device. disconnect after bonding 3336ad83dc6aS[email protected] * @param device 3337ad83dc6aS[email protected] * @param request MITM protection 3338ad83dc6aS[email protected] * @result GAP_DEDICATED_BONDING_COMPLETE 3339ad83dc6aS[email protected] */ 3340ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){ 3341ad83dc6aS[email protected] 3342ad83dc6aS[email protected] // create connection state machine 334396a45072S[email protected] hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_CLASSIC); 3344ad83dc6aS[email protected] 3345ad83dc6aS[email protected] if (!connection){ 3346ad83dc6aS[email protected] return BTSTACK_MEMORY_ALLOC_FAILED; 3347ad83dc6aS[email protected] } 3348ad83dc6aS[email protected] 3349ad83dc6aS[email protected] // delete linkn key 335015a95bd5SMatthias Ringwald gap_drop_link_key_for_bd_addr(device); 3351ad83dc6aS[email protected] 3352ad83dc6aS[email protected] // configure LEVEL_2/3, dedicated bonding 3353ad83dc6aS[email protected] connection->state = SEND_CREATE_CONNECTION; 3354ad83dc6aS[email protected] connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2; 3355f04a0c31SMatthias Ringwald log_info("gap_dedicated_bonding, mitm %d -> level %u", mitm_protection_required, connection->requested_security_level); 3356ad83dc6aS[email protected] connection->bonding_flags = BONDING_DEDICATED; 3357ad83dc6aS[email protected] 3358ad83dc6aS[email protected] // wait for GAP Security Result and send GAP Dedicated Bonding complete 3359ad83dc6aS[email protected] 3360ad83dc6aS[email protected] // handle: connnection failure (connection complete != ok) 3361ad83dc6aS[email protected] // handle: authentication failure 3362ad83dc6aS[email protected] // handle: disconnect on done 3363ad83dc6aS[email protected] 3364ad83dc6aS[email protected] hci_run(); 3365ad83dc6aS[email protected] 3366ad83dc6aS[email protected] return 0; 3367ad83dc6aS[email protected] } 3368*35454696SMatthias Ringwald #endif 33698e618f72S[email protected] 33708e618f72S[email protected] void gap_set_local_name(const char * local_name){ 33718e618f72S[email protected] hci_stack->local_name = local_name; 33728e618f72S[email protected] } 33738e618f72S[email protected] 3374*35454696SMatthias Ringwald 3375*35454696SMatthias Ringwald #ifdef ENABLE_BLE 3376*35454696SMatthias Ringwald 3377d8e8f12aSMatthias Ringwald void gap_start_scan(void){ 3378d8e8f12aSMatthias Ringwald if (hci_stack->le_scanning_state == LE_SCANNING) return; 33797bdc6798S[email protected] hci_stack->le_scanning_state = LE_START_SCAN; 33807bdc6798S[email protected] hci_run(); 33817bdc6798S[email protected] } 33828e618f72S[email protected] 3383d8e8f12aSMatthias Ringwald void gap_stop_scan(void){ 3384d8e8f12aSMatthias Ringwald if ( hci_stack->le_scanning_state == LE_SCAN_IDLE) return; 33857bdc6798S[email protected] hci_stack->le_scanning_state = LE_STOP_SCAN; 33867bdc6798S[email protected] hci_run(); 33877bdc6798S[email protected] } 33884f3229d8S[email protected] 3389d8e8f12aSMatthias Ringwald void gap_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){ 3390ef11999fSmatthias.ringwald hci_stack->le_scan_type = scan_type; 3391ef11999fSmatthias.ringwald hci_stack->le_scan_interval = scan_interval; 3392ef11999fSmatthias.ringwald hci_stack->le_scan_window = scan_window; 3393ef11999fSmatthias.ringwald hci_run(); 3394ef11999fSmatthias.ringwald } 33954f3229d8S[email protected] 3396d8e8f12aSMatthias Ringwald uint8_t gap_connect(bd_addr_t addr, bd_addr_type_t addr_type){ 33974f3229d8S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 33984f3229d8S[email protected] if (!conn){ 3399d8e8f12aSMatthias Ringwald log_info("gap_connect: no connection exists yet, creating context"); 34002e77e513S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 34014f3229d8S[email protected] if (!conn){ 34024f3229d8S[email protected] // notify client that alloc failed 34036e2e9a6bS[email protected] hci_emit_le_connection_complete(addr_type, addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 3404d8e8f12aSMatthias Ringwald log_info("gap_connect: failed to alloc hci_connection_t"); 3405472a5742SMatthias Ringwald return GATT_CLIENT_NOT_CONNECTED; // don't sent packet to controller 34064f3229d8S[email protected] } 34074f3229d8S[email protected] conn->state = SEND_CREATE_CONNECTION; 3408d8e8f12aSMatthias Ringwald log_info("gap_connect: send create connection next"); 3409564fca32S[email protected] hci_run(); 3410616edd56SMatthias Ringwald return 0; 34114f3229d8S[email protected] } 34120bf6344aS[email protected] 34130bf6344aS[email protected] if (!hci_is_le_connection(conn) || 34140bf6344aS[email protected] conn->state == SEND_CREATE_CONNECTION || 34150bf6344aS[email protected] conn->state == SENT_CREATE_CONNECTION) { 34162e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_COMMAND_DISALLOWED); 3417d8e8f12aSMatthias Ringwald log_error("gap_connect: classic connection or connect is already being created"); 3418616edd56SMatthias Ringwald return GATT_CLIENT_IN_WRONG_STATE; 34190bf6344aS[email protected] } 34200bf6344aS[email protected] 3421d8e8f12aSMatthias Ringwald log_info("gap_connect: context exists with state %u", conn->state); 34222e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, conn->con_handle, 0); 34234f3229d8S[email protected] hci_run(); 3424616edd56SMatthias Ringwald return 0; 34254f3229d8S[email protected] } 34264f3229d8S[email protected] 34277851196eSmatthias.ringwald // @assumption: only a single outgoing LE Connection exists 3428d8e8f12aSMatthias Ringwald static hci_connection_t * gap_get_outgoing_connection(void){ 3429665d90f2SMatthias Ringwald btstack_linked_item_t *it; 3430665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 34310bf6344aS[email protected] hci_connection_t * conn = (hci_connection_t *) it; 34320bf6344aS[email protected] if (!hci_is_le_connection(conn)) continue; 34330bf6344aS[email protected] switch (conn->state){ 3434a6725849S[email protected] case SEND_CREATE_CONNECTION: 34357851196eSmatthias.ringwald case SENT_CREATE_CONNECTION: 34367851196eSmatthias.ringwald return conn; 34377851196eSmatthias.ringwald default: 34387851196eSmatthias.ringwald break; 34397851196eSmatthias.ringwald }; 34407851196eSmatthias.ringwald } 34417851196eSmatthias.ringwald return NULL; 34427851196eSmatthias.ringwald } 34437851196eSmatthias.ringwald 3444d8e8f12aSMatthias Ringwald uint8_t gap_connect_cancel(void){ 3445d8e8f12aSMatthias Ringwald hci_connection_t * conn = gap_get_outgoing_connection(); 3446616edd56SMatthias Ringwald if (!conn) return 0; 34477851196eSmatthias.ringwald switch (conn->state){ 34487851196eSmatthias.ringwald case SEND_CREATE_CONNECTION: 34497851196eSmatthias.ringwald // skip sending create connection and emit event instead 34502e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER); 3451665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 34527851196eSmatthias.ringwald btstack_memory_hci_connection_free( conn ); 34530bf6344aS[email protected] break; 3454a6725849S[email protected] case SENT_CREATE_CONNECTION: 34557851196eSmatthias.ringwald // request to send cancel connection 34560bf6344aS[email protected] conn->state = SEND_CANCEL_CONNECTION; 34570bf6344aS[email protected] hci_run(); 34580bf6344aS[email protected] break; 34590bf6344aS[email protected] default: 34600bf6344aS[email protected] break; 34610bf6344aS[email protected] } 3462616edd56SMatthias Ringwald return 0; 3463e31f89a7S[email protected] } 34644f3229d8S[email protected] 3465c37a3166S[email protected] /** 3466c37a3166S[email protected] * @brief Updates the connection parameters for a given LE connection 3467c37a3166S[email protected] * @param handle 3468c37a3166S[email protected] * @param conn_interval_min (unit: 1.25ms) 3469c37a3166S[email protected] * @param conn_interval_max (unit: 1.25ms) 3470c37a3166S[email protected] * @param conn_latency 3471c37a3166S[email protected] * @param supervision_timeout (unit: 10ms) 3472c37a3166S[email protected] * @returns 0 if ok 3473c37a3166S[email protected] */ 3474c37a3166S[email protected] int gap_update_connection_parameters(hci_con_handle_t con_handle, uint16_t conn_interval_min, 3475c37a3166S[email protected] uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 3476c37a3166S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 3477c37a3166S[email protected] if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3478c37a3166S[email protected] connection->le_conn_interval_min = conn_interval_min; 3479c37a3166S[email protected] connection->le_conn_interval_max = conn_interval_max; 3480c37a3166S[email protected] connection->le_conn_latency = conn_latency; 3481c37a3166S[email protected] connection->le_supervision_timeout = supervision_timeout; 348284cf6d83SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS; 3483cfc59f1bSMatthias Ringwald hci_run(); 3484c37a3166S[email protected] return 0; 3485c37a3166S[email protected] } 3486c37a3166S[email protected] 348745c102fdSMatthias Ringwald /** 3488b68d7bc3SMatthias Ringwald * @brief Request an update of the connection parameter for a given LE connection 3489b68d7bc3SMatthias Ringwald * @param handle 3490b68d7bc3SMatthias Ringwald * @param conn_interval_min (unit: 1.25ms) 3491b68d7bc3SMatthias Ringwald * @param conn_interval_max (unit: 1.25ms) 3492b68d7bc3SMatthias Ringwald * @param conn_latency 3493b68d7bc3SMatthias Ringwald * @param supervision_timeout (unit: 10ms) 3494b68d7bc3SMatthias Ringwald * @returns 0 if ok 3495b68d7bc3SMatthias Ringwald */ 3496b68d7bc3SMatthias Ringwald int gap_request_connection_parameter_update(hci_con_handle_t con_handle, uint16_t conn_interval_min, 3497b68d7bc3SMatthias Ringwald uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 3498b68d7bc3SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 3499b68d7bc3SMatthias Ringwald if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3500b68d7bc3SMatthias Ringwald connection->le_conn_interval_min = conn_interval_min; 3501b68d7bc3SMatthias Ringwald connection->le_conn_interval_max = conn_interval_max; 3502b68d7bc3SMatthias Ringwald connection->le_conn_latency = conn_latency; 3503b68d7bc3SMatthias Ringwald connection->le_supervision_timeout = supervision_timeout; 3504b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_REQUEST; 3505b68d7bc3SMatthias Ringwald hci_run(); 3506b68d7bc3SMatthias Ringwald return 0; 3507b68d7bc3SMatthias Ringwald } 3508b68d7bc3SMatthias Ringwald 3509501f56b3SMatthias Ringwald static void gap_advertisments_changed(void){ 3510501f56b3SMatthias Ringwald // disable advertisements before updating adv, scan data, or adv params 3511501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_active){ 3512501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_DISABLE | LE_ADVERTISEMENT_TASKS_ENABLE; 3513501f56b3SMatthias Ringwald } 3514501f56b3SMatthias Ringwald hci_run(); 3515501f56b3SMatthias Ringwald } 3516501f56b3SMatthias Ringwald 3517b68d7bc3SMatthias Ringwald /** 351845c102fdSMatthias Ringwald * @brief Set Advertisement Data 351945c102fdSMatthias Ringwald * @param advertising_data_length 352045c102fdSMatthias Ringwald * @param advertising_data (max 31 octets) 352145c102fdSMatthias Ringwald * @note data is not copied, pointer has to stay valid 352245c102fdSMatthias Ringwald */ 352345c102fdSMatthias Ringwald void gap_advertisements_set_data(uint8_t advertising_data_length, uint8_t * advertising_data){ 352445c102fdSMatthias Ringwald hci_stack->le_advertisements_data_len = advertising_data_length; 352545c102fdSMatthias Ringwald hci_stack->le_advertisements_data = advertising_data; 3526501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 3527501f56b3SMatthias Ringwald gap_advertisments_changed(); 352845c102fdSMatthias Ringwald } 3529501f56b3SMatthias Ringwald 3530501f56b3SMatthias Ringwald /** 3531501f56b3SMatthias Ringwald * @brief Set Scan Response Data 3532501f56b3SMatthias Ringwald * @param advertising_data_length 3533501f56b3SMatthias Ringwald * @param advertising_data (max 31 octets) 3534501f56b3SMatthias Ringwald * @note data is not copied, pointer has to stay valid 3535501f56b3SMatthias Ringwald */ 3536501f56b3SMatthias Ringwald void gap_scan_response_set_data(uint8_t scan_response_data_length, uint8_t * scan_response_data){ 3537501f56b3SMatthias Ringwald hci_stack->le_scan_response_data_len = scan_response_data_length; 3538501f56b3SMatthias Ringwald hci_stack->le_scan_response_data = scan_response_data; 3539501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 3540501f56b3SMatthias Ringwald gap_advertisments_changed(); 354145c102fdSMatthias Ringwald } 354245c102fdSMatthias Ringwald 354345c102fdSMatthias Ringwald /** 354445c102fdSMatthias Ringwald * @brief Set Advertisement Parameters 354545c102fdSMatthias Ringwald * @param adv_int_min 354645c102fdSMatthias Ringwald * @param adv_int_max 354745c102fdSMatthias Ringwald * @param adv_type 354845c102fdSMatthias Ringwald * @param own_address_type 354945c102fdSMatthias Ringwald * @param direct_address_type 355045c102fdSMatthias Ringwald * @param direct_address 355145c102fdSMatthias Ringwald * @param channel_map 355245c102fdSMatthias Ringwald * @param filter_policy 355345c102fdSMatthias Ringwald * 355445c102fdSMatthias Ringwald * @note internal use. use gap_advertisements_set_params from gap_le.h instead. 355545c102fdSMatthias Ringwald */ 355645c102fdSMatthias Ringwald void hci_le_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type, 355745c102fdSMatthias Ringwald uint8_t own_address_type, uint8_t direct_address_typ, bd_addr_t direct_address, 355845c102fdSMatthias Ringwald uint8_t channel_map, uint8_t filter_policy) { 355945c102fdSMatthias Ringwald 356045c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_min = adv_int_min; 356145c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_max = adv_int_max; 356245c102fdSMatthias Ringwald hci_stack->le_advertisements_type = adv_type; 356345c102fdSMatthias Ringwald hci_stack->le_advertisements_own_address_type = own_address_type; 356445c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address_type = direct_address_typ; 356545c102fdSMatthias Ringwald hci_stack->le_advertisements_channel_map = channel_map; 356645c102fdSMatthias Ringwald hci_stack->le_advertisements_filter_policy = filter_policy; 356745c102fdSMatthias Ringwald memcpy(hci_stack->le_advertisements_direct_address, direct_address, 6); 356845c102fdSMatthias Ringwald 356945c102fdSMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 3570501f56b3SMatthias Ringwald gap_advertisments_changed(); 357145c102fdSMatthias Ringwald } 357245c102fdSMatthias Ringwald 357345c102fdSMatthias Ringwald /** 357445c102fdSMatthias Ringwald * @brief Enable/Disable Advertisements 357545c102fdSMatthias Ringwald * @param enabled 357645c102fdSMatthias Ringwald */ 357745c102fdSMatthias Ringwald void gap_advertisements_enable(int enabled){ 357845c102fdSMatthias Ringwald hci_stack->le_advertisements_enabled = enabled; 357945c102fdSMatthias Ringwald if (enabled && !hci_stack->le_advertisements_active){ 358045c102fdSMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_ENABLE; 358145c102fdSMatthias Ringwald } 358245c102fdSMatthias Ringwald if (!enabled && hci_stack->le_advertisements_active){ 358345c102fdSMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_DISABLE; 358445c102fdSMatthias Ringwald } 3585cfc59f1bSMatthias Ringwald hci_run(); 358645c102fdSMatthias Ringwald } 358745c102fdSMatthias Ringwald 3588*35454696SMatthias Ringwald #endif 358945c102fdSMatthias Ringwald 3590616edd56SMatthias Ringwald uint8_t gap_disconnect(hci_con_handle_t handle){ 35915917a5c5S[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 35925917a5c5S[email protected] if (!conn){ 35937851196eSmatthias.ringwald hci_emit_disconnection_complete(handle, 0); 3594616edd56SMatthias Ringwald return 0; 35955917a5c5S[email protected] } 35965917a5c5S[email protected] conn->state = SEND_DISCONNECT; 35975917a5c5S[email protected] hci_run(); 3598616edd56SMatthias Ringwald return 0; 35994f3229d8S[email protected] } 360004a6ef8cSmatthias.ringwald 3601a1bf5ae7SMatthias Ringwald /** 3602a1bf5ae7SMatthias Ringwald * @brief Get connection type 3603a1bf5ae7SMatthias Ringwald * @param con_handle 3604a1bf5ae7SMatthias Ringwald * @result connection_type 3605a1bf5ae7SMatthias Ringwald */ 3606a1bf5ae7SMatthias Ringwald gap_connection_type_t gap_get_connection_type(hci_con_handle_t connection_handle){ 3607a1bf5ae7SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(connection_handle); 3608a1bf5ae7SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 3609a1bf5ae7SMatthias Ringwald switch (conn->address_type){ 3610a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 3611a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 3612a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_LE; 3613a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_SCO: 3614a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_SCO; 3615a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_CLASSIC: 3616a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_ACL; 3617a1bf5ae7SMatthias Ringwald default: 3618a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_INVALID; 3619a1bf5ae7SMatthias Ringwald } 3620a1bf5ae7SMatthias Ringwald } 3621a1bf5ae7SMatthias Ringwald 3622a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 36234f551432SMatthias Ringwald 3624d23838ecSMatthias Ringwald /** 3625ac9c45e0SMatthias Ringwald * @brief Auto Connection Establishment - Start Connecting to device 3626ac9c45e0SMatthias Ringwald * @param address_typ 3627ac9c45e0SMatthias Ringwald * @param address 3628ac9c45e0SMatthias Ringwald * @returns 0 if ok 3629ac9c45e0SMatthias Ringwald */ 36304f551432SMatthias Ringwald int gap_auto_connection_start(bd_addr_type_t address_type, bd_addr_t address){ 3631e83201bcSMatthias Ringwald // check capacity 3632665d90f2SMatthias Ringwald int num_entries = btstack_linked_list_count(&hci_stack->le_whitelist); 363391915b0bSMatthias Ringwald if (num_entries >= hci_stack->le_whitelist_capacity) return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED; 3634e83201bcSMatthias Ringwald whitelist_entry_t * entry = btstack_memory_whitelist_entry_get(); 3635e83201bcSMatthias Ringwald if (!entry) return BTSTACK_MEMORY_ALLOC_FAILED; 3636e83201bcSMatthias Ringwald entry->address_type = address_type; 3637e83201bcSMatthias Ringwald memcpy(entry->address, address, 6); 3638e83201bcSMatthias Ringwald entry->state = LE_WHITELIST_ADD_TO_CONTROLLER; 3639665d90f2SMatthias Ringwald btstack_linked_list_add(&hci_stack->le_whitelist, (btstack_linked_item_t*) entry); 3640e83201bcSMatthias Ringwald hci_run(); 3641e83201bcSMatthias Ringwald return 0; 3642ac9c45e0SMatthias Ringwald } 3643ac9c45e0SMatthias Ringwald 364442ff5ba1SMatthias Ringwald static void hci_remove_from_whitelist(bd_addr_type_t address_type, bd_addr_t address){ 3645665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 3646665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 3647665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 3648665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it); 3649e83201bcSMatthias Ringwald if (entry->address_type != address_type) continue; 3650e83201bcSMatthias Ringwald if (memcmp(entry->address, address, 6) != 0) continue; 3651e83201bcSMatthias Ringwald if (entry->state & LE_WHITELIST_ON_CONTROLLER){ 3652e83201bcSMatthias Ringwald // remove from controller if already present 3653e83201bcSMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER; 3654e83201bcSMatthias Ringwald continue; 3655e83201bcSMatthias Ringwald } 3656e83201bcSMatthias Ringwald // direclty remove entry from whitelist 3657665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 3658e83201bcSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 3659e83201bcSMatthias Ringwald } 366042ff5ba1SMatthias Ringwald } 366142ff5ba1SMatthias Ringwald 366242ff5ba1SMatthias Ringwald /** 366342ff5ba1SMatthias Ringwald * @brief Auto Connection Establishment - Stop Connecting to device 366442ff5ba1SMatthias Ringwald * @param address_typ 366542ff5ba1SMatthias Ringwald * @param address 366642ff5ba1SMatthias Ringwald * @returns 0 if ok 366742ff5ba1SMatthias Ringwald */ 366842ff5ba1SMatthias Ringwald int gap_auto_connection_stop(bd_addr_type_t address_type, bd_addr_t address){ 366942ff5ba1SMatthias Ringwald hci_remove_from_whitelist(address_type, address); 3670e83201bcSMatthias Ringwald hci_run(); 3671e83201bcSMatthias Ringwald return 0; 3672ac9c45e0SMatthias Ringwald } 3673ac9c45e0SMatthias Ringwald 3674ac9c45e0SMatthias Ringwald /** 3675ac9c45e0SMatthias Ringwald * @brief Auto Connection Establishment - Stop everything 3676ac9c45e0SMatthias Ringwald * @note Convenience function to stop all active auto connection attempts 3677ac9c45e0SMatthias Ringwald */ 3678ac9c45e0SMatthias Ringwald void gap_auto_connection_stop_all(void){ 3679665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 3680665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 3681665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 3682665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it); 3683e83201bcSMatthias Ringwald if (entry->state & LE_WHITELIST_ON_CONTROLLER){ 3684e83201bcSMatthias Ringwald // remove from controller if already present 3685e83201bcSMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER; 3686e83201bcSMatthias Ringwald continue; 3687e83201bcSMatthias Ringwald } 368891915b0bSMatthias Ringwald // directly remove entry from whitelist 3689665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 3690e83201bcSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 3691e83201bcSMatthias Ringwald } 3692e83201bcSMatthias Ringwald hci_run(); 3693ac9c45e0SMatthias Ringwald } 3694ac9c45e0SMatthias Ringwald 36954f551432SMatthias Ringwald #endif 36964f551432SMatthias Ringwald 3697*35454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 3698ac9c45e0SMatthias Ringwald /** 3699ff00ed1cSMatthias Ringwald * @brief Set Extended Inquiry Response data 3700ff00ed1cSMatthias Ringwald * @param eir_data size 240 bytes, is not copied make sure memory is accessible during stack startup 3701ff00ed1cSMatthias Ringwald * @note has to be done before stack starts up 3702ff00ed1cSMatthias Ringwald */ 3703ff00ed1cSMatthias Ringwald void gap_set_extended_inquiry_response(const uint8_t * data){ 3704ff00ed1cSMatthias Ringwald hci_stack->eir_data = data; 3705ff00ed1cSMatthias Ringwald } 3706ff00ed1cSMatthias Ringwald 3707ff00ed1cSMatthias Ringwald /** 3708f6858d14SMatthias Ringwald * @brief Set inquiry mode: standard, with RSSI, with RSSI + Extended Inquiry Results. Has to be called before power on. 3709f6858d14SMatthias Ringwald * @param inquriy_mode see bluetooth_defines.h 3710f6858d14SMatthias Ringwald */ 3711f6858d14SMatthias Ringwald void hci_set_inquiry_mode(inquiry_mode_t mode){ 3712f6858d14SMatthias Ringwald hci_stack->inquiry_mode = mode; 3713f6858d14SMatthias Ringwald } 3714f6858d14SMatthias Ringwald 3715f6858d14SMatthias Ringwald /** 3716d950d659SMatthias Ringwald * @brief Configure Voice Setting for use with SCO data in HSP/HFP 3717d950d659SMatthias Ringwald */ 3718d950d659SMatthias Ringwald void hci_set_sco_voice_setting(uint16_t voice_setting){ 3719d950d659SMatthias Ringwald hci_stack->sco_voice_setting = voice_setting; 3720d950d659SMatthias Ringwald } 3721d950d659SMatthias Ringwald 3722d950d659SMatthias Ringwald /** 3723d950d659SMatthias Ringwald * @brief Get SCO Voice Setting 3724d950d659SMatthias Ringwald * @return current voice setting 3725d950d659SMatthias Ringwald */ 37260cb5b971SMatthias Ringwald uint16_t hci_get_sco_voice_setting(void){ 3727d950d659SMatthias Ringwald return hci_stack->sco_voice_setting; 3728d950d659SMatthias Ringwald } 3729d950d659SMatthias Ringwald 3730b3aad8daSMatthias Ringwald /** @brief Get SCO packet length for current SCO Voice setting 3731b3aad8daSMatthias Ringwald * @note Using SCO packets of the exact length is required for USB transfer 3732b3aad8daSMatthias Ringwald * @return Length of SCO packets in bytes (not audio frames) 3733b3aad8daSMatthias Ringwald */ 3734b3aad8daSMatthias Ringwald int hci_get_sco_packet_length(void){ 3735b3aad8daSMatthias Ringwald // see Core Spec for H2 USB Transfer. 3736b3aad8daSMatthias Ringwald if (hci_stack->sco_voice_setting & 0x0020) return 51; 3737b3aad8daSMatthias Ringwald return 27; 3738b3aad8daSMatthias Ringwald } 3739*35454696SMatthias Ringwald #endif 3740b3aad8daSMatthias Ringwald 3741d950d659SMatthias Ringwald /** 3742d23838ecSMatthias Ringwald * @brief Set callback for Bluetooth Hardware Error 3743d23838ecSMatthias Ringwald */ 3744c2e1fa60SMatthias Ringwald void hci_set_hardware_error_callback(void (*fn)(uint8_t error)){ 3745d23838ecSMatthias Ringwald hci_stack->hardware_error_callback = fn; 3746d23838ecSMatthias Ringwald } 3747d23838ecSMatthias Ringwald 3748a6ddbcb9SMatthias Ringwald /** 3749a6ddbcb9SMatthias Ringwald * @brief Set callback for local information from Bluetooth controller right after HCI Reset 3750a6ddbcb9SMatthias Ringwald * @note Can be used to select chipset driver dynamically during startup 3751a6ddbcb9SMatthias Ringwald */ 3752a6ddbcb9SMatthias Ringwald void hci_set_local_version_information_callback(void (*fn)(uint8_t * local_version_information)){ 3753a6ddbcb9SMatthias Ringwald hci_stack->local_version_information_callback = fn; 3754a6ddbcb9SMatthias Ringwald } 3755a6ddbcb9SMatthias Ringwald 375671de195eSMatthias Ringwald void hci_disconnect_all(void){ 3757665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 3758665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 3759665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 3760665d90f2SMatthias Ringwald hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 376104a6ef8cSmatthias.ringwald if (con->state == SENT_DISCONNECT) continue; 376204a6ef8cSmatthias.ringwald con->state = SEND_DISCONNECT; 376304a6ef8cSmatthias.ringwald } 3764d31fba26S[email protected] hci_run(); 376504a6ef8cSmatthias.ringwald } 3766